Realm.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/TypeCasts.h>
  8. #include <LibJS/Heap/DeferGC.h>
  9. #include <LibJS/Runtime/GlobalEnvironment.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. #include <LibJS/Runtime/Realm.h>
  12. #include <LibJS/Runtime/VM.h>
  13. namespace JS {
  14. // 9.3.1 CreateRealm ( ), https://tc39.es/ecma262/#sec-createrealm
  15. ThrowCompletionOr<NonnullGCPtr<Realm>> Realm::create(VM& vm)
  16. {
  17. // 1. Let realmRec be a new Realm Record.
  18. auto realm = vm.heap().allocate_without_realm<Realm>();
  19. // 2. Perform CreateIntrinsics(realmRec).
  20. MUST_OR_THROW_OOM(Intrinsics::create(*realm));
  21. // 3. Set realmRec.[[GlobalObject]] to undefined.
  22. // 4. Set realmRec.[[GlobalEnv]] to undefined.
  23. // 5. Set realmRec.[[TemplateMap]] to a new empty List.
  24. // 6. Return realmRec.
  25. return realm;
  26. }
  27. // 9.6 InitializeHostDefinedRealm ( ), https://tc39.es/ecma262/#sec-initializehostdefinedrealm
  28. ThrowCompletionOr<NonnullOwnPtr<ExecutionContext>> Realm::initialize_host_defined_realm(VM& vm, Function<Object*(Realm&)> create_global_object, Function<Object*(Realm&)> create_global_this_value)
  29. {
  30. DeferGC defer_gc(vm.heap());
  31. // 1. Let realm be CreateRealm().
  32. auto realm = MUST_OR_THROW_OOM(Realm::create(vm));
  33. // 2. Let newContext be a new execution context.
  34. auto new_context = make<ExecutionContext>(vm.heap());
  35. // 3. Set the Function of newContext to null.
  36. new_context->function = nullptr;
  37. // 4. Set the Realm of newContext to realm.
  38. new_context->realm = realm;
  39. // 5. Set the ScriptOrModule of newContext to null.
  40. new_context->script_or_module = {};
  41. // 6. Push newContext onto the execution context stack; newContext is now the running execution context.
  42. vm.push_execution_context(*new_context);
  43. // 7. If the host requires use of an exotic object to serve as realm's global object,
  44. // let global be such an object created in a host-defined manner.
  45. // Otherwise, let global be undefined, indicating that an ordinary object should be created as the global object.
  46. Object* global = nullptr;
  47. if (create_global_object)
  48. global = create_global_object(*realm);
  49. // 8. If the host requires that the this binding in realm's global scope return an object other than the global object,
  50. // let thisValue be such an object created in a host-defined manner.
  51. // Otherwise, let thisValue be undefined, indicating that realm's global this binding should be the global object.
  52. Object* this_value = nullptr;
  53. if (create_global_this_value)
  54. this_value = create_global_this_value(*realm);
  55. // 9. Perform SetRealmGlobalObject(realm, global, thisValue).
  56. realm->set_global_object(global, this_value);
  57. // 10. Let globalObj be ? SetDefaultGlobalBindings(realm).
  58. auto& global_object = set_default_global_bindings(*realm);
  59. // 11. Create any host-defined global object properties on globalObj.
  60. global_object.initialize(*realm);
  61. // 12. Return unused.
  62. return new_context;
  63. }
  64. // 9.3.3 SetRealmGlobalObject ( realmRec, globalObj, thisValue ), https://tc39.es/ecma262/#sec-setrealmglobalobject
  65. void Realm::set_global_object(Object* global_object, Object* this_value)
  66. {
  67. // 1. If globalObj is undefined, then
  68. if (global_object == nullptr) {
  69. // a. Let intrinsics be realmRec.[[Intrinsics]].
  70. // b. Set globalObj to OrdinaryObjectCreate(intrinsics.[[%Object.prototype%]]).
  71. // NOTE: We allocate a proper GlobalObject directly as this plain object is
  72. // turned into one via SetDefaultGlobalBindings in the spec.
  73. global_object = heap().allocate_without_realm<GlobalObject>(*this);
  74. }
  75. // 2. Assert: Type(globalObj) is Object.
  76. VERIFY(global_object);
  77. // 3. If thisValue is undefined, set thisValue to globalObj.
  78. if (this_value == nullptr)
  79. this_value = global_object;
  80. // Non-standard
  81. VERIFY(this_value);
  82. // 4. Set realmRec.[[GlobalObject]] to globalObj.
  83. m_global_object = global_object;
  84. // 5. Let newGlobalEnv be NewGlobalEnvironment(globalObj, thisValue).
  85. // 6. Set realmRec.[[GlobalEnv]] to newGlobalEnv.
  86. m_global_environment = m_global_object->heap().allocate_without_realm<GlobalEnvironment>(*global_object, *this_value);
  87. // 7. Return unused.
  88. }
  89. void Realm::visit_edges(Visitor& visitor)
  90. {
  91. Base::visit_edges(visitor);
  92. visitor.visit(m_intrinsics);
  93. visitor.visit(m_global_object);
  94. visitor.visit(m_global_environment);
  95. if (m_host_defined)
  96. m_host_defined->visit_edges(visitor);
  97. }
  98. }