@@ -30,11 +30,11 @@ TEST(capi_execute, execute)
3030 module , nullptr , 0 , nullptr , nullptr , nullptr , 0 , FizzyMemoryPagesLimitDefault, nullptr );
3131 ASSERT_NE (instance, nullptr );
3232
33- EXPECT_THAT (fizzy_execute (instance, 0 , nullptr ), CResult ());
34- EXPECT_THAT (fizzy_execute (instance, 1 , nullptr ), CResult (42_u32));
33+ EXPECT_THAT (fizzy_execute (instance, 0 , nullptr , nullptr ), CResult ());
34+ EXPECT_THAT (fizzy_execute (instance, 1 , nullptr , nullptr ), CResult (42_u32));
3535 FizzyValue args[] = {{42 }, {2 }};
36- EXPECT_THAT (fizzy_execute (instance, 2 , args), CResult (21_u32));
37- EXPECT_THAT (fizzy_execute (instance, 3 , nullptr ), CTraps ());
36+ EXPECT_THAT (fizzy_execute (instance, 2 , args, nullptr ), CResult (21_u32));
37+ EXPECT_THAT (fizzy_execute (instance, 3 , nullptr , nullptr ), CTraps ());
3838
3939 fizzy_free_instance (instance);
4040}
@@ -71,10 +71,10 @@ TEST(capi_execute, execute_with_host_function)
7171 module , host_funcs, 2 , nullptr , nullptr , nullptr , 0 , FizzyMemoryPagesLimitDefault, nullptr );
7272 ASSERT_NE (instance, nullptr );
7373
74- EXPECT_THAT (fizzy_execute (instance, 0 , nullptr ), CResult (42_u32));
74+ EXPECT_THAT (fizzy_execute (instance, 0 , nullptr , nullptr ), CResult (42_u32));
7575
7676 FizzyValue args[] = {{42 }, {2 }};
77- EXPECT_THAT (fizzy_execute (instance, 1 , args), CResult (21_u32));
77+ EXPECT_THAT (fizzy_execute (instance, 1 , args, nullptr ), CResult (21_u32));
7878
7979 fizzy_free_instance (instance);
8080}
@@ -102,7 +102,7 @@ TEST(capi_execute, imported_function_traps)
102102 module , host_funcs, 1 , nullptr , nullptr , nullptr , 0 , FizzyMemoryPagesLimitDefault, nullptr );
103103 ASSERT_NE (instance, nullptr );
104104
105- EXPECT_THAT (fizzy_execute (instance, 1 , nullptr ), CTraps ());
105+ EXPECT_THAT (fizzy_execute (instance, 1 , nullptr , nullptr ), CTraps ());
106106
107107 fizzy_free_instance (instance);
108108}
@@ -132,7 +132,7 @@ TEST(capi_execute, imported_function_void)
132132 module , host_funcs, 1 , nullptr , nullptr , nullptr , 0 , FizzyMemoryPagesLimitDefault, nullptr );
133133 ASSERT_NE (instance, nullptr );
134134
135- EXPECT_THAT (fizzy_execute (instance, 1 , nullptr ), CResult ());
135+ EXPECT_THAT (fizzy_execute (instance, 1 , nullptr , nullptr ), CResult ());
136136 EXPECT_TRUE (called);
137137
138138 fizzy_free_instance (instance);
@@ -182,13 +182,68 @@ TEST(capi_execute, imported_function_from_another_module)
182182 ASSERT_NE (instance2, nullptr );
183183
184184 FizzyValue args[] = {{44 }, {2 }};
185- EXPECT_THAT (fizzy_execute (instance2, 1 , args), CResult (42_u32));
185+ EXPECT_THAT (fizzy_execute (instance2, 1 , args, nullptr ), CResult (42_u32));
186186
187187 fizzy_free_exported_function (&func);
188188 fizzy_free_instance (instance2);
189189 fizzy_free_instance (instance1);
190190}
191191
192+ TEST (capi_execute, imported_function_from_another_module_via_host_function)
193+ {
194+ /* wat2wasm
195+ (module
196+ (func $sub (param $lhs i32) (param $rhs i32) (result i32)
197+ local.get $lhs
198+ local.get $rhs
199+ i32.sub)
200+ )
201+ */
202+ const auto bin1 = from_hex (" 0061736d0100000001070160027f7f017f030201000a09010700200020016b0b" );
203+ auto module1 = fizzy_parse (bin1.data (), bin1.size (), nullptr );
204+ ASSERT_NE (module1, nullptr );
205+ auto instance1 = fizzy_instantiate (
206+ module1, nullptr , 0 , nullptr , nullptr , nullptr , 0 , FizzyMemoryPagesLimitDefault, nullptr );
207+ ASSERT_NE (instance1, nullptr );
208+
209+ /* wat2wasm
210+ (module
211+ (func $sub (import "m1" "sub") (param $lhs i32) (param $rhs i32) (result i32))
212+
213+ (func $main (param i32) (param i32) (result i32)
214+ local.get 0
215+ local.get 1
216+ call $sub
217+ )
218+ )
219+ */
220+ const auto bin2 = from_hex (
221+ " 0061736d0100000001070160027f7f017f020a01026d31037375620000030201000a0a0108002000200110000"
222+ " b" );
223+ auto module2 = fizzy_parse (bin2.data (), bin2.size (), nullptr );
224+ ASSERT_NE (module2, nullptr );
225+
226+ auto sub = [](void * host_context, FizzyInstance*, const FizzyValue* args,
227+ FizzyExecutionContext* ctx) noexcept -> FizzyExecutionResult {
228+ auto * instance = static_cast <FizzyInstance*>(host_context);
229+ return fizzy_execute (instance, 0 , args, ctx);
230+ };
231+
232+ const FizzyValueType inputs[] = {FizzyValueTypeI32, FizzyValueTypeI32};
233+
234+ FizzyExternalFunction host_funcs[] = {{{FizzyValueTypeI32, &inputs[0 ], 2 }, sub, instance1}};
235+
236+ auto instance2 = fizzy_instantiate (module2, host_funcs, 1 , nullptr , nullptr , nullptr , 0 ,
237+ FizzyMemoryPagesLimitDefault, nullptr );
238+ ASSERT_NE (instance2, nullptr );
239+
240+ FizzyValue args[] = {{44 }, {2 }};
241+ EXPECT_THAT (fizzy_execute (instance2, 1 , args, nullptr ), CResult (42_u32));
242+
243+ fizzy_free_instance (instance2);
244+ fizzy_free_instance (instance1);
245+ }
246+
192247TEST (capi_execute, imported_table_from_another_module)
193248{
194249 /* wat2wasm
@@ -224,7 +279,7 @@ TEST(capi_execute, imported_table_from_another_module)
224279 module2, nullptr , 0 , &table, nullptr , nullptr , 0 , FizzyMemoryPagesLimitDefault, nullptr );
225280 ASSERT_NE (instance2, nullptr );
226281
227- EXPECT_THAT (fizzy_execute (instance2, 0 , nullptr ), CResult (42_u32));
282+ EXPECT_THAT (fizzy_execute (instance2, 0 , nullptr , nullptr ), CResult (42_u32));
228283
229284 fizzy_free_instance (instance2);
230285 fizzy_free_instance (instance1);
@@ -262,7 +317,7 @@ TEST(capi_execute, imported_memory_from_another_module)
262317 module2, nullptr , 0 , nullptr , &memory, nullptr , 0 , FizzyMemoryPagesLimitDefault, nullptr );
263318 ASSERT_NE (instance2, nullptr );
264319
265- EXPECT_THAT (fizzy_execute (instance2, 0 , nullptr ), CResult (0x00ffaa00_u32));
320+ EXPECT_THAT (fizzy_execute (instance2, 0 , nullptr , nullptr ), CResult (0x00ffaa00_u32));
266321
267322 fizzy_free_instance (instance2);
268323 fizzy_free_instance (instance1);
@@ -300,8 +355,102 @@ TEST(capi_execute, imported_global_from_another_module)
300355 module2, nullptr , 0 , nullptr , nullptr , &global, 1 , FizzyMemoryPagesLimitDefault, nullptr );
301356 ASSERT_NE (instance2, nullptr );
302357
303- EXPECT_THAT (fizzy_execute (instance2, 0 , nullptr ), CResult (42_u32));
358+ EXPECT_THAT (fizzy_execute (instance2, 0 , nullptr , nullptr ), CResult (42_u32));
304359
305360 fizzy_free_instance (instance2);
306361 fizzy_free_instance (instance1);
307362}
363+
364+ TEST (capi_execute, execute_with_execution_conext)
365+ {
366+ /* wat2wasm
367+ (func (result i32) i32.const 42)
368+ (func (result i32) call 0)
369+ */
370+ const auto wasm =
371+ from_hex (" 0061736d010000000105016000017f03030200000a0b020400412a0b040010000b" );
372+
373+ auto module = fizzy_parse (wasm.data (), wasm.size (), nullptr );
374+ ASSERT_NE (module , nullptr );
375+
376+ auto instance = fizzy_instantiate (
377+ module , nullptr , 0 , nullptr , nullptr , nullptr , 0 , FizzyMemoryPagesLimitDefault, nullptr );
378+ ASSERT_NE (instance, nullptr );
379+
380+ auto * ctx = fizzy_create_execution_context (0 );
381+ auto * depth = fizzy_get_execution_context_depth (ctx);
382+
383+ EXPECT_EQ (*depth, 0 );
384+ EXPECT_THAT (fizzy_execute (instance, 0 , nullptr , ctx), CResult (42_u32));
385+ EXPECT_EQ (*depth, 0 );
386+ EXPECT_THAT (fizzy_execute (instance, 1 , nullptr , ctx), CResult (42_u32));
387+ EXPECT_EQ (*depth, 0 );
388+
389+ *depth = 2047 ;
390+ EXPECT_THAT (fizzy_execute (instance, 0 , nullptr , ctx), CResult (42_u32));
391+ EXPECT_EQ (*depth, 2047 );
392+ EXPECT_THAT (fizzy_execute (instance, 1 , nullptr , ctx), CTraps ());
393+ EXPECT_EQ (*depth, 2047 );
394+
395+ *depth = 2048 ;
396+ EXPECT_THAT (fizzy_execute (instance, 0 , nullptr , ctx), CTraps ());
397+ EXPECT_EQ (*depth, 2048 );
398+ EXPECT_THAT (fizzy_execute (instance, 1 , nullptr , ctx), CTraps ());
399+ EXPECT_EQ (*depth, 2048 );
400+
401+ fizzy_free_execution_context (ctx);
402+ fizzy_free_instance (instance);
403+ }
404+
405+ TEST (capi_execute, execute_with_metered_execution_conext)
406+ {
407+ /* wat2wasm
408+ (func (result i32) i32.const 42)
409+ (func (result i32) call 0)
410+ */
411+ const auto wasm =
412+ from_hex (" 0061736d010000000105016000017f03030200000a0b020400412a0b040010000b" );
413+
414+ auto module = fizzy_parse (wasm.data (), wasm.size (), nullptr );
415+ ASSERT_NE (module , nullptr );
416+
417+ auto instance = fizzy_instantiate (
418+ module , nullptr , 0 , nullptr , nullptr , nullptr , 0 , FizzyMemoryPagesLimitDefault, nullptr );
419+ ASSERT_NE (instance, nullptr );
420+
421+ auto * ctx = fizzy_create_metered_execution_context (0 , 100 );
422+ auto * ticks = fizzy_get_execution_context_ticks (ctx);
423+ EXPECT_EQ (*ticks, 100 );
424+
425+ EXPECT_THAT (fizzy_execute (instance, 0 , nullptr , ctx), CResult (42_u32));
426+ EXPECT_EQ (*ticks, 98 );
427+ *ticks = 100 ;
428+ EXPECT_THAT (fizzy_execute (instance, 1 , nullptr , ctx), CResult (42_u32));
429+ EXPECT_EQ (*ticks, 96 );
430+
431+ *ticks = 4 ;
432+ EXPECT_THAT (fizzy_execute (instance, 0 , nullptr , ctx), CResult (42_u32));
433+ EXPECT_EQ (*ticks, 2 );
434+ *ticks = 4 ;
435+ EXPECT_THAT (fizzy_execute (instance, 1 , nullptr , ctx), CResult (42_u32));
436+ EXPECT_EQ (*ticks, 0 );
437+
438+ *ticks = 2 ;
439+ EXPECT_THAT (fizzy_execute (instance, 0 , nullptr , ctx), CResult (42_u32));
440+ EXPECT_EQ (*ticks, 0 );
441+ *ticks = 2 ;
442+ EXPECT_THAT (fizzy_execute (instance, 1 , nullptr , ctx), CTraps ());
443+
444+ *ticks = 1 ;
445+ EXPECT_THAT (fizzy_execute (instance, 0 , nullptr , ctx), CTraps ());
446+ *ticks = 1 ;
447+ EXPECT_THAT (fizzy_execute (instance, 1 , nullptr , ctx), CTraps ());
448+
449+ *ticks = 0 ;
450+ EXPECT_THAT (fizzy_execute (instance, 0 , nullptr , ctx), CTraps ());
451+ *ticks = 0 ;
452+ EXPECT_THAT (fizzy_execute (instance, 1 , nullptr , ctx), CTraps ());
453+
454+ fizzy_free_execution_context (ctx);
455+ fizzy_free_instance (instance);
456+ }
0 commit comments