| 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/. */ |
| 6 |
|
| 7 |
#ifndef js_Realm_h |
| 8 |
#define js_Realm_h |
| 9 |
|
| 10 |
#include "js/shadow/Realm.h" // JS::shadow::Realm |
| 11 |
|
| 12 |
#include "js/GCPolicyAPI.h" |
| 13 |
#include "js/TypeDecls.h" // forward-declaration of JS::Realm |
| 14 |
|
| 15 |
/************************************************************************/ |
| 16 |
|
| 17 |
// [SMDOC] Realms |
| 18 |
// |
| 19 |
// Data associated with a global object. In the browser each frame has its |
| 20 |
// own global/realm. |
| 21 |
|
| 22 |
namespace js { |
| 23 |
namespace gc { |
| 24 |
JS_PUBLIC_API void TraceRealmRoot(JSTracer* trc, JS::Realm* realm, |
| 25 |
const char* name); |
| 26 |
} // namespace gc |
| 27 |
} // namespace js |
| 28 |
|
| 29 |
namespace JS { |
| 30 |
class JS_PUBLIC_API AutoRequireNoGC; |
| 31 |
|
| 32 |
// Each Realm holds a weak reference to its GlobalObject. |
| 33 |
template <> |
| 34 |
struct GCPolicy<Realm*> : public NonGCPointerPolicy<Realm*> { |
| 35 |
static void trace(JSTracer* trc, Realm** vp, const char* name) { |
| 36 |
if (*vp) { |
| 37 |
::js::gc::TraceRealmRoot(trc, *vp, name); |
| 38 |
} |
| 39 |
} |
| 40 |
}; |
| 41 |
|
| 42 |
// Get the current realm, if any. The ECMAScript spec calls this "the current |
| 43 |
// Realm Record". |
| 44 |
extern JS_PUBLIC_API Realm* GetCurrentRealmOrNull(JSContext* cx); |
| 45 |
|
| 46 |
// Return the compartment that contains a given realm. |
| 47 |
inline JS::Compartment* GetCompartmentForRealm(Realm* realm) { |
| 48 |
return shadow::Realm::get(realm)->compartment(); |
| 49 |
} |
| 50 |
|
| 51 |
// Return an object's realm. All objects except cross-compartment wrappers are |
| 52 |
// created in a particular realm, which never changes. Returns null if obj is |
| 53 |
// a cross-compartment wrapper. |
| 54 |
extern JS_PUBLIC_API Realm* GetObjectRealmOrNull(JSObject* obj); |
| 55 |
|
| 56 |
// Get the value of the "private data" internal field of the given Realm. |
| 57 |
// This field is initially null and is set using SetRealmPrivate. |
| 58 |
// It's a pointer to embeddding-specific data that SpiderMonkey never uses. |
| 59 |
extern JS_PUBLIC_API void* GetRealmPrivate(Realm* realm); |
| 60 |
|
| 61 |
// Set the "private data" internal field of the given Realm. |
| 62 |
extern JS_PUBLIC_API void SetRealmPrivate(Realm* realm, void* data); |
| 63 |
|
| 64 |
typedef void (*DestroyRealmCallback)(JS::GCContext* gcx, Realm* realm); |
| 65 |
|
| 66 |
// Set the callback SpiderMonkey calls just before garbage-collecting a realm. |
| 67 |
// Embeddings can use this callback to free private data associated with the |
| 68 |
// realm via SetRealmPrivate. |
| 69 |
// |
| 70 |
// By the time this is called, the global object for the realm has already been |
| 71 |
// collected. |
| 72 |
extern JS_PUBLIC_API void SetDestroyRealmCallback( |
| 73 |
JSContext* cx, DestroyRealmCallback callback); |
| 74 |
|
| 75 |
using RealmNameCallback = void (*)(JSContext* cx, Realm* realm, char* buf, |
| 76 |
size_t bufsize, |
| 77 |
const JS::AutoRequireNoGC& nogc); |
| 78 |
|
| 79 |
// Set the callback SpiderMonkey calls to get the name of a realm, for |
| 80 |
// diagnostic output. |
| 81 |
extern JS_PUBLIC_API void SetRealmNameCallback(JSContext* cx, |
| 82 |
RealmNameCallback callback); |
| 83 |
|
| 84 |
// Get the global object for the given realm. This only returns nullptr during |
| 85 |
// GC, between collecting the global object and destroying the Realm. |
| 86 |
extern JS_PUBLIC_API JSObject* GetRealmGlobalOrNull(Realm* realm); |
| 87 |
|
| 88 |
// Initialize standard JS class constructors, prototypes, and any top-level |
| 89 |
// functions and constants associated with the standard classes (e.g. isNaN |
| 90 |
// for Number). |
| 91 |
extern JS_PUBLIC_API bool InitRealmStandardClasses(JSContext* cx); |
| 92 |
|
| 93 |
// If the current realm has the non-standard freezeBuiltins option set to true, |
| 94 |
// freeze the constructor object and seal the prototype. |
| 95 |
extern JS_PUBLIC_API bool MaybeFreezeCtorAndPrototype(JSContext* cx, |
| 96 |
HandleObject ctor, |
| 97 |
HandleObject maybeProto); |
| 98 |
|
| 99 |
/* |
| 100 |
* Ways to get various per-Realm objects. All the getters declared below operate |
| 101 |
* on the JSContext's current Realm. |
| 102 |
*/ |
| 103 |
|
| 104 |
extern JS_PUBLIC_API JSObject* GetRealmObjectPrototype(JSContext* cx); |
| 105 |
extern JS_PUBLIC_API JS::Handle<JSObject*> GetRealmObjectPrototypeHandle( |
| 106 |
JSContext* cx); |
| 107 |
|
| 108 |
extern JS_PUBLIC_API JSObject* GetRealmFunctionPrototype(JSContext* cx); |
| 109 |
extern JS_PUBLIC_API JS::Handle<JSObject*> GetRealmFunctionPrototypeHandle( |
| 110 |
JSContext* cx); |
| 111 |
|
| 112 |
extern JS_PUBLIC_API JSObject* GetRealmArrayPrototype(JSContext* cx); |
| 113 |
|
| 114 |
extern JS_PUBLIC_API JSObject* GetRealmErrorPrototype(JSContext* cx); |
| 115 |
|
| 116 |
extern JS_PUBLIC_API JSObject* GetRealmIteratorPrototype(JSContext* cx); |
| 117 |
|
| 118 |
extern JS_PUBLIC_API JSObject* GetRealmAsyncIteratorPrototype(JSContext* cx); |
| 119 |
|
| 120 |
// Returns an object that represents the realm, that can be referred from |
| 121 |
// other realm/compartment. |
| 122 |
// See the consumer in `MaybeCrossOriginObjectMixins::EnsureHolder` for details. |
| 123 |
extern JS_PUBLIC_API JSObject* GetRealmKeyObject(JSContext* cx); |
| 124 |
|
| 125 |
// Implements https://tc39.github.io/ecma262/#sec-getfunctionrealm |
| 126 |
// 7.3.22 GetFunctionRealm ( obj ) |
| 127 |
// |
| 128 |
// WARNING: may return a realm in a different compartment! |
| 129 |
// |
| 130 |
// Will throw an exception and return nullptr when a security wrapper or revoked |
| 131 |
// proxy is encountered. |
| 132 |
extern JS_PUBLIC_API Realm* GetFunctionRealm(JSContext* cx, |
| 133 |
HandleObject objArg); |
| 134 |
|
| 135 |
/** NB: This API is infallible; a nullptr return value does not indicate error. |
| 136 |
* |
| 137 |
* |target| must not be a cross-compartment wrapper because CCWs are not |
| 138 |
* associated with a single realm. |
| 139 |
* |
| 140 |
* Entering a realm roots the realm and its global object until the matching |
| 141 |
* JS::LeaveRealm() call. |
| 142 |
*/ |
| 143 |
extern JS_PUBLIC_API JS::Realm* EnterRealm(JSContext* cx, JSObject* target); |
| 144 |
|
| 145 |
extern JS_PUBLIC_API void LeaveRealm(JSContext* cx, JS::Realm* oldRealm); |
| 146 |
|
| 147 |
/** |
| 148 |
* Reset the seed for Math.random() within the current realm. |
| 149 |
* |
| 150 |
* Enables embedders to reset the seed at controlled points, e.g. after |
| 151 |
* resuming execution from an instance snapshot of SpiderMonkey's VM. |
| 152 |
*/ |
| 153 |
extern JS_PUBLIC_API void ResetRealmMathRandomSeed(JSContext* cx); |
| 154 |
|
| 155 |
} // namespace JS |
| 156 |
|
| 157 |
/* |
| 158 |
* At any time, a JSContext has a current (possibly-nullptr) realm. The |
| 159 |
* preferred way to change the current realm is with JSAutoRealm: |
| 160 |
* |
| 161 |
* void foo(JSContext* cx, JSObject* obj) { |
| 162 |
* // in some realm 'r' |
| 163 |
* { |
| 164 |
* JSAutoRealm ar(cx, obj); // constructor enters |
| 165 |
* // in the realm of 'obj' |
| 166 |
* } // destructor leaves |
| 167 |
* // back in realm 'r' |
| 168 |
* } |
| 169 |
* |
| 170 |
* The object passed to JSAutoRealm must *not* be a cross-compartment wrapper, |
| 171 |
* because CCWs are not associated with a single realm. |
| 172 |
* |
| 173 |
* For more complicated uses that don't neatly fit in a C++ stack frame, the |
| 174 |
* realm can be entered and left using separate function calls: |
| 175 |
* |
| 176 |
* void foo(JSContext* cx, JSObject* obj) { |
| 177 |
* // in 'oldRealm' |
| 178 |
* JS::Realm* oldRealm = JS::EnterRealm(cx, obj); |
| 179 |
* // in the realm of 'obj' |
| 180 |
* JS::LeaveRealm(cx, oldRealm); |
| 181 |
* // back in 'oldRealm' |
| 182 |
* } |
| 183 |
* |
| 184 |
* Note: these calls must still execute in a LIFO manner w.r.t all other |
| 185 |
* enter/leave calls on the context. Furthermore, only the return value of a |
| 186 |
* JS::EnterRealm call may be passed as the 'oldRealm' argument of |
| 187 |
* the corresponding JS::LeaveRealm call. |
| 188 |
* |
| 189 |
* Entering a realm roots the realm and its global object for the lifetime of |
| 190 |
* the JSAutoRealm. |
| 191 |
*/ |
| 192 |
|
| 193 |
class MOZ_RAII JS_PUBLIC_API JSAutoRealm { |
| 194 |
JSContext* cx_; |
| 195 |
JS::Realm* oldRealm_; |
| 196 |
|
| 197 |
public: |
| 198 |
JSAutoRealm(JSContext* cx, JSObject* target); |
| 199 |
JSAutoRealm(JSContext* cx, JSScript* target); |
| 200 |
~JSAutoRealm(); |
| 201 |
}; |
| 202 |
|
| 203 |
class MOZ_RAII JS_PUBLIC_API JSAutoNullableRealm { |
| 204 |
JSContext* cx_; |
| 205 |
JS::Realm* oldRealm_; |
| 206 |
|
| 207 |
public: |
| 208 |
explicit JSAutoNullableRealm(JSContext* cx, JSObject* targetOrNull); |
| 209 |
~JSAutoNullableRealm(); |
| 210 |
}; |
| 211 |
|
| 212 |
#endif // js_Realm_h |