Przeglądaj źródła

[GC] Add API to get obj defined type and call func ref (#2180)

Xu Jun 2 lat temu
rodzic
commit
453bf2d20b
2 zmienionych plików z 179 dodań i 0 usunięć
  1. 89 0
      core/iwasm/common/gc/gc_common.c
  2. 90 0
      core/iwasm/include/gc_export.h

+ 89 - 0
core/iwasm/common/gc/gc_common.c

@@ -50,6 +50,46 @@ wasm_get_defined_type(WASMModuleCommon *const module, uint32 index)
     return type;
     return type;
 }
 }
 
 
+WASMType *
+wasm_obj_get_defined_type(const WASMObjectRef obj)
+{
+    if ((!wasm_obj_is_struct_obj(obj)) && (!wasm_obj_is_array_obj(obj))
+        && (!wasm_obj_is_func_obj(obj))) {
+        bh_assert(false);
+    }
+
+    return ((WASMRttTypeRef)(obj->header))->defined_type;
+}
+
+int32
+wasm_obj_get_defined_type_idx(WASMModuleCommon *const module,
+                              const WASMObjectRef obj)
+{
+    WASMType *type = wasm_obj_get_defined_type(obj);
+    int32 i, type_idx = -1;
+
+#if WASM_ENABLE_INTERP != 0
+    if (module->module_type == Wasm_Module_Bytecode) {
+        WASMModule *wasm_module = (WASMModule *)module;
+        uint32 type_count = wasm_module->type_count;
+
+        for (i = 0; i < type_count; i++) {
+            if (wasm_module->types[i] == type) {
+                type_idx = i;
+                break;
+            }
+        }
+
+        bh_assert(type_idx < type_count);
+    }
+#endif
+#if WASM_ENABLE_AOT != 0
+    /* TODO */
+#endif
+
+    return type_idx;
+}
+
 bool
 bool
 wasm_defined_type_is_func_type(WASMType *const def_type)
 wasm_defined_type_is_func_type(WASMType *const def_type)
 {
 {
@@ -444,6 +484,55 @@ wasm_func_obj_new_with_type(WASMExecEnv *exec_env, WASMFuncType *type)
     return NULL;
     return NULL;
 }
 }
 
 
+bool
+wasm_runtime_call_func_ref(WASMExecEnv *exec_env,
+                           const WASMFuncObjectRef func_obj, uint32 argc,
+                           uint32 argv[])
+{
+    WASMFunctionInstance *func_inst = NULL;
+    uint32 func_idx = wasm_func_obj_get_func_idx_bound(func_obj);
+
+#if WASM_ENABLE_INTERP != 0
+    if (exec_env->module_inst->module_type == Wasm_Module_Bytecode) {
+        WASMModuleInstance *module_inst =
+            (WASMModuleInstance *)exec_env->module_inst;
+
+        bh_assert(func_idx < module_inst->module->import_function_count
+                                 + module_inst->module->function_count);
+        func_inst = module_inst->e->functions + func_idx;
+    }
+#endif
+#if WASM_ENABLE_AOT != 0
+    if (exec_env->module_inst->module_type == Wasm_Module_AoT) {
+        /* TODO */
+        return false;
+    }
+#endif
+
+    bh_assert(func_inst);
+    return wasm_runtime_call_wasm(exec_env, func_inst, argc, argv);
+}
+
+bool
+wasm_runtime_call_func_ref_a(WASMExecEnv *exec_env,
+                             const WASMFuncObjectRef func_obj,
+                             uint32 num_results, wasm_val_t results[],
+                             uint32 num_args, wasm_val_t *args)
+{
+    /* TODO */
+    return false;
+}
+
+bool
+wasm_runtime_call_func_ref_v(wasm_exec_env_t exec_env,
+                             const WASMFuncObjectRef func_obj,
+                             uint32 num_results, wasm_val_t results[],
+                             uint32 num_args, ...)
+{
+    /* TODO */
+    return false;
+}
+
 bool
 bool
 wasm_obj_is_instance_of_defined_type(WASMObjectRef obj, WASMType *defined_type,
 wasm_obj_is_instance_of_defined_type(WASMObjectRef obj, WASMType *defined_type,
                                      WASMModuleCommon *const module)
                                      WASMModuleCommon *const module)

+ 90 - 0
core/iwasm/include/gc_export.h

