/work/obj-fuzz/dist/include/js/Realm.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
2 | | * vim: set ts=8 sts=4 et sw=4 tw=99: |
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 "jspubtd.h" |
11 | | #include "js/GCPolicyAPI.h" |
12 | | #include "js/TypeDecls.h" // forward-declaration of JS::Realm |
13 | | |
14 | | namespace js { |
15 | | namespace gc { |
16 | | JS_PUBLIC_API(void) TraceRealm(JSTracer* trc, JS::Realm* realm, const char* name); |
17 | | JS_PUBLIC_API(bool) RealmNeedsSweep(JS::Realm* realm); |
18 | | } |
19 | | } |
20 | | |
21 | | namespace JS { |
22 | | |
23 | | // Each Realm holds a strong reference to its GlobalObject, and vice versa. |
24 | | template <> |
25 | | struct GCPolicy<Realm*> : public NonGCPointerPolicy<Realm*> |
26 | | { |
27 | 0 | static void trace(JSTracer* trc, Realm** vp, const char* name) { |
28 | 0 | if (*vp) { |
29 | 0 | ::js::gc::TraceRealm(trc, *vp, name); |
30 | 0 | } |
31 | 0 | } |
32 | 0 | static bool needsSweep(Realm** vp) { |
33 | 0 | return *vp && ::js::gc::RealmNeedsSweep(*vp); |
34 | 0 | } |
35 | | }; |
36 | | |
37 | | // Get the current realm, if any. The ECMAScript spec calls this "the current |
38 | | // Realm Record". |
39 | | extern JS_PUBLIC_API(Realm*) |
40 | | GetCurrentRealmOrNull(JSContext* cx); |
41 | | |
42 | | namespace shadow { |
43 | | |
44 | | class Realm |
45 | | { |
46 | | protected: |
47 | | JS::Compartment* compartment_; |
48 | | |
49 | | explicit Realm(JS::Compartment* comp) |
50 | | : compartment_(comp) |
51 | 0 | {} |
52 | | |
53 | | public: |
54 | | JS::Compartment* compartment() { |
55 | | return compartment_; |
56 | | } |
57 | | static shadow::Realm* get(JS::Realm* realm) { |
58 | | return reinterpret_cast<shadow::Realm*>(realm); |
59 | | } |
60 | | }; |
61 | | |
62 | | }; // namespace shadow |
63 | | |
64 | | // Return the compartment that contains a given realm. |
65 | | inline JS::Compartment* |
66 | | GetCompartmentForRealm(Realm* realm) |
67 | | { |
68 | | return shadow::Realm::get(realm)->compartment(); |
69 | | } |
70 | | |
71 | | // Return an object's realm. All objects except cross-compartment wrappers are |
72 | | // created in a particular realm, which never changes. Returns null if obj is |
73 | | // a cross-compartment wrapper. |
74 | | extern JS_PUBLIC_API(Realm*) |
75 | | GetObjectRealmOrNull(JSObject* obj); |
76 | | |
77 | | // Return a script's realm. All scripts are created in a particular realm, which |
78 | | // never changes. |
79 | | extern JS_PUBLIC_API(Realm*) |
80 | | GetScriptRealm(JSScript* script); |
81 | | |
82 | | // Get the value of the "private data" internal field of the given Realm. |
83 | | // This field is initially null and is set using SetRealmPrivate. |
84 | | // It's a pointer to embeddding-specific data that SpiderMonkey never uses. |
85 | | extern JS_PUBLIC_API(void*) |
86 | | GetRealmPrivate(Realm* realm); |
87 | | |
88 | | // Set the "private data" internal field of the given Realm. |
89 | | extern JS_PUBLIC_API(void) |
90 | | SetRealmPrivate(Realm* realm, void* data); |
91 | | |
92 | | typedef void |
93 | | (* DestroyRealmCallback)(JSFreeOp* fop, Realm* realm); |
94 | | |
95 | | // Set the callback SpiderMonkey calls just before garbage-collecting a realm. |
96 | | // Embeddings can use this callback to free private data associated with the |
97 | | // realm via SetRealmPrivate. |
98 | | // |
99 | | // By the time this is called, the global object for the realm has already been |
100 | | // collected. |
101 | | extern JS_PUBLIC_API(void) |
102 | | SetDestroyRealmCallback(JSContext* cx, DestroyRealmCallback callback); |
103 | | |
104 | | typedef void |
105 | | (* RealmNameCallback)(JSContext* cx, Handle<Realm*> realm, char* buf, size_t bufsize); |
106 | | |
107 | | // Set the callback SpiderMonkey calls to get the name of a realm, for |
108 | | // diagnostic output. |
109 | | extern JS_PUBLIC_API(void) |
110 | | SetRealmNameCallback(JSContext* cx, RealmNameCallback callback); |
111 | | |
112 | | // Get the global object for the given realm. This only returns nullptr during |
113 | | // GC, between collecting the global object and destroying the Realm. |
114 | | extern JS_PUBLIC_API(JSObject*) |
115 | | GetRealmGlobalOrNull(Handle<Realm*> realm); |
116 | | |
117 | | // Initialize standard JS class constructors, prototypes, and any top-level |
118 | | // functions and constants associated with the standard classes (e.g. isNaN |
119 | | // for Number). |
120 | | extern JS_PUBLIC_API(bool) |
121 | | InitRealmStandardClasses(JSContext* cx); |
122 | | |
123 | | /* |
124 | | * Ways to get various per-Realm objects. All the getters declared below operate |
125 | | * on the JSContext's current Realm. |
126 | | */ |
127 | | |
128 | | extern JS_PUBLIC_API(JSObject*) |
129 | | GetRealmObjectPrototype(JSContext* cx); |
130 | | |
131 | | extern JS_PUBLIC_API(JSObject*) |
132 | | GetRealmFunctionPrototype(JSContext* cx); |
133 | | |
134 | | extern JS_PUBLIC_API(JSObject*) |
135 | | GetRealmArrayPrototype(JSContext* cx); |
136 | | |
137 | | extern JS_PUBLIC_API(JSObject*) |
138 | | GetRealmErrorPrototype(JSContext* cx); |
139 | | |
140 | | extern JS_PUBLIC_API(JSObject*) |
141 | | GetRealmIteratorPrototype(JSContext* cx); |
142 | | |
143 | | } // namespace JS |
144 | | |
145 | | #endif // js_Realm_h |
146 | | |
147 | | |