diff --git a/lib/fizzy/execute.cpp b/lib/fizzy/execute.cpp index 673a5e0a1..00d14a74c 100644 --- a/lib/fizzy/execute.cpp +++ b/lib/fizzy/execute.cpp @@ -495,12 +495,11 @@ inline bool invoke_function(const FuncType& func_type, const F& func, Instance& stack.drop(num_args); - const auto num_outputs = func_type.outputs.size(); // NOTE: we can assume these two from validation - assert(num_outputs <= 1); - assert(ret.has_value == (num_outputs == 1)); + assert(func_type.outputs.size() <= 1); + assert(ret.has_value == !func_type.outputs.empty()); // Push back the result - if (num_outputs != 0) + if (ret.has_value != 0) stack.push(ret.value); return true; @@ -509,10 +508,25 @@ inline bool invoke_function(const FuncType& func_type, const F& func, Instance& inline bool invoke_function(const FuncType& func_type, uint32_t func_idx, Instance& instance, OperandStack& stack, int depth) noexcept { - const auto func = [func_idx](Instance& _instance, span args, int _depth) noexcept { - return execute(_instance, func_idx, args.data(), _depth); - }; - return invoke_function(func_type, func, instance, stack, depth); + const auto num_args = func_type.inputs.size(); + assert(stack.size() >= num_args); + + const auto ret = execute(instance, func_idx, stack.rend() - num_args, depth + 1); + // Bubble up traps + if (ret.trapped) + return false; + + stack.drop(num_args); + + const auto num_outputs = func_type.outputs.size(); + // NOTE: we can assume these two from validation + assert(num_outputs <= 1); + assert(ret.has_value == (num_outputs == 1)); + // Push back the result + if (num_outputs != 0) + stack.push(ret.value); + + return true; } } // namespace