| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
/* JS reflection package. */ |
| 8 |
|
| 9 |
#include "mozilla/DebugOnly.h" |
| 10 |
|
| 11 |
#include <stdlib.h> |
| 12 |
#include <utility> |
| 13 |
|
| 14 |
#include "jspubtd.h" |
| 15 |
|
| 16 |
#include "builtin/Array.h" |
| 17 |
#include "frontend/CompilationStencil.h" |
| 18 |
#include "frontend/FrontendContext.h" // AutoReportFrontendContext |
| 19 |
#include "frontend/ModuleSharedContext.h" |
| 20 |
#include "frontend/ParseNode.h" |
| 21 |
#include "frontend/Parser.h" |
| 22 |
#include "js/ColumnNumber.h" // JS::LimitedColumnNumberOneOrigin |
| 23 |
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_* |
| 24 |
#include "js/friend/StackLimits.h" // js::AutoCheckRecursionLimit |
| 25 |
#include "js/PropertyAndElement.h" // JS_DefineFunction |
| 26 |
#include "js/StableStringChars.h" |
| 27 |
#include "vm/FunctionFlags.h" // js::FunctionFlags |
| 28 |
#include "vm/Interpreter.h" |
| 29 |
#include "vm/JSAtomUtils.h" // Atomize, AtomizeUTF8Chars |
| 30 |
#include "vm/JSObject.h" |
| 31 |
#include "vm/ModuleBuilder.h" // js::ModuleBuilder |
| 32 |
#include "vm/PlainObject.h" // js::PlainObject |
| 33 |
#include "vm/RegExpObject.h" |
| 34 |
|
| 35 |
#include "vm/JSContext-inl.h" |
| 36 |
#include "vm/JSObject-inl.h" |
| 37 |
#include "vm/ObjectOperations-inl.h" |
| 38 |
|
| 39 |
using namespace js; |
| 40 |
using namespace js::frontend; |
| 41 |
|
| 42 |
using JS::AutoStableStringChars; |
| 43 |
using JS::CompileOptions; |
| 44 |
using JS::RootedValueArray; |
| 45 |
using mozilla::DebugOnly; |
| 46 |
|
| 47 |
enum ASTType { |
| 48 |
AST_ERROR = -1, |
| 49 |
#define ASTDEF(ast, str) ast, |
| 50 |
#include "jsast.tbl" |
| 51 |
#undef ASTDEF |
| 52 |
AST_LIMIT |
| 53 |
}; |
| 54 |
|
| 55 |
enum AssignmentOperator { |
| 56 |
AOP_ERR = -1, |
| 57 |
|
| 58 |
/* assign */ |
| 59 |
AOP_ASSIGN = 0, |
| 60 |
/* operator-assign */ |
| 61 |
AOP_PLUS, |
| 62 |
AOP_MINUS, |
| 63 |
AOP_STAR, |
| 64 |
AOP_DIV, |
| 65 |
AOP_MOD, |
| 66 |
AOP_POW, |
| 67 |
/* shift-assign */ |
| 68 |
AOP_LSH, |
| 69 |
AOP_RSH, |
| 70 |
AOP_URSH, |
| 71 |
/* binary */ |
| 72 |
AOP_BITOR, |
| 73 |
AOP_BITXOR, |
| 74 |
AOP_BITAND, |
| 75 |
/* short-circuit */ |
| 76 |
AOP_COALESCE, |
| 77 |
AOP_OR, |
| 78 |
AOP_AND, |
| 79 |
|
| 80 |
AOP_LIMIT |
| 81 |
}; |
| 82 |
|
| 83 |
enum BinaryOperator { |
| 84 |
BINOP_ERR = -1, |
| 85 |
|
| 86 |
/* eq */ |
| 87 |
BINOP_EQ = 0, |
| 88 |
BINOP_NE, |
| 89 |
BINOP_STRICTEQ, |
| 90 |
BINOP_STRICTNE, |
| 91 |
/* rel */ |
| 92 |
BINOP_LT, |
| 93 |
BINOP_LE, |
| 94 |
BINOP_GT, |
| 95 |
BINOP_GE, |
| 96 |
/* shift */ |
| 97 |
BINOP_LSH, |
| 98 |
BINOP_RSH, |
| 99 |
BINOP_URSH, |
| 100 |
/* arithmetic */ |
| 101 |
BINOP_ADD, |
| 102 |
BINOP_SUB, |
| 103 |
BINOP_STAR, |
| 104 |
BINOP_DIV, |
| 105 |
BINOP_MOD, |
| 106 |
BINOP_POW, |
| 107 |
/* binary */ |
| 108 |
BINOP_BITOR, |
| 109 |
BINOP_BITXOR, |
| 110 |
BINOP_BITAND, |
| 111 |
/* misc */ |
| 112 |
BINOP_IN, |
| 113 |
BINOP_INSTANCEOF, |
| 114 |
BINOP_COALESCE, |
| 115 |
|
| 116 |
BINOP_LIMIT |
| 117 |
}; |
| 118 |
|
| 119 |
enum UnaryOperator { |
| 120 |
UNOP_ERR = -1, |
| 121 |
|
| 122 |
UNOP_DELETE = 0, |
| 123 |
UNOP_NEG, |
| 124 |
UNOP_POS, |
| 125 |
UNOP_NOT, |
| 126 |
UNOP_BITNOT, |
| 127 |
UNOP_TYPEOF, |
| 128 |
UNOP_VOID, |
| 129 |
UNOP_AWAIT, |
| 130 |
|
| 131 |
UNOP_LIMIT |
| 132 |
}; |
| 133 |
|
| 134 |
enum VarDeclKind { |
| 135 |
VARDECL_ERR = -1, |
| 136 |
VARDECL_VAR = 0, |
| 137 |
VARDECL_CONST, |
| 138 |
VARDECL_LET, |
| 139 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 140 |
VARDECL_USING, |
| 141 |
VARDECL_AWAIT_USING, |
| 142 |
#endif |
| 143 |
VARDECL_LIMIT |
| 144 |
}; |
| 145 |
|
| 146 |
enum PropKind { |
| 147 |
PROP_ERR = -1, |
| 148 |
PROP_INIT = 0, |
| 149 |
PROP_GETTER, |
| 150 |
PROP_SETTER, |
| 151 |
PROP_MUTATEPROTO, |
| 152 |
PROP_LIMIT |
| 153 |
}; |
| 154 |
|
| 155 |
static const char* const aopNames[] = { |
| 156 |
"=", /* AOP_ASSIGN */ |
| 157 |
"+=", /* AOP_PLUS */ |
| 158 |
"-=", /* AOP_MINUS */ |
| 159 |
"*=", /* AOP_STAR */ |
| 160 |
"/=", /* AOP_DIV */ |
| 161 |
"%=", /* AOP_MOD */ |
| 162 |
"**=", /* AOP_POW */ |
| 163 |
"<<=", /* AOP_LSH */ |
| 164 |
">>=", /* AOP_RSH */ |
| 165 |
">>>=", /* AOP_URSH */ |
| 166 |
"|=", /* AOP_BITOR */ |
| 167 |
"^=", /* AOP_BITXOR */ |
| 168 |
"&=", /* AOP_BITAND */ |
| 169 |
"\?\?=", /* AOP_COALESCE */ |
| 170 |
"||=", /* AOP_OR */ |
| 171 |
"&&=", /* AOP_AND */ |
| 172 |
}; |
| 173 |
|
| 174 |
static const char* const binopNames[] = { |
| 175 |
"==", /* BINOP_EQ */ |
| 176 |
"!=", /* BINOP_NE */ |
| 177 |
"===", /* BINOP_STRICTEQ */ |
| 178 |
"!==", /* BINOP_STRICTNE */ |
| 179 |
"<", /* BINOP_LT */ |
| 180 |
"<=", /* BINOP_LE */ |
| 181 |
">", /* BINOP_GT */ |
| 182 |
">=", /* BINOP_GE */ |
| 183 |
"<<", /* BINOP_LSH */ |
| 184 |
">>", /* BINOP_RSH */ |
| 185 |
">>>", /* BINOP_URSH */ |
| 186 |
"+", /* BINOP_PLUS */ |
| 187 |
"-", /* BINOP_MINUS */ |
| 188 |
"*", /* BINOP_STAR */ |
| 189 |
"/", /* BINOP_DIV */ |
| 190 |
"%", /* BINOP_MOD */ |
| 191 |
"**", /* BINOP_POW */ |
| 192 |
"|", /* BINOP_BITOR */ |
| 193 |
"^", /* BINOP_BITXOR */ |
| 194 |
"&", /* BINOP_BITAND */ |
| 195 |
"in", /* BINOP_IN */ |
| 196 |
"instanceof", /* BINOP_INSTANCEOF */ |
| 197 |
"??", /* BINOP_COALESCE */ |
| 198 |
}; |
| 199 |
|
| 200 |
static const char* const unopNames[] = { |
| 201 |
"delete", /* UNOP_DELETE */ |
| 202 |
"-", /* UNOP_NEG */ |
| 203 |
"+", /* UNOP_POS */ |
| 204 |
"!", /* UNOP_NOT */ |
| 205 |
"~", /* UNOP_BITNOT */ |
| 206 |
"typeof", /* UNOP_TYPEOF */ |
| 207 |
"void", /* UNOP_VOID */ |
| 208 |
"await", /* UNOP_AWAIT */ |
| 209 |
}; |
| 210 |
|
| 211 |
static const char* const nodeTypeNames[] = { |
| 212 |
#define ASTDEF(ast, str) str, |
| 213 |
#include "jsast.tbl" |
| 214 |
#undef ASTDEF |
| 215 |
nullptr}; |
| 216 |
|
| 217 |
enum YieldKind { Delegating, NotDelegating }; |
| 218 |
|
| 219 |
using NodeVector = RootedValueVector; |
| 220 |
|
| 221 |
/* |
| 222 |
* ParseNode is a somewhat intricate data structure, and its invariants have |
| 223 |
* evolved, making it more likely that there could be a disconnect between the |
| 224 |
* parser and the AST serializer. We use these macros to check invariants on a |
| 225 |
* parse node and raise a dynamic error on failure. |
| 226 |
*/ |
| 227 |
#define LOCAL_ASSERT(expr) \ |
| 228 |
JS_BEGIN_MACRO \ |
| 229 |
MOZ_ASSERT(expr); \ |
| 230 |
if (!(expr)) { \ |
| 231 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, \ |
| 232 |
JSMSG_BAD_PARSE_NODE); \ |
| 233 |
return false; \ |
| 234 |
} \ |
| 235 |
JS_END_MACRO |
| 236 |
|
| 237 |
#define LOCAL_NOT_REACHED(expr) \ |
| 238 |
JS_BEGIN_MACRO \ |
| 239 |
MOZ_ASSERT(false); \ |
| 240 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, \ |
| 241 |
JSMSG_BAD_PARSE_NODE); \ |
| 242 |
return false; \ |
| 243 |
JS_END_MACRO |
| 244 |
|
| 245 |
namespace { |
| 246 |
|
| 247 |
/* Set 'result' to obj[id] if any such property exists, else defaultValue. */ |
| 248 |
static bool GetPropertyDefault(JSContext* cx, HandleObject obj, HandleId id, |
| 249 |
HandleValue defaultValue, |
| 250 |
MutableHandleValue result) { |
| 251 |
bool found; |
| 252 |
if (!HasProperty(cx, obj, id, &found)) { |
| 253 |
return false; |
| 254 |
} |
| 255 |
if (!found) { |
| 256 |
result.set(defaultValue); |
| 257 |
return true; |
| 258 |
} |
| 259 |
return GetProperty(cx, obj, obj, id, result); |
| 260 |
} |
| 261 |
|
| 262 |
enum class GeneratorStyle { None, ES6 }; |
| 263 |
|
| 264 |
/* |
| 265 |
* Builder class that constructs JavaScript AST node objects. |
| 266 |
*/ |
| 267 |
class NodeBuilder { |
| 268 |
using CallbackArray = RootedValueArray<AST_LIMIT>; |
| 269 |
|
| 270 |
JSContext* cx; |
| 271 |
FrontendContext* fc; |
| 272 |
frontend::Parser<frontend::FullParseHandler, char16_t>* parser; |
| 273 |
bool saveLoc; /* save source location information? */ |
| 274 |
char const* src; /* UTF-8 encoded source filename or null */ |
| 275 |
RootedValue srcval; /* source filename JS value or null */ |
| 276 |
|
| 277 |
public: |
| 278 |
NodeBuilder(JSContext* c, FrontendContext* f, bool l, char const* s) |
| 279 |
: cx(c), fc(f), parser(nullptr), saveLoc(l), src(s), srcval(c) {} |
| 280 |
|
| 281 |
[[nodiscard]] bool init() { |
| 282 |
if (src) { |
| 283 |
if (!atomValueUtf8(src, &srcval)) { |
| 284 |
return false; |
| 285 |
} |
| 286 |
} else { |
| 287 |
srcval.setNull(); |
| 288 |
} |
| 289 |
|
| 290 |
return true; |
| 291 |
} |
| 292 |
|
| 293 |
void setParser(frontend::Parser<frontend::FullParseHandler, char16_t>* p) { |
| 294 |
parser = p; |
| 295 |
} |
| 296 |
|
| 297 |
private: |
| 298 |
[[nodiscard]] bool atomValue(const char* s, MutableHandleValue dst) { |
| 299 |
MOZ_ASSERT(JS::StringIsASCII(s)); |
| 300 |
|
| 301 |
/* |
| 302 |
* Bug 575416: instead of Atomize, lookup constant atoms in tbl file |
| 303 |
*/ |
| 304 |
Rooted<JSAtom*> atom(cx, Atomize(cx, s, strlen(s))); |
| 305 |
if (!atom) { |
| 306 |
return false; |
| 307 |
} |
| 308 |
|
| 309 |
dst.setString(atom); |
| 310 |
return true; |
| 311 |
} |
| 312 |
|
| 313 |
[[nodiscard]] bool atomValueUtf8(const char* s, MutableHandleValue dst) { |
| 314 |
Rooted<JSAtom*> atom(cx, AtomizeUTF8Chars(cx, s, strlen(s))); |
| 315 |
if (!atom) { |
| 316 |
return false; |
| 317 |
} |
| 318 |
|
| 319 |
dst.setString(atom); |
| 320 |
return true; |
| 321 |
} |
| 322 |
|
| 323 |
[[nodiscard]] bool newObject(MutableHandleObject dst) { |
| 324 |
Rooted<PlainObject*> nobj(cx, NewPlainObject(cx)); |
| 325 |
if (!nobj) { |
| 326 |
return false; |
| 327 |
} |
| 328 |
|
| 329 |
dst.set(nobj); |
| 330 |
return true; |
| 331 |
} |
| 332 |
|
| 333 |
[[nodiscard]] bool newArray(NodeVector& elts, MutableHandleValue dst); |
| 334 |
|
| 335 |
[[nodiscard]] bool createNode(ASTType type, TokenPos* pos, |
| 336 |
MutableHandleObject dst); |
| 337 |
|
| 338 |
[[nodiscard]] bool newNodeHelper(HandleObject obj, MutableHandleValue dst) { |
| 339 |
// The end of the implementation of newNode(). |
| 340 |
MOZ_ASSERT(obj); |
| 341 |
dst.setObject(*obj); |
| 342 |
return true; |
| 343 |
} |
| 344 |
|
| 345 |
template <typename... Arguments> |
| 346 |
[[nodiscard]] bool newNodeHelper(HandleObject obj, const char* name, |
| 347 |
HandleValue value, Arguments&&... rest) { |
| 348 |
// Recursive loop to define properties. Note that the newNodeHelper() |
| 349 |
// call below passes two fewer arguments than we received, as we omit |
| 350 |
// `name` and `value`. This eventually bottoms out in a call to the |
| 351 |
// non-template newNodeHelper() above. |
| 352 |
return defineProperty(obj, name, value) && |
| 353 |
newNodeHelper(obj, std::forward<Arguments>(rest)...); |
| 354 |
} |
| 355 |
|
| 356 |
// Create a node object with "type" and "loc" properties, as well as zero |
| 357 |
// or more properties passed in as arguments. The signature is really more |
| 358 |
// like: |
| 359 |
// |
| 360 |
// bool newNode(ASTType type, TokenPos* pos, |
| 361 |
// {const char *name0, HandleValue value0,}... |
| 362 |
// MutableHandleValue dst); |
| 363 |
template <typename... Arguments> |
| 364 |
[[nodiscard]] bool newNode(ASTType type, TokenPos* pos, Arguments&&... args) { |
| 365 |
RootedObject node(cx); |
| 366 |
return createNode(type, pos, &node) && |
| 367 |
newNodeHelper(node, std::forward<Arguments>(args)...); |
| 368 |
} |
| 369 |
|
| 370 |
[[nodiscard]] bool listNode(ASTType type, const char* propName, |
| 371 |
NodeVector& elts, TokenPos* pos, |
| 372 |
MutableHandleValue dst) { |
| 373 |
RootedValue array(cx); |
| 374 |
if (!newArray(elts, &array)) { |
| 375 |
return false; |
| 376 |
} |
| 377 |
|
| 378 |
return newNode(type, pos, propName, array, dst); |
| 379 |
} |
| 380 |
|
| 381 |
[[nodiscard]] bool defineProperty(HandleObject obj, const char* name, |
| 382 |
HandleValue val) { |
| 383 |
MOZ_ASSERT_IF(val.isMagic(), val.whyMagic() == JS_SERIALIZE_NO_NODE); |
| 384 |
|
| 385 |
/* |
| 386 |
* Bug 575416: instead of Atomize, lookup constant atoms in tbl file |
| 387 |
*/ |
| 388 |
Rooted<JSAtom*> atom(cx, Atomize(cx, name, strlen(name))); |
| 389 |
if (!atom) { |
| 390 |
return false; |
| 391 |
} |
| 392 |
|
| 393 |
// Represent "no node" as null and ensure users are not exposed to magic |
| 394 |
// values. |
| 395 |
RootedValue optVal(cx, |
| 396 |
val.isMagic(JS_SERIALIZE_NO_NODE) ? NullValue() : val); |
| 397 |
return DefineDataProperty(cx, obj, atom->asPropertyName(), optVal); |
| 398 |
} |
| 399 |
|
| 400 |
[[nodiscard]] bool newNodeLoc(TokenPos* pos, MutableHandleValue dst); |
| 401 |
|
| 402 |
[[nodiscard]] bool setNodeLoc(HandleObject node, TokenPos* pos); |
| 403 |
|
| 404 |
public: |
| 405 |
/* |
| 406 |
* All of the public builder methods take as their last two |
| 407 |
* arguments a nullable token position and a non-nullable, rooted |
| 408 |
* outparam. |
| 409 |
* |
| 410 |
* Any Value arguments representing optional subnodes may be a |
| 411 |
* JS_SERIALIZE_NO_NODE magic value. |
| 412 |
*/ |
| 413 |
|
| 414 |
/* |
| 415 |
* misc nodes |
| 416 |
*/ |
| 417 |
|
| 418 |
[[nodiscard]] bool program(NodeVector& elts, TokenPos* pos, |
| 419 |
MutableHandleValue dst); |
| 420 |
|
| 421 |
[[nodiscard]] bool literal(HandleValue val, TokenPos* pos, |
| 422 |
MutableHandleValue dst); |
| 423 |
|
| 424 |
[[nodiscard]] bool identifier(HandleValue name, TokenPos* pos, |
| 425 |
MutableHandleValue dst); |
| 426 |
|
| 427 |
[[nodiscard]] bool function(ASTType type, TokenPos* pos, HandleValue id, |
| 428 |
NodeVector& args, NodeVector& defaults, |
| 429 |
HandleValue body, HandleValue rest, |
| 430 |
GeneratorStyle generatorStyle, bool isAsync, |
| 431 |
bool isExpression, MutableHandleValue dst); |
| 432 |
|
| 433 |
[[nodiscard]] bool variableDeclarator(HandleValue id, HandleValue init, |
| 434 |
TokenPos* pos, MutableHandleValue dst); |
| 435 |
|
| 436 |
[[nodiscard]] bool switchCase(HandleValue expr, NodeVector& elts, |
| 437 |
TokenPos* pos, MutableHandleValue dst); |
| 438 |
|
| 439 |
[[nodiscard]] bool catchClause(HandleValue var, HandleValue body, |
| 440 |
TokenPos* pos, MutableHandleValue dst); |
| 441 |
|
| 442 |
[[nodiscard]] bool prototypeMutation(HandleValue val, TokenPos* pos, |
| 443 |
MutableHandleValue dst); |
| 444 |
[[nodiscard]] bool propertyInitializer(HandleValue key, HandleValue val, |
| 445 |
PropKind kind, bool isShorthand, |
| 446 |
bool isMethod, TokenPos* pos, |
| 447 |
MutableHandleValue dst); |
| 448 |
|
| 449 |
/* |
| 450 |
* statements |
| 451 |
*/ |
| 452 |
|
| 453 |
[[nodiscard]] bool blockStatement(NodeVector& elts, TokenPos* pos, |
| 454 |
MutableHandleValue dst); |
| 455 |
|
| 456 |
[[nodiscard]] bool expressionStatement(HandleValue expr, TokenPos* pos, |
| 457 |
MutableHandleValue dst); |
| 458 |
|
| 459 |
[[nodiscard]] bool emptyStatement(TokenPos* pos, MutableHandleValue dst); |
| 460 |
|
| 461 |
[[nodiscard]] bool ifStatement(HandleValue test, HandleValue cons, |
| 462 |
HandleValue alt, TokenPos* pos, |
| 463 |
MutableHandleValue dst); |
| 464 |
|
| 465 |
[[nodiscard]] bool breakStatement(HandleValue label, TokenPos* pos, |
| 466 |
MutableHandleValue dst); |
| 467 |
|
| 468 |
[[nodiscard]] bool continueStatement(HandleValue label, TokenPos* pos, |
| 469 |
MutableHandleValue dst); |
| 470 |
|
| 471 |
[[nodiscard]] bool labeledStatement(HandleValue label, HandleValue stmt, |
| 472 |
TokenPos* pos, MutableHandleValue dst); |
| 473 |
|
| 474 |
[[nodiscard]] bool throwStatement(HandleValue arg, TokenPos* pos, |
| 475 |
MutableHandleValue dst); |
| 476 |
|
| 477 |
[[nodiscard]] bool returnStatement(HandleValue arg, TokenPos* pos, |
| 478 |
MutableHandleValue dst); |
| 479 |
|
| 480 |
[[nodiscard]] bool forStatement(HandleValue init, HandleValue test, |
| 481 |
HandleValue update, HandleValue stmt, |
| 482 |
TokenPos* pos, MutableHandleValue dst); |
| 483 |
|
| 484 |
[[nodiscard]] bool forInStatement(HandleValue var, HandleValue expr, |
| 485 |
HandleValue stmt, TokenPos* pos, |
| 486 |
MutableHandleValue dst); |
| 487 |
|
| 488 |
[[nodiscard]] bool forOfStatement(HandleValue var, HandleValue expr, |
| 489 |
HandleValue stmt, TokenPos* pos, |
| 490 |
MutableHandleValue dst); |
| 491 |
|
| 492 |
[[nodiscard]] bool withStatement(HandleValue expr, HandleValue stmt, |
| 493 |
TokenPos* pos, MutableHandleValue dst); |
| 494 |
|
| 495 |
[[nodiscard]] bool whileStatement(HandleValue test, HandleValue stmt, |
| 496 |
TokenPos* pos, MutableHandleValue dst); |
| 497 |
|
| 498 |
[[nodiscard]] bool doWhileStatement(HandleValue stmt, HandleValue test, |
| 499 |
TokenPos* pos, MutableHandleValue dst); |
| 500 |
|
| 501 |
[[nodiscard]] bool switchStatement(HandleValue disc, NodeVector& elts, |
| 502 |
bool lexical, TokenPos* pos, |
| 503 |
MutableHandleValue dst); |
| 504 |
|
| 505 |
[[nodiscard]] bool tryStatement(HandleValue body, HandleValue handler, |
| 506 |
HandleValue finally, TokenPos* pos, |
| 507 |
MutableHandleValue dst); |
| 508 |
|
| 509 |
[[nodiscard]] bool debuggerStatement(TokenPos* pos, MutableHandleValue dst); |
| 510 |
|
| 511 |
[[nodiscard]] bool moduleRequest(HandleValue moduleSpec, |
| 512 |
NodeVector& attributes, TokenPos* pos, |
| 513 |
MutableHandleValue dst); |
| 514 |
|
| 515 |
[[nodiscard]] bool importAttribute(HandleValue key, HandleValue value, |
| 516 |
TokenPos* pos, MutableHandleValue dst); |
| 517 |
|
| 518 |
[[nodiscard]] bool importDeclaration(NodeVector& elts, HandleValue moduleSpec, |
| 519 |
TokenPos* pos, MutableHandleValue dst); |
| 520 |
|
| 521 |
[[nodiscard]] bool importSpecifier(HandleValue importName, |
| 522 |
HandleValue bindingName, TokenPos* pos, |
| 523 |
MutableHandleValue dst); |
| 524 |
|
| 525 |
[[nodiscard]] bool importNamespaceSpecifier(HandleValue bindingName, |
| 526 |
TokenPos* pos, |
| 527 |
MutableHandleValue dst); |
| 528 |
|
| 529 |
[[nodiscard]] bool exportDeclaration(HandleValue decl, NodeVector& elts, |
| 530 |
HandleValue moduleSpec, |
| 531 |
HandleValue isDefault, TokenPos* pos, |
| 532 |
MutableHandleValue dst); |
| 533 |
|
| 534 |
[[nodiscard]] bool exportSpecifier(HandleValue bindingName, |
| 535 |
HandleValue exportName, TokenPos* pos, |
| 536 |
MutableHandleValue dst); |
| 537 |
|
| 538 |
[[nodiscard]] bool exportNamespaceSpecifier(HandleValue exportName, |
| 539 |
TokenPos* pos, |
| 540 |
MutableHandleValue dst); |
| 541 |
|
| 542 |
[[nodiscard]] bool exportBatchSpecifier(TokenPos* pos, |
| 543 |
MutableHandleValue dst); |
| 544 |
|
| 545 |
[[nodiscard]] bool classDefinition(bool expr, HandleValue name, |
| 546 |
HandleValue heritage, HandleValue block, |
| 547 |
#ifdef ENABLE_DECORATORS |
| 548 |
HandleValue decorators, |
| 549 |
#endif |
| 550 |
TokenPos* pos, MutableHandleValue dst); |
| 551 |
[[nodiscard]] bool classMembers(NodeVector& members, MutableHandleValue dst); |
| 552 |
[[nodiscard]] bool classMethod(HandleValue name, HandleValue body, |
| 553 |
#ifdef ENABLE_DECORATORS |
| 554 |
HandleValue decorators, |
| 555 |
#endif |
| 556 |
PropKind kind, bool isStatic, TokenPos* pos, |
| 557 |
MutableHandleValue dst); |
| 558 |
[[nodiscard]] bool classField(HandleValue name, HandleValue initializer, |
| 559 |
#ifdef ENABLE_DECORATORS |
| 560 |
HandleValue decorators, |
| 561 |
#endif |
| 562 |
TokenPos* pos, MutableHandleValue dst); |
| 563 |
[[nodiscard]] bool staticClassBlock(HandleValue body, TokenPos* pos, |
| 564 |
MutableHandleValue dst); |
| 565 |
|
| 566 |
/* |
| 567 |
* expressions |
| 568 |
*/ |
| 569 |
|
| 570 |
[[nodiscard]] bool binaryExpression(BinaryOperator op, HandleValue left, |
| 571 |
HandleValue right, TokenPos* pos, |
| 572 |
MutableHandleValue dst); |
| 573 |
|
| 574 |
[[nodiscard]] bool unaryExpression(UnaryOperator op, HandleValue expr, |
| 575 |
TokenPos* pos, MutableHandleValue dst); |
| 576 |
|
| 577 |
[[nodiscard]] bool assignmentExpression(AssignmentOperator op, |
| 578 |
HandleValue lhs, HandleValue rhs, |
| 579 |
TokenPos* pos, |
| 580 |
MutableHandleValue dst); |
| 581 |
|
| 582 |
[[nodiscard]] bool updateExpression(HandleValue expr, bool incr, bool prefix, |
| 583 |
TokenPos* pos, MutableHandleValue dst); |
| 584 |
|
| 585 |
[[nodiscard]] bool logicalExpression(ParseNodeKind pnk, HandleValue left, |
| 586 |
HandleValue right, TokenPos* pos, |
| 587 |
MutableHandleValue dst); |
| 588 |
|
| 589 |
[[nodiscard]] bool conditionalExpression(HandleValue test, HandleValue cons, |
| 590 |
HandleValue alt, TokenPos* pos, |
| 591 |
MutableHandleValue dst); |
| 592 |
|
| 593 |
[[nodiscard]] bool sequenceExpression(NodeVector& elts, TokenPos* pos, |
| 594 |
MutableHandleValue dst); |
| 595 |
|
| 596 |
[[nodiscard]] bool newExpression(HandleValue callee, NodeVector& args, |
| 597 |
TokenPos* pos, MutableHandleValue dst); |
| 598 |
|
| 599 |
[[nodiscard]] bool callExpression(HandleValue callee, NodeVector& args, |
| 600 |
TokenPos* pos, MutableHandleValue dst, |
| 601 |
bool isOptional = false); |
| 602 |
|
| 603 |
[[nodiscard]] bool memberExpression(bool computed, HandleValue expr, |
| 604 |
HandleValue member, TokenPos* pos, |
| 605 |
MutableHandleValue dst, |
| 606 |
bool isOptional = false); |
| 607 |
|
| 608 |
[[nodiscard]] bool arrayExpression(NodeVector& elts, TokenPos* pos, |
| 609 |
MutableHandleValue dst); |
| 610 |
|
| 611 |
[[nodiscard]] bool templateLiteral(NodeVector& elts, TokenPos* pos, |
| 612 |
MutableHandleValue dst); |
| 613 |
|
| 614 |
[[nodiscard]] bool taggedTemplate(HandleValue callee, NodeVector& args, |
| 615 |
TokenPos* pos, MutableHandleValue dst); |
| 616 |
|
| 617 |
[[nodiscard]] bool callSiteObj(NodeVector& raw, NodeVector& cooked, |
| 618 |
TokenPos* pos, MutableHandleValue dst); |
| 619 |
|
| 620 |
[[nodiscard]] bool spreadExpression(HandleValue expr, TokenPos* pos, |
| 621 |
MutableHandleValue dst); |
| 622 |
|
| 623 |
[[nodiscard]] bool optionalExpression(HandleValue expr, TokenPos* pos, |
| 624 |
MutableHandleValue dst); |
| 625 |
|
| 626 |
[[nodiscard]] bool deleteOptionalExpression(HandleValue expr, TokenPos* pos, |
| 627 |
MutableHandleValue dst); |
| 628 |
|
| 629 |
[[nodiscard]] bool computedName(HandleValue name, TokenPos* pos, |
| 630 |
MutableHandleValue dst); |
| 631 |
|
| 632 |
[[nodiscard]] bool objectExpression(NodeVector& elts, TokenPos* pos, |
| 633 |
MutableHandleValue dst); |
| 634 |
|
| 635 |
[[nodiscard]] bool thisExpression(TokenPos* pos, MutableHandleValue dst); |
| 636 |
|
| 637 |
[[nodiscard]] bool yieldExpression(HandleValue arg, YieldKind kind, |
| 638 |
TokenPos* pos, MutableHandleValue dst); |
| 639 |
|
| 640 |
[[nodiscard]] bool metaProperty(HandleValue meta, HandleValue property, |
| 641 |
TokenPos* pos, MutableHandleValue dst); |
| 642 |
|
| 643 |
[[nodiscard]] bool callImportExpression(HandleValue ident, NodeVector& args, |
| 644 |
TokenPos* pos, |
| 645 |
MutableHandleValue dst); |
| 646 |
|
| 647 |
[[nodiscard]] bool super(TokenPos* pos, MutableHandleValue dst); |
| 648 |
|
| 649 |
/* |
| 650 |
* declarations |
| 651 |
*/ |
| 652 |
|
| 653 |
[[nodiscard]] bool variableDeclaration(NodeVector& elts, VarDeclKind kind, |
| 654 |
TokenPos* pos, MutableHandleValue dst); |
| 655 |
|
| 656 |
/* |
| 657 |
* patterns |
| 658 |
*/ |
| 659 |
|
| 660 |
[[nodiscard]] bool arrayPattern(NodeVector& elts, TokenPos* pos, |
| 661 |
MutableHandleValue dst); |
| 662 |
|
| 663 |
[[nodiscard]] bool objectPattern(NodeVector& elts, TokenPos* pos, |
| 664 |
MutableHandleValue dst); |
| 665 |
|
| 666 |
[[nodiscard]] bool propertyPattern(HandleValue key, HandleValue patt, |
| 667 |
bool isShorthand, TokenPos* pos, |
| 668 |
MutableHandleValue dst); |
| 669 |
}; |
| 670 |
|
| 671 |
} /* anonymous namespace */ |
| 672 |
|
| 673 |
bool NodeBuilder::createNode(ASTType type, TokenPos* pos, |
| 674 |
MutableHandleObject dst) { |
| 675 |
MOZ_ASSERT(type > AST_ERROR && type < AST_LIMIT); |
| 676 |
|
| 677 |
RootedValue tv(cx); |
| 678 |
Rooted<PlainObject*> node(cx, NewPlainObject(cx)); |
| 679 |
if (!node || !setNodeLoc(node, pos) || !atomValue(nodeTypeNames[type], &tv) || |
| 680 |
!defineProperty(node, "type", tv)) { |
| 681 |
return false; |
| 682 |
} |
| 683 |
|
| 684 |
dst.set(node); |
| 685 |
return true; |
| 686 |
} |
| 687 |
|
| 688 |
bool NodeBuilder::newArray(NodeVector& elts, MutableHandleValue dst) { |
| 689 |
const size_t len = elts.length(); |
| 690 |
if (len > UINT32_MAX) { |
| 691 |
ReportAllocationOverflow(fc); |
| 692 |
return false; |
| 693 |
} |
| 694 |
RootedObject array(cx, NewDenseFullyAllocatedArray(cx, uint32_t(len))); |
| 695 |
if (!array) { |
| 696 |
return false; |
| 697 |
} |
| 698 |
|
| 699 |
for (size_t i = 0; i < len; i++) { |
| 700 |
RootedValue val(cx, elts[i]); |
| 701 |
|
| 702 |
MOZ_ASSERT_IF(val.isMagic(), val.whyMagic() == JS_SERIALIZE_NO_NODE); |
| 703 |
|
| 704 |
/* Represent "no node" as an array hole by not adding the value. */ |
| 705 |
if (val.isMagic(JS_SERIALIZE_NO_NODE)) { |
| 706 |
continue; |
| 707 |
} |
| 708 |
|
| 709 |
if (!DefineDataElement(cx, array, i, val)) { |
| 710 |
return false; |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
dst.setObject(*array); |
| 715 |
return true; |
| 716 |
} |
| 717 |
|
| 718 |
bool NodeBuilder::newNodeLoc(TokenPos* pos, MutableHandleValue dst) { |
| 719 |
if (!pos) { |
| 720 |
dst.setNull(); |
| 721 |
return true; |
| 722 |
} |
| 723 |
|
| 724 |
RootedObject loc(cx); |
| 725 |
RootedObject to(cx); |
| 726 |
RootedValue val(cx); |
| 727 |
|
| 728 |
if (!newObject(&loc)) { |
| 729 |
return false; |
| 730 |
} |
| 731 |
|
| 732 |
dst.setObject(*loc); |
| 733 |
|
| 734 |
uint32_t startLineNum, endLineNum; |
| 735 |
JS::LimitedColumnNumberOneOrigin startColumnIndex, endColumnIndex; |
| 736 |
parser->tokenStream.computeLineAndColumn(pos->begin, &startLineNum, |
| 737 |
&startColumnIndex); |
| 738 |
parser->tokenStream.computeLineAndColumn(pos->end, &endLineNum, |
| 739 |
&endColumnIndex); |
| 740 |
|
| 741 |
if (!newObject(&to)) { |
| 742 |
return false; |
| 743 |
} |
| 744 |
val.setObject(*to); |
| 745 |
if (!defineProperty(loc, "start", val)) { |
| 746 |
return false; |
| 747 |
} |
| 748 |
val.setNumber(startLineNum); |
| 749 |
if (!defineProperty(to, "line", val)) { |
| 750 |
return false; |
| 751 |
} |
| 752 |
val.setNumber(startColumnIndex.oneOriginValue()); |
| 753 |
if (!defineProperty(to, "column", val)) { |
| 754 |
return false; |
| 755 |
} |
| 756 |
|
| 757 |
if (!newObject(&to)) { |
| 758 |
return false; |
| 759 |
} |
| 760 |
val.setObject(*to); |
| 761 |
if (!defineProperty(loc, "end", val)) { |
| 762 |
return false; |
| 763 |
} |
| 764 |
val.setNumber(endLineNum); |
| 765 |
if (!defineProperty(to, "line", val)) { |
| 766 |
return false; |
| 767 |
} |
| 768 |
val.setNumber(endColumnIndex.oneOriginValue()); |
| 769 |
if (!defineProperty(to, "column", val)) { |
| 770 |
return false; |
| 771 |
} |
| 772 |
|
| 773 |
if (!defineProperty(loc, "source", srcval)) { |
| 774 |
return false; |
| 775 |
} |
| 776 |
|
| 777 |
return true; |
| 778 |
} |
| 779 |
|
| 780 |
bool NodeBuilder::setNodeLoc(HandleObject node, TokenPos* pos) { |
| 781 |
if (!saveLoc) { |
| 782 |
return true; |
| 783 |
} |
| 784 |
|
| 785 |
RootedValue loc(cx); |
| 786 |
return newNodeLoc(pos, &loc) && defineProperty(node, "loc", loc); |
| 787 |
} |
| 788 |
|
| 789 |
bool NodeBuilder::program(NodeVector& elts, TokenPos* pos, |
| 790 |
MutableHandleValue dst) { |
| 791 |
return listNode(AST_PROGRAM, "body", elts, pos, dst); |
| 792 |
} |
| 793 |
|
| 794 |
bool NodeBuilder::blockStatement(NodeVector& elts, TokenPos* pos, |
| 795 |
MutableHandleValue dst) { |
| 796 |
return listNode(AST_BLOCK_STMT, "body", elts, pos, dst); |
| 797 |
} |
| 798 |
|
| 799 |
bool NodeBuilder::expressionStatement(HandleValue expr, TokenPos* pos, |
| 800 |
MutableHandleValue dst) { |
| 801 |
return newNode(AST_EXPR_STMT, pos, "expression", expr, dst); |
| 802 |
} |
| 803 |
|
| 804 |
bool NodeBuilder::emptyStatement(TokenPos* pos, MutableHandleValue dst) { |
| 805 |
return newNode(AST_EMPTY_STMT, pos, dst); |
| 806 |
} |
| 807 |
|
| 808 |
bool NodeBuilder::ifStatement(HandleValue test, HandleValue cons, |
| 809 |
HandleValue alt, TokenPos* pos, |
| 810 |
MutableHandleValue dst) { |
| 811 |
return newNode(AST_IF_STMT, pos, "test", test, "consequent", cons, |
| 812 |
"alternate", alt, dst); |
| 813 |
} |
| 814 |
|
| 815 |
bool NodeBuilder::breakStatement(HandleValue label, TokenPos* pos, |
| 816 |
MutableHandleValue dst) { |
| 817 |
return newNode(AST_BREAK_STMT, pos, "label", label, dst); |
| 818 |
} |
| 819 |
|
| 820 |
bool NodeBuilder::continueStatement(HandleValue label, TokenPos* pos, |
| 821 |
MutableHandleValue dst) { |
| 822 |
return newNode(AST_CONTINUE_STMT, pos, "label", label, dst); |
| 823 |
} |
| 824 |
|
| 825 |
bool NodeBuilder::labeledStatement(HandleValue label, HandleValue stmt, |
| 826 |
TokenPos* pos, MutableHandleValue dst) { |
| 827 |
return newNode(AST_LAB_STMT, pos, "label", label, "body", stmt, dst); |
| 828 |
} |
| 829 |
|
| 830 |
bool NodeBuilder::throwStatement(HandleValue arg, TokenPos* pos, |
| 831 |
MutableHandleValue dst) { |
| 832 |
return newNode(AST_THROW_STMT, pos, "argument", arg, dst); |
| 833 |
} |
| 834 |
|
| 835 |
bool NodeBuilder::returnStatement(HandleValue arg, TokenPos* pos, |
| 836 |
MutableHandleValue dst) { |
| 837 |
return newNode(AST_RETURN_STMT, pos, "argument", arg, dst); |
| 838 |
} |
| 839 |
|
| 840 |
bool NodeBuilder::forStatement(HandleValue init, HandleValue test, |
| 841 |
HandleValue update, HandleValue stmt, |
| 842 |
TokenPos* pos, MutableHandleValue dst) { |
| 843 |
return newNode(AST_FOR_STMT, pos, "init", init, "test", test, "update", |
| 844 |
update, "body", stmt, dst); |
| 845 |
} |
| 846 |
|
| 847 |
bool NodeBuilder::forInStatement(HandleValue var, HandleValue expr, |
| 848 |
HandleValue stmt, TokenPos* pos, |
| 849 |
MutableHandleValue dst) { |
| 850 |
return newNode(AST_FOR_IN_STMT, pos, "left", var, "right", expr, "body", stmt, |
| 851 |
dst); |
| 852 |
} |
| 853 |
|
| 854 |
bool NodeBuilder::forOfStatement(HandleValue var, HandleValue expr, |
| 855 |
HandleValue stmt, TokenPos* pos, |
| 856 |
MutableHandleValue dst) { |
| 857 |
return newNode(AST_FOR_OF_STMT, pos, "left", var, "right", expr, "body", stmt, |
| 858 |
dst); |
| 859 |
} |
| 860 |
|
| 861 |
bool NodeBuilder::withStatement(HandleValue expr, HandleValue stmt, |
| 862 |
TokenPos* pos, MutableHandleValue dst) { |
| 863 |
return newNode(AST_WITH_STMT, pos, "object", expr, "body", stmt, dst); |
| 864 |
} |
| 865 |
|
| 866 |
bool NodeBuilder::whileStatement(HandleValue test, HandleValue stmt, |
| 867 |
TokenPos* pos, MutableHandleValue dst) { |
| 868 |
return newNode(AST_WHILE_STMT, pos, "test", test, "body", stmt, dst); |
| 869 |
} |
| 870 |
|
| 871 |
bool NodeBuilder::doWhileStatement(HandleValue stmt, HandleValue test, |
| 872 |
TokenPos* pos, MutableHandleValue dst) { |
| 873 |
return newNode(AST_DO_STMT, pos, "body", stmt, "test", test, dst); |
| 874 |
} |
| 875 |
|
| 876 |
bool NodeBuilder::switchStatement(HandleValue disc, NodeVector& elts, |
| 877 |
bool lexical, TokenPos* pos, |
| 878 |
MutableHandleValue dst) { |
| 879 |
RootedValue array(cx); |
| 880 |
if (!newArray(elts, &array)) { |
| 881 |
return false; |
| 882 |
} |
| 883 |
|
| 884 |
RootedValue lexicalVal(cx, BooleanValue(lexical)); |
| 885 |
return newNode(AST_SWITCH_STMT, pos, "discriminant", disc, "cases", array, |
| 886 |
"lexical", lexicalVal, dst); |
| 887 |
} |
| 888 |
|
| 889 |
bool NodeBuilder::tryStatement(HandleValue body, HandleValue handler, |
| 890 |
HandleValue finally, TokenPos* pos, |
| 891 |
MutableHandleValue dst) { |
| 892 |
return newNode(AST_TRY_STMT, pos, "block", body, "handler", handler, |
| 893 |
"finalizer", finally, dst); |
| 894 |
} |
| 895 |
|
| 896 |
bool NodeBuilder::debuggerStatement(TokenPos* pos, MutableHandleValue dst) { |
| 897 |
return newNode(AST_DEBUGGER_STMT, pos, dst); |
| 898 |
} |
| 899 |
|
| 900 |
bool NodeBuilder::binaryExpression(BinaryOperator op, HandleValue left, |
| 901 |
HandleValue right, TokenPos* pos, |
| 902 |
MutableHandleValue dst) { |
| 903 |
MOZ_ASSERT(op > BINOP_ERR && op < BINOP_LIMIT); |
| 904 |
|
| 905 |
RootedValue opName(cx); |
| 906 |
if (!atomValue(binopNames[op], &opName)) { |
| 907 |
return false; |
| 908 |
} |
| 909 |
|
| 910 |
return newNode(AST_BINARY_EXPR, pos, "operator", opName, "left", left, |
| 911 |
"right", right, dst); |
| 912 |
} |
| 913 |
|
| 914 |
bool NodeBuilder::unaryExpression(UnaryOperator unop, HandleValue expr, |
| 915 |
TokenPos* pos, MutableHandleValue dst) { |
| 916 |
MOZ_ASSERT(unop > UNOP_ERR && unop < UNOP_LIMIT); |
| 917 |
|
| 918 |
RootedValue opName(cx); |
| 919 |
if (!atomValue(unopNames[unop], &opName)) { |
| 920 |
return false; |
| 921 |
} |
| 922 |
|
| 923 |
RootedValue trueVal(cx, BooleanValue(true)); |
| 924 |
return newNode(AST_UNARY_EXPR, pos, "operator", opName, "argument", expr, |
| 925 |
"prefix", trueVal, dst); |
| 926 |
} |
| 927 |
|
| 928 |
bool NodeBuilder::assignmentExpression(AssignmentOperator aop, HandleValue lhs, |
| 929 |
HandleValue rhs, TokenPos* pos, |
| 930 |
MutableHandleValue dst) { |
| 931 |
MOZ_ASSERT(aop > AOP_ERR && aop < AOP_LIMIT); |
| 932 |
|
| 933 |
RootedValue opName(cx); |
| 934 |
if (!atomValue(aopNames[aop], &opName)) { |
| 935 |
return false; |
| 936 |
} |
| 937 |
|
| 938 |
return newNode(AST_ASSIGN_EXPR, pos, "operator", opName, "left", lhs, "right", |
| 939 |
rhs, dst); |
| 940 |
} |
| 941 |
|
| 942 |
bool NodeBuilder::updateExpression(HandleValue expr, bool incr, bool prefix, |
| 943 |
TokenPos* pos, MutableHandleValue dst) { |
| 944 |
RootedValue opName(cx); |
| 945 |
if (!atomValue(incr ? "++" : "--", &opName)) { |
| 946 |
return false; |
| 947 |
} |
| 948 |
|
| 949 |
RootedValue prefixVal(cx, BooleanValue(prefix)); |
| 950 |
|
| 951 |
return newNode(AST_UPDATE_EXPR, pos, "operator", opName, "argument", expr, |
| 952 |
"prefix", prefixVal, dst); |
| 953 |
} |
| 954 |
|
| 955 |
bool NodeBuilder::logicalExpression(ParseNodeKind pnk, HandleValue left, |
| 956 |
HandleValue right, TokenPos* pos, |
| 957 |
MutableHandleValue dst) { |
| 958 |
RootedValue opName(cx); |
| 959 |
switch (pnk) { |
| 960 |
case ParseNodeKind::OrExpr: |
| 961 |
if (!atomValue("||", &opName)) { |
| 962 |
return false; |
| 963 |
} |
| 964 |
break; |
| 965 |
case ParseNodeKind::CoalesceExpr: |
| 966 |
if (!atomValue("??", &opName)) { |
| 967 |
return false; |
| 968 |
} |
| 969 |
break; |
| 970 |
case ParseNodeKind::AndExpr: |
| 971 |
if (!atomValue("&&", &opName)) { |
| 972 |
return false; |
| 973 |
} |
| 974 |
break; |
| 975 |
default: |
| 976 |
MOZ_CRASH("Unexpected ParseNodeKind: Must be `Or`, `And`, or `Coalesce`"); |
| 977 |
} |
| 978 |
|
| 979 |
return newNode(AST_LOGICAL_EXPR, pos, "operator", opName, "left", left, |
| 980 |
"right", right, dst); |
| 981 |
} |
| 982 |
|
| 983 |
bool NodeBuilder::conditionalExpression(HandleValue test, HandleValue cons, |
| 984 |
HandleValue alt, TokenPos* pos, |
| 985 |
MutableHandleValue dst) { |
| 986 |
return newNode(AST_COND_EXPR, pos, "test", test, "consequent", cons, |
| 987 |
"alternate", alt, dst); |
| 988 |
} |
| 989 |
|
| 990 |
bool NodeBuilder::sequenceExpression(NodeVector& elts, TokenPos* pos, |
| 991 |
MutableHandleValue dst) { |
| 992 |
return listNode(AST_LIST_EXPR, "expressions", elts, pos, dst); |
| 993 |
} |
| 994 |
|
| 995 |
bool NodeBuilder::callExpression(HandleValue callee, NodeVector& args, |
| 996 |
TokenPos* pos, MutableHandleValue dst, |
| 997 |
bool isOptional) { |
| 998 |
RootedValue array(cx); |
| 999 |
if (!newArray(args, &array)) { |
| 1000 |
return false; |
| 1001 |
} |
| 1002 |
|
| 1003 |
return newNode(isOptional ? AST_OPT_CALL_EXPR : AST_CALL_EXPR, pos, "callee", |
| 1004 |
callee, "arguments", array, dst); |
| 1005 |
} |
| 1006 |
|
| 1007 |
bool NodeBuilder::newExpression(HandleValue callee, NodeVector& args, |
| 1008 |
TokenPos* pos, MutableHandleValue dst) { |
| 1009 |
RootedValue array(cx); |
| 1010 |
if (!newArray(args, &array)) { |
| 1011 |
return false; |
| 1012 |
} |
| 1013 |
|
| 1014 |
return newNode(AST_NEW_EXPR, pos, "callee", callee, "arguments", array, dst); |
| 1015 |
} |
| 1016 |
|
| 1017 |
bool NodeBuilder::memberExpression(bool computed, HandleValue expr, |
| 1018 |
HandleValue member, TokenPos* pos, |
| 1019 |
MutableHandleValue dst, |
| 1020 |
bool isOptional /* = false */) { |
| 1021 |
RootedValue computedVal(cx, BooleanValue(computed)); |
| 1022 |
|
| 1023 |
return newNode(isOptional ? AST_OPT_MEMBER_EXPR : AST_MEMBER_EXPR, pos, |
| 1024 |
"object", expr, "property", member, "computed", computedVal, |
| 1025 |
dst); |
| 1026 |
} |
| 1027 |
|
| 1028 |
bool NodeBuilder::arrayExpression(NodeVector& elts, TokenPos* pos, |
| 1029 |
MutableHandleValue dst) { |
| 1030 |
return listNode(AST_ARRAY_EXPR, "elements", elts, pos, dst); |
| 1031 |
} |
| 1032 |
|
| 1033 |
bool NodeBuilder::callSiteObj(NodeVector& raw, NodeVector& cooked, |
| 1034 |
TokenPos* pos, MutableHandleValue dst) { |
| 1035 |
RootedValue rawVal(cx); |
| 1036 |
if (!newArray(raw, &rawVal)) { |
| 1037 |
return false; |
| 1038 |
} |
| 1039 |
|
| 1040 |
RootedValue cookedVal(cx); |
| 1041 |
if (!newArray(cooked, &cookedVal)) { |
| 1042 |
return false; |
| 1043 |
} |
| 1044 |
|
| 1045 |
return newNode(AST_CALL_SITE_OBJ, pos, "raw", rawVal, "cooked", cookedVal, |
| 1046 |
dst); |
| 1047 |
} |
| 1048 |
|
| 1049 |
bool NodeBuilder::taggedTemplate(HandleValue callee, NodeVector& args, |
| 1050 |
TokenPos* pos, MutableHandleValue dst) { |
| 1051 |
RootedValue array(cx); |
| 1052 |
if (!newArray(args, &array)) { |
| 1053 |
return false; |
| 1054 |
} |
| 1055 |
|
| 1056 |
return newNode(AST_TAGGED_TEMPLATE, pos, "callee", callee, "arguments", array, |
| 1057 |
dst); |
| 1058 |
} |
| 1059 |
|
| 1060 |
bool NodeBuilder::templateLiteral(NodeVector& elts, TokenPos* pos, |
| 1061 |
MutableHandleValue dst) { |
| 1062 |
return listNode(AST_TEMPLATE_LITERAL, "elements", elts, pos, dst); |
| 1063 |
} |
| 1064 |
|
| 1065 |
bool NodeBuilder::computedName(HandleValue name, TokenPos* pos, |
| 1066 |
MutableHandleValue dst) { |
| 1067 |
return newNode(AST_COMPUTED_NAME, pos, "name", name, dst); |
| 1068 |
} |
| 1069 |
|
| 1070 |
bool NodeBuilder::spreadExpression(HandleValue expr, TokenPos* pos, |
| 1071 |
MutableHandleValue dst) { |
| 1072 |
return newNode(AST_SPREAD_EXPR, pos, "expression", expr, dst); |
| 1073 |
} |
| 1074 |
|
| 1075 |
bool NodeBuilder::optionalExpression(HandleValue expr, TokenPos* pos, |
| 1076 |
MutableHandleValue dst) { |
| 1077 |
return newNode(AST_OPTIONAL_EXPR, pos, "expression", expr, dst); |
| 1078 |
} |
| 1079 |
|
| 1080 |
bool NodeBuilder::deleteOptionalExpression(HandleValue expr, TokenPos* pos, |
| 1081 |
MutableHandleValue dst) { |
| 1082 |
return newNode(AST_DELETE_OPTIONAL_EXPR, pos, "expression", expr, dst); |
| 1083 |
} |
| 1084 |
|
| 1085 |
bool NodeBuilder::propertyPattern(HandleValue key, HandleValue patt, |
| 1086 |
bool isShorthand, TokenPos* pos, |
| 1087 |
MutableHandleValue dst) { |
| 1088 |
RootedValue kindName(cx); |
| 1089 |
if (!atomValue("init", &kindName)) { |
| 1090 |
return false; |
| 1091 |
} |
| 1092 |
|
| 1093 |
RootedValue isShorthandVal(cx, BooleanValue(isShorthand)); |
| 1094 |
|
| 1095 |
return newNode(AST_PROP_PATT, pos, "key", key, "value", patt, "kind", |
| 1096 |
kindName, "shorthand", isShorthandVal, dst); |
| 1097 |
} |
| 1098 |
|
| 1099 |
bool NodeBuilder::prototypeMutation(HandleValue val, TokenPos* pos, |
| 1100 |
MutableHandleValue dst) { |
| 1101 |
return newNode(AST_PROTOTYPEMUTATION, pos, "value", val, dst); |
| 1102 |
} |
| 1103 |
|
| 1104 |
bool NodeBuilder::propertyInitializer(HandleValue key, HandleValue val, |
| 1105 |
PropKind kind, bool isShorthand, |
| 1106 |
bool isMethod, TokenPos* pos, |
| 1107 |
MutableHandleValue dst) { |
| 1108 |
RootedValue kindName(cx); |
| 1109 |
if (!atomValue(kind == PROP_INIT ? "init" |
| 1110 |
: kind == PROP_GETTER ? "get" |
| 1111 |
: "set", |
| 1112 |
&kindName)) { |
| 1113 |
return false; |
| 1114 |
} |
| 1115 |
|
| 1116 |
RootedValue isShorthandVal(cx, BooleanValue(isShorthand)); |
| 1117 |
RootedValue isMethodVal(cx, BooleanValue(isMethod)); |
| 1118 |
|
| 1119 |
return newNode(AST_PROPERTY, pos, "key", key, "value", val, "kind", kindName, |
| 1120 |
"method", isMethodVal, "shorthand", isShorthandVal, dst); |
| 1121 |
} |
| 1122 |
|
| 1123 |
bool NodeBuilder::objectExpression(NodeVector& elts, TokenPos* pos, |
| 1124 |
MutableHandleValue dst) { |
| 1125 |
return listNode(AST_OBJECT_EXPR, "properties", elts, pos, dst); |
| 1126 |
} |
| 1127 |
|
| 1128 |
bool NodeBuilder::thisExpression(TokenPos* pos, MutableHandleValue dst) { |
| 1129 |
return newNode(AST_THIS_EXPR, pos, dst); |
| 1130 |
} |
| 1131 |
|
| 1132 |
bool NodeBuilder::yieldExpression(HandleValue arg, YieldKind kind, |
| 1133 |
TokenPos* pos, MutableHandleValue dst) { |
| 1134 |
RootedValue delegateVal(cx); |
| 1135 |
switch (kind) { |
| 1136 |
case Delegating: |
| 1137 |
delegateVal = BooleanValue(true); |
| 1138 |
break; |
| 1139 |
case NotDelegating: |
| 1140 |
delegateVal = BooleanValue(false); |
| 1141 |
break; |
| 1142 |
} |
| 1143 |
return newNode(AST_YIELD_EXPR, pos, "argument", arg, "delegate", delegateVal, |
| 1144 |
dst); |
| 1145 |
} |
| 1146 |
|
| 1147 |
bool NodeBuilder::moduleRequest(HandleValue moduleSpec, NodeVector& attributes, |
| 1148 |
TokenPos* pos, MutableHandleValue dst) { |
| 1149 |
RootedValue array(cx); |
| 1150 |
if (!newArray(attributes, &array)) { |
| 1151 |
return false; |
| 1152 |
} |
| 1153 |
|
| 1154 |
return newNode(AST_MODULE_REQUEST, pos, "source", moduleSpec, "attributes", |
| 1155 |
array, dst); |
| 1156 |
} |
| 1157 |
|
| 1158 |
bool NodeBuilder::importAttribute(HandleValue key, HandleValue value, |
| 1159 |
TokenPos* pos, MutableHandleValue dst) { |
| 1160 |
return newNode(AST_IMPORT_ATTRIBUTE, pos, "key", key, "value", value, dst); |
| 1161 |
} |
| 1162 |
|
| 1163 |
bool NodeBuilder::importDeclaration(NodeVector& elts, HandleValue moduleRequest, |
| 1164 |
TokenPos* pos, MutableHandleValue dst) { |
| 1165 |
RootedValue array(cx); |
| 1166 |
if (!newArray(elts, &array)) { |
| 1167 |
return false; |
| 1168 |
} |
| 1169 |
|
| 1170 |
return newNode(AST_IMPORT_DECL, pos, "specifiers", array, "moduleRequest", |
| 1171 |
moduleRequest, dst); |
| 1172 |
} |
| 1173 |
|
| 1174 |
bool NodeBuilder::importSpecifier(HandleValue importName, |
| 1175 |
HandleValue bindingName, TokenPos* pos, |
| 1176 |
MutableHandleValue dst) { |
| 1177 |
return newNode(AST_IMPORT_SPEC, pos, "id", importName, "name", bindingName, |
| 1178 |
dst); |
| 1179 |
} |
| 1180 |
|
| 1181 |
bool NodeBuilder::importNamespaceSpecifier(HandleValue bindingName, |
| 1182 |
TokenPos* pos, |
| 1183 |
MutableHandleValue dst) { |
| 1184 |
return newNode(AST_IMPORT_NAMESPACE_SPEC, pos, "name", bindingName, dst); |
| 1185 |
} |
| 1186 |
|
| 1187 |
bool NodeBuilder::exportDeclaration(HandleValue decl, NodeVector& elts, |
| 1188 |
HandleValue moduleRequest, |
| 1189 |
HandleValue isDefault, TokenPos* pos, |
| 1190 |
MutableHandleValue dst) { |
| 1191 |
RootedValue array(cx, NullValue()); |
| 1192 |
if (decl.isNull() && !newArray(elts, &array)) { |
| 1193 |
return false; |
| 1194 |
} |
| 1195 |
|
| 1196 |
return newNode(AST_EXPORT_DECL, pos, "declaration", decl, "specifiers", array, |
| 1197 |
"moduleRequest", moduleRequest, "isDefault", isDefault, dst); |
| 1198 |
} |
| 1199 |
|
| 1200 |
bool NodeBuilder::exportSpecifier(HandleValue bindingName, |
| 1201 |
HandleValue exportName, TokenPos* pos, |
| 1202 |
MutableHandleValue dst) { |
| 1203 |
return newNode(AST_EXPORT_SPEC, pos, "id", bindingName, "name", exportName, |
| 1204 |
dst); |
| 1205 |
} |
| 1206 |
|
| 1207 |
bool NodeBuilder::exportNamespaceSpecifier(HandleValue exportName, |
| 1208 |
TokenPos* pos, |
| 1209 |
MutableHandleValue dst) { |
| 1210 |
return newNode(AST_EXPORT_NAMESPACE_SPEC, pos, "name", exportName, dst); |
| 1211 |
} |
| 1212 |
|
| 1213 |
bool NodeBuilder::exportBatchSpecifier(TokenPos* pos, MutableHandleValue dst) { |
| 1214 |
return newNode(AST_EXPORT_BATCH_SPEC, pos, dst); |
| 1215 |
} |
| 1216 |
|
| 1217 |
bool NodeBuilder::variableDeclaration(NodeVector& elts, VarDeclKind kind, |
| 1218 |
TokenPos* pos, MutableHandleValue dst) { |
| 1219 |
MOZ_ASSERT(kind > VARDECL_ERR && kind < VARDECL_LIMIT); |
| 1220 |
|
| 1221 |
RootedValue array(cx), kindName(cx); |
| 1222 |
const char* s; |
| 1223 |
switch (kind) { |
| 1224 |
case VARDECL_CONST: |
| 1225 |
s = "const"; |
| 1226 |
break; |
| 1227 |
case VARDECL_LET: |
| 1228 |
s = "let"; |
| 1229 |
break; |
| 1230 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1231 |
case VARDECL_USING: |
| 1232 |
s = "using"; |
| 1233 |
break; |
| 1234 |
case VARDECL_AWAIT_USING: |
| 1235 |
s = "await using"; |
| 1236 |
break; |
| 1237 |
#endif |
| 1238 |
default: |
| 1239 |
s = "var"; |
| 1240 |
} |
| 1241 |
if (!newArray(elts, &array) || !atomValue(s, &kindName)) { |
| 1242 |
return false; |
| 1243 |
} |
| 1244 |
|
| 1245 |
return newNode(AST_VAR_DECL, pos, "kind", kindName, "declarations", array, |
| 1246 |
dst); |
| 1247 |
} |
| 1248 |
|
| 1249 |
bool NodeBuilder::variableDeclarator(HandleValue id, HandleValue init, |
| 1250 |
TokenPos* pos, MutableHandleValue dst) { |
| 1251 |
return newNode(AST_VAR_DTOR, pos, "id", id, "init", init, dst); |
| 1252 |
} |
| 1253 |
|
| 1254 |
bool NodeBuilder::switchCase(HandleValue expr, NodeVector& elts, TokenPos* pos, |
| 1255 |
MutableHandleValue dst) { |
| 1256 |
RootedValue array(cx); |
| 1257 |
if (!newArray(elts, &array)) { |
| 1258 |
return false; |
| 1259 |
} |
| 1260 |
|
| 1261 |
return newNode(AST_CASE, pos, "test", expr, "consequent", array, dst); |
| 1262 |
} |
| 1263 |
|
| 1264 |
bool NodeBuilder::catchClause(HandleValue var, HandleValue body, TokenPos* pos, |
| 1265 |
MutableHandleValue dst) { |
| 1266 |
return newNode(AST_CATCH, pos, "param", var, "body", body, dst); |
| 1267 |
} |
| 1268 |
|
| 1269 |
bool NodeBuilder::literal(HandleValue val, TokenPos* pos, |
| 1270 |
MutableHandleValue dst) { |
| 1271 |
return newNode(AST_LITERAL, pos, "value", val, dst); |
| 1272 |
} |
| 1273 |
|
| 1274 |
bool NodeBuilder::identifier(HandleValue name, TokenPos* pos, |
| 1275 |
MutableHandleValue dst) { |
| 1276 |
return newNode(AST_IDENTIFIER, pos, "name", name, dst); |
| 1277 |
} |
| 1278 |
|
| 1279 |
bool NodeBuilder::objectPattern(NodeVector& elts, TokenPos* pos, |
| 1280 |
MutableHandleValue dst) { |
| 1281 |
return listNode(AST_OBJECT_PATT, "properties", elts, pos, dst); |
| 1282 |
} |
| 1283 |
|
| 1284 |
bool NodeBuilder::arrayPattern(NodeVector& elts, TokenPos* pos, |
| 1285 |
MutableHandleValue dst) { |
| 1286 |
return listNode(AST_ARRAY_PATT, "elements", elts, pos, dst); |
| 1287 |
} |
| 1288 |
|
| 1289 |
bool NodeBuilder::function(ASTType type, TokenPos* pos, HandleValue id, |
| 1290 |
NodeVector& args, NodeVector& defaults, |
| 1291 |
HandleValue body, HandleValue rest, |
| 1292 |
GeneratorStyle generatorStyle, bool isAsync, |
| 1293 |
bool isExpression, MutableHandleValue dst) { |
| 1294 |
RootedValue array(cx), defarray(cx); |
| 1295 |
if (!newArray(args, &array)) { |
| 1296 |
return false; |
| 1297 |
} |
| 1298 |
if (!newArray(defaults, &defarray)) { |
| 1299 |
return false; |
| 1300 |
} |
| 1301 |
|
| 1302 |
bool isGenerator = generatorStyle != GeneratorStyle::None; |
| 1303 |
RootedValue isGeneratorVal(cx, BooleanValue(isGenerator)); |
| 1304 |
RootedValue isAsyncVal(cx, BooleanValue(isAsync)); |
| 1305 |
RootedValue isExpressionVal(cx, BooleanValue(isExpression)); |
| 1306 |
|
| 1307 |
if (isGenerator) { |
| 1308 |
MOZ_ASSERT(generatorStyle == GeneratorStyle::ES6); |
| 1309 |
JSAtom* styleStr = Atomize(cx, "es6", 3); |
| 1310 |
if (!styleStr) { |
| 1311 |
return false; |
| 1312 |
} |
| 1313 |
RootedValue styleVal(cx, StringValue(styleStr)); |
| 1314 |
return newNode(type, pos, "id", id, "params", array, "defaults", defarray, |
| 1315 |
"body", body, "rest", rest, "generator", isGeneratorVal, |
| 1316 |
"async", isAsyncVal, "style", styleVal, "expression", |
| 1317 |
isExpressionVal, dst); |
| 1318 |
} |
| 1319 |
|
| 1320 |
return newNode(type, pos, "id", id, "params", array, "defaults", defarray, |
| 1321 |
"body", body, "rest", rest, "generator", isGeneratorVal, |
| 1322 |
"async", isAsyncVal, "expression", isExpressionVal, dst); |
| 1323 |
} |
| 1324 |
|
| 1325 |
bool NodeBuilder::classMethod(HandleValue name, HandleValue body, |
| 1326 |
#ifdef ENABLE_DECORATORS |
| 1327 |
HandleValue decorators, |
| 1328 |
#endif |
| 1329 |
PropKind kind, bool isStatic, TokenPos* pos, |
| 1330 |
MutableHandleValue dst) { |
| 1331 |
RootedValue kindName(cx); |
| 1332 |
if (!atomValue(kind == PROP_INIT ? "method" |
| 1333 |
: kind == PROP_GETTER ? "get" |
| 1334 |
: "set", |
| 1335 |
&kindName)) { |
| 1336 |
return false; |
| 1337 |
} |
| 1338 |
|
| 1339 |
RootedValue isStaticVal(cx, BooleanValue(isStatic)); |
| 1340 |
return newNode(AST_CLASS_METHOD, pos, "name", name, "body", body, "kind", |
| 1341 |
kindName, "static", isStaticVal, |
| 1342 |
#ifdef ENABLE_DECORATORS |
| 1343 |
"decorators", decorators, |
| 1344 |
#endif |
| 1345 |
dst); |
| 1346 |
} |
| 1347 |
|
| 1348 |
bool NodeBuilder::classField(HandleValue name, HandleValue initializer, |
| 1349 |
#ifdef ENABLE_DECORATORS |
| 1350 |
HandleValue decorators, |
| 1351 |
#endif |
| 1352 |
TokenPos* pos, MutableHandleValue dst) { |
| 1353 |
return newNode(AST_CLASS_FIELD, pos, "name", name, "init", initializer, |
| 1354 |
#ifdef ENABLE_DECORATORS |
| 1355 |
"decorators", decorators, |
| 1356 |
#endif |
| 1357 |
dst); |
| 1358 |
} |
| 1359 |
|
| 1360 |
bool NodeBuilder::staticClassBlock(HandleValue body, TokenPos* pos, |
| 1361 |
MutableHandleValue dst) { |
| 1362 |
return newNode(AST_STATIC_CLASS_BLOCK, pos, "body", body, dst); |
| 1363 |
} |
| 1364 |
|
| 1365 |
bool NodeBuilder::classMembers(NodeVector& members, MutableHandleValue dst) { |
| 1366 |
return newArray(members, dst); |
| 1367 |
} |
| 1368 |
|
| 1369 |
bool NodeBuilder::classDefinition(bool expr, HandleValue name, |
| 1370 |
HandleValue heritage, HandleValue block, |
| 1371 |
#ifdef ENABLE_DECORATORS |
| 1372 |
HandleValue decorators, |
| 1373 |
#endif |
| 1374 |
TokenPos* pos, MutableHandleValue dst) { |
| 1375 |
ASTType type = expr ? AST_CLASS_EXPR : AST_CLASS_STMT; |
| 1376 |
return newNode(type, pos, "id", name, "superClass", heritage, "body", block, |
| 1377 |
#ifdef ENABLE_DECORATORS |
| 1378 |
"decorators", decorators, |
| 1379 |
#endif |
| 1380 |
dst); |
| 1381 |
} |
| 1382 |
|
| 1383 |
bool NodeBuilder::metaProperty(HandleValue meta, HandleValue property, |
| 1384 |
TokenPos* pos, MutableHandleValue dst) { |
| 1385 |
return newNode(AST_METAPROPERTY, pos, "meta", meta, "property", property, |
| 1386 |
dst); |
| 1387 |
} |
| 1388 |
|
| 1389 |
bool NodeBuilder::callImportExpression(HandleValue ident, NodeVector& args, |
| 1390 |
TokenPos* pos, MutableHandleValue dst) { |
| 1391 |
RootedValue array(cx); |
| 1392 |
if (!newArray(args, &array)) { |
| 1393 |
return false; |
| 1394 |
} |
| 1395 |
|
| 1396 |
return newNode(AST_CALL_IMPORT, pos, "ident", ident, "arguments", array, dst); |
| 1397 |
} |
| 1398 |
|
| 1399 |
bool NodeBuilder::super(TokenPos* pos, MutableHandleValue dst) { |
| 1400 |
return newNode(AST_SUPER, pos, dst); |
| 1401 |
} |
| 1402 |
|
| 1403 |
namespace { |
| 1404 |
|
| 1405 |
/* |
| 1406 |
* Serialization of parse nodes to JavaScript objects. |
| 1407 |
* |
| 1408 |
* All serialization methods take a non-nullable ParseNode pointer. |
| 1409 |
*/ |
| 1410 |
class ASTSerializer { |
| 1411 |
JSContext* cx; |
| 1412 |
FrontendContext* fc; |
| 1413 |
Parser<FullParseHandler, char16_t>* parser; |
| 1414 |
NodeBuilder builder; |
| 1415 |
DebugOnly<uint32_t> lineno; |
| 1416 |
|
| 1417 |
Value unrootedAtomContents(JSAtom* atom) { |
| 1418 |
return StringValue(atom ? atom : cx->names().empty_); |
| 1419 |
} |
| 1420 |
|
| 1421 |
BinaryOperator binop(ParseNodeKind kind); |
| 1422 |
UnaryOperator unop(ParseNodeKind kind); |
| 1423 |
AssignmentOperator aop(ParseNodeKind kind); |
| 1424 |
|
| 1425 |
bool statements(ListNode* stmtList, NodeVector& elts); |
| 1426 |
bool expressions(ListNode* exprList, NodeVector& elts); |
| 1427 |
bool leftAssociate(ListNode* node, MutableHandleValue dst); |
| 1428 |
bool rightAssociate(ListNode* node, MutableHandleValue dst); |
| 1429 |
bool functionArgs(ParamsBodyNode* pn, NodeVector& args, NodeVector& defaults, |
| 1430 |
MutableHandleValue rest); |
| 1431 |
|
| 1432 |
bool sourceElement(ParseNode* pn, MutableHandleValue dst); |
| 1433 |
|
| 1434 |
bool declaration(ParseNode* pn, MutableHandleValue dst); |
| 1435 |
bool variableDeclaration(ListNode* declList, bool lexical, |
| 1436 |
MutableHandleValue dst); |
| 1437 |
bool variableDeclarator(ParseNode* pn, MutableHandleValue dst); |
| 1438 |
bool importDeclaration(BinaryNode* importNode, MutableHandleValue dst); |
| 1439 |
bool importSpecifier(BinaryNode* importSpec, MutableHandleValue dst); |
| 1440 |
bool importNamespaceSpecifier(UnaryNode* importSpec, MutableHandleValue dst); |
| 1441 |
bool exportDeclaration(ParseNode* exportNode, MutableHandleValue dst); |
| 1442 |
bool exportSpecifier(BinaryNode* exportSpec, MutableHandleValue dst); |
| 1443 |
bool exportNamespaceSpecifier(UnaryNode* exportSpec, MutableHandleValue dst); |
| 1444 |
bool classDefinition(ClassNode* pn, bool expr, MutableHandleValue dst); |
| 1445 |
bool importAttributes(ListNode* attributeList, NodeVector& attributes); |
| 1446 |
|
| 1447 |
bool optStatement(ParseNode* pn, MutableHandleValue dst) { |
| 1448 |
if (!pn) { |
| 1449 |
dst.setMagic(JS_SERIALIZE_NO_NODE); |
| 1450 |
return true; |
| 1451 |
} |
| 1452 |
return statement(pn, dst); |
| 1453 |
} |
| 1454 |
|
| 1455 |
bool forInit(ParseNode* pn, MutableHandleValue dst); |
| 1456 |
bool forIn(ForNode* loop, ParseNode* iterExpr, HandleValue var, |
| 1457 |
HandleValue stmt, MutableHandleValue dst); |
| 1458 |
bool forOf(ForNode* loop, ParseNode* iterExpr, HandleValue var, |
| 1459 |
HandleValue stmt, MutableHandleValue dst); |
| 1460 |
bool statement(ParseNode* pn, MutableHandleValue dst); |
| 1461 |
bool blockStatement(ListNode* node, MutableHandleValue dst); |
| 1462 |
bool switchStatement(SwitchStatement* switchStmt, MutableHandleValue dst); |
| 1463 |
bool switchCase(CaseClause* caseClause, MutableHandleValue dst); |
| 1464 |
bool tryStatement(TryNode* tryNode, MutableHandleValue dst); |
| 1465 |
bool catchClause(BinaryNode* catchClause, MutableHandleValue dst); |
| 1466 |
|
| 1467 |
bool optExpression(ParseNode* pn, MutableHandleValue dst) { |
| 1468 |
if (!pn) { |
| 1469 |
dst.setMagic(JS_SERIALIZE_NO_NODE); |
| 1470 |
return true; |
| 1471 |
} |
| 1472 |
return expression(pn, dst); |
| 1473 |
} |
| 1474 |
|
| 1475 |
bool expression(ParseNode* pn, MutableHandleValue dst); |
| 1476 |
|
| 1477 |
bool propertyName(ParseNode* key, MutableHandleValue dst); |
| 1478 |
bool property(ParseNode* pn, MutableHandleValue dst); |
| 1479 |
|
| 1480 |
bool classMethod(ClassMethod* classMethod, MutableHandleValue dst); |
| 1481 |
bool classField(ClassField* classField, MutableHandleValue dst); |
| 1482 |
bool staticClassBlock(StaticClassBlock* staticClassBlock, |
| 1483 |
MutableHandleValue dst); |
| 1484 |
|
| 1485 |
bool optIdentifier(Handle<JSAtom*> atom, TokenPos* pos, |
| 1486 |
MutableHandleValue dst) { |
| 1487 |
if (!atom) { |
| 1488 |
dst.setMagic(JS_SERIALIZE_NO_NODE); |
| 1489 |
return true; |
| 1490 |
} |
| 1491 |
return identifier(atom, pos, dst); |
| 1492 |
} |
| 1493 |
|
| 1494 |
bool identifier(Handle<JSAtom*> atom, TokenPos* pos, MutableHandleValue dst); |
| 1495 |
bool identifier(NameNode* id, MutableHandleValue dst); |
| 1496 |
bool identifierOrLiteral(ParseNode* id, MutableHandleValue dst); |
| 1497 |
bool literal(ParseNode* pn, MutableHandleValue dst); |
| 1498 |
|
| 1499 |
bool optPattern(ParseNode* pn, MutableHandleValue dst) { |
| 1500 |
if (!pn) { |
| 1501 |
dst.setMagic(JS_SERIALIZE_NO_NODE); |
| 1502 |
return true; |
| 1503 |
} |
| 1504 |
return pattern(pn, dst); |
| 1505 |
} |
| 1506 |
|
| 1507 |
bool pattern(ParseNode* pn, MutableHandleValue dst); |
| 1508 |
bool arrayPattern(ListNode* array, MutableHandleValue dst); |
| 1509 |
bool objectPattern(ListNode* obj, MutableHandleValue dst); |
| 1510 |
|
| 1511 |
bool function(FunctionNode* funNode, ASTType type, MutableHandleValue dst); |
| 1512 |
bool functionArgsAndBody(ParamsBodyNode* pn, NodeVector& args, |
| 1513 |
NodeVector& defaults, bool isAsync, |
| 1514 |
bool isExpression, MutableHandleValue body, |
| 1515 |
MutableHandleValue rest); |
| 1516 |
bool functionBody(ParseNode* pn, TokenPos* pos, MutableHandleValue dst); |
| 1517 |
|
| 1518 |
public: |
| 1519 |
ASTSerializer(JSContext* c, FrontendContext* f, bool l, char const* src, |
| 1520 |
uint32_t ln) |
| 1521 |
: cx(c), |
| 1522 |
fc(f), |
| 1523 |
parser(nullptr), |
| 1524 |
builder(c, f, l, src) |
| 1525 |
#ifdef DEBUG |
| 1526 |
, |
| 1527 |
lineno(ln) |
| 1528 |
#endif |
| 1529 |
{ |
| 1530 |
} |
| 1531 |
|
| 1532 |
bool init() { return builder.init(); } |
| 1533 |
|
| 1534 |
void setParser(frontend::Parser<frontend::FullParseHandler, char16_t>* p) { |
| 1535 |
parser = p; |
| 1536 |
builder.setParser(p); |
| 1537 |
} |
| 1538 |
|
| 1539 |
bool program(ListNode* node, MutableHandleValue dst); |
| 1540 |
}; |
| 1541 |
|
| 1542 |
} /* anonymous namespace */ |
| 1543 |
|
| 1544 |
AssignmentOperator ASTSerializer::aop(ParseNodeKind kind) { |
| 1545 |
switch (kind) { |
| 1546 |
case ParseNodeKind::AssignExpr: |
| 1547 |
return AOP_ASSIGN; |
| 1548 |
case ParseNodeKind::AddAssignExpr: |
| 1549 |
return AOP_PLUS; |
| 1550 |
case ParseNodeKind::SubAssignExpr: |
| 1551 |
return AOP_MINUS; |
| 1552 |
case ParseNodeKind::MulAssignExpr: |
| 1553 |
return AOP_STAR; |
| 1554 |
case ParseNodeKind::DivAssignExpr: |
| 1555 |
return AOP_DIV; |
| 1556 |
case ParseNodeKind::ModAssignExpr: |
| 1557 |
return AOP_MOD; |
| 1558 |
case ParseNodeKind::PowAssignExpr: |
| 1559 |
return AOP_POW; |
| 1560 |
case ParseNodeKind::LshAssignExpr: |
| 1561 |
return AOP_LSH; |
| 1562 |
case ParseNodeKind::RshAssignExpr: |
| 1563 |
return AOP_RSH; |
| 1564 |
case ParseNodeKind::UrshAssignExpr: |
| 1565 |
return AOP_URSH; |
| 1566 |
case ParseNodeKind::BitOrAssignExpr: |
| 1567 |
return AOP_BITOR; |
| 1568 |
case ParseNodeKind::BitXorAssignExpr: |
| 1569 |
return AOP_BITXOR; |
| 1570 |
case ParseNodeKind::BitAndAssignExpr: |
| 1571 |
return AOP_BITAND; |
| 1572 |
case ParseNodeKind::CoalesceAssignExpr: |
| 1573 |
return AOP_COALESCE; |
| 1574 |
case ParseNodeKind::OrAssignExpr: |
| 1575 |
return AOP_OR; |
| 1576 |
case ParseNodeKind::AndAssignExpr: |
| 1577 |
return AOP_AND; |
| 1578 |
default: |
| 1579 |
return AOP_ERR; |
| 1580 |
} |
| 1581 |
} |
| 1582 |
|
| 1583 |
UnaryOperator ASTSerializer::unop(ParseNodeKind kind) { |
| 1584 |
if (IsDeleteKind(kind)) { |
| 1585 |
return UNOP_DELETE; |
| 1586 |
} |
| 1587 |
|
| 1588 |
if (IsTypeofKind(kind)) { |
| 1589 |
return UNOP_TYPEOF; |
| 1590 |
} |
| 1591 |
|
| 1592 |
switch (kind) { |
| 1593 |
case ParseNodeKind::AwaitExpr: |
| 1594 |
return UNOP_AWAIT; |
| 1595 |
case ParseNodeKind::NegExpr: |
| 1596 |
return UNOP_NEG; |
| 1597 |
case ParseNodeKind::PosExpr: |
| 1598 |
return UNOP_POS; |
| 1599 |
case ParseNodeKind::NotExpr: |
| 1600 |
return UNOP_NOT; |
| 1601 |
case ParseNodeKind::BitNotExpr: |
| 1602 |
return UNOP_BITNOT; |
| 1603 |
case ParseNodeKind::VoidExpr: |
| 1604 |
return UNOP_VOID; |
| 1605 |
default: |
| 1606 |
return UNOP_ERR; |
| 1607 |
} |
| 1608 |
} |
| 1609 |
|
| 1610 |
BinaryOperator ASTSerializer::binop(ParseNodeKind kind) { |
| 1611 |
switch (kind) { |
| 1612 |
case ParseNodeKind::LshExpr: |
| 1613 |
return BINOP_LSH; |
| 1614 |
case ParseNodeKind::RshExpr: |
| 1615 |
return BINOP_RSH; |
| 1616 |
case ParseNodeKind::UrshExpr: |
| 1617 |
return BINOP_URSH; |
| 1618 |
case ParseNodeKind::LtExpr: |
| 1619 |
return BINOP_LT; |
| 1620 |
case ParseNodeKind::LeExpr: |
| 1621 |
return BINOP_LE; |
| 1622 |
case ParseNodeKind::GtExpr: |
| 1623 |
return BINOP_GT; |
| 1624 |
case ParseNodeKind::GeExpr: |
| 1625 |
return BINOP_GE; |
| 1626 |
case ParseNodeKind::EqExpr: |
| 1627 |
return BINOP_EQ; |
| 1628 |
case ParseNodeKind::NeExpr: |
| 1629 |
return BINOP_NE; |
| 1630 |
case ParseNodeKind::StrictEqExpr: |
| 1631 |
return BINOP_STRICTEQ; |
| 1632 |
case ParseNodeKind::StrictNeExpr: |
| 1633 |
return BINOP_STRICTNE; |
| 1634 |
case ParseNodeKind::AddExpr: |
| 1635 |
return BINOP_ADD; |
| 1636 |
case ParseNodeKind::SubExpr: |
| 1637 |
return BINOP_SUB; |
| 1638 |
case ParseNodeKind::MulExpr: |
| 1639 |
return BINOP_STAR; |
| 1640 |
case ParseNodeKind::DivExpr: |
| 1641 |
return BINOP_DIV; |
| 1642 |
case ParseNodeKind::ModExpr: |
| 1643 |
return BINOP_MOD; |
| 1644 |
case ParseNodeKind::PowExpr: |
| 1645 |
return BINOP_POW; |
| 1646 |
case ParseNodeKind::BitOrExpr: |
| 1647 |
return BINOP_BITOR; |
| 1648 |
case ParseNodeKind::BitXorExpr: |
| 1649 |
return BINOP_BITXOR; |
| 1650 |
case ParseNodeKind::BitAndExpr: |
| 1651 |
return BINOP_BITAND; |
| 1652 |
case ParseNodeKind::InExpr: |
| 1653 |
case ParseNodeKind::PrivateInExpr: |
| 1654 |
return BINOP_IN; |
| 1655 |
case ParseNodeKind::InstanceOfExpr: |
| 1656 |
return BINOP_INSTANCEOF; |
| 1657 |
case ParseNodeKind::CoalesceExpr: |
| 1658 |
return BINOP_COALESCE; |
| 1659 |
default: |
| 1660 |
return BINOP_ERR; |
| 1661 |
} |
| 1662 |
} |
| 1663 |
|
| 1664 |
bool ASTSerializer::statements(ListNode* stmtList, NodeVector& elts) { |
| 1665 |
MOZ_ASSERT(stmtList->isKind(ParseNodeKind::StatementList)); |
| 1666 |
|
| 1667 |
if (!elts.reserve(stmtList->count())) { |
| 1668 |
return false; |
| 1669 |
} |
| 1670 |
|
| 1671 |
for (ParseNode* stmt : stmtList->contents()) { |
| 1672 |
MOZ_ASSERT(stmtList->pn_pos.encloses(stmt->pn_pos)); |
| 1673 |
|
| 1674 |
RootedValue elt(cx); |
| 1675 |
if (!sourceElement(stmt, &elt)) { |
| 1676 |
return false; |
| 1677 |
} |
| 1678 |
elts.infallibleAppend(elt); |
| 1679 |
} |
| 1680 |
|
| 1681 |
return true; |
| 1682 |
} |
| 1683 |
|
| 1684 |
bool ASTSerializer::expressions(ListNode* exprList, NodeVector& elts) { |
| 1685 |
if (!elts.reserve(exprList->count())) { |
| 1686 |
return false; |
| 1687 |
} |
| 1688 |
|
| 1689 |
for (ParseNode* expr : exprList->contents()) { |
| 1690 |
MOZ_ASSERT(exprList->pn_pos.encloses(expr->pn_pos)); |
| 1691 |
|
| 1692 |
RootedValue elt(cx); |
| 1693 |
if (!expression(expr, &elt)) { |
| 1694 |
return false; |
| 1695 |
} |
| 1696 |
elts.infallibleAppend(elt); |
| 1697 |
} |
| 1698 |
|
| 1699 |
return true; |
| 1700 |
} |
| 1701 |
|
| 1702 |
bool ASTSerializer::blockStatement(ListNode* node, MutableHandleValue dst) { |
| 1703 |
MOZ_ASSERT(node->isKind(ParseNodeKind::StatementList)); |
| 1704 |
|
| 1705 |
NodeVector stmts(cx); |
| 1706 |
return statements(node, stmts) && |
| 1707 |
builder.blockStatement(stmts, &node->pn_pos, dst); |
| 1708 |
} |
| 1709 |
|
| 1710 |
bool ASTSerializer::program(ListNode* node, MutableHandleValue dst) { |
| 1711 |
#ifdef DEBUG |
| 1712 |
{ |
| 1713 |
const TokenStreamAnyChars& anyChars = parser->anyChars; |
| 1714 |
auto lineToken = anyChars.lineToken(node->pn_pos.begin); |
| 1715 |
MOZ_ASSERT(anyChars.lineNumber(lineToken) == lineno); |
| 1716 |
} |
| 1717 |
#endif |
| 1718 |
|
| 1719 |
NodeVector stmts(cx); |
| 1720 |
return statements(node, stmts) && builder.program(stmts, &node->pn_pos, dst); |
| 1721 |
} |
| 1722 |
|
| 1723 |
bool ASTSerializer::sourceElement(ParseNode* pn, MutableHandleValue dst) { |
| 1724 |
/* SpiderMonkey allows declarations even in pure statement contexts. */ |
| 1725 |
return statement(pn, dst); |
| 1726 |
} |
| 1727 |
|
| 1728 |
bool ASTSerializer::declaration(ParseNode* pn, MutableHandleValue dst) { |
| 1729 |
MOZ_ASSERT(pn->isKind(ParseNodeKind::Function) || |
| 1730 |
pn->isKind(ParseNodeKind::VarStmt) || |
| 1731 |
pn->isKind(ParseNodeKind::LetDecl) || |
| 1732 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1733 |
pn->isKind(ParseNodeKind::UsingDecl) || |
| 1734 |
pn->isKind(ParseNodeKind::AwaitUsingDecl) || |
| 1735 |
#endif |
| 1736 |
pn->isKind(ParseNodeKind::ConstDecl)); |
| 1737 |
|
| 1738 |
switch (pn->getKind()) { |
| 1739 |
case ParseNodeKind::Function: |
| 1740 |
return function(&pn->as<FunctionNode>(), AST_FUNC_DECL, dst); |
| 1741 |
|
| 1742 |
case ParseNodeKind::VarStmt: |
| 1743 |
return variableDeclaration(&pn->as<ListNode>(), false, dst); |
| 1744 |
|
| 1745 |
default: |
| 1746 |
MOZ_ASSERT(pn->isKind(ParseNodeKind::LetDecl) || |
| 1747 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1748 |
pn->isKind(ParseNodeKind::UsingDecl) || |
| 1749 |
pn->isKind(ParseNodeKind::AwaitUsingDecl) || |
| 1750 |
#endif |
| 1751 |
pn->isKind(ParseNodeKind::ConstDecl)); |
| 1752 |
return variableDeclaration(&pn->as<ListNode>(), true, dst); |
| 1753 |
} |
| 1754 |
} |
| 1755 |
|
| 1756 |
bool ASTSerializer::variableDeclaration(ListNode* declList, bool lexical, |
| 1757 |
MutableHandleValue dst) { |
| 1758 |
MOZ_ASSERT_IF(lexical, declList->isKind(ParseNodeKind::LetDecl) || |
| 1759 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1760 |
declList->isKind(ParseNodeKind::UsingDecl) || |
| 1761 |
declList->isKind(ParseNodeKind::AwaitUsingDecl) || |
| 1762 |
#endif |
| 1763 |
declList->isKind(ParseNodeKind::ConstDecl)); |
| 1764 |
MOZ_ASSERT_IF(!lexical, declList->isKind(ParseNodeKind::VarStmt)); |
| 1765 |
|
| 1766 |
VarDeclKind kind = VARDECL_ERR; |
| 1767 |
// Treat both the toplevel const binding (secretly var-like) and the lexical |
| 1768 |
// const the same way |
| 1769 |
if (lexical) { |
| 1770 |
if (declList->isKind(ParseNodeKind::LetDecl)) { |
| 1771 |
kind = VARDECL_LET; |
| 1772 |
} |
| 1773 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1774 |
else if (declList->isKind(ParseNodeKind::UsingDecl)) { |
| 1775 |
kind = VARDECL_USING; |
| 1776 |
} else if (declList->isKind(ParseNodeKind::AwaitUsingDecl)) { |
| 1777 |
kind = VARDECL_AWAIT_USING; |
| 1778 |
} |
| 1779 |
#endif |
| 1780 |
else { |
| 1781 |
kind = VARDECL_CONST; |
| 1782 |
} |
| 1783 |
} else { |
| 1784 |
kind = |
| 1785 |
declList->isKind(ParseNodeKind::VarStmt) ? VARDECL_VAR : VARDECL_CONST; |
| 1786 |
} |
| 1787 |
|
| 1788 |
NodeVector dtors(cx); |
| 1789 |
if (!dtors.reserve(declList->count())) { |
| 1790 |
return false; |
| 1791 |
} |
| 1792 |
for (ParseNode* decl : declList->contents()) { |
| 1793 |
RootedValue child(cx); |
| 1794 |
if (!variableDeclarator(decl, &child)) { |
| 1795 |
return false; |
| 1796 |
} |
| 1797 |
dtors.infallibleAppend(child); |
| 1798 |
} |
| 1799 |
return builder.variableDeclaration(dtors, kind, &declList->pn_pos, dst); |
| 1800 |
} |
| 1801 |
|
| 1802 |
bool ASTSerializer::variableDeclarator(ParseNode* pn, MutableHandleValue dst) { |
| 1803 |
ParseNode* patternNode; |
| 1804 |
ParseNode* initNode; |
| 1805 |
|
| 1806 |
if (pn->isKind(ParseNodeKind::Name)) { |
| 1807 |
patternNode = pn; |
| 1808 |
initNode = nullptr; |
| 1809 |
} else if (pn->isKind(ParseNodeKind::AssignExpr)) { |
| 1810 |
AssignmentNode* assignNode = &pn->as<AssignmentNode>(); |
| 1811 |
patternNode = assignNode->left(); |
| 1812 |
initNode = assignNode->right(); |
| 1813 |
MOZ_ASSERT(pn->pn_pos.encloses(patternNode->pn_pos)); |
| 1814 |
MOZ_ASSERT(pn->pn_pos.encloses(initNode->pn_pos)); |
| 1815 |
} else { |
| 1816 |
/* This happens for a destructuring declarator in a for-in/of loop. */ |
| 1817 |
patternNode = pn; |
| 1818 |
initNode = nullptr; |
| 1819 |
} |
| 1820 |
|
| 1821 |
RootedValue patternVal(cx), init(cx); |
| 1822 |
return pattern(patternNode, &patternVal) && optExpression(initNode, &init) && |
| 1823 |
builder.variableDeclarator(patternVal, init, &pn->pn_pos, dst); |
| 1824 |
} |
| 1825 |
|
| 1826 |
bool ASTSerializer::importDeclaration(BinaryNode* importNode, |
| 1827 |
MutableHandleValue dst) { |
| 1828 |
MOZ_ASSERT(importNode->isKind(ParseNodeKind::ImportDecl)); |
| 1829 |
|
| 1830 |
ListNode* specList = &importNode->left()->as<ListNode>(); |
| 1831 |
MOZ_ASSERT(specList->isKind(ParseNodeKind::ImportSpecList)); |
| 1832 |
|
| 1833 |
auto* moduleRequest = &importNode->right()->as<BinaryNode>(); |
| 1834 |
MOZ_ASSERT(moduleRequest->isKind(ParseNodeKind::ImportModuleRequest)); |
| 1835 |
|
| 1836 |
ParseNode* moduleSpecNode = moduleRequest->left(); |
| 1837 |
MOZ_ASSERT(moduleSpecNode->isKind(ParseNodeKind::StringExpr)); |
| 1838 |
|
| 1839 |
auto* attributeList = &moduleRequest->right()->as<ListNode>(); |
| 1840 |
MOZ_ASSERT(attributeList->isKind(ParseNodeKind::ImportAttributeList)); |
| 1841 |
|
| 1842 |
NodeVector elts(cx); |
| 1843 |
if (!elts.reserve(specList->count())) { |
| 1844 |
return false; |
| 1845 |
} |
| 1846 |
|
| 1847 |
for (ParseNode* item : specList->contents()) { |
| 1848 |
RootedValue elt(cx); |
| 1849 |
if (item->is<UnaryNode>()) { |
| 1850 |
auto* spec = &item->as<UnaryNode>(); |
| 1851 |
if (!importNamespaceSpecifier(spec, &elt)) { |
| 1852 |
return false; |
| 1853 |
} |
| 1854 |
} else { |
| 1855 |
auto* spec = &item->as<BinaryNode>(); |
| 1856 |
if (!importSpecifier(spec, &elt)) { |
| 1857 |
return false; |
| 1858 |
} |
| 1859 |
} |
| 1860 |
elts.infallibleAppend(elt); |
| 1861 |
} |
| 1862 |
|
| 1863 |
RootedValue moduleSpec(cx); |
| 1864 |
if (!literal(moduleSpecNode, &moduleSpec)) { |
| 1865 |
return false; |
| 1866 |
} |
| 1867 |
|
| 1868 |
NodeVector attributes(cx); |
| 1869 |
if (!importAttributes(attributeList, attributes)) { |
| 1870 |
return false; |
| 1871 |
} |
| 1872 |
|
| 1873 |
RootedValue moduleRequestValue(cx); |
| 1874 |
if (!builder.moduleRequest(moduleSpec, attributes, &importNode->pn_pos, |
| 1875 |
&moduleRequestValue)) { |
| 1876 |
return false; |
| 1877 |
} |
| 1878 |
|
| 1879 |
return builder.importDeclaration(elts, moduleRequestValue, |
| 1880 |
&importNode->pn_pos, dst); |
| 1881 |
} |
| 1882 |
|
| 1883 |
bool ASTSerializer::importSpecifier(BinaryNode* importSpec, |
| 1884 |
MutableHandleValue dst) { |
| 1885 |
MOZ_ASSERT(importSpec->isKind(ParseNodeKind::ImportSpec)); |
| 1886 |
NameNode* importNameNode = &importSpec->left()->as<NameNode>(); |
| 1887 |
NameNode* bindingNameNode = &importSpec->right()->as<NameNode>(); |
| 1888 |
|
| 1889 |
RootedValue importName(cx); |
| 1890 |
RootedValue bindingName(cx); |
| 1891 |
return identifierOrLiteral(importNameNode, &importName) && |
| 1892 |
identifier(bindingNameNode, &bindingName) && |
| 1893 |
builder.importSpecifier(importName, bindingName, &importSpec->pn_pos, |
| 1894 |
dst); |
| 1895 |
} |
| 1896 |
|
| 1897 |
bool ASTSerializer::importNamespaceSpecifier(UnaryNode* importSpec, |
| 1898 |
MutableHandleValue dst) { |
| 1899 |
MOZ_ASSERT(importSpec->isKind(ParseNodeKind::ImportNamespaceSpec)); |
| 1900 |
NameNode* bindingNameNode = &importSpec->kid()->as<NameNode>(); |
| 1901 |
|
| 1902 |
RootedValue bindingName(cx); |
| 1903 |
return identifier(bindingNameNode, &bindingName) && |
| 1904 |
builder.importNamespaceSpecifier(bindingName, &importSpec->pn_pos, |
| 1905 |
dst); |
| 1906 |
} |
| 1907 |
|
| 1908 |
bool ASTSerializer::exportDeclaration(ParseNode* exportNode, |
| 1909 |
MutableHandleValue dst) { |
| 1910 |
MOZ_ASSERT(exportNode->isKind(ParseNodeKind::ExportStmt) || |
| 1911 |
exportNode->isKind(ParseNodeKind::ExportFromStmt) || |
| 1912 |
exportNode->isKind(ParseNodeKind::ExportDefaultStmt)); |
| 1913 |
MOZ_ASSERT_IF(exportNode->isKind(ParseNodeKind::ExportStmt), |
| 1914 |
exportNode->is<UnaryNode>()); |
| 1915 |
MOZ_ASSERT_IF(exportNode->isKind(ParseNodeKind::ExportFromStmt), |
| 1916 |
exportNode->as<BinaryNode>().right()->isKind( |
| 1917 |
ParseNodeKind::ImportModuleRequest)); |
| 1918 |
|
| 1919 |
RootedValue decl(cx, NullValue()); |
| 1920 |
NodeVector elts(cx); |
| 1921 |
|
| 1922 |
ParseNode* kid = exportNode->isKind(ParseNodeKind::ExportStmt) |
| 1923 |
? exportNode->as<UnaryNode>().kid() |
| 1924 |
: exportNode->as<BinaryNode>().left(); |
| 1925 |
switch (ParseNodeKind kind = kid->getKind()) { |
| 1926 |
case ParseNodeKind::ExportSpecList: { |
| 1927 |
ListNode* specList = &kid->as<ListNode>(); |
| 1928 |
if (!elts.reserve(specList->count())) { |
| 1929 |
return false; |
| 1930 |
} |
| 1931 |
|
| 1932 |
for (ParseNode* spec : specList->contents()) { |
| 1933 |
RootedValue elt(cx); |
| 1934 |
if (spec->isKind(ParseNodeKind::ExportSpec)) { |
| 1935 |
if (!exportSpecifier(&spec->as<BinaryNode>(), &elt)) { |
| 1936 |
return false; |
| 1937 |
} |
| 1938 |
} else if (spec->isKind(ParseNodeKind::ExportNamespaceSpec)) { |
| 1939 |
if (!exportNamespaceSpecifier(&spec->as<UnaryNode>(), &elt)) { |
| 1940 |
return false; |
| 1941 |
} |
| 1942 |
} else { |
| 1943 |
MOZ_ASSERT(spec->isKind(ParseNodeKind::ExportBatchSpecStmt)); |
| 1944 |
if (!builder.exportBatchSpecifier(&exportNode->pn_pos, &elt)) { |
| 1945 |
return false; |
| 1946 |
} |
| 1947 |
} |
| 1948 |
elts.infallibleAppend(elt); |
| 1949 |
} |
| 1950 |
break; |
| 1951 |
} |
| 1952 |
|
| 1953 |
case ParseNodeKind::Function: |
| 1954 |
if (!function(&kid->as<FunctionNode>(), AST_FUNC_DECL, &decl)) { |
| 1955 |
return false; |
| 1956 |
} |
| 1957 |
break; |
| 1958 |
|
| 1959 |
case ParseNodeKind::ClassDecl: |
| 1960 |
if (!classDefinition(&kid->as<ClassNode>(), false, &decl)) { |
| 1961 |
return false; |
| 1962 |
} |
| 1963 |
break; |
| 1964 |
|
| 1965 |
case ParseNodeKind::VarStmt: |
| 1966 |
case ParseNodeKind::ConstDecl: |
| 1967 |
case ParseNodeKind::LetDecl: |
| 1968 |
if (!variableDeclaration(&kid->as<ListNode>(), |
| 1969 |
kind != ParseNodeKind::VarStmt, &decl)) { |
| 1970 |
return false; |
| 1971 |
} |
| 1972 |
break; |
| 1973 |
|
| 1974 |
default: |
| 1975 |
if (!expression(kid, &decl)) { |
| 1976 |
return false; |
| 1977 |
} |
| 1978 |
break; |
| 1979 |
} |
| 1980 |
|
| 1981 |
RootedValue moduleSpec(cx, NullValue()); |
| 1982 |
RootedValue moduleRequestValue(cx, NullValue()); |
| 1983 |
if (exportNode->isKind(ParseNodeKind::ExportFromStmt)) { |
| 1984 |
ParseNode* moduleRequest = exportNode->as<BinaryNode>().right(); |
| 1985 |
if (!literal(moduleRequest->as<BinaryNode>().left(), &moduleSpec)) { |
| 1986 |
return false; |
| 1987 |
} |
| 1988 |
|
| 1989 |
auto* attributeList = |
| 1990 |
&moduleRequest->as<BinaryNode>().right()->as<ListNode>(); |
| 1991 |
MOZ_ASSERT(attributeList->isKind(ParseNodeKind::ImportAttributeList)); |
| 1992 |
|
| 1993 |
NodeVector attributes(cx); |
| 1994 |
if (!importAttributes(attributeList, attributes)) { |
| 1995 |
return false; |
| 1996 |
} |
| 1997 |
|
| 1998 |
if (!builder.moduleRequest(moduleSpec, attributes, &exportNode->pn_pos, |
| 1999 |
&moduleRequestValue)) { |
| 2000 |
return false; |
| 2001 |
} |
| 2002 |
} |
| 2003 |
|
| 2004 |
RootedValue isDefault(cx, BooleanValue(false)); |
| 2005 |
if (exportNode->isKind(ParseNodeKind::ExportDefaultStmt)) { |
| 2006 |
isDefault.setBoolean(true); |
| 2007 |
} |
| 2008 |
|
| 2009 |
return builder.exportDeclaration(decl, elts, moduleRequestValue, isDefault, |
| 2010 |
&exportNode->pn_pos, dst); |
| 2011 |
} |
| 2012 |
|
| 2013 |
bool ASTSerializer::exportSpecifier(BinaryNode* exportSpec, |
| 2014 |
MutableHandleValue dst) { |
| 2015 |
MOZ_ASSERT(exportSpec->isKind(ParseNodeKind::ExportSpec)); |
| 2016 |
NameNode* bindingNameNode = &exportSpec->left()->as<NameNode>(); |
| 2017 |
NameNode* exportNameNode = &exportSpec->right()->as<NameNode>(); |
| 2018 |
|
| 2019 |
RootedValue bindingName(cx); |
| 2020 |
RootedValue exportName(cx); |
| 2021 |
return identifierOrLiteral(bindingNameNode, &bindingName) && |
| 2022 |
identifierOrLiteral(exportNameNode, &exportName) && |
| 2023 |
builder.exportSpecifier(bindingName, exportName, &exportSpec->pn_pos, |
| 2024 |
dst); |
| 2025 |
} |
| 2026 |
|
| 2027 |
bool ASTSerializer::exportNamespaceSpecifier(UnaryNode* exportSpec, |
| 2028 |
MutableHandleValue dst) { |
| 2029 |
MOZ_ASSERT(exportSpec->isKind(ParseNodeKind::ExportNamespaceSpec)); |
| 2030 |
NameNode* exportNameNode = &exportSpec->kid()->as<NameNode>(); |
| 2031 |
|
| 2032 |
RootedValue exportName(cx); |
| 2033 |
return identifierOrLiteral(exportNameNode, &exportName) && |
| 2034 |
builder.exportNamespaceSpecifier(exportName, &exportSpec->pn_pos, dst); |
| 2035 |
} |
| 2036 |
|
| 2037 |
bool ASTSerializer::importAttributes(ListNode* attributeList, |
| 2038 |
NodeVector& attributes) { |
| 2039 |
for (ParseNode* attributeItem : attributeList->contents()) { |
| 2040 |
BinaryNode* attributeNode = &attributeItem->as<BinaryNode>(); |
| 2041 |
MOZ_ASSERT(attributeNode->isKind(ParseNodeKind::ImportAttribute)); |
| 2042 |
|
| 2043 |
NameNode* keyNameNode = &attributeNode->left()->as<NameNode>(); |
| 2044 |
NameNode* valueNameNode = &attributeNode->right()->as<NameNode>(); |
| 2045 |
|
| 2046 |
RootedValue key(cx); |
| 2047 |
if (!identifierOrLiteral(keyNameNode, &key)) { |
| 2048 |
return false; |
| 2049 |
} |
| 2050 |
|
| 2051 |
RootedValue value(cx); |
| 2052 |
if (!literal(valueNameNode, &value)) { |
| 2053 |
return false; |
| 2054 |
} |
| 2055 |
|
| 2056 |
RootedValue attribute(cx); |
| 2057 |
if (!builder.importAttribute(key, value, &attributeNode->pn_pos, |
| 2058 |
&attribute)) { |
| 2059 |
return false; |
| 2060 |
} |
| 2061 |
|
| 2062 |
if (!attributes.append(attribute)) { |
| 2063 |
return false; |
| 2064 |
} |
| 2065 |
} |
| 2066 |
|
| 2067 |
return true; |
| 2068 |
} |
| 2069 |
|
| 2070 |
bool ASTSerializer::switchCase(CaseClause* caseClause, MutableHandleValue dst) { |
| 2071 |
MOZ_ASSERT_IF( |
| 2072 |
caseClause->caseExpression(), |
| 2073 |
caseClause->pn_pos.encloses(caseClause->caseExpression()->pn_pos)); |
| 2074 |
MOZ_ASSERT(caseClause->pn_pos.encloses(caseClause->statementList()->pn_pos)); |
| 2075 |
|
| 2076 |
NodeVector stmts(cx); |
| 2077 |
RootedValue expr(cx); |
| 2078 |
return optExpression(caseClause->caseExpression(), &expr) && |
| 2079 |
statements(caseClause->statementList(), stmts) && |
| 2080 |
builder.switchCase(expr, stmts, &caseClause->pn_pos, dst); |
| 2081 |
} |
| 2082 |
|
| 2083 |
bool ASTSerializer::switchStatement(SwitchStatement* switchStmt, |
| 2084 |
MutableHandleValue dst) { |
| 2085 |
MOZ_ASSERT(switchStmt->pn_pos.encloses(switchStmt->discriminant().pn_pos)); |
| 2086 |
MOZ_ASSERT( |
| 2087 |
switchStmt->pn_pos.encloses(switchStmt->lexicalForCaseList().pn_pos)); |
| 2088 |
|
| 2089 |
RootedValue disc(cx); |
| 2090 |
if (!expression(&switchStmt->discriminant(), &disc)) { |
| 2091 |
return false; |
| 2092 |
} |
| 2093 |
|
| 2094 |
ListNode* caseList = |
| 2095 |
&switchStmt->lexicalForCaseList().scopeBody()->as<ListNode>(); |
| 2096 |
|
| 2097 |
NodeVector cases(cx); |
| 2098 |
if (!cases.reserve(caseList->count())) { |
| 2099 |
return false; |
| 2100 |
} |
| 2101 |
|
| 2102 |
for (ParseNode* item : caseList->contents()) { |
| 2103 |
CaseClause* caseClause = &item->as<CaseClause>(); |
| 2104 |
RootedValue child(cx); |
| 2105 |
if (!switchCase(caseClause, &child)) { |
| 2106 |
return false; |
| 2107 |
} |
| 2108 |
cases.infallibleAppend(child); |
| 2109 |
} |
| 2110 |
|
| 2111 |
// `lexical` field is always true. |
| 2112 |
return builder.switchStatement(disc, cases, true, &switchStmt->pn_pos, dst); |
| 2113 |
} |
| 2114 |
|
| 2115 |
bool ASTSerializer::catchClause(BinaryNode* catchClause, |
| 2116 |
MutableHandleValue dst) { |
| 2117 |
MOZ_ASSERT(catchClause->isKind(ParseNodeKind::Catch)); |
| 2118 |
|
| 2119 |
ParseNode* varNode = catchClause->left(); |
| 2120 |
MOZ_ASSERT_IF(varNode, catchClause->pn_pos.encloses(varNode->pn_pos)); |
| 2121 |
|
| 2122 |
ParseNode* bodyNode = catchClause->right(); |
| 2123 |
MOZ_ASSERT(catchClause->pn_pos.encloses(bodyNode->pn_pos)); |
| 2124 |
|
| 2125 |
RootedValue var(cx), body(cx); |
| 2126 |
if (!optPattern(varNode, &var)) { |
| 2127 |
return false; |
| 2128 |
} |
| 2129 |
|
| 2130 |
return statement(bodyNode, &body) && |
| 2131 |
builder.catchClause(var, body, &catchClause->pn_pos, dst); |
| 2132 |
} |
| 2133 |
|
| 2134 |
bool ASTSerializer::tryStatement(TryNode* tryNode, MutableHandleValue dst) { |
| 2135 |
ParseNode* bodyNode = tryNode->body(); |
| 2136 |
MOZ_ASSERT(tryNode->pn_pos.encloses(bodyNode->pn_pos)); |
| 2137 |
|
| 2138 |
LexicalScopeNode* catchNode = tryNode->catchScope(); |
| 2139 |
MOZ_ASSERT_IF(catchNode, tryNode->pn_pos.encloses(catchNode->pn_pos)); |
| 2140 |
|
| 2141 |
ParseNode* finallyNode = tryNode->finallyBlock(); |
| 2142 |
MOZ_ASSERT_IF(finallyNode, tryNode->pn_pos.encloses(finallyNode->pn_pos)); |
| 2143 |
|
| 2144 |
RootedValue body(cx); |
| 2145 |
if (!statement(bodyNode, &body)) { |
| 2146 |
return false; |
| 2147 |
} |
| 2148 |
|
| 2149 |
RootedValue handler(cx, NullValue()); |
| 2150 |
if (catchNode) { |
| 2151 |
LexicalScopeNode* catchScope = &catchNode->as<LexicalScopeNode>(); |
| 2152 |
if (!catchClause(&catchScope->scopeBody()->as<BinaryNode>(), &handler)) { |
| 2153 |
return false; |
| 2154 |
} |
| 2155 |
} |
| 2156 |
|
| 2157 |
RootedValue finally(cx); |
| 2158 |
return optStatement(finallyNode, &finally) && |
| 2159 |
builder.tryStatement(body, handler, finally, &tryNode->pn_pos, dst); |
| 2160 |
} |
| 2161 |
|
| 2162 |
bool ASTSerializer::forInit(ParseNode* pn, MutableHandleValue dst) { |
| 2163 |
if (!pn) { |
| 2164 |
dst.setMagic(JS_SERIALIZE_NO_NODE); |
| 2165 |
return true; |
| 2166 |
} |
| 2167 |
|
| 2168 |
bool lexical = pn->isKind(ParseNodeKind::LetDecl) || |
| 2169 |
pn->isKind(ParseNodeKind::ConstDecl) |
| 2170 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 2171 |
|| pn->isKind(ParseNodeKind::UsingDecl) |
| 2172 |
#endif |
| 2173 |
; |
| 2174 |
return (lexical || pn->isKind(ParseNodeKind::VarStmt)) |
| 2175 |
? variableDeclaration(&pn->as<ListNode>(), lexical, dst) |
| 2176 |
: expression(pn, dst); |
| 2177 |
} |
| 2178 |
|
| 2179 |
bool ASTSerializer::forOf(ForNode* loop, ParseNode* iterExpr, HandleValue var, |
| 2180 |
HandleValue stmt, MutableHandleValue dst) { |
| 2181 |
RootedValue expr(cx); |
| 2182 |
|
| 2183 |
return expression(iterExpr, &expr) && |
| 2184 |
builder.forOfStatement(var, expr, stmt, &loop->pn_pos, dst); |
| 2185 |
} |
| 2186 |
|
| 2187 |
bool ASTSerializer::forIn(ForNode* loop, ParseNode* iterExpr, HandleValue var, |
| 2188 |
HandleValue stmt, MutableHandleValue dst) { |
| 2189 |
RootedValue expr(cx); |
| 2190 |
|
| 2191 |
return expression(iterExpr, &expr) && |
| 2192 |
builder.forInStatement(var, expr, stmt, &loop->pn_pos, dst); |
| 2193 |
} |
| 2194 |
|
| 2195 |
bool ASTSerializer::classDefinition(ClassNode* pn, bool expr, |
| 2196 |
MutableHandleValue dst) { |
| 2197 |
RootedValue className(cx, MagicValue(JS_SERIALIZE_NO_NODE)); |
| 2198 |
RootedValue heritage(cx); |
| 2199 |
RootedValue classBody(cx); |
| 2200 |
#ifdef ENABLE_DECORATORS |
| 2201 |
NodeVector classDecorators(cx); |
| 2202 |
RootedValue classDecoratorsArray(cx); |
| 2203 |
TokenPos decoratorPos; |
| 2204 |
bool decoratorsParsed = true; |
| 2205 |
if (pn->decorators()) { |
| 2206 |
decoratorPos.begin = pn->decorators()->head()->pn_pos.begin; |
| 2207 |
decoratorPos.end = pn->decorators()->last()->pn_pos.end; |
| 2208 |
decoratorsParsed = |
| 2209 |
expressions(pn->decorators(), classDecorators) && |
| 2210 |
builder.sequenceExpression(classDecorators, &decoratorPos, |
| 2211 |
&classDecoratorsArray); |
| 2212 |
} else { |
| 2213 |
classDecoratorsArray.setMagic(JS_SERIALIZE_NO_NODE); |
| 2214 |
} |
| 2215 |
#endif |
| 2216 |
|
| 2217 |
if (ClassNames* names = pn->names()) { |
| 2218 |
if (!identifier(names->innerBinding(), &className)) { |
| 2219 |
return false; |
| 2220 |
} |
| 2221 |
} |
| 2222 |
|
| 2223 |
return optExpression(pn->heritage(), &heritage) && |
| 2224 |
statement(pn->memberList(), &classBody) && |
| 2225 |
#ifdef ENABLE_DECORATORS |
| 2226 |
decoratorsParsed && |
| 2227 |
#endif |
| 2228 |
builder.classDefinition(expr, className, heritage, classBody, |
| 2229 |
#ifdef ENABLE_DECORATORS |
| 2230 |
classDecoratorsArray, |
| 2231 |
#endif |
| 2232 |
&pn->pn_pos, dst); |
| 2233 |
} |
| 2234 |
|
| 2235 |
bool ASTSerializer::statement(ParseNode* pn, MutableHandleValue dst) { |
| 2236 |
AutoCheckRecursionLimit recursion(cx); |
| 2237 |
if (!recursion.check(cx)) { |
| 2238 |
return false; |
| 2239 |
} |
| 2240 |
|
| 2241 |
switch (pn->getKind()) { |
| 2242 |
case ParseNodeKind::Function: |
| 2243 |
case ParseNodeKind::VarStmt: |
| 2244 |
return declaration(pn, dst); |
| 2245 |
|
| 2246 |
case ParseNodeKind::LetDecl: |
| 2247 |
case ParseNodeKind::ConstDecl: |
| 2248 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 2249 |
case ParseNodeKind::UsingDecl: |
| 2250 |
case ParseNodeKind::AwaitUsingDecl: |
| 2251 |
#endif |
| 2252 |
return declaration(pn, dst); |
| 2253 |
|
| 2254 |
case ParseNodeKind::ImportDecl: |
| 2255 |
return importDeclaration(&pn->as<BinaryNode>(), dst); |
| 2256 |
|
| 2257 |
case ParseNodeKind::ExportStmt: |
| 2258 |
case ParseNodeKind::ExportDefaultStmt: |
| 2259 |
case ParseNodeKind::ExportFromStmt: |
| 2260 |
return exportDeclaration(pn, dst); |
| 2261 |
|
| 2262 |
case ParseNodeKind::EmptyStmt: |
| 2263 |
return builder.emptyStatement(&pn->pn_pos, dst); |
| 2264 |
|
| 2265 |
case ParseNodeKind::ExpressionStmt: { |
| 2266 |
RootedValue expr(cx); |
| 2267 |
return expression(pn->as<UnaryNode>().kid(), &expr) && |
| 2268 |
builder.expressionStatement(expr, &pn->pn_pos, dst); |
| 2269 |
} |
| 2270 |
|
| 2271 |
case ParseNodeKind::LexicalScope: |
| 2272 |
pn = pn->as<LexicalScopeNode>().scopeBody(); |
| 2273 |
if (!pn->isKind(ParseNodeKind::StatementList)) { |
| 2274 |
return statement(pn, dst); |
| 2275 |
} |
| 2276 |
[[fallthrough]]; |
| 2277 |
|
| 2278 |
case ParseNodeKind::StatementList: |
| 2279 |
return blockStatement(&pn->as<ListNode>(), dst); |
| 2280 |
|
| 2281 |
case ParseNodeKind::IfStmt: { |
| 2282 |
TernaryNode* ifNode = &pn->as<TernaryNode>(); |
| 2283 |
|
| 2284 |
ParseNode* testNode = ifNode->kid1(); |
| 2285 |
MOZ_ASSERT(ifNode->pn_pos.encloses(testNode->pn_pos)); |
| 2286 |
|
| 2287 |
ParseNode* consNode = ifNode->kid2(); |
| 2288 |
MOZ_ASSERT(ifNode->pn_pos.encloses(consNode->pn_pos)); |
| 2289 |
|
| 2290 |
ParseNode* altNode = ifNode->kid3(); |
| 2291 |
MOZ_ASSERT_IF(altNode, ifNode->pn_pos.encloses(altNode->pn_pos)); |
| 2292 |
|
| 2293 |
RootedValue test(cx), cons(cx), alt(cx); |
| 2294 |
|
| 2295 |
return expression(testNode, &test) && statement(consNode, &cons) && |
| 2296 |
optStatement(altNode, &alt) && |
| 2297 |
builder.ifStatement(test, cons, alt, &ifNode->pn_pos, dst); |
| 2298 |
} |
| 2299 |
|
| 2300 |
case ParseNodeKind::SwitchStmt: |
| 2301 |
return switchStatement(&pn->as<SwitchStatement>(), dst); |
| 2302 |
|
| 2303 |
case ParseNodeKind::TryStmt: |
| 2304 |
return tryStatement(&pn->as<TryNode>(), dst); |
| 2305 |
|
| 2306 |
case ParseNodeKind::WithStmt: |
| 2307 |
case ParseNodeKind::WhileStmt: { |
| 2308 |
BinaryNode* node = &pn->as<BinaryNode>(); |
| 2309 |
|
| 2310 |
ParseNode* exprNode = node->left(); |
| 2311 |
MOZ_ASSERT(node->pn_pos.encloses(exprNode->pn_pos)); |
| 2312 |
|
| 2313 |
ParseNode* stmtNode = node->right(); |
| 2314 |
MOZ_ASSERT(node->pn_pos.encloses(stmtNode->pn_pos)); |
| 2315 |
|
| 2316 |
RootedValue expr(cx), stmt(cx); |
| 2317 |
|
| 2318 |
return expression(exprNode, &expr) && statement(stmtNode, &stmt) && |
| 2319 |
(node->isKind(ParseNodeKind::WithStmt) |
| 2320 |
? builder.withStatement(expr, stmt, &node->pn_pos, dst) |
| 2321 |
: builder.whileStatement(expr, stmt, &node->pn_pos, dst)); |
| 2322 |
} |
| 2323 |
|
| 2324 |
case ParseNodeKind::DoWhileStmt: { |
| 2325 |
BinaryNode* node = &pn->as<BinaryNode>(); |
| 2326 |
|
| 2327 |
ParseNode* stmtNode = node->left(); |
| 2328 |
MOZ_ASSERT(node->pn_pos.encloses(stmtNode->pn_pos)); |
| 2329 |
|
| 2330 |
ParseNode* testNode = node->right(); |
| 2331 |
MOZ_ASSERT(node->pn_pos.encloses(testNode->pn_pos)); |
| 2332 |
|
| 2333 |
RootedValue stmt(cx), test(cx); |
| 2334 |
|
| 2335 |
return statement(stmtNode, &stmt) && expression(testNode, &test) && |
| 2336 |
builder.doWhileStatement(stmt, test, &node->pn_pos, dst); |
| 2337 |
} |
| 2338 |
|
| 2339 |
case ParseNodeKind::ForStmt: { |
| 2340 |
ForNode* forNode = &pn->as<ForNode>(); |
| 2341 |
|
| 2342 |
TernaryNode* head = forNode->head(); |
| 2343 |
MOZ_ASSERT(forNode->pn_pos.encloses(head->pn_pos)); |
| 2344 |
|
| 2345 |
ParseNode* stmtNode = forNode->right(); |
| 2346 |
MOZ_ASSERT(forNode->pn_pos.encloses(stmtNode->pn_pos)); |
| 2347 |
|
| 2348 |
ParseNode* initNode = head->kid1(); |
| 2349 |
MOZ_ASSERT_IF(initNode, head->pn_pos.encloses(initNode->pn_pos)); |
| 2350 |
|
| 2351 |
ParseNode* maybeTest = head->kid2(); |
| 2352 |
MOZ_ASSERT_IF(maybeTest, head->pn_pos.encloses(maybeTest->pn_pos)); |
| 2353 |
|
| 2354 |
ParseNode* updateOrIter = head->kid3(); |
| 2355 |
MOZ_ASSERT_IF(updateOrIter, head->pn_pos.encloses(updateOrIter->pn_pos)); |
| 2356 |
|
| 2357 |
RootedValue stmt(cx); |
| 2358 |
if (!statement(stmtNode, &stmt)) { |
| 2359 |
return false; |
| 2360 |
} |
| 2361 |
|
| 2362 |
if (head->isKind(ParseNodeKind::ForIn) || |
| 2363 |
head->isKind(ParseNodeKind::ForOf)) { |
| 2364 |
RootedValue var(cx); |
| 2365 |
if (initNode->is<LexicalScopeNode>()) { |
| 2366 |
LexicalScopeNode* scopeNode = &initNode->as<LexicalScopeNode>(); |
| 2367 |
if (!variableDeclaration(&scopeNode->scopeBody()->as<ListNode>(), |
| 2368 |
true, &var)) { |
| 2369 |
return false; |
| 2370 |
} |
| 2371 |
} else if (!initNode->isKind(ParseNodeKind::VarStmt) && |
| 2372 |
!initNode->isKind(ParseNodeKind::LetDecl) && |
| 2373 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 2374 |
!initNode->isKind(ParseNodeKind::UsingDecl) && |
| 2375 |
!initNode->isKind(ParseNodeKind::AwaitUsingDecl) && |
| 2376 |
#endif |
| 2377 |
!initNode->isKind(ParseNodeKind::ConstDecl)) { |
| 2378 |
if (!pattern(initNode, &var)) { |
| 2379 |
return false; |
| 2380 |
} |
| 2381 |
} else { |
| 2382 |
if (!variableDeclaration( |
| 2383 |
&initNode->as<ListNode>(), |
| 2384 |
initNode->isKind(ParseNodeKind::LetDecl) || |
| 2385 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 2386 |
initNode->isKind(ParseNodeKind::UsingDecl) || |
| 2387 |
initNode->isKind(ParseNodeKind::AwaitUsingDecl) || |
| 2388 |
#endif |
| 2389 |
initNode->isKind(ParseNodeKind::ConstDecl), |
| 2390 |
&var)) { |
| 2391 |
return false; |
| 2392 |
} |
| 2393 |
} |
| 2394 |
if (head->isKind(ParseNodeKind::ForIn)) { |
| 2395 |
return forIn(forNode, updateOrIter, var, stmt, dst); |
| 2396 |
} |
| 2397 |
return forOf(forNode, updateOrIter, var, stmt, dst); |
| 2398 |
} |
| 2399 |
|
| 2400 |
RootedValue init(cx), test(cx), update(cx); |
| 2401 |
|
| 2402 |
return forInit(initNode, &init) && optExpression(maybeTest, &test) && |
| 2403 |
optExpression(updateOrIter, &update) && |
| 2404 |
builder.forStatement(init, test, update, stmt, &forNode->pn_pos, |
| 2405 |
dst); |
| 2406 |
} |
| 2407 |
|
| 2408 |
case ParseNodeKind::BreakStmt: |
| 2409 |
case ParseNodeKind::ContinueStmt: { |
| 2410 |
LoopControlStatement* node = &pn->as<LoopControlStatement>(); |
| 2411 |
RootedValue label(cx); |
| 2412 |
Rooted<JSAtom*> pnAtom(cx); |
| 2413 |
if (node->label()) { |
| 2414 |
pnAtom.set(parser->liftParserAtomToJSAtom(node->label())); |
| 2415 |
if (!pnAtom) { |
| 2416 |
return false; |
| 2417 |
} |
| 2418 |
} |
| 2419 |
return optIdentifier(pnAtom, nullptr, &label) && |
| 2420 |
(node->isKind(ParseNodeKind::BreakStmt) |
| 2421 |
? builder.breakStatement(label, &node->pn_pos, dst) |
| 2422 |
: builder.continueStatement(label, &node->pn_pos, dst)); |
| 2423 |
} |
| 2424 |
|
| 2425 |
case ParseNodeKind::LabelStmt: { |
| 2426 |
LabeledStatement* labelNode = &pn->as<LabeledStatement>(); |
| 2427 |
ParseNode* stmtNode = labelNode->statement(); |
| 2428 |
MOZ_ASSERT(labelNode->pn_pos.encloses(stmtNode->pn_pos)); |
| 2429 |
|
| 2430 |
RootedValue label(cx), stmt(cx); |
| 2431 |
Rooted<JSAtom*> pnAtom( |
| 2432 |
cx, parser->liftParserAtomToJSAtom(labelNode->label())); |
| 2433 |
if (!pnAtom.get()) { |
| 2434 |
return false; |
| 2435 |
} |
| 2436 |
return identifier(pnAtom, nullptr, &label) && |
| 2437 |
statement(stmtNode, &stmt) && |
| 2438 |
builder.labeledStatement(label, stmt, &labelNode->pn_pos, dst); |
| 2439 |
} |
| 2440 |
|
| 2441 |
case ParseNodeKind::ThrowStmt: { |
| 2442 |
UnaryNode* throwNode = &pn->as<UnaryNode>(); |
| 2443 |
ParseNode* operand = throwNode->kid(); |
| 2444 |
MOZ_ASSERT(throwNode->pn_pos.encloses(operand->pn_pos)); |
| 2445 |
|
| 2446 |
RootedValue arg(cx); |
| 2447 |
|
| 2448 |
return expression(operand, &arg) && |
| 2449 |
builder.throwStatement(arg, &throwNode->pn_pos, dst); |
| 2450 |
} |
| 2451 |
|
| 2452 |
case ParseNodeKind::ReturnStmt: { |
| 2453 |
UnaryNode* returnNode = &pn->as<UnaryNode>(); |
| 2454 |
ParseNode* operand = returnNode->kid(); |
| 2455 |
MOZ_ASSERT_IF(operand, returnNode->pn_pos.encloses(operand->pn_pos)); |
| 2456 |
|
| 2457 |
RootedValue arg(cx); |
| 2458 |
|
| 2459 |
return optExpression(operand, &arg) && |
| 2460 |
builder.returnStatement(arg, &returnNode->pn_pos, dst); |
| 2461 |
} |
| 2462 |
|
| 2463 |
case ParseNodeKind::DebuggerStmt: |
| 2464 |
return builder.debuggerStatement(&pn->pn_pos, dst); |
| 2465 |
|
| 2466 |
case ParseNodeKind::ClassDecl: |
| 2467 |
return classDefinition(&pn->as<ClassNode>(), false, dst); |
| 2468 |
|
| 2469 |
case ParseNodeKind::ClassMemberList: { |
| 2470 |
ListNode* memberList = &pn->as<ListNode>(); |
| 2471 |
NodeVector members(cx); |
| 2472 |
if (!members.reserve(memberList->count())) { |
| 2473 |
return false; |
| 2474 |
} |
| 2475 |
|
| 2476 |
for (ParseNode* item : memberList->contents()) { |
| 2477 |
if (item->is<LexicalScopeNode>()) { |
| 2478 |
item = item->as<LexicalScopeNode>().scopeBody(); |
| 2479 |
} |
| 2480 |
if (item->is<ClassField>()) { |
| 2481 |
ClassField* field = &item->as<ClassField>(); |
| 2482 |
MOZ_ASSERT(memberList->pn_pos.encloses(field->pn_pos)); |
| 2483 |
|
| 2484 |
RootedValue prop(cx); |
| 2485 |
if (!classField(field, &prop)) { |
| 2486 |
return false; |
| 2487 |
} |
| 2488 |
members.infallibleAppend(prop); |
| 2489 |
} else if (item->is<StaticClassBlock>()) { |
| 2490 |
// StaticClassBlock* block = &item->as<StaticClassBlock>(); |
| 2491 |
StaticClassBlock* scb = &item->as<StaticClassBlock>(); |
| 2492 |
MOZ_ASSERT(memberList->pn_pos.encloses(scb->pn_pos)); |
| 2493 |
RootedValue prop(cx); |
| 2494 |
if (!staticClassBlock(scb, &prop)) { |
| 2495 |
return false; |
| 2496 |
} |
| 2497 |
members.infallibleAppend(prop); |
| 2498 |
} else if (!item->isKind(ParseNodeKind::DefaultConstructor)) { |
| 2499 |
ClassMethod* method = &item->as<ClassMethod>(); |
| 2500 |
MOZ_ASSERT(memberList->pn_pos.encloses(method->pn_pos)); |
| 2501 |
|
| 2502 |
RootedValue prop(cx); |
| 2503 |
if (!classMethod(method, &prop)) { |
| 2504 |
return false; |
| 2505 |
} |
| 2506 |
members.infallibleAppend(prop); |
| 2507 |
} |
| 2508 |
} |
| 2509 |
|
| 2510 |
return builder.classMembers(members, dst); |
| 2511 |
} |
| 2512 |
|
| 2513 |
default: |
| 2514 |
LOCAL_NOT_REACHED("unexpected statement type"); |
| 2515 |
} |
| 2516 |
} |
| 2517 |
|
| 2518 |
bool ASTSerializer::classMethod(ClassMethod* classMethod, |
| 2519 |
MutableHandleValue dst) { |
| 2520 |
PropKind kind; |
| 2521 |
switch (classMethod->accessorType()) { |
| 2522 |
case AccessorType::None: |
| 2523 |
kind = PROP_INIT; |
| 2524 |
break; |
| 2525 |
|
| 2526 |
case AccessorType::Getter: |
| 2527 |
kind = PROP_GETTER; |
| 2528 |
break; |
| 2529 |
|
| 2530 |
case AccessorType::Setter: |
| 2531 |
kind = PROP_SETTER; |
| 2532 |
break; |
| 2533 |
|
| 2534 |
default: |
| 2535 |
LOCAL_NOT_REACHED("unexpected object-literal property"); |
| 2536 |
} |
| 2537 |
#ifdef ENABLE_DECORATORS |
| 2538 |
NodeVector methodDecorators(cx); |
| 2539 |
RootedValue methodDecoratorsArray(cx); |
| 2540 |
TokenPos decoratorPos; |
| 2541 |
bool decoratorsParsed = true; |
| 2542 |
if (classMethod->decorators()) { |
| 2543 |
decoratorPos.begin = classMethod->decorators()->head()->pn_pos.begin; |
| 2544 |
decoratorPos.end = classMethod->decorators()->last()->pn_pos.end; |
| 2545 |
decoratorsParsed = |
| 2546 |
expressions(classMethod->decorators(), methodDecorators) && |
| 2547 |
builder.sequenceExpression(methodDecorators, &decoratorPos, |
| 2548 |
&methodDecoratorsArray); |
| 2549 |
} else { |
| 2550 |
methodDecoratorsArray.setMagic(JS_SERIALIZE_NO_NODE); |
| 2551 |
} |
| 2552 |
#endif |
| 2553 |
|
| 2554 |
RootedValue key(cx), val(cx); |
| 2555 |
bool isStatic = classMethod->isStatic(); |
| 2556 |
return propertyName(&classMethod->name(), &key) && |
| 2557 |
expression(&classMethod->method(), &val) && |
| 2558 |
#ifdef ENABLE_DECORATORS |
| 2559 |
decoratorsParsed && |
| 2560 |
#endif |
| 2561 |
builder.classMethod(key, val, |
| 2562 |
#ifdef ENABLE_DECORATORS |
| 2563 |
methodDecoratorsArray, |
| 2564 |
#endif |
| 2565 |
kind, isStatic, &classMethod->pn_pos, dst); |
| 2566 |
} |
| 2567 |
|
| 2568 |
bool ASTSerializer::classField(ClassField* classField, MutableHandleValue dst) { |
| 2569 |
RootedValue key(cx), val(cx); |
| 2570 |
// Dig through the lambda and get to the actual expression |
| 2571 |
ParseNode* value = classField->initializer() |
| 2572 |
->body() |
| 2573 |
->body() |
| 2574 |
->scopeBody() |
| 2575 |
->as<ListNode>() |
| 2576 |
.head() |
| 2577 |
->as<UnaryNode>() |
| 2578 |
.kid() |
| 2579 |
->as<BinaryNode>() |
| 2580 |
.right(); |
| 2581 |
#ifdef ENABLE_DECORATORS |
| 2582 |
NodeVector fieldDecorators(cx); |
| 2583 |
RootedValue fieldDecoratorsArray(cx); |
| 2584 |
TokenPos decoratorPos; |
| 2585 |
bool decoratorsParsed = true; |
| 2586 |
if (classField->decorators()) { |
| 2587 |
decoratorPos.begin = classField->decorators()->head()->pn_pos.begin; |
| 2588 |
decoratorPos.end = classField->decorators()->last()->pn_pos.end; |
| 2589 |
decoratorsParsed = |
| 2590 |
expressions(classField->decorators(), fieldDecorators) && |
| 2591 |
builder.sequenceExpression(fieldDecorators, &decoratorPos, |
| 2592 |
&fieldDecoratorsArray); |
| 2593 |
} else { |
| 2594 |
fieldDecoratorsArray.setMagic(JS_SERIALIZE_NO_NODE); |
| 2595 |
} |
| 2596 |
#endif |
| 2597 |
// RawUndefinedExpr is the node we use for "there is no initializer". If one |
| 2598 |
// writes, literally, `x = undefined;`, it will not be a RawUndefinedExpr |
| 2599 |
// node, but rather a variable reference. |
| 2600 |
// Behavior for "there is no initializer" should be { ..., "init": null } |
| 2601 |
if (value->getKind() != ParseNodeKind::RawUndefinedExpr) { |
| 2602 |
if (!expression(value, &val)) { |
| 2603 |
return false; |
| 2604 |
} |
| 2605 |
} else { |
| 2606 |
val.setNull(); |
| 2607 |
} |
| 2608 |
return propertyName(&classField->name(), &key) && |
| 2609 |
#ifdef ENABLE_DECORATORS |
| 2610 |
decoratorsParsed && |
| 2611 |
#endif |
| 2612 |
builder.classField(key, val, |
| 2613 |
#ifdef ENABLE_DECORATORS |
| 2614 |
fieldDecoratorsArray, |
| 2615 |
#endif |
| 2616 |
&classField->pn_pos, dst); |
| 2617 |
} |
| 2618 |
|
| 2619 |
bool ASTSerializer::staticClassBlock(StaticClassBlock* staticClassBlock, |
| 2620 |
MutableHandleValue dst) { |
| 2621 |
FunctionNode* fun = staticClassBlock->function(); |
| 2622 |
|
| 2623 |
NodeVector args(cx); |
| 2624 |
NodeVector defaults(cx); |
| 2625 |
|
| 2626 |
RootedValue body(cx), rest(cx); |
| 2627 |
rest.setNull(); |
| 2628 |
return functionArgsAndBody(fun->body(), args, defaults, false, false, &body, |
| 2629 |
&rest) && |
| 2630 |
builder.staticClassBlock(body, &staticClassBlock->pn_pos, dst); |
| 2631 |
} |
| 2632 |
|
| 2633 |
bool ASTSerializer::leftAssociate(ListNode* node, MutableHandleValue dst) { |
| 2634 |
MOZ_ASSERT(!node->empty()); |
| 2635 |
|
| 2636 |
ParseNodeKind pnk = node->getKind(); |
| 2637 |
bool lor = pnk == ParseNodeKind::OrExpr; |
| 2638 |
bool coalesce = pnk == ParseNodeKind::CoalesceExpr; |
| 2639 |
bool logop = lor || coalesce || pnk == ParseNodeKind::AndExpr; |
| 2640 |
|
| 2641 |
ParseNode* head = node->head(); |
| 2642 |
RootedValue left(cx); |
| 2643 |
if (!expression(head, &left)) { |
| 2644 |
return false; |
| 2645 |
} |
| 2646 |
for (ParseNode* next : node->contentsFrom(head->pn_next)) { |
| 2647 |
RootedValue right(cx); |
| 2648 |
if (!expression(next, &right)) { |
| 2649 |
return false; |
| 2650 |
} |
| 2651 |
|
| 2652 |
TokenPos subpos(node->pn_pos.begin, next->pn_pos.end); |
| 2653 |
|
| 2654 |
if (logop) { |
| 2655 |
if (!builder.logicalExpression(pnk, left, right, &subpos, &left)) { |
| 2656 |
return false; |
| 2657 |
} |
| 2658 |
} else { |
| 2659 |
BinaryOperator op = binop(node->getKind()); |
| 2660 |
LOCAL_ASSERT(op > BINOP_ERR && op < BINOP_LIMIT); |
| 2661 |
|
| 2662 |
if (!builder.binaryExpression(op, left, right, &subpos, &left)) { |
| 2663 |
return false; |
| 2664 |
} |
| 2665 |
} |
| 2666 |
} |
| 2667 |
|
| 2668 |
dst.set(left); |
| 2669 |
return true; |
| 2670 |
} |
| 2671 |
|
| 2672 |
bool ASTSerializer::rightAssociate(ListNode* node, MutableHandleValue dst) { |
| 2673 |
MOZ_ASSERT(!node->empty()); |
| 2674 |
|
| 2675 |
// First, we need to reverse the list, so that we can traverse it in the right |
| 2676 |
// order. It's OK to destructively reverse the list, because there are no |
| 2677 |
// other consumers. |
| 2678 |
|
| 2679 |
ParseNode* head = node->head(); |
| 2680 |
ParseNode* prev = nullptr; |
| 2681 |
ParseNode* current = head; |
| 2682 |
ParseNode* next; |
| 2683 |
while (current != nullptr) { |
| 2684 |
next = current->pn_next; |
| 2685 |
current->pn_next = prev; |
| 2686 |
prev = current; |
| 2687 |
current = next; |
| 2688 |
} |
| 2689 |
|
| 2690 |
head = prev; |
| 2691 |
|
| 2692 |
RootedValue right(cx); |
| 2693 |
if (!expression(head, &right)) { |
| 2694 |
return false; |
| 2695 |
} |
| 2696 |
for (ParseNode* next = head->pn_next; next; next = next->pn_next) { |
| 2697 |
RootedValue left(cx); |
| 2698 |
if (!expression(next, &left)) { |
| 2699 |
return false; |
| 2700 |
} |
| 2701 |
|
| 2702 |
TokenPos subpos(node->pn_pos.begin, next->pn_pos.end); |
| 2703 |
|
| 2704 |
BinaryOperator op = binop(node->getKind()); |
| 2705 |
LOCAL_ASSERT(op > BINOP_ERR && op < BINOP_LIMIT); |
| 2706 |
|
| 2707 |
if (!builder.binaryExpression(op, left, right, &subpos, &right)) { |
| 2708 |
return false; |
| 2709 |
} |
| 2710 |
} |
| 2711 |
|
| 2712 |
dst.set(right); |
| 2713 |
return true; |
| 2714 |
} |
| 2715 |
|
| 2716 |
bool ASTSerializer::expression(ParseNode* pn, MutableHandleValue dst) { |
| 2717 |
AutoCheckRecursionLimit recursion(cx); |
| 2718 |
if (!recursion.check(cx)) { |
| 2719 |
return false; |
| 2720 |
} |
| 2721 |
|
| 2722 |
switch (pn->getKind()) { |
| 2723 |
case ParseNodeKind::Function: { |
| 2724 |
FunctionNode* funNode = &pn->as<FunctionNode>(); |
| 2725 |
ASTType type = |
| 2726 |
funNode->funbox()->isArrow() ? AST_ARROW_EXPR : AST_FUNC_EXPR; |
| 2727 |
return function(funNode, type, dst); |
| 2728 |
} |
| 2729 |
|
| 2730 |
case ParseNodeKind::CommaExpr: { |
| 2731 |
NodeVector exprs(cx); |
| 2732 |
return expressions(&pn->as<ListNode>(), exprs) && |
| 2733 |
builder.sequenceExpression(exprs, &pn->pn_pos, dst); |
| 2734 |
} |
| 2735 |
|
| 2736 |
case ParseNodeKind::ConditionalExpr: { |
| 2737 |
ConditionalExpression* condNode = &pn->as<ConditionalExpression>(); |
| 2738 |
ParseNode* testNode = condNode->kid1(); |
| 2739 |
ParseNode* consNode = condNode->kid2(); |
| 2740 |
ParseNode* altNode = condNode->kid3(); |
| 2741 |
MOZ_ASSERT(condNode->pn_pos.encloses(testNode->pn_pos)); |
| 2742 |
MOZ_ASSERT(condNode->pn_pos.encloses(consNode->pn_pos)); |
| 2743 |
MOZ_ASSERT(condNode->pn_pos.encloses(altNode->pn_pos)); |
| 2744 |
|
| 2745 |
RootedValue test(cx), cons(cx), alt(cx); |
| 2746 |
|
| 2747 |
return expression(testNode, &test) && expression(consNode, &cons) && |
| 2748 |
expression(altNode, &alt) && |
| 2749 |
builder.conditionalExpression(test, cons, alt, &condNode->pn_pos, |
| 2750 |
dst); |
| 2751 |
} |
| 2752 |
|
| 2753 |
case ParseNodeKind::CoalesceExpr: |
| 2754 |
case ParseNodeKind::OrExpr: |
| 2755 |
case ParseNodeKind::AndExpr: |
| 2756 |
return leftAssociate(&pn->as<ListNode>(), dst); |
| 2757 |
|
| 2758 |
case ParseNodeKind::PreIncrementExpr: |
| 2759 |
case ParseNodeKind::PreDecrementExpr: { |
| 2760 |
UnaryNode* incDec = &pn->as<UnaryNode>(); |
| 2761 |
ParseNode* operand = incDec->kid(); |
| 2762 |
MOZ_ASSERT(incDec->pn_pos.encloses(operand->pn_pos)); |
| 2763 |
|
| 2764 |
bool inc = incDec->isKind(ParseNodeKind::PreIncrementExpr); |
| 2765 |
RootedValue expr(cx); |
| 2766 |
return expression(operand, &expr) && |
| 2767 |
builder.updateExpression(expr, inc, true, &incDec->pn_pos, dst); |
| 2768 |
} |
| 2769 |
|
| 2770 |
case ParseNodeKind::PostIncrementExpr: |
| 2771 |
case ParseNodeKind::PostDecrementExpr: { |
| 2772 |
UnaryNode* incDec = &pn->as<UnaryNode>(); |
| 2773 |
ParseNode* operand = incDec->kid(); |
| 2774 |
MOZ_ASSERT(incDec->pn_pos.encloses(operand->pn_pos)); |
| 2775 |
|
| 2776 |
bool inc = incDec->isKind(ParseNodeKind::PostIncrementExpr); |
| 2777 |
RootedValue expr(cx); |
| 2778 |
return expression(operand, &expr) && |
| 2779 |
builder.updateExpression(expr, inc, false, &incDec->pn_pos, dst); |
| 2780 |
} |
| 2781 |
|
| 2782 |
case ParseNodeKind::AssignExpr: |
| 2783 |
case ParseNodeKind::AddAssignExpr: |
| 2784 |
case ParseNodeKind::SubAssignExpr: |
| 2785 |
case ParseNodeKind::CoalesceAssignExpr: |
| 2786 |
case ParseNodeKind::OrAssignExpr: |
| 2787 |
case ParseNodeKind::AndAssignExpr: |
| 2788 |
case ParseNodeKind::BitOrAssignExpr: |
| 2789 |
case ParseNodeKind::BitXorAssignExpr: |
| 2790 |
case ParseNodeKind::BitAndAssignExpr: |
| 2791 |
case ParseNodeKind::LshAssignExpr: |
| 2792 |
case ParseNodeKind::RshAssignExpr: |
| 2793 |
case ParseNodeKind::UrshAssignExpr: |
| 2794 |
case ParseNodeKind::MulAssignExpr: |
| 2795 |
case ParseNodeKind::DivAssignExpr: |
| 2796 |
case ParseNodeKind::ModAssignExpr: |
| 2797 |
case ParseNodeKind::PowAssignExpr: { |
| 2798 |
AssignmentNode* assignNode = &pn->as<AssignmentNode>(); |
| 2799 |
ParseNode* lhsNode = assignNode->left(); |
| 2800 |
ParseNode* rhsNode = assignNode->right(); |
| 2801 |
MOZ_ASSERT(assignNode->pn_pos.encloses(lhsNode->pn_pos)); |
| 2802 |
MOZ_ASSERT(assignNode->pn_pos.encloses(rhsNode->pn_pos)); |
| 2803 |
|
| 2804 |
AssignmentOperator op = aop(assignNode->getKind()); |
| 2805 |
LOCAL_ASSERT(op > AOP_ERR && op < AOP_LIMIT); |
| 2806 |
|
| 2807 |
RootedValue lhs(cx), rhs(cx); |
| 2808 |
return pattern(lhsNode, &lhs) && expression(rhsNode, &rhs) && |
| 2809 |
builder.assignmentExpression(op, lhs, rhs, &assignNode->pn_pos, |
| 2810 |
dst); |
| 2811 |
} |
| 2812 |
|
| 2813 |
case ParseNodeKind::AddExpr: |
| 2814 |
case ParseNodeKind::SubExpr: |
| 2815 |
case ParseNodeKind::StrictEqExpr: |
| 2816 |
case ParseNodeKind::EqExpr: |
| 2817 |
case ParseNodeKind::StrictNeExpr: |
| 2818 |
case ParseNodeKind::NeExpr: |
| 2819 |
case ParseNodeKind::LtExpr: |
| 2820 |
case ParseNodeKind::LeExpr: |
| 2821 |
case ParseNodeKind::GtExpr: |
| 2822 |
case ParseNodeKind::GeExpr: |
| 2823 |
case ParseNodeKind::LshExpr: |
| 2824 |
case ParseNodeKind::RshExpr: |
| 2825 |
case ParseNodeKind::UrshExpr: |
| 2826 |
case ParseNodeKind::MulExpr: |
| 2827 |
case ParseNodeKind::DivExpr: |
| 2828 |
case ParseNodeKind::ModExpr: |
| 2829 |
case ParseNodeKind::BitOrExpr: |
| 2830 |
case ParseNodeKind::BitXorExpr: |
| 2831 |
case ParseNodeKind::BitAndExpr: |
| 2832 |
case ParseNodeKind::InExpr: |
| 2833 |
case ParseNodeKind::PrivateInExpr: |
| 2834 |
case ParseNodeKind::InstanceOfExpr: |
| 2835 |
return leftAssociate(&pn->as<ListNode>(), dst); |
| 2836 |
|
| 2837 |
case ParseNodeKind::PowExpr: |
| 2838 |
return rightAssociate(&pn->as<ListNode>(), dst); |
| 2839 |
|
| 2840 |
case ParseNodeKind::DeleteNameExpr: |
| 2841 |
case ParseNodeKind::DeletePropExpr: |
| 2842 |
case ParseNodeKind::DeleteElemExpr: |
| 2843 |
case ParseNodeKind::DeleteExpr: |
| 2844 |
case ParseNodeKind::TypeOfNameExpr: |
| 2845 |
case ParseNodeKind::TypeOfExpr: |
| 2846 |
case ParseNodeKind::VoidExpr: |
| 2847 |
case ParseNodeKind::NotExpr: |
| 2848 |
case ParseNodeKind::BitNotExpr: |
| 2849 |
case ParseNodeKind::PosExpr: |
| 2850 |
case ParseNodeKind::AwaitExpr: |
| 2851 |
case ParseNodeKind::NegExpr: { |
| 2852 |
UnaryNode* unaryNode = &pn->as<UnaryNode>(); |
| 2853 |
ParseNode* operand = unaryNode->kid(); |
| 2854 |
MOZ_ASSERT(unaryNode->pn_pos.encloses(operand->pn_pos)); |
| 2855 |
|
| 2856 |
UnaryOperator op = unop(unaryNode->getKind()); |
| 2857 |
LOCAL_ASSERT(op > UNOP_ERR && op < UNOP_LIMIT); |
| 2858 |
|
| 2859 |
RootedValue expr(cx); |
| 2860 |
return expression(operand, &expr) && |
| 2861 |
builder.unaryExpression(op, expr, &unaryNode->pn_pos, dst); |
| 2862 |
} |
| 2863 |
|
| 2864 |
case ParseNodeKind::DeleteOptionalChainExpr: { |
| 2865 |
RootedValue expr(cx); |
| 2866 |
return expression(pn->as<UnaryNode>().kid(), &expr) && |
| 2867 |
builder.deleteOptionalExpression(expr, &pn->pn_pos, dst); |
| 2868 |
} |
| 2869 |
|
| 2870 |
case ParseNodeKind::OptionalChain: { |
| 2871 |
RootedValue expr(cx); |
| 2872 |
return expression(pn->as<UnaryNode>().kid(), &expr) && |
| 2873 |
builder.optionalExpression(expr, &pn->pn_pos, dst); |
| 2874 |
} |
| 2875 |
|
| 2876 |
case ParseNodeKind::NewExpr: |
| 2877 |
case ParseNodeKind::TaggedTemplateExpr: |
| 2878 |
case ParseNodeKind::CallExpr: |
| 2879 |
case ParseNodeKind::OptionalCallExpr: |
| 2880 |
case ParseNodeKind::SuperCallExpr: { |
| 2881 |
BinaryNode* node = &pn->as<BinaryNode>(); |
| 2882 |
ParseNode* calleeNode = node->left(); |
| 2883 |
ListNode* argsList = &node->right()->as<ListNode>(); |
| 2884 |
MOZ_ASSERT(node->pn_pos.encloses(calleeNode->pn_pos)); |
| 2885 |
|
| 2886 |
RootedValue callee(cx); |
| 2887 |
if (node->isKind(ParseNodeKind::SuperCallExpr)) { |
| 2888 |
MOZ_ASSERT(calleeNode->isKind(ParseNodeKind::SuperBase)); |
| 2889 |
if (!builder.super(&calleeNode->pn_pos, &callee)) { |
| 2890 |
return false; |
| 2891 |
} |
| 2892 |
} else { |
| 2893 |
if (!expression(calleeNode, &callee)) { |
| 2894 |
return false; |
| 2895 |
} |
| 2896 |
} |
| 2897 |
|
| 2898 |
NodeVector args(cx); |
| 2899 |
if (!args.reserve(argsList->count())) { |
| 2900 |
return false; |
| 2901 |
} |
| 2902 |
|
| 2903 |
for (ParseNode* argNode : argsList->contents()) { |
| 2904 |
MOZ_ASSERT(node->pn_pos.encloses(argNode->pn_pos)); |
| 2905 |
|
| 2906 |
RootedValue arg(cx); |
| 2907 |
if (!expression(argNode, &arg)) { |
| 2908 |
return false; |
| 2909 |
} |
| 2910 |
args.infallibleAppend(arg); |
| 2911 |
} |
| 2912 |
|
| 2913 |
if (node->getKind() == ParseNodeKind::TaggedTemplateExpr) { |
| 2914 |
return builder.taggedTemplate(callee, args, &node->pn_pos, dst); |
| 2915 |
} |
| 2916 |
|
| 2917 |
bool isOptional = node->isKind(ParseNodeKind::OptionalCallExpr); |
| 2918 |
|
| 2919 |
// SUPERCALL is Call(super, args) |
| 2920 |
return node->isKind(ParseNodeKind::NewExpr) |
| 2921 |
? builder.newExpression(callee, args, &node->pn_pos, dst) |
| 2922 |
: builder.callExpression(callee, args, &node->pn_pos, dst, |
| 2923 |
isOptional); |
| 2924 |
} |
| 2925 |
|
| 2926 |
case ParseNodeKind::DotExpr: |
| 2927 |
case ParseNodeKind::OptionalDotExpr: |
| 2928 |
case ParseNodeKind::ArgumentsLength: { |
| 2929 |
PropertyAccessBase* prop = &pn->as<PropertyAccessBase>(); |
| 2930 |
MOZ_ASSERT(prop->pn_pos.encloses(prop->expression().pn_pos)); |
| 2931 |
|
| 2932 |
bool isSuper = |
| 2933 |
prop->is<PropertyAccess>() && prop->as<PropertyAccess>().isSuper(); |
| 2934 |
|
| 2935 |
RootedValue expr(cx); |
| 2936 |
RootedValue propname(cx); |
| 2937 |
Rooted<JSAtom*> pnAtom( |
| 2938 |
cx, parser->liftParserAtomToJSAtom(prop->key().atom())); |
| 2939 |
if (!pnAtom.get()) { |
| 2940 |
return false; |
| 2941 |
} |
| 2942 |
|
| 2943 |
if (isSuper) { |
| 2944 |
if (!builder.super(&prop->expression().pn_pos, &expr)) { |
| 2945 |
return false; |
| 2946 |
} |
| 2947 |
} else { |
| 2948 |
if (!expression(&prop->expression(), &expr)) { |
| 2949 |
return false; |
| 2950 |
} |
| 2951 |
} |
| 2952 |
|
| 2953 |
bool isOptional = prop->isKind(ParseNodeKind::OptionalDotExpr); |
| 2954 |
|
| 2955 |
return identifier(pnAtom, nullptr, &propname) && |
| 2956 |
builder.memberExpression(false, expr, propname, &prop->pn_pos, dst, |
| 2957 |
isOptional); |
| 2958 |
} |
| 2959 |
|
| 2960 |
case ParseNodeKind::ElemExpr: |
| 2961 |
case ParseNodeKind::OptionalElemExpr: { |
| 2962 |
PropertyByValueBase* elem = &pn->as<PropertyByValueBase>(); |
| 2963 |
MOZ_ASSERT(elem->pn_pos.encloses(elem->expression().pn_pos)); |
| 2964 |
MOZ_ASSERT(elem->pn_pos.encloses(elem->key().pn_pos)); |
| 2965 |
|
| 2966 |
bool isSuper = |
| 2967 |
elem->is<PropertyByValue>() && elem->as<PropertyByValue>().isSuper(); |
| 2968 |
|
| 2969 |
RootedValue expr(cx), key(cx); |
| 2970 |
|
| 2971 |
if (isSuper) { |
| 2972 |
if (!builder.super(&elem->expression().pn_pos, &expr)) { |
| 2973 |
return false; |
| 2974 |
} |
| 2975 |
} else { |
| 2976 |
if (!expression(&elem->expression(), &expr)) { |
| 2977 |
return false; |
| 2978 |
} |
| 2979 |
} |
| 2980 |
|
| 2981 |
bool isOptional = elem->isKind(ParseNodeKind::OptionalElemExpr); |
| 2982 |
|
| 2983 |
return expression(&elem->key(), &key) && |
| 2984 |
builder.memberExpression(true, expr, key, &elem->pn_pos, dst, |
| 2985 |
isOptional); |
| 2986 |
} |
| 2987 |
|
| 2988 |
case ParseNodeKind::PrivateMemberExpr: |
| 2989 |
case ParseNodeKind::OptionalPrivateMemberExpr: { |
| 2990 |
PrivateMemberAccessBase* privateExpr = &pn->as<PrivateMemberAccessBase>(); |
| 2991 |
MOZ_ASSERT( |
| 2992 |
privateExpr->pn_pos.encloses(privateExpr->expression().pn_pos)); |
| 2993 |
MOZ_ASSERT( |
| 2994 |
privateExpr->pn_pos.encloses(privateExpr->privateName().pn_pos)); |
| 2995 |
|
| 2996 |
RootedValue expr(cx), key(cx); |
| 2997 |
|
| 2998 |
if (!expression(&privateExpr->expression(), &expr)) { |
| 2999 |
return false; |
| 3000 |
} |
| 3001 |
|
| 3002 |
bool isOptional = |
| 3003 |
privateExpr->isKind(ParseNodeKind::OptionalPrivateMemberExpr); |
| 3004 |
|
| 3005 |
return expression(&privateExpr->privateName(), &key) && |
| 3006 |
builder.memberExpression(true, expr, key, &privateExpr->pn_pos, |
| 3007 |
dst, isOptional); |
| 3008 |
} |
| 3009 |
|
| 3010 |
case ParseNodeKind::CallSiteObj: { |
| 3011 |
CallSiteNode* callSiteObj = &pn->as<CallSiteNode>(); |
| 3012 |
ListNode* rawNodes = callSiteObj->rawNodes(); |
| 3013 |
NodeVector raw(cx); |
| 3014 |
if (!raw.reserve(rawNodes->count())) { |
| 3015 |
return false; |
| 3016 |
} |
| 3017 |
for (ParseNode* item : rawNodes->contents()) { |
| 3018 |
NameNode* rawItem = &item->as<NameNode>(); |
| 3019 |
MOZ_ASSERT(callSiteObj->pn_pos.encloses(rawItem->pn_pos)); |
| 3020 |
|
| 3021 |
JSAtom* exprAtom = parser->liftParserAtomToJSAtom(rawItem->atom()); |
| 3022 |
if (!exprAtom) { |
| 3023 |
return false; |
| 3024 |
} |
| 3025 |
RootedValue expr(cx, StringValue(exprAtom)); |
| 3026 |
raw.infallibleAppend(expr); |
| 3027 |
} |
| 3028 |
|
| 3029 |
NodeVector cooked(cx); |
| 3030 |
if (!cooked.reserve(callSiteObj->count() - 1)) { |
| 3031 |
return false; |
| 3032 |
} |
| 3033 |
|
| 3034 |
for (ParseNode* cookedItem : |
| 3035 |
callSiteObj->contentsFrom(rawNodes->pn_next)) { |
| 3036 |
MOZ_ASSERT(callSiteObj->pn_pos.encloses(cookedItem->pn_pos)); |
| 3037 |
|
| 3038 |
RootedValue expr(cx); |
| 3039 |
if (cookedItem->isKind(ParseNodeKind::RawUndefinedExpr)) { |
| 3040 |
expr.setUndefined(); |
| 3041 |
} else { |
| 3042 |
MOZ_ASSERT(cookedItem->isKind(ParseNodeKind::TemplateStringExpr)); |
| 3043 |
JSAtom* exprAtom = |
| 3044 |
parser->liftParserAtomToJSAtom(cookedItem->as<NameNode>().atom()); |
| 3045 |
if (!exprAtom) { |
| 3046 |
return false; |
| 3047 |
} |
| 3048 |
expr.setString(exprAtom); |
| 3049 |
} |
| 3050 |
cooked.infallibleAppend(expr); |
| 3051 |
} |
| 3052 |
|
| 3053 |
return builder.callSiteObj(raw, cooked, &callSiteObj->pn_pos, dst); |
| 3054 |
} |
| 3055 |
|
| 3056 |
case ParseNodeKind::ArrayExpr: { |
| 3057 |
ListNode* array = &pn->as<ListNode>(); |
| 3058 |
NodeVector elts(cx); |
| 3059 |
if (!elts.reserve(array->count())) { |
| 3060 |
return false; |
| 3061 |
} |
| 3062 |
|
| 3063 |
for (ParseNode* item : array->contents()) { |
| 3064 |
MOZ_ASSERT(array->pn_pos.encloses(item->pn_pos)); |
| 3065 |
|
| 3066 |
if (item->isKind(ParseNodeKind::Elision)) { |
| 3067 |
elts.infallibleAppend(NullValue()); |
| 3068 |
} else { |
| 3069 |
RootedValue expr(cx); |
| 3070 |
if (!expression(item, &expr)) { |
| 3071 |
return false; |
| 3072 |
} |
| 3073 |
elts.infallibleAppend(expr); |
| 3074 |
} |
| 3075 |
} |
| 3076 |
|
| 3077 |
return builder.arrayExpression(elts, &array->pn_pos, dst); |
| 3078 |
} |
| 3079 |
|
| 3080 |
case ParseNodeKind::Spread: { |
| 3081 |
RootedValue expr(cx); |
| 3082 |
return expression(pn->as<UnaryNode>().kid(), &expr) && |
| 3083 |
builder.spreadExpression(expr, &pn->pn_pos, dst); |
| 3084 |
} |
| 3085 |
|
| 3086 |
case ParseNodeKind::ComputedName: { |
| 3087 |
if (pn->as<UnaryNode>().isSyntheticComputedName()) { |
| 3088 |
return literal(pn->as<UnaryNode>().kid(), dst); |
| 3089 |
} |
| 3090 |
RootedValue name(cx); |
| 3091 |
return expression(pn->as<UnaryNode>().kid(), &name) && |
| 3092 |
builder.computedName(name, &pn->pn_pos, dst); |
| 3093 |
} |
| 3094 |
|
| 3095 |
case ParseNodeKind::ObjectExpr: { |
| 3096 |
ListNode* obj = &pn->as<ListNode>(); |
| 3097 |
NodeVector elts(cx); |
| 3098 |
if (!elts.reserve(obj->count())) { |
| 3099 |
return false; |
| 3100 |
} |
| 3101 |
|
| 3102 |
for (ParseNode* item : obj->contents()) { |
| 3103 |
MOZ_ASSERT(obj->pn_pos.encloses(item->pn_pos)); |
| 3104 |
|
| 3105 |
RootedValue prop(cx); |
| 3106 |
if (!property(item, &prop)) { |
| 3107 |
return false; |
| 3108 |
} |
| 3109 |
elts.infallibleAppend(prop); |
| 3110 |
} |
| 3111 |
|
| 3112 |
return builder.objectExpression(elts, &obj->pn_pos, dst); |
| 3113 |
} |
| 3114 |
|
| 3115 |
case ParseNodeKind::PrivateName: |
| 3116 |
case ParseNodeKind::Name: |
| 3117 |
return identifier(&pn->as<NameNode>(), dst); |
| 3118 |
|
| 3119 |
case ParseNodeKind::ThisExpr: |
| 3120 |
return builder.thisExpression(&pn->pn_pos, dst); |
| 3121 |
|
| 3122 |
case ParseNodeKind::TemplateStringListExpr: { |
| 3123 |
ListNode* list = &pn->as<ListNode>(); |
| 3124 |
NodeVector elts(cx); |
| 3125 |
if (!elts.reserve(list->count())) { |
| 3126 |
return false; |
| 3127 |
} |
| 3128 |
|
| 3129 |
for (ParseNode* item : list->contents()) { |
| 3130 |
MOZ_ASSERT(list->pn_pos.encloses(item->pn_pos)); |
| 3131 |
|
| 3132 |
RootedValue expr(cx); |
| 3133 |
if (!expression(item, &expr)) { |
| 3134 |
return false; |
| 3135 |
} |
| 3136 |
elts.infallibleAppend(expr); |
| 3137 |
} |
| 3138 |
|
| 3139 |
return builder.templateLiteral(elts, &list->pn_pos, dst); |
| 3140 |
} |
| 3141 |
|
| 3142 |
case ParseNodeKind::TemplateStringExpr: |
| 3143 |
case ParseNodeKind::StringExpr: |
| 3144 |
case ParseNodeKind::RegExpExpr: |
| 3145 |
case ParseNodeKind::NumberExpr: |
| 3146 |
case ParseNodeKind::BigIntExpr: |
| 3147 |
case ParseNodeKind::TrueExpr: |
| 3148 |
case ParseNodeKind::FalseExpr: |
| 3149 |
case ParseNodeKind::NullExpr: |
| 3150 |
case ParseNodeKind::RawUndefinedExpr: |
| 3151 |
return literal(pn, dst); |
| 3152 |
|
| 3153 |
case ParseNodeKind::YieldStarExpr: { |
| 3154 |
UnaryNode* yieldNode = &pn->as<UnaryNode>(); |
| 3155 |
ParseNode* operand = yieldNode->kid(); |
| 3156 |
MOZ_ASSERT(yieldNode->pn_pos.encloses(operand->pn_pos)); |
| 3157 |
|
| 3158 |
RootedValue arg(cx); |
| 3159 |
return expression(operand, &arg) && |
| 3160 |
builder.yieldExpression(arg, Delegating, &yieldNode->pn_pos, dst); |
| 3161 |
} |
| 3162 |
|
| 3163 |
case ParseNodeKind::YieldExpr: { |
| 3164 |
UnaryNode* yieldNode = &pn->as<UnaryNode>(); |
| 3165 |
ParseNode* operand = yieldNode->kid(); |
| 3166 |
MOZ_ASSERT_IF(operand, yieldNode->pn_pos.encloses(operand->pn_pos)); |
| 3167 |
|
| 3168 |
RootedValue arg(cx); |
| 3169 |
return optExpression(operand, &arg) && |
| 3170 |
builder.yieldExpression(arg, NotDelegating, &yieldNode->pn_pos, |
| 3171 |
dst); |
| 3172 |
} |
| 3173 |
|
| 3174 |
case ParseNodeKind::ClassDecl: |
| 3175 |
return classDefinition(&pn->as<ClassNode>(), true, dst); |
| 3176 |
|
| 3177 |
case ParseNodeKind::NewTargetExpr: { |
| 3178 |
auto* node = &pn->as<NewTargetNode>(); |
| 3179 |
ParseNode* firstNode = node->newHolder(); |
| 3180 |
MOZ_ASSERT(firstNode->isKind(ParseNodeKind::PosHolder)); |
| 3181 |
MOZ_ASSERT(node->pn_pos.encloses(firstNode->pn_pos)); |
| 3182 |
|
| 3183 |
ParseNode* secondNode = node->targetHolder(); |
| 3184 |
MOZ_ASSERT(secondNode->isKind(ParseNodeKind::PosHolder)); |
| 3185 |
MOZ_ASSERT(node->pn_pos.encloses(secondNode->pn_pos)); |
| 3186 |
|
| 3187 |
RootedValue firstIdent(cx); |
| 3188 |
RootedValue secondIdent(cx); |
| 3189 |
|
| 3190 |
Rooted<JSAtom*> firstStr(cx, cx->names().new_); |
| 3191 |
Rooted<JSAtom*> secondStr(cx, cx->names().target); |
| 3192 |
|
| 3193 |
return identifier(firstStr, &firstNode->pn_pos, &firstIdent) && |
| 3194 |
identifier(secondStr, &secondNode->pn_pos, &secondIdent) && |
| 3195 |
builder.metaProperty(firstIdent, secondIdent, &node->pn_pos, dst); |
| 3196 |
} |
| 3197 |
|
| 3198 |
case ParseNodeKind::ImportMetaExpr: { |
| 3199 |
BinaryNode* node = &pn->as<BinaryNode>(); |
| 3200 |
ParseNode* firstNode = node->left(); |
| 3201 |
MOZ_ASSERT(firstNode->isKind(ParseNodeKind::PosHolder)); |
| 3202 |
MOZ_ASSERT(node->pn_pos.encloses(firstNode->pn_pos)); |
| 3203 |
|
| 3204 |
ParseNode* secondNode = node->right(); |
| 3205 |
MOZ_ASSERT(secondNode->isKind(ParseNodeKind::PosHolder)); |
| 3206 |
MOZ_ASSERT(node->pn_pos.encloses(secondNode->pn_pos)); |
| 3207 |
|
| 3208 |
RootedValue firstIdent(cx); |
| 3209 |
RootedValue secondIdent(cx); |
| 3210 |
|
| 3211 |
Rooted<JSAtom*> firstStr(cx, cx->names().import); |
| 3212 |
Rooted<JSAtom*> secondStr(cx, cx->names().meta); |
| 3213 |
|
| 3214 |
return identifier(firstStr, &firstNode->pn_pos, &firstIdent) && |
| 3215 |
identifier(secondStr, &secondNode->pn_pos, &secondIdent) && |
| 3216 |
builder.metaProperty(firstIdent, secondIdent, &node->pn_pos, dst); |
| 3217 |
} |
| 3218 |
|
| 3219 |
case ParseNodeKind::CallImportExpr: { |
| 3220 |
BinaryNode* node = &pn->as<BinaryNode>(); |
| 3221 |
ParseNode* identNode = node->left(); |
| 3222 |
MOZ_ASSERT(identNode->isKind(ParseNodeKind::PosHolder)); |
| 3223 |
MOZ_ASSERT(identNode->pn_pos.encloses(identNode->pn_pos)); |
| 3224 |
|
| 3225 |
ParseNode* specNode = node->right(); |
| 3226 |
MOZ_ASSERT(specNode->is<BinaryNode>()); |
| 3227 |
MOZ_ASSERT(specNode->isKind(ParseNodeKind::CallImportSpec)); |
| 3228 |
|
| 3229 |
ParseNode* argNode = specNode->as<BinaryNode>().left(); |
| 3230 |
MOZ_ASSERT(node->pn_pos.encloses(argNode->pn_pos)); |
| 3231 |
|
| 3232 |
ParseNode* optionsArgNode = specNode->as<BinaryNode>().right(); |
| 3233 |
MOZ_ASSERT(node->pn_pos.encloses(optionsArgNode->pn_pos)); |
| 3234 |
|
| 3235 |
RootedValue ident(cx); |
| 3236 |
Handle<PropertyName*> name = cx->names().import; |
| 3237 |
if (!identifier(name, &identNode->pn_pos, &ident)) { |
| 3238 |
return false; |
| 3239 |
} |
| 3240 |
|
| 3241 |
NodeVector args(cx); |
| 3242 |
|
| 3243 |
RootedValue arg(cx); |
| 3244 |
if (!expression(argNode, &arg)) { |
| 3245 |
return false; |
| 3246 |
} |
| 3247 |
if (!args.append(arg)) { |
| 3248 |
return false; |
| 3249 |
} |
| 3250 |
|
| 3251 |
if (!optionsArgNode->isKind(ParseNodeKind::PosHolder)) { |
| 3252 |
RootedValue optionsArg(cx); |
| 3253 |
if (!expression(optionsArgNode, &optionsArg)) { |
| 3254 |
return false; |
| 3255 |
} |
| 3256 |
if (!args.append(optionsArg)) { |
| 3257 |
return false; |
| 3258 |
} |
| 3259 |
} |
| 3260 |
|
| 3261 |
return builder.callImportExpression(ident, args, &pn->pn_pos, dst); |
| 3262 |
} |
| 3263 |
|
| 3264 |
case ParseNodeKind::SetThis: { |
| 3265 |
// SETTHIS is used to assign the result of a super() call to |this|. |
| 3266 |
// It's not part of the original AST, so just forward to the call. |
| 3267 |
BinaryNode* node = &pn->as<BinaryNode>(); |
| 3268 |
MOZ_ASSERT(node->left()->isKind(ParseNodeKind::Name)); |
| 3269 |
return expression(node->right(), dst); |
| 3270 |
} |
| 3271 |
|
| 3272 |
default: |
| 3273 |
LOCAL_NOT_REACHED("unexpected expression type"); |
| 3274 |
} |
| 3275 |
} |
| 3276 |
|
| 3277 |
bool ASTSerializer::propertyName(ParseNode* key, MutableHandleValue dst) { |
| 3278 |
if (key->isKind(ParseNodeKind::ComputedName)) { |
| 3279 |
return expression(key, dst); |
| 3280 |
} |
| 3281 |
if (key->isKind(ParseNodeKind::ObjectPropertyName) || |
| 3282 |
key->isKind(ParseNodeKind::PrivateName)) { |
| 3283 |
return identifier(&key->as<NameNode>(), dst); |
| 3284 |
} |
| 3285 |
|
| 3286 |
LOCAL_ASSERT(key->isKind(ParseNodeKind::StringExpr) || |
| 3287 |
key->isKind(ParseNodeKind::NumberExpr) || |
| 3288 |
key->isKind(ParseNodeKind::BigIntExpr)); |
| 3289 |
|
| 3290 |
return literal(key, dst); |
| 3291 |
} |
| 3292 |
|
| 3293 |
bool ASTSerializer::property(ParseNode* pn, MutableHandleValue dst) { |
| 3294 |
if (pn->isKind(ParseNodeKind::MutateProto)) { |
| 3295 |
RootedValue val(cx); |
| 3296 |
return expression(pn->as<UnaryNode>().kid(), &val) && |
| 3297 |
builder.prototypeMutation(val, &pn->pn_pos, dst); |
| 3298 |
} |
| 3299 |
if (pn->isKind(ParseNodeKind::Spread)) { |
| 3300 |
return expression(pn, dst); |
| 3301 |
} |
| 3302 |
|
| 3303 |
PropKind kind; |
| 3304 |
if (pn->is<PropertyDefinition>()) { |
| 3305 |
switch (pn->as<PropertyDefinition>().accessorType()) { |
| 3306 |
case AccessorType::None: |
| 3307 |
kind = PROP_INIT; |
| 3308 |
break; |
| 3309 |
|
| 3310 |
case AccessorType::Getter: |
| 3311 |
kind = PROP_GETTER; |
| 3312 |
break; |
| 3313 |
|
| 3314 |
case AccessorType::Setter: |
| 3315 |
kind = PROP_SETTER; |
| 3316 |
break; |
| 3317 |
|
| 3318 |
default: |
| 3319 |
LOCAL_NOT_REACHED("unexpected object-literal property"); |
| 3320 |
} |
| 3321 |
} else { |
| 3322 |
MOZ_ASSERT(pn->isKind(ParseNodeKind::Shorthand)); |
| 3323 |
kind = PROP_INIT; |
| 3324 |
} |
| 3325 |
|
| 3326 |
BinaryNode* node = &pn->as<BinaryNode>(); |
| 3327 |
ParseNode* keyNode = node->left(); |
| 3328 |
ParseNode* valNode = node->right(); |
| 3329 |
|
| 3330 |
bool isShorthand = node->isKind(ParseNodeKind::Shorthand); |
| 3331 |
bool isMethod = |
| 3332 |
valNode->is<FunctionNode>() && |
| 3333 |
valNode->as<FunctionNode>().funbox()->kind() == FunctionFlags::Method; |
| 3334 |
RootedValue key(cx), val(cx); |
| 3335 |
return propertyName(keyNode, &key) && expression(valNode, &val) && |
| 3336 |
builder.propertyInitializer(key, val, kind, isShorthand, isMethod, |
| 3337 |
&node->pn_pos, dst); |
| 3338 |
} |
| 3339 |
|
| 3340 |
bool ASTSerializer::literal(ParseNode* pn, MutableHandleValue dst) { |
| 3341 |
RootedValue val(cx); |
| 3342 |
switch (pn->getKind()) { |
| 3343 |
case ParseNodeKind::TemplateStringExpr: |
| 3344 |
case ParseNodeKind::StringExpr: { |
| 3345 |
JSAtom* exprAtom = |
| 3346 |
parser->liftParserAtomToJSAtom(pn->as<NameNode>().atom()); |
| 3347 |
if (!exprAtom) { |
| 3348 |
return false; |
| 3349 |
} |
| 3350 |
val.setString(exprAtom); |
| 3351 |
break; |
| 3352 |
} |
| 3353 |
|
| 3354 |
case ParseNodeKind::RegExpExpr: { |
| 3355 |
RegExpObject* re = pn->as<RegExpLiteral>().create( |
| 3356 |
cx, fc, parser->parserAtoms(), |
| 3357 |
parser->getCompilationState().input.atomCache, |
| 3358 |
parser->getCompilationState()); |
| 3359 |
if (!re) { |
| 3360 |
return false; |
| 3361 |
} |
| 3362 |
|
| 3363 |
val.setObject(*re); |
| 3364 |
break; |
| 3365 |
} |
| 3366 |
|
| 3367 |
case ParseNodeKind::NumberExpr: |
| 3368 |
val.setNumber(pn->as<NumericLiteral>().value()); |
| 3369 |
break; |
| 3370 |
|
| 3371 |
case ParseNodeKind::BigIntExpr: { |
| 3372 |
auto index = pn->as<BigIntLiteral>().index(); |
| 3373 |
BigInt* x = parser->compilationState_.bigIntData[index].createBigInt(cx); |
| 3374 |
if (!x) { |
| 3375 |
return false; |
| 3376 |
} |
| 3377 |
cx->check(x); |
| 3378 |
val.setBigInt(x); |
| 3379 |
break; |
| 3380 |
} |
| 3381 |
|
| 3382 |
case ParseNodeKind::NullExpr: |
| 3383 |
val.setNull(); |
| 3384 |
break; |
| 3385 |
|
| 3386 |
case ParseNodeKind::RawUndefinedExpr: |
| 3387 |
val.setUndefined(); |
| 3388 |
break; |
| 3389 |
|
| 3390 |
case ParseNodeKind::TrueExpr: |
| 3391 |
val.setBoolean(true); |
| 3392 |
break; |
| 3393 |
|
| 3394 |
case ParseNodeKind::FalseExpr: |
| 3395 |
val.setBoolean(false); |
| 3396 |
break; |
| 3397 |
|
| 3398 |
default: |
| 3399 |
LOCAL_NOT_REACHED("unexpected literal type"); |
| 3400 |
} |
| 3401 |
|
| 3402 |
return builder.literal(val, &pn->pn_pos, dst); |
| 3403 |
} |
| 3404 |
|
| 3405 |
bool ASTSerializer::arrayPattern(ListNode* array, MutableHandleValue dst) { |
| 3406 |
MOZ_ASSERT(array->isKind(ParseNodeKind::ArrayExpr)); |
| 3407 |
|
| 3408 |
NodeVector elts(cx); |
| 3409 |
if (!elts.reserve(array->count())) { |
| 3410 |
return false; |
| 3411 |
} |
| 3412 |
|
| 3413 |
for (ParseNode* item : array->contents()) { |
| 3414 |
if (item->isKind(ParseNodeKind::Elision)) { |
| 3415 |
elts.infallibleAppend(NullValue()); |
| 3416 |
} else if (item->isKind(ParseNodeKind::Spread)) { |
| 3417 |
RootedValue target(cx); |
| 3418 |
RootedValue spread(cx); |
| 3419 |
if (!pattern(item->as<UnaryNode>().kid(), &target)) { |
| 3420 |
return false; |
| 3421 |
} |
| 3422 |
if (!builder.spreadExpression(target, &item->pn_pos, &spread)) |
| 3423 |
return false; |
| 3424 |
elts.infallibleAppend(spread); |
| 3425 |
} else { |
| 3426 |
RootedValue patt(cx); |
| 3427 |
if (!pattern(item, &patt)) { |
| 3428 |
return false; |
| 3429 |
} |
| 3430 |
elts.infallibleAppend(patt); |
| 3431 |
} |
| 3432 |
} |
| 3433 |
|
| 3434 |
return builder.arrayPattern(elts, &array->pn_pos, dst); |
| 3435 |
} |
| 3436 |
|
| 3437 |
bool ASTSerializer::objectPattern(ListNode* obj, MutableHandleValue dst) { |
| 3438 |
MOZ_ASSERT(obj->isKind(ParseNodeKind::ObjectExpr)); |
| 3439 |
|
| 3440 |
NodeVector elts(cx); |
| 3441 |
if (!elts.reserve(obj->count())) { |
| 3442 |
return false; |
| 3443 |
} |
| 3444 |
|
| 3445 |
for (ParseNode* propdef : obj->contents()) { |
| 3446 |
if (propdef->isKind(ParseNodeKind::Spread)) { |
| 3447 |
RootedValue target(cx); |
| 3448 |
RootedValue spread(cx); |
| 3449 |
if (!pattern(propdef->as<UnaryNode>().kid(), &target)) { |
| 3450 |
return false; |
| 3451 |
} |
| 3452 |
if (!builder.spreadExpression(target, &propdef->pn_pos, &spread)) |
| 3453 |
return false; |
| 3454 |
elts.infallibleAppend(spread); |
| 3455 |
continue; |
| 3456 |
} |
| 3457 |
// Patterns can't have getters/setters. |
| 3458 |
LOCAL_ASSERT(!propdef->is<PropertyDefinition>() || |
| 3459 |
propdef->as<PropertyDefinition>().accessorType() == |
| 3460 |
AccessorType::None); |
| 3461 |
|
| 3462 |
RootedValue key(cx); |
| 3463 |
ParseNode* target; |
| 3464 |
if (propdef->isKind(ParseNodeKind::MutateProto)) { |
| 3465 |
RootedValue pname(cx, StringValue(cx->names().proto_)); |
| 3466 |
if (!builder.literal(pname, &propdef->pn_pos, &key)) { |
| 3467 |
return false; |
| 3468 |
} |
| 3469 |
target = propdef->as<UnaryNode>().kid(); |
| 3470 |
} else { |
| 3471 |
BinaryNode* prop = &propdef->as<BinaryNode>(); |
| 3472 |
if (!propertyName(prop->left(), &key)) { |
| 3473 |
return false; |
| 3474 |
} |
| 3475 |
target = prop->right(); |
| 3476 |
} |
| 3477 |
|
| 3478 |
RootedValue patt(cx), prop(cx); |
| 3479 |
if (!pattern(target, &patt) || |
| 3480 |
!builder.propertyPattern(key, patt, |
| 3481 |
propdef->isKind(ParseNodeKind::Shorthand), |
| 3482 |
&propdef->pn_pos, &prop)) { |
| 3483 |
return false; |
| 3484 |
} |
| 3485 |
|
| 3486 |
elts.infallibleAppend(prop); |
| 3487 |
} |
| 3488 |
|
| 3489 |
return builder.objectPattern(elts, &obj->pn_pos, dst); |
| 3490 |
} |
| 3491 |
|
| 3492 |
bool ASTSerializer::pattern(ParseNode* pn, MutableHandleValue dst) { |
| 3493 |
AutoCheckRecursionLimit recursion(cx); |
| 3494 |
if (!recursion.check(cx)) { |
| 3495 |
return false; |
| 3496 |
} |
| 3497 |
|
| 3498 |
switch (pn->getKind()) { |
| 3499 |
case ParseNodeKind::ObjectExpr: |
| 3500 |
return objectPattern(&pn->as<ListNode>(), dst); |
| 3501 |
|
| 3502 |
case ParseNodeKind::ArrayExpr: |
| 3503 |
return arrayPattern(&pn->as<ListNode>(), dst); |
| 3504 |
|
| 3505 |
default: |
| 3506 |
return expression(pn, dst); |
| 3507 |
} |
| 3508 |
} |
| 3509 |
|
| 3510 |
bool ASTSerializer::identifier(Handle<JSAtom*> atom, TokenPos* pos, |
| 3511 |
MutableHandleValue dst) { |
| 3512 |
RootedValue atomContentsVal(cx, unrootedAtomContents(atom)); |
| 3513 |
return builder.identifier(atomContentsVal, pos, dst); |
| 3514 |
} |
| 3515 |
|
| 3516 |
bool ASTSerializer::identifier(NameNode* id, MutableHandleValue dst) { |
| 3517 |
LOCAL_ASSERT(id->atom()); |
| 3518 |
|
| 3519 |
Rooted<JSAtom*> pnAtom(cx, parser->liftParserAtomToJSAtom(id->atom())); |
| 3520 |
if (!pnAtom.get()) { |
| 3521 |
return false; |
| 3522 |
} |
| 3523 |
return identifier(pnAtom, &id->pn_pos, dst); |
| 3524 |
} |
| 3525 |
|
| 3526 |
bool ASTSerializer::identifierOrLiteral(ParseNode* id, MutableHandleValue dst) { |
| 3527 |
if (id->getKind() == ParseNodeKind::Name) { |
| 3528 |
return identifier(&id->as<NameNode>(), dst); |
| 3529 |
} |
| 3530 |
return literal(id, dst); |
| 3531 |
} |
| 3532 |
|
| 3533 |
bool ASTSerializer::function(FunctionNode* funNode, ASTType type, |
| 3534 |
MutableHandleValue dst) { |
| 3535 |
FunctionBox* funbox = funNode->funbox(); |
| 3536 |
|
| 3537 |
GeneratorStyle generatorStyle = |
| 3538 |
funbox->isGenerator() ? GeneratorStyle::ES6 : GeneratorStyle::None; |
| 3539 |
|
| 3540 |
bool isAsync = funbox->isAsync(); |
| 3541 |
bool isExpression = funbox->hasExprBody(); |
| 3542 |
|
| 3543 |
RootedValue id(cx); |
| 3544 |
Rooted<JSAtom*> funcAtom(cx); |
| 3545 |
if (funbox->explicitName()) { |
| 3546 |
funcAtom.set(parser->liftParserAtomToJSAtom(funbox->explicitName())); |
| 3547 |
if (!funcAtom) { |
| 3548 |
return false; |
| 3549 |
} |
| 3550 |
} |
| 3551 |
if (!optIdentifier(funcAtom, nullptr, &id)) { |
| 3552 |
return false; |
| 3553 |
} |
| 3554 |
|
| 3555 |
NodeVector args(cx); |
| 3556 |
NodeVector defaults(cx); |
| 3557 |
|
| 3558 |
RootedValue body(cx), rest(cx); |
| 3559 |
if (funbox->hasRest()) { |
| 3560 |
rest.setUndefined(); |
| 3561 |
} else { |
| 3562 |
rest.setNull(); |
| 3563 |
} |
| 3564 |
return functionArgsAndBody(funNode->body(), args, defaults, isAsync, |
| 3565 |
isExpression, &body, &rest) && |
| 3566 |
builder.function(type, &funNode->pn_pos, id, args, defaults, body, |
| 3567 |
rest, generatorStyle, isAsync, isExpression, dst); |
| 3568 |
} |
| 3569 |
|
| 3570 |
bool ASTSerializer::functionArgsAndBody(ParamsBodyNode* pn, NodeVector& args, |
| 3571 |
NodeVector& defaults, bool isAsync, |
| 3572 |
bool isExpression, |
| 3573 |
MutableHandleValue body, |
| 3574 |
MutableHandleValue rest) { |
| 3575 |
// Serialize the arguments. |
| 3576 |
if (!functionArgs(pn, args, defaults, rest)) { |
| 3577 |
return false; |
| 3578 |
} |
| 3579 |
|
| 3580 |
// Skip the enclosing lexical scope. |
| 3581 |
ParseNode* bodyNode = pn->body()->scopeBody(); |
| 3582 |
|
| 3583 |
// Serialize the body. |
| 3584 |
switch (bodyNode->getKind()) { |
| 3585 |
// Arrow function with expression body. |
| 3586 |
case ParseNodeKind::ReturnStmt: |
| 3587 |
MOZ_ASSERT(isExpression); |
| 3588 |
return expression(bodyNode->as<UnaryNode>().kid(), body); |
| 3589 |
|
| 3590 |
// Function with statement body. |
| 3591 |
case ParseNodeKind::StatementList: { |
| 3592 |
ParseNode* firstNode = bodyNode->as<ListNode>().head(); |
| 3593 |
|
| 3594 |
// Skip over initial yield in generator. |
| 3595 |
if (firstNode && firstNode->isKind(ParseNodeKind::InitialYield)) { |
| 3596 |
firstNode = firstNode->pn_next; |
| 3597 |
} |
| 3598 |
|
| 3599 |
// Async arrow with expression body is converted into STATEMENTLIST |
| 3600 |
// to insert initial yield. |
| 3601 |
if (isAsync && isExpression) { |
| 3602 |
MOZ_ASSERT(firstNode->getKind() == ParseNodeKind::ReturnStmt); |
| 3603 |
return expression(firstNode->as<UnaryNode>().kid(), body); |
| 3604 |
} |
| 3605 |
|
| 3606 |
return functionBody(firstNode, &bodyNode->pn_pos, body); |
| 3607 |
} |
| 3608 |
|
| 3609 |
default: |
| 3610 |
LOCAL_NOT_REACHED("unexpected function contents"); |
| 3611 |
} |
| 3612 |
} |
| 3613 |
|
| 3614 |
bool ASTSerializer::functionArgs(ParamsBodyNode* pn, NodeVector& args, |
| 3615 |
NodeVector& defaults, |
| 3616 |
MutableHandleValue rest) { |
| 3617 |
RootedValue node(cx); |
| 3618 |
bool defaultsNull = true; |
| 3619 |
MOZ_ASSERT(defaults.empty(), |
| 3620 |
"must be initially empty for it to be proper to clear this " |
| 3621 |
"when there are no defaults"); |
| 3622 |
|
| 3623 |
MOZ_ASSERT(rest.isNullOrUndefined(), |
| 3624 |
"rest is set to |undefined| when a rest argument is present, " |
| 3625 |
"otherwise rest is set to |null|"); |
| 3626 |
|
| 3627 |
for (ParseNode* arg : pn->parameters()) { |
| 3628 |
ParseNode* pat; |
| 3629 |
ParseNode* defNode; |
| 3630 |
if (arg->isKind(ParseNodeKind::Name) || |
| 3631 |
arg->isKind(ParseNodeKind::ArrayExpr) || |
| 3632 |
arg->isKind(ParseNodeKind::ObjectExpr)) { |
| 3633 |
pat = arg; |
| 3634 |
defNode = nullptr; |
| 3635 |
} else { |
| 3636 |
MOZ_ASSERT(arg->isKind(ParseNodeKind::AssignExpr)); |
| 3637 |
AssignmentNode* assignNode = &arg->as<AssignmentNode>(); |
| 3638 |
pat = assignNode->left(); |
| 3639 |
defNode = assignNode->right(); |
| 3640 |
} |
| 3641 |
|
| 3642 |
// Process the name or pattern. |
| 3643 |
MOZ_ASSERT(pat->isKind(ParseNodeKind::Name) || |
| 3644 |
pat->isKind(ParseNodeKind::ArrayExpr) || |
| 3645 |
pat->isKind(ParseNodeKind::ObjectExpr)); |
| 3646 |
if (!pattern(pat, &node)) { |
| 3647 |
return false; |
| 3648 |
} |
| 3649 |
if (rest.isUndefined() && arg->pn_next == *std::end(pn->parameters())) { |
| 3650 |
rest.setObject(node.toObject()); |
| 3651 |
} else { |
| 3652 |
if (!args.append(node)) { |
| 3653 |
return false; |
| 3654 |
} |
| 3655 |
} |
| 3656 |
|
| 3657 |
// Process its default (or lack thereof). |
| 3658 |
if (defNode) { |
| 3659 |
defaultsNull = false; |
| 3660 |
RootedValue def(cx); |
| 3661 |
if (!expression(defNode, &def) || !defaults.append(def)) { |
| 3662 |
return false; |
| 3663 |
} |
| 3664 |
} else { |
| 3665 |
if (!defaults.append(NullValue())) { |
| 3666 |
return false; |
| 3667 |
} |
| 3668 |
} |
| 3669 |
} |
| 3670 |
MOZ_ASSERT(!rest.isUndefined(), |
| 3671 |
"if a rest argument was present (signified by " |
| 3672 |
"|rest.isUndefined()| initially), the rest node was properly " |
| 3673 |
"recorded"); |
| 3674 |
|
| 3675 |
if (defaultsNull) { |
| 3676 |
defaults.clear(); |
| 3677 |
} |
| 3678 |
|
| 3679 |
return true; |
| 3680 |
} |
| 3681 |
|
| 3682 |
bool ASTSerializer::functionBody(ParseNode* pn, TokenPos* pos, |
| 3683 |
MutableHandleValue dst) { |
| 3684 |
NodeVector elts(cx); |
| 3685 |
|
| 3686 |
// We aren't sure how many elements there are up front, so we'll check each |
| 3687 |
// append. |
| 3688 |
for (ParseNode* next = pn; next; next = next->pn_next) { |
| 3689 |
RootedValue child(cx); |
| 3690 |
if (!sourceElement(next, &child) || !elts.append(child)) { |
| 3691 |
return false; |
| 3692 |
} |
| 3693 |
} |
| 3694 |
|
| 3695 |
return builder.blockStatement(elts, pos, dst); |
| 3696 |
} |
| 3697 |
|
| 3698 |
static bool reflect_parse(JSContext* cx, uint32_t argc, Value* vp) { |
| 3699 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 3700 |
|
| 3701 |
if (!args.requireAtLeast(cx, "Reflect.parse", 1)) { |
| 3702 |
return false; |
| 3703 |
} |
| 3704 |
|
| 3705 |
RootedString src(cx, ToString<CanGC>(cx, args[0])); |
| 3706 |
if (!src) { |
| 3707 |
return false; |
| 3708 |
} |
| 3709 |
|
| 3710 |
UniqueChars filename; |
| 3711 |
uint32_t lineno = 1; |
| 3712 |
bool loc = true; |
| 3713 |
ParseGoal target = ParseGoal::Script; |
| 3714 |
|
| 3715 |
RootedValue arg(cx, args.get(1)); |
| 3716 |
|
| 3717 |
if (!arg.isNullOrUndefined()) { |
| 3718 |
if (!arg.isObject()) { |
| 3719 |
ReportValueError(cx, JSMSG_UNEXPECTED_TYPE, JSDVG_SEARCH_STACK, arg, |
| 3720 |
nullptr, "not an object"); |
| 3721 |
return false; |
| 3722 |
} |
| 3723 |
|
| 3724 |
RootedObject config(cx, &arg.toObject()); |
| 3725 |
|
| 3726 |
RootedValue prop(cx); |
| 3727 |
|
| 3728 |
/* config.loc */ |
| 3729 |
RootedId locId(cx, NameToId(cx->names().loc)); |
| 3730 |
RootedValue trueVal(cx, BooleanValue(true)); |
| 3731 |
if (!GetPropertyDefault(cx, config, locId, trueVal, &prop)) { |
| 3732 |
return false; |
| 3733 |
} |
| 3734 |
|
| 3735 |
loc = ToBoolean(prop); |
| 3736 |
|
| 3737 |
if (loc) { |
| 3738 |
/* config.source */ |
| 3739 |
RootedId sourceId(cx, NameToId(cx->names().source)); |
| 3740 |
RootedValue nullVal(cx, NullValue()); |
| 3741 |
if (!GetPropertyDefault(cx, config, sourceId, nullVal, &prop)) { |
| 3742 |
return false; |
| 3743 |
} |
| 3744 |
|
| 3745 |
if (!prop.isNullOrUndefined()) { |
| 3746 |
RootedString str(cx, ToString<CanGC>(cx, prop)); |
| 3747 |
if (!str) { |
| 3748 |
return false; |
| 3749 |
} |
| 3750 |
|
| 3751 |
filename = StringToNewUTF8CharsZ(cx, *str); |
| 3752 |
if (!filename) { |
| 3753 |
return false; |
| 3754 |
} |
| 3755 |
} |
| 3756 |
|
| 3757 |
/* config.line */ |
| 3758 |
RootedId lineId(cx, NameToId(cx->names().line)); |
| 3759 |
RootedValue oneValue(cx, Int32Value(1)); |
| 3760 |
if (!GetPropertyDefault(cx, config, lineId, oneValue, &prop) || |
| 3761 |
!ToUint32(cx, prop, &lineno)) { |
| 3762 |
return false; |
| 3763 |
} |
| 3764 |
} |
| 3765 |
|
| 3766 |
/* config.target */ |
| 3767 |
RootedId targetId(cx, NameToId(cx->names().target)); |
| 3768 |
RootedValue scriptVal(cx, StringValue(cx->names().script)); |
| 3769 |
if (!GetPropertyDefault(cx, config, targetId, scriptVal, &prop)) { |
| 3770 |
return false; |
| 3771 |
} |
| 3772 |
|
| 3773 |
if (!prop.isString()) { |
| 3774 |
ReportValueError(cx, JSMSG_UNEXPECTED_TYPE, JSDVG_SEARCH_STACK, prop, |
| 3775 |
nullptr, "not 'script' or 'module'"); |
| 3776 |
return false; |
| 3777 |
} |
| 3778 |
|
| 3779 |
RootedString stringProp(cx, prop.toString()); |
| 3780 |
bool isScript = false; |
| 3781 |
bool isModule = false; |
| 3782 |
if (!EqualStrings(cx, stringProp, cx->names().script, &isScript)) { |
| 3783 |
return false; |
| 3784 |
} |
| 3785 |
|
| 3786 |
if (!EqualStrings(cx, stringProp, cx->names().module, &isModule)) { |
| 3787 |
return false; |
| 3788 |
} |
| 3789 |
|
| 3790 |
if (isScript) { |
| 3791 |
target = ParseGoal::Script; |
| 3792 |
} else if (isModule) { |
| 3793 |
target = ParseGoal::Module; |
| 3794 |
} else { |
| 3795 |
JS_ReportErrorASCII(cx, |
| 3796 |
"Bad target value, expected 'script' or 'module'"); |
| 3797 |
return false; |
| 3798 |
} |
| 3799 |
} |
| 3800 |
|
| 3801 |
AutoReportFrontendContext fc(cx); |
| 3802 |
ASTSerializer serialize(cx, &fc, loc, filename.get(), lineno); |
| 3803 |
if (!serialize.init()) { |
| 3804 |
return false; |
| 3805 |
} |
| 3806 |
|
| 3807 |
JSLinearString* linear = src->ensureLinear(cx); |
| 3808 |
if (!linear) { |
| 3809 |
return false; |
| 3810 |
} |
| 3811 |
|
| 3812 |
AutoStableStringChars linearChars(cx); |
| 3813 |
if (!linearChars.initTwoByte(cx, linear)) { |
| 3814 |
return false; |
| 3815 |
} |
| 3816 |
|
| 3817 |
CompileOptions options(cx); |
| 3818 |
options.setFileAndLine(filename.get(), lineno); |
| 3819 |
options.setForceFullParse(); |
| 3820 |
options.allowHTMLComments = target == ParseGoal::Script; |
| 3821 |
mozilla::Range<const char16_t> chars = linearChars.twoByteRange(); |
| 3822 |
|
| 3823 |
Rooted<CompilationInput> input(cx, CompilationInput(options)); |
| 3824 |
if (target == ParseGoal::Script) { |
| 3825 |
if (!input.get().initForGlobal(&fc)) { |
| 3826 |
return false; |
| 3827 |
} |
| 3828 |
} else { |
| 3829 |
if (!input.get().initForModule(&fc)) { |
| 3830 |
return false; |
| 3831 |
} |
| 3832 |
} |
| 3833 |
|
| 3834 |
LifoAllocScope allocScope(&cx->tempLifoAlloc()); |
| 3835 |
frontend::NoScopeBindingCache scopeCache; |
| 3836 |
frontend::CompilationState compilationState(&fc, allocScope, input.get()); |
| 3837 |
if (!compilationState.init(&fc, &scopeCache)) { |
| 3838 |
return false; |
| 3839 |
} |
| 3840 |
|
| 3841 |
Parser<FullParseHandler, char16_t> parser(&fc, options, chars.begin().get(), |
| 3842 |
chars.length(), compilationState, |
| 3843 |
/* syntaxParser = */ nullptr); |
| 3844 |
if (!parser.checkOptions()) { |
| 3845 |
return false; |
| 3846 |
} |
| 3847 |
|
| 3848 |
serialize.setParser(&parser); |
| 3849 |
|
| 3850 |
ParseNode* pn; |
| 3851 |
if (target == ParseGoal::Script) { |
| 3852 |
pn = parser.parse().unwrapOr(nullptr); |
| 3853 |
if (!pn) { |
| 3854 |
return false; |
| 3855 |
} |
| 3856 |
} else { |
| 3857 |
ModuleBuilder builder(&fc, &parser); |
| 3858 |
|
| 3859 |
uint32_t len = chars.length(); |
| 3860 |
SourceExtent extent = SourceExtent::makeGlobalExtent( |
| 3861 |
len, options.lineno, |
| 3862 |
JS::LimitedColumnNumberOneOrigin::fromUnlimited( |
| 3863 |
JS::ColumnNumberOneOrigin(options.column))); |
| 3864 |
ModuleSharedContext modulesc(&fc, options, builder, extent); |
| 3865 |
pn = parser.moduleBody(&modulesc).unwrapOr(nullptr); |
| 3866 |
if (!pn) { |
| 3867 |
return false; |
| 3868 |
} |
| 3869 |
|
| 3870 |
pn = pn->as<ModuleNode>().body(); |
| 3871 |
} |
| 3872 |
|
| 3873 |
RootedValue val(cx); |
| 3874 |
if (!serialize.program(&pn->as<ListNode>(), &val)) { |
| 3875 |
args.rval().setNull(); |
| 3876 |
return false; |
| 3877 |
} |
| 3878 |
|
| 3879 |
args.rval().set(val); |
| 3880 |
return true; |
| 3881 |
} |
| 3882 |
|
| 3883 |
JS_PUBLIC_API bool JS_InitReflectParse(JSContext* cx, HandleObject global) { |
| 3884 |
RootedValue reflectVal(cx); |
| 3885 |
if (!GetProperty(cx, global, global, cx->names().Reflect, &reflectVal)) { |
| 3886 |
return false; |
| 3887 |
} |
| 3888 |
if (!reflectVal.isObject()) { |
| 3889 |
JS_ReportErrorASCII( |
| 3890 |
cx, "JS_InitReflectParse must be called during global initialization"); |
| 3891 |
return false; |
| 3892 |
} |
| 3893 |
|
| 3894 |
RootedObject reflectObj(cx, &reflectVal.toObject()); |
| 3895 |
return JS_DefineFunction(cx, reflectObj, "parse", reflect_parse, 1, 0); |
| 3896 |
} |