Interpreter.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibJS/Bytecode/Executable.h>
  8. #include <LibJS/Bytecode/Label.h>
  9. #include <LibJS/Bytecode/Register.h>
  10. #include <LibJS/Forward.h>
  11. #include <LibJS/Heap/Cell.h>
  12. #include <LibJS/Runtime/FunctionKind.h>
  13. #include <LibJS/Runtime/VM.h>
  14. #include <LibJS/Runtime/Value.h>
  15. namespace JS::Bytecode {
  16. class InstructionStreamIterator;
  17. class Interpreter {
  18. public:
  19. explicit Interpreter(VM&);
  20. ~Interpreter();
  21. [[nodiscard]] Realm& realm() { return *m_realm; }
  22. [[nodiscard]] Object& global_object() { return *m_global_object; }
  23. [[nodiscard]] DeclarativeEnvironment& global_declarative_environment() { return *m_global_declarative_environment; }
  24. VM& vm() { return m_vm; }
  25. VM const& vm() const { return m_vm; }
  26. ThrowCompletionOr<Value> run(Script&, JS::GCPtr<Environment> lexical_environment_override = nullptr);
  27. ThrowCompletionOr<Value> run(SourceTextModule&);
  28. ThrowCompletionOr<Value> run(Bytecode::Executable& executable, Optional<size_t> entry_point = {}, Value initial_accumulator_value = {})
  29. {
  30. auto result_and_return_register = run_executable(executable, entry_point, initial_accumulator_value);
  31. return move(result_and_return_register.value);
  32. }
  33. struct ResultAndReturnRegister {
  34. ThrowCompletionOr<Value> value;
  35. Value return_register_value;
  36. };
  37. ResultAndReturnRegister run_executable(Bytecode::Executable&, Optional<size_t> entry_point, Value initial_accumulator_value = {});
  38. ALWAYS_INLINE Value& accumulator() { return reg(Register::accumulator()); }
  39. ALWAYS_INLINE Value& saved_return_value() { return reg(Register::saved_return_value()); }
  40. Value& reg(Register const& r)
  41. {
  42. return m_registers_and_constants_and_locals.data()[r.index()];
  43. }
  44. Value reg(Register const& r) const
  45. {
  46. return m_registers_and_constants_and_locals.data()[r.index()];
  47. }
  48. [[nodiscard]] Value get(Operand) const;
  49. void set(Operand, Value);
  50. Value do_yield(Value value, Optional<Label> continuation);
  51. void do_return(Value value)
  52. {
  53. reg(Register::return_value()) = value;
  54. reg(Register::exception()) = {};
  55. }
  56. void enter_unwind_context();
  57. void leave_unwind_context();
  58. void catch_exception(Operand dst);
  59. void restore_scheduled_jump();
  60. void leave_finally();
  61. void enter_object_environment(Object&);
  62. Executable& current_executable() { return *m_current_executable; }
  63. Executable const& current_executable() const { return *m_current_executable; }
  64. Optional<size_t> program_counter() const { return m_program_counter; }
  65. Span<Value> allocate_argument_values(size_t argument_count)
  66. {
  67. m_argument_values_buffer.resize_and_keep_capacity(argument_count);
  68. return m_argument_values_buffer.span();
  69. }
  70. ExecutionContext& running_execution_context() { return *m_running_execution_context; }
  71. private:
  72. void run_bytecode(size_t entry_point);
  73. enum class HandleExceptionResponse {
  74. ExitFromExecutable,
  75. ContinueInThisExecutable,
  76. };
  77. [[nodiscard]] HandleExceptionResponse handle_exception(size_t& program_counter, Value exception);
  78. VM& m_vm;
  79. Optional<size_t> m_scheduled_jump;
  80. GCPtr<Executable> m_current_executable { nullptr };
  81. GCPtr<Realm> m_realm { nullptr };
  82. GCPtr<Object> m_global_object { nullptr };
  83. GCPtr<DeclarativeEnvironment> m_global_declarative_environment { nullptr };
  84. Optional<size_t&> m_program_counter;
  85. Span<Value> m_arguments;
  86. Span<Value> m_registers_and_constants_and_locals;
  87. Vector<Value> m_argument_values_buffer;
  88. ExecutionContext* m_running_execution_context { nullptr };
  89. };
  90. extern bool g_dump_bytecode;
  91. ThrowCompletionOr<NonnullGCPtr<Bytecode::Executable>> compile(VM&, ASTNode const&, JS::FunctionKind kind, DeprecatedFlyString const& name);
  92. ThrowCompletionOr<NonnullGCPtr<Bytecode::Executable>> compile(VM&, ECMAScriptFunctionObject const&);
  93. }