2023-03-16 12:59:32 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
|
|
|
|
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Optional.h>
|
|
|
|
#include <LibJS/Forward.h>
|
|
|
|
#include <LibJS/Heap/Handle.h>
|
2023-03-16 14:11:21 -04:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2023-03-16 12:59:32 -04:00
|
|
|
#include <LibJS/Runtime/Value.h>
|
2023-03-16 14:11:21 -04:00
|
|
|
#include <LibWasm/AbstractMachine/AbstractMachine.h>
|
2023-03-16 12:59:32 -04:00
|
|
|
#include <LibWeb/Bindings/ExceptionOrUtils.h>
|
|
|
|
#include <LibWeb/Forward.h>
|
|
|
|
|
|
|
|
namespace Web::WebAssembly {
|
|
|
|
|
2024-04-25 19:09:34 +01:00
|
|
|
void visit_edges(JS::Object&, JS::Cell::Visitor&);
|
2024-04-25 19:09:34 +01:00
|
|
|
void finalize(JS::Object&);
|
2023-03-16 12:59:32 -04:00
|
|
|
|
2023-11-23 20:07:25 +13:00
|
|
|
bool validate(JS::VM&, JS::Handle<WebIDL::BufferSource>& bytes);
|
|
|
|
WebIDL::ExceptionOr<JS::Value> compile(JS::VM&, JS::Handle<WebIDL::BufferSource>& bytes);
|
2023-03-16 12:59:32 -04:00
|
|
|
|
2023-11-23 20:07:25 +13:00
|
|
|
WebIDL::ExceptionOr<JS::Value> instantiate(JS::VM&, JS::Handle<WebIDL::BufferSource>& bytes, Optional<JS::Handle<JS::Object>>& import_object);
|
2023-03-16 12:59:32 -04:00
|
|
|
WebIDL::ExceptionOr<JS::Value> instantiate(JS::VM&, Module const& module_object, Optional<JS::Handle<JS::Object>>& import_object);
|
|
|
|
|
2023-03-16 14:11:21 -04:00
|
|
|
namespace Detail {
|
2024-04-25 19:09:34 +01:00
|
|
|
struct CompiledWebAssemblyModule : public RefCounted<CompiledWebAssemblyModule> {
|
2024-08-22 01:13:37 +02:00
|
|
|
explicit CompiledWebAssemblyModule(NonnullRefPtr<Wasm::Module> module)
|
2023-03-16 14:11:21 -04:00
|
|
|
: module(move(module))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-08-22 01:13:37 +02:00
|
|
|
NonnullRefPtr<Wasm::Module> module;
|
2023-03-16 14:11:21 -04:00
|
|
|
};
|
|
|
|
|
2024-04-25 19:09:34 +01:00
|
|
|
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); }
|
2024-07-31 12:34:55 +02:00
|
|
|
void add_imported_object(JS::GCPtr<JS::Object> object) { m_imported_objects.set(object); }
|
2024-08-17 15:40:21 -07:00
|
|
|
void add_extern_value(Wasm::ExternAddress address, JS::Value value) { m_extern_values.set(address, value); }
|
2024-04-25 19:09:34 +01:00
|
|
|
|
|
|
|
Optional<JS::GCPtr<JS::NativeFunction>> get_function_instance(Wasm::FunctionAddress address) { return m_function_instances.get(address); }
|
2024-08-17 15:40:21 -07:00
|
|
|
Optional<JS::Value> get_extern_value(Wasm::ExternAddress address) { return m_extern_values.get(address); }
|
2024-04-25 19:09:34 +01:00
|
|
|
|
|
|
|
HashMap<Wasm::FunctionAddress, JS::GCPtr<JS::NativeFunction>> function_instances() const { return m_function_instances; }
|
2024-08-17 15:40:21 -07:00
|
|
|
HashMap<Wasm::ExternAddress, JS::Value> extern_values() const { return m_extern_values; }
|
2024-07-31 12:34:55 +02:00
|
|
|
HashTable<JS::GCPtr<JS::Object>> imported_objects() const { return m_imported_objects; }
|
2024-04-25 19:09:34 +01:00
|
|
|
Wasm::AbstractMachine& abstract_machine() { return m_abstract_machine; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
HashMap<Wasm::FunctionAddress, JS::GCPtr<JS::NativeFunction>> m_function_instances;
|
2024-08-17 15:40:21 -07:00
|
|
|
HashMap<Wasm::ExternAddress, JS::Value> m_extern_values;
|
2024-04-25 19:09:34 +01:00
|
|
|
Vector<NonnullRefPtr<CompiledWebAssemblyModule>> m_compiled_modules;
|
2024-07-31 12:34:55 +02:00
|
|
|
HashTable<JS::GCPtr<JS::Object>> m_imported_objects;
|
2024-04-25 19:09:34 +01:00
|
|
|
Wasm::AbstractMachine m_abstract_machine;
|
2023-03-16 14:11:21 -04:00
|
|
|
};
|
|
|
|
|
2024-04-25 19:09:34 +01:00
|
|
|
WebAssemblyCache& get_cache(JS::Realm&);
|
|
|
|
|
|
|
|
JS::ThrowCompletionOr<NonnullOwnPtr<Wasm::ModuleInstance>> instantiate_module(JS::VM&, Wasm::Module const&);
|
|
|
|
JS::ThrowCompletionOr<NonnullRefPtr<CompiledWebAssemblyModule>> parse_module(JS::VM&, JS::Object* buffer);
|
2024-05-18 20:01:29 +02:00
|
|
|
JS::NativeFunction* create_native_function(JS::VM&, Wasm::FunctionAddress address, ByteString const& name, Instance* instance = nullptr);
|
2024-04-25 19:09:34 +01:00
|
|
|
JS::ThrowCompletionOr<Wasm::Value> to_webassembly_value(JS::VM&, JS::Value value, Wasm::ValueType const& type);
|
2024-08-17 17:02:59 -07:00
|
|
|
Wasm::Value default_webassembly_value(JS::VM&, Wasm::ValueType type);
|
2024-08-04 08:06:50 -07:00
|
|
|
JS::Value to_js_value(JS::VM&, Wasm::Value& wasm_value, Wasm::ValueType type);
|
2024-04-25 19:09:34 +01:00
|
|
|
|
|
|
|
extern HashMap<JS::GCPtr<JS::Object>, WebAssemblyCache> s_caches;
|
2023-03-16 14:11:21 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-16 12:59:32 -04:00
|
|
|
}
|