js/src/jspubtd.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/jspubtd.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,455 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99:
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef jspubtd_h
    1.11 +#define jspubtd_h
    1.12 +
    1.13 +/*
    1.14 + * JS public API typedefs.
    1.15 + */
    1.16 +
    1.17 +#include "mozilla/LinkedList.h"
    1.18 +#include "mozilla/NullPtr.h"
    1.19 +#include "mozilla/PodOperations.h"
    1.20 +
    1.21 +#include "jsprototypes.h"
    1.22 +#include "jstypes.h"
    1.23 +
    1.24 +#include "js/TypeDecls.h"
    1.25 +
    1.26 +#if defined(JSGC_USE_EXACT_ROOTING) || defined(JS_DEBUG)
    1.27 +# define JSGC_TRACK_EXACT_ROOTS
    1.28 +#endif
    1.29 +
    1.30 +namespace JS {
    1.31 +
    1.32 +class AutoIdVector;
    1.33 +class CallArgs;
    1.34 +
    1.35 +template <typename T>
    1.36 +class Rooted;
    1.37 +
    1.38 +class JS_PUBLIC_API(AutoGCRooter);
    1.39 +
    1.40 +class JS_FRIEND_API(CompileOptions);
    1.41 +class JS_FRIEND_API(ReadOnlyCompileOptions);
    1.42 +class JS_FRIEND_API(OwningCompileOptions);
    1.43 +class JS_PUBLIC_API(CompartmentOptions);
    1.44 +
    1.45 +struct Zone;
    1.46 +
    1.47 +} /* namespace JS */
    1.48 +
    1.49 +/*
    1.50 + * Run-time version enumeration.  For compile-time version checking, please use
    1.51 + * the JS_HAS_* macros in jsversion.h, or use MOZJS_MAJOR_VERSION,
    1.52 + * MOZJS_MINOR_VERSION, MOZJS_PATCH_VERSION, and MOZJS_ALPHA definitions.
    1.53 + */
    1.54 +enum JSVersion {
    1.55 +    JSVERSION_ECMA_3  = 148,
    1.56 +    JSVERSION_1_6     = 160,
    1.57 +    JSVERSION_1_7     = 170,
    1.58 +    JSVERSION_1_8     = 180,
    1.59 +    JSVERSION_ECMA_5  = 185,
    1.60 +    JSVERSION_DEFAULT = 0,
    1.61 +    JSVERSION_UNKNOWN = -1,
    1.62 +    JSVERSION_LATEST  = JSVERSION_ECMA_5
    1.63 +};
    1.64 +
    1.65 +/* Result of typeof operator enumeration. */
    1.66 +enum JSType {
    1.67 +    JSTYPE_VOID,                /* undefined */
    1.68 +    JSTYPE_OBJECT,              /* object */
    1.69 +    JSTYPE_FUNCTION,            /* function */
    1.70 +    JSTYPE_STRING,              /* string */
    1.71 +    JSTYPE_NUMBER,              /* number */
    1.72 +    JSTYPE_BOOLEAN,             /* boolean */
    1.73 +    JSTYPE_NULL,                /* null */
    1.74 +    JSTYPE_LIMIT
    1.75 +};
    1.76 +
    1.77 +/* Dense index into cached prototypes and class atoms for standard objects. */
    1.78 +enum JSProtoKey {
    1.79 +#define PROTOKEY_AND_INITIALIZER(name,code,init,clasp) JSProto_##name = code,
    1.80 +    JS_FOR_EACH_PROTOTYPE(PROTOKEY_AND_INITIALIZER)
    1.81 +#undef PROTOKEY_AND_INITIALIZER
    1.82 +    JSProto_LIMIT
    1.83 +};
    1.84 +
    1.85 +/*
    1.86 + * This enum type is used to control the behavior of a JSObject property
    1.87 + * iterator function that has type JSNewEnumerate.
    1.88 + */
    1.89 +enum JSIterateOp {
    1.90 +    /* Create new iterator state over enumerable properties. */
    1.91 +    JSENUMERATE_INIT,
    1.92 +
    1.93 +    /* Create new iterator state over all properties. */
    1.94 +    JSENUMERATE_INIT_ALL,
    1.95 +
    1.96 +    /* Iterate once. */
    1.97 +    JSENUMERATE_NEXT,
    1.98 +
    1.99 +    /* Destroy iterator state. */
   1.100 +    JSENUMERATE_DESTROY
   1.101 +};
   1.102 +
   1.103 +/* See Value::gcKind() and JSTraceCallback in Tracer.h. */
   1.104 +enum JSGCTraceKind {
   1.105 +    JSTRACE_OBJECT,
   1.106 +    JSTRACE_STRING,
   1.107 +    JSTRACE_SCRIPT,
   1.108 +
   1.109 +    /*
   1.110 +     * Trace kinds internal to the engine. The embedding can only see them if
   1.111 +     * it implements JSTraceCallback.
   1.112 +     */
   1.113 +    JSTRACE_LAZY_SCRIPT,
   1.114 +    JSTRACE_JITCODE,
   1.115 +    JSTRACE_SHAPE,
   1.116 +    JSTRACE_BASE_SHAPE,
   1.117 +    JSTRACE_TYPE_OBJECT,
   1.118 +    JSTRACE_LAST = JSTRACE_TYPE_OBJECT
   1.119 +};
   1.120 +
   1.121 +/* Struct forward declarations. */
   1.122 +struct JSClass;
   1.123 +struct JSCompartment;
   1.124 +struct JSConstDoubleSpec;
   1.125 +struct JSCrossCompartmentCall;
   1.126 +struct JSErrorReport;
   1.127 +struct JSExceptionState;
   1.128 +struct JSFunctionSpec;
   1.129 +struct JSIdArray;
   1.130 +struct JSLocaleCallbacks;
   1.131 +struct JSObjectMap;
   1.132 +struct JSPrincipals;
   1.133 +struct JSPropertyDescriptor;
   1.134 +struct JSPropertyName;
   1.135 +struct JSPropertySpec;
   1.136 +struct JSRuntime;
   1.137 +struct JSSecurityCallbacks;
   1.138 +struct JSStructuredCloneCallbacks;
   1.139 +struct JSStructuredCloneReader;
   1.140 +struct JSStructuredCloneWriter;
   1.141 +class JS_PUBLIC_API(JSTracer);
   1.142 +
   1.143 +class JSFlatString;
   1.144 +
   1.145 +#ifdef JS_THREADSAFE
   1.146 +typedef struct PRCallOnceType   JSCallOnceType;
   1.147 +#else
   1.148 +typedef bool                    JSCallOnceType;
   1.149 +#endif
   1.150 +typedef bool                    (*JSInitCallback)(void);
   1.151 +
   1.152 +/*
   1.153 + * Generic trace operation that calls JS_CallTracer on each traceable thing
   1.154 + * stored in data.
   1.155 + */
   1.156 +typedef void
   1.157 +(* JSTraceDataOp)(JSTracer *trc, void *data);
   1.158 +
   1.159 +void js_FinishGC(JSRuntime *rt);
   1.160 +
   1.161 +namespace js {
   1.162 +namespace gc {
   1.163 +class StoreBuffer;
   1.164 +void MarkPersistentRootedChains(JSTracer *);
   1.165 +}
   1.166 +}
   1.167 +
   1.168 +namespace JS {
   1.169 +
   1.170 +typedef void (*OffThreadCompileCallback)(void *token, void *callbackData);
   1.171 +
   1.172 +namespace shadow {
   1.173 +
   1.174 +struct Runtime
   1.175 +{
   1.176 +    /* Restrict zone access during Minor GC. */
   1.177 +    bool needsBarrier_;
   1.178 +
   1.179 +#ifdef JSGC_GENERATIONAL
   1.180 +    /* Allow inlining of Nursery::isInside. */
   1.181 +    uintptr_t gcNurseryStart_;
   1.182 +    uintptr_t gcNurseryEnd_;
   1.183 +
   1.184 +  private:
   1.185 +    js::gc::StoreBuffer *gcStoreBufferPtr_;
   1.186 +#endif
   1.187 +
   1.188 +  public:
   1.189 +    Runtime(
   1.190 +#ifdef JSGC_GENERATIONAL
   1.191 +        js::gc::StoreBuffer *storeBuffer
   1.192 +#endif
   1.193 +    )
   1.194 +      : needsBarrier_(false)
   1.195 +#ifdef JSGC_GENERATIONAL
   1.196 +      , gcNurseryStart_(0)
   1.197 +      , gcNurseryEnd_(0)
   1.198 +      , gcStoreBufferPtr_(storeBuffer)
   1.199 +#endif
   1.200 +    {}
   1.201 +
   1.202 +    bool needsBarrier() const {
   1.203 +        return needsBarrier_;
   1.204 +    }
   1.205 +
   1.206 +#ifdef JSGC_GENERATIONAL
   1.207 +    js::gc::StoreBuffer *gcStoreBufferPtr() { return gcStoreBufferPtr_; }
   1.208 +#endif
   1.209 +
   1.210 +    static JS::shadow::Runtime *asShadowRuntime(JSRuntime *rt) {
   1.211 +        return reinterpret_cast<JS::shadow::Runtime*>(rt);
   1.212 +    }
   1.213 +
   1.214 +    /* Allow inlining of PersistentRooted constructors and destructors. */
   1.215 +  private:
   1.216 +    template <typename Referent> friend class JS::PersistentRooted;
   1.217 +    friend void js::gc::MarkPersistentRootedChains(JSTracer *);
   1.218 +    friend void ::js_FinishGC(JSRuntime *rt);
   1.219 +
   1.220 +    mozilla::LinkedList<PersistentRootedFunction> functionPersistentRooteds;
   1.221 +    mozilla::LinkedList<PersistentRootedId>       idPersistentRooteds;
   1.222 +    mozilla::LinkedList<PersistentRootedObject>   objectPersistentRooteds;
   1.223 +    mozilla::LinkedList<PersistentRootedScript>   scriptPersistentRooteds;
   1.224 +    mozilla::LinkedList<PersistentRootedString>   stringPersistentRooteds;
   1.225 +    mozilla::LinkedList<PersistentRootedValue>    valuePersistentRooteds;
   1.226 +
   1.227 +    /* Specializations of this return references to the appropriate list. */
   1.228 +    template<typename Referent>
   1.229 +    inline mozilla::LinkedList<PersistentRooted<Referent> > &getPersistentRootedList();
   1.230 +};
   1.231 +
   1.232 +template<>
   1.233 +inline mozilla::LinkedList<PersistentRootedFunction>
   1.234 +&Runtime::getPersistentRootedList<JSFunction *>() { return functionPersistentRooteds; }
   1.235 +
   1.236 +template<>
   1.237 +inline mozilla::LinkedList<PersistentRootedId>
   1.238 +&Runtime::getPersistentRootedList<jsid>() { return idPersistentRooteds; }
   1.239 +
   1.240 +template<>
   1.241 +inline mozilla::LinkedList<PersistentRootedObject>
   1.242 +&Runtime::getPersistentRootedList<JSObject *>() { return objectPersistentRooteds; }
   1.243 +
   1.244 +template<>
   1.245 +inline mozilla::LinkedList<PersistentRootedScript>
   1.246 +&Runtime::getPersistentRootedList<JSScript *>() { return scriptPersistentRooteds; }
   1.247 +
   1.248 +template<>
   1.249 +inline mozilla::LinkedList<PersistentRootedString>
   1.250 +&Runtime::getPersistentRootedList<JSString *>() { return stringPersistentRooteds; }
   1.251 +
   1.252 +template<>
   1.253 +inline mozilla::LinkedList<PersistentRootedValue>
   1.254 +&Runtime::getPersistentRootedList<Value>() { return valuePersistentRooteds; }
   1.255 +
   1.256 +} /* namespace shadow */
   1.257 +} /* namespace JS */
   1.258 +
   1.259 +namespace js {
   1.260 +
   1.261 +/*
   1.262 + * Parallel operations in general can have one of three states. They may
   1.263 + * succeed, fail, or "bail", where bail indicates that the code encountered an
   1.264 + * unexpected condition and should be re-run sequentially. Different
   1.265 + * subcategories of the "bail" state are encoded as variants of TP_RETRY_*.
   1.266 + */
   1.267 +enum ParallelResult { TP_SUCCESS, TP_RETRY_SEQUENTIALLY, TP_RETRY_AFTER_GC, TP_FATAL };
   1.268 +
   1.269 +struct ThreadSafeContext;
   1.270 +struct ForkJoinContext;
   1.271 +class ExclusiveContext;
   1.272 +
   1.273 +class Allocator;
   1.274 +
   1.275 +enum ThingRootKind
   1.276 +{
   1.277 +    THING_ROOT_OBJECT,
   1.278 +    THING_ROOT_SHAPE,
   1.279 +    THING_ROOT_BASE_SHAPE,
   1.280 +    THING_ROOT_TYPE_OBJECT,
   1.281 +    THING_ROOT_STRING,
   1.282 +    THING_ROOT_JIT_CODE,
   1.283 +    THING_ROOT_SCRIPT,
   1.284 +    THING_ROOT_LAZY_SCRIPT,
   1.285 +    THING_ROOT_ID,
   1.286 +    THING_ROOT_VALUE,
   1.287 +    THING_ROOT_TYPE,
   1.288 +    THING_ROOT_BINDINGS,
   1.289 +    THING_ROOT_PROPERTY_DESCRIPTOR,
   1.290 +    THING_ROOT_CUSTOM,
   1.291 +    THING_ROOT_LIMIT
   1.292 +};
   1.293 +
   1.294 +/*
   1.295 + * This list enumerates the different types of conceptual stacks we have in
   1.296 + * SpiderMonkey. In reality, they all share the C stack, but we allow different
   1.297 + * stack limits depending on the type of code running.
   1.298 + */
   1.299 +enum StackKind
   1.300 +{
   1.301 +    StackForSystemCode,      // C++, such as the GC, running on behalf of the VM.
   1.302 +    StackForTrustedScript,   // Script running with trusted principals.
   1.303 +    StackForUntrustedScript, // Script running with untrusted principals.
   1.304 +    StackKindCount
   1.305 +};
   1.306 +
   1.307 +template <typename T>
   1.308 +struct RootKind;
   1.309 +
   1.310 +/*
   1.311 + * Specifically mark the ThingRootKind of externally visible types, so that
   1.312 + * JSAPI users may use JSRooted... types without having the class definition
   1.313 + * available.
   1.314 + */
   1.315 +template<typename T, ThingRootKind Kind>
   1.316 +struct SpecificRootKind
   1.317 +{
   1.318 +    static ThingRootKind rootKind() { return Kind; }
   1.319 +};
   1.320 +
   1.321 +template <> struct RootKind<JSObject *> : SpecificRootKind<JSObject *, THING_ROOT_OBJECT> {};
   1.322 +template <> struct RootKind<JSFlatString *> : SpecificRootKind<JSFlatString *, THING_ROOT_STRING> {};
   1.323 +template <> struct RootKind<JSFunction *> : SpecificRootKind<JSFunction *, THING_ROOT_OBJECT> {};
   1.324 +template <> struct RootKind<JSString *> : SpecificRootKind<JSString *, THING_ROOT_STRING> {};
   1.325 +template <> struct RootKind<JSScript *> : SpecificRootKind<JSScript *, THING_ROOT_SCRIPT> {};
   1.326 +template <> struct RootKind<jsid> : SpecificRootKind<jsid, THING_ROOT_ID> {};
   1.327 +template <> struct RootKind<JS::Value> : SpecificRootKind<JS::Value, THING_ROOT_VALUE> {};
   1.328 +
   1.329 +struct ContextFriendFields
   1.330 +{
   1.331 +  protected:
   1.332 +    JSRuntime *const     runtime_;
   1.333 +
   1.334 +    /* The current compartment. */
   1.335 +    JSCompartment       *compartment_;
   1.336 +
   1.337 +    /* The current zone. */
   1.338 +    JS::Zone            *zone_;
   1.339 +
   1.340 +  public:
   1.341 +    explicit ContextFriendFields(JSRuntime *rt)
   1.342 +      : runtime_(rt), compartment_(nullptr), zone_(nullptr), autoGCRooters(nullptr)
   1.343 +    {
   1.344 +#ifdef JSGC_TRACK_EXACT_ROOTS
   1.345 +        mozilla::PodArrayZero(thingGCRooters);
   1.346 +#endif
   1.347 +    }
   1.348 +
   1.349 +    static const ContextFriendFields *get(const JSContext *cx) {
   1.350 +        return reinterpret_cast<const ContextFriendFields *>(cx);
   1.351 +    }
   1.352 +
   1.353 +    static ContextFriendFields *get(JSContext *cx) {
   1.354 +        return reinterpret_cast<ContextFriendFields *>(cx);
   1.355 +    }
   1.356 +
   1.357 +#ifdef JSGC_TRACK_EXACT_ROOTS
   1.358 +    /*
   1.359 +     * Stack allocated GC roots for stack GC heap pointers, which may be
   1.360 +     * overwritten if moved during a GC.
   1.361 +     */
   1.362 +    JS::Rooted<void*> *thingGCRooters[THING_ROOT_LIMIT];
   1.363 +#endif
   1.364 +
   1.365 +    /* Stack of thread-stack-allocated GC roots. */
   1.366 +    JS::AutoGCRooter   *autoGCRooters;
   1.367 +
   1.368 +    friend JSRuntime *GetRuntime(const JSContext *cx);
   1.369 +    friend JSCompartment *GetContextCompartment(const JSContext *cx);
   1.370 +    friend JS::Zone *GetContextZone(const JSContext *cx);
   1.371 +};
   1.372 +
   1.373 +/*
   1.374 + * Inlinable accessors for JSContext.
   1.375 + *
   1.376 + * - These must not be available on the more restricted superclasses of
   1.377 + *   JSContext, so we can't simply define them on ContextFriendFields.
   1.378 + *
   1.379 + * - They're perfectly ordinary JSContext functionality, so ought to be
   1.380 + *   usable without resorting to jsfriendapi.h, and when JSContext is an
   1.381 + *   incomplete type.
   1.382 + */
   1.383 +inline JSRuntime *
   1.384 +GetRuntime(const JSContext *cx)
   1.385 +{
   1.386 +    return ContextFriendFields::get(cx)->runtime_;
   1.387 +}
   1.388 +
   1.389 +inline JSCompartment *
   1.390 +GetContextCompartment(const JSContext *cx)
   1.391 +{
   1.392 +    return ContextFriendFields::get(cx)->compartment_;
   1.393 +}
   1.394 +
   1.395 +inline JS::Zone *
   1.396 +GetContextZone(const JSContext *cx)
   1.397 +{
   1.398 +    return ContextFriendFields::get(cx)->zone_;
   1.399 +}
   1.400 +
   1.401 +class PerThreadData;
   1.402 +
   1.403 +struct PerThreadDataFriendFields
   1.404 +{
   1.405 +  private:
   1.406 +    // Note: this type only exists to permit us to derive the offset of
   1.407 +    // the perThread data within the real JSRuntime* type in a portable
   1.408 +    // way.
   1.409 +    struct RuntimeDummy : JS::shadow::Runtime
   1.410 +    {
   1.411 +        struct PerThreadDummy {
   1.412 +            void *field1;
   1.413 +            uintptr_t field2;
   1.414 +#ifdef JS_DEBUG
   1.415 +            uint64_t field3;
   1.416 +#endif
   1.417 +        } mainThread;
   1.418 +    };
   1.419 +
   1.420 +  public:
   1.421 +
   1.422 +    PerThreadDataFriendFields();
   1.423 +
   1.424 +#ifdef JSGC_TRACK_EXACT_ROOTS
   1.425 +    /*
   1.426 +     * Stack allocated GC roots for stack GC heap pointers, which may be
   1.427 +     * overwritten if moved during a GC.
   1.428 +     */
   1.429 +    JS::Rooted<void*> *thingGCRooters[THING_ROOT_LIMIT];
   1.430 +#endif
   1.431 +
   1.432 +    /* Limit pointer for checking native stack consumption. */
   1.433 +    uintptr_t nativeStackLimit[StackKindCount];
   1.434 +
   1.435 +    static const size_t RuntimeMainThreadOffset = offsetof(RuntimeDummy, mainThread);
   1.436 +
   1.437 +    static inline PerThreadDataFriendFields *get(js::PerThreadData *pt) {
   1.438 +        return reinterpret_cast<PerThreadDataFriendFields *>(pt);
   1.439 +    }
   1.440 +
   1.441 +    static inline PerThreadDataFriendFields *getMainThread(JSRuntime *rt) {
   1.442 +        // mainThread must always appear directly after |JS::shadow::Runtime|.
   1.443 +        // Tested by a JS_STATIC_ASSERT in |jsfriendapi.cpp|
   1.444 +        return reinterpret_cast<PerThreadDataFriendFields *>(
   1.445 +            reinterpret_cast<char*>(rt) + RuntimeMainThreadOffset);
   1.446 +    }
   1.447 +
   1.448 +    static inline const PerThreadDataFriendFields *getMainThread(const JSRuntime *rt) {
   1.449 +        // mainThread must always appear directly after |JS::shadow::Runtime|.
   1.450 +        // Tested by a JS_STATIC_ASSERT in |jsfriendapi.cpp|
   1.451 +        return reinterpret_cast<const PerThreadDataFriendFields *>(
   1.452 +            reinterpret_cast<const char*>(rt) + RuntimeMainThreadOffset);
   1.453 +    }
   1.454 +};
   1.455 +
   1.456 +} /* namespace js */
   1.457 +
   1.458 +#endif /* jspubtd_h */

mercurial