Skip to content

Commit

Permalink
Use vectors for calls and imports (#145)
Browse files Browse the repository at this point in the history
* Use vectors for calls and instantiate

* Macros; fix val initialisation in examples

* Add non-own vec of Extern for instantiate
  • Loading branch information
rossberg authored Aug 6, 2020
1 parent 340fd95 commit af6e0fb
Show file tree
Hide file tree
Showing 30 changed files with 318 additions and 247 deletions.
31 changes: 15 additions & 16 deletions example/callback.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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");
Expand Down
11 changes: 6 additions & 5 deletions example/callback.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<wasm::Val>& args, wasm::vec<wasm::Val>& results
) -> wasm::own<wasm::Trap> {
std::cout << "Calling back..." << std::endl << "> " << args[0] << std::endl;
results[0] = args[0].copy();
Expand All @@ -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<wasm::Val>& args, wasm::vec<wasm::Val>& results
) -> wasm::own<wasm::Trap> {
auto i = *reinterpret_cast<int*>(env);
std::cout << "Calling back closure..." << std::endl;
Expand Down Expand Up @@ -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<wasm::Extern*>::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;
Expand All @@ -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<wasm::Val>::make(wasm::Val::i32(3), wasm::Val::i32(4));
auto results = wasm::vec<wasm::Val>::make_uninitialized(1);
if (run_func->call(args, results)) {
std::cout << "> Error calling function!" << std::endl;
exit(1);
Expand Down
3 changes: 2 additions & 1 deletion example/finalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion example/finalize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<wasm::Extern*>::make();
auto instance = wasm::Instance::make(store, module.get(), imports);
if (!instance) {
std::cout << "> Error instantiating module " << i << "!" << std::endl;
exit(1);
Expand Down
50 changes: 29 additions & 21 deletions example/global.c
Original file line number Diff line number Diff line change
Expand Up @@ -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); \
}


Expand Down Expand Up @@ -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);

Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
14 changes: 8 additions & 6 deletions example/global.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ 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<wasm::Val>::make();
auto results = wasm::vec<wasm::Val>::make_uninitialized(1);
if (func->call(args, results)) {
std::cout << "> Error calling function!" << std::endl;
exit(1);
}
return results[0].copy();
}

void call(const wasm::Func* func, wasm::Val&& arg) {
wasm::Val args[1] = {std::move(arg)};
if (func->call(args)) {
auto args = wasm::vec<wasm::Val>::make(std::move(arg));
auto results = wasm::vec<wasm::Val>::make();
if (func->call(args, results)) {
std::cout << "> Error calling function!" << std::endl;
exit(1);
}
Expand Down Expand Up @@ -95,10 +97,10 @@ void run() {

// Instantiate.
std::cout << "Instantiating module..." << std::endl;
wasm::Extern* imports[] = {
auto imports = wasm::vec<wasm::Extern*>::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;
Expand Down
11 changes: 7 additions & 4 deletions example/hello.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
8 changes: 5 additions & 3 deletions example/hello.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<wasm::Val>& args, wasm::vec<wasm::Val>& results
) -> wasm::own<wasm::Trap> {
std::cout << "Calling back..." << std::endl;
std::cout << "> Hello world!" << std::endl;
Expand Down Expand Up @@ -55,7 +55,7 @@ void run() {

// Instantiate.
std::cout << "Instantiating module..." << std::endl;
wasm::Extern* imports[] = {hello_func.get()};
auto imports = wasm::vec<wasm::Extern*>::make(hello_func.get());
auto instance = wasm::Instance::make(store, module.get(), imports);
if (!instance) {
std::cout << "> Error instantiating module!" << std::endl;
Expand All @@ -73,7 +73,9 @@ void run() {

// Call.
std::cout << "Calling export..." << std::endl;
if (run_func->call()) {
auto args = wasm::vec<wasm::Val>::make();
auto results = wasm::vec<wasm::Val>::make();
if (run_func->call(args, results)) {
std::cout << "> Error calling function!" << std::endl;
exit(1);
}
Expand Down
Loading

0 comments on commit af6e0fb

Please sign in to comment.