js/src/jspubtd.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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
    10 /*
    11  * JS public API typedefs.
    12  */
    14 #include "mozilla/LinkedList.h"
    15 #include "mozilla/NullPtr.h"
    16 #include "mozilla/PodOperations.h"
    18 #include "jsprototypes.h"
    19 #include "jstypes.h"
    21 #include "js/TypeDecls.h"
    23 #if defined(JSGC_USE_EXACT_ROOTING) || defined(JS_DEBUG)
    24 # define JSGC_TRACK_EXACT_ROOTS
    25 #endif
    27 namespace JS {
    29 class AutoIdVector;
    30 class CallArgs;
    32 template <typename T>
    33 class Rooted;
    35 class JS_PUBLIC_API(AutoGCRooter);
    37 class JS_FRIEND_API(CompileOptions);
    38 class JS_FRIEND_API(ReadOnlyCompileOptions);
    39 class JS_FRIEND_API(OwningCompileOptions);
    40 class JS_PUBLIC_API(CompartmentOptions);
    42 struct Zone;
    44 } /* namespace JS */
    46 /*
    47  * Run-time version enumeration.  For compile-time version checking, please use
    48  * the JS_HAS_* macros in jsversion.h, or use MOZJS_MAJOR_VERSION,
    49  * MOZJS_MINOR_VERSION, MOZJS_PATCH_VERSION, and MOZJS_ALPHA definitions.
    50  */
    51 enum JSVersion {
    52     JSVERSION_ECMA_3  = 148,
    53     JSVERSION_1_6     = 160,
    54     JSVERSION_1_7     = 170,
    55     JSVERSION_1_8     = 180,
    56     JSVERSION_ECMA_5  = 185,
    57     JSVERSION_DEFAULT = 0,
    58     JSVERSION_UNKNOWN = -1,
    59     JSVERSION_LATEST  = JSVERSION_ECMA_5
    60 };
    62 /* Result of typeof operator enumeration. */
    63 enum JSType {
    64     JSTYPE_VOID,                /* undefined */
    65     JSTYPE_OBJECT,              /* object */
    66     JSTYPE_FUNCTION,            /* function */
    67     JSTYPE_STRING,              /* string */
    68     JSTYPE_NUMBER,              /* number */
    69     JSTYPE_BOOLEAN,             /* boolean */
    70     JSTYPE_NULL,                /* null */
    71     JSTYPE_LIMIT
    72 };
    74 /* Dense index into cached prototypes and class atoms for standard objects. */
    75 enum JSProtoKey {
    76 #define PROTOKEY_AND_INITIALIZER(name,code,init,clasp) JSProto_##name = code,
    77     JS_FOR_EACH_PROTOTYPE(PROTOKEY_AND_INITIALIZER)
    78 #undef PROTOKEY_AND_INITIALIZER
    79     JSProto_LIMIT
    80 };
    82 /*
    83  * This enum type is used to control the behavior of a JSObject property
    84  * iterator function that has type JSNewEnumerate.
    85  */
    86 enum JSIterateOp {
    87     /* Create new iterator state over enumerable properties. */
    88     JSENUMERATE_INIT,
    90     /* Create new iterator state over all properties. */
    91     JSENUMERATE_INIT_ALL,
    93     /* Iterate once. */
    94     JSENUMERATE_NEXT,
    96     /* Destroy iterator state. */
    97     JSENUMERATE_DESTROY
    98 };
   100 /* See Value::gcKind() and JSTraceCallback in Tracer.h. */
   101 enum JSGCTraceKind {
   102     JSTRACE_OBJECT,
   103     JSTRACE_STRING,
   104     JSTRACE_SCRIPT,
   106     /*
   107      * Trace kinds internal to the engine. The embedding can only see them if
   108      * it implements JSTraceCallback.
   109      */
   110     JSTRACE_LAZY_SCRIPT,
   111     JSTRACE_JITCODE,
   112     JSTRACE_SHAPE,
   113     JSTRACE_BASE_SHAPE,
   114     JSTRACE_TYPE_OBJECT,
   115     JSTRACE_LAST = JSTRACE_TYPE_OBJECT
   116 };
   118 /* Struct forward declarations. */
   119 struct JSClass;
   120 struct JSCompartment;
   121 struct JSConstDoubleSpec;
   122 struct JSCrossCompartmentCall;
   123 struct JSErrorReport;
   124 struct JSExceptionState;
   125 struct JSFunctionSpec;
   126 struct JSIdArray;
   127 struct JSLocaleCallbacks;
   128 struct JSObjectMap;
   129 struct JSPrincipals;
   130 struct JSPropertyDescriptor;
   131 struct JSPropertyName;
   132 struct JSPropertySpec;
   133 struct JSRuntime;
   134 struct JSSecurityCallbacks;
   135 struct JSStructuredCloneCallbacks;
   136 struct JSStructuredCloneReader;
   137 struct JSStructuredCloneWriter;
   138 class JS_PUBLIC_API(JSTracer);
   140 class JSFlatString;
   142 #ifdef JS_THREADSAFE
   143 typedef struct PRCallOnceType   JSCallOnceType;
   144 #else
   145 typedef bool                    JSCallOnceType;
   146 #endif
   147 typedef bool                    (*JSInitCallback)(void);
   149 /*
   150  * Generic trace operation that calls JS_CallTracer on each traceable thing
   151  * stored in data.
   152  */
   153 typedef void
   154 (* JSTraceDataOp)(JSTracer *trc, void *data);
   156 void js_FinishGC(JSRuntime *rt);
   158 namespace js {
   159 namespace gc {
   160 class StoreBuffer;
   161 void MarkPersistentRootedChains(JSTracer *);
   162 }
   163 }
   165 namespace JS {
   167 typedef void (*OffThreadCompileCallback)(void *token, void *callbackData);
   169 namespace shadow {
   171 struct Runtime
   172 {
   173     /* Restrict zone access during Minor GC. */
   174     bool needsBarrier_;
   176 #ifdef JSGC_GENERATIONAL
   177     /* Allow inlining of Nursery::isInside. */
   178     uintptr_t gcNurseryStart_;
   179     uintptr_t gcNurseryEnd_;
   181   private:
   182     js::gc::StoreBuffer *gcStoreBufferPtr_;
   183 #endif
   185   public:
   186     Runtime(
   187 #ifdef JSGC_GENERATIONAL
   188         js::gc::StoreBuffer *storeBuffer
   189 #endif
   190     )
   191       : needsBarrier_(false)
   192 #ifdef JSGC_GENERATIONAL
   193       , gcNurseryStart_(0)
   194       , gcNurseryEnd_(0)
   195       , gcStoreBufferPtr_(storeBuffer)
   196 #endif
   197     {}
   199     bool needsBarrier() const {
   200         return needsBarrier_;
   201     }
   203 #ifdef JSGC_GENERATIONAL
   204     js::gc::StoreBuffer *gcStoreBufferPtr() { return gcStoreBufferPtr_; }
   205 #endif
   207     static JS::shadow::Runtime *asShadowRuntime(JSRuntime *rt) {
   208         return reinterpret_cast<JS::shadow::Runtime*>(rt);
   209     }
   211     /* Allow inlining of PersistentRooted constructors and destructors. */
   212   private:
   213     template <typename Referent> friend class JS::PersistentRooted;
   214     friend void js::gc::MarkPersistentRootedChains(JSTracer *);
   215     friend void ::js_FinishGC(JSRuntime *rt);
   217     mozilla::LinkedList<PersistentRootedFunction> functionPersistentRooteds;
   218     mozilla::LinkedList<PersistentRootedId>       idPersistentRooteds;
   219     mozilla::LinkedList<PersistentRootedObject>   objectPersistentRooteds;
   220     mozilla::LinkedList<PersistentRootedScript>   scriptPersistentRooteds;
   221     mozilla::LinkedList<PersistentRootedString>   stringPersistentRooteds;
   222     mozilla::LinkedList<PersistentRootedValue>    valuePersistentRooteds;
   224     /* Specializations of this return references to the appropriate list. */
   225     template<typename Referent>
   226     inline mozilla::LinkedList<PersistentRooted<Referent> > &getPersistentRootedList();
   227 };
   229 template<>
   230 inline mozilla::LinkedList<PersistentRootedFunction>
   231 &Runtime::getPersistentRootedList<JSFunction *>() { return functionPersistentRooteds; }
   233 template<>
   234 inline mozilla::LinkedList<PersistentRootedId>
   235 &Runtime::getPersistentRootedList<jsid>() { return idPersistentRooteds; }
   237 template<>
   238 inline mozilla::LinkedList<PersistentRootedObject>
   239 &Runtime::getPersistentRootedList<JSObject *>() { return objectPersistentRooteds; }
   241 template<>
   242 inline mozilla::LinkedList<PersistentRootedScript>
   243 &Runtime::getPersistentRootedList<JSScript *>() { return scriptPersistentRooteds; }
   245 template<>
   246 inline mozilla::LinkedList<PersistentRootedString>
   247 &Runtime::getPersistentRootedList<JSString *>() { return stringPersistentRooteds; }
   249 template<>
   250 inline mozilla::LinkedList<PersistentRootedValue>
   251 &Runtime::getPersistentRootedList<Value>() { return valuePersistentRooteds; }
   253 } /* namespace shadow */
   254 } /* namespace JS */
   256 namespace js {
   258 /*
   259  * Parallel operations in general can have one of three states. They may
   260  * succeed, fail, or "bail", where bail indicates that the code encountered an
   261  * unexpected condition and should be re-run sequentially. Different
   262  * subcategories of the "bail" state are encoded as variants of TP_RETRY_*.
   263  */
   264 enum ParallelResult { TP_SUCCESS, TP_RETRY_SEQUENTIALLY, TP_RETRY_AFTER_GC, TP_FATAL };
   266 struct ThreadSafeContext;
   267 struct ForkJoinContext;
   268 class ExclusiveContext;
   270 class Allocator;
   272 enum ThingRootKind
   273 {
   274     THING_ROOT_OBJECT,
   275     THING_ROOT_SHAPE,
   276     THING_ROOT_BASE_SHAPE,
   277     THING_ROOT_TYPE_OBJECT,
   278     THING_ROOT_STRING,
   279     THING_ROOT_JIT_CODE,
   280     THING_ROOT_SCRIPT,
   281     THING_ROOT_LAZY_SCRIPT,
   282     THING_ROOT_ID,
   283     THING_ROOT_VALUE,
   284     THING_ROOT_TYPE,
   285     THING_ROOT_BINDINGS,
   286     THING_ROOT_PROPERTY_DESCRIPTOR,
   287     THING_ROOT_CUSTOM,
   288     THING_ROOT_LIMIT
   289 };
   291 /*
   292  * This list enumerates the different types of conceptual stacks we have in
   293  * SpiderMonkey. In reality, they all share the C stack, but we allow different
   294  * stack limits depending on the type of code running.
   295  */
   296 enum StackKind
   297 {
   298     StackForSystemCode,      // C++, such as the GC, running on behalf of the VM.
   299     StackForTrustedScript,   // Script running with trusted principals.
   300     StackForUntrustedScript, // Script running with untrusted principals.
   301     StackKindCount
   302 };
   304 template <typename T>
   305 struct RootKind;
   307 /*
   308  * Specifically mark the ThingRootKind of externally visible types, so that
   309  * JSAPI users may use JSRooted... types without having the class definition
   310  * available.
   311  */
   312 template<typename T, ThingRootKind Kind>
   313 struct SpecificRootKind
   314 {
   315     static ThingRootKind rootKind() { return Kind; }
   316 };
   318 template <> struct RootKind<JSObject *> : SpecificRootKind<JSObject *, THING_ROOT_OBJECT> {};
   319 template <> struct RootKind<JSFlatString *> : SpecificRootKind<JSFlatString *, THING_ROOT_STRING> {};
   320 template <> struct RootKind<JSFunction *> : SpecificRootKind<JSFunction *, THING_ROOT_OBJECT> {};
   321 template <> struct RootKind<JSString *> : SpecificRootKind<JSString *, THING_ROOT_STRING> {};
   322 template <> struct RootKind<JSScript *> : SpecificRootKind<JSScript *, THING_ROOT_SCRIPT> {};
   323 template <> struct RootKind<jsid> : SpecificRootKind<jsid, THING_ROOT_ID> {};
   324 template <> struct RootKind<JS::Value> : SpecificRootKind<JS::Value, THING_ROOT_VALUE> {};
   326 struct ContextFriendFields
   327 {
   328   protected:
   329     JSRuntime *const     runtime_;
   331     /* The current compartment. */
   332     JSCompartment       *compartment_;
   334     /* The current zone. */
   335     JS::Zone            *zone_;
   337   public:
   338     explicit ContextFriendFields(JSRuntime *rt)
   339       : runtime_(rt), compartment_(nullptr), zone_(nullptr), autoGCRooters(nullptr)
   340     {
   341 #ifdef JSGC_TRACK_EXACT_ROOTS
   342         mozilla::PodArrayZero(thingGCRooters);
   343 #endif
   344     }
   346     static const ContextFriendFields *get(const JSContext *cx) {
   347         return reinterpret_cast<const ContextFriendFields *>(cx);
   348     }
   350     static ContextFriendFields *get(JSContext *cx) {
   351         return reinterpret_cast<ContextFriendFields *>(cx);
   352     }
   354 #ifdef JSGC_TRACK_EXACT_ROOTS
   355     /*
   356      * Stack allocated GC roots for stack GC heap pointers, which may be
   357      * overwritten if moved during a GC.
   358      */
   359     JS::Rooted<void*> *thingGCRooters[THING_ROOT_LIMIT];
   360 #endif
   362     /* Stack of thread-stack-allocated GC roots. */
   363     JS::AutoGCRooter   *autoGCRooters;
   365     friend JSRuntime *GetRuntime(const JSContext *cx);
   366     friend JSCompartment *GetContextCompartment(const JSContext *cx);
   367     friend JS::Zone *GetContextZone(const JSContext *cx);
   368 };
   370 /*
   371  * Inlinable accessors for JSContext.
   372  *
   373  * - These must not be available on the more restricted superclasses of
   374  *   JSContext, so we can't simply define them on ContextFriendFields.
   375  *
   376  * - They're perfectly ordinary JSContext functionality, so ought to be
   377  *   usable without resorting to jsfriendapi.h, and when JSContext is an
   378  *   incomplete type.
   379  */
   380 inline JSRuntime *
   381 GetRuntime(const JSContext *cx)
   382 {
   383     return ContextFriendFields::get(cx)->runtime_;
   384 }
   386 inline JSCompartment *
   387 GetContextCompartment(const JSContext *cx)
   388 {
   389     return ContextFriendFields::get(cx)->compartment_;
   390 }
   392 inline JS::Zone *
   393 GetContextZone(const JSContext *cx)
   394 {
   395     return ContextFriendFields::get(cx)->zone_;
   396 }
   398 class PerThreadData;
   400 struct PerThreadDataFriendFields
   401 {
   402   private:
   403     // Note: this type only exists to permit us to derive the offset of
   404     // the perThread data within the real JSRuntime* type in a portable
   405     // way.
   406     struct RuntimeDummy : JS::shadow::Runtime
   407     {
   408         struct PerThreadDummy {
   409             void *field1;
   410             uintptr_t field2;
   411 #ifdef JS_DEBUG
   412             uint64_t field3;
   413 #endif
   414         } mainThread;
   415     };
   417   public:
   419     PerThreadDataFriendFields();
   421 #ifdef JSGC_TRACK_EXACT_ROOTS
   422     /*
   423      * Stack allocated GC roots for stack GC heap pointers, which may be
   424      * overwritten if moved during a GC.
   425      */
   426     JS::Rooted<void*> *thingGCRooters[THING_ROOT_LIMIT];
   427 #endif
   429     /* Limit pointer for checking native stack consumption. */
   430     uintptr_t nativeStackLimit[StackKindCount];
   432     static const size_t RuntimeMainThreadOffset = offsetof(RuntimeDummy, mainThread);
   434     static inline PerThreadDataFriendFields *get(js::PerThreadData *pt) {
   435         return reinterpret_cast<PerThreadDataFriendFields *>(pt);
   436     }
   438     static inline PerThreadDataFriendFields *getMainThread(JSRuntime *rt) {
   439         // mainThread must always appear directly after |JS::shadow::Runtime|.
   440         // Tested by a JS_STATIC_ASSERT in |jsfriendapi.cpp|
   441         return reinterpret_cast<PerThreadDataFriendFields *>(
   442             reinterpret_cast<char*>(rt) + RuntimeMainThreadOffset);
   443     }
   445     static inline const PerThreadDataFriendFields *getMainThread(const JSRuntime *rt) {
   446         // mainThread must always appear directly after |JS::shadow::Runtime|.
   447         // Tested by a JS_STATIC_ASSERT in |jsfriendapi.cpp|
   448         return reinterpret_cast<const PerThreadDataFriendFields *>(
   449             reinterpret_cast<const char*>(rt) + RuntimeMainThreadOffset);
   450     }
   451 };
   453 } /* namespace js */
   455 #endif /* jspubtd_h */

mercurial