/src/serenity/Userland/Libraries/LibWeb/WebAssembly/WebAssembly.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org> |
3 | | * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org> |
4 | | * |
5 | | * SPDX-License-Identifier: BSD-2-Clause |
6 | | */ |
7 | | |
8 | | #include <AK/MemoryStream.h> |
9 | | #include <AK/ScopeGuard.h> |
10 | | #include <AK/StringBuilder.h> |
11 | | #include <LibJS/Runtime/Array.h> |
12 | | #include <LibJS/Runtime/ArrayBuffer.h> |
13 | | #include <LibJS/Runtime/BigInt.h> |
14 | | #include <LibJS/Runtime/DataView.h> |
15 | | #include <LibJS/Runtime/Iterator.h> |
16 | | #include <LibJS/Runtime/NativeFunction.h> |
17 | | #include <LibJS/Runtime/Object.h> |
18 | | #include <LibJS/Runtime/Promise.h> |
19 | | #include <LibJS/Runtime/TypedArray.h> |
20 | | #include <LibJS/Runtime/VM.h> |
21 | | #include <LibWasm/AbstractMachine/Validator.h> |
22 | | #include <LibWeb/WebAssembly/Instance.h> |
23 | | #include <LibWeb/WebAssembly/Memory.h> |
24 | | #include <LibWeb/WebAssembly/Module.h> |
25 | | #include <LibWeb/WebAssembly/Table.h> |
26 | | #include <LibWeb/WebAssembly/WebAssembly.h> |
27 | | #include <LibWeb/WebIDL/Buffers.h> |
28 | | |
29 | | namespace Web::WebAssembly { |
30 | | |
31 | | namespace Detail { |
32 | | |
33 | | HashMap<JS::GCPtr<JS::Object>, WebAssemblyCache> s_caches; |
34 | | |
35 | | WebAssemblyCache& get_cache(JS::Realm& realm) |
36 | 0 | { |
37 | 0 | return s_caches.ensure(realm.global_object()); |
38 | 0 | } |
39 | | |
40 | | } |
41 | | |
42 | | void visit_edges(JS::Object& object, JS::Cell::Visitor& visitor) |
43 | 0 | { |
44 | 0 | auto& global_object = HTML::relevant_global_object(object); |
45 | 0 | if (auto maybe_cache = Detail::s_caches.get(global_object); maybe_cache.has_value()) { |
46 | 0 | auto& cache = maybe_cache.release_value(); |
47 | 0 | visitor.visit(cache.function_instances()); |
48 | 0 | visitor.visit(cache.imported_objects()); |
49 | 0 | } |
50 | 0 | } |
51 | | |
52 | | void finalize(JS::Object& object) |
53 | 0 | { |
54 | 0 | auto& global_object = HTML::relevant_global_object(object); |
55 | 0 | Detail::s_caches.remove(global_object); |
56 | 0 | } |
57 | | |
58 | | // https://webassembly.github.io/spec/js-api/#dom-webassembly-validate |
59 | | bool validate(JS::VM& vm, JS::Handle<WebIDL::BufferSource>& bytes) |
60 | 0 | { |
61 | | // 1. Let stableBytes be a copy of the bytes held by the buffer bytes. |
62 | | // Note: There's no need to copy the bytes here as the buffer data cannot change while we're compiling the module. |
63 | | |
64 | | // 2. Compile stableBytes as a WebAssembly module and store the results as module. |
65 | 0 | auto module_or_error = Detail::parse_module(vm, bytes->raw_object()); |
66 | | |
67 | | // 3. If module is error, return false. |
68 | 0 | if (module_or_error.is_error()) |
69 | 0 | return false; |
70 | | |
71 | | // 3 continued - our "compile" step is lazy with validation, explicitly do the validation. |
72 | 0 | auto compiled_module = module_or_error.release_value(); |
73 | 0 | auto& cache = Detail::get_cache(*vm.current_realm()); |
74 | 0 | if (cache.abstract_machine().validate(compiled_module->module).is_error()) |
75 | 0 | return false; |
76 | | |
77 | | // 4. Return true. |
78 | 0 | return true; |
79 | 0 | } |
80 | | |
81 | | // https://webassembly.github.io/spec/js-api/#dom-webassembly-compile |
82 | | WebIDL::ExceptionOr<JS::Value> compile(JS::VM& vm, JS::Handle<WebIDL::BufferSource>& bytes) |
83 | 0 | { |
84 | 0 | auto& realm = *vm.current_realm(); |
85 | | |
86 | | // FIXME: This shouldn't block! |
87 | 0 | auto compiled_module_or_error = Detail::parse_module(vm, bytes->raw_object()); |
88 | 0 | auto promise = JS::Promise::create(realm); |
89 | |
|
90 | 0 | if (compiled_module_or_error.is_error()) { |
91 | 0 | promise->reject(*compiled_module_or_error.release_error().value()); |
92 | 0 | } else { |
93 | 0 | auto module_object = vm.heap().allocate<Module>(realm, realm, compiled_module_or_error.release_value()); |
94 | 0 | promise->fulfill(module_object); |
95 | 0 | } |
96 | |
|
97 | 0 | return promise; |
98 | 0 | } |
99 | | |
100 | | // https://webassembly.github.io/spec/js-api/#dom-webassembly-instantiate |
101 | | WebIDL::ExceptionOr<JS::Value> instantiate(JS::VM& vm, JS::Handle<WebIDL::BufferSource>& bytes, Optional<JS::Handle<JS::Object>>& import_object) |
102 | 0 | { |
103 | | // FIXME: Implement the importObject parameter. |
104 | 0 | (void)import_object; |
105 | |
|
106 | 0 | auto& realm = *vm.current_realm(); |
107 | | |
108 | | // FIXME: This shouldn't block! |
109 | 0 | auto compiled_module_or_error = Detail::parse_module(vm, bytes->raw_object()); |
110 | 0 | auto promise = JS::Promise::create(realm); |
111 | |
|
112 | 0 | if (compiled_module_or_error.is_error()) { |
113 | 0 | promise->reject(*compiled_module_or_error.release_error().value()); |
114 | 0 | return promise; |
115 | 0 | } |
116 | | |
117 | 0 | auto compiled_module = compiled_module_or_error.release_value(); |
118 | 0 | auto result = Detail::instantiate_module(vm, compiled_module->module); |
119 | |
|
120 | 0 | if (result.is_error()) { |
121 | 0 | promise->reject(*result.release_error().value()); |
122 | 0 | } else { |
123 | 0 | auto module_object = vm.heap().allocate<Module>(realm, realm, move(compiled_module)); |
124 | 0 | auto instance_object = vm.heap().allocate<Instance>(realm, realm, result.release_value()); |
125 | |
|
126 | 0 | auto object = JS::Object::create(realm, nullptr); |
127 | 0 | object->define_direct_property("module", module_object, JS::default_attributes); |
128 | 0 | object->define_direct_property("instance", instance_object, JS::default_attributes); |
129 | 0 | promise->fulfill(object); |
130 | 0 | } |
131 | |
|
132 | 0 | return promise; |
133 | 0 | } |
134 | | |
135 | | // https://webassembly.github.io/spec/js-api/#dom-webassembly-instantiate-moduleobject-importobject |
136 | | WebIDL::ExceptionOr<JS::Value> instantiate(JS::VM& vm, Module const& module_object, Optional<JS::Handle<JS::Object>>& import_object) |
137 | 0 | { |
138 | | // FIXME: Implement the importObject parameter. |
139 | 0 | (void)import_object; |
140 | |
|
141 | 0 | auto& realm = *vm.current_realm(); |
142 | 0 | auto promise = JS::Promise::create(realm); |
143 | |
|
144 | 0 | auto const& compiled_module = module_object.compiled_module(); |
145 | 0 | auto result = Detail::instantiate_module(vm, compiled_module->module); |
146 | |
|
147 | 0 | if (result.is_error()) { |
148 | 0 | promise->reject(*result.release_error().value()); |
149 | 0 | } else { |
150 | 0 | auto instance_object = vm.heap().allocate<Instance>(realm, realm, result.release_value()); |
151 | 0 | promise->fulfill(instance_object); |
152 | 0 | } |
153 | |
|
154 | 0 | return promise; |
155 | 0 | } |
156 | | |
157 | | namespace Detail { |
158 | | |
159 | | JS::ThrowCompletionOr<NonnullOwnPtr<Wasm::ModuleInstance>> instantiate_module(JS::VM& vm, Wasm::Module const& module) |
160 | 0 | { |
161 | 0 | Wasm::Linker linker { module }; |
162 | 0 | HashMap<Wasm::Linker::Name, Wasm::ExternValue> resolved_imports; |
163 | 0 | auto import_argument = vm.argument(1); |
164 | 0 | auto& cache = get_cache(*vm.current_realm()); |
165 | 0 | if (!import_argument.is_undefined()) { |
166 | 0 | auto import_object = TRY(import_argument.to_object(vm)); |
167 | 0 | dbgln("Trying to resolve stuff because import object was specified"); |
168 | 0 | for (Wasm::Linker::Name const& import_name : linker.unresolved_imports()) { |
169 | 0 | dbgln("Trying to resolve {}::{}", import_name.module, import_name.name); |
170 | 0 | auto value_or_error = import_object->get(import_name.module); |
171 | 0 | if (value_or_error.is_error()) |
172 | 0 | break; |
173 | 0 | auto value = value_or_error.release_value(); |
174 | 0 | auto object_or_error = value.to_object(vm); |
175 | 0 | if (object_or_error.is_error()) |
176 | 0 | break; |
177 | 0 | auto object = object_or_error.release_value(); |
178 | 0 | auto import_or_error = object->get(import_name.name); |
179 | 0 | if (import_or_error.is_error()) |
180 | 0 | break; |
181 | 0 | auto import_ = import_or_error.release_value(); |
182 | 0 | TRY(import_name.type.visit( |
183 | 0 | [&](Wasm::TypeIndex index) -> JS::ThrowCompletionOr<void> { |
184 | 0 | dbgln("Trying to resolve a function {}::{}, type index {}", import_name.module, import_name.name, index.value()); |
185 | 0 | auto& type = module.type_section().types()[index.value()]; |
186 | | // FIXME: IsCallable() |
187 | 0 | if (!import_.is_function()) |
188 | 0 | return {}; |
189 | 0 | auto& function = import_.as_function(); |
190 | 0 | cache.add_imported_object(function); |
191 | | // FIXME: If this is a function created by create_native_function(), |
192 | | // just extract its address and resolve to that. |
193 | 0 | Wasm::HostFunction host_function { |
194 | 0 | [&](auto&, auto& arguments) -> Wasm::Result { |
195 | 0 | JS::MarkedVector<JS::Value> argument_values { vm.heap() }; |
196 | 0 | size_t index = 0; |
197 | 0 | for (auto& entry : arguments) { |
198 | 0 | argument_values.append(to_js_value(vm, entry, type.parameters()[index])); |
199 | 0 | ++index; |
200 | 0 | } |
201 | |
|
202 | 0 | auto result = TRY(JS::call(vm, function, JS::js_undefined(), argument_values.span())); |
203 | 0 | if (type.results().is_empty()) |
204 | 0 | return Wasm::Result { Vector<Wasm::Value> {} }; |
205 | |
|
206 | 0 | if (type.results().size() == 1) |
207 | 0 | return Wasm::Result { Vector<Wasm::Value> { TRY(to_webassembly_value(vm, result, type.results().first())) } }; |
208 | |
|
209 | 0 | auto method = TRY(result.get_method(vm, vm.names.iterator)); |
210 | 0 | if (method == JS::js_undefined()) |
211 | 0 | return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotIterable, result.to_string_without_side_effects()); |
212 | |
|
213 | 0 | auto values = TRY(JS::iterator_to_list(vm, TRY(JS::get_iterator_from_method(vm, result, *method)))); |
214 | |
|
215 | 0 | if (values.size() != type.results().size()) |
216 | 0 | return vm.throw_completion<JS::TypeError>(ByteString::formatted("Invalid number of return values for multi-value wasm return of {} objects", type.results().size())); |
217 | |
|
218 | 0 | Vector<Wasm::Value> wasm_values; |
219 | 0 | TRY_OR_THROW_OOM(vm, wasm_values.try_ensure_capacity(values.size())); |
220 | |
|
221 | 0 | size_t i = 0; |
222 | 0 | for (auto& value : values) |
223 | 0 | wasm_values.append(TRY(to_webassembly_value(vm, value, type.results()[i++]))); |
224 | |
|
225 | 0 | return Wasm::Result { move(wasm_values) }; |
226 | 0 | }, |
227 | 0 | type, |
228 | 0 | ByteString::formatted("func{}", resolved_imports.size()), |
229 | 0 | }; |
230 | 0 | auto address = cache.abstract_machine().store().allocate(move(host_function)); |
231 | 0 | dbgln("Resolved to {}", address->value()); |
232 | | // FIXME: LinkError instead. |
233 | 0 | VERIFY(address.has_value()); |
234 | |
|
235 | 0 | resolved_imports.set(import_name, Wasm::ExternValue { Wasm::FunctionAddress { *address } }); |
236 | 0 | return {}; |
237 | 0 | }, |
238 | 0 | [&](Wasm::GlobalType const& type) -> JS::ThrowCompletionOr<void> { |
239 | 0 | Optional<Wasm::GlobalAddress> address; |
240 | | // https://webassembly.github.io/spec/js-api/#read-the-imports step 5.1 |
241 | 0 | if (import_.is_number() || import_.is_bigint()) { |
242 | 0 | if (import_.is_number() && type.type().kind() == Wasm::ValueType::I64) { |
243 | | // FIXME: Throw a LinkError instead. |
244 | 0 | return vm.throw_completion<JS::TypeError>("LinkError: Import resolution attempted to cast a Number to a BigInteger"sv); |
245 | 0 | } |
246 | 0 | if (import_.is_bigint() && type.type().kind() != Wasm::ValueType::I64) { |
247 | | // FIXME: Throw a LinkError instead. |
248 | 0 | return vm.throw_completion<JS::TypeError>("LinkError: Import resolution attempted to cast a BigInteger to a Number"sv); |
249 | 0 | } |
250 | 0 | auto cast_value = TRY(to_webassembly_value(vm, import_, type.type())); |
251 | 0 | address = cache.abstract_machine().store().allocate({ type.type(), false }, cast_value); |
252 | 0 | } else { |
253 | | // FIXME: https://webassembly.github.io/spec/js-api/#read-the-imports step 5.2 |
254 | | // if v implements Global |
255 | | // let globaladdr be v.[[Global]] |
256 | | |
257 | | // FIXME: Throw a LinkError instead |
258 | 0 | return vm.throw_completion<JS::TypeError>("LinkError: Invalid value for global type"sv); |
259 | 0 | } |
260 | |
|
261 | 0 | resolved_imports.set(import_name, Wasm::ExternValue { *address }); |
262 | 0 | return {}; |
263 | 0 | }, |
264 | 0 | [&](Wasm::MemoryType const&) -> JS::ThrowCompletionOr<void> { |
265 | 0 | if (!import_.is_object() || !is<WebAssembly::Memory>(import_.as_object())) { |
266 | | // FIXME: Throw a LinkError instead |
267 | 0 | return vm.throw_completion<JS::TypeError>("LinkError: Expected an instance of WebAssembly.Memory for a memory import"sv); |
268 | 0 | } |
269 | 0 | auto address = static_cast<WebAssembly::Memory const&>(import_.as_object()).address(); |
270 | 0 | resolved_imports.set(import_name, Wasm::ExternValue { address }); |
271 | 0 | return {}; |
272 | 0 | }, |
273 | 0 | [&](Wasm::TableType const&) -> JS::ThrowCompletionOr<void> { |
274 | 0 | if (!import_.is_object() || !is<WebAssembly::Table>(import_.as_object())) { |
275 | | // FIXME: Throw a LinkError instead |
276 | 0 | return vm.throw_completion<JS::TypeError>("LinkError: Expected an instance of WebAssembly.Table for a table import"sv); |
277 | 0 | } |
278 | 0 | auto address = static_cast<WebAssembly::Table const&>(import_.as_object()).address(); |
279 | 0 | resolved_imports.set(import_name, Wasm::ExternValue { address }); |
280 | 0 | return {}; |
281 | 0 | }, |
282 | 0 | [&](auto const&) -> JS::ThrowCompletionOr<void> { |
283 | | // FIXME: Implement these. |
284 | 0 | dbgln("Unimplemented import of non-function attempted"); |
285 | 0 | return vm.throw_completion<JS::TypeError>("LinkError: Not Implemented"sv); |
286 | 0 | })); |
287 | 0 | } |
288 | 0 | } |
289 | | |
290 | 0 | linker.link(resolved_imports); |
291 | 0 | auto link_result = linker.finish(); |
292 | 0 | if (link_result.is_error()) { |
293 | | // FIXME: Throw a LinkError. |
294 | 0 | StringBuilder builder; |
295 | 0 | builder.append("LinkError: Missing "sv); |
296 | 0 | builder.join(' ', link_result.error().missing_imports); |
297 | 0 | return vm.throw_completion<JS::TypeError>(MUST(builder.to_string())); |
298 | 0 | } |
299 | | |
300 | 0 | auto instance_result = cache.abstract_machine().instantiate(module, link_result.release_value()); |
301 | 0 | if (instance_result.is_error()) { |
302 | | // FIXME: Throw a LinkError instead. |
303 | 0 | return vm.throw_completion<JS::TypeError>(instance_result.error().error); |
304 | 0 | } |
305 | | |
306 | 0 | return instance_result.release_value(); |
307 | 0 | } |
308 | | |
309 | | JS::ThrowCompletionOr<NonnullRefPtr<CompiledWebAssemblyModule>> parse_module(JS::VM& vm, JS::Object* buffer_object) |
310 | 0 | { |
311 | 0 | ReadonlyBytes data; |
312 | 0 | if (is<JS::ArrayBuffer>(buffer_object)) { |
313 | 0 | auto& buffer = static_cast<JS::ArrayBuffer&>(*buffer_object); |
314 | 0 | data = buffer.buffer(); |
315 | 0 | } else if (is<JS::TypedArrayBase>(buffer_object)) { |
316 | 0 | auto& buffer = static_cast<JS::TypedArrayBase&>(*buffer_object); |
317 | |
|
318 | 0 | auto typed_array_record = JS::make_typed_array_with_buffer_witness_record(buffer, JS::ArrayBuffer::Order::SeqCst); |
319 | 0 | if (JS::is_typed_array_out_of_bounds(typed_array_record)) |
320 | 0 | return vm.throw_completion<JS::TypeError>(JS::ErrorType::BufferOutOfBounds, "TypedArray"sv); |
321 | | |
322 | 0 | data = buffer.viewed_array_buffer()->buffer().span().slice(buffer.byte_offset(), JS::typed_array_byte_length(typed_array_record)); |
323 | 0 | } else if (is<JS::DataView>(buffer_object)) { |
324 | 0 | auto& buffer = static_cast<JS::DataView&>(*buffer_object); |
325 | |
|
326 | 0 | auto view_record = JS::make_data_view_with_buffer_witness_record(buffer, JS::ArrayBuffer::Order::SeqCst); |
327 | 0 | if (JS::is_view_out_of_bounds(view_record)) |
328 | 0 | return vm.throw_completion<JS::TypeError>(JS::ErrorType::BufferOutOfBounds, "DataView"sv); |
329 | | |
330 | 0 | data = buffer.viewed_array_buffer()->buffer().span().slice(buffer.byte_offset(), JS::get_view_byte_length(view_record)); |
331 | 0 | } else { |
332 | 0 | return vm.throw_completion<JS::TypeError>("Not a BufferSource"sv); |
333 | 0 | } |
334 | 0 | FixedMemoryStream stream { data }; |
335 | 0 | auto module_result = Wasm::Module::parse(stream); |
336 | 0 | if (module_result.is_error()) { |
337 | | // FIXME: Throw CompileError instead. |
338 | 0 | return vm.throw_completion<JS::TypeError>(Wasm::parse_error_to_byte_string(module_result.error())); |
339 | 0 | } |
340 | | |
341 | 0 | auto& cache = get_cache(*vm.current_realm()); |
342 | 0 | if (auto validation_result = cache.abstract_machine().validate(module_result.value()); validation_result.is_error()) { |
343 | | // FIXME: Throw CompileError instead. |
344 | 0 | return vm.throw_completion<JS::TypeError>(validation_result.error().error_string); |
345 | 0 | } |
346 | 0 | auto compiled_module = make_ref_counted<CompiledWebAssemblyModule>(module_result.release_value()); |
347 | 0 | cache.add_compiled_module(compiled_module); |
348 | 0 | return compiled_module; |
349 | 0 | } |
350 | | |
351 | | JS::NativeFunction* create_native_function(JS::VM& vm, Wasm::FunctionAddress address, ByteString const& name, Instance* instance) |
352 | 0 | { |
353 | 0 | auto& realm = *vm.current_realm(); |
354 | 0 | Optional<Wasm::FunctionType> type; |
355 | 0 | auto& cache = get_cache(realm); |
356 | 0 | cache.abstract_machine().store().get(address)->visit([&](auto const& value) { type = value.type(); });Unexecuted instantiation: WebAssembly.cpp:auto Web::WebAssembly::Detail::create_native_function(JS::VM&, AK::DistinctNumeric<unsigned long, Wasm::__FunctionAddress_tag, AK::DistinctNumericFeature::Arithmetic, AK::DistinctNumericFeature::Comparison, AK::DistinctNumericFeature::Increment>, AK::ByteString const&, Web::WebAssembly::Instance*)::$_0::operator()<Wasm::WasmFunction>(Wasm::WasmFunction const&) const Unexecuted instantiation: WebAssembly.cpp:auto Web::WebAssembly::Detail::create_native_function(JS::VM&, AK::DistinctNumeric<unsigned long, Wasm::__FunctionAddress_tag, AK::DistinctNumericFeature::Arithmetic, AK::DistinctNumericFeature::Comparison, AK::DistinctNumericFeature::Increment>, AK::ByteString const&, Web::WebAssembly::Instance*)::$_0::operator()<Wasm::HostFunction>(Wasm::HostFunction const&) const |
357 | 0 | if (auto entry = cache.get_function_instance(address); entry.has_value()) |
358 | 0 | return *entry; |
359 | | |
360 | 0 | auto function = JS::NativeFunction::create( |
361 | 0 | realm, |
362 | 0 | name, |
363 | 0 | [address, type = type.release_value(), instance](JS::VM& vm) -> JS::ThrowCompletionOr<JS::Value> { |
364 | 0 | (void)instance; |
365 | 0 | auto& realm = *vm.current_realm(); |
366 | 0 | Vector<Wasm::Value> values; |
367 | 0 | values.ensure_capacity(type.parameters().size()); |
368 | | |
369 | | // Grab as many values as needed and convert them. |
370 | 0 | size_t index = 0; |
371 | 0 | for (auto& type : type.parameters()) |
372 | 0 | values.append(TRY(to_webassembly_value(vm, vm.argument(index++), type))); |
373 | |
|
374 | 0 | auto& cache = get_cache(realm); |
375 | 0 | auto result = cache.abstract_machine().invoke(address, move(values)); |
376 | | // FIXME: Use the convoluted mapping of errors defined in the spec. |
377 | 0 | if (result.is_trap()) |
378 | 0 | return vm.throw_completion<JS::TypeError>(TRY_OR_THROW_OOM(vm, String::formatted("Wasm execution trapped (WIP): {}", result.trap().reason))); |
379 | | |
380 | 0 | if (result.values().is_empty()) |
381 | 0 | return JS::js_undefined(); |
382 | | |
383 | 0 | if (result.values().size() == 1) |
384 | 0 | return to_js_value(vm, result.values().first(), type.results().first()); |
385 | | |
386 | | // Put result values into a JS::Array in reverse order. |
387 | 0 | auto js_result_values = JS::MarkedVector<JS::Value> { realm.heap() }; |
388 | 0 | js_result_values.ensure_capacity(result.values().size()); |
389 | |
|
390 | 0 | for (size_t i = result.values().size(); i > 0; i--) { |
391 | | // Safety: ensure_capacity is called just before this. |
392 | 0 | js_result_values.unchecked_append(to_js_value(vm, result.values().at(i - 1), type.results().at(i - 1))); |
393 | 0 | } |
394 | |
|
395 | 0 | return JS::Value(JS::Array::create_from(realm, js_result_values)); |
396 | 0 | }); |
397 | |
|
398 | 0 | cache.add_function_instance(address, function); |
399 | 0 | return function; |
400 | 0 | } |
401 | | |
402 | | JS::ThrowCompletionOr<Wasm::Value> to_webassembly_value(JS::VM& vm, JS::Value value, Wasm::ValueType const& type) |
403 | 0 | { |
404 | 0 | static ::Crypto::SignedBigInteger two_64 = "1"_sbigint.shift_left(64); |
405 | |
|
406 | 0 | switch (type.kind()) { |
407 | 0 | case Wasm::ValueType::I64: { |
408 | 0 | auto bigint = TRY(value.to_bigint(vm)); |
409 | 0 | auto value = bigint->big_integer().divided_by(two_64).remainder; |
410 | 0 | VERIFY(value.unsigned_value().trimmed_length() <= 2); |
411 | 0 | i64 integer = static_cast<i64>(value.unsigned_value().to_u64()); |
412 | 0 | if (value.is_negative()) |
413 | 0 | integer = -integer; |
414 | 0 | return Wasm::Value { integer }; |
415 | 0 | } |
416 | 0 | case Wasm::ValueType::I32: { |
417 | 0 | auto _i32 = TRY(value.to_i32(vm)); |
418 | 0 | return Wasm::Value { static_cast<i32>(_i32) }; |
419 | 0 | } |
420 | 0 | case Wasm::ValueType::F64: { |
421 | 0 | auto number = TRY(value.to_double(vm)); |
422 | 0 | return Wasm::Value { static_cast<double>(number) }; |
423 | 0 | } |
424 | 0 | case Wasm::ValueType::F32: { |
425 | 0 | auto number = TRY(value.to_double(vm)); |
426 | 0 | return Wasm::Value { static_cast<float>(number) }; |
427 | 0 | } |
428 | 0 | case Wasm::ValueType::FunctionReference: { |
429 | 0 | if (value.is_null()) |
430 | 0 | return Wasm::Value(); |
431 | | |
432 | 0 | if (value.is_function()) { |
433 | 0 | auto& function = value.as_function(); |
434 | 0 | auto& cache = get_cache(*vm.current_realm()); |
435 | 0 | for (auto& entry : cache.function_instances()) { |
436 | 0 | if (entry.value == &function) |
437 | 0 | return Wasm::Value { Wasm::Reference { Wasm::Reference::Func { entry.key, cache.abstract_machine().store().get_module_for(entry.key) } } }; |
438 | 0 | } |
439 | 0 | } |
440 | | |
441 | 0 | return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "Exported function"); |
442 | 0 | } |
443 | 0 | case Wasm::ValueType::ExternReference: |
444 | 0 | TODO(); |
445 | 0 | case Wasm::ValueType::V128: |
446 | 0 | return vm.throw_completion<JS::TypeError>("Cannot convert a vector value to a javascript value"sv); |
447 | 0 | } |
448 | | |
449 | 0 | VERIFY_NOT_REACHED(); |
450 | 0 | } |
451 | | |
452 | | JS::Value to_js_value(JS::VM& vm, Wasm::Value& wasm_value, Wasm::ValueType type) |
453 | 0 | { |
454 | 0 | auto& realm = *vm.current_realm(); |
455 | 0 | switch (type.kind()) { |
456 | 0 | case Wasm::ValueType::I64: |
457 | 0 | return realm.heap().allocate<JS::BigInt>(realm, ::Crypto::SignedBigInteger { wasm_value.to<i64>() }); |
458 | 0 | case Wasm::ValueType::I32: |
459 | 0 | return JS::Value(wasm_value.to<i32>()); |
460 | 0 | case Wasm::ValueType::F64: |
461 | 0 | return JS::Value(wasm_value.to<double>()); |
462 | 0 | case Wasm::ValueType::F32: |
463 | 0 | return JS::Value(static_cast<double>(wasm_value.to<float>())); |
464 | 0 | case Wasm::ValueType::FunctionReference: { |
465 | 0 | auto ref_ = wasm_value.to<Wasm::Reference>(); |
466 | 0 | if (ref_.ref().has<Wasm::Reference::Null>()) |
467 | 0 | return JS::js_null(); |
468 | 0 | auto address = ref_.ref().get<Wasm::Reference::Func>().address; |
469 | 0 | auto& cache = get_cache(realm); |
470 | 0 | auto* function = cache.abstract_machine().store().get(address); |
471 | 0 | auto name = function->visit( |
472 | 0 | [&](Wasm::WasmFunction& wasm_function) { |
473 | 0 | auto index = *wasm_function.module().functions().find_first_index(address); |
474 | 0 | return ByteString::formatted("func{}", index); |
475 | 0 | }, |
476 | 0 | [](Wasm::HostFunction& host_function) { |
477 | 0 | return host_function.name(); |
478 | 0 | }); |
479 | 0 | return create_native_function(vm, address, name); |
480 | 0 | } |
481 | 0 | case Wasm::ValueType::V128: |
482 | 0 | case Wasm::ValueType::ExternReference: |
483 | 0 | TODO(); |
484 | 0 | } |
485 | 0 | VERIFY_NOT_REACHED(); |
486 | 0 | } |
487 | | |
488 | | } |
489 | | |
490 | | } |