mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-05 01:42:54 +00:00
LibGC+Everywhere: Factor out a LibGC from LibJS
Resulting in a massive rename across almost everywhere! Alongside the namespace change, we now have the following names: * JS::NonnullGCPtr -> GC::Ref * JS::GCPtr -> GC::Ptr * JS::HeapFunction -> GC::Function * JS::CellImpl -> GC::Cell * JS::Handle -> GC::Root
This commit is contained in:
parent
ce23efc5f6
commit
f87041bf3a
Notes:
github-actions[bot]
2024-11-15 13:50:17 +00:00
Author: https://github.com/shannonbooth
Commit: f87041bf3a
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2345
1722 changed files with 9939 additions and 9906 deletions
|
@ -19,11 +19,11 @@
|
|||
|
||||
namespace Web::WebAssembly {
|
||||
|
||||
JS_DEFINE_ALLOCATOR(Instance);
|
||||
GC_DEFINE_ALLOCATOR(Instance);
|
||||
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<Instance>> Instance::construct_impl(JS::Realm& realm, Module& module, Optional<JS::Handle<JS::Object>>& import_object_handle)
|
||||
WebIDL::ExceptionOr<GC::Ref<Instance>> Instance::construct_impl(JS::Realm& realm, Module& module, Optional<GC::Root<JS::Object>>& import_object_handle)
|
||||
{
|
||||
JS::GCPtr<JS::Object> import_object = import_object_handle.has_value() ? import_object_handle.value().ptr() : nullptr;
|
||||
GC::Ptr<JS::Object> import_object = import_object_handle.has_value() ? import_object_handle.value().ptr() : nullptr;
|
||||
|
||||
auto& vm = realm.vm();
|
||||
|
||||
|
@ -48,7 +48,7 @@ void Instance::initialize(JS::Realm& realm)
|
|||
for (auto& export_ : m_module_instance->exports()) {
|
||||
export_.value().visit(
|
||||
[&](Wasm::FunctionAddress const& address) {
|
||||
Optional<JS::GCPtr<JS::FunctionObject>> object = m_function_instances.get(address);
|
||||
Optional<GC::Ptr<JS::FunctionObject>> object = m_function_instances.get(address);
|
||||
if (!object.has_value()) {
|
||||
object = Detail::create_native_function(vm, address, export_.name(), this);
|
||||
m_function_instances.set(address, *object);
|
||||
|
@ -57,7 +57,7 @@ void Instance::initialize(JS::Realm& realm)
|
|||
m_exports->define_direct_property(export_.name(), *object, JS::default_attributes);
|
||||
},
|
||||
[&](Wasm::MemoryAddress const& address) {
|
||||
Optional<JS::GCPtr<Memory>> object = m_memory_instances.get(address);
|
||||
Optional<GC::Ptr<Memory>> object = m_memory_instances.get(address);
|
||||
if (!object.has_value()) {
|
||||
object = realm.create<Memory>(realm, address);
|
||||
m_memory_instances.set(address, *object);
|
||||
|
@ -66,7 +66,7 @@ void Instance::initialize(JS::Realm& realm)
|
|||
m_exports->define_direct_property(export_.name(), *object, JS::default_attributes);
|
||||
},
|
||||
[&](Wasm::TableAddress const& address) {
|
||||
Optional<JS::GCPtr<Table>> object = m_table_instances.get(address);
|
||||
Optional<GC::Ptr<Table>> object = m_table_instances.get(address);
|
||||
if (!object.has_value()) {
|
||||
object = realm.create<Table>(realm, address);
|
||||
m_table_instances.set(address, *object);
|
||||
|
|
|
@ -8,9 +8,9 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Optional.h>
|
||||
#include <LibGC/Ptr.h>
|
||||
#include <LibGC/Root.h>
|
||||
#include <LibJS/Forward.h>
|
||||
#include <LibJS/Heap/GCPtr.h>
|
||||
#include <LibJS/Heap/Handle.h>
|
||||
#include <LibWasm/AbstractMachine/AbstractMachine.h>
|
||||
#include <LibWeb/Bindings/ExceptionOrUtils.h>
|
||||
#include <LibWeb/Bindings/PlatformObject.h>
|
||||
|
@ -20,10 +20,10 @@ namespace Web::WebAssembly {
|
|||
|
||||
class Instance : public Bindings::PlatformObject {
|
||||
WEB_PLATFORM_OBJECT(Instance, Bindings::PlatformObject);
|
||||
JS_DECLARE_ALLOCATOR(Instance);
|
||||
GC_DECLARE_ALLOCATOR(Instance);
|
||||
|
||||
public:
|
||||
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Instance>> construct_impl(JS::Realm&, Module& module, Optional<JS::Handle<JS::Object>>& import_object);
|
||||
static WebIDL::ExceptionOr<GC::Ref<Instance>> construct_impl(JS::Realm&, Module& module, Optional<GC::Root<JS::Object>>& import_object);
|
||||
|
||||
Object const* exports() const { return m_exports.ptr(); }
|
||||
|
||||
|
@ -33,11 +33,11 @@ private:
|
|||
virtual void initialize(JS::Realm&) override;
|
||||
virtual void visit_edges(Visitor&) override;
|
||||
|
||||
JS::NonnullGCPtr<Object> m_exports;
|
||||
GC::Ref<Object> m_exports;
|
||||
NonnullOwnPtr<Wasm::ModuleInstance> m_module_instance;
|
||||
HashMap<Wasm::FunctionAddress, JS::GCPtr<JS::FunctionObject>> m_function_instances;
|
||||
HashMap<Wasm::MemoryAddress, JS::GCPtr<WebAssembly::Memory>> m_memory_instances;
|
||||
HashMap<Wasm::TableAddress, JS::GCPtr<WebAssembly::Table>> m_table_instances;
|
||||
HashMap<Wasm::FunctionAddress, GC::Ptr<JS::FunctionObject>> m_function_instances;
|
||||
HashMap<Wasm::MemoryAddress, GC::Ptr<WebAssembly::Memory>> m_memory_instances;
|
||||
HashMap<Wasm::TableAddress, GC::Ptr<WebAssembly::Table>> m_table_instances;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -15,9 +15,9 @@
|
|||
|
||||
namespace Web::WebAssembly {
|
||||
|
||||
JS_DEFINE_ALLOCATOR(Memory);
|
||||
GC_DEFINE_ALLOCATOR(Memory);
|
||||
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<Memory>> Memory::construct_impl(JS::Realm& realm, MemoryDescriptor& descriptor)
|
||||
WebIDL::ExceptionOr<GC::Ref<Memory>> Memory::construct_impl(JS::Realm& realm, MemoryDescriptor& descriptor)
|
||||
{
|
||||
auto& vm = realm.vm();
|
||||
|
||||
|
@ -92,7 +92,7 @@ WebIDL::ExceptionOr<void> Memory::reset_the_memory_buffer()
|
|||
}
|
||||
|
||||
// https://webassembly.github.io/spec/js-api/#dom-memory-buffer
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> Memory::buffer() const
|
||||
WebIDL::ExceptionOr<GC::Ref<JS::ArrayBuffer>> Memory::buffer() const
|
||||
{
|
||||
auto& vm = this->vm();
|
||||
auto& realm = *vm.current_realm();
|
||||
|
@ -100,11 +100,11 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> Memory::buffer() const
|
|||
if (!m_buffer)
|
||||
m_buffer = TRY(create_a_memory_buffer(vm, realm, m_address));
|
||||
|
||||
return JS::NonnullGCPtr(*m_buffer);
|
||||
return GC::Ref(*m_buffer);
|
||||
}
|
||||
|
||||
// https://webassembly.github.io/spec/js-api/#create-a-memory-buffer
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> Memory::create_a_memory_buffer(JS::VM& vm, JS::Realm& realm, Wasm::MemoryAddress address)
|
||||
WebIDL::ExceptionOr<GC::Ref<JS::ArrayBuffer>> Memory::create_a_memory_buffer(JS::VM& vm, JS::Realm& realm, Wasm::MemoryAddress address)
|
||||
{
|
||||
auto& context = Detail::get_cache(realm);
|
||||
auto* memory = context.abstract_machine().store().get(address);
|
||||
|
@ -114,7 +114,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> Memory::create_a_memory_b
|
|||
auto array_buffer = JS::ArrayBuffer::create(realm, &memory->data());
|
||||
array_buffer->set_detach_key(JS::PrimitiveString::create(vm, "WebAssembly.Memory"_string));
|
||||
|
||||
return JS::NonnullGCPtr(*array_buffer);
|
||||
return GC::Ref(*array_buffer);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,8 +8,8 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Optional.h>
|
||||
#include <LibGC/Ptr.h>
|
||||
#include <LibJS/Forward.h>
|
||||
#include <LibJS/Heap/GCPtr.h>
|
||||
#include <LibJS/Runtime/ArrayBuffer.h>
|
||||
#include <LibWasm/AbstractMachine/AbstractMachine.h>
|
||||
#include <LibWeb/Bindings/ExceptionOrUtils.h>
|
||||
|
@ -24,13 +24,13 @@ struct MemoryDescriptor {
|
|||
|
||||
class Memory : public Bindings::PlatformObject {
|
||||
WEB_PLATFORM_OBJECT(Memory, Bindings::PlatformObject);
|
||||
JS_DECLARE_ALLOCATOR(Memory);
|
||||
GC_DECLARE_ALLOCATOR(Memory);
|
||||
|
||||
public:
|
||||
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Memory>> construct_impl(JS::Realm&, MemoryDescriptor& descriptor);
|
||||
static WebIDL::ExceptionOr<GC::Ref<Memory>> construct_impl(JS::Realm&, MemoryDescriptor& descriptor);
|
||||
|
||||
WebIDL::ExceptionOr<u32> grow(u32 delta);
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> buffer() const;
|
||||
WebIDL::ExceptionOr<GC::Ref<JS::ArrayBuffer>> buffer() const;
|
||||
|
||||
Wasm::MemoryAddress address() const { return m_address; }
|
||||
|
||||
|
@ -41,10 +41,10 @@ private:
|
|||
virtual void visit_edges(Visitor&) override;
|
||||
|
||||
WebIDL::ExceptionOr<void> reset_the_memory_buffer();
|
||||
static WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> create_a_memory_buffer(JS::VM&, JS::Realm&, Wasm::MemoryAddress);
|
||||
static WebIDL::ExceptionOr<GC::Ref<JS::ArrayBuffer>> create_a_memory_buffer(JS::VM&, JS::Realm&, Wasm::MemoryAddress);
|
||||
|
||||
Wasm::MemoryAddress m_address;
|
||||
mutable JS::GCPtr<JS::ArrayBuffer> m_buffer;
|
||||
mutable GC::Ptr<JS::ArrayBuffer> m_buffer;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -16,9 +16,9 @@
|
|||
|
||||
namespace Web::WebAssembly {
|
||||
|
||||
JS_DEFINE_ALLOCATOR(Module);
|
||||
GC_DEFINE_ALLOCATOR(Module);
|
||||
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<Module>> Module::construct_impl(JS::Realm& realm, JS::Handle<WebIDL::BufferSource>& bytes)
|
||||
WebIDL::ExceptionOr<GC::Ref<Module>> Module::construct_impl(JS::Realm& realm, GC::Root<WebIDL::BufferSource>& bytes)
|
||||
{
|
||||
auto& vm = realm.vm();
|
||||
|
||||
|
|
|
@ -7,9 +7,9 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <LibGC/Ptr.h>
|
||||
#include <LibGC/Root.h>
|
||||
#include <LibJS/Forward.h>
|
||||
#include <LibJS/Heap/GCPtr.h>
|
||||
#include <LibJS/Heap/Handle.h>
|
||||
#include <LibWasm/Types.h>
|
||||
#include <LibWeb/Bindings/ExceptionOrUtils.h>
|
||||
#include <LibWeb/Bindings/PlatformObject.h>
|
||||
|
@ -19,10 +19,10 @@ namespace Web::WebAssembly {
|
|||
|
||||
class Module : public Bindings::PlatformObject {
|
||||
WEB_PLATFORM_OBJECT(Module, Bindings::PlatformObject);
|
||||
JS_DECLARE_ALLOCATOR(Module);
|
||||
GC_DECLARE_ALLOCATOR(Module);
|
||||
|
||||
public:
|
||||
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Module>> construct_impl(JS::Realm&, JS::Handle<WebIDL::BufferSource>& bytes);
|
||||
static WebIDL::ExceptionOr<GC::Ref<Module>> construct_impl(JS::Realm&, GC::Root<WebIDL::BufferSource>& bytes);
|
||||
|
||||
NonnullRefPtr<Detail::CompiledWebAssemblyModule> compiled_module() const { return m_compiled_module; }
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
|
||||
namespace Web::WebAssembly {
|
||||
|
||||
JS_DEFINE_ALLOCATOR(Table);
|
||||
GC_DEFINE_ALLOCATOR(Table);
|
||||
|
||||
static Wasm::ValueType table_kind_to_value_type(Bindings::TableKind kind)
|
||||
{
|
||||
|
@ -29,7 +29,7 @@ static Wasm::ValueType table_kind_to_value_type(Bindings::TableKind kind)
|
|||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<Table>> Table::construct_impl(JS::Realm& realm, TableDescriptor& descriptor, JS::Value value)
|
||||
WebIDL::ExceptionOr<GC::Ref<Table>> Table::construct_impl(JS::Realm& realm, TableDescriptor& descriptor, JS::Value value)
|
||||
{
|
||||
auto& vm = realm.vm();
|
||||
|
||||
|
|
|
@ -8,8 +8,8 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Optional.h>
|
||||
#include <LibGC/Ptr.h>
|
||||
#include <LibJS/Forward.h>
|
||||
#include <LibJS/Heap/GCPtr.h>
|
||||
#include <LibJS/Runtime/Value.h>
|
||||
#include <LibWasm/AbstractMachine/AbstractMachine.h>
|
||||
#include <LibWeb/Bindings/ExceptionOrUtils.h>
|
||||
|
@ -26,10 +26,10 @@ struct TableDescriptor {
|
|||
|
||||
class Table : public Bindings::PlatformObject {
|
||||
WEB_PLATFORM_OBJECT(Table, Bindings::PlatformObject);
|
||||
JS_DECLARE_ALLOCATOR(Table);
|
||||
GC_DECLARE_ALLOCATOR(Table);
|
||||
|
||||
public:
|
||||
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Table>> construct_impl(JS::Realm&, TableDescriptor& descriptor, JS::Value value);
|
||||
static WebIDL::ExceptionOr<GC::Ref<Table>> construct_impl(JS::Realm&, TableDescriptor& descriptor, JS::Value value);
|
||||
|
||||
WebIDL::ExceptionOr<u32> grow(u32 delta, JS::Value value);
|
||||
|
||||
|
|
|
@ -34,14 +34,14 @@
|
|||
|
||||
namespace Web::WebAssembly {
|
||||
|
||||
static JS::NonnullGCPtr<WebIDL::Promise> asynchronously_compile_webassembly_module(JS::VM&, ByteBuffer, HTML::Task::Source = HTML::Task::Source::Unspecified);
|
||||
static JS::NonnullGCPtr<WebIDL::Promise> instantiate_promise_of_module(JS::VM&, JS::NonnullGCPtr<WebIDL::Promise>, JS::GCPtr<JS::Object> import_object);
|
||||
static JS::NonnullGCPtr<WebIDL::Promise> asynchronously_instantiate_webassembly_module(JS::VM&, JS::NonnullGCPtr<Module>, JS::GCPtr<JS::Object> import_object);
|
||||
static JS::NonnullGCPtr<WebIDL::Promise> compile_potential_webassembly_response(JS::VM&, JS::NonnullGCPtr<WebIDL::Promise>);
|
||||
static GC::Ref<WebIDL::Promise> asynchronously_compile_webassembly_module(JS::VM&, ByteBuffer, HTML::Task::Source = HTML::Task::Source::Unspecified);
|
||||
static GC::Ref<WebIDL::Promise> instantiate_promise_of_module(JS::VM&, GC::Ref<WebIDL::Promise>, GC::Ptr<JS::Object> import_object);
|
||||
static GC::Ref<WebIDL::Promise> asynchronously_instantiate_webassembly_module(JS::VM&, GC::Ref<Module>, GC::Ptr<JS::Object> import_object);
|
||||
static GC::Ref<WebIDL::Promise> compile_potential_webassembly_response(JS::VM&, GC::Ref<WebIDL::Promise>);
|
||||
|
||||
namespace Detail {
|
||||
|
||||
HashMap<JS::GCPtr<JS::Object>, WebAssemblyCache> s_caches;
|
||||
HashMap<GC::Ptr<JS::Object>, WebAssemblyCache> s_caches;
|
||||
|
||||
WebAssemblyCache& get_cache(JS::Realm& realm)
|
||||
{
|
||||
|
@ -68,7 +68,7 @@ void finalize(JS::Object& object)
|
|||
}
|
||||
|
||||
// https://webassembly.github.io/spec/js-api/#dom-webassembly-validate
|
||||
bool validate(JS::VM& vm, JS::Handle<WebIDL::BufferSource>& bytes)
|
||||
bool validate(JS::VM& vm, GC::Root<WebIDL::BufferSource>& bytes)
|
||||
{
|
||||
// 1. Let stableBytes be a copy of the bytes held by the buffer bytes.
|
||||
auto stable_bytes = WebIDL::get_buffer_source_copy(*bytes->raw_object());
|
||||
|
@ -89,7 +89,7 @@ bool validate(JS::VM& vm, JS::Handle<WebIDL::BufferSource>& bytes)
|
|||
}
|
||||
|
||||
// https://webassembly.github.io/spec/js-api/#dom-webassembly-compile
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> compile(JS::VM& vm, JS::Handle<WebIDL::BufferSource>& bytes)
|
||||
WebIDL::ExceptionOr<GC::Ref<WebIDL::Promise>> compile(JS::VM& vm, GC::Root<WebIDL::BufferSource>& bytes)
|
||||
{
|
||||
auto& realm = *vm.current_realm();
|
||||
|
||||
|
@ -105,14 +105,14 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> compile(JS::VM& vm, JS::H
|
|||
}
|
||||
|
||||
// https://webassembly.github.io/spec/web-api/index.html#dom-webassembly-compilestreaming
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> compile_streaming(JS::VM& vm, JS::Handle<WebIDL::Promise> source)
|
||||
WebIDL::ExceptionOr<GC::Ref<WebIDL::Promise>> compile_streaming(JS::VM& vm, GC::Root<WebIDL::Promise> source)
|
||||
{
|
||||
// The compileStreaming(source) method, when invoked, returns the result of compiling a potential WebAssembly response with source.
|
||||
return compile_potential_webassembly_response(vm, *source);
|
||||
}
|
||||
|
||||
// https://webassembly.github.io/spec/js-api/#dom-webassembly-instantiate
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> instantiate(JS::VM& vm, JS::Handle<WebIDL::BufferSource>& bytes, Optional<JS::Handle<JS::Object>>& import_object_handle)
|
||||
WebIDL::ExceptionOr<GC::Ref<WebIDL::Promise>> instantiate(JS::VM& vm, GC::Root<WebIDL::BufferSource>& bytes, Optional<GC::Root<JS::Object>>& import_object_handle)
|
||||
{
|
||||
auto& realm = *vm.current_realm();
|
||||
|
||||
|
@ -127,21 +127,21 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> instantiate(JS::VM& vm, J
|
|||
auto promise_of_module = asynchronously_compile_webassembly_module(vm, stable_bytes.release_value());
|
||||
|
||||
// 3. Instantiate promiseOfModule with imports importObject and return the result.
|
||||
JS::GCPtr<JS::Object> const import_object = import_object_handle.has_value() ? import_object_handle.value().ptr() : nullptr;
|
||||
GC::Ptr<JS::Object> const import_object = import_object_handle.has_value() ? import_object_handle.value().ptr() : nullptr;
|
||||
return instantiate_promise_of_module(vm, promise_of_module, import_object);
|
||||
}
|
||||
|
||||
// https://webassembly.github.io/spec/js-api/#dom-webassembly-instantiate-moduleobject-importobject
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> instantiate(JS::VM& vm, Module const& module_object, Optional<JS::Handle<JS::Object>>& import_object)
|
||||
WebIDL::ExceptionOr<GC::Ref<WebIDL::Promise>> instantiate(JS::VM& vm, Module const& module_object, Optional<GC::Root<JS::Object>>& import_object)
|
||||
{
|
||||
// 1. Asynchronously instantiate the WebAssembly module moduleObject importing importObject, and return the result.
|
||||
JS::NonnullGCPtr<Module> module { const_cast<Module&>(module_object) };
|
||||
JS::GCPtr<JS::Object> const imports = import_object.has_value() ? import_object.value().ptr() : nullptr;
|
||||
GC::Ref<Module> module { const_cast<Module&>(module_object) };
|
||||
GC::Ptr<JS::Object> const imports = import_object.has_value() ? import_object.value().ptr() : nullptr;
|
||||
return asynchronously_instantiate_webassembly_module(vm, module, imports);
|
||||
}
|
||||
|
||||
// https://webassembly.github.io/spec/web-api/index.html#dom-webassembly-instantiatestreaming
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> instantiate_streaming(JS::VM& vm, JS::Handle<WebIDL::Promise> source, Optional<JS::Handle<JS::Object>>& import_object)
|
||||
WebIDL::ExceptionOr<GC::Ref<WebIDL::Promise>> instantiate_streaming(JS::VM& vm, GC::Root<WebIDL::Promise> source, Optional<GC::Root<JS::Object>>& import_object)
|
||||
{
|
||||
// The instantiateStreaming(source, importObject) method, when invoked, performs the following steps:
|
||||
|
||||
|
@ -149,13 +149,13 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> instantiate_streaming(JS:
|
|||
auto promise_of_module = compile_potential_webassembly_response(vm, *source);
|
||||
|
||||
// 2. Return the result of instantiating the promise of a module promiseOfModule with imports importObject.
|
||||
auto imports = JS::GCPtr { import_object.has_value() ? import_object.value().ptr() : nullptr };
|
||||
auto imports = GC::Ptr { import_object.has_value() ? import_object.value().ptr() : nullptr };
|
||||
return instantiate_promise_of_module(vm, promise_of_module, imports);
|
||||
}
|
||||
|
||||
namespace Detail {
|
||||
|
||||
JS::ThrowCompletionOr<NonnullOwnPtr<Wasm::ModuleInstance>> instantiate_module(JS::VM& vm, Wasm::Module const& module, JS::GCPtr<JS::Object> import_object)
|
||||
JS::ThrowCompletionOr<NonnullOwnPtr<Wasm::ModuleInstance>> instantiate_module(JS::VM& vm, Wasm::Module const& module, GC::Ptr<JS::Object> import_object)
|
||||
{
|
||||
Wasm::Linker linker { module };
|
||||
HashMap<Wasm::Linker::Name, Wasm::ExternValue> resolved_imports;
|
||||
|
@ -189,7 +189,7 @@ JS::ThrowCompletionOr<NonnullOwnPtr<Wasm::ModuleInstance>> instantiate_module(JS
|
|||
// just extract its address and resolve to that.
|
||||
Wasm::HostFunction host_function {
|
||||
[&](auto&, auto& arguments) -> Wasm::Result {
|
||||
JS::MarkedVector<JS::Value> argument_values { vm.heap() };
|
||||
GC::MarkedVector<JS::Value> argument_values { vm.heap() };
|
||||
size_t index = 0;
|
||||
for (auto& entry : arguments) {
|
||||
argument_values.append(to_js_value(vm, entry, type.parameters()[index]));
|
||||
|
@ -359,7 +359,7 @@ JS::NativeFunction* create_native_function(JS::VM& vm, Wasm::FunctionAddress add
|
|||
return to_js_value(vm, result.values().first(), type.results().first());
|
||||
|
||||
// Put result values into a JS::Array in reverse order.
|
||||
auto js_result_values = JS::MarkedVector<JS::Value> { realm.heap() };
|
||||
auto js_result_values = GC::MarkedVector<JS::Value> { realm.heap() };
|
||||
js_result_values.ensure_capacity(result.values().size());
|
||||
|
||||
for (size_t i = result.values().size(); i > 0; i--) {
|
||||
|
@ -498,7 +498,7 @@ JS::Value to_js_value(JS::VM& vm, Wasm::Value& wasm_value, Wasm::ValueType type)
|
|||
}
|
||||
|
||||
// https://webassembly.github.io/spec/js-api/#asynchronously-compile-a-webassembly-module
|
||||
JS::NonnullGCPtr<WebIDL::Promise> asynchronously_compile_webassembly_module(JS::VM& vm, ByteBuffer bytes, HTML::Task::Source task_source)
|
||||
GC::Ref<WebIDL::Promise> asynchronously_compile_webassembly_module(JS::VM& vm, ByteBuffer bytes, HTML::Task::Source task_source)
|
||||
{
|
||||
auto& realm = *vm.current_realm();
|
||||
|
||||
|
@ -506,13 +506,13 @@ JS::NonnullGCPtr<WebIDL::Promise> asynchronously_compile_webassembly_module(JS::
|
|||
auto promise = WebIDL::create_promise(realm);
|
||||
|
||||
// 2. Run the following steps in parallel:
|
||||
Platform::EventLoopPlugin::the().deferred_invoke(JS::create_heap_function(vm.heap(), [&vm, &realm, bytes = move(bytes), promise, task_source]() mutable {
|
||||
Platform::EventLoopPlugin::the().deferred_invoke(GC::create_function(vm.heap(), [&vm, &realm, bytes = move(bytes), promise, task_source]() mutable {
|
||||
HTML::TemporaryExecutionContext context(realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes);
|
||||
// 1. Compile the WebAssembly module bytes and store the result as module.
|
||||
auto module_or_error = Detail::compile_a_webassembly_module(vm, move(bytes));
|
||||
|
||||
// 2. Queue a task to perform the following steps. If taskSource was provided, queue the task on that task source.
|
||||
HTML::queue_a_task(task_source, nullptr, nullptr, JS::create_heap_function(vm.heap(), [&realm, promise, module_or_error = move(module_or_error)]() mutable {
|
||||
HTML::queue_a_task(task_source, nullptr, nullptr, GC::create_function(vm.heap(), [&realm, promise, module_or_error = move(module_or_error)]() mutable {
|
||||
HTML::TemporaryExecutionContext context(realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes);
|
||||
auto& realm = HTML::relevant_realm(*promise->promise());
|
||||
|
||||
|
@ -538,7 +538,7 @@ JS::NonnullGCPtr<WebIDL::Promise> asynchronously_compile_webassembly_module(JS::
|
|||
}
|
||||
|
||||
// https://webassembly.github.io/spec/js-api/#asynchronously-instantiate-a-webassembly-module
|
||||
JS::NonnullGCPtr<WebIDL::Promise> asynchronously_instantiate_webassembly_module(JS::VM& vm, JS::NonnullGCPtr<Module> module_object, JS::GCPtr<JS::Object> import_object)
|
||||
GC::Ref<WebIDL::Promise> asynchronously_instantiate_webassembly_module(JS::VM& vm, GC::Ref<Module> module_object, GC::Ptr<JS::Object> import_object)
|
||||
{
|
||||
auto& realm = *vm.current_realm();
|
||||
|
||||
|
@ -554,7 +554,7 @@ JS::NonnullGCPtr<WebIDL::Promise> asynchronously_instantiate_webassembly_module(
|
|||
|
||||
// 4. Run the following steps in parallel:
|
||||
// 1. Queue a task to perform the following steps: Note: Implementation-specific work may be performed here.
|
||||
HTML::queue_a_task(HTML::Task::Source::Unspecified, nullptr, nullptr, JS::create_heap_function(vm.heap(), [&vm, &realm, promise, module, import_object]() {
|
||||
HTML::queue_a_task(HTML::Task::Source::Unspecified, nullptr, nullptr, GC::create_function(vm.heap(), [&vm, &realm, promise, module, import_object]() {
|
||||
HTML::TemporaryExecutionContext context(realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes);
|
||||
auto& realm = HTML::relevant_realm(*promise->promise());
|
||||
|
||||
|
@ -581,7 +581,7 @@ JS::NonnullGCPtr<WebIDL::Promise> asynchronously_instantiate_webassembly_module(
|
|||
}
|
||||
|
||||
// https://webassembly.github.io/spec/js-api/#instantiate-a-promise-of-a-module
|
||||
JS::NonnullGCPtr<WebIDL::Promise> instantiate_promise_of_module(JS::VM& vm, JS::NonnullGCPtr<WebIDL::Promise> promise_of_module, JS::GCPtr<JS::Object> import_object)
|
||||
GC::Ref<WebIDL::Promise> instantiate_promise_of_module(JS::VM& vm, GC::Ref<WebIDL::Promise> promise_of_module, GC::Ptr<JS::Object> import_object)
|
||||
{
|
||||
auto& realm = *vm.current_realm();
|
||||
|
||||
|
@ -591,19 +591,19 @@ JS::NonnullGCPtr<WebIDL::Promise> instantiate_promise_of_module(JS::VM& vm, JS::
|
|||
// FIXME: Spec should use react to promise here instead of separate upon fulfillment and upon rejection steps
|
||||
|
||||
// 2. Upon fulfillment of promiseOfModule with value module:
|
||||
auto fulfillment_steps = JS::create_heap_function(vm.heap(), [&vm, promise, import_object](JS::Value module_value) -> WebIDL::ExceptionOr<JS::Value> {
|
||||
auto fulfillment_steps = GC::create_function(vm.heap(), [&vm, promise, import_object](JS::Value module_value) -> WebIDL::ExceptionOr<JS::Value> {
|
||||
VERIFY(module_value.is_object() && is<Module>(module_value.as_object()));
|
||||
auto module = JS::NonnullGCPtr { static_cast<Module&>(module_value.as_object()) };
|
||||
auto module = GC::Ref { static_cast<Module&>(module_value.as_object()) };
|
||||
|
||||
// 1. Instantiate the WebAssembly module module importing importObject, and let innerPromise be the result.
|
||||
auto inner_promise = asynchronously_instantiate_webassembly_module(vm, module, import_object);
|
||||
|
||||
// 2. Upon fulfillment of innerPromise with value instance.
|
||||
auto instantiate_fulfillment_steps = JS::create_heap_function(vm.heap(), [promise, module](JS::Value instance_value) -> WebIDL::ExceptionOr<JS::Value> {
|
||||
auto instantiate_fulfillment_steps = GC::create_function(vm.heap(), [promise, module](JS::Value instance_value) -> WebIDL::ExceptionOr<JS::Value> {
|
||||
auto& realm = HTML::relevant_realm(*promise->promise());
|
||||
|
||||
VERIFY(instance_value.is_object() && is<Instance>(instance_value.as_object()));
|
||||
auto instance = JS::NonnullGCPtr { static_cast<Instance&>(instance_value.as_object()) };
|
||||
auto instance = GC::Ref { static_cast<Instance&>(instance_value.as_object()) };
|
||||
|
||||
// 1. Let result be the WebAssemblyInstantiatedSource value «[ "module" → module, "instance" → instance ]».
|
||||
auto result = JS::Object::create(realm, nullptr);
|
||||
|
@ -617,7 +617,7 @@ JS::NonnullGCPtr<WebIDL::Promise> instantiate_promise_of_module(JS::VM& vm, JS::
|
|||
});
|
||||
|
||||
// 3. Upon rejection of innerPromise with reason reason.
|
||||
auto instantiate_rejection_steps = JS::create_heap_function(vm.heap(), [promise](JS::Value reason) -> WebIDL::ExceptionOr<JS::Value> {
|
||||
auto instantiate_rejection_steps = GC::create_function(vm.heap(), [promise](JS::Value reason) -> WebIDL::ExceptionOr<JS::Value> {
|
||||
auto& realm = HTML::relevant_realm(*promise->promise());
|
||||
|
||||
// 1. Reject promise with reason.
|
||||
|
@ -632,7 +632,7 @@ JS::NonnullGCPtr<WebIDL::Promise> instantiate_promise_of_module(JS::VM& vm, JS::
|
|||
});
|
||||
|
||||
// 3. Upon rejection of promiseOfModule with reason reason:
|
||||
auto rejection_steps = JS::create_heap_function(vm.heap(), [promise](JS::Value reason) -> WebIDL::ExceptionOr<JS::Value> {
|
||||
auto rejection_steps = GC::create_function(vm.heap(), [promise](JS::Value reason) -> WebIDL::ExceptionOr<JS::Value> {
|
||||
auto& realm = HTML::relevant_realm(*promise->promise());
|
||||
|
||||
// 1. Reject promise with reason.
|
||||
|
@ -648,7 +648,7 @@ JS::NonnullGCPtr<WebIDL::Promise> instantiate_promise_of_module(JS::VM& vm, JS::
|
|||
}
|
||||
|
||||
// https://webassembly.github.io/spec/web-api/index.html#compile-a-potential-webassembly-response
|
||||
JS::NonnullGCPtr<WebIDL::Promise> compile_potential_webassembly_response(JS::VM& vm, JS::NonnullGCPtr<WebIDL::Promise> source)
|
||||
GC::Ref<WebIDL::Promise> compile_potential_webassembly_response(JS::VM& vm, GC::Ref<WebIDL::Promise> source)
|
||||
{
|
||||
auto& realm = *vm.current_realm();
|
||||
|
||||
|
@ -662,7 +662,7 @@ JS::NonnullGCPtr<WebIDL::Promise> compile_potential_webassembly_response(JS::VM&
|
|||
auto return_value = WebIDL::create_promise(realm);
|
||||
|
||||
// 2. Upon fulfillment of source with value unwrappedSource:
|
||||
auto fulfillment_steps = JS::create_heap_function(vm.heap(), [&vm, return_value](JS::Value unwrapped_source) -> WebIDL::ExceptionOr<JS::Value> {
|
||||
auto fulfillment_steps = GC::create_function(vm.heap(), [&vm, return_value](JS::Value unwrapped_source) -> WebIDL::ExceptionOr<JS::Value> {
|
||||
auto& realm = HTML::relevant_realm(*return_value->promise());
|
||||
|
||||
// 1. Let response be unwrappedSource’s response.
|
||||
|
@ -708,7 +708,7 @@ JS::NonnullGCPtr<WebIDL::Promise> compile_potential_webassembly_response(JS::VM&
|
|||
auto body_promise = body_promise_or_error.release_value();
|
||||
|
||||
// 9. Upon fulfillment of bodyPromise with value bodyArrayBuffer:
|
||||
auto body_fulfillment_steps = JS::create_heap_function(vm.heap(), [&vm, return_value](JS::Value body_array_buffer) -> WebIDL::ExceptionOr<JS::Value> {
|
||||
auto body_fulfillment_steps = GC::create_function(vm.heap(), [&vm, return_value](JS::Value body_array_buffer) -> WebIDL::ExceptionOr<JS::Value> {
|
||||
// 1. Let stableBytes be a copy of the bytes held by the buffer bodyArrayBuffer.
|
||||
VERIFY(body_array_buffer.is_object());
|
||||
auto stable_bytes = WebIDL::get_buffer_source_copy(body_array_buffer.as_object());
|
||||
|
@ -728,7 +728,7 @@ JS::NonnullGCPtr<WebIDL::Promise> compile_potential_webassembly_response(JS::VM&
|
|||
});
|
||||
|
||||
// 10. Upon rejection of bodyPromise with reason reason:
|
||||
auto body_rejection_steps = JS::create_heap_function(vm.heap(), [return_value](JS::Value reason) -> WebIDL::ExceptionOr<JS::Value> {
|
||||
auto body_rejection_steps = GC::create_function(vm.heap(), [return_value](JS::Value reason) -> WebIDL::ExceptionOr<JS::Value> {
|
||||
// 1. Reject returnValue with reason.
|
||||
WebIDL::reject_promise(HTML::relevant_realm(*return_value->promise()), return_value, reason);
|
||||
return JS::js_undefined();
|
||||
|
@ -740,7 +740,7 @@ JS::NonnullGCPtr<WebIDL::Promise> compile_potential_webassembly_response(JS::VM&
|
|||
});
|
||||
|
||||
// 3. Upon rejection of source with reason reason:
|
||||
auto rejection_steps = JS::create_heap_function(vm.heap(), [return_value](JS::Value reason) -> WebIDL::ExceptionOr<JS::Value> {
|
||||
auto rejection_steps = GC::create_function(vm.heap(), [return_value](JS::Value reason) -> WebIDL::ExceptionOr<JS::Value> {
|
||||
// 1. Reject returnValue with reason.
|
||||
WebIDL::reject_promise(HTML::relevant_realm(*return_value->promise()), return_value, reason);
|
||||
|
||||
|
|
|
@ -8,8 +8,8 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Optional.h>
|
||||
#include <LibGC/Root.h>
|
||||
#include <LibJS/Forward.h>
|
||||
#include <LibJS/Heap/Handle.h>
|
||||
#include <LibJS/Runtime/Completion.h>
|
||||
#include <LibJS/Runtime/Value.h>
|
||||
#include <LibWasm/AbstractMachine/AbstractMachine.h>
|
||||
|
@ -21,13 +21,13 @@ namespace Web::WebAssembly {
|
|||
void visit_edges(JS::Object&, JS::Cell::Visitor&);
|
||||
void finalize(JS::Object&);
|
||||
|
||||
bool validate(JS::VM&, JS::Handle<WebIDL::BufferSource>& bytes);
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> compile(JS::VM&, JS::Handle<WebIDL::BufferSource>& bytes);
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> compile_streaming(JS::VM&, JS::Handle<WebIDL::Promise> source);
|
||||
bool validate(JS::VM&, GC::Root<WebIDL::BufferSource>& bytes);
|
||||
WebIDL::ExceptionOr<GC::Ref<WebIDL::Promise>> compile(JS::VM&, GC::Root<WebIDL::BufferSource>& bytes);
|
||||
WebIDL::ExceptionOr<GC::Ref<WebIDL::Promise>> compile_streaming(JS::VM&, GC::Root<WebIDL::Promise> source);
|
||||
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> instantiate(JS::VM&, JS::Handle<WebIDL::BufferSource>& bytes, Optional<JS::Handle<JS::Object>>& import_object);
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> instantiate(JS::VM&, Module const& module_object, Optional<JS::Handle<JS::Object>>& import_object);
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> instantiate_streaming(JS::VM&, JS::Handle<WebIDL::Promise> source, Optional<JS::Handle<JS::Object>>& import_object);
|
||||
WebIDL::ExceptionOr<GC::Ref<WebIDL::Promise>> instantiate(JS::VM&, GC::Root<WebIDL::BufferSource>& bytes, Optional<GC::Root<JS::Object>>& import_object);
|
||||
WebIDL::ExceptionOr<GC::Ref<WebIDL::Promise>> instantiate(JS::VM&, Module const& module_object, Optional<GC::Root<JS::Object>>& import_object);
|
||||
WebIDL::ExceptionOr<GC::Ref<WebIDL::Promise>> instantiate_streaming(JS::VM&, GC::Root<WebIDL::Promise> source, Optional<GC::Root<JS::Object>>& import_object);
|
||||
|
||||
namespace Detail {
|
||||
struct CompiledWebAssemblyModule : public RefCounted<CompiledWebAssemblyModule> {
|
||||
|
@ -42,36 +42,36 @@ struct CompiledWebAssemblyModule : public RefCounted<CompiledWebAssemblyModule>
|
|||
class WebAssemblyCache {
|
||||
public:
|
||||
void add_compiled_module(NonnullRefPtr<CompiledWebAssemblyModule> module) { m_compiled_modules.append(module); }
|
||||
void add_function_instance(Wasm::FunctionAddress address, JS::GCPtr<JS::NativeFunction> function) { m_function_instances.set(address, function); }
|
||||
void add_imported_object(JS::GCPtr<JS::Object> object) { m_imported_objects.set(object); }
|
||||
void add_function_instance(Wasm::FunctionAddress address, GC::Ptr<JS::NativeFunction> function) { m_function_instances.set(address, function); }
|
||||
void add_imported_object(GC::Ptr<JS::Object> object) { m_imported_objects.set(object); }
|
||||
void add_extern_value(Wasm::ExternAddress address, JS::Value value) { m_extern_values.set(address, value); }
|
||||
|
||||
Optional<JS::GCPtr<JS::NativeFunction>> get_function_instance(Wasm::FunctionAddress address) { return m_function_instances.get(address); }
|
||||
Optional<GC::Ptr<JS::NativeFunction>> get_function_instance(Wasm::FunctionAddress address) { return m_function_instances.get(address); }
|
||||
Optional<JS::Value> get_extern_value(Wasm::ExternAddress address) { return m_extern_values.get(address); }
|
||||
|
||||
HashMap<Wasm::FunctionAddress, JS::GCPtr<JS::NativeFunction>> function_instances() const { return m_function_instances; }
|
||||
HashMap<Wasm::FunctionAddress, GC::Ptr<JS::NativeFunction>> function_instances() const { return m_function_instances; }
|
||||
HashMap<Wasm::ExternAddress, JS::Value> extern_values() const { return m_extern_values; }
|
||||
HashTable<JS::GCPtr<JS::Object>> imported_objects() const { return m_imported_objects; }
|
||||
HashTable<GC::Ptr<JS::Object>> imported_objects() const { return m_imported_objects; }
|
||||
Wasm::AbstractMachine& abstract_machine() { return m_abstract_machine; }
|
||||
|
||||
private:
|
||||
HashMap<Wasm::FunctionAddress, JS::GCPtr<JS::NativeFunction>> m_function_instances;
|
||||
HashMap<Wasm::FunctionAddress, GC::Ptr<JS::NativeFunction>> m_function_instances;
|
||||
HashMap<Wasm::ExternAddress, JS::Value> m_extern_values;
|
||||
Vector<NonnullRefPtr<CompiledWebAssemblyModule>> m_compiled_modules;
|
||||
HashTable<JS::GCPtr<JS::Object>> m_imported_objects;
|
||||
HashTable<GC::Ptr<JS::Object>> m_imported_objects;
|
||||
Wasm::AbstractMachine m_abstract_machine;
|
||||
};
|
||||
|
||||
WebAssemblyCache& get_cache(JS::Realm&);
|
||||
|
||||
JS::ThrowCompletionOr<NonnullOwnPtr<Wasm::ModuleInstance>> instantiate_module(JS::VM&, Wasm::Module const&, JS::GCPtr<JS::Object> import_object);
|
||||
JS::ThrowCompletionOr<NonnullOwnPtr<Wasm::ModuleInstance>> instantiate_module(JS::VM&, Wasm::Module const&, GC::Ptr<JS::Object> import_object);
|
||||
JS::ThrowCompletionOr<NonnullRefPtr<CompiledWebAssemblyModule>> compile_a_webassembly_module(JS::VM&, ByteBuffer);
|
||||
JS::NativeFunction* create_native_function(JS::VM&, Wasm::FunctionAddress address, ByteString const& name, Instance* instance = nullptr);
|
||||
JS::ThrowCompletionOr<Wasm::Value> to_webassembly_value(JS::VM&, JS::Value value, Wasm::ValueType const& type);
|
||||
Wasm::Value default_webassembly_value(JS::VM&, Wasm::ValueType type);
|
||||
JS::Value to_js_value(JS::VM&, Wasm::Value& wasm_value, Wasm::ValueType type);
|
||||
|
||||
extern HashMap<JS::GCPtr<JS::Object>, WebAssemblyCache> s_caches;
|
||||
extern HashMap<GC::Ptr<JS::Object>, WebAssemblyCache> s_caches;
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue