1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
/*
* 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/Memory.h>
#include <LibWeb/WebAssembly/Module.h>
#include <LibWeb/WebAssembly/Table.h>
#include <LibWeb/WebAssembly/WebAssemblyObject.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<Memory>> object = cache.memory_instances.get(address);
if (!object.has_value()) {
object = MUST_OR_THROW_OOM(heap().allocate<Memory>(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<Table>> object = cache.table_instances.get(address);
if (!object.has_value()) {
object = MUST_OR_THROW_OOM(heap().allocate<Table>(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);
}
}
|