@@ -32,6 +32,43 @@ TEST(capi, free_module_null)
3232 fizzy_free_module (nullptr );
3333}
3434
35+ TEST (capi, get_function_type)
36+ {
37+ /* wat2wasm
38+ (func)
39+ (func (param i32 i32) (result i32) (i32.const 0))
40+ (func (param i64))
41+ (func (param f64) (result f32) (f32.const 0))
42+ */
43+ const auto wasm = from_hex (
44+ " 0061736d0100000001130460000060027f7f017f60017e0060017c017d030504000102030a140402000b040041"
45+ " 000b02000b070043000000000b" );
46+ const auto module = fizzy_parse (wasm.data (), wasm.size ());
47+ ASSERT_NE (module , nullptr );
48+
49+ const auto type0 = fizzy_get_function_type (module , 0 );
50+ EXPECT_EQ (type0.inputs_size , 0 );
51+ EXPECT_EQ (type0.output , FizzyValueTypeVoid);
52+
53+ const auto type1 = fizzy_get_function_type (module , 1 );
54+ EXPECT_EQ (type1.inputs_size , 2 );
55+ EXPECT_EQ (type1.inputs [0 ], FizzyValueTypeI32);
56+ EXPECT_EQ (type1.inputs [1 ], FizzyValueTypeI32);
57+ EXPECT_EQ (type1.output , FizzyValueTypeI32);
58+
59+ const auto type2 = fizzy_get_function_type (module , 2 );
60+ EXPECT_EQ (type2.inputs_size , 1 );
61+ EXPECT_EQ (type2.inputs [0 ], FizzyValueTypeI64);
62+ EXPECT_EQ (type2.output , FizzyValueTypeVoid);
63+
64+ const auto type3 = fizzy_get_function_type (module , 3 );
65+ EXPECT_EQ (type3.inputs_size , 1 );
66+ EXPECT_EQ (type3.inputs [0 ], FizzyValueTypeF64);
67+ EXPECT_EQ (type3.output , FizzyValueTypeF32);
68+
69+ fizzy_free_module (module );
70+ }
71+
3572TEST (capi, find_exported_function)
3673{
3774 /* wat2wasm
@@ -87,9 +124,10 @@ TEST(capi, instantiate_imported_function)
87124 module = fizzy_parse (wasm.data (), wasm.size ());
88125 ASSERT_NE (module , nullptr );
89126
90- FizzyExternalFunction host_funcs[] = {{[](void *, FizzyInstance*, const FizzyValue*, int ) {
91- return FizzyExecutionResult{false , true , {42 }};
92- },
127+ FizzyExternalFunction host_funcs[] = {{{FizzyValueTypeI32, nullptr , 0 },
128+ [](void *, FizzyInstance*, const FizzyValue*, int ) {
129+ return FizzyExecutionResult{false , true , {42 }};
130+ },
93131 nullptr }};
94132
95133 auto instance = fizzy_instantiate (module , host_funcs, 1 );
@@ -209,13 +247,17 @@ TEST(capi, execute_with_host_function)
209247 auto module = fizzy_parse (wasm.data (), wasm.size ());
210248 ASSERT_NE (module , nullptr );
211249
212- FizzyExternalFunction host_funcs[] = {{[](void *, FizzyInstance*, const FizzyValue*, int ) {
213- return FizzyExecutionResult{false , true , {42 }};
214- },
250+ const FizzyValueType inputs[] = {FizzyValueTypeI32, FizzyValueTypeI32};
251+
252+ FizzyExternalFunction host_funcs[] = {{{FizzyValueTypeI32, nullptr , 0 },
253+ [](void *, FizzyInstance*, const FizzyValue*, int ) {
254+ return FizzyExecutionResult{false , true , {42 }};
255+ },
215256 nullptr },
216- {[](void *, FizzyInstance*, const FizzyValue* args, int ) {
217- return FizzyExecutionResult{false , true , {args[0 ].i64 / args[1 ].i64 }};
218- },
257+ {{FizzyValueTypeI32, &inputs[0 ], 2 },
258+ [](void *, FizzyInstance*, const FizzyValue* args, int ) {
259+ return FizzyExecutionResult{false , true , {args[0 ].i64 / args[1 ].i64 }};
260+ },
219261 nullptr }};
220262
221263 auto instance = fizzy_instantiate (module , host_funcs, 2 );
@@ -242,9 +284,10 @@ TEST(capi, imported_function_traps)
242284 auto module = fizzy_parse (wasm.data (), wasm.size ());
243285 ASSERT_NE (module , nullptr );
244286
245- FizzyExternalFunction host_funcs[] = {{[](void *, FizzyInstance*, const FizzyValue*, int ) {
246- return FizzyExecutionResult{true , false , {}};
247- },
287+ FizzyExternalFunction host_funcs[] = {{{FizzyValueTypeI32, nullptr , 0 },
288+ [](void *, FizzyInstance*, const FizzyValue*, int ) {
289+ return FizzyExecutionResult{true , false , {}};
290+ },
248291 nullptr }};
249292
250293 auto instance = fizzy_instantiate (module , host_funcs, 1 );
@@ -269,12 +312,12 @@ TEST(capi, imported_function_void)
269312 ASSERT_NE (module , nullptr );
270313
271314 bool called = false ;
272- FizzyExternalFunction host_funcs[] = {
273- { [](void * context, FizzyInstance*, const FizzyValue*, int ) {
274- *static_cast <bool *>(context) = true ;
275- return FizzyExecutionResult{false , false , {}};
276- },
277- &called}};
315+ FizzyExternalFunction host_funcs[] = {{{},
316+ [](void * context, FizzyInstance*, const FizzyValue*, int ) {
317+ *static_cast <bool *>(context) = true ;
318+ return FizzyExecutionResult{false , false , {}};
319+ },
320+ &called}};
278321
279322 auto instance = fizzy_instantiate (module , host_funcs, 1 );
280323 ASSERT_NE (instance, nullptr );
@@ -332,7 +375,10 @@ TEST(capi, imported_function_from_another_module)
332375 return fizzy_execute (
333376 instance_and_func_idx->first , instance_and_func_idx->second , args, depth + 1 );
334377 };
335- FizzyExternalFunction host_funcs[] = {{sub, &host_context}};
378+
379+ const FizzyValueType inputs[] = {FizzyValueTypeI32, FizzyValueTypeI32};
380+
381+ FizzyExternalFunction host_funcs[] = {{{FizzyValueTypeI32, &inputs[0 ], 2 }, sub, &host_context}};
336382
337383 auto instance2 = fizzy_instantiate (module2, host_funcs, 1 );
338384 ASSERT_NE (instance2, nullptr );
0 commit comments