Coverage Report

Created: 2026-07-25 07:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibWeb/WebAssembly/WebAssembly.h
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
#pragma once
9
10
#include <AK/Optional.h>
11
#include <LibJS/Forward.h>
12
#include <LibJS/Heap/Handle.h>
13
#include <LibJS/Runtime/Completion.h>
14
#include <LibJS/Runtime/Value.h>
15
#include <LibWasm/AbstractMachine/AbstractMachine.h>
16
#include <LibWeb/Bindings/ExceptionOrUtils.h>
17
#include <LibWeb/Forward.h>
18
19
namespace Web::WebAssembly {
20
21
void visit_edges(JS::Object&, JS::Cell::Visitor&);
22
void finalize(JS::Object&);
23
24
bool validate(JS::VM&, JS::Handle<WebIDL::BufferSource>& bytes);
25
WebIDL::ExceptionOr<JS::Value> compile(JS::VM&, JS::Handle<WebIDL::BufferSource>& bytes);
26
27
WebIDL::ExceptionOr<JS::Value> instantiate(JS::VM&, JS::Handle<WebIDL::BufferSource>& bytes, Optional<JS::Handle<JS::Object>>& import_object);
28
WebIDL::ExceptionOr<JS::Value> instantiate(JS::VM&, Module const& module_object, Optional<JS::Handle<JS::Object>>& import_object);
29
30
namespace Detail {
31
struct CompiledWebAssemblyModule : public RefCounted<CompiledWebAssemblyModule> {
32
    explicit CompiledWebAssemblyModule(NonnullRefPtr<Wasm::Module> module)
33
0
        : module(move(module))
34
0
    {
35
0
    }
36
37
    NonnullRefPtr<Wasm::Module> module;
38
};
39
40
class WebAssemblyCache {
41
public:
42
0
    void add_compiled_module(NonnullRefPtr<CompiledWebAssemblyModule> module) { m_compiled_modules.append(module); }
43
0
    void add_function_instance(Wasm::FunctionAddress address, JS::GCPtr<JS::NativeFunction> function) { m_function_instances.set(address, function); }
44
0
    void add_imported_object(JS::GCPtr<JS::Object> object) { m_imported_objects.set(object); }
45
46
0
    Optional<JS::GCPtr<JS::NativeFunction>> get_function_instance(Wasm::FunctionAddress address) { return m_function_instances.get(address).copy(); }
47
48
0
    HashMap<Wasm::FunctionAddress, JS::GCPtr<JS::NativeFunction>> function_instances() const { return m_function_instances; }
49
0
    HashTable<JS::GCPtr<JS::Object>> imported_objects() const { return m_imported_objects; }
50
0
    Wasm::AbstractMachine& abstract_machine() { return m_abstract_machine; }
51
52
private:
53
    HashMap<Wasm::FunctionAddress, JS::GCPtr<JS::NativeFunction>> m_function_instances;
54
    Vector<NonnullRefPtr<CompiledWebAssemblyModule>> m_compiled_modules;
55
    HashTable<JS::GCPtr<JS::Object>> m_imported_objects;
56
    Wasm::AbstractMachine m_abstract_machine;
57
};
58
59
WebAssemblyCache& get_cache(JS::Realm&);
60
61
JS::ThrowCompletionOr<NonnullOwnPtr<Wasm::ModuleInstance>> instantiate_module(JS::VM&, Wasm::Module const&);
62
JS::ThrowCompletionOr<NonnullRefPtr<CompiledWebAssemblyModule>> parse_module(JS::VM&, JS::Object* buffer);
63
JS::NativeFunction* create_native_function(JS::VM&, Wasm::FunctionAddress address, ByteString const& name, Instance* instance = nullptr);
64
JS::ThrowCompletionOr<Wasm::Value> to_webassembly_value(JS::VM&, JS::Value value, Wasm::ValueType const& type);
65
JS::Value to_js_value(JS::VM&, Wasm::Value& wasm_value, Wasm::ValueType type);
66
67
extern HashMap<JS::GCPtr<JS::Object>, WebAssemblyCache> s_caches;
68
69
}
70
71
}