Skip to content

Commit af4d025

Browse files
committed
Split resolve
1 parent 11cf068 commit af4d025

File tree

6 files changed

+107
-90
lines changed

6 files changed

+107
-90
lines changed

Diff for: lib/fizzy/capi.cpp

+15-11
Original file line numberDiff line numberDiff line change
@@ -367,23 +367,27 @@ FizzyInstance* fizzy_instantiate(const FizzyModule* module,
367367

368368
FizzyInstance* fizzy_resolve_instantiate(const FizzyModule* c_module,
369369
const FizzyImportedFunction* c_imported_functions, size_t imported_functions_size,
370-
const FizzyExternalTable* imported_table, const FizzyExternalMemory* imported_memory,
371-
const FizzyExternalGlobal* imported_globals, size_t imported_globals_size)
370+
const FizzyExternalTable* c_imported_table, const FizzyExternalMemory* c_imported_memory,
371+
const FizzyExternalGlobal* c_imported_globals, size_t imported_globals_size)
372372
{
373373
try
374374
{
375375
auto imported_functions = unwrap(c_imported_functions, imported_functions_size);
376-
auto table = unwrap(imported_table);
377-
auto memory = unwrap(imported_memory);
378-
auto globals = unwrap(imported_globals, imported_globals_size);
376+
auto imported_tables = unwrap(c_imported_table);
377+
auto imported_memories = unwrap(c_imported_memory);
378+
auto imported_globals = unwrap(c_imported_globals, imported_globals_size);
379379

380380
std::unique_ptr<const fizzy::Module> module{unwrap(c_module)};
381-
auto [resolved_imported_functions, resolved_table, resolved_memory, resolved_globals] =
382-
fizzy::resolve_imports(*module, imported_functions, {}, {}, {});
383-
384-
auto instance =
385-
fizzy::instantiate(std::move(module), std::move(resolved_imported_functions),
386-
std::move(table), std::move(memory), std::move(globals));
381+
auto resolved_imported_functions =
382+
fizzy::resolve_imported_functions(*module, imported_functions);
383+
// auto resolved_tables = fizzy::resolve_imported_tables(*module, imported_tables);
384+
// auto resolved_memories = fizzy::resolve_imported_memories(*module,
385+
// imported_memories); auto resolved_globals =
386+
// fizzy::resolve_imported_globals(*module, imported_globals);
387+
388+
auto instance = fizzy::instantiate(std::move(module),
389+
std::move(resolved_imported_functions), std::move(imported_tables),
390+
std::move(imported_memories), std::move(imported_globals));
387391

388392
return wrap(instance.release());
389393
}

Diff for: lib/fizzy/instantiate.cpp

+57-36
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ Value eval_constant_expression(ConstantExpression expr,
228228
}
229229

230230
ExternalFunction find_imported_function(const std::string& module, const std::string& name,
231-
FuncType module_func_type, std::vector<ImportedFunction>& imported_functions)
231+
FuncType module_func_type, const std::vector<ImportedFunction>& imported_functions)
232232
{
233233
const auto it = std::find_if(imported_functions.begin(), imported_functions.end(),
234234
[module, name](const auto& func) { return module == func.module && name == func.name; });
@@ -255,7 +255,7 @@ ExternalFunction find_imported_function(const std::string& module, const std::st
255255
" output type doesn't match imported function in module"};
256256
}
257257

258-
return {std::move(it->function), module_func_type};
258+
return {it->function, module_func_type};
259259
}
260260

