Skip to content

Commit cba4c78

Browse files
authored
Enable call wasm-c-api native func directly from interpreter (#656)
And update loader error messages for latest spec cases, fix aot compiler build error based on latest LLVM code base.
1 parent c6783ef commit cba4c78

File tree

13 files changed

+249
-364
lines changed

13 files changed

+249
-364
lines changed

core/iwasm/aot/aot_runtime.c

Lines changed: 3 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#if WASM_ENABLE_THREAD_MGR != 0
1414
#include "../libraries/thread-mgr/thread_manager.h"
1515
#endif
16-
#include "../common/wasm_c_api_internal.h"
1716

1817
static void
1918
set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
@@ -2204,140 +2203,6 @@ aot_is_wasm_type_equal(AOTModuleInstance *module_inst,
22042203
return wasm_type_equal(type1, type2);
22052204
}
22062205

2207-
static inline bool
2208-
argv_to_params(wasm_val_t *out_params, const uint32 *argv,
2209-
AOTFuncType *func_type)
2210-
{
2211-
wasm_val_t *param = out_params;
2212-
uint32 i = 0, *u32;
2213-
2214-
for (i = 0; i < func_type->param_count; i++, param++) {
2215-
switch (func_type->types[i]) {
2216-
case VALUE_TYPE_I32:
2217-
param->kind = WASM_I32;
2218-
param->of.i32 = *argv++;
2219-
break;
2220-
case VALUE_TYPE_I64:
2221-
param->kind = WASM_I64;
2222-
u32 = (uint32*)&param->of.i64;
2223-
u32[0] = *argv++;
2224-
u32[1] = *argv++;
2225-
break;
2226-
case VALUE_TYPE_F32:
2227-
param->kind = WASM_F32;
2228-
param->of.f32 = *(float32 *)argv++;
2229-
break;
2230-
case VALUE_TYPE_F64:
2231-
param->kind = WASM_F64;
2232-
u32 = (uint32*)&param->of.i64;
2233-
u32[0] = *argv++;
2234-
u32[1] = *argv++;
2235-
break;
2236-
default:
2237-
return false;
2238-
}
2239-
}
2240-
2241-
return true;
2242-
}
2243-
2244-
static inline bool
2245-
results_to_argv(uint32 *out_argv, const wasm_val_t *results,
2246-
AOTFuncType *func_type)
2247-
{
2248-
const wasm_val_t *result = results;
2249-
uint32 *argv = out_argv, *u32, i;
2250-
uint8 *result_types = func_type->types + func_type->param_count;
2251-
2252-
for (i = 0; i < func_type->result_count; i++, result++) {
2253-
switch (result_types[i]) {
2254-
case VALUE_TYPE_I32:
2255-
case VALUE_TYPE_F32:
2256-
*(int32*)argv++ = result->of.i32;
2257-
break;
2258-
case VALUE_TYPE_I64:
2259-
case VALUE_TYPE_F64:
2260-
u32 = (uint32*)&result->of.i64;
2261-
*argv++ = u32[0];
2262-
*argv++ = u32[1];
2263-
break;
2264-
default:
2265-
return false;
2266-
}
2267-
}
2268-
2269-
return true;
2270-
}
2271-
2272-
static inline bool
2273-
invoke_wasm_c_api_native(AOTModuleInstance *module_inst, void *func_ptr,
2274-
AOTFuncType *func_type, uint32 argc, uint32 *argv,
2275-
bool with_env, void *wasm_c_api_env)
2276-
{
2277-
wasm_val_t params_buf[16], results_buf[4];
2278-
wasm_val_t *params = params_buf, *results = results_buf;
2279-
wasm_trap_t *trap = NULL;
2280-
bool ret = false;
2281-
char fmt[16];
2282-
2283-
if (func_type->param_count > 16
2284-
&& !(params = wasm_runtime_malloc(sizeof(wasm_val_t)
2285-
* func_type->param_count))) {
2286-
aot_set_exception_with_id(module_inst, EXCE_OUT_OF_MEMORY);
2287-
return false;
2288-
}
2289-
2290-
if (!argv_to_params(params, argv, func_type)) {
2291-
aot_set_exception(module_inst, "unsupported param type");
2292-
goto fail;
2293-
}
2294-
2295-
if (!with_env) {
2296-
wasm_func_callback_t callback = (wasm_func_callback_t)func_ptr;
2297-
trap = callback(params, results);
2298-
}
2299-
else {
2300-
wasm_func_callback_with_env_t callback =
2301-
(wasm_func_callback_with_env_t)func_ptr;
2302-
trap = callback(wasm_c_api_env, params, results);
2303-
}
2304-
if (trap) {
2305-
if (trap->message->data) {
2306-
snprintf(fmt, sizeof(fmt), "%%.%us", (uint32)trap->message->size);
2307-
snprintf(module_inst->cur_exception,
2308-
sizeof(module_inst->cur_exception),
2309-
fmt, trap->message->data);
2310-
}
2311-
else {
2312-
aot_set_exception(module_inst,
2313-
"native function throw unknown exception");
2314-
}
2315-
wasm_trap_delete(trap);
2316-
goto fail;
2317-
}
2318-
2319-
if (func_type->result_count > 4
2320-
&& !(results = wasm_runtime_malloc(sizeof(wasm_val_t)
2321-
* func_type->result_count))) {
2322-
aot_set_exception_with_id(module_inst, EXCE_OUT_OF_MEMORY);
2323-
goto fail;
2324-
}
2325-
2326-
if (!results_to_argv(argv, results, func_type)) {
2327-
aot_set_exception(module_inst, "unsupported result type");
2328-
goto fail;
2329-
}
2330-
2331-
ret = true;
2332-
2333-
fail:
2334-
if (params != params_buf)
2335-
wasm_runtime_free(params);
2336-
if (results != results_buf)
2337-
wasm_runtime_free(results);
2338-
return ret;
2339-
}
2340-
23412206
bool
23422207
aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx,
23432208
uint32 argc, uint32 *argv)
@@ -2368,10 +2233,9 @@ aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx,
23682233

23692234
attachment = import_func->attachment;
23702235
if (import_func->call_conv_wasm_c_api) {
2371-
return invoke_wasm_c_api_native(module_inst, func_ptr,
2372-
func_type, argc, argv,
2373-
import_func->wasm_c_api_with_env,
2374-
attachment);
2236+
return wasm_runtime_invoke_c_api_native(
2237+
(WASMModuleInstanceCommon *)module_inst, func_ptr, func_type, argc,
2238+
argv, import_func->wasm_c_api_with_env, attachment);
23752239
}
23762240
else if (!import_func->call_conv_raw) {
23772241
signature = import_func->signature;

0 commit comments

Comments
 (0)