Interpreter.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/Vector.h>
  12. #include <AK/Weakable.h>
  13. #include <LibJS/AST.h>
  14. #include <LibJS/Forward.h>
  15. #include <LibJS/Heap/DeferGC.h>
  16. #include <LibJS/Heap/Heap.h>
  17. #include <LibJS/Runtime/Completion.h>
  18. #include <LibJS/Runtime/DeclarativeEnvironment.h>
  19. #include <LibJS/Runtime/ErrorTypes.h>
  20. #include <LibJS/Runtime/Exception.h>
  21. #include <LibJS/Runtime/GlobalEnvironment.h>
  22. #include <LibJS/Runtime/GlobalObject.h>
  23. #include <LibJS/Runtime/MarkedValueList.h>
  24. #include <LibJS/Runtime/Realm.h>
  25. #include <LibJS/Runtime/VM.h>
  26. #include <LibJS/Runtime/Value.h>
  27. #include <LibJS/Script.h>
  28. #include <LibJS/SourceTextModule.h>
  29. namespace JS {
  30. struct ExecutingASTNodeChain {
  31. ExecutingASTNodeChain* previous { nullptr };
  32. const ASTNode& node;
  33. };
  34. class Interpreter : public Weakable<Interpreter> {
  35. public:
  36. // 9.6 InitializeHostDefinedRealm ( ), https://tc39.es/ecma262/#sec-initializehostdefinedrealm
  37. template<typename GlobalObjectType, typename GlobalThisObjectType, typename... Args>
  38. static NonnullOwnPtr<Interpreter> create(VM& vm, Args&&... args) requires(IsBaseOf<GlobalObject, GlobalObjectType>&& IsBaseOf<Object, GlobalThisObjectType>)
  39. {
  40. DeferGC defer_gc(vm.heap());
  41. auto interpreter = adopt_own(*new Interpreter(vm));
  42. VM::InterpreterExecutionScope scope(*interpreter);
  43. // 1. Let realm be CreateRealm().
  44. auto* realm = Realm::create(vm);
  45. // 2. Let newContext be a new execution context. (This was done in the Interpreter constructor)
  46. // 3. Set the Function of newContext to null. (This is done for us when the execution context is constructed)
  47. // 4. Set the Realm of newContext to realm.
  48. interpreter->m_global_execution_context.realm = realm;
  49. // 5. Set the ScriptOrModule of newContext to null. (This was done during execution context construction)
  50. // 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.
  51. // Otherwise, let global be undefined, indicating that an ordinary object should be created as the global object.
  52. auto* global_object = static_cast<GlobalObject*>(interpreter->heap().allocate_without_global_object<GlobalObjectType>(forward<Args>(args)...));
  53. // 6. Push newContext onto the execution context stack; newContext is now the running execution context.
  54. // NOTE: This is out of order from the spec, but it shouldn't matter here.
  55. vm.push_execution_context(interpreter->m_global_execution_context, *global_object);
  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. if constexpr (IsSame<GlobalObjectType, GlobalThisObjectType>) {
  59. // 9. Perform SetRealmGlobalObject(realm, global, thisValue).
  60. realm->set_global_object(*global_object, global_object);
  61. } else {
  62. // FIXME: Should we pass args in here? Let's er on the side of caution and say yes.
  63. auto* global_this_value = static_cast<Object*>(interpreter->heap().allocate_without_global_object<GlobalThisObjectType>(forward<Args>(args)...));
  64. // 9. Perform SetRealmGlobalObject(realm, global, thisValue).
  65. realm->set_global_object(*global_object, global_this_value);
  66. }
  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 NormalCompletion(empty).
  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();
  86. ThrowCompletionOr<Value> run(Script&);
  87. ThrowCompletionOr<Value> run(SourceTextModule&);
  88. GlobalObject& global_object();
  89. const GlobalObject& 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. ALWAYS_INLINE Exception* exception() { return vm().exception(); }
  96. Environment* lexical_environment() { return vm().lexical_environment(); }
  97. void push_ast_node(ExecutingASTNodeChain& chain_node)
  98. {
  99. chain_node.previous = m_ast_node_chain;
  100. m_ast_node_chain = &chain_node;
  101. }
  102. void pop_ast_node()
  103. {
  104. VERIFY(m_ast_node_chain);
  105. m_ast_node_chain = m_ast_node_chain->previous;
  106. }
  107. const ASTNode* current_node() const { return m_ast_node_chain ? &m_ast_node_chain->node : nullptr; }
  108. private:
  109. explicit Interpreter(VM&);
  110. ExecutingASTNodeChain* m_ast_node_chain { nullptr };
  111. NonnullRefPtr<VM> m_vm;
  112. Handle<GlobalObject> m_global_object;
  113. Handle<Realm> m_realm;
  114. // This is here to keep the global execution context alive for the entire lifespan of the Interpreter.
  115. ExecutionContext m_global_execution_context;
  116. };
  117. }