261261
ExternalTable find_imported_table(const std::string& module, const std::string& name,
@@ -470,46 +470,67 @@ std::unique_ptr<Instance> instantiate(std::unique_ptr<const Module> module,
470470
return instance;
471471
}
472472

473-
Externals resolve_imports(const Module& module, std::vector<ImportedFunction> imported_functions,
474-
std::vector<ImportedTable> imported_tables, std::vector<ImportedMemory> imported_memories,
475-
std::vector<ImportedGlobal> imported_globals)
473+
std::vector<ExternalFunction> resolve_imported_functions(
474+
const Module& module, const std::vector<ImportedFunction>& imported_functions)
476475
{
477-
Externals externals;
476+
std::vector<ExternalFunction> external_functions;
478477
for (const auto& import : module.importsec)
479478
{
480-
switch (import.kind)
481-
{
482-
case (ExternalKind::Function):
483-
{
484-
assert(import.desc.function_type_index < module.typesec.size());
485-
const auto& module_func_type = module.typesec[import.desc.function_type_index];
479+
if (import.kind != ExternalKind::Function)
480+
continue;
486481

487-
externals.functions.emplace_back(find_imported_function(
488-
import.module, import.name, module_func_type, imported_functions));
489-
break;
490-
}
491-
case (ExternalKind::Table):
492-
{
493-
externals.tables.emplace_back(find_imported_table(
494-
import.module, import.name, import.desc.table.limits, imported_tables));
495-
break;
496-
}
497-
case (ExternalKind::Memory):
498-
{
499-
externals.memories.emplace_back(find_imported_memory(
500-
import.module, import.name, import.desc.table.limits, imported_memories));
501-
break;
502-
}
503-
case (ExternalKind::Global):
504-
{
505-
externals.globals.emplace_back(find_imported_global(
506-
import.module, import.name, import.desc.global, imported_globals));
507-
break;
508-
}
509-
}
482+
assert(import.desc.function_type_index < module.typesec.size());
483+
const auto& module_func_type = module.typesec[import.desc.function_type_index];
484+
485+
external_functions.emplace_back(find_imported_function(
486+
import.module, import.name, module_func_type, imported_functions));
510487
}
488+
return external_functions;
489+
}
490+
491+
std::vector<ExternalTable> resolve_imported_table(
492+
const Module& module, const std::vector<ImportedTable>& imported_tables)
493+
{
494+
std::vector<ExternalTable> external_tables;
495+
for (const auto& import : module.importsec)
496+
{
497+
if (import.kind != ExternalKind::Table)
498+
continue;
511499

512-
return externals;
500+
external_tables.emplace_back(find_imported_table(
501+
import.module, import.name, import.desc.table.limits, imported_tables));
502+
}
503+
return external_tables;
504+
}
505+
506+
std::vector<ExternalMemory> resolve_imported_memory(
507+
const Module& module, const std::vector<ImportedMemory>& imported_memories)
508+
{
509+
std::vector<ExternalMemory> external_memories;
510+
for (const auto& import : module.importsec)
511+
{
512+
if (import.kind != ExternalKind::Memory)
513+
continue;
514+
515+
external_memories.emplace_back(find_imported_memory(
516+
import.module, import.name, import.desc.memory.limits, imported_memories));
517+
}
518+
return external_memories;
519+
}
520+
521+
std::vector<ExternalGlobal> resolve_imported_globals(
522+
const Module& module, const std::vector<ImportedGlobal>& imported_globals)
523+
{
524+
std::vector<ExternalGlobal> external_globals;
525+
for (const auto& import : module.importsec)
526+
{
527+
if (import.kind != ExternalKind::Global)
528+
continue;
529+
530+
external_globals.emplace_back(
531+
find_imported_global(import.module, import.name, import.desc.global, imported_globals));
532+
}
533+
return external_globals;
513534
}
514535

515536
std::optional<FuncIdx> find_exported_function(const Module& module, std::string_view name)

Diff for: lib/fizzy/instantiate.hpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ struct ImportedFunction
117117
std::function<ExecutionResult(Instance&, const Value*, int depth)> function;
118118
};
119119

120+
// Create vectors of External* ready to be passed to instantiate.
121+
// imported_* may be in any order,
122+
// but must contain imports for all of the import names defined in the module.
123+
std::vector<ExternalFunction> resolve_imported_functions(
124+
const Module& module, const std::vector<ImportedFunction>& imported_functions);
125+
120126
struct ImportedTable
121127
{
122128
std::string module;
@@ -126,6 +132,9 @@ struct ImportedTable
126132
std::optional<uint32_t> max;
127133
};
128134

135+
std::vector<ExternalTable> resolve_imported_table(
136+
const Module& module, const std::vector<ImportedTable>& imported_tables);
137+
129138
struct ImportedMemory
130139
{
131140
std::string module;
@@ -135,6 +144,9 @@ struct ImportedMemory
135144
std::optional<uint32_t> max;
136145
};
137146

