Interpreter.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/DeprecatedString.h>
  9. #include <AK/FlyString.h>
  10. #include <AK/HashMap.h>
  11. #include <AK/Weakable.h>
  12. #include <LibJS/Forward.h>
  13. #include <LibJS/Heap/DeferGC.h>
  14. #include <LibJS/Heap/Heap.h>
  15. #include <LibJS/Heap/MarkedVector.h>
  16. #include <LibJS/Runtime/Completion.h>
  17. #include <LibJS/Runtime/DeclarativeEnvironment.h>
  18. #include <LibJS/Runtime/ErrorTypes.h>
  19. #include <LibJS/Runtime/GlobalEnvironment.h>
  20. #include <LibJS/Runtime/GlobalObject.h>
  21. #include <LibJS/Runtime/Realm.h>
  22. #include <LibJS/Runtime/VM.h>
  23. #include <LibJS/Runtime/Value.h>
  24. #include <LibJS/Script.h>
  25. #include <LibJS/SourceTextModule.h>
  26. namespace JS {
  27. struct ExecutingASTNodeChain {
  28. ExecutingASTNodeChain* previous { nullptr };
  29. ASTNode const& node;
  30. };
  31. class Interpreter : public Weakable<Interpreter> {
  32. public:
  33. template<typename GlobalObjectType, typename... Args>
  34. static NonnullOwnPtr<Interpreter> create(VM& vm, Args&&... args)
  35. requires(IsBaseOf<GlobalObject, GlobalObjectType>)
  36. {
  37. DeferGC defer_gc(vm.heap());
  38. auto interpreter = adopt_own(*new Interpreter(vm));
  39. VM::InterpreterExecutionScope scope(*interpreter);
  40. Realm* realm { nullptr };
  41. interpreter->m_global_execution_context = MUST(Realm::initialize_host_defined_realm(
  42. vm,
  43. [&](Realm& realm_) -> GlobalObject* {
  44. realm = &realm_;
  45. return interpreter->heap().allocate_without_realm<GlobalObjectType>(realm_, forward<Args>(args)...);
  46. },
  47. nullptr));
  48. // NOTE: These are not in the spec.
  49. static FlyString global_execution_context_name = "(global execution context)";
  50. interpreter->m_global_execution_context->function_name = global_execution_context_name;
  51. interpreter->m_realm = make_handle(realm);
  52. return interpreter;
  53. }
  54. static NonnullOwnPtr<Interpreter> create_with_existing_realm(Realm&);
  55. ~Interpreter() = default;
  56. ThrowCompletionOr<Value> run(Script&, JS::GCPtr<Environment> lexical_environment_override = {});
  57. ThrowCompletionOr<Value> run(SourceTextModule&);
  58. Realm& realm();
  59. Realm const& realm() const;
  60. ALWAYS_INLINE VM& vm() { return *m_vm; }
  61. ALWAYS_INLINE const VM& vm() const { return *m_vm; }
  62. ALWAYS_INLINE Heap& heap() { return vm().heap(); }
  63. Environment* lexical_environment() { return vm().lexical_environment(); }
  64. void push_ast_node(ExecutingASTNodeChain& chain_node)
  65. {
  66. chain_node.previous = m_ast_node_chain;
  67. m_ast_node_chain = &chain_node;
  68. }
  69. void pop_ast_node()
  70. {
  71. VERIFY(m_ast_node_chain);
  72. m_ast_node_chain = m_ast_node_chain->previous;
  73. }
  74. ASTNode const* current_node() const { return m_ast_node_chain ? &m_ast_node_chain->node : nullptr; }
  75. private:
  76. explicit Interpreter(VM&);
  77. ExecutingASTNodeChain* m_ast_node_chain { nullptr };
  78. NonnullRefPtr<VM> m_vm;
  79. Handle<Realm> m_realm;
  80. // This is here to keep the global execution context alive for the entire lifespan of the Interpreter.
  81. OwnPtr<ExecutionContext> m_global_execution_context;
  82. };
  83. }