LibWasm: Make sure to place imported functions before the module's

aafef1e92d broke this while trying to
make the global import available in initialisation, this commit makes
sure we place the module's own functions after all resolved imports.
This commit is contained in:
Ali Mohammad Pur 2023-09-24 21:20:45 +03:30 committed by Ali Mohammad Pur
parent 78f56a0908
commit 6820e0e175
2 changed files with 9 additions and 4 deletions

View file

@ -162,11 +162,14 @@ InstantiationResult AbstractMachine::instantiate(Module const& module, Vector<Ex
auxiliary_instance.globals().append(*ptr);
}
Vector<FunctionAddress> module_functions;
module_functions.ensure_capacity(module.functions().size());
for (auto& func : module.functions()) {
auto address = m_store.allocate(main_module_instance, func);
VERIFY(address.has_value());
auxiliary_instance.functions().append(*address);
main_module_instance.functions().append(*address);
module_functions.append(*address);
}
BytecodeInterpreter interpreter(m_stack_info);
@ -193,7 +196,7 @@ InstantiationResult AbstractMachine::instantiate(Module const& module, Vector<Ex
if (instantiation_result.has_value())
return instantiation_result.release_value();
if (auto result = allocate_all_initial_phase(module, main_module_instance, externs, global_values); result.has_value())
if (auto result = allocate_all_initial_phase(module, main_module_instance, externs, global_values, module_functions); result.has_value())
return result.release_value();
module.for_each_section_of_type<ElementSection>([&](ElementSection const& section) {
@ -393,7 +396,7 @@ InstantiationResult AbstractMachine::instantiate(Module const& module, Vector<Ex
return InstantiationResult { move(main_module_instance_pointer) };
}
Optional<InstantiationError> AbstractMachine::allocate_all_initial_phase(Module const& module, ModuleInstance& module_instance, Vector<ExternValue>& externs, Vector<Value>& global_values)
Optional<InstantiationError> AbstractMachine::allocate_all_initial_phase(Module const& module, ModuleInstance& module_instance, Vector<ExternValue>& externs, Vector<Value>& global_values, Vector<FunctionAddress>& own_functions)
{
Optional<InstantiationError> result;
@ -405,6 +408,8 @@ Optional<InstantiationError> AbstractMachine::allocate_all_initial_phase(Module
[&](GlobalAddress const& address) { module_instance.globals().append(address); });
}
module_instance.functions().extend(own_functions);
// FIXME: What if this fails?
module.for_each_section_of_type<TableSection>([&](TableSection const& section) {

View file

@ -623,7 +623,7 @@ public:
void enable_instruction_count_limit() { m_should_limit_instruction_count = true; }
private:
Optional<InstantiationError> allocate_all_initial_phase(Module const&, ModuleInstance&, Vector<ExternValue>&, Vector<Value>& global_values);
Optional<InstantiationError> allocate_all_initial_phase(Module const&, ModuleInstance&, Vector<ExternValue>&, Vector<Value>& global_values, Vector<FunctionAddress>& own_functions);
Optional<InstantiationError> allocate_all_final_phase(Module const&, ModuleInstance&, Vector<Vector<Reference>>& elements);
Store m_store;
StackInfo m_stack_info;