Skip to content

Commit faf28fc

Browse files
committed
refactor: Instantiate with module passed by value
1 parent aade338 commit faf28fc

File tree

11 files changed

+131
-131
lines changed

11 files changed

+131
-131
lines changed

lib/fizzy/instantiate.cpp

Lines changed: 3 additions & 3 deletions
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<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*/)
@@ -373,12 +373,12 @@ std::unique_ptr<Instance> instantiate(std::unique_ptr<Module>&& module,
373373
return instance;
374374
}
375375

376-
std::unique_ptr<Instance> instantiate(const std::unique_ptr<Module>& module,
376+
std::unique_ptr<Instance> instantiate(Module module,
377377
std::vector<ExternalFunction> imported_functions, std::vector<ExternalTable> imported_tables,
378378
std::vector<ExternalMemory> imported_memories, std::vector<ExternalGlobal> imported_globals,
379379
uint32_t memory_pages_limit)
380380
{
381-
return instantiate(std::make_unique<Module>(*module), std::move(imported_functions),
381+
return instantiate(std::make_unique<Module>(std::move(module)), std::move(imported_functions),
382382
std::move(imported_tables), std::move(imported_memories), std::move(imported_globals),
383383
memory_pages_limit);
384384
}

lib/fizzy/instantiate.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,15 @@ struct Instance
8787
};
8888

8989
// Instantiate a module.
90-
// Transfers ownership of passed module to instance.
91-
std::unique_ptr<Instance> instantiate(std::unique_ptr<Module>&& module,
90+
std::unique_ptr<Instance> instantiate(std::unique_ptr<Module> module,
9291
std::vector<ExternalFunction> imported_functions = {},
9392
std::vector<ExternalTable> imported_tables = {},
9493
std::vector<ExternalMemory> imported_memories = {},
9594
std::vector<ExternalGlobal> imported_globals = {},
9695
uint32_t memory_pages_limit = DefaultMemoryPagesLimit);
9796

9897
// Instantiate a module.
99-
// Makes a copy of passed module and stores it in instance.
100-
std::unique_ptr<Instance> instantiate(const std::unique_ptr<Module>& module,
98+
std::unique_ptr<Instance> instantiate(Module _module,
10199
std::vector<ExternalFunction> imported_functions = {},
102100
std::vector<ExternalTable> imported_tables = {},
103101
std::vector<ExternalMemory> imported_memories = {},

test/spectests/spectests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class test_runner
8989
explicit test_runner(const test_settings& ts)
9090
: m_settings{ts}, m_registered_names{{spectest_name, spectest_name}}
9191
{
92-
m_instances[spectest_name] = fizzy::instantiate(spectest_module);
92+
m_instances[spectest_name] = fizzy::instantiate(*spectest_module);
9393
}
9494

9595
test_results run_from_file(const fs::path& path)

test/unittests/api_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ TEST(api, resolve_imported_functions)
9696
Value global = 0;
9797
const std::vector<ExternalGlobal> external_globals{{&global, {ValType::i32, false}}};
9898
auto instance = instantiate(
99-
module, external_functions, {}, {}, std::vector<ExternalGlobal>(external_globals));
99+
*module, external_functions, {}, {}, std::vector<ExternalGlobal>(external_globals));
100100

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

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

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

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

143143
EXPECT_THAT(execute(*instance_extra, 0, {}), Result(0));
144144
EXPECT_THAT(execute(*instance_extra, 1, {Value{0}}), Result(1));

test/unittests/end_to_end_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ TEST(end_to_end, milestone1)
3131
const auto wasm = from_hex(
3232
"0061736d0100000001070160027f7f017f030201000a13011101017f200020016a20026a220220006a0b");
3333
const auto module = parse(wasm);
34-
auto instance = instantiate(module);
34+
auto instance = instantiate(*module);
3535

3636
EXPECT_THAT(execute(*instance, 0, {20, 22}), Result(20 + 22 + 20));
3737
}
@@ -71,7 +71,7 @@ TEST(end_to_end, milestone2)
7171

7272
const auto module = parse(bin);
7373

74-
auto instance = instantiate(module);
74+
auto instance = instantiate(*module);
7575

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

132-
auto instance = instantiate(module);
132+
auto instance = instantiate(*module);
133133
EXPECT_THAT(execute(*instance, *func_idx, {10, 2, 5}), Result(4));
134134
}
135135

@@ -171,7 +171,7 @@ TEST(end_to_end, memset)
171171
const auto func_idx = find_exported_function(*module, "test");
172172
ASSERT_TRUE(func_idx);
173173

174-
auto instance = instantiate(module);
174+
auto instance = instantiate(*module);
175175
EXPECT_THAT(execute(*instance, *func_idx, {0, 2}), Result());
176176
EXPECT_EQ(hex(instance->memory->substr(0, 2 * sizeof(int))), "d2040000d2040000");
177177
}

