Skip to content

Commit

Permalink
refactor: Instantiate with module passed by value
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Oct 8, 2020
1 parent aade338 commit e8db255
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 129 deletions.
6 changes: 3 additions & 3 deletions 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<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 Expand Up @@ -373,12 +373,12 @@ std::unique_ptr<Instance> instantiate(std::unique_ptr<Module>&& module,
return instance;
}

std::unique_ptr<Instance> instantiate(const std::unique_ptr<Module>& module,
std::unique_ptr<Instance> instantiate(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)
{
return instantiate(std::make_unique<Module>(*module), std::move(imported_functions),
return instantiate(std::make_unique<Module>(std::move(module)), std::move(imported_functions),
std::move(imported_tables), std::move(imported_memories), std::move(imported_globals),
memory_pages_limit);
}
Expand Down
6 changes: 2 additions & 4 deletions lib/fizzy/instantiate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,15 @@ struct Instance
};

// Instantiate a module.
// Transfers ownership of passed module to instance.
std::unique_ptr<Instance> instantiate(std::unique_ptr<Module>&& module,
std::unique_ptr<Instance> instantiate(std::unique_ptr<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);

// Instantiate a module.
// Makes a copy of passed module and stores it in instance.
std::unique_ptr<Instance> instantiate(const std::unique_ptr<Module>& module,
std::unique_ptr<Instance> instantiate(Module _module,
std::vector<ExternalFunction> imported_functions = {},
std::vector<ExternalTable> imported_tables = {},
std::vector<ExternalMemory> imported_memories = {},
Expand Down
2 changes: 1 addition & 1 deletion test/spectests/spectests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class test_runner
explicit test_runner(const test_settings& ts)
: m_settings{ts}, m_registered_names{{spectest_name, spectest_name}}
{
m_instances[spectest_name] = fizzy::instantiate(spectest_module);
m_instances[spectest_name] = fizzy::instantiate(*spectest_module);
}

test_results run_from_file(const fs::path& path)
Expand Down
6 changes: 3 additions & 3 deletions test/unittests/api_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ TEST(api, resolve_imported_functions)
Value global = 0;
const std::vector<ExternalGlobal> external_globals{{&global, {ValType::i32, false}}};
auto instance = instantiate(
module, external_functions, {}, {}, std::vector<ExternalGlobal>(external_globals));
*module, external_functions, {}, {}, std::vector<ExternalGlobal>(external_globals));

EXPECT_THAT(execute(*instance, 0, {}), Result(0));
EXPECT_THAT(execute(*instance, 1, {Value{0}}), Result(1));
Expand All @@ -115,7 +115,7 @@ TEST(api, resolve_imported_functions)
resolve_imported_functions(*module, std::move(imported_functions_reordered));
EXPECT_EQ(external_functions_reordered.size(), 4);

auto instance_reordered = instantiate(module, external_functions_reordered, {}, {},
auto instance_reordered = instantiate(*module, external_functions_reordered, {}, {},
std::vector<ExternalGlobal>(external_globals));

EXPECT_THAT(execute(*instance_reordered, 0, {}), Result(0));
Expand All @@ -138,7 +138,7 @@ TEST(api, resolve_imported_functions)
EXPECT_EQ(external_functions_extra.size(), 4);

auto instance_extra = instantiate(
module, external_functions_extra, {}, {}, std::vector<ExternalGlobal>(external_globals));
*module, external_functions_extra, {}, {}, std::vector<ExternalGlobal>(external_globals));

EXPECT_THAT(execute(*instance_extra, 0, {}), Result(0));
EXPECT_THAT(execute(*instance_extra, 1, {Value{0}}), Result(1));
Expand Down
8 changes: 4 additions & 4 deletions test/unittests/end_to_end_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ TEST(end_to_end, milestone1)
const auto wasm = from_hex(
"0061736d0100000001070160027f7f017f030201000a13011101017f200020016a20026a220220006a0b");
const auto module = parse(wasm);
auto instance = instantiate(module);
auto instance = instantiate(*module);

EXPECT_THAT(execute(*instance, 0, {20, 22}), Result(20 + 22 + 20));
}
Expand Down Expand Up @@ -71,7 +71,7 @@ TEST(end_to_end, milestone2)

const auto module = parse(bin);

auto instance = instantiate(module);
auto instance = instantiate(*module);

auto& memory = *instance->memory;
// This performs uint256 x uint256 -> uint512 multiplication.
Expand Down Expand Up @@ -129,7 +129,7 @@ TEST(end_to_end, nested_loops_in_c)
const auto func_idx = find_exported_function(*module, "test");
ASSERT_TRUE(func_idx);

auto instance = instantiate(module);
auto instance = instantiate(*module);
EXPECT_THAT(execute(*instance, *func_idx, {10, 2, 5}), Result(4));
}

Expand Down Expand Up @@ -171,7 +171,7 @@ TEST(end_to_end, memset)
const auto func_idx = find_exported_function(*module, "test");
ASSERT_TRUE(func_idx);

auto instance = instantiate(module);
auto instance = instantiate(*module);
EXPECT_THAT(execute(*instance, *func_idx, {0, 2}), Result());
EXPECT_EQ(hex(instance->memory->substr(0, 2 * sizeof(int))), "d2040000d2040000");
}
36 changes: 18 additions & 18 deletions test/unittests/execute_call_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ TEST(execute_call, call_indirect_imported_table)
table_elements table{
{{f3, out_i32}}, {{f2, out_i32}}, {{f1, out_i32}}, {{f4, out_i64}}, {{f5, out_i32}}};

