Interpreter.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/FlyString.h>
  8. #include <AK/HashMap.h>
  9. #include <AK/String.h>
  10. #include <AK/Vector.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/Runtime/DeclarativeEnvironment.h>
  17. #include <LibJS/Runtime/ErrorTypes.h>
  18. #include <LibJS/Runtime/Exception.h>
  19. #include <LibJS/Runtime/GlobalObject.h>
  20. #include <LibJS/Runtime/MarkedValueList.h>
  21. #include <LibJS/Runtime/Realm.h>
  22. #include <LibJS/Runtime/VM.h>
  23. #include <LibJS/Runtime/Value.h>
  24. namespace JS {
  25. struct ExecutingASTNodeChain {
  26. ExecutingASTNodeChain* previous { nullptr };
  27. const ASTNode& node;
  28. };
  29. class Interpreter : public Weakable<Interpreter> {
  30. public:
  31. template<typename GlobalObjectType, typename... Args>
  32. static NonnullOwnPtr<Interpreter> create(VM& vm, Args&&... args)
  33. {
  34. DeferGC defer_gc(vm.heap());
  35. auto interpreter = adopt_own(*new Interpreter(vm));
  36. VM::InterpreterExecutionScope scope(*interpreter);
  37. auto* global_object = static_cast<GlobalObject*>(interpreter->heap().allocate_without_global_object<GlobalObjectType>(forward<Args>(args)...));
  38. auto* realm = Realm::create(vm);
  39. realm->set_global_object(*global_object, global_object);
  40. interpreter->m_global_object = make_handle(global_object);
  41. interpreter->m_realm = make_handle(realm);
  42. static_cast<GlobalObjectType*>(global_object)->initialize_global_object();
  43. return interpreter;
  44. }
  45. static NonnullOwnPtr<Interpreter> create_with_existing_realm(Realm&);
  46. ~Interpreter();
  47. void run(GlobalObject&, const Program&);
  48. GlobalObject& global_object();
  49. const GlobalObject& global_object() const;
  50. Realm& realm();
  51. Realm const& realm() const;
  52. ALWAYS_INLINE VM& vm() { return *m_vm; }
  53. ALWAYS_INLINE const VM& vm() const { return *m_vm; }
  54. ALWAYS_INLINE Heap& heap() { return vm().heap(); }
  55. ALWAYS_INLINE Exception* exception() { return vm().exception(); }
  56. Environment* lexical_environment() { return vm().lexical_environment(); }
  57. FunctionEnvironment* current_function_environment();
  58. void enter_scope(const ScopeNode&, ScopeType, GlobalObject&);
  59. void exit_scope(const ScopeNode&);
  60. void push_ast_node(ExecutingASTNodeChain& chain_node)
  61. {
  62. chain_node.previous = m_ast_node_chain;
  63. m_ast_node_chain = &chain_node;
  64. }
  65. void pop_ast_node()
  66. {
  67. VERIFY(m_ast_node_chain);
  68. m_ast_node_chain = m_ast_node_chain->previous;
  69. }
  70. const ASTNode* current_node() const { return m_ast_node_chain ? &m_ast_node_chain->node : nullptr; }
  71. ExecutingASTNodeChain* executing_ast_node_chain() { return m_ast_node_chain; }
  72. const ExecutingASTNodeChain* executing_ast_node_chain() const { return m_ast_node_chain; }
  73. Value execute_statement(GlobalObject&, const Statement&, ScopeType = ScopeType::Block);
  74. private:
  75. explicit Interpreter(VM&);
  76. void push_scope(ScopeFrame frame);
  77. Vector<ScopeFrame> m_scope_stack;
  78. ExecutingASTNodeChain* m_ast_node_chain { nullptr };
  79. NonnullRefPtr<VM> m_vm;
  80. Handle<GlobalObject> m_global_object;
  81. Handle<Realm> m_realm;
  82. };
  83. }