Realm.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/GlobalEnvironment.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/Realm.h>
  9. #include <LibJS/Runtime/VM.h>
  10. namespace JS {
  11. // 9.3.1 CreateRealm ( ), https://tc39.es/ecma262/#sec-createrealm
  12. Realm* Realm::create(VM& vm)
  13. {
  14. return vm.heap().allocate_without_global_object<Realm>();
  15. }
  16. // 9.3.3 SetRealmGlobalObject ( realmRec, globalObj, thisValue ), https://tc39.es/ecma262/#sec-setrealmglobalobject
  17. void Realm::set_global_object(GlobalObject& global_object, Object* this_value)
  18. {
  19. // NOTE: Step 1 is not supported, the global object must be allocated elsewhere.
  20. // 2. Assert: Type(globalObj) is Object.
  21. // Non-standard
  22. global_object.set_associated_realm({}, *this);
  23. // 3. If thisValue is undefined, set thisValue to globalObj.
  24. if (!this_value)
  25. this_value = &global_object;
  26. // 4. Set realmRec.[[GlobalObject]] to globalObj.
  27. m_global_object = &global_object;
  28. // 5. Let newGlobalEnv be NewGlobalEnvironment(globalObj, thisValue).
  29. // 6. Set realmRec.[[GlobalEnv]] to newGlobalEnv.
  30. m_global_environment = global_object.heap().allocate_without_global_object<GlobalEnvironment>(global_object, *this_value);
  31. // 7. Return unused.
  32. }
  33. void Realm::visit_edges(Visitor& visitor)
  34. {
  35. visitor.visit(m_global_object);
  36. visitor.visit(m_global_environment);
  37. }
  38. }