147+
std::vector<ExternalMemory> resolve_imported_memory(
148+
const Module& module, const std::vector<ImportedMemory>& imported_memories);
149+
138150
struct ImportedGlobal
139151
{
140152
std::string module;
@@ -144,20 +156,8 @@ struct ImportedGlobal
144156
bool is_mutable = false;
145157
};
146158

147-
struct Externals
148-
{
149-
std::vector<ExternalFunction> functions;
150-
std::vector<ExternalTable> tables;
151-
std::vector<ExternalMemory> memories;
152-
std::vector<ExternalGlobal> globals;
153-
};
154-
155-
// Create vectors of External* ready to be passed to instantiate.
156-
// imported_* may be in any order,
157-
// but must contain imports for all of the import names defined in the module.
158-
Externals resolve_imports(const Module& module, std::vector<ImportedFunction> imported_functions,
159-
std::vector<ImportedTable> imported_tables, std::vector<ImportedMemory> imported_memories,
160-
std::vector<ImportedGlobal> imported_globals);
159+
std::vector<ExternalGlobal> resolve_imported_globals(
160+
const Module& module, const std::vector<ImportedGlobal>& imported_globals);
161161

162162
// Find exported function index by name.
163163
std::optional<FuncIdx> find_exported_function(const Module& module, std::string_view name);

Diff for: test/unittests/api_test.cpp

+15-22
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,13 @@ TEST(api, resolve_imported_functions)
9090
};
9191

9292
Value global{0};
93-
const std::vector<ImportedGlobal> imported_globals = {
94-
{"mod1", "g", &global, ValType::i32, false}};
93+
std::vector<ImportedGlobal> imported_globals = {{"mod1", "g", &global, ValType::i32, false}};
9594

96-
const auto [external_functions, _1, _2, external_globals] =
97-
resolve_imports(*module, std::move(imported_functions), {}, {}, imported_globals);
95+
const auto external_functions = resolve_imported_functions(*module, imported_functions);
96+
const auto external_globals = resolve_imported_globals(*module, imported_globals);
9897
EXPECT_EQ(external_functions.size(), 4);
9998

100-
auto instance = instantiate(
101-
*module, external_functions, {}, {}, std::vector<ExternalGlobal>(external_globals));
99+
auto instance = instantiate(*module, external_functions, {}, {}, external_globals);
102100

103101
EXPECT_THAT(execute(*instance, 0, {}), Result(0));
104102
EXPECT_THAT(execute(*instance, 1, {Value{0}}), Result(1));
@@ -113,12 +111,12 @@ TEST(api, resolve_imported_functions)
113111
{"mod2", "foo2", {ValType::i64, ValType::i32}, std::nullopt, function_returning_void},
114112
};
115113

116-
const auto [external_functions_reordered, _4, _5, _6] =
117-
resolve_imports(*module, std::move(imported_functions_reordered), {}, {}, imported_globals);
114+
const auto external_functions_reordered =
115+
resolve_imported_functions(*module, imported_functions_reordered);
118116
EXPECT_EQ(external_functions_reordered.size(), 4);
119117

120-
auto instance_reordered = instantiate(*module, external_functions_reordered, {}, {},
121-
std::vector<ExternalGlobal>(external_globals));
118+
auto instance_reordered =
119+
instantiate(*module, external_functions_reordered, {}, {}, external_globals);
122120

123121
EXPECT_THAT(execute(*instance_reordered, 0, {}), Result(0));
124122
EXPECT_THAT(execute(*instance_reordered, 1, {Value{0}}), Result(1));
@@ -135,12 +133,11 @@ TEST(api, resolve_imported_functions)
135133
{"mod3", "foo2", {}, std::nullopt, function_returning_value(5)},
136134
};
137135

138-
const auto [external_functions_extra, _7, _8, _9] =
139-
resolve_imports(*module, std::move(imported_functions_extra), {}, {}, imported_globals);
136+
const auto external_functions_extra =
137+
resolve_imported_functions(*module, imported_functions_extra);
140138
EXPECT_EQ(external_functions_extra.size(), 4);
141139

142-
auto instance_extra = instantiate(
143-
*module, external_functions_extra, {}, {}, std::vector<ExternalGlobal>(external_globals));
140+
auto instance_extra = instantiate(*module, external_functions_extra, {}, {}, external_globals);
144141

