js/src/jsreflect.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/jsreflect.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,3350 @@
     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 +/* JS reflection package. */
    1.11 +
    1.12 +#include "jsreflect.h"
    1.13 +
    1.14 +#include "mozilla/ArrayUtils.h"
    1.15 +#include "mozilla/DebugOnly.h"
    1.16 +
    1.17 +#include <stdlib.h>
    1.18 +
    1.19 +#include "jsarray.h"
    1.20 +#include "jsatom.h"
    1.21 +#include "jsobj.h"
    1.22 +#include "jspubtd.h"
    1.23 +
    1.24 +#include "frontend/Parser.h"
    1.25 +#include "frontend/TokenStream.h"
    1.26 +#include "js/CharacterEncoding.h"
    1.27 +#include "vm/RegExpObject.h"
    1.28 +
    1.29 +#include "jsobjinlines.h"
    1.30 +
    1.31 +#include "frontend/ParseNode-inl.h"
    1.32 +
    1.33 +using namespace js;
    1.34 +using namespace js::frontend;
    1.35 +
    1.36 +using JS::AutoValueArray;
    1.37 +using mozilla::ArrayLength;
    1.38 +using mozilla::DebugOnly;
    1.39 +
    1.40 +char const * const js::aopNames[] = {
    1.41 +    "=",    /* AOP_ASSIGN */
    1.42 +    "+=",   /* AOP_PLUS */
    1.43 +    "-=",   /* AOP_MINUS */
    1.44 +    "*=",   /* AOP_STAR */
    1.45 +    "/=",   /* AOP_DIV */
    1.46 +    "%=",   /* AOP_MOD */
    1.47 +    "<<=",  /* AOP_LSH */
    1.48 +    ">>=",  /* AOP_RSH */
    1.49 +    ">>>=", /* AOP_URSH */
    1.50 +    "|=",   /* AOP_BITOR */
    1.51 +    "^=",   /* AOP_BITXOR */
    1.52 +    "&="    /* AOP_BITAND */
    1.53 +};
    1.54 +
    1.55 +char const * const js::binopNames[] = {
    1.56 +    "==",         /* BINOP_EQ */
    1.57 +    "!=",         /* BINOP_NE */
    1.58 +    "===",        /* BINOP_STRICTEQ */
    1.59 +    "!==",        /* BINOP_STRICTNE */
    1.60 +    "<",          /* BINOP_LT */
    1.61 +    "<=",         /* BINOP_LE */
    1.62 +    ">",          /* BINOP_GT */
    1.63 +    ">=",         /* BINOP_GE */
    1.64 +    "<<",         /* BINOP_LSH */
    1.65 +    ">>",         /* BINOP_RSH */
    1.66 +    ">>>",        /* BINOP_URSH */
    1.67 +    "+",          /* BINOP_PLUS */
    1.68 +    "-",          /* BINOP_MINUS */
    1.69 +    "*",          /* BINOP_STAR */
    1.70 +    "/",          /* BINOP_DIV */
    1.71 +    "%",          /* BINOP_MOD */
    1.72 +    "|",          /* BINOP_BITOR */
    1.73 +    "^",          /* BINOP_BITXOR */
    1.74 +    "&",          /* BINOP_BITAND */
    1.75 +    "in",         /* BINOP_IN */
    1.76 +    "instanceof", /* BINOP_INSTANCEOF */
    1.77 +};
    1.78 +
    1.79 +char const * const js::unopNames[] = {
    1.80 +    "delete",  /* UNOP_DELETE */
    1.81 +    "-",       /* UNOP_NEG */
    1.82 +    "+",       /* UNOP_POS */
    1.83 +    "!",       /* UNOP_NOT */
    1.84 +    "~",       /* UNOP_BITNOT */
    1.85 +    "typeof",  /* UNOP_TYPEOF */
    1.86 +    "void"     /* UNOP_VOID */
    1.87 +};
    1.88 +
    1.89 +char const * const js::nodeTypeNames[] = {
    1.90 +#define ASTDEF(ast, str, method) str,
    1.91 +#include "jsast.tbl"
    1.92 +#undef ASTDEF
    1.93 +    nullptr
    1.94 +};
    1.95 +
    1.96 +static char const * const callbackNames[] = {
    1.97 +#define ASTDEF(ast, str, method) method,
    1.98 +#include "jsast.tbl"
    1.99 +#undef ASTDEF
   1.100 +    nullptr
   1.101 +};
   1.102 +
   1.103 +enum YieldKind { Delegating, NotDelegating };
   1.104 +
   1.105 +typedef AutoValueVector NodeVector;
   1.106 +
   1.107 +/*
   1.108 + * ParseNode is a somewhat intricate data structure, and its invariants have
   1.109 + * evolved, making it more likely that there could be a disconnect between the
   1.110 + * parser and the AST serializer. We use these macros to check invariants on a
   1.111 + * parse node and raise a dynamic error on failure.
   1.112 + */
   1.113 +#define LOCAL_ASSERT(expr)                                                                \
   1.114 +    JS_BEGIN_MACRO                                                                        \
   1.115 +        JS_ASSERT(expr);                                                                  \
   1.116 +        if (!(expr)) {                                                                    \
   1.117 +            JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, JSMSG_BAD_PARSE_NODE);  \
   1.118 +            return false;                                                                 \
   1.119 +        }                                                                                 \
   1.120 +    JS_END_MACRO
   1.121 +
   1.122 +#define LOCAL_NOT_REACHED(expr)                                                           \
   1.123 +    JS_BEGIN_MACRO                                                                        \
   1.124 +        MOZ_ASSERT(false);                                                                \
   1.125 +        JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, JSMSG_BAD_PARSE_NODE);      \
   1.126 +        return false;                                                                     \
   1.127 +    JS_END_MACRO
   1.128 +
   1.129 +namespace {
   1.130 +
   1.131 +/* Set 'result' to obj[id] if any such property exists, else defaultValue. */
   1.132 +static bool
   1.133 +GetPropertyDefault(JSContext *cx, HandleObject obj, HandleId id, HandleValue defaultValue,
   1.134 +                   MutableHandleValue result)
   1.135 +{
   1.136 +    bool found;
   1.137 +    if (!JSObject::hasProperty(cx, obj, id, &found))
   1.138 +        return false;
   1.139 +    if (!found) {
   1.140 +        result.set(defaultValue);
   1.141 +        return true;
   1.142 +    }
   1.143 +    return JSObject::getGeneric(cx, obj, obj, id, result);
   1.144 +}
   1.145 +
   1.146 +/*
   1.147 + * Builder class that constructs JavaScript AST node objects. See:
   1.148 + *
   1.149 + *     https://developer.mozilla.org/en/SpiderMonkey/Parser_API
   1.150 + *
   1.151 + * Bug 569487: generalize builder interface
   1.152 + */
   1.153 +class NodeBuilder
   1.154 +{
   1.155 +    typedef AutoValueArray<AST_LIMIT> CallbackArray;
   1.156 +
   1.157 +    JSContext   *cx;
   1.158 +    TokenStream *tokenStream;
   1.159 +    bool        saveLoc;               /* save source location information?     */
   1.160 +    char const  *src;                  /* source filename or null               */
   1.161 +    RootedValue srcval;                /* source filename JS value or null      */
   1.162 +    CallbackArray callbacks;           /* user-specified callbacks              */
   1.163 +    RootedValue userv;                 /* user-specified builder object or null */
   1.164 +
   1.165 +  public:
   1.166 +    NodeBuilder(JSContext *c, bool l, char const *s)
   1.167 +      : cx(c), tokenStream(nullptr), saveLoc(l), src(s), srcval(c), callbacks(cx),
   1.168 +          userv(c)
   1.169 +    {}
   1.170 +
   1.171 +    bool init(HandleObject userobj = js::NullPtr()) {
   1.172 +        if (src) {
   1.173 +            if (!atomValue(src, &srcval))
   1.174 +                return false;
   1.175 +        } else {
   1.176 +            srcval.setNull();
   1.177 +        }
   1.178 +
   1.179 +        if (!userobj) {
   1.180 +            userv.setNull();
   1.181 +            for (unsigned i = 0; i < AST_LIMIT; i++) {
   1.182 +                callbacks[i].setNull();
   1.183 +            }
   1.184 +            return true;
   1.185 +        }
   1.186 +
   1.187 +        userv.setObject(*userobj);
   1.188 +
   1.189 +        RootedValue nullVal(cx, NullValue());
   1.190 +        RootedValue funv(cx);
   1.191 +        for (unsigned i = 0; i < AST_LIMIT; i++) {
   1.192 +            const char *name = callbackNames[i];
   1.193 +            RootedAtom atom(cx, Atomize(cx, name, strlen(name)));
   1.194 +            if (!atom)
   1.195 +                return false;
   1.196 +            RootedId id(cx, AtomToId(atom));
   1.197 +            if (!GetPropertyDefault(cx, userobj, id, nullVal, &funv))
   1.198 +                return false;
   1.199 +
   1.200 +            if (funv.isNullOrUndefined()) {
   1.201 +                callbacks[i].setNull();
   1.202 +                continue;
   1.203 +            }
   1.204 +
   1.205 +            if (!funv.isObject() || !funv.toObject().is<JSFunction>()) {
   1.206 +                js_ReportValueErrorFlags(cx, JSREPORT_ERROR, JSMSG_NOT_FUNCTION,
   1.207 +                                         JSDVG_SEARCH_STACK, funv, js::NullPtr(), nullptr, nullptr);
   1.208 +                return false;
   1.209 +            }
   1.210 +
   1.211 +            callbacks[i].set(funv);
   1.212 +        }
   1.213 +
   1.214 +        return true;
   1.215 +    }
   1.216 +
   1.217 +    void setTokenStream(TokenStream *ts) {
   1.218 +        tokenStream = ts;
   1.219 +    }
   1.220 +
   1.221 +  private:
   1.222 +    bool callback(HandleValue fun, TokenPos *pos, MutableHandleValue dst) {
   1.223 +        if (saveLoc) {
   1.224 +            RootedValue loc(cx);
   1.225 +            if (!newNodeLoc(pos, &loc))
   1.226 +                return false;
   1.227 +            AutoValueArray<1> argv(cx);
   1.228 +            argv[0].set(loc);
   1.229 +            return Invoke(cx, userv, fun, argv.length(), argv.begin(), dst);
   1.230 +        }
   1.231 +
   1.232 +        AutoValueArray<1> argv(cx);
   1.233 +        argv[0].setNull(); /* no zero-length arrays allowed! */
   1.234 +        return Invoke(cx, userv, fun, 0, argv.begin(), dst);
   1.235 +    }
   1.236 +
   1.237 +    bool callback(HandleValue fun, HandleValue v1, TokenPos *pos, MutableHandleValue dst) {
   1.238 +        if (saveLoc) {
   1.239 +            RootedValue loc(cx);
   1.240 +            if (!newNodeLoc(pos, &loc))
   1.241 +                return false;
   1.242 +            AutoValueArray<2> argv(cx);
   1.243 +            argv[0].set(v1);
   1.244 +            argv[1].set(loc);
   1.245 +            return Invoke(cx, userv, fun, argv.length(), argv.begin(), dst);
   1.246 +        }
   1.247 +
   1.248 +        AutoValueArray<1> argv(cx);
   1.249 +        argv[0].set(v1);
   1.250 +        return Invoke(cx, userv, fun, argv.length(), argv.begin(), dst);
   1.251 +    }
   1.252 +
   1.253 +    bool callback(HandleValue fun, HandleValue v1, HandleValue v2, TokenPos *pos,
   1.254 +                  MutableHandleValue dst) {
   1.255 +        if (saveLoc) {
   1.256 +            RootedValue loc(cx);
   1.257 +            if (!newNodeLoc(pos, &loc))
   1.258 +                return false;
   1.259 +            AutoValueArray<3> argv(cx);
   1.260 +            argv[0].set(v1);
   1.261 +            argv[1].set(v2);
   1.262 +            argv[2].set(loc);
   1.263 +            return Invoke(cx, userv, fun, argv.length(), argv.begin(), dst);
   1.264 +        }
   1.265 +
   1.266 +        AutoValueArray<2> argv(cx);
   1.267 +        argv[0].set(v1);
   1.268 +        argv[1].set(v2);
   1.269 +        return Invoke(cx, userv, fun, argv.length(), argv.begin(), dst);
   1.270 +    }
   1.271 +
   1.272 +    bool callback(HandleValue fun, HandleValue v1, HandleValue v2, HandleValue v3, TokenPos *pos,
   1.273 +                  MutableHandleValue dst) {
   1.274 +        if (saveLoc) {
   1.275 +            RootedValue loc(cx);
   1.276 +            if (!newNodeLoc(pos, &loc))
   1.277 +                return false;
   1.278 +            AutoValueArray<4> argv(cx);
   1.279 +            argv[0].set(v1);
   1.280 +            argv[1].set(v2);
   1.281 +            argv[2].set(v3);
   1.282 +            argv[3].set(loc);
   1.283 +            return Invoke(cx, userv, fun, argv.length(), argv.begin(), dst);
   1.284 +        }
   1.285 +
   1.286 +        AutoValueArray<3> argv(cx);
   1.287 +        argv[0].set(v1);
   1.288 +        argv[1].set(v2);
   1.289 +        argv[2].set(v3);
   1.290 +        return Invoke(cx, userv, fun, argv.length(), argv.begin(), dst);
   1.291 +    }
   1.292 +
   1.293 +    bool callback(HandleValue fun, HandleValue v1, HandleValue v2, HandleValue v3, HandleValue v4,
   1.294 +                  TokenPos *pos, MutableHandleValue dst) {
   1.295 +        if (saveLoc) {
   1.296 +            RootedValue loc(cx);
   1.297 +            if (!newNodeLoc(pos, &loc))
   1.298 +                return false;
   1.299 +            AutoValueArray<5> argv(cx);
   1.300 +            argv[0].set(v1);
   1.301 +            argv[1].set(v2);
   1.302 +            argv[2].set(v3);
   1.303 +            argv[3].set(v4);
   1.304 +            argv[4].set(loc);
   1.305 +            return Invoke(cx, userv, fun, argv.length(), argv.begin(), dst);
   1.306 +        }
   1.307 +
   1.308 +        AutoValueArray<4> argv(cx);
   1.309 +        argv[0].set(v1);
   1.310 +        argv[1].set(v2);
   1.311 +        argv[2].set(v3);
   1.312 +        argv[3].set(v4);
   1.313 +        return Invoke(cx, userv, fun, argv.length(), argv.begin(), dst);
   1.314 +    }
   1.315 +
   1.316 +    bool callback(HandleValue fun, HandleValue v1, HandleValue v2, HandleValue v3, HandleValue v4,
   1.317 +                  HandleValue v5, TokenPos *pos, MutableHandleValue dst) {
   1.318 +        if (saveLoc) {
   1.319 +            RootedValue loc(cx);
   1.320 +            if (!newNodeLoc(pos, &loc))
   1.321 +                return false;
   1.322 +            AutoValueArray<6> argv(cx);
   1.323 +            argv[0].set(v1);
   1.324 +            argv[1].set(v2);
   1.325 +            argv[2].set(v3);
   1.326 +            argv[3].set(v4);
   1.327 +            argv[4].set(v5);
   1.328 +            argv[5].set(loc);
   1.329 +            return Invoke(cx, userv, fun, argv.length(), argv.begin(), dst);
   1.330 +        }
   1.331 +
   1.332 +        AutoValueArray<5> argv(cx);
   1.333 +        argv[0].set(v1);
   1.334 +        argv[1].set(v2);
   1.335 +        argv[2].set(v3);
   1.336 +        argv[3].set(v4);
   1.337 +        argv[4].set(v5);
   1.338 +        return Invoke(cx, userv, fun, argv.length(), argv.begin(), dst);
   1.339 +    }
   1.340 +
   1.341 +    // WARNING: Returning a Handle is non-standard, but it works in this case
   1.342 +    // because both |v| and |UndefinedHandleValue| are definitely rooted on a
   1.343 +    // previous stack frame (i.e. we're just choosing between two
   1.344 +    // already-rooted values).
   1.345 +    HandleValue opt(HandleValue v) {
   1.346 +        JS_ASSERT_IF(v.isMagic(), v.whyMagic() == JS_SERIALIZE_NO_NODE);
   1.347 +        return v.isMagic(JS_SERIALIZE_NO_NODE) ? JS::UndefinedHandleValue : v;
   1.348 +    }
   1.349 +
   1.350 +    bool atomValue(const char *s, MutableHandleValue dst) {
   1.351 +        /*
   1.352 +         * Bug 575416: instead of Atomize, lookup constant atoms in tbl file
   1.353 +         */
   1.354 +        RootedAtom atom(cx, Atomize(cx, s, strlen(s)));
   1.355 +        if (!atom)
   1.356 +            return false;
   1.357 +
   1.358 +        dst.setString(atom);
   1.359 +        return true;
   1.360 +    }
   1.361 +
   1.362 +    bool newObject(MutableHandleObject dst) {
   1.363 +        RootedObject nobj(cx, NewBuiltinClassInstance(cx, &JSObject::class_));
   1.364 +        if (!nobj)
   1.365 +            return false;
   1.366 +
   1.367 +        dst.set(nobj);
   1.368 +        return true;
   1.369 +    }
   1.370 +
   1.371 +    bool newArray(NodeVector &elts, MutableHandleValue dst);
   1.372 +
   1.373 +    bool newNode(ASTType type, TokenPos *pos, MutableHandleObject dst);
   1.374 +
   1.375 +    bool newNode(ASTType type, TokenPos *pos, MutableHandleValue dst) {
   1.376 +        RootedObject node(cx);
   1.377 +        return newNode(type, pos, &node) &&
   1.378 +               setResult(node, dst);
   1.379 +    }
   1.380 +
   1.381 +    bool newNode(ASTType type, TokenPos *pos,
   1.382 +                 const char *childName, HandleValue child,
   1.383 +                 MutableHandleValue dst) {
   1.384 +        RootedObject node(cx);
   1.385 +        return newNode(type, pos, &node) &&
   1.386 +               setProperty(node, childName, child) &&
   1.387 +               setResult(node, dst);
   1.388 +    }
   1.389 +
   1.390 +    bool newNode(ASTType type, TokenPos *pos,
   1.391 +                 const char *childName1, HandleValue child1,
   1.392 +                 const char *childName2, HandleValue child2,
   1.393 +                 MutableHandleValue dst) {
   1.394 +        RootedObject node(cx);
   1.395 +        return newNode(type, pos, &node) &&
   1.396 +               setProperty(node, childName1, child1) &&
   1.397 +               setProperty(node, childName2, child2) &&
   1.398 +               setResult(node, dst);
   1.399 +    }
   1.400 +
   1.401 +    bool newNode(ASTType type, TokenPos *pos,
   1.402 +                 const char *childName1, HandleValue child1,
   1.403 +                 const char *childName2, HandleValue child2,
   1.404 +                 const char *childName3, HandleValue child3,
   1.405 +                 MutableHandleValue dst) {
   1.406 +        RootedObject node(cx);
   1.407 +        return newNode(type, pos, &node) &&
   1.408 +               setProperty(node, childName1, child1) &&
   1.409 +               setProperty(node, childName2, child2) &&
   1.410 +               setProperty(node, childName3, child3) &&
   1.411 +               setResult(node, dst);
   1.412 +    }
   1.413 +
   1.414 +    bool newNode(ASTType type, TokenPos *pos,
   1.415 +                 const char *childName1, HandleValue child1,
   1.416 +                 const char *childName2, HandleValue child2,
   1.417 +                 const char *childName3, HandleValue child3,
   1.418 +                 const char *childName4, HandleValue child4,
   1.419 +                 MutableHandleValue dst) {
   1.420 +        RootedObject node(cx);
   1.421 +        return newNode(type, pos, &node) &&
   1.422 +               setProperty(node, childName1, child1) &&
   1.423 +               setProperty(node, childName2, child2) &&
   1.424 +               setProperty(node, childName3, child3) &&
   1.425 +               setProperty(node, childName4, child4) &&
   1.426 +               setResult(node, dst);
   1.427 +    }
   1.428 +
   1.429 +    bool newNode(ASTType type, TokenPos *pos,
   1.430 +                 const char *childName1, HandleValue child1,
   1.431 +                 const char *childName2, HandleValue child2,
   1.432 +                 const char *childName3, HandleValue child3,
   1.433 +                 const char *childName4, HandleValue child4,
   1.434 +                 const char *childName5, HandleValue child5,
   1.435 +                 MutableHandleValue dst) {
   1.436 +        RootedObject node(cx);
   1.437 +        return newNode(type, pos, &node) &&
   1.438 +               setProperty(node, childName1, child1) &&
   1.439 +               setProperty(node, childName2, child2) &&
   1.440 +               setProperty(node, childName3, child3) &&
   1.441 +               setProperty(node, childName4, child4) &&
   1.442 +               setProperty(node, childName5, child5) &&
   1.443 +               setResult(node, dst);
   1.444 +    }
   1.445 +
   1.446 +    bool newNode(ASTType type, TokenPos *pos,
   1.447 +                 const char *childName1, HandleValue child1,
   1.448 +                 const char *childName2, HandleValue child2,
   1.449 +                 const char *childName3, HandleValue child3,
   1.450 +                 const char *childName4, HandleValue child4,
   1.451 +                 const char *childName5, HandleValue child5,
   1.452 +                 const char *childName6, HandleValue child6,
   1.453 +                 const char *childName7, HandleValue child7,
   1.454 +                 MutableHandleValue dst) {
   1.455 +        RootedObject node(cx);
   1.456 +        return newNode(type, pos, &node) &&
   1.457 +               setProperty(node, childName1, child1) &&
   1.458 +               setProperty(node, childName2, child2) &&
   1.459 +               setProperty(node, childName3, child3) &&
   1.460 +               setProperty(node, childName4, child4) &&
   1.461 +               setProperty(node, childName5, child5) &&
   1.462 +               setProperty(node, childName6, child6) &&
   1.463 +               setProperty(node, childName7, child7) &&
   1.464 +               setResult(node, dst);
   1.465 +    }
   1.466 +
   1.467 +    bool listNode(ASTType type, const char *propName, NodeVector &elts, TokenPos *pos,
   1.468 +                  MutableHandleValue dst) {
   1.469 +        RootedValue array(cx);
   1.470 +        if (!newArray(elts, &array))
   1.471 +            return false;
   1.472 +
   1.473 +        RootedValue cb(cx, callbacks[type]);
   1.474 +        if (!cb.isNull())
   1.475 +            return callback(cb, array, pos, dst);
   1.476 +
   1.477 +        return newNode(type, pos, propName, array, dst);
   1.478 +    }
   1.479 +
   1.480 +    bool setProperty(HandleObject obj, const char *name, HandleValue val) {
   1.481 +        JS_ASSERT_IF(val.isMagic(), val.whyMagic() == JS_SERIALIZE_NO_NODE);
   1.482 +
   1.483 +        /*
   1.484 +         * Bug 575416: instead of Atomize, lookup constant atoms in tbl file
   1.485 +         */
   1.486 +        RootedAtom atom(cx, Atomize(cx, name, strlen(name)));
   1.487 +        if (!atom)
   1.488 +            return false;
   1.489 +
   1.490 +        /* Represent "no node" as null and ensure users are not exposed to magic values. */
   1.491 +        RootedValue optVal(cx, val.isMagic(JS_SERIALIZE_NO_NODE) ? NullValue() : val);
   1.492 +        return JSObject::defineProperty(cx, obj, atom->asPropertyName(), optVal);
   1.493 +    }
   1.494 +
   1.495 +    bool newNodeLoc(TokenPos *pos, MutableHandleValue dst);
   1.496 +
   1.497 +    bool setNodeLoc(HandleObject node, TokenPos *pos);
   1.498 +
   1.499 +    bool setResult(HandleObject obj, MutableHandleValue dst) {
   1.500 +        JS_ASSERT(obj);
   1.501 +        dst.setObject(*obj);
   1.502 +        return true;
   1.503 +    }
   1.504 +
   1.505 +  public:
   1.506 +    /*
   1.507 +     * All of the public builder methods take as their last two
   1.508 +     * arguments a nullable token position and a non-nullable, rooted
   1.509 +     * outparam.
   1.510 +     *
   1.511 +     * Any Value arguments representing optional subnodes may be a
   1.512 +     * JS_SERIALIZE_NO_NODE magic value.
   1.513 +     */
   1.514 +
   1.515 +    /*
   1.516 +     * misc nodes
   1.517 +     */
   1.518 +
   1.519 +    bool program(NodeVector &elts, TokenPos *pos, MutableHandleValue dst);
   1.520 +
   1.521 +    bool literal(HandleValue val, TokenPos *pos, MutableHandleValue dst);
   1.522 +
   1.523 +    bool identifier(HandleValue name, TokenPos *pos, MutableHandleValue dst);
   1.524 +
   1.525 +    bool function(ASTType type, TokenPos *pos,
   1.526 +                  HandleValue id, NodeVector &args, NodeVector &defaults,
   1.527 +                  HandleValue body, HandleValue rest, bool isGenerator, bool isExpression,
   1.528 +                  MutableHandleValue dst);
   1.529 +
   1.530 +    bool variableDeclarator(HandleValue id, HandleValue init, TokenPos *pos,
   1.531 +                            MutableHandleValue dst);
   1.532 +
   1.533 +    bool switchCase(HandleValue expr, NodeVector &elts, TokenPos *pos, MutableHandleValue dst);
   1.534 +
   1.535 +    bool catchClause(HandleValue var, HandleValue guard, HandleValue body, TokenPos *pos,
   1.536 +                     MutableHandleValue dst);
   1.537 +
   1.538 +    bool propertyInitializer(HandleValue key, HandleValue val, PropKind kind, TokenPos *pos,
   1.539 +                             MutableHandleValue dst);
   1.540 +
   1.541 +
   1.542 +    /*
   1.543 +     * statements
   1.544 +     */
   1.545 +
   1.546 +    bool blockStatement(NodeVector &elts, TokenPos *pos, MutableHandleValue dst);
   1.547 +
   1.548 +    bool expressionStatement(HandleValue expr, TokenPos *pos, MutableHandleValue dst);
   1.549 +
   1.550 +    bool emptyStatement(TokenPos *pos, MutableHandleValue dst);
   1.551 +
   1.552 +    bool ifStatement(HandleValue test, HandleValue cons, HandleValue alt, TokenPos *pos,
   1.553 +                     MutableHandleValue dst);
   1.554 +
   1.555 +    bool breakStatement(HandleValue label, TokenPos *pos, MutableHandleValue dst);
   1.556 +
   1.557 +    bool continueStatement(HandleValue label, TokenPos *pos, MutableHandleValue dst);
   1.558 +
   1.559 +    bool labeledStatement(HandleValue label, HandleValue stmt, TokenPos *pos,
   1.560 +                          MutableHandleValue dst);
   1.561 +
   1.562 +    bool throwStatement(HandleValue arg, TokenPos *pos, MutableHandleValue dst);
   1.563 +
   1.564 +    bool returnStatement(HandleValue arg, TokenPos *pos, MutableHandleValue dst);
   1.565 +
   1.566 +    bool forStatement(HandleValue init, HandleValue test, HandleValue update, HandleValue stmt,
   1.567 +                      TokenPos *pos, MutableHandleValue dst);
   1.568 +
   1.569 +    bool forInStatement(HandleValue var, HandleValue expr, HandleValue stmt,
   1.570 +                        bool isForEach, TokenPos *pos, MutableHandleValue dst);
   1.571 +
   1.572 +    bool forOfStatement(HandleValue var, HandleValue expr, HandleValue stmt, TokenPos *pos,
   1.573 +                        MutableHandleValue dst);
   1.574 +
   1.575 +    bool withStatement(HandleValue expr, HandleValue stmt, TokenPos *pos, MutableHandleValue dst);
   1.576 +
   1.577 +    bool whileStatement(HandleValue test, HandleValue stmt, TokenPos *pos, MutableHandleValue dst);
   1.578 +
   1.579 +    bool doWhileStatement(HandleValue stmt, HandleValue test, TokenPos *pos,
   1.580 +                          MutableHandleValue dst);
   1.581 +
   1.582 +    bool switchStatement(HandleValue disc, NodeVector &elts, bool lexical, TokenPos *pos,
   1.583 +                         MutableHandleValue dst);
   1.584 +
   1.585 +    bool tryStatement(HandleValue body, NodeVector &guarded, HandleValue unguarded,
   1.586 +                      HandleValue finally, TokenPos *pos, MutableHandleValue dst);
   1.587 +
   1.588 +    bool debuggerStatement(TokenPos *pos, MutableHandleValue dst);
   1.589 +
   1.590 +    bool letStatement(NodeVector &head, HandleValue stmt, TokenPos *pos, MutableHandleValue dst);
   1.591 +
   1.592 +    bool importDeclaration(NodeVector &elts, HandleValue moduleSpec, TokenPos *pos, MutableHandleValue dst);
   1.593 +
   1.594 +    bool importSpecifier(HandleValue importName, HandleValue bindingName, TokenPos *pos, MutableHandleValue dst);
   1.595 +
   1.596 +    bool exportDeclaration(HandleValue decl, NodeVector &elts, HandleValue moduleSpec, TokenPos *pos, MutableHandleValue dst);
   1.597 +
   1.598 +    bool exportSpecifier(HandleValue bindingName, HandleValue exportName, TokenPos *pos, MutableHandleValue dst);
   1.599 +
   1.600 +    bool exportBatchSpecifier(TokenPos *pos, MutableHandleValue dst);
   1.601 +
   1.602 +    /*
   1.603 +     * expressions
   1.604 +     */
   1.605 +
   1.606 +    bool binaryExpression(BinaryOperator op, HandleValue left, HandleValue right, TokenPos *pos,
   1.607 +                          MutableHandleValue dst);
   1.608 +
   1.609 +    bool unaryExpression(UnaryOperator op, HandleValue expr, TokenPos *pos, MutableHandleValue dst);
   1.610 +
   1.611 +    bool assignmentExpression(AssignmentOperator op, HandleValue lhs, HandleValue rhs,
   1.612 +                              TokenPos *pos, MutableHandleValue dst);
   1.613 +
   1.614 +    bool updateExpression(HandleValue expr, bool incr, bool prefix, TokenPos *pos,
   1.615 +                          MutableHandleValue dst);
   1.616 +
   1.617 +    bool logicalExpression(bool lor, HandleValue left, HandleValue right, TokenPos *pos,
   1.618 +                           MutableHandleValue dst);
   1.619 +
   1.620 +    bool conditionalExpression(HandleValue test, HandleValue cons, HandleValue alt, TokenPos *pos,
   1.621 +                               MutableHandleValue dst);
   1.622 +
   1.623 +    bool sequenceExpression(NodeVector &elts, TokenPos *pos, MutableHandleValue dst);
   1.624 +
   1.625 +    bool newExpression(HandleValue callee, NodeVector &args, TokenPos *pos, MutableHandleValue dst);
   1.626 +
   1.627 +    bool callExpression(HandleValue callee, NodeVector &args, TokenPos *pos,
   1.628 +                        MutableHandleValue dst);
   1.629 +
   1.630 +    bool memberExpression(bool computed, HandleValue expr, HandleValue member, TokenPos *pos,
   1.631 +                          MutableHandleValue dst);
   1.632 +
   1.633 +    bool arrayExpression(NodeVector &elts, TokenPos *pos, MutableHandleValue dst);
   1.634 +
   1.635 +    bool spreadExpression(HandleValue expr, TokenPos *pos, MutableHandleValue dst);
   1.636 +
   1.637 +    bool objectExpression(NodeVector &elts, TokenPos *pos, MutableHandleValue dst);
   1.638 +
   1.639 +    bool thisExpression(TokenPos *pos, MutableHandleValue dst);
   1.640 +
   1.641 +    bool yieldExpression(HandleValue arg, YieldKind kind, TokenPos *pos, MutableHandleValue dst);
   1.642 +
   1.643 +    bool comprehensionBlock(HandleValue patt, HandleValue src, bool isForEach, bool isForOf, TokenPos *pos,
   1.644 +                            MutableHandleValue dst);
   1.645 +
   1.646 +    bool comprehensionExpression(HandleValue body, NodeVector &blocks, HandleValue filter,
   1.647 +                                 TokenPos *pos, MutableHandleValue dst);
   1.648 +
   1.649 +    bool generatorExpression(HandleValue body, NodeVector &blocks, HandleValue filter,
   1.650 +                             TokenPos *pos, MutableHandleValue dst);
   1.651 +
   1.652 +    bool letExpression(NodeVector &head, HandleValue expr, TokenPos *pos, MutableHandleValue dst);
   1.653 +
   1.654 +    /*
   1.655 +     * declarations
   1.656 +     */
   1.657 +
   1.658 +    bool variableDeclaration(NodeVector &elts, VarDeclKind kind, TokenPos *pos,
   1.659 +                             MutableHandleValue dst);
   1.660 +
   1.661 +    /*
   1.662 +     * patterns
   1.663 +     */
   1.664 +
   1.665 +    bool arrayPattern(NodeVector &elts, TokenPos *pos, MutableHandleValue dst);
   1.666 +
   1.667 +    bool objectPattern(NodeVector &elts, TokenPos *pos, MutableHandleValue dst);
   1.668 +
   1.669 +    bool propertyPattern(HandleValue key, HandleValue patt, TokenPos *pos, MutableHandleValue dst);
   1.670 +};
   1.671 +
   1.672 +} /* anonymous namespace */
   1.673 +
   1.674 +bool
   1.675 +NodeBuilder::newNode(ASTType type, TokenPos *pos, MutableHandleObject dst)
   1.676 +{
   1.677 +    JS_ASSERT(type > AST_ERROR && type < AST_LIMIT);
   1.678 +
   1.679 +    RootedValue tv(cx);
   1.680 +    RootedObject node(cx, NewBuiltinClassInstance(cx, &JSObject::class_));
   1.681 +    if (!node ||
   1.682 +        !setNodeLoc(node, pos) ||
   1.683 +        !atomValue(nodeTypeNames[type], &tv) ||
   1.684 +        !setProperty(node, "type", tv)) {
   1.685 +        return false;
   1.686 +    }
   1.687 +
   1.688 +    dst.set(node);
   1.689 +    return true;
   1.690 +}
   1.691 +
   1.692 +bool
   1.693 +NodeBuilder::newArray(NodeVector &elts, MutableHandleValue dst)
   1.694 +{
   1.695 +    const size_t len = elts.length();
   1.696 +    if (len > UINT32_MAX) {
   1.697 +        js_ReportAllocationOverflow(cx);
   1.698 +        return false;
   1.699 +    }
   1.700 +    RootedObject array(cx, NewDenseAllocatedArray(cx, uint32_t(len)));
   1.701 +    if (!array)
   1.702 +        return false;
   1.703 +
   1.704 +    for (size_t i = 0; i < len; i++) {
   1.705 +        RootedValue val(cx, elts[i]);
   1.706 +
   1.707 +        JS_ASSERT_IF(val.isMagic(), val.whyMagic() == JS_SERIALIZE_NO_NODE);
   1.708 +
   1.709 +        /* Represent "no node" as an array hole by not adding the value. */
   1.710 +        if (val.isMagic(JS_SERIALIZE_NO_NODE))
   1.711 +            continue;
   1.712 +
   1.713 +        if (!JSObject::setElement(cx, array, array, i, &val, false))
   1.714 +            return false;
   1.715 +    }
   1.716 +
   1.717 +    dst.setObject(*array);
   1.718 +    return true;
   1.719 +}
   1.720 +
   1.721 +bool
   1.722 +NodeBuilder::newNodeLoc(TokenPos *pos, MutableHandleValue dst)
   1.723 +{
   1.724 +    if (!pos) {
   1.725 +        dst.setNull();
   1.726 +        return true;
   1.727 +    }
   1.728 +
   1.729 +    RootedObject loc(cx);
   1.730 +    RootedObject to(cx);
   1.731 +    RootedValue val(cx);
   1.732 +
   1.733 +    if (!newObject(&loc))
   1.734 +        return false;
   1.735 +
   1.736 +    dst.setObject(*loc);
   1.737 +
   1.738 +    uint32_t startLineNum, startColumnIndex;
   1.739 +    uint32_t endLineNum, endColumnIndex;
   1.740 +    tokenStream->srcCoords.lineNumAndColumnIndex(pos->begin, &startLineNum, &startColumnIndex);
   1.741 +    tokenStream->srcCoords.lineNumAndColumnIndex(pos->end, &endLineNum, &endColumnIndex);
   1.742 +
   1.743 +    if (!newObject(&to))
   1.744 +        return false;
   1.745 +    val.setObject(*to);
   1.746 +    if (!setProperty(loc, "start", val))
   1.747 +        return false;
   1.748 +    val.setNumber(startLineNum);
   1.749 +    if (!setProperty(to, "line", val))
   1.750 +        return false;
   1.751 +    val.setNumber(startColumnIndex);
   1.752 +    if (!setProperty(to, "column", val))
   1.753 +        return false;
   1.754 +
   1.755 +    if (!newObject(&to))
   1.756 +        return false;
   1.757 +    val.setObject(*to);
   1.758 +    if (!setProperty(loc, "end", val))
   1.759 +        return false;
   1.760 +    val.setNumber(endLineNum);
   1.761 +    if (!setProperty(to, "line", val))
   1.762 +        return false;
   1.763 +    val.setNumber(endColumnIndex);
   1.764 +    if (!setProperty(to, "column", val))
   1.765 +        return false;
   1.766 +
   1.767 +    if (!setProperty(loc, "source", srcval))
   1.768 +        return false;
   1.769 +
   1.770 +    return true;
   1.771 +}
   1.772 +
   1.773 +bool
   1.774 +NodeBuilder::setNodeLoc(HandleObject node, TokenPos *pos)
   1.775 +{
   1.776 +    if (!saveLoc) {
   1.777 +        RootedValue nullVal(cx, NullValue());
   1.778 +        setProperty(node, "loc", nullVal);
   1.779 +        return true;
   1.780 +    }
   1.781 +
   1.782 +    RootedValue loc(cx);
   1.783 +    return newNodeLoc(pos, &loc) &&
   1.784 +           setProperty(node, "loc", loc);
   1.785 +}
   1.786 +
   1.787 +bool
   1.788 +NodeBuilder::program(NodeVector &elts, TokenPos *pos, MutableHandleValue dst)
   1.789 +{
   1.790 +    return listNode(AST_PROGRAM, "body", elts, pos, dst);
   1.791 +}
   1.792 +
   1.793 +bool
   1.794 +NodeBuilder::blockStatement(NodeVector &elts, TokenPos *pos, MutableHandleValue dst)
   1.795 +{
   1.796 +    return listNode(AST_BLOCK_STMT, "body", elts, pos, dst);
   1.797 +}
   1.798 +
   1.799 +bool
   1.800 +NodeBuilder::expressionStatement(HandleValue expr, TokenPos *pos, MutableHandleValue dst)
   1.801 +{
   1.802 +    RootedValue cb(cx, callbacks[AST_EXPR_STMT]);
   1.803 +    if (!cb.isNull())
   1.804 +        return callback(cb, expr, pos, dst);
   1.805 +
   1.806 +    return newNode(AST_EXPR_STMT, pos, "expression", expr, dst);
   1.807 +}
   1.808 +
   1.809 +bool
   1.810 +NodeBuilder::emptyStatement(TokenPos *pos, MutableHandleValue dst)
   1.811 +{
   1.812 +    RootedValue cb(cx, callbacks[AST_EMPTY_STMT]);
   1.813 +    if (!cb.isNull())
   1.814 +        return callback(cb, pos, dst);
   1.815 +
   1.816 +    return newNode(AST_EMPTY_STMT, pos, dst);
   1.817 +}
   1.818 +
   1.819 +bool
   1.820 +NodeBuilder::ifStatement(HandleValue test, HandleValue cons, HandleValue alt, TokenPos *pos,
   1.821 +                         MutableHandleValue dst)
   1.822 +{
   1.823 +    RootedValue cb(cx, callbacks[AST_IF_STMT]);
   1.824 +    if (!cb.isNull())
   1.825 +        return callback(cb, test, cons, opt(alt), pos, dst);
   1.826 +
   1.827 +    return newNode(AST_IF_STMT, pos,
   1.828 +                   "test", test,
   1.829 +                   "consequent", cons,
   1.830 +                   "alternate", alt,
   1.831 +                   dst);
   1.832 +}
   1.833 +
   1.834 +bool
   1.835 +NodeBuilder::breakStatement(HandleValue label, TokenPos *pos, MutableHandleValue dst)
   1.836 +{
   1.837 +    RootedValue cb(cx, callbacks[AST_BREAK_STMT]);
   1.838 +    if (!cb.isNull())
   1.839 +        return callback(cb, opt(label), pos, dst);
   1.840 +
   1.841 +    return newNode(AST_BREAK_STMT, pos, "label", label, dst);
   1.842 +}
   1.843 +
   1.844 +bool
   1.845 +NodeBuilder::continueStatement(HandleValue label, TokenPos *pos, MutableHandleValue dst)
   1.846 +{
   1.847 +    RootedValue cb(cx, callbacks[AST_CONTINUE_STMT]);
   1.848 +    if (!cb.isNull())
   1.849 +        return callback(cb, opt(label), pos, dst);
   1.850 +
   1.851 +    return newNode(AST_CONTINUE_STMT, pos, "label", label, dst);
   1.852 +}
   1.853 +
   1.854 +bool
   1.855 +NodeBuilder::labeledStatement(HandleValue label, HandleValue stmt, TokenPos *pos,
   1.856 +                              MutableHandleValue dst)
   1.857 +{
   1.858 +    RootedValue cb(cx, callbacks[AST_LAB_STMT]);
   1.859 +    if (!cb.isNull())
   1.860 +        return callback(cb, label, stmt, pos, dst);
   1.861 +
   1.862 +    return newNode(AST_LAB_STMT, pos,
   1.863 +                   "label", label,
   1.864 +                   "body", stmt,
   1.865 +                   dst);
   1.866 +}
   1.867 +
   1.868 +bool
   1.869 +NodeBuilder::throwStatement(HandleValue arg, TokenPos *pos, MutableHandleValue dst)
   1.870 +{
   1.871 +    RootedValue cb(cx, callbacks[AST_THROW_STMT]);
   1.872 +    if (!cb.isNull())
   1.873 +        return callback(cb, arg, pos, dst);
   1.874 +
   1.875 +    return newNode(AST_THROW_STMT, pos, "argument", arg, dst);
   1.876 +}
   1.877 +
   1.878 +bool
   1.879 +NodeBuilder::returnStatement(HandleValue arg, TokenPos *pos, MutableHandleValue dst)
   1.880 +{
   1.881 +    RootedValue cb(cx, callbacks[AST_RETURN_STMT]);
   1.882 +    if (!cb.isNull())
   1.883 +        return callback(cb, opt(arg), pos, dst);
   1.884 +
   1.885 +    return newNode(AST_RETURN_STMT, pos, "argument", arg, dst);
   1.886 +}
   1.887 +
   1.888 +bool
   1.889 +NodeBuilder::forStatement(HandleValue init, HandleValue test, HandleValue update, HandleValue stmt,
   1.890 +                          TokenPos *pos, MutableHandleValue dst)
   1.891 +{
   1.892 +    RootedValue cb(cx, callbacks[AST_FOR_STMT]);
   1.893 +    if (!cb.isNull())
   1.894 +        return callback(cb, opt(init), opt(test), opt(update), stmt, pos, dst);
   1.895 +
   1.896 +    return newNode(AST_FOR_STMT, pos,
   1.897 +                   "init", init,
   1.898 +                   "test", test,
   1.899 +                   "update", update,
   1.900 +                   "body", stmt,
   1.901 +                   dst);
   1.902 +}
   1.903 +
   1.904 +bool
   1.905 +NodeBuilder::forInStatement(HandleValue var, HandleValue expr, HandleValue stmt, bool isForEach,
   1.906 +                            TokenPos *pos, MutableHandleValue dst)
   1.907 +{
   1.908 +    RootedValue isForEachVal(cx, BooleanValue(isForEach));
   1.909 +
   1.910 +    RootedValue cb(cx, callbacks[AST_FOR_IN_STMT]);
   1.911 +    if (!cb.isNull())
   1.912 +        return callback(cb, var, expr, stmt, isForEachVal, pos, dst);
   1.913 +
   1.914 +    return newNode(AST_FOR_IN_STMT, pos,
   1.915 +                   "left", var,
   1.916 +                   "right", expr,
   1.917 +                   "body", stmt,
   1.918 +                   "each", isForEachVal,
   1.919 +                   dst);
   1.920 +}
   1.921 +
   1.922 +bool
   1.923 +NodeBuilder::forOfStatement(HandleValue var, HandleValue expr, HandleValue stmt, TokenPos *pos,
   1.924 +                            MutableHandleValue dst)
   1.925 +{
   1.926 +    RootedValue cb(cx, callbacks[AST_FOR_OF_STMT]);
   1.927 +    if (!cb.isNull())
   1.928 +        return callback(cb, var, expr, stmt, pos, dst);
   1.929 +
   1.930 +    return newNode(AST_FOR_OF_STMT, pos,
   1.931 +                   "left", var,
   1.932 +                   "right", expr,
   1.933 +                   "body", stmt,
   1.934 +                   dst);
   1.935 +}
   1.936 +
   1.937 +bool
   1.938 +NodeBuilder::withStatement(HandleValue expr, HandleValue stmt, TokenPos *pos,
   1.939 +                           MutableHandleValue dst)
   1.940 +{
   1.941 +    RootedValue cb(cx, callbacks[AST_WITH_STMT]);
   1.942 +    if (!cb.isNull())
   1.943 +        return callback(cb, expr, stmt, pos, dst);
   1.944 +
   1.945 +    return newNode(AST_WITH_STMT, pos,
   1.946 +                   "object", expr,
   1.947 +                   "body", stmt,
   1.948 +                   dst);
   1.949 +}
   1.950 +
   1.951 +bool
   1.952 +NodeBuilder::whileStatement(HandleValue test, HandleValue stmt, TokenPos *pos,
   1.953 +                            MutableHandleValue dst)
   1.954 +{
   1.955 +    RootedValue cb(cx, callbacks[AST_WHILE_STMT]);
   1.956 +    if (!cb.isNull())
   1.957 +        return callback(cb, test, stmt, pos, dst);
   1.958 +
   1.959 +    return newNode(AST_WHILE_STMT, pos,
   1.960 +                   "test", test,
   1.961 +                   "body", stmt,
   1.962 +                   dst);
   1.963 +}
   1.964 +
   1.965 +bool
   1.966 +NodeBuilder::doWhileStatement(HandleValue stmt, HandleValue test, TokenPos *pos,
   1.967 +                              MutableHandleValue dst)
   1.968 +{
   1.969 +    RootedValue cb(cx, callbacks[AST_DO_STMT]);
   1.970 +    if (!cb.isNull())
   1.971 +        return callback(cb, stmt, test, pos, dst);
   1.972 +
   1.973 +    return newNode(AST_DO_STMT, pos,
   1.974 +                   "body", stmt,
   1.975 +                   "test", test,
   1.976 +                   dst);
   1.977 +}
   1.978 +
   1.979 +bool
   1.980 +NodeBuilder::switchStatement(HandleValue disc, NodeVector &elts, bool lexical, TokenPos *pos,
   1.981 +                             MutableHandleValue dst)
   1.982 +{
   1.983 +    RootedValue array(cx);
   1.984 +    if (!newArray(elts, &array))
   1.985 +        return false;
   1.986 +
   1.987 +    RootedValue lexicalVal(cx, BooleanValue(lexical));
   1.988 +
   1.989 +    RootedValue cb(cx, callbacks[AST_SWITCH_STMT]);
   1.990 +    if (!cb.isNull())
   1.991 +        return callback(cb, disc, array, lexicalVal, pos, dst);
   1.992 +
   1.993 +    return newNode(AST_SWITCH_STMT, pos,
   1.994 +                   "discriminant", disc,
   1.995 +                   "cases", array,
   1.996 +                   "lexical", lexicalVal,
   1.997 +                   dst);
   1.998 +}
   1.999 +
  1.1000 +bool
  1.1001 +NodeBuilder::tryStatement(HandleValue body, NodeVector &guarded, HandleValue unguarded,
  1.1002 +                          HandleValue finally, TokenPos *pos, MutableHandleValue dst)
  1.1003 +{
  1.1004 +    RootedValue guardedHandlers(cx);
  1.1005 +    if (!newArray(guarded, &guardedHandlers))
  1.1006 +        return false;
  1.1007 +
  1.1008 +    RootedValue cb(cx, callbacks[AST_TRY_STMT]);
  1.1009 +    if (!cb.isNull())
  1.1010 +        return callback(cb, body, guardedHandlers, unguarded, opt(finally), pos, dst);
  1.1011 +
  1.1012 +    return newNode(AST_TRY_STMT, pos,
  1.1013 +                   "block", body,
  1.1014 +                   "guardedHandlers", guardedHandlers,
  1.1015 +                   "handler", unguarded,
  1.1016 +                   "finalizer", finally,
  1.1017 +                   dst);
  1.1018 +}
  1.1019 +
  1.1020 +bool
  1.1021 +NodeBuilder::debuggerStatement(TokenPos *pos, MutableHandleValue dst)
  1.1022 +{
  1.1023 +    RootedValue cb(cx, callbacks[AST_DEBUGGER_STMT]);
  1.1024 +    if (!cb.isNull())
  1.1025 +        return callback(cb, pos, dst);
  1.1026 +
  1.1027 +    return newNode(AST_DEBUGGER_STMT, pos, dst);
  1.1028 +}
  1.1029 +
  1.1030 +bool
  1.1031 +NodeBuilder::binaryExpression(BinaryOperator op, HandleValue left, HandleValue right, TokenPos *pos,
  1.1032 +                              MutableHandleValue dst)
  1.1033 +{
  1.1034 +    JS_ASSERT(op > BINOP_ERR && op < BINOP_LIMIT);
  1.1035 +
  1.1036 +    RootedValue opName(cx);
  1.1037 +    if (!atomValue(binopNames[op], &opName))
  1.1038 +        return false;
  1.1039 +
  1.1040 +    RootedValue cb(cx, callbacks[AST_BINARY_EXPR]);
  1.1041 +    if (!cb.isNull())
  1.1042 +        return callback(cb, opName, left, right, pos, dst);
  1.1043 +
  1.1044 +    return newNode(AST_BINARY_EXPR, pos,
  1.1045 +                   "operator", opName,
  1.1046 +                   "left", left,
  1.1047 +                   "right", right,
  1.1048 +                   dst);
  1.1049 +}
  1.1050 +
  1.1051 +bool
  1.1052 +NodeBuilder::unaryExpression(UnaryOperator unop, HandleValue expr, TokenPos *pos,
  1.1053 +                             MutableHandleValue dst)
  1.1054 +{
  1.1055 +    JS_ASSERT(unop > UNOP_ERR && unop < UNOP_LIMIT);
  1.1056 +
  1.1057 +    RootedValue opName(cx);
  1.1058 +    if (!atomValue(unopNames[unop], &opName))
  1.1059 +        return false;
  1.1060 +
  1.1061 +    RootedValue cb(cx, callbacks[AST_UNARY_EXPR]);
  1.1062 +    if (!cb.isNull())
  1.1063 +        return callback(cb, opName, expr, pos, dst);
  1.1064 +
  1.1065 +    RootedValue trueVal(cx, BooleanValue(true));
  1.1066 +    return newNode(AST_UNARY_EXPR, pos,
  1.1067 +                   "operator", opName,
  1.1068 +                   "argument", expr,
  1.1069 +                   "prefix", trueVal,
  1.1070 +                   dst);
  1.1071 +}
  1.1072 +
  1.1073 +bool
  1.1074 +NodeBuilder::assignmentExpression(AssignmentOperator aop, HandleValue lhs, HandleValue rhs,
  1.1075 +                                  TokenPos *pos, MutableHandleValue dst)
  1.1076 +{
  1.1077 +    JS_ASSERT(aop > AOP_ERR && aop < AOP_LIMIT);
  1.1078 +
  1.1079 +    RootedValue opName(cx);
  1.1080 +    if (!atomValue(aopNames[aop], &opName))
  1.1081 +        return false;
  1.1082 +
  1.1083 +    RootedValue cb(cx, callbacks[AST_ASSIGN_EXPR]);
  1.1084 +    if (!cb.isNull())
  1.1085 +        return callback(cb, opName, lhs, rhs, pos, dst);
  1.1086 +
  1.1087 +    return newNode(AST_ASSIGN_EXPR, pos,
  1.1088 +                   "operator", opName,
  1.1089 +                   "left", lhs,
  1.1090 +                   "right", rhs,
  1.1091 +                   dst);
  1.1092 +}
  1.1093 +
  1.1094 +bool
  1.1095 +NodeBuilder::updateExpression(HandleValue expr, bool incr, bool prefix, TokenPos *pos,
  1.1096 +                              MutableHandleValue dst)
  1.1097 +{
  1.1098 +    RootedValue opName(cx);
  1.1099 +    if (!atomValue(incr ? "++" : "--", &opName))
  1.1100 +        return false;
  1.1101 +
  1.1102 +    RootedValue prefixVal(cx, BooleanValue(prefix));
  1.1103 +
  1.1104 +    RootedValue cb(cx, callbacks[AST_UPDATE_EXPR]);
  1.1105 +    if (!cb.isNull())
  1.1106 +        return callback(cb, expr, opName, prefixVal, pos, dst);
  1.1107 +
  1.1108 +    return newNode(AST_UPDATE_EXPR, pos,
  1.1109 +                   "operator", opName,
  1.1110 +                   "argument", expr,
  1.1111 +                   "prefix", prefixVal,
  1.1112 +                   dst);
  1.1113 +}
  1.1114 +
  1.1115 +bool
  1.1116 +NodeBuilder::logicalExpression(bool lor, HandleValue left, HandleValue right, TokenPos *pos,
  1.1117 +                               MutableHandleValue dst)
  1.1118 +{
  1.1119 +    RootedValue opName(cx);
  1.1120 +    if (!atomValue(lor ? "||" : "&&", &opName))
  1.1121 +        return false;
  1.1122 +
  1.1123 +    RootedValue cb(cx, callbacks[AST_LOGICAL_EXPR]);
  1.1124 +    if (!cb.isNull())
  1.1125 +        return callback(cb, opName, left, right, pos, dst);
  1.1126 +
  1.1127 +    return newNode(AST_LOGICAL_EXPR, pos,
  1.1128 +                   "operator", opName,
  1.1129 +                   "left", left,
  1.1130 +                   "right", right,
  1.1131 +                   dst);
  1.1132 +}
  1.1133 +
  1.1134 +bool
  1.1135 +NodeBuilder::conditionalExpression(HandleValue test, HandleValue cons, HandleValue alt,
  1.1136 +                                   TokenPos *pos, MutableHandleValue dst)
  1.1137 +{
  1.1138 +    RootedValue cb(cx, callbacks[AST_COND_EXPR]);
  1.1139 +    if (!cb.isNull())
  1.1140 +        return callback(cb, test, cons, alt, pos, dst);
  1.1141 +
  1.1142 +    return newNode(AST_COND_EXPR, pos,
  1.1143 +                   "test", test,
  1.1144 +                   "consequent", cons,
  1.1145 +                   "alternate", alt,
  1.1146 +                   dst);
  1.1147 +}
  1.1148 +
  1.1149 +bool
  1.1150 +NodeBuilder::sequenceExpression(NodeVector &elts, TokenPos *pos, MutableHandleValue dst)
  1.1151 +{
  1.1152 +    return listNode(AST_LIST_EXPR, "expressions", elts, pos, dst);
  1.1153 +}
  1.1154 +
  1.1155 +bool
  1.1156 +NodeBuilder::callExpression(HandleValue callee, NodeVector &args, TokenPos *pos,
  1.1157 +                            MutableHandleValue dst)
  1.1158 +{
  1.1159 +    RootedValue array(cx);
  1.1160 +    if (!newArray(args, &array))
  1.1161 +        return false;
  1.1162 +
  1.1163 +    RootedValue cb(cx, callbacks[AST_CALL_EXPR]);
  1.1164 +    if (!cb.isNull())
  1.1165 +        return callback(cb, callee, array, pos, dst);
  1.1166 +
  1.1167 +    return newNode(AST_CALL_EXPR, pos,
  1.1168 +                   "callee", callee,
  1.1169 +                   "arguments", array,
  1.1170 +                   dst);
  1.1171 +}
  1.1172 +
  1.1173 +bool
  1.1174 +NodeBuilder::newExpression(HandleValue callee, NodeVector &args, TokenPos *pos,
  1.1175 +                           MutableHandleValue dst)
  1.1176 +{
  1.1177 +    RootedValue array(cx);
  1.1178 +    if (!newArray(args, &array))
  1.1179 +        return false;
  1.1180 +
  1.1181 +    RootedValue cb(cx, callbacks[AST_NEW_EXPR]);
  1.1182 +    if (!cb.isNull())
  1.1183 +        return callback(cb, callee, array, pos, dst);
  1.1184 +
  1.1185 +    return newNode(AST_NEW_EXPR, pos,
  1.1186 +                   "callee", callee,
  1.1187 +                   "arguments", array,
  1.1188 +                   dst);
  1.1189 +}
  1.1190 +
  1.1191 +bool
  1.1192 +NodeBuilder::memberExpression(bool computed, HandleValue expr, HandleValue member, TokenPos *pos,
  1.1193 +                              MutableHandleValue dst)
  1.1194 +{
  1.1195 +    RootedValue computedVal(cx, BooleanValue(computed));
  1.1196 +
  1.1197 +    RootedValue cb(cx, callbacks[AST_MEMBER_EXPR]);
  1.1198 +    if (!cb.isNull())
  1.1199 +        return callback(cb, computedVal, expr, member, pos, dst);
  1.1200 +
  1.1201 +    return newNode(AST_MEMBER_EXPR, pos,
  1.1202 +                   "object", expr,
  1.1203 +                   "property", member,
  1.1204 +                   "computed", computedVal,
  1.1205 +                   dst);
  1.1206 +}
  1.1207 +
  1.1208 +bool
  1.1209 +NodeBuilder::arrayExpression(NodeVector &elts, TokenPos *pos, MutableHandleValue dst)
  1.1210 +{
  1.1211 +    return listNode(AST_ARRAY_EXPR, "elements", elts, pos, dst);
  1.1212 +}
  1.1213 +
  1.1214 +bool
  1.1215 +NodeBuilder::spreadExpression(HandleValue expr, TokenPos *pos, MutableHandleValue dst)
  1.1216 +{
  1.1217 +    return newNode(AST_SPREAD_EXPR, pos,
  1.1218 +                   "expression", expr,
  1.1219 +                   dst);
  1.1220 +}
  1.1221 +
  1.1222 +bool
  1.1223 +NodeBuilder::propertyPattern(HandleValue key, HandleValue patt, TokenPos *pos,
  1.1224 +                             MutableHandleValue dst)
  1.1225 +{
  1.1226 +    RootedValue kindName(cx);
  1.1227 +    if (!atomValue("init", &kindName))
  1.1228 +        return false;
  1.1229 +
  1.1230 +    RootedValue cb(cx, callbacks[AST_PROP_PATT]);
  1.1231 +    if (!cb.isNull())
  1.1232 +        return callback(cb, key, patt, pos, dst);
  1.1233 +
  1.1234 +    return newNode(AST_PROP_PATT, pos,
  1.1235 +                   "key", key,
  1.1236 +                   "value", patt,
  1.1237 +                   "kind", kindName,
  1.1238 +                   dst);
  1.1239 +}
  1.1240 +
  1.1241 +bool
  1.1242 +NodeBuilder::propertyInitializer(HandleValue key, HandleValue val, PropKind kind, TokenPos *pos,
  1.1243 +                                 MutableHandleValue dst)
  1.1244 +{
  1.1245 +    RootedValue kindName(cx);
  1.1246 +    if (!atomValue(kind == PROP_INIT
  1.1247 +                   ? "init"
  1.1248 +                   : kind == PROP_GETTER
  1.1249 +                   ? "get"
  1.1250 +                   : "set", &kindName)) {
  1.1251 +        return false;
  1.1252 +    }
  1.1253 +
  1.1254 +    RootedValue cb(cx, callbacks[AST_PROPERTY]);
  1.1255 +    if (!cb.isNull())
  1.1256 +        return callback(cb, kindName, key, val, pos, dst);
  1.1257 +
  1.1258 +    return newNode(AST_PROPERTY, pos,
  1.1259 +                   "key", key,
  1.1260 +                   "value", val,
  1.1261 +                   "kind", kindName,
  1.1262 +                   dst);
  1.1263 +}
  1.1264 +
  1.1265 +bool
  1.1266 +NodeBuilder::objectExpression(NodeVector &elts, TokenPos *pos, MutableHandleValue dst)
  1.1267 +{
  1.1268 +    return listNode(AST_OBJECT_EXPR, "properties", elts, pos, dst);
  1.1269 +}
  1.1270 +
  1.1271 +bool
  1.1272 +NodeBuilder::thisExpression(TokenPos *pos, MutableHandleValue dst)
  1.1273 +{
  1.1274 +    RootedValue cb(cx, callbacks[AST_THIS_EXPR]);
  1.1275 +    if (!cb.isNull())
  1.1276 +        return callback(cb, pos, dst);
  1.1277 +
  1.1278 +    return newNode(AST_THIS_EXPR, pos, dst);
  1.1279 +}
  1.1280 +
  1.1281 +bool
  1.1282 +NodeBuilder::yieldExpression(HandleValue arg, YieldKind kind, TokenPos *pos, MutableHandleValue dst)
  1.1283 +{
  1.1284 +    RootedValue cb(cx, callbacks[AST_YIELD_EXPR]);
  1.1285 +    RootedValue delegateVal(cx);
  1.1286 +
  1.1287 +    switch (kind) {
  1.1288 +      case Delegating:
  1.1289 +        delegateVal = BooleanValue(true);
  1.1290 +        break;
  1.1291 +      case NotDelegating:
  1.1292 +        delegateVal = BooleanValue(false);
  1.1293 +        break;
  1.1294 +    }
  1.1295 +
  1.1296 +    if (!cb.isNull())
  1.1297 +        return callback(cb, opt(arg), delegateVal, pos, dst);
  1.1298 +    return newNode(AST_YIELD_EXPR, pos, "argument", arg, "delegate", delegateVal, dst);
  1.1299 +}
  1.1300 +
  1.1301 +bool
  1.1302 +NodeBuilder::comprehensionBlock(HandleValue patt, HandleValue src, bool isForEach, bool isForOf, TokenPos *pos,
  1.1303 +                                MutableHandleValue dst)
  1.1304 +{
  1.1305 +    RootedValue isForEachVal(cx, BooleanValue(isForEach));
  1.1306 +    RootedValue isForOfVal(cx, BooleanValue(isForOf));
  1.1307 +
  1.1308 +    RootedValue cb(cx, callbacks[AST_COMP_BLOCK]);
  1.1309 +    if (!cb.isNull())
  1.1310 +        return callback(cb, patt, src, isForEachVal, isForOfVal, pos, dst);
  1.1311 +
  1.1312 +    return newNode(AST_COMP_BLOCK, pos,
  1.1313 +                   "left", patt,
  1.1314 +                   "right", src,
  1.1315 +                   "each", isForEachVal,
  1.1316 +                   "of", isForOfVal,
  1.1317 +                   dst);
  1.1318 +}
  1.1319 +
  1.1320 +bool
  1.1321 +NodeBuilder::comprehensionExpression(HandleValue body, NodeVector &blocks, HandleValue filter,
  1.1322 +                                     TokenPos *pos, MutableHandleValue dst)
  1.1323 +{
  1.1324 +    RootedValue array(cx);
  1.1325 +    if (!newArray(blocks, &array))
  1.1326 +        return false;
  1.1327 +
  1.1328 +    RootedValue cb(cx, callbacks[AST_COMP_EXPR]);
  1.1329 +    if (!cb.isNull())
  1.1330 +        return callback(cb, body, array, opt(filter), pos, dst);
  1.1331 +
  1.1332 +    return newNode(AST_COMP_EXPR, pos,
  1.1333 +                   "body", body,
  1.1334 +                   "blocks", array,
  1.1335 +                   "filter", filter,
  1.1336 +                   dst);
  1.1337 +}
  1.1338 +
  1.1339 +bool
  1.1340 +NodeBuilder::generatorExpression(HandleValue body, NodeVector &blocks, HandleValue filter,
  1.1341 +                                 TokenPos *pos, MutableHandleValue dst)
  1.1342 +{
  1.1343 +    RootedValue array(cx);
  1.1344 +    if (!newArray(blocks, &array))
  1.1345 +        return false;
  1.1346 +
  1.1347 +    RootedValue cb(cx, callbacks[AST_GENERATOR_EXPR]);
  1.1348 +    if (!cb.isNull())
  1.1349 +        return callback(cb, body, array, opt(filter), pos, dst);
  1.1350 +
  1.1351 +    return newNode(AST_GENERATOR_EXPR, pos,
  1.1352 +                   "body", body,
  1.1353 +                   "blocks", array,
  1.1354 +                   "filter", filter,
  1.1355 +                   dst);
  1.1356 +}
  1.1357 +
  1.1358 +bool
  1.1359 +NodeBuilder::letExpression(NodeVector &head, HandleValue expr, TokenPos *pos,
  1.1360 +                           MutableHandleValue dst)
  1.1361 +{
  1.1362 +    RootedValue array(cx);
  1.1363 +    if (!newArray(head, &array))
  1.1364 +        return false;
  1.1365 +
  1.1366 +    RootedValue cb(cx, callbacks[AST_LET_EXPR]);
  1.1367 +    if (!cb.isNull())
  1.1368 +        return callback(cb, array, expr, pos, dst);
  1.1369 +
  1.1370 +    return newNode(AST_LET_EXPR, pos,
  1.1371 +                   "head", array,
  1.1372 +                   "body", expr,
  1.1373 +                   dst);
  1.1374 +}
  1.1375 +
  1.1376 +bool
  1.1377 +NodeBuilder::letStatement(NodeVector &head, HandleValue stmt, TokenPos *pos, MutableHandleValue dst)
  1.1378 +{
  1.1379 +    RootedValue array(cx);
  1.1380 +    if (!newArray(head, &array))
  1.1381 +        return false;
  1.1382 +
  1.1383 +    RootedValue cb(cx, callbacks[AST_LET_STMT]);
  1.1384 +    if (!cb.isNull())
  1.1385 +        return callback(cb, array, stmt, pos, dst);
  1.1386 +
  1.1387 +    return newNode(AST_LET_STMT, pos,
  1.1388 +                   "head", array,
  1.1389 +                   "body", stmt,
  1.1390 +                   dst);
  1.1391 +}
  1.1392 +
  1.1393 +bool
  1.1394 +NodeBuilder::importDeclaration(NodeVector &elts, HandleValue moduleSpec, TokenPos *pos,
  1.1395 +                               MutableHandleValue dst)
  1.1396 +{
  1.1397 +    RootedValue array(cx);
  1.1398 +    if (!newArray(elts, &array))
  1.1399 +        return false;
  1.1400 +
  1.1401 +    RootedValue cb(cx, callbacks[AST_IMPORT_DECL]);
  1.1402 +    if (!cb.isNull())
  1.1403 +        return callback(cb, array, moduleSpec, pos, dst);
  1.1404 +
  1.1405 +    return newNode(AST_IMPORT_DECL, pos,
  1.1406 +                   "specifiers", array,
  1.1407 +                   "source", moduleSpec,
  1.1408 +                   dst);
  1.1409 +}
  1.1410 +
  1.1411 +bool
  1.1412 +NodeBuilder::importSpecifier(HandleValue importName, HandleValue bindingName, TokenPos *pos,
  1.1413 +                             MutableHandleValue dst)
  1.1414 +{
  1.1415 +    RootedValue cb(cx, callbacks[AST_IMPORT_SPEC]);
  1.1416 +    if (!cb.isNull())
  1.1417 +        return callback(cb, importName, bindingName, pos, dst);
  1.1418 +
  1.1419 +    return newNode(AST_IMPORT_SPEC, pos,
  1.1420 +                   "id", importName,
  1.1421 +                   "name", bindingName,
  1.1422 +                   dst);
  1.1423 +}
  1.1424 +
  1.1425 +bool
  1.1426 +NodeBuilder::exportDeclaration(HandleValue decl, NodeVector &elts, HandleValue moduleSpec,
  1.1427 +                               TokenPos *pos, MutableHandleValue dst)
  1.1428 +{
  1.1429 +    RootedValue array(cx, NullValue());
  1.1430 +    if (decl.isNull() && !newArray(elts, &array))
  1.1431 +        return false;
  1.1432 +
  1.1433 +    RootedValue cb(cx, callbacks[AST_IMPORT_DECL]);
  1.1434 +
  1.1435 +    if (!cb.isNull())
  1.1436 +        return callback(cb, decl, array, moduleSpec, pos, dst);
  1.1437 +
  1.1438 +    return newNode(AST_EXPORT_DECL, pos,
  1.1439 +                   "declaration", decl,
  1.1440 +                   "specifiers", array,
  1.1441 +                   "source", moduleSpec,
  1.1442 +                   dst);
  1.1443 +}
  1.1444 +
  1.1445 +bool
  1.1446 +NodeBuilder::exportSpecifier(HandleValue bindingName, HandleValue exportName, TokenPos *pos,
  1.1447 +                             MutableHandleValue dst)
  1.1448 +{
  1.1449 +    RootedValue cb(cx, callbacks[AST_EXPORT_SPEC]);
  1.1450 +    if (!cb.isNull())
  1.1451 +        return callback(cb, bindingName, exportName, pos, dst);
  1.1452 +
  1.1453 +    return newNode(AST_EXPORT_SPEC, pos,
  1.1454 +                   "id", bindingName,
  1.1455 +                   "name", exportName,
  1.1456 +                   dst);
  1.1457 +}
  1.1458 +
  1.1459 +bool
  1.1460 +NodeBuilder::exportBatchSpecifier(TokenPos *pos, MutableHandleValue dst)
  1.1461 +{
  1.1462 +    RootedValue cb(cx, callbacks[AST_EXPORT_BATCH_SPEC]);
  1.1463 +    if (!cb.isNull())
  1.1464 +        return callback(cb, pos, dst);
  1.1465 +
  1.1466 +    return newNode(AST_EXPORT_BATCH_SPEC, pos, dst);
  1.1467 +}
  1.1468 +
  1.1469 +bool
  1.1470 +NodeBuilder::variableDeclaration(NodeVector &elts, VarDeclKind kind, TokenPos *pos,
  1.1471 +                                 MutableHandleValue dst)
  1.1472 +{
  1.1473 +    JS_ASSERT(kind > VARDECL_ERR && kind < VARDECL_LIMIT);
  1.1474 +
  1.1475 +    RootedValue array(cx), kindName(cx);
  1.1476 +    if (!newArray(elts, &array) ||
  1.1477 +        !atomValue(kind == VARDECL_CONST
  1.1478 +                   ? "const"
  1.1479 +                   : kind == VARDECL_LET
  1.1480 +                   ? "let"
  1.1481 +                   : "var", &kindName)) {
  1.1482 +        return false;
  1.1483 +    }
  1.1484 +
  1.1485 +    RootedValue cb(cx, callbacks[AST_VAR_DECL]);
  1.1486 +    if (!cb.isNull())
  1.1487 +        return callback(cb, kindName, array, pos, dst);
  1.1488 +
  1.1489 +    return newNode(AST_VAR_DECL, pos,
  1.1490 +                   "kind", kindName,
  1.1491 +                   "declarations", array,
  1.1492 +                   dst);
  1.1493 +}
  1.1494 +
  1.1495 +bool
  1.1496 +NodeBuilder::variableDeclarator(HandleValue id, HandleValue init, TokenPos *pos,
  1.1497 +                                MutableHandleValue dst)
  1.1498 +{
  1.1499 +    RootedValue cb(cx, callbacks[AST_VAR_DTOR]);
  1.1500 +    if (!cb.isNull())
  1.1501 +        return callback(cb, id, opt(init), pos, dst);
  1.1502 +
  1.1503 +    return newNode(AST_VAR_DTOR, pos, "id", id, "init", init, dst);
  1.1504 +}
  1.1505 +
  1.1506 +bool
  1.1507 +NodeBuilder::switchCase(HandleValue expr, NodeVector &elts, TokenPos *pos, MutableHandleValue dst)
  1.1508 +{
  1.1509 +    RootedValue array(cx);
  1.1510 +    if (!newArray(elts, &array))
  1.1511 +        return false;
  1.1512 +
  1.1513 +    RootedValue cb(cx, callbacks[AST_CASE]);
  1.1514 +    if (!cb.isNull())
  1.1515 +        return callback(cb, opt(expr), array, pos, dst);
  1.1516 +
  1.1517 +    return newNode(AST_CASE, pos,
  1.1518 +                   "test", expr,
  1.1519 +                   "consequent", array,
  1.1520 +                   dst);
  1.1521 +}
  1.1522 +
  1.1523 +bool
  1.1524 +NodeBuilder::catchClause(HandleValue var, HandleValue guard, HandleValue body, TokenPos *pos,
  1.1525 +                         MutableHandleValue dst)
  1.1526 +{
  1.1527 +    RootedValue cb(cx, callbacks[AST_CATCH]);
  1.1528 +    if (!cb.isNull())
  1.1529 +        return callback(cb, var, opt(guard), body, pos, dst);
  1.1530 +
  1.1531 +    return newNode(AST_CATCH, pos,
  1.1532 +                   "param", var,
  1.1533 +                   "guard", guard,
  1.1534 +                   "body", body,
  1.1535 +                   dst);
  1.1536 +}
  1.1537 +
  1.1538 +bool
  1.1539 +NodeBuilder::literal(HandleValue val, TokenPos *pos, MutableHandleValue dst)
  1.1540 +{
  1.1541 +    RootedValue cb(cx, callbacks[AST_LITERAL]);
  1.1542 +    if (!cb.isNull())
  1.1543 +        return callback(cb, val, pos, dst);
  1.1544 +
  1.1545 +    return newNode(AST_LITERAL, pos, "value", val, dst);
  1.1546 +}
  1.1547 +
  1.1548 +bool
  1.1549 +NodeBuilder::identifier(HandleValue name, TokenPos *pos, MutableHandleValue dst)
  1.1550 +{
  1.1551 +    RootedValue cb(cx, callbacks[AST_IDENTIFIER]);
  1.1552 +    if (!cb.isNull())
  1.1553 +        return callback(cb, name, pos, dst);
  1.1554 +
  1.1555 +    return newNode(AST_IDENTIFIER, pos, "name", name, dst);
  1.1556 +}
  1.1557 +
  1.1558 +bool
  1.1559 +NodeBuilder::objectPattern(NodeVector &elts, TokenPos *pos, MutableHandleValue dst)
  1.1560 +{
  1.1561 +    return listNode(AST_OBJECT_PATT, "properties", elts, pos, dst);
  1.1562 +}
  1.1563 +
  1.1564 +bool
  1.1565 +NodeBuilder::arrayPattern(NodeVector &elts, TokenPos *pos, MutableHandleValue dst)
  1.1566 +{
  1.1567 +    return listNode(AST_ARRAY_PATT, "elements", elts, pos, dst);
  1.1568 +}
  1.1569 +
  1.1570 +bool
  1.1571 +NodeBuilder::function(ASTType type, TokenPos *pos,
  1.1572 +                      HandleValue id, NodeVector &args, NodeVector &defaults,
  1.1573 +                      HandleValue body, HandleValue rest,
  1.1574 +                      bool isGenerator, bool isExpression,
  1.1575 +                      MutableHandleValue dst)
  1.1576 +{
  1.1577 +    RootedValue array(cx), defarray(cx);
  1.1578 +    if (!newArray(args, &array))
  1.1579 +        return false;
  1.1580 +    if (!newArray(defaults, &defarray))
  1.1581 +        return false;
  1.1582 +
  1.1583 +    RootedValue isGeneratorVal(cx, BooleanValue(isGenerator));
  1.1584 +    RootedValue isExpressionVal(cx, BooleanValue(isExpression));
  1.1585 +
  1.1586 +    RootedValue cb(cx, callbacks[type]);
  1.1587 +    if (!cb.isNull()) {
  1.1588 +        return callback(cb, opt(id), array, body, isGeneratorVal, isExpressionVal, pos, dst);
  1.1589 +    }
  1.1590 +
  1.1591 +    return newNode(type, pos,
  1.1592 +                   "id", id,
  1.1593 +                   "params", array,
  1.1594 +                   "defaults", defarray,
  1.1595 +                   "body", body,
  1.1596 +                   "rest", rest,
  1.1597 +                   "generator", isGeneratorVal,
  1.1598 +                   "expression", isExpressionVal,
  1.1599 +                   dst);
  1.1600 +}
  1.1601 +
  1.1602 +namespace {
  1.1603 +
  1.1604 +/*
  1.1605 + * Serialization of parse nodes to JavaScript objects.
  1.1606 + *
  1.1607 + * All serialization methods take a non-nullable ParseNode pointer.
  1.1608 + */
  1.1609 +class ASTSerializer
  1.1610 +{
  1.1611 +    JSContext           *cx;
  1.1612 +    Parser<FullParseHandler> *parser;
  1.1613 +    NodeBuilder         builder;
  1.1614 +    DebugOnly<uint32_t> lineno;
  1.1615 +
  1.1616 +    Value unrootedAtomContents(JSAtom *atom) {
  1.1617 +        return StringValue(atom ? atom : cx->names().empty);
  1.1618 +    }
  1.1619 +
  1.1620 +    BinaryOperator binop(ParseNodeKind kind, JSOp op);
  1.1621 +    UnaryOperator unop(ParseNodeKind kind, JSOp op);
  1.1622 +    AssignmentOperator aop(JSOp op);
  1.1623 +
  1.1624 +    bool statements(ParseNode *pn, NodeVector &elts);
  1.1625 +    bool expressions(ParseNode *pn, NodeVector &elts);
  1.1626 +    bool leftAssociate(ParseNode *pn, MutableHandleValue dst);
  1.1627 +    bool functionArgs(ParseNode *pn, ParseNode *pnargs, ParseNode *pndestruct, ParseNode *pnbody,
  1.1628 +                      NodeVector &args, NodeVector &defaults, MutableHandleValue rest);
  1.1629 +
  1.1630 +    bool sourceElement(ParseNode *pn, MutableHandleValue dst);
  1.1631 +
  1.1632 +    bool declaration(ParseNode *pn, MutableHandleValue dst);
  1.1633 +    bool variableDeclaration(ParseNode *pn, bool let, MutableHandleValue dst);
  1.1634 +    bool variableDeclarator(ParseNode *pn, VarDeclKind *pkind, MutableHandleValue dst);
  1.1635 +    bool let(ParseNode *pn, bool expr, MutableHandleValue dst);
  1.1636 +    bool importDeclaration(ParseNode *pn, MutableHandleValue dst);
  1.1637 +    bool importSpecifier(ParseNode *pn, MutableHandleValue dst);
  1.1638 +    bool exportDeclaration(ParseNode *pn, MutableHandleValue dst);
  1.1639 +    bool exportSpecifier(ParseNode *pn, MutableHandleValue dst);
  1.1640 +
  1.1641 +    bool optStatement(ParseNode *pn, MutableHandleValue dst) {
  1.1642 +        if (!pn) {
  1.1643 +            dst.setMagic(JS_SERIALIZE_NO_NODE);
  1.1644 +            return true;
  1.1645 +        }
  1.1646 +        return statement(pn, dst);
  1.1647 +    }
  1.1648 +
  1.1649 +    bool forInit(ParseNode *pn, MutableHandleValue dst);
  1.1650 +    bool forIn(ParseNode *loop, ParseNode *head, HandleValue var, HandleValue stmt,
  1.1651 +               MutableHandleValue dst);
  1.1652 +    bool forOf(ParseNode *loop, ParseNode *head, HandleValue var, HandleValue stmt,
  1.1653 +               MutableHandleValue dst);
  1.1654 +    bool statement(ParseNode *pn, MutableHandleValue dst);
  1.1655 +    bool blockStatement(ParseNode *pn, MutableHandleValue dst);
  1.1656 +    bool switchStatement(ParseNode *pn, MutableHandleValue dst);
  1.1657 +    bool switchCase(ParseNode *pn, MutableHandleValue dst);
  1.1658 +    bool tryStatement(ParseNode *pn, MutableHandleValue dst);
  1.1659 +    bool catchClause(ParseNode *pn, bool *isGuarded, MutableHandleValue dst);
  1.1660 +
  1.1661 +    bool optExpression(ParseNode *pn, MutableHandleValue dst) {
  1.1662 +        if (!pn) {
  1.1663 +            dst.setMagic(JS_SERIALIZE_NO_NODE);
  1.1664 +            return true;
  1.1665 +        }
  1.1666 +        return expression(pn, dst);
  1.1667 +    }
  1.1668 +
  1.1669 +    bool expression(ParseNode *pn, MutableHandleValue dst);
  1.1670 +
  1.1671 +    bool propertyName(ParseNode *pn, MutableHandleValue dst);
  1.1672 +    bool property(ParseNode *pn, MutableHandleValue dst);
  1.1673 +
  1.1674 +    bool optIdentifier(HandleAtom atom, TokenPos *pos, MutableHandleValue dst) {
  1.1675 +        if (!atom) {
  1.1676 +            dst.setMagic(JS_SERIALIZE_NO_NODE);
  1.1677 +            return true;
  1.1678 +        }
  1.1679 +        return identifier(atom, pos, dst);
  1.1680 +    }
  1.1681 +
  1.1682 +    bool identifier(HandleAtom atom, TokenPos *pos, MutableHandleValue dst);
  1.1683 +    bool identifier(ParseNode *pn, MutableHandleValue dst);
  1.1684 +    bool literal(ParseNode *pn, MutableHandleValue dst);
  1.1685 +
  1.1686 +    bool pattern(ParseNode *pn, VarDeclKind *pkind, MutableHandleValue dst);
  1.1687 +    bool arrayPattern(ParseNode *pn, VarDeclKind *pkind, MutableHandleValue dst);
  1.1688 +    bool objectPattern(ParseNode *pn, VarDeclKind *pkind, MutableHandleValue dst);
  1.1689 +
  1.1690 +    bool function(ParseNode *pn, ASTType type, MutableHandleValue dst);
  1.1691 +    bool functionArgsAndBody(ParseNode *pn, NodeVector &args, NodeVector &defaults,
  1.1692 +                             MutableHandleValue body, MutableHandleValue rest);
  1.1693 +    bool functionBody(ParseNode *pn, TokenPos *pos, MutableHandleValue dst);
  1.1694 +
  1.1695 +    bool comprehensionBlock(ParseNode *pn, MutableHandleValue dst);
  1.1696 +    bool comprehension(ParseNode *pn, MutableHandleValue dst);
  1.1697 +    bool generatorExpression(ParseNode *pn, MutableHandleValue dst);
  1.1698 +
  1.1699 +  public:
  1.1700 +    ASTSerializer(JSContext *c, bool l, char const *src, uint32_t ln)
  1.1701 +        : cx(c)
  1.1702 +        , builder(c, l, src)
  1.1703 +#ifdef DEBUG
  1.1704 +        , lineno(ln)
  1.1705 +#endif
  1.1706 +    {}
  1.1707 +
  1.1708 +    bool init(HandleObject userobj) {
  1.1709 +        return builder.init(userobj);
  1.1710 +    }
  1.1711 +
  1.1712 +    void setParser(Parser<FullParseHandler> *p) {
  1.1713 +        parser = p;
  1.1714 +        builder.setTokenStream(&p->tokenStream);
  1.1715 +    }
  1.1716 +
  1.1717 +    bool program(ParseNode *pn, MutableHandleValue dst);
  1.1718 +};
  1.1719 +
  1.1720 +} /* anonymous namespace */
  1.1721 +
  1.1722 +AssignmentOperator
  1.1723 +ASTSerializer::aop(JSOp op)
  1.1724 +{
  1.1725 +    switch (op) {
  1.1726 +      case JSOP_NOP:
  1.1727 +        return AOP_ASSIGN;
  1.1728 +      case JSOP_ADD:
  1.1729 +        return AOP_PLUS;
  1.1730 +      case JSOP_SUB:
  1.1731 +        return AOP_MINUS;
  1.1732 +      case JSOP_MUL:
  1.1733 +        return AOP_STAR;
  1.1734 +      case JSOP_DIV:
  1.1735 +        return AOP_DIV;
  1.1736 +      case JSOP_MOD:
  1.1737 +        return AOP_MOD;
  1.1738 +      case JSOP_LSH:
  1.1739 +        return AOP_LSH;
  1.1740 +      case JSOP_RSH:
  1.1741 +        return AOP_RSH;
  1.1742 +      case JSOP_URSH:
  1.1743 +        return AOP_URSH;
  1.1744 +      case JSOP_BITOR:
  1.1745 +        return AOP_BITOR;
  1.1746 +      case JSOP_BITXOR:
  1.1747 +        return AOP_BITXOR;
  1.1748 +      case JSOP_BITAND:
  1.1749 +        return AOP_BITAND;
  1.1750 +      default:
  1.1751 +        return AOP_ERR;
  1.1752 +    }
  1.1753 +}
  1.1754 +
  1.1755 +UnaryOperator
  1.1756 +ASTSerializer::unop(ParseNodeKind kind, JSOp op)
  1.1757 +{
  1.1758 +    if (kind == PNK_DELETE)
  1.1759 +        return UNOP_DELETE;
  1.1760 +
  1.1761 +    switch (op) {
  1.1762 +      case JSOP_NEG:
  1.1763 +        return UNOP_NEG;
  1.1764 +      case JSOP_POS:
  1.1765 +        return UNOP_POS;
  1.1766 +      case JSOP_NOT:
  1.1767 +        return UNOP_NOT;
  1.1768 +      case JSOP_BITNOT:
  1.1769 +        return UNOP_BITNOT;
  1.1770 +      case JSOP_TYPEOF:
  1.1771 +      case JSOP_TYPEOFEXPR:
  1.1772 +        return UNOP_TYPEOF;
  1.1773 +      case JSOP_VOID:
  1.1774 +        return UNOP_VOID;
  1.1775 +      default:
  1.1776 +        return UNOP_ERR;
  1.1777 +    }
  1.1778 +}
  1.1779 +
  1.1780 +BinaryOperator
  1.1781 +ASTSerializer::binop(ParseNodeKind kind, JSOp op)
  1.1782 +{
  1.1783 +    switch (kind) {
  1.1784 +      case PNK_LSH:
  1.1785 +        return BINOP_LSH;
  1.1786 +      case PNK_RSH:
  1.1787 +        return BINOP_RSH;
  1.1788 +      case PNK_URSH:
  1.1789 +        return BINOP_URSH;
  1.1790 +      case PNK_LT:
  1.1791 +        return BINOP_LT;
  1.1792 +      case PNK_LE:
  1.1793 +        return BINOP_LE;
  1.1794 +      case PNK_GT:
  1.1795 +        return BINOP_GT;
  1.1796 +      case PNK_GE:
  1.1797 +        return BINOP_GE;
  1.1798 +      case PNK_EQ:
  1.1799 +        return BINOP_EQ;
  1.1800 +      case PNK_NE:
  1.1801 +        return BINOP_NE;
  1.1802 +      case PNK_STRICTEQ:
  1.1803 +        return BINOP_STRICTEQ;
  1.1804 +      case PNK_STRICTNE:
  1.1805 +        return BINOP_STRICTNE;
  1.1806 +      case PNK_ADD:
  1.1807 +        return BINOP_ADD;
  1.1808 +      case PNK_SUB:
  1.1809 +        return BINOP_SUB;
  1.1810 +      case PNK_STAR:
  1.1811 +        return BINOP_STAR;
  1.1812 +      case PNK_DIV:
  1.1813 +        return BINOP_DIV;
  1.1814 +      case PNK_MOD:
  1.1815 +        return BINOP_MOD;
  1.1816 +      case PNK_BITOR:
  1.1817 +        return BINOP_BITOR;
  1.1818 +      case PNK_BITXOR:
  1.1819 +        return BINOP_BITXOR;
  1.1820 +      case PNK_BITAND:
  1.1821 +        return BINOP_BITAND;
  1.1822 +      case PNK_IN:
  1.1823 +        return BINOP_IN;
  1.1824 +      case PNK_INSTANCEOF:
  1.1825 +        return BINOP_INSTANCEOF;
  1.1826 +      default:
  1.1827 +        return BINOP_ERR;
  1.1828 +    }
  1.1829 +}
  1.1830 +
  1.1831 +bool
  1.1832 +ASTSerializer::statements(ParseNode *pn, NodeVector &elts)
  1.1833 +{
  1.1834 +    JS_ASSERT(pn->isKind(PNK_STATEMENTLIST));
  1.1835 +    JS_ASSERT(pn->isArity(PN_LIST));
  1.1836 +
  1.1837 +    if (!elts.reserve(pn->pn_count))
  1.1838 +        return false;
  1.1839 +
  1.1840 +    for (ParseNode *next = pn->pn_head; next; next = next->pn_next) {
  1.1841 +        JS_ASSERT(pn->pn_pos.encloses(next->pn_pos));
  1.1842 +
  1.1843 +        RootedValue elt(cx);
  1.1844 +        if (!sourceElement(next, &elt))
  1.1845 +            return false;
  1.1846 +        elts.infallibleAppend(elt);
  1.1847 +    }
  1.1848 +
  1.1849 +    return true;
  1.1850 +}
  1.1851 +
  1.1852 +bool
  1.1853 +ASTSerializer::expressions(ParseNode *pn, NodeVector &elts)
  1.1854 +{
  1.1855 +    if (!elts.reserve(pn->pn_count))
  1.1856 +        return false;
  1.1857 +
  1.1858 +    for (ParseNode *next = pn->pn_head; next; next = next->pn_next) {
  1.1859 +        JS_ASSERT(pn->pn_pos.encloses(next->pn_pos));
  1.1860 +
  1.1861 +        RootedValue elt(cx);
  1.1862 +        if (!expression(next, &elt))
  1.1863 +            return false;
  1.1864 +        elts.infallibleAppend(elt);
  1.1865 +    }
  1.1866 +
  1.1867 +    return true;
  1.1868 +}
  1.1869 +
  1.1870 +bool
  1.1871 +ASTSerializer::blockStatement(ParseNode *pn, MutableHandleValue dst)
  1.1872 +{
  1.1873 +    JS_ASSERT(pn->isKind(PNK_STATEMENTLIST));
  1.1874 +
  1.1875 +    NodeVector stmts(cx);
  1.1876 +    return statements(pn, stmts) &&
  1.1877 +           builder.blockStatement(stmts, &pn->pn_pos, dst);
  1.1878 +}
  1.1879 +
  1.1880 +bool
  1.1881 +ASTSerializer::program(ParseNode *pn, MutableHandleValue dst)
  1.1882 +{
  1.1883 +    JS_ASSERT(parser->tokenStream.srcCoords.lineNum(pn->pn_pos.begin) == lineno);
  1.1884 +
  1.1885 +    NodeVector stmts(cx);
  1.1886 +    return statements(pn, stmts) &&
  1.1887 +           builder.program(stmts, &pn->pn_pos, dst);
  1.1888 +}
  1.1889 +
  1.1890 +bool
  1.1891 +ASTSerializer::sourceElement(ParseNode *pn, MutableHandleValue dst)
  1.1892 +{
  1.1893 +    /* SpiderMonkey allows declarations even in pure statement contexts. */
  1.1894 +    return statement(pn, dst);
  1.1895 +}
  1.1896 +
  1.1897 +bool
  1.1898 +ASTSerializer::declaration(ParseNode *pn, MutableHandleValue dst)
  1.1899 +{
  1.1900 +    JS_ASSERT(pn->isKind(PNK_FUNCTION) ||
  1.1901 +              pn->isKind(PNK_VAR) ||
  1.1902 +              pn->isKind(PNK_LET) ||
  1.1903 +              pn->isKind(PNK_CONST));
  1.1904 +
  1.1905 +    switch (pn->getKind()) {
  1.1906 +      case PNK_FUNCTION:
  1.1907 +        return function(pn, AST_FUNC_DECL, dst);
  1.1908 +
  1.1909 +      case PNK_VAR:
  1.1910 +      case PNK_CONST:
  1.1911 +        return variableDeclaration(pn, false, dst);
  1.1912 +
  1.1913 +      default:
  1.1914 +        JS_ASSERT(pn->isKind(PNK_LET));
  1.1915 +        return variableDeclaration(pn, true, dst);
  1.1916 +    }
  1.1917 +}
  1.1918 +
  1.1919 +bool
  1.1920 +ASTSerializer::variableDeclaration(ParseNode *pn, bool let, MutableHandleValue dst)
  1.1921 +{
  1.1922 +    JS_ASSERT(let ? pn->isKind(PNK_LET) : (pn->isKind(PNK_VAR) || pn->isKind(PNK_CONST)));
  1.1923 +
  1.1924 +    /* Later updated to VARDECL_CONST if we find a PND_CONST declarator. */
  1.1925 +    VarDeclKind kind = let ? VARDECL_LET : VARDECL_VAR;
  1.1926 +
  1.1927 +    NodeVector dtors(cx);
  1.1928 +    if (!dtors.reserve(pn->pn_count))
  1.1929 +        return false;
  1.1930 +    for (ParseNode *next = pn->pn_head; next; next = next->pn_next) {
  1.1931 +        RootedValue child(cx);
  1.1932 +        if (!variableDeclarator(next, &kind, &child))
  1.1933 +            return false;
  1.1934 +        dtors.infallibleAppend(child);
  1.1935 +    }
  1.1936 +    return builder.variableDeclaration(dtors, kind, &pn->pn_pos, dst);
  1.1937 +}
  1.1938 +
  1.1939 +bool
  1.1940 +ASTSerializer::variableDeclarator(ParseNode *pn, VarDeclKind *pkind, MutableHandleValue dst)
  1.1941 +{
  1.1942 +    ParseNode *pnleft;
  1.1943 +    ParseNode *pnright;
  1.1944 +
  1.1945 +    if (pn->isKind(PNK_NAME)) {
  1.1946 +        pnleft = pn;
  1.1947 +        pnright = pn->isUsed() ? nullptr : pn->pn_expr;
  1.1948 +        JS_ASSERT_IF(pnright, pn->pn_pos.encloses(pnright->pn_pos));
  1.1949 +    } else if (pn->isKind(PNK_ASSIGN)) {
  1.1950 +        pnleft = pn->pn_left;
  1.1951 +        pnright = pn->pn_right;
  1.1952 +        JS_ASSERT(pn->pn_pos.encloses(pnleft->pn_pos));
  1.1953 +        JS_ASSERT(pn->pn_pos.encloses(pnright->pn_pos));
  1.1954 +    } else {
  1.1955 +        /* This happens for a destructuring declarator in a for-in/of loop. */
  1.1956 +        pnleft = pn;
  1.1957 +        pnright = nullptr;
  1.1958 +    }
  1.1959 +
  1.1960 +    RootedValue left(cx), right(cx);
  1.1961 +    return pattern(pnleft, pkind, &left) &&
  1.1962 +           optExpression(pnright, &right) &&
  1.1963 +           builder.variableDeclarator(left, right, &pn->pn_pos, dst);
  1.1964 +}
  1.1965 +
  1.1966 +bool
  1.1967 +ASTSerializer::let(ParseNode *pn, bool expr, MutableHandleValue dst)
  1.1968 +{
  1.1969 +    JS_ASSERT(pn->pn_pos.encloses(pn->pn_left->pn_pos));
  1.1970 +    JS_ASSERT(pn->pn_pos.encloses(pn->pn_right->pn_pos));
  1.1971 +
  1.1972 +    ParseNode *letHead = pn->pn_left;
  1.1973 +    LOCAL_ASSERT(letHead->isArity(PN_LIST));
  1.1974 +
  1.1975 +    ParseNode *letBody = pn->pn_right;
  1.1976 +    LOCAL_ASSERT(letBody->isKind(PNK_LEXICALSCOPE));
  1.1977 +
  1.1978 +    NodeVector dtors(cx);
  1.1979 +    if (!dtors.reserve(letHead->pn_count))
  1.1980 +        return false;
  1.1981 +
  1.1982 +    VarDeclKind kind = VARDECL_LET_HEAD;
  1.1983 +
  1.1984 +    for (ParseNode *next = letHead->pn_head; next; next = next->pn_next) {
  1.1985 +        RootedValue child(cx);
  1.1986 +        /*
  1.1987 +         * Unlike in |variableDeclaration|, this does not update |kind|; since let-heads do
  1.1988 +         * not contain const declarations, declarators should never have PND_CONST set.
  1.1989 +         */
  1.1990 +        if (!variableDeclarator(next, &kind, &child))
  1.1991 +            return false;
  1.1992 +        dtors.infallibleAppend(child);
  1.1993 +    }
  1.1994 +
  1.1995 +    RootedValue v(cx);
  1.1996 +    return expr
  1.1997 +           ? expression(letBody->pn_expr, &v) &&
  1.1998 +             builder.letExpression(dtors, v, &pn->pn_pos, dst)
  1.1999 +           : statement(letBody->pn_expr, &v) &&
  1.2000 +             builder.letStatement(dtors, v, &pn->pn_pos, dst);
  1.2001 +}
  1.2002 +
  1.2003 +bool
  1.2004 +ASTSerializer::importDeclaration(ParseNode *pn, MutableHandleValue dst)
  1.2005 +{
  1.2006 +    JS_ASSERT(pn->isKind(PNK_IMPORT));
  1.2007 +    JS_ASSERT(pn->pn_left->isKind(PNK_IMPORT_SPEC_LIST));
  1.2008 +    JS_ASSERT(pn->pn_right->isKind(PNK_STRING));
  1.2009 +
  1.2010 +    NodeVector elts(cx);
  1.2011 +    if (!elts.reserve(pn->pn_count))
  1.2012 +        return false;
  1.2013 +
  1.2014 +    for (ParseNode *next = pn->pn_left->pn_head; next; next = next->pn_next) {
  1.2015 +        RootedValue elt(cx);
  1.2016 +        if (!importSpecifier(next, &elt))
  1.2017 +            return false;
  1.2018 +        elts.infallibleAppend(elt);
  1.2019 +    }
  1.2020 +
  1.2021 +    RootedValue moduleSpec(cx);
  1.2022 +    return literal(pn->pn_right, &moduleSpec) &&
  1.2023 +           builder.importDeclaration(elts, moduleSpec, &pn->pn_pos, dst);
  1.2024 +}
  1.2025 +
  1.2026 +bool
  1.2027 +ASTSerializer::importSpecifier(ParseNode *pn, MutableHandleValue dst)
  1.2028 +{
  1.2029 +    JS_ASSERT(pn->isKind(PNK_IMPORT_SPEC));
  1.2030 +
  1.2031 +    RootedValue importName(cx);
  1.2032 +    RootedValue bindingName(cx);
  1.2033 +    return identifier(pn->pn_left, &importName) &&
  1.2034 +           identifier(pn->pn_right, &bindingName) &&
  1.2035 +           builder.importSpecifier(importName, bindingName, &pn->pn_pos, dst);
  1.2036 +}
  1.2037 +
  1.2038 +bool
  1.2039 +ASTSerializer::exportDeclaration(ParseNode *pn, MutableHandleValue dst)
  1.2040 +{
  1.2041 +    JS_ASSERT(pn->isKind(PNK_EXPORT) || pn->isKind(PNK_EXPORT_FROM));
  1.2042 +    JS_ASSERT_IF(pn->isKind(PNK_EXPORT_FROM), pn->pn_right->isKind(PNK_STRING));
  1.2043 +
  1.2044 +    RootedValue decl(cx, NullValue());
  1.2045 +    NodeVector elts(cx);
  1.2046 +
  1.2047 +    ParseNode *kid = pn->isKind(PNK_EXPORT) ? pn->pn_kid : pn->pn_left;
  1.2048 +    switch (ParseNodeKind kind = kid->getKind()) {
  1.2049 +      case PNK_EXPORT_SPEC_LIST:
  1.2050 +        if (!elts.reserve(pn->pn_count))
  1.2051 +            return false;
  1.2052 +
  1.2053 +        for (ParseNode *next = pn->pn_left->pn_head; next; next = next->pn_next) {
  1.2054 +            RootedValue elt(cx);
  1.2055 +            if (next->isKind(PNK_EXPORT_SPEC)) {
  1.2056 +                if (!exportSpecifier(next, &elt))
  1.2057 +                    return false;
  1.2058 +            } else {
  1.2059 +                if (!builder.exportBatchSpecifier(&pn->pn_pos, &elt))
  1.2060 +                    return false;
  1.2061 +            }
  1.2062 +            elts.infallibleAppend(elt);
  1.2063 +        }
  1.2064 +        break;
  1.2065 +
  1.2066 +      case PNK_FUNCTION:
  1.2067 +        if (!function(kid, AST_FUNC_DECL, &decl))
  1.2068 +            return false;
  1.2069 +        break;
  1.2070 +
  1.2071 +      case PNK_VAR:
  1.2072 +      case PNK_CONST:
  1.2073 +      case PNK_LET:
  1.2074 +        if (!variableDeclaration(kid, kind == PNK_LET, &decl))
  1.2075 +            return false;
  1.2076 +        break;
  1.2077 +
  1.2078 +      default:
  1.2079 +        LOCAL_NOT_REACHED("unexpected statement type");
  1.2080 +    }
  1.2081 +
  1.2082 +    RootedValue moduleSpec(cx, NullValue());
  1.2083 +    if (pn->isKind(PNK_EXPORT_FROM) && !literal(pn->pn_right, &moduleSpec))
  1.2084 +        return false;
  1.2085 +
  1.2086 +    return builder.exportDeclaration(decl, elts, moduleSpec, &pn->pn_pos, dst);
  1.2087 +}
  1.2088 +
  1.2089 +bool
  1.2090 +ASTSerializer::exportSpecifier(ParseNode *pn, MutableHandleValue dst)
  1.2091 +{
  1.2092 +    JS_ASSERT(pn->isKind(PNK_EXPORT_SPEC));
  1.2093 +
  1.2094 +    RootedValue bindingName(cx);
  1.2095 +    RootedValue exportName(cx);
  1.2096 +    return identifier(pn->pn_left, &bindingName) &&
  1.2097 +           identifier(pn->pn_right, &exportName) &&
  1.2098 +           builder.exportSpecifier(bindingName, exportName, &pn->pn_pos, dst);
  1.2099 +}
  1.2100 +
  1.2101 +bool
  1.2102 +ASTSerializer::switchCase(ParseNode *pn, MutableHandleValue dst)
  1.2103 +{
  1.2104 +    JS_ASSERT_IF(pn->pn_left, pn->pn_pos.encloses(pn->pn_left->pn_pos));
  1.2105 +    JS_ASSERT(pn->pn_pos.encloses(pn->pn_right->pn_pos));
  1.2106 +
  1.2107 +    NodeVector stmts(cx);
  1.2108 +
  1.2109 +    RootedValue expr(cx);
  1.2110 +
  1.2111 +    return optExpression(pn->pn_left, &expr) &&
  1.2112 +           statements(pn->pn_right, stmts) &&
  1.2113 +           builder.switchCase(expr, stmts, &pn->pn_pos, dst);
  1.2114 +}
  1.2115 +
  1.2116 +bool
  1.2117 +ASTSerializer::switchStatement(ParseNode *pn, MutableHandleValue dst)
  1.2118 +{
  1.2119 +    JS_ASSERT(pn->pn_pos.encloses(pn->pn_left->pn_pos));
  1.2120 +    JS_ASSERT(pn->pn_pos.encloses(pn->pn_right->pn_pos));
  1.2121 +
  1.2122 +    RootedValue disc(cx);
  1.2123 +
  1.2124 +    if (!expression(pn->pn_left, &disc))
  1.2125 +        return false;
  1.2126 +
  1.2127 +    ParseNode *listNode;
  1.2128 +    bool lexical;
  1.2129 +
  1.2130 +    if (pn->pn_right->isKind(PNK_LEXICALSCOPE)) {
  1.2131 +        listNode = pn->pn_right->pn_expr;
  1.2132 +        lexical = true;
  1.2133 +    } else {
  1.2134 +        listNode = pn->pn_right;
  1.2135 +        lexical = false;
  1.2136 +    }
  1.2137 +
  1.2138 +    NodeVector cases(cx);
  1.2139 +    if (!cases.reserve(listNode->pn_count))
  1.2140 +        return false;
  1.2141 +
  1.2142 +    for (ParseNode *next = listNode->pn_head; next; next = next->pn_next) {
  1.2143 +        RootedValue child(cx);
  1.2144 +        if (!switchCase(next, &child))
  1.2145 +            return false;
  1.2146 +        cases.infallibleAppend(child);
  1.2147 +    }
  1.2148 +
  1.2149 +    return builder.switchStatement(disc, cases, lexical, &pn->pn_pos, dst);
  1.2150 +}
  1.2151 +
  1.2152 +bool
  1.2153 +ASTSerializer::catchClause(ParseNode *pn, bool *isGuarded, MutableHandleValue dst)
  1.2154 +{
  1.2155 +    JS_ASSERT(pn->pn_pos.encloses(pn->pn_kid1->pn_pos));
  1.2156 +    JS_ASSERT_IF(pn->pn_kid2, pn->pn_pos.encloses(pn->pn_kid2->pn_pos));
  1.2157 +    JS_ASSERT(pn->pn_pos.encloses(pn->pn_kid3->pn_pos));
  1.2158 +
  1.2159 +    RootedValue var(cx), guard(cx), body(cx);
  1.2160 +
  1.2161 +    if (!pattern(pn->pn_kid1, nullptr, &var) ||
  1.2162 +        !optExpression(pn->pn_kid2, &guard)) {
  1.2163 +        return false;
  1.2164 +    }
  1.2165 +
  1.2166 +    *isGuarded = !guard.isMagic(JS_SERIALIZE_NO_NODE);
  1.2167 +
  1.2168 +    return statement(pn->pn_kid3, &body) &&
  1.2169 +           builder.catchClause(var, guard, body, &pn->pn_pos, dst);
  1.2170 +}
  1.2171 +
  1.2172 +bool
  1.2173 +ASTSerializer::tryStatement(ParseNode *pn, MutableHandleValue dst)
  1.2174 +{
  1.2175 +    JS_ASSERT(pn->pn_pos.encloses(pn->pn_kid1->pn_pos));
  1.2176 +    JS_ASSERT_IF(pn->pn_kid2, pn->pn_pos.encloses(pn->pn_kid2->pn_pos));
  1.2177 +    JS_ASSERT_IF(pn->pn_kid3, pn->pn_pos.encloses(pn->pn_kid3->pn_pos));
  1.2178 +
  1.2179 +    RootedValue body(cx);
  1.2180 +    if (!statement(pn->pn_kid1, &body))
  1.2181 +        return false;
  1.2182 +
  1.2183 +    NodeVector guarded(cx);
  1.2184 +    RootedValue unguarded(cx, NullValue());
  1.2185 +
  1.2186 +    if (pn->pn_kid2) {
  1.2187 +        if (!guarded.reserve(pn->pn_kid2->pn_count))
  1.2188 +            return false;
  1.2189 +
  1.2190 +        for (ParseNode *next = pn->pn_kid2->pn_head; next; next = next->pn_next) {
  1.2191 +            RootedValue clause(cx);
  1.2192 +            bool isGuarded;
  1.2193 +            if (!catchClause(next->pn_expr, &isGuarded, &clause))
  1.2194 +                return false;
  1.2195 +            if (isGuarded)
  1.2196 +                guarded.infallibleAppend(clause);
  1.2197 +            else
  1.2198 +                unguarded = clause;
  1.2199 +        }
  1.2200 +    }
  1.2201 +
  1.2202 +    RootedValue finally(cx);
  1.2203 +    return optStatement(pn->pn_kid3, &finally) &&
  1.2204 +           builder.tryStatement(body, guarded, unguarded, finally, &pn->pn_pos, dst);
  1.2205 +}
  1.2206 +
  1.2207 +bool
  1.2208 +ASTSerializer::forInit(ParseNode *pn, MutableHandleValue dst)
  1.2209 +{
  1.2210 +    if (!pn) {
  1.2211 +        dst.setMagic(JS_SERIALIZE_NO_NODE);
  1.2212 +        return true;
  1.2213 +    }
  1.2214 +
  1.2215 +    return (pn->isKind(PNK_VAR) || pn->isKind(PNK_CONST))
  1.2216 +           ? variableDeclaration(pn, false, dst)
  1.2217 +           : expression(pn, dst);
  1.2218 +}
  1.2219 +
  1.2220 +bool
  1.2221 +ASTSerializer::forOf(ParseNode *loop, ParseNode *head, HandleValue var, HandleValue stmt,
  1.2222 +                         MutableHandleValue dst)
  1.2223 +{
  1.2224 +    RootedValue expr(cx);
  1.2225 +
  1.2226 +    return expression(head->pn_kid3, &expr) &&
  1.2227 +        builder.forOfStatement(var, expr, stmt, &loop->pn_pos, dst);
  1.2228 +}
  1.2229 +
  1.2230 +bool
  1.2231 +ASTSerializer::forIn(ParseNode *loop, ParseNode *head, HandleValue var, HandleValue stmt,
  1.2232 +                         MutableHandleValue dst)
  1.2233 +{
  1.2234 +    RootedValue expr(cx);
  1.2235 +    bool isForEach = loop->pn_iflags & JSITER_FOREACH;
  1.2236 +
  1.2237 +    return expression(head->pn_kid3, &expr) &&
  1.2238 +        builder.forInStatement(var, expr, stmt, isForEach, &loop->pn_pos, dst);
  1.2239 +}
  1.2240 +
  1.2241 +bool
  1.2242 +ASTSerializer::statement(ParseNode *pn, MutableHandleValue dst)
  1.2243 +{
  1.2244 +    JS_CHECK_RECURSION(cx, return false);
  1.2245 +    switch (pn->getKind()) {
  1.2246 +      case PNK_FUNCTION:
  1.2247 +      case PNK_VAR:
  1.2248 +      case PNK_CONST:
  1.2249 +        return declaration(pn, dst);
  1.2250 +
  1.2251 +      case PNK_LET:
  1.2252 +        return pn->isArity(PN_BINARY)
  1.2253 +               ? let(pn, false, dst)
  1.2254 +               : declaration(pn, dst);
  1.2255 +
  1.2256 +      case PNK_IMPORT:
  1.2257 +        return importDeclaration(pn, dst);
  1.2258 +
  1.2259 +      case PNK_EXPORT:
  1.2260 +      case PNK_EXPORT_FROM:
  1.2261 +        return exportDeclaration(pn, dst);
  1.2262 +
  1.2263 +      case PNK_NAME:
  1.2264 +        LOCAL_ASSERT(pn->isUsed());
  1.2265 +        return statement(pn->pn_lexdef, dst);
  1.2266 +
  1.2267 +      case PNK_SEMI:
  1.2268 +        if (pn->pn_kid) {
  1.2269 +            RootedValue expr(cx);
  1.2270 +            return expression(pn->pn_kid, &expr) &&
  1.2271 +                   builder.expressionStatement(expr, &pn->pn_pos, dst);
  1.2272 +        }
  1.2273 +        return builder.emptyStatement(&pn->pn_pos, dst);
  1.2274 +
  1.2275 +      case PNK_LEXICALSCOPE:
  1.2276 +        pn = pn->pn_expr;
  1.2277 +        if (!pn->isKind(PNK_STATEMENTLIST))
  1.2278 +            return statement(pn, dst);
  1.2279 +        /* FALL THROUGH */
  1.2280 +
  1.2281 +      case PNK_STATEMENTLIST:
  1.2282 +        return blockStatement(pn, dst);
  1.2283 +
  1.2284 +      case PNK_IF:
  1.2285 +      {
  1.2286 +        JS_ASSERT(pn->pn_pos.encloses(pn->pn_kid1->pn_pos));
  1.2287 +        JS_ASSERT(pn->pn_pos.encloses(pn->pn_kid2->pn_pos));
  1.2288 +        JS_ASSERT_IF(pn->pn_kid3, pn->pn_pos.encloses(pn->pn_kid3->pn_pos));
  1.2289 +
  1.2290 +        RootedValue test(cx), cons(cx), alt(cx);
  1.2291 +
  1.2292 +        return expression(pn->pn_kid1, &test) &&
  1.2293 +               statement(pn->pn_kid2, &cons) &&
  1.2294 +               optStatement(pn->pn_kid3, &alt) &&
  1.2295 +               builder.ifStatement(test, cons, alt, &pn->pn_pos, dst);
  1.2296 +      }
  1.2297 +
  1.2298 +      case PNK_SWITCH:
  1.2299 +        return switchStatement(pn, dst);
  1.2300 +
  1.2301 +      case PNK_TRY:
  1.2302 +        return tryStatement(pn, dst);
  1.2303 +
  1.2304 +      case PNK_WITH:
  1.2305 +      case PNK_WHILE:
  1.2306 +      {
  1.2307 +        JS_ASSERT(pn->pn_pos.encloses(pn->pn_left->pn_pos));
  1.2308 +        JS_ASSERT(pn->pn_pos.encloses(pn->pn_right->pn_pos));
  1.2309 +
  1.2310 +        RootedValue expr(cx), stmt(cx);
  1.2311 +
  1.2312 +        return expression(pn->pn_left, &expr) &&
  1.2313 +               statement(pn->pn_right, &stmt) &&
  1.2314 +               (pn->isKind(PNK_WITH)
  1.2315 +                ? builder.withStatement(expr, stmt, &pn->pn_pos, dst)
  1.2316 +                : builder.whileStatement(expr, stmt, &pn->pn_pos, dst));
  1.2317 +      }
  1.2318 +
  1.2319 +      case PNK_DOWHILE:
  1.2320 +      {
  1.2321 +        JS_ASSERT(pn->pn_pos.encloses(pn->pn_left->pn_pos));
  1.2322 +        JS_ASSERT(pn->pn_pos.encloses(pn->pn_right->pn_pos));
  1.2323 +
  1.2324 +        RootedValue stmt(cx), test(cx);
  1.2325 +
  1.2326 +        return statement(pn->pn_left, &stmt) &&
  1.2327 +               expression(pn->pn_right, &test) &&
  1.2328 +               builder.doWhileStatement(stmt, test, &pn->pn_pos, dst);
  1.2329 +      }
  1.2330 +
  1.2331 +      case PNK_FOR:
  1.2332 +      {
  1.2333 +        JS_ASSERT(pn->pn_pos.encloses(pn->pn_left->pn_pos));
  1.2334 +        JS_ASSERT(pn->pn_pos.encloses(pn->pn_right->pn_pos));
  1.2335 +
  1.2336 +        ParseNode *head = pn->pn_left;
  1.2337 +
  1.2338 +        JS_ASSERT_IF(head->pn_kid1, head->pn_pos.encloses(head->pn_kid1->pn_pos));
  1.2339 +        JS_ASSERT_IF(head->pn_kid2, head->pn_pos.encloses(head->pn_kid2->pn_pos));
  1.2340 +        JS_ASSERT_IF(head->pn_kid3, head->pn_pos.encloses(head->pn_kid3->pn_pos));
  1.2341 +
  1.2342 +        RootedValue stmt(cx);
  1.2343 +        if (!statement(pn->pn_right, &stmt))
  1.2344 +            return false;
  1.2345 +
  1.2346 +        if (head->isKind(PNK_FORIN)) {
  1.2347 +            RootedValue var(cx);
  1.2348 +            return (!head->pn_kid1
  1.2349 +                    ? pattern(head->pn_kid2, nullptr, &var)
  1.2350 +                    : head->pn_kid1->isKind(PNK_LEXICALSCOPE)
  1.2351 +                    ? variableDeclaration(head->pn_kid1->pn_expr, true, &var)
  1.2352 +                    : variableDeclaration(head->pn_kid1, false, &var)) &&
  1.2353 +                forIn(pn, head, var, stmt, dst);
  1.2354 +        }
  1.2355 +
  1.2356 +        if (head->isKind(PNK_FOROF)) {
  1.2357 +            RootedValue var(cx);
  1.2358 +            return (!head->pn_kid1
  1.2359 +                    ? pattern(head->pn_kid2, nullptr, &var)
  1.2360 +                    : head->pn_kid1->isKind(PNK_LEXICALSCOPE)
  1.2361 +                    ? variableDeclaration(head->pn_kid1->pn_expr, true, &var)
  1.2362 +                    : variableDeclaration(head->pn_kid1, false, &var)) &&
  1.2363 +                forOf(pn, head, var, stmt, dst);
  1.2364 +        }
  1.2365 +
  1.2366 +        RootedValue init(cx), test(cx), update(cx);
  1.2367 +
  1.2368 +        return forInit(head->pn_kid1, &init) &&
  1.2369 +               optExpression(head->pn_kid2, &test) &&
  1.2370 +               optExpression(head->pn_kid3, &update) &&
  1.2371 +               builder.forStatement(init, test, update, stmt, &pn->pn_pos, dst);
  1.2372 +      }
  1.2373 +
  1.2374 +      /* Synthesized by the parser when a for-in loop contains a variable initializer. */
  1.2375 +      case PNK_SEQ:
  1.2376 +      {
  1.2377 +        LOCAL_ASSERT(pn->pn_count == 2);
  1.2378 +
  1.2379 +        ParseNode *prelude = pn->pn_head;
  1.2380 +        ParseNode *loop = prelude->pn_next;
  1.2381 +
  1.2382 +        LOCAL_ASSERT(prelude->isKind(PNK_VAR) && loop->isKind(PNK_FOR));
  1.2383 +
  1.2384 +        RootedValue var(cx);
  1.2385 +        if (!variableDeclaration(prelude, false, &var))
  1.2386 +            return false;
  1.2387 +
  1.2388 +        ParseNode *head = loop->pn_left;
  1.2389 +        JS_ASSERT(head->isKind(PNK_FORIN));
  1.2390 +
  1.2391 +        RootedValue stmt(cx);
  1.2392 +
  1.2393 +        return statement(loop->pn_right, &stmt) && forIn(loop, head, var, stmt, dst);
  1.2394 +      }
  1.2395 +
  1.2396 +      case PNK_BREAK:
  1.2397 +      case PNK_CONTINUE:
  1.2398 +      {
  1.2399 +        RootedValue label(cx);
  1.2400 +        RootedAtom pnAtom(cx, pn->pn_atom);
  1.2401 +        return optIdentifier(pnAtom, nullptr, &label) &&
  1.2402 +               (pn->isKind(PNK_BREAK)
  1.2403 +                ? builder.breakStatement(label, &pn->pn_pos, dst)
  1.2404 +                : builder.continueStatement(label, &pn->pn_pos, dst));
  1.2405 +      }
  1.2406 +
  1.2407 +      case PNK_LABEL:
  1.2408 +      {
  1.2409 +        JS_ASSERT(pn->pn_pos.encloses(pn->pn_expr->pn_pos));
  1.2410 +
  1.2411 +        RootedValue label(cx), stmt(cx);
  1.2412 +        RootedAtom pnAtom(cx, pn->as<LabeledStatement>().label());
  1.2413 +        return identifier(pnAtom, nullptr, &label) &&
  1.2414 +               statement(pn->pn_expr, &stmt) &&
  1.2415 +               builder.labeledStatement(label, stmt, &pn->pn_pos, dst);
  1.2416 +      }
  1.2417 +
  1.2418 +      case PNK_THROW:
  1.2419 +      case PNK_RETURN:
  1.2420 +      {
  1.2421 +        JS_ASSERT_IF(pn->pn_kid, pn->pn_pos.encloses(pn->pn_kid->pn_pos));
  1.2422 +
  1.2423 +        RootedValue arg(cx);
  1.2424 +
  1.2425 +        return optExpression(pn->pn_kid, &arg) &&
  1.2426 +               (pn->isKind(PNK_THROW)
  1.2427 +                ? builder.throwStatement(arg, &pn->pn_pos, dst)
  1.2428 +                : builder.returnStatement(arg, &pn->pn_pos, dst));
  1.2429 +      }
  1.2430 +
  1.2431 +      case PNK_DEBUGGER:
  1.2432 +        return builder.debuggerStatement(&pn->pn_pos, dst);
  1.2433 +
  1.2434 +      case PNK_NOP:
  1.2435 +        return builder.emptyStatement(&pn->pn_pos, dst);
  1.2436 +
  1.2437 +      default:
  1.2438 +        LOCAL_NOT_REACHED("unexpected statement type");
  1.2439 +    }
  1.2440 +}
  1.2441 +
  1.2442 +bool
  1.2443 +ASTSerializer::leftAssociate(ParseNode *pn, MutableHandleValue dst)
  1.2444 +{
  1.2445 +    JS_ASSERT(pn->isArity(PN_LIST));
  1.2446 +    JS_ASSERT(pn->pn_count >= 1);
  1.2447 +
  1.2448 +    ParseNodeKind kind = pn->getKind();
  1.2449 +    bool lor = kind == PNK_OR;
  1.2450 +    bool logop = lor || (kind == PNK_AND);
  1.2451 +
  1.2452 +    ParseNode *head = pn->pn_head;
  1.2453 +    RootedValue left(cx);
  1.2454 +    if (!expression(head, &left))
  1.2455 +        return false;
  1.2456 +    for (ParseNode *next = head->pn_next; next; next = next->pn_next) {
  1.2457 +        RootedValue right(cx);
  1.2458 +        if (!expression(next, &right))
  1.2459 +            return false;
  1.2460 +
  1.2461 +        TokenPos subpos(pn->pn_pos.begin, next->pn_pos.end);
  1.2462 +
  1.2463 +        if (logop) {
  1.2464 +            if (!builder.logicalExpression(lor, left, right, &subpos, &left))
  1.2465 +                return false;
  1.2466 +        } else {
  1.2467 +            BinaryOperator op = binop(pn->getKind(), pn->getOp());
  1.2468 +            LOCAL_ASSERT(op > BINOP_ERR && op < BINOP_LIMIT);
  1.2469 +
  1.2470 +            if (!builder.binaryExpression(op, left, right, &subpos, &left))
  1.2471 +                return false;
  1.2472 +        }
  1.2473 +    }
  1.2474 +
  1.2475 +    dst.set(left);
  1.2476 +    return true;
  1.2477 +}
  1.2478 +
  1.2479 +bool
  1.2480 +ASTSerializer::comprehensionBlock(ParseNode *pn, MutableHandleValue dst)
  1.2481 +{
  1.2482 +    LOCAL_ASSERT(pn->isArity(PN_BINARY));
  1.2483 +
  1.2484 +    ParseNode *in = pn->pn_left;
  1.2485 +
  1.2486 +    LOCAL_ASSERT(in && (in->isKind(PNK_FORIN) || in->isKind(PNK_FOROF)));
  1.2487 +
  1.2488 +    bool isForEach = pn->pn_iflags & JSITER_FOREACH;
  1.2489 +    bool isForOf = in->isKind(PNK_FOROF);
  1.2490 +
  1.2491 +    RootedValue patt(cx), src(cx);
  1.2492 +    return pattern(in->pn_kid2, nullptr, &patt) &&
  1.2493 +           expression(in->pn_kid3, &src) &&
  1.2494 +           builder.comprehensionBlock(patt, src, isForEach, isForOf, &in->pn_pos, dst);
  1.2495 +}
  1.2496 +
  1.2497 +bool
  1.2498 +ASTSerializer::comprehension(ParseNode *pn, MutableHandleValue dst)
  1.2499 +{
  1.2500 +    LOCAL_ASSERT(pn->isKind(PNK_FOR));
  1.2501 +
  1.2502 +    NodeVector blocks(cx);
  1.2503 +
  1.2504 +    ParseNode *next = pn;
  1.2505 +    while (next->isKind(PNK_FOR)) {
  1.2506 +        RootedValue block(cx);
  1.2507 +        if (!comprehensionBlock(next, &block) || !blocks.append(block))
  1.2508 +            return false;
  1.2509 +        next = next->pn_right;
  1.2510 +    }
  1.2511 +
  1.2512 +    RootedValue filter(cx, MagicValue(JS_SERIALIZE_NO_NODE));
  1.2513 +
  1.2514 +    if (next->isKind(PNK_IF)) {
  1.2515 +        if (!optExpression(next->pn_kid1, &filter))
  1.2516 +            return false;
  1.2517 +        next = next->pn_kid2;
  1.2518 +    } else if (next->isKind(PNK_STATEMENTLIST) && next->pn_count == 0) {
  1.2519 +        /* FoldConstants optimized away the push. */
  1.2520 +        NodeVector empty(cx);
  1.2521 +        return builder.arrayExpression(empty, &pn->pn_pos, dst);
  1.2522 +    }
  1.2523 +
  1.2524 +    LOCAL_ASSERT(next->isKind(PNK_ARRAYPUSH));
  1.2525 +
  1.2526 +    RootedValue body(cx);
  1.2527 +
  1.2528 +    return expression(next->pn_kid, &body) &&
  1.2529 +           builder.comprehensionExpression(body, blocks, filter, &pn->pn_pos, dst);
  1.2530 +}
  1.2531 +
  1.2532 +bool
  1.2533 +ASTSerializer::generatorExpression(ParseNode *pn, MutableHandleValue dst)
  1.2534 +{
  1.2535 +    LOCAL_ASSERT(pn->isKind(PNK_FOR));
  1.2536 +
  1.2537 +    NodeVector blocks(cx);
  1.2538 +
  1.2539 +    ParseNode *next = pn;
  1.2540 +    while (next->isKind(PNK_FOR)) {
  1.2541 +        RootedValue block(cx);
  1.2542 +        if (!comprehensionBlock(next, &block) || !blocks.append(block))
  1.2543 +            return false;
  1.2544 +        next = next->pn_right;
  1.2545 +    }
  1.2546 +
  1.2547 +    RootedValue filter(cx, MagicValue(JS_SERIALIZE_NO_NODE));
  1.2548 +
  1.2549 +    if (next->isKind(PNK_IF)) {
  1.2550 +        if (!optExpression(next->pn_kid1, &filter))
  1.2551 +            return false;
  1.2552 +        next = next->pn_kid2;
  1.2553 +    }
  1.2554 +
  1.2555 +    LOCAL_ASSERT(next->isKind(PNK_SEMI) &&
  1.2556 +                 next->pn_kid->isKind(PNK_YIELD) &&
  1.2557 +                 next->pn_kid->pn_kid);
  1.2558 +
  1.2559 +    RootedValue body(cx);
  1.2560 +
  1.2561 +    return expression(next->pn_kid->pn_kid, &body) &&
  1.2562 +           builder.generatorExpression(body, blocks, filter, &pn->pn_pos, dst);
  1.2563 +}
  1.2564 +
  1.2565 +bool
  1.2566 +ASTSerializer::expression(ParseNode *pn, MutableHandleValue dst)
  1.2567 +{
  1.2568 +    JS_CHECK_RECURSION(cx, return false);
  1.2569 +    switch (pn->getKind()) {
  1.2570 +      case PNK_FUNCTION:
  1.2571 +      {
  1.2572 +        ASTType type = pn->pn_funbox->function()->isArrow() ? AST_ARROW_EXPR : AST_FUNC_EXPR;
  1.2573 +        return function(pn, type, dst);
  1.2574 +      }
  1.2575 +
  1.2576 +      case PNK_COMMA:
  1.2577 +      {
  1.2578 +        NodeVector exprs(cx);
  1.2579 +        return expressions(pn, exprs) &&
  1.2580 +               builder.sequenceExpression(exprs, &pn->pn_pos, dst);
  1.2581 +      }
  1.2582 +
  1.2583 +      case PNK_CONDITIONAL:
  1.2584 +      {
  1.2585 +        JS_ASSERT(pn->pn_pos.encloses(pn->pn_kid1->pn_pos));
  1.2586 +        JS_ASSERT(pn->pn_pos.encloses(pn->pn_kid2->pn_pos));
  1.2587 +        JS_ASSERT(pn->pn_pos.encloses(pn->pn_kid3->pn_pos));
  1.2588 +
  1.2589 +        RootedValue test(cx), cons(cx), alt(cx);
  1.2590 +
  1.2591 +        return expression(pn->pn_kid1, &test) &&
  1.2592 +               expression(pn->pn_kid2, &cons) &&
  1.2593 +               expression(pn->pn_kid3, &alt) &&
  1.2594 +               builder.conditionalExpression(test, cons, alt, &pn->pn_pos, dst);
  1.2595 +      }
  1.2596 +
  1.2597 +      case PNK_OR:
  1.2598 +      case PNK_AND:
  1.2599 +      {
  1.2600 +        if (pn->isArity(PN_BINARY)) {
  1.2601 +            JS_ASSERT(pn->pn_pos.encloses(pn->pn_left->pn_pos));
  1.2602 +            JS_ASSERT(pn->pn_pos.encloses(pn->pn_right->pn_pos));
  1.2603 +
  1.2604 +            RootedValue left(cx), right(cx);
  1.2605 +            return expression(pn->pn_left, &left) &&
  1.2606 +                   expression(pn->pn_right, &right) &&
  1.2607 +                   builder.logicalExpression(pn->isKind(PNK_OR), left, right, &pn->pn_pos, dst);
  1.2608 +        }
  1.2609 +        return leftAssociate(pn, dst);
  1.2610 +      }
  1.2611 +
  1.2612 +      case PNK_PREINCREMENT:
  1.2613 +      case PNK_PREDECREMENT:
  1.2614 +      {
  1.2615 +        JS_ASSERT(pn->pn_pos.encloses(pn->pn_kid->pn_pos));
  1.2616 +
  1.2617 +        bool inc = pn->isKind(PNK_PREINCREMENT);
  1.2618 +        RootedValue expr(cx);
  1.2619 +        return expression(pn->pn_kid, &expr) &&
  1.2620 +               builder.updateExpression(expr, inc, true, &pn->pn_pos, dst);
  1.2621 +      }
  1.2622 +
  1.2623 +      case PNK_POSTINCREMENT:
  1.2624 +      case PNK_POSTDECREMENT:
  1.2625 +      {
  1.2626 +        JS_ASSERT(pn->pn_pos.encloses(pn->pn_kid->pn_pos));
  1.2627 +
  1.2628 +        bool inc = pn->isKind(PNK_POSTINCREMENT);
  1.2629 +        RootedValue expr(cx);
  1.2630 +        return expression(pn->pn_kid, &expr) &&
  1.2631 +               builder.updateExpression(expr, inc, false, &pn->pn_pos, dst);
  1.2632 +      }
  1.2633 +
  1.2634 +      case PNK_ASSIGN:
  1.2635 +      case PNK_ADDASSIGN:
  1.2636 +      case PNK_SUBASSIGN:
  1.2637 +      case PNK_BITORASSIGN:
  1.2638 +      case PNK_BITXORASSIGN:
  1.2639 +      case PNK_BITANDASSIGN:
  1.2640 +      case PNK_LSHASSIGN:
  1.2641 +      case PNK_RSHASSIGN:
  1.2642 +      case PNK_URSHASSIGN:
  1.2643 +      case PNK_MULASSIGN:
  1.2644 +      case PNK_DIVASSIGN:
  1.2645 +      case PNK_MODASSIGN:
  1.2646 +      {
  1.2647 +        JS_ASSERT(pn->pn_pos.encloses(pn->pn_left->pn_pos));
  1.2648 +        JS_ASSERT(pn->pn_pos.encloses(pn->pn_right->pn_pos));
  1.2649 +
  1.2650 +        AssignmentOperator op = aop(pn->getOp());
  1.2651 +        LOCAL_ASSERT(op > AOP_ERR && op < AOP_LIMIT);
  1.2652 +
  1.2653 +        RootedValue lhs(cx), rhs(cx);
  1.2654 +        return pattern(pn->pn_left, nullptr, &lhs) &&
  1.2655 +               expression(pn->pn_right, &rhs) &&
  1.2656 +               builder.assignmentExpression(op, lhs, rhs, &pn->pn_pos, dst);
  1.2657 +      }
  1.2658 +
  1.2659 +      case PNK_ADD:
  1.2660 +      case PNK_SUB:
  1.2661 +      case PNK_STRICTEQ:
  1.2662 +      case PNK_EQ:
  1.2663 +      case PNK_STRICTNE:
  1.2664 +      case PNK_NE:
  1.2665 +      case PNK_LT:
  1.2666 +      case PNK_LE:
  1.2667 +      case PNK_GT:
  1.2668 +      case PNK_GE:
  1.2669 +      case PNK_LSH:
  1.2670 +      case PNK_RSH:
  1.2671 +      case PNK_URSH:
  1.2672 +      case PNK_STAR:
  1.2673 +      case PNK_DIV:
  1.2674 +      case PNK_MOD:
  1.2675 +      case PNK_BITOR:
  1.2676 +      case PNK_BITXOR:
  1.2677 +      case PNK_BITAND:
  1.2678 +      case PNK_IN:
  1.2679 +      case PNK_INSTANCEOF:
  1.2680 +        if (pn->isArity(PN_BINARY)) {
  1.2681 +            JS_ASSERT(pn->pn_pos.encloses(pn->pn_left->pn_pos));
  1.2682 +            JS_ASSERT(pn->pn_pos.encloses(pn->pn_right->pn_pos));
  1.2683 +
  1.2684 +            BinaryOperator op = binop(pn->getKind(), pn->getOp());
  1.2685 +            LOCAL_ASSERT(op > BINOP_ERR && op < BINOP_LIMIT);
  1.2686 +
  1.2687 +            RootedValue left(cx), right(cx);
  1.2688 +            return expression(pn->pn_left, &left) &&
  1.2689 +                   expression(pn->pn_right, &right) &&
  1.2690 +                   builder.binaryExpression(op, left, right, &pn->pn_pos, dst);
  1.2691 +        }
  1.2692 +        return leftAssociate(pn, dst);
  1.2693 +
  1.2694 +      case PNK_DELETE:
  1.2695 +      case PNK_TYPEOF:
  1.2696 +      case PNK_VOID:
  1.2697 +      case PNK_NOT:
  1.2698 +      case PNK_BITNOT:
  1.2699 +      case PNK_POS:
  1.2700 +      case PNK_NEG: {
  1.2701 +        JS_ASSERT(pn->pn_pos.encloses(pn->pn_kid->pn_pos));
  1.2702 +
  1.2703 +        UnaryOperator op = unop(pn->getKind(), pn->getOp());
  1.2704 +        LOCAL_ASSERT(op > UNOP_ERR && op < UNOP_LIMIT);
  1.2705 +
  1.2706 +        RootedValue expr(cx);
  1.2707 +        return expression(pn->pn_kid, &expr) &&
  1.2708 +               builder.unaryExpression(op, expr, &pn->pn_pos, dst);
  1.2709 +      }
  1.2710 +
  1.2711 +#if JS_HAS_GENERATOR_EXPRS
  1.2712 +      case PNK_GENEXP:
  1.2713 +        return generatorExpression(pn->generatorExpr(), dst);
  1.2714 +#endif
  1.2715 +
  1.2716 +      case PNK_NEW:
  1.2717 +      case PNK_CALL:
  1.2718 +      {
  1.2719 +        ParseNode *next = pn->pn_head;
  1.2720 +        JS_ASSERT(pn->pn_pos.encloses(next->pn_pos));
  1.2721 +
  1.2722 +        RootedValue callee(cx);
  1.2723 +        if (!expression(next, &callee))
  1.2724 +            return false;
  1.2725 +
  1.2726 +        NodeVector args(cx);
  1.2727 +        if (!args.reserve(pn->pn_count - 1))
  1.2728 +            return false;
  1.2729 +
  1.2730 +        for (next = next->pn_next; next; next = next->pn_next) {
  1.2731 +            JS_ASSERT(pn->pn_pos.encloses(next->pn_pos));
  1.2732 +
  1.2733 +            RootedValue arg(cx);
  1.2734 +            if (!expression(next, &arg))
  1.2735 +                return false;
  1.2736 +            args.infallibleAppend(arg);
  1.2737 +        }
  1.2738 +
  1.2739 +        return pn->isKind(PNK_NEW)
  1.2740 +               ? builder.newExpression(callee, args, &pn->pn_pos, dst)
  1.2741 +
  1.2742 +            : builder.callExpression(callee, args, &pn->pn_pos, dst);
  1.2743 +      }
  1.2744 +
  1.2745 +      case PNK_DOT:
  1.2746 +      {
  1.2747 +        JS_ASSERT(pn->pn_pos.encloses(pn->pn_expr->pn_pos));
  1.2748 +
  1.2749 +        RootedValue expr(cx), id(cx);
  1.2750 +        RootedAtom pnAtom(cx, pn->pn_atom);
  1.2751 +        return expression(pn->pn_expr, &expr) &&
  1.2752 +               identifier(pnAtom, nullptr, &id) &&
  1.2753 +               builder.memberExpression(false, expr, id, &pn->pn_pos, dst);
  1.2754 +      }
  1.2755 +
  1.2756 +      case PNK_ELEM:
  1.2757 +      {
  1.2758 +        JS_ASSERT(pn->pn_pos.encloses(pn->pn_left->pn_pos));
  1.2759 +        JS_ASSERT(pn->pn_pos.encloses(pn->pn_right->pn_pos));
  1.2760 +
  1.2761 +        RootedValue left(cx), right(cx);
  1.2762 +        return expression(pn->pn_left, &left) &&
  1.2763 +               expression(pn->pn_right, &right) &&
  1.2764 +               builder.memberExpression(true, left, right, &pn->pn_pos, dst);
  1.2765 +      }
  1.2766 +
  1.2767 +      case PNK_ARRAY:
  1.2768 +      {
  1.2769 +        NodeVector elts(cx);
  1.2770 +        if (!elts.reserve(pn->pn_count))
  1.2771 +            return false;
  1.2772 +
  1.2773 +        for (ParseNode *next = pn->pn_head; next; next = next->pn_next) {
  1.2774 +            JS_ASSERT(pn->pn_pos.encloses(next->pn_pos));
  1.2775 +
  1.2776 +            if (next->isKind(PNK_ELISION)) {
  1.2777 +                elts.infallibleAppend(NullValue());
  1.2778 +            } else {
  1.2779 +                RootedValue expr(cx);
  1.2780 +                if (!expression(next, &expr))
  1.2781 +                    return false;
  1.2782 +                elts.infallibleAppend(expr);
  1.2783 +            }
  1.2784 +        }
  1.2785 +
  1.2786 +        return builder.arrayExpression(elts, &pn->pn_pos, dst);
  1.2787 +      }
  1.2788 +
  1.2789 +      case PNK_SPREAD:
  1.2790 +      {
  1.2791 +          RootedValue expr(cx);
  1.2792 +          return expression(pn->pn_kid, &expr) &&
  1.2793 +                 builder.spreadExpression(expr, &pn->pn_pos, dst);
  1.2794 +      }
  1.2795 +
  1.2796 +      case PNK_OBJECT:
  1.2797 +      {
  1.2798 +        /* The parser notes any uninitialized properties by setting the PNX_DESTRUCT flag. */
  1.2799 +        if (pn->pn_xflags & PNX_DESTRUCT) {
  1.2800 +            JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, JSMSG_BAD_OBJECT_INIT);
  1.2801 +            return false;
  1.2802 +        }
  1.2803 +        NodeVector elts(cx);
  1.2804 +        if (!elts.reserve(pn->pn_count))
  1.2805 +            return false;
  1.2806 +
  1.2807 +        for (ParseNode *next = pn->pn_head; next; next = next->pn_next) {
  1.2808 +            JS_ASSERT(pn->pn_pos.encloses(next->pn_pos));
  1.2809 +
  1.2810 +            RootedValue prop(cx);
  1.2811 +            if (!property(next, &prop))
  1.2812 +                return false;
  1.2813 +            elts.infallibleAppend(prop);
  1.2814 +        }
  1.2815 +
  1.2816 +        return builder.objectExpression(elts, &pn->pn_pos, dst);
  1.2817 +      }
  1.2818 +
  1.2819 +      case PNK_NAME:
  1.2820 +        return identifier(pn, dst);
  1.2821 +
  1.2822 +      case PNK_THIS:
  1.2823 +        return builder.thisExpression(&pn->pn_pos, dst);
  1.2824 +
  1.2825 +      case PNK_STRING:
  1.2826 +      case PNK_REGEXP:
  1.2827 +      case PNK_NUMBER:
  1.2828 +      case PNK_TRUE:
  1.2829 +      case PNK_FALSE:
  1.2830 +      case PNK_NULL:
  1.2831 +        return literal(pn, dst);
  1.2832 +
  1.2833 +      case PNK_YIELD_STAR:
  1.2834 +      {
  1.2835 +        JS_ASSERT(pn->pn_pos.encloses(pn->pn_kid->pn_pos));
  1.2836 +
  1.2837 +        RootedValue arg(cx);
  1.2838 +        return expression(pn->pn_kid, &arg) &&
  1.2839 +               builder.yieldExpression(arg, Delegating, &pn->pn_pos, dst);
  1.2840 +      }
  1.2841 +
  1.2842 +      case PNK_YIELD:
  1.2843 +      {
  1.2844 +        JS_ASSERT_IF(pn->pn_kid, pn->pn_pos.encloses(pn->pn_kid->pn_pos));
  1.2845 +
  1.2846 +        RootedValue arg(cx);
  1.2847 +        return optExpression(pn->pn_kid, &arg) &&
  1.2848 +               builder.yieldExpression(arg, NotDelegating, &pn->pn_pos, dst);
  1.2849 +      }
  1.2850 +
  1.2851 +      case PNK_ARRAYCOMP:
  1.2852 +        JS_ASSERT(pn->pn_pos.encloses(pn->pn_head->pn_pos));
  1.2853 +
  1.2854 +        /* NB: it's no longer the case that pn_count could be 2. */
  1.2855 +        LOCAL_ASSERT(pn->pn_count == 1);
  1.2856 +        LOCAL_ASSERT(pn->pn_head->isKind(PNK_LEXICALSCOPE));
  1.2857 +
  1.2858 +        return comprehension(pn->pn_head->pn_expr, dst);
  1.2859 +
  1.2860 +      case PNK_LET:
  1.2861 +        return let(pn, true, dst);
  1.2862 +
  1.2863 +      default:
  1.2864 +        LOCAL_NOT_REACHED("unexpected expression type");
  1.2865 +    }
  1.2866 +}
  1.2867 +
  1.2868 +bool
  1.2869 +ASTSerializer::propertyName(ParseNode *pn, MutableHandleValue dst)
  1.2870 +{
  1.2871 +    if (pn->isKind(PNK_NAME))
  1.2872 +        return identifier(pn, dst);
  1.2873 +
  1.2874 +    LOCAL_ASSERT(pn->isKind(PNK_STRING) || pn->isKind(PNK_NUMBER));
  1.2875 +
  1.2876 +    return literal(pn, dst);
  1.2877 +}
  1.2878 +
  1.2879 +bool
  1.2880 +ASTSerializer::property(ParseNode *pn, MutableHandleValue dst)
  1.2881 +{
  1.2882 +    PropKind kind;
  1.2883 +    switch (pn->getOp()) {
  1.2884 +      case JSOP_INITPROP:
  1.2885 +        kind = PROP_INIT;
  1.2886 +        break;
  1.2887 +
  1.2888 +      case JSOP_INITPROP_GETTER:
  1.2889 +        kind = PROP_GETTER;
  1.2890 +        break;
  1.2891 +
  1.2892 +      case JSOP_INITPROP_SETTER:
  1.2893 +        kind = PROP_SETTER;
  1.2894 +        break;
  1.2895 +
  1.2896 +      default:
  1.2897 +        LOCAL_NOT_REACHED("unexpected object-literal property");
  1.2898 +    }
  1.2899 +
  1.2900 +    RootedValue key(cx), val(cx);
  1.2901 +    return propertyName(pn->pn_left, &key) &&
  1.2902 +           expression(pn->pn_right, &val) &&
  1.2903 +           builder.propertyInitializer(key, val, kind, &pn->pn_pos, dst);
  1.2904 +}
  1.2905 +
  1.2906 +bool
  1.2907 +ASTSerializer::literal(ParseNode *pn, MutableHandleValue dst)
  1.2908 +{
  1.2909 +    RootedValue val(cx);
  1.2910 +    switch (pn->getKind()) {
  1.2911 +      case PNK_STRING:
  1.2912 +        val.setString(pn->pn_atom);
  1.2913 +        break;
  1.2914 +
  1.2915 +      case PNK_REGEXP:
  1.2916 +      {
  1.2917 +        RootedObject re1(cx, pn->as<RegExpLiteral>().objbox()->object);
  1.2918 +        LOCAL_ASSERT(re1 && re1->is<RegExpObject>());
  1.2919 +
  1.2920 +        RootedObject re2(cx, CloneRegExpObject(cx, re1));
  1.2921 +        if (!re2)
  1.2922 +            return false;
  1.2923 +
  1.2924 +        val.setObject(*re2);
  1.2925 +        break;
  1.2926 +      }
  1.2927 +
  1.2928 +      case PNK_NUMBER:
  1.2929 +        val.setNumber(pn->pn_dval);
  1.2930 +        break;
  1.2931 +
  1.2932 +      case PNK_NULL:
  1.2933 +        val.setNull();
  1.2934 +        break;
  1.2935 +
  1.2936 +      case PNK_TRUE:
  1.2937 +        val.setBoolean(true);
  1.2938 +        break;
  1.2939 +
  1.2940 +      case PNK_FALSE:
  1.2941 +        val.setBoolean(false);
  1.2942 +        break;
  1.2943 +
  1.2944 +      default:
  1.2945 +        LOCAL_NOT_REACHED("unexpected literal type");
  1.2946 +    }
  1.2947 +
  1.2948 +    return builder.literal(val, &pn->pn_pos, dst);
  1.2949 +}
  1.2950 +
  1.2951 +bool
  1.2952 +ASTSerializer::arrayPattern(ParseNode *pn, VarDeclKind *pkind, MutableHandleValue dst)
  1.2953 +{
  1.2954 +    JS_ASSERT(pn->isKind(PNK_ARRAY));
  1.2955 +
  1.2956 +    NodeVector elts(cx);
  1.2957 +    if (!elts.reserve(pn->pn_count))
  1.2958 +        return false;
  1.2959 +
  1.2960 +    for (ParseNode *next = pn->pn_head; next; next = next->pn_next) {
  1.2961 +        if (next->isKind(PNK_ELISION)) {
  1.2962 +            elts.infallibleAppend(NullValue());
  1.2963 +        } else {
  1.2964 +            RootedValue patt(cx);
  1.2965 +            if (!pattern(next, pkind, &patt))
  1.2966 +                return false;
  1.2967 +            elts.infallibleAppend(patt);
  1.2968 +        }
  1.2969 +    }
  1.2970 +
  1.2971 +    return builder.arrayPattern(elts, &pn->pn_pos, dst);
  1.2972 +}
  1.2973 +
  1.2974 +bool
  1.2975 +ASTSerializer::objectPattern(ParseNode *pn, VarDeclKind *pkind, MutableHandleValue dst)
  1.2976 +{
  1.2977 +    JS_ASSERT(pn->isKind(PNK_OBJECT));
  1.2978 +
  1.2979 +    NodeVector elts(cx);
  1.2980 +    if (!elts.reserve(pn->pn_count))
  1.2981 +        return false;
  1.2982 +
  1.2983 +    for (ParseNode *next = pn->pn_head; next; next = next->pn_next) {
  1.2984 +        LOCAL_ASSERT(next->isOp(JSOP_INITPROP));
  1.2985 +
  1.2986 +        RootedValue key(cx), patt(cx), prop(cx);
  1.2987 +        if (!propertyName(next->pn_left, &key) ||
  1.2988 +            !pattern(next->pn_right, pkind, &patt) ||
  1.2989 +            !builder.propertyPattern(key, patt, &next->pn_pos, &prop)) {
  1.2990 +            return false;
  1.2991 +        }
  1.2992 +
  1.2993 +        elts.infallibleAppend(prop);
  1.2994 +    }
  1.2995 +
  1.2996 +    return builder.objectPattern(elts, &pn->pn_pos, dst);
  1.2997 +}
  1.2998 +
  1.2999 +bool
  1.3000 +ASTSerializer::pattern(ParseNode *pn, VarDeclKind *pkind, MutableHandleValue dst)
  1.3001 +{
  1.3002 +    JS_CHECK_RECURSION(cx, return false);
  1.3003 +    switch (pn->getKind()) {
  1.3004 +      case PNK_OBJECT:
  1.3005 +        return objectPattern(pn, pkind, dst);
  1.3006 +
  1.3007 +      case PNK_ARRAY:
  1.3008 +        return arrayPattern(pn, pkind, dst);
  1.3009 +
  1.3010 +      case PNK_NAME:
  1.3011 +        if (pkind && (pn->pn_dflags & PND_CONST))
  1.3012 +            *pkind = VARDECL_CONST;
  1.3013 +        /* FALL THROUGH */
  1.3014 +
  1.3015 +      default:
  1.3016 +        return expression(pn, dst);
  1.3017 +    }
  1.3018 +}
  1.3019 +
  1.3020 +bool
  1.3021 +ASTSerializer::identifier(HandleAtom atom, TokenPos *pos, MutableHandleValue dst)
  1.3022 +{
  1.3023 +    RootedValue atomContentsVal(cx, unrootedAtomContents(atom));
  1.3024 +    return builder.identifier(atomContentsVal, pos, dst);
  1.3025 +}
  1.3026 +
  1.3027 +bool
  1.3028 +ASTSerializer::identifier(ParseNode *pn, MutableHandleValue dst)
  1.3029 +{
  1.3030 +    LOCAL_ASSERT(pn->isArity(PN_NAME) || pn->isArity(PN_NULLARY));
  1.3031 +    LOCAL_ASSERT(pn->pn_atom);
  1.3032 +
  1.3033 +    RootedAtom pnAtom(cx, pn->pn_atom);
  1.3034 +    return identifier(pnAtom, &pn->pn_pos, dst);
  1.3035 +}
  1.3036 +
  1.3037 +bool
  1.3038 +ASTSerializer::function(ParseNode *pn, ASTType type, MutableHandleValue dst)
  1.3039 +{
  1.3040 +    RootedFunction func(cx, pn->pn_funbox->function());
  1.3041 +
  1.3042 +    // FIXME: Provide more information (legacy generator vs star generator).
  1.3043 +    bool isGenerator = pn->pn_funbox->isGenerator();
  1.3044 +
  1.3045 +    bool isExpression =
  1.3046 +#if JS_HAS_EXPR_CLOSURES
  1.3047 +        func->isExprClosure();
  1.3048 +#else
  1.3049 +        false;
  1.3050 +#endif
  1.3051 +
  1.3052 +    RootedValue id(cx);
  1.3053 +    RootedAtom funcAtom(cx, func->atom());
  1.3054 +    if (!optIdentifier(funcAtom, nullptr, &id))
  1.3055 +        return false;
  1.3056 +
  1.3057 +    NodeVector args(cx);
  1.3058 +    NodeVector defaults(cx);
  1.3059 +
  1.3060 +    RootedValue body(cx), rest(cx);
  1.3061 +    if (func->hasRest())
  1.3062 +        rest.setUndefined();
  1.3063 +    else
  1.3064 +        rest.setNull();
  1.3065 +    return functionArgsAndBody(pn->pn_body, args, defaults, &body, &rest) &&
  1.3066 +        builder.function(type, &pn->pn_pos, id, args, defaults, body,
  1.3067 +                         rest, isGenerator, isExpression, dst);
  1.3068 +}
  1.3069 +
  1.3070 +bool
  1.3071 +ASTSerializer::functionArgsAndBody(ParseNode *pn, NodeVector &args, NodeVector &defaults,
  1.3072 +                                   MutableHandleValue body, MutableHandleValue rest)
  1.3073 +{
  1.3074 +    ParseNode *pnargs;
  1.3075 +    ParseNode *pnbody;
  1.3076 +
  1.3077 +    /* Extract the args and body separately. */
  1.3078 +    if (pn->isKind(PNK_ARGSBODY)) {
  1.3079 +        pnargs = pn;
  1.3080 +        pnbody = pn->last();
  1.3081 +    } else {
  1.3082 +        pnargs = nullptr;
  1.3083 +        pnbody = pn;
  1.3084 +    }
  1.3085 +
  1.3086 +    ParseNode *pndestruct;
  1.3087 +
  1.3088 +    /* Extract the destructuring assignments. */
  1.3089 +    if (pnbody->isArity(PN_LIST) && (pnbody->pn_xflags & PNX_DESTRUCT)) {
  1.3090 +        ParseNode *head = pnbody->pn_head;
  1.3091 +        LOCAL_ASSERT(head && head->isKind(PNK_SEMI));
  1.3092 +
  1.3093 +        pndestruct = head->pn_kid;
  1.3094 +        LOCAL_ASSERT(pndestruct);
  1.3095 +        LOCAL_ASSERT(pndestruct->isKind(PNK_VAR));
  1.3096 +    } else {
  1.3097 +        pndestruct = nullptr;
  1.3098 +    }
  1.3099 +
  1.3100 +    /* Serialize the arguments and body. */
  1.3101 +    switch (pnbody->getKind()) {
  1.3102 +      case PNK_RETURN: /* expression closure, no destructured args */
  1.3103 +        return functionArgs(pn, pnargs, nullptr, pnbody, args, defaults, rest) &&
  1.3104 +               expression(pnbody->pn_kid, body);
  1.3105 +
  1.3106 +      case PNK_SEQ:    /* expression closure with destructured args */
  1.3107 +      {
  1.3108 +        ParseNode *pnstart = pnbody->pn_head->pn_next;
  1.3109 +        LOCAL_ASSERT(pnstart && pnstart->isKind(PNK_RETURN));
  1.3110 +
  1.3111 +        return functionArgs(pn, pnargs, pndestruct, pnbody, args, defaults, rest) &&
  1.3112 +               expression(pnstart->pn_kid, body);
  1.3113 +      }
  1.3114 +
  1.3115 +      case PNK_STATEMENTLIST:     /* statement closure */
  1.3116 +      {
  1.3117 +        ParseNode *pnstart = (pnbody->pn_xflags & PNX_DESTRUCT)
  1.3118 +                               ? pnbody->pn_head->pn_next
  1.3119 +                               : pnbody->pn_head;
  1.3120 +
  1.3121 +        return functionArgs(pn, pnargs, pndestruct, pnbody, args, defaults, rest) &&
  1.3122 +               functionBody(pnstart, &pnbody->pn_pos, body);
  1.3123 +      }
  1.3124 +
  1.3125 +      default:
  1.3126 +        LOCAL_NOT_REACHED("unexpected function contents");
  1.3127 +    }
  1.3128 +}
  1.3129 +
  1.3130 +bool
  1.3131 +ASTSerializer::functionArgs(ParseNode *pn, ParseNode *pnargs, ParseNode *pndestruct,
  1.3132 +                            ParseNode *pnbody, NodeVector &args, NodeVector &defaults,
  1.3133 +                            MutableHandleValue rest)
  1.3134 +{
  1.3135 +    uint32_t i = 0;
  1.3136 +    ParseNode *arg = pnargs ? pnargs->pn_head : nullptr;
  1.3137 +    ParseNode *destruct = pndestruct ? pndestruct->pn_head : nullptr;
  1.3138 +    RootedValue node(cx);
  1.3139 +
  1.3140 +    /*
  1.3141 +     * Arguments are found in potentially two different places: 1) the
  1.3142 +     * argsbody sequence (which ends with the body node), or 2) a
  1.3143 +     * destructuring initialization at the beginning of the body. Loop
  1.3144 +     * |arg| through the argsbody and |destruct| through the initial
  1.3145 +     * destructuring assignments, stopping only when we've exhausted
  1.3146 +     * both.
  1.3147 +     */
  1.3148 +    while ((arg && arg != pnbody) || destruct) {
  1.3149 +        if (destruct && destruct->pn_right->frameSlot() == i) {
  1.3150 +            if (!pattern(destruct->pn_left, nullptr, &node) || !args.append(node))
  1.3151 +                return false;
  1.3152 +            destruct = destruct->pn_next;
  1.3153 +        } else if (arg && arg != pnbody) {
  1.3154 +            /*
  1.3155 +             * We don't check that arg->frameSlot() == i since we
  1.3156 +             * can't call that method if the arg def has been turned
  1.3157 +             * into a use, e.g.:
  1.3158 +             *
  1.3159 +             *     function(a) { function a() { } }
  1.3160 +             *
  1.3161 +             * There's no other way to ask a non-destructuring arg its
  1.3162 +             * index in the formals list, so we rely on the ability to
  1.3163 +             * ask destructuring args their index above.
  1.3164 +             */
  1.3165 +            JS_ASSERT(arg->isKind(PNK_NAME) || arg->isKind(PNK_ASSIGN));
  1.3166 +            ParseNode *argName = arg->isKind(PNK_NAME) ? arg : arg->pn_left;
  1.3167 +            if (!identifier(argName, &node))
  1.3168 +                return false;
  1.3169 +            if (rest.isUndefined() && arg->pn_next == pnbody)
  1.3170 +                rest.setObject(node.toObject());
  1.3171 +            else if (!args.append(node))
  1.3172 +                return false;
  1.3173 +            if (arg->pn_dflags & PND_DEFAULT) {
  1.3174 +                ParseNode *expr = arg->isDefn() ? arg->expr() : arg->pn_kid->pn_right;
  1.3175 +                RootedValue def(cx);
  1.3176 +                if (!expression(expr, &def) || !defaults.append(def))
  1.3177 +                    return false;
  1.3178 +            }
  1.3179 +            arg = arg->pn_next;
  1.3180 +        } else {
  1.3181 +            LOCAL_NOT_REACHED("missing function argument");
  1.3182 +        }
  1.3183 +        ++i;
  1.3184 +    }
  1.3185 +    JS_ASSERT(!rest.isUndefined());
  1.3186 +
  1.3187 +    return true;
  1.3188 +}
  1.3189 +
  1.3190 +bool
  1.3191 +ASTSerializer::functionBody(ParseNode *pn, TokenPos *pos, MutableHandleValue dst)
  1.3192 +{
  1.3193 +    NodeVector elts(cx);
  1.3194 +
  1.3195 +    /* We aren't sure how many elements there are up front, so we'll check each append. */
  1.3196 +    for (ParseNode *next = pn; next; next = next->pn_next) {
  1.3197 +        RootedValue child(cx);
  1.3198 +        if (!sourceElement(next, &child) || !elts.append(child))
  1.3199 +            return false;
  1.3200 +    }
  1.3201 +
  1.3202 +    return builder.blockStatement(elts, pos, dst);
  1.3203 +}
  1.3204 +
  1.3205 +static bool
  1.3206 +reflect_parse(JSContext *cx, uint32_t argc, jsval *vp)
  1.3207 +{
  1.3208 +    CallArgs args = CallArgsFromVp(argc, vp);
  1.3209 +
  1.3210 +    if (args.length() < 1) {
  1.3211 +        JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, JSMSG_MORE_ARGS_NEEDED,
  1.3212 +                             "Reflect.parse", "0", "s");
  1.3213 +        return false;
  1.3214 +    }
  1.3215 +
  1.3216 +    RootedString src(cx, ToString<CanGC>(cx, args[0]));
  1.3217 +    if (!src)
  1.3218 +        return false;
  1.3219 +
  1.3220 +    ScopedJSFreePtr<char> filename;
  1.3221 +    uint32_t lineno = 1;
  1.3222 +    bool loc = true;
  1.3223 +
  1.3224 +    RootedObject builder(cx);
  1.3225 +
  1.3226 +    RootedValue arg(cx, args.get(1));
  1.3227 +
  1.3228 +    if (!arg.isNullOrUndefined()) {
  1.3229 +        if (!arg.isObject()) {
  1.3230 +            js_ReportValueErrorFlags(cx, JSREPORT_ERROR, JSMSG_UNEXPECTED_TYPE,
  1.3231 +                                     JSDVG_SEARCH_STACK, arg, js::NullPtr(),
  1.3232 +                                     "not an object", nullptr);
  1.3233 +            return false;
  1.3234 +        }
  1.3235 +
  1.3236 +        RootedObject config(cx, &arg.toObject());
  1.3237 +
  1.3238 +        RootedValue prop(cx);
  1.3239 +
  1.3240 +        /* config.loc */
  1.3241 +        RootedId locId(cx, NameToId(cx->names().loc));
  1.3242 +        RootedValue trueVal(cx, BooleanValue(true));
  1.3243 +        if (!GetPropertyDefault(cx, config, locId, trueVal, &prop))
  1.3244 +            return false;
  1.3245 +
  1.3246 +        loc = ToBoolean(prop);
  1.3247 +
  1.3248 +        if (loc) {
  1.3249 +            /* config.source */
  1.3250 +            RootedId sourceId(cx, NameToId(cx->names().source));
  1.3251 +            RootedValue nullVal(cx, NullValue());
  1.3252 +            if (!GetPropertyDefault(cx, config, sourceId, nullVal, &prop))
  1.3253 +                return false;
  1.3254 +
  1.3255 +            if (!prop.isNullOrUndefined()) {
  1.3256 +                RootedString str(cx, ToString<CanGC>(cx, prop));
  1.3257 +                if (!str)
  1.3258 +                    return false;
  1.3259 +
  1.3260 +                size_t length = str->length();
  1.3261 +                const jschar *chars = str->getChars(cx);
  1.3262 +                if (!chars)
  1.3263 +                    return false;
  1.3264 +
  1.3265 +                TwoByteChars tbchars(chars, length);
  1.3266 +                filename = LossyTwoByteCharsToNewLatin1CharsZ(cx, tbchars).c_str();
  1.3267 +                if (!filename)
  1.3268 +                    return false;
  1.3269 +            }
  1.3270 +
  1.3271 +            /* config.line */
  1.3272 +            RootedId lineId(cx, NameToId(cx->names().line));
  1.3273 +            RootedValue oneValue(cx, Int32Value(1));
  1.3274 +            if (!GetPropertyDefault(cx, config, lineId, oneValue, &prop) ||
  1.3275 +                !ToUint32(cx, prop, &lineno)) {
  1.3276 +                return false;
  1.3277 +            }
  1.3278 +        }
  1.3279 +
  1.3280 +        /* config.builder */
  1.3281 +        RootedId builderId(cx, NameToId(cx->names().builder));
  1.3282 +        RootedValue nullVal(cx, NullValue());
  1.3283 +        if (!GetPropertyDefault(cx, config, builderId, nullVal, &prop))
  1.3284 +            return false;
  1.3285 +
  1.3286 +        if (!prop.isNullOrUndefined()) {
  1.3287 +            if (!prop.isObject()) {
  1.3288 +                js_ReportValueErrorFlags(cx, JSREPORT_ERROR, JSMSG_UNEXPECTED_TYPE,
  1.3289 +                                         JSDVG_SEARCH_STACK, prop, js::NullPtr(),
  1.3290 +                                         "not an object", nullptr);
  1.3291 +                return false;
  1.3292 +            }
  1.3293 +            builder = &prop.toObject();
  1.3294 +        }
  1.3295 +    }
  1.3296 +
  1.3297 +    /* Extract the builder methods first to report errors before parsing. */
  1.3298 +    ASTSerializer serialize(cx, loc, filename, lineno);
  1.3299 +    if (!serialize.init(builder))
  1.3300 +        return false;
  1.3301 +
  1.3302 +    JSFlatString *flat = src->ensureFlat(cx);
  1.3303 +    if (!flat)
  1.3304 +        return false;
  1.3305 +
  1.3306 +    CompileOptions options(cx);
  1.3307 +    options.setFileAndLine(filename, lineno);
  1.3308 +    options.setCanLazilyParse(false);
  1.3309 +    Parser<FullParseHandler> parser(cx, &cx->tempLifoAlloc(), options, flat->chars(),
  1.3310 +                                    flat->length(), /* foldConstants = */ false, nullptr, nullptr);
  1.3311 +
  1.3312 +    serialize.setParser(&parser);
  1.3313 +
  1.3314 +    ParseNode *pn = parser.parse(nullptr);
  1.3315 +    if (!pn)
  1.3316 +        return false;
  1.3317 +
  1.3318 +    RootedValue val(cx);
  1.3319 +    if (!serialize.program(pn, &val)) {
  1.3320 +        args.rval().setNull();
  1.3321 +        return false;
  1.3322 +    }
  1.3323 +
  1.3324 +    args.rval().set(val);
  1.3325 +    return true;
  1.3326 +}
  1.3327 +
  1.3328 +JS_PUBLIC_API(JSObject *)
  1.3329 +JS_InitReflect(JSContext *cx, HandleObject obj)
  1.3330 +{
  1.3331 +    static const JSFunctionSpec static_methods[] = {
  1.3332 +        JS_FN("parse", reflect_parse, 1, 0),
  1.3333 +        JS_FS_END
  1.3334 +    };
  1.3335 +
  1.3336 +    RootedObject proto(cx, obj->as<GlobalObject>().getOrCreateObjectPrototype(cx));
  1.3337 +    if (!proto)
  1.3338 +        return nullptr;
  1.3339 +    RootedObject Reflect(cx, NewObjectWithGivenProto(cx, &JSObject::class_, proto,
  1.3340 +                                                     obj, SingletonObject));
  1.3341 +    if (!Reflect)
  1.3342 +        return nullptr;
  1.3343 +
  1.3344 +    if (!JS_DefineProperty(cx, obj, "Reflect", Reflect, 0,
  1.3345 +                           JS_PropertyStub, JS_StrictPropertyStub)) {
  1.3346 +        return nullptr;
  1.3347 +    }
  1.3348 +
  1.3349 +    if (!JS_DefineFunctions(cx, Reflect, static_methods))
  1.3350 +        return nullptr;
  1.3351 +
  1.3352 +    return Reflect;
  1.3353 +}

mercurial