From: Brendan Hansen Date: Fri, 5 May 2023 03:12:36 +0000 (-0500) Subject: changed: undefined function imports X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=66fb5b22f7b919fc4f9ee1db251a653ac40a4fa1;p=onyx.git changed: undefined function imports --- diff --git a/CHANGELOG b/CHANGELOG index ce9741bd..c796711e 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -9,6 +9,8 @@ Additions: Removals: Changes: +* Undefined function imports in `onyx run` no longer cause an immediate error. + - Instead, an error is produced when the function is called. Bugfixes: * Fixed missing `use core` in `optional.onyx`. diff --git a/compiler/src/wasm_runtime.c b/compiler/src/wasm_runtime.c index 75e9b69c..a790be00 100644 --- a/compiler/src/wasm_runtime.c +++ b/compiler/src/wasm_runtime.c @@ -341,6 +341,12 @@ static void cleanup_wasm_objects() { if (wasm_engine) wasm_engine_delete(wasm_engine); } +static wasm_trap_t *__error_on_call(void *env, const wasm_val_vec_t *args, wasm_val_vec_t *results) { + printf("Attempted to invoke imported function with no defintion, '%s'\n", (char *) env); + exit(1); + return NULL; +} + // // This could be cleaned up a bit, as this function directly modifies various global variables. // Those being wasm_memory and wasm_imports. @@ -421,7 +427,20 @@ static b32 link_wasm_imports( } } - goto bad_import; + // + // If no matching function is not found, do not produce an error. Instead + // make an empty function with the correct type that will produce an error + // and crash when it is called. This enables safely having undefined references + // in the program, so long as they are not called. It also enables binding + // generators like `jsbindgen`, as during the generation, the imported functions + // will not be called. + { + char *funcname = bh_aprintf(bh_heap_allocator(), "%b.%b", module_name->data, module_name->size, import_name->data, import_name->size); + + wasm_functype_t *wasm_functype = wasm_externtype_as_functype(wasm_importtype_type(module_imports.data[i])); + wasm_func_t* wasm_func = wasm_func_new_with_env(wasm_store, wasm_functype, __error_on_call, funcname, NULL); + import = wasm_func_as_extern(wasm_func); + } import_found: wasm_imports.data[i] = import;