145142
EXPECT_THAT(execute(*instance_extra, 0, {}), Result(0));
146143
EXPECT_THAT(execute(*instance_extra, 1, {Value{0}}), Result(1));
@@ -154,8 +151,7 @@ TEST(api, resolve_imported_functions)
154151
{"mod2", "foo1", {ValType::i32}, ValType::i32, function_returning_value(2)},
155152
};
156153

157-
EXPECT_THROW_MESSAGE(
158-
resolve_imports(*module, std::move(imported_functions_missing), {}, {}, imported_globals),
154+
EXPECT_THROW_MESSAGE(resolve_imported_functions(*module, imported_functions_missing),
159155
instantiate_error, "imported function mod2.foo2 is required");
160156

161157

@@ -166,8 +162,7 @@ TEST(api, resolve_imported_functions)
166162
{"mod2", "foo2", {ValType::i64, ValType::i32}, std::nullopt, function_returning_void},
167163
};
168164

169-
EXPECT_THROW_MESSAGE(resolve_imports(*module, std::move(imported_functions_invalid_type1), {},
170-
{}, imported_globals),
165+
EXPECT_THROW_MESSAGE(resolve_imported_functions(*module, imported_functions_invalid_type1),
171166
instantiate_error,
172167
"function mod1.foo1 input types don't match imported function in module");
173168

@@ -178,8 +173,7 @@ TEST(api, resolve_imported_functions)
178173
{"mod2", "foo2", {ValType::i64, ValType::i32}, ValType::i64, function_returning_value(3)},
179174
};
180175

181-
EXPECT_THROW_MESSAGE(resolve_imports(*module, std::move(imported_functions_invalid_type2), {},
182-
{}, imported_globals),
176+
EXPECT_THROW_MESSAGE(resolve_imported_functions(*module, imported_functions_invalid_type2),
183177
instantiate_error, "function mod2.foo2 has output but is defined void in module");
184178

185179
std::vector<ImportedFunction> imported_functions_invalid_type3 = {
@@ -189,8 +183,7 @@ TEST(api, resolve_imported_functions)
189183
{"mod2", "foo2", {ValType::i64, ValType::i32}, std::nullopt, function_returning_void},
190184
};
191185

192-
EXPECT_THROW_MESSAGE(resolve_imports(*module, std::move(imported_functions_invalid_type3), {},
193-
{}, imported_globals),
186+
EXPECT_THROW_MESSAGE(resolve_imported_functions(*module, imported_functions_invalid_type3),
194187
instantiate_error,
195188
"function mod1.foo2 output type doesn't match imported function in module");
196189
}

Diff for: test/utils/fizzy_engine.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,11 @@ bool FizzyEngine::instantiate(bytes_view wasm_binary)
8585
try
8686
{
8787
auto module = fizzy::parse(wasm_binary);
88-
auto [imports, table, memory, globals] = fizzy::resolve_imports(*module,
89-
{
90-
{"env", "adler32", {fizzy::ValType::i32, fizzy::ValType::i32}, fizzy::ValType::i32,
91-
env_adler32},
92-
},
93-
{}, {}, {});
88+
auto imports = fizzy::resolve_imported_functions(
89+
*module, {
90+
{"env", "adler32", {fizzy::ValType::i32, fizzy::ValType::i32},
91+
fizzy::ValType::i32, env_adler32},
92+
});
9493
m_instance = fizzy::instantiate(std::move(module), std::move(imports));
9594
}
9695
catch (...)

Diff for: tools/wasi/wasi.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ bool run(int argc, const char** argv)
153153
fizzy::bytes(std::istreambuf_iterator<char>{wasm_file}, std::istreambuf_iterator<char>{});
154154

155155
auto module = fizzy::parse(wasm_binary);
156-
auto [imports, _1, _2, _3] = fizzy::resolve_imports(*module, wasi_functions, {}, {}, {});
156+
auto imports = fizzy::resolve_imported_functions(*module, wasi_functions);
157157
auto instance = fizzy::instantiate(
158158
std::move(module), std::move(imports), {}, {}, {}, fizzy::MemoryPagesValidationLimit);
159159
assert(instance != nullptr);

0 commit comments

Comments
 (0)