Bumping manifests a=b2g-bump
[gecko.git] / js / src / jspubtd.h
blob2309e13d2070bbb7d0c87e8156a30b295cbebd41
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/. */
7 #ifndef jspubtd_h
8 #define jspubtd_h
11 * JS public API typedefs.
14 #include "mozilla/Assertions.h"
15 #include "mozilla/LinkedList.h"
16 #include "mozilla/PodOperations.h"
18 #include "jsprototypes.h"
19 #include "jstypes.h"
21 #include "js/TypeDecls.h"
23 #if (defined(JS_GC_ZEAL)) || \
24 (defined(JSGC_COMPACTING) && defined(DEBUG))
25 # define JSGC_HASH_TABLE_CHECKS
26 #endif
28 namespace JS {
30 class AutoIdVector;
31 class CallArgs;
33 template <typename T>
34 class Rooted;
36 class JS_FRIEND_API(CompileOptions);
37 class JS_FRIEND_API(ReadOnlyCompileOptions);
38 class JS_FRIEND_API(OwningCompileOptions);
39 class JS_PUBLIC_API(CompartmentOptions);
41 struct Zone;
43 } /* namespace JS */
45 namespace js {
46 struct ContextFriendFields;
47 } // namespace js
50 * Run-time version enumeration. For compile-time version checking, please use
51 * the JS_HAS_* macros in jsversion.h, or use MOZJS_MAJOR_VERSION,
52 * MOZJS_MINOR_VERSION, MOZJS_PATCH_VERSION, and MOZJS_ALPHA definitions.
54 enum JSVersion {
55 JSVERSION_ECMA_3 = 148,
56 JSVERSION_1_6 = 160,
57 JSVERSION_1_7 = 170,
58 JSVERSION_1_8 = 180,
59 JSVERSION_ECMA_5 = 185,
60 JSVERSION_DEFAULT = 0,
61 JSVERSION_UNKNOWN = -1,
62 JSVERSION_LATEST = JSVERSION_ECMA_5
65 /* Result of typeof operator enumeration. */
66 enum JSType {
67 JSTYPE_VOID, /* undefined */
68 JSTYPE_OBJECT, /* object */
69 JSTYPE_FUNCTION, /* function */
70 JSTYPE_STRING, /* string */
71 JSTYPE_NUMBER, /* number */
72 JSTYPE_BOOLEAN, /* boolean */
73 JSTYPE_NULL, /* null */
74 JSTYPE_SYMBOL, /* symbol */
75 JSTYPE_LIMIT
78 /* Dense index into cached prototypes and class atoms for standard objects. */
79 enum JSProtoKey {
80 #define PROTOKEY_AND_INITIALIZER(name,code,init,clasp) JSProto_##name = code,
81 JS_FOR_EACH_PROTOTYPE(PROTOKEY_AND_INITIALIZER)
82 #undef PROTOKEY_AND_INITIALIZER
83 JSProto_LIMIT
86 /* Struct forward declarations. */
87 struct JSClass;
88 struct JSCompartment;
89 struct JSCrossCompartmentCall;
90 class JSErrorReport;
91 struct JSExceptionState;
92 struct JSFunctionSpec;
93 struct JSIdArray;
94 struct JSLocaleCallbacks;
95 struct JSObjectMap;
96 struct JSPrincipals;
97 struct JSPropertyDescriptor;
98 struct JSPropertyName;
99 struct JSPropertySpec;
100 struct JSRuntime;
101 struct JSSecurityCallbacks;
102 struct JSStructuredCloneCallbacks;
103 struct JSStructuredCloneReader;
104 struct JSStructuredCloneWriter;
105 class JS_PUBLIC_API(JSTracer);
107 class JSFlatString;
109 typedef struct PRCallOnceType JSCallOnceType;
110 typedef bool (*JSInitCallback)(void);
112 template<typename T> struct JSConstScalarSpec;
113 typedef JSConstScalarSpec<double> JSConstDoubleSpec;
114 typedef JSConstScalarSpec<int32_t> JSConstIntegerSpec;
117 * Generic trace operation that calls JS_CallTracer on each traceable thing
118 * stored in data.
120 typedef void
121 (* JSTraceDataOp)(JSTracer* trc, void* data);
123 void js_FinishGC(JSRuntime* rt);
125 namespace js {
126 namespace gc {
127 class StoreBuffer;
128 void MarkPersistentRootedChains(JSTracer*);
129 void FinishPersistentRootedChains(JSRuntime*);
133 namespace JS {
135 typedef void (*OffThreadCompileCallback)(void* token, void* callbackData);
137 namespace shadow {
139 struct Runtime
141 /* Restrict zone access during Minor GC. */
142 bool needsIncrementalBarrier_;
144 private:
145 js::gc::StoreBuffer* gcStoreBufferPtr_;
147 public:
148 Runtime()
149 : needsIncrementalBarrier_(false)
150 , gcStoreBufferPtr_(nullptr)
153 bool needsIncrementalBarrier() const {
154 return needsIncrementalBarrier_;
157 js::gc::StoreBuffer* gcStoreBufferPtr() { return gcStoreBufferPtr_; }
159 static JS::shadow::Runtime* asShadowRuntime(JSRuntime* rt) {
160 return reinterpret_cast<JS::shadow::Runtime*>(rt);
163 protected:
164 void setGCStoreBufferPtr(js::gc::StoreBuffer* storeBuffer) {
165 gcStoreBufferPtr_ = storeBuffer;
168 /* Allow inlining of PersistentRooted constructors and destructors. */
169 private:
170 template <typename Referent> friend class JS::PersistentRooted;
171 friend void js::gc::MarkPersistentRootedChains(JSTracer*);
172 friend void js::gc::FinishPersistentRootedChains(JSRuntime* rt);
174 mozilla::LinkedList<PersistentRootedFunction> functionPersistentRooteds;
175 mozilla::LinkedList<PersistentRootedId> idPersistentRooteds;
176 mozilla::LinkedList<PersistentRootedObject> objectPersistentRooteds;
177 mozilla::LinkedList<PersistentRootedScript> scriptPersistentRooteds;
178 mozilla::LinkedList<PersistentRootedString> stringPersistentRooteds;
179 mozilla::LinkedList<PersistentRootedValue> valuePersistentRooteds;
181 /* Specializations of this return references to the appropriate list. */
182 template<typename Referent>
183 inline mozilla::LinkedList<PersistentRooted<Referent> >& getPersistentRootedList();
186 template<>
187 inline mozilla::LinkedList<PersistentRootedFunction>
188 &Runtime::getPersistentRootedList<JSFunction*>() { return functionPersistentRooteds; }
190 template<>
191 inline mozilla::LinkedList<PersistentRootedId>
192 &Runtime::getPersistentRootedList<jsid>() { return idPersistentRooteds; }
194 template<>
195 inline mozilla::LinkedList<PersistentRootedObject>
196 &Runtime::getPersistentRootedList<JSObject*>() { return objectPersistentRooteds; }
198 template<>
199 inline mozilla::LinkedList<PersistentRootedScript>
200 &Runtime::getPersistentRootedList<JSScript*>() { return scriptPersistentRooteds; }
202 template<>
203 inline mozilla::LinkedList<PersistentRootedString>
204 &Runtime::getPersistentRootedList<JSString*>() { return stringPersistentRooteds; }
206 template<>
207 inline mozilla::LinkedList<PersistentRootedValue>
208 &Runtime::getPersistentRootedList<Value>() { return valuePersistentRooteds; }
210 } /* namespace shadow */
212 class JS_PUBLIC_API(AutoGCRooter)
214 public:
215 AutoGCRooter(JSContext* cx, ptrdiff_t tag);
216 AutoGCRooter(js::ContextFriendFields* cx, ptrdiff_t tag);
218 ~AutoGCRooter() {
219 MOZ_ASSERT(this == *stackTop);
220 *stackTop = down;
223 /* Implemented in gc/RootMarking.cpp. */
224 inline void trace(JSTracer* trc);
225 static void traceAll(JSTracer* trc);
226 static void traceAllWrappers(JSTracer* trc);
228 /* T must be a context type */
229 template<typename T>
230 static void traceAllInContext(T* cx, JSTracer* trc) {
231 for (AutoGCRooter* gcr = cx->autoGCRooters; gcr; gcr = gcr->down)
232 gcr->trace(trc);
235 protected:
236 AutoGCRooter * const down;
239 * Discriminates actual subclass of this being used. If non-negative, the
240 * subclass roots an array of values of the length stored in this field.
241 * If negative, meaning is indicated by the corresponding value in the enum
242 * below. Any other negative value indicates some deeper problem such as
243 * memory corruption.
245 ptrdiff_t tag_;
247 enum {
248 VALARRAY = -2, /* js::AutoValueArray */
249 PARSER = -3, /* js::frontend::Parser */
250 SHAPEVECTOR = -4, /* js::AutoShapeVector */
251 IDARRAY = -6, /* js::AutoIdArray */
252 DESCVECTOR = -7, /* js::AutoPropDescVector */
253 VALVECTOR = -10, /* js::AutoValueVector */
254 IDVECTOR = -13, /* js::AutoIdVector */
255 OBJVECTOR = -14, /* js::AutoObjectVector */
256 STRINGVECTOR =-15, /* js::AutoStringVector */
257 SCRIPTVECTOR =-16, /* js::AutoScriptVector */
258 NAMEVECTOR = -17, /* js::AutoNameVector */
259 HASHABLEVALUE=-18, /* js::HashableValue */
260 IONMASM = -19, /* js::jit::MacroAssembler */
261 WRAPVECTOR = -20, /* js::AutoWrapperVector */
262 WRAPPER = -21, /* js::AutoWrapperRooter */
263 OBJOBJHASHMAP=-22, /* js::AutoObjectObjectHashMap */
264 OBJU32HASHMAP=-23, /* js::AutoObjectUnsigned32HashMap */
265 OBJHASHSET = -24, /* js::AutoObjectHashSet */
266 JSONPARSER = -25, /* js::JSONParser */
267 CUSTOM = -26, /* js::CustomAutoRooter */
268 FUNVECTOR = -27 /* js::AutoFunctionVector */
271 private:
272 AutoGCRooter ** const stackTop;
274 /* No copy or assignment semantics. */
275 AutoGCRooter(AutoGCRooter& ida) = delete;
276 void operator=(AutoGCRooter& ida) = delete;
279 } /* namespace JS */
281 namespace js {
284 * Parallel operations in general can have one of three states. They may
285 * succeed, fail, or "bail", where bail indicates that the code encountered an
286 * unexpected condition and should be re-run sequentially. Different
287 * subcategories of the "bail" state are encoded as variants of TP_RETRY_*.
289 enum ParallelResult { TP_SUCCESS, TP_RETRY_SEQUENTIALLY, TP_RETRY_AFTER_GC, TP_FATAL };
291 struct ThreadSafeContext;
292 class ForkJoinContext;
293 class ExclusiveContext;
295 class Allocator;
297 enum ThingRootKind
299 THING_ROOT_OBJECT,
300 THING_ROOT_SHAPE,
301 THING_ROOT_BASE_SHAPE,
302 THING_ROOT_TYPE_OBJECT,
303 THING_ROOT_STRING,
304 THING_ROOT_SYMBOL,
305 THING_ROOT_JIT_CODE,
306 THING_ROOT_SCRIPT,
307 THING_ROOT_LAZY_SCRIPT,
308 THING_ROOT_ID,
309 THING_ROOT_VALUE,
310 THING_ROOT_TYPE,
311 THING_ROOT_BINDINGS,
312 THING_ROOT_PROPERTY_DESCRIPTOR,
313 THING_ROOT_PROP_DESC,
314 THING_ROOT_LIMIT
318 * This list enumerates the different types of conceptual stacks we have in
319 * SpiderMonkey. In reality, they all share the C stack, but we allow different
320 * stack limits depending on the type of code running.
322 enum StackKind
324 StackForSystemCode, // C++, such as the GC, running on behalf of the VM.
325 StackForTrustedScript, // Script running with trusted principals.
326 StackForUntrustedScript, // Script running with untrusted principals.
327 StackKindCount
330 template <typename T>
331 struct RootKind;
334 * Specifically mark the ThingRootKind of externally visible types, so that
335 * JSAPI users may use JSRooted... types without having the class definition
336 * available.
338 template<typename T, ThingRootKind Kind>
339 struct SpecificRootKind
341 static ThingRootKind rootKind() { return Kind; }
344 template <> struct RootKind<JSObject*> : SpecificRootKind<JSObject*, THING_ROOT_OBJECT> {};
345 template <> struct RootKind<JSFlatString*> : SpecificRootKind<JSFlatString*, THING_ROOT_STRING> {};
346 template <> struct RootKind<JSFunction*> : SpecificRootKind<JSFunction*, THING_ROOT_OBJECT> {};
347 template <> struct RootKind<JSString*> : SpecificRootKind<JSString*, THING_ROOT_STRING> {};
348 template <> struct RootKind<JS::Symbol*> : SpecificRootKind<JS::Symbol*, THING_ROOT_SYMBOL> {};
349 template <> struct RootKind<JSScript*> : SpecificRootKind<JSScript*, THING_ROOT_SCRIPT> {};
350 template <> struct RootKind<jsid> : SpecificRootKind<jsid, THING_ROOT_ID> {};
351 template <> struct RootKind<JS::Value> : SpecificRootKind<JS::Value, THING_ROOT_VALUE> {};
353 struct ContextFriendFields
355 protected:
356 JSRuntime* const runtime_;
358 /* The current compartment. */
359 JSCompartment* compartment_;
361 /* The current zone. */
362 JS::Zone* zone_;
364 public:
365 explicit ContextFriendFields(JSRuntime* rt)
366 : runtime_(rt), compartment_(nullptr), zone_(nullptr), autoGCRooters(nullptr)
368 mozilla::PodArrayZero(thingGCRooters);
371 static const ContextFriendFields* get(const JSContext* cx) {
372 return reinterpret_cast<const ContextFriendFields*>(cx);
375 static ContextFriendFields* get(JSContext* cx) {
376 return reinterpret_cast<ContextFriendFields*>(cx);
379 private:
381 * Stack allocated GC roots for stack GC heap pointers, which may be
382 * overwritten if moved during a GC.
384 JS::Rooted<void*>* thingGCRooters[THING_ROOT_LIMIT];
386 public:
387 template <class T>
388 inline JS::Rooted<T>* gcRooters() {
389 js::ThingRootKind kind = RootKind<T>::rootKind();
390 return reinterpret_cast<JS::Rooted<T>*>(thingGCRooters[kind]);
393 void checkNoGCRooters();
395 /* Stack of thread-stack-allocated GC roots. */
396 JS::AutoGCRooter* autoGCRooters;
398 friend JSRuntime* GetRuntime(const JSContext* cx);
399 friend JSCompartment* GetContextCompartment(const JSContext* cx);
400 friend JS::Zone* GetContextZone(const JSContext* cx);
401 template <typename T> friend class JS::Rooted;
405 * Inlinable accessors for JSContext.
407 * - These must not be available on the more restricted superclasses of
408 * JSContext, so we can't simply define them on ContextFriendFields.
410 * - They're perfectly ordinary JSContext functionality, so ought to be
411 * usable without resorting to jsfriendapi.h, and when JSContext is an
412 * incomplete type.
414 inline JSRuntime*
415 GetRuntime(const JSContext* cx)
417 return ContextFriendFields::get(cx)->runtime_;
420 inline JSCompartment*
421 GetContextCompartment(const JSContext* cx)
423 return ContextFriendFields::get(cx)->compartment_;
426 inline JS::Zone*
427 GetContextZone(const JSContext* cx)
429 return ContextFriendFields::get(cx)->zone_;
432 class PerThreadData;
434 struct PerThreadDataFriendFields
436 private:
437 // Note: this type only exists to permit us to derive the offset of
438 // the perThread data within the real JSRuntime* type in a portable
439 // way.
440 struct RuntimeDummy : JS::shadow::Runtime
442 struct PerThreadDummy {
443 void* field1;
444 uintptr_t field2;
445 #ifdef JS_DEBUG
446 uint64_t field3;
447 #endif
448 } mainThread;
451 public:
453 PerThreadDataFriendFields();
455 private:
457 * Stack allocated GC roots for stack GC heap pointers, which may be
458 * overwritten if moved during a GC.
460 JS::Rooted<void*>* thingGCRooters[THING_ROOT_LIMIT];
462 public:
463 template <class T>
464 inline JS::Rooted<T>* gcRooters() {
465 js::ThingRootKind kind = RootKind<T>::rootKind();
466 return reinterpret_cast<JS::Rooted<T>*>(thingGCRooters[kind]);
469 /* Limit pointer for checking native stack consumption. */
470 uintptr_t nativeStackLimit[StackKindCount];
472 static const size_t RuntimeMainThreadOffset = offsetof(RuntimeDummy, mainThread);
474 static inline PerThreadDataFriendFields* get(js::PerThreadData* pt) {
475 return reinterpret_cast<PerThreadDataFriendFields*>(pt);
478 static inline PerThreadDataFriendFields* getMainThread(JSRuntime* rt) {
479 // mainThread must always appear directly after |JS::shadow::Runtime|.
480 // Tested by a JS_STATIC_ASSERT in |jsfriendapi.cpp|
481 return reinterpret_cast<PerThreadDataFriendFields*>(
482 reinterpret_cast<char*>(rt) + RuntimeMainThreadOffset);
485 static inline const PerThreadDataFriendFields* getMainThread(const JSRuntime* rt) {
486 // mainThread must always appear directly after |JS::shadow::Runtime|.
487 // Tested by a JS_STATIC_ASSERT in |jsfriendapi.cpp|
488 return reinterpret_cast<const PerThreadDataFriendFields*>(
489 reinterpret_cast<const char*>(rt) + RuntimeMainThreadOffset);
492 template <typename T> friend class JS::Rooted;
495 } /* namespace js */
497 #endif /* jspubtd_h */