summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Bytecode/Interpreter.h
blob: 26b2dc9078c0d3f5d28dfd8e2dd5cf8637af1605 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include "Generator.h"
#include <LibJS/Bytecode/Label.h>
#include <LibJS/Bytecode/Register.h>
#include <LibJS/Forward.h>
#include <LibJS/Heap/Cell.h>
#include <LibJS/Heap/Handle.h>
#include <LibJS/Runtime/Exception.h>
#include <LibJS/Runtime/Value.h>

namespace JS::Bytecode {

using RegisterWindow = Vector<Value>;

class Interpreter {
public:
    explicit Interpreter(GlobalObject&);
    ~Interpreter();

    // FIXME: Remove this thing once we don't need it anymore!
    static Interpreter* current();

    GlobalObject& global_object() { return m_global_object; }
    VM& vm() { return m_vm; }

    Value run(Bytecode::Executable const&, Bytecode::BasicBlock const* entry_point = nullptr);

    ALWAYS_INLINE Value& accumulator() { return reg(Register::accumulator()); }
    Value& reg(Register const& r) { return registers()[r.index()]; }
    [[nodiscard]] RegisterWindow snapshot_frame() const { return m_register_windows.last(); }

    void enter_frame(RegisterWindow const& frame)
    {
        ++m_manually_entered_frames;
        m_register_windows.append(make<RegisterWindow>(frame));
    }
    void leave_frame()
    {
        VERIFY(m_manually_entered_frames);
        --m_manually_entered_frames;
        m_register_windows.take_last();
    }

    void jump(Label const& label)
    {
        m_pending_jump = &label.block();
    }
    void do_return(Value return_value) { m_return_value = return_value; }

    void enter_unwind_context(Optional<Label> handler_target, Optional<Label> finalizer_target);
    void leave_unwind_context();
    void continue_pending_unwind(Label const& resume_label);

    Executable const& current_executable() { return *m_current_executable; }

private:
    RegisterWindow& registers() { return m_register_windows.last(); }

    VM& m_vm;
    GlobalObject& m_global_object;
    NonnullOwnPtrVector<RegisterWindow> m_register_windows;
    Optional<BasicBlock const*> m_pending_jump;
    Value m_return_value;
    size_t m_manually_entered_frames { 0 };
    Executable const* m_current_executable { nullptr };
    Vector<UnwindInfo> m_unwind_contexts;
    Handle<Exception> m_saved_exception;
};

}