/* * Copyright (c) 2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include namespace JS::Bytecode { class InstructionStreamIterator; struct CallFrame { void visit_edges(Cell::Visitor& visitor) { for (auto const& value : registers) visitor.visit(value); for (auto const& environment : saved_lexical_environments) visitor.visit(environment); for (auto& context : unwind_contexts) { visitor.visit(context.lexical_environment); } } Vector registers; Vector> saved_lexical_environments; Vector unwind_contexts; }; class Interpreter { public: [[nodiscard]] static bool enabled(); static void set_enabled(bool); explicit Interpreter(VM&); ~Interpreter(); Realm& realm(); VM& vm() { return m_vm; } ThrowCompletionOr run(Script&, JS::GCPtr lexical_environment_override = nullptr); ThrowCompletionOr run(SourceTextModule&); ThrowCompletionOr run(Realm& realm, Bytecode::Executable& executable, Bytecode::BasicBlock const* entry_point = nullptr) { auto value_and_frame = run_and_return_frame(realm, executable, entry_point); return move(value_and_frame.value); } struct ValueAndFrame { ThrowCompletionOr value; OwnPtr frame; }; ValueAndFrame run_and_return_frame(Realm&, Bytecode::Executable&, Bytecode::BasicBlock const* entry_point, CallFrame* = nullptr); ALWAYS_INLINE Value& accumulator() { return reg(Register::accumulator()); } Value& reg(Register const& r) { return registers()[r.index()]; } auto& saved_lexical_environment_stack() { return call_frame().saved_lexical_environments; } auto& unwind_contexts() { return call_frame().unwind_contexts; } void jump(Label const& label) { m_pending_jump = &label.block(); } void schedule_jump(Label const& label) { m_scheduled_jump = &label.block(); VERIFY(unwind_contexts().last().finalizer); jump(Label { *unwind_contexts().last().finalizer }); } void do_return(Value return_value) { m_return_value = return_value; m_saved_exception = {}; } void enter_unwind_context(Optional