Skip to content

Commit af6e0fb

Browse files
authored
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
1 parent 340fd95 commit af6e0fb

30 files changed

+318
-247
lines changed

example/callback.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,27 @@ void wasm_val_print(wasm_val_t val) {
3535

3636
// A function to be called from Wasm code.
3737
own wasm_trap_t* print_callback(
38-
const wasm_val_t args[], wasm_val_t results[]
38+
const wasm_val_vec_t* args, wasm_val_vec_t* results
3939
) {
4040
printf("Calling back...\n> ");
41-
wasm_val_print(args[0]);
41+
wasm_val_print(args->data[0]);
4242
printf("\n");
4343

44-
wasm_val_copy(&results[0], &args[0]);
44+
wasm_val_copy(&results->data[0], &args->data[0]);
4545
return NULL;
4646
}
4747

4848

4949
// A function closure.
5050
own wasm_trap_t* closure_callback(
51-
void* env, const wasm_val_t args[], wasm_val_t results[]
51+
void* env, const wasm_val_vec_t* args, wasm_val_vec_t* results
5252
) {
5353
int i = *(int*)env;
5454
printf("Calling back closure...\n");
5555
printf("> %d\n", i);
5656

57-
results[0].kind = WASM_I32;
58-
results[0].of.i32 = (int32_t)i;
57+
results->data[0].kind = WASM_I32;
58+
results->data[0].of.i32 = (int32_t)i;
5959
return NULL;
6060
}
6161

@@ -108,11 +108,12 @@ int main(int argc, const char* argv[]) {
108108

109109
// Instantiate.
110110
printf("Instantiating module...\n");
111-
const wasm_extern_t* imports[] = {
111+
wasm_extern_t* externs[] = {
112112
wasm_func_as_extern(print_func), wasm_func_as_extern(closure_func)
113113
};
114+
wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
114115
own wasm_instance_t* instance =
115-
wasm_instance_new(store, module, imports, NULL);
116+
wasm_instance_new(store, module, &imports, NULL);
116117
if (!instance) {
117118
printf("> Error instantiating module!\n");
118119
return 1;
@@ -140,13 +141,11 @@ int main(int argc, const char* argv[]) {
140141

141142
// Call.
142143
printf("Calling export...\n");
143-
wasm_val_t args[2];
144-
args[0].kind = WASM_I32;
145-
args[0].of.i32 = 3;
146-
args[1].kind = WASM_I32;
147-
args[1].of.i32 = 4;
148-
wasm_val_t results[1];
149-
if (wasm_func_call(run_func, args, results)) {
144+
wasm_val_t as[2] = { WASM_I32_VAL(3), WASM_I32_VAL(4) };
145+
wasm_val_t rs[1] = { WASM_INIT_VAL };
146+
wasm_val_vec_t args = WASM_ARRAY_VEC(as);
147+
wasm_val_vec_t results = WASM_ARRAY_VEC(rs);
148+
if (wasm_func_call(run_func, &args, &results)) {
150149
printf("> Error calling function!\n");
151150
return 1;
152151
}
@@ -155,7 +154,7 @@ int main(int argc, const char* argv[]) {
155154

156155
// Print result.
157156
printf("Printing result...\n");
158-
printf("> %u\n", results[0].of.i32);
157+
printf("> %u\n", rs[0].of.i32);
159158

160159
// Shut down.
161160
printf("Shutting down...\n");

example/callback.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ auto operator<<(std::ostream& out, const wasm::Val& val) -> std::ostream& {
3535

3636
// A function to be called from Wasm code.
3737
auto print_callback(
38-
const wasm::Val args[], wasm::Val results[]
38+
const wasm::vec<wasm::Val>& args, wasm::vec<wasm::Val>& results
3939
) -> wasm::own<wasm::Trap> {
4040
std::cout << "Calling back..." << std::endl << "> " << args[0] << std::endl;
4141
results[0] = args[0].copy();
@@ -45,7 +45,7 @@ auto print_callback(
4545

4646
// A function closure.
4747
auto closure_callback(
48-
void* env, const wasm::Val args[], wasm::Val results[]
48+
void* env, const wasm::vec<wasm::Val>& args, wasm::vec<wasm::Val>& results
4949
) -> wasm::own<wasm::Trap> {
5050
auto i = *reinterpret_cast<int*>(env);
5151
std::cout << "Calling back closure..." << std::endl;
@@ -103,7 +103,8 @@ void run() {
103103

104104
// Instantiate.
105105
std::cout << "Instantiating module..." << std::endl;
106-
wasm::Extern* imports[] = {print_func.get(), closure_func.get()};
106+
auto imports = wasm::vec<wasm::Extern*>::make(
107+
print_func.get(), closure_func.get());
107108
auto instance = wasm::Instance::make(store, module.get(), imports);
108109
if (!instance) {
109110
std::cout << "> Error instantiating module!" << std::endl;
@@ -121,8 +122,8 @@ void run() {
121122

122123
// Call.
123124
std::cout << "Calling export..." << std::endl;
124-
wasm::Val args[] = {wasm::Val::i32(3), wasm::Val::i32(4)};
125-
wasm::Val results[1];
125+
auto args = wasm::vec<wasm::Val>::make(wasm::Val::i32(3), wasm::Val::i32(4));
126+
auto results = wasm::vec<wasm::Val>::make_uninitialized(1);
126127
if (run_func->call(args, results)) {
127128
std::cout << "> Error calling function!" << std::endl;
128129
exit(1);

example/finalize.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ void run_in_store(wasm_store_t* store) {
5050
printf("Instantiating modules...\n");
5151
for (int i = 0; i <= iterations; ++i) {
5252
if (i % (iterations / 10) == 0) printf("%d\n", i);
53+
wasm_extern_vec_t imports = WASM_EMPTY_VEC;
5354
own wasm_instance_t* instance =
54-
wasm_instance_new(store, module, NULL, NULL);
55+
wasm_instance_new(store, module, &imports, NULL);
5556
if (!instance) {
5657
printf("> Error instantiating module %d!\n", i);
5758
exit(1);

example/finalize.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ void run_in_store(wasm::Store* store) {
4646
std::cout << "Instantiating modules..." << std::endl;
4747
for (int i = 0; i <= iterations; ++i) {
4848
if (i % (iterations / 10) == 0) std::cout << i << std::endl;
49-
auto instance = wasm::Instance::make(store, module.get(), nullptr);
49+
auto imports = wasm::vec<wasm::Extern*>::make();
50+
auto instance = wasm::Instance::make(store, module.get(), imports);
5051
if (!instance) {
5152
std::cout << "> Error instantiating module " << i << "!" << std::endl;
5253
exit(1);

example/global.c

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
3939

4040
#define check_call(func, type, expected) \
4141
{ \
42-
wasm_val_t results[1]; \
43-
wasm_func_call(func, NULL, results); \
44-
check(results[0], type, expected); \
42+
wasm_val_t vs[1]; \
43+
wasm_val_vec_t args = WASM_EMPTY_VEC; \
44+
wasm_val_vec_t results = WASM_ARRAY_VEC(vs); \
45+
wasm_func_call(func, &args, &results); \
46+
check(vs[0], type, expected); \
4547
}
4648

4749

@@ -90,16 +92,16 @@ int main(int argc, const char* argv[]) {
9092
own wasm_globaltype_t* var_i64_type = wasm_globaltype_new(
9193
wasm_valtype_new(WASM_I64), WASM_VAR);
9294

93-
wasm_val_t val_f32_1 = {.kind = WASM_F32, .of = {.f32 = 1}};
95+
wasm_val_t val_f32_1 = WASM_F32_VAL(1);
9496
own wasm_global_t* const_f32_import =
9597
wasm_global_new(store, const_f32_type, &val_f32_1);
96-
wasm_val_t val_i64_2 = {.kind = WASM_I64, .of = {.i64 = 2}};
98+
wasm_val_t val_i64_2 = WASM_I64_VAL(2);
9799
own wasm_global_t* const_i64_import =
98100
wasm_global_new(store, const_i64_type, &val_i64_2);
99-
wasm_val_t val_f32_3 = {.kind = WASM_F32, .of = {.f32 = 3}};
101+
wasm_val_t val_f32_3 = WASM_F32_VAL(3);
100102
own wasm_global_t* var_f32_import =
101103
wasm_global_new(store, var_f32_type, &val_f32_3);
102-
wasm_val_t val_i64_4 = {.kind = WASM_I64, .of = {.i64 = 4}};
104+
wasm_val_t val_i64_4 = WASM_I64_VAL(4);
103105
own wasm_global_t* var_i64_import =
104106
wasm_global_new(store, var_i64_type, &val_i64_4);
105107

@@ -110,14 +112,15 @@ int main(int argc, const char* argv[]) {
110112

111113
// Instantiate.
112114
printf("Instantiating module...\n");
113-
const wasm_extern_t* imports[] = {
115+
wasm_extern_t* externs[] = {
114116
wasm_global_as_extern(const_f32_import),
115117
wasm_global_as_extern(const_i64_import),
116118
wasm_global_as_extern(var_f32_import),
117119
wasm_global_as_extern(var_i64_import)
118120
};
121+
wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
119122
own wasm_instance_t* instance =
120-
wasm_instance_new(store, module, imports, NULL);
123+
wasm_instance_new(store, module, &imports, NULL);
121124
if (!instance) {
122125
printf("> Error instantiating module!\n");
123126
return 1;
@@ -175,13 +178,13 @@ int main(int argc, const char* argv[]) {
175178
check_call(get_var_i64_export, i64, 8);
176179

177180
// Modify variables through API and check again.
178-
wasm_val_t val33 = {.kind = WASM_F32, .of = {.f32 = 33}};
181+
wasm_val_t val33 = WASM_F32_VAL(33);
179182
wasm_global_set(var_f32_import, &val33);
180-
wasm_val_t val34 = {.kind = WASM_I64, .of = {.i64 = 34}};
183+
wasm_val_t val34 = WASM_I64_VAL(34);
181184
wasm_global_set(var_i64_import, &val34);
182-
wasm_val_t val37 = {.kind = WASM_F32, .of = {.f32 = 37}};
185+
wasm_val_t val37 = WASM_F32_VAL(37);
183186
wasm_global_set(var_f32_export, &val37);
184-
wasm_val_t val38 = {.kind = WASM_I64, .of = {.i64 = 38}};
187+
wasm_val_t val38 = WASM_I64_VAL(38);
185188
wasm_global_set(var_i64_export, &val38);
186189

187190
check_global(var_f32_import, f32, 33);
@@ -195,14 +198,19 @@ int main(int argc, const char* argv[]) {
195198
check_call(get_var_i64_export, i64, 38);
196199

197200
// Modify variables through calls and check again.
198-
wasm_val_t args73[] = { {.kind = WASM_F32, .of = {.f32 = 73}} };
199-
wasm_func_call(set_var_f32_import, args73, NULL);
200-
wasm_val_t args74[] = { {.kind = WASM_I64, .of = {.i64 = 74}} };
201-
wasm_func_call(set_var_i64_import, args74, NULL);
202-
wasm_val_t args77[] = { {.kind = WASM_F32, .of = {.f32 = 77}} };
203-
wasm_func_call(set_var_f32_export, args77, NULL);
204-
wasm_val_t args78[] = { {.kind = WASM_I64, .of = {.i64 = 78}} };
205-
wasm_func_call(set_var_i64_export, args78, NULL);
201+
wasm_val_vec_t res = WASM_EMPTY_VEC;
202+
wasm_val_t vs73[] = { WASM_F32_VAL(73) };
203+
wasm_val_vec_t args73 = WASM_ARRAY_VEC(vs73);
204+
wasm_func_call(set_var_f32_import, &args73, &res);
205+
wasm_val_t vs74[] = { WASM_I64_VAL(74) };
206+
wasm_val_vec_t args74 = WASM_ARRAY_VEC(vs74);
207+
wasm_func_call(set_var_i64_import, &args74, &res);
208+
wasm_val_t vs77[] = { WASM_F32_VAL(77) };
209+
wasm_val_vec_t args77 = WASM_ARRAY_VEC(vs77);
210+
wasm_func_call(set_var_f32_export, &args77, &res);
211+
wasm_val_t vs78[] = { WASM_I64_VAL(78) };
212+
wasm_val_vec_t args78 = WASM_ARRAY_VEC(vs78);
213+
wasm_func_call(set_var_i64_export, &args78, &res);
206214

207215
check_global(var_f32_import, f32, 73);
208216
check_global(var_i64_import, i64, 74);

example/global.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,19 @@ void check(T actual, U expected) {
3232
}
3333

3434
auto call(const wasm::Func* func) -> wasm::Val {
35-
wasm::Val results[1];
36-
if (func->call(nullptr, results)) {
35+
auto args = wasm::vec<wasm::Val>::make();
36+
auto results = wasm::vec<wasm::Val>::make_uninitialized(1);
37+
if (func->call(args, results)) {
3738
std::cout << "> Error calling function!" << std::endl;
3839
exit(1);
3940
}
4041
return results[0].copy();
4142
}
4243

4344
void call(const wasm::Func* func, wasm::Val&& arg) {
44-
wasm::Val args[1] = {std::move(arg)};
45-
if (func->call(args)) {
45+
auto args = wasm::vec<wasm::Val>::make(std::move(arg));
46+
auto results = wasm::vec<wasm::Val>::make();
47+
if (func->call(args, results)) {
4648
std::cout << "> Error calling function!" << std::endl;
4749
exit(1);
4850
}
@@ -95,10 +97,10 @@ void run() {
9597

9698
// Instantiate.
9799
std::cout << "Instantiating module..." << std::endl;
98-
wasm::Extern* imports[] = {
100+
auto imports = wasm::vec<wasm::Extern*>::make(
99101
const_f32_import.get(), const_i64_import.get(),
100102
var_f32_import.get(), var_i64_import.get()
101-
};
103+
);
102104
auto instance = wasm::Instance::make(store, module.get(), imports);
103105
if (!instance) {
104106
std::cout << "> Error instantiating module!" << std::endl;

example/hello.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
// A function to be called from Wasm code.
1111
own wasm_trap_t* hello_callback(
12-
const wasm_val_t args[], wasm_val_t results[]
12+
const wasm_val_vec_t* args, wasm_val_vec_t* results
1313
) {
1414
printf("Calling back...\n");
1515
printf("> Hello World!\n");
@@ -61,9 +61,10 @@ int main(int argc, const char* argv[]) {
6161

6262
// Instantiate.
6363
printf("Instantiating module...\n");
64-
const wasm_extern_t* imports[] = { wasm_func_as_extern(hello_func) };
64+
wasm_extern_t* externs[] = { wasm_func_as_extern(hello_func) };
65+
wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
6566
own wasm_instance_t* instance =
66-
wasm_instance_new(store, module, imports, NULL);
67+
wasm_instance_new(store, module, &imports, NULL);
6768
if (!instance) {
6869
printf("> Error instantiating module!\n");
6970
return 1;
@@ -90,7 +91,9 @@ int main(int argc, const char* argv[]) {
9091

9192
// Call.
9293
printf("Calling export...\n");
93-
if (wasm_func_call(run_func, NULL, NULL)) {
94+
wasm_val_vec_t args = WASM_EMPTY_VEC;
95+
wasm_val_vec_t results = WASM_EMPTY_VEC;
96+
if (wasm_func_call(run_func, &args, &results)) {
9497
printf("> Error calling function!\n");
9598
return 1;
9699
}

example/hello.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
// A function to be called from Wasm code.
1111
auto hello_callback(
12-
const wasm::Val args[], wasm::Val results[]
12+
const wasm::vec<wasm::Val>& args, wasm::vec<wasm::Val>& results
1313
) -> wasm::own<wasm::Trap> {
1414
std::cout << "Calling back..." << std::endl;
1515
std::cout << "> Hello world!" << std::endl;
@@ -55,7 +55,7 @@ void run() {
5555

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

7474
// Call.
7575
std::cout << "Calling export..." << std::endl;
76-
if (run_func->call()) {
76+
auto args = wasm::vec<wasm::Val>::make();
77+
auto results = wasm::vec<wasm::Val>::make();
78+
if (run_func->call(args, results)) {
7779
std::cout << "> Error calling function!" << std::endl;
7880
exit(1);
7981
}

0 commit comments

Comments
 (0)