Skip to content

Commit

Permalink
Add Module::get_code() helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Sep 24, 2020
1 parent 9f7cfa6 commit cb6d014
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
5 changes: 1 addition & 4 deletions lib/fizzy/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,7 @@ ExecutionResult execute(
if (func_idx < instance.imported_functions.size())
return instance.imported_functions[func_idx].function(instance, args, depth);

const auto code_idx = func_idx - instance.imported_functions.size();
assert(code_idx < instance.module.codesec.size());

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

OperandStack stack(args, code.local_count, static_cast<size_t>(code.max_stack_height));
Expand Down
8 changes: 8 additions & 0 deletions lib/fizzy/module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ struct Module
globalsec[idx - imported_global_types.size()].type;
}

const Code& get_code(FuncIdx func_idx) const noexcept
{
assert(func_idx >= imported_function_types.size()); // Cannot be imported function.
const auto code_idx = func_idx - imported_function_types.size();
assert(code_idx < codesec.size());
return codesec[code_idx];
}

bool has_table() const noexcept { return !tablesec.empty() || !imported_table_types.empty(); }

bool has_memory() const noexcept
Expand Down
11 changes: 9 additions & 2 deletions test/unittests/module_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ TEST(module, functions)
/* wat2wasm
(func (import "m" "f1") (param i32 i32) (result i32))
(func)
(func (param i64))
(func (param i64) (local i32))
(func (result f32) (f32.const 0))
*/
const auto bin = from_hex(
"0061736d0100000001120460027f7f017f60000060017e006000017d020801016d02663100000304030102030a"
"0f0302000b02000b070043000000000b");
"110302000b0401017f0b070043000000000b");
const auto module = parse(bin);

ASSERT_EQ(module.get_function_count(), 4);
Expand All @@ -29,6 +29,13 @@ TEST(module, functions)
EXPECT_EQ(module.get_function_type(1), (FuncType{}));
EXPECT_EQ(module.get_function_type(2), (FuncType{{ValType::i64}, {}}));
EXPECT_EQ(module.get_function_type(3), (FuncType{{}, {ValType::f32}}));

EXPECT_EQ(module.get_code(1).instructions.size(), 1);
EXPECT_EQ(module.get_code(1).local_count, 0);
EXPECT_EQ(module.get_code(2).instructions.size(), 1);
EXPECT_EQ(module.get_code(2).local_count, 1);
EXPECT_EQ(module.get_code(3).instructions.size(), 2);
EXPECT_EQ(module.get_code(3).local_count, 0);
}

TEST(module, globals)
Expand Down

0 comments on commit cb6d014

Please sign in to comment.