diff --git a/lib/fizzy/execute.cpp b/lib/fizzy/execute.cpp index 1dc71f83ae..8313a2538a 100644 --- a/lib/fizzy/execute.cpp +++ b/lib/fizzy/execute.cpp @@ -302,11 +302,11 @@ bool invoke_function( const auto num_outputs = func_type.outputs.size(); // NOTE: we can assume these two from validation - assert(ret.stack.size() == num_outputs); assert(num_outputs <= 1); + assert(ret.result.has_value() == num_outputs); // Push back the result if (num_outputs != 0) - stack.push(ret.stack[0]); + stack.push(*ret.result); return true; } @@ -1567,8 +1567,10 @@ execution_result execute( end: assert(labels.empty() || trap); - // move allows to return derived Stack instance into base vector value - return {trap, std::move(stack)}; + if (stack.empty()) + return {trap, std::nullopt}; + else + return {trap, stack.pop()}; } execution_result execute(const Module& module, FuncIdx func_idx, std::vector args) diff --git a/lib/fizzy/execute.hpp b/lib/fizzy/execute.hpp index a74df1ca17..884da231f3 100644 --- a/lib/fizzy/execute.hpp +++ b/lib/fizzy/execute.hpp @@ -19,7 +19,7 @@ struct execution_result bool trapped; // the resulting stack (e.g. return values) // NOTE: this can be either 0 or 1 items - std::vector stack; + std::optional result; }; struct Instance; diff --git a/test/spectests/spectests.cpp b/test/spectests/spectests.cpp index 8eefa155fc..6042035c8b 100644 --- a/test/spectests/spectests.cpp +++ b/test/spectests/spectests.cpp @@ -210,20 +210,20 @@ class test_runner const auto& expected = cmd.at("expected"); if (expected.empty()) { - if (result->stack.empty()) + if (!result->result.has_value()) pass(); else fail("Unexpected returned value."); continue; } - if (result->stack.size() != 1) + if (!result->result.has_value()) { fail("More than 1 value returned."); continue; } - if (!check_result(result->stack[0], expected.at(0))) + if (!check_result(*result->result, expected.at(0))) continue; pass(); diff --git a/test/unittests/instantiate_test.cpp b/test/unittests/instantiate_test.cpp index fc237d870d..926abde43b 100644 --- a/test/unittests/instantiate_test.cpp +++ b/test/unittests/instantiate_test.cpp @@ -17,7 +17,8 @@ uint64_t call_table_func(Instance& instance, size_t idx) { const auto& elem = (*instance.table)[idx]; const auto res = elem->function(instance, {}, 0); - return res.stack.front(); + // ASSERT_TRUE(res.result.has_value()); + return *res.result; } } // namespace diff --git a/test/utils/asserts.cpp b/test/utils/asserts.cpp index c5ffae3b9b..c2658f6433 100644 --- a/test/utils/asserts.cpp +++ b/test/utils/asserts.cpp @@ -13,12 +13,8 @@ std::ostream& operator<<(std::ostream& os, execution_result result) return os << "trapped"; os << "result("; - std::string_view separator; - for (const auto& x : result.stack) - { - os << x << separator; - separator = ", "; - } + if (result.result.has_value()) + os << *result.result; os << ")"; return os; } diff --git a/test/utils/asserts.hpp b/test/utils/asserts.hpp index 79ff674697..479e90329f 100644 --- a/test/utils/asserts.hpp +++ b/test/utils/asserts.hpp @@ -15,12 +15,12 @@ MATCHER(Traps, "") MATCHER(Result, "empty result") { - return !arg.trapped && arg.stack.size() == 0; + return !arg.trapped && !arg.result.has_value(); } MATCHER_P(Result, value, "") { - return !arg.trapped && arg.stack.size() == 1 && arg.stack[0] == uint64_t(value); + return !arg.trapped && arg.result.has_value() && *arg.result == uint64_t(value); } #define EXPECT_THROW_MESSAGE(stmt, ex_type, expected) \ diff --git a/test/utils/fizzy_engine.cpp b/test/utils/fizzy_engine.cpp index 18d0a3435e..61fa532e1f 100644 --- a/test/utils/fizzy_engine.cpp +++ b/test/utils/fizzy_engine.cpp @@ -84,9 +84,8 @@ std::optional FizzyEngine::find_function(std::string_view n WasmEngine::Result FizzyEngine::execute( WasmEngine::FuncRef func_ref, const std::vector& args) { - const auto [trapped, result_stack] = + const auto [trapped, result] = fizzy::execute(*m_instance, static_cast(func_ref), args); - assert(result_stack.size() <= 1); - return {trapped, !result_stack.empty() ? result_stack.back() : std::optional{}}; + return {trapped, result}; } } // namespace fizzy::test