Skip to content

Commit

Permalink
refactor wasm_table_inst_t and add functions to retrieve export type …
Browse files Browse the repository at this point in the history
…by name
  • Loading branch information
lum1n0us committed Nov 15, 2024
1 parent 0e4dffc commit 41f7526
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 117 deletions.
3 changes: 3 additions & 0 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,8 @@ tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
tbl_inst->elem_ref_type.elem_ref_type =
module->tables[i].table_type.elem_ref_type;
#endif
tbl_inst->is_table64 =
import_table->table_type.flags & TABLE64_FLAG;
}
else {
AOTTable *table = module->tables + (i - module->import_table_count);
Expand All @@ -704,6 +706,7 @@ tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
tbl_inst->elem_ref_type.elem_ref_type =
module->tables[i].table_type.elem_ref_type;
#endif
tbl_inst->is_table64 = table->table_type.flags & TABLE64_FLAG;
}

/* Set all elements to -1 or NULL_REF to mark them as uninitialized
Expand Down
266 changes: 168 additions & 98 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -2095,62 +2095,65 @@ wasm_runtime_get_export_global_inst(WASMModuleInstanceCommon *const module_inst,
return false;
}

bool
WASMTableInstance *
wasm_runtime_get_export_table_inst(WASMModuleInstanceCommon *const module_inst,
char const *name,
wasm_table_inst_t *table_inst)
const char *name)
{
if (!module_inst || !name) {
return NULL;
}

#if WASM_ENABLE_INTERP != 0
if (module_inst->module_type == Wasm_Module_Bytecode) {
const WASMModuleInstance *wasm_module_inst =
(const WASMModuleInstance *)module_inst;
const WASMModule *wasm_module = wasm_module_inst->module;
uint32 i;
for (i = 0; i < wasm_module->export_count; i++) {

for (uint32 i = 0; i < wasm_module->export_count; i++) {
const WASMExport *wasm_export = &wasm_module->exports[i];
if ((wasm_export->kind == WASM_IMPORT_EXPORT_KIND_TABLE)
&& !strcmp(wasm_export->name, name)) {
const WASMTableInstance *wasm_table_inst =
wasm_module_inst->tables[wasm_export->index];
table_inst->elem_kind =
val_type_to_val_kind(wasm_table_inst->elem_type);
table_inst->cur_size = wasm_table_inst->cur_size;
table_inst->max_size = wasm_table_inst->max_size;
table_inst->elems = (void *)wasm_table_inst->elems;
return true;
}

if (wasm_export->kind != WASM_IMPORT_EXPORT_KIND_TABLE)
continue;

if (strcmp(wasm_export->name, name))
continue;

return wasm_module_inst->tables[wasm_export->index];
}

return NULL;
}
#endif

#if WASM_ENABLE_AOT != 0
if (module_inst->module_type == Wasm_Module_AoT) {
const AOTModuleInstance *aot_module_inst =
(AOTModuleInstance *)module_inst;
const AOTModule *aot_module = (AOTModule *)aot_module_inst->module;
uint32 i;
for (i = 0; i < aot_module->export_count; i++) {

for (uint32 i = 0; i < aot_module->export_count; i++) {
const AOTExport *aot_export = &aot_module->exports[i];
if ((aot_export->kind == WASM_IMPORT_EXPORT_KIND_TABLE)
&& !strcmp(aot_export->name, name)) {
const AOTTableInstance *aot_table_inst =
aot_module_inst->tables[aot_export->index];
table_inst->elem_kind =
val_type_to_val_kind(aot_table_inst->elem_type);
table_inst->cur_size = aot_table_inst->cur_size;
table_inst->max_size = aot_table_inst->max_size;
table_inst->elems = (void *)aot_table_inst->elems;
return true;
}

if (aot_export->kind == WASM_IMPORT_EXPORT_KIND_TABLE)
continue;

if (strcmp(aot_export->name, name))
continue;

return aot_module_inst->tables[aot_export->index];
}

return NULL;
}
#endif

return false;
LOG_ERROR("Unsupported module type: %d", module_inst->module_type);
return NULL;
}

WASMFunctionInstanceCommon *
wasm_table_get_func_inst(struct WASMModuleInstanceCommon *const module_inst,
const wasm_table_inst_t *table_inst, uint32_t idx)
WASMTableInstance *const table_inst, uint32_t idx)
{
if (!table_inst) {
bh_assert(0);
Expand Down Expand Up @@ -4230,6 +4233,84 @@ wasm_runtime_get_export_count(WASMModuleCommon *const module)
return -1;
}

#if WASM_ENABLE_AOT != 0
static void
populate_export_type_from_aot(const AOTModule *module, const AOTExport *export,
wasm_export_t *out)
{
if (!module || !export || !out)
return;

out->name = export->name;
out->kind = export->kind;

switch (out->kind) {
case WASM_IMPORT_EXPORT_KIND_FUNC:
out->u.func_type =
(AOTFuncType *)module
->types[module->func_type_indexes
[export->index - module->import_func_count]];
break;
case WASM_IMPORT_EXPORT_KIND_GLOBAL:
out->u.global_type =
&module->globals[export->index - module->import_global_count]
.type;
break;
case WASM_IMPORT_EXPORT_KIND_TABLE:
out->u.table_type =
&module->tables[export->index - module->import_table_count]
.table_type;
break;
case WASM_IMPORT_EXPORT_KIND_MEMORY:
out->u.memory_type =
&module->memories[export->index - module->import_memory_count];
break;
default:
bh_assert(0);
return;
}
}
#endif

#if WASM_ENABLE_INTERP != 0
static void
populate_export_type_from_wasm(const WASMModule *module,
const WASMExport *export, wasm_export_t *out)
{
if (!out || !module || !export) {
return;
}

out->name = export->name;
out->kind = export->kind;

switch (out->kind) {
case WASM_IMPORT_EXPORT_KIND_FUNC:
out->u.func_type =
module->functions[export->index - module->import_function_count]
->func_type;
break;
case WASM_IMPORT_EXPORT_KIND_GLOBAL:
out->u.global_type =
&module->globals[export->index - module->import_global_count]
.type;
break;
case WASM_IMPORT_EXPORT_KIND_TABLE:
out->u.table_type =
&module->tables[export->index - module->import_table_count]
.table_type;
break;
case WASM_IMPORT_EXPORT_KIND_MEMORY:
out->u.memory_type =
&module->memories[export->index - module->import_memory_count];
break;
default:
bh_assert(0);
return;
}
}
#endif

void
wasm_runtime_get_export_type(WASMModuleCommon *const module, int32 export_index,
wasm_export_t *export_type)
Expand All @@ -4256,39 +4337,7 @@ wasm_runtime_get_export_type(WASMModuleCommon *const module, int32 export_index,
}

const AOTExport *aot_export = &aot_module->exports[export_index];
export_type->name = aot_export->name;
export_type->kind = aot_export->kind;
switch (export_type->kind) {
case WASM_IMPORT_EXPORT_KIND_FUNC:
export_type->u.func_type =
(AOTFuncType *)aot_module
->types[aot_module->func_type_indexes
[aot_export->index
- aot_module->import_func_count]];
break;
case WASM_IMPORT_EXPORT_KIND_GLOBAL:
export_type->u.global_type =
&aot_module
->globals[aot_export->index
- aot_module->import_global_count]
.type;
break;
case WASM_IMPORT_EXPORT_KIND_TABLE:
export_type->u.table_type =
&aot_module
->tables[aot_export->index
- aot_module->import_table_count]
.table_type;
break;
case WASM_IMPORT_EXPORT_KIND_MEMORY:
export_type->u.memory_type =
&aot_module->memories[aot_export->index
- aot_module->import_memory_count];
break;
default:
bh_assert(0);
break;
}
populate_export_type_from_aot(aot_module, aot_export, export_type);
return;
}
#endif
Expand All @@ -4302,41 +4351,62 @@ wasm_runtime_get_export_type(WASMModuleCommon *const module, int32 export_index,
}

const WASMExport *wasm_export = &wasm_module->exports[export_index];
export_type->name = wasm_export->name;
export_type->kind = wasm_export->kind;
switch (export_type->kind) {
case WASM_IMPORT_EXPORT_KIND_FUNC:
export_type->u.func_type =
wasm_module
->functions[wasm_export->index
- wasm_module->import_function_count]
->func_type;
break;
case WASM_IMPORT_EXPORT_KIND_GLOBAL:
export_type->u.global_type =
&wasm_module
->globals[wasm_export->index
- wasm_module->import_global_count]
.type;
break;
case WASM_IMPORT_EXPORT_KIND_TABLE:
export_type->u.table_type =
&wasm_module
->tables[wasm_export->index
- wasm_module->import_table_count]
.table_type;
break;
case WASM_IMPORT_EXPORT_KIND_MEMORY:
export_type->u.memory_type =
&wasm_module->memories[wasm_export->index
- wasm_module->import_memory_count];
break;
default:
bh_assert(0);
break;
}
populate_export_type_from_wasm(wasm_module, wasm_export, export_type);
return;
}
#endif
}

void
wasm_runtime_get_export_type_by_name(WASMModuleCommon *const module,
const char *name,
wasm_export_t *export_type)
{
if (!export_type) {
bh_assert(0);
return;
}

memset(export_type, 0, sizeof(wasm_export_t));

if (!module) {
bh_assert(0);
return;
}

#if WASM_ENABLE_INTERP != 0
if (module->module_type == Wasm_Module_Bytecode) {
const WASMModule *wasm_module = (const WASMModule *)module;

for (uint32 i = 0; i < wasm_module->export_count; i++) {
WASMExport *wasm_export = &wasm_module->exports[i];

if (strcmp(name, wasm_export->name)) {
continue;
}

populate_export_type_from_wasm(wasm_module, wasm_export,
export_type);
return;
}
}
#endif

#if WASM_ENABLE_AOT != 0
if (module->module_type == Wasm_Module_AoT) {
const AOTModule *aot_module = (const AOTModule *)module;

for (uint32 i = 0; i < aot_module->export_count; i++) {
AOTExport *aot_export = &aot_module->exports[i];

if (strcmp(name, aot_export->name)) {
continue;
}

populate_export_type_from_aot(aot_module, aot_export, export_type);
return;
}
}
#endif
}

Expand Down Expand Up @@ -4479,7 +4549,7 @@ wasm_table_type_get_shared(WASMTableType *const table_type)
{
bh_assert(table_type);

return (table_type->flags & 2) ? true : false;
return (table_type->flags & SHARED_TABLE_FLAG) ? true : false;
}

uint32
Expand Down
Loading

0 comments on commit 41f7526

Please sign in to comment.