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/. */
11 * JS public API typedefs.
14 #include "mozilla/Assertions.h"
15 #include "mozilla/LinkedList.h"
16 #include "mozilla/PodOperations.h"
18 #include "jsprototypes.h"
21 #include "js/TypeDecls.h"
23 #if (defined(JS_GC_ZEAL)) || \
24 (defined(JSGC_COMPACTING) && defined(DEBUG))
25 # define JSGC_HASH_TABLE_CHECKS
36 class JS_FRIEND_API(CompileOptions
);
37 class JS_FRIEND_API(ReadOnlyCompileOptions
);
38 class JS_FRIEND_API(OwningCompileOptions
);
39 class JS_PUBLIC_API(CompartmentOptions
);
46 struct ContextFriendFields
;
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.
55 JSVERSION_ECMA_3
= 148,
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. */
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 */
78 /* Dense index into cached prototypes and class atoms for standard objects. */
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
86 /* Struct forward declarations. */
89 struct JSCrossCompartmentCall
;
91 struct JSExceptionState
;
92 struct JSFunctionSpec
;
94 struct JSLocaleCallbacks
;
97 struct JSPropertyDescriptor
;
98 struct JSPropertyName
;
99 struct JSPropertySpec
;
101 struct JSSecurityCallbacks
;
102 struct JSStructuredCloneCallbacks
;
103 struct JSStructuredCloneReader
;
104 struct JSStructuredCloneWriter
;
105 class JS_PUBLIC_API(JSTracer
);
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
121 (* JSTraceDataOp
)(JSTracer
* trc
, void* data
);
123 void js_FinishGC(JSRuntime
* rt
);
128 void MarkPersistentRootedChains(JSTracer
*);
129 void FinishPersistentRootedChains(JSRuntime
*);
135 typedef void (*OffThreadCompileCallback
)(void* token
, void* callbackData
);
141 /* Restrict zone access during Minor GC. */
142 bool needsIncrementalBarrier_
;
145 js::gc::StoreBuffer
* gcStoreBufferPtr_
;
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
);
164 void setGCStoreBufferPtr(js::gc::StoreBuffer
* storeBuffer
) {
165 gcStoreBufferPtr_
= storeBuffer
;
168 /* Allow inlining of PersistentRooted constructors and destructors. */
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();
187 inline mozilla::LinkedList
<PersistentRootedFunction
>
188 &Runtime::getPersistentRootedList
<JSFunction
*>() { return functionPersistentRooteds
; }
191 inline mozilla::LinkedList
<PersistentRootedId
>
192 &Runtime::getPersistentRootedList
<jsid
>() { return idPersistentRooteds
; }
195 inline mozilla::LinkedList
<PersistentRootedObject
>
196 &Runtime::getPersistentRootedList
<JSObject
*>() { return objectPersistentRooteds
; }
199 inline mozilla::LinkedList
<PersistentRootedScript
>
200 &Runtime::getPersistentRootedList
<JSScript
*>() { return scriptPersistentRooteds
; }
203 inline mozilla::LinkedList
<PersistentRootedString
>
204 &Runtime::getPersistentRootedList
<JSString
*>() { return stringPersistentRooteds
; }
207 inline mozilla::LinkedList
<PersistentRootedValue
>
208 &Runtime::getPersistentRootedList
<Value
>() { return valuePersistentRooteds
; }
210 } /* namespace shadow */
212 class JS_PUBLIC_API(AutoGCRooter
)
215 AutoGCRooter(JSContext
* cx
, ptrdiff_t tag
);
216 AutoGCRooter(js::ContextFriendFields
* cx
, ptrdiff_t tag
);
219 MOZ_ASSERT(this == *stackTop
);
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 */
230 static void traceAllInContext(T
* cx
, JSTracer
* trc
) {
231 for (AutoGCRooter
* gcr
= cx
->autoGCRooters
; gcr
; gcr
= gcr
->down
)
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
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 */
272 AutoGCRooter
** const stackTop
;
274 /* No copy or assignment semantics. */
275 AutoGCRooter(AutoGCRooter
& ida
) = delete;
276 void operator=(AutoGCRooter
& ida
) = delete;
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
;
301 THING_ROOT_BASE_SHAPE
,
302 THING_ROOT_TYPE_OBJECT
,
307 THING_ROOT_LAZY_SCRIPT
,
312 THING_ROOT_PROPERTY_DESCRIPTOR
,
313 THING_ROOT_PROP_DESC
,
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.
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.
330 template <typename T
>
334 * Specifically mark the ThingRootKind of externally visible types, so that
335 * JSAPI users may use JSRooted... types without having the class definition
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
356 JSRuntime
* const runtime_
;
358 /* The current compartment. */
359 JSCompartment
* compartment_
;
361 /* The current zone. */
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
);
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
];
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
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_
;
427 GetContextZone(const JSContext
* cx
)
429 return ContextFriendFields::get(cx
)->zone_
;
434 struct PerThreadDataFriendFields
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
440 struct RuntimeDummy
: JS::shadow::Runtime
442 struct PerThreadDummy
{
453 PerThreadDataFriendFields();
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
];
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
;
497 #endif /* jspubtd_h */