Skip to content

Commit

Permalink
Pass arguments as const Value*
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast authored and axic committed Oct 1, 2020
1 parent 082b253 commit b080eab
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
12 changes: 6 additions & 6 deletions lib/fizzy/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ inline bool invoke_function(const FuncType& func_type, uint32_t func_idx, Instan
OperandStack& stack, int depth) noexcept
{
const auto func = [func_idx](Instance& _instance, span<const Value> args, int _depth) noexcept {
return execute(_instance, func_idx, args, _depth);
return execute(_instance, func_idx, args.begin(), _depth);
};
return invoke_function(func_type, func, instance, stack, depth);
}
Expand Down Expand Up @@ -461,23 +461,23 @@ __attribute__((no_sanitize("float-cast-overflow"))) inline constexpr float demot

} // namespace

ExecutionResult execute(
Instance& instance, FuncIdx func_idx, span<const Value> args, int depth) noexcept
ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args, int depth) noexcept
{
assert(depth >= 0);
if (depth > CallStackLimit)
return Trap;

assert(args.size() == instance.module.get_function_type(func_idx).inputs.size());
const auto& func_type = instance.module.get_function_type(func_idx);

assert(instance.module.imported_function_types.size() == instance.imported_functions.size());
if (func_idx < instance.imported_functions.size())
return instance.imported_functions[func_idx].function(instance, args, depth);
return instance.imported_functions[func_idx].function(
instance, {args, func_type.inputs.size()}, depth);

const auto& code = instance.module.get_code(func_idx);
auto* const memory = instance.memory.get();

OperandStack stack(args.data(), func_type.inputs.size(), code.local_count,
OperandStack stack(args, func_type.inputs.size(), code.local_count,
static_cast<size_t>(code.max_stack_height));

const Instr* pc = code.instructions.data();
Expand Down
5 changes: 3 additions & 2 deletions lib/fizzy/execute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ constexpr ExecutionResult Trap{false};

// Execute a function on an instance.
ExecutionResult execute(
Instance& instance, FuncIdx func_idx, span<const Value> args, int depth = 0) noexcept;
Instance& instance, FuncIdx func_idx, const Value* args, int depth = 0) noexcept;

inline ExecutionResult execute(
Instance& instance, FuncIdx func_idx, std::initializer_list<Value> args) noexcept
{
return execute(instance, func_idx, span<const Value>{args});
assert(args.size() == instance.module.get_function_type(func_idx).inputs.size());
return execute(instance, func_idx, args.begin());
}
} // namespace fizzy
4 changes: 2 additions & 2 deletions lib/fizzy/instantiate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ std::unique_ptr<Instance> instantiate(Module module,
for (const auto idx : instance->module.elementsec[i].init)
{
auto func = [idx, &instance_ref = *instance](fizzy::Instance&, span<const Value> args,
int depth) { return execute(instance_ref, idx, args, depth); };
int depth) { return execute(instance_ref, idx, args.begin(), depth); };

*it_table++ =
ExternalFunction{std::move(func), instance->module.get_function_type(idx)};
Expand Down Expand Up @@ -433,7 +433,7 @@ std::optional<ExternalFunction> find_exported_function(Instance& instance, std::

const auto idx = *opt_index;
auto func = [idx, &instance](fizzy::Instance&, span<const Value> args, int depth) {
return execute(instance, idx, args, depth);
return execute(instance, idx, args.begin(), depth);
};

return ExternalFunction{std::move(func), instance.module.get_function_type(idx)};
Expand Down
2 changes: 1 addition & 1 deletion test/spectests/spectests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ class test_runner
args.push_back(*arg_value);
}

return fizzy::execute(*instance, *func_idx, args);
return fizzy::execute(*instance, *func_idx, args.data());
}

bool check_integer_result(fizzy::Value actual_value, const json& expected)
Expand Down
2 changes: 1 addition & 1 deletion test/unittests/execute_call_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ TEST(execute_call, imported_function_from_another_module)
ASSERT_TRUE(func_idx.has_value());

auto sub = [&instance1, func_idx](Instance&, span<const Value> args, int) -> ExecutionResult {
return fizzy::execute(*instance1, *func_idx, args);
return fizzy::execute(*instance1, *func_idx, args.begin());
};

auto instance2 = instantiate(module2, {{sub, module1.typesec[0]}});
Expand Down
2 changes: 1 addition & 1 deletion test/unittests/execute_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ TEST(execute, reuse_args)

const std::vector<Value> args{20, 3};
const auto expected = args[0].i64 % (args[0].i64 / args[1].i64);
EXPECT_THAT(execute(*instance, 0, args), Result(expected));
EXPECT_THAT(execute(*instance, 0, args.data()), Result(expected));
EXPECT_THAT(args[0].i64, 20);
EXPECT_THAT(args[1].i64, 3);

Expand Down
3 changes: 1 addition & 2 deletions test/utils/fizzy_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ WasmEngine::Result FizzyEngine::execute(
{
static_assert(sizeof(uint64_t) == sizeof(Value));
const auto first_arg = reinterpret_cast<const Value*>(args.data());
const auto status = fizzy::execute(
*m_instance, static_cast<uint32_t>(func_ref), span<const Value>(first_arg, args.size()));
const auto status = fizzy::execute(*m_instance, static_cast<uint32_t>(func_ref), first_arg);
if (status.trapped)
return {true, std::nullopt};
else if (status.has_value)
Expand Down

0 comments on commit b080eab

Please sign in to comment.