Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use vectors for calls and imports #145

Merged
merged 3 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 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 = { 2, 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 @@ -146,7 +147,9 @@ int main(int argc, const char* argv[]) {
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_vec_t args_ = {2, args};
wasm_val_vec_t results_ = {1, results};
if (wasm_func_call(run_func, &args_, &results_)) {
printf("> Error calling function!\n");
return 1;
}
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::ownvec<wasm::Extern>::make(
move(print_func), move(closure_func));
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 = {0, NULL};
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::ownvec<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
34 changes: 21 additions & 13 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 = {0, NULL}; \
wasm_val_vec_t results = {1, vs}; \
wasm_func_call(func, &args, &results); \
check(vs[0], type, expected); \
}


Expand Down Expand Up @@ -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 = {4, 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 @@ -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 = {0, NULL};
wasm_val_t vs73[] = { {.kind = WASM_F32, .of = {.f32 = 73}} };
wasm_val_vec_t args73 = {1, vs73};
wasm_func_call(set_var_f32_import, &args73, &res);
wasm_val_t vs74[] = { {.kind = WASM_I64, .of = {.i64 = 74}} };
wasm_val_vec_t args74 = {1, vs74};
wasm_func_call(set_var_i64_import, &args74, &res);
wasm_val_t vs77[] = { {.kind = WASM_F32, .of = {.f32 = 77}} };
wasm_val_vec_t args77 = {1, vs77};
wasm_func_call(set_var_f32_export, &args77, &res);
wasm_val_t vs78[] = { {.kind = WASM_I64, .of = {.i64 = 78}} };
wasm_val_vec_t args78 = {1, 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
18 changes: 10 additions & 8 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[] = {
const_f32_import.get(), const_i64_import.get(),
var_f32_import.get(), var_i64_import.get()
};
auto imports = wasm::ownvec<wasm::Extern>::make(
const_f32_import->copy(), const_i64_import->copy(),
var_f32_import->copy(), var_i64_import->copy()
);
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 = { 1, 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 = {0, NULL};
wasm_val_vec_t results = {0, NULL};
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::ownvec<wasm::Extern>::make(move(hello_func));
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