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:
@ -9,7 +9,7 @@
|
||||
|
||||
// A function to be called from Wasm code.
|
||||
own wasm_trap_t* callback(
|
||||
const wasm_val_vec_t *args, wasm_val_vec_t *results
|
||||
const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||
) {
|
||||
printf("Calling back...\n> ");
|
||||
printf("> %"PRIu32" %"PRIu64" %"PRIu64" %"PRIu32"\n",
|
||||
@ -27,14 +27,14 @@ own wasm_trap_t* callback(
|
||||
|
||||
// A function closure.
|
||||
own wasm_trap_t* closure_callback(
|
||||
void* env, const wasm_val_t args[], wasm_val_t results[]
|
||||
void* env, const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||
) {
|
||||
int i = *(int*)env;
|
||||
printf("Calling back closure...\n");
|
||||
printf("> %d\n", i);
|
||||
|
||||
results[0].kind = WASM_I32;
|
||||
results[0].of.i32 = (int32_t)i;
|
||||
results->data[0].kind = WASM_I32;
|
||||
results->data[0].of.i32 = (int32_t)i;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -47,7 +47,11 @@ int main(int argc, const char* argv[]) {
|
||||
|
||||
// Load binary.
|
||||
printf("Loading binary...\n");
|
||||
#if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
|
||||
FILE* file = fopen("multi.aot", "rb");
|
||||
#else
|
||||
FILE* file = fopen("multi.wasm", "rb");
|
||||
#endif
|
||||
if (!file) {
|
||||
printf("> Error loading module!\n");
|
||||
return 1;
|
||||
@ -91,8 +95,8 @@ int main(int argc, const char* argv[]) {
|
||||
|
||||
// Instantiate.
|
||||
printf("Instantiating module...\n");
|
||||
wasm_extern_vec_t imports;
|
||||
wasm_extern_vec_new(&imports, 1, (wasm_extern_t *[]) { wasm_func_as_extern(callback_func) });
|
||||
wasm_extern_t* externs[] = { wasm_func_as_extern(callback_func) };
|
||||
wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
|
||||
own wasm_instance_t* instance =
|
||||
wasm_instance_new(store, module, &imports, NULL);
|
||||
if (!instance) {
|
||||
@ -121,13 +125,14 @@ int main(int argc, const char* argv[]) {
|
||||
|
||||
// Call.
|
||||
printf("Calling export...\n");
|
||||
wasm_val_vec_t args, results;
|
||||
wasm_val_vec_new(&args, 4, (wasm_val_t []){
|
||||
WASM_I32_VAL(1), WASM_I64_VAL(2), WASM_I64_VAL(3), WASM_I32_VAL(4)
|
||||
});
|
||||
wasm_val_vec_new(&results, 4, (wasm_val_t []) {
|
||||
WASM_INIT_VAL, WASM_INIT_VAL, WASM_INIT_VAL, WASM_INIT_VAL
|
||||
});
|
||||
wasm_val_t vals[4] = {
|
||||
WASM_I32_VAL(1), WASM_I64_VAL(2), WASM_I64_VAL(3), WASM_I32_VAL(4)
|
||||
};
|
||||
wasm_val_t res[4] = {
|
||||
WASM_INIT_VAL, WASM_INIT_VAL, WASM_INIT_VAL, WASM_INIT_VAL
|
||||
};
|
||||
wasm_val_vec_t args = WASM_ARRAY_VEC(vals);
|
||||
wasm_val_vec_t results = WASM_ARRAY_VEC(res);
|
||||
if (wasm_func_call(run_func, &args, &results)) {
|
||||
printf("> Error calling function!\n");
|
||||
return 1;
|
||||
@ -138,12 +143,12 @@ int main(int argc, const char* argv[]) {
|
||||
// Print result.
|
||||
printf("Printing result...\n");
|
||||
printf("> %"PRIu32" %"PRIu64" %"PRIu64" %"PRIu32"\n",
|
||||
results.data[0].of.i32, results.data[1].of.i64, results.data[2].of.i64, results.data[3].of.i32);
|
||||
res[0].of.i32, res[1].of.i64, res[2].of.i64, res[3].of.i32);
|
||||
|
||||
assert(results.data[0].of.i32 == 1);
|
||||
assert(results.data[1].of.i64 == 2);
|
||||
assert(results.data[2].of.i64 == 3);
|
||||
assert(results.data[3].of.i32 == 4);
|
||||
assert(res[0].of.i32 == 4);
|
||||
assert(res[1].of.i64 == 3);
|
||||
assert(res[2].of.i64 == 2);
|
||||
assert(res[3].of.i32 == 1);
|
||||
|
||||
// Shut down.
|
||||
printf("Shutting down...\n");
|
||||
|
||||
Reference in New Issue
Block a user