Skip to content

Commit

Permalink
feat: enhance table instance handling with shared and 64-bit flags
Browse files Browse the repository at this point in the history
  • Loading branch information
lum1n0us committed Nov 15, 2024
1 parent 28b39b5 commit dfb490e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
6 changes: 6 additions & 0 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,10 @@ 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;
tbl_inst->is_shared =
import_table->table_type.flags & SHARED_TABLE_FLAG;
}
else {
AOTTable *table = module->tables + (i - module->import_table_count);
Expand All @@ -704,6 +708,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 = table->table_type.flags & TABLE64_FLAG;
tbl_inst->is_shared = table->table_type.flags & SHARED_TABLE_FLAG;
}

/* Set all elements to -1 or NULL_REF to mark them as uninitialized
Expand Down
39 changes: 38 additions & 1 deletion core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -2207,6 +2207,43 @@ wasm_table_get_func_inst(struct WASMModuleInstanceCommon *const module_inst,
return NULL;
}

/* cur_size instead of init_size */
bool
wasm_runtime_get_table_type(struct WASMModuleInstanceCommon *const inst,
const wasm_table_inst_t table,
struct WASMTableType *const out)
{
if (!inst || !table || !out)
return false;

#if WASM_ENABLE_INTERP != 0
if (inst->module_type == Wasm_Module_Bytecode) {
const WASMTableInstance *wasm_table = (const WASMTableInstance *)table;
out->elem_type = wasm_table->elem_type;
out->init_size = wasm_table->cur_size;
out->max_size = wasm_table->max_size;
out->flags = 0 | (wasm_table->is_shared ? SHARED_TABLE_FLAG : 0)
| (wasm_table->is_table64 ? TABLE64_FLAG : 0);
return true;
}
#endif

#if WASM_ENABLE_AOT != 0
if (inst->module_type == Wasm_Module_AoT) {
const AOTTableInstance *aot_table = (const AOTTableInstance *)table;
out->elem_type = aot_table->elem_type;
out->init_size = aot_table->cur_size;
out->max_size = aot_table->max_size;
out->flags = 0 | (aot_table->is_shared ? SHARED_TABLE_FLAG : 0)
| (aot_table->is_table64 ? TABLE64_FLAG : 0);
return true;
}
#endif

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

void *
wasm_runtime_get_function_attachment(WASMExecEnv *exec_env)
{
Expand Down Expand Up @@ -4479,7 +4516,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
16 changes: 16 additions & 0 deletions core/iwasm/include/wasm_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,22 @@ wasm_runtime_get_export_table_inst(const wasm_module_inst_t module_inst,
const char *name,
wasm_table_inst_t table_inst);

/**
* @brief Retrieve the type of a specified table instance.
*
* This function retrieves the type of a given table instance within a module
* instance.
*
* @param module_inst The module instance containing the table.
* @param table_inst The table instance whose type is to be retrieved.
* @param table_type The output parameter to store the retrieved table type.
* @return true if the table type is successfully retrieved, false otherwise.
*/
WASM_RUNTIME_API_EXTERN bool
wasm_runtime_get_table_type(const wasm_module_inst_t module_inst,
const wasm_table_inst_t table_inst,
wasm_table_type_t table_type);

/**
* Get a function instance from a table.
*
Expand Down
4 changes: 4 additions & 0 deletions core/iwasm/interpreter/wasm_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ tables_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
#endif

table->is_table64 = import->u.table.table_type.flags & TABLE64_FLAG;
table->is_shared = import->u.table.table_type.flags & SHARED_TABLE_FLAG;

#if WASM_ENABLE_MULTI_MODULE != 0
*table_linked = table_inst_linked;
Expand Down Expand Up @@ -739,6 +740,9 @@ tables_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
uninitialized elements */
#endif
table->is_table64 = module->tables[i].table_type.flags & TABLE64_FLAG;
table->is_shared =
module->tables[i].table_type.flags & SHARED_TABLE_FLAG;

table->elem_type = module->tables[i].table_type.elem_type;
#if WASM_ENABLE_GC != 0
table->elem_ref_type.elem_ref_type =
Expand Down
3 changes: 2 additions & 1 deletion core/iwasm/interpreter/wasm_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ struct WASMTableInstance {
/* The element type */
uint8 elem_type;
uint8 is_table64;
uint8 __padding__[6];
uint8 is_shared;
uint8 __padding__[5];
union {
#if WASM_ENABLE_GC != 0
WASMRefType *elem_ref_type;
Expand Down

0 comments on commit dfb490e

Please sign in to comment.