Interpreter.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/FlyString.h>
  9. #include <AK/HashMap.h>
  10. #include <AK/String.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) requires(IsBaseOf<GlobalObject, GlobalObjectType>)
  35. {
  36. DeferGC defer_gc(vm.heap());
  37. auto interpreter = adopt_own(*new Interpreter(vm));
  38. VM::InterpreterExecutionScope scope(*interpreter);
  39. Realm* realm { nullptr };
  40. interpreter->m_global_execution_context = MUST(Realm::initialize_host_defined_realm(
  41. vm,
  42. [&](Realm& realm_) -> GlobalObject* {
  43. realm = &realm_;
  44. return interpreter->heap().allocate_without_realm<GlobalObjectType>(realm_, forward<Args>(args)...);
  45. },
  46. nullptr));
  47. // NOTE: These are not in the spec.
  48. static FlyString global_execution_context_name = "(global execution context)";
  49. interpreter->m_global_execution_context->function_name = global_execution_context_name;
  50. interpreter->m_realm = make_handle(realm);
  51. return interpreter;
  52. }
  53. static NonnullOwnPtr<Interpreter> create_with_existing_realm(Realm&);
  54. ~Interpreter() = default;
  55. ThrowCompletionOr<Value> run(Script&);
  56. ThrowCompletionOr<Value> run(SourceTextModule&);
  57. Realm& realm();
  58. Realm const& realm() const;
  59. ALWAYS_INLINE VM& vm() { return *m_vm; }
  60. ALWAYS_INLINE const VM& vm() const { return *m_vm; }
  61. ALWAYS_INLINE Heap& heap() { return vm().heap(); }
  62. Environment* lexical_environment() { return vm().lexical_environment(); }
  63. void push_ast_node(ExecutingASTNodeChain& chain_node)
  64. {
  65. chain_node.previous = m_ast_node_chain;
  66. m_ast_node_chain = &chain_node;
  67. }
  68. void pop_ast_node()
  69. {
  70. VERIFY(m_ast_node_chain);
  71. m_ast_node_chain = m_ast_node_chain->previous;
  72. }
  73. ASTNode const* current_node() const { return m_ast_node_chain ? &m_ast_node_chain->node : nullptr; }
  74. private:
  75. explicit Interpreter(VM&);
  76. ExecutingASTNodeChain* m_ast_node_chain { nullptr };
  77. NonnullRefPtr<VM> m_vm;
  78. Handle<Realm> m_realm;
  79. // This is here to keep the global execution context alive for the entire lifespan of the Interpreter.
  80. OwnPtr<ExecutionContext> m_global_execution_context;
  81. };
  82. }