Realm.cpp 4.6 KB

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