Interpreter.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/FlyString.h>
  28. #include <AK/HashMap.h>
  29. #include <AK/String.h>
  30. #include <AK/Vector.h>
  31. #include <AK/Weakable.h>
  32. #include <LibJS/AST.h>
  33. #include <LibJS/Forward.h>
  34. #include <LibJS/Heap/DeferGC.h>
  35. #include <LibJS/Heap/Heap.h>
  36. #include <LibJS/Runtime/ErrorTypes.h>
  37. #include <LibJS/Runtime/Exception.h>
  38. #include <LibJS/Runtime/LexicalEnvironment.h>
  39. #include <LibJS/Runtime/MarkedValueList.h>
  40. #include <LibJS/Runtime/VM.h>
  41. #include <LibJS/Runtime/Value.h>
  42. namespace JS {
  43. class Interpreter : public Weakable<Interpreter> {
  44. public:
  45. template<typename GlobalObjectType, typename... Args>
  46. static NonnullOwnPtr<Interpreter> create(VM& vm, Args&&... args)
  47. {
  48. DeferGC defer_gc(vm.heap());
  49. auto interpreter = adopt_own(*new Interpreter(vm));
  50. VM::InterpreterExecutionScope scope(*interpreter);
  51. interpreter->m_global_object = make_handle(static_cast<Object*>(interpreter->heap().allocate_without_global_object<GlobalObjectType>(forward<Args>(args)...)));
  52. static_cast<GlobalObjectType*>(interpreter->m_global_object.cell())->initialize_global_object();
  53. return interpreter;
  54. }
  55. static NonnullOwnPtr<Interpreter> create_with_existing_global_object(GlobalObject&);
  56. ~Interpreter();
  57. void run(GlobalObject&, const Program&);
  58. GlobalObject& global_object();
  59. const GlobalObject& global_object() const;
  60. VM& vm() { return *m_vm; }
  61. const VM& vm() const { return *m_vm; }
  62. Heap& heap() { return vm().heap(); }
  63. Exception* exception() { return vm().exception(); }
  64. ScopeObject* current_scope() { return vm().current_scope(); }
  65. LexicalEnvironment* current_environment();
  66. void enter_scope(const ScopeNode&, ScopeType, GlobalObject&);
  67. void exit_scope(const ScopeNode&);
  68. void enter_node(const ASTNode&);
  69. void exit_node(const ASTNode&);
  70. Value execute_statement(GlobalObject&, const Statement&, ScopeType = ScopeType::Block);
  71. private:
  72. explicit Interpreter(VM&);
  73. void push_scope(ScopeFrame frame);
  74. Vector<ScopeFrame> m_scope_stack;
  75. NonnullRefPtr<VM> m_vm;
  76. Handle<Object> m_global_object;
  77. };
  78. }