/* * 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 #include namespace Web::WebAssembly { void visit_edges(JS::Object&, JS::Cell::Visitor&); void finalize(JS::Object&); void initialize(JS::Object&, JS::Realm&); bool validate(JS::VM&, GC::Root& bytes); WebIDL::ExceptionOr> compile(JS::VM&, GC::Root& bytes); WebIDL::ExceptionOr> compile_streaming(JS::VM&, GC::Root source); WebIDL::ExceptionOr> instantiate(JS::VM&, GC::Root& bytes, Optional>& import_object); WebIDL::ExceptionOr> instantiate(JS::VM&, Module const& module_object, Optional>& import_object); WebIDL::ExceptionOr> instantiate_streaming(JS::VM&, GC::Root source, Optional>& import_object); namespace Detail { struct CompiledWebAssemblyModule : public RefCounted { explicit CompiledWebAssemblyModule(NonnullRefPtr module) : module(move(module)) { } NonnullRefPtr module; }; class WebAssemblyCache { public: void add_compiled_module(NonnullRefPtr module) { m_compiled_modules.append(module); } void add_function_instance(Wasm::FunctionAddress address, GC::Ptr function) { m_function_instances.set(address, function); } void add_imported_object(GC::Ptr object) { m_imported_objects.set(object); } void add_extern_value(Wasm::ExternAddress address, JS::Value value) { m_extern_values.set(address, value); } void add_global_instance(Wasm::GlobalAddress address, GC::Ptr global) { m_global_instances.set(address, global); } Optional> get_function_instance(Wasm::FunctionAddress address) { return m_function_instances.get(address); } Optional get_extern_value(Wasm::ExternAddress address) { return m_extern_values.get(address); } Optional> get_global_instance(Wasm::GlobalAddress address) { return m_global_instances.get(address); } HashMap> function_instances() const { return m_function_instances; } HashMap extern_values() const { return m_extern_values; } HashMap> global_instances() const { return m_global_instances; } HashTable> imported_objects() const { return m_imported_objects; } Wasm::AbstractMachine& abstract_machine() { return m_abstract_machine; } private: HashMap> m_function_instances; HashMap m_extern_values; HashMap> m_global_instances; Vector> m_compiled_modules; HashTable> m_imported_objects; Wasm::AbstractMachine m_abstract_machine; }; class ExportedWasmFunction final : public JS::NativeFunction { JS_OBJECT(ExportedWasmFunction, JS::NativeFunction); GC_DECLARE_ALLOCATOR(ExportedWasmFunction); public: static GC::Ref create(JS::Realm&, FlyString const& name, ESCAPING Function(JS::VM&)>, Wasm::FunctionAddress); virtual ~ExportedWasmFunction() override = default; Wasm::FunctionAddress exported_address() const { return m_exported_address; } protected: ExportedWasmFunction(FlyString name, AK::Function(JS::VM&)>, Wasm::FunctionAddress, Object& prototype); private: Wasm::FunctionAddress m_exported_address; }; WebAssemblyCache& get_cache(JS::Realm&); JS::ThrowCompletionOr> instantiate_module(JS::VM&, Wasm::Module const&, GC::Ptr import_object); JS::ThrowCompletionOr> compile_a_webassembly_module(JS::VM&, ByteBuffer); JS::NativeFunction* create_native_function(JS::VM&, Wasm::FunctionAddress address, String const& name, Instance* instance = nullptr); JS::ThrowCompletionOr to_webassembly_value(JS::VM&, JS::Value value, Wasm::ValueType const& type); Wasm::Value default_webassembly_value(JS::VM&, Wasm::ValueType type); JS::Value to_js_value(JS::VM&, Wasm::Value& wasm_value, Wasm::ValueType type); extern HashMap, WebAssemblyCache> s_caches; } // NOTE: This is technically not allowed by ECMA262, as the set of native errors is closed // our implementation uses this fact in places, but for the purposes of wasm returning // *some* kind of error, named e.g. 'WebAssembly.RuntimeError', this is sufficient. #define DECLARE_WASM_NATIVE_ERROR(ClassName, snake_name, PrototypeName, ConstructorName) \ class ClassName final : public JS::Error { \ JS_OBJECT(ClassName, Error); \ GC_DECLARE_ALLOCATOR(ClassName); \ \ public: \ static GC::Ref create(JS::Realm&); \ static GC::Ref create(JS::Realm&, String message); \ static GC::Ref create(JS::Realm&, StringView message); \ \ explicit ClassName(Object& prototype); \ virtual ~ClassName() override = default; \ }; #define DECLARE_WASM_NATIVE_ERROR_CONSTRUCTOR(ClassName, snake_name, PrototypeName, ConstructorName) \ class ConstructorName final : public JS::NativeFunction { \ JS_OBJECT(ConstructorName, NativeFunction); \ GC_DECLARE_ALLOCATOR(ConstructorName); \ \ public: \ virtual void initialize(JS::Realm&) override; \ virtual ~ConstructorName() override; \ virtual JS::ThrowCompletionOr call() override; \ virtual JS::ThrowCompletionOr> construct(JS::FunctionObject& new_target) override; \ \ private: \ explicit ConstructorName(JS::Realm&); \ \ virtual bool has_constructor() const override \ { \ return true; \ } \ }; #define DECLARE_WASM_NATIVE_ERROR_PROTOTYPE(ClassName, snake_name, PrototypeName, ConstructorName) \ class PrototypeName final : public JS::PrototypeObject { \ JS_PROTOTYPE_OBJECT(PrototypeName, ClassName, ClassName); \ GC_DECLARE_ALLOCATOR(PrototypeName); \ \ public: \ virtual void initialize(JS::Realm&) override; \ virtual ~PrototypeName() override = default; \ \ private: \ explicit PrototypeName(JS::Realm&); \ }; DECLARE_WASM_NATIVE_ERROR_CONSTRUCTOR(CompilError, compile_error, CompileErrorPrototype, CompileErrorConstructor) DECLARE_WASM_NATIVE_ERROR_CONSTRUCTOR(LinkError, link_error, LinkErrorPrototype, LinkErrorConstructor) DECLARE_WASM_NATIVE_ERROR_CONSTRUCTOR(RuntimeError, runtime_error, RuntimeErrorPrototype, RuntimeErrorConstructor) DECLARE_WASM_NATIVE_ERROR(CompileError, compile_error, CompileErrorPrototype, CompileErrorConstructor) DECLARE_WASM_NATIVE_ERROR(LinkError, link_error, LinkErrorPrototype, LinkErrorConstructor) DECLARE_WASM_NATIVE_ERROR(RuntimeError, runtime_error, LinkErrorPrototype, LinkErrorConstructor) DECLARE_WASM_NATIVE_ERROR_PROTOTYPE(CompileError, compile_error, CompileErrorPrototype, CompileErrorConstructor) DECLARE_WASM_NATIVE_ERROR_PROTOTYPE(LinkError, link_error, LinkErrorPrototype, LinkErrorConstructor) DECLARE_WASM_NATIVE_ERROR_PROTOTYPE(RuntimeError, runtime_error, RuntimeErrorPrototype, LinkErrorConstructor) #undef DECLARE_WASM_NATIVE_ERROR #undef DECLARE_WASM_NATIVE_ERROR_PROTOTYPE #undef DECLARE_WASM_NATIVE_ERROR_CONSTRUCTOR }