Bug 1865597 - Add error checking when initializing parallel marking and disable on...
[gecko.git] / js / src / vm / Realm-inl.h
blob698d0b3f74022bdb8153b0c49578e9f2622c9b1f
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/. */
7 #ifndef vm_Realm_inl_h
8 #define vm_Realm_inl_h
10 #include "vm/Realm.h"
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);
20 MOZ_ASSERT(!global_);
21 global_.set(&global);
24 js::GlobalObject* JS::Realm::maybeGlobal() const {
25 MOZ_ASSERT_IF(global_, global_->realm() == this);
26 return global_;
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_));
34 return bool(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_;
59 template <typename T>
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,
74 HandleObject fun) {
75 JS::Realm* realm = JS::GetFunctionRealm(cx, fun);
76 if (!realm) {
77 cx->clearPendingException();
78 return;
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 */