Interpreter.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/MarkedValueList.h>
  20. #include <LibJS/Runtime/VM.h>
  21. #include <LibJS/Runtime/Value.h>
  22. namespace JS {
  23. struct ExecutingASTNodeChain {
  24. ExecutingASTNodeChain* previous { nullptr };
  25. const ASTNode& node;
  26. };
  27. class Interpreter : public Weakable<Interpreter> {
  28. public:
  29. template<typename GlobalObjectType, typename... Args>
  30. static NonnullOwnPtr<Interpreter> create(VM& vm, Args&&... args)
  31. {
  32. DeferGC defer_gc(vm.heap());
  33. auto interpreter = adopt_own(*new Interpreter(vm));
  34. VM::InterpreterExecutionScope scope(*interpreter);
  35. interpreter->m_global_object = make_handle(static_cast<Object*>(interpreter->heap().allocate_without_global_object<GlobalObjectType>(forward<Args>(args)...)));
  36. static_cast<GlobalObjectType*>(interpreter->m_global_object.cell())->initialize_global_object();
  37. return interpreter;
  38. }
  39. static NonnullOwnPtr<Interpreter> create_with_existing_global_object(GlobalObject&);
  40. ~Interpreter();
  41. void run(GlobalObject&, const Program&);
  42. GlobalObject& global_object();
  43. const GlobalObject& global_object() const;
  44. ALWAYS_INLINE VM& vm() { return *m_vm; }
  45. ALWAYS_INLINE const VM& vm() const { return *m_vm; }
  46. ALWAYS_INLINE Heap& heap() { return vm().heap(); }
  47. ALWAYS_INLINE Exception* exception() { return vm().exception(); }
  48. Environment* lexical_environment() { return vm().lexical_environment(); }
  49. FunctionEnvironment* current_function_environment();
  50. void enter_scope(const ScopeNode&, ScopeType, GlobalObject&);
  51. void exit_scope(const ScopeNode&);
  52. void push_ast_node(ExecutingASTNodeChain& chain_node)
  53. {
  54. chain_node.previous = m_ast_node_chain;
  55. m_ast_node_chain = &chain_node;
  56. }
  57. void pop_ast_node()
  58. {
  59. VERIFY(m_ast_node_chain);
  60. m_ast_node_chain = m_ast_node_chain->previous;
  61. }
  62. const ASTNode* current_node() const { return m_ast_node_chain ? &m_ast_node_chain->node : nullptr; }
  63. ExecutingASTNodeChain* executing_ast_node_chain() { return m_ast_node_chain; }
  64. const ExecutingASTNodeChain* executing_ast_node_chain() const { return m_ast_node_chain; }
  65. Value execute_statement(GlobalObject&, const Statement&, ScopeType = ScopeType::Block);
  66. private:
  67. explicit Interpreter(VM&);
  68. void push_scope(ScopeFrame frame);
  69. Vector<ScopeFrame> m_scope_stack;
  70. ExecutingASTNodeChain* m_ast_node_chain { nullptr };
  71. NonnullRefPtr<VM> m_vm;
  72. Handle<Object> m_global_object;
  73. };
  74. }