Interpreter.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. void push_ast_node(ExecutingASTNodeChain& chain_node)
  58. {
  59. chain_node.previous = m_ast_node_chain;
  60. m_ast_node_chain = &chain_node;
  61. }
  62. void pop_ast_node()
  63. {
  64. VERIFY(m_ast_node_chain);
  65. m_ast_node_chain = m_ast_node_chain->previous;
  66. }
  67. const ASTNode* current_node() const { return m_ast_node_chain ? &m_ast_node_chain->node : nullptr; }
  68. private:
  69. explicit Interpreter(VM&);
  70. ExecutingASTNodeChain* m_ast_node_chain { nullptr };
  71. NonnullRefPtr<VM> m_vm;
  72. Handle<GlobalObject> m_global_object;
  73. Handle<Realm> m_realm;
  74. };
  75. }