Skip to content

Commit af47c53

Browse files
committed
refactor: parse returns unique_ptr<const Module>
1 parent 987d825 commit af47c53

7 files changed

Lines changed: 17 additions & 17 deletions

File tree

lib/fizzy/instantiate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ std::optional<uint32_t> find_export(const Module& module, ExternalKind kind, std
237237

238238
} // namespace
239239

240-
std::unique_ptr<Instance> instantiate(std::unique_ptr<Module> module,
240+
std::unique_ptr<Instance> instantiate(std::unique_ptr<const Module> module,
241241
std::vector<ExternalFunction> imported_functions, std::vector<ExternalTable> imported_tables,
242242
std::vector<ExternalMemory> imported_memories, std::vector<ExternalGlobal> imported_globals,
243243
uint32_t memory_pages_limit /*= DefaultMemoryPagesLimit*/)

lib/fizzy/instantiate.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ using bytes_ptr = std::unique_ptr<bytes, void (*)(bytes*)>;
5454
// The module instance.
5555
struct Instance
5656
{
57-
std::unique_ptr<Module> module;
57+
std::unique_ptr<const Module> module;
5858
// Memory is either allocated and owned by the instance or imported as already allocated bytes
5959
// and owned externally.
6060
// For these cases unique_ptr would either have a normal deleter or noop deleter respectively
@@ -70,7 +70,7 @@ struct Instance
7070
std::vector<ExternalFunction> imported_functions;
7171
std::vector<ExternalGlobal> imported_globals;
7272

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

8989
// Instantiate a module.
90-
std::unique_ptr<Instance> instantiate(std::unique_ptr<Module> module,
90+
std::unique_ptr<Instance> instantiate(std::unique_ptr<const Module> module,
9191
std::vector<ExternalFunction> imported_functions = {},
9292
std::vector<ExternalTable> imported_tables = {},
9393
std::vector<ExternalMemory> imported_memories = {},

lib/fizzy/parser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ inline parser_result<Data> parse(const uint8_t* pos, const uint8_t* end)
430430
return {{offset, std::move(init)}, pos};
431431
}
432432

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

661-
return module;
661+
return std::unique_ptr<const Module>(module.release());
662662
}
663663

664664
parser_result<std::vector<uint32_t>> parse_vec_i32(const uint8_t* pos, const uint8_t* end)

lib/fizzy/parser.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ constexpr bytes_view wasm_prefix{wasm_prefix_data, sizeof(wasm_prefix_data)};
1717
template <typename T>
1818
using parser_result = std::pair<T, const uint8_t*>;
1919

20-
std::unique_ptr<Module> parse(bytes_view input);
20+
std::unique_ptr<const Module> parse(bytes_view input);
2121

2222
inline parser_result<uint8_t> parse_byte(const uint8_t* pos, const uint8_t* end)
2323
{

test/unittests/execute_numeric_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,25 @@ namespace
1616
{
1717
ExecutionResult execute_unary_operation(Instr instr, uint64_t arg)
1818
{
19-
const auto module{std::make_unique<Module>()};
19+
auto module{std::make_unique<Module>()};
2020
// type is currently needed only to get arity of function, so exact value types don't matter
2121
module->typesec.emplace_back(FuncType{{ValType::i32}, {ValType::i32}});
2222
module->funcsec.emplace_back(TypeIdx{0});
2323
module->codesec.emplace_back(Code{1, 0, {Instr::local_get, instr, Instr::end}, {0, 0, 0, 0}});
2424

25-
return execute(module, 0, {arg});
25+
return execute(*instantiate(std::move(module)), 0, {arg});
2626
}
2727

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

37-
return execute(module, 0, {lhs, rhs});
37+
return execute(*instantiate(std::move(module)), 0, {lhs, rhs});
3838
}
3939
} // namespace
4040

test/unittests/execute_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ TEST(execute, i32_load_all_variants)
380380
from_hex("0061736d0100000001060160017f017f030201000504010101010a0901070020002802000b");
381381
const auto module = parse(wasm);
382382

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

@@ -418,7 +418,7 @@ TEST(execute, i64_load_all_variants)
418418
from_hex("0061736d0100000001060160017f017e030201000504010101010a0901070020002903000b");
419419
const auto module = parse(wasm);
420420

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

@@ -529,7 +529,7 @@ TEST(execute, i32_store_all_variants)
529529
from_hex("0061736d0100000001060160027f7f00030201000504010101010a0b010900200120003602000b");
530530
const auto module = parse(wasm);
531531

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

@@ -565,7 +565,7 @@ TEST(execute, i64_store_all_variants)
565565
from_hex("0061736d0100000001060160027e7f00030201000504010101010a0b010900200120003703000b");
566566
const auto module = parse(wasm);
567567

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

test/utils/execute_helpers.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
namespace fizzy::test
1212
{
13-
inline ExecutionResult execute(
14-
const std::unique_ptr<Module>& module, FuncIdx func_idx, std::initializer_list<Value> args)
13+
inline ExecutionResult execute(const std::unique_ptr<const Module>& module, FuncIdx func_idx,
14+
std::initializer_list<Value> args)
1515
{
1616
auto instance = instantiate(*module);
1717
return execute(*instance, func_idx, args);

0 commit comments

Comments
 (0)