LibWeb: Add WebAssembly.Global and exports support for global instances

This commit is contained in:
Andrew Kaster 2024-12-23 14:47:25 -07:00 committed by Ali Mohammad Pur
commit 44e3817219
Notes: github-actions[bot] 2024-12-24 14:21:40 +00:00
10 changed files with 268 additions and 4 deletions

View file

@ -11,6 +11,7 @@
#include <LibJS/Runtime/VM.h>
#include <LibWeb/Bindings/InstancePrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/WebAssembly/Global.h>
#include <LibWeb/WebAssembly/Instance.h>
#include <LibWeb/WebAssembly/Memory.h>
#include <LibWeb/WebAssembly/Module.h>
@ -45,6 +46,9 @@ void Instance::initialize(JS::Realm& realm)
Base::initialize(realm);
WEB_SET_PROTOTYPE_FOR_INTERFACE_WITH_CUSTOM_NAME(Instance, WebAssembly.Instance);
auto& cache = Detail::get_cache(realm);
// https://webassembly.github.io/spec/js-api/#create-an-exports-object
for (auto& export_ : m_module_instance->exports()) {
export_.value().visit(
[&](Wasm::FunctionAddress const& address) {
@ -56,6 +60,14 @@ void Instance::initialize(JS::Realm& realm)
m_exports->define_direct_property(export_.name(), *object, JS::default_attributes);
},
[&](Wasm::GlobalAddress const& address) {
Optional<GC::Ptr<Global>> object = cache.get_global_instance(address);
if (!object.has_value()) {
object = realm.create<Global>(realm, address);
}
m_exports->define_direct_property(export_.name(), *object, JS::default_attributes);
},
[&](Wasm::MemoryAddress const& address) {
Optional<GC::Ptr<Memory>> object = m_memory_instances.get(address);
if (!object.has_value()) {
@ -73,9 +85,6 @@ void Instance::initialize(JS::Realm& realm)
}
m_exports->define_direct_property(export_.name(), *object, JS::default_attributes);
},
[&](auto const&) {
// FIXME: Implement other exports!
});
}