Interpreter.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. const ASTNode& 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. (This was done in the Interpreter constructor)
  44. // 3. Set the Function of newContext to null. (This is done for us when the execution context is constructed)
  45. // 4. Set the Realm of newContext to realm.
  46. interpreter->m_global_execution_context.realm = realm;
  47. // 5. Set the ScriptOrModule of newContext to null. (This was done during execution context construction)
  48. // 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.
  49. // Otherwise, let global be undefined, indicating that an ordinary object should be created as the global object.
  50. auto* global_object = static_cast<GlobalObject*>(interpreter->heap().allocate_without_global_object<GlobalObjectType>(forward<Args>(args)...));
  51. // 6. Push newContext onto the execution context stack; newContext is now the running execution context.
  52. // NOTE: This is out of order from the spec, but it shouldn't matter here.
  53. vm.push_execution_context(interpreter->m_global_execution_context, *global_object);
  54. // 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
  55. // in a host-defined manner. Otherwise, let thisValue be undefined, indicating that realm's global this binding should be the global object.
  56. if constexpr (IsSame<GlobalObjectType, GlobalThisObjectType>) {
  57. // 9. Perform SetRealmGlobalObject(realm, global, thisValue).
  58. realm->set_global_object(*global_object, global_object);
  59. } else {
  60. // FIXME: Should we pass args in here? Let's er on the side of caution and say yes.
  61. auto* global_this_value = static_cast<Object*>(interpreter->heap().allocate_without_global_object<GlobalThisObjectType>(forward<Args>(args)...));
  62. // 9. Perform SetRealmGlobalObject(realm, global, thisValue).
  63. realm->set_global_object(*global_object, global_this_value);
  64. }
  65. // NOTE: These are not in the spec.
  66. static FlyString global_execution_context_name = "(global execution context)";
  67. interpreter->m_global_execution_context.function_name = global_execution_context_name;
  68. interpreter->m_global_object = make_handle(global_object);
  69. interpreter->m_realm = make_handle(realm);
  70. // 10. Let globalObj be ? SetDefaultGlobalBindings(realm).
  71. // 11. Create any host-defined global object properties on globalObj.
  72. static_cast<GlobalObjectType*>(global_object)->initialize_global_object();
  73. // 12. Return NormalCompletion(empty).
  74. return interpreter;
  75. }
  76. template<typename GlobalObjectType, typename... Args>
  77. static NonnullOwnPtr<Interpreter> create(VM& vm, Args&&... args) requires IsBaseOf<GlobalObject, GlobalObjectType>
  78. {
  79. // NOTE: This function is here to facilitate step 8 of InitializeHostDefinedRealm. (Callers don't have to specify the same type twice if not necessary)
  80. return create<GlobalObjectType, GlobalObjectType>(vm, args...);
  81. }
  82. static NonnullOwnPtr<Interpreter> create_with_existing_realm(Realm&);
  83. ~Interpreter();
  84. ThrowCompletionOr<Value> run(Script&);
  85. ThrowCompletionOr<Value> run(SourceTextModule&);
  86. GlobalObject& global_object();
  87. const GlobalObject& global_object() const;
  88. Realm& realm();
  89. Realm const& realm() const;
  90. ALWAYS_INLINE VM& vm() { return *m_vm; }
  91. ALWAYS_INLINE const VM& vm() const { return *m_vm; }
  92. ALWAYS_INLINE Heap& heap() { return vm().heap(); }
  93. Environment* lexical_environment() { return vm().lexical_environment(); }
  94. void push_ast_node(ExecutingASTNodeChain& chain_node)
  95. {
  96. chain_node.previous = m_ast_node_chain;
  97. m_ast_node_chain = &chain_node;
  98. }
  99. void pop_ast_node()
  100. {
  101. VERIFY(m_ast_node_chain);
  102. m_ast_node_chain = m_ast_node_chain->previous;
  103. }
  104. const ASTNode* current_node() const { return m_ast_node_chain ? &m_ast_node_chain->node : nullptr; }
  105. private:
  106. explicit Interpreter(VM&);
  107. ExecutingASTNodeChain* m_ast_node_chain { nullptr };
  108. NonnullRefPtr<VM> m_vm;
  109. Handle<GlobalObject> m_global_object;
  110. Handle<Realm> m_realm;
  111. // This is here to keep the global execution context alive for the entire lifespan of the Interpreter.
  112. ExecutionContext m_global_execution_context;
  113. };
  114. }