Add wasm module global type information APIs (#3406)
Support getting global type from `wasm_runtime_get_import_type` and `wasm_runtime_get_export_type`, and add two APIs: ```C wasm_valkind_t wasm_global_type_get_valkind(const wasm_global_type_t global_type); bool wasm_global_type_get_mutable(const wasm_global_type_t global_type); ```
This commit is contained in:
@ -2521,8 +2521,8 @@ wasm_module_imports(const wasm_module_t *module, own wasm_importtype_vec_t *out)
|
||||
+ (i - import_func_count);
|
||||
module_name_rt = import->u.names.module_name;
|
||||
field_name_rt = import->u.names.field_name;
|
||||
val_type_rt = import->u.global.type;
|
||||
mutability_rt = import->u.global.is_mutable;
|
||||
val_type_rt = import->u.global.type.val_type;
|
||||
mutability_rt = import->u.global.type.is_mutable;
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -2532,8 +2532,8 @@ wasm_module_imports(const wasm_module_t *module, own wasm_importtype_vec_t *out)
|
||||
+ (i - import_func_count);
|
||||
module_name_rt = import->module_name;
|
||||
field_name_rt = import->global_name;
|
||||
val_type_rt = import->type;
|
||||
mutability_rt = import->is_mutable;
|
||||
val_type_rt = import->type.val_type;
|
||||
mutability_rt = import->type.is_mutable;
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -3634,7 +3634,7 @@ aot_global_set(const AOTModuleInstance *inst_aot, uint16 global_idx_rt,
|
||||
|
||||
if (global_idx_rt < module_aot->import_global_count) {
|
||||
data_offset = module_aot->import_globals[global_idx_rt].data_offset;
|
||||
val_type_rt = module_aot->import_globals[global_idx_rt].type;
|
||||
val_type_rt = module_aot->import_globals[global_idx_rt].type.val_type;
|
||||
}
|
||||
else {
|
||||
data_offset =
|
||||
@ -3642,7 +3642,7 @@ aot_global_set(const AOTModuleInstance *inst_aot, uint16 global_idx_rt,
|
||||
.data_offset;
|
||||
val_type_rt =
|
||||
module_aot->globals[global_idx_rt - module_aot->import_global_count]
|
||||
.type;
|
||||
.type.val_type;
|
||||
}
|
||||
|
||||
data = (void *)(inst_aot->global_data + data_offset);
|
||||
@ -3661,7 +3661,7 @@ aot_global_get(const AOTModuleInstance *inst_aot, uint16 global_idx_rt,
|
||||
|
||||
if (global_idx_rt < module_aot->import_global_count) {
|
||||
data_offset = module_aot->import_globals[global_idx_rt].data_offset;
|
||||
val_type_rt = module_aot->import_globals[global_idx_rt].type;
|
||||
val_type_rt = module_aot->import_globals[global_idx_rt].type.val_type;
|
||||
}
|
||||
else {
|
||||
data_offset =
|
||||
@ -3669,7 +3669,7 @@ aot_global_get(const AOTModuleInstance *inst_aot, uint16 global_idx_rt,
|
||||
.data_offset;
|
||||
val_type_rt =
|
||||
module_aot->globals[global_idx_rt - module_aot->import_global_count]
|
||||
.type;
|
||||
.type.val_type;
|
||||
}
|
||||
|
||||
data = inst_aot->global_data + data_offset;
|
||||
@ -3786,15 +3786,15 @@ wasm_global_new_internal(wasm_store_t *store, uint16 global_idx_rt,
|
||||
if (global_idx_rt < module_aot->import_global_count) {
|
||||
AOTImportGlobal *global_import_aot =
|
||||
module_aot->import_globals + global_idx_rt;
|
||||
val_type_rt = global_import_aot->type;
|
||||
is_mutable = global_import_aot->is_mutable;
|
||||
val_type_rt = global_import_aot->type.val_type;
|
||||
is_mutable = global_import_aot->type.is_mutable;
|
||||
}
|
||||
else {
|
||||
AOTGlobal *global_aot =
|
||||
module_aot->globals
|
||||
+ (global_idx_rt - module_aot->import_global_count);
|
||||
val_type_rt = global_aot->type;
|
||||
is_mutable = global_aot->is_mutable;
|
||||
val_type_rt = global_aot->type.val_type;
|
||||
is_mutable = global_aot->type.is_mutable;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@ -4511,8 +4511,9 @@ interp_link_global(const WASMModule *module_interp, uint16 global_idx_rt,
|
||||
return true;
|
||||
|
||||
/* type comparison */
|
||||
if (!cmp_val_kind_with_val_type(wasm_valtype_kind(import->type->val_type),
|
||||
imported_global_interp->u.global.type))
|
||||
if (!cmp_val_kind_with_val_type(
|
||||
wasm_valtype_kind(import->type->val_type),
|
||||
imported_global_interp->u.global.type.val_type))
|
||||
return false;
|
||||
|
||||
/* set init value */
|
||||
@ -4685,7 +4686,7 @@ aot_link_global(const AOTModule *module_aot, uint16 global_idx_rt,
|
||||
bh_assert(val_type);
|
||||
|
||||
if (!cmp_val_kind_with_val_type(wasm_valtype_kind(val_type),
|
||||
import_aot_global->type))
|
||||
import_aot_global->type.val_type))
|
||||
return false;
|
||||
|
||||
bh_assert(import->init);
|
||||
|
||||
@ -3795,6 +3795,8 @@ wasm_runtime_get_import_type(WASMModuleCommon *const module, int32 import_index,
|
||||
import_type->name = aot_import_global->global_name;
|
||||
import_type->kind = WASM_IMPORT_EXPORT_KIND_GLOBAL;
|
||||
import_type->linked = aot_import_global->is_linked;
|
||||
import_type->u.global_type =
|
||||
(WASMGlobalType *)&aot_import_global->type;
|
||||
return;
|
||||
}
|
||||
|
||||
@ -3845,6 +3847,8 @@ wasm_runtime_get_import_type(WASMModuleCommon *const module, int32 import_index,
|
||||
break;
|
||||
case WASM_IMPORT_EXPORT_KIND_GLOBAL:
|
||||
import_type->linked = wasm_import->u.global.is_linked;
|
||||
import_type->u.global_type =
|
||||
(WASMGlobalType *)&wasm_import->u.global.type;
|
||||
break;
|
||||
case WASM_IMPORT_EXPORT_KIND_TABLE:
|
||||
/* not supported */
|
||||
@ -3916,11 +3920,32 @@ 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;
|
||||
if (export_type->kind == 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]];
|
||||
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:
|
||||
/* not supported */
|
||||
// export_type->linked = false;
|
||||
break;
|
||||
case WASM_IMPORT_EXPORT_KIND_MEMORY:
|
||||
/* not supported */
|
||||
// export_type->linked = false;
|
||||
break;
|
||||
default:
|
||||
bh_assert(0);
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
@ -3937,12 +3962,31 @@ 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;
|
||||
if (wasm_export->kind == EXPORT_KIND_FUNC) {
|
||||
export_type->u.func_type =
|
||||
wasm_module
|
||||
->functions[wasm_export->index
|
||||
- wasm_module->import_function_count]
|
||||
->func_type;
|
||||
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:
|
||||
/* not supported */
|
||||
// export_type->linked = false;
|
||||
break;
|
||||
case WASM_IMPORT_EXPORT_KIND_MEMORY:
|
||||
/* not supported */
|
||||
// export_type->linked = false;
|
||||
break;
|
||||
bh_assert(0);
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
@ -4033,6 +4077,22 @@ wasm_func_type_get_result_valkind(WASMFuncType *const func_type,
|
||||
}
|
||||
}
|
||||
|
||||
wasm_valkind_t
|
||||
wasm_global_type_get_valkind(const wasm_global_type_t global_type)
|
||||
{
|
||||
bh_assert(global_type);
|
||||
|
||||
return val_type_to_val_kind(global_type->val_type);
|
||||
}
|
||||
|
||||
bool
|
||||
wasm_global_type_get_mutable(const wasm_global_type_t global_type)
|
||||
{
|
||||
bh_assert(global_type);
|
||||
|
||||
return global_type->is_mutable;
|
||||
}
|
||||
|
||||
bool
|
||||
wasm_runtime_register_natives(const char *module_name,
|
||||
NativeSymbol *native_symbols,
|
||||
@ -5991,7 +6051,7 @@ aot_mark_all_externrefs(AOTModuleInstance *module_inst)
|
||||
const AOTTableInstance *table_inst;
|
||||
|
||||
for (i = 0; i < module->global_count; i++, global++) {
|
||||
if (global->type == VALUE_TYPE_EXTERNREF) {
|
||||
if (global->type.val_type == VALUE_TYPE_EXTERNREF) {
|
||||
mark_externref(
|
||||
*(uint32 *)(module_inst->global_data + global->data_offset));
|
||||
}
|
||||
@ -6320,14 +6380,14 @@ wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
|
||||
if (export->index < module->import_global_count) {
|
||||
WASMGlobalImport *import_global =
|
||||
&((module->import_globals + export->index)->u.global);
|
||||
*out_val_type = import_global->type;
|
||||
*out_mutability = import_global->is_mutable;
|
||||
*out_val_type = import_global->type.val_type;
|
||||
*out_mutability = import_global->type.is_mutable;
|
||||
}
|
||||
else {
|
||||
WASMGlobal *global =
|
||||
module->globals + (export->index - module->import_global_count);
|
||||
*out_val_type = global->type;
|
||||
*out_mutability = global->is_mutable;
|
||||
*out_val_type = global->type.val_type;
|
||||
*out_mutability = global->type.is_mutable;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -6340,14 +6400,14 @@ wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
|
||||
if (export->index < module->import_global_count) {
|
||||
AOTImportGlobal *import_global =
|
||||
module->import_globals + export->index;
|
||||
*out_val_type = import_global->type;
|
||||
*out_mutability = import_global->is_mutable;
|
||||
*out_val_type = import_global->type.val_type;
|
||||
*out_mutability = import_global->type.is_mutable;
|
||||
}
|
||||
else {
|
||||
AOTGlobal *global =
|
||||
module->globals + (export->index - module->import_global_count);
|
||||
*out_val_type = global->type;
|
||||
*out_mutability = global->is_mutable;
|
||||
*out_val_type = global->type.val_type;
|
||||
*out_mutability = global->type.is_mutable;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user