Skip to content

Commit ef5751e

Browse files
committed
chore: More wit -> wamr thunking
Signed-off-by: Gordon Smith <GordonJSmith@gmail.com>
1 parent 4de8818 commit ef5751e

File tree

3 files changed

+129
-18
lines changed

3 files changed

+129
-18
lines changed

samples/wamr/main.cpp

Lines changed: 115 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,74 @@ std::pair<void *, size_t> convert(void *dest, uint32_t dest_byte_len, const void
5151
assert(false);
5252
}
5353

54-
int main()
54+
std::vector<wasm_val_t> toWamr(const WasmValVector &values)
55+
{
56+
std::vector<wasm_val_t> result(values.size());
57+
for (size_t i = 0; i < values.size(); i++)
58+
{
59+
switch (values[i].index())
60+
{
61+
case WASM_I32:
62+
result[i].kind = WASM_I32;
63+
result[i].of.i32 = std::get<int32_t>(values[i]);
64+
break;
65+
case WASM_I64:
66+
result[i].kind = WASM_I64;
67+
result[i].of.i64 = std::get<int64_t>(values[i]);
68+
break;
69+
case WASM_F32:
70+
result[i].kind = WASM_F32;
71+
result[i].of.f32 = std::get<float>(values[i]);
72+
break;
73+
case WASM_F64:
74+
result[i].kind = WASM_F64;
75+
result[i].of.f64 = std::get<double>(values[i]);
76+
break;
77+
default:
78+
assert(false);
79+
}
80+
}
81+
return result;
82+
}
83+
84+
template <typename T>
85+
WasmValVector fromWamr(size_t count, const wasm_val_t *values)
5586
{
56-
std::cout << "Hello, World!" << std::endl;
87+
auto size = ValTrait<T>::size;
88+
auto alignment = ValTrait<T>::alignment;
89+
auto target_count = ValTrait<T>::flat_types.size();
5790

91+
WasmValVector result(count);
92+
for (size_t i = 0; i < count; i++)
93+
{
94+
switch (values[i].kind)
95+
{
96+
case WASM_I32:
97+
result[i] = values[i].of.i32;
98+
break;
99+
case WASM_I64:
100+
result[i] = values[i].of.i64;
101+
break;
102+
case WASM_F32:
103+
result[i] = values[i].of.f32;
104+
break;
105+
case WASM_F64:
106+
result[i] = values[i].of.f64;
107+
break;
108+
default:
109+
std::cout << "values[i].kind: " << values[i].kind << std::endl;
110+
assert(false);
111+
}
112+
}
113+
return result;
114+
}
115+
116+
int main()
117+
{
58118
char *buffer, error_buf[128];
59119
wasm_module_t module;
60120
wasm_module_inst_t module_inst;
61-
wasm_function_inst_t cabi_realloc, func;
121+
wasm_function_inst_t cabi_realloc;
62122
wasm_exec_env_t exec_env;
63123
uint32_t size, stack_size = 8092, heap_size = 8092;
64124

@@ -104,19 +164,59 @@ int main()
104164
.post_return = nullptr,
105165
};
106166

107-
func = wasm_runtime_lookup_function(module_inst, "example:sample/boolean#and");
108-
109-
// bool_t arg1 = true;
110-
// bool_t arg2 = false;
111-
// auto args_flat = cmcpp::lower_flat(callContext, args);
112-
uint32_t argv[2] = {true, false};
167+
auto and_func = wasm_runtime_lookup_function(module_inst, "example:sample/booleans#and");
168+
auto call_and = [&](bool_t input1, bool_t input2)
169+
{
170+
using inputs_t = tuple_t<bool_t, bool_t>;
171+
using outputs_t = bool_t;
172+
173+
auto inputs = toWamr(lower_flat(callContext, inputs_t{input1, input2}));
174+
auto output_size = 1;
175+
wasm_val_t outputs[output_size];
176+
auto call_result = wasm_runtime_call_wasm_a(exec_env, and_func, output_size, outputs, inputs.size(), inputs.data());
177+
auto result = lift_flat<outputs_t>(callContext, fromWamr<outputs_t>(output_size, outputs));
178+
std::cout << "and_func(" << input1 << ", " << input2 << "): " << result << std::endl;
179+
return result;
180+
};
181+
call_and(false, false);
182+
call_and(false, true);
183+
call_and(true, false);
184+
call_and(true, true);
185+
186+
auto reverse_func = wasm_runtime_lookup_function(module_inst, "example:sample/strings#reverse");
187+
auto reverse_cleanup_func = wasm_runtime_lookup_function(module_inst, "cabi_post_example:sample/strings#reverse");
188+
auto call_reverse = [&](const std::string &input1)
189+
{
190+
using inputs_t = string_t;
191+
using outputs_t = string_t;
192+
193+
auto and_inputs = toWamr(lower_flat(callContext, inputs_t{input1}));
194+
auto output_size = 1;
195+
wasm_val_t outputs[output_size];
196+
auto call_result = wasm_runtime_call_wasm_a(exec_env, reverse_func, output_size, outputs, and_inputs.size(), and_inputs.data());
197+
auto result = load<outputs_t>(callContext, outputs[0].of.i32);
198+
std::cout << "reverse_string(" << input1 << "): " << result << std::endl;
199+
call_result = wasm_runtime_call_wasm_a(exec_env, reverse_cleanup_func, 0, nullptr, 0, nullptr);
200+
return result;
201+
};
202+
call_reverse("Hello World!");
113203

