Fixes and cleanup for import/export apis

Should address suggestions from @lum1n0us.
This commit is contained in:
Benbuck Nason 2024-04-18 11:01:34 -07:00
parent 859ead7185
commit 6ec840d3e3
2 changed files with 180 additions and 64 deletions

View File

@ -3597,101 +3597,219 @@ static union {
#define is_little_endian() (__ue.b == 1) /* NOLINT */
uint32_t
wasm_runtime_import_count(const wasm_module_t module)
int32_t
wasm_runtime_get_import_count(const wasm_module_t module)
{
if (!module) {
return 0;
bh_assert(0);
return -1;
}
const WASMModule *wasm_module = (const WASMModule *)module;
#if WASM_ENABLE_AOT != 0
if (module->module_type == Wasm_Module_AoT) {
const AOTModule *aot_module = (const AOTModule *)module;
return (int32_t)(aot_module->import_func_count
+ aot_module->import_global_count
+ aot_module->import_table_count
+ aot_module->import_memory_count);
}
#endif
#if WASM_ENABLE_INTERP != 0
if (module->module_type == Wasm_Module_Bytecode) {
const WASMModule *wasm_module = (const WASMModule *)module;
return (int32_t)wasm_module->import_count;
}
#endif
return wasm_module->import_count;
return -1;
}
void
wasm_runtime_import_info(const wasm_module_t module, uint32_t import_index,
wasm_import_info_t *import_info)
wasm_runtime_get_import_type(const wasm_module_t module, int32_t import_index,
wasm_import_type *import_type)
{
if (!import_info) {
if (!import_type) {
bh_assert(0);
return;
}
memset(import_info, 0, sizeof(wasm_import_info_t));
memset(import_type, 0, sizeof(wasm_import_type));
if (!module) {
bh_assert(0);
return;
}
const WASMModule *wasm_module = (const WASMModule *)module;
if (import_index >= wasm_module->import_count) {
bh_assert(0);
return;
}
#if WASM_ENABLE_AOT != 0
if (module->module_type == Wasm_Module_AoT) {
const AOTModule *aot_module = (const AOTModule *)module;
import_info->module_name =
wasm_module->imports[import_index].u.names.module_name;
import_info->name = wasm_module->imports[import_index].u.names.field_name;
import_info->kind = wasm_module->imports[import_index].kind;
switch (import_info->kind) {
case WASM_IMPORT_EXPORT_KIND_FUNC:
import_info->linked =
wasm_module->imports[import_index].u.function.func_ptr_linked;
break;
case WASM_IMPORT_EXPORT_KIND_GLOBAL:
import_info->linked =
wasm_module->imports[import_index].u.global.is_linked;
break;
case WASM_IMPORT_EXPORT_KIND_TABLE:
import_info->linked = true; /* is this correct? */
break;
case WASM_IMPORT_EXPORT_KIND_MEMORY:
import_info->linked = true; /* is this correct? */
break;
default:
if (import_index < 0) {
bh_assert(0);
break;
return;
}
uint32_t func_index = (uint32_t)import_index;
if (func_index < aot_module->import_func_count) {
const AOTImportFunc *aot_import_func =
&aot_module->import_funcs[func_index];
import_type->module_name = aot_import_func->module_name;
import_type->name = aot_import_func->func_name;
import_type->kind = WASM_IMPORT_EXPORT_KIND_FUNC;
import_type->linked =
aot_import_func->func_ptr_linked ? true : false;
return;
}
uint32_t global_index = func_index - aot_module->import_func_count;
if (global_index < aot_module->import_global_count) {
const AOTImportGlobal *aot_import_global =
&aot_module->import_globals[global_index];
import_type->module_name = aot_import_global->module_name;
import_type->name = aot_import_global->global_name;
import_type->kind = WASM_IMPORT_EXPORT_KIND_GLOBAL;
import_type->linked = aot_import_global->is_linked;
return;
}
uint32_t table_index = global_index - aot_module->import_global_count;
if (table_index < aot_module->import_table_count) {
const AOTImportTable *aot_import_table =
&aot_module->import_tables[table_index];
import_type->module_name = aot_import_table->module_name;
import_type->name = aot_import_table->table_name;
import_type->kind = WASM_IMPORT_EXPORT_KIND_TABLE;
import_type->linked = false;
return;
}
uint32_t memory_index = table_index - aot_module->import_table_count;
if (memory_index < aot_module->import_memory_count) {
const AOTImportMemory *aot_import_memory =
&aot_module->import_memories[memory_index];
import_type->module_name = aot_import_memory->module_name;
import_type->name = aot_import_memory->memory_name;
import_type->kind = WASM_IMPORT_EXPORT_KIND_MEMORY;
import_type->linked = false;
return;
}
bh_assert(0);
return;
}
#endif
#if WASM_ENABLE_INTERP != 0
if (module->module_type == Wasm_Module_Bytecode) {
const WASMModule *wasm_module = (const WASMModule *)module;
if ((import_index < 0)
|| ((uint32_t)import_index >= wasm_module->import_count)) {
bh_assert(0);
return;
}
const WASMImport *wasm_import = &wasm_module->imports[import_index];
import_type->module_name = wasm_import->u.names.module_name;
import_type->name = wasm_import->u.names.field_name;
import_type->kind = wasm_import->kind;
switch (import_type->kind) {
case WASM_IMPORT_EXPORT_KIND_FUNC:
import_type->linked = wasm_import->u.function.func_ptr_linked;
break;
case WASM_IMPORT_EXPORT_KIND_GLOBAL:
import_type->linked = wasm_import->u.global.is_linked;
break;
case WASM_IMPORT_EXPORT_KIND_TABLE:
/* not supported */
import_type->linked = false;
break;
case WASM_IMPORT_EXPORT_KIND_MEMORY:
/* not supported */
import_type->linked = false;
break;
default:
bh_assert(0);
break;
}
return;
}
#endif
}
uint32_t
wasm_runtime_export_count(const wasm_module_t module)
int32_t
wasm_runtime_get_export_count(const wasm_module_t module)
{
if (!module) {
return 0;
bh_assert(0);
return -1;
}
const WASMModule *wasm_module = (const WASMModule *)module;
#if WASM_ENABLE_AOT != 0
if (module->module_type == Wasm_Module_AoT) {
const AOTModule *aot_module = (const AOTModule *)module;
return (int32_t)aot_module->export_count;
}
#endif
#if WASM_ENABLE_INTERP != 0
if (module->module_type == Wasm_Module_Bytecode) {
const WASMModule *wasm_module = (const WASMModule *)module;
return (int32_t)wasm_module->export_count;
}
#endif
return wasm_module->export_count;
return -1;
}
void
wasm_runtime_export_info(const wasm_module_t module, uint32_t export_index,
wasm_export_info_t *export_info)
wasm_runtime_get_export_type(const wasm_module_t module, int32_t export_index,
wasm_export_type *export_type)
{
if (!export_info) {
if (!export_type) {
bh_assert(0);
return;
}
memset(export_info, 0, sizeof(wasm_export_info_t));
memset(export_type, 0, sizeof(wasm_export_type));
if (!module) {
bh_assert(0);
return;
}
const WASMModule *wasm_module = (const WASMModule *)module;
if (export_index >= wasm_module->export_count) {
bh_assert(0);
#if WASM_ENABLE_AOT != 0
if (module->module_type == Wasm_Module_AoT) {
const AOTModule *aot_module = (const AOTModule *)module;
if ((export_index < 0)
|| ((uint32_t)export_index >= aot_module->export_count)) {
bh_assert(0);
return;
}
const AOTExport *aot_export = &aot_module->exports[export_index];
export_type->name = aot_export->name;
export_type->kind = aot_export->kind;
return;
}
#endif
#if WASM_ENABLE_INTERP != 0
if (module->module_type == Wasm_Module_Bytecode) {
const WASMModule *wasm_module = (const WASMModule *)module;
export_info->name = wasm_module->exports[export_index].name;
export_info->kind = wasm_module->exports[export_index].kind;
if ((export_index < 0)
|| ((uint32_t)export_index >= wasm_module->export_count)) {
bh_assert(0);
return;
}
const WASMExport *wasm_export = &wasm_module->exports[export_index];
export_type->name = wasm_export->name;
export_type->kind = wasm_export->kind;
return;
}
#endif
}
bool

View File

@ -66,26 +66,24 @@ typedef struct WASMModuleCommon *wasm_module_t;
typedef enum
{
WASM_IMPORT_EXPORT_KIND_FUNC,
WASM_IMPORT_EXPORT_KIND_GLOBAL,
WASM_IMPORT_EXPORT_KIND_TABLE,
WASM_IMPORT_EXPORT_KIND_MEMORY,
WASM_IMPORT_EXPORT_KIND_GLOBAL
} wasm_import_export_kind_t;
typedef struct wasm_import_info_t
typedef struct wasm_import_type
{
const char * module_name;
const char * name;
wasm_import_export_kind_t kind;
bool linked;
} wasm_import_info_t;
} wasm_import_type;
typedef struct wasm_export_info_t
typedef struct wasm_export_type
{
const char * name;
wasm_import_export_kind_t kind;
} wasm_export_info_t;
} wasm_export_type;
/* Instantiated WASM module */
struct WASMModuleInstanceCommon;
@ -1182,19 +1180,19 @@ wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst,
uint8_t **p_native_end_addr);
WASM_RUNTIME_API_EXTERN uint32_t wasm_runtime_import_count(const wasm_module_t module);
WASM_RUNTIME_API_EXTERN int32_t wasm_runtime_get_import_count(const wasm_module_t module);
WASM_RUNTIME_API_EXTERN void
wasm_runtime_import_info(const wasm_module_t module,
uint32_t import_index,
wasm_import_info_t * import_info);
wasm_runtime_get_import_type(const wasm_module_t module,
int32_t import_index,
wasm_import_type * import_type);
WASM_RUNTIME_API_EXTERN uint32_t wasm_runtime_export_count(const wasm_module_t module);
WASM_RUNTIME_API_EXTERN int32_t wasm_runtime_get_export_count(const wasm_module_t module);
WASM_RUNTIME_API_EXTERN void
wasm_runtime_export_info(const wasm_module_t module,
uint32_t export_index,
wasm_export_info_t * export_info);
wasm_runtime_get_export_type(const wasm_module_t module,
int32_t export_index,
wasm_export_type * export_type);
/**
* Register native functions with same module name