LibWeb: Port WebAssembly.Instance to IDL

This commit is contained in:
Timothy Flynn 2023-03-15 18:15:28 -04:00 committed by Andreas Kling
parent fb1f15774f
commit de32c44762
Notes: sideshowbarker 2024-07-16 23:34:44 +09:00
17 changed files with 152 additions and 279 deletions

View file

@ -37,6 +37,7 @@ static bool is_platform_object(Type const& type)
"FileList"sv,
"FormData"sv,
"ImageData"sv,
"Instance"sv,
"Module"sv,
"MutationRecord"sv,
"NamedNodeMap"sv,

View file

@ -104,7 +104,6 @@ class @legacy_constructor_class@;)~~~");
{
auto gen = generator.fork();
add_interface(gen, "WebAssemblyMemoryPrototype"sv, "WebAssemblyMemoryConstructor"sv, {});
add_interface(gen, "WebAssemblyInstancePrototype"sv, "WebAssemblyInstanceConstructor"sv, {});
add_interface(gen, "WebAssemblyTablePrototype"sv, "WebAssemblyTableConstructor"sv, {});
}
@ -156,8 +155,6 @@ static ErrorOr<void> generate_intrinsic_definitions(StringView output_path, Vect
generator.append(R"~~~(
#include <LibWeb/WebAssembly/WebAssemblyMemoryConstructor.h>
#include <LibWeb/WebAssembly/WebAssemblyMemoryPrototype.h>
#include <LibWeb/WebAssembly/WebAssemblyInstanceConstructor.h>
#include <LibWeb/WebAssembly/WebAssemblyInstanceObjectPrototype.h>
#include <LibWeb/WebAssembly/WebAssemblyTableConstructor.h>
#include <LibWeb/WebAssembly/WebAssemblyTablePrototype.h>)~~~");
@ -229,7 +226,6 @@ void Intrinsics::create_web_prototype_and_constructor<@prototype_class@>(JS::Rea
{
auto gen = generator.fork();
add_interface(gen, "WebAssembly.Memory"sv, "WebAssemblyMemoryPrototype"sv, "WebAssemblyMemoryConstructor"sv, {});
add_interface(gen, "WebAssembly.Instance"sv, "WebAssemblyInstancePrototype"sv, "WebAssemblyInstanceConstructor"sv, {});
add_interface(gen, "WebAssembly.Table"sv, "WebAssemblyTablePrototype"sv, "WebAssemblyTableConstructor"sv, {});
}

View file

@ -442,10 +442,8 @@ set(SOURCES
URL/URL.cpp
URL/URLSearchParams.cpp
URL/URLSearchParamsIterator.cpp
WebAssembly/Instance.cpp
WebAssembly/Module.cpp
WebAssembly/WebAssemblyInstanceConstructor.cpp
WebAssembly/WebAssemblyInstanceObject.cpp
WebAssembly/WebAssemblyInstanceObjectPrototype.cpp
WebAssembly/WebAssemblyMemoryConstructor.cpp
WebAssembly/WebAssemblyMemoryPrototype.cpp
WebAssembly/WebAssemblyObject.cpp

View file

@ -482,6 +482,7 @@ class ResourceLoader;
}
namespace Web::WebAssembly {
class Instance;
class Module;
}

View file

@ -0,0 +1,98 @@
/*
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/Realm.h>
#include <LibJS/Runtime/VM.h>
#include <LibWasm/AbstractMachine/AbstractMachine.h>
#include <LibWeb/Bindings/InstancePrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/WebAssembly/Instance.h>
#include <LibWeb/WebAssembly/Module.h>
#include <LibWeb/WebAssembly/WebAssemblyObject.h>
#include <LibWeb/WebAssembly/WebAssemblyTableObject.h>
namespace Web::WebAssembly {
WebIDL::ExceptionOr<JS::NonnullGCPtr<Instance>> Instance::construct_impl(JS::Realm& realm, Module& module, Optional<JS::Handle<JS::Object>>& import_object)
{
// FIXME: Implement the importObject parameter.
(void)import_object;
auto& vm = realm.vm();
auto index = TRY(Bindings::WebAssemblyObject::instantiate_module(vm, module.module()));
return MUST_OR_THROW_OOM(vm.heap().allocate<Instance>(realm, realm, index));
}
Instance::Instance(JS::Realm& realm, size_t index)
: Bindings::PlatformObject(realm)
, m_exports(Object::create(realm, nullptr))
, m_index(index)
{
}
JS::ThrowCompletionOr<void> Instance::initialize(JS::Realm& realm)
{
auto& vm = this->vm();
MUST_OR_THROW_OOM(Base::initialize(realm));
set_prototype(&Bindings::ensure_web_prototype<Bindings::InstancePrototype>(realm, "WebAssembly.Instance"sv));
auto& instance = *Bindings::WebAssemblyObject::s_instantiated_modules[m_index];
auto& cache = Bindings::WebAssemblyObject::s_module_caches.at(m_index);
for (auto& export_ : instance.exports()) {
TRY(export_.value().visit(
[&](Wasm::FunctionAddress const& address) -> JS::ThrowCompletionOr<void> {
Optional<JS::GCPtr<JS::FunctionObject>> object = cache.function_instances.get(address);
if (!object.has_value()) {
object = Bindings::create_native_function(vm, address, export_.name());
cache.function_instances.set(address, *object);
}
m_exports->define_direct_property(export_.name(), *object, JS::default_attributes);
return {};
},
[&](Wasm::MemoryAddress const& address) -> JS::ThrowCompletionOr<void> {
Optional<JS::GCPtr<Bindings::WebAssemblyMemoryObject>> object = cache.memory_instances.get(address);
if (!object.has_value()) {
object = MUST_OR_THROW_OOM(heap().allocate<Web::Bindings::WebAssemblyMemoryObject>(realm, realm, address));
cache.memory_instances.set(address, *object);
}
m_exports->define_direct_property(export_.name(), *object, JS::default_attributes);
return {};
},
[&](Wasm::TableAddress const& address) -> JS::ThrowCompletionOr<void> {
Optional<JS::GCPtr<Bindings::WebAssemblyTableObject>> object = cache.table_instances.get(address);
if (!object.has_value()) {
object = MUST_OR_THROW_OOM(heap().allocate<Web::Bindings::WebAssemblyTableObject>(realm, realm, address));
cache.table_instances.set(address, *object);
}
m_exports->define_direct_property(export_.name(), *object, JS::default_attributes);
return {};
},
[&](auto const&) -> JS::ThrowCompletionOr<void> {
// FIXME: Implement other exports!
return {};
}));
}
MUST(m_exports->set_integrity_level(IntegrityLevel::Frozen));
return {};
}
void Instance::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_exports);
}
}

View file

@ -0,0 +1,37 @@
/*
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Optional.h>
#include <LibJS/Forward.h>
#include <LibJS/Heap/GCPtr.h>
#include <LibJS/Heap/Handle.h>
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/Bindings/PlatformObject.h>
namespace Web::WebAssembly {
class Instance : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(Instance, Bindings::PlatformObject);
public:
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Instance>> construct_impl(JS::Realm&, Module& module, Optional<JS::Handle<JS::Object>>& import_object);
Object const* exports() const { return m_exports.ptr(); }
private:
Instance(JS::Realm&, size_t index);
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
virtual void visit_edges(Visitor&) override;
JS::NonnullGCPtr<Object> m_exports;
size_t m_index { 0 };
};
}

View file

@ -0,0 +1,9 @@
#import <WebAssembly/Module.idl>
// https://webassembly.github.io/spec/js-api/#instances
[LegacyNamespace=WebAssembly, Exposed=*]
interface Instance {
constructor(Module module, optional object importObject);
readonly attribute object exports;
};

View file

@ -1,53 +0,0 @@
/*
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/GlobalObject.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/WebAssembly/Module.h>
#include <LibWeb/WebAssembly/WebAssemblyInstanceConstructor.h>
#include <LibWeb/WebAssembly/WebAssemblyInstanceObject.h>
#include <LibWeb/WebAssembly/WebAssemblyInstanceObjectPrototype.h>
#include <LibWeb/WebAssembly/WebAssemblyObject.h>
namespace Web::Bindings {
WebAssemblyInstanceConstructor::WebAssemblyInstanceConstructor(JS::Realm& realm)
: NativeFunction(*realm.intrinsics().function_prototype())
{
}
WebAssemblyInstanceConstructor::~WebAssemblyInstanceConstructor() = default;
JS::ThrowCompletionOr<JS::Value> WebAssemblyInstanceConstructor::call()
{
return vm().throw_completion<JS::TypeError>(JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Instance");
}
JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Object>> WebAssemblyInstanceConstructor::construct(FunctionObject&)
{
auto& vm = this->vm();
auto& realm = *vm.current_realm();
auto* module_argument = TRY(vm.argument(0).to_object(vm));
if (!is<WebAssembly::Module>(module_argument))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WebAssembly.Module");
auto& module_object = static_cast<WebAssembly::Module&>(*module_argument);
auto result = TRY(WebAssemblyObject::instantiate_module(vm, module_object.module()));
return MUST_OR_THROW_OOM(heap().allocate<WebAssemblyInstanceObject>(realm, realm, result));
}
JS::ThrowCompletionOr<void> WebAssemblyInstanceConstructor::initialize(JS::Realm& realm)
{
auto& vm = this->vm();
MUST_OR_THROW_OOM(NativeFunction::initialize(realm));
define_direct_property(vm.names.prototype, &Bindings::ensure_web_prototype<WebAssemblyInstancePrototype>(realm, "WebAssembly.Instance"), 0);
define_direct_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable);
return {};
}
}

View file

@ -1,28 +0,0 @@
/*
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/NativeFunction.h>
namespace Web::Bindings {
class WebAssemblyInstanceConstructor : public JS::NativeFunction {
JS_OBJECT(WebAssemblyInstanceConstructor, JS::NativeFunction);
public:
explicit WebAssemblyInstanceConstructor(JS::Realm&);
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
virtual ~WebAssemblyInstanceConstructor() override;
virtual JS::ThrowCompletionOr<JS::Value> call() override;
virtual JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Object>> construct(JS::FunctionObject& new_target) override;
private:
virtual bool has_constructor() const override { return true; }
};
}

View file

@ -1,83 +0,0 @@
/*
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/ScopeGuard.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/BigInt.h>
#include <LibJS/Runtime/TypedArray.h>
#include <LibWasm/AbstractMachine/Interpreter.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/WebAssembly/WebAssemblyInstanceObject.h>
#include <LibWeb/WebAssembly/WebAssemblyMemoryPrototype.h>
#include <LibWeb/WebAssembly/WebAssemblyObject.h>
#include <LibWeb/WebAssembly/WebAssemblyTableObject.h>
namespace Web::Bindings {
WebAssemblyInstanceObject::WebAssemblyInstanceObject(JS::Realm& realm, size_t index)
: Object(ConstructWithPrototypeTag::Tag, Bindings::ensure_web_prototype<WebAssemblyInstancePrototype>(realm, "WebAssembly.Instance"))
, m_index(index)
{
}
JS::ThrowCompletionOr<void> WebAssemblyInstanceObject::initialize(JS::Realm& realm)
{
MUST_OR_THROW_OOM(Object::initialize(realm));
auto& vm = this->vm();
VERIFY(!m_exports_object);
m_exports_object = create(realm, nullptr);
auto& instance = this->instance();
auto& cache = this->cache();
for (auto& export_ : instance.exports()) {
TRY(export_.value().visit(
[&](Wasm::FunctionAddress const& address) -> JS::ThrowCompletionOr<void> {
Optional<JS::GCPtr<JS::FunctionObject>> object = cache.function_instances.get(address);
if (!object.has_value()) {
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);
return {};
},
[&](Wasm::MemoryAddress const& address) -> JS::ThrowCompletionOr<void> {
Optional<JS::GCPtr<WebAssemblyMemoryObject>> object = cache.memory_instances.get(address);
if (!object.has_value()) {
object = MUST_OR_THROW_OOM(heap().allocate<Web::Bindings::WebAssemblyMemoryObject>(realm, realm, address));
cache.memory_instances.set(address, *object);
}
m_exports_object->define_direct_property(export_.name(), *object, JS::default_attributes);
return {};
},
[&](Wasm::TableAddress const& address) -> JS::ThrowCompletionOr<void> {
Optional<JS::GCPtr<WebAssemblyTableObject>> object = cache.table_instances.get(address);
if (!object.has_value()) {
object = MUST_OR_THROW_OOM(heap().allocate<Web::Bindings::WebAssemblyTableObject>(realm, realm, address));
cache.table_instances.set(address, *object);
}
m_exports_object->define_direct_property(export_.name(), *object, JS::default_attributes);
return {};
},
[&](auto const&) -> JS::ThrowCompletionOr<void> {
// FIXME: Implement other exports!
return {};
}));
}
MUST(m_exports_object->set_integrity_level(IntegrityLevel::Frozen));
return {};
}
void WebAssemblyInstanceObject::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_exports_object);
}
}

View file

@ -1,39 +0,0 @@
/*
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/VM.h>
#include <LibWasm/AbstractMachine/AbstractMachine.h>
#include <LibWeb/Forward.h>
#include <LibWeb/WebAssembly/WebAssemblyObject.h>
namespace Web::Bindings {
class WebAssemblyInstanceObject final : public JS::Object {
JS_OBJECT(WebAssemblyInstanceObject, Object);
public:
explicit WebAssemblyInstanceObject(JS::Realm&, size_t index);
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
virtual ~WebAssemblyInstanceObject() override = default;
size_t index() const { return m_index; }
Wasm::ModuleInstance& instance() const { return *WebAssemblyObject::s_instantiated_modules[m_index]; }
auto& cache() { return WebAssemblyObject::s_module_caches.at(m_index); }
void visit_edges(Visitor&) override;
friend class WebAssemblyInstancePrototype;
private:
size_t m_index { 0 };
JS::GCPtr<Object> m_exports_object;
};
}

View file

@ -1,31 +0,0 @@
/*
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "WebAssemblyInstanceObject.h"
#include <AK/TypeCasts.h>
#include <LibWeb/WebAssembly/WebAssemblyInstanceObjectPrototype.h>
namespace Web::Bindings {
JS::ThrowCompletionOr<void> WebAssemblyInstancePrototype::initialize(JS::Realm& realm)
{
MUST_OR_THROW_OOM(Object::initialize(realm));
define_native_accessor(realm, "exports", exports_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
return {};
}
JS_DEFINE_NATIVE_FUNCTION(WebAssemblyInstancePrototype::exports_getter)
{
auto this_value = vm.this_value();
auto* this_object = TRY(this_value.to_object(vm));
if (!is<WebAssemblyInstanceObject>(this_object))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WebAssembly.Instance");
auto object = static_cast<WebAssemblyInstanceObject*>(this_object);
return object->m_exports_object;
}
}

View file

@ -1,32 +0,0 @@
/*
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/VM.h>
#include <LibWeb/Forward.h>
namespace Web::Bindings {
class WebAssemblyInstancePrototype final : public JS::Object {
JS_OBJECT(WebAssemblyInstancePrototype, Object);
public:
explicit WebAssemblyInstancePrototype(JS::Realm& realm)
: JS::Object(ConstructWithPrototypeTag::Tag, *realm.intrinsics().object_prototype())
{
}
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
private:
JS_DECLARE_NATIVE_FUNCTION(exports_getter);
static JS::Handle<WebAssemblyInstancePrototype> s_instance;
};
}

View file

@ -4,7 +4,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "WebAssemblyInstanceObject.h"
#include "WebAssemblyMemoryPrototype.h"
#include "WebAssemblyTableObject.h"
#include "WebAssemblyTablePrototype.h"
@ -19,10 +18,11 @@
#include <LibJS/Runtime/TypedArray.h>
#include <LibWasm/AbstractMachine/Interpreter.h>
#include <LibWasm/AbstractMachine/Validator.h>
#include <LibWeb/Bindings/InstancePrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/ModulePrototype.h>
#include <LibWeb/WebAssembly/Instance.h>
#include <LibWeb/WebAssembly/Module.h>
#include <LibWeb/WebAssembly/WebAssemblyInstanceConstructor.h>
#include <LibWeb/WebAssembly/WebAssemblyObject.h>
namespace Web::Bindings {
@ -45,7 +45,7 @@ JS::ThrowCompletionOr<void> WebAssemblyObject::initialize(JS::Realm& realm)
auto& memory_constructor = Bindings::ensure_web_constructor<WebAssemblyMemoryPrototype>(realm, "WebAssembly.Memory"sv);
define_direct_property("Memory", &memory_constructor, JS::Attribute::Writable | JS::Attribute::Configurable);
auto& instance_constructor = Bindings::ensure_web_constructor<WebAssemblyInstancePrototype>(realm, "WebAssembly.Instance"sv);
auto& instance_constructor = Bindings::ensure_web_constructor<InstancePrototype>(realm, "WebAssembly.Instance"sv);
define_direct_property("Instance", &instance_constructor, JS::Attribute::Writable | JS::Attribute::Configurable);
auto& module_constructor = Bindings::ensure_web_constructor<ModulePrototype>(realm, "WebAssembly.Module"sv);
@ -345,7 +345,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::instantiate)
if (result.is_error()) {
promise->reject(*result.release_error().value());
} else {
auto instance_object = MUST_OR_THROW_OOM(vm.heap().allocate<WebAssemblyInstanceObject>(realm, realm, result.release_value()));
auto instance_object = MUST_OR_THROW_OOM(vm.heap().allocate<WebAssembly::Instance>(realm, realm, result.release_value()));
if (should_return_module) {
auto object = JS::Object::create(realm, nullptr);
object->define_direct_property("module", MUST_OR_THROW_OOM(vm.heap().allocate<WebAssembly::Module>(realm, realm, s_compiled_modules.size() - 1)), JS::default_attributes);

View file

@ -9,7 +9,6 @@
#include <LibJS/Runtime/Object.h>
#include <LibWasm/AbstractMachine/AbstractMachine.h>
#include <LibWeb/Forward.h>
#include <LibWeb/WebAssembly/WebAssemblyInstanceObjectPrototype.h>
namespace Web::Bindings {

View file

@ -9,7 +9,6 @@
#include <LibJS/Runtime/Object.h>
#include <LibWasm/AbstractMachine/AbstractMachine.h>
#include <LibWeb/Forward.h>
#include <LibWeb/WebAssembly/WebAssemblyInstanceObjectPrototype.h>
#include <LibWeb/WebAssembly/WebAssemblyObject.h>
namespace Web::Bindings {

View file

@ -200,6 +200,7 @@ libweb_js_bindings(UIEvents/UIEvent)
libweb_js_bindings(UIEvents/WheelEvent)
libweb_js_bindings(URL/URL)
libweb_js_bindings(URL/URLSearchParams ITERABLE)
libweb_js_bindings(WebAssembly/Instance)
libweb_js_bindings(WebAssembly/Module)
libweb_js_bindings(WebGL/WebGLContextEvent)
libweb_js_bindings(WebGL/WebGLRenderingContext)