mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-05-13 14:42:51 +00:00
No functional changes - we can still very easily get to the global object via `Realm::global_object()`. This is in preparation of moving the intrinsics to the realm and no longer having to pass a global object when allocating any object. In a few (now, and many more in subsequent commits) places we get a realm using `GlobalObject::associated_realm()`, this is intended to be temporary. For example, create() functions will later receive the same treatment and are passed a realm instead of a global object.
68 lines
2.5 KiB
C++
68 lines
2.5 KiB
C++
/*
|
|
* 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/WindowObject.h>
|
|
#include <LibWeb/WebAssembly/WebAssemblyInstanceObject.h>
|
|
#include <LibWeb/WebAssembly/WebAssemblyMemoryPrototype.h>
|
|
#include <LibWeb/WebAssembly/WebAssemblyObject.h>
|
|
|
|
namespace Web::Bindings {
|
|
|
|
WebAssemblyInstanceObject::WebAssemblyInstanceObject(JS::Realm& realm, size_t index)
|
|
: Object(static_cast<Web::Bindings::WindowObject&>(realm.global_object()).ensure_web_prototype<WebAssemblyInstancePrototype>("WebAssemblyInstancePrototype"))
|
|
, m_index(index)
|
|
{
|
|
}
|
|
|
|
void WebAssemblyInstanceObject::initialize(JS::GlobalObject& global_object)
|
|
{
|
|
Object::initialize(global_object);
|
|
|
|
auto& realm = *global_object.associated_realm();
|
|
|
|
VERIFY(!m_exports_object);
|
|
m_exports_object = create(global_object, nullptr);
|
|
auto& instance = this->instance();
|
|
auto& cache = this->cache();
|
|
for (auto& export_ : instance.exports()) {
|
|
export_.value().visit(
|
|
[&](Wasm::FunctionAddress const& address) {
|
|
Optional<JS::FunctionObject*> object = cache.function_instances.get(address);
|
|
if (!object.has_value()) {
|
|
object = create_native_function(global_object, address, export_.name());
|
|
cache.function_instances.set(address, *object);
|
|
}
|
|
m_exports_object->define_direct_property(export_.name(), *object, JS::default_attributes);
|
|
},
|
|
[&](Wasm::MemoryAddress const& address) {
|
|
Optional<WebAssemblyMemoryObject*> object = cache.memory_instances.get(address);
|
|
if (!object.has_value()) {
|
|
object = heap().allocate<Web::Bindings::WebAssemblyMemoryObject>(global_object, realm, address);
|
|
cache.memory_instances.set(address, *object);
|
|
}
|
|
m_exports_object->define_direct_property(export_.name(), *object, JS::default_attributes);
|
|
},
|
|
[&](auto const&) {
|
|
// FIXME: Implement other exports!
|
|
});
|
|
}
|
|
|
|
MUST(m_exports_object->set_integrity_level(IntegrityLevel::Frozen));
|
|
}
|
|
|
|
void WebAssemblyInstanceObject::visit_edges(Visitor& visitor)
|
|
{
|
|
Base::visit_edges(visitor);
|
|
visitor.visit(m_exports_object);
|
|
}
|
|
|
|
}
|