2021-06-03 10:46:30 +02:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
|
2021-06-03 10:46:30 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-10-30 22:06:27 +01:00
|
|
|
#include <LibJS/Bytecode/Executable.h>
|
2021-06-04 12:07:38 +02:00
|
|
|
#include <LibJS/Bytecode/Label.h>
|
2021-06-03 10:46:30 +02:00
|
|
|
#include <LibJS/Bytecode/Register.h>
|
|
|
|
#include <LibJS/Forward.h>
|
|
|
|
#include <LibJS/Heap/Cell.h>
|
2023-06-22 15:59:18 +02:00
|
|
|
#include <LibJS/Runtime/FunctionKind.h>
|
2022-02-11 22:38:21 +03:30
|
|
|
#include <LibJS/Runtime/VM.h>
|
2021-06-03 10:46:30 +02:00
|
|
|
#include <LibJS/Runtime/Value.h>
|
|
|
|
|
|
|
|
namespace JS::Bytecode {
|
|
|
|
|
2023-06-17 13:16:35 +02:00
|
|
|
class InstructionStreamIterator;
|
|
|
|
|
2021-06-03 10:46:30 +02:00
|
|
|
class Interpreter {
|
|
|
|
public:
|
2023-06-22 15:59:18 +02:00
|
|
|
explicit Interpreter(VM&);
|
2021-06-03 10:46:30 +02:00
|
|
|
~Interpreter();
|
|
|
|
|
2024-02-28 18:49:52 +01:00
|
|
|
[[nodiscard]] Realm& realm() { return *m_realm; }
|
|
|
|
[[nodiscard]] Object& global_object() { return *m_global_object; }
|
|
|
|
[[nodiscard]] DeclarativeEnvironment& global_declarative_environment() { return *m_global_declarative_environment; }
|
2021-06-03 18:26:13 +02:00
|
|
|
VM& vm() { return m_vm; }
|
2024-02-02 10:19:17 +01:00
|
|
|
VM const& vm() const { return m_vm; }
|
2021-06-03 10:46:30 +02:00
|
|
|
|
2024-10-31 08:03:09 -04:00
|
|
|
ThrowCompletionOr<Value> run(Script&, JS::GCPtr<Environment> lexical_environment_override = nullptr);
|
2023-06-15 12:36:57 +02:00
|
|
|
ThrowCompletionOr<Value> run(SourceTextModule&);
|
|
|
|
|
2024-05-06 06:44:08 +02:00
|
|
|
ThrowCompletionOr<Value> run(Bytecode::Executable& executable, Optional<size_t> entry_point = {}, Value initial_accumulator_value = {})
|
2021-11-11 00:34:44 +03:30
|
|
|
{
|
2024-05-06 06:44:08 +02:00
|
|
|
auto result_and_return_register = run_executable(executable, entry_point, initial_accumulator_value);
|
2024-05-01 19:33:49 +02:00
|
|
|
return move(result_and_return_register.value);
|
2021-11-11 00:34:44 +03:30
|
|
|
}
|
|
|
|
|
2024-05-01 19:33:49 +02:00
|
|
|
struct ResultAndReturnRegister {
|
2021-11-11 04:11:56 +03:30
|
|
|
ThrowCompletionOr<Value> value;
|
2024-05-01 19:33:49 +02:00
|
|
|
Value return_register_value;
|
2021-11-11 00:34:44 +03:30
|
|
|
};
|
2024-05-06 06:44:08 +02:00
|
|
|
ResultAndReturnRegister run_executable(Bytecode::Executable&, Optional<size_t> entry_point, Value initial_accumulator_value = {});
|
2021-06-03 10:46:30 +02:00
|
|
|
|
2021-06-07 20:58:36 -07:00
|
|
|
ALWAYS_INLINE Value& accumulator() { return reg(Register::accumulator()); }
|
2023-07-21 17:30:07 +02:00
|
|
|
ALWAYS_INLINE Value& saved_return_value() { return reg(Register::saved_return_value()); }
|
2024-05-01 19:33:49 +02:00
|
|
|
Value& reg(Register const& r)
|
|
|
|
{
|
2024-05-12 18:49:03 +02:00
|
|
|
return m_registers_and_constants_and_locals.data()[r.index()];
|
2024-05-01 19:33:49 +02:00
|
|
|
}
|
|
|
|
Value reg(Register const& r) const
|
|
|
|
{
|
2024-05-12 18:49:03 +02:00
|
|
|
return m_registers_and_constants_and_locals.data()[r.index()];
|
2024-05-01 19:33:49 +02:00
|
|
|
}
|
2024-02-02 10:19:17 +01:00
|
|
|
|
|
|
|
[[nodiscard]] Value get(Operand) const;
|
|
|
|
void set(Operand, Value);
|
2021-06-11 01:38:30 +04:30
|
|
|
|
2024-05-18 17:25:43 +02:00
|
|
|
Value do_yield(Value value, Optional<Label> continuation);
|
2023-09-26 15:32:46 +02:00
|
|
|
void do_return(Value value)
|
2022-12-25 17:15:29 +01:00
|
|
|
{
|
2023-09-26 15:32:46 +02:00
|
|
|
reg(Register::return_value()) = value;
|
2023-09-26 11:57:42 +02:00
|
|
|
reg(Register::exception()) = {};
|
2022-12-25 17:15:29 +01:00
|
|
|
}
|
2021-06-04 12:07:38 +02:00
|
|
|
|
2023-10-20 00:33:51 +02:00
|
|
|
void enter_unwind_context();
|
2021-06-10 15:04:38 +02:00
|
|
|
void leave_unwind_context();
|
2024-02-04 08:00:54 +01:00
|
|
|
void catch_exception(Operand dst);
|
2024-04-11 11:07:35 +02:00
|
|
|
void restore_scheduled_jump();
|
2024-04-11 11:58:18 +02:00
|
|
|
void leave_finally();
|
2021-06-10 15:04:38 +02:00
|
|
|
|
2023-11-12 00:12:21 +01:00
|
|
|
void enter_object_environment(Object&);
|
|
|
|
|
2023-07-08 17:43:26 +02:00
|
|
|
Executable& current_executable() { return *m_current_executable; }
|
|
|
|
Executable const& current_executable() const { return *m_current_executable; }
|
2024-05-06 06:44:08 +02:00
|
|
|
Optional<size_t> program_counter() const { return m_program_counter; }
|
2024-10-31 22:46:44 +01:00
|
|
|
Span<Value> allocate_argument_values(size_t argument_count)
|
|
|
|
{
|
|
|
|
m_argument_values_buffer.resize_and_keep_capacity(argument_count);
|
|
|
|
return m_argument_values_buffer.span();
|
|
|
|
}
|
2021-06-09 10:02:01 +02:00
|
|
|
|
2024-05-11 18:16:27 +02:00
|
|
|
ExecutionContext& running_execution_context() { return *m_running_execution_context; }
|
|
|
|
|
2021-06-03 10:46:30 +02:00
|
|
|
private:
|
2024-05-06 06:44:08 +02:00
|
|
|
void run_bytecode(size_t entry_point);
|
|
|
|
|
|
|
|
enum class HandleExceptionResponse {
|
|
|
|
ExitFromExecutable,
|
|
|
|
ContinueInThisExecutable,
|
|
|
|
};
|
|
|
|
[[nodiscard]] HandleExceptionResponse handle_exception(size_t& program_counter, Value exception);
|
2023-09-26 16:41:42 +02:00
|
|
|
|
2021-06-03 18:26:13 +02:00
|
|
|
VM& m_vm;
|
2024-05-06 06:44:08 +02:00
|
|
|
Optional<size_t> m_scheduled_jump;
|
2024-04-05 13:47:41 -07:00
|
|
|
GCPtr<Executable> m_current_executable { nullptr };
|
|
|
|
GCPtr<Realm> m_realm { nullptr };
|
|
|
|
GCPtr<Object> m_global_object { nullptr };
|
|
|
|
GCPtr<DeclarativeEnvironment> m_global_declarative_environment { nullptr };
|
2024-05-06 06:44:08 +02:00
|
|
|
Optional<size_t&> m_program_counter;
|
2024-05-05 22:06:55 +02:00
|
|
|
Span<Value> m_arguments;
|
2024-05-12 18:49:03 +02:00
|
|
|
Span<Value> m_registers_and_constants_and_locals;
|
2024-10-31 22:46:44 +01:00
|
|
|
Vector<Value> m_argument_values_buffer;
|
2024-05-11 18:16:27 +02:00
|
|
|
ExecutionContext* m_running_execution_context { nullptr };
|
2021-06-03 10:46:30 +02:00
|
|
|
};
|
|
|
|
|
2021-10-24 13:34:46 +02:00
|
|
|
extern bool g_dump_bytecode;
|
|
|
|
|
2024-05-05 22:06:55 +02:00
|
|
|
ThrowCompletionOr<NonnullGCPtr<Bytecode::Executable>> compile(VM&, ASTNode const&, JS::FunctionKind kind, DeprecatedFlyString const& name);
|
|
|
|
ThrowCompletionOr<NonnullGCPtr<Bytecode::Executable>> compile(VM&, ECMAScriptFunctionObject const&);
|
2023-06-22 15:59:18 +02:00
|
|
|
|
2021-06-03 10:46:30 +02:00
|
|
|
}
|