Skip to content

Commit

Permalink
refactor: parse returns unique_ptr<const Module>
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Oct 9, 2020
1 parent 987d825 commit af47c53
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/fizzy/instantiate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ std::optional<uint32_t> find_export(const Module& module, ExternalKind kind, std

} // namespace

std::unique_ptr<Instance> instantiate(std::unique_ptr<Module> module,
std::unique_ptr<Instance> instantiate(std::unique_ptr<const Module> module,
std::vector<ExternalFunction> imported_functions, std::vector<ExternalTable> imported_tables,
std::vector<ExternalMemory> imported_memories, std::vector<ExternalGlobal> imported_globals,
uint32_t memory_pages_limit /*= DefaultMemoryPagesLimit*/)
Expand Down
6 changes: 3 additions & 3 deletions lib/fizzy/instantiate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ using bytes_ptr = std::unique_ptr<bytes, void (*)(bytes*)>;
// The module instance.
struct Instance
{
std::unique_ptr<Module> module;
std::unique_ptr<const Module> module;
// Memory is either allocated and owned by the instance or imported as already allocated bytes
// and owned externally.
// For these cases unique_ptr would either have a normal deleter or noop deleter respectively
Expand All @@ -70,7 +70,7 @@ struct Instance
std::vector<ExternalFunction> imported_functions;
std::vector<ExternalGlobal> imported_globals;

Instance(std::unique_ptr<Module> _module, bytes_ptr _memory, Limits _memory_limits,
Instance(std::unique_ptr<const Module> _module, bytes_ptr _memory, Limits _memory_limits,
uint32_t _memory_pages_limit, table_ptr _table, Limits _table_limits,
std::vector<Value> _globals, std::vector<ExternalFunction> _imported_functions,
std::vector<ExternalGlobal> _imported_globals)
Expand All @@ -87,7 +87,7 @@ struct Instance
};

// Instantiate a module.
std::unique_ptr<Instance> instantiate(std::unique_ptr<Module> module,
std::unique_ptr<Instance> instantiate(std::unique_ptr<const Module> module,
std::vector<ExternalFunction> imported_functions = {},
std::vector<ExternalTable> imported_tables = {},
std::vector<ExternalMemory> imported_memories = {},
Expand Down
4 changes: 2 additions & 2 deletions lib/fizzy/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ inline parser_result<Data> parse(const uint8_t* pos, const uint8_t* end)
return {{offset, std::move(init)}, pos};
}

std::unique_ptr<Module> parse(bytes_view input)
std::unique_ptr<const Module> parse(bytes_view input)
{
if (input.substr(0, wasm_prefix.size()) != wasm_prefix)
throw parser_error{"invalid wasm module prefix"};
Expand Down Expand Up @@ -658,7 +658,7 @@ std::unique_ptr<Module> parse(bytes_view input)
module->codesec.emplace_back(
parse_code(code_binaries[i], static_cast<FuncIdx>(i), *module));

return module;
return std::unique_ptr<const Module>(module.release());
}

parser_result<std::vector<uint32_t>> parse_vec_i32(const uint8_t* pos, const uint8_t* end)
Expand Down
2 changes: 1 addition & 1 deletion lib/fizzy/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ constexpr bytes_view wasm_prefix{wasm_prefix_data, sizeof(wasm_prefix_data)};
template <typename T>
using parser_result = std::pair<T, const uint8_t*>;

std::unique_ptr<Module> parse(bytes_view input);
std::unique_ptr<const Module> parse(bytes_view input);

inline parser_result<uint8_t> parse_byte(const uint8_t* pos, const uint8_t* end)
{
Expand Down
8 changes: 4 additions & 4 deletions test/unittests/execute_numeric_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ namespace
{
ExecutionResult execute_unary_operation(Instr instr, uint64_t arg)
{
const auto module{std::make_unique<Module>()};
auto module{std::make_unique<Module>()};
// type is currently needed only to get arity of function, so exact value types don't matter
module->typesec.emplace_back(FuncType{{ValType::i32}, {ValType::i32}});
module->funcsec.emplace_back(TypeIdx{0});
module->codesec.emplace_back(Code{1, 0, {Instr::local_get, instr, Instr::end}, {0, 0, 0, 0}});

return execute(module, 0, {arg});
return execute(*instantiate(std::move(module)), 0, {arg});
}

ExecutionResult execute_binary_operation(Instr instr, uint64_t lhs, uint64_t rhs)
{
const auto module{std::make_unique<Module>()};
auto module{std::make_unique<Module>()};
// type is currently needed only to get arity of function, so exact value types don't matter
module->typesec.emplace_back(FuncType{{ValType::i32, ValType::i32}, {ValType::i32}});
module->funcsec.emplace_back(TypeIdx{0});
module->codesec.emplace_back(Code{
2, 0, {Instr::local_get, Instr::local_get, instr, Instr::end}, {0, 0, 0, 0, 1, 0, 0, 0}});

return execute(module, 0, {lhs, rhs});
return execute(*instantiate(std::move(module)), 0, {lhs, rhs});
}
} // namespace

Expand Down
8 changes: 4 additions & 4 deletions test/unittests/execute_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ TEST(execute, i32_load_all_variants)
from_hex("0061736d0100000001060160017f017f030201000504010101010a0901070020002802000b");
const auto module = parse(wasm);

auto& load_instr = module->codesec[0].instructions[1];
auto& load_instr = const_cast<Instr&>(module->codesec[0].instructions[1]);
ASSERT_EQ(load_instr, Instr::i32_load);
ASSERT_EQ(module->codesec[0].immediates.substr(4), "00000000"_bytes); // load offset.

Expand Down Expand Up @@ -418,7 +418,7 @@ TEST(execute, i64_load_all_variants)
from_hex("0061736d0100000001060160017f017e030201000504010101010a0901070020002903000b");
const auto module = parse(wasm);

auto& load_instr = module->codesec[0].instructions[1];
auto& load_instr = const_cast<Instr&>(module->codesec[0].instructions[1]);
ASSERT_EQ(load_instr, Instr::i64_load);
ASSERT_EQ(module->codesec[0].immediates.substr(4), "00000000"_bytes); // load offset.

Expand Down Expand Up @@ -529,7 +529,7 @@ TEST(execute, i32_store_all_variants)
from_hex("0061736d0100000001060160027f7f00030201000504010101010a0b010900200120003602000b");
const auto module = parse(wasm);

auto& store_instr = module->codesec[0].instructions[2];
auto& store_instr = const_cast<Instr&>(module->codesec[0].instructions[2]);
ASSERT_EQ(store_instr, Instr::i32_store);
ASSERT_EQ(module->codesec[0].immediates.substr(8), "00000000"_bytes); // store offset

Expand Down Expand Up @@ -565,7 +565,7 @@ TEST(execute, i64_store_all_variants)
from_hex("0061736d0100000001060160027e7f00030201000504010101010a0b010900200120003703000b");
const auto module = parse(wasm);

auto& store_instr = module->codesec[0].instructions[2];
auto& store_instr = const_cast<Instr&>(module->codesec[0].instructions[2]);
ASSERT_EQ(store_instr, Instr::i64_store);
ASSERT_EQ(module->codesec[0].immediates.substr(8), "00000000"_bytes); // store offset

Expand Down
4 changes: 2 additions & 2 deletions test/utils/execute_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

namespace fizzy::test
{
inline ExecutionResult execute(
const std::unique_ptr<Module>& module, FuncIdx func_idx, std::initializer_list<Value> args)
inline ExecutionResult execute(const std::unique_ptr<const Module>& module, FuncIdx func_idx,
std::initializer_list<Value> args)
{
auto instance = instantiate(*module);
return execute(*instance, func_idx, args);
Expand Down

0 comments on commit af47c53

Please sign in to comment.