add simple host program that loads a wasm module using the wamr runtime
This commit is contained in:
94
wasm-base/examples/embed/host.c
Normal file
94
wasm-base/examples/embed/host.c
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
// https://github.com/bytecodealliance/wasm-micro-runtime/blob/WAMR-2.4.4/doc/embed_wamr.md
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "bh_platform.h"
|
||||||
|
#include "bh_read_file.h"
|
||||||
|
#include "wasm_export.h"
|
||||||
|
|
||||||
|
#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) {
|
||||||
|
if (argc != 2) {
|
||||||
|
printf("Missing .wasm or .aot file argument!\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!strstr(argv[1], ".wasm") && !strstr(argv[1], ".aot")) {
|
||||||
|
printf("Only .wasm or .aot files are supported!\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8 *buffer;
|
||||||
|
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 size;
|
||||||
|
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));
|
||||||
|
|
||||||
|
/* 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;
|
||||||
|
}
|
||||||
@ -7,6 +7,7 @@ int main(int argc, char **argv) {
|
|||||||
char *buf;
|
char *buf;
|
||||||
|
|
||||||
printf("Hello world!\n");
|
printf("Hello world!\n");
|
||||||
|
printf("Got %d args!\n", argc);
|
||||||
|
|
||||||
buf = malloc(1024);
|
buf = malloc(1024);
|
||||||
if (!buf) {
|
if (!buf) {
|
||||||
|
|||||||
Reference in New Issue
Block a user