@@ -478,41 +478,40 @@ inline bool invoke_function(const FuncType& func_type, uint32_t func_idx, Instan
478478 OperandStack& stack, int depth)
479479{
480480 const auto num_args = func_type.inputs .size ();
481+ const auto num_outputs = func_type.outputs .size ();
481482 assert (stack.size () >= num_args);
482483
483484 const auto ret = execute_internal (instance, func_idx, stack.rend (), depth + 1 );
484485 // Bubble up traps
485486 if (ret.trapped )
486487 return false ;
487488
488- stack.drop (num_args);
489-
490- const auto num_outputs = func_type.outputs .size ();
491- // NOTE: we can assume these two from validation
492- assert (num_outputs <= 1 );
493- assert (ret.has_value == (num_outputs == 1 ));
494- // Push back the result
495- if (num_outputs != 0 )
496- stack.push (ret.value );
497-
489+ stack.drop (num_args - num_outputs);
498490 return true ;
499491}
500492
501493} // namespace
502494
503495ExecutionResult execute_internal (Instance& instance, FuncIdx func_idx, Value* args_end, int depth)
504496{
497+ assert (args_end != nullptr );
498+
505499 assert (depth >= 0 );
506500 if (depth > CallStackLimit)
507501 return Trap;
508502
509503 const auto & func_type = instance.module ->get_function_type (func_idx);
510504
511- const auto * args = args_end - func_type.inputs .size ();
505+ auto * args = args_end - func_type.inputs .size ();
512506
513507 assert (instance.module ->imported_function_types .size () == instance.imported_functions .size ());
514508 if (func_idx < instance.imported_functions .size ())
515- return instance.imported_functions [func_idx].function (instance, args, depth);
509+ {
510+ const auto res = instance.imported_functions [func_idx].function (instance, args, depth);
511+ if (res.has_value )
512+ args[0 ] = res.value ;
513+ return res;
514+ }
516515
517516 const auto & code = instance.module ->get_code (func_idx);
518517 auto * const memory = instance.memory .get ();
@@ -1536,7 +1535,10 @@ ExecutionResult execute_internal(Instance& instance, FuncIdx func_idx, Value* ar
15361535 assert (pc == &code.instructions [code.instructions .size ()]); // End of code must be reached.
15371536 assert (stack.size () == instance.module ->get_function_type (func_idx).outputs .size ());
15381537
1539- return stack.size () != 0 ? ExecutionResult{stack.top ()} : Void;
1538+ if (stack.size () != 0 && args != nullptr )
1539+ args[0 ] = stack.top ();
1540+
1541+ return Void;
15401542
15411543trap:
15421544 return Trap;
0 commit comments