Interpreter.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/FlyString.h>
  9. #include <AK/HashMap.h>
  10. #include <AK/String.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/Heap/MarkedVector.h>
  17. #include <LibJS/Runtime/Completion.h>
  18. #include <LibJS/Runtime/DeclarativeEnvironment.h>
  19. #include <LibJS/Runtime/ErrorTypes.h>
  20. #include <LibJS/Runtime/GlobalEnvironment.h>
  21. #include <LibJS/Runtime/GlobalObject.h>
  22. #include <LibJS/Runtime/Realm.h>
  23. #include <LibJS/Runtime/VM.h>
  24. #include <LibJS/Runtime/Value.h>
  25. #include <LibJS/Script.h>
  26. #include <LibJS/SourceTextModule.h>
  27. namespace JS {
  28. struct ExecutingASTNodeChain {
  29. ExecutingASTNodeChain* previous { nullptr };
  30. ASTNode const& node;
  31. };
  32. class Interpreter : public Weakable<Interpreter> {
  33. public:
  34. // 9.6 InitializeHostDefinedRealm ( ), https://tc39.es/ecma262/#sec-initializehostdefinedrealm
  35. template<typename GlobalObjectType, typename GlobalThisObjectType, typename... Args>
  36. static NonnullOwnPtr<Interpreter> create(VM& vm, Args&&... args) requires(IsBaseOf<GlobalObject, GlobalObjectType>&& IsBaseOf<Object, GlobalThisObjectType>)
  37. {
  38. DeferGC defer_gc(vm.heap());
  39. auto interpreter = adopt_own(*new Interpreter(vm));
  40. VM::InterpreterExecutionScope scope(*interpreter);
  41. // 1. Let realm be CreateRealm().
  42. auto* realm = Realm::create(vm);
  43. // 2. Let newContext be a new execution context.
  44. auto& new_context = interpreter->m_global_execution_context;
  45. // 3. Set the Function of newContext to null.
  46. // NOTE: This was done during execution context construction.
  47. // 4. Set the Realm of newContext to realm.
  48. new_context.realm = realm;
  49. // 5. Set the ScriptOrModule of newContext to null.
  50. // NOTE: This was done during execution context construction.
  51. // 6. Push newContext onto the execution context stack; newContext is now the running execution context.
  52. vm.push_execution_context(new_context);
  53. // 7. If the host requires use of an exotic object to serve as realm's global object, let global be such an object created in a host-defined manner.
  54. // Otherwise, let global be undefined, indicating that an ordinary object should be created as the global object.
  55. auto* global_object = static_cast<GlobalObject*>(interpreter->heap().allocate_without_global_object<GlobalObjectType>(forward<Args>(args)...));
  56. // 8. If the host requires that the this binding in realm's global scope return an object other than the global object, let thisValue be such an object created
  57. // in a host-defined manner. Otherwise, let thisValue be undefined, indicating that realm's global this binding should be the global object.
  58. Object* this_value;
  59. if constexpr (IsSame<GlobalObjectType, GlobalThisObjectType>) {
  60. this_value = global_object;
  61. } else {
  62. // FIXME: Should we pass args in here? Let's er on the side of caution and say yes.
  63. this_value = static_cast<Object*>(interpreter->heap().allocate_without_global_object<GlobalThisObjectType>(forward<Args>(args)...));
  64. }
  65. // 9. Perform SetRealmGlobalObject(realm, global, thisValue).
  66. realm->set_global_object(*global_object, this_value);
  67. // NOTE: These are not in the spec.
  68. static FlyString global_execution_context_name = "(global execution context)";
  69. interpreter->m_global_execution_context.function_name = global_execution_context_name;
  70. interpreter->m_global_object = make_handle(global_object);
  71. interpreter->m_realm = make_handle(realm);
  72. // 10. Let globalObj be ? SetDefaultGlobalBindings(realm).
  73. // 11. Create any host-defined global object properties on globalObj.
  74. static_cast<GlobalObjectType*>(global_object)->initialize_global_object();
  75. // 12. Return unused.
  76. return interpreter;
  77. }
  78. template<typename GlobalObjectType, typename... Args>
  79. static NonnullOwnPtr<Interpreter> create(VM& vm, Args&&... args) requires IsBaseOf<GlobalObject, GlobalObjectType>
  80. {
  81. // NOTE: This function is here to facilitate step 8 of InitializeHostDefinedRealm. (Callers don't have to specify the same type twice if not necessary)
  82. return create<GlobalObjectType, GlobalObjectType>(vm, args...);
  83. }
  84. static NonnullOwnPtr<Interpreter> create_with_existing_realm(Realm&);
  85. ~Interpreter() = default;
  86. ThrowCompletionOr<Value> run(Script&);
  87. ThrowCompletionOr<Value> run(SourceTextModule&);
  88. GlobalObject& global_object();
  89. GlobalObject const& global_object() const;
  90. Realm& realm();
  91. Realm const& realm() const;
  92. ALWAYS_INLINE VM& vm() { return *m_vm; }
  93. ALWAYS_INLINE const VM& vm() const { return *m_vm; }
  94. ALWAYS_INLINE Heap& heap() { return vm().heap(); }
  95. Environment* lexical_environment() { return vm().lexical_environment(); }
  96. void push_ast_node(ExecutingASTNodeChain& chain_node)
  97. {
  98. chain_node.previous = m_ast_node_chain;
  99. m_ast_node_chain = &chain_node;
  100. }
  101. void pop_ast_node()
  102. {
  103. VERIFY(m_ast_node_chain);
  104. m_ast_node_chain = m_ast_node_chain->previous;
  105. }
  106. ASTNode const* current_node() const { return m_ast_node_chain ? &m_ast_node_chain->node : nullptr; }
  107. private:
  108. explicit Interpreter(VM&);
  109. ExecutingASTNodeChain* m_ast_node_chain { nullptr };
  110. NonnullRefPtr<VM> m_vm;
  111. Handle<GlobalObject> m_global_object;
  112. Handle<Realm> m_realm;
  113. // This is here to keep the global execution context alive for the entire lifespan of the Interpreter.
  114. ExecutionContext m_global_execution_context;
  115. };
  116. }