Refactor externref related APIs of reference types feature (#971)
Currently when calling wasm_runtime_call_wasm() to invoke wasm function with externref type argument from runtime embedder, developer needs to use wasm_externref_obj2ref() to convert externref obj into an internal ref index firstly, which is not convenient to developer. To align with GC feature in which all the references passed to wasm_runtime_call_wasm() can be object pointers directly, we change the interface of wasm_runtime_call_wasm() to allow to pass object pointer directly for the externref argument, and refactor the related codes, update the related samples and the document.
This commit is contained in:
@ -13,52 +13,170 @@
|
||||
static char global_heap_buf[10 * 1024 * 1024] = { 0 };
|
||||
#endif
|
||||
|
||||
static int
|
||||
test_write_wrapper(wasm_exec_env_t exec_env, uint32 externref_idx_of_file,
|
||||
const char *str, int len)
|
||||
static uintptr_t global_objects[10] = { 0 };
|
||||
|
||||
int32
|
||||
local_cmp_externref(wasm_exec_env_t exec_env, uintptr_t externref_a,
|
||||
uintptr_t externref_b)
|
||||
{
|
||||
FILE *file;
|
||||
char buf[16];
|
||||
return externref_a == externref_b;
|
||||
}
|
||||
|
||||
printf("## retrieve file handle from externref index\n");
|
||||
if (!wasm_externref_ref2obj(externref_idx_of_file, (void **)&file)) {
|
||||
printf("failed to get host object from externref index!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
snprintf(buf, sizeof(buf), "%%%ds", len);
|
||||
|
||||
printf("## write string to file: ");
|
||||
printf(buf, str);
|
||||
|
||||
return fprintf(file, buf, str);
|
||||
int32
|
||||
local_chk_externref(wasm_exec_env_t exec_env, int32 index, uintptr_t externref)
|
||||
{
|
||||
return externref == global_objects[index];
|
||||
}
|
||||
|
||||
/* clang-format off */
|
||||
static NativeSymbol native_symbols[] = {
|
||||
{ "test_write", test_write_wrapper, "(i*~)i", NULL }
|
||||
{ "native-cmp-externref", local_cmp_externref, "(II)i", NULL },
|
||||
{ "native-chk-externref", local_chk_externref, "(iI)i", NULL },
|
||||
};
|
||||
/* clang-format on */
|
||||
|
||||
static inline void
|
||||
local_set_externref(int32 index, uintptr_t externref)
|
||||
{
|
||||
global_objects[index] = externref;
|
||||
}
|
||||
|
||||
static WASMFunctionInstanceCommon *wasm_set_externref_ptr;
|
||||
static WASMFunctionInstanceCommon *wasm_get_externref_ptr;
|
||||
static WASMFunctionInstanceCommon *wasm_cmp_externref_ptr;
|
||||
|
||||
static bool
|
||||
wasm_set_externref(wasm_exec_env_t exec_env, wasm_module_inst_t inst,
|
||||
int32 index, uintptr_t externref)
|
||||
{
|
||||
union {
|
||||
uintptr_t val;
|
||||
uint32 parts[2];
|
||||
} u;
|
||||
uint32 argv[3] = { 0 };
|
||||
|
||||
if (!exec_env || !wasm_set_externref_ptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
u.val = externref;
|
||||
argv[0] = index;
|
||||
argv[1] = u.parts[0];
|
||||
argv[2] = u.parts[1];
|
||||
if (!wasm_runtime_call_wasm(exec_env, wasm_set_externref_ptr, 2, argv)) {
|
||||
const char *exception;
|
||||
if ((exception = wasm_runtime_get_exception(inst))) {
|
||||
printf("Exception: %s\n", exception);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
wasm_get_externref(wasm_exec_env_t exec_env, wasm_module_inst_t inst,
|
||||
int32 index, uintptr_t *ret_externref)
|
||||
{
|
||||
wasm_val_t results[1] = { 0 };
|
||||
|
||||
if (!exec_env || !wasm_get_externref_ptr || !ret_externref) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!wasm_runtime_call_wasm_v(exec_env, wasm_get_externref_ptr, 1, results,
|
||||
1, index)) {
|
||||
const char *exception;
|
||||
if ((exception = wasm_runtime_get_exception(inst))) {
|
||||
printf("Exception: %s\n", exception);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (WASM_ANYREF != results[0].kind) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*ret_externref = results[0].of.foreign;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
wasm_cmp_externref(wasm_exec_env_t exec_env, wasm_module_inst_t inst,
|
||||
int32 index, uintptr_t externref, int32 *ret_result)
|
||||
{
|
||||
wasm_val_t results[1] = { 0 };
|
||||
wasm_val_t arguments[2] = {
|
||||
{ .kind = WASM_I32, .of.i32 = index },
|
||||
{ .kind = WASM_ANYREF, .of.foreign = externref },
|
||||
};
|
||||
|
||||
if (!exec_env || !wasm_cmp_externref_ptr || !ret_result) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!wasm_runtime_call_wasm_a(exec_env, wasm_cmp_externref_ptr, 1, results,
|
||||
2, arguments)) {
|
||||
const char *exception;
|
||||
if ((exception = wasm_runtime_get_exception(inst))) {
|
||||
printf("Exception: %s\n", exception);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (results[0].kind != WASM_I32) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*ret_result = results[0].of.i32;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
set_and_cmp(wasm_exec_env_t exec_env, wasm_module_inst_t inst, int32 i,
|
||||
uintptr_t externref)
|
||||
{
|
||||
int32 cmp_result = 0;
|
||||
uintptr_t wasm_externref = 0;
|
||||
|
||||
wasm_set_externref(exec_env, inst, i, externref);
|
||||
local_set_externref(i, externref);
|
||||
|
||||
wasm_get_externref(exec_env, inst, 0, &wasm_externref);
|
||||
if (!local_chk_externref(exec_env, 0, wasm_externref)) {
|
||||
printf("#%d, In host language world Wasm Externref 0x%lx Vs. Native "
|
||||
"Externref 0x%lx FAILED\n",
|
||||
i, wasm_externref, externref);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!wasm_cmp_externref(exec_env, inst, i, global_objects[i], &cmp_result)
|
||||
|| !cmp_result) {
|
||||
printf("#%d, In Wasm world Native Externref 0x%lx Vs, Wasm Externref "
|
||||
"FAILED\n",
|
||||
i, global_objects[i]);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
char *wasm_file = "hello.wasm";
|
||||
uint8 *wasm_file_buf = NULL;
|
||||
uint32 wasm_file_size, externref_idx;
|
||||
uint32 wasm_file_size;
|
||||
uint32 stack_size = 16 * 1024, heap_size = 16 * 1024;
|
||||
wasm_module_t wasm_module = NULL;
|
||||
wasm_module_inst_t wasm_module_inst = NULL;
|
||||
wasm_function_inst_t func_inst = NULL;
|
||||
wasm_exec_env_t exec_env = NULL;
|
||||
RuntimeInitArgs init_args;
|
||||
char error_buf[128] = { 0 };
|
||||
const char *exce;
|
||||
unsigned argv1[8];
|
||||
#if WASM_ENABLE_LOG != 0
|
||||
int log_verbose_level = 2;
|
||||
#endif
|
||||
FILE *file;
|
||||
const uint64 big_number = 0x123456789abc;
|
||||
|
||||
memset(&init_args, 0, sizeof(RuntimeInitArgs));
|
||||
|
||||
@ -90,13 +208,13 @@ main(int argc, char *argv[])
|
||||
/* load WASM byte buffer from WASM bin file */
|
||||
if (!(wasm_file_buf =
|
||||
(uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size)))
|
||||
goto fail1;
|
||||
goto fail;
|
||||
|
||||
/* load WASM module */
|
||||
if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
|
||||
error_buf, sizeof(error_buf)))) {
|
||||
printf("%s\n", error_buf);
|
||||
goto fail2;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* instantiate the module */
|
||||
@ -104,62 +222,66 @@ main(int argc, char *argv[])
|
||||
wasm_runtime_instantiate(wasm_module, stack_size, heap_size,
|
||||
error_buf, sizeof(error_buf)))) {
|
||||
printf("%s\n", error_buf);
|
||||
goto fail3;
|
||||
}
|
||||
|
||||
/* lookup function instance */
|
||||
if (!(func_inst =
|
||||
wasm_runtime_lookup_function(wasm_module_inst, "test", NULL))) {
|
||||
printf("%s\n", "lookup function test failed");
|
||||
goto fail4;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* create an execution env */
|
||||
if (!(exec_env =
|
||||
wasm_runtime_create_exec_env(wasm_module_inst, stack_size))) {
|
||||
printf("%s\n", "create exec env failed");
|
||||
goto fail4;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
printf("## open file test.txt\n");
|
||||
if (!(file = fopen("test.txt", "wb+"))) {
|
||||
printf("%s\n", "open file text.txt failed");
|
||||
goto fail5;
|
||||
/* lookup function instance */
|
||||
if (!(wasm_cmp_externref_ptr = wasm_runtime_lookup_function(
|
||||
wasm_module_inst, "cmp-externref", NULL))) {
|
||||
printf("%s\n", "lookup function cmp-externref failed");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
printf("## map file handle to externref index\n");
|
||||
if (!wasm_externref_obj2ref(wasm_module_inst, file, &externref_idx)) {
|
||||
printf("%s\n", "map host object to externref index failed");
|
||||
goto fail6;
|
||||
if (!(wasm_get_externref_ptr = wasm_runtime_lookup_function(
|
||||
wasm_module_inst, "get-externref", NULL))) {
|
||||
printf("%s\n", "lookup function get-externref failed");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
printf("## call wasm function with externref index\n");
|
||||
argv1[0] = externref_idx;
|
||||
wasm_runtime_call_wasm(exec_env, func_inst, 1, argv1);
|
||||
|
||||
if ((exce = wasm_runtime_get_exception(wasm_module_inst))) {
|
||||
printf("Exception: %s\n", exce);
|
||||
if (!(wasm_set_externref_ptr = wasm_runtime_lookup_function(
|
||||
wasm_module_inst, "set-externref", NULL))) {
|
||||
printf("%s\n", "lookup function set-externref failed");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
fail6:
|
||||
fclose(file);
|
||||
/* test with NULL */
|
||||
if (!set_and_cmp(exec_env, wasm_module_inst, 0, 0)
|
||||
|| !set_and_cmp(exec_env, wasm_module_inst, 1, big_number + 1)
|
||||
|| !set_and_cmp(exec_env, wasm_module_inst, 2, big_number + 2)
|
||||
|| !set_and_cmp(exec_env, wasm_module_inst, 3, big_number + 3)) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
fail5:
|
||||
printf("GREAT! PASS ALL CHKs\n");
|
||||
|
||||
fail:
|
||||
/* destroy exec env */
|
||||
wasm_runtime_destroy_exec_env(exec_env);
|
||||
if (exec_env) {
|
||||
wasm_runtime_destroy_exec_env(exec_env);
|
||||
}
|
||||
|
||||
fail4:
|
||||
/* destroy the module instance */
|
||||
wasm_runtime_deinstantiate(wasm_module_inst);
|
||||
if (wasm_module_inst) {
|
||||
wasm_runtime_deinstantiate(wasm_module_inst);
|
||||
}
|
||||
|
||||
fail3:
|
||||
/* unload the module */
|
||||
wasm_runtime_unload(wasm_module);
|
||||
if (wasm_module) {
|
||||
wasm_runtime_unload(wasm_module);
|
||||
}
|
||||
|
||||
fail2:
|
||||
/* free the file buffer */
|
||||
wasm_runtime_free(wasm_file_buf);
|
||||
if (wasm_file_buf) {
|
||||
wasm_runtime_free(wasm_file_buf);
|
||||
}
|
||||
|
||||
fail1:
|
||||
/* destroy runtime environment */
|
||||
wasm_runtime_destroy();
|
||||
return 0;
|
||||
|
||||
@ -2,21 +2,43 @@
|
||||
;; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
(module
|
||||
;; import test_write function which is implemented by host
|
||||
(import "env" "test_write"
|
||||
(func $test_write (param externref i32 i32) (result i32)))
|
||||
(type $t0 (func (param i32 externref) (result i32)))
|
||||
|
||||
;; memory with one page (64KiB).
|
||||
(memory (export "memory") 1)
|
||||
(import "env" "native-cmp-externref"
|
||||
(func $native-cmp-externref (param externref externref) (result i32))
|
||||
)
|
||||
|
||||
(data (i32.const 0x8) "Hello, world!\n")
|
||||
(import "env" "native-chk-externref"
|
||||
(func $native-chk-externref (param i32 externref) (result i32))
|
||||
)
|
||||
|
||||
;; function that writes string to a given open file handle
|
||||
(func (export "test") (param externref)
|
||||
(local.get 0)
|
||||
(i32.const 0x8)
|
||||
(i32.const 14)
|
||||
(call $test_write)
|
||||
drop
|
||||
(table $t1 8 8 externref)
|
||||
(table $t2 funcref
|
||||
(elem
|
||||
$native-cmp-externref
|
||||
$native-chk-externref
|
||||
)
|
||||
)
|
||||
|
||||
(func (export "set-externref") (param $i i32) (param $r externref)
|
||||
(table.set $t1 (local.get $i) (local.get $r))
|
||||
)
|
||||
|
||||
(func (export "get-externref") (param $i i32) (result externref)
|
||||
(table.get $t1 (local.get $i))
|
||||
)
|
||||
|
||||
(func (export "cmp-externref") (param $i i32) (param $r externref) (result i32)
|
||||
(table.get $t1 (local.get $i))
|
||||
(local.get $r)
|
||||
(call $native-cmp-externref)
|
||||
)
|
||||
|
||||
(func (export "chk-externref") (param $i i32) (param $r externref) (result i32)
|
||||
(call_indirect $t2 (type $t0)
|
||||
(local.get $i)
|
||||
(local.get $r)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user