Skip to content

Commit

Permalink
Change the return value of execute() from std::vector to std::optional
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Apr 24, 2020
1 parent 48b650e commit 49d3278
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 20 deletions.
10 changes: 6 additions & 4 deletions lib/fizzy/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -1567,8 +1567,10 @@ execution_result execute(

end:
assert(labels.empty() || trap);
// move allows to return derived Stack<uint64_t> instance into base vector<uint64_t> 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<uint64_t> args)
Expand Down
2 changes: 1 addition & 1 deletion lib/fizzy/execute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t> stack;
std::optional<uint64_t> result;
};

struct Instance;
Expand Down
6 changes: 3 additions & 3 deletions test/spectests/spectests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion test/unittests/instantiate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 2 additions & 6 deletions test/utils/asserts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions test/utils/asserts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down
5 changes: 2 additions & 3 deletions test/utils/fizzy_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ std::optional<WasmEngine::FuncRef> FizzyEngine::find_function(std::string_view n
WasmEngine::Result FizzyEngine::execute(
WasmEngine::FuncRef func_ref, const std::vector<uint64_t>& args)
{
const auto [trapped, result_stack] =
const auto [trapped, result] =
fizzy::execute(*m_instance, static_cast<uint32_t>(func_ref), args);
assert(result_stack.size() <= 1);
return {trapped, !result_stack.empty() ? result_stack.back() : std::optional<uint64_t>{}};
return {trapped, result};
}
} // namespace fizzy::test

0 comments on commit 49d3278

Please sign in to comment.