Interpreter.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/AST.h>
  13. #include <LibJS/Forward.h>
  14. #include <LibJS/Heap/DeferGC.h>
  15. #include <LibJS/Heap/Heap.h>
  16. #include <LibJS/Heap/MarkedVector.h>
  17. #include <LibJS/Runtime/Completion.h>
  18. #include <LibJS/Runtime/DeclarativeEnvironment.h>
  19. #include <LibJS/Runtime/ErrorTypes.h>
  20. #include <LibJS/Runtime/GlobalEnvironment.h>
  21. #include <LibJS/Runtime/GlobalObject.h>
  22. #include <LibJS/Runtime/Realm.h>
  23. #include <LibJS/Runtime/VM.h>
  24. #include <LibJS/Runtime/Value.h>
  25. #include <LibJS/Script.h>
  26. #include <LibJS/SourceTextModule.h>
  27. namespace JS {
  28. struct ExecutingASTNodeChain {
  29. ExecutingASTNodeChain* previous { nullptr };
  30. ASTNode const& node;
  31. };
  32. class Interpreter : public Weakable<Interpreter> {
  33. public:
  34. template<typename GlobalObjectType, typename... Args>
  35. static NonnullOwnPtr<Interpreter> create(VM& vm, Args&&... args) 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. GlobalObject* global_object { nullptr };
  41. interpreter->m_global_execution_context = MUST(Realm::initialize_host_defined_realm(
  42. vm,
  43. [&](Realm& realm) -> Value {
  44. global_object = interpreter->heap().allocate_without_global_object<GlobalObjectType>(realm, forward<Args>(args)...);
  45. return global_object;
  46. },
  47. [](Realm&) -> Value {
  48. return js_undefined();
  49. }));
  50. // NOTE: These are not in the spec.
  51. static FlyString global_execution_context_name = "(global execution context)";
  52. interpreter->m_global_execution_context->function_name = global_execution_context_name;
  53. interpreter->m_global_object = make_handle(global_object);
  54. interpreter->m_realm = make_handle(global_object->associated_realm());
  55. return interpreter;
  56. }
  57. static NonnullOwnPtr<Interpreter> create_with_existing_realm(Realm&);
  58. ~Interpreter() = default;
  59. ThrowCompletionOr<Value> run(Script&);
  60. ThrowCompletionOr<Value> run(SourceTextModule&);
  61. GlobalObject& global_object();
  62. GlobalObject const& global_object() const;
  63. Realm& realm();
  64. Realm const& realm() const;
  65. ALWAYS_INLINE VM& vm() { return *m_vm; }
  66. ALWAYS_INLINE const VM& vm() const { return *m_vm; }
  67. ALWAYS_INLINE Heap& heap() { return vm().heap(); }
  68. Environment* lexical_environment() { return vm().lexical_environment(); }
  69. void push_ast_node(ExecutingASTNodeChain& chain_node)
  70. {
  71. chain_node.previous = m_ast_node_chain;
  72. m_ast_node_chain = &chain_node;
  73. }
  74. void pop_ast_node()
  75. {
  76. VERIFY(m_ast_node_chain);
  77. m_ast_node_chain = m_ast_node_chain->previous;
  78. }
  79. ASTNode const* current_node() const { return m_ast_node_chain ? &m_ast_node_chain->node : nullptr; }
  80. private:
  81. explicit Interpreter(VM&);
  82. ExecutingASTNodeChain* m_ast_node_chain { nullptr };
  83. NonnullRefPtr<VM> m_vm;
  84. Handle<GlobalObject> m_global_object;
  85. Handle<Realm> m_realm;
  86. // This is here to keep the global execution context alive for the entire lifespan of the Interpreter.
  87. OwnPtr<ExecutionContext> m_global_execution_context;
  88. };
  89. }