// https://github.com/bytecodealliance/wasm-micro-runtime/blob/WAMR-2.4.4/doc/embed_wamr.md #include #include #include "bh_platform.h" #include "bh_read_file.h" #include "wasm_export.h" #include "../__WASM_ARRAY_FILE__" #define STACK_SIZE (8 * 1024) #define HEAP_SIZE (8 * 1024) // TODO: Set this up so the lsp actually finds the includes... int main(int argc, char **argv) { // uint8 *buffer; // uint32 size; char error_buf[128]; wasm_module_t module; wasm_module_inst_t module_inst; wasm_function_inst_t func; wasm_exec_env_t exec_env; uint32 stack_size = 8 * 1024; uint32 heap_size = 8 * 1024; /* initialize the wasm runtime by default configurations */ // wasm_runtime_init(); printf("Initializing WAMR runtime\n"); RuntimeInitArgs init_args; memset(&init_args, 0, sizeof(init_args)); init_args.mem_alloc_type = Alloc_With_System_Allocator; if (!wasm_runtime_full_init(&init_args)) { printf("Failed to initialize WAMR runtime\n"); return 1; } /* read WASM file into a memory buffer */ // printf("Instantiating module from file '%s'\n", argv[1]); // buffer = bh_read_file_to_buffer(argv[1], &size); /* add line below if we want to export native functions to WASM app */ // wasm_runtime_register_natives(...); /* parse the WASM file from buffer and create a WASM module */ // module = wasm_runtime_load(buffer, size, error_buf, sizeof(error_buf)); printf("Instantiating module from buffer\n"); module = wasm_runtime_load(__WASM_ARRAY__, __WASM_ARRAY_LEN__, error_buf, sizeof(error_buf)); /* create an instance of the WASM module (WASM linear memory is ready) */ module_inst = wasm_runtime_instantiate(module, stack_size, heap_size, error_buf, sizeof(error_buf)); /* lookup a WASM function by its name The function signature can NULL here */ // func = wasm_runtime_lookup_function(module_inst, "wasm_module"); /* creat an execution environment to execute the WASM functions */ // exec_env = wasm_runtime_create_exec_env(module_inst, stack_size); // TODO: Also when using execute_main? /* arguments are always transferred in 32-bit element */ uint32 args[1]; args[0] = 8; args[1] = 16; // TODO: The arguments have to be passed differently, probably using // wasm_runtime_set_wasi_args? /* call the WASM function */ printf("Calling WASM function\n"); wasm_application_execute_main(module_inst, 2, args); // if (wasm_runtime_call_wasm(exec_env, func, 1, args)) { // /* the return value is stored in argv[0] */ // printf("wasm function return: %d\n", args[0]); // } else { // /* exception is thrown if call fails */ // printf("%s\n", wasm_runtime_get_exception(module_inst)); // } // wasm_runtime_destroy_exec_env(exec_env); wasm_runtime_deinstantiate(module_inst); wasm_runtime_unload(module); wasm_runtime_destroy(); return 0; }