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:
@ -2042,8 +2042,8 @@ load_import_globals(const uint8 **p_buf, const uint8 *buf_end,
|
||||
/* Create each import global */
|
||||
for (i = 0; i < module->import_global_count; i++) {
|
||||
buf = (uint8 *)align_ptr(buf, 2);
|
||||
read_uint8(buf, buf_end, import_globals[i].type);
|
||||
read_uint8(buf, buf_end, import_globals[i].is_mutable);
|
||||
read_uint8(buf, buf_end, import_globals[i].type.val_type);
|
||||
read_uint8(buf, buf_end, import_globals[i].type.is_mutable);
|
||||
read_string(buf, buf_end, import_globals[i].module_name);
|
||||
read_string(buf, buf_end, import_globals[i].global_name);
|
||||
|
||||
@ -2051,8 +2051,9 @@ load_import_globals(const uint8 **p_buf, const uint8 *buf_end,
|
||||
if (wasm_native_lookup_libc_builtin_global(
|
||||
import_globals[i].module_name, import_globals[i].global_name,
|
||||
&tmp_global)) {
|
||||
if (tmp_global.type != import_globals[i].type
|
||||
|| tmp_global.is_mutable != import_globals[i].is_mutable) {
|
||||
if (tmp_global.type.val_type != import_globals[i].type.val_type
|
||||
|| tmp_global.type.is_mutable
|
||||
!= import_globals[i].type.is_mutable) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"incompatible import type");
|
||||
return false;
|
||||
@ -2065,7 +2066,8 @@ load_import_globals(const uint8 **p_buf, const uint8 *buf_end,
|
||||
import_globals[i].is_linked = false;
|
||||
#endif
|
||||
|
||||
import_globals[i].size = wasm_value_type_size(import_globals[i].type);
|
||||
import_globals[i].size =
|
||||
wasm_value_type_size(import_globals[i].type.val_type);
|
||||
import_globals[i].data_offset = data_offset;
|
||||
data_offset += import_globals[i].size;
|
||||
module->global_data_size += import_globals[i].size;
|
||||
@ -2130,8 +2132,8 @@ load_globals(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
|
||||
|
||||
/* Create each global */
|
||||
for (i = 0; i < module->global_count; i++) {
|
||||
read_uint8(buf, buf_end, globals[i].type);
|
||||
read_uint8(buf, buf_end, globals[i].is_mutable);
|
||||
read_uint8(buf, buf_end, globals[i].type.val_type);
|
||||
read_uint8(buf, buf_end, globals[i].type.is_mutable);
|
||||
|
||||
buf = align_ptr(buf, 4);
|
||||
|
||||
@ -2139,7 +2141,7 @@ load_globals(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
|
||||
error_buf, error_buf_size))
|
||||
return false;
|
||||
|
||||
globals[i].size = wasm_value_type_size(globals[i].type);
|
||||
globals[i].size = wasm_value_type_size(globals[i].type.val_type);
|
||||
globals[i].data_offset = data_offset;
|
||||
data_offset += globals[i].size;
|
||||
module->global_data_size += globals[i].size;
|
||||
|
||||
@ -129,7 +129,7 @@ check_global_init_expr(const AOTModule *module, uint32 global_index,
|
||||
* And initializer expression cannot reference a mutable global.
|
||||
*/
|
||||
if (global_index >= module->import_global_count
|
||||
|| module->import_globals->is_mutable) {
|
||||
|| module->import_globals->type.is_mutable) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"constant expression required");
|
||||
return false;
|
||||
@ -141,7 +141,7 @@ check_global_init_expr(const AOTModule *module, uint32 global_index,
|
||||
return false;
|
||||
}
|
||||
if (global_index < module->import_global_count
|
||||
&& module->import_globals[global_index].is_mutable) {
|
||||
&& module->import_globals[global_index].type.is_mutable) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"constant expression required");
|
||||
return false;
|
||||
@ -389,7 +389,7 @@ global_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
|
||||
for (i = 0; i < module->import_global_count; i++, import_global++) {
|
||||
bh_assert(import_global->data_offset
|
||||
== (uint32)(p - module_inst->global_data));
|
||||
init_global_data(p, import_global->type,
|
||||
init_global_data(p, import_global->type.val_type,
|
||||
&import_global->global_data_linked);
|
||||
p += import_global->size;
|
||||
}
|
||||
@ -410,20 +410,20 @@ global_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
|
||||
}
|
||||
#if WASM_ENABLE_GC == 0
|
||||
init_global_data(
|
||||
p, global->type,
|
||||
p, global->type.val_type,
|
||||
&module->import_globals[init_expr->u.global_index]
|
||||
.global_data_linked);
|
||||
#else
|
||||
if (init_expr->u.global_index < module->import_global_count) {
|
||||
init_global_data(
|
||||
p, global->type,
|
||||
p, global->type.val_type,
|
||||
&module->import_globals[init_expr->u.global_index]
|
||||
.global_data_linked);
|
||||
}
|
||||
else {
|
||||
uint32 global_idx =
|
||||
init_expr->u.global_index - module->import_global_count;
|
||||
init_global_data(p, global->type,
|
||||
init_global_data(p, global->type.val_type,
|
||||
&module->globals[global_idx].init_expr.u);
|
||||
}
|
||||
#endif
|
||||
@ -581,7 +581,7 @@ global_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
|
||||
#endif /* end of WASM_ENABLE_GC != 0 */
|
||||
default:
|
||||
{
|
||||
init_global_data(p, global->type, &init_expr->u);
|
||||
init_global_data(p, global->type.val_type, &init_expr->u);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -4549,7 +4549,7 @@ aot_global_traverse_gc_rootset(AOTModuleInstance *module_inst, void *heap)
|
||||
uint32 i;
|
||||
|
||||
for (i = 0; i < module->import_global_count; i++, import_global++) {
|
||||
if (wasm_is_type_reftype(import_global->type)) {
|
||||
if (wasm_is_type_reftype(import_global->type.val_type)) {
|
||||
gc_obj = GET_REF_FROM_ADDR((uint32 *)global_data);
|
||||
if (wasm_obj_is_created_from_heap(gc_obj)) {
|
||||
if (0 != mem_allocator_add_root((mem_allocator_t)heap, gc_obj))
|
||||
@ -4560,7 +4560,7 @@ aot_global_traverse_gc_rootset(AOTModuleInstance *module_inst, void *heap)
|
||||
}
|
||||
|
||||
for (i = 0; i < module->global_count; i++, global++) {
|
||||
if (wasm_is_type_reftype(global->type)) {
|
||||
if (wasm_is_type_reftype(global->type.val_type)) {
|
||||
gc_obj = GET_REF_FROM_ADDR((uint32 *)global_data);
|
||||
if (wasm_obj_is_created_from_heap(gc_obj)) {
|
||||
if (0 != mem_allocator_add_root((mem_allocator_t)heap, gc_obj))
|
||||
|
||||
Reference in New Issue
Block a user