@@ -144,6 +144,28 @@ wasm_get_defined_type_count(const wasm_module_t module);
 WASM_RUNTIME_API_EXTERN wasm_defined_type_t
 WASM_RUNTIME_API_EXTERN wasm_defined_type_t
 wasm_get_defined_type(const wasm_module_t module, uint32_t index);
 wasm_get_defined_type(const wasm_module_t module, uint32_t index);
 
 
+/**
+ * Get defined type of the GC managed object, the object must be struct,
+ * array or func.
+ *
+ * @param obj the object
+ *
+ * @return defined type of the object.
+ */
+WASM_RUNTIME_API_EXTERN wasm_defined_type_t
+wasm_obj_get_defined_type(const wasm_obj_t obj);
+
+/**
+ * Get defined type index of the GC managed object, the object must be struct,
+ * array or func.
+ *
+ * @param obj the object
+ *
+ * @return defined type index of the object.
+ */
+WASM_RUNTIME_API_EXTERN int32_t
+wasm_obj_get_defined_type_idx(const wasm_module_t module, const wasm_obj_t obj);
+
 /**
 /**
  * Check whether a defined type is a function type
  * Check whether a defined type is a function type
  */
  */
@@ -368,6 +390,74 @@ wasm_func_obj_get_func_idx_bound(const wasm_func_obj_t func_obj);
 WASM_RUNTIME_API_EXTERN wasm_func_type_t
 WASM_RUNTIME_API_EXTERN wasm_func_type_t
 wasm_func_obj_get_func_type(const wasm_func_obj_t func_obj);
 wasm_func_obj_get_func_type(const wasm_func_obj_t func_obj);
 
 
+/**
+ * Call the given WASM function object with arguments (bytecode and AoT).
+ *
+ * @param exec_env the execution environment to call the function,
+ *   which must be created from wasm_create_exec_env()
+ * @param func_obj the function object to call
+ * @param argc total cell number that the function parameters occupy,
+ *   a cell is a slot of the uint32 array argv[], e.g. i32/f32 argument
+ *   occupies one cell, i64/f64 argument occupies two cells, note that
+ *   it might be different from the parameter number of the function
+ * @param argv the arguments. If the function has return value,
+ *   the first (or first two in case 64-bit return value) element of
+ *   argv stores the return value of the called WASM function after this
+ *   function returns.
+ *
+ * @return true if success, false otherwise and exception will be thrown,
+ *   the caller can call wasm_runtime_get_exception to get the exception
+ *   info.
+ */
+WASM_RUNTIME_API_EXTERN bool
+wasm_runtime_call_func_ref(wasm_exec_env_t exec_env,
+                           const wasm_func_obj_t func_obj, uint32_t argc,
+                           uint32_t argv[]);
+
+/**
+ * Call the given WASM function object with provided results space
+ * and arguments (bytecode and AoT).
+ *
+ * @param exec_env the execution environment to call the function,
+ *   which must be created from wasm_create_exec_env()
+ * @param func_obj the function object to call
+ * @param num_results the number of results
+ * @param results the pre-alloced pointer to get the results
+ * @param num_args the number of arguments
+ * @param args the arguments
+ *
+ * @return true if success, false otherwise and exception will be thrown,
+ *   the caller can call wasm_runtime_get_exception to get the exception
+ *   info.
+ */
+WASM_RUNTIME_API_EXTERN bool
+wasm_runtime_call_func_ref_a(wasm_exec_env_t exec_env,
+                             const wasm_func_obj_t func_obj,
+                             uint32_t num_results, wasm_val_t results[],
+                             uint32_t num_args, wasm_val_t *args);
+
+/**
+ * Call the given WASM function object with provided results space and
+ * variant arguments (bytecode and AoT).
+ *
+ * @param exec_env the execution environment to call the function,
+ *   which must be created from wasm_create_exec_env()
+ * @param func_obj the function object to call
+ * @param num_results the number of results
+ * @param results the pre-alloced pointer to get the results
+ * @param num_args the number of arguments
+ * @param ... the variant arguments
+ *
+ * @return true if success, false otherwise and exception will be thrown,
+ *   the caller can call wasm_runtime_get_exception to get the exception
+ *   info.
+ */
+WASM_RUNTIME_API_EXTERN bool
+wasm_runtime_call_func_ref_v(wasm_exec_env_t exec_env,
+                             const wasm_func_obj_t func_obj,
+                             uint32_t num_results, wasm_val_t results[],
+                             uint32_t num_args, ...);
+
 /**
 /**
  * Create an externref object with host object
  * Create an externref object with host object
  */
  */