Implement register/call native API with raw (unextracted) arguments (#222)

This commit is contained in:
wenyongh
2020-04-01 12:52:08 +08:00
committed by GitHub
parent d9890d2ccb
commit c1a0e6d877
17 changed files with 344 additions and 41 deletions

View File

@ -16,15 +16,23 @@ typedef struct NativeSymbol {
const char *symbol;
void *func_ptr;
const char *signature;
/* attachment which can be retrieved in native API by
calling wasm_runtime_get_function_attachment(exec_env) */
void *attachment;
} NativeSymbol;
#define EXPORT_WASM_API(symbol) {#symbol, (void*)symbol, NULL}
#define EXPORT_WASM_API2(symbol) {#symbol, (void*)symbol##_wrapper, NULL}
#define EXPORT_WASM_API(symbol) {#symbol, (void*)symbol, NULL, NULL}
#define EXPORT_WASM_API2(symbol) {#symbol, (void*)symbol##_wrapper, NULL, NULL}
#define EXPORT_WASM_API_WITH_SIG(symbol, signature) \
{#symbol, (void*)symbol, signature}
{#symbol, (void*)symbol, signature, NULL}
#define EXPORT_WASM_API_WITH_SIG2(symbol, signature) \
{#symbol, (void*)symbol##_wrapper, signature}
{#symbol, (void*)symbol##_wrapper, signature, NULL}
#define EXPORT_WASM_API_WITH_ATT(symbol, signature, attachment) \
{#symbol, (void*)symbol, signature, attachment}
#define EXPORT_WASM_API_WITH_ATT2(symbol, signature, attachment) \
{#symbol, (void*)symbol##_wrapper, signature, attachment}
/**
* Get the exported APIs of base lib

View File

@ -36,6 +36,11 @@ extern "C" {
#define module_free(offset) \
wasm_runtime_module_free(module_inst, offset)
#define native_raw_return_type(type, args) type *raw_ret = (type*)(args)
#define native_raw_get_arg(type, name, args) type name = *((type*)(args++))
#define native_raw_set_return(val) *raw_ret = (val)
/* Uninstantiated WASM module loaded from WASM binary file
@ -561,6 +566,49 @@ bool wasm_runtime_register_natives(const char *module_name,
NativeSymbol *native_symbols,
uint32_t n_native_symbols);
/**
* Register native functions with same module name, similar to
* wasm_runtime_register_natives, the difference is that runtime passes raw
* arguments to native API, which means that the native API should be defined as:
* void foo(wasm_exec_env_t exec_env, uint64 *args);
* and native API should extract arguments one by one from args array with macro
* native_raw_get_arg
* and write the return value back to args[0] with macro
* native_raw_return_type and native_raw_set_return
*/
bool wasm_runtime_register_natives_raw(const char *module_name,
NativeSymbol *native_symbols,
uint32_t n_native_symbols);
/**
* Get attachment of native function from execution environment
*
* @param exec_env the execution environment to retrieve
*
* @return the attachment of native function
*/
void *
wasm_runtime_get_function_attachment(wasm_exec_env_t exec_env);
/**
* Set user data to execution environment.
*
* @param exec_env the execution environment
* @param user_data the user data to be set
*/
void
wasm_runtime_set_user_data(wasm_exec_env_t exec_env,
void *user_data);
/**
* Get the user data within execution environment.
*
* @param exec_env the execution environment
*
* @return the user data (NULL if not set yet)
*/
void *
wasm_runtime_get_user_data(wasm_exec_env_t exec_env);
#ifdef __cplusplus
}
#endif