test/unittests/execute_call_test.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ TEST(execute_call, call_indirect_imported_table)
185185
table_elements table{
186186
{{f3, out_i32}}, {{f2, out_i32}}, {{f1, out_i32}}, {{f4, out_i64}}, {{f5, out_i32}}};
187187

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

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

278-
auto instance = instantiate(module, {{host_foo, host_foo_type}});
278+
auto instance = instantiate(*module, {{host_foo, host_foo_type}});
279279

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

305-
auto instance = instantiate(module, {{host_foo, host_foo_type}});
305+
auto instance = instantiate(*module, {{host_foo, host_foo_type}});
306306

307307
EXPECT_THAT(execute(*instance, 1, {20}), Result(42));
308308
}
@@ -350,7 +350,7 @@ TEST(execute_call, imported_functions_call_indirect)
350350
return Value{(11 + uint64_t{x} / 11) / 2};
351351
};
352352

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

375375
/* wat2wasm
376376
(module
@@ -395,7 +395,7 @@ TEST(execute_call, imported_function_from_another_module)
395395
return fizzy::execute(*instance1, *func_idx, args.data());
396396
};
397397

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

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

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

441-
auto instance2 = instantiate(module2, {}, {*table});
441+
auto instance2 = instantiate(*module2, {}, {*table});
442442

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

465465
/* wat2wasm
466466
(module
@@ -483,7 +483,7 @@ TEST(execute_call, imported_table_modified_by_uninstantiable_module)
483483
ASSERT_TRUE(table.has_value());
484484

485485
EXPECT_THROW_MESSAGE(
486-
instantiate(module2, {}, {*table}), instantiate_error, "start function failed to execute");
486+
instantiate(*module2, {}, {*table}), instantiate_error, "start function failed to execute");
487487

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

533533
const auto module = parse(bin);
534-
auto instance = instantiate(module);
534+
auto instance = instantiate(*module);
535535

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

577-
auto instance = instantiate(module, {{host_foo, host_foo_type}});
577+
auto instance = instantiate(*module, {{host_foo, host_foo_type}});
578578

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

601-
auto instance = instantiate(module, {{host_foo, host_foo_type}});
601+
auto instance = instantiate(*module, {{host_foo, host_foo_type}});
602602

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

621-
auto instance = instantiate(module, {{host_foo, host_foo_type}});
621+
auto instance = instantiate(*module, {{host_foo, host_foo_type}});
622622

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

646-
auto instance = instantiate(module, {{host_foo, host_foo_type}});
646+
auto instance = instantiate(*module, {{host_foo, host_foo_type}});
647647

648648
EXPECT_THAT(execute(*instance, 1, {}), Result(uint32_t{1}));
649649
}
@@ -664,7 +664,7 @@ TEST(execute_call, call_indirect_imported_table_infinite_recursion)
664664
"0061736d010000000105016000017f030201000404017000020707010374616201000907010041000b01000a09"
665665
"01070041011100000b");
666666
const auto module1 = parse(bin1);
667-
auto instance1 = instantiate(module1);
667+
auto instance1 = instantiate(*module1);
668668

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

687-
auto instance2 = instantiate(module2, {}, {*table});
687+
auto instance2 = instantiate(*module2, {}, {*table});
688688

689689
EXPECT_THAT(execute(*instance1, 0, {}), Traps());
690690
}
@@ -708,6 +708,6 @@ TEST(execute_call, drop_call_result)
708708
EXPECT_EQ(module->codesec[0].max_stack_height, 1);
709709
EXPECT_EQ(module->codesec[1].max_stack_height, 1);
710710
const auto func_idx = find_exported_function(*module, "drop_call_result");
711-
auto instance = instantiate(module);
711+
auto instance = instantiate(*module);
712712
EXPECT_THAT(fizzy::execute(*instance, *func_idx, {}), Result());
713713
}

test/unittests/execute_control_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ TEST(execute_control, br_1_out_of_function_and_imported_function)
668668
int) noexcept -> ExecutionResult { return Void; };
669669

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

test/unittests/execute_floating_point_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,7 +2016,7 @@ TEST(execute_floating_point, f32_store)
20162016

20172017
for (const auto& [arg, expected] : test_cases)
20182018
{
2019-
auto instance = instantiate(module);
2019+
auto instance = instantiate(*module);
20202020

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

20942094
for (const auto& [arg, expected] : test_cases)
20952095
{
2096-
auto instance = instantiate(module);
2096+
auto instance = instantiate(*module);
20972097

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

0 commit comments

Comments
 (0)