wasm-micro-runtime icon indicating copy to clipboard operation
wasm-micro-runtime copied to clipboard

Allow calling pure C functions directly

Open pannous opened this issue 3 years ago • 3 comments

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}
};

pannous avatar May 26 '21 11:05 pannous

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.

wenyongh avatar May 27 '21 01:05 wenyongh

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.

pannous avatar May 27 '21 07:05 pannous

There are situations in which wasm-micro-runtime is used as a library and creating ad hoc wrappers is not an option.

pannous avatar May 27 '21 07:05 pannous

If there are no additional checks, maybe it's easier to encapsulate your function with a macro.

tonibofarull avatar Feb 13 '23 22:02 tonibofarull

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.

wenyongh avatar Mar 28 '23 06:03 wenyongh