Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functions to expose module import/export info #3330

Merged
merged 3 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -3597,6 +3597,103 @@ static union {

#define is_little_endian() (__ue.b == 1) /* NOLINT */

uint32_t
wasm_runtime_import_count(const wasm_module_t module)
{
if (!module) {
return 0;
bnason-nf marked this conversation as resolved.
Show resolved Hide resolved
}

const WASMModule *wasm_module = (const WASMModule *)module;

return wasm_module->import_count;
}

void
wasm_runtime_import_info(const wasm_module_t module, uint32_t import_index,
wasm_import_info_t *import_info)
{
if (!import_info) {
bh_assert(0);
return;
}

memset(import_info, 0, sizeof(wasm_import_info_t));

if (!module) {
bh_assert(0);
return;
}
const WASMModule *wasm_module = (const WASMModule *)module;

if (import_index >= wasm_module->import_count) {
bh_assert(0);
return;
}

import_info->module_name =
wasm_module->imports[import_index].u.names.module_name;
import_info->name = wasm_module->imports[import_index].u.names.field_name;
import_info->kind = wasm_module->imports[import_index].kind;
switch (import_info->kind) {
case WASM_IMPORT_EXPORT_KIND_FUNC:
import_info->linked =
wasm_module->imports[import_index].u.function.func_ptr_linked;
break;
case WASM_IMPORT_EXPORT_KIND_GLOBAL:
import_info->linked =
wasm_module->imports[import_index].u.global.is_linked;
break;
case WASM_IMPORT_EXPORT_KIND_TABLE:
import_info->linked = true; /* is this correct? */
bnason-nf marked this conversation as resolved.
Show resolved Hide resolved
break;
case WASM_IMPORT_EXPORT_KIND_MEMORY:
import_info->linked = true; /* is this correct? */
bnason-nf marked this conversation as resolved.
Show resolved Hide resolved
break;
default:
bh_assert(0);
break;
}
}

uint32_t
wasm_runtime_export_count(const wasm_module_t module)
{
if (!module) {
return 0;
bnason-nf marked this conversation as resolved.
Show resolved Hide resolved
}

const WASMModule *wasm_module = (const WASMModule *)module;

return wasm_module->export_count;
}

void
wasm_runtime_export_info(const wasm_module_t module, uint32_t export_index,
wasm_export_info_t *export_info)
{
if (!export_info) {
bh_assert(0);
return;
}

memset(export_info, 0, sizeof(wasm_export_info_t));

if (!module) {
bh_assert(0);
return;
}
const WASMModule *wasm_module = (const WASMModule *)module;

if (export_index >= wasm_module->export_count) {
bh_assert(0);
return;
}

export_info->name = wasm_module->exports[export_index].name;
export_info->kind = wasm_module->exports[export_index].kind;
}

bool
wasm_runtime_register_natives(const char *module_name,
NativeSymbol *native_symbols,
Expand Down
39 changes: 39 additions & 0 deletions core/iwasm/include/wasm_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,30 @@ struct WASMModuleCommon;
typedef struct WASMModuleCommon *wasm_module_t;
#endif

typedef enum
{
bnason-nf marked this conversation as resolved.
Show resolved Hide resolved
WASM_IMPORT_EXPORT_KIND_FUNC,
WASM_IMPORT_EXPORT_KIND_GLOBAL,
WASM_IMPORT_EXPORT_KIND_TABLE,
WASM_IMPORT_EXPORT_KIND_MEMORY,
} wasm_import_export_kind_t;

typedef struct wasm_import_info_t
bnason-nf marked this conversation as resolved.
Show resolved Hide resolved
{
const char * module_name;
const char * name;
bnason-nf marked this conversation as resolved.
Show resolved Hide resolved
wasm_import_export_kind_t kind;
bool linked;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I ask why linked is required here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's useful for informational purposes, it allows me to tell when I've properly provided all of the necessary imports by checking that they show up as linked.

} wasm_import_info_t;

typedef struct wasm_export_info_t
{
const char * name;
wasm_import_export_kind_t kind;
} wasm_export_info_t;
bnason-nf marked this conversation as resolved.
Show resolved Hide resolved



/* Instantiated WASM module */
struct WASMModuleInstanceCommon;
typedef struct WASMModuleInstanceCommon *wasm_module_inst_t;
Expand Down Expand Up @@ -1157,6 +1181,21 @@ wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst,
uint8_t **p_native_start_addr,
uint8_t **p_native_end_addr);


WASM_RUNTIME_API_EXTERN uint32_t wasm_runtime_import_count(const wasm_module_t module);
bnason-nf marked this conversation as resolved.
Show resolved Hide resolved

WASM_RUNTIME_API_EXTERN void
wasm_runtime_import_info(const wasm_module_t module,
bnason-nf marked this conversation as resolved.
Show resolved Hide resolved
uint32_t import_index,
wasm_import_info_t * import_info);

WASM_RUNTIME_API_EXTERN uint32_t wasm_runtime_export_count(const wasm_module_t module);
bnason-nf marked this conversation as resolved.
Show resolved Hide resolved

WASM_RUNTIME_API_EXTERN void
wasm_runtime_export_info(const wasm_module_t module,
bnason-nf marked this conversation as resolved.
Show resolved Hide resolved
uint32_t export_index,
wasm_export_info_t * export_info);

/**
* Register native functions with same module name
*
Expand Down
Loading