Skip to content

Commit cb6d014

Browse files
committed
Add Module::get_code() helper method
1 parent 9f7cfa6 commit cb6d014

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

Diff for: lib/fizzy/execute.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,7 @@ ExecutionResult execute(
474474
if (func_idx < instance.imported_functions.size())
475475
return instance.imported_functions[func_idx].function(instance, args, depth);
476476

477-
const auto code_idx = func_idx - instance.imported_functions.size();
478-
assert(code_idx < instance.module.codesec.size());
479-
480-
const auto& code = instance.module.codesec[code_idx];
477+
const auto& code = instance.module.get_code(func_idx);
481478
auto* const memory = instance.memory.get();
482479

483480
OperandStack stack(args, code.local_count, static_cast<size_t>(code.max_stack_height));

Diff for: lib/fizzy/module.hpp

+8
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ struct Module
7676
globalsec[idx - imported_global_types.size()].type;
7777
}
7878

79+
const Code& get_code(FuncIdx func_idx) const noexcept
80+
{
81+
assert(func_idx >= imported_function_types.size()); // Cannot be imported function.
82+
const auto code_idx = func_idx - imported_function_types.size();
83+
assert(code_idx < codesec.size());
84+
return codesec[code_idx];
85+
}
86+
7987
bool has_table() const noexcept { return !tablesec.empty() || !imported_table_types.empty(); }
8088

8189
bool has_memory() const noexcept

Diff for: test/unittests/module_test.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ TEST(module, functions)
1515
/* wat2wasm
1616
(func (import "m" "f1") (param i32 i32) (result i32))
1717
(func)
18-
(func (param i64))
18+
(func (param i64) (local i32))
1919
(func (result f32) (f32.const 0))
2020
*/
2121
const auto bin = from_hex(
2222
"0061736d0100000001120460027f7f017f60000060017e006000017d020801016d02663100000304030102030a"
23-
"0f0302000b02000b070043000000000b");
23+
"110302000b0401017f0b070043000000000b");
2424
const auto module = parse(bin);
2525

2626
ASSERT_EQ(module.get_function_count(), 4);
@@ -29,6 +29,13 @@ TEST(module, functions)
2929
EXPECT_EQ(module.get_function_type(1), (FuncType{}));
3030
EXPECT_EQ(module.get_function_type(2), (FuncType{{ValType::i64}, {}}));
3131
EXPECT_EQ(module.get_function_type(3), (FuncType{{}, {ValType::f32}}));
32+
33+
EXPECT_EQ(module.get_code(1).instructions.size(), 1);
34+
EXPECT_EQ(module.get_code(1).local_count, 0);
35+
EXPECT_EQ(module.get_code(2).instructions.size(), 1);
36+
EXPECT_EQ(module.get_code(2).local_count, 1);
37+
EXPECT_EQ(module.get_code(3).instructions.size(), 2);
38+
EXPECT_EQ(module.get_code(3).local_count, 0);
3239
}
3340

3441
TEST(module, globals)

0 commit comments

Comments
 (0)