LibWasm: Make the instantiation process produce an OwnPtr

Managing the instantiated modules becomes a pain if they're on the
stack, since an instantiated module will eventually reference itself.
To make using this simpler, just avoid copying the instance.
This commit is contained in:
Ali Mohammad Pur 2021-05-11 04:44:59 +04:30 committed by Linus Groh
parent efb106069b
commit 3283c8a495
Notes: sideshowbarker 2024-07-18 17:39:42 +09:00
5 changed files with 15 additions and 20 deletions

View file

@ -60,7 +60,7 @@ private:
static Wasm::AbstractMachine m_machine;
Optional<Wasm::Module> m_module;
Optional<Wasm::ModuleInstance> m_module_instance;
OwnPtr<Wasm::ModuleInstance> m_module_instance;
};
Wasm::AbstractMachine WebAssemblyModule::m_machine;
@ -149,15 +149,6 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke)
auto address = static_cast<unsigned long>(vm.argument(0).to_double(global_object));
if (vm.exception())
return {};
auto this_value = vm.this_value(global_object);
auto object = this_value.to_object(global_object);
if (vm.exception())
return {};
if (!object || !is<WebAssemblyModule>(object)) {
vm.throw_exception<JS::TypeError>(global_object, "Not a WebAssemblyModule");
return {};
}
auto instance = static_cast<WebAssemblyModule*>(object);
Wasm::FunctionAddress function_address { address };
auto function_instance = WebAssemblyModule::machine().store().get(function_address);
if (!function_instance) {

View file

@ -85,7 +85,8 @@ GlobalInstance* Store::get(GlobalAddress address)
InstantiationResult AbstractMachine::instantiate(const Module& module, Vector<ExternValue> externs)
{
ModuleInstance main_module_instance;
auto main_module_instance_pointer = make<ModuleInstance>();
auto& main_module_instance = *main_module_instance_pointer;
Optional<InstantiationResult> instantiation_result;
module.for_each_section_of_type<TypeSection>([&](const TypeSection& section) {
@ -181,7 +182,10 @@ InstantiationResult AbstractMachine::instantiate(const Module& module, Vector<Ex
invoke(functions[index.value()], {});
});
return instantiation_result.value_or(move(main_module_instance));
if (instantiation_result.has_value())
return instantiation_result.release_value();
return InstantiationResult { move(main_module_instance_pointer) };
}
Optional<InstantiationError> AbstractMachine::allocate_all(const Module& module, ModuleInstance& module_instance, Vector<ExternValue>& externs, Vector<Value>& global_values)

View file

@ -219,7 +219,7 @@ public:
auto& code() const { return m_code; }
private:
const FunctionType& m_type;
FunctionType m_type;
const ModuleInstance& m_module;
const Module::Function& m_code;
};
@ -237,7 +237,7 @@ public:
private:
FlatPtr m_ptr { 0 };
const FunctionType& m_type;
FunctionType m_type;
};
using FunctionInstance = Variant<WasmFunction, HostFunction>;
@ -418,7 +418,7 @@ private:
Vector<EntryType> m_data;
};
using InstantiationResult = AK::Result<ModuleInstance, InstantiationError>;
using InstantiationResult = AK::Result<NonnullOwnPtr<ModuleInstance>, InstantiationError>;
class AbstractMachine {
public:

View file

@ -955,10 +955,10 @@ class Module {
public:
class Function {
public:
explicit Function(TypeIndex type, Vector<ValueType> local_types, const Expression& body)
explicit Function(TypeIndex type, Vector<ValueType> local_types, Expression body)
: m_type(type)
, m_local_types(move(local_types))
, m_body(body)
, m_body(move(body))
{
}
@ -969,7 +969,7 @@ public:
private:
TypeIndex m_type;
Vector<ValueType> m_local_types;
const Expression& m_body;
Expression m_body;
};
using AnySection = Variant<

View file

@ -91,7 +91,7 @@ int main(int argc, char* argv[])
};
if (print) {
// Now, let's dump the functions!
for (auto& address : module_instance.functions()) {
for (auto& address : module_instance->functions()) {
print_func(address);
}
}
@ -99,7 +99,7 @@ int main(int argc, char* argv[])
if (!exported_function_to_execute.is_empty()) {
Optional<Wasm::FunctionAddress> run_address;
Vector<Wasm::Value> values;
for (auto& entry : module_instance.exports()) {
for (auto& entry : module_instance->exports()) {
if (entry.name() == exported_function_to_execute) {
if (auto addr = entry.value().get_pointer<Wasm::FunctionAddress>())
run_address = *addr;