Skip to content

Commit c5ab862

Browse files
authored
Add api to get export global instance (#3452)
Add API ```C bool wasm_runtime_get_export_global_inst(const wasm_module_inst_t module_inst, const char *name, wasm_global_inst_t *global_inst); ```
1 parent 591a20b commit c5ab862

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

core/iwasm/common/wasm_runtime_common.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ wasm_runtime_destroy_registered_module_list();
9191

9292
#define E_TYPE_XIP 4
9393

94+
static uint8
95+
val_type_to_val_kind(uint8 value_type);
96+
9497
#if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
9598
/* Initialize externref hashmap */
9699
static bool
@@ -1900,6 +1903,67 @@ wasm_runtime_set_module_inst(WASMExecEnv *exec_env,
19001903
wasm_exec_env_set_module_inst(exec_env, module_inst);
19011904
}
19021905

1906+
bool
1907+
wasm_runtime_get_export_global_inst(WASMModuleInstanceCommon *const module_inst,
1908+
char const *name,
1909+
wasm_global_inst_t *global_inst)
1910+
{
1911+
#if WASM_ENABLE_INTERP != 0
1912+
if (module_inst->module_type == Wasm_Module_Bytecode) {
1913+
const WASMModuleInstance *wasm_module_inst =
1914+
(const WASMModuleInstance *)module_inst;
1915+
const WASMModule *wasm_module = wasm_module_inst->module;
1916+
uint32 i;
1917+
for (i = 0; i < wasm_module->export_count; i++) {
1918+
const WASMExport *wasm_export = &wasm_module->exports[i];
1919+
if ((wasm_export->kind == WASM_IMPORT_EXPORT_KIND_GLOBAL)
1920+
&& !strcmp(wasm_export->name, name)) {
1921+
const WASMModuleInstanceExtra *e =
1922+
(WASMModuleInstanceExtra *)wasm_module_inst->e;
1923+
const WASMGlobalInstance *global =
1924+
&e->globals[wasm_export->index];
1925+
global_inst->kind = val_type_to_val_kind(global->type);
1926+
global_inst->is_mutable = global->is_mutable;
1927+
#if WASM_ENABLE_MULTI_MODULE == 0
1928+
global_inst->global_data =
1929+
wasm_module_inst->global_data + global->data_offset;
1930+
#else
1931+
global_inst->global_data =
1932+
global->import_global_inst
1933+
? global->import_module_inst->global_data
1934+
+ global->import_global_inst->data_offset
1935+
: wasm_module_inst->global_data + global->data_offset;
1936+
#endif
1937+
return true;
1938+
}
1939+
}
1940+
}
1941+
#endif
1942+
#if WASM_ENABLE_AOT != 0
1943+
if (module_inst->module_type == Wasm_Module_AoT) {
1944+
const AOTModuleInstance *aot_module_inst =
1945+
(AOTModuleInstance *)module_inst;
1946+
const AOTModule *aot_module = (AOTModule *)aot_module_inst->module;
1947+
uint32 i;
1948+
for (i = 0; i < aot_module->export_count; i++) {
1949+
const AOTExport *aot_export = &aot_module->exports[i];
1950+
if ((aot_export->kind == WASM_IMPORT_EXPORT_KIND_GLOBAL)
1951+
&& !strcmp(aot_export->name, name)) {
1952+
const AOTGlobal *global =
1953+
&aot_module->globals[aot_export->index];
1954+
global_inst->kind = val_type_to_val_kind(global->type.val_type);
1955+
global_inst->is_mutable = global->type.is_mutable;
1956+
global_inst->global_data =
1957+
aot_module_inst->global_data + global->data_offset;
1958+
return true;
1959+
}
1960+
}
1961+
}
1962+
#endif
1963+
1964+
return false;
1965+
}
1966+
19031967
void *
19041968
wasm_runtime_get_function_attachment(WASMExecEnv *exec_env)
19051969
{

core/iwasm/include/wasm_export.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,13 @@ typedef struct wasm_val_t {
282282
} wasm_val_t;
283283
#endif
284284

285+
/* Global instance*/
286+
typedef struct wasm_global_inst_t {
287+
wasm_valkind_t kind;
288+
bool is_mutable;
289+
void *global_data;
290+
} wasm_global_inst_t;
291+
285292
typedef enum {
286293
WASM_LOG_LEVEL_FATAL = 0,
287294
WASM_LOG_LEVEL_ERROR = 1,
@@ -1414,6 +1421,21 @@ WASM_RUNTIME_API_EXTERN bool
14141421
wasm_runtime_unregister_natives(const char *module_name,
14151422
NativeSymbol *native_symbols);
14161423

1424+
/**
1425+
* Get an export global instance
1426+
*
1427+
* @param module_inst the module instance
1428+
* @param name the export global name
1429+
* @param global_inst location to store the global instance
1430+
*
1431+
* @return true if success, false otherwise
1432+
*
1433+
*/
1434+
WASM_RUNTIME_API_EXTERN bool
1435+
wasm_runtime_get_export_global_inst(const wasm_module_inst_t module_inst,
1436+
const char *name,
1437+
wasm_global_inst_t *global_inst);
1438+
14171439
/**
14181440
* Get attachment of native function from execution environment
14191441
*

0 commit comments

Comments
 (0)