js/src/jsapi.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/jsapi.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,5044 @@
     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 +/* JavaScript API. */
    1.11 +
    1.12 +#ifndef jsapi_h
    1.13 +#define jsapi_h
    1.14 +
    1.15 +#include "mozilla/FloatingPoint.h"
    1.16 +#include "mozilla/MemoryReporting.h"
    1.17 +#include "mozilla/RangedPtr.h"
    1.18 +
    1.19 +#include <stdarg.h>
    1.20 +#include <stddef.h>
    1.21 +#include <stdint.h>
    1.22 +#include <stdio.h>
    1.23 +
    1.24 +#include "jsalloc.h"
    1.25 +#include "jspubtd.h"
    1.26 +
    1.27 +#include "js/CallArgs.h"
    1.28 +#include "js/Class.h"
    1.29 +#include "js/HashTable.h"
    1.30 +#include "js/Id.h"
    1.31 +#include "js/Principals.h"
    1.32 +#include "js/RootingAPI.h"
    1.33 +#include "js/TracingAPI.h"
    1.34 +#include "js/Utility.h"
    1.35 +#include "js/Value.h"
    1.36 +#include "js/Vector.h"
    1.37 +
    1.38 +/************************************************************************/
    1.39 +
    1.40 +namespace JS {
    1.41 +
    1.42 +class Latin1CharsZ;
    1.43 +class TwoByteChars;
    1.44 +
    1.45 +#if defined JS_THREADSAFE && defined JS_DEBUG
    1.46 +
    1.47 +class JS_PUBLIC_API(AutoCheckRequestDepth)
    1.48 +{
    1.49 +    JSContext *cx;
    1.50 +  public:
    1.51 +    AutoCheckRequestDepth(JSContext *cx);
    1.52 +    AutoCheckRequestDepth(js::ContextFriendFields *cx);
    1.53 +    ~AutoCheckRequestDepth();
    1.54 +};
    1.55 +
    1.56 +# define CHECK_REQUEST(cx) \
    1.57 +    JS::AutoCheckRequestDepth _autoCheckRequestDepth(cx)
    1.58 +
    1.59 +#else
    1.60 +
    1.61 +# define CHECK_REQUEST(cx) \
    1.62 +    ((void) 0)
    1.63 +
    1.64 +#endif /* JS_THREADSAFE && JS_DEBUG */
    1.65 +
    1.66 +#ifdef JS_DEBUG
    1.67 +/*
    1.68 + * Assert that we're not doing GC on cx, that we're in a request as
    1.69 + * needed, and that the compartments for cx and v are correct.
    1.70 + * Also check that GC would be safe at this point.
    1.71 + */
    1.72 +JS_PUBLIC_API(void)
    1.73 +AssertArgumentsAreSane(JSContext *cx, JS::HandleValue v);
    1.74 +#else
    1.75 +inline void AssertArgumentsAreSane(JSContext *cx, JS::HandleValue v) {
    1.76 +    /* Do nothing */
    1.77 +}
    1.78 +#endif /* JS_DEBUG */
    1.79 +
    1.80 +class JS_PUBLIC_API(AutoGCRooter) {
    1.81 +  public:
    1.82 +    AutoGCRooter(JSContext *cx, ptrdiff_t tag);
    1.83 +    AutoGCRooter(js::ContextFriendFields *cx, ptrdiff_t tag);
    1.84 +
    1.85 +    ~AutoGCRooter() {
    1.86 +        JS_ASSERT(this == *stackTop);
    1.87 +        *stackTop = down;
    1.88 +    }
    1.89 +
    1.90 +    /* Implemented in gc/RootMarking.cpp. */
    1.91 +    inline void trace(JSTracer *trc);
    1.92 +    static void traceAll(JSTracer *trc);
    1.93 +    static void traceAllWrappers(JSTracer *trc);
    1.94 +
    1.95 +  protected:
    1.96 +    AutoGCRooter * const down;
    1.97 +
    1.98 +    /*
    1.99 +     * Discriminates actual subclass of this being used.  If non-negative, the
   1.100 +     * subclass roots an array of values of the length stored in this field.
   1.101 +     * If negative, meaning is indicated by the corresponding value in the enum
   1.102 +     * below.  Any other negative value indicates some deeper problem such as
   1.103 +     * memory corruption.
   1.104 +     */
   1.105 +    ptrdiff_t tag_;
   1.106 +
   1.107 +    enum {
   1.108 +        VALARRAY =     -2, /* js::AutoValueArray */
   1.109 +        PARSER =       -3, /* js::frontend::Parser */
   1.110 +        SHAPEVECTOR =  -4, /* js::AutoShapeVector */
   1.111 +        IDARRAY =      -6, /* js::AutoIdArray */
   1.112 +        DESCRIPTORS =  -7, /* js::AutoPropDescArrayRooter */
   1.113 +        ID =           -9, /* js::AutoIdRooter */
   1.114 +        VALVECTOR =   -10, /* js::AutoValueVector */
   1.115 +        IDVECTOR =    -13, /* js::AutoIdVector */
   1.116 +        OBJVECTOR =   -14, /* js::AutoObjectVector */
   1.117 +        STRINGVECTOR =-15, /* js::AutoStringVector */
   1.118 +        SCRIPTVECTOR =-16, /* js::AutoScriptVector */
   1.119 +        NAMEVECTOR =  -17, /* js::AutoNameVector */
   1.120 +        HASHABLEVALUE=-18, /* js::HashableValue */
   1.121 +        IONMASM =     -19, /* js::jit::MacroAssembler */
   1.122 +        IONALLOC =    -20, /* js::jit::AutoTempAllocatorRooter */
   1.123 +        WRAPVECTOR =  -21, /* js::AutoWrapperVector */
   1.124 +        WRAPPER =     -22, /* js::AutoWrapperRooter */
   1.125 +        OBJOBJHASHMAP=-23, /* js::AutoObjectObjectHashMap */
   1.126 +        OBJU32HASHMAP=-24, /* js::AutoObjectUnsigned32HashMap */
   1.127 +        OBJHASHSET =  -25, /* js::AutoObjectHashSet */
   1.128 +        JSONPARSER =  -26, /* js::JSONParser */
   1.129 +        CUSTOM =      -27, /* js::CustomAutoRooter */
   1.130 +        FUNVECTOR =   -28  /* js::AutoFunctionVector */
   1.131 +    };
   1.132 +
   1.133 +  private:
   1.134 +    AutoGCRooter ** const stackTop;
   1.135 +
   1.136 +    /* No copy or assignment semantics. */
   1.137 +    AutoGCRooter(AutoGCRooter &ida) MOZ_DELETE;
   1.138 +    void operator=(AutoGCRooter &ida) MOZ_DELETE;
   1.139 +};
   1.140 +
   1.141 +/* AutoValueArray roots an internal fixed-size array of Values. */
   1.142 +template <size_t N>
   1.143 +class AutoValueArray : public AutoGCRooter
   1.144 +{
   1.145 +    const size_t length_;
   1.146 +    Value elements_[N];
   1.147 +
   1.148 +  public:
   1.149 +    AutoValueArray(JSContext *cx
   1.150 +                   MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
   1.151 +      : AutoGCRooter(cx, VALARRAY), length_(N)
   1.152 +    {
   1.153 +        /* Always initialize in case we GC before assignment. */
   1.154 +        mozilla::PodArrayZero(elements_);
   1.155 +        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
   1.156 +    }
   1.157 +
   1.158 +    unsigned length() const { return length_; }
   1.159 +    const Value *begin() const { return elements_; }
   1.160 +    Value *begin() { return elements_; }
   1.161 +
   1.162 +    HandleValue operator[](unsigned i) const {
   1.163 +        JS_ASSERT(i < N);
   1.164 +        return HandleValue::fromMarkedLocation(&elements_[i]);
   1.165 +    }
   1.166 +    MutableHandleValue operator[](unsigned i) {
   1.167 +        JS_ASSERT(i < N);
   1.168 +        return MutableHandleValue::fromMarkedLocation(&elements_[i]);
   1.169 +    }
   1.170 +
   1.171 +    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
   1.172 +};
   1.173 +
   1.174 +template<class T>
   1.175 +class AutoVectorRooter : protected AutoGCRooter
   1.176 +{
   1.177 +    typedef js::Vector<T, 8> VectorImpl;
   1.178 +    VectorImpl vector;
   1.179 +
   1.180 +  public:
   1.181 +    explicit AutoVectorRooter(JSContext *cx, ptrdiff_t tag
   1.182 +                              MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
   1.183 +      : AutoGCRooter(cx, tag), vector(cx)
   1.184 +    {
   1.185 +        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
   1.186 +    }
   1.187 +
   1.188 +    explicit AutoVectorRooter(js::ContextFriendFields *cx, ptrdiff_t tag
   1.189 +                              MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
   1.190 +      : AutoGCRooter(cx, tag), vector(cx)
   1.191 +    {
   1.192 +        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
   1.193 +    }
   1.194 +
   1.195 +    typedef T ElementType;
   1.196 +    typedef typename VectorImpl::Range Range;
   1.197 +
   1.198 +    size_t length() const { return vector.length(); }
   1.199 +    bool empty() const { return vector.empty(); }
   1.200 +
   1.201 +    bool append(const T &v) { return vector.append(v); }
   1.202 +    bool append(const T *ptr, size_t len) { return vector.append(ptr, len); }
   1.203 +    bool appendAll(const AutoVectorRooter<T> &other) {
   1.204 +        return vector.appendAll(other.vector);
   1.205 +    }
   1.206 +
   1.207 +    bool insert(T *p, const T &val) { return vector.insert(p, val); }
   1.208 +
   1.209 +    /* For use when space has already been reserved. */
   1.210 +    void infallibleAppend(const T &v) { vector.infallibleAppend(v); }
   1.211 +
   1.212 +    void popBack() { vector.popBack(); }
   1.213 +    T popCopy() { return vector.popCopy(); }
   1.214 +
   1.215 +    bool growBy(size_t inc) {
   1.216 +        size_t oldLength = vector.length();
   1.217 +        if (!vector.growByUninitialized(inc))
   1.218 +            return false;
   1.219 +        makeRangeGCSafe(oldLength);
   1.220 +        return true;
   1.221 +    }
   1.222 +
   1.223 +    bool resize(size_t newLength) {
   1.224 +        size_t oldLength = vector.length();
   1.225 +        if (newLength <= oldLength) {
   1.226 +            vector.shrinkBy(oldLength - newLength);
   1.227 +            return true;
   1.228 +        }
   1.229 +        if (!vector.growByUninitialized(newLength - oldLength))
   1.230 +            return false;
   1.231 +        makeRangeGCSafe(oldLength);
   1.232 +        return true;
   1.233 +    }
   1.234 +
   1.235 +    void clear() { vector.clear(); }
   1.236 +
   1.237 +    bool reserve(size_t newLength) {
   1.238 +        return vector.reserve(newLength);
   1.239 +    }
   1.240 +
   1.241 +    T &operator[](size_t i) { return vector[i]; }
   1.242 +    const T &operator[](size_t i) const { return vector[i]; }
   1.243 +
   1.244 +    JS::MutableHandle<T> handleAt(size_t i) {
   1.245 +        return JS::MutableHandle<T>::fromMarkedLocation(&vector[i]);
   1.246 +    }
   1.247 +    JS::Handle<T> handleAt(size_t i) const {
   1.248 +        return JS::Handle<T>::fromMarkedLocation(&vector[i]);
   1.249 +    }
   1.250 +
   1.251 +    const T *begin() const { return vector.begin(); }
   1.252 +    T *begin() { return vector.begin(); }
   1.253 +
   1.254 +    const T *end() const { return vector.end(); }
   1.255 +    T *end() { return vector.end(); }
   1.256 +
   1.257 +    Range all() { return vector.all(); }
   1.258 +
   1.259 +    const T &back() const { return vector.back(); }
   1.260 +
   1.261 +    friend void AutoGCRooter::trace(JSTracer *trc);
   1.262 +
   1.263 +  private:
   1.264 +    void makeRangeGCSafe(size_t oldLength) {
   1.265 +        T *t = vector.begin() + oldLength;
   1.266 +        for (size_t i = oldLength; i < vector.length(); ++i, ++t)
   1.267 +            memset(t, 0, sizeof(T));
   1.268 +    }
   1.269 +
   1.270 +    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
   1.271 +};
   1.272 +
   1.273 +template<class Key, class Value>
   1.274 +class AutoHashMapRooter : protected AutoGCRooter
   1.275 +{
   1.276 +  private:
   1.277 +    typedef js::HashMap<Key, Value> HashMapImpl;
   1.278 +
   1.279 +  public:
   1.280 +    explicit AutoHashMapRooter(JSContext *cx, ptrdiff_t tag
   1.281 +                               MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
   1.282 +      : AutoGCRooter(cx, tag), map(cx)
   1.283 +    {
   1.284 +        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
   1.285 +    }
   1.286 +
   1.287 +    typedef Key KeyType;
   1.288 +    typedef Value ValueType;
   1.289 +    typedef typename HashMapImpl::Entry Entry;
   1.290 +    typedef typename HashMapImpl::Lookup Lookup;
   1.291 +    typedef typename HashMapImpl::Ptr Ptr;
   1.292 +    typedef typename HashMapImpl::AddPtr AddPtr;
   1.293 +
   1.294 +    bool init(uint32_t len = 16) {
   1.295 +        return map.init(len);
   1.296 +    }
   1.297 +    bool initialized() const {
   1.298 +        return map.initialized();
   1.299 +    }
   1.300 +    Ptr lookup(const Lookup &l) const {
   1.301 +        return map.lookup(l);
   1.302 +    }
   1.303 +    void remove(Ptr p) {
   1.304 +        map.remove(p);
   1.305 +    }
   1.306 +    AddPtr lookupForAdd(const Lookup &l) const {
   1.307 +        return map.lookupForAdd(l);
   1.308 +    }
   1.309 +
   1.310 +    template<typename KeyInput, typename ValueInput>
   1.311 +    bool add(AddPtr &p, const KeyInput &k, const ValueInput &v) {
   1.312 +        return map.add(p, k, v);
   1.313 +    }
   1.314 +
   1.315 +    bool add(AddPtr &p, const Key &k) {
   1.316 +        return map.add(p, k);
   1.317 +    }
   1.318 +
   1.319 +    template<typename KeyInput, typename ValueInput>
   1.320 +    bool relookupOrAdd(AddPtr &p, const KeyInput &k, const ValueInput &v) {
   1.321 +        return map.relookupOrAdd(p, k, v);
   1.322 +    }
   1.323 +
   1.324 +    typedef typename HashMapImpl::Range Range;
   1.325 +    Range all() const {
   1.326 +        return map.all();
   1.327 +    }
   1.328 +
   1.329 +    typedef typename HashMapImpl::Enum Enum;
   1.330 +
   1.331 +    void clear() {
   1.332 +        map.clear();
   1.333 +    }
   1.334 +
   1.335 +    void finish() {
   1.336 +        map.finish();
   1.337 +    }
   1.338 +
   1.339 +    bool empty() const {
   1.340 +        return map.empty();
   1.341 +    }
   1.342 +
   1.343 +    uint32_t count() const {
   1.344 +        return map.count();
   1.345 +    }
   1.346 +
   1.347 +    size_t capacity() const {
   1.348 +        return map.capacity();
   1.349 +    }
   1.350 +
   1.351 +    size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
   1.352 +        return map.sizeOfExcludingThis(mallocSizeOf);
   1.353 +    }
   1.354 +    size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
   1.355 +        return map.sizeOfIncludingThis(mallocSizeOf);
   1.356 +    }
   1.357 +
   1.358 +    unsigned generation() const {
   1.359 +        return map.generation();
   1.360 +    }
   1.361 +
   1.362 +    /************************************************** Shorthand operations */
   1.363 +
   1.364 +    bool has(const Lookup &l) const {
   1.365 +        return map.has(l);
   1.366 +    }
   1.367 +
   1.368 +    template<typename KeyInput, typename ValueInput>
   1.369 +    bool put(const KeyInput &k, const ValueInput &v) {
   1.370 +        return map.put(k, v);
   1.371 +    }
   1.372 +
   1.373 +    template<typename KeyInput, typename ValueInput>
   1.374 +    bool putNew(const KeyInput &k, const ValueInput &v) {
   1.375 +        return map.putNew(k, v);
   1.376 +    }
   1.377 +
   1.378 +    Ptr lookupWithDefault(const Key &k, const Value &defaultValue) {
   1.379 +        return map.lookupWithDefault(k, defaultValue);
   1.380 +    }
   1.381 +
   1.382 +    void remove(const Lookup &l) {
   1.383 +        map.remove(l);
   1.384 +    }
   1.385 +
   1.386 +    friend void AutoGCRooter::trace(JSTracer *trc);
   1.387 +
   1.388 +  private:
   1.389 +    AutoHashMapRooter(const AutoHashMapRooter &hmr) MOZ_DELETE;
   1.390 +    AutoHashMapRooter &operator=(const AutoHashMapRooter &hmr) MOZ_DELETE;
   1.391 +
   1.392 +    HashMapImpl map;
   1.393 +
   1.394 +    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
   1.395 +};
   1.396 +
   1.397 +template<class T>
   1.398 +class AutoHashSetRooter : protected AutoGCRooter
   1.399 +{
   1.400 +  private:
   1.401 +    typedef js::HashSet<T> HashSetImpl;
   1.402 +
   1.403 +  public:
   1.404 +    explicit AutoHashSetRooter(JSContext *cx, ptrdiff_t tag
   1.405 +                               MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
   1.406 +      : AutoGCRooter(cx, tag), set(cx)
   1.407 +    {
   1.408 +        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
   1.409 +    }
   1.410 +
   1.411 +    typedef typename HashSetImpl::Lookup Lookup;
   1.412 +    typedef typename HashSetImpl::Ptr Ptr;
   1.413 +    typedef typename HashSetImpl::AddPtr AddPtr;
   1.414 +
   1.415 +    bool init(uint32_t len = 16) {
   1.416 +        return set.init(len);
   1.417 +    }
   1.418 +    bool initialized() const {
   1.419 +        return set.initialized();
   1.420 +    }
   1.421 +    Ptr lookup(const Lookup &l) const {
   1.422 +        return set.lookup(l);
   1.423 +    }
   1.424 +    void remove(Ptr p) {
   1.425 +        set.remove(p);
   1.426 +    }
   1.427 +    AddPtr lookupForAdd(const Lookup &l) const {
   1.428 +        return set.lookupForAdd(l);
   1.429 +    }
   1.430 +
   1.431 +    bool add(AddPtr &p, const T &t) {
   1.432 +        return set.add(p, t);
   1.433 +    }
   1.434 +
   1.435 +    bool relookupOrAdd(AddPtr &p, const Lookup &l, const T &t) {
   1.436 +        return set.relookupOrAdd(p, l, t);
   1.437 +    }
   1.438 +
   1.439 +    typedef typename HashSetImpl::Range Range;
   1.440 +    Range all() const {
   1.441 +        return set.all();
   1.442 +    }
   1.443 +
   1.444 +    typedef typename HashSetImpl::Enum Enum;
   1.445 +
   1.446 +    void clear() {
   1.447 +        set.clear();
   1.448 +    }
   1.449 +
   1.450 +    void finish() {
   1.451 +        set.finish();
   1.452 +    }
   1.453 +
   1.454 +    bool empty() const {
   1.455 +        return set.empty();
   1.456 +    }
   1.457 +
   1.458 +    uint32_t count() const {
   1.459 +        return set.count();
   1.460 +    }
   1.461 +
   1.462 +    size_t capacity() const {
   1.463 +        return set.capacity();
   1.464 +    }
   1.465 +
   1.466 +    size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
   1.467 +        return set.sizeOfExcludingThis(mallocSizeOf);
   1.468 +    }
   1.469 +    size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
   1.470 +        return set.sizeOfIncludingThis(mallocSizeOf);
   1.471 +    }
   1.472 +
   1.473 +    unsigned generation() const {
   1.474 +        return set.generation();
   1.475 +    }
   1.476 +
   1.477 +    /************************************************** Shorthand operations */
   1.478 +
   1.479 +    bool has(const Lookup &l) const {
   1.480 +        return set.has(l);
   1.481 +    }
   1.482 +
   1.483 +    bool put(const T &t) {
   1.484 +        return set.put(t);
   1.485 +    }
   1.486 +
   1.487 +    bool putNew(const T &t) {
   1.488 +        return set.putNew(t);
   1.489 +    }
   1.490 +
   1.491 +    void remove(const Lookup &l) {
   1.492 +        set.remove(l);
   1.493 +    }
   1.494 +
   1.495 +    friend void AutoGCRooter::trace(JSTracer *trc);
   1.496 +
   1.497 +  private:
   1.498 +    AutoHashSetRooter(const AutoHashSetRooter &hmr) MOZ_DELETE;
   1.499 +    AutoHashSetRooter &operator=(const AutoHashSetRooter &hmr) MOZ_DELETE;
   1.500 +
   1.501 +    HashSetImpl set;
   1.502 +
   1.503 +    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
   1.504 +};
   1.505 +
   1.506 +class MOZ_STACK_CLASS AutoValueVector : public AutoVectorRooter<Value>
   1.507 +{
   1.508 +  public:
   1.509 +    explicit AutoValueVector(JSContext *cx
   1.510 +                             MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
   1.511 +        : AutoVectorRooter<Value>(cx, VALVECTOR)
   1.512 +    {
   1.513 +        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
   1.514 +    }
   1.515 +
   1.516 +    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
   1.517 +};
   1.518 +
   1.519 +class AutoIdVector : public AutoVectorRooter<jsid>
   1.520 +{
   1.521 +  public:
   1.522 +    explicit AutoIdVector(JSContext *cx
   1.523 +                          MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
   1.524 +        : AutoVectorRooter<jsid>(cx, IDVECTOR)
   1.525 +    {
   1.526 +        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
   1.527 +    }
   1.528 +
   1.529 +    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
   1.530 +};
   1.531 +
   1.532 +class AutoObjectVector : public AutoVectorRooter<JSObject *>
   1.533 +{
   1.534 +  public:
   1.535 +    explicit AutoObjectVector(JSContext *cx
   1.536 +                              MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
   1.537 +        : AutoVectorRooter<JSObject *>(cx, OBJVECTOR)
   1.538 +    {
   1.539 +        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
   1.540 +    }
   1.541 +
   1.542 +    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
   1.543 +};
   1.544 +
   1.545 +class AutoFunctionVector : public AutoVectorRooter<JSFunction *>
   1.546 +{
   1.547 +  public:
   1.548 +    explicit AutoFunctionVector(JSContext *cx
   1.549 +                                MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
   1.550 +        : AutoVectorRooter<JSFunction *>(cx, FUNVECTOR)
   1.551 +    {
   1.552 +        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
   1.553 +    }
   1.554 +
   1.555 +    explicit AutoFunctionVector(js::ContextFriendFields *cx
   1.556 +                                MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
   1.557 +        : AutoVectorRooter<JSFunction *>(cx, FUNVECTOR)
   1.558 +    {
   1.559 +        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
   1.560 +    }
   1.561 +
   1.562 +    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
   1.563 +};
   1.564 +
   1.565 +class AutoScriptVector : public AutoVectorRooter<JSScript *>
   1.566 +{
   1.567 +  public:
   1.568 +    explicit AutoScriptVector(JSContext *cx
   1.569 +                              MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
   1.570 +        : AutoVectorRooter<JSScript *>(cx, SCRIPTVECTOR)
   1.571 +    {
   1.572 +        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
   1.573 +    }
   1.574 +
   1.575 +    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
   1.576 +};
   1.577 +
   1.578 +/*
   1.579 + * Cutsom rooting behavior for internal and external clients.
   1.580 + */
   1.581 +class JS_PUBLIC_API(CustomAutoRooter) : private AutoGCRooter
   1.582 +{
   1.583 +  public:
   1.584 +    template <typename CX>
   1.585 +    explicit CustomAutoRooter(CX *cx MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
   1.586 +      : AutoGCRooter(cx, CUSTOM)
   1.587 +    {
   1.588 +        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
   1.589 +    }
   1.590 +
   1.591 +    friend void AutoGCRooter::trace(JSTracer *trc);
   1.592 +
   1.593 +  protected:
   1.594 +    /* Supplied by derived class to trace roots. */
   1.595 +    virtual void trace(JSTracer *trc) = 0;
   1.596 +
   1.597 +  private:
   1.598 +    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
   1.599 +};
   1.600 +
   1.601 +/* A handle to an array of rooted values. */
   1.602 +class HandleValueArray
   1.603 +{
   1.604 +    const size_t length_;
   1.605 +    const Value * const elements_;
   1.606 +
   1.607 +    HandleValueArray(size_t len, const Value *elements) : length_(len), elements_(elements) {}
   1.608 +
   1.609 +  public:
   1.610 +    HandleValueArray(const RootedValue& value) : length_(1), elements_(value.address()) {}
   1.611 +
   1.612 +    HandleValueArray(const AutoValueVector& values)
   1.613 +      : length_(values.length()), elements_(values.begin()) {}
   1.614 +
   1.615 +    template <size_t N>
   1.616 +    HandleValueArray(const AutoValueArray<N>& values) : length_(N), elements_(values.begin()) {}
   1.617 +
   1.618 +    /* CallArgs must already be rooted somewhere up the stack. */
   1.619 +    HandleValueArray(const JS::CallArgs& args) : length_(args.length()), elements_(args.array()) {}
   1.620 +
   1.621 +    /* Use with care! Only call this if the data is guaranteed to be marked. */
   1.622 +    static HandleValueArray fromMarkedLocation(size_t len, const Value *elements) {
   1.623 +        return HandleValueArray(len, elements);
   1.624 +    }
   1.625 +
   1.626 +    static HandleValueArray subarray(const HandleValueArray& values, size_t startIndex, size_t len) {
   1.627 +        JS_ASSERT(startIndex + len <= values.length());
   1.628 +        return HandleValueArray(len, values.begin() + startIndex);
   1.629 +    }
   1.630 +
   1.631 +    static HandleValueArray empty() {
   1.632 +        return HandleValueArray(0, nullptr);
   1.633 +    }
   1.634 +
   1.635 +    size_t length() const { return length_; }
   1.636 +    const Value *begin() const { return elements_; }
   1.637 +
   1.638 +    HandleValue operator[](size_t i) const {
   1.639 +        JS_ASSERT(i < length_);
   1.640 +        return HandleValue::fromMarkedLocation(&elements_[i]);
   1.641 +    }
   1.642 +};
   1.643 +
   1.644 +}  /* namespace JS */
   1.645 +
   1.646 +/************************************************************************/
   1.647 +
   1.648 +struct JSFreeOp {
   1.649 +  private:
   1.650 +    JSRuntime   *runtime_;
   1.651 +
   1.652 +  protected:
   1.653 +    JSFreeOp(JSRuntime *rt)
   1.654 +      : runtime_(rt) { }
   1.655 +
   1.656 +  public:
   1.657 +    JSRuntime *runtime() const {
   1.658 +        return runtime_;
   1.659 +    }
   1.660 +};
   1.661 +
   1.662 +/* Callbacks and their arguments. */
   1.663 +
   1.664 +/************************************************************************/
   1.665 +
   1.666 +typedef enum JSContextOp {
   1.667 +    JSCONTEXT_NEW,
   1.668 +    JSCONTEXT_DESTROY
   1.669 +} JSContextOp;
   1.670 +
   1.671 +/*
   1.672 + * The possible values for contextOp when the runtime calls the callback are:
   1.673 + *   JSCONTEXT_NEW      JS_NewContext successfully created a new JSContext
   1.674 + *                      instance. The callback can initialize the instance as
   1.675 + *                      required. If the callback returns false, the instance
   1.676 + *                      will be destroyed and JS_NewContext returns null. In
   1.677 + *                      this case the callback is not called again.
   1.678 + *   JSCONTEXT_DESTROY  One of JS_DestroyContext* methods is called. The
   1.679 + *                      callback may perform its own cleanup and must always
   1.680 + *                      return true.
   1.681 + *   Any other value    For future compatibility the callback must do nothing
   1.682 + *                      and return true in this case.
   1.683 + */
   1.684 +typedef bool
   1.685 +(* JSContextCallback)(JSContext *cx, unsigned contextOp, void *data);
   1.686 +
   1.687 +typedef enum JSGCStatus {
   1.688 +    JSGC_BEGIN,
   1.689 +    JSGC_END
   1.690 +} JSGCStatus;
   1.691 +
   1.692 +typedef void
   1.693 +(* JSGCCallback)(JSRuntime *rt, JSGCStatus status, void *data);
   1.694 +
   1.695 +typedef enum JSFinalizeStatus {
   1.696 +    /*
   1.697 +     * Called when preparing to sweep a group of compartments, before anything
   1.698 +     * has been swept.  The collector will not yield to the mutator before
   1.699 +     * calling the callback with JSFINALIZE_GROUP_END status.
   1.700 +     */
   1.701 +    JSFINALIZE_GROUP_START,
   1.702 +
   1.703 +    /*
   1.704 +     * Called when preparing to sweep a group of compartments. Weak references
   1.705 +     * to unmarked things have been removed and things that are not swept
   1.706 +     * incrementally have been finalized at this point.  The collector may yield
   1.707 +     * to the mutator after this point.
   1.708 +     */
   1.709 +    JSFINALIZE_GROUP_END,
   1.710 +
   1.711 +    /*
   1.712 +     * Called at the end of collection when everything has been swept.
   1.713 +     */
   1.714 +    JSFINALIZE_COLLECTION_END
   1.715 +} JSFinalizeStatus;
   1.716 +
   1.717 +typedef void
   1.718 +(* JSFinalizeCallback)(JSFreeOp *fop, JSFinalizeStatus status, bool isCompartment);
   1.719 +
   1.720 +typedef bool
   1.721 +(* JSInterruptCallback)(JSContext *cx);
   1.722 +
   1.723 +typedef void
   1.724 +(* JSErrorReporter)(JSContext *cx, const char *message, JSErrorReport *report);
   1.725 +
   1.726 +#ifdef MOZ_TRACE_JSCALLS
   1.727 +typedef void
   1.728 +(* JSFunctionCallback)(const JSFunction *fun,
   1.729 +                       const JSScript *scr,
   1.730 +                       const JSContext *cx,
   1.731 +                       int entering);
   1.732 +#endif
   1.733 +
   1.734 +/*
   1.735 + * Possible exception types. These types are part of a JSErrorFormatString
   1.736 + * structure. They define which error to throw in case of a runtime error.
   1.737 + * JSEXN_NONE marks an unthrowable error.
   1.738 + */
   1.739 +typedef enum JSExnType {
   1.740 +    JSEXN_NONE = -1,
   1.741 +      JSEXN_ERR,
   1.742 +        JSEXN_INTERNALERR,
   1.743 +        JSEXN_EVALERR,
   1.744 +        JSEXN_RANGEERR,
   1.745 +        JSEXN_REFERENCEERR,
   1.746 +        JSEXN_SYNTAXERR,
   1.747 +        JSEXN_TYPEERR,
   1.748 +        JSEXN_URIERR,
   1.749 +        JSEXN_LIMIT
   1.750 +} JSExnType;
   1.751 +
   1.752 +typedef struct JSErrorFormatString {
   1.753 +    /* The error format string in ASCII. */
   1.754 +    const char *format;
   1.755 +
   1.756 +    /* The number of arguments to expand in the formatted error message. */
   1.757 +    uint16_t argCount;
   1.758 +
   1.759 +    /* One of the JSExnType constants above. */
   1.760 +    int16_t exnType;
   1.761 +} JSErrorFormatString;
   1.762 +
   1.763 +typedef const JSErrorFormatString *
   1.764 +(* JSErrorCallback)(void *userRef, const char *locale,
   1.765 +                    const unsigned errorNumber);
   1.766 +
   1.767 +typedef bool
   1.768 +(* JSLocaleToUpperCase)(JSContext *cx, JS::HandleString src, JS::MutableHandleValue rval);
   1.769 +
   1.770 +typedef bool
   1.771 +(* JSLocaleToLowerCase)(JSContext *cx, JS::HandleString src, JS::MutableHandleValue rval);
   1.772 +
   1.773 +typedef bool
   1.774 +(* JSLocaleCompare)(JSContext *cx, JS::HandleString src1, JS::HandleString src2,
   1.775 +                    JS::MutableHandleValue rval);
   1.776 +
   1.777 +typedef bool
   1.778 +(* JSLocaleToUnicode)(JSContext *cx, const char *src, JS::MutableHandleValue rval);
   1.779 +
   1.780 +/*
   1.781 + * Callback used to ask the embedding for the cross compartment wrapper handler
   1.782 + * that implements the desired prolicy for this kind of object in the
   1.783 + * destination compartment. |obj| is the object to be wrapped. If |existing| is
   1.784 + * non-nullptr, it will point to an existing wrapper object that should be
   1.785 + * re-used if possible. |existing| is guaranteed to be a cross-compartment
   1.786 + * wrapper with a lazily-defined prototype and the correct global. It is
   1.787 + * guaranteed not to wrap a function.
   1.788 + */
   1.789 +typedef JSObject *
   1.790 +(* JSWrapObjectCallback)(JSContext *cx, JS::HandleObject existing, JS::HandleObject obj,
   1.791 +                         JS::HandleObject proto, JS::HandleObject parent,
   1.792 +                         unsigned flags);
   1.793 +
   1.794 +/*
   1.795 + * Callback used by the wrap hook to ask the embedding to prepare an object
   1.796 + * for wrapping in a context. This might include unwrapping other wrappers
   1.797 + * or even finding a more suitable object for the new compartment.
   1.798 + */
   1.799 +typedef JSObject *
   1.800 +(* JSPreWrapCallback)(JSContext *cx, JS::HandleObject scope, JS::HandleObject obj,
   1.801 +                      unsigned flags);
   1.802 +
   1.803 +struct JSWrapObjectCallbacks
   1.804 +{
   1.805 +    JSWrapObjectCallback wrap;
   1.806 +    JSPreWrapCallback preWrap;
   1.807 +};
   1.808 +
   1.809 +typedef void
   1.810 +(* JSDestroyCompartmentCallback)(JSFreeOp *fop, JSCompartment *compartment);
   1.811 +
   1.812 +typedef void
   1.813 +(* JSZoneCallback)(JS::Zone *zone);
   1.814 +
   1.815 +typedef void
   1.816 +(* JSCompartmentNameCallback)(JSRuntime *rt, JSCompartment *compartment,
   1.817 +                              char *buf, size_t bufsize);
   1.818 +
   1.819 +/************************************************************************/
   1.820 +
   1.821 +static MOZ_ALWAYS_INLINE jsval
   1.822 +JS_NumberValue(double d)
   1.823 +{
   1.824 +    int32_t i;
   1.825 +    d = JS::CanonicalizeNaN(d);
   1.826 +    if (mozilla::NumberIsInt32(d, &i))
   1.827 +        return INT_TO_JSVAL(i);
   1.828 +    return DOUBLE_TO_JSVAL(d);
   1.829 +}
   1.830 +
   1.831 +/************************************************************************/
   1.832 +
   1.833 +JS_PUBLIC_API(bool)
   1.834 +JS_StringHasBeenInterned(JSContext *cx, JSString *str);
   1.835 +
   1.836 +/*
   1.837 + * Only JSStrings that have been interned via the JSAPI can be turned into
   1.838 + * jsids by API clients.
   1.839 + *
   1.840 + * N.B. if a jsid is backed by a string which has not been interned, that
   1.841 + * string must be appropriately rooted to avoid being collected by the GC.
   1.842 + */
   1.843 +JS_PUBLIC_API(jsid)
   1.844 +INTERNED_STRING_TO_JSID(JSContext *cx, JSString *str);
   1.845 +
   1.846 +/*
   1.847 + * Returns true iff the given jsval is immune to GC and can be used across
   1.848 + * multiple JSRuntimes without requiring any conversion API.
   1.849 + */
   1.850 +static MOZ_ALWAYS_INLINE bool
   1.851 +JSVAL_IS_UNIVERSAL(jsval v)
   1.852 +{
   1.853 +    return !JSVAL_IS_GCTHING(v);
   1.854 +}
   1.855 +
   1.856 +namespace JS {
   1.857 +
   1.858 +class AutoIdRooter : private AutoGCRooter
   1.859 +{
   1.860 +  public:
   1.861 +    explicit AutoIdRooter(JSContext *cx, jsid aId = INT_TO_JSID(0)
   1.862 +                          MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
   1.863 +      : AutoGCRooter(cx, ID), id_(aId)
   1.864 +    {
   1.865 +        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
   1.866 +    }
   1.867 +
   1.868 +    jsid id() {
   1.869 +        return id_;
   1.870 +    }
   1.871 +
   1.872 +    jsid * addr() {
   1.873 +        return &id_;
   1.874 +    }
   1.875 +
   1.876 +    friend void AutoGCRooter::trace(JSTracer *trc);
   1.877 +
   1.878 +  private:
   1.879 +    jsid id_;
   1.880 +    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
   1.881 +};
   1.882 +
   1.883 +// Container class for passing in script source buffers to the JS engine.  This
   1.884 +// not only groups the buffer and length values, it also provides a way to
   1.885 +// optionally pass ownership of the buffer to the JS engine without copying.
   1.886 +// Rules for use:
   1.887 +//
   1.888 +//  1) The data array must be allocated with js_malloc() or js_realloc() if
   1.889 +//     ownership is being granted to the SourceBufferHolder.
   1.890 +//  2) If ownership is not given to the SourceBufferHolder, then the memory
   1.891 +//     must be kept alive until the JS compilation is complete.
   1.892 +//  3) Any code calling SourceBufferHolder::take() must guarantee to keep the
   1.893 +//     memory alive until JS compilation completes.  Normally only the JS
   1.894 +//     engine should be calling take().
   1.895 +//
   1.896 +// Example use:
   1.897 +//
   1.898 +//    size_t length = 512;
   1.899 +//    jschar* chars = static_cast<jschar*>(js_malloc(sizeof(jschar) * length));
   1.900 +//    JS::SourceBufferHolder srcBuf(chars, length, JS::SourceBufferHolder::GiveOwnership);
   1.901 +//    JS::Compile(cx, obj, options, srcBuf);
   1.902 +//
   1.903 +class MOZ_STACK_CLASS SourceBufferHolder MOZ_FINAL
   1.904 +{
   1.905 +  public:
   1.906 +    enum Ownership {
   1.907 +      NoOwnership,
   1.908 +      GiveOwnership
   1.909 +    };
   1.910 +
   1.911 +    SourceBufferHolder(const jschar *data, size_t dataLength, Ownership ownership)
   1.912 +      : data_(data),
   1.913 +        length_(dataLength),
   1.914 +        ownsChars_(ownership == GiveOwnership)
   1.915 +    {
   1.916 +        // Ensure that null buffers properly return an unowned, empty,
   1.917 +        // null-terminated string.
   1.918 +        static const jschar NullChar_ = 0;
   1.919 +        if (!get()) {
   1.920 +            data_ = &NullChar_;
   1.921 +            length_ = 0;
   1.922 +            ownsChars_ = false;
   1.923 +        }
   1.924 +    }
   1.925 +
   1.926 +    ~SourceBufferHolder() {
   1.927 +        if (ownsChars_)
   1.928 +            js_free(const_cast<jschar *>(data_));
   1.929 +    }
   1.930 +
   1.931 +    // Access the underlying source buffer without affecting ownership.
   1.932 +    const jschar *get() const { return data_; }
   1.933 +
   1.934 +    // Length of the source buffer in jschars (not bytes)
   1.935 +    size_t length() const { return length_; }
   1.936 +
   1.937 +    // Returns true if the SourceBufferHolder owns the buffer and will free
   1.938 +    // it upon destruction.  If true, it is legal to call take().
   1.939 +    bool ownsChars() const { return ownsChars_; }
   1.940 +
   1.941 +    // Retrieve and take ownership of the underlying data buffer.  The caller
   1.942 +    // is now responsible for calling js_free() on the returned value, *but only
   1.943 +    // after JS script compilation has completed*.
   1.944 +    //
   1.945 +    // After the buffer has been taken the SourceBufferHolder functions as if
   1.946 +    // it had been constructed on an unowned buffer;  get() and length() still
   1.947 +    // work.  In order for this to be safe the taken buffer must be kept alive
   1.948 +    // until after JS script compilation completes as noted above.
   1.949 +    //
   1.950 +    // Note, it's the caller's responsibility to check ownsChars() before taking
   1.951 +    // the buffer.  Taking and then free'ing an unowned buffer will have dire
   1.952 +    // consequences.
   1.953 +    jschar *take() {
   1.954 +        JS_ASSERT(ownsChars_);
   1.955 +        ownsChars_ = false;
   1.956 +        return const_cast<jschar *>(data_);
   1.957 +    }
   1.958 +
   1.959 +  private:
   1.960 +    SourceBufferHolder(SourceBufferHolder &) MOZ_DELETE;
   1.961 +    SourceBufferHolder &operator=(SourceBufferHolder &) MOZ_DELETE;
   1.962 +
   1.963 +    const jschar *data_;
   1.964 +    size_t length_;
   1.965 +    bool ownsChars_;
   1.966 +};
   1.967 +
   1.968 +} /* namespace JS */
   1.969 +
   1.970 +/************************************************************************/
   1.971 +
   1.972 +/* Property attributes, set in JSPropertySpec and passed to API functions. */
   1.973 +#define JSPROP_ENUMERATE        0x01    /* property is visible to for/in loop */
   1.974 +#define JSPROP_READONLY         0x02    /* not settable: assignment is no-op.
   1.975 +                                           This flag is only valid when neither
   1.976 +                                           JSPROP_GETTER nor JSPROP_SETTER is
   1.977 +                                           set. */
   1.978 +#define JSPROP_PERMANENT        0x04    /* property cannot be deleted */
   1.979 +#define JSPROP_NATIVE_ACCESSORS 0x08    /* set in JSPropertyDescriptor.flags
   1.980 +                                           if getters/setters are JSNatives */
   1.981 +#define JSPROP_GETTER           0x10    /* property holds getter function */
   1.982 +#define JSPROP_SETTER           0x20    /* property holds setter function */
   1.983 +#define JSPROP_SHARED           0x40    /* don't allocate a value slot for this
   1.984 +                                           property; don't copy the property on
   1.985 +                                           set of the same-named property in an
   1.986 +                                           object that delegates to a prototype
   1.987 +                                           containing this property */
   1.988 +#define JSPROP_INDEX            0x80    /* name is actually (int) index */
   1.989 +
   1.990 +#define JSFUN_STUB_GSOPS       0x200    /* use JS_PropertyStub getter/setter
   1.991 +                                           instead of defaulting to class gsops
   1.992 +                                           for property holding function */
   1.993 +
   1.994 +#define JSFUN_CONSTRUCTOR      0x400    /* native that can be called as a ctor */
   1.995 +
   1.996 +
   1.997 +/*
   1.998 + * Specify a generic native prototype methods, i.e., methods of a class
   1.999 + * prototype that are exposed as static methods taking an extra leading
  1.1000 + * argument: the generic |this| parameter.
  1.1001 + *
  1.1002 + * If you set this flag in a JSFunctionSpec struct's flags initializer, then
  1.1003 + * that struct must live at least as long as the native static method object
  1.1004 + * created due to this flag by JS_DefineFunctions or JS_InitClass.  Typically
  1.1005 + * JSFunctionSpec structs are allocated in static arrays.
  1.1006 + */
  1.1007 +#define JSFUN_GENERIC_NATIVE   0x800
  1.1008 +
  1.1009 +#define JSFUN_FLAGS_MASK       0xe00    /* | of all the JSFUN_* flags */
  1.1010 +
  1.1011 +/*
  1.1012 + * The first call to JS_CallOnce by any thread in a process will call 'func'.
  1.1013 + * Later calls to JS_CallOnce with the same JSCallOnceType object will be
  1.1014 + * suppressed.
  1.1015 + *
  1.1016 + * Equivalently: each distinct JSCallOnceType object will allow one JS_CallOnce
  1.1017 + * to invoke its JSInitCallback.
  1.1018 + */
  1.1019 +extern JS_PUBLIC_API(bool)
  1.1020 +JS_CallOnce(JSCallOnceType *once, JSInitCallback func);
  1.1021 +
  1.1022 +/* Microseconds since the epoch, midnight, January 1, 1970 UTC. */
  1.1023 +extern JS_PUBLIC_API(int64_t)
  1.1024 +JS_Now(void);
  1.1025 +
  1.1026 +/* Don't want to export data, so provide accessors for non-inline jsvals. */
  1.1027 +extern JS_PUBLIC_API(jsval)
  1.1028 +JS_GetNaNValue(JSContext *cx);
  1.1029 +
  1.1030 +extern JS_PUBLIC_API(jsval)
  1.1031 +JS_GetNegativeInfinityValue(JSContext *cx);
  1.1032 +
  1.1033 +extern JS_PUBLIC_API(jsval)
  1.1034 +JS_GetPositiveInfinityValue(JSContext *cx);
  1.1035 +
  1.1036 +extern JS_PUBLIC_API(jsval)
  1.1037 +JS_GetEmptyStringValue(JSContext *cx);
  1.1038 +
  1.1039 +extern JS_PUBLIC_API(JSString *)
  1.1040 +JS_GetEmptyString(JSRuntime *rt);
  1.1041 +
  1.1042 +/*
  1.1043 + * Format is a string of the following characters (spaces are insignificant),
  1.1044 + * specifying the tabulated type conversions:
  1.1045 + *
  1.1046 + *   b      bool            Boolean
  1.1047 + *   c      uint16_t/jschar ECMA uint16_t, Unicode char
  1.1048 + *   i      int32_t         ECMA int32_t
  1.1049 + *   j      int32_t         ECMA int32_t (used to be different)
  1.1050 + *   u      uint32_t        ECMA uint32_t
  1.1051 + *   d      double          IEEE double
  1.1052 + *   I      double          Integral IEEE double
  1.1053 + *   S      JSString *      Unicode string, accessed by a JSString pointer
  1.1054 + *   W      jschar *        Unicode character vector, 0-terminated (W for wide)
  1.1055 + *   o      JSObject *      Object reference
  1.1056 + *   f      JSFunction *    Function private
  1.1057 + *   v      jsval           Argument value (no conversion)
  1.1058 + *   *      N/A             Skip this argument (no vararg)
  1.1059 + *   /      N/A             End of required arguments
  1.1060 + *
  1.1061 + * The variable argument list after format must consist of &b, &c, &s, e.g.,
  1.1062 + * where those variables have the types given above.  For the pointer types
  1.1063 + * char *, JSString *, and JSObject *, the pointed-at memory returned belongs
  1.1064 + * to the JS runtime, not to the calling native code.  The runtime promises
  1.1065 + * to keep this memory valid so long as argv refers to allocated stack space
  1.1066 + * (so long as the native function is active).
  1.1067 + *
  1.1068 + * Fewer arguments than format specifies may be passed only if there is a /
  1.1069 + * in format after the last required argument specifier and argc is at least
  1.1070 + * the number of required arguments.  More arguments than format specifies
  1.1071 + * may be passed without error; it is up to the caller to deal with trailing
  1.1072 + * unconverted arguments.
  1.1073 + */
  1.1074 +extern JS_PUBLIC_API(bool)
  1.1075 +JS_ConvertArguments(JSContext *cx, const JS::CallArgs &args, const char *format, ...);
  1.1076 +
  1.1077 +#ifdef va_start
  1.1078 +extern JS_PUBLIC_API(bool)
  1.1079 +JS_ConvertArgumentsVA(JSContext *cx, const JS::CallArgs &args, const char *format,
  1.1080 +                      va_list ap);
  1.1081 +#endif
  1.1082 +
  1.1083 +extern JS_PUBLIC_API(bool)
  1.1084 +JS_ConvertValue(JSContext *cx, JS::HandleValue v, JSType type, JS::MutableHandleValue vp);
  1.1085 +
  1.1086 +extern JS_PUBLIC_API(bool)
  1.1087 +JS_ValueToObject(JSContext *cx, JS::HandleValue v, JS::MutableHandleObject objp);
  1.1088 +
  1.1089 +extern JS_PUBLIC_API(JSFunction *)
  1.1090 +JS_ValueToFunction(JSContext *cx, JS::HandleValue v);
  1.1091 +
  1.1092 +extern JS_PUBLIC_API(JSFunction *)
  1.1093 +JS_ValueToConstructor(JSContext *cx, JS::HandleValue v);
  1.1094 +
  1.1095 +extern JS_PUBLIC_API(JSString *)
  1.1096 +JS_ValueToSource(JSContext *cx, JS::Handle<JS::Value> v);
  1.1097 +
  1.1098 +namespace js {
  1.1099 +/*
  1.1100 + * DO NOT CALL THIS.  Use JS::ToNumber
  1.1101 + */
  1.1102 +extern JS_PUBLIC_API(bool)
  1.1103 +ToNumberSlow(JSContext *cx, JS::Value v, double *dp);
  1.1104 +
  1.1105 +/*
  1.1106 + * DO NOT CALL THIS. Use JS::ToBoolean
  1.1107 + */
  1.1108 +extern JS_PUBLIC_API(bool)
  1.1109 +ToBooleanSlow(JS::HandleValue v);
  1.1110 +
  1.1111 +/*
  1.1112 + * DO NOT CALL THIS. Use JS::ToString
  1.1113 + */
  1.1114 +extern JS_PUBLIC_API(JSString*)
  1.1115 +ToStringSlow(JSContext *cx, JS::HandleValue v);
  1.1116 +} /* namespace js */
  1.1117 +
  1.1118 +namespace JS {
  1.1119 +
  1.1120 +/* ES5 9.3 ToNumber. */
  1.1121 +MOZ_ALWAYS_INLINE bool
  1.1122 +ToNumber(JSContext *cx, HandleValue v, double *out)
  1.1123 +{
  1.1124 +    AssertArgumentsAreSane(cx, v);
  1.1125 +
  1.1126 +    if (v.isNumber()) {
  1.1127 +        *out = v.toNumber();
  1.1128 +        return true;
  1.1129 +    }
  1.1130 +    return js::ToNumberSlow(cx, v, out);
  1.1131 +}
  1.1132 +
  1.1133 +MOZ_ALWAYS_INLINE bool
  1.1134 +ToBoolean(HandleValue v)
  1.1135 +{
  1.1136 +    if (v.isBoolean())
  1.1137 +        return v.toBoolean();
  1.1138 +    if (v.isInt32())
  1.1139 +        return v.toInt32() != 0;
  1.1140 +    if (v.isNullOrUndefined())
  1.1141 +        return false;
  1.1142 +    if (v.isDouble()) {
  1.1143 +        double d = v.toDouble();
  1.1144 +        return !mozilla::IsNaN(d) && d != 0;
  1.1145 +    }
  1.1146 +
  1.1147 +    /* The slow path handles strings and objects. */
  1.1148 +    return js::ToBooleanSlow(v);
  1.1149 +}
  1.1150 +
  1.1151 +MOZ_ALWAYS_INLINE JSString*
  1.1152 +ToString(JSContext *cx, HandleValue v)
  1.1153 +{
  1.1154 +    if (v.isString())
  1.1155 +        return v.toString();
  1.1156 +    return js::ToStringSlow(cx, v);
  1.1157 +}
  1.1158 +
  1.1159 +} /* namespace JS */
  1.1160 +
  1.1161 +extern JS_PUBLIC_API(bool)
  1.1162 +JS_DoubleIsInt32(double d, int32_t *ip);
  1.1163 +
  1.1164 +extern JS_PUBLIC_API(int32_t)
  1.1165 +JS_DoubleToInt32(double d);
  1.1166 +
  1.1167 +extern JS_PUBLIC_API(uint32_t)
  1.1168 +JS_DoubleToUint32(double d);
  1.1169 +
  1.1170 +
  1.1171 +namespace js {
  1.1172 +/* DO NOT CALL THIS. Use JS::ToUint16. */
  1.1173 +extern JS_PUBLIC_API(bool)
  1.1174 +ToUint16Slow(JSContext *cx, JS::HandleValue v, uint16_t *out);
  1.1175 +
  1.1176 +/* DO NOT CALL THIS. Use JS::ToInt32. */
  1.1177 +extern JS_PUBLIC_API(bool)
  1.1178 +ToInt32Slow(JSContext *cx, JS::HandleValue v, int32_t *out);
  1.1179 +
  1.1180 +/* DO NOT CALL THIS. Use JS::ToUint32. */
  1.1181 +extern JS_PUBLIC_API(bool)
  1.1182 +ToUint32Slow(JSContext *cx, JS::HandleValue v, uint32_t *out);
  1.1183 +
  1.1184 +/* DO NOT CALL THIS. Use JS::ToInt64. */
  1.1185 +extern JS_PUBLIC_API(bool)
  1.1186 +ToInt64Slow(JSContext *cx, JS::HandleValue v, int64_t *out);
  1.1187 +
  1.1188 +/* DO NOT CALL THIS. Use JS::ToUint64. */
  1.1189 +extern JS_PUBLIC_API(bool)
  1.1190 +ToUint64Slow(JSContext *cx, JS::HandleValue v, uint64_t *out);
  1.1191 +} /* namespace js */
  1.1192 +
  1.1193 +namespace JS {
  1.1194 +
  1.1195 +MOZ_ALWAYS_INLINE bool
  1.1196 +ToUint16(JSContext *cx, JS::HandleValue v, uint16_t *out)
  1.1197 +{
  1.1198 +    AssertArgumentsAreSane(cx, v);
  1.1199 +
  1.1200 +    if (v.isInt32()) {
  1.1201 +        *out = uint16_t(v.toInt32());
  1.1202 +        return true;
  1.1203 +    }
  1.1204 +    return js::ToUint16Slow(cx, v, out);
  1.1205 +}
  1.1206 +
  1.1207 +MOZ_ALWAYS_INLINE bool
  1.1208 +ToInt32(JSContext *cx, JS::HandleValue v, int32_t *out)
  1.1209 +{
  1.1210 +    AssertArgumentsAreSane(cx, v);
  1.1211 +
  1.1212 +    if (v.isInt32()) {
  1.1213 +        *out = v.toInt32();
  1.1214 +        return true;
  1.1215 +    }
  1.1216 +    return js::ToInt32Slow(cx, v, out);
  1.1217 +}
  1.1218 +
  1.1219 +MOZ_ALWAYS_INLINE bool
  1.1220 +ToUint32(JSContext *cx, JS::HandleValue v, uint32_t *out)
  1.1221 +{
  1.1222 +    AssertArgumentsAreSane(cx, v);
  1.1223 +
  1.1224 +    if (v.isInt32()) {
  1.1225 +        *out = uint32_t(v.toInt32());
  1.1226 +        return true;
  1.1227 +    }
  1.1228 +    return js::ToUint32Slow(cx, v, out);
  1.1229 +}
  1.1230 +
  1.1231 +MOZ_ALWAYS_INLINE bool
  1.1232 +ToInt64(JSContext *cx, JS::HandleValue v, int64_t *out)
  1.1233 +{
  1.1234 +    AssertArgumentsAreSane(cx, v);
  1.1235 +
  1.1236 +    if (v.isInt32()) {
  1.1237 +        *out = int64_t(v.toInt32());
  1.1238 +        return true;
  1.1239 +    }
  1.1240 +    return js::ToInt64Slow(cx, v, out);
  1.1241 +}
  1.1242 +
  1.1243 +MOZ_ALWAYS_INLINE bool
  1.1244 +ToUint64(JSContext *cx, JS::HandleValue v, uint64_t *out)
  1.1245 +{
  1.1246 +    AssertArgumentsAreSane(cx, v);
  1.1247 +
  1.1248 +    if (v.isInt32()) {
  1.1249 +        /* Account for sign extension of negatives into the longer 64bit space. */
  1.1250 +        *out = uint64_t(int64_t(v.toInt32()));
  1.1251 +        return true;
  1.1252 +    }
  1.1253 +    return js::ToUint64Slow(cx, v, out);
  1.1254 +}
  1.1255 +
  1.1256 +
  1.1257 +} /* namespace JS */
  1.1258 +
  1.1259 +extern JS_PUBLIC_API(JSType)
  1.1260 +JS_TypeOfValue(JSContext *cx, JS::Handle<JS::Value> v);
  1.1261 +
  1.1262 +extern JS_PUBLIC_API(const char *)
  1.1263 +JS_GetTypeName(JSContext *cx, JSType type);
  1.1264 +
  1.1265 +extern JS_PUBLIC_API(bool)
  1.1266 +JS_StrictlyEqual(JSContext *cx, jsval v1, jsval v2, bool *equal);
  1.1267 +
  1.1268 +extern JS_PUBLIC_API(bool)
  1.1269 +JS_LooselyEqual(JSContext *cx, JS::Handle<JS::Value> v1, JS::Handle<JS::Value> v2, bool *equal);
  1.1270 +
  1.1271 +extern JS_PUBLIC_API(bool)
  1.1272 +JS_SameValue(JSContext *cx, jsval v1, jsval v2, bool *same);
  1.1273 +
  1.1274 +/* True iff fun is the global eval function. */
  1.1275 +extern JS_PUBLIC_API(bool)
  1.1276 +JS_IsBuiltinEvalFunction(JSFunction *fun);
  1.1277 +
  1.1278 +/* True iff fun is the Function constructor. */
  1.1279 +extern JS_PUBLIC_API(bool)
  1.1280 +JS_IsBuiltinFunctionConstructor(JSFunction *fun);
  1.1281 +
  1.1282 +/************************************************************************/
  1.1283 +
  1.1284 +/*
  1.1285 + * Initialization, locking, contexts, and memory allocation.
  1.1286 + *
  1.1287 + * It is important that the first runtime and first context be created in a
  1.1288 + * single-threaded fashion, otherwise the behavior of the library is undefined.
  1.1289 + * See: http://developer.mozilla.org/en/docs/Category:JSAPI_Reference
  1.1290 + */
  1.1291 +
  1.1292 +typedef enum JSUseHelperThreads
  1.1293 +{
  1.1294 +    JS_NO_HELPER_THREADS,
  1.1295 +    JS_USE_HELPER_THREADS
  1.1296 +} JSUseHelperThreads;
  1.1297 +
  1.1298 +/**
  1.1299 + * Initialize SpiderMonkey, returning true only if initialization succeeded.
  1.1300 + * Once this method has succeeded, it is safe to call JS_NewRuntime and other
  1.1301 + * JSAPI methods.
  1.1302 + *
  1.1303 + * This method must be called before any other JSAPI method is used on any
  1.1304 + * thread.  Once it has been used, it is safe to call any JSAPI method, and it
  1.1305 + * remains safe to do so until JS_ShutDown is correctly called.
  1.1306 + *
  1.1307 + * It is currently not possible to initialize SpiderMonkey multiple times (that
  1.1308 + * is, calling JS_Init/JSAPI methods/JS_ShutDown in that order, then doing so
  1.1309 + * again).  This restriction may eventually be lifted.
  1.1310 + */
  1.1311 +extern JS_PUBLIC_API(bool)
  1.1312 +JS_Init(void);
  1.1313 +
  1.1314 +/**
  1.1315 + * Destroy free-standing resources allocated by SpiderMonkey, not associated
  1.1316 + * with any runtime, context, or other structure.
  1.1317 + *
  1.1318 + * This method should be called after all other JSAPI data has been properly
  1.1319 + * cleaned up: every new runtime must have been destroyed, every new context
  1.1320 + * must have been destroyed, and so on.  Calling this method before all other
  1.1321 + * resources have been destroyed has undefined behavior.
  1.1322 + *
  1.1323 + * Failure to call this method, at present, has no adverse effects other than
  1.1324 + * leaking memory.  This may not always be the case; it's recommended that all
  1.1325 + * embedders call this method when all other JSAPI operations have completed.
  1.1326 + *
  1.1327 + * It is currently not possible to initialize SpiderMonkey multiple times (that
  1.1328 + * is, calling JS_Init/JSAPI methods/JS_ShutDown in that order, then doing so
  1.1329 + * again).  This restriction may eventually be lifted.
  1.1330 + */
  1.1331 +extern JS_PUBLIC_API(void)
  1.1332 +JS_ShutDown(void);
  1.1333 +
  1.1334 +extern JS_PUBLIC_API(JSRuntime *)
  1.1335 +JS_NewRuntime(uint32_t maxbytes, JSUseHelperThreads useHelperThreads,
  1.1336 +              JSRuntime *parentRuntime = nullptr);
  1.1337 +
  1.1338 +extern JS_PUBLIC_API(void)
  1.1339 +JS_DestroyRuntime(JSRuntime *rt);
  1.1340 +
  1.1341 +// These are equivalent to ICU's |UMemAllocFn|, |UMemReallocFn|, and
  1.1342 +// |UMemFreeFn| types.  The first argument (called |context| in the ICU docs)
  1.1343 +// will always be nullptr, and should be ignored.
  1.1344 +typedef void *(*JS_ICUAllocFn)(const void *, size_t size);
  1.1345 +typedef void *(*JS_ICUReallocFn)(const void *, void *p, size_t size);
  1.1346 +typedef void (*JS_ICUFreeFn)(const void *, void *p);
  1.1347 +
  1.1348 +// This function can be used to track memory used by ICU.
  1.1349 +// Do not use it unless you know what you are doing!
  1.1350 +extern JS_PUBLIC_API(bool)
  1.1351 +JS_SetICUMemoryFunctions(JS_ICUAllocFn allocFn, JS_ICUReallocFn reallocFn, JS_ICUFreeFn freeFn);
  1.1352 +
  1.1353 +JS_PUBLIC_API(void *)
  1.1354 +JS_GetRuntimePrivate(JSRuntime *rt);
  1.1355 +
  1.1356 +extern JS_PUBLIC_API(JSRuntime *)
  1.1357 +JS_GetRuntime(JSContext *cx);
  1.1358 +
  1.1359 +extern JS_PUBLIC_API(JSRuntime *)
  1.1360 +JS_GetParentRuntime(JSContext *cx);
  1.1361 +
  1.1362 +JS_PUBLIC_API(void)
  1.1363 +JS_SetRuntimePrivate(JSRuntime *rt, void *data);
  1.1364 +
  1.1365 +extern JS_PUBLIC_API(void)
  1.1366 +JS_BeginRequest(JSContext *cx);
  1.1367 +
  1.1368 +extern JS_PUBLIC_API(void)
  1.1369 +JS_EndRequest(JSContext *cx);
  1.1370 +
  1.1371 +extern JS_PUBLIC_API(bool)
  1.1372 +JS_IsInRequest(JSRuntime *rt);
  1.1373 +
  1.1374 +namespace js {
  1.1375 +
  1.1376 +void
  1.1377 +AssertHeapIsIdle(JSRuntime *rt);
  1.1378 +
  1.1379 +void
  1.1380 +AssertHeapIsIdle(JSContext *cx);
  1.1381 +
  1.1382 +} /* namespace js */
  1.1383 +
  1.1384 +class JSAutoRequest
  1.1385 +{
  1.1386 +  public:
  1.1387 +    JSAutoRequest(JSContext *cx
  1.1388 +                  MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
  1.1389 +      : mContext(cx)
  1.1390 +    {
  1.1391 +        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
  1.1392 +        JS_BeginRequest(mContext);
  1.1393 +    }
  1.1394 +    ~JSAutoRequest() {
  1.1395 +        JS_EndRequest(mContext);
  1.1396 +    }
  1.1397 +
  1.1398 +  protected:
  1.1399 +    JSContext *mContext;
  1.1400 +    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
  1.1401 +
  1.1402 +#if 0
  1.1403 +  private:
  1.1404 +    static void *operator new(size_t) CPP_THROW_NEW { return 0; };
  1.1405 +    static void operator delete(void *, size_t) { };
  1.1406 +#endif
  1.1407 +};
  1.1408 +
  1.1409 +class JSAutoCheckRequest
  1.1410 +{
  1.1411 +  public:
  1.1412 +    JSAutoCheckRequest(JSContext *cx
  1.1413 +                       MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
  1.1414 +    {
  1.1415 +#if defined JS_THREADSAFE && defined JS_DEBUG
  1.1416 +        mContext = cx;
  1.1417 +        JS_ASSERT(JS_IsInRequest(JS_GetRuntime(cx)));
  1.1418 +#endif
  1.1419 +        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
  1.1420 +    }
  1.1421 +
  1.1422 +    ~JSAutoCheckRequest() {
  1.1423 +#if defined JS_THREADSAFE && defined JS_DEBUG
  1.1424 +        JS_ASSERT(JS_IsInRequest(JS_GetRuntime(mContext)));
  1.1425 +#endif
  1.1426 +    }
  1.1427 +
  1.1428 +
  1.1429 +  private:
  1.1430 +#if defined JS_THREADSAFE && defined JS_DEBUG
  1.1431 +    JSContext *mContext;
  1.1432 +#endif
  1.1433 +    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
  1.1434 +};
  1.1435 +
  1.1436 +extern JS_PUBLIC_API(void)
  1.1437 +JS_SetContextCallback(JSRuntime *rt, JSContextCallback cxCallback, void *data);
  1.1438 +
  1.1439 +extern JS_PUBLIC_API(JSContext *)
  1.1440 +JS_NewContext(JSRuntime *rt, size_t stackChunkSize);
  1.1441 +
  1.1442 +extern JS_PUBLIC_API(void)
  1.1443 +JS_DestroyContext(JSContext *cx);
  1.1444 +
  1.1445 +extern JS_PUBLIC_API(void)
  1.1446 +JS_DestroyContextNoGC(JSContext *cx);
  1.1447 +
  1.1448 +extern JS_PUBLIC_API(void *)
  1.1449 +JS_GetContextPrivate(JSContext *cx);
  1.1450 +
  1.1451 +extern JS_PUBLIC_API(void)
  1.1452 +JS_SetContextPrivate(JSContext *cx, void *data);
  1.1453 +
  1.1454 +extern JS_PUBLIC_API(void *)
  1.1455 +JS_GetSecondContextPrivate(JSContext *cx);
  1.1456 +
  1.1457 +extern JS_PUBLIC_API(void)
  1.1458 +JS_SetSecondContextPrivate(JSContext *cx, void *data);
  1.1459 +
  1.1460 +extern JS_PUBLIC_API(JSRuntime *)
  1.1461 +JS_GetRuntime(JSContext *cx);
  1.1462 +
  1.1463 +extern JS_PUBLIC_API(JSContext *)
  1.1464 +JS_ContextIterator(JSRuntime *rt, JSContext **iterp);
  1.1465 +
  1.1466 +extern JS_PUBLIC_API(JSVersion)
  1.1467 +JS_GetVersion(JSContext *cx);
  1.1468 +
  1.1469 +// Mutate the version on the compartment. This is generally discouraged, but
  1.1470 +// necessary to support the version mutation in the js and xpc shell command
  1.1471 +// set.
  1.1472 +//
  1.1473 +// It would be nice to put this in jsfriendapi, but the linkage requirements
  1.1474 +// of the shells make that impossible.
  1.1475 +JS_PUBLIC_API(void)
  1.1476 +JS_SetVersionForCompartment(JSCompartment *compartment, JSVersion version);
  1.1477 +
  1.1478 +extern JS_PUBLIC_API(const char *)
  1.1479 +JS_VersionToString(JSVersion version);
  1.1480 +
  1.1481 +extern JS_PUBLIC_API(JSVersion)
  1.1482 +JS_StringToVersion(const char *string);
  1.1483 +
  1.1484 +namespace JS {
  1.1485 +
  1.1486 +class JS_PUBLIC_API(RuntimeOptions) {
  1.1487 +  public:
  1.1488 +    RuntimeOptions()
  1.1489 +      : baseline_(false),
  1.1490 +        ion_(false),
  1.1491 +        asmJS_(false)
  1.1492 +    {
  1.1493 +    }
  1.1494 +
  1.1495 +    bool baseline() const { return baseline_; }
  1.1496 +    RuntimeOptions &setBaseline(bool flag) {
  1.1497 +        baseline_ = flag;
  1.1498 +        return *this;
  1.1499 +    }
  1.1500 +    RuntimeOptions &toggleBaseline() {
  1.1501 +        baseline_ = !baseline_;
  1.1502 +        return *this;
  1.1503 +    }
  1.1504 +
  1.1505 +    bool ion() const { return ion_; }
  1.1506 +    RuntimeOptions &setIon(bool flag) {
  1.1507 +        ion_ = flag;
  1.1508 +        return *this;
  1.1509 +    }
  1.1510 +    RuntimeOptions &toggleIon() {
  1.1511 +        ion_ = !ion_;
  1.1512 +        return *this;
  1.1513 +    }
  1.1514 +
  1.1515 +    bool asmJS() const { return asmJS_; }
  1.1516 +    RuntimeOptions &setAsmJS(bool flag) {
  1.1517 +        asmJS_ = flag;
  1.1518 +        return *this;
  1.1519 +    }
  1.1520 +    RuntimeOptions &toggleAsmJS() {
  1.1521 +        asmJS_ = !asmJS_;
  1.1522 +        return *this;
  1.1523 +    }
  1.1524 +
  1.1525 +  private:
  1.1526 +    bool baseline_ : 1;
  1.1527 +    bool ion_ : 1;
  1.1528 +    bool asmJS_ : 1;
  1.1529 +};
  1.1530 +
  1.1531 +JS_PUBLIC_API(RuntimeOptions &)
  1.1532 +RuntimeOptionsRef(JSRuntime *rt);
  1.1533 +
  1.1534 +JS_PUBLIC_API(RuntimeOptions &)
  1.1535 +RuntimeOptionsRef(JSContext *cx);
  1.1536 +
  1.1537 +class JS_PUBLIC_API(ContextOptions) {
  1.1538 +  public:
  1.1539 +    ContextOptions()
  1.1540 +      : extraWarnings_(false),
  1.1541 +        werror_(false),
  1.1542 +        varObjFix_(false),
  1.1543 +        privateIsNSISupports_(false),
  1.1544 +        dontReportUncaught_(false),
  1.1545 +        noDefaultCompartmentObject_(false),
  1.1546 +        noScriptRval_(false),
  1.1547 +        strictMode_(false),
  1.1548 +        cloneSingletons_(false)
  1.1549 +    {
  1.1550 +    }
  1.1551 +
  1.1552 +    bool extraWarnings() const { return extraWarnings_; }
  1.1553 +    ContextOptions &setExtraWarnings(bool flag) {
  1.1554 +        extraWarnings_ = flag;
  1.1555 +        return *this;
  1.1556 +    }
  1.1557 +    ContextOptions &toggleExtraWarnings() {
  1.1558 +        extraWarnings_ = !extraWarnings_;
  1.1559 +        return *this;
  1.1560 +    }
  1.1561 +
  1.1562 +    bool werror() const { return werror_; }
  1.1563 +    ContextOptions &setWerror(bool flag) {
  1.1564 +        werror_ = flag;
  1.1565 +        return *this;
  1.1566 +    }
  1.1567 +    ContextOptions &toggleWerror() {
  1.1568 +        werror_ = !werror_;
  1.1569 +        return *this;
  1.1570 +    }
  1.1571 +
  1.1572 +    bool varObjFix() const { return varObjFix_; }
  1.1573 +    ContextOptions &setVarObjFix(bool flag) {
  1.1574 +        varObjFix_ = flag;
  1.1575 +        return *this;
  1.1576 +    }
  1.1577 +    ContextOptions &toggleVarObjFix() {
  1.1578 +        varObjFix_ = !varObjFix_;
  1.1579 +        return *this;
  1.1580 +    }
  1.1581 +
  1.1582 +    bool privateIsNSISupports() const { return privateIsNSISupports_; }
  1.1583 +    ContextOptions &setPrivateIsNSISupports(bool flag) {
  1.1584 +        privateIsNSISupports_ = flag;
  1.1585 +        return *this;
  1.1586 +    }
  1.1587 +    ContextOptions &togglePrivateIsNSISupports() {
  1.1588 +        privateIsNSISupports_ = !privateIsNSISupports_;
  1.1589 +        return *this;
  1.1590 +    }
  1.1591 +
  1.1592 +    bool dontReportUncaught() const { return dontReportUncaught_; }
  1.1593 +    ContextOptions &setDontReportUncaught(bool flag) {
  1.1594 +        dontReportUncaught_ = flag;
  1.1595 +        return *this;
  1.1596 +    }
  1.1597 +    ContextOptions &toggleDontReportUncaught() {
  1.1598 +        dontReportUncaught_ = !dontReportUncaught_;
  1.1599 +        return *this;
  1.1600 +    }
  1.1601 +
  1.1602 +    bool noDefaultCompartmentObject() const { return noDefaultCompartmentObject_; }
  1.1603 +    ContextOptions &setNoDefaultCompartmentObject(bool flag) {
  1.1604 +        noDefaultCompartmentObject_ = flag;
  1.1605 +        return *this;
  1.1606 +    }
  1.1607 +    ContextOptions &toggleNoDefaultCompartmentObject() {
  1.1608 +        noDefaultCompartmentObject_ = !noDefaultCompartmentObject_;
  1.1609 +        return *this;
  1.1610 +    }
  1.1611 +
  1.1612 +    bool noScriptRval() const { return noScriptRval_; }
  1.1613 +    ContextOptions &setNoScriptRval(bool flag) {
  1.1614 +        noScriptRval_ = flag;
  1.1615 +        return *this;
  1.1616 +    }
  1.1617 +    ContextOptions &toggleNoScriptRval() {
  1.1618 +        noScriptRval_ = !noScriptRval_;
  1.1619 +        return *this;
  1.1620 +    }
  1.1621 +
  1.1622 +    bool strictMode() const { return strictMode_; }
  1.1623 +    ContextOptions &setStrictMode(bool flag) {
  1.1624 +        strictMode_ = flag;
  1.1625 +        return *this;
  1.1626 +    }
  1.1627 +    ContextOptions &toggleStrictMode() {
  1.1628 +        strictMode_ = !strictMode_;
  1.1629 +        return *this;
  1.1630 +    }
  1.1631 +
  1.1632 +    bool cloneSingletons() const { return cloneSingletons_; }
  1.1633 +    ContextOptions &setCloneSingletons(bool flag) {
  1.1634 +        cloneSingletons_ = flag;
  1.1635 +        return *this;
  1.1636 +    }
  1.1637 +    ContextOptions &toggleCloneSingletons() {
  1.1638 +        cloneSingletons_ = !cloneSingletons_;
  1.1639 +        return *this;
  1.1640 +    }
  1.1641 +
  1.1642 +  private:
  1.1643 +    bool extraWarnings_ : 1;
  1.1644 +    bool werror_ : 1;
  1.1645 +    bool varObjFix_ : 1;
  1.1646 +    bool privateIsNSISupports_ : 1;
  1.1647 +    bool dontReportUncaught_ : 1;
  1.1648 +    bool noDefaultCompartmentObject_ : 1;
  1.1649 +    bool noScriptRval_ : 1;
  1.1650 +    bool strictMode_ : 1;
  1.1651 +    bool cloneSingletons_ : 1;
  1.1652 +};
  1.1653 +
  1.1654 +JS_PUBLIC_API(ContextOptions &)
  1.1655 +ContextOptionsRef(JSContext *cx);
  1.1656 +
  1.1657 +class JS_PUBLIC_API(AutoSaveContextOptions) {
  1.1658 +  public:
  1.1659 +    AutoSaveContextOptions(JSContext *cx)
  1.1660 +      : cx_(cx),
  1.1661 +        oldOptions_(ContextOptionsRef(cx_))
  1.1662 +    {
  1.1663 +    }
  1.1664 +
  1.1665 +    ~AutoSaveContextOptions()
  1.1666 +    {
  1.1667 +        ContextOptionsRef(cx_) = oldOptions_;
  1.1668 +    }
  1.1669 +
  1.1670 +  private:
  1.1671 +    JSContext *cx_;
  1.1672 +    JS::ContextOptions oldOptions_;
  1.1673 +};
  1.1674 +
  1.1675 +} /* namespace JS */
  1.1676 +
  1.1677 +extern JS_PUBLIC_API(const char *)
  1.1678 +JS_GetImplementationVersion(void);
  1.1679 +
  1.1680 +extern JS_PUBLIC_API(void)
  1.1681 +JS_SetDestroyCompartmentCallback(JSRuntime *rt, JSDestroyCompartmentCallback callback);
  1.1682 +
  1.1683 +extern JS_PUBLIC_API(void)
  1.1684 +JS_SetDestroyZoneCallback(JSRuntime *rt, JSZoneCallback callback);
  1.1685 +
  1.1686 +extern JS_PUBLIC_API(void)
  1.1687 +JS_SetSweepZoneCallback(JSRuntime *rt, JSZoneCallback callback);
  1.1688 +
  1.1689 +extern JS_PUBLIC_API(void)
  1.1690 +JS_SetCompartmentNameCallback(JSRuntime *rt, JSCompartmentNameCallback callback);
  1.1691 +
  1.1692 +extern JS_PUBLIC_API(void)
  1.1693 +JS_SetWrapObjectCallbacks(JSRuntime *rt, const JSWrapObjectCallbacks *callbacks);
  1.1694 +
  1.1695 +extern JS_PUBLIC_API(void)
  1.1696 +JS_SetCompartmentPrivate(JSCompartment *compartment, void *data);
  1.1697 +
  1.1698 +extern JS_PUBLIC_API(void *)
  1.1699 +JS_GetCompartmentPrivate(JSCompartment *compartment);
  1.1700 +
  1.1701 +extern JS_PUBLIC_API(void)
  1.1702 +JS_SetZoneUserData(JS::Zone *zone, void *data);
  1.1703 +
  1.1704 +extern JS_PUBLIC_API(void *)
  1.1705 +JS_GetZoneUserData(JS::Zone *zone);
  1.1706 +
  1.1707 +extern JS_PUBLIC_API(bool)
  1.1708 +JS_WrapObject(JSContext *cx, JS::MutableHandleObject objp);
  1.1709 +
  1.1710 +extern JS_PUBLIC_API(bool)
  1.1711 +JS_WrapValue(JSContext *cx, JS::MutableHandleValue vp);
  1.1712 +
  1.1713 +extern JS_PUBLIC_API(bool)
  1.1714 +JS_WrapId(JSContext *cx, JS::MutableHandleId idp);
  1.1715 +
  1.1716 +extern JS_PUBLIC_API(JSObject *)
  1.1717 +JS_TransplantObject(JSContext *cx, JS::HandleObject origobj, JS::HandleObject target);
  1.1718 +
  1.1719 +extern JS_PUBLIC_API(bool)
  1.1720 +JS_RefreshCrossCompartmentWrappers(JSContext *cx, JS::Handle<JSObject*> obj);
  1.1721 +
  1.1722 +/*
  1.1723 + * At any time, a JSContext has a current (possibly-nullptr) compartment.
  1.1724 + * Compartments are described in:
  1.1725 + *
  1.1726 + *   developer.mozilla.org/en-US/docs/SpiderMonkey/SpiderMonkey_compartments
  1.1727 + *
  1.1728 + * The current compartment of a context may be changed. The preferred way to do
  1.1729 + * this is with JSAutoCompartment:
  1.1730 + *
  1.1731 + *   void foo(JSContext *cx, JSObject *obj) {
  1.1732 + *     // in some compartment 'c'
  1.1733 + *     {
  1.1734 + *       JSAutoCompartment ac(cx, obj);  // constructor enters
  1.1735 + *       // in the compartment of 'obj'
  1.1736 + *     }                                 // destructor leaves
  1.1737 + *     // back in compartment 'c'
  1.1738 + *   }
  1.1739 + *
  1.1740 + * For more complicated uses that don't neatly fit in a C++ stack frame, the
  1.1741 + * compartment can entered and left using separate function calls:
  1.1742 + *
  1.1743 + *   void foo(JSContext *cx, JSObject *obj) {
  1.1744 + *     // in 'oldCompartment'
  1.1745 + *     JSCompartment *oldCompartment = JS_EnterCompartment(cx, obj);
  1.1746 + *     // in the compartment of 'obj'
  1.1747 + *     JS_LeaveCompartment(cx, oldCompartment);
  1.1748 + *     // back in 'oldCompartment'
  1.1749 + *   }
  1.1750 + *
  1.1751 + * Note: these calls must still execute in a LIFO manner w.r.t all other
  1.1752 + * enter/leave calls on the context. Furthermore, only the return value of a
  1.1753 + * JS_EnterCompartment call may be passed as the 'oldCompartment' argument of
  1.1754 + * the corresponding JS_LeaveCompartment call.
  1.1755 + */
  1.1756 +
  1.1757 +class JS_PUBLIC_API(JSAutoCompartment)
  1.1758 +{
  1.1759 +    JSContext *cx_;
  1.1760 +    JSCompartment *oldCompartment_;
  1.1761 +  public:
  1.1762 +    JSAutoCompartment(JSContext *cx, JSObject *target);
  1.1763 +    JSAutoCompartment(JSContext *cx, JSScript *target);
  1.1764 +    ~JSAutoCompartment();
  1.1765 +};
  1.1766 +
  1.1767 +class JS_PUBLIC_API(JSAutoNullCompartment)
  1.1768 +{
  1.1769 +    JSContext *cx_;
  1.1770 +    JSCompartment *oldCompartment_;
  1.1771 +  public:
  1.1772 +    JSAutoNullCompartment(JSContext *cx);
  1.1773 +    ~JSAutoNullCompartment();
  1.1774 +};
  1.1775 +
  1.1776 +/* NB: This API is infallible; a nullptr return value does not indicate error. */
  1.1777 +extern JS_PUBLIC_API(JSCompartment *)
  1.1778 +JS_EnterCompartment(JSContext *cx, JSObject *target);
  1.1779 +
  1.1780 +extern JS_PUBLIC_API(void)
  1.1781 +JS_LeaveCompartment(JSContext *cx, JSCompartment *oldCompartment);
  1.1782 +
  1.1783 +typedef void (*JSIterateCompartmentCallback)(JSRuntime *rt, void *data, JSCompartment *compartment);
  1.1784 +
  1.1785 +/*
  1.1786 + * This function calls |compartmentCallback| on every compartment.  Beware that
  1.1787 + * there is no guarantee that the compartment will survive after the callback
  1.1788 + * returns.
  1.1789 + */
  1.1790 +extern JS_PUBLIC_API(void)
  1.1791 +JS_IterateCompartments(JSRuntime *rt, void *data,
  1.1792 +                       JSIterateCompartmentCallback compartmentCallback);
  1.1793 +
  1.1794 +/*
  1.1795 + * Initialize standard JS class constructors, prototypes, and any top-level
  1.1796 + * functions and constants associated with the standard classes (e.g. isNaN
  1.1797 + * for Number).
  1.1798 + *
  1.1799 + * NB: This sets cx's global object to obj if it was null.
  1.1800 + */
  1.1801 +extern JS_PUBLIC_API(bool)
  1.1802 +JS_InitStandardClasses(JSContext *cx, JS::Handle<JSObject*> obj);
  1.1803 +
  1.1804 +/*
  1.1805 + * Resolve id, which must contain either a string or an int, to a standard
  1.1806 + * class name in obj if possible, defining the class's constructor and/or
  1.1807 + * prototype and storing true in *resolved.  If id does not name a standard
  1.1808 + * class or a top-level property induced by initializing a standard class,
  1.1809 + * store false in *resolved and just return true.  Return false on error,
  1.1810 + * as usual for bool result-typed API entry points.
  1.1811 + *
  1.1812 + * This API can be called directly from a global object class's resolve op,
  1.1813 + * to define standard classes lazily.  The class's enumerate op should call
  1.1814 + * JS_EnumerateStandardClasses(cx, obj), to define eagerly during for..in
  1.1815 + * loops any classes not yet resolved lazily.
  1.1816 + */
  1.1817 +extern JS_PUBLIC_API(bool)
  1.1818 +JS_ResolveStandardClass(JSContext *cx, JS::HandleObject obj, JS::HandleId id, bool *resolved);
  1.1819 +
  1.1820 +extern JS_PUBLIC_API(bool)
  1.1821 +JS_EnumerateStandardClasses(JSContext *cx, JS::HandleObject obj);
  1.1822 +
  1.1823 +extern JS_PUBLIC_API(bool)
  1.1824 +JS_GetClassObject(JSContext *cx, JSProtoKey key, JS::MutableHandle<JSObject*> objp);
  1.1825 +
  1.1826 +extern JS_PUBLIC_API(bool)
  1.1827 +JS_GetClassPrototype(JSContext *cx, JSProtoKey key, JS::MutableHandle<JSObject*> objp);
  1.1828 +
  1.1829 +namespace JS {
  1.1830 +
  1.1831 +/*
  1.1832 + * Determine if the given object is an instance or prototype for a standard
  1.1833 + * class. If so, return the associated JSProtoKey. If not, return JSProto_Null.
  1.1834 + */
  1.1835 +
  1.1836 +extern JS_PUBLIC_API(JSProtoKey)
  1.1837 +IdentifyStandardInstance(JSObject *obj);
  1.1838 +
  1.1839 +extern JS_PUBLIC_API(JSProtoKey)
  1.1840 +IdentifyStandardPrototype(JSObject *obj);
  1.1841 +
  1.1842 +extern JS_PUBLIC_API(JSProtoKey)
  1.1843 +IdentifyStandardInstanceOrPrototype(JSObject *obj);
  1.1844 +
  1.1845 +} /* namespace JS */
  1.1846 +
  1.1847 +extern JS_PUBLIC_API(JSProtoKey)
  1.1848 +JS_IdToProtoKey(JSContext *cx, JS::HandleId id);
  1.1849 +
  1.1850 +/*
  1.1851 + * Returns the original value of |Function.prototype| from the global object in
  1.1852 + * which |forObj| was created.
  1.1853 + */
  1.1854 +extern JS_PUBLIC_API(JSObject *)
  1.1855 +JS_GetFunctionPrototype(JSContext *cx, JS::HandleObject forObj);
  1.1856 +
  1.1857 +/*
  1.1858 + * Returns the original value of |Object.prototype| from the global object in
  1.1859 + * which |forObj| was created.
  1.1860 + */
  1.1861 +extern JS_PUBLIC_API(JSObject *)
  1.1862 +JS_GetObjectPrototype(JSContext *cx, JS::HandleObject forObj);
  1.1863 +
  1.1864 +/*
  1.1865 + * Returns the original value of |Array.prototype| from the global object in
  1.1866 + * which |forObj| was created.
  1.1867 + */
  1.1868 +extern JS_PUBLIC_API(JSObject *)
  1.1869 +JS_GetArrayPrototype(JSContext *cx, JS::HandleObject forObj);
  1.1870 +
  1.1871 +extern JS_PUBLIC_API(JSObject *)
  1.1872 +JS_GetGlobalForObject(JSContext *cx, JSObject *obj);
  1.1873 +
  1.1874 +extern JS_PUBLIC_API(bool)
  1.1875 +JS_IsGlobalObject(JSObject *obj);
  1.1876 +
  1.1877 +/*
  1.1878 + * May return nullptr, if |c| never had a global (e.g. the atoms compartment),
  1.1879 + * or if |c|'s global has been collected.
  1.1880 + */
  1.1881 +extern JS_PUBLIC_API(JSObject *)
  1.1882 +JS_GetGlobalForCompartmentOrNull(JSContext *cx, JSCompartment *c);
  1.1883 +
  1.1884 +namespace JS {
  1.1885 +
  1.1886 +extern JS_PUBLIC_API(JSObject *)
  1.1887 +CurrentGlobalOrNull(JSContext *cx);
  1.1888 +
  1.1889 +}
  1.1890 +
  1.1891 +/*
  1.1892 + * Initialize the 'Reflect' object on a global object.
  1.1893 + */
  1.1894 +extern JS_PUBLIC_API(JSObject *)
  1.1895 +JS_InitReflect(JSContext *cx, JS::HandleObject global);
  1.1896 +
  1.1897 +#ifdef JS_HAS_CTYPES
  1.1898 +/*
  1.1899 + * Initialize the 'ctypes' object on a global variable 'obj'. The 'ctypes'
  1.1900 + * object will be sealed.
  1.1901 + */
  1.1902 +extern JS_PUBLIC_API(bool)
  1.1903 +JS_InitCTypesClass(JSContext *cx, JS::HandleObject global);
  1.1904 +
  1.1905 +/*
  1.1906 + * Convert a unicode string 'source' of length 'slen' to the platform native
  1.1907 + * charset, returning a null-terminated string allocated with JS_malloc. On
  1.1908 + * failure, this function should report an error.
  1.1909 + */
  1.1910 +typedef char *
  1.1911 +(* JSCTypesUnicodeToNativeFun)(JSContext *cx, const jschar *source, size_t slen);
  1.1912 +
  1.1913 +/*
  1.1914 + * Set of function pointers that ctypes can use for various internal functions.
  1.1915 + * See JS_SetCTypesCallbacks below. Providing nullptr for a function is safe,
  1.1916 + * and will result in the applicable ctypes functionality not being available.
  1.1917 + */
  1.1918 +struct JSCTypesCallbacks {
  1.1919 +    JSCTypesUnicodeToNativeFun unicodeToNative;
  1.1920 +};
  1.1921 +
  1.1922 +typedef struct JSCTypesCallbacks JSCTypesCallbacks;
  1.1923 +
  1.1924 +/*
  1.1925 + * Set the callbacks on the provided 'ctypesObj' object. 'callbacks' should be a
  1.1926 + * pointer to static data that exists for the lifetime of 'ctypesObj', but it
  1.1927 + * may safely be altered after calling this function and without having
  1.1928 + * to call this function again.
  1.1929 + */
  1.1930 +extern JS_PUBLIC_API(void)
  1.1931 +JS_SetCTypesCallbacks(JSObject *ctypesObj, JSCTypesCallbacks *callbacks);
  1.1932 +#endif
  1.1933 +
  1.1934 +typedef bool
  1.1935 +(* JSEnumerateDiagnosticMemoryCallback)(void *ptr, size_t length);
  1.1936 +
  1.1937 +/*
  1.1938 + * Enumerate memory regions that contain diagnostic information
  1.1939 + * intended to be included in crash report minidumps.
  1.1940 + */
  1.1941 +extern JS_PUBLIC_API(void)
  1.1942 +JS_EnumerateDiagnosticMemoryRegions(JSEnumerateDiagnosticMemoryCallback callback);
  1.1943 +
  1.1944 +extern JS_PUBLIC_API(void *)
  1.1945 +JS_malloc(JSContext *cx, size_t nbytes);
  1.1946 +
  1.1947 +extern JS_PUBLIC_API(void *)
  1.1948 +JS_realloc(JSContext *cx, void *p, size_t nbytes);
  1.1949 +
  1.1950 +/*
  1.1951 + * A wrapper for js_free(p) that may delay js_free(p) invocation as a
  1.1952 + * performance optimization.
  1.1953 + * cx may be nullptr.
  1.1954 + */
  1.1955 +extern JS_PUBLIC_API(void)
  1.1956 +JS_free(JSContext *cx, void *p);
  1.1957 +
  1.1958 +/*
  1.1959 + * A wrapper for js_free(p) that may delay js_free(p) invocation as a
  1.1960 + * performance optimization as specified by the given JSFreeOp instance.
  1.1961 + */
  1.1962 +extern JS_PUBLIC_API(void)
  1.1963 +JS_freeop(JSFreeOp *fop, void *p);
  1.1964 +
  1.1965 +extern JS_PUBLIC_API(JSFreeOp *)
  1.1966 +JS_GetDefaultFreeOp(JSRuntime *rt);
  1.1967 +
  1.1968 +extern JS_PUBLIC_API(void)
  1.1969 +JS_updateMallocCounter(JSContext *cx, size_t nbytes);
  1.1970 +
  1.1971 +extern JS_PUBLIC_API(char *)
  1.1972 +JS_strdup(JSContext *cx, const char *s);
  1.1973 +
  1.1974 +/* Duplicate a string.  Does not report an error on failure. */
  1.1975 +extern JS_PUBLIC_API(char *)
  1.1976 +JS_strdup(JSRuntime *rt, const char *s);
  1.1977 +
  1.1978 +namespace JS {
  1.1979 +
  1.1980 +/*
  1.1981 + * A GC root is a pointer to a jsval, JSObject * or JSString * that itself
  1.1982 + * points into the GC heap. JS_AddValueRoot takes a pointer to a jsval and
  1.1983 + * JS_AddGCThingRoot takes a pointer to a JSObject * or JString *.
  1.1984 + *
  1.1985 + * Note that, since JS_Add*Root stores the address of a variable (of type
  1.1986 + * jsval, JSString *, or JSObject *), that variable must live until
  1.1987 + * JS_Remove*Root is called to remove that variable. For example, after:
  1.1988 + *
  1.1989 + *   void some_function() {
  1.1990 + *     jsval v;
  1.1991 + *     JS_AddNamedValueRoot(cx, &v, "name");
  1.1992 + *
  1.1993 + * the caller must perform
  1.1994 + *
  1.1995 + *     JS_RemoveValueRoot(cx, &v);
  1.1996 + *
  1.1997 + * before some_function() returns.
  1.1998 + *
  1.1999 + * Also, use JS_AddNamed*Root(cx, &structPtr->memberObj, "structPtr->memberObj")
  1.2000 + * in preference to JS_Add*Root(cx, &structPtr->memberObj), in order to identify
  1.2001 + * roots by their source callsites.  This way, you can find the callsite while
  1.2002 + * debugging if you should fail to do JS_Remove*Root(cx, &structPtr->memberObj)
  1.2003 + * before freeing structPtr's memory.
  1.2004 + */
  1.2005 +extern JS_PUBLIC_API(bool)
  1.2006 +AddValueRoot(JSContext *cx, JS::Heap<JS::Value> *vp);
  1.2007 +
  1.2008 +extern JS_PUBLIC_API(bool)
  1.2009 +AddStringRoot(JSContext *cx, JS::Heap<JSString *> *rp);
  1.2010 +
  1.2011 +extern JS_PUBLIC_API(bool)
  1.2012 +AddObjectRoot(JSContext *cx, JS::Heap<JSObject *> *rp);
  1.2013 +
  1.2014 +extern JS_PUBLIC_API(bool)
  1.2015 +AddNamedValueRoot(JSContext *cx, JS::Heap<JS::Value> *vp, const char *name);
  1.2016 +
  1.2017 +extern JS_PUBLIC_API(bool)
  1.2018 +AddNamedValueRootRT(JSRuntime *rt, JS::Heap<JS::Value> *vp, const char *name);
  1.2019 +
  1.2020 +extern JS_PUBLIC_API(bool)
  1.2021 +AddNamedStringRoot(JSContext *cx, JS::Heap<JSString *> *rp, const char *name);
  1.2022 +
  1.2023 +extern JS_PUBLIC_API(bool)
  1.2024 +AddNamedObjectRoot(JSContext *cx, JS::Heap<JSObject *> *rp, const char *name);
  1.2025 +
  1.2026 +extern JS_PUBLIC_API(bool)
  1.2027 +AddNamedScriptRoot(JSContext *cx, JS::Heap<JSScript *> *rp, const char *name);
  1.2028 +
  1.2029 +extern JS_PUBLIC_API(void)
  1.2030 +RemoveValueRoot(JSContext *cx, JS::Heap<JS::Value> *vp);
  1.2031 +
  1.2032 +extern JS_PUBLIC_API(void)
  1.2033 +RemoveStringRoot(JSContext *cx, JS::Heap<JSString *> *rp);
  1.2034 +
  1.2035 +extern JS_PUBLIC_API(void)
  1.2036 +RemoveObjectRoot(JSContext *cx, JS::Heap<JSObject *> *rp);
  1.2037 +
  1.2038 +extern JS_PUBLIC_API(void)
  1.2039 +RemoveScriptRoot(JSContext *cx, JS::Heap<JSScript *> *rp);
  1.2040 +
  1.2041 +extern JS_PUBLIC_API(void)
  1.2042 +RemoveValueRootRT(JSRuntime *rt, JS::Heap<JS::Value> *vp);
  1.2043 +
  1.2044 +extern JS_PUBLIC_API(void)
  1.2045 +RemoveStringRootRT(JSRuntime *rt, JS::Heap<JSString *> *rp);
  1.2046 +
  1.2047 +extern JS_PUBLIC_API(void)
  1.2048 +RemoveObjectRootRT(JSRuntime *rt, JS::Heap<JSObject *> *rp);
  1.2049 +
  1.2050 +extern JS_PUBLIC_API(void)
  1.2051 +RemoveScriptRootRT(JSRuntime *rt, JS::Heap<JSScript *> *rp);
  1.2052 +
  1.2053 +} /* namespace JS */
  1.2054 +
  1.2055 +/*
  1.2056 + * Register externally maintained GC roots.
  1.2057 + *
  1.2058 + * traceOp: the trace operation. For each root the implementation should call
  1.2059 + *          JS_CallTracer whenever the root contains a traceable thing.
  1.2060 + * data:    the data argument to pass to each invocation of traceOp.
  1.2061 + */
  1.2062 +extern JS_PUBLIC_API(bool)
  1.2063 +JS_AddExtraGCRootsTracer(JSRuntime *rt, JSTraceDataOp traceOp, void *data);
  1.2064 +
  1.2065 +/* Undo a call to JS_AddExtraGCRootsTracer. */
  1.2066 +extern JS_PUBLIC_API(void)
  1.2067 +JS_RemoveExtraGCRootsTracer(JSRuntime *rt, JSTraceDataOp traceOp, void *data);
  1.2068 +
  1.2069 +#ifdef JS_DEBUG
  1.2070 +
  1.2071 +/*
  1.2072 + * Debug-only method to dump the object graph of heap-allocated things.
  1.2073 + *
  1.2074 + * fp:              file for the dump output.
  1.2075 + * start:           when non-null, dump only things reachable from start
  1.2076 + *                  thing. Otherwise dump all things reachable from the
  1.2077 + *                  runtime roots.
  1.2078 + * startKind:       trace kind of start if start is not null. Must be
  1.2079 + *                  JSTRACE_OBJECT when start is null.
  1.2080 + * thingToFind:     dump only paths in the object graph leading to thingToFind
  1.2081 + *                  when non-null.
  1.2082 + * maxDepth:        the upper bound on the number of edges to descend from the
  1.2083 + *                  graph roots.
  1.2084 + * thingToIgnore:   thing to ignore during the graph traversal when non-null.
  1.2085 + */
  1.2086 +extern JS_PUBLIC_API(bool)
  1.2087 +JS_DumpHeap(JSRuntime *rt, FILE *fp, void* startThing, JSGCTraceKind kind,
  1.2088 +            void *thingToFind, size_t maxDepth, void *thingToIgnore);
  1.2089 +
  1.2090 +#endif
  1.2091 +
  1.2092 +/*
  1.2093 + * Garbage collector API.
  1.2094 + */
  1.2095 +extern JS_PUBLIC_API(void)
  1.2096 +JS_GC(JSRuntime *rt);
  1.2097 +
  1.2098 +extern JS_PUBLIC_API(void)
  1.2099 +JS_MaybeGC(JSContext *cx);
  1.2100 +
  1.2101 +extern JS_PUBLIC_API(void)
  1.2102 +JS_SetGCCallback(JSRuntime *rt, JSGCCallback cb, void *data);
  1.2103 +
  1.2104 +extern JS_PUBLIC_API(void)
  1.2105 +JS_SetFinalizeCallback(JSRuntime *rt, JSFinalizeCallback cb);
  1.2106 +
  1.2107 +extern JS_PUBLIC_API(bool)
  1.2108 +JS_IsGCMarkingTracer(JSTracer *trc);
  1.2109 +
  1.2110 +/* For assertions only. */
  1.2111 +#ifdef JS_DEBUG
  1.2112 +extern JS_PUBLIC_API(bool)
  1.2113 +JS_IsMarkingGray(JSTracer *trc);
  1.2114 +#endif
  1.2115 +
  1.2116 +/*
  1.2117 + * JS_IsAboutToBeFinalized checks if the given object is going to be finalized
  1.2118 + * at the end of the current GC. When called outside of the context of a GC,
  1.2119 + * this function will return false. Typically this function is used on weak
  1.2120 + * references, where the reference should be nulled out or destroyed if the
  1.2121 + * given object is about to be finalized.
  1.2122 + *
  1.2123 + * The argument to JS_IsAboutToBeFinalized is an in-out param: when the
  1.2124 + * function returns false, the object being referenced is still alive, but the
  1.2125 + * garbage collector might have moved it. In this case, the reference passed
  1.2126 + * to JS_IsAboutToBeFinalized will be updated to the object's new location.
  1.2127 + * Callers of this method are responsible for updating any state that is
  1.2128 + * dependent on the object's address. For example, if the object's address is
  1.2129 + * used as a key in a hashtable, then the object must be removed and
  1.2130 + * re-inserted with the correct hash.
  1.2131 + */
  1.2132 +extern JS_PUBLIC_API(bool)
  1.2133 +JS_IsAboutToBeFinalized(JS::Heap<JSObject *> *objp);
  1.2134 +
  1.2135 +extern JS_PUBLIC_API(bool)
  1.2136 +JS_IsAboutToBeFinalizedUnbarriered(JSObject **objp);
  1.2137 +
  1.2138 +typedef enum JSGCParamKey {
  1.2139 +    /* Maximum nominal heap before last ditch GC. */
  1.2140 +    JSGC_MAX_BYTES          = 0,
  1.2141 +
  1.2142 +    /* Number of JS_malloc bytes before last ditch GC. */
  1.2143 +    JSGC_MAX_MALLOC_BYTES   = 1,
  1.2144 +
  1.2145 +    /* Amount of bytes allocated by the GC. */
  1.2146 +    JSGC_BYTES = 3,
  1.2147 +
  1.2148 +    /* Number of times when GC was invoked. */
  1.2149 +    JSGC_NUMBER = 4,
  1.2150 +
  1.2151 +    /* Max size of the code cache in bytes. */
  1.2152 +    JSGC_MAX_CODE_CACHE_BYTES = 5,
  1.2153 +
  1.2154 +    /* Select GC mode. */
  1.2155 +    JSGC_MODE = 6,
  1.2156 +
  1.2157 +    /* Number of cached empty GC chunks. */
  1.2158 +    JSGC_UNUSED_CHUNKS = 7,
  1.2159 +
  1.2160 +    /* Total number of allocated GC chunks. */
  1.2161 +    JSGC_TOTAL_CHUNKS = 8,
  1.2162 +
  1.2163 +    /* Max milliseconds to spend in an incremental GC slice. */
  1.2164 +    JSGC_SLICE_TIME_BUDGET = 9,
  1.2165 +
  1.2166 +    /* Maximum size the GC mark stack can grow to. */
  1.2167 +    JSGC_MARK_STACK_LIMIT = 10,
  1.2168 +
  1.2169 +    /*
  1.2170 +     * GCs less than this far apart in time will be considered 'high-frequency GCs'.
  1.2171 +     * See setGCLastBytes in jsgc.cpp.
  1.2172 +     */
  1.2173 +    JSGC_HIGH_FREQUENCY_TIME_LIMIT = 11,
  1.2174 +
  1.2175 +    /* Start of dynamic heap growth. */
  1.2176 +    JSGC_HIGH_FREQUENCY_LOW_LIMIT = 12,
  1.2177 +
  1.2178 +    /* End of dynamic heap growth. */
  1.2179 +    JSGC_HIGH_FREQUENCY_HIGH_LIMIT = 13,
  1.2180 +
  1.2181 +    /* Upper bound of heap growth. */
  1.2182 +    JSGC_HIGH_FREQUENCY_HEAP_GROWTH_MAX = 14,
  1.2183 +
  1.2184 +    /* Lower bound of heap growth. */
  1.2185 +    JSGC_HIGH_FREQUENCY_HEAP_GROWTH_MIN = 15,
  1.2186 +
  1.2187 +    /* Heap growth for low frequency GCs. */
  1.2188 +    JSGC_LOW_FREQUENCY_HEAP_GROWTH = 16,
  1.2189 +
  1.2190 +    /*
  1.2191 +     * If false, the heap growth factor is fixed at 3. If true, it is determined
  1.2192 +     * based on whether GCs are high- or low- frequency.
  1.2193 +     */
  1.2194 +    JSGC_DYNAMIC_HEAP_GROWTH = 17,
  1.2195 +
  1.2196 +    /* If true, high-frequency GCs will use a longer mark slice. */
  1.2197 +    JSGC_DYNAMIC_MARK_SLICE = 18,
  1.2198 +
  1.2199 +    /* Lower limit after which we limit the heap growth. */
  1.2200 +    JSGC_ALLOCATION_THRESHOLD = 19,
  1.2201 +
  1.2202 +    /*
  1.2203 +     * We decommit memory lazily. If more than this number of megabytes is
  1.2204 +     * available to be decommitted, then JS_MaybeGC will trigger a shrinking GC
  1.2205 +     * to decommit it.
  1.2206 +     */
  1.2207 +    JSGC_DECOMMIT_THRESHOLD = 20
  1.2208 +} JSGCParamKey;
  1.2209 +
  1.2210 +extern JS_PUBLIC_API(void)
  1.2211 +JS_SetGCParameter(JSRuntime *rt, JSGCParamKey key, uint32_t value);
  1.2212 +
  1.2213 +extern JS_PUBLIC_API(uint32_t)
  1.2214 +JS_GetGCParameter(JSRuntime *rt, JSGCParamKey key);
  1.2215 +
  1.2216 +extern JS_PUBLIC_API(void)
  1.2217 +JS_SetGCParameterForThread(JSContext *cx, JSGCParamKey key, uint32_t value);
  1.2218 +
  1.2219 +extern JS_PUBLIC_API(uint32_t)
  1.2220 +JS_GetGCParameterForThread(JSContext *cx, JSGCParamKey key);
  1.2221 +
  1.2222 +extern JS_PUBLIC_API(void)
  1.2223 +JS_SetGCParametersBasedOnAvailableMemory(JSRuntime *rt, uint32_t availMem);
  1.2224 +
  1.2225 +/*
  1.2226 + * Create a new JSString whose chars member refers to external memory, i.e.,
  1.2227 + * memory requiring application-specific finalization.
  1.2228 + */
  1.2229 +extern JS_PUBLIC_API(JSString *)
  1.2230 +JS_NewExternalString(JSContext *cx, const jschar *chars, size_t length,
  1.2231 +                     const JSStringFinalizer *fin);
  1.2232 +
  1.2233 +/*
  1.2234 + * Return whether 'str' was created with JS_NewExternalString or
  1.2235 + * JS_NewExternalStringWithClosure.
  1.2236 + */
  1.2237 +extern JS_PUBLIC_API(bool)
  1.2238 +JS_IsExternalString(JSString *str);
  1.2239 +
  1.2240 +/*
  1.2241 + * Return the 'closure' arg passed to JS_NewExternalStringWithClosure or
  1.2242 + * nullptr if the external string was created via JS_NewExternalString.
  1.2243 + */
  1.2244 +extern JS_PUBLIC_API(const JSStringFinalizer *)
  1.2245 +JS_GetExternalStringFinalizer(JSString *str);
  1.2246 +
  1.2247 +/*
  1.2248 + * Set the size of the native stack that should not be exceed. To disable
  1.2249 + * stack size checking pass 0.
  1.2250 + *
  1.2251 + * SpiderMonkey allows for a distinction between system code (such as GCs, which
  1.2252 + * may incidentally be triggered by script but are not strictly performed on
  1.2253 + * behalf of such script), trusted script (as determined by JS_SetTrustedPrincipals),
  1.2254 + * and untrusted script. Each kind of code may have a different stack quota,
  1.2255 + * allowing embedders to keep higher-priority machinery running in the face of
  1.2256 + * scripted stack exhaustion by something else.
  1.2257 + *
  1.2258 + * The stack quotas for each kind of code should be monotonically descending,
  1.2259 + * and may be specified with this function. If 0 is passed for a given kind
  1.2260 + * of code, it defaults to the value of the next-highest-priority kind.
  1.2261 + */
  1.2262 +extern JS_PUBLIC_API(void)
  1.2263 +JS_SetNativeStackQuota(JSRuntime *cx, size_t systemCodeStackSize,
  1.2264 +                       size_t trustedScriptStackSize = 0,
  1.2265 +                       size_t untrustedScriptStackSize = 0);
  1.2266 +
  1.2267 +/************************************************************************/
  1.2268 +
  1.2269 +extern JS_PUBLIC_API(int)
  1.2270 +JS_IdArrayLength(JSContext *cx, JSIdArray *ida);
  1.2271 +
  1.2272 +extern JS_PUBLIC_API(jsid)
  1.2273 +JS_IdArrayGet(JSContext *cx, JSIdArray *ida, int index);
  1.2274 +
  1.2275 +extern JS_PUBLIC_API(void)
  1.2276 +JS_DestroyIdArray(JSContext *cx, JSIdArray *ida);
  1.2277 +
  1.2278 +namespace JS {
  1.2279 +
  1.2280 +class AutoIdArray : private AutoGCRooter
  1.2281 +{
  1.2282 +  public:
  1.2283 +    AutoIdArray(JSContext *cx, JSIdArray *ida
  1.2284 +                MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
  1.2285 +      : AutoGCRooter(cx, IDARRAY), context(cx), idArray(ida)
  1.2286 +    {
  1.2287 +        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
  1.2288 +    }
  1.2289 +    ~AutoIdArray() {
  1.2290 +        if (idArray)
  1.2291 +            JS_DestroyIdArray(context, idArray);
  1.2292 +    }
  1.2293 +    bool operator!() {
  1.2294 +        return !idArray;
  1.2295 +    }
  1.2296 +    jsid operator[](size_t i) const {
  1.2297 +        JS_ASSERT(idArray);
  1.2298 +        return JS_IdArrayGet(context, idArray, i);
  1.2299 +    }
  1.2300 +    size_t length() const {
  1.2301 +        return JS_IdArrayLength(context, idArray);
  1.2302 +    }
  1.2303 +
  1.2304 +    friend void AutoGCRooter::trace(JSTracer *trc);
  1.2305 +
  1.2306 +    JSIdArray *steal() {
  1.2307 +        JSIdArray *copy = idArray;
  1.2308 +        idArray = nullptr;
  1.2309 +        return copy;
  1.2310 +    }
  1.2311 +
  1.2312 +  protected:
  1.2313 +    inline void trace(JSTracer *trc);
  1.2314 +
  1.2315 +  private:
  1.2316 +    JSContext *context;
  1.2317 +    JSIdArray *idArray;
  1.2318 +    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
  1.2319 +
  1.2320 +    /* No copy or assignment semantics. */
  1.2321 +    AutoIdArray(AutoIdArray &ida) MOZ_DELETE;
  1.2322 +    void operator=(AutoIdArray &ida) MOZ_DELETE;
  1.2323 +};
  1.2324 +
  1.2325 +} /* namespace JS */
  1.2326 +
  1.2327 +extern JS_PUBLIC_API(bool)
  1.2328 +JS_ValueToId(JSContext *cx, JS::HandleValue v, JS::MutableHandleId idp);
  1.2329 +
  1.2330 +extern JS_PUBLIC_API(bool)
  1.2331 +JS_StringToId(JSContext *cx, JS::HandleString s, JS::MutableHandleId idp);
  1.2332 +
  1.2333 +extern JS_PUBLIC_API(bool)
  1.2334 +JS_IdToValue(JSContext *cx, jsid id, JS::MutableHandle<JS::Value> vp);
  1.2335 +
  1.2336 +/*
  1.2337 + * Invoke the [[DefaultValue]] hook (see ES5 8.6.2) with the provided hint on
  1.2338 + * the specified object, computing a primitive default value for the object.
  1.2339 + * The hint must be JSTYPE_STRING, JSTYPE_NUMBER, or JSTYPE_VOID (no hint).  On
  1.2340 + * success the resulting value is stored in *vp.
  1.2341 + */
  1.2342 +extern JS_PUBLIC_API(bool)
  1.2343 +JS_DefaultValue(JSContext *cx, JS::Handle<JSObject*> obj, JSType hint,
  1.2344 +                JS::MutableHandle<JS::Value> vp);
  1.2345 +
  1.2346 +extern JS_PUBLIC_API(bool)
  1.2347 +JS_PropertyStub(JSContext *cx, JS::HandleObject obj, JS::HandleId id,
  1.2348 +                JS::MutableHandleValue vp);
  1.2349 +
  1.2350 +extern JS_PUBLIC_API(bool)
  1.2351 +JS_StrictPropertyStub(JSContext *cx, JS::HandleObject obj, JS::HandleId id, bool strict,
  1.2352 +                      JS::MutableHandleValue vp);
  1.2353 +
  1.2354 +extern JS_PUBLIC_API(bool)
  1.2355 +JS_DeletePropertyStub(JSContext *cx, JS::HandleObject obj, JS::HandleId id,
  1.2356 +                      bool *succeeded);
  1.2357 +
  1.2358 +extern JS_PUBLIC_API(bool)
  1.2359 +JS_EnumerateStub(JSContext *cx, JS::HandleObject obj);
  1.2360 +
  1.2361 +extern JS_PUBLIC_API(bool)
  1.2362 +JS_ResolveStub(JSContext *cx, JS::HandleObject obj, JS::HandleId id);
  1.2363 +
  1.2364 +extern JS_PUBLIC_API(bool)
  1.2365 +JS_ConvertStub(JSContext *cx, JS::HandleObject obj, JSType type,
  1.2366 +               JS::MutableHandleValue vp);
  1.2367 +
  1.2368 +struct JSConstDoubleSpec {
  1.2369 +    double          dval;
  1.2370 +    const char      *name;
  1.2371 +    uint8_t         flags;
  1.2372 +    uint8_t         spare[3];
  1.2373 +};
  1.2374 +
  1.2375 +struct JSJitInfo;
  1.2376 +
  1.2377 +/*
  1.2378 + * Wrappers to replace {Strict,}PropertyOp for JSPropertySpecs. This will allow
  1.2379 + * us to pass one JSJitInfo per function with the property spec, without
  1.2380 + * additional field overhead.
  1.2381 + */
  1.2382 +typedef struct JSStrictPropertyOpWrapper {
  1.2383 +    JSStrictPropertyOp  op;
  1.2384 +    const JSJitInfo     *info;
  1.2385 +} JSStrictPropertyOpWrapper;
  1.2386 +
  1.2387 +typedef struct JSPropertyOpWrapper {
  1.2388 +    JSPropertyOp        op;
  1.2389 +    const JSJitInfo     *info;
  1.2390 +} JSPropertyOpWrapper;
  1.2391 +
  1.2392 +/*
  1.2393 + * Wrapper to do as above, but for JSNatives for JSFunctionSpecs.
  1.2394 + */
  1.2395 +typedef struct JSNativeWrapper {
  1.2396 +    JSNative        op;
  1.2397 +    const JSJitInfo *info;
  1.2398 +} JSNativeWrapper;
  1.2399 +
  1.2400 +/*
  1.2401 + * Macro static initializers which make it easy to pass no JSJitInfo as part of a
  1.2402 + * JSPropertySpec or JSFunctionSpec.
  1.2403 + */
  1.2404 +#define JSOP_WRAPPER(op) { {op, nullptr} }
  1.2405 +#define JSOP_NULLWRAPPER JSOP_WRAPPER(nullptr)
  1.2406 +
  1.2407 +/*
  1.2408 + * To define an array element rather than a named property member, cast the
  1.2409 + * element's index to (const char *) and initialize name with it, and set the
  1.2410 + * JSPROP_INDEX bit in flags.
  1.2411 + */
  1.2412 +struct JSPropertySpec {
  1.2413 +    struct SelfHostedWrapper {
  1.2414 +        void       *unused;
  1.2415 +        const char *funname;
  1.2416 +    };
  1.2417 +
  1.2418 +    const char                  *name;
  1.2419 +    uint8_t                     flags;
  1.2420 +    union {
  1.2421 +        JSPropertyOpWrapper propertyOp;
  1.2422 +        SelfHostedWrapper   selfHosted;
  1.2423 +    } getter;
  1.2424 +    union {
  1.2425 +        JSStrictPropertyOpWrapper propertyOp;
  1.2426 +        SelfHostedWrapper         selfHosted;
  1.2427 +    } setter;
  1.2428 +
  1.2429 +private:
  1.2430 +    void StaticAsserts() {
  1.2431 +        JS_STATIC_ASSERT(sizeof(SelfHostedWrapper) == sizeof(JSPropertyOpWrapper));
  1.2432 +        JS_STATIC_ASSERT(sizeof(SelfHostedWrapper) == sizeof(JSStrictPropertyOpWrapper));
  1.2433 +        JS_STATIC_ASSERT(offsetof(SelfHostedWrapper, funname) ==
  1.2434 +                         offsetof(JSPropertyOpWrapper, info));
  1.2435 +    }
  1.2436 +};
  1.2437 +
  1.2438 +namespace JS {
  1.2439 +namespace detail {
  1.2440 +
  1.2441 +/* NEVER DEFINED, DON'T USE.  For use by JS_CAST_NATIVE_TO only. */
  1.2442 +inline int CheckIsNative(JSNative native);
  1.2443 +
  1.2444 +/* NEVER DEFINED, DON'T USE.  For use by JS_CAST_STRING_TO only. */
  1.2445 +template<size_t N>
  1.2446 +inline int
  1.2447 +CheckIsCharacterLiteral(const char (&arr)[N]);
  1.2448 +
  1.2449 +} // namespace detail
  1.2450 +} // namespace JS
  1.2451 +
  1.2452 +#define JS_CAST_NATIVE_TO(v, To) \
  1.2453 +  (static_cast<void>(sizeof(JS::detail::CheckIsNative(v))), \
  1.2454 +   reinterpret_cast<To>(v))
  1.2455 +
  1.2456 +#define JS_CAST_STRING_TO(s, To) \
  1.2457 +  (static_cast<void>(sizeof(JS::detail::CheckIsCharacterLiteral(s))), \
  1.2458 +   reinterpret_cast<To>(s))
  1.2459 +
  1.2460 +#define JS_CHECK_ACCESSOR_FLAGS(flags) \
  1.2461 +  (static_cast<mozilla::EnableIf<!((flags) & (JSPROP_READONLY | JSPROP_SHARED | JSPROP_NATIVE_ACCESSORS))>::Type>(0), \
  1.2462 +   (flags))
  1.2463 +
  1.2464 +/*
  1.2465 + * JSPropertySpec uses JSAPI JSPropertyOp and JSStrictPropertyOp in function
  1.2466 + * signatures.  These macros encapsulate the definition of JSNative-backed
  1.2467 + * JSPropertySpecs, performing type-safe casts on the getter/setter functions
  1.2468 + * and adding the necessary property flags to trigger interpretation as
  1.2469 + * JSNatives.
  1.2470 + */
  1.2471 +#define JS_PSG(name, getter, flags) \
  1.2472 +    {name, \
  1.2473 +     uint8_t(JS_CHECK_ACCESSOR_FLAGS(flags) | JSPROP_SHARED | JSPROP_NATIVE_ACCESSORS), \
  1.2474 +     JSOP_WRAPPER(JS_CAST_NATIVE_TO(getter, JSPropertyOp)), \
  1.2475 +     JSOP_NULLWRAPPER}
  1.2476 +#define JS_PSGS(name, getter, setter, flags) \
  1.2477 +    {name, \
  1.2478 +     uint8_t(JS_CHECK_ACCESSOR_FLAGS(flags) | JSPROP_SHARED | JSPROP_NATIVE_ACCESSORS), \
  1.2479 +     JSOP_WRAPPER(JS_CAST_NATIVE_TO(getter, JSPropertyOp)), \
  1.2480 +     JSOP_WRAPPER(JS_CAST_NATIVE_TO(setter, JSStrictPropertyOp))}
  1.2481 +#define JS_SELF_HOSTED_GET(name, getterName, flags) \
  1.2482 +    {name, \
  1.2483 +     uint8_t(JS_CHECK_ACCESSOR_FLAGS(flags) | JSPROP_SHARED | JSPROP_GETTER), \
  1.2484 +     { nullptr, JS_CAST_STRING_TO(getterName, const JSJitInfo *) }, \
  1.2485 +     JSOP_NULLWRAPPER }
  1.2486 +#define JS_SELF_HOSTED_GETSET(name, getterName, setterName, flags) \
  1.2487 +    {name, \
  1.2488 +     uint8_t(JS_CHECK_ACCESSOR_FLAGS(flags) | JSPROP_SHARED | JSPROP_GETTER | JSPROP_SETTER), \
  1.2489 +     { nullptr, JS_CAST_STRING_TO(getterName, const JSJitInfo *) },  \
  1.2490 +     { nullptr, JS_CAST_STRING_TO(setterName, const JSJitInfo *) } }
  1.2491 +#define JS_PS_END { nullptr, 0, JSOP_NULLWRAPPER, JSOP_NULLWRAPPER }
  1.2492 +
  1.2493 +/*
  1.2494 + * To define a native function, set call to a JSNativeWrapper. To define a
  1.2495 + * self-hosted function, set selfHostedName to the name of a function
  1.2496 + * compiled during JSRuntime::initSelfHosting.
  1.2497 + */
  1.2498 +struct JSFunctionSpec {
  1.2499 +    const char      *name;
  1.2500 +    JSNativeWrapper call;
  1.2501 +    uint16_t        nargs;
  1.2502 +    uint16_t        flags;
  1.2503 +    const char      *selfHostedName;
  1.2504 +};
  1.2505 +
  1.2506 +/*
  1.2507 + * Terminating sentinel initializer to put at the end of a JSFunctionSpec array
  1.2508 + * that's passed to JS_DefineFunctions or JS_InitClass.
  1.2509 + */
  1.2510 +#define JS_FS_END JS_FS(nullptr,nullptr,0,0)
  1.2511 +
  1.2512 +/*
  1.2513 + * Initializer macros for a JSFunctionSpec array element. JS_FN (whose name pays
  1.2514 + * homage to the old JSNative/JSFastNative split) simply adds the flag
  1.2515 + * JSFUN_STUB_GSOPS. JS_FNINFO allows the simple adding of
  1.2516 + * JSJitInfos. JS_SELF_HOSTED_FN declares a self-hosted function. Finally
  1.2517 + * JS_FNSPEC has slots for all the fields.
  1.2518 + */
  1.2519 +#define JS_FS(name,call,nargs,flags)                                          \
  1.2520 +    JS_FNSPEC(name, call, nullptr, nargs, flags, nullptr)
  1.2521 +#define JS_FN(name,call,nargs,flags)                                          \
  1.2522 +    JS_FNSPEC(name, call, nullptr, nargs, (flags) | JSFUN_STUB_GSOPS, nullptr)
  1.2523 +#define JS_FNINFO(name,call,info,nargs,flags)                                 \
  1.2524 +    JS_FNSPEC(name, call, info, nargs, flags, nullptr)
  1.2525 +#define JS_SELF_HOSTED_FN(name,selfHostedName,nargs,flags)                    \
  1.2526 +    JS_FNSPEC(name, nullptr, nullptr, nargs, flags, selfHostedName)
  1.2527 +#define JS_FNSPEC(name,call,info,nargs,flags,selfHostedName)                  \
  1.2528 +    {name, {call, info}, nargs, flags, selfHostedName}
  1.2529 +
  1.2530 +extern JS_PUBLIC_API(JSObject *)
  1.2531 +JS_InitClass(JSContext *cx, JS::HandleObject obj, JS::HandleObject parent_proto,
  1.2532 +             const JSClass *clasp, JSNative constructor, unsigned nargs,
  1.2533 +             const JSPropertySpec *ps, const JSFunctionSpec *fs,
  1.2534 +             const JSPropertySpec *static_ps, const JSFunctionSpec *static_fs);
  1.2535 +
  1.2536 +/*
  1.2537 + * Set up ctor.prototype = proto and proto.constructor = ctor with the
  1.2538 + * right property flags.
  1.2539 + */
  1.2540 +extern JS_PUBLIC_API(bool)
  1.2541 +JS_LinkConstructorAndPrototype(JSContext *cx, JS::Handle<JSObject*> ctor,
  1.2542 +                               JS::Handle<JSObject*> proto);
  1.2543 +
  1.2544 +extern JS_PUBLIC_API(const JSClass *)
  1.2545 +JS_GetClass(JSObject *obj);
  1.2546 +
  1.2547 +extern JS_PUBLIC_API(bool)
  1.2548 +JS_InstanceOf(JSContext *cx, JS::Handle<JSObject*> obj, const JSClass *clasp, JS::CallArgs *args);
  1.2549 +
  1.2550 +extern JS_PUBLIC_API(bool)
  1.2551 +JS_HasInstance(JSContext *cx, JS::Handle<JSObject*> obj, JS::Handle<JS::Value> v, bool *bp);
  1.2552 +
  1.2553 +extern JS_PUBLIC_API(void *)
  1.2554 +JS_GetPrivate(JSObject *obj);
  1.2555 +
  1.2556 +extern JS_PUBLIC_API(void)
  1.2557 +JS_SetPrivate(JSObject *obj, void *data);
  1.2558 +
  1.2559 +extern JS_PUBLIC_API(void *)
  1.2560 +JS_GetInstancePrivate(JSContext *cx, JS::Handle<JSObject*> obj, const JSClass *clasp,
  1.2561 +                      JS::CallArgs *args);
  1.2562 +
  1.2563 +extern JS_PUBLIC_API(bool)
  1.2564 +JS_GetPrototype(JSContext *cx, JS::HandleObject obj, JS::MutableHandleObject protop);
  1.2565 +
  1.2566 +extern JS_PUBLIC_API(bool)
  1.2567 +JS_SetPrototype(JSContext *cx, JS::HandleObject obj, JS::HandleObject proto);
  1.2568 +
  1.2569 +extern JS_PUBLIC_API(JSObject *)
  1.2570 +JS_GetParent(JSObject *obj);
  1.2571 +
  1.2572 +extern JS_PUBLIC_API(bool)
  1.2573 +JS_SetParent(JSContext *cx, JS::HandleObject obj, JS::HandleObject parent);
  1.2574 +
  1.2575 +extern JS_PUBLIC_API(JSObject *)
  1.2576 +JS_GetConstructor(JSContext *cx, JS::Handle<JSObject*> proto);
  1.2577 +
  1.2578 +namespace JS {
  1.2579 +
  1.2580 +enum ZoneSpecifier {
  1.2581 +    FreshZone = 0,
  1.2582 +    SystemZone = 1
  1.2583 +};
  1.2584 +
  1.2585 +class JS_PUBLIC_API(CompartmentOptions)
  1.2586 +{
  1.2587 +  public:
  1.2588 +    class Override {
  1.2589 +      public:
  1.2590 +        Override() : mode_(Default) {}
  1.2591 +
  1.2592 +        bool get(bool defaultValue) const {
  1.2593 +            if (mode_ == Default)
  1.2594 +                return defaultValue;
  1.2595 +            return mode_ == ForceTrue;
  1.2596 +        };
  1.2597 +
  1.2598 +        void set(bool overrideValue) {
  1.2599 +            mode_ = overrideValue ? ForceTrue : ForceFalse;
  1.2600 +        };
  1.2601 +
  1.2602 +        void reset() {
  1.2603 +            mode_ = Default;
  1.2604 +        }
  1.2605 +
  1.2606 +      private:
  1.2607 +        enum Mode {
  1.2608 +            Default,
  1.2609 +            ForceTrue,
  1.2610 +            ForceFalse
  1.2611 +        };
  1.2612 +
  1.2613 +        Mode mode_;
  1.2614 +    };
  1.2615 +
  1.2616 +    explicit CompartmentOptions()
  1.2617 +      : version_(JSVERSION_UNKNOWN)
  1.2618 +      , invisibleToDebugger_(false)
  1.2619 +      , mergeable_(false)
  1.2620 +      , discardSource_(false)
  1.2621 +      , traceGlobal_(nullptr)
  1.2622 +      , singletonsAsTemplates_(true)
  1.2623 +    {
  1.2624 +        zone_.spec = JS::FreshZone;
  1.2625 +    }
  1.2626 +
  1.2627 +    JSVersion version() const { return version_; }
  1.2628 +    CompartmentOptions &setVersion(JSVersion aVersion) {
  1.2629 +        MOZ_ASSERT(aVersion != JSVERSION_UNKNOWN);
  1.2630 +        version_ = aVersion;
  1.2631 +        return *this;
  1.2632 +    }
  1.2633 +
  1.2634 +    // Certain scopes (i.e. XBL compilation scopes) are implementation details
  1.2635 +    // of the embedding, and references to them should never leak out to script.
  1.2636 +    // This flag causes the this compartment to skip firing onNewGlobalObject
  1.2637 +    // and makes addDebuggee a no-op for this global.
  1.2638 +    bool invisibleToDebugger() const { return invisibleToDebugger_; }
  1.2639 +    CompartmentOptions &setInvisibleToDebugger(bool flag) {
  1.2640 +        invisibleToDebugger_ = flag;
  1.2641 +        return *this;
  1.2642 +    }
  1.2643 +
  1.2644 +    // Compartments used for off-thread compilation have their contents merged
  1.2645 +    // into a target compartment when the compilation is finished. This is only
  1.2646 +    // allowed if this flag is set.  The invisibleToDebugger flag must also be
  1.2647 +    // set for such compartments.
  1.2648 +    bool mergeable() const { return mergeable_; }
  1.2649 +    CompartmentOptions &setMergeable(bool flag) {
  1.2650 +        mergeable_ = flag;
  1.2651 +        return *this;
  1.2652 +    }
  1.2653 +
  1.2654 +    // For certain globals, we know enough about the code that will run in them
  1.2655 +    // that we can discard script source entirely.
  1.2656 +    bool discardSource() const { return discardSource_; }
  1.2657 +    CompartmentOptions &setDiscardSource(bool flag) {
  1.2658 +        discardSource_ = flag;
  1.2659 +        return *this;
  1.2660 +    }
  1.2661 +
  1.2662 +
  1.2663 +    bool cloneSingletons(JSContext *cx) const;
  1.2664 +    Override &cloneSingletonsOverride() { return cloneSingletonsOverride_; }
  1.2665 +
  1.2666 +    void *zonePointer() const {
  1.2667 +        JS_ASSERT(uintptr_t(zone_.pointer) > uintptr_t(JS::SystemZone));
  1.2668 +        return zone_.pointer;
  1.2669 +    }
  1.2670 +    ZoneSpecifier zoneSpecifier() const { return zone_.spec; }
  1.2671 +    CompartmentOptions &setZone(ZoneSpecifier spec);
  1.2672 +    CompartmentOptions &setSameZoneAs(JSObject *obj);
  1.2673 +
  1.2674 +    void setSingletonsAsValues() {
  1.2675 +        singletonsAsTemplates_ = false;
  1.2676 +    }
  1.2677 +    bool getSingletonsAsTemplates() const {
  1.2678 +        return singletonsAsTemplates_;
  1.2679 +    };
  1.2680 +
  1.2681 +    CompartmentOptions &setTrace(JSTraceOp op) {
  1.2682 +        traceGlobal_ = op;
  1.2683 +        return *this;
  1.2684 +    }
  1.2685 +    JSTraceOp getTrace() const {
  1.2686 +        return traceGlobal_;
  1.2687 +    }
  1.2688 +
  1.2689 +  private:
  1.2690 +    JSVersion version_;
  1.2691 +    bool invisibleToDebugger_;
  1.2692 +    bool mergeable_;
  1.2693 +    bool discardSource_;
  1.2694 +    Override cloneSingletonsOverride_;
  1.2695 +    union {
  1.2696 +        ZoneSpecifier spec;
  1.2697 +        void *pointer; // js::Zone* is not exposed in the API.
  1.2698 +    } zone_;
  1.2699 +    JSTraceOp traceGlobal_;
  1.2700 +
  1.2701 +    // To XDR singletons, we need to ensure that all singletons are all used as
  1.2702 +    // templates, by making JSOP_OBJECT return a clone of the JSScript
  1.2703 +    // singleton, instead of returning the value which is baked in the JSScript.
  1.2704 +    bool singletonsAsTemplates_;
  1.2705 +};
  1.2706 +
  1.2707 +JS_PUBLIC_API(CompartmentOptions &)
  1.2708 +CompartmentOptionsRef(JSCompartment *compartment);
  1.2709 +
  1.2710 +JS_PUBLIC_API(CompartmentOptions &)
  1.2711 +CompartmentOptionsRef(JSObject *obj);
  1.2712 +
  1.2713 +JS_PUBLIC_API(CompartmentOptions &)
  1.2714 +CompartmentOptionsRef(JSContext *cx);
  1.2715 +
  1.2716 +// During global creation, we fire notifications to callbacks registered
  1.2717 +// via the Debugger API. These callbacks are arbitrary script, and can touch
  1.2718 +// the global in arbitrary ways. When that happens, the global should not be
  1.2719 +// in a half-baked state. But this creates a problem for consumers that need
  1.2720 +// to set slots on the global to put it in a consistent state.
  1.2721 +//
  1.2722 +// This API provides a way for consumers to set slots atomically (immediately
  1.2723 +// after the global is created), before any debugger hooks are fired. It's
  1.2724 +// unfortunately on the clunky side, but that's the way the cookie crumbles.
  1.2725 +//
  1.2726 +// If callers have no additional state on the global to set up, they may pass
  1.2727 +// |FireOnNewGlobalHook| to JS_NewGlobalObject, which causes that function to
  1.2728 +// fire the hook as its final act before returning. Otherwise, callers should
  1.2729 +// pass |DontFireOnNewGlobalHook|, which means that they are responsible for
  1.2730 +// invoking JS_FireOnNewGlobalObject upon successfully creating the global. If
  1.2731 +// an error occurs and the operation aborts, callers should skip firing the
  1.2732 +// hook. But otherwise, callers must take care to fire the hook exactly once
  1.2733 +// before compiling any script in the global's scope (we have assertions in
  1.2734 +// place to enforce this). This lets us be sure that debugger clients never miss
  1.2735 +// breakpoints.
  1.2736 +enum OnNewGlobalHookOption {
  1.2737 +    FireOnNewGlobalHook,
  1.2738 +    DontFireOnNewGlobalHook
  1.2739 +};
  1.2740 +
  1.2741 +} /* namespace JS */
  1.2742 +
  1.2743 +extern JS_PUBLIC_API(JSObject *)
  1.2744 +JS_NewGlobalObject(JSContext *cx, const JSClass *clasp, JSPrincipals *principals,
  1.2745 +                   JS::OnNewGlobalHookOption hookOption,
  1.2746 +                   const JS::CompartmentOptions &options = JS::CompartmentOptions());
  1.2747 +/*
  1.2748 + * Spidermonkey does not have a good way of keeping track of what compartments should be marked on
  1.2749 + * their own. We can mark the roots unconditionally, but marking GC things only relevant in live
  1.2750 + * compartments is hard. To mitigate this, we create a static trace hook, installed on each global
  1.2751 + * object, from which we can be sure the compartment is relevant, and mark it.
  1.2752 + *
  1.2753 + * It is still possible to specify custom trace hooks for global object classes. They can be
  1.2754 + * provided via the CompartmentOptions passed to JS_NewGlobalObject.
  1.2755 + */
  1.2756 +extern JS_PUBLIC_API(void)
  1.2757 +JS_GlobalObjectTraceHook(JSTracer *trc, JSObject *global);
  1.2758 +
  1.2759 +extern JS_PUBLIC_API(void)
  1.2760 +JS_FireOnNewGlobalObject(JSContext *cx, JS::HandleObject global);
  1.2761 +
  1.2762 +extern JS_PUBLIC_API(JSObject *)
  1.2763 +JS_NewObject(JSContext *cx, const JSClass *clasp, JS::Handle<JSObject*> proto,
  1.2764 +             JS::Handle<JSObject*> parent);
  1.2765 +
  1.2766 +/* Queries the [[Extensible]] property of the object. */
  1.2767 +extern JS_PUBLIC_API(bool)
  1.2768 +JS_IsExtensible(JSContext *cx, JS::HandleObject obj, bool *extensible);
  1.2769 +
  1.2770 +extern JS_PUBLIC_API(bool)
  1.2771 +JS_IsNative(JSObject *obj);
  1.2772 +
  1.2773 +extern JS_PUBLIC_API(JSRuntime *)
  1.2774 +JS_GetObjectRuntime(JSObject *obj);
  1.2775 +
  1.2776 +/*
  1.2777 + * Unlike JS_NewObject, JS_NewObjectWithGivenProto does not compute a default
  1.2778 + * proto if proto's actual parameter value is null.
  1.2779 + */
  1.2780 +extern JS_PUBLIC_API(JSObject *)
  1.2781 +JS_NewObjectWithGivenProto(JSContext *cx, const JSClass *clasp, JS::Handle<JSObject*> proto,
  1.2782 +                           JS::Handle<JSObject*> parent);
  1.2783 +
  1.2784 +/*
  1.2785 + * Freeze obj, and all objects it refers to, recursively. This will not recurse
  1.2786 + * through non-extensible objects, on the assumption that those are already
  1.2787 + * deep-frozen.
  1.2788 + */
  1.2789 +extern JS_PUBLIC_API(bool)
  1.2790 +JS_DeepFreezeObject(JSContext *cx, JS::Handle<JSObject*> obj);
  1.2791 +
  1.2792 +/*
  1.2793 + * Freezes an object; see ES5's Object.freeze(obj) method.
  1.2794 + */
  1.2795 +extern JS_PUBLIC_API(bool)
  1.2796 +JS_FreezeObject(JSContext *cx, JS::Handle<JSObject*> obj);
  1.2797 +
  1.2798 +extern JS_PUBLIC_API(bool)
  1.2799 +JS_PreventExtensions(JSContext *cx, JS::HandleObject obj);
  1.2800 +
  1.2801 +extern JS_PUBLIC_API(JSObject *)
  1.2802 +JS_New(JSContext *cx, JS::HandleObject ctor, const JS::HandleValueArray& args);
  1.2803 +
  1.2804 +extern JS_PUBLIC_API(JSObject *)
  1.2805 +JS_DefineObject(JSContext *cx, JSObject *obj, const char *name, const JSClass *clasp,
  1.2806 +                JSObject *proto, unsigned attrs);
  1.2807 +
  1.2808 +extern JS_PUBLIC_API(bool)
  1.2809 +JS_DefineConstDoubles(JSContext *cx, JS::HandleObject obj, const JSConstDoubleSpec *cds);
  1.2810 +
  1.2811 +extern JS_PUBLIC_API(bool)
  1.2812 +JS_DefineProperties(JSContext *cx, JS::HandleObject obj, const JSPropertySpec *ps);
  1.2813 +
  1.2814 +extern JS_PUBLIC_API(bool)
  1.2815 +JS_DefineProperty(JSContext *cx, JS::HandleObject obj, const char *name, JS::HandleValue value,
  1.2816 +                  unsigned attrs,
  1.2817 +                  JSPropertyOp getter = nullptr, JSStrictPropertyOp setter = nullptr);
  1.2818 +
  1.2819 +extern JS_PUBLIC_API(bool)
  1.2820 +JS_DefineProperty(JSContext *cx, JS::HandleObject obj, const char *name, JS::HandleObject value,
  1.2821 +                  unsigned attrs,
  1.2822 +                  JSPropertyOp getter = nullptr, JSStrictPropertyOp setter = nullptr);
  1.2823 +
  1.2824 +extern JS_PUBLIC_API(bool)
  1.2825 +JS_DefineProperty(JSContext *cx, JS::HandleObject obj, const char *name, JS::HandleString value,
  1.2826 +                  unsigned attrs,
  1.2827 +                  JSPropertyOp getter = nullptr, JSStrictPropertyOp setter = nullptr);
  1.2828 +
  1.2829 +extern JS_PUBLIC_API(bool)
  1.2830 +JS_DefineProperty(JSContext *cx, JS::HandleObject obj, const char *name, int32_t value,
  1.2831 +                  unsigned attrs,
  1.2832 +                  JSPropertyOp getter = nullptr, JSStrictPropertyOp setter = nullptr);
  1.2833 +
  1.2834 +extern JS_PUBLIC_API(bool)
  1.2835 +JS_DefineProperty(JSContext *cx, JS::HandleObject obj, const char *name, uint32_t value,
  1.2836 +                  unsigned attrs,
  1.2837 +                  JSPropertyOp getter = nullptr, JSStrictPropertyOp setter = nullptr);
  1.2838 +
  1.2839 +extern JS_PUBLIC_API(bool)
  1.2840 +JS_DefineProperty(JSContext *cx, JS::HandleObject obj, const char *name, double value,
  1.2841 +                  unsigned attrs,
  1.2842 +                  JSPropertyOp getter = nullptr, JSStrictPropertyOp setter = nullptr);
  1.2843 +
  1.2844 +extern JS_PUBLIC_API(bool)
  1.2845 +JS_DefinePropertyById(JSContext *cx, JSObject *obj, jsid id, jsval value,
  1.2846 +                      JSPropertyOp getter, JSStrictPropertyOp setter, unsigned attrs);
  1.2847 +
  1.2848 +extern JS_PUBLIC_API(bool)
  1.2849 +JS_DefineOwnProperty(JSContext *cx, JS::HandleObject obj, JS::HandleId id,
  1.2850 +                     JS::HandleValue descriptor, bool *bp);
  1.2851 +
  1.2852 +extern JS_PUBLIC_API(bool)
  1.2853 +JS_AlreadyHasOwnProperty(JSContext *cx, JS::HandleObject obj, const char *name,
  1.2854 +                         bool *foundp);
  1.2855 +
  1.2856 +extern JS_PUBLIC_API(bool)
  1.2857 +JS_AlreadyHasOwnPropertyById(JSContext *cx, JS::HandleObject obj, JS::HandleId id,
  1.2858 +                             bool *foundp);
  1.2859 +
  1.2860 +extern JS_PUBLIC_API(bool)
  1.2861 +JS_HasProperty(JSContext *cx, JS::HandleObject obj, const char *name, bool *foundp);
  1.2862 +
  1.2863 +extern JS_PUBLIC_API(bool)
  1.2864 +JS_HasPropertyById(JSContext *cx, JS::HandleObject obj, JS::HandleId id, bool *foundp);
  1.2865 +
  1.2866 +extern JS_PUBLIC_API(bool)
  1.2867 +JS_LookupProperty(JSContext *cx, JS::HandleObject obj, const char *name, JS::MutableHandleValue vp);
  1.2868 +
  1.2869 +extern JS_PUBLIC_API(bool)
  1.2870 +JS_LookupPropertyById(JSContext *cx, JS::HandleObject obj, JS::HandleId id,
  1.2871 +                      JS::MutableHandleValue vp);
  1.2872 +
  1.2873 +struct JSPropertyDescriptor {
  1.2874 +    JSObject           *obj;
  1.2875 +    unsigned           attrs;
  1.2876 +    JSPropertyOp       getter;
  1.2877 +    JSStrictPropertyOp setter;
  1.2878 +    JS::Value          value;
  1.2879 +
  1.2880 +    JSPropertyDescriptor()
  1.2881 +      : obj(nullptr), attrs(0), getter(nullptr), setter(nullptr), value(JSVAL_VOID)
  1.2882 +    {}
  1.2883 +
  1.2884 +    void trace(JSTracer *trc);
  1.2885 +};
  1.2886 +
  1.2887 +namespace JS {
  1.2888 +
  1.2889 +template <typename Outer>
  1.2890 +class PropertyDescriptorOperations
  1.2891 +{
  1.2892 +    const JSPropertyDescriptor * desc() const { return static_cast<const Outer*>(this)->extract(); }
  1.2893 +
  1.2894 +  public:
  1.2895 +    bool isEnumerable() const { return desc()->attrs & JSPROP_ENUMERATE; }
  1.2896 +    bool isReadonly() const { return desc()->attrs & JSPROP_READONLY; }
  1.2897 +    bool isPermanent() const { return desc()->attrs & JSPROP_PERMANENT; }
  1.2898 +    bool hasNativeAccessors() const { return desc()->attrs & JSPROP_NATIVE_ACCESSORS; }
  1.2899 +    bool hasGetterObject() const { return desc()->attrs & JSPROP_GETTER; }
  1.2900 +    bool hasSetterObject() const { return desc()->attrs & JSPROP_SETTER; }
  1.2901 +    bool hasGetterOrSetterObject() const { return desc()->attrs & (JSPROP_GETTER | JSPROP_SETTER); }
  1.2902 +    bool isShared() const { return desc()->attrs & JSPROP_SHARED; }
  1.2903 +    bool isIndex() const { return desc()->attrs & JSPROP_INDEX; }
  1.2904 +    bool hasAttributes(unsigned attrs) const { return desc()->attrs & attrs; }
  1.2905 +
  1.2906 +    JS::HandleObject object() const {
  1.2907 +        return JS::HandleObject::fromMarkedLocation(&desc()->obj);
  1.2908 +    }
  1.2909 +    unsigned attributes() const { return desc()->attrs; }
  1.2910 +    JSPropertyOp getter() const { return desc()->getter; }
  1.2911 +    JSStrictPropertyOp setter() const { return desc()->setter; }
  1.2912 +    JS::HandleObject getterObject() const {
  1.2913 +        MOZ_ASSERT(hasGetterObject());
  1.2914 +        return JS::HandleObject::fromMarkedLocation(
  1.2915 +                reinterpret_cast<JSObject *const *>(&desc()->getter));
  1.2916 +    }
  1.2917 +    JS::HandleObject setterObject() const {
  1.2918 +        MOZ_ASSERT(hasSetterObject());
  1.2919 +        return JS::HandleObject::fromMarkedLocation(
  1.2920 +                reinterpret_cast<JSObject *const *>(&desc()->setter));
  1.2921 +    }
  1.2922 +    JS::HandleValue value() const {
  1.2923 +        return JS::HandleValue::fromMarkedLocation(&desc()->value);
  1.2924 +    }
  1.2925 +};
  1.2926 +
  1.2927 +template <typename Outer>
  1.2928 +class MutablePropertyDescriptorOperations : public PropertyDescriptorOperations<Outer>
  1.2929 +{
  1.2930 +    JSPropertyDescriptor * desc() { return static_cast<Outer*>(this)->extractMutable(); }
  1.2931 +
  1.2932 +  public:
  1.2933 +
  1.2934 +    void clear() {
  1.2935 +        object().set(nullptr);
  1.2936 +        setAttributes(0);
  1.2937 +        setGetter(nullptr);
  1.2938 +        setSetter(nullptr);
  1.2939 +        value().setUndefined();
  1.2940 +    }
  1.2941 +
  1.2942 +    JS::MutableHandleObject object() {
  1.2943 +        return JS::MutableHandleObject::fromMarkedLocation(&desc()->obj);
  1.2944 +    }
  1.2945 +    unsigned &attributesRef() { return desc()->attrs; }
  1.2946 +    JSPropertyOp &getter() { return desc()->getter; }
  1.2947 +    JSStrictPropertyOp &setter() { return desc()->setter; }
  1.2948 +    JS::MutableHandleValue value() {
  1.2949 +        return JS::MutableHandleValue::fromMarkedLocation(&desc()->value);
  1.2950 +    }
  1.2951 +
  1.2952 +    void setEnumerable() { desc()->attrs |= JSPROP_ENUMERATE; }
  1.2953 +    void setAttributes(unsigned attrs) { desc()->attrs = attrs; }
  1.2954 +
  1.2955 +    void setGetter(JSPropertyOp op) { desc()->getter = op; }
  1.2956 +    void setSetter(JSStrictPropertyOp op) { desc()->setter = op; }
  1.2957 +    void setGetterObject(JSObject *obj) { desc()->getter = reinterpret_cast<JSPropertyOp>(obj); }
  1.2958 +    void setSetterObject(JSObject *obj) { desc()->setter = reinterpret_cast<JSStrictPropertyOp>(obj); }
  1.2959 +};
  1.2960 +
  1.2961 +} /* namespace JS */
  1.2962 +
  1.2963 +namespace js {
  1.2964 +
  1.2965 +template <>
  1.2966 +struct GCMethods<JSPropertyDescriptor> {
  1.2967 +    static JSPropertyDescriptor initial() { return JSPropertyDescriptor(); }
  1.2968 +    static ThingRootKind kind() { return THING_ROOT_PROPERTY_DESCRIPTOR; }
  1.2969 +    static bool poisoned(const JSPropertyDescriptor &desc) {
  1.2970 +        return (desc.obj && JS::IsPoisonedPtr(desc.obj)) ||
  1.2971 +               (desc.attrs & JSPROP_GETTER && desc.getter && JS::IsPoisonedPtr(desc.getter)) ||
  1.2972 +               (desc.attrs & JSPROP_SETTER && desc.setter && JS::IsPoisonedPtr(desc.setter)) ||
  1.2973 +               (desc.value.isGCThing() && JS::IsPoisonedPtr(desc.value.toGCThing()));
  1.2974 +    }
  1.2975 +};
  1.2976 +
  1.2977 +template <>
  1.2978 +class RootedBase<JSPropertyDescriptor>
  1.2979 +  : public JS::MutablePropertyDescriptorOperations<JS::Rooted<JSPropertyDescriptor> >
  1.2980 +{
  1.2981 +    friend class JS::PropertyDescriptorOperations<JS::Rooted<JSPropertyDescriptor> >;
  1.2982 +    friend class JS::MutablePropertyDescriptorOperations<JS::Rooted<JSPropertyDescriptor> >;
  1.2983 +    const JSPropertyDescriptor *extract() const {
  1.2984 +        return static_cast<const JS::Rooted<JSPropertyDescriptor>*>(this)->address();
  1.2985 +    }
  1.2986 +    JSPropertyDescriptor *extractMutable() {
  1.2987 +        return static_cast<JS::Rooted<JSPropertyDescriptor>*>(this)->address();
  1.2988 +    }
  1.2989 +};
  1.2990 +
  1.2991 +template <>
  1.2992 +class HandleBase<JSPropertyDescriptor>
  1.2993 +  : public JS::PropertyDescriptorOperations<JS::Handle<JSPropertyDescriptor> >
  1.2994 +{
  1.2995 +    friend class JS::PropertyDescriptorOperations<JS::Handle<JSPropertyDescriptor> >;
  1.2996 +    const JSPropertyDescriptor *extract() const {
  1.2997 +        return static_cast<const JS::Handle<JSPropertyDescriptor>*>(this)->address();
  1.2998 +    }
  1.2999 +};
  1.3000 +
  1.3001 +template <>
  1.3002 +class MutableHandleBase<JSPropertyDescriptor>
  1.3003 +  : public JS::MutablePropertyDescriptorOperations<JS::MutableHandle<JSPropertyDescriptor> >
  1.3004 +{
  1.3005 +    friend class JS::PropertyDescriptorOperations<JS::MutableHandle<JSPropertyDescriptor> >;
  1.3006 +    friend class JS::MutablePropertyDescriptorOperations<JS::MutableHandle<JSPropertyDescriptor> >;
  1.3007 +    const JSPropertyDescriptor *extract() const {
  1.3008 +        return static_cast<const JS::MutableHandle<JSPropertyDescriptor>*>(this)->address();
  1.3009 +    }
  1.3010 +    JSPropertyDescriptor *extractMutable() {
  1.3011 +        return static_cast<JS::MutableHandle<JSPropertyDescriptor>*>(this)->address();
  1.3012 +    }
  1.3013 +};
  1.3014 +
  1.3015 +} /* namespace js */
  1.3016 +
  1.3017 +extern JS_PUBLIC_API(bool)
  1.3018 +JS_GetOwnPropertyDescriptorById(JSContext *cx, JS::HandleObject obj, JS::HandleId id,
  1.3019 +                                JS::MutableHandle<JSPropertyDescriptor> desc);
  1.3020 +
  1.3021 +extern JS_PUBLIC_API(bool)
  1.3022 +JS_GetOwnPropertyDescriptor(JSContext *cx, JS::HandleObject obj, const char *name,
  1.3023 +                            JS::MutableHandle<JSPropertyDescriptor> desc);
  1.3024 +
  1.3025 +/*
  1.3026 + * Like JS_GetOwnPropertyDescriptorById but will return a property on
  1.3027 + * an object on the prototype chain (returned in desc->obj). If desc->obj is null,
  1.3028 + * then this property was not found on the prototype chain.
  1.3029 + */
  1.3030 +extern JS_PUBLIC_API(bool)
  1.3031 +JS_GetPropertyDescriptorById(JSContext *cx, JS::HandleObject obj, JS::HandleId id,
  1.3032 +                             JS::MutableHandle<JSPropertyDescriptor> desc);
  1.3033 +
  1.3034 +extern JS_PUBLIC_API(bool)
  1.3035 +JS_GetPropertyDescriptor(JSContext *cx, JS::HandleObject obj, const char *name,
  1.3036 +                         JS::MutableHandle<JSPropertyDescriptor> desc);
  1.3037 +
  1.3038 +extern JS_PUBLIC_API(bool)
  1.3039 +JS_GetProperty(JSContext *cx, JS::HandleObject obj, const char *name, JS::MutableHandleValue vp);
  1.3040 +
  1.3041 +extern JS_PUBLIC_API(bool)
  1.3042 +JS_GetPropertyById(JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp);
  1.3043 +
  1.3044 +extern JS_PUBLIC_API(bool)
  1.3045 +JS_ForwardGetPropertyTo(JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::HandleObject onBehalfOf,
  1.3046 +                        JS::MutableHandleValue vp);
  1.3047 +
  1.3048 +extern JS_PUBLIC_API(bool)
  1.3049 +JS_SetProperty(JSContext *cx, JS::HandleObject obj, const char *name, JS::HandleValue v);
  1.3050 +
  1.3051 +extern JS_PUBLIC_API(bool)
  1.3052 +JS_SetPropertyById(JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::HandleValue v);
  1.3053 +
  1.3054 +extern JS_PUBLIC_API(bool)
  1.3055 +JS_DeleteProperty(JSContext *cx, JS::HandleObject obj, const char *name);
  1.3056 +
  1.3057 +extern JS_PUBLIC_API(bool)
  1.3058 +JS_DeleteProperty2(JSContext *cx, JS::HandleObject obj, const char *name, bool *succeeded);
  1.3059 +
  1.3060 +extern JS_PUBLIC_API(bool)
  1.3061 +JS_DeletePropertyById(JSContext *cx, JS::HandleObject obj, jsid id);
  1.3062 +
  1.3063 +extern JS_PUBLIC_API(bool)
  1.3064 +JS_DeletePropertyById2(JSContext *cx, JS::HandleObject obj, JS::HandleId id, bool *succeeded);
  1.3065 +
  1.3066 +extern JS_PUBLIC_API(bool)
  1.3067 +JS_DefineUCProperty(JSContext *cx, JSObject *obj,
  1.3068 +                    const jschar *name, size_t namelen, jsval value,
  1.3069 +                    JSPropertyOp getter, JSStrictPropertyOp setter,
  1.3070 +                    unsigned attrs);
  1.3071 +
  1.3072 +extern JS_PUBLIC_API(bool)
  1.3073 +JS_AlreadyHasOwnUCProperty(JSContext *cx, JS::HandleObject obj, const jschar *name,
  1.3074 +                           size_t namelen, bool *foundp);
  1.3075 +
  1.3076 +extern JS_PUBLIC_API(bool)
  1.3077 +JS_HasUCProperty(JSContext *cx, JS::HandleObject obj,
  1.3078 +                 const jschar *name, size_t namelen,
  1.3079 +                 bool *vp);
  1.3080 +
  1.3081 +extern JS_PUBLIC_API(bool)
  1.3082 +JS_LookupUCProperty(JSContext *cx, JS::HandleObject obj,
  1.3083 +                    const jschar *name, size_t namelen,
  1.3084 +                    JS::MutableHandleValue vp);
  1.3085 +
  1.3086 +extern JS_PUBLIC_API(bool)
  1.3087 +JS_GetUCProperty(JSContext *cx, JS::HandleObject obj,
  1.3088 +                 const jschar *name, size_t namelen,
  1.3089 +                 JS::MutableHandleValue vp);
  1.3090 +
  1.3091 +extern JS_PUBLIC_API(bool)
  1.3092 +JS_SetUCProperty(JSContext *cx, JS::HandleObject obj,
  1.3093 +                 const jschar *name, size_t namelen,
  1.3094 +                 JS::HandleValue v);
  1.3095 +
  1.3096 +extern JS_PUBLIC_API(bool)
  1.3097 +JS_DeleteUCProperty2(JSContext *cx, JS::HandleObject obj, const jschar *name, size_t namelen,
  1.3098 +                     bool *succeeded);
  1.3099 +
  1.3100 +extern JS_PUBLIC_API(JSObject *)
  1.3101 +JS_NewArrayObject(JSContext *cx, const JS::HandleValueArray& contents);
  1.3102 +
  1.3103 +extern JS_PUBLIC_API(JSObject *)
  1.3104 +JS_NewArrayObject(JSContext *cx, size_t length);
  1.3105 +
  1.3106 +extern JS_PUBLIC_API(bool)
  1.3107 +JS_IsArrayObject(JSContext *cx, JS::HandleValue value);
  1.3108 +
  1.3109 +extern JS_PUBLIC_API(bool)
  1.3110 +JS_IsArrayObject(JSContext *cx, JS::HandleObject obj);
  1.3111 +
  1.3112 +extern JS_PUBLIC_API(bool)
  1.3113 +JS_GetArrayLength(JSContext *cx, JS::Handle<JSObject*> obj, uint32_t *lengthp);
  1.3114 +
  1.3115 +extern JS_PUBLIC_API(bool)
  1.3116 +JS_SetArrayLength(JSContext *cx, JS::Handle<JSObject*> obj, uint32_t length);
  1.3117 +
  1.3118 +extern JS_PUBLIC_API(bool)
  1.3119 +JS_DefineElement(JSContext *cx, JSObject *obj, uint32_t index, jsval value,
  1.3120 +                 JSPropertyOp getter, JSStrictPropertyOp setter, unsigned attrs);
  1.3121 +
  1.3122 +extern JS_PUBLIC_API(bool)
  1.3123 +JS_AlreadyHasOwnElement(JSContext *cx, JS::HandleObject obj, uint32_t index, bool *foundp);
  1.3124 +
  1.3125 +extern JS_PUBLIC_API(bool)
  1.3126 +JS_HasElement(JSContext *cx, JS::HandleObject obj, uint32_t index, bool *foundp);
  1.3127 +
  1.3128 +extern JS_PUBLIC_API(bool)
  1.3129 +JS_LookupElement(JSContext *cx, JS::HandleObject obj, uint32_t index, JS::MutableHandleValue vp);
  1.3130 +
  1.3131 +extern JS_PUBLIC_API(bool)
  1.3132 +JS_GetElement(JSContext *cx, JS::HandleObject obj, uint32_t index, JS::MutableHandleValue vp);
  1.3133 +
  1.3134 +extern JS_PUBLIC_API(bool)
  1.3135 +JS_ForwardGetElementTo(JSContext *cx, JS::HandleObject obj, uint32_t index,
  1.3136 +                       JS::HandleObject onBehalfOf, JS::MutableHandleValue vp);
  1.3137 +
  1.3138 +extern JS_PUBLIC_API(bool)
  1.3139 +JS_SetElement(JSContext *cx, JS::HandleObject obj, uint32_t index, JS::HandleValue v);
  1.3140 +
  1.3141 +extern JS_PUBLIC_API(bool)
  1.3142 +JS_SetElement(JSContext *cx, JS::HandleObject obj, uint32_t index, JS::HandleObject v);
  1.3143 +
  1.3144 +extern JS_PUBLIC_API(bool)
  1.3145 +JS_SetElement(JSContext *cx, JS::HandleObject obj, uint32_t index, JS::HandleString v);
  1.3146 +
  1.3147 +extern JS_PUBLIC_API(bool)
  1.3148 +JS_SetElement(JSContext *cx, JS::HandleObject obj, uint32_t index, int32_t v);
  1.3149 +
  1.3150 +extern JS_PUBLIC_API(bool)
  1.3151 +JS_SetElement(JSContext *cx, JS::HandleObject obj, uint32_t index, uint32_t v);
  1.3152 +
  1.3153 +extern JS_PUBLIC_API(bool)
  1.3154 +JS_SetElement(JSContext *cx, JS::HandleObject obj, uint32_t index, double v);
  1.3155 +
  1.3156 +extern JS_PUBLIC_API(bool)
  1.3157 +JS_DeleteElement(JSContext *cx, JS::HandleObject obj, uint32_t index);
  1.3158 +
  1.3159 +extern JS_PUBLIC_API(bool)
  1.3160 +JS_DeleteElement2(JSContext *cx, JS::HandleObject obj, uint32_t index, bool *succeeded);
  1.3161 +
  1.3162 +/*
  1.3163 + * Remove all configurable properties from the given (non-global) object and
  1.3164 + * assign undefined to all writable data properties.
  1.3165 + */
  1.3166 +JS_PUBLIC_API(void)
  1.3167 +JS_ClearNonGlobalObject(JSContext *cx, JS::HandleObject obj);
  1.3168 +
  1.3169 +/*
  1.3170 + * Assign 'undefined' to all of the object's non-reserved slots. Note: this is
  1.3171 + * done for all slots, regardless of the associated property descriptor.
  1.3172 + */
  1.3173 +JS_PUBLIC_API(void)
  1.3174 +JS_SetAllNonReservedSlotsToUndefined(JSContext *cx, JSObject *objArg);
  1.3175 +
  1.3176 +/*
  1.3177 + * Create a new array buffer with the given contents. On success, the ownership
  1.3178 + * is transferred to the new array buffer.
  1.3179 + */
  1.3180 +extern JS_PUBLIC_API(JSObject *)
  1.3181 +JS_NewArrayBufferWithContents(JSContext *cx, size_t nbytes, void *contents);
  1.3182 +
  1.3183 +/*
  1.3184 + * Steal the contents of the given array buffer. The array buffer has its
  1.3185 + * length set to 0 and its contents array cleared. The caller takes ownership
  1.3186 + * of the return value and must free it or transfer ownership via
  1.3187 + * JS_NewArrayBufferWithContents when done using it.
  1.3188 + */
  1.3189 +extern JS_PUBLIC_API(void *)
  1.3190 +JS_StealArrayBufferContents(JSContext *cx, JS::HandleObject obj);
  1.3191 +
  1.3192 +/*
  1.3193 + * Allocate memory that may be eventually passed to
  1.3194 + * JS_NewArrayBufferWithContents. |maybecx| is optional; if a non-nullptr cx is
  1.3195 + * given, it will be used for memory accounting and OOM reporting. |nbytes| is
  1.3196 + * the number of payload bytes required.
  1.3197 + */
  1.3198 +extern JS_PUBLIC_API(void *)
  1.3199 +JS_AllocateArrayBufferContents(JSContext *maybecx, uint32_t nbytes);
  1.3200 +
  1.3201 +/*
  1.3202 + * Reallocate memory allocated by JS_AllocateArrayBufferContents, growing or
  1.3203 + * shrinking it as appropriate. If oldContents is null then this behaves like
  1.3204 + * JS_AllocateArrayBufferContents.
  1.3205 + */
  1.3206 +extern JS_PUBLIC_API(void *)
  1.3207 +JS_ReallocateArrayBufferContents(JSContext *cx, uint32_t nbytes, void *oldContents, uint32_t oldNbytes);
  1.3208 +
  1.3209 +/*
  1.3210 + * Create a new mapped array buffer with the given memory mapped contents. On success,
  1.3211 + * the ownership is transferred to the new mapped array buffer.
  1.3212 + */
  1.3213 +extern JS_PUBLIC_API(JSObject *)
  1.3214 +JS_NewMappedArrayBufferWithContents(JSContext *cx, size_t nbytes, void *contents);
  1.3215 +
  1.3216 +/*
  1.3217 + * Create memory mapped array buffer contents.
  1.3218 + * Caller must take care of closing fd after calling this function.
  1.3219 + */
  1.3220 +extern JS_PUBLIC_API(void *)
  1.3221 +JS_CreateMappedArrayBufferContents(int fd, size_t offset, size_t length);
  1.3222 +
  1.3223 +/*
  1.3224 + * Release the allocated resource of mapped array buffer contents before the
  1.3225 + * object is created.
  1.3226 + * If a new object has been created by JS_NewMappedArrayBufferWithContents()
  1.3227 + * with this content, then JS_NeuterArrayBuffer() should be used instead to
  1.3228 + * release the resource used by the object.
  1.3229 + */
  1.3230 +extern JS_PUBLIC_API(void)
  1.3231 +JS_ReleaseMappedArrayBufferContents(void *contents, size_t length);
  1.3232 +
  1.3233 +extern JS_PUBLIC_API(JSIdArray *)
  1.3234 +JS_Enumerate(JSContext *cx, JS::HandleObject obj);
  1.3235 +
  1.3236 +/*
  1.3237 + * Create an object to iterate over enumerable properties of obj, in arbitrary
  1.3238 + * property definition order.  NB: This differs from longstanding for..in loop
  1.3239 + * order, which uses order of property definition in obj.
  1.3240 + */
  1.3241 +extern JS_PUBLIC_API(JSObject *)
  1.3242 +JS_NewPropertyIterator(JSContext *cx, JS::Handle<JSObject*> obj);
  1.3243 +
  1.3244 +/*
  1.3245 + * Return true on success with *idp containing the id of the next enumerable
  1.3246 + * property to visit using iterobj, or JSID_IS_VOID if there is no such property
  1.3247 + * left to visit.  Return false on error.
  1.3248 + */
  1.3249 +extern JS_PUBLIC_API(bool)
  1.3250 +JS_NextProperty(JSContext *cx, JS::Handle<JSObject*> iterobj, jsid *idp);
  1.3251 +
  1.3252 +extern JS_PUBLIC_API(jsval)
  1.3253 +JS_GetReservedSlot(JSObject *obj, uint32_t index);
  1.3254 +
  1.3255 +extern JS_PUBLIC_API(void)
  1.3256 +JS_SetReservedSlot(JSObject *obj, uint32_t index, jsval v);
  1.3257 +
  1.3258 +/************************************************************************/
  1.3259 +
  1.3260 +/*
  1.3261 + * Functions and scripts.
  1.3262 + */
  1.3263 +extern JS_PUBLIC_API(JSFunction *)
  1.3264 +JS_NewFunction(JSContext *cx, JSNative call, unsigned nargs, unsigned flags,
  1.3265 +               JS::Handle<JSObject*> parent, const char *name);
  1.3266 +
  1.3267 +/*
  1.3268 + * Create the function with the name given by the id. JSID_IS_STRING(id) must
  1.3269 + * be true.
  1.3270 + */
  1.3271 +extern JS_PUBLIC_API(JSFunction *)
  1.3272 +JS_NewFunctionById(JSContext *cx, JSNative call, unsigned nargs, unsigned flags,
  1.3273 +                   JS::Handle<JSObject*> parent, JS::Handle<jsid> id);
  1.3274 +
  1.3275 +namespace JS {
  1.3276 +
  1.3277 +extern JS_PUBLIC_API(JSFunction *)
  1.3278 +GetSelfHostedFunction(JSContext *cx, const char *selfHostedName, JS::Handle<jsid> id,
  1.3279 +                      unsigned nargs);
  1.3280 +
  1.3281 +} /* namespace JS */
  1.3282 +
  1.3283 +extern JS_PUBLIC_API(JSObject *)
  1.3284 +JS_GetFunctionObject(JSFunction *fun);
  1.3285 +
  1.3286 +/*
  1.3287 + * Return the function's identifier as a JSString, or null if fun is unnamed.
  1.3288 + * The returned string lives as long as fun, so you don't need to root a saved
  1.3289 + * reference to it if fun is well-connected or rooted, and provided you bound
  1.3290 + * the use of the saved reference by fun's lifetime.
  1.3291 + */
  1.3292 +extern JS_PUBLIC_API(JSString *)
  1.3293 +JS_GetFunctionId(JSFunction *fun);
  1.3294 +
  1.3295 +/*
  1.3296 + * Return a function's display name. This is the defined name if one was given
  1.3297 + * where the function was defined, or it could be an inferred name by the JS
  1.3298 + * engine in the case that the function was defined to be anonymous. This can
  1.3299 + * still return nullptr if a useful display name could not be inferred. The
  1.3300 + * same restrictions on rooting as those in JS_GetFunctionId apply.
  1.3301 + */
  1.3302 +extern JS_PUBLIC_API(JSString *)
  1.3303 +JS_GetFunctionDisplayId(JSFunction *fun);
  1.3304 +
  1.3305 +/*
  1.3306 + * Return the arity (length) of fun.
  1.3307 + */
  1.3308 +extern JS_PUBLIC_API(uint16_t)
  1.3309 +JS_GetFunctionArity(JSFunction *fun);
  1.3310 +
  1.3311 +/*
  1.3312 + * Infallible predicate to test whether obj is a function object (faster than
  1.3313 + * comparing obj's class name to "Function", but equivalent unless someone has
  1.3314 + * overwritten the "Function" identifier with a different constructor and then
  1.3315 + * created instances using that constructor that might be passed in as obj).
  1.3316 + */
  1.3317 +extern JS_PUBLIC_API(bool)
  1.3318 +JS_ObjectIsFunction(JSContext *cx, JSObject *obj);
  1.3319 +
  1.3320 +extern JS_PUBLIC_API(bool)
  1.3321 +JS_ObjectIsCallable(JSContext *cx, JSObject *obj);
  1.3322 +
  1.3323 +extern JS_PUBLIC_API(bool)
  1.3324 +JS_IsNativeFunction(JSObject *funobj, JSNative call);
  1.3325 +
  1.3326 +/* Return whether the given function is a valid constructor. */
  1.3327 +extern JS_PUBLIC_API(bool)
  1.3328 +JS_IsConstructor(JSFunction *fun);
  1.3329 +
  1.3330 +/*
  1.3331 + * Bind the given callable to use the given object as "this".
  1.3332 + *
  1.3333 + * If |callable| is not callable, will throw and return nullptr.
  1.3334 + */
  1.3335 +extern JS_PUBLIC_API(JSObject*)
  1.3336 +JS_BindCallable(JSContext *cx, JS::Handle<JSObject*> callable, JS::Handle<JSObject*> newThis);
  1.3337 +
  1.3338 +extern JS_PUBLIC_API(bool)
  1.3339 +JS_DefineFunctions(JSContext *cx, JS::Handle<JSObject*> obj, const JSFunctionSpec *fs);
  1.3340 +
  1.3341 +extern JS_PUBLIC_API(JSFunction *)
  1.3342 +JS_DefineFunction(JSContext *cx, JS::Handle<JSObject*> obj, const char *name, JSNative call,
  1.3343 +                  unsigned nargs, unsigned attrs);
  1.3344 +
  1.3345 +extern JS_PUBLIC_API(JSFunction *)
  1.3346 +JS_DefineUCFunction(JSContext *cx, JS::Handle<JSObject*> obj,
  1.3347 +                    const jschar *name, size_t namelen, JSNative call,
  1.3348 +                    unsigned nargs, unsigned attrs);
  1.3349 +
  1.3350 +extern JS_PUBLIC_API(JSFunction *)
  1.3351 +JS_DefineFunctionById(JSContext *cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id, JSNative call,
  1.3352 +                      unsigned nargs, unsigned attrs);
  1.3353 +
  1.3354 +/*
  1.3355 + * Clone a top-level function into a new scope. This function will dynamically
  1.3356 + * fail if funobj was lexically nested inside some other function.
  1.3357 + */
  1.3358 +extern JS_PUBLIC_API(JSObject *)
  1.3359 +JS_CloneFunctionObject(JSContext *cx, JS::Handle<JSObject*> funobj, JS::Handle<JSObject*> parent);
  1.3360 +
  1.3361 +/*
  1.3362 + * Given a buffer, return false if the buffer might become a valid
  1.3363 + * javascript statement with the addition of more lines.  Otherwise return
  1.3364 + * true.  The intent is to support interactive compilation - accumulate
  1.3365 + * lines in a buffer until JS_BufferIsCompilableUnit is true, then pass it to
  1.3366 + * the compiler.
  1.3367 + */
  1.3368 +extern JS_PUBLIC_API(bool)
  1.3369 +JS_BufferIsCompilableUnit(JSContext *cx, JS::Handle<JSObject*> obj, const char *utf8,
  1.3370 +                          size_t length);
  1.3371 +
  1.3372 +extern JS_PUBLIC_API(JSScript *)
  1.3373 +JS_CompileScript(JSContext *cx, JS::HandleObject obj,
  1.3374 +                 const char *ascii, size_t length,
  1.3375 +                 const JS::CompileOptions &options);
  1.3376 +
  1.3377 +extern JS_PUBLIC_API(JSScript *)
  1.3378 +JS_CompileUCScript(JSContext *cx, JS::HandleObject obj,
  1.3379 +                   const jschar *chars, size_t length,
  1.3380 +                   const JS::CompileOptions &options);
  1.3381 +
  1.3382 +extern JS_PUBLIC_API(JSObject *)
  1.3383 +JS_GetGlobalFromScript(JSScript *script);
  1.3384 +
  1.3385 +extern JS_PUBLIC_API(JSFunction *)
  1.3386 +JS_CompileFunction(JSContext *cx, JS::HandleObject obj, const char *name,
  1.3387 +                   unsigned nargs, const char *const *argnames,
  1.3388 +                   const char *bytes, size_t length,
  1.3389 +                   const JS::CompileOptions &options);
  1.3390 +
  1.3391 +extern JS_PUBLIC_API(JSFunction *)
  1.3392 +JS_CompileUCFunction(JSContext *cx, JS::HandleObject obj, const char *name,
  1.3393 +                     unsigned nargs, const char *const *argnames,
  1.3394 +                     const jschar *chars, size_t length,
  1.3395 +                     const JS::CompileOptions &options);
  1.3396 +
  1.3397 +namespace JS {
  1.3398 +
  1.3399 +/* Options for JavaScript compilation. */
  1.3400 +
  1.3401 +/*
  1.3402 + * In the most common use case, a CompileOptions instance is allocated on the
  1.3403 + * stack, and holds non-owning references to non-POD option values: strings;
  1.3404 + * principals; objects; and so on. The code declaring the instance guarantees
  1.3405 + * that such option values will outlive the CompileOptions itself: objects are
  1.3406 + * otherwise rooted; principals have had their reference counts bumped; strings
  1.3407 + * will not be freed until the CompileOptions goes out of scope. In this
  1.3408 + * situation, CompileOptions only refers to things others own, so it can be
  1.3409 + * lightweight.
  1.3410 + *
  1.3411 + * In some cases, however, we need to hold compilation options with a
  1.3412 + * non-stack-like lifetime. For example, JS::CompileOffThread needs to save
  1.3413 + * compilation options where a worker thread can find them, and then return
  1.3414 + * immediately. The worker thread will come along at some later point, and use
  1.3415 + * the options.
  1.3416 + *
  1.3417 + * The compiler itself just needs to be able to access a collection of options;
  1.3418 + * it doesn't care who owns them, or what's keeping them alive. It does its own
  1.3419 + * addrefs/copies/tracing/etc.
  1.3420 + *
  1.3421 + * So, we have a class hierarchy that reflects these three use cases:
  1.3422 + *
  1.3423 + * - ReadOnlyCompileOptions is the common base class. It can be used by code
  1.3424 + *   that simply needs to access options set elsewhere, like the compiler.
  1.3425 + *
  1.3426 + * - The usual CompileOptions class must be stack-allocated, and holds
  1.3427 + *   non-owning references to the filename, element, and so on. It's derived
  1.3428 + *   from ReadOnlyCompileOptions, so the compiler can use it.
  1.3429 + *
  1.3430 + * - OwningCompileOptions roots / copies / reference counts of all its values,
  1.3431 + *   and unroots / frees / releases them when it is destructed. It too is
  1.3432 + *   derived from ReadOnlyCompileOptions, so the compiler accepts it.
  1.3433 + */
  1.3434 +
  1.3435 +/*
  1.3436 + * The common base class for the CompileOptions hierarchy.
  1.3437 + *
  1.3438 + * Use this in code that only needs to access compilation options created
  1.3439 + * elsewhere, like the compiler. Don't instantiate this class (the constructor
  1.3440 + * is protected anyway); instead, create instances only of the derived classes:
  1.3441 + * CompileOptions and OwningCompileOptions.
  1.3442 + */
  1.3443 +class JS_FRIEND_API(ReadOnlyCompileOptions)
  1.3444 +{
  1.3445 +    friend class CompileOptions;
  1.3446 +
  1.3447 +  protected:
  1.3448 +    JSPrincipals *originPrincipals_;
  1.3449 +    const char *filename_;
  1.3450 +    const char *introducerFilename_;
  1.3451 +    const jschar *sourceMapURL_;
  1.3452 +
  1.3453 +    // This constructor leaves 'version' set to JSVERSION_UNKNOWN. The structure
  1.3454 +    // is unusable until that's set to something more specific; the derived
  1.3455 +    // classes' constructors take care of that, in ways appropriate to their
  1.3456 +    // purpose.
  1.3457 +    ReadOnlyCompileOptions()
  1.3458 +      : originPrincipals_(nullptr),
  1.3459 +        filename_(nullptr),
  1.3460 +        introducerFilename_(nullptr),
  1.3461 +        sourceMapURL_(nullptr),
  1.3462 +        version(JSVERSION_UNKNOWN),
  1.3463 +        versionSet(false),
  1.3464 +        utf8(false),
  1.3465 +        lineno(1),
  1.3466 +        column(0),
  1.3467 +        compileAndGo(false),
  1.3468 +        forEval(false),
  1.3469 +        defineOnScope(true),
  1.3470 +        noScriptRval(false),
  1.3471 +        selfHostingMode(false),
  1.3472 +        canLazilyParse(true),
  1.3473 +        strictOption(false),
  1.3474 +        extraWarningsOption(false),
  1.3475 +        werrorOption(false),
  1.3476 +        asmJSOption(false),
  1.3477 +        forceAsync(false),
  1.3478 +        installedFile(false),
  1.3479 +        sourceIsLazy(false),
  1.3480 +        introductionType(nullptr),
  1.3481 +        introductionLineno(0),
  1.3482 +        introductionOffset(0),
  1.3483 +        hasIntroductionInfo(false)
  1.3484 +    { }
  1.3485 +
  1.3486 +    // Set all POD options (those not requiring reference counts, copies,
  1.3487 +    // rooting, or other hand-holding) to their values in |rhs|.
  1.3488 +    void copyPODOptions(const ReadOnlyCompileOptions &rhs);
  1.3489 +
  1.3490 +  public:
  1.3491 +    // Read-only accessors for non-POD options. The proper way to set these
  1.3492 +    // depends on the derived type.
  1.3493 +    JSPrincipals *originPrincipals(js::ExclusiveContext *cx) const;
  1.3494 +    const char *filename() const { return filename_; }
  1.3495 +    const char *introducerFilename() const { return introducerFilename_; }
  1.3496 +    const jschar *sourceMapURL() const { return sourceMapURL_; }
  1.3497 +    virtual JSObject *element() const = 0;
  1.3498 +    virtual JSString *elementAttributeName() const = 0;
  1.3499 +    virtual JSScript *introductionScript() const = 0;
  1.3500 +
  1.3501 +    // POD options.
  1.3502 +    JSVersion version;
  1.3503 +    bool versionSet;
  1.3504 +    bool utf8;
  1.3505 +    unsigned lineno;
  1.3506 +    unsigned column;
  1.3507 +    bool compileAndGo;
  1.3508 +    bool forEval;
  1.3509 +    bool defineOnScope;
  1.3510 +    bool noScriptRval;
  1.3511 +    bool selfHostingMode;
  1.3512 +    bool canLazilyParse;
  1.3513 +    bool strictOption;
  1.3514 +    bool extraWarningsOption;
  1.3515 +    bool werrorOption;
  1.3516 +    bool asmJSOption;
  1.3517 +    bool forceAsync;
  1.3518 +    bool installedFile;  // 'true' iff pre-compiling js file in packaged app
  1.3519 +    bool sourceIsLazy;
  1.3520 +
  1.3521 +    // |introductionType| is a statically allocated C string:
  1.3522 +    // one of "eval", "Function", or "GeneratorFunction".
  1.3523 +    const char *introductionType;
  1.3524 +    unsigned introductionLineno;
  1.3525 +    uint32_t introductionOffset;
  1.3526 +    bool hasIntroductionInfo;
  1.3527 +
  1.3528 +    // Wrap any compilation option values that need it as appropriate for
  1.3529 +    // use from |compartment|.
  1.3530 +    virtual bool wrap(JSContext *cx, JSCompartment *compartment) = 0;
  1.3531 +
  1.3532 +  private:
  1.3533 +    static JSObject * const nullObjectPtr;
  1.3534 +    void operator=(const ReadOnlyCompileOptions &) MOZ_DELETE;
  1.3535 +};
  1.3536 +
  1.3537 +/*
  1.3538 + * Compilation options, with dynamic lifetime. An instance of this type
  1.3539 + * makes a copy of / holds / roots all dynamically allocated resources
  1.3540 + * (principals; elements; strings) that it refers to. Its destructor frees
  1.3541 + * / drops / unroots them. This is heavier than CompileOptions, below, but
  1.3542 + * unlike CompileOptions, it can outlive any given stack frame.
  1.3543 + *
  1.3544 + * Note that this *roots* any JS values it refers to - they're live
  1.3545 + * unconditionally. Thus, instances of this type can't be owned, directly
  1.3546 + * or indirectly, by a JavaScript object: if any value that this roots ever
  1.3547 + * comes to refer to the object that owns this, then the whole cycle, and
  1.3548 + * anything else it entrains, will never be freed.
  1.3549 + */
  1.3550 +class JS_FRIEND_API(OwningCompileOptions) : public ReadOnlyCompileOptions
  1.3551 +{
  1.3552 +    JSRuntime *runtime;
  1.3553 +    PersistentRootedObject elementRoot;
  1.3554 +    PersistentRootedString elementAttributeNameRoot;
  1.3555 +    PersistentRootedScript introductionScriptRoot;
  1.3556 +
  1.3557 +  public:
  1.3558 +    // A minimal constructor, for use with OwningCompileOptions::copy. This
  1.3559 +    // leaves |this.version| set to JSVERSION_UNKNOWN; the instance
  1.3560 +    // shouldn't be used until we've set that to something real (as |copy|
  1.3561 +    // will).
  1.3562 +    explicit OwningCompileOptions(JSContext *cx);
  1.3563 +    ~OwningCompileOptions();
  1.3564 +
  1.3565 +    JSObject *element() const MOZ_OVERRIDE { return elementRoot; }
  1.3566 +    JSString *elementAttributeName() const MOZ_OVERRIDE { return elementAttributeNameRoot; }
  1.3567 +    JSScript *introductionScript() const MOZ_OVERRIDE { return introductionScriptRoot; }
  1.3568 +
  1.3569 +    // Set this to a copy of |rhs|. Return false on OOM.
  1.3570 +    bool copy(JSContext *cx, const ReadOnlyCompileOptions &rhs);
  1.3571 +
  1.3572 +    /* These setters make copies of their string arguments, and are fallible. */
  1.3573 +    bool setFile(JSContext *cx, const char *f);
  1.3574 +    bool setFileAndLine(JSContext *cx, const char *f, unsigned l);
  1.3575 +    bool setSourceMapURL(JSContext *cx, const jschar *s);
  1.3576 +    bool setIntroducerFilename(JSContext *cx, const char *s);
  1.3577 +
  1.3578 +    /* These setters are infallible, and can be chained. */
  1.3579 +    OwningCompileOptions &setLine(unsigned l)             { lineno = l;              return *this; }
  1.3580 +    OwningCompileOptions &setElement(JSObject *e) {
  1.3581 +        elementRoot = e;
  1.3582 +        return *this;
  1.3583 +    }
  1.3584 +    OwningCompileOptions &setElementAttributeName(JSString *p) {
  1.3585 +        elementAttributeNameRoot = p;
  1.3586 +        return *this;
  1.3587 +    }
  1.3588 +    OwningCompileOptions &setIntroductionScript(JSScript *s) {
  1.3589 +        introductionScriptRoot = s;
  1.3590 +        return *this;
  1.3591 +    }
  1.3592 +    OwningCompileOptions &setOriginPrincipals(JSPrincipals *p) {
  1.3593 +        if (p) JS_HoldPrincipals(p);
  1.3594 +        if (originPrincipals_) JS_DropPrincipals(runtime, originPrincipals_);
  1.3595 +        originPrincipals_ = p;
  1.3596 +        return *this;
  1.3597 +    }
  1.3598 +    OwningCompileOptions &setVersion(JSVersion v) {
  1.3599 +        version = v;
  1.3600 +        versionSet = true;
  1.3601 +        return *this;
  1.3602 +    }
  1.3603 +    OwningCompileOptions &setUTF8(bool u) { utf8 = u; return *this; }
  1.3604 +    OwningCompileOptions &setColumn(unsigned c) { column = c; return *this; }
  1.3605 +    OwningCompileOptions &setCompileAndGo(bool cng) { compileAndGo = cng; return *this; }
  1.3606 +    OwningCompileOptions &setForEval(bool eval) { forEval = eval; return *this; }
  1.3607 +    OwningCompileOptions &setDefineOnScope(bool define) { defineOnScope = define; return *this; }
  1.3608 +    OwningCompileOptions &setNoScriptRval(bool nsr) { noScriptRval = nsr; return *this; }
  1.3609 +    OwningCompileOptions &setSelfHostingMode(bool shm) { selfHostingMode = shm; return *this; }
  1.3610 +    OwningCompileOptions &setCanLazilyParse(bool clp) { canLazilyParse = clp; return *this; }
  1.3611 +    OwningCompileOptions &setSourceIsLazy(bool l) { sourceIsLazy = l; return *this; }
  1.3612 +    OwningCompileOptions &setIntroductionType(const char *t) { introductionType = t; return *this; }
  1.3613 +    bool setIntroductionInfo(JSContext *cx, const char *introducerFn, const char *intro,
  1.3614 +                             unsigned line, JSScript *script, uint32_t offset)
  1.3615 +    {
  1.3616 +        if (!setIntroducerFilename(cx, introducerFn))
  1.3617 +            return false;
  1.3618 +        introductionType = intro;
  1.3619 +        introductionLineno = line;
  1.3620 +        introductionScriptRoot = script;
  1.3621 +        introductionOffset = offset;
  1.3622 +        hasIntroductionInfo = true;
  1.3623 +        return true;
  1.3624 +    }
  1.3625 +
  1.3626 +    virtual bool wrap(JSContext *cx, JSCompartment *compartment) MOZ_OVERRIDE;
  1.3627 +
  1.3628 +  private:
  1.3629 +    void operator=(const CompileOptions &rhs) MOZ_DELETE;
  1.3630 +};
  1.3631 +
  1.3632 +/*
  1.3633 + * Compilation options stored on the stack. An instance of this type
  1.3634 + * simply holds references to dynamically allocated resources (element;
  1.3635 + * filename; source map URL) that are owned by something else. If you
  1.3636 + * create an instance of this type, it's up to you to guarantee that
  1.3637 + * everything you store in it will outlive it.
  1.3638 + */
  1.3639 +class MOZ_STACK_CLASS JS_FRIEND_API(CompileOptions) : public ReadOnlyCompileOptions
  1.3640 +{
  1.3641 +    RootedObject elementRoot;
  1.3642 +    RootedString elementAttributeNameRoot;
  1.3643 +    RootedScript introductionScriptRoot;
  1.3644 +
  1.3645 +  public:
  1.3646 +    explicit CompileOptions(JSContext *cx, JSVersion version = JSVERSION_UNKNOWN);
  1.3647 +    CompileOptions(js::ContextFriendFields *cx, const ReadOnlyCompileOptions &rhs)
  1.3648 +      : ReadOnlyCompileOptions(), elementRoot(cx), elementAttributeNameRoot(cx),
  1.3649 +        introductionScriptRoot(cx)
  1.3650 +    {
  1.3651 +        copyPODOptions(rhs);
  1.3652 +
  1.3653 +        originPrincipals_ = rhs.originPrincipals_;
  1.3654 +        filename_ = rhs.filename();
  1.3655 +        sourceMapURL_ = rhs.sourceMapURL();
  1.3656 +        elementRoot = rhs.element();
  1.3657 +        elementAttributeNameRoot = rhs.elementAttributeName();
  1.3658 +        introductionScriptRoot = rhs.introductionScript();
  1.3659 +    }
  1.3660 +
  1.3661 +    JSObject *element() const MOZ_OVERRIDE { return elementRoot; }
  1.3662 +    JSString *elementAttributeName() const MOZ_OVERRIDE { return elementAttributeNameRoot; }
  1.3663 +    JSScript *introductionScript() const MOZ_OVERRIDE { return introductionScriptRoot; }
  1.3664 +
  1.3665 +    CompileOptions &setFile(const char *f) { filename_ = f; return *this; }
  1.3666 +    CompileOptions &setLine(unsigned l) { lineno = l; return *this; }
  1.3667 +    CompileOptions &setFileAndLine(const char *f, unsigned l) {
  1.3668 +        filename_ = f; lineno = l; return *this;
  1.3669 +    }
  1.3670 +    CompileOptions &setSourceMapURL(const jschar *s) { sourceMapURL_ = s;       return *this; }
  1.3671 +    CompileOptions &setElement(JSObject *e)          { elementRoot = e;         return *this; }
  1.3672 +    CompileOptions &setElementAttributeName(JSString *p) {
  1.3673 +        elementAttributeNameRoot = p;
  1.3674 +        return *this;
  1.3675 +    }
  1.3676 +    CompileOptions &setIntroductionScript(JSScript *s) {
  1.3677 +        introductionScriptRoot = s;
  1.3678 +        return *this;
  1.3679 +    }
  1.3680 +    CompileOptions &setOriginPrincipals(JSPrincipals *p) {
  1.3681 +        originPrincipals_ = p;
  1.3682 +        return *this;
  1.3683 +    }
  1.3684 +    CompileOptions &setVersion(JSVersion v) {
  1.3685 +        version = v;
  1.3686 +        versionSet = true;
  1.3687 +        return *this;
  1.3688 +    }
  1.3689 +    CompileOptions &setUTF8(bool u) { utf8 = u; return *this; }
  1.3690 +    CompileOptions &setColumn(unsigned c) { column = c; return *this; }
  1.3691 +    CompileOptions &setCompileAndGo(bool cng) { compileAndGo = cng; return *this; }
  1.3692 +    CompileOptions &setForEval(bool eval) { forEval = eval; return *this; }
  1.3693 +    CompileOptions &setDefineOnScope(bool define) { defineOnScope = define; return *this; }
  1.3694 +    CompileOptions &setNoScriptRval(bool nsr) { noScriptRval = nsr; return *this; }
  1.3695 +    CompileOptions &setSelfHostingMode(bool shm) { selfHostingMode = shm; return *this; }
  1.3696 +    CompileOptions &setCanLazilyParse(bool clp) { canLazilyParse = clp; return *this; }
  1.3697 +    CompileOptions &setSourceIsLazy(bool l) { sourceIsLazy = l; return *this; }
  1.3698 +    CompileOptions &setIntroductionType(const char *t) { introductionType = t; return *this; }
  1.3699 +    CompileOptions &setIntroductionInfo(const char *introducerFn, const char *intro,
  1.3700 +                                        unsigned line, JSScript *script, uint32_t offset)
  1.3701 +    {
  1.3702 +        introducerFilename_ = introducerFn;
  1.3703 +        introductionType = intro;
  1.3704 +        introductionLineno = line;
  1.3705 +        introductionScriptRoot = script;
  1.3706 +        introductionOffset = offset;
  1.3707 +        hasIntroductionInfo = true;
  1.3708 +        return *this;
  1.3709 +    }
  1.3710 +
  1.3711 +    virtual bool wrap(JSContext *cx, JSCompartment *compartment) MOZ_OVERRIDE;
  1.3712 +
  1.3713 +  private:
  1.3714 +    void operator=(const CompileOptions &rhs) MOZ_DELETE;
  1.3715 +};
  1.3716 +
  1.3717 +/*
  1.3718 + * |script| will always be set. On failure, it will be set to nullptr.
  1.3719 + */
  1.3720 +extern JS_PUBLIC_API(bool)
  1.3721 +Compile(JSContext *cx, JS::HandleObject obj, const ReadOnlyCompileOptions &options,
  1.3722 +        SourceBufferHolder &srcBuf, JS::MutableHandleScript script);
  1.3723 +
  1.3724 +extern JS_PUBLIC_API(JSScript *)
  1.3725 +Compile(JSContext *cx, JS::HandleObject obj, const ReadOnlyCompileOptions &options,
  1.3726 +        const char *bytes, size_t length);
  1.3727 +
  1.3728 +extern JS_PUBLIC_API(JSScript *)
  1.3729 +Compile(JSContext *cx, JS::HandleObject obj, const ReadOnlyCompileOptions &options,
  1.3730 +        const jschar *chars, size_t length);
  1.3731 +
  1.3732 +extern JS_PUBLIC_API(JSScript *)
  1.3733 +Compile(JSContext *cx, JS::HandleObject obj, const ReadOnlyCompileOptions &options, FILE *file);
  1.3734 +
  1.3735 +extern JS_PUBLIC_API(JSScript *)
  1.3736 +Compile(JSContext *cx, JS::HandleObject obj, const ReadOnlyCompileOptions &options, const char *filename);
  1.3737 +
  1.3738 +extern JS_PUBLIC_API(bool)
  1.3739 +CanCompileOffThread(JSContext *cx, const ReadOnlyCompileOptions &options, size_t length);
  1.3740 +
  1.3741 +/*
  1.3742 + * Off thread compilation control flow.
  1.3743 + *
  1.3744 + * After successfully triggering an off thread compile of a script, the
  1.3745 + * callback will eventually be invoked with the specified data and a token
  1.3746 + * for the compilation. The callback will be invoked while off the main thread,
  1.3747 + * so must ensure that its operations are thread safe. Afterwards,
  1.3748 + * FinishOffThreadScript must be invoked on the main thread to get the result
  1.3749 + * script or nullptr. If maybecx is not specified, the resources will be freed,
  1.3750 + * but no script will be returned.
  1.3751 + *
  1.3752 + * The characters passed in to CompileOffThread must remain live until the
  1.3753 + * callback is invoked, and the resulting script will be rooted until the call
  1.3754 + * to FinishOffThreadScript.
  1.3755 + */
  1.3756 +
  1.3757 +extern JS_PUBLIC_API(bool)
  1.3758 +CompileOffThread(JSContext *cx, const ReadOnlyCompileOptions &options,
  1.3759 +                 const jschar *chars, size_t length,
  1.3760 +                 OffThreadCompileCallback callback, void *callbackData);
  1.3761 +
  1.3762 +extern JS_PUBLIC_API(JSScript *)
  1.3763 +FinishOffThreadScript(JSContext *maybecx, JSRuntime *rt, void *token);
  1.3764 +
  1.3765 +extern JS_PUBLIC_API(bool)
  1.3766 +CompileFunction(JSContext *cx, JS::HandleObject obj, const ReadOnlyCompileOptions &options,
  1.3767 +                const char *name, unsigned nargs, const char *const *argnames,
  1.3768 +                SourceBufferHolder &srcBuf, JS::MutableHandleFunction fun);
  1.3769 +
  1.3770 +extern JS_PUBLIC_API(JSFunction *)
  1.3771 +CompileFunction(JSContext *cx, JS::HandleObject obj, const ReadOnlyCompileOptions &options,
  1.3772 +                const char *name, unsigned nargs, const char *const *argnames,
  1.3773 +                const char *bytes, size_t length);
  1.3774 +
  1.3775 +extern JS_PUBLIC_API(JSFunction *)
  1.3776 +CompileFunction(JSContext *cx, JS::HandleObject obj, const ReadOnlyCompileOptions &options,
  1.3777 +                const char *name, unsigned nargs, const char *const *argnames,
  1.3778 +                const jschar *chars, size_t length);
  1.3779 +
  1.3780 +} /* namespace JS */
  1.3781 +
  1.3782 +extern JS_PUBLIC_API(JSString *)
  1.3783 +JS_DecompileScript(JSContext *cx, JS::Handle<JSScript*> script, const char *name, unsigned indent);
  1.3784 +
  1.3785 +/*
  1.3786 + * API extension: OR this into indent to avoid pretty-printing the decompiled
  1.3787 + * source resulting from JS_DecompileFunction{,Body}.
  1.3788 + */
  1.3789 +#define JS_DONT_PRETTY_PRINT    ((unsigned)0x8000)
  1.3790 +
  1.3791 +extern JS_PUBLIC_API(JSString *)
  1.3792 +JS_DecompileFunction(JSContext *cx, JS::Handle<JSFunction*> fun, unsigned indent);
  1.3793 +
  1.3794 +extern JS_PUBLIC_API(JSString *)
  1.3795 +JS_DecompileFunctionBody(JSContext *cx, JS::Handle<JSFunction*> fun, unsigned indent);
  1.3796 +
  1.3797 +/*
  1.3798 + * NB: JS_ExecuteScript and the JS_Evaluate*Script* quadruplets use the obj
  1.3799 + * parameter as the initial scope chain header, the 'this' keyword value, and
  1.3800 + * the variables object (ECMA parlance for where 'var' and 'function' bind
  1.3801 + * names) of the execution context for script.
  1.3802 + *
  1.3803 + * Using obj as the variables object is problematic if obj's parent (which is
  1.3804 + * the scope chain link; see JS_SetParent and JS_NewObject) is not null: in
  1.3805 + * this case, variables created by 'var x = 0', e.g., go in obj, but variables
  1.3806 + * created by assignment to an unbound id, 'x = 0', go in the last object on
  1.3807 + * the scope chain linked by parent.
  1.3808 + *
  1.3809 + * ECMA calls that last scoping object the "global object", but note that many
  1.3810 + * embeddings have several such objects.  ECMA requires that "global code" be
  1.3811 + * executed with the variables object equal to this global object.  But these
  1.3812 + * JS API entry points provide freedom to execute code against a "sub-global",
  1.3813 + * i.e., a parented or scoped object, in which case the variables object will
  1.3814 + * differ from the last object on the scope chain, resulting in confusing and
  1.3815 + * non-ECMA explicit vs. implicit variable creation.
  1.3816 + *
  1.3817 + * Caveat embedders: unless you already depend on this buggy variables object
  1.3818 + * binding behavior, you should call ContextOptionsRef(cx).setVarObjFix(true)
  1.3819 + * for each context in the application, if you pass parented objects as the obj
  1.3820 + * parameter, or may ever pass such objects in the future.
  1.3821 + *
  1.3822 + * Why a runtime option?  The alternative is to add six or so new API entry
  1.3823 + * points with signatures matching the following six, and that doesn't seem
  1.3824 + * worth the code bloat cost.  Such new entry points would probably have less
  1.3825 + * obvious names, too, so would not tend to be used.  The JS_SetOption call,
  1.3826 + * OTOH, can be more easily hacked into existing code that does not depend on
  1.3827 + * the bug; such code can continue to use the familiar JS_EvaluateScript,
  1.3828 + * etc., entry points.
  1.3829 + */
  1.3830 +extern JS_PUBLIC_API(bool)
  1.3831 +JS_ExecuteScript(JSContext *cx, JS::HandleObject obj, JS::HandleScript script, JS::MutableHandleValue rval);
  1.3832 +
  1.3833 +extern JS_PUBLIC_API(bool)
  1.3834 +JS_ExecuteScript(JSContext *cx, JS::HandleObject obj, JS::HandleScript script);
  1.3835 +
  1.3836 +namespace JS {
  1.3837 +
  1.3838 +/*
  1.3839 + * Like the above, but handles a cross-compartment script. If the script is
  1.3840 + * cross-compartment, it is cloned into the current compartment before executing.
  1.3841 + */
  1.3842 +extern JS_PUBLIC_API(bool)
  1.3843 +CloneAndExecuteScript(JSContext *cx, JS::Handle<JSObject*> obj, JS::Handle<JSScript*> script);
  1.3844 +
  1.3845 +} /* namespace JS */
  1.3846 +
  1.3847 +extern JS_PUBLIC_API(bool)
  1.3848 +JS_ExecuteScriptVersion(JSContext *cx, JS::HandleObject obj, JS::HandleScript script,
  1.3849 +                        JS::MutableHandleValue rval, JSVersion version);
  1.3850 +
  1.3851 +extern JS_PUBLIC_API(bool)
  1.3852 +JS_ExecuteScriptVersion(JSContext *cx, JS::HandleObject obj, JS::HandleScript script,
  1.3853 +                        JSVersion version);
  1.3854 +
  1.3855 +extern JS_PUBLIC_API(bool)
  1.3856 +JS_EvaluateScript(JSContext *cx, JS::HandleObject obj,
  1.3857 +                  const char *bytes, unsigned length,
  1.3858 +                  const char *filename, unsigned lineno,
  1.3859 +                  JS::MutableHandleValue rval);
  1.3860 +
  1.3861 +extern JS_PUBLIC_API(bool)
  1.3862 +JS_EvaluateScript(JSContext *cx, JS::HandleObject obj,
  1.3863 +                  const char *bytes, unsigned length,
  1.3864 +                  const char *filename, unsigned lineno);
  1.3865 +
  1.3866 +extern JS_PUBLIC_API(bool)
  1.3867 +JS_EvaluateUCScript(JSContext *cx, JS::Handle<JSObject*> obj,
  1.3868 +                    const jschar *chars, unsigned length,
  1.3869 +                    const char *filename, unsigned lineno,
  1.3870 +                    JS::MutableHandle<JS::Value> rval);
  1.3871 +
  1.3872 +namespace JS {
  1.3873 +
  1.3874 +extern JS_PUBLIC_API(bool)
  1.3875 +Evaluate(JSContext *cx, JS::HandleObject obj, const ReadOnlyCompileOptions &options,
  1.3876 +         SourceBufferHolder &srcBuf, JS::MutableHandleValue rval);
  1.3877 +
  1.3878 +extern JS_PUBLIC_API(bool)
  1.3879 +Evaluate(JSContext *cx, JS::HandleObject obj, const ReadOnlyCompileOptions &options,
  1.3880 +         const jschar *chars, size_t length, JS::MutableHandleValue rval);
  1.3881 +
  1.3882 +extern JS_PUBLIC_API(bool)
  1.3883 +Evaluate(JSContext *cx, JS::HandleObject obj, const ReadOnlyCompileOptions &options,
  1.3884 +         const char *bytes, size_t length, JS::MutableHandleValue rval);
  1.3885 +
  1.3886 +extern JS_PUBLIC_API(bool)
  1.3887 +Evaluate(JSContext *cx, JS::HandleObject obj, const ReadOnlyCompileOptions &options,
  1.3888 +         const char *filename, JS::MutableHandleValue rval);
  1.3889 +
  1.3890 +extern JS_PUBLIC_API(bool)
  1.3891 +Evaluate(JSContext *cx, JS::HandleObject obj, const ReadOnlyCompileOptions &options,
  1.3892 +         SourceBufferHolder &srcBuf);
  1.3893 +
  1.3894 +extern JS_PUBLIC_API(bool)
  1.3895 +Evaluate(JSContext *cx, JS::HandleObject obj, const ReadOnlyCompileOptions &options,
  1.3896 +         const jschar *chars, size_t length);
  1.3897 +
  1.3898 +extern JS_PUBLIC_API(bool)
  1.3899 +Evaluate(JSContext *cx, JS::HandleObject obj, const ReadOnlyCompileOptions &options,
  1.3900 +         const char *bytes, size_t length);
  1.3901 +
  1.3902 +extern JS_PUBLIC_API(bool)
  1.3903 +Evaluate(JSContext *cx, JS::HandleObject obj, const ReadOnlyCompileOptions &options,
  1.3904 +         const char *filename);
  1.3905 +
  1.3906 +} /* namespace JS */
  1.3907 +
  1.3908 +extern JS_PUBLIC_API(bool)
  1.3909 +JS_CallFunction(JSContext *cx, JS::HandleObject obj, JS::HandleFunction fun,
  1.3910 +                const JS::HandleValueArray& args, JS::MutableHandleValue rval);
  1.3911 +
  1.3912 +extern JS_PUBLIC_API(bool)
  1.3913 +JS_CallFunctionName(JSContext *cx, JS::HandleObject obj, const char *name,
  1.3914 +                    const JS::HandleValueArray& args, JS::MutableHandleValue rval);
  1.3915 +
  1.3916 +extern JS_PUBLIC_API(bool)
  1.3917 +JS_CallFunctionValue(JSContext *cx, JS::HandleObject obj, JS::HandleValue fval,
  1.3918 +                     const JS::HandleValueArray& args, JS::MutableHandleValue rval);
  1.3919 +
  1.3920 +namespace JS {
  1.3921 +
  1.3922 +static inline bool
  1.3923 +Call(JSContext *cx, JS::HandleObject thisObj, JS::HandleFunction fun,
  1.3924 +     const JS::HandleValueArray &args, MutableHandleValue rval)
  1.3925 +{
  1.3926 +    return !!JS_CallFunction(cx, thisObj, fun, args, rval);
  1.3927 +}
  1.3928 +
  1.3929 +static inline bool
  1.3930 +Call(JSContext *cx, JS::HandleObject thisObj, const char *name, const JS::HandleValueArray& args,
  1.3931 +     MutableHandleValue rval)
  1.3932 +{
  1.3933 +    return !!JS_CallFunctionName(cx, thisObj, name, args, rval);
  1.3934 +}
  1.3935 +
  1.3936 +static inline bool
  1.3937 +Call(JSContext *cx, JS::HandleObject thisObj, JS::HandleValue fun, const JS::HandleValueArray& args,
  1.3938 +     MutableHandleValue rval)
  1.3939 +{
  1.3940 +    return !!JS_CallFunctionValue(cx, thisObj, fun, args, rval);
  1.3941 +}
  1.3942 +
  1.3943 +extern JS_PUBLIC_API(bool)
  1.3944 +Call(JSContext *cx, JS::HandleValue thisv, JS::HandleValue fun, const JS::HandleValueArray& args,
  1.3945 +     MutableHandleValue rval);
  1.3946 +
  1.3947 +static inline bool
  1.3948 +Call(JSContext *cx, JS::HandleValue thisv, JS::HandleObject funObj, const JS::HandleValueArray& args,
  1.3949 +     MutableHandleValue rval)
  1.3950 +{
  1.3951 +    JS_ASSERT(funObj);
  1.3952 +    JS::RootedValue fun(cx, JS::ObjectValue(*funObj));
  1.3953 +    return Call(cx, thisv, fun, args, rval);
  1.3954 +}
  1.3955 +
  1.3956 +} /* namespace JS */
  1.3957 +
  1.3958 +/*
  1.3959 + * These functions allow setting an interrupt callback that will be called
  1.3960 + * from the JS thread some time after any thread triggered the callback using
  1.3961 + * JS_RequestInterruptCallback(rt).
  1.3962 + *
  1.3963 + * To schedule the GC and for other activities the engine internally triggers
  1.3964 + * interrupt callbacks. The embedding should thus not rely on callbacks being
  1.3965 + * triggered through the external API only.
  1.3966 + *
  1.3967 + * Important note: Additional callbacks can occur inside the callback handler
  1.3968 + * if it re-enters the JS engine. The embedding must ensure that the callback
  1.3969 + * is disconnected before attempting such re-entry.
  1.3970 + */
  1.3971 +extern JS_PUBLIC_API(JSInterruptCallback)
  1.3972 +JS_SetInterruptCallback(JSRuntime *rt, JSInterruptCallback callback);
  1.3973 +
  1.3974 +extern JS_PUBLIC_API(JSInterruptCallback)
  1.3975 +JS_GetInterruptCallback(JSRuntime *rt);
  1.3976 +
  1.3977 +extern JS_PUBLIC_API(void)
  1.3978 +JS_RequestInterruptCallback(JSRuntime *rt);
  1.3979 +
  1.3980 +extern JS_PUBLIC_API(bool)
  1.3981 +JS_IsRunning(JSContext *cx);
  1.3982 +
  1.3983 +/*
  1.3984 + * Saving and restoring frame chains.
  1.3985 + *
  1.3986 + * These two functions are used to set aside cx's call stack while that stack
  1.3987 + * is inactive. After a call to JS_SaveFrameChain, it looks as if there is no
  1.3988 + * code running on cx. Before calling JS_RestoreFrameChain, cx's call stack
  1.3989 + * must be balanced and all nested calls to JS_SaveFrameChain must have had
  1.3990 + * matching JS_RestoreFrameChain calls.
  1.3991 + *
  1.3992 + * JS_SaveFrameChain deals with cx not having any code running on it.
  1.3993 + */
  1.3994 +extern JS_PUBLIC_API(bool)
  1.3995 +JS_SaveFrameChain(JSContext *cx);
  1.3996 +
  1.3997 +extern JS_PUBLIC_API(void)
  1.3998 +JS_RestoreFrameChain(JSContext *cx);
  1.3999 +
  1.4000 +#ifdef MOZ_TRACE_JSCALLS
  1.4001 +/*
  1.4002 + * The callback is expected to be quick and noninvasive. It should not
  1.4003 + * request interrupts, turn on debugging, or produce uncaught JS
  1.4004 + * exceptions. The state of the stack and registers in the context
  1.4005 + * cannot be relied upon, since this callback may be invoked directly
  1.4006 + * from either JIT. The 'entering' field means we are entering a
  1.4007 + * function if it is positive, leaving a function if it is zero or
  1.4008 + * negative.
  1.4009 + */
  1.4010 +extern JS_PUBLIC_API(void)
  1.4011 +JS_SetFunctionCallback(JSContext *cx, JSFunctionCallback fcb);
  1.4012 +
  1.4013 +extern JS_PUBLIC_API(JSFunctionCallback)
  1.4014 +JS_GetFunctionCallback(JSContext *cx);
  1.4015 +#endif /* MOZ_TRACE_JSCALLS */
  1.4016 +
  1.4017 +/************************************************************************/
  1.4018 +
  1.4019 +/*
  1.4020 + * Strings.
  1.4021 + *
  1.4022 + * NB: JS_NewUCString takes ownership of bytes on success, avoiding a copy;
  1.4023 + * but on error (signified by null return), it leaves chars owned by the
  1.4024 + * caller. So the caller must free bytes in the error case, if it has no use
  1.4025 + * for them. In contrast, all the JS_New*StringCopy* functions do not take
  1.4026 + * ownership of the character memory passed to them -- they copy it.
  1.4027 + */
  1.4028 +extern JS_PUBLIC_API(JSString *)
  1.4029 +JS_NewStringCopyN(JSContext *cx, const char *s, size_t n);
  1.4030 +
  1.4031 +extern JS_PUBLIC_API(JSString *)
  1.4032 +JS_NewStringCopyZ(JSContext *cx, const char *s);
  1.4033 +
  1.4034 +extern JS_PUBLIC_API(JSString *)
  1.4035 +JS_InternJSString(JSContext *cx, JS::HandleString str);
  1.4036 +
  1.4037 +extern JS_PUBLIC_API(JSString *)
  1.4038 +JS_InternStringN(JSContext *cx, const char *s, size_t length);
  1.4039 +
  1.4040 +extern JS_PUBLIC_API(JSString *)
  1.4041 +JS_InternString(JSContext *cx, const char *s);
  1.4042 +
  1.4043 +extern JS_PUBLIC_API(JSString *)
  1.4044 +JS_NewUCString(JSContext *cx, jschar *chars, size_t length);
  1.4045 +
  1.4046 +extern JS_PUBLIC_API(JSString *)
  1.4047 +JS_NewUCStringCopyN(JSContext *cx, const jschar *s, size_t n);
  1.4048 +
  1.4049 +extern JS_PUBLIC_API(JSString *)
  1.4050 +JS_NewUCStringCopyZ(JSContext *cx, const jschar *s);
  1.4051 +
  1.4052 +extern JS_PUBLIC_API(JSString *)
  1.4053 +JS_InternUCStringN(JSContext *cx, const jschar *s, size_t length);
  1.4054 +
  1.4055 +extern JS_PUBLIC_API(JSString *)
  1.4056 +JS_InternUCString(JSContext *cx, const jschar *s);
  1.4057 +
  1.4058 +extern JS_PUBLIC_API(bool)
  1.4059 +JS_CompareStrings(JSContext *cx, JSString *str1, JSString *str2, int32_t *result);
  1.4060 +
  1.4061 +extern JS_PUBLIC_API(bool)
  1.4062 +JS_StringEqualsAscii(JSContext *cx, JSString *str, const char *asciiBytes, bool *match);
  1.4063 +
  1.4064 +extern JS_PUBLIC_API(size_t)
  1.4065 +JS_PutEscapedString(JSContext *cx, char *buffer, size_t size, JSString *str, char quote);
  1.4066 +
  1.4067 +extern JS_PUBLIC_API(bool)
  1.4068 +JS_FileEscapedString(FILE *fp, JSString *str, char quote);
  1.4069 +
  1.4070 +/*
  1.4071 + * Extracting string characters and length.
  1.4072 + *
  1.4073 + * While getting the length of a string is infallible, getting the chars can
  1.4074 + * fail. As indicated by the lack of a JSContext parameter, there are two
  1.4075 + * special cases where getting the chars is infallible:
  1.4076 + *
  1.4077 + * The first case is interned strings, i.e., strings from JS_InternString or
  1.4078 + * JSID_TO_STRING(id), using JS_GetInternedStringChars*.
  1.4079 + *
  1.4080 + * The second case is "flat" strings that have been explicitly prepared in a
  1.4081 + * fallible context by JS_FlattenString. To catch errors, a separate opaque
  1.4082 + * JSFlatString type is returned by JS_FlattenString and expected by
  1.4083 + * JS_GetFlatStringChars. Note, though, that this is purely a syntactic
  1.4084 + * distinction: the input and output of JS_FlattenString are the same actual
  1.4085 + * GC-thing so only one needs to be rooted. If a JSString is known to be flat,
  1.4086 + * JS_ASSERT_STRING_IS_FLAT can be used to make a debug-checked cast. Example:
  1.4087 + *
  1.4088 + *   // in a fallible context
  1.4089 + *   JSFlatString *fstr = JS_FlattenString(cx, str);
  1.4090 + *   if (!fstr)
  1.4091 + *     return false;
  1.4092 + *   JS_ASSERT(fstr == JS_ASSERT_STRING_IS_FLAT(str));
  1.4093 + *
  1.4094 + *   // in an infallible context, for the same 'str'
  1.4095 + *   const jschar *chars = JS_GetFlatStringChars(fstr)
  1.4096 + *   JS_ASSERT(chars);
  1.4097 + *
  1.4098 + * The CharsZ APIs guarantee that the returned array has a null character at
  1.4099 + * chars[length]. This can require additional copying so clients should prefer
  1.4100 + * APIs without CharsZ if possible. The infallible functions also return
  1.4101 + * null-terminated arrays. (There is no additional cost or non-Z alternative
  1.4102 + * for the infallible functions, so 'Z' is left out of the identifier.)
  1.4103 + */
  1.4104 +
  1.4105 +extern JS_PUBLIC_API(size_t)
  1.4106 +JS_GetStringLength(JSString *str);
  1.4107 +
  1.4108 +extern JS_PUBLIC_API(const jschar *)
  1.4109 +JS_GetStringCharsAndLength(JSContext *cx, JSString *str, size_t *length);
  1.4110 +
  1.4111 +extern JS_PUBLIC_API(const jschar *)
  1.4112 +JS_GetInternedStringChars(JSString *str);
  1.4113 +
  1.4114 +extern JS_PUBLIC_API(const jschar *)
  1.4115 +JS_GetInternedStringCharsAndLength(JSString *str, size_t *length);
  1.4116 +
  1.4117 +extern JS_PUBLIC_API(const jschar *)
  1.4118 +JS_GetStringCharsZ(JSContext *cx, JSString *str);
  1.4119 +
  1.4120 +extern JS_PUBLIC_API(const jschar *)
  1.4121 +JS_GetStringCharsZAndLength(JSContext *cx, JSString *str, size_t *length);
  1.4122 +
  1.4123 +extern JS_PUBLIC_API(JSFlatString *)
  1.4124 +JS_FlattenString(JSContext *cx, JSString *str);
  1.4125 +
  1.4126 +extern JS_PUBLIC_API(const jschar *)
  1.4127 +JS_GetFlatStringChars(JSFlatString *str);
  1.4128 +
  1.4129 +static MOZ_ALWAYS_INLINE JSFlatString *
  1.4130 +JSID_TO_FLAT_STRING(jsid id)
  1.4131 +{
  1.4132 +    JS_ASSERT(JSID_IS_STRING(id));
  1.4133 +    return (JSFlatString *)(JSID_BITS(id));
  1.4134 +}
  1.4135 +
  1.4136 +static MOZ_ALWAYS_INLINE JSFlatString *
  1.4137 +JS_ASSERT_STRING_IS_FLAT(JSString *str)
  1.4138 +{
  1.4139 +    JS_ASSERT(JS_GetFlatStringChars((JSFlatString *)str));
  1.4140 +    return (JSFlatString *)str;
  1.4141 +}
  1.4142 +
  1.4143 +static MOZ_ALWAYS_INLINE JSString *
  1.4144 +JS_FORGET_STRING_FLATNESS(JSFlatString *fstr)
  1.4145 +{
  1.4146 +    return (JSString *)fstr;
  1.4147 +}
  1.4148 +
  1.4149 +/*
  1.4150 + * Additional APIs that avoid fallibility when given a flat string.
  1.4151 + */
  1.4152 +
  1.4153 +extern JS_PUBLIC_API(bool)
  1.4154 +JS_FlatStringEqualsAscii(JSFlatString *str, const char *asciiBytes);
  1.4155 +
  1.4156 +extern JS_PUBLIC_API(size_t)
  1.4157 +JS_PutEscapedFlatString(char *buffer, size_t size, JSFlatString *str, char quote);
  1.4158 +
  1.4159 +/*
  1.4160 + * Create a dependent string, i.e., a string that owns no character storage,
  1.4161 + * but that refers to a slice of another string's chars.  Dependent strings
  1.4162 + * are mutable by definition, so the thread safety comments above apply.
  1.4163 + */
  1.4164 +extern JS_PUBLIC_API(JSString *)
  1.4165 +JS_NewDependentString(JSContext *cx, JS::HandleString str, size_t start,
  1.4166 +                      size_t length);
  1.4167 +
  1.4168 +/*
  1.4169 + * Concatenate two strings, possibly resulting in a rope.
  1.4170 + * See above for thread safety comments.
  1.4171 + */
  1.4172 +extern JS_PUBLIC_API(JSString *)
  1.4173 +JS_ConcatStrings(JSContext *cx, JS::HandleString left, JS::HandleString right);
  1.4174 +
  1.4175 +/*
  1.4176 + * For JS_DecodeBytes, set *dstlenp to the size of the destination buffer before
  1.4177 + * the call; on return, *dstlenp contains the number of jschars actually stored.
  1.4178 + * To determine the necessary destination buffer size, make a sizing call that
  1.4179 + * passes nullptr for dst.
  1.4180 + *
  1.4181 + * On errors, the functions report the error. In that case, *dstlenp contains
  1.4182 + * the number of characters or bytes transferred so far.  If cx is nullptr, no
  1.4183 + * error is reported on failure, and the functions simply return false.
  1.4184 + *
  1.4185 + * NB: This function does not store an additional zero byte or jschar after the
  1.4186 + * transcoded string.
  1.4187 + */
  1.4188 +JS_PUBLIC_API(bool)
  1.4189 +JS_DecodeBytes(JSContext *cx, const char *src, size_t srclen, jschar *dst,
  1.4190 +               size_t *dstlenp);
  1.4191 +
  1.4192 +/*
  1.4193 + * A variation on JS_EncodeCharacters where a null terminated string is
  1.4194 + * returned that you are expected to call JS_free on when done.
  1.4195 + */
  1.4196 +JS_PUBLIC_API(char *)
  1.4197 +JS_EncodeString(JSContext *cx, JSString *str);
  1.4198 +
  1.4199 +/*
  1.4200 + * Same behavior as JS_EncodeString(), but encode into UTF-8 string
  1.4201 + */
  1.4202 +JS_PUBLIC_API(char *)
  1.4203 +JS_EncodeStringToUTF8(JSContext *cx, JS::HandleString str);
  1.4204 +
  1.4205 +/*
  1.4206 + * Get number of bytes in the string encoding (without accounting for a
  1.4207 + * terminating zero bytes. The function returns (size_t) -1 if the string
  1.4208 + * can not be encoded into bytes and reports an error using cx accordingly.
  1.4209 + */
  1.4210 +JS_PUBLIC_API(size_t)
  1.4211 +JS_GetStringEncodingLength(JSContext *cx, JSString *str);
  1.4212 +
  1.4213 +/*
  1.4214 + * Encode string into a buffer. The function does not stores an additional
  1.4215 + * zero byte. The function returns (size_t) -1 if the string can not be
  1.4216 + * encoded into bytes with no error reported. Otherwise it returns the number
  1.4217 + * of bytes that are necessary to encode the string. If that exceeds the
  1.4218 + * length parameter, the string will be cut and only length bytes will be
  1.4219 + * written into the buffer.
  1.4220 + */
  1.4221 +JS_PUBLIC_API(size_t)
  1.4222 +JS_EncodeStringToBuffer(JSContext *cx, JSString *str, char *buffer, size_t length);
  1.4223 +
  1.4224 +class JSAutoByteString
  1.4225 +{
  1.4226 +  public:
  1.4227 +    JSAutoByteString(JSContext *cx, JSString *str
  1.4228 +                     MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
  1.4229 +      : mBytes(JS_EncodeString(cx, str))
  1.4230 +    {
  1.4231 +        JS_ASSERT(cx);
  1.4232 +        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
  1.4233 +    }
  1.4234 +
  1.4235 +    JSAutoByteString(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM)
  1.4236 +      : mBytes(nullptr)
  1.4237 +    {
  1.4238 +        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
  1.4239 +    }
  1.4240 +
  1.4241 +    ~JSAutoByteString() {
  1.4242 +        js_free(mBytes);
  1.4243 +    }
  1.4244 +
  1.4245 +    /* Take ownership of the given byte array. */
  1.4246 +    void initBytes(char *bytes) {
  1.4247 +        JS_ASSERT(!mBytes);
  1.4248 +        mBytes = bytes;
  1.4249 +    }
  1.4250 +
  1.4251 +    char *encodeLatin1(JSContext *cx, JSString *str) {
  1.4252 +        JS_ASSERT(!mBytes);
  1.4253 +        JS_ASSERT(cx);
  1.4254 +        mBytes = JS_EncodeString(cx, str);
  1.4255 +        return mBytes;
  1.4256 +    }
  1.4257 +
  1.4258 +    char *encodeLatin1(js::ExclusiveContext *cx, JSString *str);
  1.4259 +
  1.4260 +    char *encodeUtf8(JSContext *cx, JS::HandleString str) {
  1.4261 +        JS_ASSERT(!mBytes);
  1.4262 +        JS_ASSERT(cx);
  1.4263 +        mBytes = JS_EncodeStringToUTF8(cx, str);
  1.4264 +        return mBytes;
  1.4265 +    }
  1.4266 +
  1.4267 +    void clear() {
  1.4268 +        js_free(mBytes);
  1.4269 +        mBytes = nullptr;
  1.4270 +    }
  1.4271 +
  1.4272 +    char *ptr() const {
  1.4273 +        return mBytes;
  1.4274 +    }
  1.4275 +
  1.4276 +    bool operator!() const {
  1.4277 +        return !mBytes;
  1.4278 +    }
  1.4279 +
  1.4280 +    size_t length() const {
  1.4281 +        if (!mBytes)
  1.4282 +            return 0;
  1.4283 +        return strlen(mBytes);
  1.4284 +    }
  1.4285 +
  1.4286 +  private:
  1.4287 +    char        *mBytes;
  1.4288 +    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
  1.4289 +
  1.4290 +    /* Copy and assignment are not supported. */
  1.4291 +    JSAutoByteString(const JSAutoByteString &another);
  1.4292 +    JSAutoByteString &operator=(const JSAutoByteString &another);
  1.4293 +};
  1.4294 +
  1.4295 +/************************************************************************/
  1.4296 +/*
  1.4297 + * JSON functions
  1.4298 + */
  1.4299 +typedef bool (* JSONWriteCallback)(const jschar *buf, uint32_t len, void *data);
  1.4300 +
  1.4301 +/*
  1.4302 + * JSON.stringify as specified by ES5.
  1.4303 + */
  1.4304 +JS_PUBLIC_API(bool)
  1.4305 +JS_Stringify(JSContext *cx, JS::MutableHandleValue value, JS::HandleObject replacer,
  1.4306 +             JS::HandleValue space, JSONWriteCallback callback, void *data);
  1.4307 +
  1.4308 +/*
  1.4309 + * JSON.parse as specified by ES5.
  1.4310 + */
  1.4311 +JS_PUBLIC_API(bool)
  1.4312 +JS_ParseJSON(JSContext *cx, const jschar *chars, uint32_t len, JS::MutableHandleValue vp);
  1.4313 +
  1.4314 +JS_PUBLIC_API(bool)
  1.4315 +JS_ParseJSONWithReviver(JSContext *cx, const jschar *chars, uint32_t len, JS::HandleValue reviver,
  1.4316 +                        JS::MutableHandleValue vp);
  1.4317 +
  1.4318 +/************************************************************************/
  1.4319 +
  1.4320 +/*
  1.4321 + * The default locale for the ECMAScript Internationalization API
  1.4322 + * (Intl.Collator, Intl.NumberFormat, Intl.DateTimeFormat).
  1.4323 + * Note that the Internationalization API encourages clients to
  1.4324 + * specify their own locales.
  1.4325 + * The locale string remains owned by the caller.
  1.4326 + */
  1.4327 +extern JS_PUBLIC_API(bool)
  1.4328 +JS_SetDefaultLocale(JSRuntime *rt, const char *locale);
  1.4329 +
  1.4330 +/*
  1.4331 + * Returns the default locale for the ECMAScript Internationalization API
  1.4332 + * (Intl.Collator, Intl.NumberFormat, Intl.DateTimeFormat).
  1.4333 + * Note that the Internationalization API encourages clients to
  1.4334 + * specify their own locales.
  1.4335 + */
  1.4336 +extern JS_PUBLIC_API(const char*)
  1.4337 +JS_GetDefaultLocale(JSRuntime *rt);
  1.4338 +
  1.4339 +/*
  1.4340 + * Reset the default locale to OS defaults.
  1.4341 + */
  1.4342 +extern JS_PUBLIC_API(void)
  1.4343 +JS_ResetDefaultLocale(JSRuntime *rt);
  1.4344 +
  1.4345 +/*
  1.4346 + * Locale specific string conversion and error message callbacks.
  1.4347 + */
  1.4348 +struct JSLocaleCallbacks {
  1.4349 +    JSLocaleToUpperCase     localeToUpperCase;
  1.4350 +    JSLocaleToLowerCase     localeToLowerCase;
  1.4351 +    JSLocaleCompare         localeCompare; // not used #if EXPOSE_INTL_API
  1.4352 +    JSLocaleToUnicode       localeToUnicode;
  1.4353 +    JSErrorCallback         localeGetErrorMessage;
  1.4354 +};
  1.4355 +
  1.4356 +/*
  1.4357 + * Establish locale callbacks. The pointer must persist as long as the
  1.4358 + * JSRuntime.  Passing nullptr restores the default behaviour.
  1.4359 + */
  1.4360 +extern JS_PUBLIC_API(void)
  1.4361 +JS_SetLocaleCallbacks(JSRuntime *rt, JSLocaleCallbacks *callbacks);
  1.4362 +
  1.4363 +/*
  1.4364 + * Return the address of the current locale callbacks struct, which may
  1.4365 + * be nullptr.
  1.4366 + */
  1.4367 +extern JS_PUBLIC_API(JSLocaleCallbacks *)
  1.4368 +JS_GetLocaleCallbacks(JSRuntime *rt);
  1.4369 +
  1.4370 +/************************************************************************/
  1.4371 +
  1.4372 +/*
  1.4373 + * Error reporting.
  1.4374 + */
  1.4375 +
  1.4376 +/*
  1.4377 + * Report an exception represented by the sprintf-like conversion of format
  1.4378 + * and its arguments.  This exception message string is passed to a pre-set
  1.4379 + * JSErrorReporter function (set by JS_SetErrorReporter).
  1.4380 + */
  1.4381 +extern JS_PUBLIC_API(void)
  1.4382 +JS_ReportError(JSContext *cx, const char *format, ...);
  1.4383 +
  1.4384 +/*
  1.4385 + * Use an errorNumber to retrieve the format string, args are char *
  1.4386 + */
  1.4387 +extern JS_PUBLIC_API(void)
  1.4388 +JS_ReportErrorNumber(JSContext *cx, JSErrorCallback errorCallback,
  1.4389 +                     void *userRef, const unsigned errorNumber, ...);
  1.4390 +
  1.4391 +#ifdef va_start
  1.4392 +extern JS_PUBLIC_API(void)
  1.4393 +JS_ReportErrorNumberVA(JSContext *cx, JSErrorCallback errorCallback,
  1.4394 +                       void *userRef, const unsigned errorNumber, va_list ap);
  1.4395 +#endif
  1.4396 +
  1.4397 +/*
  1.4398 + * Use an errorNumber to retrieve the format string, args are jschar *
  1.4399 + */
  1.4400 +extern JS_PUBLIC_API(void)
  1.4401 +JS_ReportErrorNumberUC(JSContext *cx, JSErrorCallback errorCallback,
  1.4402 +                     void *userRef, const unsigned errorNumber, ...);
  1.4403 +
  1.4404 +extern JS_PUBLIC_API(void)
  1.4405 +JS_ReportErrorNumberUCArray(JSContext *cx, JSErrorCallback errorCallback,
  1.4406 +                            void *userRef, const unsigned errorNumber,
  1.4407 +                            const jschar **args);
  1.4408 +
  1.4409 +/*
  1.4410 + * As above, but report a warning instead (JSREPORT_IS_WARNING(report.flags)).
  1.4411 + * Return true if there was no error trying to issue the warning, and if the
  1.4412 + * warning was not converted into an error due to the JSOPTION_WERROR option
  1.4413 + * being set, false otherwise.
  1.4414 + */
  1.4415 +extern JS_PUBLIC_API(bool)
  1.4416 +JS_ReportWarning(JSContext *cx, const char *format, ...);
  1.4417 +
  1.4418 +extern JS_PUBLIC_API(bool)
  1.4419 +JS_ReportErrorFlagsAndNumber(JSContext *cx, unsigned flags,
  1.4420 +                             JSErrorCallback errorCallback, void *userRef,
  1.4421 +                             const unsigned errorNumber, ...);
  1.4422 +
  1.4423 +extern JS_PUBLIC_API(bool)
  1.4424 +JS_ReportErrorFlagsAndNumberUC(JSContext *cx, unsigned flags,
  1.4425 +                               JSErrorCallback errorCallback, void *userRef,
  1.4426 +                               const unsigned errorNumber, ...);
  1.4427 +
  1.4428 +/*
  1.4429 + * Complain when out of memory.
  1.4430 + */
  1.4431 +extern JS_PUBLIC_API(void)
  1.4432 +JS_ReportOutOfMemory(JSContext *cx);
  1.4433 +
  1.4434 +/*
  1.4435 + * Complain when an allocation size overflows the maximum supported limit.
  1.4436 + */
  1.4437 +extern JS_PUBLIC_API(void)
  1.4438 +JS_ReportAllocationOverflow(JSContext *cx);
  1.4439 +
  1.4440 +struct JSErrorReport {
  1.4441 +    const char      *filename;      /* source file name, URL, etc., or null */
  1.4442 +    JSPrincipals    *originPrincipals; /* see 'originPrincipals' comment above */
  1.4443 +    unsigned        lineno;         /* source line number */
  1.4444 +    const char      *linebuf;       /* offending source line without final \n */
  1.4445 +    const char      *tokenptr;      /* pointer to error token in linebuf */
  1.4446 +    const jschar    *uclinebuf;     /* unicode (original) line buffer */
  1.4447 +    const jschar    *uctokenptr;    /* unicode (original) token pointer */
  1.4448 +    unsigned        flags;          /* error/warning, etc. */
  1.4449 +    unsigned        errorNumber;    /* the error number, e.g. see js.msg */
  1.4450 +    const jschar    *ucmessage;     /* the (default) error message */
  1.4451 +    const jschar    **messageArgs;  /* arguments for the error message */
  1.4452 +    int16_t         exnType;        /* One of the JSExnType constants */
  1.4453 +    unsigned        column;         /* zero-based column index in line */
  1.4454 +};
  1.4455 +
  1.4456 +/*
  1.4457 + * JSErrorReport flag values.  These may be freely composed.
  1.4458 + */
  1.4459 +#define JSREPORT_ERROR      0x0     /* pseudo-flag for default case */
  1.4460 +#define JSREPORT_WARNING    0x1     /* reported via JS_ReportWarning */
  1.4461 +#define JSREPORT_EXCEPTION  0x2     /* exception was thrown */
  1.4462 +#define JSREPORT_STRICT     0x4     /* error or warning due to strict option */
  1.4463 +
  1.4464 +/*
  1.4465 + * This condition is an error in strict mode code, a warning if
  1.4466 + * JS_HAS_STRICT_OPTION(cx), and otherwise should not be reported at
  1.4467 + * all.  We check the strictness of the context's top frame's script;
  1.4468 + * where that isn't appropriate, the caller should do the right checks
  1.4469 + * itself instead of using this flag.
  1.4470 + */
  1.4471 +#define JSREPORT_STRICT_MODE_ERROR 0x8
  1.4472 +
  1.4473 +/*
  1.4474 + * If JSREPORT_EXCEPTION is set, then a JavaScript-catchable exception
  1.4475 + * has been thrown for this runtime error, and the host should ignore it.
  1.4476 + * Exception-aware hosts should also check for JS_IsExceptionPending if
  1.4477 + * JS_ExecuteScript returns failure, and signal or propagate the exception, as
  1.4478 + * appropriate.
  1.4479 + */
  1.4480 +#define JSREPORT_IS_WARNING(flags)      (((flags) & JSREPORT_WARNING) != 0)
  1.4481 +#define JSREPORT_IS_EXCEPTION(flags)    (((flags) & JSREPORT_EXCEPTION) != 0)
  1.4482 +#define JSREPORT_IS_STRICT(flags)       (((flags) & JSREPORT_STRICT) != 0)
  1.4483 +#define JSREPORT_IS_STRICT_MODE_ERROR(flags) (((flags) &                      \
  1.4484 +                                              JSREPORT_STRICT_MODE_ERROR) != 0)
  1.4485 +extern JS_PUBLIC_API(JSErrorReporter)
  1.4486 +JS_GetErrorReporter(JSContext *cx);
  1.4487 +
  1.4488 +extern JS_PUBLIC_API(JSErrorReporter)
  1.4489 +JS_SetErrorReporter(JSContext *cx, JSErrorReporter er);
  1.4490 +
  1.4491 +namespace JS {
  1.4492 +
  1.4493 +extern JS_PUBLIC_API(bool)
  1.4494 +CreateTypeError(JSContext *cx, HandleString stack, HandleString fileName,
  1.4495 +                uint32_t lineNumber, uint32_t columnNumber, JSErrorReport *report,
  1.4496 +                HandleString message, MutableHandleValue rval);
  1.4497 +
  1.4498 +/************************************************************************/
  1.4499 +
  1.4500 +/*
  1.4501 + * Weak Maps.
  1.4502 + */
  1.4503 +
  1.4504 +extern JS_PUBLIC_API(JSObject *)
  1.4505 +NewWeakMapObject(JSContext *cx);
  1.4506 +
  1.4507 +extern JS_PUBLIC_API(bool)
  1.4508 +IsWeakMapObject(JSObject *obj);
  1.4509 +
  1.4510 +extern JS_PUBLIC_API(bool)
  1.4511 +GetWeakMapEntry(JSContext *cx, JS::HandleObject mapObj, JS::HandleObject key,
  1.4512 +                JS::MutableHandleValue val);
  1.4513 +
  1.4514 +extern JS_PUBLIC_API(bool)
  1.4515 +SetWeakMapEntry(JSContext *cx, JS::HandleObject mapObj, JS::HandleObject key,
  1.4516 +                JS::HandleValue val);
  1.4517 +
  1.4518 +} /* namespace JS */
  1.4519 +
  1.4520 +/*
  1.4521 + * Dates.
  1.4522 + */
  1.4523 +
  1.4524 +extern JS_PUBLIC_API(JSObject *)
  1.4525 +JS_NewDateObject(JSContext *cx, int year, int mon, int mday, int hour, int min, int sec);
  1.4526 +
  1.4527 +extern JS_PUBLIC_API(JSObject *)
  1.4528 +JS_NewDateObjectMsec(JSContext *cx, double msec);
  1.4529 +
  1.4530 +/*
  1.4531 + * Infallible predicate to test whether obj is a date object.
  1.4532 + */
  1.4533 +extern JS_PUBLIC_API(bool)
  1.4534 +JS_ObjectIsDate(JSContext *cx, JS::HandleObject obj);
  1.4535 +
  1.4536 +/*
  1.4537 + * Clears the cache of calculated local time from each Date object.
  1.4538 + * Call to propagate a system timezone change.
  1.4539 + */
  1.4540 +extern JS_PUBLIC_API(void)
  1.4541 +JS_ClearDateCaches(JSContext *cx);
  1.4542 +
  1.4543 +/************************************************************************/
  1.4544 +
  1.4545 +/*
  1.4546 + * Regular Expressions.
  1.4547 + */
  1.4548 +#define JSREG_FOLD      0x01    /* fold uppercase to lowercase */
  1.4549 +#define JSREG_GLOB      0x02    /* global exec, creates array of matches */
  1.4550 +#define JSREG_MULTILINE 0x04    /* treat ^ and $ as begin and end of line */
  1.4551 +#define JSREG_STICKY    0x08    /* only match starting at lastIndex */
  1.4552 +
  1.4553 +extern JS_PUBLIC_API(JSObject *)
  1.4554 +JS_NewRegExpObject(JSContext *cx, JS::HandleObject obj, char *bytes, size_t length,
  1.4555 +                   unsigned flags);
  1.4556 +
  1.4557 +extern JS_PUBLIC_API(JSObject *)
  1.4558 +JS_NewUCRegExpObject(JSContext *cx, JS::HandleObject obj, jschar *chars, size_t length,
  1.4559 +                     unsigned flags);
  1.4560 +
  1.4561 +extern JS_PUBLIC_API(void)
  1.4562 +JS_SetRegExpInput(JSContext *cx, JS::HandleObject obj, JS::HandleString input,
  1.4563 +                  bool multiline);
  1.4564 +
  1.4565 +extern JS_PUBLIC_API(void)
  1.4566 +JS_ClearRegExpStatics(JSContext *cx, JS::HandleObject obj);
  1.4567 +
  1.4568 +extern JS_PUBLIC_API(bool)
  1.4569 +JS_ExecuteRegExp(JSContext *cx, JS::HandleObject obj, JS::HandleObject reobj,
  1.4570 +                 jschar *chars, size_t length, size_t *indexp, bool test,
  1.4571 +                 JS::MutableHandleValue rval);
  1.4572 +
  1.4573 +/* RegExp interface for clients without a global object. */
  1.4574 +
  1.4575 +extern JS_PUBLIC_API(JSObject *)
  1.4576 +JS_NewRegExpObjectNoStatics(JSContext *cx, char *bytes, size_t length, unsigned flags);
  1.4577 +
  1.4578 +extern JS_PUBLIC_API(JSObject *)
  1.4579 +JS_NewUCRegExpObjectNoStatics(JSContext *cx, jschar *chars, size_t length, unsigned flags);
  1.4580 +
  1.4581 +extern JS_PUBLIC_API(bool)
  1.4582 +JS_ExecuteRegExpNoStatics(JSContext *cx, JS::HandleObject reobj, jschar *chars, size_t length,
  1.4583 +                          size_t *indexp, bool test, JS::MutableHandleValue rval);
  1.4584 +
  1.4585 +extern JS_PUBLIC_API(bool)
  1.4586 +JS_ObjectIsRegExp(JSContext *cx, JS::HandleObject obj);
  1.4587 +
  1.4588 +extern JS_PUBLIC_API(unsigned)
  1.4589 +JS_GetRegExpFlags(JSContext *cx, JS::HandleObject obj);
  1.4590 +
  1.4591 +extern JS_PUBLIC_API(JSString *)
  1.4592 +JS_GetRegExpSource(JSContext *cx, JS::HandleObject obj);
  1.4593 +
  1.4594 +/************************************************************************/
  1.4595 +
  1.4596 +extern JS_PUBLIC_API(bool)
  1.4597 +JS_IsExceptionPending(JSContext *cx);
  1.4598 +
  1.4599 +extern JS_PUBLIC_API(bool)
  1.4600 +JS_GetPendingException(JSContext *cx, JS::MutableHandleValue vp);
  1.4601 +
  1.4602 +extern JS_PUBLIC_API(void)
  1.4603 +JS_SetPendingException(JSContext *cx, JS::HandleValue v);
  1.4604 +
  1.4605 +extern JS_PUBLIC_API(void)
  1.4606 +JS_ClearPendingException(JSContext *cx);
  1.4607 +
  1.4608 +extern JS_PUBLIC_API(bool)
  1.4609 +JS_ReportPendingException(JSContext *cx);
  1.4610 +
  1.4611 +namespace JS {
  1.4612 +
  1.4613 +/*
  1.4614 + * Save and later restore the current exception state of a given JSContext.
  1.4615 + * This is useful for implementing behavior in C++ that's like try/catch
  1.4616 + * or try/finally in JS.
  1.4617 + *
  1.4618 + * Typical usage:
  1.4619 + *
  1.4620 + *     bool ok = JS_EvaluateScript(cx, ...);
  1.4621 + *     AutoSaveExceptionState savedExc(cx);
  1.4622 + *     ... cleanup that might re-enter JS ...
  1.4623 + *     return ok;
  1.4624 + */
  1.4625 +class JS_PUBLIC_API(AutoSaveExceptionState)
  1.4626 +{
  1.4627 +  private:
  1.4628 +    JSContext *context;
  1.4629 +    bool wasThrowing;
  1.4630 +    RootedValue exceptionValue;
  1.4631 +
  1.4632 +  public:
  1.4633 +    /*
  1.4634 +     * Take a snapshot of cx's current exception state. Then clear any current
  1.4635 +     * pending exception in cx.
  1.4636 +     */
  1.4637 +    explicit AutoSaveExceptionState(JSContext *cx);
  1.4638 +
  1.4639 +    /*
  1.4640 +     * If neither drop() nor restore() was called, restore the exception
  1.4641 +     * state only if no exception is currently pending on cx.
  1.4642 +     */
  1.4643 +    ~AutoSaveExceptionState();
  1.4644 +
  1.4645 +    /*
  1.4646 +     * Discard any stored exception state.
  1.4647 +     * If this is called, the destructor is a no-op.
  1.4648 +     */
  1.4649 +    void drop() {
  1.4650 +        wasThrowing = false;
  1.4651 +        exceptionValue.setUndefined();
  1.4652 +    }
  1.4653 +
  1.4654 +    /*
  1.4655 +     * Replace cx's exception state with the stored exception state. Then
  1.4656 +     * discard the stored exception state. If this is called, the
  1.4657 +     * destructor is a no-op.
  1.4658 +     */
  1.4659 +    void restore();
  1.4660 +};
  1.4661 +
  1.4662 +} /* namespace JS */
  1.4663 +
  1.4664 +/* Deprecated API. Use AutoSaveExceptionState instead. */
  1.4665 +extern JS_PUBLIC_API(JSExceptionState *)
  1.4666 +JS_SaveExceptionState(JSContext *cx);
  1.4667 +
  1.4668 +extern JS_PUBLIC_API(void)
  1.4669 +JS_RestoreExceptionState(JSContext *cx, JSExceptionState *state);
  1.4670 +
  1.4671 +extern JS_PUBLIC_API(void)
  1.4672 +JS_DropExceptionState(JSContext *cx, JSExceptionState *state);
  1.4673 +
  1.4674 +/*
  1.4675 + * If the given object is an exception object, the exception will have (or be
  1.4676 + * able to lazily create) an error report struct, and this function will return
  1.4677 + * the address of that struct.  Otherwise, it returns nullptr. The lifetime
  1.4678 + * of the error report struct that might be returned is the same as the
  1.4679 + * lifetime of the exception object.
  1.4680 + */
  1.4681 +extern JS_PUBLIC_API(JSErrorReport *)
  1.4682 +JS_ErrorFromException(JSContext *cx, JS::HandleObject obj);
  1.4683 +
  1.4684 +/*
  1.4685 + * Throws a StopIteration exception on cx.
  1.4686 + */
  1.4687 +extern JS_PUBLIC_API(bool)
  1.4688 +JS_ThrowStopIteration(JSContext *cx);
  1.4689 +
  1.4690 +extern JS_PUBLIC_API(bool)
  1.4691 +JS_IsStopIteration(jsval v);
  1.4692 +
  1.4693 +extern JS_PUBLIC_API(intptr_t)
  1.4694 +JS_GetCurrentThread();
  1.4695 +
  1.4696 +/*
  1.4697 + * A JS runtime always has an "owner thread". The owner thread is set when the
  1.4698 + * runtime is created (to the current thread) and practically all entry points
  1.4699 + * into the JS engine check that a runtime (or anything contained in the
  1.4700 + * runtime: context, compartment, object, etc) is only touched by its owner
  1.4701 + * thread. Embeddings may check this invariant outside the JS engine by calling
  1.4702 + * JS_AbortIfWrongThread (which will abort if not on the owner thread, even for
  1.4703 + * non-debug builds).
  1.4704 + */
  1.4705 +
  1.4706 +extern JS_PUBLIC_API(void)
  1.4707 +JS_AbortIfWrongThread(JSRuntime *rt);
  1.4708 +
  1.4709 +/************************************************************************/
  1.4710 +
  1.4711 +/*
  1.4712 + * A constructor can request that the JS engine create a default new 'this'
  1.4713 + * object of the given class, using the callee to determine parentage and
  1.4714 + * [[Prototype]].
  1.4715 + */
  1.4716 +extern JS_PUBLIC_API(JSObject *)
  1.4717 +JS_NewObjectForConstructor(JSContext *cx, const JSClass *clasp, const JS::CallArgs& args);
  1.4718 +
  1.4719 +/************************************************************************/
  1.4720 +
  1.4721 +#ifdef JS_GC_ZEAL
  1.4722 +#define JS_DEFAULT_ZEAL_FREQ 100
  1.4723 +
  1.4724 +extern JS_PUBLIC_API(void)
  1.4725 +JS_SetGCZeal(JSContext *cx, uint8_t zeal, uint32_t frequency);
  1.4726 +
  1.4727 +extern JS_PUBLIC_API(void)
  1.4728 +JS_ScheduleGC(JSContext *cx, uint32_t count);
  1.4729 +#endif
  1.4730 +
  1.4731 +extern JS_PUBLIC_API(void)
  1.4732 +JS_SetParallelParsingEnabled(JSRuntime *rt, bool enabled);
  1.4733 +
  1.4734 +extern JS_PUBLIC_API(void)
  1.4735 +JS_SetParallelIonCompilationEnabled(JSRuntime *rt, bool enabled);
  1.4736 +
  1.4737 +#define JIT_COMPILER_OPTIONS(Register)                                  \
  1.4738 +    Register(BASELINE_USECOUNT_TRIGGER, "baseline.usecount.trigger")    \
  1.4739 +    Register(ION_USECOUNT_TRIGGER, "ion.usecount.trigger")              \
  1.4740 +    Register(ION_ENABLE, "ion.enable")                                  \
  1.4741 +    Register(BASELINE_ENABLE, "baseline.enable")                        \
  1.4742 +    Register(PARALLEL_COMPILATION_ENABLE, "parallel-compilation.enable")
  1.4743 +
  1.4744 +typedef enum JSJitCompilerOption {
  1.4745 +#define JIT_COMPILER_DECLARE(key, str) \
  1.4746 +    JSJITCOMPILER_ ## key,
  1.4747 +
  1.4748 +    JIT_COMPILER_OPTIONS(JIT_COMPILER_DECLARE)
  1.4749 +#undef JIT_COMPILER_DECLARE
  1.4750 +
  1.4751 +    JSJITCOMPILER_NOT_AN_OPTION
  1.4752 +} JSJitCompilerOption;
  1.4753 +
  1.4754 +extern JS_PUBLIC_API(void)
  1.4755 +JS_SetGlobalJitCompilerOption(JSRuntime *rt, JSJitCompilerOption opt, uint32_t value);
  1.4756 +extern JS_PUBLIC_API(int)
  1.4757 +JS_GetGlobalJitCompilerOption(JSRuntime *rt, JSJitCompilerOption opt);
  1.4758 +
  1.4759 +/*
  1.4760 + * Convert a uint32_t index into a jsid.
  1.4761 + */
  1.4762 +extern JS_PUBLIC_API(bool)
  1.4763 +JS_IndexToId(JSContext *cx, uint32_t index, JS::MutableHandleId);
  1.4764 +
  1.4765 +/*
  1.4766 + * Convert chars into a jsid.
  1.4767 + *
  1.4768 + * |chars| may not be an index.
  1.4769 + */
  1.4770 +extern JS_PUBLIC_API(bool)
  1.4771 +JS_CharsToId(JSContext* cx, JS::TwoByteChars chars, JS::MutableHandleId);
  1.4772 +
  1.4773 +/*
  1.4774 + *  Test if the given string is a valid ECMAScript identifier
  1.4775 + */
  1.4776 +extern JS_PUBLIC_API(bool)
  1.4777 +JS_IsIdentifier(JSContext *cx, JS::HandleString str, bool *isIdentifier);
  1.4778 +
  1.4779 +namespace JS {
  1.4780 +
  1.4781 +/*
  1.4782 + * AutoFilename encapsulates a pointer to a C-string and keeps the C-string
  1.4783 + * alive for as long as the associated AutoFilename object is alive.
  1.4784 + */
  1.4785 +class MOZ_STACK_CLASS JS_PUBLIC_API(AutoFilename)
  1.4786 +{
  1.4787 +    void *scriptSource_;
  1.4788 +
  1.4789 +    AutoFilename(const AutoFilename &) MOZ_DELETE;
  1.4790 +    void operator=(const AutoFilename &) MOZ_DELETE;
  1.4791 +
  1.4792 +  public:
  1.4793 +    AutoFilename() : scriptSource_(nullptr) {}
  1.4794 +    ~AutoFilename() { reset(nullptr); }
  1.4795 +
  1.4796 +    const char *get() const;
  1.4797 +
  1.4798 +    void reset(void *newScriptSource);
  1.4799 +};
  1.4800 +
  1.4801 +/*
  1.4802 + * Return the current filename and line number of the most currently running
  1.4803 + * frame. Returns true if a scripted frame was found, false otherwise.
  1.4804 + *
  1.4805 + * If a the embedding has hidden the scripted caller for the topmost activation
  1.4806 + * record, this will also return false.
  1.4807 + */
  1.4808 +extern JS_PUBLIC_API(bool)
  1.4809 +DescribeScriptedCaller(JSContext *cx, AutoFilename *filename = nullptr,
  1.4810 +                       unsigned *lineno = nullptr);
  1.4811 +
  1.4812 +extern JS_PUBLIC_API(JSObject *)
  1.4813 +GetScriptedCallerGlobal(JSContext *cx);
  1.4814 +
  1.4815 +/*
  1.4816 + * Informs the JS engine that the scripted caller should be hidden. This can be
  1.4817 + * used by the embedding to maintain an override of the scripted caller in its
  1.4818 + * calculations, by hiding the scripted caller in the JS engine and pushing data
  1.4819 + * onto a separate stack, which it inspects when DescribeScriptedCaller returns
  1.4820 + * null.
  1.4821 + *
  1.4822 + * We maintain a counter on each activation record. Add() increments the counter
  1.4823 + * of the topmost activation, and Remove() decrements it. The count may never
  1.4824 + * drop below zero, and must always be exactly zero when the activation is
  1.4825 + * popped from the stack.
  1.4826 + */
  1.4827 +extern JS_PUBLIC_API(void)
  1.4828 +HideScriptedCaller(JSContext *cx);
  1.4829 +
  1.4830 +extern JS_PUBLIC_API(void)
  1.4831 +UnhideScriptedCaller(JSContext *cx);
  1.4832 +
  1.4833 +class AutoHideScriptedCaller
  1.4834 +{
  1.4835 +  public:
  1.4836 +    AutoHideScriptedCaller(JSContext *cx
  1.4837 +                           MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
  1.4838 +      : mContext(cx)
  1.4839 +    {
  1.4840 +        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
  1.4841 +        HideScriptedCaller(mContext);
  1.4842 +    }
  1.4843 +    ~AutoHideScriptedCaller() {
  1.4844 +        UnhideScriptedCaller(mContext);
  1.4845 +    }
  1.4846 +
  1.4847 +  protected:
  1.4848 +    JSContext *mContext;
  1.4849 +    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
  1.4850 +};
  1.4851 +
  1.4852 +} /* namespace JS */
  1.4853 +
  1.4854 +/*
  1.4855 + * Encode/Decode interpreted scripts and functions to/from memory.
  1.4856 + */
  1.4857 +
  1.4858 +extern JS_PUBLIC_API(void *)
  1.4859 +JS_EncodeScript(JSContext *cx, JS::HandleScript script, uint32_t *lengthp);
  1.4860 +
  1.4861 +extern JS_PUBLIC_API(void *)
  1.4862 +JS_EncodeInterpretedFunction(JSContext *cx, JS::HandleObject funobj, uint32_t *lengthp);
  1.4863 +
  1.4864 +extern JS_PUBLIC_API(JSScript *)
  1.4865 +JS_DecodeScript(JSContext *cx, const void *data, uint32_t length, JSPrincipals *originPrincipals);
  1.4866 +
  1.4867 +extern JS_PUBLIC_API(JSObject *)
  1.4868 +JS_DecodeInterpretedFunction(JSContext *cx, const void *data, uint32_t length,
  1.4869 +                             JSPrincipals *originPrincipals);
  1.4870 +
  1.4871 +namespace JS {
  1.4872 +
  1.4873 +/*
  1.4874 + * This callback represents a request by the JS engine to open for reading the
  1.4875 + * existing cache entry for the given global and char range that may contain a
  1.4876 + * module. If a cache entry exists, the callback shall return 'true' and return
  1.4877 + * the size, base address and an opaque file handle as outparams. If the
  1.4878 + * callback returns 'true', the JS engine guarantees a call to
  1.4879 + * CloseAsmJSCacheEntryForReadOp, passing the same base address, size and
  1.4880 + * handle.
  1.4881 + */
  1.4882 +typedef bool
  1.4883 +(* OpenAsmJSCacheEntryForReadOp)(HandleObject global, const jschar *begin, const jschar *limit,
  1.4884 +                                 size_t *size, const uint8_t **memory, intptr_t *handle);
  1.4885 +typedef void
  1.4886 +(* CloseAsmJSCacheEntryForReadOp)(HandleObject global, size_t size, const uint8_t *memory,
  1.4887 +                                  intptr_t handle);
  1.4888 +
  1.4889 +/*
  1.4890 + * This callback represents a request by the JS engine to open for writing a
  1.4891 + * cache entry of the given size for the given global and char range containing
  1.4892 + * the just-compiled module. If cache entry space is available, the callback
  1.4893 + * shall return 'true' and return the base address and an opaque file handle as
  1.4894 + * outparams. If the callback returns 'true', the JS engine guarantees a call
  1.4895 + * to CloseAsmJSCacheEntryForWriteOp passing the same base address, size and
  1.4896 + * handle.
  1.4897 + *
  1.4898 + * If 'installed' is true, then the cache entry is associated with a permanently
  1.4899 + * installed JS file (e.g., in a packaged webapp). This information allows the
  1.4900 + * embedding to store the cache entry in a installed location associated with
  1.4901 + * the principal of 'global' where it will not be evicted until the associated
  1.4902 + * installed JS file is removed.
  1.4903 + */
  1.4904 +typedef bool
  1.4905 +(* OpenAsmJSCacheEntryForWriteOp)(HandleObject global, bool installed,
  1.4906 +                                  const jschar *begin, const jschar *end,
  1.4907 +                                  size_t size, uint8_t **memory, intptr_t *handle);
  1.4908 +typedef void
  1.4909 +(* CloseAsmJSCacheEntryForWriteOp)(HandleObject global, size_t size, uint8_t *memory,
  1.4910 +                                   intptr_t handle);
  1.4911 +
  1.4912 +typedef js::Vector<char, 0, js::SystemAllocPolicy> BuildIdCharVector;
  1.4913 +
  1.4914 +// Return the buildId (represented as a sequence of characters) associated with
  1.4915 +// the currently-executing build. If the JS engine is embedded such that a
  1.4916 +// single cache entry can be observed by different compiled versions of the JS
  1.4917 +// engine, it is critical that the buildId shall change for each new build of
  1.4918 +// the JS engine.
  1.4919 +
  1.4920 +typedef bool
  1.4921 +(* BuildIdOp)(BuildIdCharVector *buildId);
  1.4922 +
  1.4923 +struct AsmJSCacheOps
  1.4924 +{
  1.4925 +    OpenAsmJSCacheEntryForReadOp openEntryForRead;
  1.4926 +    CloseAsmJSCacheEntryForReadOp closeEntryForRead;
  1.4927 +    OpenAsmJSCacheEntryForWriteOp openEntryForWrite;
  1.4928 +    CloseAsmJSCacheEntryForWriteOp closeEntryForWrite;
  1.4929 +    BuildIdOp buildId;
  1.4930 +};
  1.4931 +
  1.4932 +extern JS_PUBLIC_API(void)
  1.4933 +SetAsmJSCacheOps(JSRuntime *rt, const AsmJSCacheOps *callbacks);
  1.4934 +
  1.4935 +/*
  1.4936 + * Convenience class for imitating a JS level for-of loop. Typical usage:
  1.4937 + *
  1.4938 + *     ForOfIterator it(cx);
  1.4939 + *     if (!it.init(iterable))
  1.4940 + *       return false;
  1.4941 + *     RootedValue val(cx);
  1.4942 + *     while (true) {
  1.4943 + *       bool done;
  1.4944 + *       if (!it.next(&val, &done))
  1.4945 + *         return false;
  1.4946 + *       if (done)
  1.4947 + *         break;
  1.4948 + *       if (!DoStuff(cx, val))
  1.4949 + *         return false;
  1.4950 + *     }
  1.4951 + */
  1.4952 +class MOZ_STACK_CLASS JS_PUBLIC_API(ForOfIterator) {
  1.4953 +  protected:
  1.4954 +    JSContext *cx_;
  1.4955 +    /*
  1.4956 +     * Use the ForOfPIC on the global object (see vm/GlobalObject.h) to try
  1.4957 +     * to optimize iteration across arrays.
  1.4958 +     *
  1.4959 +     *  Case 1: Regular Iteration
  1.4960 +     *      iterator - pointer to the iterator object.
  1.4961 +     *      index - fixed to NOT_ARRAY (== UINT32_MAX)
  1.4962 +     *
  1.4963 +     *  Case 2: Optimized Array Iteration
  1.4964 +     *      iterator - pointer to the array object.
  1.4965 +     *      index - current position in array.
  1.4966 +     *
  1.4967 +     * The cases are distinguished by whether or not |index| is equal to NOT_ARRAY.
  1.4968 +     */
  1.4969 +    JS::RootedObject iterator;
  1.4970 +    uint32_t index;
  1.4971 +
  1.4972 +    static const uint32_t NOT_ARRAY = UINT32_MAX;
  1.4973 +
  1.4974 +    ForOfIterator(const ForOfIterator &) MOZ_DELETE;
  1.4975 +    ForOfIterator &operator=(const ForOfIterator &) MOZ_DELETE;
  1.4976 +
  1.4977 +  public:
  1.4978 +    ForOfIterator(JSContext *cx) : cx_(cx), iterator(cx_), index(NOT_ARRAY) { }
  1.4979 +
  1.4980 +    enum NonIterableBehavior {
  1.4981 +        ThrowOnNonIterable,
  1.4982 +        AllowNonIterable
  1.4983 +    };
  1.4984 +
  1.4985 +    /*
  1.4986 +     * Initialize the iterator.  If AllowNonIterable is passed then if iterable
  1.4987 +     * does not have a callable @@iterator init() will just return true instead
  1.4988 +     * of throwing.  Callers should then check valueIsIterable() before
  1.4989 +     * continuing with the iteration.
  1.4990 +     */
  1.4991 +    bool init(JS::HandleValue iterable,
  1.4992 +              NonIterableBehavior nonIterableBehavior = ThrowOnNonIterable);
  1.4993 +
  1.4994 +    /*
  1.4995 +     * Get the next value from the iterator.  If false *done is true
  1.4996 +     * after this call, do not examine val.
  1.4997 +     */
  1.4998 +    bool next(JS::MutableHandleValue val, bool *done);
  1.4999 +
  1.5000 +    /*
  1.5001 +     * If initialized with throwOnNonCallable = false, check whether
  1.5002 +     * the value is iterable.
  1.5003 +     */
  1.5004 +    bool valueIsIterable() const {
  1.5005 +        return iterator;
  1.5006 +    }
  1.5007 +
  1.5008 +  private:
  1.5009 +    inline bool nextFromOptimizedArray(MutableHandleValue val, bool *done);
  1.5010 +    bool materializeArrayIterator();
  1.5011 +};
  1.5012 +
  1.5013 +
  1.5014 +/*
  1.5015 + * If a large allocation fails, the JS engine may call the large-allocation-
  1.5016 + * failure callback, if set, to allow the embedding to flush caches, possibly
  1.5017 + * perform shrinking GCs, etc. to make some room so that the allocation will
  1.5018 + * succeed if retried. After the callback returns, the JS engine will try to
  1.5019 + * allocate again and may be succesful.
  1.5020 + */
  1.5021 +
  1.5022 +typedef void
  1.5023 +(* LargeAllocationFailureCallback)();
  1.5024 +
  1.5025 +extern JS_PUBLIC_API(void)
  1.5026 +SetLargeAllocationFailureCallback(JSRuntime *rt, LargeAllocationFailureCallback afc);
  1.5027 +
  1.5028 +/*
  1.5029 + * Unlike the error reporter, which is only called if the exception for an OOM
  1.5030 + * bubbles up and is not caught, the OutOfMemoryCallback is called immediately
  1.5031 + * at the OOM site to allow the embedding to capture the current state of heap
  1.5032 + * allocation before anything is freed. If the large-allocation-failure callback
  1.5033 + * is called at all (not all allocation sites call the large-allocation-failure
  1.5034 + * callback on failure), it is called before the out-of-memory callback; the
  1.5035 + * out-of-memory callback is only called if the allocation still fails after the
  1.5036 + * large-allocation-failure callback has returned.
  1.5037 + */
  1.5038 +
  1.5039 +typedef void
  1.5040 +(* OutOfMemoryCallback)(JSContext *cx);
  1.5041 +
  1.5042 +extern JS_PUBLIC_API(void)
  1.5043 +SetOutOfMemoryCallback(JSRuntime *rt, OutOfMemoryCallback cb);
  1.5044 +
  1.5045 +} /* namespace JS */
  1.5046 +
  1.5047 +#endif /* jsapi_h */

mercurial