auto instance = instantiate(module, {}, {{&table, {5, 20}}});
auto instance = instantiate(*module, {}, {{&table, {5, 20}}});

for (const auto param : {0u, 1u, 2u})
{
Expand Down Expand Up @@ -275,7 +275,7 @@ TEST(execute_call, imported_function_call)
constexpr auto host_foo = [](Instance&, span<const Value>, int) { return Value{42}; };
const auto host_foo_type = module->typesec[0];

auto instance = instantiate(module, {{host_foo, host_foo_type}});
auto instance = instantiate(*module, {{host_foo, host_foo_type}});

EXPECT_THAT(execute(*instance, 1, {}), Result(42));
}
Expand All @@ -302,7 +302,7 @@ TEST(execute_call, imported_function_call_with_arguments)
};
const auto host_foo_type = module->typesec[0];

auto instance = instantiate(module, {{host_foo, host_foo_type}});
auto instance = instantiate(*module, {{host_foo, host_foo_type}});

EXPECT_THAT(execute(*instance, 1, {20}), Result(42));
}
Expand Down Expand Up @@ -350,7 +350,7 @@ TEST(execute_call, imported_functions_call_indirect)
return Value{(11 + uint64_t{x} / 11) / 2};
};

auto instance = instantiate(module, {{sqr, module->typesec[0]}, {isqrt, module->typesec[0]}});
auto instance = instantiate(*module, {{sqr, module->typesec[0]}, {isqrt, module->typesec[0]}});
EXPECT_THAT(execute(*instance, 3, {0, 10}), Result(20)); // double(10)
EXPECT_THAT(execute(*instance, 3, {1, 9}), Result(81)); // sqr(9)
EXPECT_THAT(execute(*instance, 3, {2, 50}), Result(7)); // isqrt(50)
Expand All @@ -370,7 +370,7 @@ TEST(execute_call, imported_function_from_another_module)
const auto bin1 = from_hex(
"0061736d0100000001070160027f7f017f030201000707010373756200000a09010700200020016b0b");
const auto module1 = parse(bin1);
auto instance1 = instantiate(module1);
auto instance1 = instantiate(*module1);

