/* * Copyright (c) 2021, Ali Mohammad Pur * Copyright (c) 2023, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include namespace Web::WebAssembly { void visit_edges(JS::Object&, JS::Cell::Visitor&); void finalize(JS::Object&); bool validate(JS::VM&, JS::Handle& bytes); WebIDL::ExceptionOr compile(JS::VM&, JS::Handle& bytes); WebIDL::ExceptionOr instantiate(JS::VM&, JS::Handle& bytes, Optional>& import_object); WebIDL::ExceptionOr instantiate(JS::VM&, Module const& module_object, Optional>& import_object); namespace Detail { struct CompiledWebAssemblyModule : public RefCounted { explicit CompiledWebAssemblyModule(Wasm::Module&& module) : module(move(module)) { } Wasm::Module module; }; class WebAssemblyCache { public: void add_compiled_module(NonnullRefPtr module) { m_compiled_modules.append(module); } void add_function_instance(Wasm::FunctionAddress address, JS::GCPtr function) { m_function_instances.set(address, function); } Optional> get_function_instance(Wasm::FunctionAddress address) { return m_function_instances.get(address); } HashMap> function_instances() const { return m_function_instances; } Wasm::AbstractMachine& abstract_machine() { return m_abstract_machine; } private: HashMap> m_function_instances; Vector> m_compiled_modules; Wasm::AbstractMachine m_abstract_machine; }; WebAssemblyCache& get_cache(JS::Realm&); JS::ThrowCompletionOr> instantiate_module(JS::VM&, Wasm::Module const&); JS::ThrowCompletionOr> parse_module(JS::VM&, JS::Object* buffer); JS::NativeFunction* create_native_function(JS::VM&, Wasm::FunctionAddress address, ByteString const& name); JS::ThrowCompletionOr to_webassembly_value(JS::VM&, JS::Value value, Wasm::ValueType const& type); JS::Value to_js_value(JS::VM&, Wasm::Value& wasm_value); extern HashMap, WebAssemblyCache> s_caches; } }