1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
12 #include "gc/Barrier.h"
13 #include "gc/Marking.h"
14 #include "vm/GlobalObject.h"
16 #include "vm/JSContext-inl.h"
18 inline void JS::Realm::initGlobal(js::GlobalObject
& global
) {
19 MOZ_ASSERT(global
.realm() == this);
24 js::GlobalObject
* JS::Realm::maybeGlobal() const {
25 MOZ_ASSERT_IF(global_
, global_
->realm() == this);
29 inline bool JS::Realm::hasLiveGlobal() const {
30 // The global is swept by traceWeakGlobalEdge when we start sweeping a zone
31 // group. This frees the GlobalObjectData, so the realm must live at least as
32 // long as the global.
33 MOZ_ASSERT_IF(global_
, !js::gc::IsAboutToBeFinalized(global_
));
37 inline bool JS::Realm::hasInitializedGlobal() const {
38 return hasLiveGlobal() && !initializingGlobal_
;
41 inline bool JS::Realm::marked() const {
42 // The Realm survives in the following cases:
43 // - its global is live
44 // - it has been entered (to ensure we don't destroy the Realm while we're
45 // allocating its global)
46 // - it was allocated after the start of an incremental GC (as there may be
47 // pointers to it from other GC things)
48 return hasLiveGlobal() || hasBeenEnteredIgnoringJit() ||
49 allocatedDuringIncrementalGC_
;
52 /* static */ inline js::ObjectRealm
& js::ObjectRealm::get(const JSObject
* obj
) {
53 // Note: obj might be a CCW if we're accessing ObjectRealm::enumerators.
54 // CCWs here are fine because we always return the same ObjectRealm for a
55 // particular (CCW) object.
56 return obj
->maybeCCWRealm()->objects_
;
60 js::AutoRealm::AutoRealm(JSContext
* cx
, const T
& target
)
61 : cx_(cx
), origin_(cx
->realm()) {
62 cx_
->enterRealmOf(target
);
65 // Protected constructor that bypasses assertions in enterRealmOf.
66 js::AutoRealm::AutoRealm(JSContext
* cx
, JS::Realm
* target
)
67 : cx_(cx
), origin_(cx
->realm()) {
68 cx_
->enterRealm(target
);
71 js::AutoRealm::~AutoRealm() { cx_
->leaveRealm(origin_
); }
73 js::AutoFunctionOrCurrentRealm::AutoFunctionOrCurrentRealm(JSContext
* cx
,
75 JS::Realm
* realm
= JS::GetFunctionRealm(cx
, fun
);
77 cx
->clearPendingException();
81 // Enter the function's realm.
82 ar_
.emplace(cx
, realm
);
85 js::AutoAllocInAtomsZone::AutoAllocInAtomsZone(JSContext
* cx
)
86 : cx_(cx
), origin_(cx
->realm()) {
87 cx_
->enterAtomsZone();
90 js::AutoAllocInAtomsZone::~AutoAllocInAtomsZone() {
91 cx_
->leaveAtomsZone(origin_
);
94 js::AutoMaybeLeaveAtomsZone::AutoMaybeLeaveAtomsZone(JSContext
* cx
)
95 : cx_(cx
), wasInAtomsZone_(cx
->zone() && cx
->zone()->isAtomsZone()) {
96 if (wasInAtomsZone_
) {
97 cx_
->leaveAtomsZone(nullptr);
101 js::AutoMaybeLeaveAtomsZone::~AutoMaybeLeaveAtomsZone() {
102 if (wasInAtomsZone_
) {
103 cx_
->enterAtomsZone();
107 js::AutoRealmUnchecked::AutoRealmUnchecked(JSContext
* cx
, JS::Realm
* target
)
108 : AutoRealm(cx
, target
) {}
110 #endif /* vm_Realm_inl_h */