/* wat2wasm
(module
Expand All @@ -395,7 +395,7 @@ TEST(execute_call, imported_function_from_another_module)
return fizzy::execute(*instance1, *func_idx, args.data());
};

auto instance2 = instantiate(module2, {{sub, module1->typesec[0]}});
auto instance2 = instantiate(*module2, {{sub, module1->typesec[0]}});

EXPECT_THAT(execute(*instance2, 1, {44, 2}), Result(42));
}
Expand All @@ -416,7 +416,7 @@ TEST(execute_call, imported_table_from_another_module)
"0061736d0100000001070160027f7f017f030201000404017000010707010374616201000907010041000b0100"
"0a09010700200020016b0b");
const auto module1 = parse(bin1);
auto instance1 = instantiate(module1);
auto instance1 = instantiate(*module1);

/* wat2wasm
(module
Expand All @@ -438,7 +438,7 @@ TEST(execute_call, imported_table_from_another_module)
const auto table = fizzy::find_exported_table(*instance1, "tab");
ASSERT_TRUE(table.has_value());

auto instance2 = instantiate(module2, {}, {*table});
auto instance2 = instantiate(*module2, {}, {*table});

EXPECT_THAT(execute(*instance2, 0, {44, 2}), Result(42));
}
Expand All @@ -460,7 +460,7 @@ TEST(execute_call, imported_table_modified_by_uninstantiable_module)
"0061736d0100000001070160027f7f017f030201000404017000010707010374616201000a0d010b0020002001"
"41001100000b");
const auto module1 = parse(bin1);
auto instance1 = instantiate(module1);
auto instance1 = instantiate(*module1);

/* wat2wasm
(module
Expand All @@ -483,7 +483,7 @@ TEST(execute_call, imported_table_modified_by_uninstantiable_module)
ASSERT_TRUE(table.has_value());

EXPECT_THROW_MESSAGE(
instantiate(module2, {}, {*table}), instantiate_error, "start function failed to execute");
instantiate(*module2, {}, {*table}), instantiate_error, "start function failed to execute");

EXPECT_THAT(execute(*instance1, 0, {44, 2}), Result(42));
}
Expand Down Expand Up @@ -531,7 +531,7 @@ TEST(execute_call, call_max_depth)
const auto bin = from_hex("0061736d010000000105016000017f03030200000a0b020400412a0b040010000b");

const auto module = parse(bin);
auto instance = instantiate(module);
auto instance = instantiate(*module);

EXPECT_THAT(execute(*instance, 0, {}, MaxDepth), Result(42));
EXPECT_THAT(execute(*instance, 1, {}, MaxDepth), Traps());
Expand Down Expand Up @@ -574,7 +574,7 @@ TEST(execute_call, call_imported_infinite_recursion)
};
const auto host_foo_type = module->typesec[0];

auto instance = instantiate(module, {{host_foo, host_foo_type}});
auto instance = instantiate(*module, {{host_foo, host_foo_type}});

EXPECT_THAT(execute(*instance, 0, {}), Traps());
}
Expand All @@ -598,7 +598,7 @@ TEST(execute_call, call_via_imported_infinite_recursion)
};
const auto host_foo_type = module->typesec[0];

auto instance = instantiate(module, {{host_foo, host_foo_type}});
auto instance = instantiate(*module, {{host_foo, host_foo_type}});

EXPECT_THAT(execute(*instance, 1, {}), Traps());
}
Expand All @@ -618,7 +618,7 @@ TEST(execute_call, call_imported_max_depth_recursion)
};
const auto host_foo_type = module->typesec[0];

auto instance = instantiate(module, {{host_foo, host_foo_type}});
auto instance = instantiate(*module, {{host_foo, host_foo_type}});

EXPECT_THAT(execute(*instance, 0, {}), Result(uint32_t{1}));
}
Expand All @@ -643,7 +643,7 @@ TEST(execute_call, call_via_imported_max_depth_recursion)
};
const auto host_foo_type = module->typesec[0];

auto instance = instantiate(module, {{host_foo, host_foo_type}});
auto instance = instantiate(*module, {{host_foo, host_foo_type}});

EXPECT_THAT(execute(*instance, 1, {}), Result(uint32_t{1}));
}
Expand All @@ -664,7 +664,7 @@ TEST(execute_call, call_indirect_imported_table_infinite_recursion)
"0061736d010000000105016000017f030201000404017000020707010374616201000907010041000b01000a09"
"01070041011100000b");
const auto module1 = parse(bin1);
auto instance1 = instantiate(module1);
auto instance1 = instantiate(*module1);

/* wat2wasm
(module
Expand All @@ -684,7 +684,7 @@ TEST(execute_call, call_indirect_imported_table_infinite_recursion)
const auto table = fizzy::find_exported_table(*instance1, "tab");
ASSERT_TRUE(table.has_value());

auto instance2 = instantiate(module2, {}, {*table});
auto instance2 = instantiate(*module2, {}, {*table});

EXPECT_THAT(execute(*instance1, 0, {}), Traps());
}
Expand All @@ -708,6 +708,6 @@ TEST(execute_call, drop_call_result)
EXPECT_EQ(module->codesec[0].max_stack_height, 1);
EXPECT_EQ(module->codesec[1].max_stack_height, 1);
const auto func_idx = find_exported_function(*module, "drop_call_result");
auto instance = instantiate(module);
auto instance = instantiate(*module);
EXPECT_THAT(fizzy::execute(*instance, *func_idx, {}), Result());
}
2 changes: 1 addition & 1 deletion test/unittests/execute_control_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ TEST(execute_control, br_1_out_of_function_and_imported_function)
int) noexcept -> ExecutionResult { return Void; };

const auto module = parse(bin);
auto instance = instantiate(module, {{fake_imported_function, module->typesec[0]}});
auto instance = instantiate(*module, {{fake_imported_function, module->typesec[0]}});
EXPECT_THAT(execute(*instance, 1, {}), Result(1));
}

Expand Down
4 changes: 2 additions & 2 deletions test/unittests/execute_floating_point_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2016,7 +2016,7 @@ TEST(execute_floating_point, f32_store)

for (const auto& [arg, expected] : test_cases)
{
auto instance = instantiate(module);
auto instance = instantiate(*module);

EXPECT_THAT(execute(*instance, 0, {arg, 1}), Result());
EXPECT_EQ(instance->memory->substr(0, 6), expected);
Expand Down Expand Up @@ -2093,7 +2093,7 @@ TEST(execute_floating_point, f64_store)

for (const auto& [arg, expected] : test_cases)
{
auto instance = instantiate(module);
auto instance = instantiate(*module);

EXPECT_THAT(execute(*instance, 0, {arg, 1}), Result());
EXPECT_EQ(instance->memory->substr(0, 10), expected);
Expand Down
Loading

0 comments on commit e8db255

Please sign in to comment.