Interpreter.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.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.data()[r.index()];
  43. }
  44. Value reg(Register const& r) const
  45. {
  46. return m_registers.data()[r.index()];
  47. }
  48. [[nodiscard]] Value get(Operand) const;
  49. void set(Operand, Value);
  50. void do_return(Value value)
  51. {
  52. reg(Register::return_value()) = value;
  53. reg(Register::exception()) = {};
  54. }
  55. void enter_unwind_context();
  56. void leave_unwind_context();
  57. void catch_exception(Operand dst);
  58. void restore_scheduled_jump();
  59. void leave_finally();
  60. void enter_object_environment(Object&);
  61. Executable& current_executable() { return *m_current_executable; }
  62. Executable const& current_executable() const { return *m_current_executable; }
  63. Optional<size_t> program_counter() const { return m_program_counter; }
  64. Vector<Value>& registers() { return vm().running_execution_context().registers; }
  65. Vector<Value> const& registers() const { return vm().running_execution_context().registers; }
  66. private:
  67. void run_bytecode(size_t entry_point);
  68. enum class HandleExceptionResponse {
  69. ExitFromExecutable,
  70. ContinueInThisExecutable,
  71. };
  72. [[nodiscard]] HandleExceptionResponse handle_exception(size_t& program_counter, Value exception);
  73. VM& m_vm;
  74. Optional<size_t> m_scheduled_jump;
  75. GCPtr<Executable> m_current_executable { nullptr };
  76. GCPtr<Realm> m_realm { nullptr };
  77. GCPtr<Object> m_global_object { nullptr };
  78. GCPtr<DeclarativeEnvironment> m_global_declarative_environment { nullptr };
  79. Optional<size_t&> m_program_counter;
  80. Span<Value> m_arguments;
  81. Span<Value> m_registers;
  82. Span<Value> m_locals;
  83. Span<Value> m_constants;
  84. };
  85. extern bool g_dump_bytecode;
  86. ThrowCompletionOr<NonnullGCPtr<Bytecode::Executable>> compile(VM&, ASTNode const&, JS::FunctionKind kind, DeprecatedFlyString const& name);
  87. ThrowCompletionOr<NonnullGCPtr<Bytecode::Executable>> compile(VM&, ECMAScriptFunctionObject const&);
  88. }