Enable multi-module support for wasm-c-api (#426)

it is allowed that all imported functions and globals can be
linked by multi-module feature automatically or by wasm-c-api manually
This commit is contained in:
lum1n0us
2020-10-16 17:43:57 +08:00
committed by GitHub
parent f1fe5d7872
commit 4787b150b8
26 changed files with 550 additions and 185 deletions

View File

@ -796,10 +796,11 @@ load_function_import(const WASMModule *parent_module, WASMModule *sub_module,
declare_func_type = parent_module->types[declare_type_index];
is_built_in_module = wasm_runtime_is_built_in_module(sub_module_name);
if (is_built_in_module) {
LOG_DEBUG("%s is a function of a built-in module %s",
function_name, sub_module_name);
if (wasm_runtime_is_host_module(sub_module_name)) {
/* do nothing, wait for injecting host created fuctions */
}
else if ((is_built_in_module =
wasm_runtime_is_built_in_module(sub_module_name))) {
/* check built-in modules */
linked_func = wasm_native_resolve_symbol(sub_module_name,
function_name,
@ -810,8 +811,6 @@ load_function_import(const WASMModule *parent_module, WASMModule *sub_module,
}
#if WASM_ENABLE_MULTI_MODULE != 0
else {
LOG_DEBUG("%s is a function of a sub-module %s",
function_name, sub_module_name);
linked_func = wasm_loader_resolve_function(sub_module_name,
function_name,
declare_func_type,
@ -820,19 +819,6 @@ load_function_import(const WASMModule *parent_module, WASMModule *sub_module,
}
#endif
if (!linked_func) {
#if WASM_ENABLE_SPEC_TEST != 0
set_error_buf(error_buf, error_buf_size,
"unknown import or incompatible import type");
return false;
#else
#if WASM_ENABLE_WAMR_COMPILER == 0
LOG_WARNING("warning: fail to link import function (%s, %s)",
sub_module_name, function_name);
#endif
#endif
}
function->module_name = sub_module_name;
function->field_name = function_name;
function->func_type = declare_func_type;
@ -1096,8 +1082,6 @@ load_global_import(const WASMModule *parent_module,
const uint8 *p = *p_buf, *p_end = buf_end;
uint8 declare_type = 0;
uint8 declare_mutable = 0;
bool is_mutable = false;
bool ret = false;
CHECK_BUF(p, p_end, 2);
declare_type = read_uint8(p);
@ -1109,50 +1093,33 @@ load_global_import(const WASMModule *parent_module,
return false;
}
is_mutable = declare_mutable & 1 ? true : false;
#if WASM_ENABLE_LIBC_BUILTIN != 0
ret = wasm_runtime_is_built_in_module(sub_module_name);
if (ret) {
/* check built-in modules */
ret = wasm_native_lookup_libc_builtin_global(sub_module_name,
global_name, global);
if (ret) {
LOG_DEBUG("(%s, %s) is a global of a built-in module",
sub_module_name, global_name);
}
if (wasm_runtime_is_host_module(sub_module_name)) {
/* do nothing, let host injects the symbol */
}
else if (wasm_runtime_is_built_in_module(sub_module_name)) {
/* check built-in modules */
global->is_linked = wasm_native_lookup_libc_builtin_global(
sub_module_name, global_name, global);
}
#endif /* WASM_ENABLE_LIBC_BUILTIN */
#if WASM_ENABLE_MULTI_MODULE != 0
if (!ret) {
else {
/* check sub modules */
WASMGlobal *linked_global =
wasm_loader_resolve_global(sub_module_name, global_name,
declare_type, declare_mutable,
error_buf, error_buf_size);
if (linked_global) {
LOG_DEBUG("(%s, %s) is a global of external module",
sub_module_name, global_name);
global->import_module = sub_module;
global->import_global_linked = linked_global;
ret = true;
global->is_linked = true;
}
}
#endif
if (!ret) {
#if WASM_ENABLE_SPEC_TEST != 0
set_error_buf(error_buf, error_buf_size,
"unknown import or incompatible import type");
return false;
#endif
}
global->module_name = sub_module_name;
global->field_name = global_name;
global->type = declare_type;
global->is_mutable = is_mutable;
global->is_mutable = (declare_mutable == 1);
return true;
fail:
return false;
@ -1363,8 +1330,7 @@ load_depended_module(const WASMModule *parent_module,
ret = reader(sub_module_name, &buffer, &buffer_size);
if (!ret) {
LOG_DEBUG("read the file of %s failed", sub_module_name);
set_error_buf_v(error_buf, error_buf_size,
"failed to read module file of %s",
set_error_buf_v(error_buf, error_buf_size, "unknown import",
sub_module_name);
goto DELETE_FROM_LOADING_LIST;
}
@ -1568,7 +1534,8 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
LOG_DEBUG("import #%d: (%s, %s)", i, sub_module_name, field_name);
#if WASM_ENABLE_MULTI_MODULE != 0
/* assume built-in modules have been loaded */
if (!wasm_runtime_is_built_in_module(sub_module_name)) {
if (!wasm_runtime_is_host_module(sub_module_name)
&& !wasm_runtime_is_built_in_module(sub_module_name)) {
LOG_DEBUG("%s is an exported field of a %s", field_name,
sub_module_name);
/*