/* * Copyright (c) 2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include "Generator.h" #include "PassManager.h" #include #include #include #include #include #include #include namespace JS::Bytecode { struct RegisterWindow { MarkedVector registers; MarkedVector saved_lexical_environments; MarkedVector saved_variable_environments; }; class Interpreter { public: explicit Interpreter(Realm&); ~Interpreter(); // FIXME: Remove this thing once we don't need it anymore! static Interpreter* current(); Realm& realm() { return m_realm; } VM& vm() { return m_vm; } ThrowCompletionOr run(Bytecode::Executable const& executable, Bytecode::BasicBlock const* entry_point = nullptr) { auto value_and_frame = run_and_return_frame(executable, entry_point); return move(value_and_frame.value); } struct ValueAndFrame { ThrowCompletionOr value; OwnPtr frame; }; ValueAndFrame run_and_return_frame(Bytecode::Executable const&, Bytecode::BasicBlock const* entry_point, RegisterWindow* = nullptr); ALWAYS_INLINE Value& accumulator() { return reg(Register::accumulator()); } Value& reg(Register const& r) { return registers()[r.index()]; } auto& saved_lexical_environment_stack() { return window().saved_lexical_environments; } auto& saved_variable_environment_stack() { return window().saved_variable_environments; } 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