From c0e33f08b04a5e03220ef74c2a9dbea8f1f35996 Mon Sep 17 00:00:00 2001 From: Benbuck Nason Date: Thu, 2 May 2024 18:41:08 -0700 Subject: [PATCH] Expose API to get import/export function's param/result valkind (#3363) Export API: ```C wasm_func_type_get_param_count wasm_func_type_get_param_valkind wasm_func_type_get_result_count wasm_func_type_get_result_valkind ``` And change wasm_import_type/wasm_export_type to wasm_import_t/wasm_export_t. --- core/iwasm/common/gc/gc_common.c | 12 --- core/iwasm/common/wasm_runtime_common.c | 107 +++++++++++++++++++++++- core/iwasm/include/gc_export.h | 20 ----- core/iwasm/include/wasm_export.h | 65 ++++++++++++-- 4 files changed, 162 insertions(+), 42 deletions(-) diff --git a/core/iwasm/common/gc/gc_common.c b/core/iwasm/common/gc/gc_common.c index 99fca86d..5cc869a6 100644 --- a/core/iwasm/common/gc/gc_common.c +++ b/core/iwasm/common/gc/gc_common.c @@ -176,12 +176,6 @@ wasm_defined_type_is_array_type(WASMType *const def_type) return wasm_type_is_array_type(def_type); } -uint32 -wasm_func_type_get_param_count(WASMFuncType *const func_type) -{ - return func_type->param_count; -} - wasm_ref_type_t wasm_func_type_get_param_type(WASMFuncType *const func_type, uint32 param_idx) { @@ -202,12 +196,6 @@ wasm_func_type_get_param_type(WASMFuncType *const func_type, uint32 param_idx) return ref_type; } -uint32 -wasm_func_type_get_result_count(WASMFuncType *const func_type) -{ - return (uint32)func_type->result_count; -} - wasm_ref_type_t wasm_func_type_get_result_type(WASMFuncType *const func_type, uint32 result_idx) { diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 2b481710..fe5b2bae 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -3756,14 +3756,14 @@ wasm_runtime_get_import_count(WASMModuleCommon *const module) void wasm_runtime_get_import_type(WASMModuleCommon *const module, int32 import_index, - wasm_import_type *import_type) + wasm_import_t *import_type) { if (!import_type) { bh_assert(0); return; } - memset(import_type, 0, sizeof(wasm_import_type)); + memset(import_type, 0, sizeof(wasm_import_t)); if (!module) { bh_assert(0); @@ -3783,6 +3783,7 @@ wasm_runtime_get_import_type(WASMModuleCommon *const module, int32 import_index, import_type->kind = WASM_IMPORT_EXPORT_KIND_FUNC; import_type->linked = aot_import_func->func_ptr_linked ? true : false; + import_type->u.func_type = aot_import_func->func_type; return; } @@ -3840,6 +3841,7 @@ wasm_runtime_get_import_type(WASMModuleCommon *const module, int32 import_index, switch (import_type->kind) { case WASM_IMPORT_EXPORT_KIND_FUNC: import_type->linked = wasm_import->u.function.func_ptr_linked; + import_type->u.func_type = wasm_import->u.function.func_type; break; case WASM_IMPORT_EXPORT_KIND_GLOBAL: import_type->linked = wasm_import->u.global.is_linked; @@ -3888,14 +3890,14 @@ wasm_runtime_get_export_count(WASMModuleCommon *const module) void wasm_runtime_get_export_type(WASMModuleCommon *const module, int32 export_index, - wasm_export_type *export_type) + wasm_export_t *export_type) { if (!export_type) { bh_assert(0); return; } - memset(export_type, 0, sizeof(wasm_export_type)); + memset(export_type, 0, sizeof(wasm_export_t)); if (!module) { bh_assert(0); @@ -3914,6 +3916,12 @@ 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]]; + } return; } #endif @@ -3929,11 +3937,102 @@ 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; + } return; } #endif } +uint32 +wasm_func_type_get_param_count(WASMFuncType *const func_type) +{ + bh_assert(func_type); + + return func_type->param_count; +} + +wasm_valkind_t +wasm_func_type_get_param_valkind(WASMFuncType *const func_type, + uint32 param_index) +{ + if (!func_type || (param_index >= func_type->param_count)) { + bh_assert(0); + return (wasm_valkind_t)-1; + } + + switch (func_type->types[param_index]) { + case VALUE_TYPE_I32: + return WASM_I32; + case VALUE_TYPE_I64: + return WASM_I64; + case VALUE_TYPE_F32: + return WASM_F32; + case VALUE_TYPE_F64: + return WASM_F64; + case VALUE_TYPE_FUNCREF: + return WASM_FUNCREF; + + case VALUE_TYPE_V128: + case VALUE_TYPE_EXTERNREF: + case VALUE_TYPE_VOID: + default: + { + bh_assert(0); + return (wasm_valkind_t)-1; + } + } +} + +uint32 +wasm_func_type_get_result_count(WASMFuncType *const func_type) +{ + bh_assert(func_type); + + return func_type->result_count; +} + +wasm_valkind_t +wasm_func_type_get_result_valkind(WASMFuncType *const func_type, + uint32 result_index) +{ + if (!func_type || (result_index >= func_type->result_count)) { + bh_assert(0); + return (wasm_valkind_t)-1; + } + + switch (func_type->types[func_type->param_count + result_index]) { + case VALUE_TYPE_I32: + return WASM_I32; + case VALUE_TYPE_I64: + return WASM_I64; + case VALUE_TYPE_F32: + return WASM_F32; + case VALUE_TYPE_F64: + return WASM_F64; + case VALUE_TYPE_FUNCREF: + return WASM_FUNCREF; + +#if WASM_ENABLE_SIMD != 0 + case VALUE_TYPE_V128: +#endif +#if WASM_ENABLE_REF_TYPES != 0 + case VALUE_TYPE_EXTERNREF: +#endif + case VALUE_TYPE_VOID: + default: + { + bh_assert(0); + return (wasm_valkind_t)-1; + } + } +} + bool wasm_runtime_register_natives(const char *module_name, NativeSymbol *native_symbols, diff --git a/core/iwasm/include/gc_export.h b/core/iwasm/include/gc_export.h index 777551ed..a9cdd148 100644 --- a/core/iwasm/include/gc_export.h +++ b/core/iwasm/include/gc_export.h @@ -230,16 +230,6 @@ wasm_defined_type_is_struct_type(const wasm_defined_type_t def_type); WASM_RUNTIME_API_EXTERN bool wasm_defined_type_is_array_type(const wasm_defined_type_t def_type); -/** - * Get parameter count of a function type - * - * @param func_type the specified function type - * - * @return the param count of the specified function type - */ -WASM_RUNTIME_API_EXTERN uint32_t -wasm_func_type_get_param_count(const wasm_func_type_t func_type); - /** * Get type of a specified parameter of a function type * @@ -253,16 +243,6 @@ WASM_RUNTIME_API_EXTERN wasm_ref_type_t wasm_func_type_get_param_type(const wasm_func_type_t func_type, uint32_t param_idx); -/** - * Get result count of a function type - * - * @param func_type the specified function type - * - * @return the result count of the specified function type - */ -WASM_RUNTIME_API_EXTERN uint32_t -wasm_func_type_get_result_count(const wasm_func_type_t func_type); - /** * Get type of a specified result of a function type * diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index 13022568..024c1eda 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -72,17 +72,26 @@ typedef enum { WASM_IMPORT_EXPORT_KIND_GLOBAL } wasm_import_export_kind_t; -typedef struct wasm_import_type { +struct WASMFuncType; +typedef struct WASMFuncType *wasm_func_type_t; + +typedef struct wasm_import_t { const char *module_name; const char *name; wasm_import_export_kind_t kind; bool linked; -} wasm_import_type; + union { + wasm_func_type_t func_type; + } u; +} wasm_import_t; -typedef struct wasm_export_type { +typedef struct wasm_export_t { const char *name; wasm_import_export_kind_t kind; -} wasm_export_type; + union { + wasm_func_type_t func_type; + } u; +} wasm_export_t; /* Instantiated WASM module */ struct WASMModuleInstanceCommon; @@ -1234,7 +1243,7 @@ wasm_runtime_get_import_count(const wasm_module_t module); */ WASM_RUNTIME_API_EXTERN void wasm_runtime_get_import_type(const wasm_module_t module, int32_t import_index, - wasm_import_type *import_type); + wasm_import_t *import_type); /** * Get the number of export items for a WASM module @@ -1255,7 +1264,51 @@ wasm_runtime_get_export_count(const wasm_module_t module); */ WASM_RUNTIME_API_EXTERN void wasm_runtime_get_export_type(const wasm_module_t module, int32_t export_index, - wasm_export_type *export_type); + wasm_export_t *export_type); + +/** + * Get the number of parameters for a function type + * + * @param func_type the function type + * + * @return the number of parameters for the function type + */ +WASM_RUNTIME_API_EXTERN uint32_t +wasm_func_type_get_param_count(wasm_func_type_t const func_type); + +/** + * Get the kind of a parameter for a function type + * + * @param func_type the function type + * @param param_index the index of the parameter to get + * + * @return the kind of the parameter if successful, -1 otherwise + */ +WASM_RUNTIME_API_EXTERN wasm_valkind_t +wasm_func_type_get_param_valkind(wasm_func_type_t const func_type, + uint32_t param_index); + +/** + * Get the number of results for a function type + * + * @param func_type the function type + * + * @return the number of results for the function type + */ +WASM_RUNTIME_API_EXTERN uint32_t +wasm_func_type_get_result_count(wasm_func_type_t const func_type); + +/** + * Get the kind of a result for a function type + * + * @param func_type the function type + * @param result_index the index of the result to get + * + * @return the kind of the result if successful, -1 otherwise + */ +WASM_RUNTIME_API_EXTERN wasm_valkind_t +wasm_func_type_get_result_valkind(wasm_func_type_t const func_type, + uint32_t result_index); /** * Register native functions with same module name