test_hello2.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. /*
  6. * This example basically does the same thing as test_hello.c,
  7. * using wasm_export.h API.
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "wasm_export.h"
  12. static int
  13. test_hello2_wrapper(wasm_exec_env_t exec_env, uint32_t nameaddr,
  14. uint32_t resultaddr, uint32_t resultlen)
  15. {
  16. /*
  17. * Perform wasm_runtime_malloc to check if the runtime has been
  18. * initialized as expected.
  19. * This would fail with "memory hasn't been initialize" error
  20. * unless we are not sharing a runtime with the loader app. (iwasm)
  21. */
  22. void *p = wasm_runtime_malloc(1);
  23. if (p == NULL) {
  24. return -1;
  25. }
  26. wasm_runtime_free(p);
  27. wasm_module_inst_t inst = wasm_runtime_get_module_inst(exec_env);
  28. if (!wasm_runtime_validate_app_str_addr(inst, nameaddr)
  29. || !wasm_runtime_validate_app_addr(inst, resultaddr, resultlen)) {
  30. return -1;
  31. }
  32. const char *name = wasm_runtime_addr_app_to_native(inst, nameaddr);
  33. char *result = wasm_runtime_addr_app_to_native(inst, resultaddr);
  34. return snprintf(result, resultlen,
  35. "Hello, %s. This is %s! Your wasm_module_inst_t is %p.\n",
  36. name, __func__, inst);
  37. }
  38. /* clang-format off */
  39. #define REG_NATIVE_FUNC(func_name, signature) \
  40. { #func_name, func_name##_wrapper, signature, NULL }
  41. static NativeSymbol native_symbols[] = {
  42. REG_NATIVE_FUNC(test_hello2, "(iii)i")
  43. };
  44. /* clang-format on */
  45. uint32_t
  46. get_native_lib(char **p_module_name, NativeSymbol **p_native_symbols)
  47. {
  48. *p_module_name = "env";
  49. *p_native_symbols = native_symbols;
  50. return sizeof(native_symbols) / sizeof(NativeSymbol);
  51. }