114-
list_t<variant_t<bool_t>> args = {true, false};
115-
auto args_flat = cmcpp::lower_flat(callContext, args);
116-
auto call_result = wasm_runtime_call_wasm(exec_env, func, 2, (uint32_t *)args_flat.data());
117-
args_flat.resize(1);
118-
auto result = cmcpp::lift_flat<bool_t>(callContext, args_flat);
119-
std::cout << "Result: " << result << std::endl;
204+
auto reverse_tuple_func = wasm_runtime_lookup_function(module_inst, "example:sample/tuples#reverse");
205+
auto reverse_tuple_cleanup_func = wasm_runtime_lookup_function(module_inst, "cabi_post_example:sample/tuples#reverse");
206+
auto call_reverse_tuple = [&](bool in1, const std::string &in2)
207+
{
208+
using inputs_t = tuple_t<bool_t, string_t>;
209+
using outputs_t = tuple_t<string_t, bool_t>;
210+
211+
auto and_inputs = toWamr(lower_flat(callContext, inputs_t{in1, in2}));
212+
auto output_size = 1;
213+
wasm_val_t outputs[output_size];
214+
auto call_result = wasm_runtime_call_wasm_a(exec_env, reverse_tuple_func, output_size, outputs, and_inputs.size(), and_inputs.data());
215+
auto result = load<outputs_t>(callContext, outputs[0].of.i32);
216+
std::cout << "reverse_tuple(" << in1 << ", " << in2 << "): " << std::get<0>(result) << ", " << std::get<1>(result) << std::endl;
217+
return result;
218+
};
219+
call_reverse_tuple(false, "Hello World!");
120220

121221
wasm_runtime_destroy_exec_env(exec_env);
122222
wasm_runtime_deinstantiate(module_inst);

samples/wasm/main.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <cstdlib>
44

5-
bool exports_example_sample_boolean_and(bool a, bool b)
5+
bool exports_example_sample_booleans_and(bool a, bool b)
66
{
77
return a && b;
88
}
@@ -15,4 +15,10 @@ void exports_example_sample_strings_reverse(sample_string_t *a, sample_string_t
1515
{
1616
ret->ptr[i] = a->ptr[a->len - i - 1];
1717
}
18+
}
19+
20+
void exports_example_sample_tuples_reverse(sample_tuple2_bool_string_t *a, sample_tuple2_string_bool_t *ret)
21+
{
22+
ret->f1 = !a->f0;
23+
exports_example_sample_strings_reverse(&a->f1, &ret->f0);
1824
}

samples/wasm/sample.wit

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package example:sample;
22

3-
interface boolean {
3+
interface booleans {
44
and: func(a: bool, b: bool) -> bool;
55
// export or: func(a: bool, b: bool) -> bool;
66
// export not: func(a: bool) -> bool;
@@ -10,7 +10,12 @@ interface strings {
1010
reverse: func(a: string) -> string;
1111
}
1212

13+
interface tuples {
14+
reverse: func(a: tuple<bool, string>) -> tuple<string, bool>;
15+
}
16+
1317
world sample {
14-
export boolean;
18+
export booleans;
1519
export strings;
20+
export tuples;
1621
}

0 commit comments

Comments
 (0)