Interpreter.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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();
  53. return interpreter;
  54. }
  55. static NonnullOwnPtr<Interpreter> create_with_existing_global_object(GlobalObject&);
  56. template<typename... Args>
  57. [[nodiscard]] ALWAYS_INLINE Value call(Function& function, Value this_value, Args... args)
  58. {
  59. // Are there any values in this argpack?
  60. // args = [] -> if constexpr (false)
  61. // args = [x, y, z] -> if constexpr ((void)x, true || ...)
  62. if constexpr ((((void)args, true) || ...)) {
  63. MarkedValueList arglist { heap() };
  64. (..., arglist.append(move(args)));
  65. return call(function, this_value, move(arglist));
  66. }
  67. return call(function, this_value);
  68. }
  69. ~Interpreter();
  70. Value run(GlobalObject&, const Program&);
  71. GlobalObject& global_object();
  72. const GlobalObject& global_object() const;
  73. VM& vm() { return *m_vm; }
  74. const VM& vm() const { return *m_vm; }
  75. Heap& heap() { return vm().heap(); }
  76. Exception* exception() { return vm().exception(); }
  77. size_t argument_count() const { return vm().argument_count(); }
  78. Value argument(size_t index) const { return vm().argument(index); }
  79. Value this_value(Object& global_object) const { return vm().this_value(global_object); }
  80. LexicalEnvironment* current_environment() { return vm().current_environment(); }
  81. const CallFrame& call_frame() { return vm().call_frame(); }
  82. void enter_scope(const ScopeNode&, ArgumentVector, ScopeType, GlobalObject&);
  83. void exit_scope(const ScopeNode&);
  84. Value execute_statement(GlobalObject&, const Statement&, ArgumentVector = {}, ScopeType = ScopeType::Block);
  85. private:
  86. explicit Interpreter(VM&);
  87. [[nodiscard]] Value call_internal(Function& function, Value this_value, Optional<MarkedValueList> arguments)
  88. {
  89. return vm().call(function, this_value, move(arguments));
  90. }
  91. void push_scope(ScopeFrame frame);
  92. Vector<ScopeFrame> m_scope_stack;
  93. NonnullRefPtr<VM> m_vm;
  94. Handle<Object> m_global_object;
  95. };
  96. template<>
  97. [[nodiscard]] ALWAYS_INLINE Value Interpreter::call(Function& function, Value this_value, MarkedValueList arguments) { return call_internal(function, this_value, move(arguments)); }
  98. template<>
  99. [[nodiscard]] ALWAYS_INLINE Value Interpreter::call(Function& function, Value this_value, Optional<MarkedValueList> arguments) { return call_internal(function, this_value, move(arguments)); }
  100. template<>
  101. [[nodiscard]] ALWAYS_INLINE Value Interpreter::call(Function& function, Value this_value) { return call(function, this_value, Optional<MarkedValueList> {}); }
  102. }