From af6e0fb23065d51540d1ef392b4ea7dd70eef24f Mon Sep 17 00:00:00 2001 From: Andreas Rossberg Date: Thu, 6 Aug 2020 12:28:36 +0200 Subject: [PATCH] Use vectors for calls and imports (#145) * Use vectors for calls and instantiate * Macros; fix val initialisation in examples * Add non-own vec of Extern for instantiate --- example/callback.c | 31 +++++++++++----------- example/callback.cc | 11 ++++---- example/finalize.c | 3 ++- example/finalize.cc | 3 ++- example/global.c | 50 +++++++++++++++++++++--------------- example/global.cc | 14 +++++----- example/hello.c | 11 +++++--- example/hello.cc | 8 +++--- example/hostref.c | 61 ++++++++++++++++++++++---------------------- example/hostref.cc | 28 +++++++++++--------- example/memory.c | 57 ++++++++++++++++++++--------------------- example/memory.cc | 17 +++++++----- example/multi.c | 55 ++++++++++++++++++++------------------- example/multi.cc | 10 ++++---- example/reflect.c | 4 ++- example/reflect.cc | 3 ++- example/serialize.c | 12 ++++++--- example/serialize.cc | 8 +++--- example/start.c | 3 ++- example/start.cc | 3 ++- example/table.c | 31 +++++++++++----------- example/table.cc | 13 +++++----- example/threads.c | 16 +++++++----- example/threads.cc | 7 ++--- example/trap.c | 11 +++++--- example/trap.cc | 8 +++--- include/wasm.h | 21 ++++++++++++--- include/wasm.hh | 18 ++++++------- src/wasm-c.cc | 35 +++++++++++++++++-------- src/wasm-v8.cc | 13 +++++----- 30 files changed, 318 insertions(+), 247 deletions(-) diff --git a/example/callback.c b/example/callback.c index 122ee30..2fddb16 100644 --- a/example/callback.c +++ b/example/callback.c @@ -35,27 +35,27 @@ void wasm_val_print(wasm_val_t val) { // A function to be called from Wasm code. own wasm_trap_t* print_callback( - const wasm_val_t args[], wasm_val_t results[] + const wasm_val_vec_t* args, wasm_val_vec_t* results ) { printf("Calling back...\n> "); - wasm_val_print(args[0]); + wasm_val_print(args->data[0]); printf("\n"); - wasm_val_copy(&results[0], &args[0]); + wasm_val_copy(&results->data[0], &args->data[0]); return NULL; } // 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; } @@ -108,11 +108,12 @@ int main(int argc, const char* argv[]) { // Instantiate. printf("Instantiating module...\n"); - const wasm_extern_t* imports[] = { + wasm_extern_t* externs[] = { wasm_func_as_extern(print_func), wasm_func_as_extern(closure_func) }; + wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs); own wasm_instance_t* instance = - wasm_instance_new(store, module, imports, NULL); + wasm_instance_new(store, module, &imports, NULL); if (!instance) { printf("> Error instantiating module!\n"); return 1; @@ -140,13 +141,11 @@ int main(int argc, const char* argv[]) { // Call. printf("Calling export...\n"); - wasm_val_t args[2]; - args[0].kind = WASM_I32; - args[0].of.i32 = 3; - args[1].kind = WASM_I32; - args[1].of.i32 = 4; - wasm_val_t results[1]; - if (wasm_func_call(run_func, args, results)) { + wasm_val_t as[2] = { WASM_I32_VAL(3), WASM_I32_VAL(4) }; + wasm_val_t rs[1] = { WASM_INIT_VAL }; + wasm_val_vec_t args = WASM_ARRAY_VEC(as); + wasm_val_vec_t results = WASM_ARRAY_VEC(rs); + if (wasm_func_call(run_func, &args, &results)) { printf("> Error calling function!\n"); return 1; } @@ -155,7 +154,7 @@ int main(int argc, const char* argv[]) { // Print result. printf("Printing result...\n"); - printf("> %u\n", results[0].of.i32); + printf("> %u\n", rs[0].of.i32); // Shut down. printf("Shutting down...\n"); diff --git a/example/callback.cc b/example/callback.cc index 41a388c..957629c 100644 --- a/example/callback.cc +++ b/example/callback.cc @@ -35,7 +35,7 @@ auto operator<<(std::ostream& out, const wasm::Val& val) -> std::ostream& { // A function to be called from Wasm code. auto print_callback( - const wasm::Val args[], wasm::Val results[] + const wasm::vec& args, wasm::vec& results ) -> wasm::own { std::cout << "Calling back..." << std::endl << "> " << args[0] << std::endl; results[0] = args[0].copy(); @@ -45,7 +45,7 @@ auto print_callback( // A function closure. auto closure_callback( - void* env, const wasm::Val args[], wasm::Val results[] + void* env, const wasm::vec& args, wasm::vec& results ) -> wasm::own { auto i = *reinterpret_cast(env); std::cout << "Calling back closure..." << std::endl; @@ -103,7 +103,8 @@ void run() { // Instantiate. std::cout << "Instantiating module..." << std::endl; - wasm::Extern* imports[] = {print_func.get(), closure_func.get()}; + auto imports = wasm::vec::make( + print_func.get(), closure_func.get()); auto instance = wasm::Instance::make(store, module.get(), imports); if (!instance) { std::cout << "> Error instantiating module!" << std::endl; @@ -121,8 +122,8 @@ void run() { // Call. std::cout << "Calling export..." << std::endl; - wasm::Val args[] = {wasm::Val::i32(3), wasm::Val::i32(4)}; - wasm::Val results[1]; + auto args = wasm::vec::make(wasm::Val::i32(3), wasm::Val::i32(4)); + auto results = wasm::vec::make_uninitialized(1); if (run_func->call(args, results)) { std::cout << "> Error calling function!" << std::endl; exit(1); diff --git a/example/finalize.c b/example/finalize.c index ff612e7..502fc60 100644 --- a/example/finalize.c +++ b/example/finalize.c @@ -50,8 +50,9 @@ void run_in_store(wasm_store_t* store) { printf("Instantiating modules...\n"); for (int i = 0; i <= iterations; ++i) { if (i % (iterations / 10) == 0) printf("%d\n", i); + wasm_extern_vec_t imports = WASM_EMPTY_VEC; own wasm_instance_t* instance = - wasm_instance_new(store, module, NULL, NULL); + wasm_instance_new(store, module, &imports, NULL); if (!instance) { printf("> Error instantiating module %d!\n", i); exit(1); diff --git a/example/finalize.cc b/example/finalize.cc index 64e134b..ce7e972 100644 --- a/example/finalize.cc +++ b/example/finalize.cc @@ -46,7 +46,8 @@ void run_in_store(wasm::Store* store) { std::cout << "Instantiating modules..." << std::endl; for (int i = 0; i <= iterations; ++i) { if (i % (iterations / 10) == 0) std::cout << i << std::endl; - auto instance = wasm::Instance::make(store, module.get(), nullptr); + auto imports = wasm::vec::make(); + auto instance = wasm::Instance::make(store, module.get(), imports); if (!instance) { std::cout << "> Error instantiating module " << i << "!" << std::endl; exit(1); diff --git a/example/global.c b/example/global.c index 593434d..5bd4033 100644 --- a/example/global.c +++ b/example/global.c @@ -39,9 +39,11 @@ wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) { #define check_call(func, type, expected) \ { \ - wasm_val_t results[1]; \ - wasm_func_call(func, NULL, results); \ - check(results[0], type, expected); \ + wasm_val_t vs[1]; \ + wasm_val_vec_t args = WASM_EMPTY_VEC; \ + wasm_val_vec_t results = WASM_ARRAY_VEC(vs); \ + wasm_func_call(func, &args, &results); \ + check(vs[0], type, expected); \ } @@ -90,16 +92,16 @@ int main(int argc, const char* argv[]) { own wasm_globaltype_t* var_i64_type = wasm_globaltype_new( wasm_valtype_new(WASM_I64), WASM_VAR); - wasm_val_t val_f32_1 = {.kind = WASM_F32, .of = {.f32 = 1}}; + wasm_val_t val_f32_1 = WASM_F32_VAL(1); own wasm_global_t* const_f32_import = wasm_global_new(store, const_f32_type, &val_f32_1); - wasm_val_t val_i64_2 = {.kind = WASM_I64, .of = {.i64 = 2}}; + wasm_val_t val_i64_2 = WASM_I64_VAL(2); own wasm_global_t* const_i64_import = wasm_global_new(store, const_i64_type, &val_i64_2); - wasm_val_t val_f32_3 = {.kind = WASM_F32, .of = {.f32 = 3}}; + wasm_val_t val_f32_3 = WASM_F32_VAL(3); own wasm_global_t* var_f32_import = wasm_global_new(store, var_f32_type, &val_f32_3); - wasm_val_t val_i64_4 = {.kind = WASM_I64, .of = {.i64 = 4}}; + wasm_val_t val_i64_4 = WASM_I64_VAL(4); own wasm_global_t* var_i64_import = wasm_global_new(store, var_i64_type, &val_i64_4); @@ -110,14 +112,15 @@ int main(int argc, const char* argv[]) { // Instantiate. printf("Instantiating module...\n"); - const wasm_extern_t* imports[] = { + wasm_extern_t* externs[] = { wasm_global_as_extern(const_f32_import), wasm_global_as_extern(const_i64_import), wasm_global_as_extern(var_f32_import), wasm_global_as_extern(var_i64_import) }; + wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs); own wasm_instance_t* instance = - wasm_instance_new(store, module, imports, NULL); + wasm_instance_new(store, module, &imports, NULL); if (!instance) { printf("> Error instantiating module!\n"); return 1; @@ -175,13 +178,13 @@ int main(int argc, const char* argv[]) { check_call(get_var_i64_export, i64, 8); // Modify variables through API and check again. - wasm_val_t val33 = {.kind = WASM_F32, .of = {.f32 = 33}}; + wasm_val_t val33 = WASM_F32_VAL(33); wasm_global_set(var_f32_import, &val33); - wasm_val_t val34 = {.kind = WASM_I64, .of = {.i64 = 34}}; + wasm_val_t val34 = WASM_I64_VAL(34); wasm_global_set(var_i64_import, &val34); - wasm_val_t val37 = {.kind = WASM_F32, .of = {.f32 = 37}}; + wasm_val_t val37 = WASM_F32_VAL(37); wasm_global_set(var_f32_export, &val37); - wasm_val_t val38 = {.kind = WASM_I64, .of = {.i64 = 38}}; + wasm_val_t val38 = WASM_I64_VAL(38); wasm_global_set(var_i64_export, &val38); check_global(var_f32_import, f32, 33); @@ -195,14 +198,19 @@ int main(int argc, const char* argv[]) { check_call(get_var_i64_export, i64, 38); // Modify variables through calls and check again. - wasm_val_t args73[] = { {.kind = WASM_F32, .of = {.f32 = 73}} }; - wasm_func_call(set_var_f32_import, args73, NULL); - wasm_val_t args74[] = { {.kind = WASM_I64, .of = {.i64 = 74}} }; - wasm_func_call(set_var_i64_import, args74, NULL); - wasm_val_t args77[] = { {.kind = WASM_F32, .of = {.f32 = 77}} }; - wasm_func_call(set_var_f32_export, args77, NULL); - wasm_val_t args78[] = { {.kind = WASM_I64, .of = {.i64 = 78}} }; - wasm_func_call(set_var_i64_export, args78, NULL); + wasm_val_vec_t res = WASM_EMPTY_VEC; + wasm_val_t vs73[] = { WASM_F32_VAL(73) }; + wasm_val_vec_t args73 = WASM_ARRAY_VEC(vs73); + wasm_func_call(set_var_f32_import, &args73, &res); + wasm_val_t vs74[] = { WASM_I64_VAL(74) }; + wasm_val_vec_t args74 = WASM_ARRAY_VEC(vs74); + wasm_func_call(set_var_i64_import, &args74, &res); + wasm_val_t vs77[] = { WASM_F32_VAL(77) }; + wasm_val_vec_t args77 = WASM_ARRAY_VEC(vs77); + wasm_func_call(set_var_f32_export, &args77, &res); + wasm_val_t vs78[] = { WASM_I64_VAL(78) }; + wasm_val_vec_t args78 = WASM_ARRAY_VEC(vs78); + wasm_func_call(set_var_i64_export, &args78, &res); check_global(var_f32_import, f32, 73); check_global(var_i64_import, i64, 74); diff --git a/example/global.cc b/example/global.cc index efe90f0..178eb61 100644 --- a/example/global.cc +++ b/example/global.cc @@ -32,8 +32,9 @@ void check(T actual, U expected) { } auto call(const wasm::Func* func) -> wasm::Val { - wasm::Val results[1]; - if (func->call(nullptr, results)) { + auto args = wasm::vec::make(); + auto results = wasm::vec::make_uninitialized(1); + if (func->call(args, results)) { std::cout << "> Error calling function!" << std::endl; exit(1); } @@ -41,8 +42,9 @@ auto call(const wasm::Func* func) -> wasm::Val { } void call(const wasm::Func* func, wasm::Val&& arg) { - wasm::Val args[1] = {std::move(arg)}; - if (func->call(args)) { + auto args = wasm::vec::make(std::move(arg)); + auto results = wasm::vec::make(); + if (func->call(args, results)) { std::cout << "> Error calling function!" << std::endl; exit(1); } @@ -95,10 +97,10 @@ void run() { // Instantiate. std::cout << "Instantiating module..." << std::endl; - wasm::Extern* imports[] = { + auto imports = wasm::vec::make( const_f32_import.get(), const_i64_import.get(), var_f32_import.get(), var_i64_import.get() - }; + ); auto instance = wasm::Instance::make(store, module.get(), imports); if (!instance) { std::cout << "> Error instantiating module!" << std::endl; diff --git a/example/hello.c b/example/hello.c index 36acf4f..740f099 100644 --- a/example/hello.c +++ b/example/hello.c @@ -9,7 +9,7 @@ // A function to be called from Wasm code. own wasm_trap_t* hello_callback( - const wasm_val_t args[], wasm_val_t results[] + const wasm_val_vec_t* args, wasm_val_vec_t* results ) { printf("Calling back...\n"); printf("> Hello World!\n"); @@ -61,9 +61,10 @@ int main(int argc, const char* argv[]) { // Instantiate. printf("Instantiating module...\n"); - const wasm_extern_t* imports[] = { wasm_func_as_extern(hello_func) }; + wasm_extern_t* externs[] = { wasm_func_as_extern(hello_func) }; + wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs); own wasm_instance_t* instance = - wasm_instance_new(store, module, imports, NULL); + wasm_instance_new(store, module, &imports, NULL); if (!instance) { printf("> Error instantiating module!\n"); return 1; @@ -90,7 +91,9 @@ int main(int argc, const char* argv[]) { // Call. printf("Calling export...\n"); - if (wasm_func_call(run_func, NULL, NULL)) { + wasm_val_vec_t args = WASM_EMPTY_VEC; + wasm_val_vec_t results = WASM_EMPTY_VEC; + if (wasm_func_call(run_func, &args, &results)) { printf("> Error calling function!\n"); return 1; } diff --git a/example/hello.cc b/example/hello.cc index a6bbe28..94eb567 100644 --- a/example/hello.cc +++ b/example/hello.cc @@ -9,7 +9,7 @@ // A function to be called from Wasm code. auto hello_callback( - const wasm::Val args[], wasm::Val results[] + const wasm::vec& args, wasm::vec& results ) -> wasm::own { std::cout << "Calling back..." << std::endl; std::cout << "> Hello world!" << std::endl; @@ -55,7 +55,7 @@ void run() { // Instantiate. std::cout << "Instantiating module..." << std::endl; - wasm::Extern* imports[] = {hello_func.get()}; + auto imports = wasm::vec::make(hello_func.get()); auto instance = wasm::Instance::make(store, module.get(), imports); if (!instance) { std::cout << "> Error instantiating module!" << std::endl; @@ -73,7 +73,9 @@ void run() { // Call. std::cout << "Calling export..." << std::endl; - if (run_func->call()) { + auto args = wasm::vec::make(); + auto results = wasm::vec::make(); + if (run_func->call(args, results)) { std::cout << "> Error calling function!" << std::endl; exit(1); } diff --git a/example/hostref.c b/example/hostref.c index 2ce156b..1e787ab 100644 --- a/example/hostref.c +++ b/example/hostref.c @@ -10,12 +10,12 @@ // A function to be called from Wasm code. own wasm_trap_t* callback( - const wasm_val_t args[], wasm_val_t results[] + const wasm_val_vec_t* args, wasm_val_vec_t* results ) { printf("Calling back...\n> "); printf("> %p\n", - args[0].of.ref ? wasm_ref_get_host_info(args[0].of.ref) : NULL); - wasm_val_copy(&results[0], &args[0]); + args->data[0].of.ref ? wasm_ref_get_host_info(args->data[0].of.ref) : NULL); + wasm_val_copy(&results->data[0], &args->data[0]); return NULL; } @@ -47,21 +47,23 @@ wasm_table_t* get_export_table(const wasm_extern_vec_t* exports, size_t i) { own wasm_ref_t* call_v_r(const wasm_func_t* func) { printf("call_v_r... "); fflush(stdout); - wasm_val_t results[1]; - if (wasm_func_call(func, NULL, results)) { + wasm_val_t rs[] = { WASM_INIT_VAL }; + wasm_val_vec_t args = WASM_EMPTY_VEC; + wasm_val_vec_t results = WASM_ARRAY_VEC(rs); + if (wasm_func_call(func, &args, &results)) { printf("> Error calling function!\n"); exit(1); } printf("okay\n"); - return results[0].of.ref; + return rs[0].of.ref; } void call_r_v(const wasm_func_t* func, wasm_ref_t* ref) { printf("call_r_v... "); fflush(stdout); - wasm_val_t args[1]; - args[0].kind = WASM_ANYREF; - args[0].of.ref = ref; - if (wasm_func_call(func, args, NULL)) { + wasm_val_t vs[1] = { WASM_REF_VAL(ref) }; + wasm_val_vec_t args = WASM_ARRAY_VEC(vs); + wasm_val_vec_t results = WASM_EMPTY_VEC; + if (wasm_func_call(func, &args, &results)) { printf("> Error calling function!\n"); exit(1); } @@ -70,26 +72,24 @@ void call_r_v(const wasm_func_t* func, wasm_ref_t* ref) { own wasm_ref_t* call_r_r(const wasm_func_t* func, wasm_ref_t* ref) { printf("call_r_r... "); fflush(stdout); - wasm_val_t args[1]; - args[0].kind = WASM_ANYREF; - args[0].of.ref = ref; - wasm_val_t results[1]; - if (wasm_func_call(func, args, results)) { + wasm_val_t vs[1] = { WASM_REF_VAL(ref) }; + wasm_val_t rs[1] = { WASM_INIT_VAL }; + wasm_val_vec_t args = WASM_ARRAY_VEC(vs); + wasm_val_vec_t results = WASM_ARRAY_VEC(rs); + if (wasm_func_call(func, &args, &results)) { printf("> Error calling function!\n"); exit(1); } printf("okay\n"); - return results[0].of.ref; + return rs[0].of.ref; } void call_ir_v(const wasm_func_t* func, int32_t i, wasm_ref_t* ref) { printf("call_ir_v... "); fflush(stdout); - wasm_val_t args[2]; - args[0].kind = WASM_I32; - args[0].of.i32 = i; - args[1].kind = WASM_ANYREF; - args[1].of.ref = ref; - if (wasm_func_call(func, args, NULL)) { + wasm_val_t vs[2] = { WASM_I32_VAL(i), WASM_REF_VAL(ref) }; + wasm_val_vec_t args = WASM_ARRAY_VEC(vs); + wasm_val_vec_t results = WASM_EMPTY_VEC; + if (wasm_func_call(func, &args, &results)) { printf("> Error calling function!\n"); exit(1); } @@ -98,16 +98,16 @@ void call_ir_v(const wasm_func_t* func, int32_t i, wasm_ref_t* ref) { own wasm_ref_t* call_i_r(const wasm_func_t* func, int32_t i) { printf("call_i_r... "); fflush(stdout); - wasm_val_t args[1]; - args[0].kind = WASM_I32; - args[0].of.i32 = i; - wasm_val_t results[1]; - if (wasm_func_call(func, args, results)) { + wasm_val_t vs[1] = { WASM_I32_VAL(i) }; + wasm_val_t rs[1] = { WASM_INIT_VAL }; + wasm_val_vec_t args = WASM_ARRAY_VEC(vs); + wasm_val_vec_t results = WASM_ARRAY_VEC(rs); + if (wasm_func_call(func, &args, &results)) { printf("> Error calling function!\n"); exit(1); } printf("okay\n"); - return results[0].of.ref; + return rs[0].of.ref; } void check(own wasm_ref_t* actual, const wasm_ref_t* expected) { @@ -167,9 +167,10 @@ int main(int argc, const char* argv[]) { // Instantiate. printf("Instantiating module...\n"); - const wasm_extern_t* imports[] = { 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); + wasm_instance_new(store, module, &imports, NULL); if (!instance) { printf("> Error instantiating module!\n"); return 1; diff --git a/example/hostref.cc b/example/hostref.cc index c94c8f5..09c239e 100644 --- a/example/hostref.cc +++ b/example/hostref.cc @@ -9,7 +9,7 @@ // A function to be called from Wasm code. auto callback( - const wasm::Val args[], wasm::Val results[] + const wasm::vec& args, wasm::vec& results ) -> wasm::own { std::cout << "Calling back..." << std::endl; std::cout << "> " << (args[0].ref() ? args[0].ref()->get_host_info() : nullptr) << std::endl; @@ -45,8 +45,9 @@ auto get_export_table(wasm::ownvec& exports, size_t i) -> wasm::Ta void call_r_v(const wasm::Func* func, const wasm::Ref* ref) { std::cout << "call_r_v... " << std::flush; - wasm::Val args[1] = {wasm::Val::ref(ref ? ref->copy() : wasm::own())}; - if (func->call(args, nullptr)) { + auto args = wasm::vec::make(wasm::Val::ref(ref ? ref->copy() : wasm::own())); + auto results = wasm::vec::make(); + if (func->call(args, results)) { std::cout << "> Error calling function!" << std::endl; exit(1); } @@ -55,8 +56,9 @@ void call_r_v(const wasm::Func* func, const wasm::Ref* ref) { auto call_v_r(const wasm::Func* func) -> wasm::own { std::cout << "call_v_r... " << std::flush; - wasm::Val results[1]; - if (func->call(nullptr, results)) { + auto args = wasm::vec::make(); + auto results = wasm::vec::make_uninitialized(1); + if (func->call(args, results)) { std::cout << "> Error calling function!" << std::endl; exit(1); } @@ -66,8 +68,8 @@ auto call_v_r(const wasm::Func* func) -> wasm::own { auto call_r_r(const wasm::Func* func, const wasm::Ref* ref) -> wasm::own { std::cout << "call_r_r... " << std::flush; - wasm::Val args[1] = {wasm::Val::ref(ref ? ref->copy() : wasm::own())}; - wasm::Val results[1]; + auto args = wasm::vec::make(wasm::Val::ref(ref ? ref->copy() : wasm::own())); + auto results = wasm::vec::make_uninitialized(1); if (func->call(args, results)) { std::cout << "> Error calling function!" << std::endl; exit(1); @@ -78,8 +80,10 @@ auto call_r_r(const wasm::Func* func, const wasm::Ref* ref) -> wasm::owncopy() : wasm::own())}; - if (func->call(args, nullptr)) { + auto args = wasm::vec::make( + wasm::Val::i32(i), wasm::Val::ref(ref ? ref->copy() : wasm::own())); + auto results = wasm::vec::make(); + if (func->call(args, results)) { std::cout << "> Error calling function!" << std::endl; exit(1); } @@ -88,8 +92,8 @@ void call_ir_v(const wasm::Func* func, int32_t i, const wasm::Ref* ref) { auto call_i_r(const wasm::Func* func, int32_t i) -> wasm::own { std::cout << "call_i_r... " << std::flush; - wasm::Val args[1] = {wasm::Val::i32(i)}; - wasm::Val results[1]; + auto args = wasm::vec::make(wasm::Val::i32(i)); + auto results = wasm::vec::make_uninitialized(1); if (func->call(args, results)) { std::cout << "> Error calling function!" << std::endl; exit(1); @@ -147,7 +151,7 @@ void run() { // Instantiate. std::cout << "Instantiating module..." << std::endl; - wasm::Extern* imports[] = {callback_func.get()}; + auto imports = wasm::vec::make(callback_func.get()); auto instance = wasm::Instance::make(store, module.get(), imports); if (!instance) { std::cout << "> Error instantiating module!" << std::endl; diff --git a/example/memory.c b/example/memory.c index 912069d..edd4eba 100644 --- a/example/memory.c +++ b/example/memory.c @@ -32,49 +32,49 @@ void check(bool success) { } } -void check_call(wasm_func_t* func, wasm_val_t args[], int32_t expected) { - wasm_val_t results[1]; - if (wasm_func_call(func, args, results) || results[0].of.i32 != expected) { +void check_call(wasm_func_t* func, int i, wasm_val_t args[], int32_t expected) { + wasm_val_t r = WASM_INIT_VAL; + wasm_val_vec_t args_ = {i, args}; + wasm_val_vec_t results = {1, &r}; + if (wasm_func_call(func, &args_, &results) || r.of.i32 != expected) { printf("> Error on result\n"); exit(1); } } void check_call0(wasm_func_t* func, int32_t expected) { - check_call(func, NULL, expected); + check_call(func, 0, NULL, expected); } void check_call1(wasm_func_t* func, int32_t arg, int32_t expected) { - wasm_val_t args[] = { {.kind = WASM_I32, .of = {.i32 = arg}} }; - check_call(func, args, expected); + wasm_val_t args[] = { WASM_I32_VAL(arg) }; + check_call(func, 1, args, expected); } void check_call2(wasm_func_t* func, int32_t arg1, int32_t arg2, int32_t expected) { - wasm_val_t args[2] = { - {.kind = WASM_I32, .of = {.i32 = arg1}}, - {.kind = WASM_I32, .of = {.i32 = arg2}} - }; - check_call(func, args, expected); + wasm_val_t args[] = { WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) }; + check_call(func, 2, args, expected); } -void check_ok(wasm_func_t* func, wasm_val_t args[]) { - if (wasm_func_call(func, args, NULL)) { +void check_ok(wasm_func_t* func, int i, wasm_val_t args[]) { + wasm_val_vec_t args_ = {i, args}; + wasm_val_vec_t results = {0, NULL}; + if (wasm_func_call(func, &args_, &results)) { printf("> Error on result, expected empty\n"); exit(1); } } void check_ok2(wasm_func_t* func, int32_t arg1, int32_t arg2) { - wasm_val_t args[2] = { - {.kind = WASM_I32, .of = {.i32 = arg1}}, - {.kind = WASM_I32, .of = {.i32 = arg2}} - }; - check_ok(func, args); + wasm_val_t args[] = { WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) }; + check_ok(func, 2, args); } -void check_trap(wasm_func_t* func, wasm_val_t args[]) { - wasm_val_t results[1]; - own wasm_trap_t* trap = wasm_func_call(func, args, results); +void check_trap(wasm_func_t* func, int i, wasm_val_t args[]) { + wasm_val_t r = WASM_INIT_VAL; + wasm_val_vec_t args_ = {i, args}; + wasm_val_vec_t results = {1, &r}; + own wasm_trap_t* trap = wasm_func_call(func, &args_, &results); if (! trap) { printf("> Error on result, expected trap\n"); exit(1); @@ -83,16 +83,13 @@ void check_trap(wasm_func_t* func, wasm_val_t args[]) { } void check_trap1(wasm_func_t* func, int32_t arg) { - wasm_val_t args[1] = { {.kind = WASM_I32, .of = {.i32 = arg}} }; - check_trap(func, args); + wasm_val_t args[] = { WASM_I32_VAL(arg) }; + check_trap(func, 1, args); } void check_trap2(wasm_func_t* func, int32_t arg1, int32_t arg2) { - wasm_val_t args[2] = { - {.kind = WASM_I32, .of = {.i32 = arg1}}, - {.kind = WASM_I32, .of = {.i32 = arg2}} - }; - check_trap(func, args); + wasm_val_t args[] = { WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) }; + check_trap(func, 2, args); } @@ -132,7 +129,9 @@ int main(int argc, const char* argv[]) { // Instantiate. printf("Instantiating module...\n"); - own wasm_instance_t* instance = wasm_instance_new(store, module, NULL, NULL); + wasm_extern_vec_t imports = WASM_EMPTY_VEC; + own wasm_instance_t* instance = + wasm_instance_new(store, module, &imports, NULL); if (!instance) { printf("> Error instantiating module!\n"); return 1; diff --git a/example/memory.cc b/example/memory.cc index 4094acc..6cc3619 100644 --- a/example/memory.cc +++ b/example/memory.cc @@ -33,8 +33,9 @@ void check(T actual, U expected) { template void check_ok(const wasm::Func* func, Args... xs) { - wasm::Val args[] = {wasm::Val::i32(xs)...}; - if (func->call(args)) { + auto args = wasm::vec::make(wasm::Val::i32(xs)...); + auto results = wasm::vec::make(); + if (func->call(args, results)) { std::cout << "> Error on result, expected return" << std::endl; exit(1); } @@ -42,8 +43,9 @@ void check_ok(const wasm::Func* func, Args... xs) { template void check_trap(const wasm::Func* func, Args... xs) { - wasm::Val args[] = {wasm::Val::i32(xs)...}; - if (! func->call(args)) { + auto args = wasm::vec::make(wasm::Val::i32(xs)...); + auto results = wasm::vec::make(); + if (! func->call(args, results)) { std::cout << "> Error on result, expected trap" << std::endl; exit(1); } @@ -51,8 +53,8 @@ void check_trap(const wasm::Func* func, Args... xs) { template auto call(const wasm::Func* func, Args... xs) -> int32_t { - wasm::Val args[] = {wasm::Val::i32(xs)...}; - wasm::Val results[1]; + auto args = wasm::vec::make(wasm::Val::i32(xs)...); + auto results = wasm::vec::make_uninitialized(1); if (func->call(args, results)) { std::cout << "> Error on result, expected return" << std::endl; exit(1); @@ -92,7 +94,8 @@ void run() { // Instantiate. std::cout << "Instantiating module..." << std::endl; - auto instance = wasm::Instance::make(store, module.get(), nullptr); + auto imports = wasm::vec::make(); + auto instance = wasm::Instance::make(store, module.get(), imports); if (!instance) { std::cout << "> Error instantiating module!" << std::endl; exit(1); diff --git a/example/multi.c b/example/multi.c index 4c1f556..846c6ec 100644 --- a/example/multi.c +++ b/example/multi.c @@ -9,31 +9,32 @@ // A function to be called from Wasm code. own wasm_trap_t* callback( - const wasm_val_t args[], wasm_val_t results[] + const wasm_val_vec_t* args, wasm_val_vec_t* results ) { printf("Calling back...\n> "); printf("> %"PRIu32" %"PRIu64" %"PRIu64" %"PRIu32"\n", - args[0].of.i32, args[1].of.i64, args[2].of.i64, args[3].of.i32); + args->data[0].of.i32, args->data[1].of.i64, + args->data[2].of.i64, args->data[3].of.i32); printf("\n"); - wasm_val_copy(&results[0], &args[3]); - wasm_val_copy(&results[1], &args[1]); - wasm_val_copy(&results[2], &args[2]); - wasm_val_copy(&results[3], &args[0]); + wasm_val_copy(&results->data[0], &args->data[3]); + wasm_val_copy(&results->data[1], &args->data[1]); + wasm_val_copy(&results->data[2], &args->data[2]); + wasm_val_copy(&results->data[3], &args->data[0]); return NULL; } // 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; } @@ -89,9 +90,10 @@ int main(int argc, const char* argv[]) { // Instantiate. printf("Instantiating module...\n"); - const wasm_extern_t* imports[] = {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); + wasm_instance_new(store, module, &imports, NULL); if (!instance) { printf("> Error instantiating module!\n"); return 1; @@ -118,17 +120,15 @@ int main(int argc, const char* argv[]) { // Call. printf("Calling export...\n"); - wasm_val_t args[4]; - args[0].kind = WASM_I32; - args[0].of.i32 = 1; - args[1].kind = WASM_I64; - args[1].of.i64 = 2; - args[2].kind = WASM_I64; - args[2].of.i64 = 3; - args[3].kind = WASM_I32; - args[3].of.i32 = 4; - wasm_val_t results[4]; - if (wasm_func_call(run_func, args, results)) { + wasm_val_t vals[4] = { + WASM_I32_VAL(1), WASM_I32_VAL(2), WASM_I32_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,13 +138,12 @@ int main(int argc, const char* argv[]) { // Print result. printf("Printing result...\n"); printf("> %"PRIu32" %"PRIu64" %"PRIu64" %"PRIu32"\n", - results[0].of.i32, results[1].of.i64, - results[2].of.i64, results[3].of.i32); + res[0].of.i32, res[1].of.i64, res[2].of.i64, res[3].of.i32); - assert(results[0].of.i32 == 4); - assert(results[1].of.i64 == 3); - assert(results[2].of.i64 == 2); - assert(results[3].of.i32 == 1); + 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"); diff --git a/example/multi.cc b/example/multi.cc index 8f7a555..6d08355 100644 --- a/example/multi.cc +++ b/example/multi.cc @@ -8,7 +8,7 @@ // A function to be called from Wasm code. auto callback( - const wasm::Val args[], wasm::Val results[] + const wasm::vec& args, wasm::vec& results ) -> wasm::own { std::cout << "Calling back..." << std::endl; std::cout << "> " << args[0].i32(); @@ -66,7 +66,7 @@ void run() { // Instantiate. std::cout << "Instantiating module..." << std::endl; - wasm::Extern* imports[] = {callback_func.get()}; + auto imports = wasm::vec::make(callback_func.get()); auto instance = wasm::Instance::make(store, module.get(), imports); if (!instance) { std::cout << "> Error instantiating module!" << std::endl; @@ -84,10 +84,10 @@ void run() { // Call. std::cout << "Calling export..." << std::endl; - wasm::Val args[] = { + auto args = wasm::vec::make( wasm::Val::i32(1), wasm::Val::i64(2), wasm::Val::i64(3), wasm::Val::i32(4) - }; - wasm::Val results[4]; + ); + auto results = wasm::vec::make_uninitialized(4); if (wasm::own trap = run_func->call(args, results)) { std::cout << "> Error calling function! " << trap->message().get() << std::endl; exit(1); diff --git a/example/reflect.c b/example/reflect.c index a8cbeed..d438318 100644 --- a/example/reflect.c +++ b/example/reflect.c @@ -118,7 +118,9 @@ int main(int argc, const char* argv[]) { // Instantiate. printf("Instantiating module...\n"); - own wasm_instance_t* instance = wasm_instance_new(store, module, NULL, NULL); + wasm_extern_vec_t imports = WASM_EMPTY_VEC; + own wasm_instance_t* instance = + wasm_instance_new(store, module, &imports, NULL); if (!instance) { printf("> Error instantiating module!\n"); return 1; diff --git a/example/reflect.cc b/example/reflect.cc index 704ae47..9820d81 100644 --- a/example/reflect.cc +++ b/example/reflect.cc @@ -101,7 +101,8 @@ void run() { // Instantiate. std::cout << "Instantiating module..." << std::endl; - auto instance = wasm::Instance::make(store, module.get(), nullptr); + auto imports = wasm::vec::make(); + auto instance = wasm::Instance::make(store, module.get(), imports); if (!instance) { std::cout << "> Error instantiating module!" << std::endl; exit(1); diff --git a/example/serialize.c b/example/serialize.c index 2a5cf5f..8ea1a37 100644 --- a/example/serialize.c +++ b/example/serialize.c @@ -8,7 +8,9 @@ #define own // A function to be called from Wasm code. -own wasm_trap_t* hello_callback(const wasm_val_t args[], wasm_val_t results[]) { +own wasm_trap_t* hello_callback( + const wasm_val_vec_t* args, wasm_val_vec_t* results +) { printf("Calling back...\n"); printf("> Hello World!\n"); return NULL; @@ -76,9 +78,10 @@ int main(int argc, const char* argv[]) { // Instantiate. printf("Instantiating deserialized module...\n"); - const wasm_extern_t* imports[] = { wasm_func_as_extern(hello_func) }; + wasm_extern_t* externs[] = { wasm_func_as_extern(hello_func) }; + wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs); own wasm_instance_t* instance = - wasm_instance_new(store, deserialized, imports, NULL); + wasm_instance_new(store, deserialized, &imports, NULL); if (!instance) { printf("> Error instantiating module!\n"); return 1; @@ -105,7 +108,8 @@ int main(int argc, const char* argv[]) { // Call. printf("Calling export...\n"); - if (wasm_func_call(run_func, NULL, NULL)) { + wasm_val_vec_t empty = WASM_EMPTY_VEC; + if (wasm_func_call(run_func, &empty, &empty)) { printf("> Error calling function!\n"); return 1; } diff --git a/example/serialize.cc b/example/serialize.cc index 3a059b5..f63baeb 100644 --- a/example/serialize.cc +++ b/example/serialize.cc @@ -9,7 +9,7 @@ // A function to be called from Wasm code. auto hello_callback( - const wasm::Val args[], wasm::Val results[] + const wasm::vec& args, wasm::vec& results ) -> wasm::own { std::cout << "Calling back..." << std::endl; std::cout << "> Hello world!" << std::endl; @@ -67,7 +67,7 @@ void run() { // Instantiate. std::cout << "Instantiating deserialized module..." << std::endl; - wasm::Extern* imports[] = {hello_func.get()}; + auto imports = wasm::vec::make(hello_func.get()); auto instance = wasm::Instance::make(store, deserialized.get(), imports); if (!instance) { std::cout << "> Error instantiating module!" << std::endl; @@ -85,7 +85,9 @@ void run() { // Call. std::cout << "Calling export..." << std::endl; - if (run_func->call()) { + auto args = wasm::vec::make(); + auto results = wasm::vec::make(); + if (run_func->call(args, results)) { std::cout << "> Error calling function!" << std::endl; exit(1); } diff --git a/example/start.c b/example/start.c index ce8e8d8..f60c03c 100644 --- a/example/start.c +++ b/example/start.c @@ -54,9 +54,10 @@ int main(int argc, const char* argv[]) { // Instantiate. printf("Instantiating module...\n"); + wasm_extern_vec_t imports = WASM_EMPTY_VEC; own wasm_trap_t* trap = NULL; own wasm_instance_t* instance = - wasm_instance_new(store, module, NULL, &trap); + wasm_instance_new(store, module, &imports, &trap); if (instance || !trap) { printf("> Error instantiating module, expected trap!\n"); return 1; diff --git a/example/start.cc b/example/start.cc index 71d6fd2..1643b9f 100644 --- a/example/start.cc +++ b/example/start.cc @@ -47,7 +47,8 @@ void run() { // Instantiate. std::cout << "Instantiating module..." << std::endl; wasm::own trap; - auto instance = wasm::Instance::make(store, module.get(), nullptr, &trap); + auto imports = wasm::vec::make(); + auto instance = wasm::Instance::make(store, module.get(), imports, &trap); if (instance || !trap) { std::cout << "> Error instantiating module, expected trap!" << std::endl; exit(1); diff --git a/example/table.c b/example/table.c index 66ae13f..5e91a34 100644 --- a/example/table.c +++ b/example/table.c @@ -9,11 +9,11 @@ // A function to be called from Wasm code. own wasm_trap_t* neg_callback( - const wasm_val_t args[], wasm_val_t results[] + const wasm_val_vec_t* args, wasm_val_vec_t* results ) { printf("Calling back...\n"); - results[0].kind = WASM_I32; - results[0].of.i32 = -args[0].of.i32; + results->data[0].kind = WASM_I32; + results->data[0].of.i32 = -args->data[0].of.i32; return NULL; } @@ -49,23 +49,22 @@ void check_table(wasm_table_t* table, int32_t i, bool expect_set) { } void check_call(wasm_func_t* func, int32_t arg1, int32_t arg2, int32_t expected) { - wasm_val_t args[2] = { - {.kind = WASM_I32, .of = {.i32 = arg1}}, - {.kind = WASM_I32, .of = {.i32 = arg2}} - }; - wasm_val_t results[1]; - if (wasm_func_call(func, args, results) || results[0].of.i32 != expected) { + wasm_val_t vs[2] = { WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) }; + wasm_val_t r[1] = { WASM_INIT_VAL }; + wasm_val_vec_t args = WASM_ARRAY_VEC(vs); + wasm_val_vec_t results = WASM_ARRAY_VEC(r); + if (wasm_func_call(func, &args, &results) || r[0].of.i32 != expected) { printf("> Error on result\n"); exit(1); } } void check_trap(wasm_func_t* func, int32_t arg1, int32_t arg2) { - wasm_val_t args[2] = { - {.kind = WASM_I32, .of = {.i32 = arg1}}, - {.kind = WASM_I32, .of = {.i32 = arg2}} - }; - own wasm_trap_t* trap = wasm_func_call(func, args, NULL); + wasm_val_t vs[2] = { WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) }; + wasm_val_t r[1] = { WASM_INIT_VAL }; + wasm_val_vec_t args = WASM_ARRAY_VEC(vs); + wasm_val_vec_t results = WASM_ARRAY_VEC(r); + own wasm_trap_t* trap = wasm_func_call(func, &args, &results); if (! trap) { printf("> Error on result, expected trap\n"); exit(1); @@ -110,7 +109,9 @@ int main(int argc, const char* argv[]) { // Instantiate. printf("Instantiating module...\n"); - own wasm_instance_t* instance = wasm_instance_new(store, module, NULL, NULL); + wasm_extern_vec_t imports = WASM_EMPTY_VEC; + own wasm_instance_t* instance = + wasm_instance_new(store, module, &imports, NULL); if (!instance) { printf("> Error instantiating module!\n"); return 1; diff --git a/example/table.cc b/example/table.cc index 6890ae7..9ddf1d9 100644 --- a/example/table.cc +++ b/example/table.cc @@ -9,7 +9,7 @@ // A function to be called from Wasm code. auto neg_callback( - const wasm::Val args[], wasm::Val results[] + const wasm::vec& args, wasm::vec& results ) -> wasm::own { std::cout << "Calling back..." << std::endl; results[0] = wasm::Val(-args[0].i32()); @@ -51,8 +51,8 @@ void check(bool success) { auto call( const wasm::Func* func, wasm::Val&& arg1, wasm::Val&& arg2 ) -> wasm::Val { - wasm::Val args[2] = {std::move(arg1), std::move(arg2)}; - wasm::Val results[1]; + auto args = wasm::vec::make(std::move(arg1), std::move(arg2)); + auto results = wasm::vec::make_uninitialized(1); if (func->call(args, results)) { std::cout << "> Error on result, expected return" << std::endl; exit(1); @@ -61,8 +61,8 @@ auto call( } void check_trap(const wasm::Func* func, wasm::Val&& arg1, wasm::Val&& arg2) { - wasm::Val args[2] = {std::move(arg1), std::move(arg2)}; - wasm::Val results[1]; + auto args = wasm::vec::make(std::move(arg1), std::move(arg2)); + auto results = wasm::vec::make_uninitialized(1); if (! func->call(args, results)) { std::cout << "> Error on result, expected trap" << std::endl; exit(1); @@ -100,7 +100,8 @@ void run() { // Instantiate. std::cout << "Instantiating module..." << std::endl; - auto instance = wasm::Instance::make(store, module.get(), nullptr); + auto imports = wasm::vec::make(); + auto instance = wasm::Instance::make(store, module.get(), imports); if (!instance) { std::cout << "> Error instantiating module!" << std::endl; exit(1); diff --git a/example/threads.c b/example/threads.c index 409c5fe..d70bd1a 100644 --- a/example/threads.c +++ b/example/threads.c @@ -13,9 +13,9 @@ const int N_THREADS = 10; const int N_REPS = 3; // A function to be called from Wasm code. -own wasm_trap_t* callback(const wasm_val_t args[], wasm_val_t results[]) { - assert(args[0].kind == WASM_I32); - printf("> Thread %d running\n", args[0].of.i32); +own wasm_trap_t* callback(const wasm_val_vec_t* args, wasm_val_vec_t* results) { + assert(args->data[0].kind == WASM_I32); + printf("> Thread %d running\n", args->data[0].of.i32); return NULL; } @@ -42,18 +42,19 @@ void* run(void* args_abs) { own wasm_func_t* func = wasm_func_new(store, func_type, callback); wasm_functype_delete(func_type); - wasm_val_t val = {.kind = WASM_I32, .of = {.i32 = (int32_t)args->id}}; + wasm_val_t val = WASM_I32_VAL((int32_t)args->id); own wasm_globaltype_t* global_type = wasm_globaltype_new(wasm_valtype_new_i32(), WASM_CONST); own wasm_global_t* global = wasm_global_new(store, global_type, &val); wasm_globaltype_delete(global_type); // Instantiate. - const wasm_extern_t* imports[] = { + wasm_extern_t* externs[] = { wasm_func_as_extern(func), wasm_global_as_extern(global), }; + wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs); own wasm_instance_t* instance = - wasm_instance_new(store, module, imports, NULL); + wasm_instance_new(store, module, &imports, NULL); if (!instance) { printf("> Error instantiating module!\n"); return NULL; @@ -78,7 +79,8 @@ void* run(void* args_abs) { wasm_instance_delete(instance); // Call. - if (wasm_func_call(run_func, NULL, NULL)) { + wasm_val_vec_t empty = WASM_EMPTY_VEC; + if (wasm_func_call(run_func, &empty, &empty)) { printf("> Error calling function!\n"); return NULL; } diff --git a/example/threads.cc b/example/threads.cc index 6148be6..e130717 100644 --- a/example/threads.cc +++ b/example/threads.cc @@ -10,7 +10,7 @@ const int N_REPS = 3; // A function to be called from Wasm code. auto callback( - void* env, const wasm::Val args[], wasm::Val results[] + void* env, const wasm::vec& args, wasm::vec& results ) -> wasm::own { assert(args[0].kind() == wasm::ValKind::I32); std::lock_guard lock(*reinterpret_cast(env)); @@ -53,7 +53,7 @@ void run( store, global_type.get(), wasm::Val::i32(i)); // Instantiate. - wasm::Extern* imports[] = {func.get(), global.get()}; + auto imports = wasm::vec::make(func.get(), global.get()); auto instance = wasm::Instance::make(store, module.get(), imports); if (!instance) { std::lock_guard lock(*mutex); @@ -71,7 +71,8 @@ void run( auto run_func = exports[0]->func(); // Call. - run_func->call(); + auto empty = wasm::vec::make(); + run_func->call(empty, empty); } } diff --git a/example/trap.c b/example/trap.c index 5cc3acf..074728e 100644 --- a/example/trap.c +++ b/example/trap.c @@ -9,7 +9,7 @@ // A function to be called from Wasm code. own wasm_trap_t* fail_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 ) { printf("Calling back...\n"); own wasm_name_t message; @@ -75,9 +75,10 @@ int main(int argc, const char* argv[]) { // Instantiate. printf("Instantiating module...\n"); - const wasm_extern_t* imports[] = { wasm_func_as_extern(fail_func) }; + wasm_extern_t* externs[] = { wasm_func_as_extern(fail_func) }; + wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs); own wasm_instance_t* instance = - wasm_instance_new(store, module, imports, NULL); + wasm_instance_new(store, module, &imports, NULL); if (!instance) { printf("> Error instantiating module!\n"); return 1; @@ -106,7 +107,9 @@ int main(int argc, const char* argv[]) { } printf("Calling export %d...\n", i); - own wasm_trap_t* trap = wasm_func_call(func, NULL, NULL); + wasm_val_vec_t args = WASM_EMPTY_VEC; + wasm_val_vec_t results = WASM_EMPTY_VEC; + own wasm_trap_t* trap = wasm_func_call(func, &args, &results); if (!trap) { printf("> Error calling function, expected trap!\n"); return 1; diff --git a/example/trap.cc b/example/trap.cc index 9cfcf50..10763b7 100644 --- a/example/trap.cc +++ b/example/trap.cc @@ -8,7 +8,7 @@ // A function to be called from Wasm code. auto fail_callback( - void* env, const wasm::Val args[], wasm::Val results[] + void* env, const wasm::vec& args, wasm::vec& results ) -> wasm::own { std::cout << "Calling back..." << std::endl; auto store = reinterpret_cast(env); @@ -65,7 +65,7 @@ void run() { // Instantiate. std::cout << "Instantiating module..." << std::endl; - wasm::Extern* imports[] = {fail_func.get()}; + auto imports = wasm::vec::make(fail_func.get()); auto instance = wasm::Instance::make(store, module.get(), imports); if (!instance) { std::cout << "> Error instantiating module!" << std::endl; @@ -85,7 +85,9 @@ void run() { // Call. for (size_t i = 0; i < 2; ++i) { std::cout << "Calling export " << i << "..." << std::endl; - auto trap = exports[i]->func()->call(); + auto args = wasm::vec::make(); + auto results = wasm::vec::make(); + auto trap = exports[i]->func()->call(args, results); if (!trap) { std::cout << "> Error calling function, expected trap!" << std::endl; exit(1); diff --git a/include/wasm.h b/include/wasm.h index 46e75ca..5739556 100644 --- a/include/wasm.h +++ b/include/wasm.h @@ -408,9 +408,9 @@ WASM_API_EXTERN own wasm_module_t* wasm_module_deserialize(wasm_store_t*, const WASM_DECLARE_REF(func) typedef own wasm_trap_t* (*wasm_func_callback_t)( - const wasm_val_t args[], wasm_val_t results[]); + const wasm_val_vec_t* args, own wasm_val_vec_t* results); typedef own wasm_trap_t* (*wasm_func_callback_with_env_t)( - void* env, const wasm_val_t args[], wasm_val_t results[]); + void* env, const wasm_val_vec_t* args, wasm_val_vec_t* results); WASM_API_EXTERN own wasm_func_t* wasm_func_new( wasm_store_t*, const wasm_functype_t*, wasm_func_callback_t); @@ -423,7 +423,7 @@ WASM_API_EXTERN size_t wasm_func_param_arity(const wasm_func_t*); WASM_API_EXTERN size_t wasm_func_result_arity(const wasm_func_t*); WASM_API_EXTERN own wasm_trap_t* wasm_func_call( - const wasm_func_t*, const wasm_val_t args[], wasm_val_t results[]); + const wasm_func_t*, const wasm_val_vec_t* args, wasm_val_vec_t* results); // Global Instances @@ -510,7 +510,7 @@ WASM_API_EXTERN const wasm_memory_t* wasm_extern_as_memory_const(const wasm_exte WASM_DECLARE_REF(instance) WASM_API_EXTERN own wasm_instance_t* wasm_instance_new( - wasm_store_t*, const wasm_module_t*, const wasm_extern_t* const imports[], + wasm_store_t*, const wasm_module_t*, const wasm_extern_vec_t* imports, own wasm_trap_t** ); @@ -520,6 +520,12 @@ WASM_API_EXTERN void wasm_instance_exports(const wasm_instance_t*, own wasm_exte /////////////////////////////////////////////////////////////////////////////// // Convenience +// Vectors + +#define WASM_EMPTY_VEC {0, NULL} +#define WASM_ARRAY_VEC(array) {sizeof(array)/sizeof(*(array)), array} + + // Value Type construction short-hands static inline own wasm_valtype_t* wasm_valtype_new_i32() { @@ -692,6 +698,13 @@ static inline void* wasm_val_ptr(const wasm_val_t* val) { #endif } +#define WASM_I32_VAL(i) {.kind = WASM_I32, .of = {.i32 = i}} +#define WASM_I64_VAL(i) {.kind = WASM_I64, .of = {.i64 = i}} +#define WASM_F32_VAL(z) {.kind = WASM_F32, .of = {.f32 = z}} +#define WASM_F64_VAL(z) {.kind = WASM_F64, .of = {.f64 = z}} +#define WASM_REF_VAL(r) {.kind = WASM_ANYREF, .of = {.ref = r}} +#define WASM_INIT_VAL {.kind = WASM_ANYREF, .of = {.ref = NULL}} + /////////////////////////////////////////////////////////////////////////////// diff --git a/include/wasm.hh b/include/wasm.hh index 29b3798..1e46b3b 100644 --- a/include/wasm.hh +++ b/include/wasm.hh @@ -431,11 +431,11 @@ class Val { public: Val() : kind_(ValKind::ANYREF) { impl_.ref = nullptr; } - Val(int32_t i) : kind_(ValKind::I32) { impl_.i32 = i; } - Val(int64_t i) : kind_(ValKind::I64) { impl_.i64 = i; } - Val(float32_t z) : kind_(ValKind::F32) { impl_.f32 = z; } - Val(float64_t z) : kind_(ValKind::F64) { impl_.f64 = z; } - Val(own&& r) : kind_(ValKind::ANYREF) { impl_.ref = r.release(); } + explicit Val(int32_t i) : kind_(ValKind::I32) { impl_.i32 = i; } + explicit Val(int64_t i) : kind_(ValKind::I64) { impl_.i64 = i; } + explicit Val(float32_t z) : kind_(ValKind::F32) { impl_.f32 = z; } + explicit Val(float64_t z) : kind_(ValKind::F64) { impl_.f64 = z; } + explicit Val(own&& r) : kind_(ValKind::ANYREF) { impl_.ref = r.release(); } Val(Val&& that) : kind_(that.kind_), impl_(that.impl_) { if (is_ref()) that.impl_.ref = nullptr; @@ -648,8 +648,8 @@ public: Func() = delete; ~Func(); - using callback = auto (*)(const Val[], Val[]) -> own; - using callback_with_env = auto (*)(void*, const Val[], Val[]) -> own; + using callback = auto (*)(const vec&, vec&) -> own; + using callback_with_env = auto (*)(void*, const vec&, vec&) -> own; static auto make(Store*, const FuncType*, callback) -> own; static auto make(Store*, const FuncType*, callback_with_env, @@ -660,7 +660,7 @@ public: auto param_arity() const -> size_t; auto result_arity() const -> size_t; - auto call(const Val[] = nullptr, Val[] = nullptr) const -> own; + auto call(const vec&, vec&) const -> own; }; @@ -731,7 +731,7 @@ public: ~Instance(); static auto make( - Store*, const Module*, const Extern* const[], own* = nullptr + Store*, const Module*, const vec&, own* = nullptr ) -> own; auto copy() const -> own; diff --git a/src/wasm-c.cc b/src/wasm-c.cc index 982cc80..b431611 100644 --- a/src/wasm-c.cc +++ b/src/wasm-c.cc @@ -60,7 +60,7 @@ struct borrowed_vec { // Vectors -#define WASM_DEFINE_VEC_BASE(name, Name, vec, ptr_or_none) \ +#define WASM_DEFINE_VEC_BASE(name, Name, vec, plainvec, ptr_or_none) \ static_assert( \ sizeof(wasm_##name##_vec_t) == sizeof(vec), \ "C/C++ incompatibility" \ @@ -86,6 +86,14 @@ struct borrowed_vec { -> wasm_##name##_t ptr_or_none const* { \ return reinterpret_cast(v); \ } \ + extern "C++" inline auto reveal_##name##_vec(wasm_##name##_vec_t* v) \ + -> plainvec* { \ + return reinterpret_cast*>(v); \ + } \ + extern "C++" inline auto reveal_##name##_vec(const wasm_##name##_vec_t* v) \ + -> const plainvec* { \ + return reinterpret_cast*>(v); \ + } \ extern "C++" inline auto reveal_##name##_vec(wasm_##name##_t ptr_or_none* v) \ -> vec::elem_type* { \ return reinterpret_cast::elem_type*>(v); \ @@ -134,7 +142,7 @@ struct borrowed_vec { // Vectors with no ownership management of elements #define WASM_DEFINE_VEC_PLAIN(name, Name) \ - WASM_DEFINE_VEC_BASE(name, Name, vec, ) \ + WASM_DEFINE_VEC_BASE(name, Name, vec, vec, ) \ \ void wasm_##name##_vec_new( \ wasm_##name##_vec_t* out, \ @@ -156,7 +164,7 @@ struct borrowed_vec { // Vectors that own their elements #define WASM_DEFINE_VEC_OWN(name, Name) \ - WASM_DEFINE_VEC_BASE(name, Name, ownvec, *) \ + WASM_DEFINE_VEC_BASE(name, Name, ownvec, vec, *) \ \ void wasm_##name##_vec_new( \ wasm_##name##_vec_t* out, \ @@ -620,7 +628,7 @@ inline auto borrow_val(const wasm_val_t* v) -> borrowed_val { } // extern "C++" -WASM_DEFINE_VEC_BASE(val, Val, vec, ) +WASM_DEFINE_VEC_BASE(val, Val, vec, vec, ) void wasm_val_vec_new( wasm_val_vec_t* out, size_t size, wasm_val_t const data[] @@ -771,7 +779,9 @@ WASM_DEFINE_REF(func, Func) extern "C++" { -auto wasm_callback(void* env, const Val args[], Val results[]) -> own { +auto wasm_callback( + void* env, const vec& args, vec& results +) -> own { auto f = reinterpret_cast(env); return adopt_trap(f(hide_val_vec(args), hide_val_vec(results))); } @@ -783,7 +793,7 @@ struct wasm_callback_env_t { }; auto wasm_callback_with_env( - void* env, const Val args[], Val results[] + void* env, const vec& args, vec& results ) -> own { auto t = static_cast(env); return adopt_trap(t->callback(t->env, hide_val_vec(args), hide_val_vec(results))); @@ -826,9 +836,11 @@ size_t wasm_func_result_arity(const wasm_func_t* func) { } wasm_trap_t* wasm_func_call( - const wasm_func_t* func, const wasm_val_t args[], wasm_val_t results[] + const wasm_func_t* func, const wasm_val_vec_t* args, wasm_val_vec_t* results ) { - return release_trap(func->call(reveal_val_vec(args), reveal_val_vec(results))); + auto args_ = borrow_val_vec(args); + auto results_ = borrow_val_vec(results); + return release_trap(func->call(args_.it, results_.it)); } @@ -995,12 +1007,13 @@ WASM_DEFINE_REF(instance, Instance) wasm_instance_t* wasm_instance_new( wasm_store_t* store, const wasm_module_t* module, - const wasm_extern_t* const imports[], + const wasm_extern_vec_t* imports, wasm_trap_t** trap ) { own error; - auto instance = release_instance(Instance::make(store, module, - reinterpret_cast(imports), &error)); + auto imports_ = reveal_extern_vec(imports); + auto instance = + release_instance(Instance::make(store, module, *imports_, &error)); if (trap) *trap = hide_trap(error.release()); return instance; } diff --git a/src/wasm-v8.cc b/src/wasm-v8.cc index 75b9e66..91ce7ec 100644 --- a/src/wasm-v8.cc +++ b/src/wasm-v8.cc @@ -229,6 +229,7 @@ DEFINE_VEC(Global, ownvec, GLOBAL) DEFINE_VEC(Table, ownvec, TABLE) DEFINE_VEC(Memory, ownvec, MEMORY) DEFINE_VEC(Extern, ownvec, EXTERN) +DEFINE_VEC(Extern*, vec, EXTERN) DEFINE_VEC(Val, vec, VAL) #endif // #ifdef WASM_API_DEBUG @@ -1687,7 +1688,7 @@ auto Func::result_arity() const -> size_t { return wasm_v8::func_type_result_arity(impl(this)->v8_object()); } -auto Func::call(const Val args[], Val results[]) const -> own { +auto Func::call(const vec& args, vec& results) const -> own { auto func = impl(this); auto store = func->store(); auto isolate = store->isolate(); @@ -1754,17 +1755,17 @@ void FuncData::v8_callback(const v8::FunctionCallbackInfo& info) { assert(param_types.size() == info.Length()); // TODO: cache params and result arrays per thread. - auto args = std::unique_ptr(new Val[param_types.size()]); - auto results = std::unique_ptr(new Val[result_types.size()]); + auto args = vec::make_uninitialized(param_types.size()); + auto results = vec::make_uninitialized(result_types.size()); for (size_t i = 0; i < param_types.size(); ++i) { args[i] = v8_to_val(store, info[i], param_types[i].get()); } own trap; if (self->kind == CALLBACK_WITH_ENV) { - trap = self->callback_with_env(self->env, args.get(), results.get()); + trap = self->callback_with_env(self->env, args, results); } else { - trap = self->callback(args.get(), results.get()); + trap = self->callback(args, results); } if (trap) { @@ -2019,7 +2020,7 @@ auto Instance::copy() const -> own { } auto Instance::make( - Store* store_abs, const Module* module_abs, const Extern* const imports[], + Store* store_abs, const Module* module_abs, const vec& imports, own* trap ) -> own { auto store = impl(store_abs);