Skip to content

Commit 52eccb0

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

File tree

6 files changed

+17
-10
lines changed

6 files changed

+17
-10
lines changed

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: 1 addition & 1 deletion
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"};

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_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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,11 @@ inline ExecutionResult execute(
1616
auto instance = instantiate(*module);
1717
return execute(*instance, func_idx, args);
1818
}
19+
20+
inline ExecutionResult execute(const std::unique_ptr<const Module>& module, FuncIdx func_idx,
21+
std::initializer_list<Value> args)
22+
{
23+
auto instance = instantiate(*module);
24+
return execute(*instance, func_idx, args);
25+
}
1926
} // namespace fizzy::test

0 commit comments

Comments
 (0)