wasm-micro-runtime
wasm-micro-runtime copied to clipboard
Allow calling pure C functions directly
Added use_wrapper to structure NativeSymbol and corresponding methods to enable linking to pure C functions, without prepending wasm_exec_env_t as first argument
example:
int square(int a) {
return a * a;
}
static NativeSymbol native_symbols[] = {
{ "square", (void *) square, "(i)i", NULL, /*use_wrapper*/ false}
};
Added use_wrapper to structure NativeSymbol and corresponding methods to enable linking to pure C functions, without prepending wasm_exec_env_t as first argument
example:
int square(int a) { return a * a; } static NativeSymbol native_symbols[] = { { "square", (void *) square, "(i)i", NULL, /*use_wrapper*/ false} };
Hi, adding the flag to control whether to pass the exec_env argument to native function or not may introduce more footprint and complexity for runtime, could you please simply add the wrapper function as normal, and call the pure C function in the wrapper function, e.g.:
int square(int a) {
return a * a;
}
int square_wrapper(wasm_exec_env_t exec_env, int a) {
return square(a);
}
Thanks.
Well this feature is only 8 lines of code, but if you don't want it for wider adoption that is OK.
I change the title in case others are looking for this functionality too.
There are situations in which wasm-micro-runtime is used as a library and creating ad hoc wrappers is not an option.
If there are no additional checks, maybe it's easier to encapsulate your function with a macro.
It is invalid to call pure C function directly since the wrapper function must have an extra argument exec_env
which is passed from runtime when calling it.