diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceConstructor.cpp b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceConstructor.cpp index 058bf31ae6..ca6a07d917 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceConstructor.cpp +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceConstructor.cpp @@ -29,14 +29,13 @@ JS::ThrowCompletionOr WebAssemblyInstanceConstructor::call() JS::ThrowCompletionOr WebAssemblyInstanceConstructor::construct(FunctionObject&) { auto& vm = this->vm(); - auto& global_object = this->global_object(); - auto& realm = *global_object.associated_realm(); + auto& realm = *vm.current_realm(); auto* module_argument = TRY(vm.argument(0).to_object(vm)); if (!is(module_argument)) return vm.throw_completion(JS::ErrorType::NotAnObjectOfType, "WebAssembly.Module"); auto& module_object = static_cast(*module_argument); - auto result = TRY(WebAssemblyObject::instantiate_module(module_object.module(), vm, global_object)); + auto result = TRY(WebAssemblyObject::instantiate_module(vm, module_object.module())); return heap().allocate(realm, realm, result); } diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceObject.cpp b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceObject.cpp index 0540f6d5e7..80eb85391f 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceObject.cpp +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceObject.cpp @@ -27,6 +27,8 @@ void WebAssemblyInstanceObject::initialize(JS::Realm& realm) { Object::initialize(realm); + auto& vm = this->vm(); + VERIFY(!m_exports_object); m_exports_object = create(realm, nullptr); auto& instance = this->instance(); @@ -36,7 +38,7 @@ void WebAssemblyInstanceObject::initialize(JS::Realm& realm) [&](Wasm::FunctionAddress const& address) { Optional object = cache.function_instances.get(address); if (!object.has_value()) { - object = create_native_function(realm.global_object(), address, export_.name()); + object = create_native_function(vm, address, export_.name()); cache.function_instances.set(address, *object); } m_exports_object->define_direct_property(export_.name(), *object, JS::default_attributes); diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyModuleConstructor.cpp b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyModuleConstructor.cpp index cfe9e30fc9..06969b48a6 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyModuleConstructor.cpp +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyModuleConstructor.cpp @@ -29,11 +29,10 @@ JS::ThrowCompletionOr WebAssemblyModuleConstructor::call() JS::ThrowCompletionOr WebAssemblyModuleConstructor::construct(FunctionObject&) { auto& vm = this->vm(); - auto& global_object = this->global_object(); - auto& realm = *global_object.associated_realm(); + auto& realm = *vm.current_realm(); auto* buffer_object = TRY(vm.argument(0).to_object(vm)); - auto result = TRY(parse_module(global_object, buffer_object)); + auto result = TRY(parse_module(vm, buffer_object)); return heap().allocate(realm, realm, result); } diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.cpp b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.cpp index 2c8531a20d..e25a1d996a 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.cpp +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.cpp @@ -95,7 +95,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::validate) auto buffer = TRY(vm.argument(0).to_object(vm)); // 2. Compile stableBytes as a WebAssembly module and store the results as module. - auto maybe_module = parse_module(global_object, buffer); + auto maybe_module = parse_module(vm, buffer); // 3. If module is error, return false. if (maybe_module.is_error()) @@ -116,10 +116,8 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::validate) return JS::Value(true); } -JS::ThrowCompletionOr parse_module(JS::GlobalObject& global_object, JS::Object* buffer_object) +JS::ThrowCompletionOr parse_module(JS::VM& vm, JS::Object* buffer_object) { - auto& vm = global_object.vm(); - ReadonlyBytes data; if (is(buffer_object)) { auto& buffer = static_cast(*buffer_object); @@ -170,7 +168,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::compile) return promise; } auto* buffer = buffer_or_error.release_value(); - auto result = parse_module(global_object, buffer); + auto result = parse_module(vm, buffer); if (result.is_error()) promise->reject(*result.release_error().value()); else @@ -178,7 +176,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::compile) return promise; } -JS::ThrowCompletionOr WebAssemblyObject::instantiate_module(Wasm::Module const& module, JS::VM& vm, JS::GlobalObject& global_object) +JS::ThrowCompletionOr WebAssemblyObject::instantiate_module(JS::VM& vm, Wasm::Module const& module) { Wasm::Linker linker { module }; HashMap resolved_imports; @@ -214,7 +212,7 @@ JS::ThrowCompletionOr WebAssemblyObject::instantiate_module(Wasm::Module [&](auto&, auto& arguments) -> Wasm::Result { JS::MarkedVector argument_values { vm.heap() }; for (auto& entry : arguments) - argument_values.append(to_js_value(global_object, entry)); + argument_values.append(to_js_value(vm, entry)); auto result_or_error = JS::call(vm, function, JS::js_undefined(), move(argument_values)); if (result_or_error.is_error()) { @@ -224,7 +222,7 @@ JS::ThrowCompletionOr WebAssemblyObject::instantiate_module(Wasm::Module return Wasm::Result { Vector {} }; if (type.results().size() == 1) { - auto value_or_error = to_webassembly_value(global_object, result_or_error.release_value(), type.results().first()); + auto value_or_error = to_webassembly_value(vm, result_or_error.release_value(), type.results().first()); if (value_or_error.is_error()) return Wasm::Trap {}; @@ -256,7 +254,7 @@ JS::ThrowCompletionOr WebAssemblyObject::instantiate_module(Wasm::Module // FIXME: Throw a LinkError instead. return vm.throw_completion("LinkError: Import resolution attempted to cast a BigInteger to a Number"); } - auto cast_value = TRY(to_webassembly_value(global_object, import_, type.type())); + auto cast_value = TRY(to_webassembly_value(vm, import_, type.type())); address = s_abstract_machine.store().allocate({ type.type(), false }, cast_value); } else { // FIXME: https://webassembly.github.io/spec/js-api/#read-the-imports step 5.2 @@ -334,7 +332,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::instantiate) Wasm::Module const* module { nullptr }; if (is(buffer) || is(buffer)) { - auto result = parse_module(global_object, buffer); + auto result = parse_module(vm, buffer); if (result.is_error()) { promise->reject(*result.release_error().value()); return promise; @@ -350,7 +348,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::instantiate) } VERIFY(module); - auto result = instantiate_module(*module, vm, global_object); + auto result = instantiate_module(vm, *module); if (result.is_error()) { promise->reject(*result.release_error().value()); } else { @@ -367,9 +365,9 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::instantiate) return promise; } -JS::Value to_js_value(JS::GlobalObject& global_object, Wasm::Value& wasm_value) +JS::Value to_js_value(JS::VM& vm, Wasm::Value& wasm_value) { - auto& realm = *global_object.associated_realm(); + auto& realm = *vm.current_realm(); switch (wasm_value.type().kind()) { case Wasm::ValueType::I64: return realm.heap().allocate(realm, ::Crypto::SignedBigInteger::create_from(wasm_value.to().value())); @@ -381,7 +379,7 @@ JS::Value to_js_value(JS::GlobalObject& global_object, Wasm::Value& wasm_value) return JS::Value(static_cast(wasm_value.to().value())); case Wasm::ValueType::FunctionReference: // FIXME: What's the name of a function reference that isn't exported? - return create_native_function(global_object, wasm_value.to().value().address, "FIXME_IHaveNoIdeaWhatThisShouldBeCalled"); + return create_native_function(vm, wasm_value.to().value().address, "FIXME_IHaveNoIdeaWhatThisShouldBeCalled"); case Wasm::ValueType::NullFunctionReference: return JS::js_null(); case Wasm::ValueType::ExternReference: @@ -391,10 +389,9 @@ JS::Value to_js_value(JS::GlobalObject& global_object, Wasm::Value& wasm_value) VERIFY_NOT_REACHED(); } -JS::ThrowCompletionOr to_webassembly_value(JS::GlobalObject& global_object, JS::Value value, Wasm::ValueType const& type) +JS::ThrowCompletionOr to_webassembly_value(JS::VM& vm, JS::Value value, Wasm::ValueType const& type) { static ::Crypto::SignedBigInteger two_64 = "1"_sbigint.shift_left(64); - auto& vm = global_object.vm(); switch (type.kind()) { case Wasm::ValueType::I64: { @@ -441,9 +438,9 @@ JS::ThrowCompletionOr to_webassembly_value(JS::GlobalObject& global VERIFY_NOT_REACHED(); } -JS::NativeFunction* create_native_function(JS::GlobalObject& global_object, Wasm::FunctionAddress address, String const& name) +JS::NativeFunction* create_native_function(JS::VM& vm, Wasm::FunctionAddress address, String const& name) { - auto& realm = *global_object.associated_realm(); + auto& realm = *vm.current_realm(); Optional type; WebAssemblyObject::s_abstract_machine.store().get(address)->visit([&](auto const& value) { type = value.type(); }); if (auto entry = WebAssemblyObject::s_global_cache.function_instances.get(address); entry.has_value()) @@ -460,7 +457,7 @@ JS::NativeFunction* create_native_function(JS::GlobalObject& global_object, Wasm // Grab as many values as needed and convert them. size_t index = 0; for (auto& type : type.parameters()) - values.append(TRY(to_webassembly_value(global_object, vm.argument(index++), type))); + values.append(TRY(to_webassembly_value(vm, vm.argument(index++), type))); auto result = WebAssemblyObject::s_abstract_machine.invoke(address, move(values)); // FIXME: Use the convoluted mapping of errors defined in the spec. @@ -471,11 +468,11 @@ JS::NativeFunction* create_native_function(JS::GlobalObject& global_object, Wasm return JS::js_undefined(); if (result.values().size() == 1) - return to_js_value(global_object, result.values().first()); + return to_js_value(vm, result.values().first()); Vector result_values; for (auto& entry : result.values()) - result_values.append(to_js_value(global_object, entry)); + result_values.append(to_js_value(vm, entry)); return JS::Value(JS::Array::create_from(realm, result_values)); }); diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.h b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.h index 35080471cd..cb4f5fae1c 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.h +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.h @@ -14,10 +14,10 @@ namespace Web::Bindings { class WebAssemblyMemoryObject; -JS::ThrowCompletionOr parse_module(JS::GlobalObject& global_object, JS::Object* buffer); -JS::NativeFunction* create_native_function(JS::GlobalObject& global_object, Wasm::FunctionAddress address, String const& name); -JS::Value to_js_value(JS::GlobalObject& global_object, Wasm::Value& wasm_value); -JS::ThrowCompletionOr to_webassembly_value(JS::GlobalObject& global_object, JS::Value value, Wasm::ValueType const& type); +JS::ThrowCompletionOr parse_module(JS::VM&, JS::Object* buffer); +JS::NativeFunction* create_native_function(JS::VM&, Wasm::FunctionAddress address, String const& name); +JS::Value to_js_value(JS::VM&, Wasm::Value& wasm_value); +JS::ThrowCompletionOr to_webassembly_value(JS::VM&, JS::Value value, Wasm::ValueType const& type); class WebAssemblyObject final : public JS::Object { JS_OBJECT(WebAssemblyObject, JS::Object); @@ -29,7 +29,7 @@ public: virtual void visit_edges(Cell::Visitor&) override; - static JS::ThrowCompletionOr instantiate_module(Wasm::Module const&, JS::VM&, JS::GlobalObject&); + static JS::ThrowCompletionOr instantiate_module(JS::VM&, Wasm::Module const&); struct CompiledWebAssemblyModule { explicit CompiledWebAssemblyModule(Wasm::Module&& module) diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTableConstructor.cpp b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTableConstructor.cpp index 4cbfea0419..951099eddd 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTableConstructor.cpp +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTableConstructor.cpp @@ -65,7 +65,7 @@ JS::ThrowCompletionOr WebAssemblyTableConstructor::construct(Functi if (value_value.is_undefined()) return Wasm::Value(*reference_type, 0ull); - return to_webassembly_value(global_object, value_value, *reference_type); + return to_webassembly_value(vm, value_value, *reference_type); }()); auto& reference = reference_value.value().get(); diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTablePrototype.cpp b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTablePrototype.cpp index 65095298b6..e1d07a9c9b 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTablePrototype.cpp +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTablePrototype.cpp @@ -38,7 +38,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::grow) if (value_value.is_undefined()) return Wasm::Value(table->type().element_type(), 0ull); - return to_webassembly_value(global_object, value_value, table->type().element_type()); + return to_webassembly_value(vm, value_value, table->type().element_type()); }()); auto& reference = reference_value.value().get(); @@ -70,7 +70,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::get) return JS::js_undefined(); Wasm::Value wasm_value { ref.value() }; - return to_js_value(global_object, wasm_value); + return to_js_value(vm, wasm_value); } JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::set) @@ -94,7 +94,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::set) if (value_value.is_undefined()) return Wasm::Value(table->type().element_type(), 0ull); - return to_webassembly_value(global_object, value_value, table->type().element_type()); + return to_webassembly_value(vm, value_value, table->type().element_type()); }()); auto& reference = reference_value.value().get();