| 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 |
/* |
| 8 |
* JS parser. |
| 9 |
* |
| 10 |
* This is a recursive-descent parser for the JavaScript language specified by |
| 11 |
* "The ECMAScript Language Specification" (Standard ECMA-262). It uses |
| 12 |
* lexical and semantic feedback to disambiguate non-LL(1) structures. It |
| 13 |
* generates trees of nodes induced by the recursive parsing (not precise |
| 14 |
* syntax trees, see Parser.h). After tree construction, it rewrites trees to |
| 15 |
* fold constants and evaluate compile-time expressions. |
| 16 |
* |
| 17 |
* This parser attempts no error recovery. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "frontend/Parser.h" |
| 21 |
|
| 22 |
#include "mozilla/ArrayUtils.h" |
| 23 |
#include "mozilla/Assertions.h" |
| 24 |
#include "mozilla/Casting.h" |
| 25 |
#include "mozilla/Range.h" |
| 26 |
#include "mozilla/Sprintf.h" |
| 27 |
#include "mozilla/Try.h" // MOZ_TRY* |
| 28 |
#include "mozilla/Utf8.h" |
| 29 |
#include "mozilla/Variant.h" |
| 30 |
|
| 31 |
#include <memory> |
| 32 |
#include <new> |
| 33 |
#include <type_traits> |
| 34 |
|
| 35 |
#include "jsnum.h" |
| 36 |
#include "jstypes.h" |
| 37 |
|
| 38 |
#include "frontend/FoldConstants.h" |
| 39 |
#include "frontend/FunctionSyntaxKind.h" // FunctionSyntaxKind |
| 40 |
#include "frontend/ModuleSharedContext.h" |
| 41 |
#include "frontend/ParseNode.h" |
| 42 |
#include "frontend/ParseNodeVerify.h" |
| 43 |
#include "frontend/Parser-macros.h" // MOZ_TRY_VAR_OR_RETURN |
| 44 |
#include "frontend/ParserAtom.h" // TaggedParserAtomIndex, ParserAtomsTable, ParserAtom |
| 45 |
#include "frontend/ScriptIndex.h" // ScriptIndex |
| 46 |
#include "frontend/TokenStream.h" // IsKeyword, ReservedWordTokenKind, ReservedWordToCharZ, DeprecatedContent, *TokenStream*, CharBuffer, TokenKindToDesc |
| 47 |
#include "irregexp/RegExpAPI.h" |
| 48 |
#include "js/ColumnNumber.h" // JS::LimitedColumnNumberOneOrigin, JS::ColumnNumberOneOrigin |
| 49 |
#include "js/ErrorReport.h" // JSErrorBase |
| 50 |
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_* |
| 51 |
#include "js/HashTable.h" |
| 52 |
#include "js/RegExpFlags.h" // JS::RegExpFlags |
| 53 |
#include "js/Stack.h" // JS::NativeStackLimit |
| 54 |
#include "util/StringBuilder.h" // StringBuilder |
| 55 |
#include "vm/BytecodeUtil.h" |
| 56 |
#include "vm/FunctionFlags.h" // js::FunctionFlags |
| 57 |
#include "vm/GeneratorAndAsyncKind.h" // js::GeneratorKind, js::FunctionAsyncKind |
| 58 |
#include "vm/JSContext.h" |
| 59 |
#include "vm/JSScript.h" |
| 60 |
#include "vm/ModuleBuilder.h" // js::ModuleBuilder |
| 61 |
#include "vm/Scope.h" // GetScopeDataTrailingNames |
| 62 |
#include "wasm/AsmJS.h" |
| 63 |
|
| 64 |
#include "frontend/ParseContext-inl.h" |
| 65 |
#include "frontend/SharedContext-inl.h" |
| 66 |
|
| 67 |
using namespace js; |
| 68 |
|
| 69 |
using mozilla::AssertedCast; |
| 70 |
using mozilla::AsVariant; |
| 71 |
using mozilla::Maybe; |
| 72 |
using mozilla::Nothing; |
| 73 |
using mozilla::PointerRangeSize; |
| 74 |
using mozilla::Some; |
| 75 |
using mozilla::Utf8Unit; |
| 76 |
|
| 77 |
using JS::ReadOnlyCompileOptions; |
| 78 |
using JS::RegExpFlags; |
| 79 |
|
| 80 |
namespace js::frontend { |
| 81 |
|
| 82 |
using DeclaredNamePtr = ParseContext::Scope::DeclaredNamePtr; |
| 83 |
using AddDeclaredNamePtr = ParseContext::Scope::AddDeclaredNamePtr; |
| 84 |
using BindingIter = ParseContext::Scope::BindingIter; |
| 85 |
using UsedNamePtr = UsedNameTracker::UsedNameMap::Ptr; |
| 86 |
|
| 87 |
using ParserBindingNameVector = Vector<ParserBindingName, 6>; |
| 88 |
|
| 89 |
static inline void PropagateTransitiveParseFlags(const FunctionBox* inner, |
| 90 |
SharedContext* outer) { |
| 91 |
if (inner->bindingsAccessedDynamically()) { |
| 92 |
outer->setBindingsAccessedDynamically(); |
| 93 |
} |
| 94 |
if (inner->hasDirectEval()) { |
| 95 |
outer->setHasDirectEval(); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
static bool StatementKindIsBraced(StatementKind kind) { |
| 100 |
return kind == StatementKind::Block || kind == StatementKind::Switch || |
| 101 |
kind == StatementKind::Try || kind == StatementKind::Catch || |
| 102 |
kind == StatementKind::Finally; |
| 103 |
} |
| 104 |
|
| 105 |
template <class ParseHandler, typename Unit> |
| 106 |
inline typename GeneralParser<ParseHandler, Unit>::FinalParser* |
| 107 |
GeneralParser<ParseHandler, Unit>::asFinalParser() { |
| 108 |
static_assert( |
| 109 |
std::is_base_of_v<GeneralParser<ParseHandler, Unit>, FinalParser>, |
| 110 |
"inheritance relationship required by the static_cast<> below"); |
| 111 |
|
| 112 |
return static_cast<FinalParser*>(this); |
| 113 |
} |
| 114 |
|
| 115 |
template <class ParseHandler, typename Unit> |
| 116 |
inline const typename GeneralParser<ParseHandler, Unit>::FinalParser* |
| 117 |
GeneralParser<ParseHandler, Unit>::asFinalParser() const { |
| 118 |
static_assert( |
| 119 |
std::is_base_of_v<GeneralParser<ParseHandler, Unit>, FinalParser>, |
| 120 |
"inheritance relationship required by the static_cast<> below"); |
| 121 |
|
| 122 |
return static_cast<const FinalParser*>(this); |
| 123 |
} |
| 124 |
|
| 125 |
template <class ParseHandler, typename Unit> |
| 126 |
template <typename ConditionT, typename ErrorReportT> |
| 127 |
bool GeneralParser<ParseHandler, Unit>::mustMatchTokenInternal( |
| 128 |
ConditionT condition, ErrorReportT errorReport) { |
| 129 |
MOZ_ASSERT(condition(TokenKind::Div) == false); |
| 130 |
MOZ_ASSERT(condition(TokenKind::DivAssign) == false); |
| 131 |
MOZ_ASSERT(condition(TokenKind::RegExp) == false); |
| 132 |
|
| 133 |
TokenKind actual; |
| 134 |
if (!tokenStream.getToken(&actual, TokenStream::SlashIsInvalid)) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
if (!condition(actual)) { |
| 138 |
errorReport(actual); |
| 139 |
return false; |
| 140 |
} |
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
ParserSharedBase::ParserSharedBase(FrontendContext* fc, |
| 145 |
CompilationState& compilationState, |
| 146 |
Kind kind) |
| 147 |
: fc_(fc), |
| 148 |
alloc_(compilationState.parserAllocScope.alloc()), |
| 149 |
compilationState_(compilationState), |
| 150 |
pc_(nullptr), |
| 151 |
usedNames_(compilationState.usedNames) { |
| 152 |
fc_->nameCollectionPool().addActiveCompilation(); |
| 153 |
} |
| 154 |
|
| 155 |
ParserSharedBase::~ParserSharedBase() { |
| 156 |
fc_->nameCollectionPool().removeActiveCompilation(); |
| 157 |
} |
| 158 |
|
| 159 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 160 |
void ParserSharedBase::dumpAtom(TaggedParserAtomIndex index) const { |
| 161 |
parserAtoms().dump(index); |
| 162 |
} |
| 163 |
#endif |
| 164 |
|
| 165 |
ParserBase::ParserBase(FrontendContext* fc, |
| 166 |
const ReadOnlyCompileOptions& options, |
| 167 |
CompilationState& compilationState) |
| 168 |
: ParserSharedBase(fc, compilationState, ParserSharedBase::Kind::Parser), |
| 169 |
anyChars(fc, options, this), |
| 170 |
ss(nullptr), |
| 171 |
#ifdef DEBUG |
| 172 |
checkOptionsCalled_(false), |
| 173 |
#endif |
| 174 |
isUnexpectedEOF_(false), |
| 175 |
awaitHandling_(AwaitIsName), |
| 176 |
inParametersOfAsyncFunction_(false) { |
| 177 |
} |
| 178 |
|
| 179 |
bool ParserBase::checkOptions() { |
| 180 |
#ifdef DEBUG |
| 181 |
checkOptionsCalled_ = true; |
| 182 |
#endif |
| 183 |
|
| 184 |
return anyChars.checkOptions(); |
| 185 |
} |
| 186 |
|
| 187 |
ParserBase::~ParserBase() { MOZ_ASSERT(checkOptionsCalled_); } |
| 188 |
|
| 189 |
JSAtom* ParserBase::liftParserAtomToJSAtom(TaggedParserAtomIndex index) { |
| 190 |
JSContext* cx = fc_->maybeCurrentJSContext(); |
| 191 |
MOZ_ASSERT(cx); |
| 192 |
return parserAtoms().toJSAtom(cx, fc_, index, |
| 193 |
compilationState_.input.atomCache); |
| 194 |
} |
| 195 |
|
| 196 |
template <class ParseHandler> |
| 197 |
PerHandlerParser<ParseHandler>::PerHandlerParser( |
| 198 |
FrontendContext* fc, const ReadOnlyCompileOptions& options, |
| 199 |
CompilationState& compilationState, void* internalSyntaxParser) |
| 200 |
: ParserBase(fc, options, compilationState), |
| 201 |
handler_(fc, compilationState), |
| 202 |
internalSyntaxParser_(internalSyntaxParser) { |
| 203 |
MOZ_ASSERT(compilationState.isInitialStencil() == |
| 204 |
compilationState.input.isInitialStencil()); |
| 205 |
} |
| 206 |
|
| 207 |
template <class ParseHandler, typename Unit> |
| 208 |
GeneralParser<ParseHandler, Unit>::GeneralParser( |
| 209 |
FrontendContext* fc, const ReadOnlyCompileOptions& options, |
| 210 |
const Unit* units, size_t length, CompilationState& compilationState, |
| 211 |
SyntaxParser* syntaxParser) |
| 212 |
: Base(fc, options, compilationState, syntaxParser), |
| 213 |
tokenStream(fc, &compilationState.parserAtoms, options, units, length) {} |
| 214 |
|
| 215 |
template <typename Unit> |
| 216 |
void Parser<SyntaxParseHandler, Unit>::setAwaitHandling( |
| 217 |
AwaitHandling awaitHandling) { |
| 218 |
this->awaitHandling_ = awaitHandling; |
| 219 |
} |
| 220 |
|
| 221 |
template <typename Unit> |
| 222 |
void Parser<FullParseHandler, Unit>::setAwaitHandling( |
| 223 |
AwaitHandling awaitHandling) { |
| 224 |
this->awaitHandling_ = awaitHandling; |
| 225 |
if (SyntaxParser* syntaxParser = getSyntaxParser()) { |
| 226 |
syntaxParser->setAwaitHandling(awaitHandling); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
template <class ParseHandler, typename Unit> |
| 231 |
inline void GeneralParser<ParseHandler, Unit>::setAwaitHandling( |
| 232 |
AwaitHandling awaitHandling) { |
| 233 |
asFinalParser()->setAwaitHandling(awaitHandling); |
| 234 |
} |
| 235 |
|
| 236 |
template <typename Unit> |
| 237 |
void Parser<SyntaxParseHandler, Unit>::setInParametersOfAsyncFunction( |
| 238 |
bool inParameters) { |
| 239 |
this->inParametersOfAsyncFunction_ = inParameters; |
| 240 |
} |
| 241 |
|
| 242 |
template <typename Unit> |
| 243 |
void Parser<FullParseHandler, Unit>::setInParametersOfAsyncFunction( |
| 244 |
bool inParameters) { |
| 245 |
this->inParametersOfAsyncFunction_ = inParameters; |
| 246 |
if (SyntaxParser* syntaxParser = getSyntaxParser()) { |
| 247 |
syntaxParser->setInParametersOfAsyncFunction(inParameters); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
template <class ParseHandler, typename Unit> |
| 252 |
inline void GeneralParser<ParseHandler, Unit>::setInParametersOfAsyncFunction( |
| 253 |
bool inParameters) { |
| 254 |
asFinalParser()->setInParametersOfAsyncFunction(inParameters); |
| 255 |
} |
| 256 |
|
| 257 |
template <class ParseHandler> |
| 258 |
FunctionBox* PerHandlerParser<ParseHandler>::newFunctionBox( |
| 259 |
FunctionNodeType funNode, TaggedParserAtomIndex explicitName, |
| 260 |
FunctionFlags flags, uint32_t toStringStart, Directives inheritedDirectives, |
| 261 |
GeneratorKind generatorKind, FunctionAsyncKind asyncKind) { |
| 262 |
MOZ_ASSERT(funNode); |
| 263 |
|
| 264 |
ScriptIndex index = ScriptIndex(compilationState_.scriptData.length()); |
| 265 |
if (uint32_t(index) >= TaggedScriptThingIndex::IndexLimit) { |
| 266 |
ReportAllocationOverflow(fc_); |
| 267 |
return nullptr; |
| 268 |
} |
| 269 |
if (!compilationState_.appendScriptStencilAndData(fc_)) { |
| 270 |
return nullptr; |
| 271 |
} |
| 272 |
|
| 273 |
bool isInitialStencil = compilationState_.isInitialStencil(); |
| 274 |
|
| 275 |
// This source extent will be further filled in during the remainder of parse. |
| 276 |
SourceExtent extent; |
| 277 |
extent.toStringStart = toStringStart; |
| 278 |
|
| 279 |
FunctionBox* funbox = alloc_.new_<FunctionBox>( |
| 280 |
fc_, extent, compilationState_, inheritedDirectives, generatorKind, |
| 281 |
asyncKind, isInitialStencil, explicitName, flags, index); |
| 282 |
if (!funbox) { |
| 283 |
ReportOutOfMemory(fc_); |
| 284 |
return nullptr; |
| 285 |
} |
| 286 |
|
| 287 |
handler_.setFunctionBox(funNode, funbox); |
| 288 |
|
| 289 |
return funbox; |
| 290 |
} |
| 291 |
|
| 292 |
template <class ParseHandler> |
| 293 |
FunctionBox* PerHandlerParser<ParseHandler>::newFunctionBox( |
| 294 |
FunctionNodeType funNode, const ScriptStencil& cachedScriptData, |
| 295 |
const ScriptStencilExtra& cachedScriptExtra) { |
| 296 |
MOZ_ASSERT(funNode); |
| 297 |
|
| 298 |
ScriptIndex index = ScriptIndex(compilationState_.scriptData.length()); |
| 299 |
if (uint32_t(index) >= TaggedScriptThingIndex::IndexLimit) { |
| 300 |
ReportAllocationOverflow(fc_); |
| 301 |
return nullptr; |
| 302 |
} |
| 303 |
if (!compilationState_.appendScriptStencilAndData(fc_)) { |
| 304 |
return nullptr; |
| 305 |
} |
| 306 |
|
| 307 |
FunctionBox* funbox = alloc_.new_<FunctionBox>( |
| 308 |
fc_, cachedScriptExtra.extent, compilationState_, |
| 309 |
Directives(/* strict = */ false), cachedScriptExtra.generatorKind(), |
| 310 |
cachedScriptExtra.asyncKind(), compilationState_.isInitialStencil(), |
| 311 |
cachedScriptData.functionAtom, cachedScriptData.functionFlags, index); |
| 312 |
if (!funbox) { |
| 313 |
ReportOutOfMemory(fc_); |
| 314 |
return nullptr; |
| 315 |
} |
| 316 |
|
| 317 |
handler_.setFunctionBox(funNode, funbox); |
| 318 |
funbox->initFromScriptStencilExtra(cachedScriptExtra); |
| 319 |
|
| 320 |
return funbox; |
| 321 |
} |
| 322 |
|
| 323 |
bool ParserBase::setSourceMapInfo() { |
| 324 |
// If support for source pragmas have been fully disabled, we can skip |
| 325 |
// processing of all of these values. |
| 326 |
if (!options().sourcePragmas()) { |
| 327 |
return true; |
| 328 |
} |
| 329 |
|
| 330 |
// Not all clients initialize ss. Can't update info to an object that isn't |
| 331 |
// there. |
| 332 |
if (!ss) { |
| 333 |
return true; |
| 334 |
} |
| 335 |
|
| 336 |
if (anyChars.hasDisplayURL()) { |
| 337 |
if (!ss->setDisplayURL(fc_, anyChars.displayURL())) { |
| 338 |
return false; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
if (anyChars.hasSourceMapURL()) { |
| 343 |
MOZ_ASSERT(!ss->hasSourceMapURL()); |
| 344 |
if (!ss->setSourceMapURL(fc_, anyChars.sourceMapURL())) { |
| 345 |
return false; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
/* |
| 350 |
* Source map URLs passed as a compile option (usually via a HTTP source map |
| 351 |
* header) override any source map urls passed as comment pragmas. |
| 352 |
*/ |
| 353 |
if (options().sourceMapURL()) { |
| 354 |
// Warn about the replacement, but use the new one. |
| 355 |
if (ss->hasSourceMapURL()) { |
| 356 |
if (!warningNoOffset(JSMSG_ALREADY_HAS_PRAGMA, ss->filename(), |
| 357 |
"//# sourceMappingURL")) { |
| 358 |
return false; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
if (!ss->setSourceMapURL(fc_, options().sourceMapURL())) { |
| 363 |
return false; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
return true; |
| 368 |
} |
| 369 |
|
| 370 |
/* |
| 371 |
* Parse a top-level JS script. |
| 372 |
*/ |
| 373 |
template <class ParseHandler, typename Unit> |
| 374 |
typename ParseHandler::ListNodeResult |
| 375 |
GeneralParser<ParseHandler, Unit>::parse() { |
| 376 |
MOZ_ASSERT(checkOptionsCalled_); |
| 377 |
|
| 378 |
SourceExtent extent = SourceExtent::makeGlobalExtent( |
| 379 |
/* len = */ 0, options().lineno, |
| 380 |
JS::LimitedColumnNumberOneOrigin::fromUnlimited( |
| 381 |
JS::ColumnNumberOneOrigin(options().column))); |
| 382 |
Directives directives(options().forceStrictMode()); |
| 383 |
GlobalSharedContext globalsc(this->fc_, ScopeKind::Global, options(), |
| 384 |
directives, extent); |
| 385 |
SourceParseContext globalpc(this, &globalsc, /* newDirectives = */ nullptr); |
| 386 |
if (!globalpc.init()) { |
| 387 |
return errorResult(); |
| 388 |
} |
| 389 |
|
| 390 |
ParseContext::VarScope varScope(this); |
| 391 |
if (!varScope.init(pc_)) { |
| 392 |
return errorResult(); |
| 393 |
} |
| 394 |
|
| 395 |
ListNodeType stmtList; |
| 396 |
MOZ_TRY_VAR(stmtList, statementList(YieldIsName)); |
| 397 |
|
| 398 |
TokenKind tt; |
| 399 |
if (!tokenStream.getToken(&tt, TokenStream::SlashIsRegExp)) { |
| 400 |
return errorResult(); |
| 401 |
} |
| 402 |
if (tt != TokenKind::Eof) { |
| 403 |
error(JSMSG_GARBAGE_AFTER_INPUT, "script", TokenKindToDesc(tt)); |
| 404 |
return errorResult(); |
| 405 |
} |
| 406 |
|
| 407 |
if (!CheckParseTree(this->fc_, alloc_, stmtList)) { |
| 408 |
return errorResult(); |
| 409 |
} |
| 410 |
|
| 411 |
return stmtList; |
| 412 |
} |
| 413 |
|
| 414 |
/* |
| 415 |
* Strict mode forbids introducing new definitions for 'eval', 'arguments', |
| 416 |
* 'let', 'static', 'yield', or for any strict mode reserved word. |
| 417 |
*/ |
| 418 |
bool ParserBase::isValidStrictBinding(TaggedParserAtomIndex name) { |
| 419 |
TokenKind tt = ReservedWordTokenKind(name); |
| 420 |
if (tt == TokenKind::Limit) { |
| 421 |
return name != TaggedParserAtomIndex::WellKnown::eval() && |
| 422 |
name != TaggedParserAtomIndex::WellKnown::arguments(); |
| 423 |
} |
| 424 |
return tt != TokenKind::Let && tt != TokenKind::Static && |
| 425 |
tt != TokenKind::Yield && !TokenKindIsStrictReservedWord(tt); |
| 426 |
} |
| 427 |
|
| 428 |
/* |
| 429 |
* Returns true if all parameter names are valid strict mode binding names and |
| 430 |
* no duplicate parameter names are present. |
| 431 |
*/ |
| 432 |
bool ParserBase::hasValidSimpleStrictParameterNames() { |
| 433 |
MOZ_ASSERT(pc_->isFunctionBox() && |
| 434 |
pc_->functionBox()->hasSimpleParameterList()); |
| 435 |
|
| 436 |
if (pc_->functionBox()->hasDuplicateParameters) { |
| 437 |
return false; |
| 438 |
} |
| 439 |
|
| 440 |
for (auto name : pc_->positionalFormalParameterNames()) { |
| 441 |
MOZ_ASSERT(name); |
| 442 |
if (!isValidStrictBinding(name)) { |
| 443 |
return false; |
| 444 |
} |
| 445 |
} |
| 446 |
return true; |
| 447 |
} |
| 448 |
|
| 449 |
template <class ParseHandler, typename Unit> |
| 450 |
void GeneralParser<ParseHandler, Unit>::reportMissingClosing( |
| 451 |
unsigned errorNumber, unsigned noteNumber, uint32_t openedPos) { |
| 452 |
auto notes = MakeUnique<JSErrorNotes>(); |
| 453 |
if (!notes) { |
| 454 |
ReportOutOfMemory(this->fc_); |
| 455 |
return; |
| 456 |
} |
| 457 |
|
| 458 |
uint32_t line; |
| 459 |
JS::LimitedColumnNumberOneOrigin column; |
| 460 |
tokenStream.computeLineAndColumn(openedPos, &line, &column); |
| 461 |
|
| 462 |
const size_t MaxWidth = sizeof("4294967295"); |
| 463 |
char columnNumber[MaxWidth]; |
| 464 |
SprintfLiteral(columnNumber, "%" PRIu32, column.oneOriginValue()); |
| 465 |
char lineNumber[MaxWidth]; |
| 466 |
SprintfLiteral(lineNumber, "%" PRIu32, line); |
| 467 |
|
| 468 |
if (!notes->addNoteASCII(this->fc_, getFilename().c_str(), 0, line, |
| 469 |
JS::ColumnNumberOneOrigin(column), GetErrorMessage, |
| 470 |
nullptr, noteNumber, lineNumber, columnNumber)) { |
| 471 |
return; |
| 472 |
} |
| 473 |
|
| 474 |
errorWithNotes(std::move(notes), errorNumber); |
| 475 |
} |
| 476 |
|
| 477 |
template <class ParseHandler, typename Unit> |
| 478 |
void GeneralParser<ParseHandler, Unit>::reportRedeclarationHelper( |
| 479 |
TaggedParserAtomIndex& name, DeclarationKind& prevKind, TokenPos& pos, |
| 480 |
uint32_t& prevPos, const unsigned& errorNumber, |
| 481 |
const unsigned& noteErrorNumber) { |
| 482 |
UniqueChars bytes = this->parserAtoms().toPrintableString(name); |
| 483 |
if (!bytes) { |
| 484 |
ReportOutOfMemory(this->fc_); |
| 485 |
return; |
| 486 |
} |
| 487 |
|
| 488 |
if (prevPos == DeclaredNameInfo::npos) { |
| 489 |
errorAt(pos.begin, errorNumber, DeclarationKindString(prevKind), |
| 490 |
bytes.get()); |
| 491 |
return; |
| 492 |
} |
| 493 |
|
| 494 |
auto notes = MakeUnique<JSErrorNotes>(); |
| 495 |
if (!notes) { |
| 496 |
ReportOutOfMemory(this->fc_); |
| 497 |
return; |
| 498 |
} |
| 499 |
|
| 500 |
uint32_t line; |
| 501 |
JS::LimitedColumnNumberOneOrigin column; |
| 502 |
tokenStream.computeLineAndColumn(prevPos, &line, &column); |
| 503 |
|
| 504 |
const size_t MaxWidth = sizeof("4294967295"); |
| 505 |
char columnNumber[MaxWidth]; |
| 506 |
SprintfLiteral(columnNumber, "%" PRIu32, column.oneOriginValue()); |
| 507 |
char lineNumber[MaxWidth]; |
| 508 |
SprintfLiteral(lineNumber, "%" PRIu32, line); |
| 509 |
|
| 510 |
if (!notes->addNoteASCII(this->fc_, getFilename().c_str(), 0, line, |
| 511 |
JS::ColumnNumberOneOrigin(column), GetErrorMessage, |
| 512 |
nullptr, noteErrorNumber, lineNumber, |
| 513 |
columnNumber)) { |
| 514 |
return; |
| 515 |
} |
| 516 |
|
| 517 |
errorWithNotesAt(std::move(notes), pos.begin, errorNumber, |
| 518 |
DeclarationKindString(prevKind), bytes.get()); |
| 519 |
} |
| 520 |
|
| 521 |
template <class ParseHandler, typename Unit> |
| 522 |
void GeneralParser<ParseHandler, Unit>::reportRedeclaration( |
| 523 |
TaggedParserAtomIndex name, DeclarationKind prevKind, TokenPos pos, |
| 524 |
uint32_t prevPos) { |
| 525 |
reportRedeclarationHelper(name, prevKind, pos, prevPos, JSMSG_REDECLARED_VAR, |
| 526 |
JSMSG_PREV_DECLARATION); |
| 527 |
} |
| 528 |
|
| 529 |
template <class ParseHandler, typename Unit> |
| 530 |
void GeneralParser<ParseHandler, Unit>::reportMismatchedPlacement( |
| 531 |
TaggedParserAtomIndex name, DeclarationKind prevKind, TokenPos pos, |
| 532 |
uint32_t prevPos) { |
| 533 |
reportRedeclarationHelper(name, prevKind, pos, prevPos, |
| 534 |
JSMSG_MISMATCHED_PLACEMENT, JSMSG_PREV_DECLARATION); |
| 535 |
} |
| 536 |
|
| 537 |
// notePositionalFormalParameter is called for both the arguments of a regular |
| 538 |
// function definition and the arguments specified by the Function |
| 539 |
// constructor. |
| 540 |
// |
| 541 |
// The 'disallowDuplicateParams' bool indicates whether the use of another |
| 542 |
// feature (destructuring or default arguments) disables duplicate arguments. |
| 543 |
// (ECMA-262 requires us to support duplicate parameter names, but, for newer |
| 544 |
// features, we consider the code to have "opted in" to higher standards and |
| 545 |
// forbid duplicates.) |
| 546 |
template <class ParseHandler, typename Unit> |
| 547 |
bool GeneralParser<ParseHandler, Unit>::notePositionalFormalParameter( |
| 548 |
FunctionNodeType funNode, TaggedParserAtomIndex name, uint32_t beginPos, |
| 549 |
bool disallowDuplicateParams, bool* duplicatedParam) { |
| 550 |
if (AddDeclaredNamePtr p = |
| 551 |
pc_->functionScope().lookupDeclaredNameForAdd(name)) { |
| 552 |
if (disallowDuplicateParams) { |
| 553 |
error(JSMSG_BAD_DUP_ARGS); |
| 554 |
return false; |
| 555 |
} |
| 556 |
|
| 557 |
// Strict-mode disallows duplicate args. We may not know whether we are |
| 558 |
// in strict mode or not (since the function body hasn't been parsed). |
| 559 |
// In such cases, report will queue up the potential error and return |
| 560 |
// 'true'. |
| 561 |
if (pc_->sc()->strict()) { |
| 562 |
UniqueChars bytes = this->parserAtoms().toPrintableString(name); |
| 563 |
if (!bytes) { |
| 564 |
ReportOutOfMemory(this->fc_); |
| 565 |
return false; |
| 566 |
} |
| 567 |
if (!strictModeError(JSMSG_DUPLICATE_FORMAL, bytes.get())) { |
| 568 |
return false; |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
*duplicatedParam = true; |
| 573 |
} else { |
| 574 |
DeclarationKind kind = DeclarationKind::PositionalFormalParameter; |
| 575 |
if (!pc_->functionScope().addDeclaredName(pc_, p, name, kind, beginPos)) { |
| 576 |
return false; |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
if (!pc_->positionalFormalParameterNames().append( |
| 581 |
TrivialTaggedParserAtomIndex::from(name))) { |
| 582 |
ReportOutOfMemory(this->fc_); |
| 583 |
return false; |
| 584 |
} |
| 585 |
|
| 586 |
NameNodeType paramNode; |
| 587 |
MOZ_TRY_VAR_OR_RETURN(paramNode, newName(name), false); |
| 588 |
|
| 589 |
handler_.addFunctionFormalParameter(funNode, paramNode); |
| 590 |
return true; |
| 591 |
} |
| 592 |
|
| 593 |
template <class ParseHandler> |
| 594 |
bool PerHandlerParser<ParseHandler>::noteDestructuredPositionalFormalParameter( |
| 595 |
FunctionNodeType funNode, Node destruct) { |
| 596 |
// Append an empty name to the positional formals vector to keep track of |
| 597 |
// argument slots when making FunctionScope::ParserData. |
| 598 |
if (!pc_->positionalFormalParameterNames().append( |
| 599 |
TrivialTaggedParserAtomIndex::null())) { |
| 600 |
ReportOutOfMemory(fc_); |
| 601 |
return false; |
| 602 |
} |
| 603 |
|
| 604 |
handler_.addFunctionFormalParameter(funNode, destruct); |
| 605 |
return true; |
| 606 |
} |
| 607 |
|
| 608 |
template <class ParseHandler, typename Unit> |
| 609 |
bool GeneralParser<ParseHandler, Unit>::noteDeclaredName( |
| 610 |
TaggedParserAtomIndex name, DeclarationKind kind, TokenPos pos, |
| 611 |
ClosedOver isClosedOver) { |
| 612 |
// The asm.js validator does all its own symbol-table management so, as an |
| 613 |
// optimization, avoid doing any work here. |
| 614 |
if (pc_->useAsmOrInsideUseAsm()) { |
| 615 |
return true; |
| 616 |
} |
| 617 |
|
| 618 |
switch (kind) { |
| 619 |
case DeclarationKind::Var: |
| 620 |
case DeclarationKind::BodyLevelFunction: { |
| 621 |
Maybe<DeclarationKind> redeclaredKind; |
| 622 |
uint32_t prevPos; |
| 623 |
if (!pc_->tryDeclareVar(name, this, kind, pos.begin, &redeclaredKind, |
| 624 |
&prevPos)) { |
| 625 |
return false; |
| 626 |
} |
| 627 |
|
| 628 |
if (redeclaredKind) { |
| 629 |
reportRedeclaration(name, *redeclaredKind, pos, prevPos); |
| 630 |
return false; |
| 631 |
} |
| 632 |
|
| 633 |
break; |
| 634 |
} |
| 635 |
|
| 636 |
case DeclarationKind::ModuleBodyLevelFunction: { |
| 637 |
MOZ_ASSERT(pc_->atModuleLevel()); |
| 638 |
|
| 639 |
AddDeclaredNamePtr p = pc_->varScope().lookupDeclaredNameForAdd(name); |
| 640 |
if (p) { |
| 641 |
reportRedeclaration(name, p->value()->kind(), pos, p->value()->pos()); |
| 642 |
return false; |
| 643 |
} |
| 644 |
|
| 645 |
if (!pc_->varScope().addDeclaredName(pc_, p, name, kind, pos.begin, |
| 646 |
isClosedOver)) { |
| 647 |
return false; |
| 648 |
} |
| 649 |
|
| 650 |
// Body-level functions in modules are always closed over. |
| 651 |
pc_->varScope().lookupDeclaredName(name)->value()->setClosedOver(); |
| 652 |
|
| 653 |
break; |
| 654 |
} |
| 655 |
|
| 656 |
case DeclarationKind::FormalParameter: { |
| 657 |
// It is an early error if any non-positional formal parameter name |
| 658 |
// (e.g., destructuring formal parameter) is duplicated. |
| 659 |
|
| 660 |
AddDeclaredNamePtr p = |
| 661 |
pc_->functionScope().lookupDeclaredNameForAdd(name); |
| 662 |
if (p) { |
| 663 |
error(JSMSG_BAD_DUP_ARGS); |
| 664 |
return false; |
| 665 |
} |
| 666 |
|
| 667 |
if (!pc_->functionScope().addDeclaredName(pc_, p, name, kind, pos.begin, |
| 668 |
isClosedOver)) { |
| 669 |
return false; |
| 670 |
} |
| 671 |
|
| 672 |
break; |
| 673 |
} |
| 674 |
|
| 675 |
case DeclarationKind::LexicalFunction: |
| 676 |
case DeclarationKind::PrivateName: |
| 677 |
case DeclarationKind::Synthetic: |
| 678 |
case DeclarationKind::PrivateMethod: { |
| 679 |
ParseContext::Scope* scope = pc_->innermostScope(); |
| 680 |
AddDeclaredNamePtr p = scope->lookupDeclaredNameForAdd(name); |
| 681 |
if (p) { |
| 682 |
reportRedeclaration(name, p->value()->kind(), pos, p->value()->pos()); |
| 683 |
return false; |
| 684 |
} |
| 685 |
|
| 686 |
if (!scope->addDeclaredName(pc_, p, name, kind, pos.begin, |
| 687 |
isClosedOver)) { |
| 688 |
return false; |
| 689 |
} |
| 690 |
|
| 691 |
break; |
| 692 |
} |
| 693 |
|
| 694 |
case DeclarationKind::SloppyLexicalFunction: { |
| 695 |
// Functions in block have complex allowances in sloppy mode for being |
| 696 |
// labelled that other lexical declarations do not have. Those checks |
| 697 |
// are done in functionStmt. |
| 698 |
|
| 699 |
ParseContext::Scope* scope = pc_->innermostScope(); |
| 700 |
if (AddDeclaredNamePtr p = scope->lookupDeclaredNameForAdd(name)) { |
| 701 |
// It is usually an early error if there is another declaration |
| 702 |
// with the same name in the same scope. |
| 703 |
// |
| 704 |
// Sloppy lexical functions may redeclare other sloppy lexical |
| 705 |
// functions for web compatibility reasons. |
| 706 |
if (p->value()->kind() != DeclarationKind::SloppyLexicalFunction) { |
| 707 |
reportRedeclaration(name, p->value()->kind(), pos, p->value()->pos()); |
| 708 |
return false; |
| 709 |
} |
| 710 |
} else { |
| 711 |
if (!scope->addDeclaredName(pc_, p, name, kind, pos.begin, |
| 712 |
isClosedOver)) { |
| 713 |
return false; |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
break; |
| 718 |
} |
| 719 |
|
| 720 |
case DeclarationKind::Let: |
| 721 |
case DeclarationKind::Const: |
| 722 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 723 |
case DeclarationKind::Using: |
| 724 |
case DeclarationKind::AwaitUsing: |
| 725 |
#endif |
| 726 |
case DeclarationKind::Class: |
| 727 |
// The BoundNames of LexicalDeclaration and ForDeclaration must not |
| 728 |
// contain 'let'. (CatchParameter is the only lexical binding form |
| 729 |
// without this restriction.) |
| 730 |
if (name == TaggedParserAtomIndex::WellKnown::let()) { |
| 731 |
errorAt(pos.begin, JSMSG_LEXICAL_DECL_DEFINES_LET); |
| 732 |
return false; |
| 733 |
} |
| 734 |
|
| 735 |
// For body-level lexically declared names in a function, it is an |
| 736 |
// early error if there is a formal parameter of the same name. This |
| 737 |
// needs a special check if there is an extra var scope due to |
| 738 |
// parameter expressions. |
| 739 |
if (pc_->isFunctionExtraBodyVarScopeInnermost()) { |
| 740 |
DeclaredNamePtr p = pc_->functionScope().lookupDeclaredName(name); |
| 741 |
if (p && DeclarationKindIsParameter(p->value()->kind())) { |
| 742 |
reportRedeclaration(name, p->value()->kind(), pos, p->value()->pos()); |
| 743 |
return false; |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
[[fallthrough]]; |
| 748 |
|
| 749 |
case DeclarationKind::Import: |
| 750 |
// Module code is always strict, so 'let' is always a keyword and never a |
| 751 |
// name. |
| 752 |
MOZ_ASSERT(name != TaggedParserAtomIndex::WellKnown::let()); |
| 753 |
[[fallthrough]]; |
| 754 |
|
| 755 |
case DeclarationKind::SimpleCatchParameter: |
| 756 |
case DeclarationKind::CatchParameter: { |
| 757 |
ParseContext::Scope* scope = pc_->innermostScope(); |
| 758 |
|
| 759 |
// It is an early error if there is another declaration with the same |
| 760 |
// name in the same scope. |
| 761 |
AddDeclaredNamePtr p = scope->lookupDeclaredNameForAdd(name); |
| 762 |
if (p) { |
| 763 |
reportRedeclaration(name, p->value()->kind(), pos, p->value()->pos()); |
| 764 |
return false; |
| 765 |
} |
| 766 |
|
| 767 |
if (!scope->addDeclaredName(pc_, p, name, kind, pos.begin, |
| 768 |
isClosedOver)) { |
| 769 |
return false; |
| 770 |
} |
| 771 |
|
| 772 |
break; |
| 773 |
} |
| 774 |
|
| 775 |
case DeclarationKind::CoverArrowParameter: |
| 776 |
// CoverArrowParameter is only used as a placeholder declaration kind. |
| 777 |
break; |
| 778 |
|
| 779 |
case DeclarationKind::PositionalFormalParameter: |
| 780 |
MOZ_CRASH( |
| 781 |
"Positional formal parameter names should use " |
| 782 |
"notePositionalFormalParameter"); |
| 783 |
break; |
| 784 |
|
| 785 |
case DeclarationKind::VarForAnnexBLexicalFunction: |
| 786 |
MOZ_CRASH( |
| 787 |
"Synthesized Annex B vars should go through " |
| 788 |
"addPossibleAnnexBFunctionBox, and " |
| 789 |
"propagateAndMarkAnnexBFunctionBoxes"); |
| 790 |
break; |
| 791 |
} |
| 792 |
|
| 793 |
return true; |
| 794 |
} |
| 795 |
|
| 796 |
template <class ParseHandler, typename Unit> |
| 797 |
bool GeneralParser<ParseHandler, Unit>::noteDeclaredPrivateName( |
| 798 |
Node nameNode, TaggedParserAtomIndex name, PropertyType propType, |
| 799 |
FieldPlacement placement, TokenPos pos) { |
| 800 |
ParseContext::Scope* scope = pc_->innermostScope(); |
| 801 |
AddDeclaredNamePtr p = scope->lookupDeclaredNameForAdd(name); |
| 802 |
|
| 803 |
DeclarationKind declKind = DeclarationKind::PrivateName; |
| 804 |
|
| 805 |
// Our strategy for enabling debugger functionality is to mark names as closed |
| 806 |
// over, even if they don't necessarily need to be, to ensure that they are |
| 807 |
// included in the environment object. This allows us to easily look them up |
| 808 |
// by name when needed, even if there is no corresponding property on an |
| 809 |
// object, as is the case with getter, setters and private methods. |
| 810 |
ClosedOver closedOver = ClosedOver::Yes; |
| 811 |
PrivateNameKind kind; |
| 812 |
switch (propType) { |
| 813 |
case PropertyType::Field: |
| 814 |
kind = PrivateNameKind::Field; |
| 815 |
closedOver = ClosedOver::No; |
| 816 |
break; |
| 817 |
case PropertyType::FieldWithAccessor: |
| 818 |
// In this case, we create a new private field for the underlying storage, |
| 819 |
// and use the current name for the getter and setter. |
| 820 |
kind = PrivateNameKind::GetterSetter; |
| 821 |
break; |
| 822 |
case PropertyType::Method: |
| 823 |
case PropertyType::GeneratorMethod: |
| 824 |
case PropertyType::AsyncMethod: |
| 825 |
case PropertyType::AsyncGeneratorMethod: |
| 826 |
if (placement == FieldPlacement::Instance) { |
| 827 |
// Optimized private method. Non-optimized paths still get |
| 828 |
// DeclarationKind::Synthetic. |
| 829 |
declKind = DeclarationKind::PrivateMethod; |
| 830 |
} |
| 831 |
kind = PrivateNameKind::Method; |
| 832 |
break; |
| 833 |
case PropertyType::Getter: |
| 834 |
kind = PrivateNameKind::Getter; |
| 835 |
break; |
| 836 |
case PropertyType::Setter: |
| 837 |
kind = PrivateNameKind::Setter; |
| 838 |
break; |
| 839 |
default: |
| 840 |
MOZ_CRASH("Invalid Property Type for noteDeclarePrivateName"); |
| 841 |
} |
| 842 |
|
| 843 |
if (p) { |
| 844 |
PrivateNameKind prevKind = p->value()->privateNameKind(); |
| 845 |
if ((prevKind == PrivateNameKind::Getter && |
| 846 |
kind == PrivateNameKind::Setter) || |
| 847 |
(prevKind == PrivateNameKind::Setter && |
| 848 |
kind == PrivateNameKind::Getter)) { |
| 849 |
// Private methods demands that |
| 850 |
// |
| 851 |
// class A { |
| 852 |
// static set #x(_) {} |
| 853 |
// get #x() { } |
| 854 |
// } |
| 855 |
// |
| 856 |
// Report a SyntaxError. |
| 857 |
if (placement == p->value()->placement()) { |
| 858 |
p->value()->setPrivateNameKind(PrivateNameKind::GetterSetter); |
| 859 |
handler_.setPrivateNameKind(nameNode, PrivateNameKind::GetterSetter); |
| 860 |
return true; |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
reportMismatchedPlacement(name, p->value()->kind(), pos, p->value()->pos()); |
| 865 |
return false; |
| 866 |
} |
| 867 |
|
| 868 |
if (!scope->addDeclaredName(pc_, p, name, declKind, pos.begin, closedOver)) { |
| 869 |
return false; |
| 870 |
} |
| 871 |
|
| 872 |
DeclaredNamePtr declared = scope->lookupDeclaredName(name); |
| 873 |
declared->value()->setPrivateNameKind(kind); |
| 874 |
declared->value()->setFieldPlacement(placement); |
| 875 |
handler_.setPrivateNameKind(nameNode, kind); |
| 876 |
|
| 877 |
return true; |
| 878 |
} |
| 879 |
|
| 880 |
bool ParserBase::noteUsedNameInternal(TaggedParserAtomIndex name, |
| 881 |
NameVisibility visibility, |
| 882 |
mozilla::Maybe<TokenPos> tokenPosition) { |
| 883 |
// The asm.js validator does all its own symbol-table management so, as an |
| 884 |
// optimization, avoid doing any work here. |
| 885 |
if (pc_->useAsmOrInsideUseAsm()) { |
| 886 |
return true; |
| 887 |
} |
| 888 |
|
| 889 |
// Global bindings are properties and not actual bindings; we don't need |
| 890 |
// to know if they are closed over. So no need to track used name at the |
| 891 |
// global scope. It is not incorrect to track them, this is an |
| 892 |
// optimization. |
| 893 |
// |
| 894 |
// Exceptions: |
| 895 |
// (a) Track private name references, as the used names tracker is used to |
| 896 |
// provide early errors for undeclared private name references |
| 897 |
// (b) If the script has extra bindings, track all references to detect |
| 898 |
// references to extra bindings |
| 899 |
ParseContext::Scope* scope = pc_->innermostScope(); |
| 900 |
if (pc_->sc()->isGlobalContext() && scope == &pc_->varScope() && |
| 901 |
visibility == NameVisibility::Public && |
| 902 |
!this->compilationState_.input.hasExtraBindings()) { |
| 903 |
return true; |
| 904 |
} |
| 905 |
|
| 906 |
return usedNames_.noteUse(fc_, name, visibility, pc_->scriptId(), scope->id(), |
| 907 |
tokenPosition); |
| 908 |
} |
| 909 |
|
| 910 |
template <class ParseHandler> |
| 911 |
bool PerHandlerParser<ParseHandler>:: |
| 912 |
propagateFreeNamesAndMarkClosedOverBindings(ParseContext::Scope& scope) { |
| 913 |
// Now that we have all the declared names in the scope, check which |
| 914 |
// functions should exhibit Annex B semantics. |
| 915 |
if (!scope.propagateAndMarkAnnexBFunctionBoxes(pc_, this)) { |
| 916 |
return false; |
| 917 |
} |
| 918 |
|
| 919 |
if (handler_.reuseClosedOverBindings()) { |
| 920 |
MOZ_ASSERT(pc_->isOutermostOfCurrentCompile()); |
| 921 |
|
| 922 |
// Closed over bindings for all scopes are stored in a contiguous array, in |
| 923 |
// the same order as the order in which scopes are visited, and seprated by |
| 924 |
// TaggedParserAtomIndex::null(). |
| 925 |
uint32_t slotCount = scope.declaredCount(); |
| 926 |
while (auto parserAtom = handler_.nextLazyClosedOverBinding()) { |
| 927 |
scope.lookupDeclaredName(parserAtom)->value()->setClosedOver(); |
| 928 |
MOZ_ASSERT(slotCount > 0); |
| 929 |
slotCount--; |
| 930 |
} |
| 931 |
|
| 932 |
if (pc_->isGeneratorOrAsync()) { |
| 933 |
scope.setOwnStackSlotCount(slotCount); |
| 934 |
} |
| 935 |
return true; |
| 936 |
} |
| 937 |
|
| 938 |
constexpr bool isSyntaxParser = |
| 939 |
std::is_same_v<ParseHandler, SyntaxParseHandler>; |
| 940 |
uint32_t scriptId = pc_->scriptId(); |
| 941 |
uint32_t scopeId = scope.id(); |
| 942 |
|
| 943 |
uint32_t slotCount = 0; |
| 944 |
for (BindingIter bi = scope.bindings(pc_); bi; bi++) { |
| 945 |
bool closedOver = false; |
| 946 |
if (UsedNamePtr p = usedNames_.lookup(bi.name())) { |
| 947 |
p->value().noteBoundInScope(scriptId, scopeId, &closedOver); |
| 948 |
if (closedOver) { |
| 949 |
bi.setClosedOver(); |
| 950 |
|
| 951 |
if constexpr (isSyntaxParser) { |
| 952 |
if (!pc_->closedOverBindingsForLazy().append( |
| 953 |
TrivialTaggedParserAtomIndex::from(bi.name()))) { |
| 954 |
ReportOutOfMemory(fc_); |
| 955 |
return false; |
| 956 |
} |
| 957 |
} |
| 958 |
} |
| 959 |
} |
| 960 |
|
| 961 |
if constexpr (!isSyntaxParser) { |
| 962 |
if (!closedOver) { |
| 963 |
slotCount++; |
| 964 |
} |
| 965 |
} |
| 966 |
} |
| 967 |
if constexpr (!isSyntaxParser) { |
| 968 |
if (pc_->isGeneratorOrAsync()) { |
| 969 |
scope.setOwnStackSlotCount(slotCount); |
| 970 |
} |
| 971 |
} |
| 972 |
|
| 973 |
// Append a nullptr to denote end-of-scope. |
| 974 |
if constexpr (isSyntaxParser) { |
| 975 |
if (!pc_->closedOverBindingsForLazy().append( |
| 976 |
TrivialTaggedParserAtomIndex::null())) { |
| 977 |
ReportOutOfMemory(fc_); |
| 978 |
return false; |
| 979 |
} |
| 980 |
} |
| 981 |
|
| 982 |
return true; |
| 983 |
} |
| 984 |
|
| 985 |
template <typename Unit> |
| 986 |
bool Parser<FullParseHandler, Unit>::checkStatementsEOF() { |
| 987 |
// This is designed to be paired with parsing a statement list at the top |
| 988 |
// level. |
| 989 |
// |
| 990 |
// The statementList() call breaks on TokenKind::RightCurly, so make sure |
| 991 |
// we've reached EOF here. |
| 992 |
TokenKind tt; |
| 993 |
if (!tokenStream.peekToken(&tt, TokenStream::SlashIsRegExp)) { |
| 994 |
return false; |
| 995 |
} |
| 996 |
if (tt != TokenKind::Eof) { |
| 997 |
error(JSMSG_UNEXPECTED_TOKEN, "expression", TokenKindToDesc(tt)); |
| 998 |
return false; |
| 999 |
} |
| 1000 |
return true; |
| 1001 |
} |
| 1002 |
|
| 1003 |
template <typename ScopeT> |
| 1004 |
typename ScopeT::ParserData* NewEmptyBindingData(FrontendContext* fc, |
| 1005 |
LifoAlloc& alloc, |
| 1006 |
uint32_t numBindings) { |
| 1007 |
using Data = typename ScopeT::ParserData; |
| 1008 |
size_t allocSize = SizeOfScopeData<Data>(numBindings); |
| 1009 |
auto* bindings = alloc.newWithSize<Data>(allocSize, numBindings); |
| 1010 |
if (!bindings) { |
| 1011 |
ReportOutOfMemory(fc); |
| 1012 |
} |
| 1013 |
return bindings; |
| 1014 |
} |
| 1015 |
|
| 1016 |
GlobalScope::ParserData* NewEmptyGlobalScopeData(FrontendContext* fc, |
| 1017 |
LifoAlloc& alloc, |
| 1018 |
uint32_t numBindings) { |
| 1019 |
return NewEmptyBindingData<GlobalScope>(fc, alloc, numBindings); |
| 1020 |
} |
| 1021 |
|
| 1022 |
LexicalScope::ParserData* NewEmptyLexicalScopeData(FrontendContext* fc, |
| 1023 |
LifoAlloc& alloc, |
| 1024 |
uint32_t numBindings) { |
| 1025 |
return NewEmptyBindingData<LexicalScope>(fc, alloc, numBindings); |
| 1026 |
} |
| 1027 |
|
| 1028 |
FunctionScope::ParserData* NewEmptyFunctionScopeData(FrontendContext* fc, |
| 1029 |
LifoAlloc& alloc, |
| 1030 |
uint32_t numBindings) { |
| 1031 |
return NewEmptyBindingData<FunctionScope>(fc, alloc, numBindings); |
| 1032 |
} |
| 1033 |
|
| 1034 |
namespace detail { |
| 1035 |
|
| 1036 |
template <class SlotInfo> |
| 1037 |
static MOZ_ALWAYS_INLINE ParserBindingName* InitializeIndexedBindings( |
| 1038 |
SlotInfo& slotInfo, ParserBindingName* start, ParserBindingName* cursor) { |
| 1039 |
return cursor; |
| 1040 |
} |
| 1041 |
|
| 1042 |
template <class SlotInfo, typename UnsignedInteger, typename... Step> |
| 1043 |
static MOZ_ALWAYS_INLINE ParserBindingName* InitializeIndexedBindings( |
| 1044 |
SlotInfo& slotInfo, ParserBindingName* start, ParserBindingName* cursor, |
| 1045 |
UnsignedInteger SlotInfo::* field, const ParserBindingNameVector& bindings, |
| 1046 |
Step&&... step) { |
| 1047 |
slotInfo.*field = |
| 1048 |
AssertedCast<UnsignedInteger>(PointerRangeSize(start, cursor)); |
| 1049 |
|
| 1050 |
ParserBindingName* newCursor = |
| 1051 |
std::uninitialized_copy(bindings.begin(), bindings.end(), cursor); |
| 1052 |
|
| 1053 |
return InitializeIndexedBindings(slotInfo, start, newCursor, |
| 1054 |
std::forward<Step>(step)...); |
| 1055 |
} |
| 1056 |
|
| 1057 |
} // namespace detail |
| 1058 |
|
| 1059 |
// Initialize the trailing name bindings of |data|, then set |data->length| to |
| 1060 |
// the count of bindings added (which must equal |count|). |
| 1061 |
// |
| 1062 |
// First, |firstBindings| are added to the trailing names. Then any |
| 1063 |
// "steps" present are performed first to last. Each step is 1) a pointer to a |
| 1064 |
// member of |data| to be set to the current number of bindings added, and 2) a |
| 1065 |
// vector of |ParserBindingName|s to then copy into |data->trailingNames|. |
| 1066 |
// (Thus each |data| member field indicates where the corresponding vector's |
| 1067 |
// names start.) |
| 1068 |
template <class Data, typename... Step> |
| 1069 |
static MOZ_ALWAYS_INLINE void InitializeBindingData( |
| 1070 |
Data* data, uint32_t count, const ParserBindingNameVector& firstBindings, |
| 1071 |
Step&&... step) { |
| 1072 |
MOZ_ASSERT(data->length == 0, "data shouldn't be filled yet"); |
| 1073 |
|
| 1074 |
ParserBindingName* start = GetScopeDataTrailingNamesPointer(data); |
| 1075 |
ParserBindingName* cursor = std::uninitialized_copy( |
| 1076 |
firstBindings.begin(), firstBindings.end(), start); |
| 1077 |
|
| 1078 |
#ifdef DEBUG |
| 1079 |
ParserBindingName* end = |
| 1080 |
#endif |
| 1081 |
detail::InitializeIndexedBindings(data->slotInfo, start, cursor, |
| 1082 |
std::forward<Step>(step)...); |
| 1083 |
|
| 1084 |
MOZ_ASSERT(PointerRangeSize(start, end) == count); |
| 1085 |
data->length = count; |
| 1086 |
} |
| 1087 |
|
| 1088 |
static Maybe<GlobalScope::ParserData*> NewGlobalScopeData( |
| 1089 |
FrontendContext* fc, ParseContext::Scope& scope, LifoAlloc& alloc, |
| 1090 |
ParseContext* pc) { |
| 1091 |
ParserBindingNameVector vars(fc); |
| 1092 |
ParserBindingNameVector lets(fc); |
| 1093 |
ParserBindingNameVector consts(fc); |
| 1094 |
|
| 1095 |
bool allBindingsClosedOver = pc->sc()->allBindingsClosedOver(); |
| 1096 |
for (BindingIter bi = scope.bindings(pc); bi; bi++) { |
| 1097 |
bool closedOver = allBindingsClosedOver || bi.closedOver(); |
| 1098 |
|
| 1099 |
switch (bi.kind()) { |
| 1100 |
case BindingKind::Var: { |
| 1101 |
bool isTopLevelFunction = |
| 1102 |
bi.declarationKind() == DeclarationKind::BodyLevelFunction; |
| 1103 |
|
| 1104 |
ParserBindingName binding(bi.name(), closedOver, isTopLevelFunction); |
| 1105 |
if (!vars.append(binding)) { |
| 1106 |
return Nothing(); |
| 1107 |
} |
| 1108 |
break; |
| 1109 |
} |
| 1110 |
case BindingKind::Let: { |
| 1111 |
ParserBindingName binding(bi.name(), closedOver); |
| 1112 |
if (!lets.append(binding)) { |
| 1113 |
return Nothing(); |
| 1114 |
} |
| 1115 |
break; |
| 1116 |
} |
| 1117 |
case BindingKind::Const: { |
| 1118 |
ParserBindingName binding(bi.name(), closedOver); |
| 1119 |
if (!consts.append(binding)) { |
| 1120 |
return Nothing(); |
| 1121 |
} |
| 1122 |
break; |
| 1123 |
} |
| 1124 |
default: |
| 1125 |
MOZ_CRASH("Bad global scope BindingKind"); |
| 1126 |
} |
| 1127 |
} |
| 1128 |
|
| 1129 |
GlobalScope::ParserData* bindings = nullptr; |
| 1130 |
uint32_t numBindings = vars.length() + lets.length() + consts.length(); |
| 1131 |
|
| 1132 |
if (numBindings > 0) { |
| 1133 |
bindings = NewEmptyBindingData<GlobalScope>(fc, alloc, numBindings); |
| 1134 |
if (!bindings) { |
| 1135 |
return Nothing(); |
| 1136 |
} |
| 1137 |
|
| 1138 |
// The ordering here is important. See comments in GlobalScope. |
| 1139 |
InitializeBindingData(bindings, numBindings, vars, |
| 1140 |
&ParserGlobalScopeSlotInfo::letStart, lets, |
| 1141 |
&ParserGlobalScopeSlotInfo::constStart, consts); |
| 1142 |
} |
| 1143 |
|
| 1144 |
return Some(bindings); |
| 1145 |
} |
| 1146 |
|
| 1147 |
Maybe<GlobalScope::ParserData*> ParserBase::newGlobalScopeData( |
| 1148 |
ParseContext::Scope& scope) { |
| 1149 |
return NewGlobalScopeData(fc_, scope, stencilAlloc(), pc_); |
| 1150 |
} |
| 1151 |
|
| 1152 |
static Maybe<ModuleScope::ParserData*> NewModuleScopeData( |
| 1153 |
FrontendContext* fc, ParseContext::Scope& scope, LifoAlloc& alloc, |
| 1154 |
ParseContext* pc) { |
| 1155 |
ParserBindingNameVector imports(fc); |
| 1156 |
ParserBindingNameVector vars(fc); |
| 1157 |
ParserBindingNameVector lets(fc); |
| 1158 |
ParserBindingNameVector consts(fc); |
| 1159 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1160 |
ParserBindingNameVector usings(fc); |
| 1161 |
#endif |
| 1162 |
|
| 1163 |
bool allBindingsClosedOver = |
| 1164 |
pc->sc()->allBindingsClosedOver() || scope.tooBigToOptimize(); |
| 1165 |
|
| 1166 |
for (BindingIter bi = scope.bindings(pc); bi; bi++) { |
| 1167 |
// Imports are indirect bindings and must not be given known slots. |
| 1168 |
ParserBindingName binding(bi.name(), |
| 1169 |
(allBindingsClosedOver || bi.closedOver()) && |
| 1170 |
bi.kind() != BindingKind::Import); |
| 1171 |
switch (bi.kind()) { |
| 1172 |
case BindingKind::Import: |
| 1173 |
if (!imports.append(binding)) { |
| 1174 |
return Nothing(); |
| 1175 |
} |
| 1176 |
break; |
| 1177 |
case BindingKind::Var: |
| 1178 |
if (!vars.append(binding)) { |
| 1179 |
return Nothing(); |
| 1180 |
} |
| 1181 |
break; |
| 1182 |
case BindingKind::Let: |
| 1183 |
if (!lets.append(binding)) { |
| 1184 |
return Nothing(); |
| 1185 |
} |
| 1186 |
break; |
| 1187 |
case BindingKind::Const: |
| 1188 |
if (!consts.append(binding)) { |
| 1189 |
return Nothing(); |
| 1190 |
} |
| 1191 |
break; |
| 1192 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1193 |
case BindingKind::Using: |
| 1194 |
if (!usings.append(binding)) { |
| 1195 |
return Nothing(); |
| 1196 |
} |
| 1197 |
break; |
| 1198 |
#endif |
| 1199 |
default: |
| 1200 |
MOZ_CRASH("Bad module scope BindingKind"); |
| 1201 |
} |
| 1202 |
} |
| 1203 |
|
| 1204 |
ModuleScope::ParserData* bindings = nullptr; |
| 1205 |
uint32_t numBindings = imports.length() + vars.length() + lets.length() + |
| 1206 |
consts.length() |
| 1207 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1208 |
+ usings.length() |
| 1209 |
#endif |
| 1210 |
; |
| 1211 |
|
| 1212 |
if (numBindings > 0) { |
| 1213 |
bindings = NewEmptyBindingData<ModuleScope>(fc, alloc, numBindings); |
| 1214 |
if (!bindings) { |
| 1215 |
return Nothing(); |
| 1216 |
} |
| 1217 |
|
| 1218 |
// The ordering here is important. See comments in ModuleScope. |
| 1219 |
InitializeBindingData(bindings, numBindings, imports, |
| 1220 |
&ParserModuleScopeSlotInfo::varStart, vars, |
| 1221 |
&ParserModuleScopeSlotInfo::letStart, lets, |
| 1222 |
&ParserModuleScopeSlotInfo::constStart, consts |
| 1223 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1224 |
, |
| 1225 |
&ParserModuleScopeSlotInfo::usingStart, usings |
| 1226 |
#endif |
| 1227 |
); |
| 1228 |
} |
| 1229 |
|
| 1230 |
return Some(bindings); |
| 1231 |
} |
| 1232 |
|
| 1233 |
Maybe<ModuleScope::ParserData*> ParserBase::newModuleScopeData( |
| 1234 |
ParseContext::Scope& scope) { |
| 1235 |
return NewModuleScopeData(fc_, scope, stencilAlloc(), pc_); |
| 1236 |
} |
| 1237 |
|
| 1238 |
static Maybe<EvalScope::ParserData*> NewEvalScopeData( |
| 1239 |
FrontendContext* fc, ParseContext::Scope& scope, LifoAlloc& alloc, |
| 1240 |
ParseContext* pc) { |
| 1241 |
ParserBindingNameVector vars(fc); |
| 1242 |
|
| 1243 |
// Treat all bindings as closed over in non-strict eval. |
| 1244 |
bool allBindingsClosedOver = |
| 1245 |
!pc->sc()->strict() || pc->sc()->allBindingsClosedOver(); |
| 1246 |
for (BindingIter bi = scope.bindings(pc); bi; bi++) { |
| 1247 |
// Eval scopes only contain 'var' bindings. |
| 1248 |
MOZ_ASSERT(bi.kind() == BindingKind::Var); |
| 1249 |
bool isTopLevelFunction = |
| 1250 |
bi.declarationKind() == DeclarationKind::BodyLevelFunction; |
| 1251 |
bool closedOver = allBindingsClosedOver || bi.closedOver(); |
| 1252 |
|
| 1253 |
ParserBindingName binding(bi.name(), closedOver, isTopLevelFunction); |
| 1254 |
if (!vars.append(binding)) { |
| 1255 |
return Nothing(); |
| 1256 |
} |
| 1257 |
} |
| 1258 |
|
| 1259 |
EvalScope::ParserData* bindings = nullptr; |
| 1260 |
uint32_t numBindings = vars.length(); |
| 1261 |
|
| 1262 |
if (numBindings > 0) { |
| 1263 |
bindings = NewEmptyBindingData<EvalScope>(fc, alloc, numBindings); |
| 1264 |
if (!bindings) { |
| 1265 |
return Nothing(); |
| 1266 |
} |
| 1267 |
|
| 1268 |
InitializeBindingData(bindings, numBindings, vars); |
| 1269 |
} |
| 1270 |
|
| 1271 |
return Some(bindings); |
| 1272 |
} |
| 1273 |
|
| 1274 |
Maybe<EvalScope::ParserData*> ParserBase::newEvalScopeData( |
| 1275 |
ParseContext::Scope& scope) { |
| 1276 |
return NewEvalScopeData(fc_, scope, stencilAlloc(), pc_); |
| 1277 |
} |
| 1278 |
|
| 1279 |
static Maybe<FunctionScope::ParserData*> NewFunctionScopeData( |
| 1280 |
FrontendContext* fc, ParseContext::Scope& scope, bool hasParameterExprs, |
| 1281 |
LifoAlloc& alloc, ParseContext* pc) { |
| 1282 |
ParserBindingNameVector positionalFormals(fc); |
| 1283 |
ParserBindingNameVector formals(fc); |
| 1284 |
ParserBindingNameVector vars(fc); |
| 1285 |
|
| 1286 |
bool allBindingsClosedOver = |
| 1287 |
pc->sc()->allBindingsClosedOver() || scope.tooBigToOptimize(); |
| 1288 |
bool argumentBindingsClosedOver = |
| 1289 |
allBindingsClosedOver || pc->isGeneratorOrAsync(); |
| 1290 |
bool hasDuplicateParams = pc->functionBox()->hasDuplicateParameters; |
| 1291 |
|
| 1292 |
// Positional parameter names must be added in order of appearance as they are |
| 1293 |
// referenced using argument slots. |
| 1294 |
for (size_t i = 0; i < pc->positionalFormalParameterNames().length(); i++) { |
| 1295 |
TaggedParserAtomIndex name = pc->positionalFormalParameterNames()[i]; |
| 1296 |
|
| 1297 |
ParserBindingName bindName; |
| 1298 |
if (name) { |
| 1299 |
DeclaredNamePtr p = scope.lookupDeclaredName(name); |
| 1300 |
|
| 1301 |
// Do not consider any positional formal parameters closed over if |
| 1302 |
// there are parameter defaults. It is the binding in the defaults |
| 1303 |
// scope that is closed over instead. |
| 1304 |
bool closedOver = |
| 1305 |
argumentBindingsClosedOver || (p && p->value()->closedOver()); |
| 1306 |
|
| 1307 |
// If the parameter name has duplicates, only the final parameter |
| 1308 |
// name should be on the environment, as otherwise the environment |
| 1309 |
// object would have multiple, same-named properties. |
| 1310 |
if (hasDuplicateParams) { |
| 1311 |
for (size_t j = pc->positionalFormalParameterNames().length() - 1; |
| 1312 |
j > i; j--) { |
| 1313 |
if (TaggedParserAtomIndex(pc->positionalFormalParameterNames()[j]) == |
| 1314 |
name) { |
| 1315 |
closedOver = false; |
| 1316 |
break; |
| 1317 |
} |
| 1318 |
} |
| 1319 |
} |
| 1320 |
|
| 1321 |
bindName = ParserBindingName(name, closedOver); |
| 1322 |
} |
| 1323 |
|
| 1324 |
if (!positionalFormals.append(bindName)) { |
| 1325 |
return Nothing(); |
| 1326 |
} |
| 1327 |
} |
| 1328 |
|
| 1329 |
for (BindingIter bi = scope.bindings(pc); bi; bi++) { |
| 1330 |
ParserBindingName binding(bi.name(), |
| 1331 |
allBindingsClosedOver || bi.closedOver()); |
| 1332 |
switch (bi.kind()) { |
| 1333 |
case BindingKind::FormalParameter: |
| 1334 |
// Positional parameter names are already handled above. |
| 1335 |
if (bi.declarationKind() == DeclarationKind::FormalParameter) { |
| 1336 |
if (!formals.append(binding)) { |
| 1337 |
return Nothing(); |
| 1338 |
} |
| 1339 |
} |
| 1340 |
break; |
| 1341 |
case BindingKind::Var: |
| 1342 |
// The only vars in the function scope when there are parameter |
| 1343 |
// exprs, which induces a separate var environment, should be the |
| 1344 |
// special bindings. |
| 1345 |
MOZ_ASSERT_IF(hasParameterExprs, |
| 1346 |
FunctionScope::isSpecialName(bi.name())); |
| 1347 |
if (!vars.append(binding)) { |
| 1348 |
return Nothing(); |
| 1349 |
} |
| 1350 |
break; |
| 1351 |
case BindingKind::Let: |
| 1352 |
case BindingKind::Const: |
| 1353 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1354 |
case BindingKind::Using: |
| 1355 |
#endif |
| 1356 |
break; |
| 1357 |
default: |
| 1358 |
MOZ_CRASH("bad function scope BindingKind"); |
| 1359 |
break; |
| 1360 |
} |
| 1361 |
} |
| 1362 |
|
| 1363 |
FunctionScope::ParserData* bindings = nullptr; |
| 1364 |
uint32_t numBindings = |
| 1365 |
positionalFormals.length() + formals.length() + vars.length(); |
| 1366 |
|
| 1367 |
if (numBindings > 0) { |
| 1368 |
bindings = NewEmptyBindingData<FunctionScope>(fc, alloc, numBindings); |
| 1369 |
if (!bindings) { |
| 1370 |
return Nothing(); |
| 1371 |
} |
| 1372 |
|
| 1373 |
// The ordering here is important. See comments in FunctionScope. |
| 1374 |
InitializeBindingData( |
| 1375 |
bindings, numBindings, positionalFormals, |
| 1376 |
&ParserFunctionScopeSlotInfo::nonPositionalFormalStart, formals, |
| 1377 |
&ParserFunctionScopeSlotInfo::varStart, vars); |
| 1378 |
} |
| 1379 |
|
| 1380 |
return Some(bindings); |
| 1381 |
} |
| 1382 |
|
| 1383 |
// Compute if `NewFunctionScopeData` would return any binding list with any |
| 1384 |
// entry marked as closed-over. This is done without the need to allocate the |
| 1385 |
// binding list. If true, an EnvironmentObject will be needed at runtime. |
| 1386 |
bool FunctionScopeHasClosedOverBindings(ParseContext* pc) { |
| 1387 |
bool allBindingsClosedOver = pc->sc()->allBindingsClosedOver() || |
| 1388 |
pc->functionScope().tooBigToOptimize(); |
| 1389 |
|
| 1390 |
for (BindingIter bi = pc->functionScope().bindings(pc); bi; bi++) { |
| 1391 |
switch (bi.kind()) { |
| 1392 |
case BindingKind::FormalParameter: |
| 1393 |
case BindingKind::Var: |
| 1394 |
if (allBindingsClosedOver || bi.closedOver()) { |
| 1395 |
return true; |
| 1396 |
} |
| 1397 |
break; |
| 1398 |
|
| 1399 |
default: |
| 1400 |
break; |
| 1401 |
} |
| 1402 |
} |
| 1403 |
|
| 1404 |
return false; |
| 1405 |
} |
| 1406 |
|
| 1407 |
Maybe<FunctionScope::ParserData*> ParserBase::newFunctionScopeData( |
| 1408 |
ParseContext::Scope& scope, bool hasParameterExprs) { |
| 1409 |
return NewFunctionScopeData(fc_, scope, hasParameterExprs, stencilAlloc(), |
| 1410 |
pc_); |
| 1411 |
} |
| 1412 |
|
| 1413 |
VarScope::ParserData* NewEmptyVarScopeData(FrontendContext* fc, |
| 1414 |
LifoAlloc& alloc, |
| 1415 |
uint32_t numBindings) { |
| 1416 |
return NewEmptyBindingData<VarScope>(fc, alloc, numBindings); |
| 1417 |
} |
| 1418 |
|
| 1419 |
static Maybe<VarScope::ParserData*> NewVarScopeData(FrontendContext* fc, |
| 1420 |
ParseContext::Scope& scope, |
| 1421 |
LifoAlloc& alloc, |
| 1422 |
ParseContext* pc) { |
| 1423 |
ParserBindingNameVector vars(fc); |
| 1424 |
|
| 1425 |
bool allBindingsClosedOver = |
| 1426 |
pc->sc()->allBindingsClosedOver() || scope.tooBigToOptimize(); |
| 1427 |
|
| 1428 |
for (BindingIter bi = scope.bindings(pc); bi; bi++) { |
| 1429 |
if (bi.kind() == BindingKind::Var) { |
| 1430 |
ParserBindingName binding(bi.name(), |
| 1431 |
allBindingsClosedOver || bi.closedOver()); |
| 1432 |
if (!vars.append(binding)) { |
| 1433 |
return Nothing(); |
| 1434 |
} |
| 1435 |
} else { |
| 1436 |
MOZ_ASSERT(bi.kind() == BindingKind::Let || |
| 1437 |
bi.kind() == BindingKind::Const |
| 1438 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1439 |
|| bi.kind() == BindingKind::Using |
| 1440 |
#endif |
| 1441 |
, |
| 1442 |
"bad var scope BindingKind"); |
| 1443 |
} |
| 1444 |
} |
| 1445 |
|
| 1446 |
VarScope::ParserData* bindings = nullptr; |
| 1447 |
uint32_t numBindings = vars.length(); |
| 1448 |
|
| 1449 |
if (numBindings > 0) { |
| 1450 |
bindings = NewEmptyBindingData<VarScope>(fc, alloc, numBindings); |
| 1451 |
if (!bindings) { |
| 1452 |
return Nothing(); |
| 1453 |
} |
| 1454 |
|
| 1455 |
InitializeBindingData(bindings, numBindings, vars); |
| 1456 |
} |
| 1457 |
|
| 1458 |
return Some(bindings); |
| 1459 |
} |
| 1460 |
|
| 1461 |
// Compute if `NewVarScopeData` would return any binding list. This is done |
| 1462 |
// without allocate the binding list. |
| 1463 |
static bool VarScopeHasBindings(ParseContext* pc) { |
| 1464 |
for (BindingIter bi = pc->varScope().bindings(pc); bi; bi++) { |
| 1465 |
if (bi.kind() == BindingKind::Var) { |
| 1466 |
return true; |
| 1467 |
} |
| 1468 |
} |
| 1469 |
|
| 1470 |
return false; |
| 1471 |
} |
| 1472 |
|
| 1473 |
Maybe<VarScope::ParserData*> ParserBase::newVarScopeData( |
| 1474 |
ParseContext::Scope& scope) { |
| 1475 |
return NewVarScopeData(fc_, scope, stencilAlloc(), pc_); |
| 1476 |
} |
| 1477 |
|
| 1478 |
static Maybe<LexicalScope::ParserData*> NewLexicalScopeData( |
| 1479 |
FrontendContext* fc, ParseContext::Scope& scope, LifoAlloc& alloc, |
| 1480 |
ParseContext* pc) { |
| 1481 |
ParserBindingNameVector lets(fc); |
| 1482 |
ParserBindingNameVector consts(fc); |
| 1483 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1484 |
ParserBindingNameVector usings(fc); |
| 1485 |
#endif |
| 1486 |
|
| 1487 |
bool allBindingsClosedOver = |
| 1488 |
pc->sc()->allBindingsClosedOver() || scope.tooBigToOptimize(); |
| 1489 |
|
| 1490 |
for (BindingIter bi = scope.bindings(pc); bi; bi++) { |
| 1491 |
ParserBindingName binding(bi.name(), |
| 1492 |
allBindingsClosedOver || bi.closedOver()); |
| 1493 |
switch (bi.kind()) { |
| 1494 |
case BindingKind::Let: |
| 1495 |
if (!lets.append(binding)) { |
| 1496 |
return Nothing(); |
| 1497 |
} |
| 1498 |
break; |
| 1499 |
case BindingKind::Const: |
| 1500 |
if (!consts.append(binding)) { |
| 1501 |
return Nothing(); |
| 1502 |
} |
| 1503 |
break; |
| 1504 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1505 |
case BindingKind::Using: |
| 1506 |
if (!usings.append(binding)) { |
| 1507 |
return Nothing(); |
| 1508 |
} |
| 1509 |
break; |
| 1510 |
#endif |
| 1511 |
case BindingKind::Var: |
| 1512 |
case BindingKind::FormalParameter: |
| 1513 |
break; |
| 1514 |
default: |
| 1515 |
MOZ_CRASH("Bad lexical scope BindingKind"); |
| 1516 |
break; |
| 1517 |
} |
| 1518 |
} |
| 1519 |
|
| 1520 |
LexicalScope::ParserData* bindings = nullptr; |
| 1521 |
uint32_t numBindings = lets.length() + consts.length() |
| 1522 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1523 |
+ usings.length() |
| 1524 |
#endif |
| 1525 |
; |
| 1526 |
|
| 1527 |
if (numBindings > 0) { |
| 1528 |
bindings = NewEmptyBindingData<LexicalScope>(fc, alloc, numBindings); |
| 1529 |
if (!bindings) { |
| 1530 |
return Nothing(); |
| 1531 |
} |
| 1532 |
|
| 1533 |
// The ordering here is important. See comments in LexicalScope. |
| 1534 |
InitializeBindingData(bindings, numBindings, lets, |
| 1535 |
&ParserLexicalScopeSlotInfo::constStart, consts |
| 1536 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1537 |
, |
| 1538 |
&ParserLexicalScopeSlotInfo::usingStart, usings |
| 1539 |
#endif |
| 1540 |
); |
| 1541 |
} |
| 1542 |
|
| 1543 |
return Some(bindings); |
| 1544 |
} |
| 1545 |
|
| 1546 |
// Compute if `NewLexicalScopeData` would return any binding list with any entry |
| 1547 |
// marked as closed-over. This is done without the need to allocate the binding |
| 1548 |
// list. If true, an EnvironmentObject will be needed at runtime. |
| 1549 |
bool LexicalScopeHasClosedOverBindings(ParseContext* pc, |
| 1550 |
ParseContext::Scope& scope) { |
| 1551 |
bool allBindingsClosedOver = |
| 1552 |
pc->sc()->allBindingsClosedOver() || scope.tooBigToOptimize(); |
| 1553 |
|
| 1554 |
for (BindingIter bi = scope.bindings(pc); bi; bi++) { |
| 1555 |
switch (bi.kind()) { |
| 1556 |
case BindingKind::Let: |
| 1557 |
case BindingKind::Const: |
| 1558 |
if (allBindingsClosedOver || bi.closedOver()) { |
| 1559 |
return true; |
| 1560 |
} |
| 1561 |
break; |
| 1562 |
|
| 1563 |
default: |
| 1564 |
break; |
| 1565 |
} |
| 1566 |
} |
| 1567 |
|
| 1568 |
return false; |
| 1569 |
} |
| 1570 |
|
| 1571 |
Maybe<LexicalScope::ParserData*> ParserBase::newLexicalScopeData( |
| 1572 |
ParseContext::Scope& scope) { |
| 1573 |
return NewLexicalScopeData(fc_, scope, stencilAlloc(), pc_); |
| 1574 |
} |
| 1575 |
|
| 1576 |
static Maybe<ClassBodyScope::ParserData*> NewClassBodyScopeData( |
| 1577 |
FrontendContext* fc, ParseContext::Scope& scope, LifoAlloc& alloc, |
| 1578 |
ParseContext* pc) { |
| 1579 |
ParserBindingNameVector privateBrand(fc); |
| 1580 |
ParserBindingNameVector synthetics(fc); |
| 1581 |
ParserBindingNameVector privateMethods(fc); |
| 1582 |
|
| 1583 |
bool allBindingsClosedOver = |
| 1584 |
pc->sc()->allBindingsClosedOver() || scope.tooBigToOptimize(); |
| 1585 |
|
| 1586 |
for (BindingIter bi = scope.bindings(pc); bi; bi++) { |
| 1587 |
ParserBindingName binding(bi.name(), |
| 1588 |
allBindingsClosedOver || bi.closedOver()); |
| 1589 |
switch (bi.kind()) { |
| 1590 |
case BindingKind::Synthetic: |
| 1591 |
if (bi.name() == |
| 1592 |
TaggedParserAtomIndex::WellKnown::dot_privateBrand_()) { |
| 1593 |
MOZ_ASSERT(privateBrand.empty()); |
| 1594 |
if (!privateBrand.append(binding)) { |
| 1595 |
return Nothing(); |
| 1596 |
} |
| 1597 |
} else { |
| 1598 |
if (!synthetics.append(binding)) { |
| 1599 |
return Nothing(); |
| 1600 |
} |
| 1601 |
} |
| 1602 |
break; |
| 1603 |
|
| 1604 |
case BindingKind::PrivateMethod: |
| 1605 |
if (!privateMethods.append(binding)) { |
| 1606 |
return Nothing(); |
| 1607 |
} |
| 1608 |
break; |
| 1609 |
|
| 1610 |
default: |
| 1611 |
MOZ_CRASH("bad class body scope BindingKind"); |
| 1612 |
break; |
| 1613 |
} |
| 1614 |
} |
| 1615 |
|
| 1616 |
// We should have zero or one private brands. |
| 1617 |
MOZ_ASSERT(privateBrand.length() == 0 || privateBrand.length() == 1); |
| 1618 |
|
| 1619 |
ClassBodyScope::ParserData* bindings = nullptr; |
| 1620 |
uint32_t numBindings = |
| 1621 |
privateBrand.length() + synthetics.length() + privateMethods.length(); |
| 1622 |
|
| 1623 |
if (numBindings > 0) { |
| 1624 |
bindings = NewEmptyBindingData<ClassBodyScope>(fc, alloc, numBindings); |
| 1625 |
if (!bindings) { |
| 1626 |
return Nothing(); |
| 1627 |
} |
| 1628 |
// To simplify initialization of the bindings, we concatenate the |
| 1629 |
// synthetics+privateBrand vector such that the private brand is always the |
| 1630 |
// first element, as ordering is important. See comments in ClassBodyScope. |
| 1631 |
ParserBindingNameVector brandAndSynthetics(fc); |
| 1632 |
if (!brandAndSynthetics.appendAll(privateBrand)) { |
| 1633 |
return Nothing(); |
| 1634 |
} |
| 1635 |
if (!brandAndSynthetics.appendAll(synthetics)) { |
| 1636 |
return Nothing(); |
| 1637 |
} |
| 1638 |
|
| 1639 |
// The ordering here is important. See comments in ClassBodyScope. |
| 1640 |
InitializeBindingData(bindings, numBindings, brandAndSynthetics, |
| 1641 |
&ParserClassBodyScopeSlotInfo::privateMethodStart, |
| 1642 |
privateMethods); |
| 1643 |
} |
| 1644 |
|
| 1645 |
// `EmitterScope::lookupPrivate()` requires `.privateBrand` to be stored in a |
| 1646 |
// predictable slot: the first slot available in the environment object, |
| 1647 |
// `ClassBodyLexicalEnvironmentObject::privateBrandSlot()`. We assume that |
| 1648 |
// if `.privateBrand` is first in the scope, it will be stored there. |
| 1649 |
MOZ_ASSERT_IF(!privateBrand.empty(), |
| 1650 |
GetScopeDataTrailingNames(bindings)[0].name() == |
| 1651 |
TaggedParserAtomIndex::WellKnown::dot_privateBrand_()); |
| 1652 |
|
| 1653 |
return Some(bindings); |
| 1654 |
} |
| 1655 |
|
| 1656 |
Maybe<ClassBodyScope::ParserData*> ParserBase::newClassBodyScopeData( |
| 1657 |
ParseContext::Scope& scope) { |
| 1658 |
return NewClassBodyScopeData(fc_, scope, stencilAlloc(), pc_); |
| 1659 |
} |
| 1660 |
|
| 1661 |
template <> |
| 1662 |
SyntaxParseHandler::LexicalScopeNodeResult |
| 1663 |
PerHandlerParser<SyntaxParseHandler>::finishLexicalScope( |
| 1664 |
ParseContext::Scope& scope, Node body, ScopeKind kind) { |
| 1665 |
if (!propagateFreeNamesAndMarkClosedOverBindings(scope)) { |
| 1666 |
return errorResult(); |
| 1667 |
} |
| 1668 |
|
| 1669 |
return handler_.newLexicalScope(body); |
| 1670 |
} |
| 1671 |
|
| 1672 |
template <> |
| 1673 |
FullParseHandler::LexicalScopeNodeResult |
| 1674 |
PerHandlerParser<FullParseHandler>::finishLexicalScope( |
| 1675 |
ParseContext::Scope& scope, ParseNode* body, ScopeKind kind) { |
| 1676 |
if (!propagateFreeNamesAndMarkClosedOverBindings(scope)) { |
| 1677 |
return errorResult(); |
| 1678 |
} |
| 1679 |
|
| 1680 |
Maybe<LexicalScope::ParserData*> bindings = newLexicalScopeData(scope); |
| 1681 |
if (!bindings) { |
| 1682 |
return errorResult(); |
| 1683 |
} |
| 1684 |
|
| 1685 |
return handler_.newLexicalScope(*bindings, body, kind); |
| 1686 |
} |
| 1687 |
|
| 1688 |
template <> |
| 1689 |
SyntaxParseHandler::ClassBodyScopeNodeResult |
| 1690 |
PerHandlerParser<SyntaxParseHandler>::finishClassBodyScope( |
| 1691 |
ParseContext::Scope& scope, ListNodeType body) { |
| 1692 |
if (!propagateFreeNamesAndMarkClosedOverBindings(scope)) { |
| 1693 |
return errorResult(); |
| 1694 |
} |
| 1695 |
|
| 1696 |
return handler_.newClassBodyScope(body); |
| 1697 |
} |
| 1698 |
|
| 1699 |
template <> |
| 1700 |
FullParseHandler::ClassBodyScopeNodeResult |
| 1701 |
PerHandlerParser<FullParseHandler>::finishClassBodyScope( |
| 1702 |
ParseContext::Scope& scope, ListNode* body) { |
| 1703 |
if (!propagateFreeNamesAndMarkClosedOverBindings(scope)) { |
| 1704 |
return errorResult(); |
| 1705 |
} |
| 1706 |
|
| 1707 |
Maybe<ClassBodyScope::ParserData*> bindings = newClassBodyScopeData(scope); |
| 1708 |
if (!bindings) { |
| 1709 |
return errorResult(); |
| 1710 |
} |
| 1711 |
|
| 1712 |
return handler_.newClassBodyScope(*bindings, body); |
| 1713 |
} |
| 1714 |
|
| 1715 |
template <class ParseHandler> |
| 1716 |
bool PerHandlerParser<ParseHandler>::checkForUndefinedPrivateFields( |
| 1717 |
EvalSharedContext* evalSc) { |
| 1718 |
if (!this->compilationState_.isInitialStencil()) { |
| 1719 |
// We're delazifying -- so we already checked private names during first |
| 1720 |
// parse. |
| 1721 |
return true; |
| 1722 |
} |
| 1723 |
|
| 1724 |
Vector<UnboundPrivateName, 8> unboundPrivateNames(fc_); |
| 1725 |
if (!usedNames_.getUnboundPrivateNames(unboundPrivateNames)) { |
| 1726 |
return false; |
| 1727 |
} |
| 1728 |
|
| 1729 |
// No unbound names, let's get out of here! |
| 1730 |
if (unboundPrivateNames.empty()) { |
| 1731 |
return true; |
| 1732 |
} |
| 1733 |
|
| 1734 |
// It is an early error if there's private name references unbound, |
| 1735 |
// unless it's an eval, in which case we need to check the scope |
| 1736 |
// chain. |
| 1737 |
if (!evalSc) { |
| 1738 |
// The unbound private names are sorted, so just grab the first one. |
| 1739 |
UnboundPrivateName minimum = unboundPrivateNames[0]; |
| 1740 |
UniqueChars str = this->parserAtoms().toPrintableString(minimum.atom); |
| 1741 |
if (!str) { |
| 1742 |
ReportOutOfMemory(this->fc_); |
| 1743 |
return false; |
| 1744 |
} |
| 1745 |
|
| 1746 |
errorAt(minimum.position.begin, JSMSG_MISSING_PRIVATE_DECL, str.get()); |
| 1747 |
return false; |
| 1748 |
} |
| 1749 |
|
| 1750 |
// It's important that the unbound private names are sorted, as we |
| 1751 |
// want our errors to always be issued to the first textually. |
| 1752 |
for (UnboundPrivateName unboundName : unboundPrivateNames) { |
| 1753 |
// If the enclosingScope is non-syntactic, then we are in a |
| 1754 |
// Debugger.Frame.prototype.eval call. In order to find the declared private |
| 1755 |
// names, we must use the effective scope that was determined when creating |
| 1756 |
// the scopeContext. |
| 1757 |
if (!this->compilationState_.scopeContext |
| 1758 |
.effectiveScopePrivateFieldCacheHas(unboundName.atom)) { |
| 1759 |
UniqueChars str = this->parserAtoms().toPrintableString(unboundName.atom); |
| 1760 |
if (!str) { |
| 1761 |
ReportOutOfMemory(this->fc_); |
| 1762 |
return false; |
| 1763 |
} |
| 1764 |
errorAt(unboundName.position.begin, JSMSG_MISSING_PRIVATE_DECL, |
| 1765 |
str.get()); |
| 1766 |
return false; |
| 1767 |
} |
| 1768 |
} |
| 1769 |
|
| 1770 |
return true; |
| 1771 |
} |
| 1772 |
|
| 1773 |
template <typename Unit> |
| 1774 |
FullParseHandler::LexicalScopeNodeResult |
| 1775 |
Parser<FullParseHandler, Unit>::evalBody(EvalSharedContext* evalsc) { |
| 1776 |
SourceParseContext evalpc(this, evalsc, /* newDirectives = */ nullptr); |
| 1777 |
if (!evalpc.init()) { |
| 1778 |
return errorResult(); |
| 1779 |
} |
| 1780 |
|
| 1781 |
ParseContext::VarScope varScope(this); |
| 1782 |
if (!varScope.init(pc_)) { |
| 1783 |
return errorResult(); |
| 1784 |
} |
| 1785 |
|
| 1786 |
LexicalScopeNode* body; |
| 1787 |
{ |
| 1788 |
// All evals have an implicit non-extensible lexical scope. |
| 1789 |
ParseContext::Scope lexicalScope(this); |
| 1790 |
if (!lexicalScope.init(pc_)) { |
| 1791 |
return errorResult(); |
| 1792 |
} |
| 1793 |
|
| 1794 |
ListNode* list; |
| 1795 |
MOZ_TRY_VAR(list, statementList(YieldIsName)); |
| 1796 |
|
| 1797 |
if (!checkStatementsEOF()) { |
| 1798 |
return errorResult(); |
| 1799 |
} |
| 1800 |
|
| 1801 |
// Private names not lexically defined must trigger a syntax error. |
| 1802 |
if (!checkForUndefinedPrivateFields(evalsc)) { |
| 1803 |
return errorResult(); |
| 1804 |
} |
| 1805 |
|
| 1806 |
MOZ_TRY_VAR(body, finishLexicalScope(lexicalScope, list)); |
| 1807 |
} |
| 1808 |
|
| 1809 |
#ifdef DEBUG |
| 1810 |
if (evalpc.superScopeNeedsHomeObject() && |
| 1811 |
!this->compilationState_.input.enclosingScope.isNull()) { |
| 1812 |
// If superScopeNeedsHomeObject_ is set and we are an entry-point |
| 1813 |
// ParseContext, then we must be emitting an eval script, and the |
| 1814 |
// outer function must already be marked as needing a home object |
| 1815 |
// since it contains an eval. |
| 1816 |
MOZ_ASSERT( |
| 1817 |
this->compilationState_.scopeContext.hasFunctionNeedsHomeObjectOnChain, |
| 1818 |
"Eval must have found an enclosing function box scope that " |
| 1819 |
"allows super.property"); |
| 1820 |
} |
| 1821 |
#endif |
| 1822 |
|
| 1823 |
if (!CheckParseTree(this->fc_, alloc_, body)) { |
| 1824 |
return errorResult(); |
| 1825 |
} |
| 1826 |
|
| 1827 |
ParseNode* node = body; |
| 1828 |
// Don't constant-fold inside "use asm" code, as this could create a parse |
| 1829 |
// tree that doesn't type-check as asm.js. |
| 1830 |
if (!pc_->useAsmOrInsideUseAsm()) { |
| 1831 |
if (!FoldConstants(this->fc_, this->parserAtoms(), this->bigInts(), &node, |
| 1832 |
&handler_)) { |
| 1833 |
return errorResult(); |
| 1834 |
} |
| 1835 |
} |
| 1836 |
body = handler_.asLexicalScopeNode(node); |
| 1837 |
|
| 1838 |
if (!this->setSourceMapInfo()) { |
| 1839 |
return errorResult(); |
| 1840 |
} |
| 1841 |
|
| 1842 |
if (pc_->sc()->strict()) { |
| 1843 |
if (!propagateFreeNamesAndMarkClosedOverBindings(varScope)) { |
| 1844 |
return errorResult(); |
| 1845 |
} |
| 1846 |
} else { |
| 1847 |
// For non-strict eval scripts, since all bindings are automatically |
| 1848 |
// considered closed over, we don't need to call propagateFreeNames- |
| 1849 |
// AndMarkClosedOverBindings. However, Annex B.3.3 functions still need to |
| 1850 |
// be marked. |
| 1851 |
if (!varScope.propagateAndMarkAnnexBFunctionBoxes(pc_, this)) { |
| 1852 |
return errorResult(); |
| 1853 |
} |
| 1854 |
} |
| 1855 |
|
| 1856 |
Maybe<EvalScope::ParserData*> bindings = newEvalScopeData(pc_->varScope()); |
| 1857 |
if (!bindings) { |
| 1858 |
return errorResult(); |
| 1859 |
} |
| 1860 |
evalsc->bindings = *bindings; |
| 1861 |
|
| 1862 |
return body; |
| 1863 |
} |
| 1864 |
|
| 1865 |
template <typename Unit> |
| 1866 |
FullParseHandler::ListNodeResult Parser<FullParseHandler, Unit>::globalBody( |
| 1867 |
GlobalSharedContext* globalsc) { |
| 1868 |
SourceParseContext globalpc(this, globalsc, /* newDirectives = */ nullptr); |
| 1869 |
if (!globalpc.init()) { |
| 1870 |
return errorResult(); |
| 1871 |
} |
| 1872 |
|
| 1873 |
ParseContext::VarScope varScope(this); |
| 1874 |
if (!varScope.init(pc_)) { |
| 1875 |
return errorResult(); |
| 1876 |
} |
| 1877 |
|
| 1878 |
ListNode* body; |
| 1879 |
MOZ_TRY_VAR(body, statementList(YieldIsName)); |
| 1880 |
|
| 1881 |
if (!checkStatementsEOF()) { |
| 1882 |
return errorResult(); |
| 1883 |
} |
| 1884 |
|
| 1885 |
if (!CheckParseTree(this->fc_, alloc_, body)) { |
| 1886 |
return errorResult(); |
| 1887 |
} |
| 1888 |
|
| 1889 |
if (!checkForUndefinedPrivateFields()) { |
| 1890 |
return errorResult(); |
| 1891 |
} |
| 1892 |
|
| 1893 |
ParseNode* node = body; |
| 1894 |
// Don't constant-fold inside "use asm" code, as this could create a parse |
| 1895 |
// tree that doesn't type-check as asm.js. |
| 1896 |
if (!pc_->useAsmOrInsideUseAsm()) { |
| 1897 |
if (!FoldConstants(this->fc_, this->parserAtoms(), this->bigInts(), &node, |
| 1898 |
&handler_)) { |
| 1899 |
return errorResult(); |
| 1900 |
} |
| 1901 |
} |
| 1902 |
body = &node->as<ListNode>(); |
| 1903 |
|
| 1904 |
if (!this->setSourceMapInfo()) { |
| 1905 |
return errorResult(); |
| 1906 |
} |
| 1907 |
|
| 1908 |
// For global scripts, whether bindings are closed over or not doesn't |
| 1909 |
// matter, so no need to call propagateFreeNamesAndMarkClosedOver- |
| 1910 |
// Bindings. However, Annex B.3.3 functions still need to be marked. |
| 1911 |
if (!varScope.propagateAndMarkAnnexBFunctionBoxes(pc_, this)) { |
| 1912 |
return errorResult(); |
| 1913 |
} |
| 1914 |
|
| 1915 |
Maybe<GlobalScope::ParserData*> bindings = |
| 1916 |
newGlobalScopeData(pc_->varScope()); |
| 1917 |
if (!bindings) { |
| 1918 |
return errorResult(); |
| 1919 |
} |
| 1920 |
globalsc->bindings = *bindings; |
| 1921 |
|
| 1922 |
return body; |
| 1923 |
} |
| 1924 |
|
| 1925 |
template <typename Unit> |
| 1926 |
FullParseHandler::ModuleNodeResult Parser<FullParseHandler, Unit>::moduleBody( |
| 1927 |
ModuleSharedContext* modulesc) { |
| 1928 |
MOZ_ASSERT(checkOptionsCalled_); |
| 1929 |
|
| 1930 |
this->compilationState_.moduleMetadata = |
| 1931 |
fc_->getAllocator()->template new_<StencilModuleMetadata>(); |
| 1932 |
if (!this->compilationState_.moduleMetadata) { |
| 1933 |
return errorResult(); |
| 1934 |
} |
| 1935 |
|
| 1936 |
SourceParseContext modulepc(this, modulesc, nullptr); |
| 1937 |
if (!modulepc.init()) { |
| 1938 |
return errorResult(); |
| 1939 |
} |
| 1940 |
|
| 1941 |
ParseContext::VarScope varScope(this); |
| 1942 |
if (!varScope.init(pc_)) { |
| 1943 |
return errorResult(); |
| 1944 |
} |
| 1945 |
|
| 1946 |
ModuleNodeType moduleNode; |
| 1947 |
MOZ_TRY_VAR(moduleNode, handler_.newModule(pos())); |
| 1948 |
|
| 1949 |
AutoAwaitIsKeyword<FullParseHandler, Unit> awaitIsKeyword( |
| 1950 |
this, AwaitIsModuleKeyword); |
| 1951 |
ListNode* stmtList; |
| 1952 |
MOZ_TRY_VAR(stmtList, statementList(YieldIsName)); |
| 1953 |
|
| 1954 |
MOZ_ASSERT(stmtList->isKind(ParseNodeKind::StatementList)); |
| 1955 |
moduleNode->setBody(&stmtList->template as<ListNode>()); |
| 1956 |
|
| 1957 |
if (pc_->isAsync()) { |
| 1958 |
if (!noteUsedName(TaggedParserAtomIndex::WellKnown::dot_generator_())) { |
| 1959 |
return errorResult(); |
| 1960 |
} |
| 1961 |
|
| 1962 |
if (!pc_->declareTopLevelDotGeneratorName()) { |
| 1963 |
return errorResult(); |
| 1964 |
} |
| 1965 |
} |
| 1966 |
|
| 1967 |
TokenKind tt; |
| 1968 |
if (!tokenStream.getToken(&tt, TokenStream::SlashIsRegExp)) { |
| 1969 |
return errorResult(); |
| 1970 |
} |
| 1971 |
if (tt != TokenKind::Eof) { |
| 1972 |
error(JSMSG_GARBAGE_AFTER_INPUT, "module", TokenKindToDesc(tt)); |
| 1973 |
return errorResult(); |
| 1974 |
} |
| 1975 |
|
| 1976 |
// Set the module to async if an await keyword was found at the top level. |
| 1977 |
if (pc_->isAsync()) { |
| 1978 |
pc_->sc()->asModuleContext()->builder.noteAsync( |
| 1979 |
*this->compilationState_.moduleMetadata); |
| 1980 |
} |
| 1981 |
|
| 1982 |
// Generate the Import/Export tables and store in CompilationState. |
| 1983 |
if (!modulesc->builder.buildTables(*this->compilationState_.moduleMetadata)) { |
| 1984 |
return errorResult(); |
| 1985 |
} |
| 1986 |
|
| 1987 |
// Check exported local bindings exist and mark them as closed over. |
| 1988 |
StencilModuleMetadata& moduleMetadata = |
| 1989 |
*this->compilationState_.moduleMetadata; |
| 1990 |
for (auto entry : moduleMetadata.localExportEntries) { |
| 1991 |
DeclaredNamePtr p = modulepc.varScope().lookupDeclaredName(entry.localName); |
| 1992 |
if (!p) { |
| 1993 |
UniqueChars str = this->parserAtoms().toPrintableString(entry.localName); |
| 1994 |
if (!str) { |
| 1995 |
ReportOutOfMemory(this->fc_); |
| 1996 |
return errorResult(); |
| 1997 |
} |
| 1998 |
|
| 1999 |
errorNoOffset(JSMSG_MISSING_EXPORT, str.get()); |
| 2000 |
return errorResult(); |
| 2001 |
} |
| 2002 |
|
| 2003 |
p->value()->setClosedOver(); |
| 2004 |
} |
| 2005 |
|
| 2006 |
// Reserve an environment slot for a "*namespace*" psuedo-binding and mark as |
| 2007 |
// closed-over. We do not know until module linking if this will be used. |
| 2008 |
if (!noteDeclaredName( |
| 2009 |
TaggedParserAtomIndex::WellKnown::star_namespace_star_(), |
| 2010 |
DeclarationKind::Const, pos())) { |
| 2011 |
return errorResult(); |
| 2012 |
} |
| 2013 |
modulepc.varScope() |
| 2014 |
.lookupDeclaredName( |
| 2015 |
TaggedParserAtomIndex::WellKnown::star_namespace_star_()) |
| 2016 |
->value() |
| 2017 |
->setClosedOver(); |
| 2018 |
|
| 2019 |
if (!CheckParseTree(this->fc_, alloc_, stmtList)) { |
| 2020 |
return errorResult(); |
| 2021 |
} |
| 2022 |
|
| 2023 |
ParseNode* node = stmtList; |
| 2024 |
// Don't constant-fold inside "use asm" code, as this could create a parse |
| 2025 |
// tree that doesn't type-check as asm.js. |
| 2026 |
if (!pc_->useAsmOrInsideUseAsm()) { |
| 2027 |
if (!FoldConstants(this->fc_, this->parserAtoms(), this->bigInts(), &node, |
| 2028 |
&handler_)) { |
| 2029 |
return errorResult(); |
| 2030 |
} |
| 2031 |
} |
| 2032 |
stmtList = &node->as<ListNode>(); |
| 2033 |
|
| 2034 |
if (!this->setSourceMapInfo()) { |
| 2035 |
return errorResult(); |
| 2036 |
} |
| 2037 |
|
| 2038 |
// Private names not lexically defined must trigger a syntax error. |
| 2039 |
if (!checkForUndefinedPrivateFields()) { |
| 2040 |
return errorResult(); |
| 2041 |
} |
| 2042 |
|
| 2043 |
if (!propagateFreeNamesAndMarkClosedOverBindings(modulepc.varScope())) { |
| 2044 |
return errorResult(); |
| 2045 |
} |
| 2046 |
|
| 2047 |
Maybe<ModuleScope::ParserData*> bindings = |
| 2048 |
newModuleScopeData(modulepc.varScope()); |
| 2049 |
if (!bindings) { |
| 2050 |
return errorResult(); |
| 2051 |
} |
| 2052 |
|
| 2053 |
modulesc->bindings = *bindings; |
| 2054 |
return moduleNode; |
| 2055 |
} |
| 2056 |
|
| 2057 |
template <typename Unit> |
| 2058 |
SyntaxParseHandler::ModuleNodeResult |
| 2059 |
Parser<SyntaxParseHandler, Unit>::moduleBody(ModuleSharedContext* modulesc) { |
| 2060 |
MOZ_ALWAYS_FALSE(abortIfSyntaxParser()); |
| 2061 |
return errorResult(); |
| 2062 |
} |
| 2063 |
|
| 2064 |
template <class ParseHandler> |
| 2065 |
typename ParseHandler::NameNodeResult |
| 2066 |
PerHandlerParser<ParseHandler>::newInternalDotName(TaggedParserAtomIndex name) { |
| 2067 |
NameNodeType nameNode; |
| 2068 |
MOZ_TRY_VAR(nameNode, newName(name)); |
| 2069 |
if (!noteUsedName(name)) { |
| 2070 |
return errorResult(); |
| 2071 |
} |
| 2072 |
return nameNode; |
| 2073 |
} |
| 2074 |
|
| 2075 |
template <class ParseHandler> |
| 2076 |
typename ParseHandler::NameNodeResult |
| 2077 |
PerHandlerParser<ParseHandler>::newThisName() { |
| 2078 |
return newInternalDotName(TaggedParserAtomIndex::WellKnown::dot_this_()); |
| 2079 |
} |
| 2080 |
|
| 2081 |
template <class ParseHandler> |
| 2082 |
typename ParseHandler::NameNodeResult |
| 2083 |
PerHandlerParser<ParseHandler>::newNewTargetName() { |
| 2084 |
return newInternalDotName(TaggedParserAtomIndex::WellKnown::dot_newTarget_()); |
| 2085 |
} |
| 2086 |
|
| 2087 |
template <class ParseHandler> |
| 2088 |
typename ParseHandler::NameNodeResult |
| 2089 |
PerHandlerParser<ParseHandler>::newDotGeneratorName() { |
| 2090 |
return newInternalDotName(TaggedParserAtomIndex::WellKnown::dot_generator_()); |
| 2091 |
} |
| 2092 |
|
| 2093 |
template <class ParseHandler> |
| 2094 |
bool PerHandlerParser<ParseHandler>::finishFunctionScopes( |
| 2095 |
bool isStandaloneFunction) { |
| 2096 |
FunctionBox* funbox = pc_->functionBox(); |
| 2097 |
|
| 2098 |
if (funbox->hasParameterExprs) { |
| 2099 |
if (!propagateFreeNamesAndMarkClosedOverBindings(pc_->functionScope())) { |
| 2100 |
return false; |
| 2101 |
} |
| 2102 |
|
| 2103 |
// Functions with parameter expressions utilize the FunctionScope for vars |
| 2104 |
// generated by sloppy-direct-evals, as well as arguments (which are |
| 2105 |
// lexicals bindings). If the function body has var bindings (or has a |
| 2106 |
// sloppy-direct-eval that might), then an extra VarScope must be created |
| 2107 |
// for them. |
| 2108 |
if (VarScopeHasBindings(pc_) || |
| 2109 |
funbox->needsExtraBodyVarEnvironmentRegardlessOfBindings()) { |
| 2110 |
funbox->setFunctionHasExtraBodyVarScope(); |
| 2111 |
} |
| 2112 |
} |
| 2113 |
|
| 2114 |
// See: JSFunction::needsCallObject() |
| 2115 |
if (FunctionScopeHasClosedOverBindings(pc_) || |
| 2116 |
funbox->needsCallObjectRegardlessOfBindings()) { |
| 2117 |
funbox->setNeedsFunctionEnvironmentObjects(); |
| 2118 |
} |
| 2119 |
|
| 2120 |
if (funbox->isNamedLambda() && !isStandaloneFunction) { |
| 2121 |
if (!propagateFreeNamesAndMarkClosedOverBindings(pc_->namedLambdaScope())) { |
| 2122 |
return false; |
| 2123 |
} |
| 2124 |
|
| 2125 |
// See: JSFunction::needsNamedLambdaEnvironment() |
| 2126 |
if (LexicalScopeHasClosedOverBindings(pc_, pc_->namedLambdaScope())) { |
| 2127 |
funbox->setNeedsFunctionEnvironmentObjects(); |
| 2128 |
} |
| 2129 |
} |
| 2130 |
|
| 2131 |
return true; |
| 2132 |
} |
| 2133 |
|
| 2134 |
template <> |
| 2135 |
bool PerHandlerParser<FullParseHandler>::finishFunction( |
| 2136 |
bool isStandaloneFunction /* = false */) { |
| 2137 |
if (!finishFunctionScopes(isStandaloneFunction)) { |
| 2138 |
return false; |
| 2139 |
} |
| 2140 |
|
| 2141 |
FunctionBox* funbox = pc_->functionBox(); |
| 2142 |
ScriptStencil& script = funbox->functionStencil(); |
| 2143 |
|
| 2144 |
if (funbox->isInterpreted()) { |
| 2145 |
// BCE will need to generate bytecode for this. |
| 2146 |
funbox->emitBytecode = true; |
| 2147 |
this->compilationState_.nonLazyFunctionCount++; |
| 2148 |
} |
| 2149 |
|
| 2150 |
bool hasParameterExprs = funbox->hasParameterExprs; |
| 2151 |
|
| 2152 |
if (hasParameterExprs) { |
| 2153 |
Maybe<VarScope::ParserData*> bindings = newVarScopeData(pc_->varScope()); |
| 2154 |
if (!bindings) { |
| 2155 |
return false; |
| 2156 |
} |
| 2157 |
funbox->setExtraVarScopeBindings(*bindings); |
| 2158 |
|
| 2159 |
MOZ_ASSERT(bool(*bindings) == VarScopeHasBindings(pc_)); |
| 2160 |
MOZ_ASSERT_IF(!funbox->needsExtraBodyVarEnvironmentRegardlessOfBindings(), |
| 2161 |
bool(*bindings) == funbox->functionHasExtraBodyVarScope()); |
| 2162 |
} |
| 2163 |
|
| 2164 |
{ |
| 2165 |
Maybe<FunctionScope::ParserData*> bindings = |
| 2166 |
newFunctionScopeData(pc_->functionScope(), hasParameterExprs); |
| 2167 |
if (!bindings) { |
| 2168 |
return false; |
| 2169 |
} |
| 2170 |
funbox->setFunctionScopeBindings(*bindings); |
| 2171 |
} |
| 2172 |
|
| 2173 |
if (funbox->isNamedLambda() && !isStandaloneFunction) { |
| 2174 |
Maybe<LexicalScope::ParserData*> bindings = |
| 2175 |
newLexicalScopeData(pc_->namedLambdaScope()); |
| 2176 |
if (!bindings) { |
| 2177 |
return false; |
| 2178 |
} |
| 2179 |
funbox->setNamedLambdaBindings(*bindings); |
| 2180 |
} |
| 2181 |
|
| 2182 |
funbox->finishScriptFlags(); |
| 2183 |
funbox->copyFunctionFields(script); |
| 2184 |
|
| 2185 |
if (this->compilationState_.isInitialStencil()) { |
| 2186 |
ScriptStencilExtra& scriptExtra = funbox->functionExtraStencil(); |
| 2187 |
funbox->copyFunctionExtraFields(scriptExtra); |
| 2188 |
funbox->copyScriptExtraFields(scriptExtra); |
| 2189 |
} |
| 2190 |
|
| 2191 |
return true; |
| 2192 |
} |
| 2193 |
|
| 2194 |
template <> |
| 2195 |
bool PerHandlerParser<SyntaxParseHandler>::finishFunction( |
| 2196 |
bool isStandaloneFunction /* = false */) { |
| 2197 |
// The BaseScript for a lazily parsed function needs to know its set of |
| 2198 |
// free variables and inner functions so that when it is fully parsed, we |
| 2199 |
// can skip over any already syntax parsed inner functions and still |
| 2200 |
// retain correct scope information. |
| 2201 |
|
| 2202 |
if (!finishFunctionScopes(isStandaloneFunction)) { |
| 2203 |
return false; |
| 2204 |
} |
| 2205 |
|
| 2206 |
FunctionBox* funbox = pc_->functionBox(); |
| 2207 |
ScriptStencil& script = funbox->functionStencil(); |
| 2208 |
|
| 2209 |
funbox->finishScriptFlags(); |
| 2210 |
funbox->copyFunctionFields(script); |
| 2211 |
|
| 2212 |
ScriptStencilExtra& scriptExtra = funbox->functionExtraStencil(); |
| 2213 |
funbox->copyFunctionExtraFields(scriptExtra); |
| 2214 |
funbox->copyScriptExtraFields(scriptExtra); |
| 2215 |
|
| 2216 |
// Elide nullptr sentinels from end of binding list. These are inserted for |
| 2217 |
// each scope regardless of if any bindings are actually closed over. |
| 2218 |
{ |
| 2219 |
AtomVector& closedOver = pc_->closedOverBindingsForLazy(); |
| 2220 |
while (!closedOver.empty() && !closedOver.back()) { |
| 2221 |
closedOver.popBack(); |
| 2222 |
} |
| 2223 |
} |
| 2224 |
|
| 2225 |
// Check if we will overflow the `ngcthings` field later. |
| 2226 |
mozilla::CheckedUint32 ngcthings = |
| 2227 |
mozilla::CheckedUint32(pc_->innerFunctionIndexesForLazy.length()) + |
| 2228 |
mozilla::CheckedUint32(pc_->closedOverBindingsForLazy().length()); |
| 2229 |
if (!ngcthings.isValid()) { |
| 2230 |
ReportAllocationOverflow(fc_); |
| 2231 |
return false; |
| 2232 |
} |
| 2233 |
|
| 2234 |
// If there are no script-things, we can return early without allocating. |
| 2235 |
if (ngcthings.value() == 0) { |
| 2236 |
MOZ_ASSERT(!script.hasGCThings()); |
| 2237 |
return true; |
| 2238 |
} |
| 2239 |
|
| 2240 |
TaggedScriptThingIndex* cursor = nullptr; |
| 2241 |
if (!this->compilationState_.allocateGCThingsUninitialized( |
| 2242 |
fc_, funbox->index(), ngcthings.value(), &cursor)) { |
| 2243 |
return false; |
| 2244 |
} |
| 2245 |
|
| 2246 |
// Copy inner-function and closed-over-binding info for the stencil. The order |
| 2247 |
// is important here. We emit functions first, followed by the bindings info. |
| 2248 |
// The bindings list uses nullptr as delimiter to separates the bindings per |
| 2249 |
// scope. |
| 2250 |
// |
| 2251 |
// See: FullParseHandler::nextLazyInnerFunction(), |
| 2252 |
// FullParseHandler::nextLazyClosedOverBinding() |
| 2253 |
for (const ScriptIndex& index : pc_->innerFunctionIndexesForLazy) { |
| 2254 |
void* raw = &(*cursor++); |
| 2255 |
new (raw) TaggedScriptThingIndex(index); |
| 2256 |
} |
| 2257 |
for (auto binding : pc_->closedOverBindingsForLazy()) { |
| 2258 |
void* raw = &(*cursor++); |
| 2259 |
if (binding) { |
| 2260 |
this->parserAtoms().markUsedByStencil(binding, ParserAtom::Atomize::Yes); |
| 2261 |
new (raw) TaggedScriptThingIndex(binding); |
| 2262 |
} else { |
| 2263 |
new (raw) TaggedScriptThingIndex(); |
| 2264 |
} |
| 2265 |
} |
| 2266 |
|
| 2267 |
return true; |
| 2268 |
} |
| 2269 |
|
| 2270 |
static YieldHandling GetYieldHandling(GeneratorKind generatorKind) { |
| 2271 |
if (generatorKind == GeneratorKind::NotGenerator) { |
| 2272 |
return YieldIsName; |
| 2273 |
} |
| 2274 |
return YieldIsKeyword; |
| 2275 |
} |
| 2276 |
|
| 2277 |
static AwaitHandling GetAwaitHandling(FunctionAsyncKind asyncKind) { |
| 2278 |
if (asyncKind == FunctionAsyncKind::SyncFunction) { |
| 2279 |
return AwaitIsName; |
| 2280 |
} |
| 2281 |
return AwaitIsKeyword; |
| 2282 |
} |
| 2283 |
|
| 2284 |
static FunctionFlags InitialFunctionFlags(FunctionSyntaxKind kind, |
| 2285 |
GeneratorKind generatorKind, |
| 2286 |
FunctionAsyncKind asyncKind, |
| 2287 |
bool isSelfHosting) { |
| 2288 |
FunctionFlags flags = {}; |
| 2289 |
|
| 2290 |
switch (kind) { |
| 2291 |
case FunctionSyntaxKind::Expression: |
| 2292 |
flags = (generatorKind == GeneratorKind::NotGenerator && |
| 2293 |
asyncKind == FunctionAsyncKind::SyncFunction |
| 2294 |
? FunctionFlags::INTERPRETED_LAMBDA |
| 2295 |
: FunctionFlags::INTERPRETED_LAMBDA_GENERATOR_OR_ASYNC); |
| 2296 |
break; |
| 2297 |
case FunctionSyntaxKind::Arrow: |
| 2298 |
flags = FunctionFlags::INTERPRETED_LAMBDA_ARROW; |
| 2299 |
break; |
| 2300 |
case FunctionSyntaxKind::Method: |
| 2301 |
case FunctionSyntaxKind::FieldInitializer: |
| 2302 |
case FunctionSyntaxKind::StaticClassBlock: |
| 2303 |
flags = FunctionFlags::INTERPRETED_METHOD; |
| 2304 |
break; |
| 2305 |
case FunctionSyntaxKind::ClassConstructor: |
| 2306 |
case FunctionSyntaxKind::DerivedClassConstructor: |
| 2307 |
flags = FunctionFlags::INTERPRETED_CLASS_CTOR; |
| 2308 |
break; |
| 2309 |
case FunctionSyntaxKind::Getter: |
| 2310 |
flags = FunctionFlags::INTERPRETED_GETTER; |
| 2311 |
break; |
| 2312 |
case FunctionSyntaxKind::Setter: |
| 2313 |
flags = FunctionFlags::INTERPRETED_SETTER; |
| 2314 |
break; |
| 2315 |
default: |
| 2316 |
MOZ_ASSERT(kind == FunctionSyntaxKind::Statement); |
| 2317 |
flags = (generatorKind == GeneratorKind::NotGenerator && |
| 2318 |
asyncKind == FunctionAsyncKind::SyncFunction |
| 2319 |
? FunctionFlags::INTERPRETED_NORMAL |
| 2320 |
: FunctionFlags::INTERPRETED_GENERATOR_OR_ASYNC); |
| 2321 |
} |
| 2322 |
|
| 2323 |
if (isSelfHosting) { |
| 2324 |
flags.setIsSelfHostedBuiltin(); |
| 2325 |
} |
| 2326 |
|
| 2327 |
return flags; |
| 2328 |
} |
| 2329 |
|
| 2330 |
template <typename Unit> |
| 2331 |
FullParseHandler::FunctionNodeResult |
| 2332 |
Parser<FullParseHandler, Unit>::standaloneFunction( |
| 2333 |
const Maybe<uint32_t>& parameterListEnd, FunctionSyntaxKind syntaxKind, |
| 2334 |
GeneratorKind generatorKind, FunctionAsyncKind asyncKind, |
| 2335 |
Directives inheritedDirectives, Directives* newDirectives) { |
| 2336 |
MOZ_ASSERT(checkOptionsCalled_); |
| 2337 |
// Skip prelude. |
| 2338 |
TokenKind tt; |
| 2339 |
if (!tokenStream.getToken(&tt, TokenStream::SlashIsRegExp)) { |
| 2340 |
return errorResult(); |
| 2341 |
} |
| 2342 |
if (asyncKind == FunctionAsyncKind::AsyncFunction) { |
| 2343 |
MOZ_ASSERT(tt == TokenKind::Async); |
| 2344 |
if (!tokenStream.getToken(&tt, TokenStream::SlashIsRegExp)) { |
| 2345 |
return errorResult(); |
| 2346 |
} |
| 2347 |
} |
| 2348 |
MOZ_ASSERT(tt == TokenKind::Function); |
| 2349 |
|
| 2350 |
if (!tokenStream.getToken(&tt)) { |
| 2351 |
return errorResult(); |
| 2352 |
} |
| 2353 |
if (generatorKind == GeneratorKind::Generator) { |
| 2354 |
MOZ_ASSERT(tt == TokenKind::Mul); |
| 2355 |
if (!tokenStream.getToken(&tt)) { |
| 2356 |
return errorResult(); |
| 2357 |
} |
| 2358 |
} |
| 2359 |
|
| 2360 |
// Skip function name, if present. |
| 2361 |
TaggedParserAtomIndex explicitName; |
| 2362 |
if (TokenKindIsPossibleIdentifierName(tt)) { |
| 2363 |
explicitName = anyChars.currentName(); |
| 2364 |
} else { |
| 2365 |
anyChars.ungetToken(); |
| 2366 |
} |
| 2367 |
|
| 2368 |
FunctionNodeType funNode; |
| 2369 |
MOZ_TRY_VAR(funNode, handler_.newFunction(syntaxKind, pos())); |
| 2370 |
|
| 2371 |
ParamsBodyNodeType argsbody; |
| 2372 |
MOZ_TRY_VAR(argsbody, handler_.newParamsBody(pos())); |
| 2373 |
funNode->setBody(argsbody); |
| 2374 |
|
| 2375 |
bool isSelfHosting = options().selfHostingMode; |
| 2376 |
FunctionFlags flags = |
| 2377 |
InitialFunctionFlags(syntaxKind, generatorKind, asyncKind, isSelfHosting); |
| 2378 |
FunctionBox* funbox = |
| 2379 |
newFunctionBox(funNode, explicitName, flags, /* toStringStart = */ 0, |
| 2380 |
inheritedDirectives, generatorKind, asyncKind); |
| 2381 |
if (!funbox) { |
| 2382 |
return errorResult(); |
| 2383 |
} |
| 2384 |
|
| 2385 |
// Function is not syntactically part of another script. |
| 2386 |
MOZ_ASSERT(funbox->index() == CompilationStencil::TopLevelIndex); |
| 2387 |
|
| 2388 |
funbox->initStandalone(this->compilationState_.scopeContext, syntaxKind); |
| 2389 |
|
| 2390 |
SourceParseContext funpc(this, funbox, newDirectives); |
| 2391 |
if (!funpc.init()) { |
| 2392 |
return errorResult(); |
| 2393 |
} |
| 2394 |
|
| 2395 |
YieldHandling yieldHandling = GetYieldHandling(generatorKind); |
| 2396 |
AwaitHandling awaitHandling = GetAwaitHandling(asyncKind); |
| 2397 |
AutoAwaitIsKeyword<FullParseHandler, Unit> awaitIsKeyword(this, |
| 2398 |
awaitHandling); |
| 2399 |
if (!functionFormalParametersAndBody(InAllowed, yieldHandling, &funNode, |
| 2400 |
syntaxKind, parameterListEnd, |
| 2401 |
/* isStandaloneFunction = */ true)) { |
| 2402 |
return errorResult(); |
| 2403 |
} |
| 2404 |
|
| 2405 |
if (!tokenStream.getToken(&tt, TokenStream::SlashIsRegExp)) { |
| 2406 |
return errorResult(); |
| 2407 |
} |
| 2408 |
if (tt != TokenKind::Eof) { |
| 2409 |
error(JSMSG_GARBAGE_AFTER_INPUT, "function body", TokenKindToDesc(tt)); |
| 2410 |
return errorResult(); |
| 2411 |
} |
| 2412 |
|
| 2413 |
if (!CheckParseTree(this->fc_, alloc_, funNode)) { |
| 2414 |
return errorResult(); |
| 2415 |
} |
| 2416 |
|
| 2417 |
ParseNode* node = funNode; |
| 2418 |
// Don't constant-fold inside "use asm" code, as this could create a parse |
| 2419 |
// tree that doesn't type-check as asm.js. |
| 2420 |
if (!pc_->useAsmOrInsideUseAsm()) { |
| 2421 |
if (!FoldConstants(this->fc_, this->parserAtoms(), this->bigInts(), &node, |
| 2422 |
&handler_)) { |
| 2423 |
return errorResult(); |
| 2424 |
} |
| 2425 |
} |
| 2426 |
funNode = &node->as<FunctionNode>(); |
| 2427 |
|
| 2428 |
if (!checkForUndefinedPrivateFields(nullptr)) { |
| 2429 |
return errorResult(); |
| 2430 |
} |
| 2431 |
|
| 2432 |
if (!this->setSourceMapInfo()) { |
| 2433 |
return errorResult(); |
| 2434 |
} |
| 2435 |
|
| 2436 |
return funNode; |
| 2437 |
} |
| 2438 |
|
| 2439 |
template <class ParseHandler, typename Unit> |
| 2440 |
typename ParseHandler::LexicalScopeNodeResult |
| 2441 |
GeneralParser<ParseHandler, Unit>::functionBody(InHandling inHandling, |
| 2442 |
YieldHandling yieldHandling, |
| 2443 |
FunctionSyntaxKind kind, |
| 2444 |
FunctionBodyType type) { |
| 2445 |
MOZ_ASSERT(pc_->isFunctionBox()); |
| 2446 |
|
| 2447 |
#ifdef DEBUG |
| 2448 |
uint32_t startYieldOffset = pc_->lastYieldOffset; |
| 2449 |
#endif |
| 2450 |
|
| 2451 |
Node body; |
| 2452 |
if (type == StatementListBody) { |
| 2453 |
bool inheritedStrict = pc_->sc()->strict(); |
| 2454 |
MOZ_TRY_VAR(body, statementList(yieldHandling)); |
| 2455 |
|
| 2456 |
// When we transitioned from non-strict to strict mode, we need to |
| 2457 |
// validate that all parameter names are valid strict mode names. |
| 2458 |
if (!inheritedStrict && pc_->sc()->strict()) { |
| 2459 |
MOZ_ASSERT(pc_->sc()->hasExplicitUseStrict(), |
| 2460 |
"strict mode should only change when a 'use strict' directive " |
| 2461 |
"is present"); |
| 2462 |
if (!hasValidSimpleStrictParameterNames()) { |
| 2463 |
// Request that this function be reparsed as strict to report |
| 2464 |
// the invalid parameter name at the correct source location. |
| 2465 |
pc_->newDirectives->setStrict(); |
| 2466 |
return errorResult(); |
| 2467 |
} |
| 2468 |
} |
| 2469 |
} else { |
| 2470 |
MOZ_ASSERT(type == ExpressionBody); |
| 2471 |
|
| 2472 |
// Async functions are implemented as generators, and generators are |
| 2473 |
// assumed to be statement lists, to prepend initial `yield`. |
| 2474 |
ListNodeType stmtList = null(); |
| 2475 |
if (pc_->isAsync()) { |
| 2476 |
MOZ_TRY_VAR(stmtList, handler_.newStatementList(pos())); |
| 2477 |
} |
| 2478 |
|
| 2479 |
Node kid; |
| 2480 |
MOZ_TRY_VAR(kid, |
| 2481 |
assignExpr(inHandling, yieldHandling, TripledotProhibited)); |
| 2482 |
|
| 2483 |
MOZ_TRY_VAR(body, handler_.newExpressionBody(kid)); |
| 2484 |
|
| 2485 |
if (pc_->isAsync()) { |
| 2486 |
handler_.addStatementToList(stmtList, body); |
| 2487 |
body = stmtList; |
| 2488 |
} |
| 2489 |
} |
| 2490 |
|
| 2491 |
MOZ_ASSERT_IF(!pc_->isGenerator() && !pc_->isAsync(), |
| 2492 |
pc_->lastYieldOffset == startYieldOffset); |
| 2493 |
MOZ_ASSERT_IF(pc_->isGenerator(), kind != FunctionSyntaxKind::Arrow); |
| 2494 |
MOZ_ASSERT_IF(pc_->isGenerator(), type == StatementListBody); |
| 2495 |
|
| 2496 |
if (pc_->needsDotGeneratorName()) { |
| 2497 |
MOZ_ASSERT_IF(!pc_->isAsync(), type == StatementListBody); |
| 2498 |
if (!pc_->declareDotGeneratorName()) { |
| 2499 |
return errorResult(); |
| 2500 |
} |
| 2501 |
if (pc_->isGenerator()) { |
| 2502 |
NameNodeType generator; |
| 2503 |
MOZ_TRY_VAR(generator, newDotGeneratorName()); |
| 2504 |
if (!handler_.prependInitialYield(handler_.asListNode(body), generator)) { |
| 2505 |
return errorResult(); |
| 2506 |
} |
| 2507 |
} |
| 2508 |
} |
| 2509 |
|
| 2510 |
if (pc_->numberOfArgumentsNames > 0 || kind == FunctionSyntaxKind::Arrow) { |
| 2511 |
MOZ_ASSERT(pc_->isFunctionBox()); |
| 2512 |
pc_->sc()->setIneligibleForArgumentsLength(); |
| 2513 |
} |
| 2514 |
|
| 2515 |
// Declare the 'arguments', 'this', and 'new.target' bindings if necessary |
| 2516 |
// before finishing up the scope so these special bindings get marked as |
| 2517 |
// closed over if necessary. Arrow functions don't have these bindings. |
| 2518 |
if (kind != FunctionSyntaxKind::Arrow) { |
| 2519 |
bool canSkipLazyClosedOverBindings = handler_.reuseClosedOverBindings(); |
| 2520 |
if (!pc_->declareFunctionArgumentsObject(usedNames_, |
| 2521 |
canSkipLazyClosedOverBindings)) { |
| 2522 |
return errorResult(); |
| 2523 |
} |
| 2524 |
if (!pc_->declareFunctionThis(usedNames_, canSkipLazyClosedOverBindings)) { |
| 2525 |
return errorResult(); |
| 2526 |
} |
| 2527 |
if (!pc_->declareNewTarget(usedNames_, canSkipLazyClosedOverBindings)) { |
| 2528 |
return errorResult(); |
| 2529 |
} |
| 2530 |
} |
| 2531 |
|
| 2532 |
return finishLexicalScope(pc_->varScope(), body, ScopeKind::FunctionLexical); |
| 2533 |
} |
| 2534 |
|
| 2535 |
template <class ParseHandler, typename Unit> |
| 2536 |
bool GeneralParser<ParseHandler, Unit>::matchOrInsertSemicolon( |
| 2537 |
Modifier modifier /* = TokenStream::SlashIsRegExp */) { |
| 2538 |
TokenKind tt = TokenKind::Eof; |
| 2539 |
if (!tokenStream.peekTokenSameLine(&tt, modifier)) { |
| 2540 |
return false; |
| 2541 |
} |
| 2542 |
if (tt != TokenKind::Eof && tt != TokenKind::Eol && tt != TokenKind::Semi && |
| 2543 |
tt != TokenKind::RightCurly) { |
| 2544 |
/* |
| 2545 |
* When current token is `await` and it's outside of async function, |
| 2546 |
* it's possibly intended to be an await expression. |
| 2547 |
* |
| 2548 |
* await f(); |
| 2549 |
* ^ |
| 2550 |
* | |
| 2551 |
* tried to insert semicolon here |
| 2552 |
* |
| 2553 |
* Detect this situation and throw an understandable error. Otherwise |
| 2554 |
* we'd throw a confusing "unexpected token: (unexpected token)" error. |
| 2555 |
*/ |
| 2556 |
if (!pc_->isAsync() && anyChars.currentToken().type == TokenKind::Await) { |
| 2557 |
error(JSMSG_AWAIT_OUTSIDE_ASYNC_OR_MODULE); |
| 2558 |
return false; |
| 2559 |
} |
| 2560 |
if (!yieldExpressionsSupported() && |
| 2561 |
anyChars.currentToken().type == TokenKind::Yield) { |
| 2562 |
error(JSMSG_YIELD_OUTSIDE_GENERATOR); |
| 2563 |
return false; |
| 2564 |
} |
| 2565 |
|
| 2566 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 2567 |
if (options().explicitResourceManagement() && |
| 2568 |
anyChars.currentToken().type == TokenKind::Using && |
| 2569 |
!this->pc_->isUsingSyntaxAllowed()) { |
| 2570 |
error(JSMSG_USING_OUTSIDE_BLOCK_OR_MODULE); |
| 2571 |
return false; |
| 2572 |
} |
| 2573 |
#endif |
| 2574 |
|
| 2575 |
/* Advance the scanner for proper error location reporting. */ |
| 2576 |
tokenStream.consumeKnownToken(tt, modifier); |
| 2577 |
error(JSMSG_UNEXPECTED_TOKEN_NO_EXPECT, TokenKindToDesc(tt)); |
| 2578 |
return false; |
| 2579 |
} |
| 2580 |
bool matched; |
| 2581 |
return tokenStream.matchToken(&matched, TokenKind::Semi, modifier); |
| 2582 |
} |
| 2583 |
|
| 2584 |
bool ParserBase::leaveInnerFunction(ParseContext* outerpc) { |
| 2585 |
MOZ_ASSERT(pc_ != outerpc); |
| 2586 |
|
| 2587 |
MOZ_ASSERT_IF(outerpc->isFunctionBox(), |
| 2588 |
outerpc->functionBox()->index() < pc_->functionBox()->index()); |
| 2589 |
|
| 2590 |
// If the current function allows super.property but cannot have a home |
| 2591 |
// object, i.e., it is an arrow function, we need to propagate the flag to |
| 2592 |
// the outer ParseContext. |
| 2593 |
if (pc_->superScopeNeedsHomeObject()) { |
| 2594 |
if (!pc_->isArrowFunction()) { |
| 2595 |
MOZ_ASSERT(pc_->functionBox()->needsHomeObject()); |
| 2596 |
} else { |
| 2597 |
outerpc->setSuperScopeNeedsHomeObject(); |
| 2598 |
} |
| 2599 |
} |
| 2600 |
|
| 2601 |
// Lazy functions inner to another lazy function need to be remembered by |
| 2602 |
// the inner function so that if the outer function is eventually parsed |
| 2603 |
// we do not need any further parsing or processing of the inner function. |
| 2604 |
// |
| 2605 |
// Append the inner function index here unconditionally; the vector is only |
| 2606 |
// used if the Parser using outerpc is a syntax parsing. See |
| 2607 |
// GeneralParser<SyntaxParseHandler>::finishFunction. |
| 2608 |
if (!outerpc->innerFunctionIndexesForLazy.append( |
| 2609 |
pc_->functionBox()->index())) { |
| 2610 |
return false; |
| 2611 |
} |
| 2612 |
|
| 2613 |
PropagateTransitiveParseFlags(pc_->functionBox(), outerpc->sc()); |
| 2614 |
|
| 2615 |
return true; |
| 2616 |
} |
| 2617 |
|
| 2618 |
TaggedParserAtomIndex ParserBase::prefixAccessorName( |
| 2619 |
PropertyType propType, TaggedParserAtomIndex propAtom) { |
| 2620 |
StringBuilder prefixed(fc_); |
| 2621 |
if (propType == PropertyType::Setter) { |
| 2622 |
if (!prefixed.append("set ")) { |
| 2623 |
return TaggedParserAtomIndex::null(); |
| 2624 |
} |
| 2625 |
} else { |
| 2626 |
if (!prefixed.append("get ")) { |
| 2627 |
return TaggedParserAtomIndex::null(); |
| 2628 |
} |
| 2629 |
} |
| 2630 |
if (!prefixed.append(this->parserAtoms(), propAtom)) { |
| 2631 |
return TaggedParserAtomIndex::null(); |
| 2632 |
} |
| 2633 |
return prefixed.finishParserAtom(this->parserAtoms(), fc_); |
| 2634 |
} |
| 2635 |
|
| 2636 |
template <class ParseHandler, typename Unit> |
| 2637 |
void GeneralParser<ParseHandler, Unit>::setFunctionStartAtPosition( |
| 2638 |
FunctionBox* funbox, TokenPos pos) const { |
| 2639 |
uint32_t startLine; |
| 2640 |
JS::LimitedColumnNumberOneOrigin startColumn; |
| 2641 |
tokenStream.computeLineAndColumn(pos.begin, &startLine, &startColumn); |
| 2642 |
|
| 2643 |
// NOTE: `Debugger::CallData::findScripts` relies on sourceStart and |
| 2644 |
// lineno/column referring to the same location. |
| 2645 |
funbox->setStart(pos.begin, startLine, startColumn); |
| 2646 |
} |
| 2647 |
|
| 2648 |
template <class ParseHandler, typename Unit> |
| 2649 |
void GeneralParser<ParseHandler, Unit>::setFunctionStartAtCurrentToken( |
| 2650 |
FunctionBox* funbox) const { |
| 2651 |
setFunctionStartAtPosition(funbox, anyChars.currentToken().pos); |
| 2652 |
} |
| 2653 |
|
| 2654 |
template <class ParseHandler, typename Unit> |
| 2655 |
bool GeneralParser<ParseHandler, Unit>::functionArguments( |
| 2656 |
YieldHandling yieldHandling, FunctionSyntaxKind kind, |
| 2657 |
FunctionNodeType funNode) { |
| 2658 |
FunctionBox* funbox = pc_->functionBox(); |
| 2659 |
|
| 2660 |
// Modifier for the following tokens. |
| 2661 |
// TokenStream::SlashIsDiv for the following cases: |
| 2662 |
// async a => 1 |
| 2663 |
// ^ |
| 2664 |
// |
| 2665 |
// (a) => 1 |
| 2666 |
// ^ |
| 2667 |
// |
| 2668 |
// async (a) => 1 |
| 2669 |
// ^ |
| 2670 |
// |
| 2671 |
// function f(a) {} |
| 2672 |
// ^ |
| 2673 |
// |
| 2674 |
// TokenStream::SlashIsRegExp for the following case: |
| 2675 |
// a => 1 |
| 2676 |
// ^ |
| 2677 |
Modifier firstTokenModifier = |
| 2678 |
kind != FunctionSyntaxKind::Arrow || funbox->isAsync() |
| 2679 |
? TokenStream::SlashIsDiv |
| 2680 |
: TokenStream::SlashIsRegExp; |
| 2681 |
TokenKind tt; |
| 2682 |
if (!tokenStream.getToken(&tt, firstTokenModifier)) { |
| 2683 |
return false; |
| 2684 |
} |
| 2685 |
|
| 2686 |
if (kind == FunctionSyntaxKind::Arrow && TokenKindIsPossibleIdentifier(tt)) { |
| 2687 |
// Record the start of function source (for FunctionToString). |
| 2688 |
setFunctionStartAtCurrentToken(funbox); |
| 2689 |
|
| 2690 |
ParamsBodyNodeType argsbody; |
| 2691 |
MOZ_TRY_VAR_OR_RETURN(argsbody, handler_.newParamsBody(pos()), false); |
| 2692 |
handler_.setFunctionFormalParametersAndBody(funNode, argsbody); |
| 2693 |
|
| 2694 |
TaggedParserAtomIndex name = bindingIdentifier(yieldHandling); |
| 2695 |
if (!name) { |
| 2696 |
return false; |
| 2697 |
} |
| 2698 |
|
| 2699 |
constexpr bool disallowDuplicateParams = true; |
| 2700 |
bool duplicatedParam = false; |
| 2701 |
if (!notePositionalFormalParameter(funNode, name, pos().begin, |
| 2702 |
disallowDuplicateParams, |
| 2703 |
&duplicatedParam)) { |
| 2704 |
return false; |
| 2705 |
} |
| 2706 |
MOZ_ASSERT(!duplicatedParam); |
| 2707 |
MOZ_ASSERT(pc_->positionalFormalParameterNames().length() == 1); |
| 2708 |
|
| 2709 |
funbox->setLength(1); |
| 2710 |
funbox->setArgCount(1); |
| 2711 |
return true; |
| 2712 |
} |
| 2713 |
|
| 2714 |
if (tt != TokenKind::LeftParen) { |
| 2715 |
error(kind == FunctionSyntaxKind::Arrow ? JSMSG_BAD_ARROW_ARGS |
| 2716 |
: JSMSG_PAREN_BEFORE_FORMAL); |
| 2717 |
return false; |
| 2718 |
} |
| 2719 |
|
| 2720 |
// Record the start of function source (for FunctionToString). |
| 2721 |
setFunctionStartAtCurrentToken(funbox); |
| 2722 |
|
| 2723 |
ParamsBodyNodeType argsbody; |
| 2724 |
MOZ_TRY_VAR_OR_RETURN(argsbody, handler_.newParamsBody(pos()), false); |
| 2725 |
handler_.setFunctionFormalParametersAndBody(funNode, argsbody); |
| 2726 |
|
| 2727 |
bool matched; |
| 2728 |
if (!tokenStream.matchToken(&matched, TokenKind::RightParen, |
| 2729 |
TokenStream::SlashIsRegExp)) { |
| 2730 |
return false; |
| 2731 |
} |
| 2732 |
if (!matched) { |
| 2733 |
bool hasRest = false; |
| 2734 |
bool hasDefault = false; |
| 2735 |
bool duplicatedParam = false; |
| 2736 |
bool disallowDuplicateParams = |
| 2737 |
kind == FunctionSyntaxKind::Arrow || |
| 2738 |
kind == FunctionSyntaxKind::Method || |
| 2739 |
kind == FunctionSyntaxKind::FieldInitializer || |
| 2740 |
kind == FunctionSyntaxKind::ClassConstructor; |
| 2741 |
AtomVector& positionalFormals = pc_->positionalFormalParameterNames(); |
| 2742 |
|
| 2743 |
if (kind == FunctionSyntaxKind::Getter) { |
| 2744 |
error(JSMSG_ACCESSOR_WRONG_ARGS, "getter", "no", "s"); |
| 2745 |
return false; |
| 2746 |
} |
| 2747 |
|
| 2748 |
while (true) { |
| 2749 |
if (hasRest) { |
| 2750 |
error(JSMSG_PARAMETER_AFTER_REST); |
| 2751 |
return false; |
| 2752 |
} |
| 2753 |
|
| 2754 |
TokenKind tt; |
| 2755 |
if (!tokenStream.getToken(&tt, TokenStream::SlashIsRegExp)) { |
| 2756 |
return false; |
| 2757 |
} |
| 2758 |
|
| 2759 |
if (tt == TokenKind::TripleDot) { |
| 2760 |
if (kind == FunctionSyntaxKind::Setter) { |
| 2761 |
error(JSMSG_ACCESSOR_WRONG_ARGS, "setter", "one", ""); |
| 2762 |
return false; |
| 2763 |
} |
| 2764 |
|
| 2765 |
disallowDuplicateParams = true; |
| 2766 |
if (duplicatedParam) { |
| 2767 |
// Has duplicated args before the rest parameter. |
| 2768 |
error(JSMSG_BAD_DUP_ARGS); |
| 2769 |
return false; |
| 2770 |
} |
| 2771 |
|
| 2772 |
hasRest = true; |
| 2773 |
funbox->setHasRest(); |
| 2774 |
|
| 2775 |
if (!tokenStream.getToken(&tt)) { |
| 2776 |
return false; |
| 2777 |
} |
| 2778 |
|
| 2779 |
if (!TokenKindIsPossibleIdentifier(tt) && |
| 2780 |
tt != TokenKind::LeftBracket && tt != TokenKind::LeftCurly) { |
| 2781 |
error(JSMSG_NO_REST_NAME); |
| 2782 |
return false; |
| 2783 |
} |
| 2784 |
} |
| 2785 |
|
| 2786 |
switch (tt) { |
| 2787 |
case TokenKind::LeftBracket: |
| 2788 |
case TokenKind::LeftCurly: { |
| 2789 |
disallowDuplicateParams = true; |
| 2790 |
if (duplicatedParam) { |
| 2791 |
// Has duplicated args before the destructuring parameter. |
| 2792 |
error(JSMSG_BAD_DUP_ARGS); |
| 2793 |
return false; |
| 2794 |
} |
| 2795 |
|
| 2796 |
funbox->hasDestructuringArgs = true; |
| 2797 |
|
| 2798 |
Node destruct; |
| 2799 |
MOZ_TRY_VAR_OR_RETURN( |
| 2800 |
destruct, |
| 2801 |
destructuringDeclarationWithoutYieldOrAwait( |
| 2802 |
DeclarationKind::FormalParameter, yieldHandling, tt), |
| 2803 |
false); |
| 2804 |
|
| 2805 |
if (!noteDestructuredPositionalFormalParameter(funNode, destruct)) { |
| 2806 |
return false; |
| 2807 |
} |
| 2808 |
|
| 2809 |
break; |
| 2810 |
} |
| 2811 |
|
| 2812 |
default: { |
| 2813 |
if (!TokenKindIsPossibleIdentifier(tt)) { |
| 2814 |
error(JSMSG_MISSING_FORMAL); |
| 2815 |
return false; |
| 2816 |
} |
| 2817 |
|
| 2818 |
TaggedParserAtomIndex name = bindingIdentifier(yieldHandling); |
| 2819 |
if (!name) { |
| 2820 |
return false; |
| 2821 |
} |
| 2822 |
|
| 2823 |
if (!notePositionalFormalParameter(funNode, name, pos().begin, |
| 2824 |
disallowDuplicateParams, |
| 2825 |
&duplicatedParam)) { |
| 2826 |
return false; |
| 2827 |
} |
| 2828 |
if (duplicatedParam) { |
| 2829 |
funbox->hasDuplicateParameters = true; |
| 2830 |
} |
| 2831 |
|
| 2832 |
break; |
| 2833 |
} |
| 2834 |
} |
| 2835 |
|
| 2836 |
if (positionalFormals.length() >= ARGNO_LIMIT) { |
| 2837 |
error(JSMSG_TOO_MANY_FUN_ARGS); |
| 2838 |
return false; |
| 2839 |
} |
| 2840 |
|
| 2841 |
bool matched; |
| 2842 |
if (!tokenStream.matchToken(&matched, TokenKind::Assign, |
| 2843 |
TokenStream::SlashIsRegExp)) { |
| 2844 |
return false; |
| 2845 |
} |
| 2846 |
if (matched) { |
| 2847 |
if (hasRest) { |
| 2848 |
error(JSMSG_REST_WITH_DEFAULT); |
| 2849 |
return false; |
| 2850 |
} |
| 2851 |
disallowDuplicateParams = true; |
| 2852 |
if (duplicatedParam) { |
| 2853 |
error(JSMSG_BAD_DUP_ARGS); |
| 2854 |
return false; |
| 2855 |
} |
| 2856 |
|
| 2857 |
if (!hasDefault) { |
| 2858 |
hasDefault = true; |
| 2859 |
|
| 2860 |
// The Function.length property is the number of formals |
| 2861 |
// before the first default argument. |
| 2862 |
funbox->setLength(positionalFormals.length() - 1); |
| 2863 |
} |
| 2864 |
funbox->hasParameterExprs = true; |
| 2865 |
|
| 2866 |
Node def_expr; |
| 2867 |
MOZ_TRY_VAR_OR_RETURN( |
| 2868 |
def_expr, assignExprWithoutYieldOrAwait(yieldHandling), false); |
| 2869 |
if (!handler_.setLastFunctionFormalParameterDefault(funNode, |
| 2870 |
def_expr)) { |
| 2871 |
return false; |
| 2872 |
} |
| 2873 |
} |
| 2874 |
|
| 2875 |
// Setter syntax uniquely requires exactly one argument. |
| 2876 |
if (kind == FunctionSyntaxKind::Setter) { |
| 2877 |
break; |
| 2878 |
} |
| 2879 |
|
| 2880 |
if (!tokenStream.matchToken(&matched, TokenKind::Comma, |
| 2881 |
TokenStream::SlashIsRegExp)) { |
| 2882 |
return false; |
| 2883 |
} |
| 2884 |
if (!matched) { |
| 2885 |
break; |
| 2886 |
} |
| 2887 |
|
| 2888 |
if (!hasRest) { |
| 2889 |
if (!tokenStream.peekToken(&tt, TokenStream::SlashIsRegExp)) { |
| 2890 |
return false; |
| 2891 |
} |
| 2892 |
if (tt == TokenKind::RightParen) { |
| 2893 |
break; |
| 2894 |
} |
| 2895 |
} |
| 2896 |
} |
| 2897 |
|
| 2898 |
TokenKind tt; |
| 2899 |
if (!tokenStream.getToken(&tt, TokenStream::SlashIsRegExp)) { |
| 2900 |
return false; |
| 2901 |
} |
| 2902 |
if (tt != TokenKind::RightParen) { |
| 2903 |
if (kind == FunctionSyntaxKind::Setter) { |
| 2904 |
error(JSMSG_ACCESSOR_WRONG_ARGS, "setter", "one", ""); |
| 2905 |
return false; |
| 2906 |
} |
| 2907 |
|
| 2908 |
error(JSMSG_PAREN_AFTER_FORMAL); |
| 2909 |
return false; |
| 2910 |
} |
| 2911 |
|
| 2912 |
if (!hasDefault) { |
| 2913 |
funbox->setLength(positionalFormals.length() - hasRest); |
| 2914 |
} |
| 2915 |
|
| 2916 |
funbox->setArgCount(positionalFormals.length()); |
| 2917 |
} else if (kind == FunctionSyntaxKind::Setter) { |
| 2918 |
error(JSMSG_ACCESSOR_WRONG_ARGS, "setter", "one", ""); |
| 2919 |
return false; |
| 2920 |
} |
| 2921 |
|
| 2922 |
return true; |
| 2923 |
} |
| 2924 |
|
| 2925 |
template <typename Unit> |
| 2926 |
bool Parser<FullParseHandler, Unit>::skipLazyInnerFunction( |
| 2927 |
FunctionNode* funNode, uint32_t toStringStart, bool tryAnnexB) { |
| 2928 |
// When a lazily-parsed function is called, we only fully parse (and emit) |
| 2929 |
// that function, not any of its nested children. The initial syntax-only |
| 2930 |
// parse recorded the free variables of nested functions and their extents, |
| 2931 |
// so we can skip over them after accounting for their free variables. |
| 2932 |
|
| 2933 |
MOZ_ASSERT(pc_->isOutermostOfCurrentCompile()); |
| 2934 |
handler_.nextLazyInnerFunction(); |
| 2935 |
const ScriptStencil& cachedData = handler_.cachedScriptData(); |
| 2936 |
const ScriptStencilExtra& cachedExtra = handler_.cachedScriptExtra(); |
| 2937 |
MOZ_ASSERT(toStringStart == cachedExtra.extent.toStringStart); |
| 2938 |
|
| 2939 |
FunctionBox* funbox = newFunctionBox(funNode, cachedData, cachedExtra); |
| 2940 |
if (!funbox) { |
| 2941 |
return false; |
| 2942 |
} |
| 2943 |
|
| 2944 |
ScriptStencil& script = funbox->functionStencil(); |
| 2945 |
funbox->copyFunctionFields(script); |
| 2946 |
|
| 2947 |
// If the inner lazy function is class constructor, connect it to the class |
| 2948 |
// statement/expression we are parsing. |
| 2949 |
if (funbox->isClassConstructor()) { |
| 2950 |
auto classStmt = |
| 2951 |
pc_->template findInnermostStatement<ParseContext::ClassStatement>(); |
| 2952 |
MOZ_ASSERT(!classStmt->constructorBox); |
| 2953 |
classStmt->constructorBox = funbox; |
| 2954 |
} |
| 2955 |
|
| 2956 |
MOZ_ASSERT_IF(pc_->isFunctionBox(), |
| 2957 |
pc_->functionBox()->index() < funbox->index()); |
| 2958 |
|
| 2959 |
PropagateTransitiveParseFlags(funbox, pc_->sc()); |
| 2960 |
|
| 2961 |
if (!tokenStream.advance(funbox->extent().sourceEnd)) { |
| 2962 |
return false; |
| 2963 |
} |
| 2964 |
|
| 2965 |
// Append possible Annex B function box only upon successfully parsing. |
| 2966 |
if (tryAnnexB && |
| 2967 |
!pc_->innermostScope()->addPossibleAnnexBFunctionBox(pc_, funbox)) { |
| 2968 |
return false; |
| 2969 |
} |
| 2970 |
|
| 2971 |
return true; |
| 2972 |
} |
| 2973 |
|
| 2974 |
template <typename Unit> |
| 2975 |
bool Parser<SyntaxParseHandler, Unit>::skipLazyInnerFunction( |
| 2976 |
FunctionNodeType funNode, uint32_t toStringStart, bool tryAnnexB) { |
| 2977 |
MOZ_CRASH("Cannot skip lazy inner functions when syntax parsing"); |
| 2978 |
} |
| 2979 |
|
| 2980 |
template <class ParseHandler, typename Unit> |
| 2981 |
bool GeneralParser<ParseHandler, Unit>::skipLazyInnerFunction( |
| 2982 |
FunctionNodeType funNode, uint32_t toStringStart, bool tryAnnexB) { |
| 2983 |
return asFinalParser()->skipLazyInnerFunction(funNode, toStringStart, |
| 2984 |
tryAnnexB); |
| 2985 |
} |
| 2986 |
|
| 2987 |
template <class ParseHandler, typename Unit> |
| 2988 |
bool GeneralParser<ParseHandler, Unit>::addExprAndGetNextTemplStrToken( |
| 2989 |
YieldHandling yieldHandling, ListNodeType nodeList, TokenKind* ttp) { |
| 2990 |
Node pn; |
| 2991 |
MOZ_TRY_VAR_OR_RETURN(pn, expr(InAllowed, yieldHandling, TripledotProhibited), |
| 2992 |
false); |
| 2993 |
handler_.addList(nodeList, pn); |
| 2994 |
|
| 2995 |
TokenKind tt; |
| 2996 |
if (!tokenStream.getToken(&tt, TokenStream::SlashIsRegExp)) { |
| 2997 |
return false; |
| 2998 |
} |
| 2999 |
if (tt != TokenKind::RightCurly) { |
| 3000 |
error(JSMSG_TEMPLSTR_UNTERM_EXPR); |
| 3001 |
return false; |
| 3002 |
} |
| 3003 |
|
| 3004 |
return tokenStream.getTemplateToken(ttp); |
| 3005 |
} |
| 3006 |
|
| 3007 |
template <class ParseHandler, typename Unit> |
| 3008 |
bool GeneralParser<ParseHandler, Unit>::taggedTemplate( |
| 3009 |
YieldHandling yieldHandling, ListNodeType tagArgsList, TokenKind tt) { |
| 3010 |
CallSiteNodeType callSiteObjNode; |
| 3011 |
MOZ_TRY_VAR_OR_RETURN(callSiteObjNode, |
| 3012 |
handler_.newCallSiteObject(pos().begin), false); |
| 3013 |
handler_.addList(tagArgsList, callSiteObjNode); |
| 3014 |
|
| 3015 |
pc_->sc()->setHasCallSiteObj(); |
| 3016 |
|
| 3017 |
while (true) { |
| 3018 |
if (!appendToCallSiteObj(callSiteObjNode)) { |
| 3019 |
return false; |
| 3020 |
} |
| 3021 |
if (tt != TokenKind::TemplateHead) { |
| 3022 |
break; |
| 3023 |
} |
| 3024 |
|
| 3025 |
if (!addExprAndGetNextTemplStrToken(yieldHandling, tagArgsList, &tt)) { |
| 3026 |
return false; |
| 3027 |
} |
| 3028 |
} |
| 3029 |
handler_.setEndPosition(tagArgsList, callSiteObjNode); |
| 3030 |
return true; |
| 3031 |
} |
| 3032 |
|
| 3033 |
template <class ParseHandler, typename Unit> |
| 3034 |
typename ParseHandler::ListNodeResult |
| 3035 |
GeneralParser<ParseHandler, Unit>::templateLiteral( |
| 3036 |
YieldHandling yieldHandling) { |
| 3037 |
NameNodeType literal; |
| 3038 |
MOZ_TRY_VAR(literal, noSubstitutionUntaggedTemplate()); |
| 3039 |
|
| 3040 |
ListNodeType nodeList; |
| 3041 |
MOZ_TRY_VAR(nodeList, |
| 3042 |
handler_.newList(ParseNodeKind::TemplateStringListExpr, literal)); |
| 3043 |
|
| 3044 |
TokenKind tt; |
| 3045 |
do { |
| 3046 |
if (!addExprAndGetNextTemplStrToken(yieldHandling, nodeList, &tt)) { |
| 3047 |
return errorResult(); |
| 3048 |
} |
| 3049 |
|
| 3050 |
MOZ_TRY_VAR(literal, noSubstitutionUntaggedTemplate()); |
| 3051 |
|
| 3052 |
handler_.addList(nodeList, literal); |
| 3053 |
} while (tt == TokenKind::TemplateHead); |
| 3054 |
return nodeList; |
| 3055 |
} |
| 3056 |
|
| 3057 |
template <class ParseHandler, typename Unit> |
| 3058 |
typename ParseHandler::FunctionNodeResult |
| 3059 |
GeneralParser<ParseHandler, Unit>::functionDefinition( |
| 3060 |
FunctionNodeType funNode, uint32_t toStringStart, InHandling inHandling, |
| 3061 |
YieldHandling yieldHandling, TaggedParserAtomIndex funName, |
| 3062 |
FunctionSyntaxKind kind, GeneratorKind generatorKind, |
| 3063 |
FunctionAsyncKind asyncKind, bool tryAnnexB /* = false */) { |
| 3064 |
MOZ_ASSERT_IF(kind == FunctionSyntaxKind::Statement, funName); |
| 3065 |
|
| 3066 |
// If we see any inner function, note it on our current context. The bytecode |
| 3067 |
// emitter may eliminate the function later, but we use a conservative |
| 3068 |
// definition for consistency between lazy and full parsing. |
| 3069 |
pc_->sc()->setHasInnerFunctions(); |
| 3070 |
|
| 3071 |
// When fully parsing a lazy script, we do not fully reparse its inner |
| 3072 |
// functions, which are also lazy. Instead, their free variables and source |
| 3073 |
// extents are recorded and may be skipped. |
| 3074 |
if (handler_.reuseLazyInnerFunctions()) { |
| 3075 |
if (!skipLazyInnerFunction(funNode, toStringStart, tryAnnexB)) { |
| 3076 |
return errorResult(); |
| 3077 |
} |
| 3078 |
|
| 3079 |
return funNode; |
| 3080 |
} |
| 3081 |
|
| 3082 |
bool isSelfHosting = options().selfHostingMode; |
| 3083 |
FunctionFlags flags = |
| 3084 |
InitialFunctionFlags(kind, generatorKind, asyncKind, isSelfHosting); |
| 3085 |
|
| 3086 |
// Self-hosted functions with special function names require extended slots |
| 3087 |
// for various purposes. |
| 3088 |
bool forceExtended = |
| 3089 |
isSelfHosting && funName && |
| 3090 |
this->parserAtoms().isExtendedUnclonedSelfHostedFunctionName(funName); |
| 3091 |
if (forceExtended) { |
| 3092 |
flags.setIsExtended(); |
| 3093 |
} |
| 3094 |
|
| 3095 |
// Speculatively parse using the directives of the parent parsing context. |
| 3096 |
// If a directive is encountered (e.g., "use strict") that changes how the |
| 3097 |
// function should have been parsed, we backup and reparse with the new set |
| 3098 |
// of directives. |
| 3099 |
Directives directives(pc_); |
| 3100 |
Directives newDirectives = directives; |
| 3101 |
|
| 3102 |
Position start(tokenStream); |
| 3103 |
auto startObj = this->compilationState_.getPosition(); |
| 3104 |
|
| 3105 |
// Parse the inner function. The following is a loop as we may attempt to |
| 3106 |
// reparse a function due to failed syntax parsing and encountering new |
| 3107 |
// "use foo" directives. |
| 3108 |
while (true) { |
| 3109 |
if (trySyntaxParseInnerFunction(&funNode, funName, flags, toStringStart, |
| 3110 |
inHandling, yieldHandling, kind, |
| 3111 |
generatorKind, asyncKind, tryAnnexB, |
| 3112 |
directives, &newDirectives)) { |
| 3113 |
break; |
| 3114 |
} |
| 3115 |
|
| 3116 |
// Return on error. |
| 3117 |
if (anyChars.hadError() || directives == newDirectives) { |
| 3118 |
return errorResult(); |
| 3119 |
} |
| 3120 |
|
| 3121 |
// Assignment must be monotonic to prevent infinitely attempting to |
| 3122 |
// reparse. |
| 3123 |
MOZ_ASSERT_IF(directives.strict(), newDirectives.strict()); |
| 3124 |
MOZ_ASSERT_IF(directives.asmJS(), newDirectives.asmJS()); |
| 3125 |
directives = newDirectives; |
| 3126 |
|
| 3127 |
// Rewind to retry parsing with new directives applied. |
| 3128 |
tokenStream.rewind(start); |
| 3129 |
this->compilationState_.rewind(startObj); |
| 3130 |
|
| 3131 |
// functionFormalParametersAndBody may have already set body before failing. |
| 3132 |
handler_.setFunctionFormalParametersAndBody(funNode, null()); |
| 3133 |
} |
| 3134 |
|
| 3135 |
return funNode; |
| 3136 |
} |
| 3137 |
|
| 3138 |
template <typename Unit> |
| 3139 |
bool Parser<FullParseHandler, Unit>::advancePastSyntaxParsedFunction( |
| 3140 |
SyntaxParser* syntaxParser) { |
| 3141 |
MOZ_ASSERT(getSyntaxParser() == syntaxParser); |
| 3142 |
|
| 3143 |
// Advance this parser over tokens processed by the syntax parser. |
| 3144 |
Position currentSyntaxPosition(syntaxParser->tokenStream); |
| 3145 |
if (!tokenStream.fastForward(currentSyntaxPosition, syntaxParser->anyChars)) { |
| 3146 |
return false; |
| 3147 |
} |
| 3148 |
|
| 3149 |
anyChars.adoptState(syntaxParser->anyChars); |
| 3150 |
tokenStream.adoptState(syntaxParser->tokenStream); |
| 3151 |
return true; |
| 3152 |
} |
| 3153 |
|
| 3154 |
template <typename Unit> |
| 3155 |
bool Parser<FullParseHandler, Unit>::trySyntaxParseInnerFunction( |
| 3156 |
FunctionNode** funNode, TaggedParserAtomIndex explicitName, |
| 3157 |
FunctionFlags flags, uint32_t toStringStart, InHandling inHandling, |
| 3158 |
YieldHandling yieldHandling, FunctionSyntaxKind kind, |
| 3159 |
GeneratorKind generatorKind, FunctionAsyncKind asyncKind, bool tryAnnexB, |
| 3160 |
Directives inheritedDirectives, Directives* newDirectives) { |
| 3161 |
// Try a syntax parse for this inner function. |
| 3162 |
do { |
| 3163 |
// If we're assuming this function is an IIFE, always perform a full |
| 3164 |
// parse to avoid the overhead of a lazy syntax-only parse. Although |
| 3165 |
// the prediction may be incorrect, IIFEs are common enough that it |
| 3166 |
// pays off for lots of code. |
| 3167 |
if ((*funNode)->isLikelyIIFE() && |
| 3168 |
generatorKind == GeneratorKind::NotGenerator && |
| 3169 |
asyncKind == FunctionAsyncKind::SyncFunction) { |
| 3170 |
break; |
| 3171 |
} |
| 3172 |
|
| 3173 |
SyntaxParser* syntaxParser = getSyntaxParser(); |
| 3174 |
if (!syntaxParser) { |
| 3175 |
break; |
| 3176 |
} |
| 3177 |
|
| 3178 |
UsedNameTracker::RewindToken token = usedNames_.getRewindToken(); |
| 3179 |
auto statePosition = this->compilationState_.getPosition(); |
| 3180 |
|
| 3181 |
// Move the syntax parser to the current position in the stream. In the |
| 3182 |
// common case this seeks forward, but it'll also seek backward *at least* |
| 3183 |
// when arrow functions appear inside arrow function argument defaults |
| 3184 |
// (because we rewind to reparse arrow functions once we're certain they're |
| 3185 |
// arrow functions): |
| 3186 |
// |
| 3187 |
// var x = (y = z => 2) => q; |
| 3188 |
// // ^ we first seek to here to syntax-parse this function |
| 3189 |
// // ^ then we seek back to here to syntax-parse the outer function |
| 3190 |
Position currentPosition(tokenStream); |
| 3191 |
if (!syntaxParser->tokenStream.seekTo(currentPosition, anyChars)) { |
| 3192 |
return false; |
| 3193 |
} |
| 3194 |
|
| 3195 |
// Make a FunctionBox before we enter the syntax parser, because |pn| |
| 3196 |
// still expects a FunctionBox to be attached to it during BCE, and |
| 3197 |
// the syntax parser cannot attach one to it. |
| 3198 |
FunctionBox* funbox = |
| 3199 |
newFunctionBox(*funNode, explicitName, flags, toStringStart, |
| 3200 |
inheritedDirectives, generatorKind, asyncKind); |
| 3201 |
if (!funbox) { |
| 3202 |
return false; |
| 3203 |
} |
| 3204 |
funbox->initWithEnclosingParseContext(pc_, kind); |
| 3205 |
|
| 3206 |
auto syntaxNodeResult = syntaxParser->innerFunctionForFunctionBox( |
| 3207 |
SyntaxParseHandler::Node::NodeGeneric, pc_, funbox, inHandling, |
| 3208 |
yieldHandling, kind, newDirectives); |
| 3209 |
if (syntaxNodeResult.isErr()) { |
| 3210 |
if (syntaxParser->hadAbortedSyntaxParse()) { |
| 3211 |
// Try again with a full parse. UsedNameTracker needs to be |
| 3212 |
// rewound to just before we tried the syntax parse for |
| 3213 |
// correctness. |
| 3214 |
syntaxParser->clearAbortedSyntaxParse(); |
| 3215 |
usedNames_.rewind(token); |
| 3216 |
this->compilationState_.rewind(statePosition); |
| 3217 |
MOZ_ASSERT(!fc_->hadErrors()); |
| 3218 |
break; |
| 3219 |
} |
| 3220 |
return false; |
| 3221 |
} |
| 3222 |
|
| 3223 |
if (!advancePastSyntaxParsedFunction(syntaxParser)) { |
| 3224 |
return false; |
| 3225 |
} |
| 3226 |
|
| 3227 |
// Update the end position of the parse node. |
| 3228 |
(*funNode)->pn_pos.end = anyChars.currentToken().pos.end; |
| 3229 |
|
| 3230 |
// Append possible Annex B function box only upon successfully parsing. |
| 3231 |
if (tryAnnexB) { |
| 3232 |
if (!pc_->innermostScope()->addPossibleAnnexBFunctionBox(pc_, funbox)) { |
| 3233 |
return false; |
| 3234 |
} |
| 3235 |
} |
| 3236 |
|
| 3237 |
return true; |
| 3238 |
} while (false); |
| 3239 |
|
| 3240 |
// We failed to do a syntax parse above, so do the full parse. |
| 3241 |
FunctionNodeType innerFunc; |
| 3242 |
MOZ_TRY_VAR_OR_RETURN( |
| 3243 |
innerFunc, |
| 3244 |
innerFunction(*funNode, pc_, explicitName, flags, toStringStart, |
| 3245 |
inHandling, yieldHandling, kind, generatorKind, asyncKind, |
| 3246 |
tryAnnexB, inheritedDirectives, newDirectives), |
| 3247 |
false); |
| 3248 |
|
| 3249 |
*funNode = innerFunc; |
| 3250 |
return true; |
| 3251 |
} |
| 3252 |
|
| 3253 |
template <typename Unit> |
| 3254 |
bool Parser<SyntaxParseHandler, Unit>::trySyntaxParseInnerFunction( |
| 3255 |
FunctionNodeType* funNode, TaggedParserAtomIndex explicitName, |
| 3256 |
FunctionFlags flags, uint32_t toStringStart, InHandling inHandling, |
| 3257 |
YieldHandling yieldHandling, FunctionSyntaxKind kind, |
| 3258 |
GeneratorKind generatorKind, FunctionAsyncKind asyncKind, bool tryAnnexB, |
| 3259 |
Directives inheritedDirectives, Directives* newDirectives) { |
| 3260 |
// This is already a syntax parser, so just parse the inner function. |
| 3261 |
FunctionNodeType innerFunc; |
| 3262 |
MOZ_TRY_VAR_OR_RETURN( |
| 3263 |
innerFunc, |
| 3264 |
innerFunction(*funNode, pc_, explicitName, flags, toStringStart, |
| 3265 |
inHandling, yieldHandling, kind, generatorKind, asyncKind, |
| 3266 |
tryAnnexB, inheritedDirectives, newDirectives), |
| 3267 |
false); |
| 3268 |
|
| 3269 |
*funNode = innerFunc; |
| 3270 |
return true; |
| 3271 |
} |
| 3272 |
|
| 3273 |
template <class ParseHandler, typename Unit> |
| 3274 |
inline bool GeneralParser<ParseHandler, Unit>::trySyntaxParseInnerFunction( |
| 3275 |
FunctionNodeType* funNode, TaggedParserAtomIndex explicitName, |
| 3276 |
FunctionFlags flags, uint32_t toStringStart, InHandling inHandling, |
| 3277 |
YieldHandling yieldHandling, FunctionSyntaxKind kind, |
| 3278 |
GeneratorKind generatorKind, FunctionAsyncKind asyncKind, bool tryAnnexB, |
| 3279 |
Directives inheritedDirectives, Directives* newDirectives) { |
| 3280 |
return asFinalParser()->trySyntaxParseInnerFunction( |
| 3281 |
funNode, explicitName, flags, toStringStart, inHandling, yieldHandling, |
| 3282 |
kind, generatorKind, asyncKind, tryAnnexB, inheritedDirectives, |
| 3283 |
newDirectives); |
| 3284 |
} |
| 3285 |
|
| 3286 |
template <class ParseHandler, typename Unit> |
| 3287 |
typename ParseHandler::FunctionNodeResult |
| 3288 |
GeneralParser<ParseHandler, Unit>::innerFunctionForFunctionBox( |
| 3289 |
FunctionNodeType funNode, ParseContext* outerpc, FunctionBox* funbox, |
| 3290 |
InHandling inHandling, YieldHandling yieldHandling, FunctionSyntaxKind kind, |
| 3291 |
Directives* newDirectives) { |
| 3292 |
// Note that it is possible for outerpc != this->pc_, as we may be |
| 3293 |
// attempting to syntax parse an inner function from an outer full |
| 3294 |
// parser. In that case, outerpc is a SourceParseContext from the full parser |
| 3295 |
// instead of the current top of the stack of the syntax parser. |
| 3296 |
|
| 3297 |
// Push a new ParseContext. |
| 3298 |
SourceParseContext funpc(this, funbox, newDirectives); |
| 3299 |
if (!funpc.init()) { |
| 3300 |
return errorResult(); |
| 3301 |
} |
| 3302 |
|
| 3303 |
if (!functionFormalParametersAndBody(inHandling, yieldHandling, &funNode, |
| 3304 |
kind)) { |
| 3305 |
return errorResult(); |
| 3306 |
} |
| 3307 |
|
| 3308 |
if (!leaveInnerFunction(outerpc)) { |
| 3309 |
return errorResult(); |
| 3310 |
} |
| 3311 |
|
| 3312 |
return funNode; |
| 3313 |
} |
| 3314 |
|
| 3315 |
template <class ParseHandler, typename Unit> |
| 3316 |
typename ParseHandler::FunctionNodeResult |
| 3317 |
GeneralParser<ParseHandler, Unit>::innerFunction( |
| 3318 |
FunctionNodeType funNode, ParseContext* outerpc, |
| 3319 |
TaggedParserAtomIndex explicitName, FunctionFlags flags, |
| 3320 |
uint32_t toStringStart, InHandling inHandling, YieldHandling yieldHandling, |
| 3321 |
FunctionSyntaxKind kind, GeneratorKind generatorKind, |
| 3322 |
FunctionAsyncKind asyncKind, bool tryAnnexB, Directives inheritedDirectives, |
| 3323 |
Directives* newDirectives) { |
| 3324 |
// Note that it is possible for outerpc != this->pc_, as we may be |
| 3325 |
// attempting to syntax parse an inner function from an outer full |
| 3326 |
// parser. In that case, outerpc is a SourceParseContext from the full parser |
| 3327 |
// instead of the current top of the stack of the syntax parser. |
| 3328 |
|
| 3329 |
FunctionBox* funbox = |
| 3330 |
newFunctionBox(funNode, explicitName, flags, toStringStart, |
| 3331 |
inheritedDirectives, generatorKind, asyncKind); |
| 3332 |
if (!funbox) { |
| 3333 |
return errorResult(); |
| 3334 |
} |
| 3335 |
funbox->initWithEnclosingParseContext(outerpc, kind); |
| 3336 |
|
| 3337 |
FunctionNodeType innerFunc; |
| 3338 |
MOZ_TRY_VAR(innerFunc, |
| 3339 |
innerFunctionForFunctionBox(funNode, outerpc, funbox, inHandling, |
| 3340 |
yieldHandling, kind, newDirectives)); |
| 3341 |
|
| 3342 |
// Append possible Annex B function box only upon successfully parsing. |
| 3343 |
if (tryAnnexB) { |
| 3344 |
if (!pc_->innermostScope()->addPossibleAnnexBFunctionBox(pc_, funbox)) { |
| 3345 |
return errorResult(); |
| 3346 |
} |
| 3347 |
} |
| 3348 |
|
| 3349 |
return innerFunc; |
| 3350 |
} |
| 3351 |
|
| 3352 |
template <class ParseHandler, typename Unit> |
| 3353 |
bool GeneralParser<ParseHandler, Unit>::appendToCallSiteObj( |
| 3354 |
CallSiteNodeType callSiteObj) { |
| 3355 |
Node cookedNode; |
| 3356 |
MOZ_TRY_VAR_OR_RETURN(cookedNode, noSubstitutionTaggedTemplate(), false); |
| 3357 |
|
| 3358 |
auto atom = tokenStream.getRawTemplateStringAtom(); |
| 3359 |
if (!atom) { |
| 3360 |
return false; |
| 3361 |
} |
| 3362 |
NameNodeType rawNode; |
| 3363 |
MOZ_TRY_VAR_OR_RETURN(rawNode, handler_.newTemplateStringLiteral(atom, pos()), |
| 3364 |
false); |
| 3365 |
|
| 3366 |
handler_.addToCallSiteObject(callSiteObj, rawNode, cookedNode); |
| 3367 |
return true; |
| 3368 |
} |
| 3369 |
|
| 3370 |
template <typename Unit> |
| 3371 |
FullParseHandler::FunctionNodeResult |
| 3372 |
Parser<FullParseHandler, Unit>::standaloneLazyFunction( |
| 3373 |
CompilationInput& input, uint32_t toStringStart, bool strict, |
| 3374 |
GeneratorKind generatorKind, FunctionAsyncKind asyncKind) { |
| 3375 |
MOZ_ASSERT(checkOptionsCalled_); |
| 3376 |
|
| 3377 |
FunctionSyntaxKind syntaxKind = input.functionSyntaxKind(); |
| 3378 |
FunctionNodeType funNode; |
| 3379 |
MOZ_TRY_VAR(funNode, handler_.newFunction(syntaxKind, pos())); |
| 3380 |
|
| 3381 |
TaggedParserAtomIndex displayAtom = |
| 3382 |
this->getCompilationState().previousParseCache.displayAtom(); |
| 3383 |
|
| 3384 |
Directives directives(strict); |
| 3385 |
FunctionBox* funbox = |
| 3386 |
newFunctionBox(funNode, displayAtom, input.functionFlags(), toStringStart, |
| 3387 |
directives, generatorKind, asyncKind); |
| 3388 |
if (!funbox) { |
| 3389 |
return errorResult(); |
| 3390 |
} |
| 3391 |
const ScriptStencilExtra& funExtra = |
| 3392 |
this->getCompilationState().previousParseCache.funExtra(); |
| 3393 |
funbox->initFromLazyFunction( |
| 3394 |
funExtra, this->getCompilationState().scopeContext, syntaxKind); |
| 3395 |
if (funbox->useMemberInitializers()) { |
| 3396 |
funbox->setMemberInitializers(funExtra.memberInitializers()); |
| 3397 |
} |
| 3398 |
|
| 3399 |
Directives newDirectives = directives; |
| 3400 |
SourceParseContext funpc(this, funbox, &newDirectives); |
| 3401 |
if (!funpc.init()) { |
| 3402 |
return errorResult(); |
| 3403 |
} |
| 3404 |
|
| 3405 |
// Our tokenStream has no current token, so funNode's position is garbage. |
| 3406 |
// Substitute the position of the first token in our source. If the |
| 3407 |
// function is a not-async arrow, use TokenStream::SlashIsRegExp to keep |
| 3408 |
// verifyConsistentModifier from complaining (we will use |
| 3409 |
// TokenStream::SlashIsRegExp in functionArguments). |
| 3410 |
Modifier modifier = (input.functionFlags().isArrow() && |
| 3411 |
asyncKind == FunctionAsyncKind::SyncFunction) |
| 3412 |
? TokenStream::SlashIsRegExp |
| 3413 |
: TokenStream::SlashIsDiv; |
| 3414 |
if (!tokenStream.peekTokenPos(&funNode->pn_pos, modifier)) { |
| 3415 |
return errorResult(); |
| 3416 |
} |
| 3417 |
|
| 3418 |
YieldHandling yieldHandling = GetYieldHandling(generatorKind); |
| 3419 |
|
| 3420 |
if (funbox->isSyntheticFunction()) { |
| 3421 |
// Currently default class constructors are the only synthetic function that |
| 3422 |
// supports delazification. |
| 3423 |
MOZ_ASSERT(funbox->isClassConstructor()); |
| 3424 |
MOZ_ASSERT(funbox->extent().toStringStart == funbox->extent().sourceStart); |
| 3425 |
|
| 3426 |
HasHeritage hasHeritage = funbox->isDerivedClassConstructor() |
| 3427 |
? HasHeritage::Yes |
| 3428 |
: HasHeritage::No; |
| 3429 |
TokenPos synthesizedBodyPos(funbox->extent().toStringStart, |
| 3430 |
funbox->extent().toStringEnd); |
| 3431 |
|
| 3432 |
// Reset pos() to the `class` keyword for predictable results. |
| 3433 |
tokenStream.consumeKnownToken(TokenKind::Class); |
| 3434 |
|
| 3435 |
if (!this->synthesizeConstructorBody(synthesizedBodyPos, hasHeritage, |
| 3436 |
funNode, funbox)) { |
| 3437 |
return errorResult(); |
| 3438 |
} |
| 3439 |
} else { |
| 3440 |
if (!functionFormalParametersAndBody(InAllowed, yieldHandling, &funNode, |
| 3441 |
syntaxKind)) { |
| 3442 |
MOZ_ASSERT(directives == newDirectives); |
| 3443 |
return errorResult(); |
| 3444 |
} |
| 3445 |
} |
| 3446 |
|
| 3447 |
if (!CheckParseTree(this->fc_, alloc_, funNode)) { |
| 3448 |
return errorResult(); |
| 3449 |
} |
| 3450 |
|
| 3451 |
ParseNode* node = funNode; |
| 3452 |
// Don't constant-fold inside "use asm" code, as this could create a parse |
| 3453 |
// tree that doesn't type-check as asm.js. |
| 3454 |
if (!pc_->useAsmOrInsideUseAsm()) { |
| 3455 |
if (!FoldConstants(this->fc_, this->parserAtoms(), this->bigInts(), &node, |
| 3456 |
&handler_)) { |
| 3457 |
return errorResult(); |
| 3458 |
} |
| 3459 |
} |
| 3460 |
funNode = &node->as<FunctionNode>(); |
| 3461 |
|
| 3462 |
return funNode; |
| 3463 |
} |
| 3464 |
|
| 3465 |
void ParserBase::setFunctionEndFromCurrentToken(FunctionBox* funbox) const { |
| 3466 |
if (compilationState_.isInitialStencil()) { |
| 3467 |
MOZ_ASSERT(anyChars.currentToken().type != TokenKind::Eof); |
| 3468 |
MOZ_ASSERT(anyChars.currentToken().type < TokenKind::Limit); |
| 3469 |
funbox->setEnd(anyChars.currentToken().pos.end); |
| 3470 |
} else { |
| 3471 |
// If we're delazifying an arrow function with expression body and |
| 3472 |
// the expression is also a function, we arrive here immediately after |
| 3473 |
// skipping the function by Parser::skipLazyInnerFunction. |
| 3474 |
// |
| 3475 |
// a => b => c |
| 3476 |
// ^ |
| 3477 |
// | |
| 3478 |
// we're here |
| 3479 |
// |
| 3480 |
// In that case, the current token's type field is either Limit or |
| 3481 |
// poisoned. |
| 3482 |
// We shouldn't read the value if it's poisoned. |
| 3483 |
// See TokenStreamSpecific<Unit, AnyCharsAccess>::advance and |
| 3484 |
// mfbt/MemoryChecking.h for more details. |
| 3485 |
// |
| 3486 |
// Also, in delazification, the FunctionBox should already have the |
| 3487 |
// correct extent, and we shouldn't overwrite it here. |
| 3488 |
// See ScriptStencil variant of PerHandlerParser::newFunctionBox. |
| 3489 |
#if !defined(MOZ_ASAN) && !defined(MOZ_MSAN) && !defined(MOZ_VALGRIND) |
| 3490 |
MOZ_ASSERT(anyChars.currentToken().type != TokenKind::Eof); |
| 3491 |
#endif |
| 3492 |
MOZ_ASSERT(funbox->extent().sourceEnd == anyChars.currentToken().pos.end); |
| 3493 |
} |
| 3494 |
} |
| 3495 |
|
| 3496 |
template <class ParseHandler, typename Unit> |
| 3497 |
bool GeneralParser<ParseHandler, Unit>::functionFormalParametersAndBody( |
| 3498 |
InHandling inHandling, YieldHandling yieldHandling, |
| 3499 |
FunctionNodeType* funNode, FunctionSyntaxKind kind, |
| 3500 |
const Maybe<uint32_t>& parameterListEnd /* = Nothing() */, |
| 3501 |
bool isStandaloneFunction /* = false */) { |
| 3502 |
// Given a properly initialized parse context, try to parse an actual |
| 3503 |
// function without concern for conversion to strict mode, use of lazy |
| 3504 |
// parsing and such. |
| 3505 |
|
| 3506 |
FunctionBox* funbox = pc_->functionBox(); |
| 3507 |
|
| 3508 |
if (kind == FunctionSyntaxKind::ClassConstructor || |
| 3509 |
kind == FunctionSyntaxKind::DerivedClassConstructor) { |
| 3510 |
if (!noteUsedName(TaggedParserAtomIndex::WellKnown::dot_initializers_())) { |
| 3511 |
return false; |
| 3512 |
} |
| 3513 |
#ifdef ENABLE_DECORATORS |
| 3514 |
if (!noteUsedName(TaggedParserAtomIndex::WellKnown:: |
| 3515 |
dot_instanceExtraInitializers_())) { |
| 3516 |
return false; |
| 3517 |
} |
| 3518 |
#endif |
| 3519 |
} |
| 3520 |
|
| 3521 |
// See below for an explanation why arrow function parameters and arrow |
| 3522 |
// function bodies are parsed with different yield/await settings. |
| 3523 |
{ |
| 3524 |
AwaitHandling awaitHandling = |
| 3525 |
kind == FunctionSyntaxKind::StaticClassBlock ? AwaitIsDisallowed |
| 3526 |
: (funbox->isAsync() || |
| 3527 |
(kind == FunctionSyntaxKind::Arrow && awaitIsKeyword())) |
| 3528 |
? AwaitIsKeyword |
| 3529 |
: AwaitIsName; |
| 3530 |
AutoAwaitIsKeyword<ParseHandler, Unit> awaitIsKeyword(this, awaitHandling); |
| 3531 |
AutoInParametersOfAsyncFunction<ParseHandler, Unit> inParameters( |
| 3532 |
this, funbox->isAsync()); |
| 3533 |
if (!functionArguments(yieldHandling, kind, *funNode)) { |
| 3534 |
return false; |
| 3535 |
} |
| 3536 |
} |
| 3537 |
|
| 3538 |
Maybe<ParseContext::VarScope> varScope; |
| 3539 |
if (funbox->hasParameterExprs) { |
| 3540 |
varScope.emplace(this); |
| 3541 |
if (!varScope->init(pc_)) { |
| 3542 |
return false; |
| 3543 |
} |
| 3544 |
} else { |
| 3545 |
pc_->functionScope().useAsVarScope(pc_); |
| 3546 |
} |
| 3547 |
|
| 3548 |
if (kind == FunctionSyntaxKind::Arrow) { |
| 3549 |
TokenKind tt; |
| 3550 |
if (!tokenStream.peekTokenSameLine(&tt)) { |
| 3551 |
return false; |
| 3552 |
} |
| 3553 |
|
| 3554 |
if (tt == TokenKind::Eol) { |
| 3555 |
error(JSMSG_UNEXPECTED_TOKEN, |
| 3556 |
"'=>' on the same line after an argument list", |
| 3557 |
TokenKindToDesc(tt)); |
| 3558 |
return false; |
| 3559 |
} |
| 3560 |
if (tt != TokenKind::Arrow) { |
| 3561 |
error(JSMSG_BAD_ARROW_ARGS); |
| 3562 |
return false; |
| 3563 |
} |
| 3564 |
tokenStream.consumeKnownToken(TokenKind::Arrow); |
| 3565 |
} |
| 3566 |
|
| 3567 |
// When parsing something for new Function() we have to make sure to |
| 3568 |
// only treat a certain part of the source as a parameter list. |
| 3569 |
if (parameterListEnd.isSome() && parameterListEnd.value() != pos().begin) { |
| 3570 |
error(JSMSG_UNEXPECTED_PARAMLIST_END); |
| 3571 |
return false; |
| 3572 |
} |
| 3573 |
|
| 3574 |
// Parse the function body. |
| 3575 |
FunctionBodyType bodyType = StatementListBody; |
| 3576 |
TokenKind tt; |
| 3577 |
if (!tokenStream.getToken(&tt, TokenStream::SlashIsRegExp)) { |
| 3578 |
return false; |
| 3579 |
} |
| 3580 |
uint32_t openedPos = 0; |
| 3581 |
if (tt != TokenKind::LeftCurly) { |
| 3582 |
if (kind != FunctionSyntaxKind::Arrow) { |
| 3583 |
error(JSMSG_CURLY_BEFORE_BODY); |
| 3584 |
return false; |
| 3585 |
} |
| 3586 |
|
| 3587 |
anyChars.ungetToken(); |
| 3588 |
bodyType = ExpressionBody; |
| 3589 |
funbox->setHasExprBody(); |
| 3590 |
} else { |
| 3591 |
openedPos = pos().begin; |
| 3592 |
} |
| 3593 |
|
| 3594 |
// Arrow function parameters inherit yieldHandling from the enclosing |
| 3595 |
// context, but the arrow body doesn't. E.g. in |(a = yield) => yield|, |
| 3596 |
// |yield| in the parameters is either a name or keyword, depending on |
| 3597 |
// whether the arrow function is enclosed in a generator function or not. |
| 3598 |
// Whereas the |yield| in the function body is always parsed as a name. |
| 3599 |
// The same goes when parsing |await| in arrow functions. |
| 3600 |
YieldHandling bodyYieldHandling = GetYieldHandling(pc_->generatorKind()); |
| 3601 |
AwaitHandling bodyAwaitHandling = GetAwaitHandling(pc_->asyncKind()); |
| 3602 |
bool inheritedStrict = pc_->sc()->strict(); |
| 3603 |
LexicalScopeNodeType body; |
| 3604 |
{ |
| 3605 |
AutoAwaitIsKeyword<ParseHandler, Unit> awaitIsKeyword(this, |
| 3606 |
bodyAwaitHandling); |
| 3607 |
AutoInParametersOfAsyncFunction<ParseHandler, Unit> inParameters(this, |
| 3608 |
false); |
| 3609 |
MOZ_TRY_VAR_OR_RETURN( |
| 3610 |
body, functionBody(inHandling, bodyYieldHandling, kind, bodyType), |
| 3611 |
false); |
| 3612 |
} |
| 3613 |
|
| 3614 |
// Revalidate the function name when we transitioned to strict mode. |
| 3615 |
if ((kind == FunctionSyntaxKind::Statement || |
| 3616 |
kind == FunctionSyntaxKind::Expression) && |
| 3617 |
funbox->explicitName() && !inheritedStrict && pc_->sc()->strict()) { |
| 3618 |
MOZ_ASSERT(pc_->sc()->hasExplicitUseStrict(), |
| 3619 |
"strict mode should only change when a 'use strict' directive " |
| 3620 |
"is present"); |
| 3621 |
|
| 3622 |
auto propertyName = funbox->explicitName(); |
| 3623 |
YieldHandling nameYieldHandling; |
| 3624 |
if (kind == FunctionSyntaxKind::Expression) { |
| 3625 |
// Named lambda has binding inside it. |
| 3626 |
nameYieldHandling = bodyYieldHandling; |
| 3627 |
} else { |
| 3628 |
// Otherwise YieldHandling cannot be checked at this point |
| 3629 |
// because of different context. |
| 3630 |
// It should already be checked before this point. |
| 3631 |
nameYieldHandling = YieldIsName; |
| 3632 |
} |
| 3633 |
|
| 3634 |
// We already use the correct await-handling at this point, therefore |
| 3635 |
// we don't need call AutoAwaitIsKeyword here. |
| 3636 |
|
| 3637 |
uint32_t nameOffset = handler_.getFunctionNameOffset(*funNode, anyChars); |
| 3638 |
if (!checkBindingIdentifier(propertyName, nameOffset, nameYieldHandling)) { |
| 3639 |
return false; |
| 3640 |
} |
| 3641 |
} |
| 3642 |
|
| 3643 |
if (bodyType == StatementListBody) { |
| 3644 |
// Cannot use mustMatchToken here because of internal compiler error on |
| 3645 |
// gcc 6.4.0, with linux 64 SM hazard build. |
| 3646 |
TokenKind actual; |
| 3647 |
if (!tokenStream.getToken(&actual, TokenStream::SlashIsRegExp)) { |
| 3648 |
return false; |
| 3649 |
} |
| 3650 |
if (actual != TokenKind::RightCurly) { |
| 3651 |
reportMissingClosing(JSMSG_CURLY_AFTER_BODY, JSMSG_CURLY_OPENED, |
| 3652 |
openedPos); |
| 3653 |
return false; |
| 3654 |
} |
| 3655 |
|
| 3656 |
setFunctionEndFromCurrentToken(funbox); |
| 3657 |
} else { |
| 3658 |
MOZ_ASSERT(kind == FunctionSyntaxKind::Arrow); |
| 3659 |
|
| 3660 |
if (anyChars.hadError()) { |
| 3661 |
return false; |
| 3662 |
} |
| 3663 |
|
| 3664 |
setFunctionEndFromCurrentToken(funbox); |
| 3665 |
|
| 3666 |
if (kind == FunctionSyntaxKind::Statement) { |
| 3667 |
if (!matchOrInsertSemicolon()) { |
| 3668 |
return false; |
| 3669 |
} |
| 3670 |
} |
| 3671 |
} |
| 3672 |
|
| 3673 |
if (IsMethodDefinitionKind(kind) && pc_->superScopeNeedsHomeObject()) { |
| 3674 |
funbox->setNeedsHomeObject(); |
| 3675 |
} |
| 3676 |
|
| 3677 |
if (!finishFunction(isStandaloneFunction)) { |
| 3678 |
return false; |
| 3679 |
} |
| 3680 |
|
| 3681 |
handler_.setEndPosition(body, pos().begin); |
| 3682 |
handler_.setEndPosition(*funNode, pos().end); |
| 3683 |
handler_.setFunctionBody(*funNode, body); |
| 3684 |
|
| 3685 |
return true; |
| 3686 |
} |
| 3687 |
|
| 3688 |
template <class ParseHandler, typename Unit> |
| 3689 |
typename ParseHandler::FunctionNodeResult |
| 3690 |
GeneralParser<ParseHandler, Unit>::functionStmt(uint32_t toStringStart, |
| 3691 |
YieldHandling yieldHandling, |
| 3692 |
DefaultHandling defaultHandling, |
| 3693 |
FunctionAsyncKind asyncKind) { |
| 3694 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Function)); |
| 3695 |
|
| 3696 |
// In sloppy mode, Annex B.3.2 allows labelled function declarations. |
| 3697 |
// Otherwise it's a parse error. |
| 3698 |
ParseContext::Statement* declaredInStmt = pc_->innermostStatement(); |
| 3699 |
if (declaredInStmt && declaredInStmt->kind() == StatementKind::Label) { |
| 3700 |
MOZ_ASSERT(!pc_->sc()->strict(), |
| 3701 |
"labeled functions shouldn't be parsed in strict mode"); |
| 3702 |
|
| 3703 |
// Find the innermost non-label statement. Report an error if it's |
| 3704 |
// unbraced: functions can't appear in it. Otherwise the statement |
| 3705 |
// (or its absence) determines the scope the function's bound in. |
| 3706 |
while (declaredInStmt && declaredInStmt->kind() == StatementKind::Label) { |
| 3707 |
declaredInStmt = declaredInStmt->enclosing(); |
| 3708 |
} |
| 3709 |
|
| 3710 |
if (declaredInStmt && !StatementKindIsBraced(declaredInStmt->kind())) { |
| 3711 |
error(JSMSG_SLOPPY_FUNCTION_LABEL); |
| 3712 |
return errorResult(); |
| 3713 |
} |
| 3714 |
} |
| 3715 |
|
| 3716 |
TokenKind tt; |
| 3717 |
if (!tokenStream.getToken(&tt)) { |
| 3718 |
return errorResult(); |
| 3719 |
} |
| 3720 |
|
| 3721 |
GeneratorKind generatorKind = GeneratorKind::NotGenerator; |
| 3722 |
if (tt == TokenKind::Mul) { |
| 3723 |
generatorKind = GeneratorKind::Generator; |
| 3724 |
if (!tokenStream.getToken(&tt)) { |
| 3725 |
return errorResult(); |
| 3726 |
} |
| 3727 |
} |
| 3728 |
|
| 3729 |
TaggedParserAtomIndex name; |
| 3730 |
if (TokenKindIsPossibleIdentifier(tt)) { |
| 3731 |
name = bindingIdentifier(yieldHandling); |
| 3732 |
if (!name) { |
| 3733 |
return errorResult(); |
| 3734 |
} |
| 3735 |
} else if (defaultHandling == AllowDefaultName) { |
| 3736 |
name = TaggedParserAtomIndex::WellKnown::default_(); |
| 3737 |
anyChars.ungetToken(); |
| 3738 |
} else { |
| 3739 |
/* Unnamed function expressions are forbidden in statement context. */ |
| 3740 |
error(JSMSG_UNNAMED_FUNCTION_STMT); |
| 3741 |
return errorResult(); |
| 3742 |
} |
| 3743 |
|
| 3744 |
// Note the declared name and check for early errors. |
| 3745 |
DeclarationKind kind; |
| 3746 |
if (declaredInStmt) { |
| 3747 |
MOZ_ASSERT(declaredInStmt->kind() != StatementKind::Label); |
| 3748 |
MOZ_ASSERT(StatementKindIsBraced(declaredInStmt->kind())); |
| 3749 |
|
| 3750 |
kind = |
| 3751 |
(!pc_->sc()->strict() && generatorKind == GeneratorKind::NotGenerator && |
| 3752 |
asyncKind == FunctionAsyncKind::SyncFunction) |
| 3753 |
? DeclarationKind::SloppyLexicalFunction |
| 3754 |
: DeclarationKind::LexicalFunction; |
| 3755 |
} else { |
| 3756 |
kind = pc_->atModuleLevel() ? DeclarationKind::ModuleBodyLevelFunction |
| 3757 |
: DeclarationKind::BodyLevelFunction; |
| 3758 |
} |
| 3759 |
|
| 3760 |
if (!noteDeclaredName(name, kind, pos())) { |
| 3761 |
return errorResult(); |
| 3762 |
} |
| 3763 |
|
| 3764 |
FunctionSyntaxKind syntaxKind = FunctionSyntaxKind::Statement; |
| 3765 |
FunctionNodeType funNode; |
| 3766 |
MOZ_TRY_VAR(funNode, handler_.newFunction(syntaxKind, pos())); |
| 3767 |
|
| 3768 |
// Under sloppy mode, try Annex B.3.3 semantics. If making an additional |
| 3769 |
// 'var' binding of the same name does not throw an early error, do so. |
| 3770 |
// This 'var' binding would be assigned the function object when its |
| 3771 |
// declaration is reached, not at the start of the block. |
| 3772 |
// |
| 3773 |
// This semantics is implemented upon Scope exit in |
| 3774 |
// Scope::propagateAndMarkAnnexBFunctionBoxes. |
| 3775 |
bool tryAnnexB = kind == DeclarationKind::SloppyLexicalFunction; |
| 3776 |
|
| 3777 |
YieldHandling newYieldHandling = GetYieldHandling(generatorKind); |
| 3778 |
return functionDefinition(funNode, toStringStart, InAllowed, newYieldHandling, |
| 3779 |
name, syntaxKind, generatorKind, asyncKind, |
| 3780 |
tryAnnexB); |
| 3781 |
} |
| 3782 |
|
| 3783 |
template <class ParseHandler, typename Unit> |
| 3784 |
typename ParseHandler::FunctionNodeResult |
| 3785 |
GeneralParser<ParseHandler, Unit>::functionExpr(uint32_t toStringStart, |
| 3786 |
InvokedPrediction invoked, |
| 3787 |
FunctionAsyncKind asyncKind) { |
| 3788 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Function)); |
| 3789 |
|
| 3790 |
AutoAwaitIsKeyword<ParseHandler, Unit> awaitIsKeyword( |
| 3791 |
this, GetAwaitHandling(asyncKind)); |
| 3792 |
GeneratorKind generatorKind = GeneratorKind::NotGenerator; |
| 3793 |
TokenKind tt; |
| 3794 |
if (!tokenStream.getToken(&tt)) { |
| 3795 |
return errorResult(); |
| 3796 |
} |
| 3797 |
|
| 3798 |
if (tt == TokenKind::Mul) { |
| 3799 |
generatorKind = GeneratorKind::Generator; |
| 3800 |
if (!tokenStream.getToken(&tt)) { |
| 3801 |
return errorResult(); |
| 3802 |
} |
| 3803 |
} |
| 3804 |
|
| 3805 |
YieldHandling yieldHandling = GetYieldHandling(generatorKind); |
| 3806 |
|
| 3807 |
TaggedParserAtomIndex name; |
| 3808 |
if (TokenKindIsPossibleIdentifier(tt)) { |
| 3809 |
name = bindingIdentifier(yieldHandling); |
| 3810 |
if (!name) { |
| 3811 |
return errorResult(); |
| 3812 |
} |
| 3813 |
} else { |
| 3814 |
anyChars.ungetToken(); |
| 3815 |
} |
| 3816 |
|
| 3817 |
FunctionSyntaxKind syntaxKind = FunctionSyntaxKind::Expression; |
| 3818 |
FunctionNodeType funNode; |
| 3819 |
MOZ_TRY_VAR(funNode, handler_.newFunction(syntaxKind, pos())); |
| 3820 |
|
| 3821 |
if (invoked) { |
| 3822 |
funNode = handler_.setLikelyIIFE(funNode); |
| 3823 |
} |
| 3824 |
|
| 3825 |
return functionDefinition(funNode, toStringStart, InAllowed, yieldHandling, |
| 3826 |
name, syntaxKind, generatorKind, asyncKind); |
| 3827 |
} |
| 3828 |
|
| 3829 |
/* |
| 3830 |
* Return true if this node, known to be an unparenthesized string literal |
| 3831 |
* that never contain escape sequences, could be the string of a directive in a |
| 3832 |
* Directive Prologue. Directive strings never contain escape sequences or line |
| 3833 |
* continuations. |
| 3834 |
*/ |
| 3835 |
static inline bool IsUseStrictDirective(const TokenPos& pos, |
| 3836 |
TaggedParserAtomIndex atom) { |
| 3837 |
// the length of "use strict", including quotation. |
| 3838 |
static constexpr size_t useStrictLength = 12; |
| 3839 |
return atom == TaggedParserAtomIndex::WellKnown::use_strict_() && |
| 3840 |
pos.begin + useStrictLength == pos.end; |
| 3841 |
} |
| 3842 |
static inline bool IsUseAsmDirective(const TokenPos& pos, |
| 3843 |
TaggedParserAtomIndex atom) { |
| 3844 |
// the length of "use asm", including quotation. |
| 3845 |
static constexpr size_t useAsmLength = 9; |
| 3846 |
return atom == TaggedParserAtomIndex::WellKnown::use_asm_() && |
| 3847 |
pos.begin + useAsmLength == pos.end; |
| 3848 |
} |
| 3849 |
|
| 3850 |
template <typename Unit> |
| 3851 |
bool Parser<SyntaxParseHandler, Unit>::asmJS(ListNodeType list) { |
| 3852 |
// While asm.js could technically be validated and compiled during syntax |
| 3853 |
// parsing, we have no guarantee that some later JS wouldn't abort the |
| 3854 |
// syntax parse and cause us to re-parse (and re-compile) the asm.js module. |
| 3855 |
// For simplicity, unconditionally abort the syntax parse when "use asm" is |
| 3856 |
// encountered so that asm.js is always validated/compiled exactly once |
| 3857 |
// during a full parse. |
| 3858 |
MOZ_ALWAYS_FALSE(abortIfSyntaxParser()); |
| 3859 |
return false; |
| 3860 |
} |
| 3861 |
|
| 3862 |
template <typename Unit> |
| 3863 |
bool Parser<FullParseHandler, Unit>::asmJS(ListNodeType list) { |
| 3864 |
// Disable syntax parsing in anything nested inside the asm.js module. |
| 3865 |
disableSyntaxParser(); |
| 3866 |
|
| 3867 |
// We should be encountering the "use asm" directive for the first time; if |
| 3868 |
// the directive is already, we must have failed asm.js validation and we're |
| 3869 |
// reparsing. In that case, don't try to validate again. A non-null |
| 3870 |
// newDirectives means we're not in a normal function. |
| 3871 |
if (!pc_->newDirectives || pc_->newDirectives->asmJS()) { |
| 3872 |
return true; |
| 3873 |
} |
| 3874 |
|
| 3875 |
// If there is no ScriptSource, then we are doing a non-compiling parse and |
| 3876 |
// so we shouldn't (and can't, without a ScriptSource) compile. |
| 3877 |
if (ss == nullptr) { |
| 3878 |
return true; |
| 3879 |
} |
| 3880 |
|
| 3881 |
pc_->functionBox()->useAsm = true; |
| 3882 |
|
| 3883 |
// Attempt to validate and compile this asm.js module. On success, the |
| 3884 |
// tokenStream has been advanced to the closing }. On failure, the |
| 3885 |
// tokenStream is in an indeterminate state and we must reparse the |
| 3886 |
// function from the beginning. Reparsing is triggered by marking that a |
| 3887 |
// new directive has been encountered and returning 'false'. |
| 3888 |
bool validated; |
| 3889 |
if (!CompileAsmJS(this->fc_, this->parserAtoms(), *this, list, &validated)) { |
| 3890 |
return false; |
| 3891 |
} |
| 3892 |
if (!validated) { |
| 3893 |
pc_->newDirectives->setAsmJS(); |
| 3894 |
return false; |
| 3895 |
} |
| 3896 |
|
| 3897 |
return true; |
| 3898 |
} |
| 3899 |
|
| 3900 |
template <class ParseHandler, typename Unit> |
| 3901 |
inline bool GeneralParser<ParseHandler, Unit>::asmJS(ListNodeType list) { |
| 3902 |
return asFinalParser()->asmJS(list); |
| 3903 |
} |
| 3904 |
|
| 3905 |
/* |
| 3906 |
* Recognize Directive Prologue members and directives. Assuming |pn| is a |
| 3907 |
* candidate for membership in a directive prologue, recognize directives and |
| 3908 |
* set |pc_|'s flags accordingly. If |pn| is indeed part of a prologue, set its |
| 3909 |
* |prologue| flag. |
| 3910 |
* |
| 3911 |
* Note that the following is a strict mode function: |
| 3912 |
* |
| 3913 |
* function foo() { |
| 3914 |
* "blah" // inserted semi colon |
| 3915 |
* "blurgh" |
| 3916 |
* "use\x20loose" |
| 3917 |
* "use strict" |
| 3918 |
* } |
| 3919 |
* |
| 3920 |
* That is, even though "use\x20loose" can never be a directive, now or in the |
| 3921 |
* future (because of the hex escape), the Directive Prologue extends through it |
| 3922 |
* to the "use strict" statement, which is indeed a directive. |
| 3923 |
*/ |
| 3924 |
template <class ParseHandler, typename Unit> |
| 3925 |
bool GeneralParser<ParseHandler, Unit>::maybeParseDirective( |
| 3926 |
ListNodeType list, Node possibleDirective, bool* cont) { |
| 3927 |
TokenPos directivePos; |
| 3928 |
TaggedParserAtomIndex directive = |
| 3929 |
handler_.isStringExprStatement(possibleDirective, &directivePos); |
| 3930 |
|
| 3931 |
*cont = !!directive; |
| 3932 |
if (!*cont) { |
| 3933 |
return true; |
| 3934 |
} |
| 3935 |
|
| 3936 |
if (IsUseStrictDirective(directivePos, directive)) { |
| 3937 |
// Functions with non-simple parameter lists (destructuring, |
| 3938 |
// default or rest parameters) must not contain a "use strict" |
| 3939 |
// directive. |
| 3940 |
if (pc_->isFunctionBox()) { |
| 3941 |
FunctionBox* funbox = pc_->functionBox(); |
| 3942 |
if (!funbox->hasSimpleParameterList()) { |
| 3943 |
const char* parameterKind = funbox->hasDestructuringArgs |
| 3944 |
? "destructuring" |
| 3945 |
: funbox->hasParameterExprs ? "default" |
| 3946 |
: "rest"; |
| 3947 |
errorAt(directivePos.begin, JSMSG_STRICT_NON_SIMPLE_PARAMS, |
| 3948 |
parameterKind); |
| 3949 |
return false; |
| 3950 |
} |
| 3951 |
} |
| 3952 |
|
| 3953 |
// We're going to be in strict mode. Note that this scope explicitly |
| 3954 |
// had "use strict"; |
| 3955 |
pc_->sc()->setExplicitUseStrict(); |
| 3956 |
if (!pc_->sc()->strict()) { |
| 3957 |
// Some strict mode violations can appear before a Use Strict Directive |
| 3958 |
// is applied. (See the |DeprecatedContent| enum initializers.) These |
| 3959 |
// violations can manifest in two ways. |
| 3960 |
// |
| 3961 |
// First, the violation can appear *before* the Use Strict Directive. |
| 3962 |
// Numeric literals (and therefore octal literals) can only precede a |
| 3963 |
// Use Strict Directive if this function's parameter list is not simple, |
| 3964 |
// but we reported an error for non-simple parameter lists above, so |
| 3965 |
// octal literals present no issue. But octal escapes and \8 and \9 can |
| 3966 |
// appear in the directive prologue before a Use Strict Directive: |
| 3967 |
// |
| 3968 |
// function f() |
| 3969 |
// { |
| 3970 |
// "hell\157 world"; // octal escape |
| 3971 |
// "\8"; "\9"; // NonOctalDecimalEscape |
| 3972 |
// "use strict"; // retroactively makes all the above errors |
| 3973 |
// } |
| 3974 |
// |
| 3975 |
// Second, the violation can appear *after* the Use Strict Directive but |
| 3976 |
// *before* the directive is recognized as terminated. This only |
| 3977 |
// happens when a directive is terminated by ASI, and the next token |
| 3978 |
// contains a violation: |
| 3979 |
// |
| 3980 |
// function a() |
| 3981 |
// { |
| 3982 |
// "use strict" // ASI |
| 3983 |
// 0755; |
| 3984 |
// } |
| 3985 |
// function b() |
| 3986 |
// { |
| 3987 |
// "use strict" // ASI |
| 3988 |
// "hell\157 world"; |
| 3989 |
// } |
| 3990 |
// function c() |
| 3991 |
// { |
| 3992 |
// "use strict" // ASI |
| 3993 |
// "\8"; |
| 3994 |
// } |
| 3995 |
// |
| 3996 |
// We note such violations when tokenizing. Then, if a violation has |
| 3997 |
// been observed at the time a "use strict" is applied, we report the |
| 3998 |
// error. |
| 3999 |
switch (anyChars.sawDeprecatedContent()) { |
| 4000 |
case DeprecatedContent::None: |
| 4001 |
break; |
| 4002 |
case DeprecatedContent::OctalLiteral: |
| 4003 |
error(JSMSG_DEPRECATED_OCTAL_LITERAL); |
| 4004 |
return false; |
| 4005 |
case DeprecatedContent::OctalEscape: |
| 4006 |
error(JSMSG_DEPRECATED_OCTAL_ESCAPE); |
| 4007 |
return false; |
| 4008 |
case DeprecatedContent::EightOrNineEscape: |
| 4009 |
error(JSMSG_DEPRECATED_EIGHT_OR_NINE_ESCAPE); |
| 4010 |
return false; |
| 4011 |
} |
| 4012 |
|
| 4013 |
pc_->sc()->setStrictScript(); |
| 4014 |
} |
| 4015 |
} else if (IsUseAsmDirective(directivePos, directive)) { |
| 4016 |
if (pc_->isFunctionBox()) { |
| 4017 |
return asmJS(list); |
| 4018 |
} |
| 4019 |
return warningAt(directivePos.begin, JSMSG_USE_ASM_DIRECTIVE_FAIL); |
| 4020 |
} |
| 4021 |
return true; |
| 4022 |
} |
| 4023 |
|
| 4024 |
template <class ParseHandler, typename Unit> |
| 4025 |
typename ParseHandler::ListNodeResult |
| 4026 |
GeneralParser<ParseHandler, Unit>::statementList(YieldHandling yieldHandling) { |
| 4027 |
AutoCheckRecursionLimit recursion(this->fc_); |
| 4028 |
if (!recursion.check(this->fc_)) { |
| 4029 |
return errorResult(); |
| 4030 |
} |
| 4031 |
|
| 4032 |
ListNodeType stmtList; |
| 4033 |
MOZ_TRY_VAR(stmtList, handler_.newStatementList(pos())); |
| 4034 |
|
| 4035 |
bool canHaveDirectives = pc_->atBodyLevel(); |
| 4036 |
if (canHaveDirectives) { |
| 4037 |
// Clear flags for deprecated content that might have been seen in an |
| 4038 |
// enclosing context. |
| 4039 |
anyChars.clearSawDeprecatedContent(); |
| 4040 |
} |
| 4041 |
|
| 4042 |
bool canHaveHashbangComment = pc_->atTopLevel(); |
| 4043 |
if (canHaveHashbangComment) { |
| 4044 |
tokenStream.consumeOptionalHashbangComment(); |
| 4045 |
} |
| 4046 |
|
| 4047 |
bool afterReturn = false; |
| 4048 |
bool warnedAboutStatementsAfterReturn = false; |
| 4049 |
uint32_t statementBegin = 0; |
| 4050 |
for (;;) { |
| 4051 |
TokenKind tt = TokenKind::Eof; |
| 4052 |
if (!tokenStream.peekToken(&tt, TokenStream::SlashIsRegExp)) { |
| 4053 |
if (anyChars.isEOF()) { |
| 4054 |
isUnexpectedEOF_ = true; |
| 4055 |
} |
| 4056 |
return errorResult(); |
| 4057 |
} |
| 4058 |
if (tt == TokenKind::Eof || tt == TokenKind::RightCurly) { |
| 4059 |
TokenPos pos; |
| 4060 |
if (!tokenStream.peekTokenPos(&pos, TokenStream::SlashIsRegExp)) { |
| 4061 |
return errorResult(); |
| 4062 |
} |
| 4063 |
handler_.setListEndPosition(stmtList, pos); |
| 4064 |
break; |
| 4065 |
} |
| 4066 |
if (afterReturn) { |
| 4067 |
if (!tokenStream.peekOffset(&statementBegin, |
| 4068 |
TokenStream::SlashIsRegExp)) { |
| 4069 |
return errorResult(); |
| 4070 |
} |
| 4071 |
} |
| 4072 |
auto nextResult = statementListItem(yieldHandling, canHaveDirectives); |
| 4073 |
if (nextResult.isErr()) { |
| 4074 |
if (anyChars.isEOF()) { |
| 4075 |
isUnexpectedEOF_ = true; |
| 4076 |
} |
| 4077 |
return errorResult(); |
| 4078 |
} |
| 4079 |
Node next = nextResult.unwrap(); |
| 4080 |
if (!warnedAboutStatementsAfterReturn) { |
| 4081 |
if (afterReturn) { |
| 4082 |
if (!handler_.isStatementPermittedAfterReturnStatement(next)) { |
| 4083 |
if (!warningAt(statementBegin, JSMSG_STMT_AFTER_RETURN)) { |
| 4084 |
return errorResult(); |
| 4085 |
} |
| 4086 |
|
| 4087 |
warnedAboutStatementsAfterReturn = true; |
| 4088 |
} |
| 4089 |
} else if (handler_.isReturnStatement(next)) { |
| 4090 |
afterReturn = true; |
| 4091 |
} |
| 4092 |
} |
| 4093 |
|
| 4094 |
if (canHaveDirectives) { |
| 4095 |
if (!maybeParseDirective(stmtList, next, &canHaveDirectives)) { |
| 4096 |
return errorResult(); |
| 4097 |
} |
| 4098 |
} |
| 4099 |
|
| 4100 |
handler_.addStatementToList(stmtList, next); |
| 4101 |
} |
| 4102 |
|
| 4103 |
return stmtList; |
| 4104 |
} |
| 4105 |
|
| 4106 |
template <class ParseHandler, typename Unit> |
| 4107 |
typename ParseHandler::NodeResult GeneralParser<ParseHandler, Unit>::condition( |
| 4108 |
InHandling inHandling, YieldHandling yieldHandling) { |
| 4109 |
if (!mustMatchToken(TokenKind::LeftParen, JSMSG_PAREN_BEFORE_COND)) { |
| 4110 |
return errorResult(); |
| 4111 |
} |
| 4112 |
|
| 4113 |
Node pn; |
| 4114 |
MOZ_TRY_VAR(pn, exprInParens(inHandling, yieldHandling, TripledotProhibited)); |
| 4115 |
|
| 4116 |
if (!mustMatchToken(TokenKind::RightParen, JSMSG_PAREN_AFTER_COND)) { |
| 4117 |
return errorResult(); |
| 4118 |
} |
| 4119 |
|
| 4120 |
return pn; |
| 4121 |
} |
| 4122 |
|
| 4123 |
template <class ParseHandler, typename Unit> |
| 4124 |
bool GeneralParser<ParseHandler, Unit>::matchLabel( |
| 4125 |
YieldHandling yieldHandling, TaggedParserAtomIndex* labelOut) { |
| 4126 |
MOZ_ASSERT(labelOut != nullptr); |
| 4127 |
TokenKind tt = TokenKind::Eof; |
| 4128 |
if (!tokenStream.peekTokenSameLine(&tt, TokenStream::SlashIsRegExp)) { |
| 4129 |
return false; |
| 4130 |
} |
| 4131 |
|
| 4132 |
if (TokenKindIsPossibleIdentifier(tt)) { |
| 4133 |
tokenStream.consumeKnownToken(tt, TokenStream::SlashIsRegExp); |
| 4134 |
|
| 4135 |
*labelOut = labelIdentifier(yieldHandling); |
| 4136 |
if (!*labelOut) { |
| 4137 |
return false; |
| 4138 |
} |
| 4139 |
} else { |
| 4140 |
*labelOut = TaggedParserAtomIndex::null(); |
| 4141 |
} |
| 4142 |
return true; |
| 4143 |
} |
| 4144 |
|
| 4145 |
template <class ParseHandler, typename Unit> |
| 4146 |
GeneralParser<ParseHandler, Unit>::PossibleError::PossibleError( |
| 4147 |
GeneralParser<ParseHandler, Unit>& parser) |
| 4148 |
: parser_(parser) {} |
| 4149 |
|
| 4150 |
template <class ParseHandler, typename Unit> |
| 4151 |
typename GeneralParser<ParseHandler, Unit>::PossibleError::Error& |
| 4152 |
GeneralParser<ParseHandler, Unit>::PossibleError::error(ErrorKind kind) { |
| 4153 |
if (kind == ErrorKind::Expression) { |
| 4154 |
return exprError_; |
| 4155 |
} |
| 4156 |
if (kind == ErrorKind::Destructuring) { |
| 4157 |
return destructuringError_; |
| 4158 |
} |
| 4159 |
MOZ_ASSERT(kind == ErrorKind::DestructuringWarning); |
| 4160 |
return destructuringWarning_; |
| 4161 |
} |
| 4162 |
|
| 4163 |
template <class ParseHandler, typename Unit> |
| 4164 |
void GeneralParser<ParseHandler, Unit>::PossibleError::setResolved( |
| 4165 |
ErrorKind kind) { |
| 4166 |
error(kind).state_ = ErrorState::None; |
| 4167 |
} |
| 4168 |
|
| 4169 |
template <class ParseHandler, typename Unit> |
| 4170 |
bool GeneralParser<ParseHandler, Unit>::PossibleError::hasError( |
| 4171 |
ErrorKind kind) { |
| 4172 |
return error(kind).state_ == ErrorState::Pending; |
| 4173 |
} |
| 4174 |
|
| 4175 |
template <class ParseHandler, typename Unit> |
| 4176 |
bool GeneralParser<ParseHandler, |
| 4177 |
Unit>::PossibleError::hasPendingDestructuringError() { |
| 4178 |
return hasError(ErrorKind::Destructuring); |
| 4179 |
} |
| 4180 |
|
| 4181 |
template <class ParseHandler, typename Unit> |
| 4182 |
void GeneralParser<ParseHandler, Unit>::PossibleError::setPending( |
| 4183 |
ErrorKind kind, const TokenPos& pos, unsigned errorNumber) { |
| 4184 |
// Don't overwrite a previously recorded error. |
| 4185 |
if (hasError(kind)) { |
| 4186 |
return; |
| 4187 |
} |
| 4188 |
|
| 4189 |
// If we report an error later, we'll do it from the position where we set |
| 4190 |
// the state to pending. |
| 4191 |
Error& err = error(kind); |
| 4192 |
err.offset_ = pos.begin; |
| 4193 |
err.errorNumber_ = errorNumber; |
| 4194 |
err.state_ = ErrorState::Pending; |
| 4195 |
} |
| 4196 |
|
| 4197 |
template <class ParseHandler, typename Unit> |
| 4198 |
void GeneralParser<ParseHandler, Unit>::PossibleError:: |
| 4199 |
setPendingDestructuringErrorAt(const TokenPos& pos, unsigned errorNumber) { |
| 4200 |
setPending(ErrorKind::Destructuring, pos, errorNumber); |
| 4201 |
} |
| 4202 |
|
| 4203 |
template <class ParseHandler, typename Unit> |
| 4204 |
void GeneralParser<ParseHandler, Unit>::PossibleError:: |
| 4205 |
setPendingDestructuringWarningAt(const TokenPos& pos, |
| 4206 |
unsigned errorNumber) { |
| 4207 |
setPending(ErrorKind::DestructuringWarning, pos, errorNumber); |
| 4208 |
} |
| 4209 |
|
| 4210 |
template <class ParseHandler, typename Unit> |
| 4211 |
void GeneralParser<ParseHandler, Unit>::PossibleError:: |
| 4212 |
setPendingExpressionErrorAt(const TokenPos& pos, unsigned errorNumber) { |
| 4213 |
setPending(ErrorKind::Expression, pos, errorNumber); |
| 4214 |
} |
| 4215 |
|
| 4216 |
template <class ParseHandler, typename Unit> |
| 4217 |
bool GeneralParser<ParseHandler, Unit>::PossibleError::checkForError( |
| 4218 |
ErrorKind kind) { |
| 4219 |
if (!hasError(kind)) { |
| 4220 |
return true; |
| 4221 |
} |
| 4222 |
|
| 4223 |
Error& err = error(kind); |
| 4224 |
parser_.errorAt(err.offset_, err.errorNumber_); |
| 4225 |
return false; |
| 4226 |
} |
| 4227 |
|
| 4228 |
template <class ParseHandler, typename Unit> |
| 4229 |
bool GeneralParser<ParseHandler, |
| 4230 |
Unit>::PossibleError::checkForDestructuringErrorOrWarning() { |
| 4231 |
// Clear pending expression error, because we're definitely not in an |
| 4232 |
// expression context. |
| 4233 |
setResolved(ErrorKind::Expression); |
| 4234 |
|
| 4235 |
// Report any pending destructuring error. |
| 4236 |
return checkForError(ErrorKind::Destructuring); |
| 4237 |
} |
| 4238 |
|
| 4239 |
template <class ParseHandler, typename Unit> |
| 4240 |
bool GeneralParser<ParseHandler, |
| 4241 |
Unit>::PossibleError::checkForExpressionError() { |
| 4242 |
// Clear pending destructuring error, because we're definitely not |
| 4243 |
// in a destructuring context. |
| 4244 |
setResolved(ErrorKind::Destructuring); |
| 4245 |
setResolved(ErrorKind::DestructuringWarning); |
| 4246 |
|
| 4247 |
// Report any pending expression error. |
| 4248 |
return checkForError(ErrorKind::Expression); |
| 4249 |
} |
| 4250 |
|
| 4251 |
template <class ParseHandler, typename Unit> |
| 4252 |
void GeneralParser<ParseHandler, Unit>::PossibleError::transferErrorTo( |
| 4253 |
ErrorKind kind, PossibleError* other) { |
| 4254 |
if (hasError(kind) && !other->hasError(kind)) { |
| 4255 |
Error& err = error(kind); |
| 4256 |
Error& otherErr = other->error(kind); |
| 4257 |
otherErr.offset_ = err.offset_; |
| 4258 |
otherErr.errorNumber_ = err.errorNumber_; |
| 4259 |
otherErr.state_ = err.state_; |
| 4260 |
} |
| 4261 |
} |
| 4262 |
|
| 4263 |
template <class ParseHandler, typename Unit> |
| 4264 |
void GeneralParser<ParseHandler, Unit>::PossibleError::transferErrorsTo( |
| 4265 |
PossibleError* other) { |
| 4266 |
MOZ_ASSERT(other); |
| 4267 |
MOZ_ASSERT(this != other); |
| 4268 |
MOZ_ASSERT(&parser_ == &other->parser_, |
| 4269 |
"Can't transfer fields to an instance which belongs to a " |
| 4270 |
"different parser"); |
| 4271 |
|
| 4272 |
transferErrorTo(ErrorKind::Destructuring, other); |
| 4273 |
transferErrorTo(ErrorKind::Expression, other); |
| 4274 |
} |
| 4275 |
|
| 4276 |
template <class ParseHandler, typename Unit> |
| 4277 |
typename ParseHandler::BinaryNodeResult |
| 4278 |
GeneralParser<ParseHandler, Unit>::bindingInitializer( |
| 4279 |
Node lhs, DeclarationKind kind, YieldHandling yieldHandling) { |
| 4280 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Assign)); |
| 4281 |
|
| 4282 |
if (kind == DeclarationKind::FormalParameter) { |
| 4283 |
pc_->functionBox()->hasParameterExprs = true; |
| 4284 |
} |
| 4285 |
|
| 4286 |
Node rhs; |
| 4287 |
MOZ_TRY_VAR(rhs, assignExpr(InAllowed, yieldHandling, TripledotProhibited)); |
| 4288 |
|
| 4289 |
BinaryNodeType assign; |
| 4290 |
MOZ_TRY_VAR(assign, |
| 4291 |
handler_.newAssignment(ParseNodeKind::AssignExpr, lhs, rhs)); |
| 4292 |
|
| 4293 |
return assign; |
| 4294 |
} |
| 4295 |
|
| 4296 |
template <class ParseHandler, typename Unit> |
| 4297 |
typename ParseHandler::NameNodeResult |
| 4298 |
GeneralParser<ParseHandler, Unit>::bindingIdentifier( |
| 4299 |
DeclarationKind kind, YieldHandling yieldHandling) { |
| 4300 |
TaggedParserAtomIndex name = bindingIdentifier(yieldHandling); |
| 4301 |
if (!name) { |
| 4302 |
return errorResult(); |
| 4303 |
} |
| 4304 |
|
| 4305 |
NameNodeType binding; |
| 4306 |
MOZ_TRY_VAR(binding, newName(name)); |
| 4307 |
if (!noteDeclaredName(name, kind, pos())) { |
| 4308 |
return errorResult(); |
| 4309 |
} |
| 4310 |
|
| 4311 |
return binding; |
| 4312 |
} |
| 4313 |
|
| 4314 |
template <class ParseHandler, typename Unit> |
| 4315 |
typename ParseHandler::NodeResult |
| 4316 |
GeneralParser<ParseHandler, Unit>::bindingIdentifierOrPattern( |
| 4317 |
DeclarationKind kind, YieldHandling yieldHandling, TokenKind tt) { |
| 4318 |
if (tt == TokenKind::LeftBracket) { |
| 4319 |
return arrayBindingPattern(kind, yieldHandling); |
| 4320 |
} |
| 4321 |
|
| 4322 |
if (tt == TokenKind::LeftCurly) { |
| 4323 |
return objectBindingPattern(kind, yieldHandling); |
| 4324 |
} |
| 4325 |
|
| 4326 |
if (!TokenKindIsPossibleIdentifierName(tt)) { |
| 4327 |
error(JSMSG_NO_VARIABLE_NAME, TokenKindToDesc(tt)); |
| 4328 |
return errorResult(); |
| 4329 |
} |
| 4330 |
|
| 4331 |
return bindingIdentifier(kind, yieldHandling); |
| 4332 |
} |
| 4333 |
|
| 4334 |
template <class ParseHandler, typename Unit> |
| 4335 |
typename ParseHandler::ListNodeResult |
| 4336 |
GeneralParser<ParseHandler, Unit>::objectBindingPattern( |
| 4337 |
DeclarationKind kind, YieldHandling yieldHandling) { |
| 4338 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::LeftCurly)); |
| 4339 |
|
| 4340 |
AutoCheckRecursionLimit recursion(this->fc_); |
| 4341 |
if (!recursion.check(this->fc_)) { |
| 4342 |
return errorResult(); |
| 4343 |
} |
| 4344 |
|
| 4345 |
uint32_t begin = pos().begin; |
| 4346 |
ListNodeType literal; |
| 4347 |
MOZ_TRY_VAR(literal, handler_.newObjectLiteral(begin)); |
| 4348 |
|
| 4349 |
Maybe<DeclarationKind> declKind = Some(kind); |
| 4350 |
TaggedParserAtomIndex propAtom; |
| 4351 |
for (;;) { |
| 4352 |
TokenKind tt; |
| 4353 |
if (!tokenStream.peekToken(&tt)) { |
| 4354 |
return errorResult(); |
| 4355 |
} |
| 4356 |
if (tt == TokenKind::RightCurly) { |
| 4357 |
break; |
| 4358 |
} |
| 4359 |
|
| 4360 |
if (tt == TokenKind::TripleDot) { |
| 4361 |
tokenStream.consumeKnownToken(TokenKind::TripleDot); |
| 4362 |
uint32_t begin = pos().begin; |
| 4363 |
|
| 4364 |
TokenKind tt; |
| 4365 |
if (!tokenStream.getToken(&tt)) { |
| 4366 |
return errorResult(); |
| 4367 |
} |
| 4368 |
|
| 4369 |
if (!TokenKindIsPossibleIdentifierName(tt)) { |
| 4370 |
error(JSMSG_NO_VARIABLE_NAME, TokenKindToDesc(tt)); |
| 4371 |
return errorResult(); |
| 4372 |
} |
| 4373 |
|
| 4374 |
NameNodeType inner; |
| 4375 |
MOZ_TRY_VAR(inner, bindingIdentifier(kind, yieldHandling)); |
| 4376 |
|
| 4377 |
if (!handler_.addSpreadProperty(literal, begin, inner)) { |
| 4378 |
return errorResult(); |
| 4379 |
} |
| 4380 |
} else { |
| 4381 |
TokenPos namePos = anyChars.nextToken().pos; |
| 4382 |
|
| 4383 |
PropertyType propType; |
| 4384 |
Node propName; |
| 4385 |
MOZ_TRY_VAR(propName, propertyOrMethodName( |
| 4386 |
yieldHandling, PropertyNameInPattern, declKind, |
| 4387 |
literal, &propType, &propAtom)); |
| 4388 |
|
| 4389 |
if (propType == PropertyType::Normal) { |
| 4390 |
// Handle e.g., |var {p: x} = o| and |var {p: x=0} = o|. |
| 4391 |
|
| 4392 |
if (!tokenStream.getToken(&tt, TokenStream::SlashIsRegExp)) { |
| 4393 |
return errorResult(); |
| 4394 |
} |
| 4395 |
|
| 4396 |
Node binding; |
| 4397 |
MOZ_TRY_VAR(binding, |
| 4398 |
bindingIdentifierOrPattern(kind, yieldHandling, tt)); |
| 4399 |
|
| 4400 |
bool hasInitializer; |
| 4401 |
if (!tokenStream.matchToken(&hasInitializer, TokenKind::Assign, |
| 4402 |
TokenStream::SlashIsRegExp)) { |
| 4403 |
return errorResult(); |
| 4404 |
} |
| 4405 |
|
| 4406 |
Node bindingExpr; |
| 4407 |
if (hasInitializer) { |
| 4408 |
MOZ_TRY_VAR(bindingExpr, |
| 4409 |
bindingInitializer(binding, kind, yieldHandling)); |
| 4410 |
} else { |
| 4411 |
bindingExpr = binding; |
| 4412 |
} |
| 4413 |
|
| 4414 |
if (!handler_.addPropertyDefinition(literal, propName, bindingExpr)) { |
| 4415 |
return errorResult(); |
| 4416 |
} |
| 4417 |
} else if (propType == PropertyType::Shorthand) { |
| 4418 |
// Handle e.g., |var {x, y} = o| as destructuring shorthand |
| 4419 |
// for |var {x: x, y: y} = o|. |
| 4420 |
MOZ_ASSERT(TokenKindIsPossibleIdentifierName(tt)); |
| 4421 |
|
| 4422 |
NameNodeType binding; |
| 4423 |
MOZ_TRY_VAR(binding, bindingIdentifier(kind, yieldHandling)); |
| 4424 |
|
| 4425 |
if (!handler_.addShorthand(literal, handler_.asNameNode(propName), |
| 4426 |
binding)) { |
| 4427 |
return errorResult(); |
| 4428 |
} |
| 4429 |
} else if (propType == PropertyType::CoverInitializedName) { |
| 4430 |
// Handle e.g., |var {x=1, y=2} = o| as destructuring |
| 4431 |
// shorthand with default values. |
| 4432 |
MOZ_ASSERT(TokenKindIsPossibleIdentifierName(tt)); |
| 4433 |
|
| 4434 |
NameNodeType binding; |
| 4435 |
MOZ_TRY_VAR(binding, bindingIdentifier(kind, yieldHandling)); |
| 4436 |
|
| 4437 |
tokenStream.consumeKnownToken(TokenKind::Assign); |
| 4438 |
|
| 4439 |
BinaryNodeType bindingExpr; |
| 4440 |
MOZ_TRY_VAR(bindingExpr, |
| 4441 |
bindingInitializer(binding, kind, yieldHandling)); |
| 4442 |
|
| 4443 |
if (!handler_.addPropertyDefinition(literal, propName, bindingExpr)) { |
| 4444 |
return errorResult(); |
| 4445 |
} |
| 4446 |
} else { |
| 4447 |
errorAt(namePos.begin, JSMSG_NO_VARIABLE_NAME, TokenKindToDesc(tt)); |
| 4448 |
return errorResult(); |
| 4449 |
} |
| 4450 |
} |
| 4451 |
|
| 4452 |
bool matched; |
| 4453 |
if (!tokenStream.matchToken(&matched, TokenKind::Comma, |
| 4454 |
TokenStream::SlashIsInvalid)) { |
| 4455 |
return errorResult(); |
| 4456 |
} |
| 4457 |
if (!matched) { |
| 4458 |
break; |
| 4459 |
} |
| 4460 |
if (tt == TokenKind::TripleDot) { |
| 4461 |
error(JSMSG_REST_WITH_COMMA); |
| 4462 |
return errorResult(); |
| 4463 |
} |
| 4464 |
} |
| 4465 |
|
| 4466 |
if (!mustMatchToken(TokenKind::RightCurly, [this, begin](TokenKind actual) { |
| 4467 |
this->reportMissingClosing(JSMSG_CURLY_AFTER_LIST, JSMSG_CURLY_OPENED, |
| 4468 |
begin); |
| 4469 |
})) { |
| 4470 |
return errorResult(); |
| 4471 |
} |
| 4472 |
|
| 4473 |
handler_.setEndPosition(literal, pos().end); |
| 4474 |
return literal; |
| 4475 |
} |
| 4476 |
|
| 4477 |
template <class ParseHandler, typename Unit> |
| 4478 |
typename ParseHandler::ListNodeResult |
| 4479 |
GeneralParser<ParseHandler, Unit>::arrayBindingPattern( |
| 4480 |
DeclarationKind kind, YieldHandling yieldHandling) { |
| 4481 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::LeftBracket)); |
| 4482 |
|
| 4483 |
AutoCheckRecursionLimit recursion(this->fc_); |
| 4484 |
if (!recursion.check(this->fc_)) { |
| 4485 |
return errorResult(); |
| 4486 |
} |
| 4487 |
|
| 4488 |
uint32_t begin = pos().begin; |
| 4489 |
ListNodeType literal; |
| 4490 |
MOZ_TRY_VAR(literal, handler_.newArrayLiteral(begin)); |
| 4491 |
|
| 4492 |
uint32_t index = 0; |
| 4493 |
for (;; index++) { |
| 4494 |
if (index >= NativeObject::MAX_DENSE_ELEMENTS_COUNT) { |
| 4495 |
error(JSMSG_ARRAY_INIT_TOO_BIG); |
| 4496 |
return errorResult(); |
| 4497 |
} |
| 4498 |
|
| 4499 |
TokenKind tt; |
| 4500 |
if (!tokenStream.getToken(&tt)) { |
| 4501 |
return errorResult(); |
| 4502 |
} |
| 4503 |
|
| 4504 |
if (tt == TokenKind::RightBracket) { |
| 4505 |
anyChars.ungetToken(); |
| 4506 |
break; |
| 4507 |
} |
| 4508 |
|
| 4509 |
if (tt == TokenKind::Comma) { |
| 4510 |
if (!handler_.addElision(literal, pos())) { |
| 4511 |
return errorResult(); |
| 4512 |
} |
| 4513 |
} else if (tt == TokenKind::TripleDot) { |
| 4514 |
uint32_t begin = pos().begin; |
| 4515 |
|
| 4516 |
TokenKind tt; |
| 4517 |
if (!tokenStream.getToken(&tt)) { |
| 4518 |
return errorResult(); |
| 4519 |
} |
| 4520 |
|
| 4521 |
Node inner; |
| 4522 |
MOZ_TRY_VAR(inner, bindingIdentifierOrPattern(kind, yieldHandling, tt)); |
| 4523 |
|
| 4524 |
if (!handler_.addSpreadElement(literal, begin, inner)) { |
| 4525 |
return errorResult(); |
| 4526 |
} |
| 4527 |
} else { |
| 4528 |
Node binding; |
| 4529 |
MOZ_TRY_VAR(binding, bindingIdentifierOrPattern(kind, yieldHandling, tt)); |
| 4530 |
|
| 4531 |
bool hasInitializer; |
| 4532 |
if (!tokenStream.matchToken(&hasInitializer, TokenKind::Assign, |
| 4533 |
TokenStream::SlashIsRegExp)) { |
| 4534 |
return errorResult(); |
| 4535 |
} |
| 4536 |
|
| 4537 |
Node element; |
| 4538 |
if (hasInitializer) { |
| 4539 |
MOZ_TRY_VAR(element, bindingInitializer(binding, kind, yieldHandling)); |
| 4540 |
} else { |
| 4541 |
element = binding; |
| 4542 |
} |
| 4543 |
|
| 4544 |
handler_.addArrayElement(literal, element); |
| 4545 |
} |
| 4546 |
|
| 4547 |
if (tt != TokenKind::Comma) { |
| 4548 |
// If we didn't already match TokenKind::Comma in above case. |
| 4549 |
bool matched; |
| 4550 |
if (!tokenStream.matchToken(&matched, TokenKind::Comma, |
| 4551 |
TokenStream::SlashIsRegExp)) { |
| 4552 |
return errorResult(); |
| 4553 |
} |
| 4554 |
if (!matched) { |
| 4555 |
break; |
| 4556 |
} |
| 4557 |
|
| 4558 |
if (tt == TokenKind::TripleDot) { |
| 4559 |
error(JSMSG_REST_WITH_COMMA); |
| 4560 |
return errorResult(); |
| 4561 |
} |
| 4562 |
} |
| 4563 |
} |
| 4564 |
|
| 4565 |
if (!mustMatchToken(TokenKind::RightBracket, [this, begin](TokenKind actual) { |
| 4566 |
this->reportMissingClosing(JSMSG_BRACKET_AFTER_LIST, |
| 4567 |
JSMSG_BRACKET_OPENED, begin); |
| 4568 |
})) { |
| 4569 |
return errorResult(); |
| 4570 |
} |
| 4571 |
|
| 4572 |
handler_.setEndPosition(literal, pos().end); |
| 4573 |
return literal; |
| 4574 |
} |
| 4575 |
|
| 4576 |
template <class ParseHandler, typename Unit> |
| 4577 |
typename ParseHandler::NodeResult |
| 4578 |
GeneralParser<ParseHandler, Unit>::destructuringDeclaration( |
| 4579 |
DeclarationKind kind, YieldHandling yieldHandling, TokenKind tt) { |
| 4580 |
MOZ_ASSERT(anyChars.isCurrentTokenType(tt)); |
| 4581 |
MOZ_ASSERT(tt == TokenKind::LeftBracket || tt == TokenKind::LeftCurly); |
| 4582 |
|
| 4583 |
if (tt == TokenKind::LeftBracket) { |
| 4584 |
return arrayBindingPattern(kind, yieldHandling); |
| 4585 |
} |
| 4586 |
return objectBindingPattern(kind, yieldHandling); |
| 4587 |
} |
| 4588 |
|
| 4589 |
template <class ParseHandler, typename Unit> |
| 4590 |
typename ParseHandler::NodeResult |
| 4591 |
GeneralParser<ParseHandler, Unit>::destructuringDeclarationWithoutYieldOrAwait( |
| 4592 |
DeclarationKind kind, YieldHandling yieldHandling, TokenKind tt) { |
| 4593 |
uint32_t startYieldOffset = pc_->lastYieldOffset; |
| 4594 |
uint32_t startAwaitOffset = pc_->lastAwaitOffset; |
| 4595 |
|
| 4596 |
Node res; |
| 4597 |
MOZ_TRY_VAR(res, destructuringDeclaration(kind, yieldHandling, tt)); |
| 4598 |
|
| 4599 |
if (pc_->lastYieldOffset != startYieldOffset) { |
| 4600 |
errorAt(pc_->lastYieldOffset, JSMSG_YIELD_IN_PARAMETER); |
| 4601 |
return errorResult(); |
| 4602 |
} |
| 4603 |
if (pc_->lastAwaitOffset != startAwaitOffset) { |
| 4604 |
errorAt(pc_->lastAwaitOffset, JSMSG_AWAIT_IN_PARAMETER); |
| 4605 |
return errorResult(); |
| 4606 |
} |
| 4607 |
return res; |
| 4608 |
} |
| 4609 |
|
| 4610 |
template <class ParseHandler, typename Unit> |
| 4611 |
typename ParseHandler::LexicalScopeNodeResult |
| 4612 |
GeneralParser<ParseHandler, Unit>::blockStatement(YieldHandling yieldHandling, |
| 4613 |
unsigned errorNumber) { |
| 4614 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::LeftCurly)); |
| 4615 |
uint32_t openedPos = pos().begin; |
| 4616 |
|
| 4617 |
ParseContext::Statement stmt(pc_, StatementKind::Block); |
| 4618 |
ParseContext::Scope scope(this); |
| 4619 |
if (!scope.init(pc_)) { |
| 4620 |
return errorResult(); |
| 4621 |
} |
| 4622 |
|
| 4623 |
ListNodeType list; |
| 4624 |
MOZ_TRY_VAR(list, statementList(yieldHandling)); |
| 4625 |
|
| 4626 |
if (!mustMatchToken(TokenKind::RightCurly, [this, errorNumber, |
| 4627 |
openedPos](TokenKind actual) { |
| 4628 |
this->reportMissingClosing(errorNumber, JSMSG_CURLY_OPENED, openedPos); |
| 4629 |
})) { |
| 4630 |
return errorResult(); |
| 4631 |
} |
| 4632 |
|
| 4633 |
return finishLexicalScope(scope, list); |
| 4634 |
} |
| 4635 |
|
| 4636 |
template <class ParseHandler, typename Unit> |
| 4637 |
typename ParseHandler::NodeResult |
| 4638 |
GeneralParser<ParseHandler, Unit>::expressionAfterForInOrOf( |
| 4639 |
ParseNodeKind forHeadKind, YieldHandling yieldHandling) { |
| 4640 |
MOZ_ASSERT(forHeadKind == ParseNodeKind::ForIn || |
| 4641 |
forHeadKind == ParseNodeKind::ForOf); |
| 4642 |
if (forHeadKind == ParseNodeKind::ForOf) { |
| 4643 |
return assignExpr(InAllowed, yieldHandling, TripledotProhibited); |
| 4644 |
} |
| 4645 |
|
| 4646 |
return expr(InAllowed, yieldHandling, TripledotProhibited); |
| 4647 |
} |
| 4648 |
|
| 4649 |
template <class ParseHandler, typename Unit> |
| 4650 |
typename ParseHandler::NodeResult |
| 4651 |
GeneralParser<ParseHandler, Unit>::declarationPattern( |
| 4652 |
DeclarationKind declKind, TokenKind tt, bool initialDeclaration, |
| 4653 |
YieldHandling yieldHandling, ParseNodeKind* forHeadKind, |
| 4654 |
Node* forInOrOfExpression) { |
| 4655 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::LeftBracket) || |
| 4656 |
anyChars.isCurrentTokenType(TokenKind::LeftCurly)); |
| 4657 |
|
| 4658 |
Node pattern; |
| 4659 |
MOZ_TRY_VAR(pattern, destructuringDeclaration(declKind, yieldHandling, tt)); |
| 4660 |
|
| 4661 |
if (initialDeclaration && forHeadKind) { |
| 4662 |
bool isForIn, isForOf; |
| 4663 |
if (!matchInOrOf(&isForIn, &isForOf)) { |
| 4664 |
return errorResult(); |
| 4665 |
} |
| 4666 |
|
| 4667 |
if (isForIn) { |
| 4668 |
*forHeadKind = ParseNodeKind::ForIn; |
| 4669 |
} else if (isForOf) { |
| 4670 |
*forHeadKind = ParseNodeKind::ForOf; |
| 4671 |
} else { |
| 4672 |
*forHeadKind = ParseNodeKind::ForHead; |
| 4673 |
} |
| 4674 |
|
| 4675 |
if (*forHeadKind != ParseNodeKind::ForHead) { |
| 4676 |
MOZ_TRY_VAR(*forInOrOfExpression, |
| 4677 |
expressionAfterForInOrOf(*forHeadKind, yieldHandling)); |
| 4678 |
|
| 4679 |
return pattern; |
| 4680 |
} |
| 4681 |
} |
| 4682 |
|
| 4683 |
if (!mustMatchToken(TokenKind::Assign, JSMSG_BAD_DESTRUCT_DECL)) { |
| 4684 |
return errorResult(); |
| 4685 |
} |
| 4686 |
|
| 4687 |
Node init; |
| 4688 |
MOZ_TRY_VAR(init, assignExpr(forHeadKind ? InProhibited : InAllowed, |
| 4689 |
yieldHandling, TripledotProhibited)); |
| 4690 |
|
| 4691 |
return handler_.newAssignment(ParseNodeKind::AssignExpr, pattern, init); |
| 4692 |
} |
| 4693 |
|
| 4694 |
template <class ParseHandler, typename Unit> |
| 4695 |
typename ParseHandler::AssignmentNodeResult |
| 4696 |
GeneralParser<ParseHandler, Unit>::initializerInNameDeclaration( |
| 4697 |
NameNodeType binding, DeclarationKind declKind, bool initialDeclaration, |
| 4698 |
YieldHandling yieldHandling, ParseNodeKind* forHeadKind, |
| 4699 |
Node* forInOrOfExpression) { |
| 4700 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Assign)); |
| 4701 |
|
| 4702 |
uint32_t initializerOffset; |
| 4703 |
if (!tokenStream.peekOffset(&initializerOffset, TokenStream::SlashIsRegExp)) { |
| 4704 |
return errorResult(); |
| 4705 |
} |
| 4706 |
|
| 4707 |
Node initializer; |
| 4708 |
MOZ_TRY_VAR(initializer, assignExpr(forHeadKind ? InProhibited : InAllowed, |
| 4709 |
yieldHandling, TripledotProhibited)); |
| 4710 |
|
| 4711 |
if (forHeadKind && initialDeclaration) { |
| 4712 |
bool isForIn, isForOf; |
| 4713 |
if (!matchInOrOf(&isForIn, &isForOf)) { |
| 4714 |
return errorResult(); |
| 4715 |
} |
| 4716 |
|
| 4717 |
// An initialized declaration can't appear in a for-of: |
| 4718 |
// |
| 4719 |
// for (var/let/const x = ... of ...); // BAD |
| 4720 |
if (isForOf) { |
| 4721 |
errorAt(initializerOffset, JSMSG_OF_AFTER_FOR_LOOP_DECL); |
| 4722 |
return errorResult(); |
| 4723 |
} |
| 4724 |
|
| 4725 |
if (isForIn) { |
| 4726 |
// Lexical declarations in for-in loops can't be initialized: |
| 4727 |
// |
| 4728 |
// for (let/const x = ... in ...); // BAD |
| 4729 |
if (DeclarationKindIsLexical(declKind)) { |
| 4730 |
errorAt(initializerOffset, JSMSG_IN_AFTER_LEXICAL_FOR_DECL); |
| 4731 |
return errorResult(); |
| 4732 |
} |
| 4733 |
|
| 4734 |
// This leaves only initialized for-in |var| declarations. ES6 |
| 4735 |
// forbids these; later ES un-forbids in non-strict mode code. |
| 4736 |
*forHeadKind = ParseNodeKind::ForIn; |
| 4737 |
if (!strictModeErrorAt(initializerOffset, |
| 4738 |
JSMSG_INVALID_FOR_IN_DECL_WITH_INIT)) { |
| 4739 |
return errorResult(); |
| 4740 |
} |
| 4741 |
|
| 4742 |
MOZ_TRY_VAR( |
| 4743 |
*forInOrOfExpression, |
| 4744 |
expressionAfterForInOrOf(ParseNodeKind::ForIn, yieldHandling)); |
| 4745 |
} else { |
| 4746 |
*forHeadKind = ParseNodeKind::ForHead; |
| 4747 |
} |
| 4748 |
} |
| 4749 |
|
| 4750 |
return handler_.finishInitializerAssignment(binding, initializer); |
| 4751 |
} |
| 4752 |
|
| 4753 |
template <class ParseHandler, typename Unit> |
| 4754 |
typename ParseHandler::NodeResult |
| 4755 |
GeneralParser<ParseHandler, Unit>::declarationName(DeclarationKind declKind, |
| 4756 |
TokenKind tt, |
| 4757 |
bool initialDeclaration, |
| 4758 |
YieldHandling yieldHandling, |
| 4759 |
ParseNodeKind* forHeadKind, |
| 4760 |
Node* forInOrOfExpression) { |
| 4761 |
// Anything other than possible identifier is an error. |
| 4762 |
if (!TokenKindIsPossibleIdentifier(tt)) { |
| 4763 |
error(JSMSG_NO_VARIABLE_NAME, TokenKindToDesc(tt)); |
| 4764 |
return errorResult(); |
| 4765 |
} |
| 4766 |
|
| 4767 |
TaggedParserAtomIndex name = bindingIdentifier(yieldHandling); |
| 4768 |
if (!name) { |
| 4769 |
return errorResult(); |
| 4770 |
} |
| 4771 |
|
| 4772 |
NameNodeType binding; |
| 4773 |
MOZ_TRY_VAR(binding, newName(name)); |
| 4774 |
|
| 4775 |
TokenPos namePos = pos(); |
| 4776 |
|
| 4777 |
// The '=' context after a variable name in a declaration is an opportunity |
| 4778 |
// for ASI, and thus for the next token to start an ExpressionStatement: |
| 4779 |
// |
| 4780 |
// var foo // VariableDeclaration |
| 4781 |
// /bar/g; // ExpressionStatement |
| 4782 |
// |
| 4783 |
// Therefore get the token here with SlashIsRegExp. |
| 4784 |
bool matched; |
| 4785 |
if (!tokenStream.matchToken(&matched, TokenKind::Assign, |
| 4786 |
TokenStream::SlashIsRegExp)) { |
| 4787 |
return errorResult(); |
| 4788 |
} |
| 4789 |
|
| 4790 |
Node declaration; |
| 4791 |
if (matched) { |
| 4792 |
MOZ_TRY_VAR(declaration, |
| 4793 |
initializerInNameDeclaration(binding, declKind, |
| 4794 |
initialDeclaration, yieldHandling, |
| 4795 |
forHeadKind, forInOrOfExpression)); |
| 4796 |
} else { |
| 4797 |
declaration = binding; |
| 4798 |
|
| 4799 |
if (initialDeclaration && forHeadKind) { |
| 4800 |
bool isForIn, isForOf; |
| 4801 |
if (!matchInOrOf(&isForIn, &isForOf)) { |
| 4802 |
return errorResult(); |
| 4803 |
} |
| 4804 |
|
| 4805 |
if (isForIn) { |
| 4806 |
*forHeadKind = ParseNodeKind::ForIn; |
| 4807 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 4808 |
if (declKind == DeclarationKind::Using || |
| 4809 |
declKind == DeclarationKind::AwaitUsing) { |
| 4810 |
errorAt(namePos.begin, JSMSG_NO_IN_WITH_USING); |
| 4811 |
return errorResult(); |
| 4812 |
} |
| 4813 |
#endif |
| 4814 |
} else if (isForOf) { |
| 4815 |
*forHeadKind = ParseNodeKind::ForOf; |
| 4816 |
} else { |
| 4817 |
*forHeadKind = ParseNodeKind::ForHead; |
| 4818 |
} |
| 4819 |
} |
| 4820 |
|
| 4821 |
if (forHeadKind && *forHeadKind != ParseNodeKind::ForHead) { |
| 4822 |
MOZ_TRY_VAR(*forInOrOfExpression, |
| 4823 |
expressionAfterForInOrOf(*forHeadKind, yieldHandling)); |
| 4824 |
} else { |
| 4825 |
// Normal const declarations, and const declarations in for(;;) |
| 4826 |
// heads, must be initialized. |
| 4827 |
if (declKind == DeclarationKind::Const) { |
| 4828 |
errorAt(namePos.begin, JSMSG_BAD_CONST_DECL); |
| 4829 |
return errorResult(); |
| 4830 |
} |
| 4831 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 4832 |
if (declKind == DeclarationKind::Using || |
| 4833 |
declKind == DeclarationKind::AwaitUsing) { |
| 4834 |
errorAt(namePos.begin, JSMSG_BAD_USING_DECL); |
| 4835 |
return errorResult(); |
| 4836 |
} |
| 4837 |
#endif |
| 4838 |
} |
| 4839 |
} |
| 4840 |
|
| 4841 |
// Note the declared name after knowing whether or not we are in a for-of |
| 4842 |
// loop, due to special early error semantics in Annex B.3.5. |
| 4843 |
if (!noteDeclaredName(name, declKind, namePos)) { |
| 4844 |
return errorResult(); |
| 4845 |
} |
| 4846 |
|
| 4847 |
return declaration; |
| 4848 |
} |
| 4849 |
|
| 4850 |
template <class ParseHandler, typename Unit> |
| 4851 |
typename ParseHandler::DeclarationListNodeResult |
| 4852 |
GeneralParser<ParseHandler, Unit>::declarationList( |
| 4853 |
YieldHandling yieldHandling, ParseNodeKind kind, |
| 4854 |
ParseNodeKind* forHeadKind /* = nullptr */, |
| 4855 |
Node* forInOrOfExpression /* = nullptr */) { |
| 4856 |
MOZ_ASSERT(kind == ParseNodeKind::VarStmt || kind == ParseNodeKind::LetDecl || |
| 4857 |
kind == ParseNodeKind::ConstDecl |
| 4858 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 4859 |
|| kind == ParseNodeKind::UsingDecl || |
| 4860 |
kind == ParseNodeKind::AwaitUsingDecl |
| 4861 |
#endif |
| 4862 |
); |
| 4863 |
|
| 4864 |
DeclarationKind declKind; |
| 4865 |
switch (kind) { |
| 4866 |
case ParseNodeKind::VarStmt: |
| 4867 |
declKind = DeclarationKind::Var; |
| 4868 |
break; |
| 4869 |
case ParseNodeKind::ConstDecl: |
| 4870 |
declKind = DeclarationKind::Const; |
| 4871 |
break; |
| 4872 |
case ParseNodeKind::LetDecl: |
| 4873 |
declKind = DeclarationKind::Let; |
| 4874 |
break; |
| 4875 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 4876 |
case ParseNodeKind::UsingDecl: |
| 4877 |
declKind = DeclarationKind::Using; |
| 4878 |
break; |
| 4879 |
case ParseNodeKind::AwaitUsingDecl: |
| 4880 |
declKind = DeclarationKind::AwaitUsing; |
| 4881 |
break; |
| 4882 |
#endif |
| 4883 |
default: |
| 4884 |
MOZ_CRASH("Unknown declaration kind"); |
| 4885 |
} |
| 4886 |
|
| 4887 |
DeclarationListNodeType decl; |
| 4888 |
MOZ_TRY_VAR(decl, handler_.newDeclarationList(kind, pos())); |
| 4889 |
|
| 4890 |
bool moreDeclarations; |
| 4891 |
bool initialDeclaration = true; |
| 4892 |
do { |
| 4893 |
MOZ_ASSERT_IF(!initialDeclaration && forHeadKind, |
| 4894 |
*forHeadKind == ParseNodeKind::ForHead); |
| 4895 |
|
| 4896 |
TokenKind tt; |
| 4897 |
if (!tokenStream.getToken(&tt)) { |
| 4898 |
return errorResult(); |
| 4899 |
} |
| 4900 |
|
| 4901 |
Node binding; |
| 4902 |
if (tt == TokenKind::LeftBracket || tt == TokenKind::LeftCurly) { |
| 4903 |
MOZ_TRY_VAR(binding, declarationPattern(declKind, tt, initialDeclaration, |
| 4904 |
yieldHandling, forHeadKind, |
| 4905 |
forInOrOfExpression)); |
| 4906 |
} else { |
| 4907 |
MOZ_TRY_VAR(binding, declarationName(declKind, tt, initialDeclaration, |
| 4908 |
yieldHandling, forHeadKind, |
| 4909 |
forInOrOfExpression)); |
| 4910 |
} |
| 4911 |
|
| 4912 |
handler_.addList(decl, binding); |
| 4913 |
|
| 4914 |
// If we have a for-in/of loop, the above call matches the entirety |
| 4915 |
// of the loop head (up to the closing parenthesis). |
| 4916 |
if (forHeadKind && *forHeadKind != ParseNodeKind::ForHead) { |
| 4917 |
break; |
| 4918 |
} |
| 4919 |
|
| 4920 |
initialDeclaration = false; |
| 4921 |
|
| 4922 |
if (!tokenStream.matchToken(&moreDeclarations, TokenKind::Comma, |
| 4923 |
TokenStream::SlashIsRegExp)) { |
| 4924 |
return errorResult(); |
| 4925 |
} |
| 4926 |
} while (moreDeclarations); |
| 4927 |
|
| 4928 |
return decl; |
| 4929 |
} |
| 4930 |
|
| 4931 |
template <class ParseHandler, typename Unit> |
| 4932 |
typename ParseHandler::DeclarationListNodeResult |
| 4933 |
GeneralParser<ParseHandler, Unit>::lexicalDeclaration( |
| 4934 |
YieldHandling yieldHandling, DeclarationKind kind) { |
| 4935 |
MOZ_ASSERT(kind == DeclarationKind::Const || kind == DeclarationKind::Let |
| 4936 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 4937 |
|| kind == DeclarationKind::Using || |
| 4938 |
kind == DeclarationKind::AwaitUsing |
| 4939 |
#endif |
| 4940 |
); |
| 4941 |
|
| 4942 |
if (options().selfHostingMode) { |
| 4943 |
error(JSMSG_SELFHOSTED_LEXICAL); |
| 4944 |
return errorResult(); |
| 4945 |
} |
| 4946 |
|
| 4947 |
/* |
| 4948 |
* Parse body-level lets without a new block object. ES6 specs |
| 4949 |
* that an execution environment's initial lexical environment |
| 4950 |
* is the VariableEnvironment, i.e., body-level lets are in |
| 4951 |
* the same environment record as vars. |
| 4952 |
* |
| 4953 |
* However, they cannot be parsed exactly as vars, as ES6 |
| 4954 |
* requires that uninitialized lets throw ReferenceError on use. |
| 4955 |
* |
| 4956 |
* See 8.1.1.1.6 and the note in 13.2.1. |
| 4957 |
*/ |
| 4958 |
DeclarationListNodeType decl; |
| 4959 |
ParseNodeKind pnk; |
| 4960 |
switch (kind) { |
| 4961 |
case DeclarationKind::Const: |
| 4962 |
pnk = ParseNodeKind::ConstDecl; |
| 4963 |
break; |
| 4964 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 4965 |
case DeclarationKind::Using: |
| 4966 |
pnk = ParseNodeKind::UsingDecl; |
| 4967 |
break; |
| 4968 |
case DeclarationKind::AwaitUsing: |
| 4969 |
pnk = ParseNodeKind::AwaitUsingDecl; |
| 4970 |
break; |
| 4971 |
#endif |
| 4972 |
case DeclarationKind::Let: |
| 4973 |
pnk = ParseNodeKind::LetDecl; |
| 4974 |
break; |
| 4975 |
default: |
| 4976 |
MOZ_CRASH("unexpected node kind"); |
| 4977 |
} |
| 4978 |
MOZ_TRY_VAR(decl, declarationList(yieldHandling, pnk)); |
| 4979 |
if (!matchOrInsertSemicolon()) { |
| 4980 |
return errorResult(); |
| 4981 |
} |
| 4982 |
|
| 4983 |
return decl; |
| 4984 |
} |
| 4985 |
|
| 4986 |
template <class ParseHandler, typename Unit> |
| 4987 |
typename ParseHandler::NameNodeResult |
| 4988 |
GeneralParser<ParseHandler, Unit>::moduleExportName() { |
| 4989 |
MOZ_ASSERT(anyChars.currentToken().type == TokenKind::String); |
| 4990 |
TaggedParserAtomIndex name = anyChars.currentToken().atom(); |
| 4991 |
if (!this->parserAtoms().isModuleExportName(name)) { |
| 4992 |
error(JSMSG_UNPAIRED_SURROGATE_EXPORT); |
| 4993 |
return errorResult(); |
| 4994 |
} |
| 4995 |
return handler_.newStringLiteral(name, pos()); |
| 4996 |
} |
| 4997 |
|
| 4998 |
template <class ParseHandler, typename Unit> |
| 4999 |
bool GeneralParser<ParseHandler, Unit>::withClause(ListNodeType attributesSet) { |
| 5000 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Assert) || |
| 5001 |
anyChars.isCurrentTokenType(TokenKind::With)); |
| 5002 |
|
| 5003 |
if (!options().importAttributes()) { |
| 5004 |
error(JSMSG_IMPORT_ATTRIBUTES_NOT_SUPPORTED); |
| 5005 |
return false; |
| 5006 |
} |
| 5007 |
|
| 5008 |
if (!abortIfSyntaxParser()) { |
| 5009 |
return false; |
| 5010 |
} |
| 5011 |
|
| 5012 |
if (!mustMatchToken(TokenKind::LeftCurly, JSMSG_CURLY_AFTER_ASSERT)) { |
| 5013 |
return false; |
| 5014 |
} |
| 5015 |
|
| 5016 |
// Handle the form |... assert {}| |
| 5017 |
TokenKind token; |
| 5018 |
if (!tokenStream.getToken(&token)) { |
| 5019 |
return false; |
| 5020 |
} |
| 5021 |
if (token == TokenKind::RightCurly) { |
| 5022 |
return true; |
| 5023 |
} |
| 5024 |
|
| 5025 |
js::HashSet<TaggedParserAtomIndex, TaggedParserAtomIndexHasher, |
| 5026 |
js::SystemAllocPolicy> |
| 5027 |
usedAssertionKeys; |
| 5028 |
|
| 5029 |
for (;;) { |
| 5030 |
TaggedParserAtomIndex keyName; |
| 5031 |
if (TokenKindIsPossibleIdentifierName(token)) { |
| 5032 |
keyName = anyChars.currentName(); |
| 5033 |
} else if (token == TokenKind::String) { |
| 5034 |
keyName = anyChars.currentToken().atom(); |
| 5035 |
} else { |
| 5036 |
error(JSMSG_ASSERT_KEY_EXPECTED); |
| 5037 |
return false; |
| 5038 |
} |
| 5039 |
|
| 5040 |
auto p = usedAssertionKeys.lookupForAdd(keyName); |
| 5041 |
if (p) { |
| 5042 |
UniqueChars str = this->parserAtoms().toPrintableString(keyName); |
| 5043 |
if (!str) { |
| 5044 |
ReportOutOfMemory(this->fc_); |
| 5045 |
return false; |
| 5046 |
} |
| 5047 |
error(JSMSG_DUPLICATE_ASSERT_KEY, str.get()); |
| 5048 |
return false; |
| 5049 |
} |
| 5050 |
if (!usedAssertionKeys.add(p, keyName)) { |
| 5051 |
ReportOutOfMemory(this->fc_); |
| 5052 |
return false; |
| 5053 |
} |
| 5054 |
|
| 5055 |
NameNodeType keyNode; |
| 5056 |
MOZ_TRY_VAR_OR_RETURN(keyNode, newName(keyName), false); |
| 5057 |
|
| 5058 |
if (!mustMatchToken(TokenKind::Colon, JSMSG_COLON_AFTER_ASSERT_KEY)) { |
| 5059 |
return false; |
| 5060 |
} |
| 5061 |
if (!mustMatchToken(TokenKind::String, JSMSG_ASSERT_STRING_LITERAL)) { |
| 5062 |
return false; |
| 5063 |
} |
| 5064 |
|
| 5065 |
NameNodeType valueNode; |
| 5066 |
MOZ_TRY_VAR_OR_RETURN(valueNode, stringLiteral(), false); |
| 5067 |
|
| 5068 |
BinaryNodeType importAttributeNode; |
| 5069 |
MOZ_TRY_VAR_OR_RETURN(importAttributeNode, |
| 5070 |
handler_.newImportAttribute(keyNode, valueNode), |
| 5071 |
false); |
| 5072 |
|
| 5073 |
handler_.addList(attributesSet, importAttributeNode); |
| 5074 |
|
| 5075 |
if (!tokenStream.getToken(&token)) { |
| 5076 |
return false; |
| 5077 |
} |
| 5078 |
if (token == TokenKind::Comma) { |
| 5079 |
if (!tokenStream.getToken(&token)) { |
| 5080 |
return false; |
| 5081 |
} |
| 5082 |
} |
| 5083 |
if (token == TokenKind::RightCurly) { |
| 5084 |
break; |
| 5085 |
} |
| 5086 |
} |
| 5087 |
|
| 5088 |
return true; |
| 5089 |
} |
| 5090 |
|
| 5091 |
template <class ParseHandler, typename Unit> |
| 5092 |
bool GeneralParser<ParseHandler, Unit>::namedImports( |
| 5093 |
ListNodeType importSpecSet) { |
| 5094 |
if (!abortIfSyntaxParser()) { |
| 5095 |
return false; |
| 5096 |
} |
| 5097 |
|
| 5098 |
while (true) { |
| 5099 |
// Handle the forms |import {} from 'a'| and |
| 5100 |
// |import { ..., } from 'a'| (where ... is non empty), by |
| 5101 |
// escaping the loop early if the next token is }. |
| 5102 |
TokenKind tt; |
| 5103 |
if (!tokenStream.getToken(&tt)) { |
| 5104 |
return false; |
| 5105 |
} |
| 5106 |
|
| 5107 |
if (tt == TokenKind::RightCurly) { |
| 5108 |
break; |
| 5109 |
} |
| 5110 |
|
| 5111 |
TaggedParserAtomIndex importName; |
| 5112 |
NameNodeType importNameNode = null(); |
| 5113 |
if (TokenKindIsPossibleIdentifierName(tt)) { |
| 5114 |
importName = anyChars.currentName(); |
| 5115 |
MOZ_TRY_VAR_OR_RETURN(importNameNode, newName(importName), false); |
| 5116 |
} else if (tt == TokenKind::String) { |
| 5117 |
MOZ_TRY_VAR_OR_RETURN(importNameNode, moduleExportName(), false); |
| 5118 |
} else { |
| 5119 |
error(JSMSG_NO_IMPORT_NAME); |
| 5120 |
return false; |
| 5121 |
} |
| 5122 |
|
| 5123 |
bool matched; |
| 5124 |
if (!tokenStream.matchToken(&matched, TokenKind::As)) { |
| 5125 |
return false; |
| 5126 |
} |
| 5127 |
|
| 5128 |
if (matched) { |
| 5129 |
TokenKind afterAs; |
| 5130 |
if (!tokenStream.getToken(&afterAs)) { |
| 5131 |
return false; |
| 5132 |
} |
| 5133 |
|
| 5134 |
if (!TokenKindIsPossibleIdentifierName(afterAs)) { |
| 5135 |
error(JSMSG_NO_BINDING_NAME); |
| 5136 |
return false; |
| 5137 |
} |
| 5138 |
} else { |
| 5139 |
// String export names can't refer to local bindings. |
| 5140 |
if (tt == TokenKind::String) { |
| 5141 |
error(JSMSG_AS_AFTER_STRING); |
| 5142 |
return false; |
| 5143 |
} |
| 5144 |
|
| 5145 |
// Keywords cannot be bound to themselves, so an import name |
| 5146 |
// that is a keyword is a syntax error if it is not followed |
| 5147 |
// by the keyword 'as'. |
| 5148 |
// See the ImportSpecifier production in ES6 section 15.2.2. |
| 5149 |
MOZ_ASSERT(importName); |
| 5150 |
if (IsKeyword(importName)) { |
| 5151 |
error(JSMSG_AS_AFTER_RESERVED_WORD, ReservedWordToCharZ(importName)); |
| 5152 |
return false; |
| 5153 |
} |
| 5154 |
} |
| 5155 |
|
| 5156 |
TaggedParserAtomIndex bindingAtom = importedBinding(); |
| 5157 |
if (!bindingAtom) { |
| 5158 |
return false; |
| 5159 |
} |
| 5160 |
|
| 5161 |
NameNodeType bindingName; |
| 5162 |
MOZ_TRY_VAR_OR_RETURN(bindingName, newName(bindingAtom), false); |
| 5163 |
if (!noteDeclaredName(bindingAtom, DeclarationKind::Import, pos())) { |
| 5164 |
return false; |
| 5165 |
} |
| 5166 |
|
| 5167 |
BinaryNodeType importSpec; |
| 5168 |
MOZ_TRY_VAR_OR_RETURN( |
| 5169 |
importSpec, handler_.newImportSpec(importNameNode, bindingName), false); |
| 5170 |
|
| 5171 |
handler_.addList(importSpecSet, importSpec); |
| 5172 |
|
| 5173 |
TokenKind next; |
| 5174 |
if (!tokenStream.getToken(&next)) { |
| 5175 |
return false; |
| 5176 |
} |
| 5177 |
|
| 5178 |
if (next == TokenKind::RightCurly) { |
| 5179 |
break; |
| 5180 |
} |
| 5181 |
|
| 5182 |
if (next != TokenKind::Comma) { |
| 5183 |
error(JSMSG_RC_AFTER_IMPORT_SPEC_LIST); |
| 5184 |
return false; |
| 5185 |
} |
| 5186 |
} |
| 5187 |
|
| 5188 |
return true; |
| 5189 |
} |
| 5190 |
|
| 5191 |
template <class ParseHandler, typename Unit> |
| 5192 |
bool GeneralParser<ParseHandler, Unit>::namespaceImport( |
| 5193 |
ListNodeType importSpecSet) { |
| 5194 |
if (!abortIfSyntaxParser()) { |
| 5195 |
return false; |
| 5196 |
} |
| 5197 |
|
| 5198 |
if (!mustMatchToken(TokenKind::As, JSMSG_AS_AFTER_IMPORT_STAR)) { |
| 5199 |
return false; |
| 5200 |
} |
| 5201 |
uint32_t begin = pos().begin; |
| 5202 |
|
| 5203 |
if (!mustMatchToken(TokenKindIsPossibleIdentifierName, |
| 5204 |
JSMSG_NO_BINDING_NAME)) { |
| 5205 |
return false; |
| 5206 |
} |
| 5207 |
|
| 5208 |
// Namespace imports are not indirect bindings but lexical |
| 5209 |
// definitions that hold a module namespace object. They are treated |
| 5210 |
// as const variables which are initialized during the |
| 5211 |
// ModuleInstantiate step. |
| 5212 |
TaggedParserAtomIndex bindingName = importedBinding(); |
| 5213 |
if (!bindingName) { |
| 5214 |
return false; |
| 5215 |
} |
| 5216 |
NameNodeType bindingNameNode; |
| 5217 |
MOZ_TRY_VAR_OR_RETURN(bindingNameNode, newName(bindingName), false); |
| 5218 |
if (!noteDeclaredName(bindingName, DeclarationKind::Const, pos())) { |
| 5219 |
return false; |
| 5220 |
} |
| 5221 |
|
| 5222 |
// The namespace import name is currently required to live on the |
| 5223 |
// environment. |
| 5224 |
pc_->varScope().lookupDeclaredName(bindingName)->value()->setClosedOver(); |
| 5225 |
|
| 5226 |
UnaryNodeType importSpec; |
| 5227 |
MOZ_TRY_VAR_OR_RETURN(importSpec, |
| 5228 |
handler_.newImportNamespaceSpec(begin, bindingNameNode), |
| 5229 |
false); |
| 5230 |
|
| 5231 |
handler_.addList(importSpecSet, importSpec); |
| 5232 |
|
| 5233 |
return true; |
| 5234 |
} |
| 5235 |
|
| 5236 |
template <class ParseHandler, typename Unit> |
| 5237 |
typename ParseHandler::BinaryNodeResult |
| 5238 |
GeneralParser<ParseHandler, Unit>::importDeclaration() { |
| 5239 |
if (!abortIfSyntaxParser()) { |
| 5240 |
return errorResult(); |
| 5241 |
} |
| 5242 |
|
| 5243 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Import)); |
| 5244 |
|
| 5245 |
if (!pc_->atModuleLevel()) { |
| 5246 |
error(JSMSG_IMPORT_DECL_AT_TOP_LEVEL); |
| 5247 |
return errorResult(); |
| 5248 |
} |
| 5249 |
|
| 5250 |
uint32_t begin = pos().begin; |
| 5251 |
TokenKind tt; |
| 5252 |
if (!tokenStream.getToken(&tt)) { |
| 5253 |
return errorResult(); |
| 5254 |
} |
| 5255 |
|
| 5256 |
ListNodeType importSpecSet; |
| 5257 |
MOZ_TRY_VAR(importSpecSet, |
| 5258 |
handler_.newList(ParseNodeKind::ImportSpecList, pos())); |
| 5259 |
|
| 5260 |
if (tt == TokenKind::String) { |
| 5261 |
// Handle the form |import 'a'| by leaving the list empty. This is |
| 5262 |
// equivalent to |import {} from 'a'|. |
| 5263 |
handler_.setEndPosition(importSpecSet, pos().begin); |
| 5264 |
} else { |
| 5265 |
if (tt == TokenKind::LeftCurly) { |
| 5266 |
if (!namedImports(importSpecSet)) { |
| 5267 |
return errorResult(); |
| 5268 |
} |
| 5269 |
} else if (tt == TokenKind::Mul) { |
| 5270 |
if (!namespaceImport(importSpecSet)) { |
| 5271 |
return errorResult(); |
| 5272 |
} |
| 5273 |
} else if (TokenKindIsPossibleIdentifierName(tt)) { |
| 5274 |
// Handle the form |import a from 'b'|, by adding a single import |
| 5275 |
// specifier to the list, with 'default' as the import name and |
| 5276 |
// 'a' as the binding name. This is equivalent to |
| 5277 |
// |import { default as a } from 'b'|. |
| 5278 |
NameNodeType importName; |
| 5279 |
MOZ_TRY_VAR(importName, |
| 5280 |
newName(TaggedParserAtomIndex::WellKnown::default_())); |
| 5281 |
|
| 5282 |
TaggedParserAtomIndex bindingAtom = importedBinding(); |
| 5283 |
if (!bindingAtom) { |
| 5284 |
return errorResult(); |
| 5285 |
} |
| 5286 |
|
| 5287 |
NameNodeType bindingName; |
| 5288 |
MOZ_TRY_VAR(bindingName, newName(bindingAtom)); |
| 5289 |
|
| 5290 |
if (!noteDeclaredName(bindingAtom, DeclarationKind::Import, pos())) { |
| 5291 |
return errorResult(); |
| 5292 |
} |
| 5293 |
|
| 5294 |
BinaryNodeType importSpec; |
| 5295 |
MOZ_TRY_VAR(importSpec, handler_.newImportSpec(importName, bindingName)); |
| 5296 |
|
| 5297 |
handler_.addList(importSpecSet, importSpec); |
| 5298 |
|
| 5299 |
if (!tokenStream.peekToken(&tt)) { |
| 5300 |
return errorResult(); |
| 5301 |
} |
| 5302 |
|
| 5303 |
if (tt == TokenKind::Comma) { |
| 5304 |
tokenStream.consumeKnownToken(tt); |
| 5305 |
if (!tokenStream.getToken(&tt)) { |
| 5306 |
return errorResult(); |
| 5307 |
} |
| 5308 |
|
| 5309 |
if (tt == TokenKind::LeftCurly) { |
| 5310 |
if (!namedImports(importSpecSet)) { |
| 5311 |
return errorResult(); |
| 5312 |
} |
| 5313 |
} else if (tt == TokenKind::Mul) { |
| 5314 |
if (!namespaceImport(importSpecSet)) { |
| 5315 |
return errorResult(); |
| 5316 |
} |
| 5317 |
} else { |
| 5318 |
error(JSMSG_NAMED_IMPORTS_OR_NAMESPACE_IMPORT); |
| 5319 |
return errorResult(); |
| 5320 |
} |
| 5321 |
} |
| 5322 |
} else { |
| 5323 |
error(JSMSG_DECLARATION_AFTER_IMPORT); |
| 5324 |
return errorResult(); |
| 5325 |
} |
| 5326 |
|
| 5327 |
if (!mustMatchToken(TokenKind::From, JSMSG_FROM_AFTER_IMPORT_CLAUSE)) { |
| 5328 |
return errorResult(); |
| 5329 |
} |
| 5330 |
|
| 5331 |
if (!mustMatchToken(TokenKind::String, JSMSG_MODULE_SPEC_AFTER_FROM)) { |
| 5332 |
return errorResult(); |
| 5333 |
} |
| 5334 |
} |
| 5335 |
|
| 5336 |
NameNodeType moduleSpec; |
| 5337 |
MOZ_TRY_VAR(moduleSpec, stringLiteral()); |
| 5338 |
|
| 5339 |
// The `assert` keyword has a [no LineTerminator here] production before it in |
| 5340 |
// the grammar -- `with` does not. We need to handle this distinction. |
| 5341 |
if (!tokenStream.peekTokenSameLine(&tt, TokenStream::SlashIsRegExp)) { |
| 5342 |
return errorResult(); |
| 5343 |
} |
| 5344 |
|
| 5345 |
// `with` may have an EOL prior, so peek the next token and replace |
| 5346 |
// EOL if the next token is `with`. |
| 5347 |
if (tt == TokenKind::Eol) { |
| 5348 |
// Doing a regular peek won't produce Eol, but the actual next token. |
| 5349 |
TokenKind peekedToken; |
| 5350 |
if (!tokenStream.peekToken(&peekedToken, TokenStream::SlashIsRegExp)) { |
| 5351 |
return errorResult(); |
| 5352 |
} |
| 5353 |
|
| 5354 |
if (peekedToken == TokenKind::With) { |
| 5355 |
tt = TokenKind::With; |
| 5356 |
} |
| 5357 |
} |
| 5358 |
|
| 5359 |
ListNodeType importAttributeList; |
| 5360 |
MOZ_TRY_VAR(importAttributeList, |
| 5361 |
handler_.newList(ParseNodeKind::ImportAttributeList, pos())); |
| 5362 |
|
| 5363 |
if (tt == TokenKind::With) { |
| 5364 |
tokenStream.consumeKnownToken(tt, TokenStream::SlashIsRegExp); |
| 5365 |
|
| 5366 |
if (!withClause(importAttributeList)) { |
| 5367 |
return errorResult(); |
| 5368 |
} |
| 5369 |
} |
| 5370 |
|
| 5371 |
if (!matchOrInsertSemicolon(TokenStream::SlashIsRegExp)) { |
| 5372 |
return errorResult(); |
| 5373 |
} |
| 5374 |
|
| 5375 |
BinaryNodeType moduleRequest; |
| 5376 |
MOZ_TRY_VAR(moduleRequest, |
| 5377 |
handler_.newModuleRequest(moduleSpec, importAttributeList, |
| 5378 |
TokenPos(begin, pos().end))); |
| 5379 |
|
| 5380 |
BinaryNodeType node; |
| 5381 |
MOZ_TRY_VAR(node, handler_.newImportDeclaration(importSpecSet, moduleRequest, |
| 5382 |
TokenPos(begin, pos().end))); |
| 5383 |
if (!processImport(node)) { |
| 5384 |
return errorResult(); |
| 5385 |
} |
| 5386 |
|
| 5387 |
return node; |
| 5388 |
} |
| 5389 |
|
| 5390 |
template <class ParseHandler, typename Unit> |
| 5391 |
inline typename ParseHandler::NodeResult |
| 5392 |
GeneralParser<ParseHandler, Unit>::importDeclarationOrImportExpr( |
| 5393 |
YieldHandling yieldHandling) { |
| 5394 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Import)); |
| 5395 |
|
| 5396 |
TokenKind tt; |
| 5397 |
if (!tokenStream.peekToken(&tt)) { |
| 5398 |
return errorResult(); |
| 5399 |
} |
| 5400 |
|
| 5401 |
if (tt == TokenKind::Dot || tt == TokenKind::LeftParen) { |
| 5402 |
return expressionStatement(yieldHandling); |
| 5403 |
} |
| 5404 |
|
| 5405 |
return importDeclaration(); |
| 5406 |
} |
| 5407 |
|
| 5408 |
template <typename Unit> |
| 5409 |
bool Parser<FullParseHandler, Unit>::checkExportedName( |
| 5410 |
TaggedParserAtomIndex exportName) { |
| 5411 |
switch (pc_->sc()->asModuleContext()->builder.noteExportedName(exportName)) { |
| 5412 |
case ModuleBuilder::NoteExportedNameResult::Success: |
| 5413 |
return true; |
| 5414 |
case ModuleBuilder::NoteExportedNameResult::OutOfMemory: |
| 5415 |
return false; |
| 5416 |
case ModuleBuilder::NoteExportedNameResult::AlreadyDeclared: |
| 5417 |
break; |
| 5418 |
} |
| 5419 |
|
| 5420 |
UniqueChars str = this->parserAtoms().toPrintableString(exportName); |
| 5421 |
if (!str) { |
| 5422 |
ReportOutOfMemory(this->fc_); |
| 5423 |
return false; |
| 5424 |
} |
| 5425 |
|
| 5426 |
error(JSMSG_DUPLICATE_EXPORT_NAME, str.get()); |
| 5427 |
return false; |
| 5428 |
} |
| 5429 |
|
| 5430 |
template <typename Unit> |
| 5431 |
inline bool Parser<SyntaxParseHandler, Unit>::checkExportedName( |
| 5432 |
TaggedParserAtomIndex exportName) { |
| 5433 |
MOZ_ALWAYS_FALSE(abortIfSyntaxParser()); |
| 5434 |
return false; |
| 5435 |
} |
| 5436 |
|
| 5437 |
template <class ParseHandler, typename Unit> |
| 5438 |
inline bool GeneralParser<ParseHandler, Unit>::checkExportedName( |
| 5439 |
TaggedParserAtomIndex exportName) { |
| 5440 |
return asFinalParser()->checkExportedName(exportName); |
| 5441 |
} |
| 5442 |
|
| 5443 |
template <typename Unit> |
| 5444 |
bool Parser<FullParseHandler, Unit>::checkExportedNamesForArrayBinding( |
| 5445 |
ListNode* array) { |
| 5446 |
MOZ_ASSERT(array->isKind(ParseNodeKind::ArrayExpr)); |
| 5447 |
|
| 5448 |
for (ParseNode* node : array->contents()) { |
| 5449 |
if (node->isKind(ParseNodeKind::Elision)) { |
| 5450 |
continue; |
| 5451 |
} |
| 5452 |
|
| 5453 |
ParseNode* binding; |
| 5454 |
if (node->isKind(ParseNodeKind::Spread)) { |
| 5455 |
binding = node->as<UnaryNode>().kid(); |
| 5456 |
} else if (node->isKind(ParseNodeKind::AssignExpr)) { |
| 5457 |
binding = node->as<AssignmentNode>().left(); |
| 5458 |
} else { |
| 5459 |
binding = node; |
| 5460 |
} |
| 5461 |
|
| 5462 |
if (!checkExportedNamesForDeclaration(binding)) { |
| 5463 |
return false; |
| 5464 |
} |
| 5465 |
} |
| 5466 |
|
| 5467 |
return true; |
| 5468 |
} |
| 5469 |
|
| 5470 |
template <typename Unit> |
| 5471 |
inline bool Parser<SyntaxParseHandler, Unit>::checkExportedNamesForArrayBinding( |
| 5472 |
ListNodeType array) { |
| 5473 |
MOZ_ALWAYS_FALSE(abortIfSyntaxParser()); |
| 5474 |
return false; |
| 5475 |
} |
| 5476 |
|
| 5477 |
template <class ParseHandler, typename Unit> |
| 5478 |
inline bool |
| 5479 |
GeneralParser<ParseHandler, Unit>::checkExportedNamesForArrayBinding( |
| 5480 |
ListNodeType array) { |
| 5481 |
return asFinalParser()->checkExportedNamesForArrayBinding(array); |
| 5482 |
} |
| 5483 |
|
| 5484 |
template <typename Unit> |
| 5485 |
bool Parser<FullParseHandler, Unit>::checkExportedNamesForObjectBinding( |
| 5486 |
ListNode* obj) { |
| 5487 |
MOZ_ASSERT(obj->isKind(ParseNodeKind::ObjectExpr)); |
| 5488 |
|
| 5489 |
for (ParseNode* node : obj->contents()) { |
| 5490 |
MOZ_ASSERT(node->isKind(ParseNodeKind::MutateProto) || |
| 5491 |
node->isKind(ParseNodeKind::PropertyDefinition) || |
| 5492 |
node->isKind(ParseNodeKind::Shorthand) || |
| 5493 |
node->isKind(ParseNodeKind::Spread)); |
| 5494 |
|
| 5495 |
ParseNode* target; |
| 5496 |
if (node->isKind(ParseNodeKind::Spread)) { |
| 5497 |
target = node->as<UnaryNode>().kid(); |
| 5498 |
} else { |
| 5499 |
if (node->isKind(ParseNodeKind::MutateProto)) { |
| 5500 |
target = node->as<UnaryNode>().kid(); |
| 5501 |
} else { |
| 5502 |
target = node->as<BinaryNode>().right(); |
| 5503 |
} |
| 5504 |
|
| 5505 |
if (target->isKind(ParseNodeKind::AssignExpr)) { |
| 5506 |
target = target->as<AssignmentNode>().left(); |
| 5507 |
} |
| 5508 |
} |
| 5509 |
|
| 5510 |
if (!checkExportedNamesForDeclaration(target)) { |
| 5511 |
return false; |
| 5512 |
} |
| 5513 |
} |
| 5514 |
|
| 5515 |
return true; |
| 5516 |
} |
| 5517 |
|
| 5518 |
template <typename Unit> |
| 5519 |
inline bool Parser<SyntaxParseHandler, |
| 5520 |
Unit>::checkExportedNamesForObjectBinding(ListNodeType obj) { |
| 5521 |
MOZ_ALWAYS_FALSE(abortIfSyntaxParser()); |
| 5522 |
return false; |
| 5523 |
} |
| 5524 |
|
| 5525 |
template <class ParseHandler, typename Unit> |
| 5526 |
inline bool |
| 5527 |
GeneralParser<ParseHandler, Unit>::checkExportedNamesForObjectBinding( |
| 5528 |
ListNodeType obj) { |
| 5529 |
return asFinalParser()->checkExportedNamesForObjectBinding(obj); |
| 5530 |
} |
| 5531 |
|
| 5532 |
template <typename Unit> |
| 5533 |
bool Parser<FullParseHandler, Unit>::checkExportedNamesForDeclaration( |
| 5534 |
ParseNode* node) { |
| 5535 |
if (node->isKind(ParseNodeKind::Name)) { |
| 5536 |
if (!checkExportedName(node->as<NameNode>().atom())) { |
| 5537 |
return false; |
| 5538 |
} |
| 5539 |
} else if (node->isKind(ParseNodeKind::ArrayExpr)) { |
| 5540 |
if (!checkExportedNamesForArrayBinding(&node->as<ListNode>())) { |
| 5541 |
return false; |
| 5542 |
} |
| 5543 |
} else { |
| 5544 |
MOZ_ASSERT(node->isKind(ParseNodeKind::ObjectExpr)); |
| 5545 |
if (!checkExportedNamesForObjectBinding(&node->as<ListNode>())) { |
| 5546 |
return false; |
| 5547 |
} |
| 5548 |
} |
| 5549 |
|
| 5550 |
return true; |
| 5551 |
} |
| 5552 |
|
| 5553 |
template <typename Unit> |
| 5554 |
inline bool Parser<SyntaxParseHandler, Unit>::checkExportedNamesForDeclaration( |
| 5555 |
Node node) { |
| 5556 |
MOZ_ALWAYS_FALSE(abortIfSyntaxParser()); |
| 5557 |
return false; |
| 5558 |
} |
| 5559 |
|
| 5560 |
template <class ParseHandler, typename Unit> |
| 5561 |
inline bool GeneralParser<ParseHandler, Unit>::checkExportedNamesForDeclaration( |
| 5562 |
Node node) { |
| 5563 |
return asFinalParser()->checkExportedNamesForDeclaration(node); |
| 5564 |
} |
| 5565 |
|
| 5566 |
template <typename Unit> |
| 5567 |
bool Parser<FullParseHandler, Unit>::checkExportedNamesForDeclarationList( |
| 5568 |
DeclarationListNodeType node) { |
| 5569 |
for (ParseNode* binding : node->contents()) { |
| 5570 |
if (binding->isKind(ParseNodeKind::AssignExpr)) { |
| 5571 |
binding = binding->as<AssignmentNode>().left(); |
| 5572 |
} else { |
| 5573 |
MOZ_ASSERT(binding->isKind(ParseNodeKind::Name)); |
| 5574 |
} |
| 5575 |
|
| 5576 |
if (!checkExportedNamesForDeclaration(binding)) { |
| 5577 |
return false; |
| 5578 |
} |
| 5579 |
} |
| 5580 |
|
| 5581 |
return true; |
| 5582 |
} |
| 5583 |
|
| 5584 |
template <typename Unit> |
| 5585 |
inline bool |
| 5586 |
Parser<SyntaxParseHandler, Unit>::checkExportedNamesForDeclarationList( |
| 5587 |
DeclarationListNodeType node) { |
| 5588 |
MOZ_ALWAYS_FALSE(abortIfSyntaxParser()); |
| 5589 |
return false; |
| 5590 |
} |
| 5591 |
|
| 5592 |
template <class ParseHandler, typename Unit> |
| 5593 |
inline bool |
| 5594 |
GeneralParser<ParseHandler, Unit>::checkExportedNamesForDeclarationList( |
| 5595 |
DeclarationListNodeType node) { |
| 5596 |
return asFinalParser()->checkExportedNamesForDeclarationList(node); |
| 5597 |
} |
| 5598 |
|
| 5599 |
template <typename Unit> |
| 5600 |
inline bool Parser<FullParseHandler, Unit>::checkExportedNameForClause( |
| 5601 |
NameNode* nameNode) { |
| 5602 |
return checkExportedName(nameNode->atom()); |
| 5603 |
} |
| 5604 |
|
| 5605 |
template <typename Unit> |
| 5606 |
inline bool Parser<SyntaxParseHandler, Unit>::checkExportedNameForClause( |
| 5607 |
NameNodeType nameNode) { |
| 5608 |
MOZ_ALWAYS_FALSE(abortIfSyntaxParser()); |
| 5609 |
return false; |
| 5610 |
} |
| 5611 |
|
| 5612 |
template <class ParseHandler, typename Unit> |
| 5613 |
inline bool GeneralParser<ParseHandler, Unit>::checkExportedNameForClause( |
| 5614 |
NameNodeType nameNode) { |
| 5615 |
return asFinalParser()->checkExportedNameForClause(nameNode); |
| 5616 |
} |
| 5617 |
|
| 5618 |
template <typename Unit> |
| 5619 |
bool Parser<FullParseHandler, Unit>::checkExportedNameForFunction( |
| 5620 |
FunctionNode* funNode) { |
| 5621 |
return checkExportedName(funNode->funbox()->explicitName()); |
| 5622 |
} |
| 5623 |
|
| 5624 |
template <typename Unit> |
| 5625 |
inline bool Parser<SyntaxParseHandler, Unit>::checkExportedNameForFunction( |
| 5626 |
FunctionNodeType funNode) { |
| 5627 |
MOZ_ALWAYS_FALSE(abortIfSyntaxParser()); |
| 5628 |
return false; |
| 5629 |
} |
| 5630 |
|
| 5631 |
template <class ParseHandler, typename Unit> |
| 5632 |
inline bool GeneralParser<ParseHandler, Unit>::checkExportedNameForFunction( |
| 5633 |
FunctionNodeType funNode) { |
| 5634 |
return asFinalParser()->checkExportedNameForFunction(funNode); |
| 5635 |
} |
| 5636 |
|
| 5637 |
template <typename Unit> |
| 5638 |
bool Parser<FullParseHandler, Unit>::checkExportedNameForClass( |
| 5639 |
ClassNode* classNode) { |
| 5640 |
MOZ_ASSERT(classNode->names()); |
| 5641 |
return checkExportedName(classNode->names()->innerBinding()->atom()); |
| 5642 |
} |
| 5643 |
|
| 5644 |
template <typename Unit> |
| 5645 |
inline bool Parser<SyntaxParseHandler, Unit>::checkExportedNameForClass( |
| 5646 |
ClassNodeType classNode) { |
| 5647 |
MOZ_ALWAYS_FALSE(abortIfSyntaxParser()); |
| 5648 |
return false; |
| 5649 |
} |
| 5650 |
|
| 5651 |
template <class ParseHandler, typename Unit> |
| 5652 |
inline bool GeneralParser<ParseHandler, Unit>::checkExportedNameForClass( |
| 5653 |
ClassNodeType classNode) { |
| 5654 |
return asFinalParser()->checkExportedNameForClass(classNode); |
| 5655 |
} |
| 5656 |
|
| 5657 |
template <> |
| 5658 |
inline bool PerHandlerParser<FullParseHandler>::processExport(ParseNode* node) { |
| 5659 |
return pc_->sc()->asModuleContext()->builder.processExport(node); |
| 5660 |
} |
| 5661 |
|
| 5662 |
template <> |
| 5663 |
inline bool PerHandlerParser<SyntaxParseHandler>::processExport(Node node) { |
| 5664 |
MOZ_ALWAYS_FALSE(abortIfSyntaxParser()); |
| 5665 |
return false; |
| 5666 |
} |
| 5667 |
|
| 5668 |
template <> |
| 5669 |
inline bool PerHandlerParser<FullParseHandler>::processExportFrom( |
| 5670 |
BinaryNodeType node) { |
| 5671 |
return pc_->sc()->asModuleContext()->builder.processExportFrom(node); |
| 5672 |
} |
| 5673 |
|
| 5674 |
template <> |
| 5675 |
inline bool PerHandlerParser<SyntaxParseHandler>::processExportFrom( |
| 5676 |
BinaryNodeType node) { |
| 5677 |
MOZ_ALWAYS_FALSE(abortIfSyntaxParser()); |
| 5678 |
return false; |
| 5679 |
} |
| 5680 |
|
| 5681 |
template <> |
| 5682 |
inline bool PerHandlerParser<FullParseHandler>::processImport( |
| 5683 |
BinaryNodeType node) { |
| 5684 |
return pc_->sc()->asModuleContext()->builder.processImport(node); |
| 5685 |
} |
| 5686 |
|
| 5687 |
template <> |
| 5688 |
inline bool PerHandlerParser<SyntaxParseHandler>::processImport( |
| 5689 |
BinaryNodeType node) { |
| 5690 |
MOZ_ALWAYS_FALSE(abortIfSyntaxParser()); |
| 5691 |
return false; |
| 5692 |
} |
| 5693 |
|
| 5694 |
template <class ParseHandler, typename Unit> |
| 5695 |
typename ParseHandler::BinaryNodeResult |
| 5696 |
GeneralParser<ParseHandler, Unit>::exportFrom(uint32_t begin, Node specList) { |
| 5697 |
if (!abortIfSyntaxParser()) { |
| 5698 |
return errorResult(); |
| 5699 |
} |
| 5700 |
|
| 5701 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::From)); |
| 5702 |
|
| 5703 |
if (!mustMatchToken(TokenKind::String, JSMSG_MODULE_SPEC_AFTER_FROM)) { |
| 5704 |
return errorResult(); |
| 5705 |
} |
| 5706 |
|
| 5707 |
NameNodeType moduleSpec; |
| 5708 |
MOZ_TRY_VAR(moduleSpec, stringLiteral()); |
| 5709 |
|
| 5710 |
TokenKind tt; |
| 5711 |
|
| 5712 |
// The `assert` keyword has a [no LineTerminator here] production before it in |
| 5713 |
// the grammar -- `with` does not. We need to handle this distinction. |
| 5714 |
if (!tokenStream.peekTokenSameLine(&tt, TokenStream::SlashIsRegExp)) { |
| 5715 |
return errorResult(); |
| 5716 |
} |
| 5717 |
|
| 5718 |
// `with` may have an EOL prior, so peek the next token and replace |
| 5719 |
// EOL if the next token is `with`. |
| 5720 |
if (tt == TokenKind::Eol) { |
| 5721 |
// Doing a regular peek won't produce Eol, but the actual next token. |
| 5722 |
TokenKind peekedToken; |
| 5723 |
if (!tokenStream.peekToken(&peekedToken, TokenStream::SlashIsRegExp)) { |
| 5724 |
return errorResult(); |
| 5725 |
} |
| 5726 |
|
| 5727 |
if (peekedToken == TokenKind::With) { |
| 5728 |
tt = TokenKind::With; |
| 5729 |
} |
| 5730 |
} |
| 5731 |
|
| 5732 |
uint32_t moduleSpecPos = pos().begin; |
| 5733 |
|
| 5734 |
ListNodeType importAttributeList; |
| 5735 |
MOZ_TRY_VAR(importAttributeList, |
| 5736 |
handler_.newList(ParseNodeKind::ImportAttributeList, pos())); |
| 5737 |
if (tt == TokenKind::With) { |
| 5738 |
tokenStream.consumeKnownToken(tt, TokenStream::SlashIsRegExp); |
| 5739 |
|
| 5740 |
if (!withClause(importAttributeList)) { |
| 5741 |
return errorResult(); |
| 5742 |
} |
| 5743 |
} |
| 5744 |
|
| 5745 |
if (!matchOrInsertSemicolon(TokenStream::SlashIsRegExp)) { |
| 5746 |
return errorResult(); |
| 5747 |
} |
| 5748 |
|
| 5749 |
BinaryNodeType moduleRequest; |
| 5750 |
MOZ_TRY_VAR(moduleRequest, |
| 5751 |
handler_.newModuleRequest(moduleSpec, importAttributeList, |
| 5752 |
TokenPos(moduleSpecPos, pos().end))); |
| 5753 |
|
| 5754 |
BinaryNodeType node; |
| 5755 |
MOZ_TRY_VAR( |
| 5756 |
node, handler_.newExportFromDeclaration(begin, specList, moduleRequest)); |
| 5757 |
|
| 5758 |
if (!processExportFrom(node)) { |
| 5759 |
return errorResult(); |
| 5760 |
} |
| 5761 |
|
| 5762 |
return node; |
| 5763 |
} |
| 5764 |
|
| 5765 |
template <class ParseHandler, typename Unit> |
| 5766 |
typename ParseHandler::BinaryNodeResult |
| 5767 |
GeneralParser<ParseHandler, Unit>::exportBatch(uint32_t begin) { |
| 5768 |
if (!abortIfSyntaxParser()) { |
| 5769 |
return errorResult(); |
| 5770 |
} |
| 5771 |
|
| 5772 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Mul)); |
| 5773 |
uint32_t beginExportSpec = pos().begin; |
| 5774 |
|
| 5775 |
ListNodeType kid; |
| 5776 |
MOZ_TRY_VAR(kid, handler_.newList(ParseNodeKind::ExportSpecList, pos())); |
| 5777 |
|
| 5778 |
bool foundAs; |
| 5779 |
if (!tokenStream.matchToken(&foundAs, TokenKind::As)) { |
| 5780 |
return errorResult(); |
| 5781 |
} |
| 5782 |
|
| 5783 |
if (foundAs) { |
| 5784 |
TokenKind tt; |
| 5785 |
if (!tokenStream.getToken(&tt)) { |
| 5786 |
return errorResult(); |
| 5787 |
} |
| 5788 |
|
| 5789 |
NameNodeType exportName = null(); |
| 5790 |
if (TokenKindIsPossibleIdentifierName(tt)) { |
| 5791 |
MOZ_TRY_VAR(exportName, newName(anyChars.currentName())); |
| 5792 |
} else if (tt == TokenKind::String) { |
| 5793 |
MOZ_TRY_VAR(exportName, moduleExportName()); |
| 5794 |
} else { |
| 5795 |
error(JSMSG_NO_EXPORT_NAME); |
| 5796 |
return errorResult(); |
| 5797 |
} |
| 5798 |
|
| 5799 |
if (!checkExportedNameForClause(exportName)) { |
| 5800 |
return errorResult(); |
| 5801 |
} |
| 5802 |
|
| 5803 |
UnaryNodeType exportSpec; |
| 5804 |
MOZ_TRY_VAR(exportSpec, |
| 5805 |
handler_.newExportNamespaceSpec(beginExportSpec, exportName)); |
| 5806 |
|
| 5807 |
handler_.addList(kid, exportSpec); |
| 5808 |
} else { |
| 5809 |
// Handle the form |export *| by adding a special export batch |
| 5810 |
// specifier to the list. |
| 5811 |
NullaryNodeType exportSpec; |
| 5812 |
MOZ_TRY_VAR(exportSpec, handler_.newExportBatchSpec(pos())); |
| 5813 |
|
| 5814 |
handler_.addList(kid, exportSpec); |
| 5815 |
} |
| 5816 |
|
| 5817 |
if (!mustMatchToken(TokenKind::From, JSMSG_FROM_AFTER_EXPORT_STAR)) { |
| 5818 |
return errorResult(); |
| 5819 |
} |
| 5820 |
|
| 5821 |
return exportFrom(begin, kid); |
| 5822 |
} |
| 5823 |
|
| 5824 |
template <typename Unit> |
| 5825 |
bool Parser<FullParseHandler, Unit>::checkLocalExportNames(ListNode* node) { |
| 5826 |
// ES 2017 draft 15.2.3.1. |
| 5827 |
for (ParseNode* next : node->contents()) { |
| 5828 |
ParseNode* name = next->as<BinaryNode>().left(); |
| 5829 |
|
| 5830 |
if (name->isKind(ParseNodeKind::StringExpr)) { |
| 5831 |
errorAt(name->pn_pos.begin, JSMSG_BAD_LOCAL_STRING_EXPORT); |
| 5832 |
return false; |
| 5833 |
} |
| 5834 |
|
| 5835 |
MOZ_ASSERT(name->isKind(ParseNodeKind::Name)); |
| 5836 |
|
| 5837 |
TaggedParserAtomIndex ident = name->as<NameNode>().atom(); |
| 5838 |
if (!checkLocalExportName(ident, name->pn_pos.begin)) { |
| 5839 |
return false; |
| 5840 |
} |
| 5841 |
} |
| 5842 |
|
| 5843 |
return true; |
| 5844 |
} |
| 5845 |
|
| 5846 |
template <typename Unit> |
| 5847 |
bool Parser<SyntaxParseHandler, Unit>::checkLocalExportNames( |
| 5848 |
ListNodeType node) { |
| 5849 |
MOZ_ALWAYS_FALSE(abortIfSyntaxParser()); |
| 5850 |
return false; |
| 5851 |
} |
| 5852 |
|
| 5853 |
template <class ParseHandler, typename Unit> |
| 5854 |
inline bool GeneralParser<ParseHandler, Unit>::checkLocalExportNames( |
| 5855 |
ListNodeType node) { |
| 5856 |
return asFinalParser()->checkLocalExportNames(node); |
| 5857 |
} |
| 5858 |
|
| 5859 |
template <class ParseHandler, typename Unit> |
| 5860 |
typename ParseHandler::NodeResult |
| 5861 |
GeneralParser<ParseHandler, Unit>::exportClause(uint32_t begin) { |
| 5862 |
if (!abortIfSyntaxParser()) { |
| 5863 |
return errorResult(); |
| 5864 |
} |
| 5865 |
|
| 5866 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::LeftCurly)); |
| 5867 |
|
| 5868 |
ListNodeType kid; |
| 5869 |
MOZ_TRY_VAR(kid, handler_.newList(ParseNodeKind::ExportSpecList, pos())); |
| 5870 |
|
| 5871 |
TokenKind tt; |
| 5872 |
while (true) { |
| 5873 |
// Handle the forms |export {}| and |export { ..., }| (where ... is non |
| 5874 |
// empty), by escaping the loop early if the next token is }. |
| 5875 |
if (!tokenStream.getToken(&tt)) { |
| 5876 |
return errorResult(); |
| 5877 |
} |
| 5878 |
|
| 5879 |
if (tt == TokenKind::RightCurly) { |
| 5880 |
break; |
| 5881 |
} |
| 5882 |
|
| 5883 |
NameNodeType bindingName = null(); |
| 5884 |
if (TokenKindIsPossibleIdentifierName(tt)) { |
| 5885 |
MOZ_TRY_VAR(bindingName, newName(anyChars.currentName())); |
| 5886 |
} else if (tt == TokenKind::String) { |
| 5887 |
MOZ_TRY_VAR(bindingName, moduleExportName()); |
| 5888 |
} else { |
| 5889 |
error(JSMSG_NO_BINDING_NAME); |
| 5890 |
return errorResult(); |
| 5891 |
} |
| 5892 |
|
| 5893 |
bool foundAs; |
| 5894 |
if (!tokenStream.matchToken(&foundAs, TokenKind::As)) { |
| 5895 |
return errorResult(); |
| 5896 |
} |
| 5897 |
|
| 5898 |
NameNodeType exportName = null(); |
| 5899 |
if (foundAs) { |
| 5900 |
TokenKind tt; |
| 5901 |
if (!tokenStream.getToken(&tt)) { |
| 5902 |
return errorResult(); |
| 5903 |
} |
| 5904 |
|
| 5905 |
if (TokenKindIsPossibleIdentifierName(tt)) { |
| 5906 |
MOZ_TRY_VAR(exportName, newName(anyChars.currentName())); |
| 5907 |
} else if (tt == TokenKind::String) { |
| 5908 |
MOZ_TRY_VAR(exportName, moduleExportName()); |
| 5909 |
} else { |
| 5910 |
error(JSMSG_NO_EXPORT_NAME); |
| 5911 |
return errorResult(); |
| 5912 |
} |
| 5913 |
} else { |
| 5914 |
if (tt != TokenKind::String) { |
| 5915 |
MOZ_TRY_VAR(exportName, newName(anyChars.currentName())); |
| 5916 |
} else { |
| 5917 |
MOZ_TRY_VAR(exportName, moduleExportName()); |
| 5918 |
} |
| 5919 |
} |
| 5920 |
|
| 5921 |
if (!checkExportedNameForClause(exportName)) { |
| 5922 |
return errorResult(); |
| 5923 |
} |
| 5924 |
|
| 5925 |
BinaryNodeType exportSpec; |
| 5926 |
MOZ_TRY_VAR(exportSpec, handler_.newExportSpec(bindingName, exportName)); |
| 5927 |
|
| 5928 |
handler_.addList(kid, exportSpec); |
| 5929 |
|
| 5930 |
TokenKind next; |
| 5931 |
if (!tokenStream.getToken(&next)) { |
| 5932 |
return errorResult(); |
| 5933 |
} |
| 5934 |
|
| 5935 |
if (next == TokenKind::RightCurly) { |
| 5936 |
break; |
| 5937 |
} |
| 5938 |
|
| 5939 |
if (next != TokenKind::Comma) { |
| 5940 |
error(JSMSG_RC_AFTER_EXPORT_SPEC_LIST); |
| 5941 |
return errorResult(); |
| 5942 |
} |
| 5943 |
} |
| 5944 |
|
| 5945 |
// Careful! If |from| follows, even on a new line, it must start a |
| 5946 |
// FromClause: |
| 5947 |
// |
| 5948 |
// export { x } |
| 5949 |
// from "foo"; // a single ExportDeclaration |
| 5950 |
// |
| 5951 |
// But if it doesn't, we might have an ASI opportunity in SlashIsRegExp |
| 5952 |
// context: |
| 5953 |
// |
| 5954 |
// export { x } // ExportDeclaration, terminated by ASI |
| 5955 |
// fro\u006D // ExpressionStatement, the name "from" |
| 5956 |
// |
| 5957 |
// In that case let matchOrInsertSemicolon sort out ASI or any necessary |
| 5958 |
// error. |
| 5959 |
bool matched; |
| 5960 |
if (!tokenStream.matchToken(&matched, TokenKind::From, |
| 5961 |
TokenStream::SlashIsRegExp)) { |
| 5962 |
return errorResult(); |
| 5963 |
} |
| 5964 |
|
| 5965 |
if (matched) { |
| 5966 |
return exportFrom(begin, kid); |
| 5967 |
} |
| 5968 |
|
| 5969 |
if (!matchOrInsertSemicolon()) { |
| 5970 |
return errorResult(); |
| 5971 |
} |
| 5972 |
|
| 5973 |
if (!checkLocalExportNames(kid)) { |
| 5974 |
return errorResult(); |
| 5975 |
} |
| 5976 |
|
| 5977 |
UnaryNodeType node; |
| 5978 |
MOZ_TRY_VAR(node, |
| 5979 |
handler_.newExportDeclaration(kid, TokenPos(begin, pos().end))); |
| 5980 |
|
| 5981 |
if (!processExport(node)) { |
| 5982 |
return errorResult(); |
| 5983 |
} |
| 5984 |
|
| 5985 |
return node; |
| 5986 |
} |
| 5987 |
|
| 5988 |
template <class ParseHandler, typename Unit> |
| 5989 |
typename ParseHandler::UnaryNodeResult |
| 5990 |
GeneralParser<ParseHandler, Unit>::exportVariableStatement(uint32_t begin) { |
| 5991 |
if (!abortIfSyntaxParser()) { |
| 5992 |
return errorResult(); |
| 5993 |
} |
| 5994 |
|
| 5995 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Var)); |
| 5996 |
|
| 5997 |
DeclarationListNodeType kid; |
| 5998 |
MOZ_TRY_VAR(kid, declarationList(YieldIsName, ParseNodeKind::VarStmt)); |
| 5999 |
if (!matchOrInsertSemicolon()) { |
| 6000 |
return errorResult(); |
| 6001 |
} |
| 6002 |
if (!checkExportedNamesForDeclarationList(kid)) { |
| 6003 |
return errorResult(); |
| 6004 |
} |
| 6005 |
|
| 6006 |
UnaryNodeType node; |
| 6007 |
MOZ_TRY_VAR(node, |
| 6008 |
handler_.newExportDeclaration(kid, TokenPos(begin, pos().end))); |
| 6009 |
|
| 6010 |
if (!processExport(node)) { |
| 6011 |
return errorResult(); |
| 6012 |
} |
| 6013 |
|
| 6014 |
return node; |
| 6015 |
} |
| 6016 |
|
| 6017 |
template <class ParseHandler, typename Unit> |
| 6018 |
typename ParseHandler::UnaryNodeResult |
| 6019 |
GeneralParser<ParseHandler, Unit>::exportFunctionDeclaration( |
| 6020 |
uint32_t begin, uint32_t toStringStart, |
| 6021 |
FunctionAsyncKind asyncKind /* = SyncFunction */) { |
| 6022 |
if (!abortIfSyntaxParser()) { |
| 6023 |
return errorResult(); |
| 6024 |
} |
| 6025 |
|
| 6026 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Function)); |
| 6027 |
|
| 6028 |
Node kid; |
| 6029 |
MOZ_TRY_VAR( |
| 6030 |
kid, functionStmt(toStringStart, YieldIsName, NameRequired, asyncKind)); |
| 6031 |
|
| 6032 |
if (!checkExportedNameForFunction(handler_.asFunctionNode(kid))) { |
| 6033 |
return errorResult(); |
| 6034 |
} |
| 6035 |
|
| 6036 |
UnaryNodeType node; |
| 6037 |
MOZ_TRY_VAR(node, |
| 6038 |
handler_.newExportDeclaration(kid, TokenPos(begin, pos().end))); |
| 6039 |
|
| 6040 |
if (!processExport(node)) { |
| 6041 |
return errorResult(); |
| 6042 |
} |
| 6043 |
|
| 6044 |
return node; |
| 6045 |
} |
| 6046 |
|
| 6047 |
template <class ParseHandler, typename Unit> |
| 6048 |
typename ParseHandler::UnaryNodeResult |
| 6049 |
GeneralParser<ParseHandler, Unit>::exportClassDeclaration(uint32_t begin) { |
| 6050 |
if (!abortIfSyntaxParser()) { |
| 6051 |
return errorResult(); |
| 6052 |
} |
| 6053 |
|
| 6054 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Class)); |
| 6055 |
|
| 6056 |
ClassNodeType kid; |
| 6057 |
MOZ_TRY_VAR(kid, classDefinition(YieldIsName, ClassStatement, NameRequired)); |
| 6058 |
|
| 6059 |
if (!checkExportedNameForClass(kid)) { |
| 6060 |
return errorResult(); |
| 6061 |
} |
| 6062 |
|
| 6063 |
UnaryNodeType node; |
| 6064 |
MOZ_TRY_VAR(node, |
| 6065 |
handler_.newExportDeclaration(kid, TokenPos(begin, pos().end))); |
| 6066 |
|
| 6067 |
if (!processExport(node)) { |
| 6068 |
return errorResult(); |
| 6069 |
} |
| 6070 |
|
| 6071 |
return node; |
| 6072 |
} |
| 6073 |
|
| 6074 |
template <class ParseHandler, typename Unit> |
| 6075 |
typename ParseHandler::UnaryNodeResult |
| 6076 |
GeneralParser<ParseHandler, Unit>::exportLexicalDeclaration( |
| 6077 |
uint32_t begin, DeclarationKind kind) { |
| 6078 |
if (!abortIfSyntaxParser()) { |
| 6079 |
return errorResult(); |
| 6080 |
} |
| 6081 |
|
| 6082 |
MOZ_ASSERT(kind == DeclarationKind::Const || kind == DeclarationKind::Let); |
| 6083 |
MOZ_ASSERT_IF(kind == DeclarationKind::Const, |
| 6084 |
anyChars.isCurrentTokenType(TokenKind::Const)); |
| 6085 |
MOZ_ASSERT_IF(kind == DeclarationKind::Let, |
| 6086 |
anyChars.isCurrentTokenType(TokenKind::Let)); |
| 6087 |
|
| 6088 |
DeclarationListNodeType kid; |
| 6089 |
MOZ_TRY_VAR(kid, lexicalDeclaration(YieldIsName, kind)); |
| 6090 |
if (!checkExportedNamesForDeclarationList(kid)) { |
| 6091 |
return errorResult(); |
| 6092 |
} |
| 6093 |
|
| 6094 |
UnaryNodeType node; |
| 6095 |
MOZ_TRY_VAR(node, |
| 6096 |
handler_.newExportDeclaration(kid, TokenPos(begin, pos().end))); |
| 6097 |
|
| 6098 |
if (!processExport(node)) { |
| 6099 |
return errorResult(); |
| 6100 |
} |
| 6101 |
|
| 6102 |
return node; |
| 6103 |
} |
| 6104 |
|
| 6105 |
template <class ParseHandler, typename Unit> |
| 6106 |
typename ParseHandler::BinaryNodeResult |
| 6107 |
GeneralParser<ParseHandler, Unit>::exportDefaultFunctionDeclaration( |
| 6108 |
uint32_t begin, uint32_t toStringStart, |
| 6109 |
FunctionAsyncKind asyncKind /* = SyncFunction */) { |
| 6110 |
if (!abortIfSyntaxParser()) { |
| 6111 |
return errorResult(); |
| 6112 |
} |
| 6113 |
|
| 6114 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Function)); |
| 6115 |
|
| 6116 |
Node kid; |
| 6117 |
MOZ_TRY_VAR(kid, functionStmt(toStringStart, YieldIsName, AllowDefaultName, |
| 6118 |
asyncKind)); |
| 6119 |
|
| 6120 |
BinaryNodeType node; |
| 6121 |
MOZ_TRY_VAR(node, handler_.newExportDefaultDeclaration( |
| 6122 |
kid, null(), TokenPos(begin, pos().end))); |
| 6123 |
|
| 6124 |
if (!processExport(node)) { |
| 6125 |
return errorResult(); |
| 6126 |
} |
| 6127 |
|
| 6128 |
return node; |
| 6129 |
} |
| 6130 |
|
| 6131 |
template <class ParseHandler, typename Unit> |
| 6132 |
typename ParseHandler::BinaryNodeResult |
| 6133 |
GeneralParser<ParseHandler, Unit>::exportDefaultClassDeclaration( |
| 6134 |
uint32_t begin) { |
| 6135 |
if (!abortIfSyntaxParser()) { |
| 6136 |
return errorResult(); |
| 6137 |
} |
| 6138 |
|
| 6139 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Class)); |
| 6140 |
|
| 6141 |
ClassNodeType kid; |
| 6142 |
MOZ_TRY_VAR(kid, |
| 6143 |
classDefinition(YieldIsName, ClassStatement, AllowDefaultName)); |
| 6144 |
|
| 6145 |
BinaryNodeType node; |
| 6146 |
MOZ_TRY_VAR(node, handler_.newExportDefaultDeclaration( |
| 6147 |
kid, null(), TokenPos(begin, pos().end))); |
| 6148 |
|
| 6149 |
if (!processExport(node)) { |
| 6150 |
return errorResult(); |
| 6151 |
} |
| 6152 |
|
| 6153 |
return node; |
| 6154 |
} |
| 6155 |
|
| 6156 |
template <class ParseHandler, typename Unit> |
| 6157 |
typename ParseHandler::BinaryNodeResult |
| 6158 |
GeneralParser<ParseHandler, Unit>::exportDefaultAssignExpr(uint32_t begin) { |
| 6159 |
if (!abortIfSyntaxParser()) { |
| 6160 |
return errorResult(); |
| 6161 |
} |
| 6162 |
|
| 6163 |
TaggedParserAtomIndex name = TaggedParserAtomIndex::WellKnown::default_(); |
| 6164 |
NameNodeType nameNode; |
| 6165 |
MOZ_TRY_VAR(nameNode, newName(name)); |
| 6166 |
if (!noteDeclaredName(name, DeclarationKind::Const, pos())) { |
| 6167 |
return errorResult(); |
| 6168 |
} |
| 6169 |
|
| 6170 |
Node kid; |
| 6171 |
MOZ_TRY_VAR(kid, assignExpr(InAllowed, YieldIsName, TripledotProhibited)); |
| 6172 |
|
| 6173 |
if (!matchOrInsertSemicolon()) { |
| 6174 |
return errorResult(); |
| 6175 |
} |
| 6176 |
|
| 6177 |
BinaryNodeType node; |
| 6178 |
MOZ_TRY_VAR(node, handler_.newExportDefaultDeclaration( |
| 6179 |
kid, nameNode, TokenPos(begin, pos().end))); |
| 6180 |
|
| 6181 |
if (!processExport(node)) { |
| 6182 |
return errorResult(); |
| 6183 |
} |
| 6184 |
|
| 6185 |
return node; |
| 6186 |
} |
| 6187 |
|
| 6188 |
template <class ParseHandler, typename Unit> |
| 6189 |
typename ParseHandler::BinaryNodeResult |
| 6190 |
GeneralParser<ParseHandler, Unit>::exportDefault(uint32_t begin) { |
| 6191 |
if (!abortIfSyntaxParser()) { |
| 6192 |
return errorResult(); |
| 6193 |
} |
| 6194 |
|
| 6195 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Default)); |
| 6196 |
|
| 6197 |
TokenKind tt; |
| 6198 |
if (!tokenStream.getToken(&tt, TokenStream::SlashIsRegExp)) { |
| 6199 |
return errorResult(); |
| 6200 |
} |
| 6201 |
|
| 6202 |
if (!checkExportedName(TaggedParserAtomIndex::WellKnown::default_())) { |
| 6203 |
return errorResult(); |
| 6204 |
} |
| 6205 |
|
| 6206 |
switch (tt) { |
| 6207 |
case TokenKind::Function: |
| 6208 |
return exportDefaultFunctionDeclaration(begin, pos().begin); |
| 6209 |
|
| 6210 |
case TokenKind::Async: { |
| 6211 |
TokenKind nextSameLine = TokenKind::Eof; |
| 6212 |
if (!tokenStream.peekTokenSameLine(&nextSameLine)) { |
| 6213 |
return errorResult(); |
| 6214 |
} |
| 6215 |
|
| 6216 |
if (nextSameLine == TokenKind::Function) { |
| 6217 |
uint32_t toStringStart = pos().begin; |
| 6218 |
tokenStream.consumeKnownToken(TokenKind::Function); |
| 6219 |
return exportDefaultFunctionDeclaration( |
| 6220 |
begin, toStringStart, FunctionAsyncKind::AsyncFunction); |
| 6221 |
} |
| 6222 |
|
| 6223 |
anyChars.ungetToken(); |
| 6224 |
return exportDefaultAssignExpr(begin); |
| 6225 |
} |
| 6226 |
|
| 6227 |
case TokenKind::Class: |
| 6228 |
return exportDefaultClassDeclaration(begin); |
| 6229 |
|
| 6230 |
default: |
| 6231 |
anyChars.ungetToken(); |
| 6232 |
return exportDefaultAssignExpr(begin); |
| 6233 |
} |
| 6234 |
} |
| 6235 |
|
| 6236 |
template <class ParseHandler, typename Unit> |
| 6237 |
typename ParseHandler::NodeResult |
| 6238 |
GeneralParser<ParseHandler, Unit>::exportDeclaration() { |
| 6239 |
if (!abortIfSyntaxParser()) { |
| 6240 |
return errorResult(); |
| 6241 |
} |
| 6242 |
|
| 6243 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Export)); |
| 6244 |
|
| 6245 |
if (!pc_->atModuleLevel()) { |
| 6246 |
error(JSMSG_EXPORT_DECL_AT_TOP_LEVEL); |
| 6247 |
return errorResult(); |
| 6248 |
} |
| 6249 |
|
| 6250 |
uint32_t begin = pos().begin; |
| 6251 |
|
| 6252 |
TokenKind tt; |
| 6253 |
if (!tokenStream.getToken(&tt)) { |
| 6254 |
return errorResult(); |
| 6255 |
} |
| 6256 |
switch (tt) { |
| 6257 |
case TokenKind::Mul: |
| 6258 |
return exportBatch(begin); |
| 6259 |
|
| 6260 |
case TokenKind::LeftCurly: |
| 6261 |
return exportClause(begin); |
| 6262 |
|
| 6263 |
case TokenKind::Var: |
| 6264 |
return exportVariableStatement(begin); |
| 6265 |
|
| 6266 |
case TokenKind::Function: |
| 6267 |
return exportFunctionDeclaration(begin, pos().begin); |
| 6268 |
|
| 6269 |
case TokenKind::Async: { |
| 6270 |
TokenKind nextSameLine = TokenKind::Eof; |
| 6271 |
if (!tokenStream.peekTokenSameLine(&nextSameLine)) { |
| 6272 |
return errorResult(); |
| 6273 |
} |
| 6274 |
|
| 6275 |
if (nextSameLine == TokenKind::Function) { |
| 6276 |
uint32_t toStringStart = pos().begin; |
| 6277 |
tokenStream.consumeKnownToken(TokenKind::Function); |
| 6278 |
return exportFunctionDeclaration(begin, toStringStart, |
| 6279 |
FunctionAsyncKind::AsyncFunction); |
| 6280 |
} |
| 6281 |
|
| 6282 |
error(JSMSG_DECLARATION_AFTER_EXPORT); |
| 6283 |
return errorResult(); |
| 6284 |
} |
| 6285 |
|
| 6286 |
case TokenKind::Class: |
| 6287 |
return exportClassDeclaration(begin); |
| 6288 |
|
| 6289 |
case TokenKind::Const: |
| 6290 |
return exportLexicalDeclaration(begin, DeclarationKind::Const); |
| 6291 |
|
| 6292 |
case TokenKind::Let: |
| 6293 |
return exportLexicalDeclaration(begin, DeclarationKind::Let); |
| 6294 |
|
| 6295 |
case TokenKind::Default: |
| 6296 |
return exportDefault(begin); |
| 6297 |
|
| 6298 |
default: |
| 6299 |
error(JSMSG_DECLARATION_AFTER_EXPORT); |
| 6300 |
return errorResult(); |
| 6301 |
} |
| 6302 |
} |
| 6303 |
|
| 6304 |
template <class ParseHandler, typename Unit> |
| 6305 |
typename ParseHandler::UnaryNodeResult |
| 6306 |
GeneralParser<ParseHandler, Unit>::expressionStatement( |
| 6307 |
YieldHandling yieldHandling, InvokedPrediction invoked) { |
| 6308 |
anyChars.ungetToken(); |
| 6309 |
Node pnexpr; |
| 6310 |
MOZ_TRY_VAR(pnexpr, expr(InAllowed, yieldHandling, TripledotProhibited, |
| 6311 |
/* possibleError = */ nullptr, invoked)); |
| 6312 |
if (!matchOrInsertSemicolon()) { |
| 6313 |
return errorResult(); |
| 6314 |
} |
| 6315 |
return handler_.newExprStatement(pnexpr, pos().end); |
| 6316 |
} |
| 6317 |
|
| 6318 |
template <class ParseHandler, typename Unit> |
| 6319 |
typename ParseHandler::NodeResult |
| 6320 |
GeneralParser<ParseHandler, Unit>::consequentOrAlternative( |
| 6321 |
YieldHandling yieldHandling) { |
| 6322 |
TokenKind next; |
| 6323 |
if (!tokenStream.peekToken(&next, TokenStream::SlashIsRegExp)) { |
| 6324 |
return errorResult(); |
| 6325 |
} |
| 6326 |
|
| 6327 |
// Annex B.3.4 says that unbraced FunctionDeclarations under if/else in |
| 6328 |
// non-strict code act as if they were braced: |if (x) function f() {}| |
| 6329 |
// parses as |if (x) { function f() {} }|. |
| 6330 |
// |
| 6331 |
// Careful! FunctionDeclaration doesn't include generators or async |
| 6332 |
// functions. |
| 6333 |
if (next == TokenKind::Function) { |
| 6334 |
tokenStream.consumeKnownToken(next, TokenStream::SlashIsRegExp); |
| 6335 |
|
| 6336 |
// Parser::statement would handle this, but as this function handles |
| 6337 |
// every other error case, it seems best to handle this. |
| 6338 |
if (pc_->sc()->strict()) { |
| 6339 |
error(JSMSG_FORBIDDEN_AS_STATEMENT, "function declarations"); |
| 6340 |
return errorResult(); |
| 6341 |
} |
| 6342 |
|
| 6343 |
TokenKind maybeStar; |
| 6344 |
if (!tokenStream.peekToken(&maybeStar)) { |
| 6345 |
return errorResult(); |
| 6346 |
} |
| 6347 |
|
| 6348 |
if (maybeStar == TokenKind::Mul) { |
| 6349 |
error(JSMSG_FORBIDDEN_AS_STATEMENT, "generator declarations"); |
| 6350 |
return errorResult(); |
| 6351 |
} |
| 6352 |
|
| 6353 |
ParseContext::Statement stmt(pc_, StatementKind::Block); |
| 6354 |
ParseContext::Scope scope(this); |
| 6355 |
if (!scope.init(pc_)) { |
| 6356 |
return errorResult(); |
| 6357 |
} |
| 6358 |
|
| 6359 |
TokenPos funcPos = pos(); |
| 6360 |
Node fun; |
| 6361 |
MOZ_TRY_VAR(fun, functionStmt(pos().begin, yieldHandling, NameRequired)); |
| 6362 |
|
| 6363 |
ListNodeType block; |
| 6364 |
MOZ_TRY_VAR(block, handler_.newStatementList(funcPos)); |
| 6365 |
|
| 6366 |
handler_.addStatementToList(block, fun); |
| 6367 |
return finishLexicalScope(scope, block); |
| 6368 |
} |
| 6369 |
|
| 6370 |
return statement(yieldHandling); |
| 6371 |
} |
| 6372 |
|
| 6373 |
template <class ParseHandler, typename Unit> |
| 6374 |
typename ParseHandler::TernaryNodeResult |
| 6375 |
GeneralParser<ParseHandler, Unit>::ifStatement(YieldHandling yieldHandling) { |
| 6376 |
Vector<Node, 4> condList(fc_), thenList(fc_); |
| 6377 |
Vector<uint32_t, 4> posList(fc_); |
| 6378 |
Node elseBranch; |
| 6379 |
|
| 6380 |
ParseContext::Statement stmt(pc_, StatementKind::If); |
| 6381 |
|
| 6382 |
while (true) { |
| 6383 |
uint32_t begin = pos().begin; |
| 6384 |
|
| 6385 |
/* An IF node has three kids: condition, then, and optional else. */ |
| 6386 |
Node cond; |
| 6387 |
MOZ_TRY_VAR(cond, condition(InAllowed, yieldHandling)); |
| 6388 |
|
| 6389 |
TokenKind tt; |
| 6390 |
if (!tokenStream.peekToken(&tt, TokenStream::SlashIsRegExp)) { |
| 6391 |
return errorResult(); |
| 6392 |
} |
| 6393 |
|
| 6394 |
Node thenBranch; |
| 6395 |
MOZ_TRY_VAR(thenBranch, consequentOrAlternative(yieldHandling)); |
| 6396 |
|
| 6397 |
if (!condList.append(cond) || !thenList.append(thenBranch) || |
| 6398 |
!posList.append(begin)) { |
| 6399 |
return errorResult(); |
| 6400 |
} |
| 6401 |
|
| 6402 |
bool matched; |
| 6403 |
if (!tokenStream.matchToken(&matched, TokenKind::Else, |
| 6404 |
TokenStream::SlashIsRegExp)) { |
| 6405 |
return errorResult(); |
| 6406 |
} |
| 6407 |
if (matched) { |
| 6408 |
if (!tokenStream.matchToken(&matched, TokenKind::If, |
| 6409 |
TokenStream::SlashIsRegExp)) { |
| 6410 |
return errorResult(); |
| 6411 |
} |
| 6412 |
if (matched) { |
| 6413 |
continue; |
| 6414 |
} |
| 6415 |
MOZ_TRY_VAR(elseBranch, consequentOrAlternative(yieldHandling)); |
| 6416 |
} else { |
| 6417 |
elseBranch = null(); |
| 6418 |
} |
| 6419 |
break; |
| 6420 |
} |
| 6421 |
|
| 6422 |
TernaryNodeType ifNode; |
| 6423 |
for (int i = condList.length() - 1; i >= 0; i--) { |
| 6424 |
MOZ_TRY_VAR(ifNode, handler_.newIfStatement(posList[i], condList[i], |
| 6425 |
thenList[i], elseBranch)); |
| 6426 |
elseBranch = ifNode; |
| 6427 |
} |
| 6428 |
|
| 6429 |
return ifNode; |
| 6430 |
} |
| 6431 |
|
| 6432 |
template <class ParseHandler, typename Unit> |
| 6433 |
typename ParseHandler::BinaryNodeResult |
| 6434 |
GeneralParser<ParseHandler, Unit>::doWhileStatement( |
| 6435 |
YieldHandling yieldHandling) { |
| 6436 |
uint32_t begin = pos().begin; |
| 6437 |
ParseContext::Statement stmt(pc_, StatementKind::DoLoop); |
| 6438 |
Node body; |
| 6439 |
MOZ_TRY_VAR(body, statement(yieldHandling)); |
| 6440 |
if (!mustMatchToken(TokenKind::While, JSMSG_WHILE_AFTER_DO)) { |
| 6441 |
return errorResult(); |
| 6442 |
} |
| 6443 |
Node cond; |
| 6444 |
MOZ_TRY_VAR(cond, condition(InAllowed, yieldHandling)); |
| 6445 |
|
| 6446 |
// The semicolon after do-while is even more optional than most |
| 6447 |
// semicolons in JS. Web compat required this by 2004: |
| 6448 |
// http://bugzilla.mozilla.org/show_bug.cgi?id=238945 |
| 6449 |
// ES3 and ES5 disagreed, but ES6 conforms to Web reality: |
| 6450 |
// https://bugs.ecmascript.org/show_bug.cgi?id=157 |
| 6451 |
// To parse |do {} while (true) false| correctly, use SlashIsRegExp. |
| 6452 |
bool ignored; |
| 6453 |
if (!tokenStream.matchToken(&ignored, TokenKind::Semi, |
| 6454 |
TokenStream::SlashIsRegExp)) { |
| 6455 |
return errorResult(); |
| 6456 |
} |
| 6457 |
return handler_.newDoWhileStatement(body, cond, TokenPos(begin, pos().end)); |
| 6458 |
} |
| 6459 |
|
| 6460 |
template <class ParseHandler, typename Unit> |
| 6461 |
typename ParseHandler::BinaryNodeResult |
| 6462 |
GeneralParser<ParseHandler, Unit>::whileStatement(YieldHandling yieldHandling) { |
| 6463 |
uint32_t begin = pos().begin; |
| 6464 |
ParseContext::Statement stmt(pc_, StatementKind::WhileLoop); |
| 6465 |
Node cond; |
| 6466 |
MOZ_TRY_VAR(cond, condition(InAllowed, yieldHandling)); |
| 6467 |
Node body; |
| 6468 |
MOZ_TRY_VAR(body, statement(yieldHandling)); |
| 6469 |
return handler_.newWhileStatement(begin, cond, body); |
| 6470 |
} |
| 6471 |
|
| 6472 |
template <class ParseHandler, typename Unit> |
| 6473 |
bool GeneralParser<ParseHandler, Unit>::matchInOrOf(bool* isForInp, |
| 6474 |
bool* isForOfp) { |
| 6475 |
TokenKind tt; |
| 6476 |
if (!tokenStream.getToken(&tt, TokenStream::SlashIsRegExp)) { |
| 6477 |
return false; |
| 6478 |
} |
| 6479 |
|
| 6480 |
*isForInp = tt == TokenKind::In; |
| 6481 |
*isForOfp = tt == TokenKind::Of; |
| 6482 |
if (!*isForInp && !*isForOfp) { |
| 6483 |
anyChars.ungetToken(); |
| 6484 |
} |
| 6485 |
|
| 6486 |
MOZ_ASSERT_IF(*isForInp || *isForOfp, *isForInp != *isForOfp); |
| 6487 |
return true; |
| 6488 |
} |
| 6489 |
|
| 6490 |
template <class ParseHandler, typename Unit> |
| 6491 |
bool GeneralParser<ParseHandler, Unit>::forHeadStart( |
| 6492 |
YieldHandling yieldHandling, IteratorKind iterKind, |
| 6493 |
ParseNodeKind* forHeadKind, Node* forInitialPart, |
| 6494 |
Maybe<ParseContext::Scope>& forLoopLexicalScope, |
| 6495 |
Node* forInOrOfExpression) { |
| 6496 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::LeftParen)); |
| 6497 |
|
| 6498 |
TokenKind tt; |
| 6499 |
if (!tokenStream.peekToken(&tt, TokenStream::SlashIsRegExp)) { |
| 6500 |
return false; |
| 6501 |
} |
| 6502 |
|
| 6503 |
// Super-duper easy case: |for (;| is a C-style for-loop with no init |
| 6504 |
// component. |
| 6505 |
if (tt == TokenKind::Semi) { |
| 6506 |
*forInitialPart = null(); |
| 6507 |
*forHeadKind = ParseNodeKind::ForHead; |
| 6508 |
return true; |
| 6509 |
} |
| 6510 |
|
| 6511 |
// Parsing after |for (var| is also relatively simple (from this method's |
| 6512 |
// point of view). No block-related work complicates matters, so delegate |
| 6513 |
// to Parser::declaration. |
| 6514 |
if (tt == TokenKind::Var) { |
| 6515 |
tokenStream.consumeKnownToken(tt, TokenStream::SlashIsRegExp); |
| 6516 |
|
| 6517 |
// Pass null for block object because |var| declarations don't use one. |
| 6518 |
MOZ_TRY_VAR_OR_RETURN(*forInitialPart, |
| 6519 |
declarationList(yieldHandling, ParseNodeKind::VarStmt, |
| 6520 |
forHeadKind, forInOrOfExpression), |
| 6521 |
false); |
| 6522 |
return true; |
| 6523 |
} |
| 6524 |
|
| 6525 |
// Otherwise we have a lexical declaration or an expression. |
| 6526 |
|
| 6527 |
// For-in loop backwards compatibility requires that |let| starting a |
| 6528 |
// for-loop that's not a (new to ES6) for-of loop, in non-strict mode code, |
| 6529 |
// parse as an identifier. (|let| in for-of is always a declaration.) |
| 6530 |
// |
| 6531 |
// For-of loops can't start with the token sequence "async of", because that |
| 6532 |
// leads to a shift-reduce conflict when parsing |for (async of => {};;)| or |
| 6533 |
// |for (async of [])|. |
| 6534 |
bool parsingLexicalDeclaration = false; |
| 6535 |
bool letIsIdentifier = false; |
| 6536 |
bool startsWithForOf = false; |
| 6537 |
|
| 6538 |
if (tt == TokenKind::Const) { |
| 6539 |
parsingLexicalDeclaration = true; |
| 6540 |
tokenStream.consumeKnownToken(tt, TokenStream::SlashIsRegExp); |
| 6541 |
} |
| 6542 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 6543 |
else if (tt == TokenKind::Await && options().explicitResourceManagement()) { |
| 6544 |
if (!pc_->isAsync()) { |
| 6545 |
if (pc_->atModuleTopLevel()) { |
| 6546 |
if (!options().topLevelAwait) { |
| 6547 |
error(JSMSG_TOP_LEVEL_AWAIT_NOT_SUPPORTED); |
| 6548 |
return false; |
| 6549 |
} |
| 6550 |
pc_->sc()->asModuleContext()->setIsAsync(); |
| 6551 |
MOZ_ASSERT(pc_->isAsync()); |
| 6552 |
} |
| 6553 |
} |
| 6554 |
if (pc_->isAsync()) { |
| 6555 |
// Try finding evidence of a AwaitUsingDeclaration the syntax for which |
| 6556 |
// would be: |
| 6557 |
// await [no LineTerminator here] using [no LineTerminator here] |
| 6558 |
// identifier |
| 6559 |
tokenStream.consumeKnownToken(tt, TokenStream::SlashIsRegExp); |
| 6560 |
|
| 6561 |
TokenKind nextTok = TokenKind::Eof; |
| 6562 |
if (!tokenStream.peekTokenSameLine(&nextTok, |
| 6563 |
TokenStream::SlashIsRegExp)) { |
| 6564 |
return false; |
| 6565 |
} |
| 6566 |
|
| 6567 |
if (nextTok == TokenKind::Using) { |
| 6568 |
tokenStream.consumeKnownToken(nextTok, TokenStream::SlashIsRegExp); |
| 6569 |
|
| 6570 |
TokenKind nextTokIdent = TokenKind::Eof; |
| 6571 |
if (!tokenStream.peekTokenSameLine(&nextTokIdent)) { |
| 6572 |
return false; |
| 6573 |
} |
| 6574 |
|
| 6575 |
if (TokenKindIsPossibleIdentifier(nextTokIdent)) { |
| 6576 |
parsingLexicalDeclaration = true; |
| 6577 |
} else { |
| 6578 |
anyChars.ungetToken(); // put back using token |
| 6579 |
anyChars.ungetToken(); // put back await token |
| 6580 |
} |
| 6581 |
} else { |
| 6582 |
anyChars.ungetToken(); // put back await token |
| 6583 |
} |
| 6584 |
} |
| 6585 |
} else if (tt == TokenKind::Using && options().explicitResourceManagement()) { |
| 6586 |
tokenStream.consumeKnownToken(tt, TokenStream::SlashIsRegExp); |
| 6587 |
|
| 6588 |
// Look ahead to find either a 'of' token or if not identifier |
| 6589 |
TokenKind nextTok = TokenKind::Eof; |
| 6590 |
if (!tokenStream.peekTokenSameLine(&nextTok)) { |
| 6591 |
return false; |
| 6592 |
} |
| 6593 |
|
| 6594 |
if (nextTok == TokenKind::Of || !TokenKindIsPossibleIdentifier(nextTok)) { |
| 6595 |
anyChars.ungetToken(); // we didnt find a valid case of using decl put |
| 6596 |
// back the token |
| 6597 |
} else { |
| 6598 |
parsingLexicalDeclaration = true; |
| 6599 |
} |
| 6600 |
} |
| 6601 |
#endif |
| 6602 |
else if (tt == TokenKind::Let) { |
| 6603 |
// We could have a {For,Lexical}Declaration, or we could have a |
| 6604 |
// LeftHandSideExpression with lookahead restrictions so it's not |
| 6605 |
// ambiguous with the former. Check for a continuation of the former |
| 6606 |
// to decide which we have. |
| 6607 |
tokenStream.consumeKnownToken(TokenKind::Let, TokenStream::SlashIsRegExp); |
| 6608 |
|
| 6609 |
TokenKind next; |
| 6610 |
if (!tokenStream.peekToken(&next)) { |
| 6611 |
return false; |
| 6612 |
} |
| 6613 |
|
| 6614 |
parsingLexicalDeclaration = nextTokenContinuesLetDeclaration(next); |
| 6615 |
if (!parsingLexicalDeclaration) { |
| 6616 |
// If we end up here, we may have `for (let <reserved word> of/in ...`, |
| 6617 |
// which is not valid. |
| 6618 |
if (next != TokenKind::In && next != TokenKind::Of && |
| 6619 |
TokenKindIsReservedWord(next)) { |
| 6620 |
tokenStream.consumeKnownToken(next); |
| 6621 |
error(JSMSG_UNEXPECTED_TOKEN_NO_EXPECT, TokenKindToDesc(next)); |
| 6622 |
return false; |
| 6623 |
} |
| 6624 |
|
| 6625 |
anyChars.ungetToken(); |
| 6626 |
letIsIdentifier = true; |
| 6627 |
} |
| 6628 |
} else if (tt == TokenKind::Async && iterKind == IteratorKind::Sync) { |
| 6629 |
tokenStream.consumeKnownToken(TokenKind::Async, TokenStream::SlashIsRegExp); |
| 6630 |
|
| 6631 |
TokenKind next; |
| 6632 |
if (!tokenStream.peekToken(&next)) { |
| 6633 |
return false; |
| 6634 |
} |
| 6635 |
|
| 6636 |
if (next == TokenKind::Of) { |
| 6637 |
startsWithForOf = true; |
| 6638 |
} |
| 6639 |
anyChars.ungetToken(); |
| 6640 |
} |
| 6641 |
|
| 6642 |
if (parsingLexicalDeclaration) { |
| 6643 |
if (options().selfHostingMode) { |
| 6644 |
error(JSMSG_SELFHOSTED_LEXICAL); |
| 6645 |
return false; |
| 6646 |
} |
| 6647 |
|
| 6648 |
forLoopLexicalScope.emplace(this); |
| 6649 |
if (!forLoopLexicalScope->init(pc_)) { |
| 6650 |
return false; |
| 6651 |
} |
| 6652 |
|
| 6653 |
// Push a temporary ForLoopLexicalHead Statement that allows for |
| 6654 |
// lexical declarations, as they are usually allowed only in braced |
| 6655 |
// statements. |
| 6656 |
ParseContext::Statement forHeadStmt(pc_, StatementKind::ForLoopLexicalHead); |
| 6657 |
|
| 6658 |
ParseNodeKind declKind; |
| 6659 |
switch (tt) { |
| 6660 |
case TokenKind::Const: |
| 6661 |
declKind = ParseNodeKind::ConstDecl; |
| 6662 |
break; |
| 6663 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 6664 |
case TokenKind::Using: |
| 6665 |
declKind = ParseNodeKind::UsingDecl; |
| 6666 |
break; |
| 6667 |
case TokenKind::Await: |
| 6668 |
declKind = ParseNodeKind::AwaitUsingDecl; |
| 6669 |
break; |
| 6670 |
#endif |
| 6671 |
case TokenKind::Let: |
| 6672 |
declKind = ParseNodeKind::LetDecl; |
| 6673 |
break; |
| 6674 |
default: |
| 6675 |
MOZ_CRASH("unexpected node kind"); |
| 6676 |
} |
| 6677 |
|
| 6678 |
MOZ_TRY_VAR_OR_RETURN(*forInitialPart, |
| 6679 |
declarationList(yieldHandling, declKind, forHeadKind, |
| 6680 |
forInOrOfExpression), |
| 6681 |
false); |
| 6682 |
return true; |
| 6683 |
} |
| 6684 |
|
| 6685 |
uint32_t exprOffset; |
| 6686 |
if (!tokenStream.peekOffset(&exprOffset, TokenStream::SlashIsRegExp)) { |
| 6687 |
return false; |
| 6688 |
} |
| 6689 |
|
| 6690 |
// Finally, handle for-loops that start with expressions. Pass |
| 6691 |
// |InProhibited| so that |in| isn't parsed in a RelationalExpression as a |
| 6692 |
// binary operator. |in| makes it a for-in loop, *not* an |in| expression. |
| 6693 |
PossibleError possibleError(*this); |
| 6694 |
MOZ_TRY_VAR_OR_RETURN( |
| 6695 |
*forInitialPart, |
| 6696 |
expr(InProhibited, yieldHandling, TripledotProhibited, &possibleError), |
| 6697 |
false); |
| 6698 |
|
| 6699 |
bool isForIn, isForOf; |
| 6700 |
if (!matchInOrOf(&isForIn, &isForOf)) { |
| 6701 |
return false; |
| 6702 |
} |
| 6703 |
|
| 6704 |
// If we don't encounter 'in'/'of', we have a for(;;) loop. We've handled |
| 6705 |
// the init expression; the caller handles the rest. |
| 6706 |
if (!isForIn && !isForOf) { |
| 6707 |
if (!possibleError.checkForExpressionError()) { |
| 6708 |
return false; |
| 6709 |
} |
| 6710 |
|
| 6711 |
*forHeadKind = ParseNodeKind::ForHead; |
| 6712 |
return true; |
| 6713 |
} |
| 6714 |
|
| 6715 |
MOZ_ASSERT(isForIn != isForOf); |
| 6716 |
|
| 6717 |
// In a for-of loop, 'let' that starts the loop head is a |let| keyword, |
| 6718 |
// per the [lookahead ≠ let] restriction on the LeftHandSideExpression |
| 6719 |
// variant of such loops. Expressions that start with |let| can't be used |
| 6720 |
// here. |
| 6721 |
// |
| 6722 |
// var let = {}; |
| 6723 |
// for (let.prop of [1]) // BAD |
| 6724 |
// break; |
| 6725 |
// |
| 6726 |
// See ES6 13.7. |
| 6727 |
if (isForOf && letIsIdentifier) { |
| 6728 |
errorAt(exprOffset, JSMSG_BAD_STARTING_FOROF_LHS, "let"); |
| 6729 |
return false; |
| 6730 |
} |
| 6731 |
|
| 6732 |
// In a for-of loop, the LeftHandSideExpression isn't allowed to be an |
| 6733 |
// identifier named "async" per the [lookahead ≠ async of] restriction. |
| 6734 |
if (isForOf && startsWithForOf) { |
| 6735 |
errorAt(exprOffset, JSMSG_BAD_STARTING_FOROF_LHS, "async of"); |
| 6736 |
return false; |
| 6737 |
} |
| 6738 |
|
| 6739 |
*forHeadKind = isForIn ? ParseNodeKind::ForIn : ParseNodeKind::ForOf; |
| 6740 |
|
| 6741 |
// Verify the left-hand side expression doesn't have a forbidden form. |
| 6742 |
if (handler_.isUnparenthesizedDestructuringPattern(*forInitialPart)) { |
| 6743 |
if (!possibleError.checkForDestructuringErrorOrWarning()) { |
| 6744 |
return false; |
| 6745 |
} |
| 6746 |
} else if (handler_.isName(*forInitialPart)) { |
| 6747 |
if (const char* chars = nameIsArgumentsOrEval(*forInitialPart)) { |
| 6748 |
// |chars| is "arguments" or "eval" here. |
| 6749 |
if (!strictModeErrorAt(exprOffset, JSMSG_BAD_STRICT_ASSIGN, chars)) { |
| 6750 |
return false; |
| 6751 |
} |
| 6752 |
} |
| 6753 |
} else if (handler_.isArgumentsLength(*forInitialPart)) { |
| 6754 |
pc_->sc()->setIneligibleForArgumentsLength(); |
| 6755 |
} else if (handler_.isPropertyOrPrivateMemberAccess(*forInitialPart)) { |
| 6756 |
// Permitted: no additional testing/fixup needed. |
| 6757 |
} else if (handler_.isFunctionCall(*forInitialPart)) { |
| 6758 |
if (!strictModeErrorAt(exprOffset, JSMSG_BAD_FOR_LEFTSIDE)) { |
| 6759 |
return false; |
| 6760 |
} |
| 6761 |
} else { |
| 6762 |
errorAt(exprOffset, JSMSG_BAD_FOR_LEFTSIDE); |
| 6763 |
return false; |
| 6764 |
} |
| 6765 |
|
| 6766 |
if (!possibleError.checkForExpressionError()) { |
| 6767 |
return false; |
| 6768 |
} |
| 6769 |
|
| 6770 |
// Finally, parse the iterated expression, making the for-loop's closing |
| 6771 |
// ')' the next token. |
| 6772 |
MOZ_TRY_VAR_OR_RETURN(*forInOrOfExpression, |
| 6773 |
expressionAfterForInOrOf(*forHeadKind, yieldHandling), |
| 6774 |
false); |
| 6775 |
return true; |
| 6776 |
} |
| 6777 |
|
| 6778 |
template <class ParseHandler, typename Unit> |
| 6779 |
typename ParseHandler::NodeResult |
| 6780 |
GeneralParser<ParseHandler, Unit>::forStatement(YieldHandling yieldHandling) { |
| 6781 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::For)); |
| 6782 |
|
| 6783 |
uint32_t begin = pos().begin; |
| 6784 |
|
| 6785 |
ParseContext::Statement stmt(pc_, StatementKind::ForLoop); |
| 6786 |
|
| 6787 |
IteratorKind iterKind = IteratorKind::Sync; |
| 6788 |
unsigned iflags = 0; |
| 6789 |
|
| 6790 |
if (pc_->isAsync() || pc_->sc()->isModuleContext()) { |
| 6791 |
bool matched; |
| 6792 |
if (!tokenStream.matchToken(&matched, TokenKind::Await)) { |
| 6793 |
return errorResult(); |
| 6794 |
} |
| 6795 |
|
| 6796 |
// If we come across a top level await here, mark the module as async. |
| 6797 |
if (matched && pc_->sc()->isModuleContext() && !pc_->isAsync()) { |
| 6798 |
if (!options().topLevelAwait) { |
| 6799 |
error(JSMSG_TOP_LEVEL_AWAIT_NOT_SUPPORTED); |
| 6800 |
return errorResult(); |
| 6801 |
} |
| 6802 |
pc_->sc()->asModuleContext()->setIsAsync(); |
| 6803 |
MOZ_ASSERT(pc_->isAsync()); |
| 6804 |
} |
| 6805 |
|
| 6806 |
if (matched) { |
| 6807 |
iflags |= JSITER_FORAWAITOF; |
| 6808 |
iterKind = IteratorKind::Async; |
| 6809 |
} |
| 6810 |
} |
| 6811 |
|
| 6812 |
if (!mustMatchToken(TokenKind::LeftParen, [this](TokenKind actual) { |
| 6813 |
this->error((actual == TokenKind::Await && !this->pc_->isAsync()) |
| 6814 |
? JSMSG_FOR_AWAIT_OUTSIDE_ASYNC |
| 6815 |
: JSMSG_PAREN_AFTER_FOR); |
| 6816 |
})) { |
| 6817 |
return errorResult(); |
| 6818 |
} |
| 6819 |
|
| 6820 |
// ParseNodeKind::ForHead, ParseNodeKind::ForIn, or |
| 6821 |
// ParseNodeKind::ForOf depending on the loop type. |
| 6822 |
ParseNodeKind headKind; |
| 6823 |
|
| 6824 |
// |x| in either |for (x; ...; ...)| or |for (x in/of ...)|. |
| 6825 |
Node startNode; |
| 6826 |
|
| 6827 |
// The next two variables are used to implement `for (let/const ...)`. |
| 6828 |
// |
| 6829 |
// We generate an implicit block, wrapping the whole loop, to store loop |
| 6830 |
// variables declared this way. Note that if the loop uses `for (var...)` |
| 6831 |
// instead, those variables go on some existing enclosing scope, so no |
| 6832 |
// implicit block scope is created. |
| 6833 |
// |
| 6834 |
// Both variables remain null/none if the loop is any other form. |
| 6835 |
|
| 6836 |
// The static block scope for the implicit block scope. |
| 6837 |
Maybe<ParseContext::Scope> forLoopLexicalScope; |
| 6838 |
|
| 6839 |
// The expression being iterated over, for for-in/of loops only. Unused |
| 6840 |
// for for(;;) loops. |
| 6841 |
Node iteratedExpr; |
| 6842 |
|
| 6843 |
// Parse the entirety of the loop-head for a for-in/of loop (so the next |
| 6844 |
// token is the closing ')'): |
| 6845 |
// |
| 6846 |
// for (... in/of ...) ... |
| 6847 |
// ^next token |
| 6848 |
// |
| 6849 |
// ...OR, parse up to the first ';' in a C-style for-loop: |
| 6850 |
// |
| 6851 |
// for (...; ...; ...) ... |
| 6852 |
// ^next token |
| 6853 |
// |
| 6854 |
// In either case the subsequent token can be consistently accessed using |
| 6855 |
// TokenStream::SlashIsDiv semantics. |
| 6856 |
if (!forHeadStart(yieldHandling, iterKind, &headKind, &startNode, |
| 6857 |
forLoopLexicalScope, &iteratedExpr)) { |
| 6858 |
return errorResult(); |
| 6859 |
} |
| 6860 |
|
| 6861 |
MOZ_ASSERT(headKind == ParseNodeKind::ForIn || |
| 6862 |
headKind == ParseNodeKind::ForOf || |
| 6863 |
headKind == ParseNodeKind::ForHead); |
| 6864 |
|
| 6865 |
if (iterKind == IteratorKind::Async && headKind != ParseNodeKind::ForOf) { |
| 6866 |
errorAt(begin, JSMSG_FOR_AWAIT_NOT_OF); |
| 6867 |
return errorResult(); |
| 6868 |
} |
| 6869 |
|
| 6870 |
TernaryNodeType forHead; |
| 6871 |
if (headKind == ParseNodeKind::ForHead) { |
| 6872 |
Node init = startNode; |
| 6873 |
|
| 6874 |
// Look for an operand: |for (;| means we might have already examined |
| 6875 |
// this semicolon with that modifier. |
| 6876 |
if (!mustMatchToken(TokenKind::Semi, JSMSG_SEMI_AFTER_FOR_INIT)) { |
| 6877 |
return errorResult(); |
| 6878 |
} |
| 6879 |
|
| 6880 |
TokenKind tt; |
| 6881 |
if (!tokenStream.peekToken(&tt, TokenStream::SlashIsRegExp)) { |
| 6882 |
return errorResult(); |
| 6883 |
} |
| 6884 |
|
| 6885 |
Node test; |
| 6886 |
if (tt == TokenKind::Semi) { |
| 6887 |
test = null(); |
| 6888 |
} else { |
| 6889 |
MOZ_TRY_VAR(test, expr(InAllowed, yieldHandling, TripledotProhibited)); |
| 6890 |
} |
| 6891 |
|
| 6892 |
if (!mustMatchToken(TokenKind::Semi, JSMSG_SEMI_AFTER_FOR_COND)) { |
| 6893 |
return errorResult(); |
| 6894 |
} |
| 6895 |
|
| 6896 |
if (!tokenStream.peekToken(&tt, TokenStream::SlashIsRegExp)) { |
| 6897 |
return errorResult(); |
| 6898 |
} |
| 6899 |
|
| 6900 |
Node update; |
| 6901 |
if (tt == TokenKind::RightParen) { |
| 6902 |
update = null(); |
| 6903 |
} else { |
| 6904 |
MOZ_TRY_VAR(update, expr(InAllowed, yieldHandling, TripledotProhibited)); |
| 6905 |
} |
| 6906 |
|
| 6907 |
if (!mustMatchToken(TokenKind::RightParen, JSMSG_PAREN_AFTER_FOR_CTRL)) { |
| 6908 |
return errorResult(); |
| 6909 |
} |
| 6910 |
|
| 6911 |
TokenPos headPos(begin, pos().end); |
| 6912 |
MOZ_TRY_VAR(forHead, handler_.newForHead(init, test, update, headPos)); |
| 6913 |
} else { |
| 6914 |
MOZ_ASSERT(headKind == ParseNodeKind::ForIn || |
| 6915 |
headKind == ParseNodeKind::ForOf); |
| 6916 |
|
| 6917 |
// |target| is the LeftHandSideExpression or declaration to which the |
| 6918 |
// per-iteration value (an arbitrary value exposed by the iteration |
| 6919 |
// protocol, or a string naming a property) is assigned. |
| 6920 |
Node target = startNode; |
| 6921 |
|
| 6922 |
// Parse the rest of the for-in/of head. |
| 6923 |
if (headKind == ParseNodeKind::ForIn) { |
| 6924 |
stmt.refineForKind(StatementKind::ForInLoop); |
| 6925 |
} else { |
| 6926 |
stmt.refineForKind(StatementKind::ForOfLoop); |
| 6927 |
} |
| 6928 |
|
| 6929 |
// Parser::declaration consumed everything up to the closing ')'. That |
| 6930 |
// token follows an {Assignment,}Expression and so must be interpreted |
| 6931 |
// as an operand to be consistent with normal expression tokenizing. |
| 6932 |
if (!mustMatchToken(TokenKind::RightParen, JSMSG_PAREN_AFTER_FOR_CTRL)) { |
| 6933 |
return errorResult(); |
| 6934 |
} |
| 6935 |
|
| 6936 |
TokenPos headPos(begin, pos().end); |
| 6937 |
MOZ_TRY_VAR(forHead, handler_.newForInOrOfHead(headKind, target, |
| 6938 |
iteratedExpr, headPos)); |
| 6939 |
} |
| 6940 |
|
| 6941 |
Node body; |
| 6942 |
MOZ_TRY_VAR(body, statement(yieldHandling)); |
| 6943 |
|
| 6944 |
ForNodeType forLoop; |
| 6945 |
MOZ_TRY_VAR(forLoop, handler_.newForStatement(begin, forHead, body, iflags)); |
| 6946 |
|
| 6947 |
if (forLoopLexicalScope) { |
| 6948 |
return finishLexicalScope(*forLoopLexicalScope, forLoop); |
| 6949 |
} |
| 6950 |
|
| 6951 |
return forLoop; |
| 6952 |
} |
| 6953 |
|
| 6954 |
template <class ParseHandler, typename Unit> |
| 6955 |
typename ParseHandler::SwitchStatementResult |
| 6956 |
GeneralParser<ParseHandler, Unit>::switchStatement( |
| 6957 |
YieldHandling yieldHandling) { |
| 6958 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Switch)); |
| 6959 |
uint32_t begin = pos().begin; |
| 6960 |
|
| 6961 |
if (!mustMatchToken(TokenKind::LeftParen, JSMSG_PAREN_BEFORE_SWITCH)) { |
| 6962 |
return errorResult(); |
| 6963 |
} |
| 6964 |
|
| 6965 |
Node discriminant; |
| 6966 |
MOZ_TRY_VAR(discriminant, |
| 6967 |
exprInParens(InAllowed, yieldHandling, TripledotProhibited)); |
| 6968 |
|
| 6969 |
if (!mustMatchToken(TokenKind::RightParen, JSMSG_PAREN_AFTER_SWITCH)) { |
| 6970 |
return errorResult(); |
| 6971 |
} |
| 6972 |
if (!mustMatchToken(TokenKind::LeftCurly, JSMSG_CURLY_BEFORE_SWITCH)) { |
| 6973 |
return errorResult(); |
| 6974 |
} |
| 6975 |
|
| 6976 |
ParseContext::Statement stmt(pc_, StatementKind::Switch); |
| 6977 |
ParseContext::Scope scope(this); |
| 6978 |
if (!scope.init(pc_)) { |
| 6979 |
return errorResult(); |
| 6980 |
} |
| 6981 |
|
| 6982 |
ListNodeType caseList; |
| 6983 |
MOZ_TRY_VAR(caseList, handler_.newStatementList(pos())); |
| 6984 |
|
| 6985 |
bool seenDefault = false; |
| 6986 |
TokenKind tt; |
| 6987 |
while (true) { |
| 6988 |
if (!tokenStream.getToken(&tt, TokenStream::SlashIsRegExp)) { |
| 6989 |
return errorResult(); |
| 6990 |
} |
| 6991 |
if (tt == TokenKind::RightCurly) { |
| 6992 |
break; |
| 6993 |
} |
| 6994 |
uint32_t caseBegin = pos().begin; |
| 6995 |
|
| 6996 |
Node caseExpr; |
| 6997 |
switch (tt) { |
| 6998 |
case TokenKind::Default: |
| 6999 |
if (seenDefault) { |
| 7000 |
error(JSMSG_TOO_MANY_DEFAULTS); |
| 7001 |
return errorResult(); |
| 7002 |
} |
| 7003 |
seenDefault = true; |
| 7004 |
caseExpr = null(); // The default case has pn_left == nullptr. |
| 7005 |
break; |
| 7006 |
|
| 7007 |
case TokenKind::Case: |
| 7008 |
MOZ_TRY_VAR(caseExpr, |
| 7009 |
expr(InAllowed, yieldHandling, TripledotProhibited)); |
| 7010 |
break; |
| 7011 |
|
| 7012 |
default: |
| 7013 |
error(JSMSG_BAD_SWITCH); |
| 7014 |
return errorResult(); |
| 7015 |
} |
| 7016 |
|
| 7017 |
if (!mustMatchToken(TokenKind::Colon, JSMSG_COLON_AFTER_CASE)) { |
| 7018 |
return errorResult(); |
| 7019 |
} |
| 7020 |
|
| 7021 |
ListNodeType body; |
| 7022 |
MOZ_TRY_VAR(body, handler_.newStatementList(pos())); |
| 7023 |
|
| 7024 |
bool afterReturn = false; |
| 7025 |
bool warnedAboutStatementsAfterReturn = false; |
| 7026 |
uint32_t statementBegin = 0; |
| 7027 |
while (true) { |
| 7028 |
if (!tokenStream.peekToken(&tt, TokenStream::SlashIsRegExp)) { |
| 7029 |
return errorResult(); |
| 7030 |
} |
| 7031 |
if (tt == TokenKind::RightCurly || tt == TokenKind::Case || |
| 7032 |
tt == TokenKind::Default) { |
| 7033 |
break; |
| 7034 |
} |
| 7035 |
if (afterReturn) { |
| 7036 |
if (!tokenStream.peekOffset(&statementBegin, |
| 7037 |
TokenStream::SlashIsRegExp)) { |
| 7038 |
return errorResult(); |
| 7039 |
} |
| 7040 |
} |
| 7041 |
Node stmt; |
| 7042 |
MOZ_TRY_VAR(stmt, statementListItem(yieldHandling)); |
| 7043 |
if (!warnedAboutStatementsAfterReturn) { |
| 7044 |
if (afterReturn) { |
| 7045 |
if (!handler_.isStatementPermittedAfterReturnStatement(stmt)) { |
| 7046 |
if (!warningAt(statementBegin, JSMSG_STMT_AFTER_RETURN)) { |
| 7047 |
return errorResult(); |
| 7048 |
} |
| 7049 |
|
| 7050 |
warnedAboutStatementsAfterReturn = true; |
| 7051 |
} |
| 7052 |
} else if (handler_.isReturnStatement(stmt)) { |
| 7053 |
afterReturn = true; |
| 7054 |
} |
| 7055 |
} |
| 7056 |
handler_.addStatementToList(body, stmt); |
| 7057 |
} |
| 7058 |
|
| 7059 |
CaseClauseType caseClause; |
| 7060 |
MOZ_TRY_VAR(caseClause, |
| 7061 |
handler_.newCaseOrDefault(caseBegin, caseExpr, body)); |
| 7062 |
handler_.addCaseStatementToList(caseList, caseClause); |
| 7063 |
} |
| 7064 |
|
| 7065 |
LexicalScopeNodeType lexicalForCaseList; |
| 7066 |
MOZ_TRY_VAR(lexicalForCaseList, finishLexicalScope(scope, caseList)); |
| 7067 |
|
| 7068 |
handler_.setEndPosition(lexicalForCaseList, pos().end); |
| 7069 |
|
| 7070 |
return handler_.newSwitchStatement(begin, discriminant, lexicalForCaseList, |
| 7071 |
seenDefault); |
| 7072 |
} |
| 7073 |
|
| 7074 |
template <class ParseHandler, typename Unit> |
| 7075 |
typename ParseHandler::ContinueStatementResult |
| 7076 |
GeneralParser<ParseHandler, Unit>::continueStatement( |
| 7077 |
YieldHandling yieldHandling) { |
| 7078 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Continue)); |
| 7079 |
uint32_t begin = pos().begin; |
| 7080 |
|
| 7081 |
TaggedParserAtomIndex label; |
| 7082 |
if (!matchLabel(yieldHandling, &label)) { |
| 7083 |
return errorResult(); |
| 7084 |
} |
| 7085 |
|
| 7086 |
auto validity = pc_->checkContinueStatement(label); |
| 7087 |
if (validity.isErr()) { |
| 7088 |
switch (validity.unwrapErr()) { |
| 7089 |
case ParseContext::ContinueStatementError::NotInALoop: |
| 7090 |
errorAt(begin, JSMSG_BAD_CONTINUE); |
| 7091 |
break; |
| 7092 |
case ParseContext::ContinueStatementError::LabelNotFound: |
| 7093 |
error(JSMSG_LABEL_NOT_FOUND); |
| 7094 |
break; |
| 7095 |
} |
| 7096 |
return errorResult(); |
| 7097 |
} |
| 7098 |
|
| 7099 |
if (!matchOrInsertSemicolon()) { |
| 7100 |
return errorResult(); |
| 7101 |
} |
| 7102 |
|
| 7103 |
return handler_.newContinueStatement(label, TokenPos(begin, pos().end)); |
| 7104 |
} |
| 7105 |
|
| 7106 |
template <class ParseHandler, typename Unit> |
| 7107 |
typename ParseHandler::BreakStatementResult |
| 7108 |
GeneralParser<ParseHandler, Unit>::breakStatement(YieldHandling yieldHandling) { |
| 7109 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Break)); |
| 7110 |
uint32_t begin = pos().begin; |
| 7111 |
|
| 7112 |
TaggedParserAtomIndex label; |
| 7113 |
if (!matchLabel(yieldHandling, &label)) { |
| 7114 |
return errorResult(); |
| 7115 |
} |
| 7116 |
|
| 7117 |
auto validity = pc_->checkBreakStatement(label); |
| 7118 |
if (validity.isErr()) { |
| 7119 |
switch (validity.unwrapErr()) { |
| 7120 |
case ParseContext::BreakStatementError::ToughBreak: |
| 7121 |
errorAt(begin, JSMSG_TOUGH_BREAK); |
| 7122 |
return errorResult(); |
| 7123 |
case ParseContext::BreakStatementError::LabelNotFound: |
| 7124 |
error(JSMSG_LABEL_NOT_FOUND); |
| 7125 |
return errorResult(); |
| 7126 |
} |
| 7127 |
} |
| 7128 |
|
| 7129 |
if (!matchOrInsertSemicolon()) { |
| 7130 |
return errorResult(); |
| 7131 |
} |
| 7132 |
|
| 7133 |
return handler_.newBreakStatement(label, TokenPos(begin, pos().end)); |
| 7134 |
} |
| 7135 |
|
| 7136 |
template <class ParseHandler, typename Unit> |
| 7137 |
typename ParseHandler::UnaryNodeResult |
| 7138 |
GeneralParser<ParseHandler, Unit>::returnStatement( |
| 7139 |
YieldHandling yieldHandling) { |
| 7140 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Return)); |
| 7141 |
uint32_t begin = pos().begin; |
| 7142 |
|
| 7143 |
MOZ_ASSERT(pc_->isFunctionBox()); |
| 7144 |
|
| 7145 |
// Parse an optional operand. |
| 7146 |
// |
| 7147 |
// This is ugly, but we don't want to require a semicolon. |
| 7148 |
Node exprNode; |
| 7149 |
TokenKind tt = TokenKind::Eof; |
| 7150 |
if (!tokenStream.peekTokenSameLine(&tt, TokenStream::SlashIsRegExp)) { |
| 7151 |
return errorResult(); |
| 7152 |
} |
| 7153 |
switch (tt) { |
| 7154 |
case TokenKind::Eol: |
| 7155 |
case TokenKind::Eof: |
| 7156 |
case TokenKind::Semi: |
| 7157 |
case TokenKind::RightCurly: |
| 7158 |
exprNode = null(); |
| 7159 |
break; |
| 7160 |
default: { |
| 7161 |
MOZ_TRY_VAR(exprNode, |
| 7162 |
expr(InAllowed, yieldHandling, TripledotProhibited)); |
| 7163 |
} |
| 7164 |
} |
| 7165 |
|
| 7166 |
if (!matchOrInsertSemicolon()) { |
| 7167 |
return errorResult(); |
| 7168 |
} |
| 7169 |
|
| 7170 |
return handler_.newReturnStatement(exprNode, TokenPos(begin, pos().end)); |
| 7171 |
} |
| 7172 |
|
| 7173 |
template <class ParseHandler, typename Unit> |
| 7174 |
typename ParseHandler::UnaryNodeResult |
| 7175 |
GeneralParser<ParseHandler, Unit>::yieldExpression(InHandling inHandling) { |
| 7176 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Yield)); |
| 7177 |
uint32_t begin = pos().begin; |
| 7178 |
|
| 7179 |
MOZ_ASSERT(pc_->isGenerator()); |
| 7180 |
MOZ_ASSERT(pc_->isFunctionBox()); |
| 7181 |
|
| 7182 |
pc_->lastYieldOffset = begin; |
| 7183 |
|
| 7184 |
Node exprNode; |
| 7185 |
ParseNodeKind kind = ParseNodeKind::YieldExpr; |
| 7186 |
TokenKind tt = TokenKind::Eof; |
| 7187 |
if (!tokenStream.peekTokenSameLine(&tt, TokenStream::SlashIsRegExp)) { |
| 7188 |
return errorResult(); |
| 7189 |
} |
| 7190 |
switch (tt) { |
| 7191 |
// TokenKind::Eol is special; it implements the [no LineTerminator here] |
| 7192 |
// quirk in the grammar. |
| 7193 |
case TokenKind::Eol: |
| 7194 |
// The rest of these make up the complete set of tokens that can |
| 7195 |
// appear after any of the places where AssignmentExpression is used |
| 7196 |
// throughout the grammar. Conveniently, none of them can also be the |
| 7197 |
// start an expression. |
| 7198 |
case TokenKind::Eof: |
| 7199 |
case TokenKind::Semi: |
| 7200 |
case TokenKind::RightCurly: |
| 7201 |
case TokenKind::RightBracket: |
| 7202 |
case TokenKind::RightParen: |
| 7203 |
case TokenKind::Colon: |
| 7204 |
case TokenKind::Comma: |
| 7205 |
case TokenKind::In: // Annex B.3.6 `for (x = yield in y) ;` |
| 7206 |
// No value. |
| 7207 |
exprNode = null(); |
| 7208 |
break; |
| 7209 |
case TokenKind::Mul: |
| 7210 |
kind = ParseNodeKind::YieldStarExpr; |
| 7211 |
tokenStream.consumeKnownToken(TokenKind::Mul, TokenStream::SlashIsRegExp); |
| 7212 |
[[fallthrough]]; |
| 7213 |
default: |
| 7214 |
MOZ_TRY_VAR(exprNode, |
| 7215 |
assignExpr(inHandling, YieldIsKeyword, TripledotProhibited)); |
| 7216 |
} |
| 7217 |
if (kind == ParseNodeKind::YieldStarExpr) { |
| 7218 |
return handler_.newYieldStarExpression(begin, exprNode); |
| 7219 |
} |
| 7220 |
return handler_.newYieldExpression(begin, exprNode); |
| 7221 |
} |
| 7222 |
|
| 7223 |
template <class ParseHandler, typename Unit> |
| 7224 |
typename ParseHandler::BinaryNodeResult |
| 7225 |
GeneralParser<ParseHandler, Unit>::withStatement(YieldHandling yieldHandling) { |
| 7226 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::With)); |
| 7227 |
uint32_t begin = pos().begin; |
| 7228 |
|
| 7229 |
if (pc_->sc()->strict()) { |
| 7230 |
if (!strictModeError(JSMSG_STRICT_CODE_WITH)) { |
| 7231 |
return errorResult(); |
| 7232 |
} |
| 7233 |
} |
| 7234 |
|
| 7235 |
if (!mustMatchToken(TokenKind::LeftParen, JSMSG_PAREN_BEFORE_WITH)) { |
| 7236 |
return errorResult(); |
| 7237 |
} |
| 7238 |
|
| 7239 |
Node objectExpr; |
| 7240 |
MOZ_TRY_VAR(objectExpr, |
| 7241 |
exprInParens(InAllowed, yieldHandling, TripledotProhibited)); |
| 7242 |
|
| 7243 |
if (!mustMatchToken(TokenKind::RightParen, JSMSG_PAREN_AFTER_WITH)) { |
| 7244 |
return errorResult(); |
| 7245 |
} |
| 7246 |
|
| 7247 |
Node innerBlock; |
| 7248 |
{ |
| 7249 |
ParseContext::Statement stmt(pc_, StatementKind::With); |
| 7250 |
MOZ_TRY_VAR(innerBlock, statement(yieldHandling)); |
| 7251 |
} |
| 7252 |
|
| 7253 |
pc_->sc()->setBindingsAccessedDynamically(); |
| 7254 |
|
| 7255 |
return handler_.newWithStatement(begin, objectExpr, innerBlock); |
| 7256 |
} |
| 7257 |
|
| 7258 |
template <class ParseHandler, typename Unit> |
| 7259 |
typename ParseHandler::NodeResult |
| 7260 |
GeneralParser<ParseHandler, Unit>::labeledItem(YieldHandling yieldHandling) { |
| 7261 |
TokenKind tt; |
| 7262 |
if (!tokenStream.getToken(&tt, TokenStream::SlashIsRegExp)) { |
| 7263 |
return errorResult(); |
| 7264 |
} |
| 7265 |
|
| 7266 |
if (tt == TokenKind::Function) { |
| 7267 |
TokenKind next; |
| 7268 |
if (!tokenStream.peekToken(&next)) { |
| 7269 |
return errorResult(); |
| 7270 |
} |
| 7271 |
|
| 7272 |
// GeneratorDeclaration is only matched by HoistableDeclaration in |
| 7273 |
// StatementListItem, so generators can't be inside labels. |
| 7274 |
if (next == TokenKind::Mul) { |
| 7275 |
error(JSMSG_GENERATOR_LABEL); |
| 7276 |
return errorResult(); |
| 7277 |
} |
| 7278 |
|
| 7279 |
// Per 13.13.1 it's a syntax error if LabelledItem: FunctionDeclaration |
| 7280 |
// is ever matched. Per Annex B.3.2 that modifies this text, this |
| 7281 |
// applies only to strict mode code. |
| 7282 |
if (pc_->sc()->strict()) { |
| 7283 |
error(JSMSG_FUNCTION_LABEL); |
| 7284 |
return errorResult(); |
| 7285 |
} |
| 7286 |
|
| 7287 |
return functionStmt(pos().begin, yieldHandling, NameRequired); |
| 7288 |
} |
| 7289 |
|
| 7290 |
anyChars.ungetToken(); |
| 7291 |
return statement(yieldHandling); |
| 7292 |
} |
| 7293 |
|
| 7294 |
template <class ParseHandler, typename Unit> |
| 7295 |
typename ParseHandler::LabeledStatementResult |
| 7296 |
GeneralParser<ParseHandler, Unit>::labeledStatement( |
| 7297 |
YieldHandling yieldHandling) { |
| 7298 |
TaggedParserAtomIndex label = labelIdentifier(yieldHandling); |
| 7299 |
if (!label) { |
| 7300 |
return errorResult(); |
| 7301 |
} |
| 7302 |
|
| 7303 |
auto hasSameLabel = [&label](ParseContext::LabelStatement* stmt) { |
| 7304 |
return stmt->label() == label; |
| 7305 |
}; |
| 7306 |
|
| 7307 |
uint32_t begin = pos().begin; |
| 7308 |
|
| 7309 |
if (pc_->template findInnermostStatement<ParseContext::LabelStatement>( |
| 7310 |
hasSameLabel)) { |
| 7311 |
errorAt(begin, JSMSG_DUPLICATE_LABEL); |
| 7312 |
return errorResult(); |
| 7313 |
} |
| 7314 |
|
| 7315 |
tokenStream.consumeKnownToken(TokenKind::Colon); |
| 7316 |
|
| 7317 |
/* Push a label struct and parse the statement. */ |
| 7318 |
ParseContext::LabelStatement stmt(pc_, label); |
| 7319 |
Node pn; |
| 7320 |
MOZ_TRY_VAR(pn, labeledItem(yieldHandling)); |
| 7321 |
|
| 7322 |
return handler_.newLabeledStatement(label, pn, begin); |
| 7323 |
} |
| 7324 |
|
| 7325 |
template <class ParseHandler, typename Unit> |
| 7326 |
typename ParseHandler::UnaryNodeResult |
| 7327 |
GeneralParser<ParseHandler, Unit>::throwStatement(YieldHandling yieldHandling) { |
| 7328 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Throw)); |
| 7329 |
uint32_t begin = pos().begin; |
| 7330 |
|
| 7331 |
/* ECMA-262 Edition 3 says 'throw [no LineTerminator here] Expr'. */ |
| 7332 |
TokenKind tt = TokenKind::Eof; |
| 7333 |
if (!tokenStream.peekTokenSameLine(&tt, TokenStream::SlashIsRegExp)) { |
| 7334 |
return errorResult(); |
| 7335 |
} |
| 7336 |
if (tt == TokenKind::Eof || tt == TokenKind::Semi || |
| 7337 |
tt == TokenKind::RightCurly) { |
| 7338 |
error(JSMSG_MISSING_EXPR_AFTER_THROW); |
| 7339 |
return errorResult(); |
| 7340 |
} |
| 7341 |
if (tt == TokenKind::Eol) { |
| 7342 |
error(JSMSG_LINE_BREAK_AFTER_THROW); |
| 7343 |
return errorResult(); |
| 7344 |
} |
| 7345 |
|
| 7346 |
Node throwExpr; |
| 7347 |
MOZ_TRY_VAR(throwExpr, expr(InAllowed, yieldHandling, TripledotProhibited)); |
| 7348 |
|
| 7349 |
if (!matchOrInsertSemicolon()) { |
| 7350 |
return errorResult(); |
| 7351 |
} |
| 7352 |
|
| 7353 |
return handler_.newThrowStatement(throwExpr, TokenPos(begin, pos().end)); |
| 7354 |
} |
| 7355 |
|
| 7356 |
template <class ParseHandler, typename Unit> |
| 7357 |
typename ParseHandler::TernaryNodeResult |
| 7358 |
GeneralParser<ParseHandler, Unit>::tryStatement(YieldHandling yieldHandling) { |
| 7359 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Try)); |
| 7360 |
uint32_t begin = pos().begin; |
| 7361 |
|
| 7362 |
/* |
| 7363 |
* try nodes are ternary. |
| 7364 |
* kid1 is the try statement |
| 7365 |
* kid2 is the catch node list or null |
| 7366 |
* kid3 is the finally statement |
| 7367 |
* |
| 7368 |
* catch nodes are binary. |
| 7369 |
* left is the catch-name/pattern or null |
| 7370 |
* right is the catch block |
| 7371 |
* |
| 7372 |
* catch lvalue nodes are either: |
| 7373 |
* a single identifier |
| 7374 |
* TokenKind::RightBracket for a destructuring left-hand side |
| 7375 |
* TokenKind::RightCurly for a destructuring left-hand side |
| 7376 |
* |
| 7377 |
* finally nodes are TokenKind::LeftCurly statement lists. |
| 7378 |
*/ |
| 7379 |
|
| 7380 |
Node innerBlock; |
| 7381 |
{ |
| 7382 |
if (!mustMatchToken(TokenKind::LeftCurly, JSMSG_CURLY_BEFORE_TRY)) { |
| 7383 |
return errorResult(); |
| 7384 |
} |
| 7385 |
|
| 7386 |
uint32_t openedPos = pos().begin; |
| 7387 |
|
| 7388 |
ParseContext::Statement stmt(pc_, StatementKind::Try); |
| 7389 |
ParseContext::Scope scope(this); |
| 7390 |
if (!scope.init(pc_)) { |
| 7391 |
return errorResult(); |
| 7392 |
} |
| 7393 |
|
| 7394 |
MOZ_TRY_VAR(innerBlock, statementList(yieldHandling)); |
| 7395 |
|
| 7396 |
MOZ_TRY_VAR(innerBlock, finishLexicalScope(scope, innerBlock)); |
| 7397 |
|
| 7398 |
if (!mustMatchToken( |
| 7399 |
TokenKind::RightCurly, [this, openedPos](TokenKind actual) { |
| 7400 |
this->reportMissingClosing(JSMSG_CURLY_AFTER_TRY, |
| 7401 |
JSMSG_CURLY_OPENED, openedPos); |
| 7402 |
})) { |
| 7403 |
return errorResult(); |
| 7404 |
} |
| 7405 |
} |
| 7406 |
|
| 7407 |
LexicalScopeNodeType catchScope = null(); |
| 7408 |
TokenKind tt; |
| 7409 |
if (!tokenStream.getToken(&tt)) { |
| 7410 |
return errorResult(); |
| 7411 |
} |
| 7412 |
if (tt == TokenKind::Catch) { |
| 7413 |
/* |
| 7414 |
* Create a lexical scope node around the whole catch clause, |
| 7415 |
* including the head. |
| 7416 |
*/ |
| 7417 |
ParseContext::Statement stmt(pc_, StatementKind::Catch); |
| 7418 |
ParseContext::Scope scope(this); |
| 7419 |
if (!scope.init(pc_)) { |
| 7420 |
return errorResult(); |
| 7421 |
} |
| 7422 |
|
| 7423 |
/* |
| 7424 |
* Legal catch forms are: |
| 7425 |
* catch (lhs) { |
| 7426 |
* catch { |
| 7427 |
* where lhs is a name or a destructuring left-hand side. |
| 7428 |
*/ |
| 7429 |
bool omittedBinding; |
| 7430 |
if (!tokenStream.matchToken(&omittedBinding, TokenKind::LeftCurly)) { |
| 7431 |
return errorResult(); |
| 7432 |
} |
| 7433 |
|
| 7434 |
Node catchName; |
| 7435 |
if (omittedBinding) { |
| 7436 |
catchName = null(); |
| 7437 |
} else { |
| 7438 |
if (!mustMatchToken(TokenKind::LeftParen, JSMSG_PAREN_BEFORE_CATCH)) { |
| 7439 |
return errorResult(); |
| 7440 |
} |
| 7441 |
|
| 7442 |
if (!tokenStream.getToken(&tt)) { |
| 7443 |
return errorResult(); |
| 7444 |
} |
| 7445 |
switch (tt) { |
| 7446 |
case TokenKind::LeftBracket: |
| 7447 |
case TokenKind::LeftCurly: |
| 7448 |
MOZ_TRY_VAR(catchName, |
| 7449 |
destructuringDeclaration(DeclarationKind::CatchParameter, |
| 7450 |
yieldHandling, tt)); |
| 7451 |
break; |
| 7452 |
|
| 7453 |
default: { |
| 7454 |
if (!TokenKindIsPossibleIdentifierName(tt)) { |
| 7455 |
error(JSMSG_CATCH_IDENTIFIER); |
| 7456 |
return errorResult(); |
| 7457 |
} |
| 7458 |
|
| 7459 |
MOZ_TRY_VAR(catchName, |
| 7460 |
bindingIdentifier(DeclarationKind::SimpleCatchParameter, |
| 7461 |
yieldHandling)); |
| 7462 |
break; |
| 7463 |
} |
| 7464 |
} |
| 7465 |
|
| 7466 |
if (!mustMatchToken(TokenKind::RightParen, JSMSG_PAREN_AFTER_CATCH)) { |
| 7467 |
return errorResult(); |
| 7468 |
} |
| 7469 |
|
| 7470 |
if (!mustMatchToken(TokenKind::LeftCurly, JSMSG_CURLY_BEFORE_CATCH)) { |
| 7471 |
return errorResult(); |
| 7472 |
} |
| 7473 |
} |
| 7474 |
|
| 7475 |
LexicalScopeNodeType catchBody; |
| 7476 |
MOZ_TRY_VAR(catchBody, catchBlockStatement(yieldHandling, scope)); |
| 7477 |
|
| 7478 |
MOZ_TRY_VAR(catchScope, finishLexicalScope(scope, catchBody)); |
| 7479 |
|
| 7480 |
if (!handler_.setupCatchScope(catchScope, catchName, catchBody)) { |
| 7481 |
return errorResult(); |
| 7482 |
} |
| 7483 |
handler_.setEndPosition(catchScope, pos().end); |
| 7484 |
|
| 7485 |
if (!tokenStream.getToken(&tt, TokenStream::SlashIsRegExp)) { |
| 7486 |
return errorResult(); |
| 7487 |
} |
| 7488 |
} |
| 7489 |
|
| 7490 |
Node finallyBlock = null(); |
| 7491 |
|
| 7492 |
if (tt == TokenKind::Finally) { |
| 7493 |
if (!mustMatchToken(TokenKind::LeftCurly, JSMSG_CURLY_BEFORE_FINALLY)) { |
| 7494 |
return errorResult(); |
| 7495 |
} |
| 7496 |
|
| 7497 |
uint32_t openedPos = pos().begin; |
| 7498 |
|
| 7499 |
ParseContext::Statement stmt(pc_, StatementKind::Finally); |
| 7500 |
ParseContext::Scope scope(this); |
| 7501 |
if (!scope.init(pc_)) { |
| 7502 |
return errorResult(); |
| 7503 |
} |
| 7504 |
|
| 7505 |
MOZ_TRY_VAR(finallyBlock, statementList(yieldHandling)); |
| 7506 |
|
| 7507 |
MOZ_TRY_VAR(finallyBlock, finishLexicalScope(scope, finallyBlock)); |
| 7508 |
|
| 7509 |
if (!mustMatchToken( |
| 7510 |
TokenKind::RightCurly, [this, openedPos](TokenKind actual) { |
| 7511 |
this->reportMissingClosing(JSMSG_CURLY_AFTER_FINALLY, |
| 7512 |
JSMSG_CURLY_OPENED, openedPos); |
| 7513 |
})) { |
| 7514 |
return errorResult(); |
| 7515 |
} |
| 7516 |
} else { |
| 7517 |
anyChars.ungetToken(); |
| 7518 |
} |
| 7519 |
if (!catchScope && !finallyBlock) { |
| 7520 |
error(JSMSG_CATCH_OR_FINALLY); |
| 7521 |
return errorResult(); |
| 7522 |
} |
| 7523 |
|
| 7524 |
return handler_.newTryStatement(begin, innerBlock, catchScope, finallyBlock); |
| 7525 |
} |
| 7526 |
|
| 7527 |
template <class ParseHandler, typename Unit> |
| 7528 |
typename ParseHandler::LexicalScopeNodeResult |
| 7529 |
GeneralParser<ParseHandler, Unit>::catchBlockStatement( |
| 7530 |
YieldHandling yieldHandling, ParseContext::Scope& catchParamScope) { |
| 7531 |
uint32_t openedPos = pos().begin; |
| 7532 |
|
| 7533 |
ParseContext::Statement stmt(pc_, StatementKind::Block); |
| 7534 |
|
| 7535 |
// ES 13.15.7 CatchClauseEvaluation |
| 7536 |
// |
| 7537 |
// Step 8 means that the body of a catch block always has an additional |
| 7538 |
// lexical scope. |
| 7539 |
ParseContext::Scope scope(this); |
| 7540 |
if (!scope.init(pc_)) { |
| 7541 |
return errorResult(); |
| 7542 |
} |
| 7543 |
|
| 7544 |
// The catch parameter names cannot be redeclared inside the catch |
| 7545 |
// block, so declare the name in the inner scope. |
| 7546 |
if (!scope.addCatchParameters(pc_, catchParamScope)) { |
| 7547 |
return errorResult(); |
| 7548 |
} |
| 7549 |
|
| 7550 |
ListNodeType list; |
| 7551 |
MOZ_TRY_VAR(list, statementList(yieldHandling)); |
| 7552 |
|
| 7553 |
if (!mustMatchToken( |
| 7554 |
TokenKind::RightCurly, [this, openedPos](TokenKind actual) { |
| 7555 |
this->reportMissingClosing(JSMSG_CURLY_AFTER_CATCH, |
| 7556 |
JSMSG_CURLY_OPENED, openedPos); |
| 7557 |
})) { |
| 7558 |
return errorResult(); |
| 7559 |
} |
| 7560 |
|
| 7561 |
// The catch parameter names are not bound in the body scope, so remove |
| 7562 |
// them before generating bindings. |
| 7563 |
scope.removeCatchParameters(pc_, catchParamScope); |
| 7564 |
return finishLexicalScope(scope, list); |
| 7565 |
} |
| 7566 |
|
| 7567 |
template <class ParseHandler, typename Unit> |
| 7568 |
typename ParseHandler::DebuggerStatementResult |
| 7569 |
GeneralParser<ParseHandler, Unit>::debuggerStatement() { |
| 7570 |
TokenPos p; |
| 7571 |
p.begin = pos().begin; |
| 7572 |
if (!matchOrInsertSemicolon()) { |
| 7573 |
return errorResult(); |
| 7574 |
} |
| 7575 |
p.end = pos().end; |
| 7576 |
|
| 7577 |
return handler_.newDebuggerStatement(p); |
| 7578 |
} |
| 7579 |
|
| 7580 |
static AccessorType ToAccessorType(PropertyType propType) { |
| 7581 |
switch (propType) { |
| 7582 |
case PropertyType::Getter: |
| 7583 |
return AccessorType::Getter; |
| 7584 |
case PropertyType::Setter: |
| 7585 |
return AccessorType::Setter; |
| 7586 |
case PropertyType::Normal: |
| 7587 |
case PropertyType::Method: |
| 7588 |
case PropertyType::GeneratorMethod: |
| 7589 |
case PropertyType::AsyncMethod: |
| 7590 |
case PropertyType::AsyncGeneratorMethod: |
| 7591 |
case PropertyType::Constructor: |
| 7592 |
case PropertyType::DerivedConstructor: |
| 7593 |
return AccessorType::None; |
| 7594 |
default: |
| 7595 |
MOZ_CRASH("unexpected property type"); |
| 7596 |
} |
| 7597 |
} |
| 7598 |
|
| 7599 |
#ifdef ENABLE_DECORATORS |
| 7600 |
template <class ParseHandler, typename Unit> |
| 7601 |
typename ParseHandler::ListNodeResult |
| 7602 |
GeneralParser<ParseHandler, Unit>::decoratorList(YieldHandling yieldHandling) { |
| 7603 |
ListNodeType decorators; |
| 7604 |
MOZ_TRY_VAR(decorators, |
| 7605 |
handler_.newList(ParseNodeKind::DecoratorList, pos())); |
| 7606 |
|
| 7607 |
// Build a decorator list element. At each entry point to this loop we have |
| 7608 |
// already consumed the |@| token |
| 7609 |
TokenKind tt; |
| 7610 |
for (;;) { |
| 7611 |
if (!tokenStream.getToken(&tt, TokenStream::SlashIsInvalid)) { |
| 7612 |
return errorResult(); |
| 7613 |
} |
| 7614 |
|
| 7615 |
Node decorator; |
| 7616 |
MOZ_TRY_VAR(decorator, decoratorExpr(yieldHandling, tt)); |
| 7617 |
|
| 7618 |
handler_.addList(decorators, decorator); |
| 7619 |
|
| 7620 |
if (!tokenStream.getToken(&tt)) { |
| 7621 |
return errorResult(); |
| 7622 |
} |
| 7623 |
if (tt != TokenKind::At) { |
| 7624 |
anyChars.ungetToken(); |
| 7625 |
break; |
| 7626 |
} |
| 7627 |
} |
| 7628 |
return decorators; |
| 7629 |
} |
| 7630 |
#endif |
| 7631 |
|
| 7632 |
template <class ParseHandler, typename Unit> |
| 7633 |
bool GeneralParser<ParseHandler, Unit>::classMember( |
| 7634 |
YieldHandling yieldHandling, const ParseContext::ClassStatement& classStmt, |
| 7635 |
TaggedParserAtomIndex className, uint32_t classStartOffset, |
| 7636 |
HasHeritage hasHeritage, ClassInitializedMembers& classInitializedMembers, |
| 7637 |
ListNodeType& classMembers, bool* done) { |
| 7638 |
*done = false; |
| 7639 |
|
| 7640 |
TokenKind tt; |
| 7641 |
if (!tokenStream.getToken(&tt, TokenStream::SlashIsInvalid)) { |
| 7642 |
return false; |
| 7643 |
} |
| 7644 |
if (tt == TokenKind::RightCurly) { |
| 7645 |
*done = true; |
| 7646 |
return true; |
| 7647 |
} |
| 7648 |
|
| 7649 |
if (tt == TokenKind::Semi) { |
| 7650 |
return true; |
| 7651 |
} |
| 7652 |
|
| 7653 |
#ifdef ENABLE_DECORATORS |
| 7654 |
ListNodeType decorators = null(); |
| 7655 |
if (tt == TokenKind::At) { |
| 7656 |
MOZ_TRY_VAR_OR_RETURN(decorators, decoratorList(yieldHandling), false); |
| 7657 |
|
| 7658 |
if (!tokenStream.getToken(&tt, TokenStream::SlashIsInvalid)) { |
| 7659 |
return false; |
| 7660 |
} |
| 7661 |
} |
| 7662 |
#endif |
| 7663 |
|
| 7664 |
bool isStatic = false; |
| 7665 |
if (tt == TokenKind::Static) { |
| 7666 |
if (!tokenStream.peekToken(&tt)) { |
| 7667 |
return false; |
| 7668 |
} |
| 7669 |
|
| 7670 |
if (tt == TokenKind::LeftCurly) { |
| 7671 |
/* Parsing static class block: static { ... } */ |
| 7672 |
FunctionNodeType staticBlockBody; |
| 7673 |
MOZ_TRY_VAR_OR_RETURN(staticBlockBody, |
| 7674 |
staticClassBlock(classInitializedMembers), false); |
| 7675 |
|
| 7676 |
StaticClassBlockType classBlock; |
| 7677 |
MOZ_TRY_VAR_OR_RETURN( |
| 7678 |
classBlock, handler_.newStaticClassBlock(staticBlockBody), false); |
| 7679 |
|
| 7680 |
return handler_.addClassMemberDefinition(classMembers, classBlock); |
| 7681 |
} |
| 7682 |
|
| 7683 |
if (tt != TokenKind::LeftParen && tt != TokenKind::Assign && |
| 7684 |
tt != TokenKind::Semi && tt != TokenKind::RightCurly) { |
| 7685 |
isStatic = true; |
| 7686 |
} else { |
| 7687 |
anyChars.ungetToken(); |
| 7688 |
} |
| 7689 |
} else { |
| 7690 |
anyChars.ungetToken(); |
| 7691 |
} |
| 7692 |
|
| 7693 |
uint32_t propNameOffset; |
| 7694 |
if (!tokenStream.peekOffset(&propNameOffset, TokenStream::SlashIsInvalid)) { |
| 7695 |
return false; |
| 7696 |
} |
| 7697 |
|
| 7698 |
TaggedParserAtomIndex propAtom; |
| 7699 |
PropertyType propType; |
| 7700 |
Node propName; |
| 7701 |
MOZ_TRY_VAR_OR_RETURN( |
| 7702 |
propName, |
| 7703 |
propertyOrMethodName(yieldHandling, PropertyNameInClass, |
| 7704 |
/* maybeDecl = */ Nothing(), classMembers, &propType, |
| 7705 |
&propAtom), |
| 7706 |
false); |
| 7707 |
|
| 7708 |
if (propType == PropertyType::Field || |
| 7709 |
propType == PropertyType::FieldWithAccessor) { |
| 7710 |
if (isStatic) { |
| 7711 |
if (propAtom == TaggedParserAtomIndex::WellKnown::prototype()) { |
| 7712 |
errorAt(propNameOffset, JSMSG_CLASS_STATIC_PROTO); |
| 7713 |
return false; |
| 7714 |
} |
| 7715 |
} |
| 7716 |
|
| 7717 |
if (propAtom == TaggedParserAtomIndex::WellKnown::constructor()) { |
| 7718 |
errorAt(propNameOffset, JSMSG_BAD_CONSTRUCTOR_DEF); |
| 7719 |
return false; |
| 7720 |
} |
| 7721 |
|
| 7722 |
if (handler_.isPrivateName(propName)) { |
| 7723 |
if (propAtom == TaggedParserAtomIndex::WellKnown::hash_constructor_()) { |
| 7724 |
errorAt(propNameOffset, JSMSG_BAD_CONSTRUCTOR_DEF); |
| 7725 |
return false; |
| 7726 |
} |
| 7727 |
|
| 7728 |
auto privateName = propAtom; |
| 7729 |
if (!noteDeclaredPrivateName( |
| 7730 |
propName, privateName, propType, |
| 7731 |
isStatic ? FieldPlacement::Static : FieldPlacement::Instance, |
| 7732 |
pos())) { |
| 7733 |
return false; |
| 7734 |
} |
| 7735 |
} |
| 7736 |
|
| 7737 |
#ifdef ENABLE_DECORATORS |
| 7738 |
ClassMethodType accessorGetterNode = null(); |
| 7739 |
ClassMethodType accessorSetterNode = null(); |
| 7740 |
if (propType == PropertyType::FieldWithAccessor) { |
| 7741 |
// Decorators Proposal |
| 7742 |
// https://arai-a.github.io/ecma262-compare/?pr=2417&id=sec-runtime-semantics-classfielddefinitionevaluation |
| 7743 |
// |
| 7744 |
// FieldDefinition : accessor ClassElementName Initializeropt |
| 7745 |
// |
| 7746 |
// Step 1. Let name be the result of evaluating ClassElementName. |
| 7747 |
// ... |
| 7748 |
// Step 3. Let privateStateDesc be the string-concatenation of name |
| 7749 |
// and " accessor storage". |
| 7750 |
StringBuilder privateStateDesc(fc_); |
| 7751 |
if (!privateStateDesc.append(this->parserAtoms(), propAtom)) { |
| 7752 |
return false; |
| 7753 |
} |
| 7754 |
if (!privateStateDesc.append(" accessor storage")) { |
| 7755 |
return false; |
| 7756 |
} |
| 7757 |
// Step 4. Let privateStateName be a new Private Name whose |
| 7758 |
// [[Description]] value is privateStateDesc. |
| 7759 |
TokenPos propNamePos(propNameOffset, pos().end); |
| 7760 |
auto privateStateName = |
| 7761 |
privateStateDesc.finishParserAtom(this->parserAtoms(), fc_); |
| 7762 |
if (!noteDeclaredPrivateName( |
| 7763 |
propName, privateStateName, propType, |
| 7764 |
isStatic ? FieldPlacement::Static : FieldPlacement::Instance, |
| 7765 |
propNamePos)) { |
| 7766 |
return false; |
| 7767 |
} |
| 7768 |
|
| 7769 |
// Step 5. Let getter be MakeAutoAccessorGetter(homeObject, name, |
| 7770 |
// privateStateName). |
| 7771 |
MOZ_TRY_VAR_OR_RETURN( |
| 7772 |
accessorGetterNode, |
| 7773 |
synthesizeAccessor(propName, propNamePos, propAtom, privateStateName, |
| 7774 |
isStatic, FunctionSyntaxKind::Getter, |
| 7775 |
classInitializedMembers), |
| 7776 |
false); |
| 7777 |
|
| 7778 |
// If the accessor is not decorated or is a non-static private field, |
| 7779 |
// add it to the class here. Otherwise, we'll handle this when the |
| 7780 |
// decorators are called. We don't need to keep a reference to the node |
| 7781 |
// after this except for non-static private accessors. Please see the |
| 7782 |
// comment in the definition of ClassField for details. |
| 7783 |
bool addAccessorImmediately = |
| 7784 |
!decorators || (!isStatic && handler_.isPrivateName(propName)); |
| 7785 |
if (addAccessorImmediately) { |
| 7786 |
if (!handler_.addClassMemberDefinition(classMembers, |
| 7787 |
accessorGetterNode)) { |
| 7788 |
return false; |
| 7789 |
} |
| 7790 |
if (!handler_.isPrivateName(propName)) { |
| 7791 |
accessorGetterNode = null(); |
| 7792 |
} |
| 7793 |
} |
| 7794 |
|
| 7795 |
// Step 6. Let setter be MakeAutoAccessorSetter(homeObject, name, |
| 7796 |
// privateStateName). |
| 7797 |
MOZ_TRY_VAR_OR_RETURN( |
| 7798 |
accessorSetterNode, |
| 7799 |
synthesizeAccessor(propName, propNamePos, propAtom, privateStateName, |
| 7800 |
isStatic, FunctionSyntaxKind::Setter, |
| 7801 |
classInitializedMembers), |
| 7802 |
false); |
| 7803 |
|
| 7804 |
if (addAccessorImmediately) { |
| 7805 |
if (!handler_.addClassMemberDefinition(classMembers, |
| 7806 |
accessorSetterNode)) { |
| 7807 |
return false; |
| 7808 |
} |
| 7809 |
if (!handler_.isPrivateName(propName)) { |
| 7810 |
accessorSetterNode = null(); |
| 7811 |
} |
| 7812 |
} |
| 7813 |
|
| 7814 |
// Step 10. Return ClassElementDefinition Record { [[Key]]: name, |
| 7815 |
// [[Kind]]: accessor, [[Get]]: getter, [[Set]]: setter, |
| 7816 |
// [[BackingStorageKey]]: privateStateName, [[Initializers]]: |
| 7817 |
// initializers, [[Decorators]]: empty }. |
| 7818 |
MOZ_TRY_VAR_OR_RETURN( |
| 7819 |
propName, handler_.newPrivateName(privateStateName, pos()), false); |
| 7820 |
propAtom = privateStateName; |
| 7821 |
// We maintain `decorators` here to perform this step at the same time: |
| 7822 |
// https://arai-a.github.io/ecma262-compare/?pr=2417&id=sec-static-semantics-classelementevaluation |
| 7823 |
// 4. Set fieldDefinition.[[Decorators]] to decorators. |
| 7824 |
} |
| 7825 |
#endif |
| 7826 |
if (isStatic) { |
| 7827 |
classInitializedMembers.staticFields++; |
| 7828 |
} else { |
| 7829 |
classInitializedMembers.instanceFields++; |
| 7830 |
#ifdef ENABLE_DECORATORS |
| 7831 |
if (decorators) { |
| 7832 |
classInitializedMembers.hasInstanceDecorators = true; |
| 7833 |
} |
| 7834 |
#endif |
| 7835 |
} |
| 7836 |
|
| 7837 |
TokenPos propNamePos(propNameOffset, pos().end); |
| 7838 |
FunctionNodeType initializer; |
| 7839 |
MOZ_TRY_VAR_OR_RETURN( |
| 7840 |
initializer, |
| 7841 |
fieldInitializerOpt(propNamePos, propName, propAtom, |
| 7842 |
classInitializedMembers, isStatic, hasHeritage), |
| 7843 |
false); |
| 7844 |
|
| 7845 |
if (!matchOrInsertSemicolon(TokenStream::SlashIsInvalid)) { |
| 7846 |
return false; |
| 7847 |
} |
| 7848 |
|
| 7849 |
ClassFieldType field; |
| 7850 |
MOZ_TRY_VAR_OR_RETURN(field, |
| 7851 |
handler_.newClassFieldDefinition( |
| 7852 |
propName, initializer, isStatic |
| 7853 |
#ifdef ENABLE_DECORATORS |
| 7854 |
, |
| 7855 |
decorators, accessorGetterNode, accessorSetterNode |
| 7856 |
#endif |
| 7857 |
), |
| 7858 |
false); |
| 7859 |
|
| 7860 |
return handler_.addClassMemberDefinition(classMembers, field); |
| 7861 |
} |
| 7862 |
|
| 7863 |
if (propType != PropertyType::Getter && propType != PropertyType::Setter && |
| 7864 |
propType != PropertyType::Method && |
| 7865 |
propType != PropertyType::GeneratorMethod && |
| 7866 |
propType != PropertyType::AsyncMethod && |
| 7867 |
propType != PropertyType::AsyncGeneratorMethod) { |
| 7868 |
errorAt(propNameOffset, JSMSG_BAD_CLASS_MEMBER_DEF); |
| 7869 |
return false; |
| 7870 |
} |
| 7871 |
|
| 7872 |
bool isConstructor = |
| 7873 |
!isStatic && propAtom == TaggedParserAtomIndex::WellKnown::constructor(); |
| 7874 |
if (isConstructor) { |
| 7875 |
if (propType != PropertyType::Method) { |
| 7876 |
errorAt(propNameOffset, JSMSG_BAD_CONSTRUCTOR_DEF); |
| 7877 |
return false; |
| 7878 |
} |
| 7879 |
if (classStmt.constructorBox) { |
| 7880 |
errorAt(propNameOffset, JSMSG_DUPLICATE_CONSTRUCTOR); |
| 7881 |
return false; |
| 7882 |
} |
| 7883 |
propType = hasHeritage == HasHeritage::Yes |
| 7884 |
? PropertyType::DerivedConstructor |
| 7885 |
: PropertyType::Constructor; |
| 7886 |
} else if (isStatic && |
| 7887 |
propAtom == TaggedParserAtomIndex::WellKnown::prototype()) { |
| 7888 |
errorAt(propNameOffset, JSMSG_CLASS_STATIC_PROTO); |
| 7889 |
return false; |
| 7890 |
} |
| 7891 |
|
| 7892 |
TaggedParserAtomIndex funName; |
| 7893 |
switch (propType) { |
| 7894 |
case PropertyType::Getter: |
| 7895 |
case PropertyType::Setter: { |
| 7896 |
bool hasStaticName = |
| 7897 |
!anyChars.isCurrentTokenType(TokenKind::RightBracket) && propAtom; |
| 7898 |
if (hasStaticName) { |
| 7899 |
funName = prefixAccessorName(propType, propAtom); |
| 7900 |
if (!funName) { |
| 7901 |
return false; |
| 7902 |
} |
| 7903 |
} |
| 7904 |
break; |
| 7905 |
} |
| 7906 |
case PropertyType::Constructor: |
| 7907 |
case PropertyType::DerivedConstructor: |
| 7908 |
funName = className; |
| 7909 |
break; |
| 7910 |
default: |
| 7911 |
if (!anyChars.isCurrentTokenType(TokenKind::RightBracket)) { |
| 7912 |
funName = propAtom; |
| 7913 |
} |
| 7914 |
} |
| 7915 |
|
| 7916 |
// When |super()| is invoked, we search for the nearest scope containing |
| 7917 |
// |.initializers| to initialize the class fields. This set-up precludes |
| 7918 |
// declaring |.initializers| in the class scope, because in some syntactic |
| 7919 |
// contexts |super()| can appear nested in a class, while actually belonging |
| 7920 |
// to an outer class definition. |
| 7921 |
// |
| 7922 |
// Example: |
| 7923 |
// class Outer extends Base { |
| 7924 |
// field = 1; |
| 7925 |
// constructor() { |
| 7926 |
// class Inner { |
| 7927 |
// field = 2; |
| 7928 |
// |
| 7929 |
// // The super() call in the computed property name mustn't access |
| 7930 |
// // Inner's |.initializers| array, but instead Outer's. |
| 7931 |
// [super()]() {} |
| 7932 |
// } |
| 7933 |
// } |
| 7934 |
// } |
| 7935 |
Maybe<ParseContext::Scope> dotInitializersScope; |
| 7936 |
if (isConstructor && !options().selfHostingMode) { |
| 7937 |
dotInitializersScope.emplace(this); |
| 7938 |
if (!dotInitializersScope->init(pc_)) { |
| 7939 |
return false; |
| 7940 |
} |
| 7941 |
|
| 7942 |
if (!noteDeclaredName(TaggedParserAtomIndex::WellKnown::dot_initializers_(), |
| 7943 |
DeclarationKind::Let, pos())) { |
| 7944 |
return false; |
| 7945 |
} |
| 7946 |
|
| 7947 |
#ifdef ENABLE_DECORATORS |
| 7948 |
if (!noteDeclaredName( |
| 7949 |
TaggedParserAtomIndex::WellKnown::dot_instanceExtraInitializers_(), |
| 7950 |
DeclarationKind::Let, pos())) { |
| 7951 |
return false; |
| 7952 |
} |
| 7953 |
#endif |
| 7954 |
} |
| 7955 |
|
| 7956 |
// Calling toString on constructors need to return the source text for |
| 7957 |
// the entire class. The end offset is unknown at this point in |
| 7958 |
// parsing and will be amended when class parsing finishes below. |
| 7959 |
FunctionNodeType funNode; |
| 7960 |
MOZ_TRY_VAR_OR_RETURN( |
| 7961 |
funNode, |
| 7962 |
methodDefinition(isConstructor ? classStartOffset : propNameOffset, |
| 7963 |
propType, funName), |
| 7964 |
false); |
| 7965 |
|
| 7966 |
AccessorType atype = ToAccessorType(propType); |
| 7967 |
|
| 7968 |
Maybe<FunctionNodeType> initializerIfPrivate = Nothing(); |
| 7969 |
if (handler_.isPrivateName(propName)) { |
| 7970 |
if (propAtom == TaggedParserAtomIndex::WellKnown::hash_constructor_()) { |
| 7971 |
// #constructor is an invalid private name. |
| 7972 |
errorAt(propNameOffset, JSMSG_BAD_CONSTRUCTOR_DEF); |
| 7973 |
return false; |
| 7974 |
} |
| 7975 |
|
| 7976 |
TaggedParserAtomIndex privateName = propAtom; |
| 7977 |
if (!noteDeclaredPrivateName( |
| 7978 |
propName, privateName, propType, |
| 7979 |
isStatic ? FieldPlacement::Static : FieldPlacement::Instance, |
| 7980 |
pos())) { |
| 7981 |
return false; |
| 7982 |
} |
| 7983 |
|
| 7984 |
// Private non-static methods are stored in the class body environment. |
| 7985 |
// Private non-static accessors are stamped onto every instance using |
| 7986 |
// initializers. Private static methods are stamped onto the constructor |
| 7987 |
// during class evaluation; see BytecodeEmitter::emitPropertyList. |
| 7988 |
if (!isStatic) { |
| 7989 |
if (atype == AccessorType::Getter || atype == AccessorType::Setter) { |
| 7990 |
classInitializedMembers.privateAccessors++; |
| 7991 |
TokenPos propNamePos(propNameOffset, pos().end); |
| 7992 |
FunctionNodeType initializerNode; |
| 7993 |
MOZ_TRY_VAR_OR_RETURN( |
| 7994 |
initializerNode, |
| 7995 |
synthesizePrivateMethodInitializer(propAtom, atype, propNamePos), |
| 7996 |
false); |
| 7997 |
initializerIfPrivate = Some(initializerNode); |
| 7998 |
} else { |
| 7999 |
MOZ_ASSERT(atype == AccessorType::None); |
| 8000 |
classInitializedMembers.privateMethods++; |
| 8001 |
} |
| 8002 |
} |
| 8003 |
} |
| 8004 |
|
| 8005 |
#ifdef ENABLE_DECORATORS |
| 8006 |
if (decorators) { |
| 8007 |
classInitializedMembers.hasInstanceDecorators = true; |
| 8008 |
} |
| 8009 |
#endif |
| 8010 |
|
| 8011 |
Node method; |
| 8012 |
MOZ_TRY_VAR_OR_RETURN( |
| 8013 |
method, |
| 8014 |
handler_.newClassMethodDefinition(propName, funNode, atype, isStatic, |
| 8015 |
initializerIfPrivate |
| 8016 |
#ifdef ENABLE_DECORATORS |
| 8017 |
, |
| 8018 |
decorators |
| 8019 |
#endif |
| 8020 |
), |
| 8021 |
false); |
| 8022 |
|
| 8023 |
if (dotInitializersScope.isSome()) { |
| 8024 |
MOZ_TRY_VAR_OR_RETURN( |
| 8025 |
method, finishLexicalScope(*dotInitializersScope, method), false); |
| 8026 |
dotInitializersScope.reset(); |
| 8027 |
} |
| 8028 |
|
| 8029 |
return handler_.addClassMemberDefinition(classMembers, method); |
| 8030 |
} |
| 8031 |
|
| 8032 |
template <class ParseHandler, typename Unit> |
| 8033 |
bool GeneralParser<ParseHandler, Unit>::finishClassConstructor( |
| 8034 |
const ParseContext::ClassStatement& classStmt, |
| 8035 |
TaggedParserAtomIndex className, HasHeritage hasHeritage, |
| 8036 |
uint32_t classStartOffset, uint32_t classEndOffset, |
| 8037 |
const ClassInitializedMembers& classInitializedMembers, |
| 8038 |
ListNodeType& classMembers) { |
| 8039 |
if (classStmt.constructorBox == nullptr) { |
| 8040 |
MOZ_ASSERT(!options().selfHostingMode); |
| 8041 |
// Unconditionally create the scope here, because it's always the |
| 8042 |
// constructor. |
| 8043 |
ParseContext::Scope dotInitializersScope(this); |
| 8044 |
if (!dotInitializersScope.init(pc_)) { |
| 8045 |
return false; |
| 8046 |
} |
| 8047 |
|
| 8048 |
if (!noteDeclaredName(TaggedParserAtomIndex::WellKnown::dot_initializers_(), |
| 8049 |
DeclarationKind::Let, pos())) { |
| 8050 |
return false; |
| 8051 |
} |
| 8052 |
|
| 8053 |
#ifdef ENABLE_DECORATORS |
| 8054 |
if (!noteDeclaredName( |
| 8055 |
TaggedParserAtomIndex::WellKnown::dot_instanceExtraInitializers_(), |
| 8056 |
DeclarationKind::Let, pos(), ClosedOver::Yes)) { |
| 8057 |
return false; |
| 8058 |
} |
| 8059 |
#endif |
| 8060 |
|
| 8061 |
// synthesizeConstructor assigns to classStmt.constructorBox |
| 8062 |
TokenPos synthesizedBodyPos(classStartOffset, classEndOffset); |
| 8063 |
FunctionNodeType synthesizedCtor; |
| 8064 |
MOZ_TRY_VAR_OR_RETURN( |
| 8065 |
synthesizedCtor, |
| 8066 |
synthesizeConstructor(className, synthesizedBodyPos, hasHeritage), |
| 8067 |
false); |
| 8068 |
|
| 8069 |
// Note: the *function* has the name of the class, but the *property* |
| 8070 |
// containing the function has the name "constructor" |
| 8071 |
Node constructorNameNode; |
| 8072 |
MOZ_TRY_VAR_OR_RETURN( |
| 8073 |
constructorNameNode, |
| 8074 |
handler_.newObjectLiteralPropertyName( |
| 8075 |
TaggedParserAtomIndex::WellKnown::constructor(), pos()), |
| 8076 |
false); |
| 8077 |
ClassMethodType method; |
| 8078 |
MOZ_TRY_VAR_OR_RETURN(method, |
| 8079 |
handler_.newDefaultClassConstructor( |
| 8080 |
constructorNameNode, synthesizedCtor), |
| 8081 |
false); |
| 8082 |
LexicalScopeNodeType scope; |
| 8083 |
MOZ_TRY_VAR_OR_RETURN( |
| 8084 |
scope, finishLexicalScope(dotInitializersScope, method), false); |
| 8085 |
if (!handler_.addClassMemberDefinition(classMembers, scope)) { |
| 8086 |
return false; |
| 8087 |
} |
| 8088 |
} |
| 8089 |
|
| 8090 |
MOZ_ASSERT(classStmt.constructorBox); |
| 8091 |
FunctionBox* ctorbox = classStmt.constructorBox; |
| 8092 |
|
| 8093 |
// Amend the toStringEnd offset for the constructor now that we've |
| 8094 |
// finished parsing the class. |
| 8095 |
ctorbox->setCtorToStringEnd(classEndOffset); |
| 8096 |
|
| 8097 |
size_t numMemberInitializers = classInitializedMembers.privateAccessors + |
| 8098 |
classInitializedMembers.instanceFields; |
| 8099 |
bool hasPrivateBrand = classInitializedMembers.hasPrivateBrand(); |
| 8100 |
if (hasPrivateBrand || numMemberInitializers > 0) { |
| 8101 |
// Now that we have full set of initializers, update the constructor. |
| 8102 |
MemberInitializers initializers( |
| 8103 |
hasPrivateBrand, |
| 8104 |
#ifdef ENABLE_DECORATORS |
| 8105 |
classInitializedMembers.hasInstanceDecorators, |
| 8106 |
#endif |
| 8107 |
numMemberInitializers); |
| 8108 |
ctorbox->setMemberInitializers(initializers); |
| 8109 |
|
| 8110 |
// Field initialization need access to `this`. |
| 8111 |
ctorbox->setCtorFunctionHasThisBinding(); |
| 8112 |
} |
| 8113 |
|
| 8114 |
return true; |
| 8115 |
} |
| 8116 |
|
| 8117 |
template <class ParseHandler, typename Unit> |
| 8118 |
typename ParseHandler::ClassNodeResult |
| 8119 |
GeneralParser<ParseHandler, Unit>::classDefinition( |
| 8120 |
YieldHandling yieldHandling, ClassContext classContext, |
| 8121 |
DefaultHandling defaultHandling) { |
| 8122 |
#ifdef ENABLE_DECORATORS |
| 8123 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::At) || |
| 8124 |
anyChars.isCurrentTokenType(TokenKind::Class)); |
| 8125 |
|
| 8126 |
ListNodeType decorators = null(); |
| 8127 |
FunctionNodeType addInitializerFunction = null(); |
| 8128 |
if (anyChars.isCurrentTokenType(TokenKind::At)) { |
| 8129 |
MOZ_TRY_VAR(decorators, decoratorList(yieldHandling)); |
| 8130 |
TokenKind next; |
| 8131 |
if (!tokenStream.getToken(&next)) { |
| 8132 |
return errorResult(); |
| 8133 |
} |
| 8134 |
if (next != TokenKind::Class) { |
| 8135 |
error(JSMSG_CLASS_EXPECTED); |
| 8136 |
return errorResult(); |
| 8137 |
} |
| 8138 |
} |
| 8139 |
#else |
| 8140 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Class)); |
| 8141 |
#endif |
| 8142 |
|
| 8143 |
uint32_t classStartOffset = pos().begin; |
| 8144 |
bool savedStrictness = setLocalStrictMode(true); |
| 8145 |
|
| 8146 |
// Classes are quite broken in self-hosted code. |
| 8147 |
if (options().selfHostingMode) { |
| 8148 |
error(JSMSG_SELFHOSTED_CLASS); |
| 8149 |
return errorResult(); |
| 8150 |
} |
| 8151 |
|
| 8152 |
TokenKind tt; |
| 8153 |
if (!tokenStream.getToken(&tt)) { |
| 8154 |
return errorResult(); |
| 8155 |
} |
| 8156 |
|
| 8157 |
TaggedParserAtomIndex className; |
| 8158 |
if (TokenKindIsPossibleIdentifier(tt)) { |
| 8159 |
className = bindingIdentifier(yieldHandling); |
| 8160 |
if (!className) { |
| 8161 |
return errorResult(); |
| 8162 |
} |
| 8163 |
} else if (classContext == ClassStatement) { |
| 8164 |
if (defaultHandling == AllowDefaultName) { |
| 8165 |
className = TaggedParserAtomIndex::WellKnown::default_(); |
| 8166 |
anyChars.ungetToken(); |
| 8167 |
} else { |
| 8168 |
// Class statements must have a bound name |
| 8169 |
error(JSMSG_UNNAMED_CLASS_STMT); |
| 8170 |
return errorResult(); |
| 8171 |
} |
| 8172 |
} else { |
| 8173 |
// Make sure to put it back, whatever it was |
| 8174 |
anyChars.ungetToken(); |
| 8175 |
} |
| 8176 |
|
| 8177 |
// Because the binding definitions keep track of their blockId, we need to |
| 8178 |
// create at least the inner binding later. Keep track of the name's |
| 8179 |
// position in order to provide it for the nodes created later. |
| 8180 |
TokenPos namePos = pos(); |
| 8181 |
|
| 8182 |
auto isClass = [](ParseContext::Statement* stmt) { |
| 8183 |
return stmt->kind() == StatementKind::Class; |
| 8184 |
}; |
| 8185 |
|
| 8186 |
bool isInClass = pc_->sc()->inClass() || pc_->findInnermostStatement(isClass); |
| 8187 |
|
| 8188 |
// Push a ParseContext::ClassStatement to keep track of the constructor |
| 8189 |
// funbox. |
| 8190 |
ParseContext::ClassStatement classStmt(pc_); |
| 8191 |
|
| 8192 |
NameNodeType innerName; |
| 8193 |
Node nameNode = null(); |
| 8194 |
Node classHeritage = null(); |
| 8195 |
LexicalScopeNodeType classBlock = null(); |
| 8196 |
ClassBodyScopeNodeType classBodyBlock = null(); |
| 8197 |
uint32_t classEndOffset; |
| 8198 |
{ |
| 8199 |
// A named class creates a new lexical scope with a const binding of the |
| 8200 |
// class name for the "inner name". |
| 8201 |
ParseContext::Statement innerScopeStmt(pc_, StatementKind::Block); |
| 8202 |
ParseContext::Scope innerScope(this); |
| 8203 |
if (!innerScope.init(pc_)) { |
| 8204 |
return errorResult(); |
| 8205 |
} |
| 8206 |
|
| 8207 |
bool hasHeritageBool; |
| 8208 |
if (!tokenStream.matchToken(&hasHeritageBool, TokenKind::Extends)) { |
| 8209 |
return errorResult(); |
| 8210 |
} |
| 8211 |
HasHeritage hasHeritage = |
| 8212 |
hasHeritageBool ? HasHeritage::Yes : HasHeritage::No; |
| 8213 |
if (hasHeritage == HasHeritage::Yes) { |
| 8214 |
if (!tokenStream.getToken(&tt)) { |
| 8215 |
return errorResult(); |
| 8216 |
} |
| 8217 |
MOZ_TRY_VAR(classHeritage, |
| 8218 |
optionalExpr(yieldHandling, TripledotProhibited, tt)); |
| 8219 |
} |
| 8220 |
|
| 8221 |
if (!mustMatchToken(TokenKind::LeftCurly, JSMSG_CURLY_BEFORE_CLASS)) { |
| 8222 |
return errorResult(); |
| 8223 |
} |
| 8224 |
|
| 8225 |
{ |
| 8226 |
ParseContext::Statement bodyScopeStmt(pc_, StatementKind::Block); |
| 8227 |
ParseContext::Scope bodyScope(this); |
| 8228 |
if (!bodyScope.init(pc_)) { |
| 8229 |
return errorResult(); |
| 8230 |
} |
| 8231 |
|
| 8232 |
ListNodeType classMembers; |
| 8233 |
MOZ_TRY_VAR(classMembers, handler_.newClassMemberList(pos().begin)); |
| 8234 |
|
| 8235 |
ClassInitializedMembers classInitializedMembers{}; |
| 8236 |
for (;;) { |
| 8237 |
bool done; |
| 8238 |
if (!classMember(yieldHandling, classStmt, className, classStartOffset, |
| 8239 |
hasHeritage, classInitializedMembers, classMembers, |
| 8240 |
&done)) { |
| 8241 |
return errorResult(); |
| 8242 |
} |
| 8243 |
if (done) { |
| 8244 |
break; |
| 8245 |
} |
| 8246 |
} |
| 8247 |
#ifdef ENABLE_DECORATORS |
| 8248 |
if (classInitializedMembers.hasInstanceDecorators) { |
| 8249 |
MOZ_TRY_VAR(addInitializerFunction, |
| 8250 |
synthesizeAddInitializerFunction( |
| 8251 |
TaggedParserAtomIndex::WellKnown:: |
| 8252 |
dot_instanceExtraInitializers_(), |
| 8253 |
yieldHandling)); |
| 8254 |
} |
| 8255 |
#endif |
| 8256 |
|
| 8257 |
if (classInitializedMembers.privateMethods + |
| 8258 |
classInitializedMembers.privateAccessors > |
| 8259 |
0) { |
| 8260 |
// We declare `.privateBrand` as ClosedOver because the constructor |
| 8261 |
// always uses it, even a default constructor. We could equivalently |
| 8262 |
// `noteUsedName` when parsing the constructor, except that at that |
| 8263 |
// time, we don't necessarily know if the class has a private brand. |
| 8264 |
if (!noteDeclaredName( |
| 8265 |
TaggedParserAtomIndex::WellKnown::dot_privateBrand_(), |
| 8266 |
DeclarationKind::Synthetic, namePos, ClosedOver::Yes)) { |
| 8267 |
return errorResult(); |
| 8268 |
} |
| 8269 |
} |
| 8270 |
|
| 8271 |
if (classInitializedMembers.instanceFieldKeys > 0) { |
| 8272 |
if (!noteDeclaredName( |
| 8273 |
TaggedParserAtomIndex::WellKnown::dot_fieldKeys_(), |
| 8274 |
DeclarationKind::Synthetic, namePos)) { |
| 8275 |
return errorResult(); |
| 8276 |
} |
| 8277 |
} |
| 8278 |
|
| 8279 |
if (classInitializedMembers.staticFields > 0) { |
| 8280 |
if (!noteDeclaredName( |
| 8281 |
TaggedParserAtomIndex::WellKnown::dot_staticInitializers_(), |
| 8282 |
DeclarationKind::Synthetic, namePos)) { |
| 8283 |
return errorResult(); |
| 8284 |
} |
| 8285 |
} |
| 8286 |
|
| 8287 |
if (classInitializedMembers.staticFieldKeys > 0) { |
| 8288 |
if (!noteDeclaredName( |
| 8289 |
TaggedParserAtomIndex::WellKnown::dot_staticFieldKeys_(), |
| 8290 |
DeclarationKind::Synthetic, namePos)) { |
| 8291 |
return errorResult(); |
| 8292 |
} |
| 8293 |
} |
| 8294 |
|
| 8295 |
classEndOffset = pos().end; |
| 8296 |
if (!finishClassConstructor(classStmt, className, hasHeritage, |
| 8297 |
classStartOffset, classEndOffset, |
| 8298 |
classInitializedMembers, classMembers)) { |
| 8299 |
return errorResult(); |
| 8300 |
} |
| 8301 |
|
| 8302 |
MOZ_TRY_VAR(classBodyBlock, |
| 8303 |
finishClassBodyScope(bodyScope, classMembers)); |
| 8304 |
|
| 8305 |
// Pop the class body scope |
| 8306 |
} |
| 8307 |
|
| 8308 |
if (className) { |
| 8309 |
// The inner name is immutable. |
| 8310 |
if (!noteDeclaredName(className, DeclarationKind::Const, namePos)) { |
| 8311 |
return errorResult(); |
| 8312 |
} |
| 8313 |
|
| 8314 |
MOZ_TRY_VAR(innerName, newName(className, namePos)); |
| 8315 |
} |
| 8316 |
|
| 8317 |
MOZ_TRY_VAR(classBlock, finishLexicalScope(innerScope, classBodyBlock)); |
| 8318 |
|
| 8319 |
// Pop the inner scope. |
| 8320 |
} |
| 8321 |
|
| 8322 |
if (className) { |
| 8323 |
NameNodeType outerName = null(); |
| 8324 |
if (classContext == ClassStatement) { |
| 8325 |
// The outer name is mutable. |
| 8326 |
if (!noteDeclaredName(className, DeclarationKind::Class, namePos)) { |
| 8327 |
return errorResult(); |
| 8328 |
} |
| 8329 |
|
| 8330 |
MOZ_TRY_VAR(outerName, newName(className, namePos)); |
| 8331 |
} |
| 8332 |
|
| 8333 |
MOZ_TRY_VAR(nameNode, |
| 8334 |
handler_.newClassNames(outerName, innerName, namePos)); |
| 8335 |
} |
| 8336 |
MOZ_ALWAYS_TRUE(setLocalStrictMode(savedStrictness)); |
| 8337 |
// We're leaving a class definition that was not itself nested within a class |
| 8338 |
if (!isInClass) { |
| 8339 |
mozilla::Maybe<UnboundPrivateName> maybeUnboundName; |
| 8340 |
if (!usedNames_.hasUnboundPrivateNames(fc_, maybeUnboundName)) { |
| 8341 |
return errorResult(); |
| 8342 |
} |
| 8343 |
if (maybeUnboundName) { |
| 8344 |
UniqueChars str = |
| 8345 |
this->parserAtoms().toPrintableString(maybeUnboundName->atom); |
| 8346 |
if (!str) { |
| 8347 |
ReportOutOfMemory(this->fc_); |
| 8348 |
return errorResult(); |
| 8349 |
} |
| 8350 |
|
| 8351 |
errorAt(maybeUnboundName->position.begin, JSMSG_MISSING_PRIVATE_DECL, |
| 8352 |
str.get()); |
| 8353 |
return errorResult(); |
| 8354 |
} |
| 8355 |
} |
| 8356 |
|
| 8357 |
return handler_.newClass(nameNode, classHeritage, classBlock, |
| 8358 |
#ifdef ENABLE_DECORATORS |
| 8359 |
decorators, addInitializerFunction, |
| 8360 |
#endif |
| 8361 |
TokenPos(classStartOffset, classEndOffset)); |
| 8362 |
} |
| 8363 |
|
| 8364 |
template <class ParseHandler, typename Unit> |
| 8365 |
typename ParseHandler::FunctionNodeResult |
| 8366 |
GeneralParser<ParseHandler, Unit>::synthesizeConstructor( |
| 8367 |
TaggedParserAtomIndex className, TokenPos synthesizedBodyPos, |
| 8368 |
HasHeritage hasHeritage) { |
| 8369 |
FunctionSyntaxKind functionSyntaxKind = |
| 8370 |
hasHeritage == HasHeritage::Yes |
| 8371 |
? FunctionSyntaxKind::DerivedClassConstructor |
| 8372 |
: FunctionSyntaxKind::ClassConstructor; |
| 8373 |
|
| 8374 |
bool isSelfHosting = options().selfHostingMode; |
| 8375 |
FunctionFlags flags = |
| 8376 |
InitialFunctionFlags(functionSyntaxKind, GeneratorKind::NotGenerator, |
| 8377 |
FunctionAsyncKind::SyncFunction, isSelfHosting); |
| 8378 |
|
| 8379 |
// Create the top-level field initializer node. |
| 8380 |
FunctionNodeType funNode; |
| 8381 |
MOZ_TRY_VAR(funNode, |
| 8382 |
handler_.newFunction(functionSyntaxKind, synthesizedBodyPos)); |
| 8383 |
|
| 8384 |
// If we see any inner function, note it on our current context. The bytecode |
| 8385 |
// emitter may eliminate the function later, but we use a conservative |
| 8386 |
// definition for consistency between lazy and full parsing. |
| 8387 |
pc_->sc()->setHasInnerFunctions(); |
| 8388 |
|
| 8389 |
// When fully parsing a lazy script, we do not fully reparse its inner |
| 8390 |
// functions, which are also lazy. Instead, their free variables and source |
| 8391 |
// extents are recorded and may be skipped. |
| 8392 |
if (handler_.reuseLazyInnerFunctions()) { |
| 8393 |
if (!skipLazyInnerFunction(funNode, synthesizedBodyPos.begin, |
| 8394 |
/* tryAnnexB = */ false)) { |
| 8395 |
return errorResult(); |
| 8396 |
} |
| 8397 |
|
| 8398 |
return funNode; |
| 8399 |
} |
| 8400 |
|
| 8401 |
// Create the FunctionBox and link it to the function object. |
| 8402 |
Directives directives(true); |
| 8403 |
FunctionBox* funbox = newFunctionBox( |
| 8404 |
funNode, className, flags, synthesizedBodyPos.begin, directives, |
| 8405 |
GeneratorKind::NotGenerator, FunctionAsyncKind::SyncFunction); |
| 8406 |
if (!funbox) { |
| 8407 |
return errorResult(); |
| 8408 |
} |
| 8409 |
funbox->initWithEnclosingParseContext(pc_, functionSyntaxKind); |
| 8410 |
setFunctionEndFromCurrentToken(funbox); |
| 8411 |
|
| 8412 |
// Mark this function as being synthesized by the parser. This means special |
| 8413 |
// handling in delazification will be used since we don't have typical |
| 8414 |
// function syntax. |
| 8415 |
funbox->setSyntheticFunction(); |
| 8416 |
|
| 8417 |
// Push a SourceParseContext on to the stack. |
| 8418 |
ParseContext* outerpc = pc_; |
| 8419 |
SourceParseContext funpc(this, funbox, /* newDirectives = */ nullptr); |
| 8420 |
if (!funpc.init()) { |
| 8421 |
return errorResult(); |
| 8422 |
} |
| 8423 |
|
| 8424 |
if (!synthesizeConstructorBody(synthesizedBodyPos, hasHeritage, funNode, |
| 8425 |
funbox)) { |
| 8426 |
return errorResult(); |
| 8427 |
} |
| 8428 |
|
| 8429 |
if (!leaveInnerFunction(outerpc)) { |
| 8430 |
return errorResult(); |
| 8431 |
} |
| 8432 |
|
| 8433 |
return funNode; |
| 8434 |
} |
| 8435 |
|
| 8436 |
template <class ParseHandler, typename Unit> |
| 8437 |
bool GeneralParser<ParseHandler, Unit>::synthesizeConstructorBody( |
| 8438 |
TokenPos synthesizedBodyPos, HasHeritage hasHeritage, |
| 8439 |
FunctionNodeType funNode, FunctionBox* funbox) { |
| 8440 |
MOZ_ASSERT(funbox->isClassConstructor()); |
| 8441 |
|
| 8442 |
// Create a ParamsBodyNode for the parameters + body (there are no |
| 8443 |
// parameters). |
| 8444 |
ParamsBodyNodeType argsbody; |
| 8445 |
MOZ_TRY_VAR_OR_RETURN(argsbody, handler_.newParamsBody(synthesizedBodyPos), |
| 8446 |
false); |
| 8447 |
handler_.setFunctionFormalParametersAndBody(funNode, argsbody); |
| 8448 |
setFunctionStartAtPosition(funbox, synthesizedBodyPos); |
| 8449 |
|
| 8450 |
if (hasHeritage == HasHeritage::Yes) { |
| 8451 |
// Synthesize the equivalent to `function f(...args)` |
| 8452 |
funbox->setHasRest(); |
| 8453 |
if (!notePositionalFormalParameter( |
| 8454 |
funNode, TaggedParserAtomIndex::WellKnown::dot_args_(), |
| 8455 |
synthesizedBodyPos.begin, |
| 8456 |
/* disallowDuplicateParams = */ false, |
| 8457 |
/* duplicatedParam = */ nullptr)) { |
| 8458 |
return false; |
| 8459 |
} |
| 8460 |
funbox->setArgCount(1); |
| 8461 |
} else { |
| 8462 |
funbox->setArgCount(0); |
| 8463 |
} |
| 8464 |
|
| 8465 |
pc_->functionScope().useAsVarScope(pc_); |
| 8466 |
|
| 8467 |
ListNodeType stmtList; |
| 8468 |
MOZ_TRY_VAR_OR_RETURN(stmtList, handler_.newStatementList(synthesizedBodyPos), |
| 8469 |
false); |
| 8470 |
|
| 8471 |
if (!noteUsedName(TaggedParserAtomIndex::WellKnown::dot_this_())) { |
| 8472 |
return false; |
| 8473 |
} |
| 8474 |
|
| 8475 |
if (!noteUsedName(TaggedParserAtomIndex::WellKnown::dot_initializers_())) { |
| 8476 |
return false; |
| 8477 |
} |
| 8478 |
|
| 8479 |
#ifdef ENABLE_DECORATORS |
| 8480 |
if (!noteUsedName( |
| 8481 |
TaggedParserAtomIndex::WellKnown::dot_instanceExtraInitializers_())) { |
| 8482 |
return false; |
| 8483 |
} |
| 8484 |
#endif |
| 8485 |
|
| 8486 |
if (hasHeritage == HasHeritage::Yes) { |
| 8487 |
// |super()| implicitly reads |new.target|. |
| 8488 |
if (!noteUsedName(TaggedParserAtomIndex::WellKnown::dot_newTarget_())) { |
| 8489 |
return false; |
| 8490 |
} |
| 8491 |
|
| 8492 |
NameNodeType thisName; |
| 8493 |
MOZ_TRY_VAR_OR_RETURN(thisName, newThisName(), false); |
| 8494 |
|
| 8495 |
UnaryNodeType superBase; |
| 8496 |
MOZ_TRY_VAR_OR_RETURN( |
| 8497 |
superBase, handler_.newSuperBase(thisName, synthesizedBodyPos), false); |
| 8498 |
|
| 8499 |
ListNodeType arguments; |
| 8500 |
MOZ_TRY_VAR_OR_RETURN(arguments, handler_.newArguments(synthesizedBodyPos), |
| 8501 |
false); |
| 8502 |
|
| 8503 |
NameNodeType argsNameNode; |
| 8504 |
MOZ_TRY_VAR_OR_RETURN(argsNameNode, |
| 8505 |
newName(TaggedParserAtomIndex::WellKnown::dot_args_(), |
| 8506 |
synthesizedBodyPos), |
| 8507 |
false); |
| 8508 |
if (!noteUsedName(TaggedParserAtomIndex::WellKnown::dot_args_())) { |
| 8509 |
return false; |
| 8510 |
} |
| 8511 |
|
| 8512 |
UnaryNodeType spreadArgs; |
| 8513 |
MOZ_TRY_VAR_OR_RETURN( |
| 8514 |
spreadArgs, handler_.newSpread(synthesizedBodyPos.begin, argsNameNode), |
| 8515 |
false); |
| 8516 |
handler_.addList(arguments, spreadArgs); |
| 8517 |
|
| 8518 |
CallNodeType superCall; |
| 8519 |
MOZ_TRY_VAR_OR_RETURN( |
| 8520 |
superCall, |
| 8521 |
handler_.newSuperCall(superBase, arguments, /* isSpread = */ true), |
| 8522 |
false); |
| 8523 |
|
| 8524 |
BinaryNodeType setThis; |
| 8525 |
MOZ_TRY_VAR_OR_RETURN(setThis, handler_.newSetThis(thisName, superCall), |
| 8526 |
false); |
| 8527 |
|
| 8528 |
UnaryNodeType exprStatement; |
| 8529 |
MOZ_TRY_VAR_OR_RETURN( |
| 8530 |
exprStatement, |
| 8531 |
handler_.newExprStatement(setThis, synthesizedBodyPos.end), false); |
| 8532 |
|
| 8533 |
handler_.addStatementToList(stmtList, exprStatement); |
| 8534 |
} |
| 8535 |
|
| 8536 |
bool canSkipLazyClosedOverBindings = handler_.reuseClosedOverBindings(); |
| 8537 |
if (!pc_->declareFunctionThis(usedNames_, canSkipLazyClosedOverBindings)) { |
| 8538 |
return false; |
| 8539 |
} |
| 8540 |
if (!pc_->declareNewTarget(usedNames_, canSkipLazyClosedOverBindings)) { |
| 8541 |
return false; |
| 8542 |
} |
| 8543 |
|
| 8544 |
LexicalScopeNodeType initializerBody; |
| 8545 |
MOZ_TRY_VAR_OR_RETURN( |
| 8546 |
initializerBody, |
| 8547 |
finishLexicalScope(pc_->varScope(), stmtList, ScopeKind::FunctionLexical), |
| 8548 |
false); |
| 8549 |
handler_.setBeginPosition(initializerBody, stmtList); |
| 8550 |
handler_.setEndPosition(initializerBody, stmtList); |
| 8551 |
|
| 8552 |
handler_.setFunctionBody(funNode, initializerBody); |
| 8553 |
|
| 8554 |
return finishFunction(); |
| 8555 |
} |
| 8556 |
|
| 8557 |
template <class ParseHandler, typename Unit> |
| 8558 |
typename ParseHandler::FunctionNodeResult |
| 8559 |
GeneralParser<ParseHandler, Unit>::privateMethodInitializer( |
| 8560 |
TokenPos propNamePos, TaggedParserAtomIndex propAtom, |
| 8561 |
TaggedParserAtomIndex storedMethodAtom) { |
| 8562 |
if (!abortIfSyntaxParser()) { |
| 8563 |
return errorResult(); |
| 8564 |
} |
| 8565 |
|
| 8566 |
// Synthesize an initializer function that the constructor can use to stamp a |
| 8567 |
// private method onto an instance object. |
| 8568 |
FunctionSyntaxKind syntaxKind = FunctionSyntaxKind::FieldInitializer; |
| 8569 |
FunctionAsyncKind asyncKind = FunctionAsyncKind::SyncFunction; |
| 8570 |
GeneratorKind generatorKind = GeneratorKind::NotGenerator; |
| 8571 |
bool isSelfHosting = options().selfHostingMode; |
| 8572 |
FunctionFlags flags = |
| 8573 |
InitialFunctionFlags(syntaxKind, generatorKind, asyncKind, isSelfHosting); |
| 8574 |
|
| 8575 |
FunctionNodeType funNode; |
| 8576 |
MOZ_TRY_VAR(funNode, handler_.newFunction(syntaxKind, propNamePos)); |
| 8577 |
|
| 8578 |
Directives directives(true); |
| 8579 |
FunctionBox* funbox = |
| 8580 |
newFunctionBox(funNode, TaggedParserAtomIndex::null(), flags, |
| 8581 |
propNamePos.begin, directives, generatorKind, asyncKind); |
| 8582 |
if (!funbox) { |
| 8583 |
return errorResult(); |
| 8584 |
} |
| 8585 |
funbox->initWithEnclosingParseContext(pc_, syntaxKind); |
| 8586 |
|
| 8587 |
// Push a SourceParseContext on to the stack. |
| 8588 |
ParseContext* outerpc = pc_; |
| 8589 |
SourceParseContext funpc(this, funbox, /* newDirectives = */ nullptr); |
| 8590 |
if (!funpc.init()) { |
| 8591 |
return errorResult(); |
| 8592 |
} |
| 8593 |
pc_->functionScope().useAsVarScope(pc_); |
| 8594 |
|
| 8595 |
// Add empty parameter list. |
| 8596 |
ParamsBodyNodeType argsbody; |
| 8597 |
MOZ_TRY_VAR(argsbody, handler_.newParamsBody(propNamePos)); |
| 8598 |
handler_.setFunctionFormalParametersAndBody(funNode, argsbody); |
| 8599 |
setFunctionStartAtCurrentToken(funbox); |
| 8600 |
funbox->setArgCount(0); |
| 8601 |
|
| 8602 |
// Note both the stored private method body and it's private name as being |
| 8603 |
// used in the initializer. They will be emitted into the method body in the |
| 8604 |
// BCE. |
| 8605 |
if (!noteUsedName(storedMethodAtom)) { |
| 8606 |
return errorResult(); |
| 8607 |
} |
| 8608 |
MOZ_TRY(privateNameReference(propAtom)); |
| 8609 |
|
| 8610 |
// Unlike field initializers, private method initializers are not created with |
| 8611 |
// a body of synthesized AST nodes. Instead, the body is left empty and the |
| 8612 |
// initializer is synthesized at the bytecode level. |
| 8613 |
// See BytecodeEmitter::emitPrivateMethodInitializer. |
| 8614 |
ListNodeType stmtList; |
| 8615 |
MOZ_TRY_VAR(stmtList, handler_.newStatementList(propNamePos)); |
| 8616 |
|
| 8617 |
bool canSkipLazyClosedOverBindings = handler_.reuseClosedOverBindings(); |
| 8618 |
if (!pc_->declareFunctionThis(usedNames_, canSkipLazyClosedOverBindings)) { |
| 8619 |
return errorResult(); |
| 8620 |
} |
| 8621 |
if (!pc_->declareNewTarget(usedNames_, canSkipLazyClosedOverBindings)) { |
| 8622 |
return errorResult(); |
| 8623 |
} |
| 8624 |
|
| 8625 |
LexicalScopeNodeType initializerBody; |
| 8626 |
MOZ_TRY_VAR(initializerBody, finishLexicalScope(pc_->varScope(), stmtList, |
| 8627 |
ScopeKind::FunctionLexical)); |
| 8628 |
handler_.setBeginPosition(initializerBody, stmtList); |
| 8629 |
handler_.setEndPosition(initializerBody, stmtList); |
| 8630 |
handler_.setFunctionBody(funNode, initializerBody); |
| 8631 |
|
| 8632 |
// Set field-initializer lambda boundary to start at property name and end |
| 8633 |
// after method body. |
| 8634 |
setFunctionStartAtPosition(funbox, propNamePos); |
| 8635 |
setFunctionEndFromCurrentToken(funbox); |
| 8636 |
|
| 8637 |
if (!finishFunction()) { |
| 8638 |
return errorResult(); |
| 8639 |
} |
| 8640 |
|
| 8641 |
if (!leaveInnerFunction(outerpc)) { |
| 8642 |
return errorResult(); |
| 8643 |
} |
| 8644 |
|
| 8645 |
return funNode; |
| 8646 |
} |
| 8647 |
|
| 8648 |
template <class ParseHandler, typename Unit> |
| 8649 |
typename ParseHandler::FunctionNodeResult |
| 8650 |
GeneralParser<ParseHandler, Unit>::staticClassBlock( |
| 8651 |
ClassInitializedMembers& classInitializedMembers) { |
| 8652 |
// Both for getting-this-done, and because this will invariably be executed, |
| 8653 |
// syntax parsing should be aborted. |
| 8654 |
if (!abortIfSyntaxParser()) { |
| 8655 |
return errorResult(); |
| 8656 |
} |
| 8657 |
|
| 8658 |
FunctionSyntaxKind syntaxKind = FunctionSyntaxKind::StaticClassBlock; |
| 8659 |
FunctionAsyncKind asyncKind = FunctionAsyncKind::SyncFunction; |
| 8660 |
GeneratorKind generatorKind = GeneratorKind::NotGenerator; |
| 8661 |
bool isSelfHosting = options().selfHostingMode; |
| 8662 |
FunctionFlags flags = |
| 8663 |
InitialFunctionFlags(syntaxKind, generatorKind, asyncKind, isSelfHosting); |
| 8664 |
|
| 8665 |
AutoAwaitIsKeyword awaitIsKeyword(this, AwaitHandling::AwaitIsDisallowed); |
| 8666 |
|
| 8667 |
// Create the function node for the static class body. |
| 8668 |
FunctionNodeType funNode; |
| 8669 |
MOZ_TRY_VAR(funNode, handler_.newFunction(syntaxKind, pos())); |
| 8670 |
|
| 8671 |
// Create the FunctionBox and link it to the function object. |
| 8672 |
Directives directives(true); |
| 8673 |
FunctionBox* funbox = |
| 8674 |
newFunctionBox(funNode, TaggedParserAtomIndex::null(), flags, pos().begin, |
| 8675 |
directives, generatorKind, asyncKind); |
| 8676 |
if (!funbox) { |
| 8677 |
return errorResult(); |
| 8678 |
} |
| 8679 |
funbox->initWithEnclosingParseContext(pc_, syntaxKind); |
| 8680 |
MOZ_ASSERT(funbox->isSyntheticFunction()); |
| 8681 |
MOZ_ASSERT(!funbox->allowSuperCall()); |
| 8682 |
MOZ_ASSERT(!funbox->allowArguments()); |
| 8683 |
MOZ_ASSERT(!funbox->allowReturn()); |
| 8684 |
|
| 8685 |
// Set start at `static` token. |
| 8686 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Static)); |
| 8687 |
setFunctionStartAtCurrentToken(funbox); |
| 8688 |
|
| 8689 |
// Push a SourceParseContext on to the stack. |
| 8690 |
ParseContext* outerpc = pc_; |
| 8691 |
SourceParseContext funpc(this, funbox, /* newDirectives = */ nullptr); |
| 8692 |
if (!funpc.init()) { |
| 8693 |
return errorResult(); |
| 8694 |
} |
| 8695 |
|
| 8696 |
pc_->functionScope().useAsVarScope(pc_); |
| 8697 |
|
| 8698 |
uint32_t start = pos().begin; |
| 8699 |
|
| 8700 |
tokenStream.consumeKnownToken(TokenKind::LeftCurly); |
| 8701 |
|
| 8702 |
// Static class blocks are code-generated as if they were static field |
| 8703 |
// initializers, so we bump the staticFields count here, which ensures |
| 8704 |
// .staticInitializers is noted as used. |
| 8705 |
classInitializedMembers.staticFields++; |
| 8706 |
|
| 8707 |
LexicalScopeNodeType body; |
| 8708 |
MOZ_TRY_VAR(body, |
| 8709 |
functionBody(InHandling::InAllowed, YieldHandling::YieldIsKeyword, |
| 8710 |
syntaxKind, FunctionBodyType::StatementListBody)); |
| 8711 |
|
| 8712 |
if (anyChars.isEOF()) { |
| 8713 |
error(JSMSG_UNTERMINATED_STATIC_CLASS_BLOCK); |
| 8714 |
return errorResult(); |
| 8715 |
} |
| 8716 |
|
| 8717 |
tokenStream.consumeKnownToken(TokenKind::RightCurly, |
| 8718 |
TokenStream::Modifier::SlashIsRegExp); |
| 8719 |
|
| 8720 |
TokenPos wholeBodyPos(start, pos().end); |
| 8721 |
|
| 8722 |
handler_.setEndPosition(funNode, wholeBodyPos.end); |
| 8723 |
setFunctionEndFromCurrentToken(funbox); |
| 8724 |
|
| 8725 |
// Create a ParamsBodyNode for the parameters + body (there are no |
| 8726 |
// parameters). |
| 8727 |
ParamsBodyNodeType argsbody; |
| 8728 |
MOZ_TRY_VAR(argsbody, handler_.newParamsBody(wholeBodyPos)); |
| 8729 |
|
| 8730 |
handler_.setFunctionFormalParametersAndBody(funNode, argsbody); |
| 8731 |
funbox->setArgCount(0); |
| 8732 |
|
| 8733 |
if (pc_->superScopeNeedsHomeObject()) { |
| 8734 |
funbox->setNeedsHomeObject(); |
| 8735 |
} |
| 8736 |
|
| 8737 |
handler_.setEndPosition(body, pos().begin); |
| 8738 |
handler_.setEndPosition(funNode, pos().end); |
| 8739 |
handler_.setFunctionBody(funNode, body); |
| 8740 |
|
| 8741 |
if (!finishFunction()) { |
| 8742 |
return errorResult(); |
| 8743 |
} |
| 8744 |
|
| 8745 |
if (!leaveInnerFunction(outerpc)) { |
| 8746 |
return errorResult(); |
| 8747 |
} |
| 8748 |
|
| 8749 |
return funNode; |
| 8750 |
} |
| 8751 |
|
| 8752 |
template <class ParseHandler, typename Unit> |
| 8753 |
typename ParseHandler::FunctionNodeResult |
| 8754 |
GeneralParser<ParseHandler, Unit>::fieldInitializerOpt( |
| 8755 |
TokenPos propNamePos, Node propName, TaggedParserAtomIndex propAtom, |
| 8756 |
ClassInitializedMembers& classInitializedMembers, bool isStatic, |
| 8757 |
HasHeritage hasHeritage) { |
| 8758 |
if (!abortIfSyntaxParser()) { |
| 8759 |
return errorResult(); |
| 8760 |
} |
| 8761 |
|
| 8762 |
bool hasInitializer = false; |
| 8763 |
if (!tokenStream.matchToken(&hasInitializer, TokenKind::Assign, |
| 8764 |
TokenStream::SlashIsDiv)) { |
| 8765 |
return errorResult(); |
| 8766 |
} |
| 8767 |
|
| 8768 |
FunctionSyntaxKind syntaxKind = FunctionSyntaxKind::FieldInitializer; |
| 8769 |
FunctionAsyncKind asyncKind = FunctionAsyncKind::SyncFunction; |
| 8770 |
GeneratorKind generatorKind = GeneratorKind::NotGenerator; |
| 8771 |
bool isSelfHosting = options().selfHostingMode; |
| 8772 |
FunctionFlags flags = |
| 8773 |
InitialFunctionFlags(syntaxKind, generatorKind, asyncKind, isSelfHosting); |
| 8774 |
|
| 8775 |
// Create the top-level field initializer node. |
| 8776 |
FunctionNodeType funNode; |
| 8777 |
MOZ_TRY_VAR(funNode, handler_.newFunction(syntaxKind, propNamePos)); |
| 8778 |
|
| 8779 |
// Create the FunctionBox and link it to the function object. |
| 8780 |
Directives directives(true); |
| 8781 |
FunctionBox* funbox = |
| 8782 |
newFunctionBox(funNode, TaggedParserAtomIndex::null(), flags, |
| 8783 |
propNamePos.begin, directives, generatorKind, asyncKind); |
| 8784 |
if (!funbox) { |
| 8785 |
return errorResult(); |
| 8786 |
} |
| 8787 |
funbox->initWithEnclosingParseContext(pc_, syntaxKind); |
| 8788 |
MOZ_ASSERT(funbox->isSyntheticFunction()); |
| 8789 |
|
| 8790 |
// We can't use setFunctionStartAtCurrentToken because that uses pos().begin, |
| 8791 |
// which is incorrect for fields without initializers (pos() points to the |
| 8792 |
// field identifier) |
| 8793 |
setFunctionStartAtPosition(funbox, propNamePos); |
| 8794 |
|
| 8795 |
// Push a SourceParseContext on to the stack. |
| 8796 |
ParseContext* outerpc = pc_; |
| 8797 |
SourceParseContext funpc(this, funbox, /* newDirectives = */ nullptr); |
| 8798 |
if (!funpc.init()) { |
| 8799 |
return errorResult(); |
| 8800 |
} |
| 8801 |
|
| 8802 |
pc_->functionScope().useAsVarScope(pc_); |
| 8803 |
|
| 8804 |
Node initializerExpr; |
| 8805 |
if (hasInitializer) { |
| 8806 |
// Parse the expression for the field initializer. |
| 8807 |
{ |
| 8808 |
AutoAwaitIsKeyword awaitHandling(this, AwaitIsName); |
| 8809 |
MOZ_TRY_VAR(initializerExpr, |
| 8810 |
assignExpr(InAllowed, YieldIsName, TripledotProhibited)); |
| 8811 |
} |
| 8812 |
|
| 8813 |
handler_.checkAndSetIsDirectRHSAnonFunction(initializerExpr); |
| 8814 |
} else { |
| 8815 |
MOZ_TRY_VAR(initializerExpr, handler_.newRawUndefinedLiteral(propNamePos)); |
| 8816 |
} |
| 8817 |
|
| 8818 |
TokenPos wholeInitializerPos(propNamePos.begin, pos().end); |
| 8819 |
|
| 8820 |
// Update the end position of the parse node. |
| 8821 |
handler_.setEndPosition(funNode, wholeInitializerPos.end); |
| 8822 |
setFunctionEndFromCurrentToken(funbox); |
| 8823 |
|
| 8824 |
// Create a ParamsBodyNode for the parameters + body (there are no |
| 8825 |
// parameters). |
| 8826 |
ParamsBodyNodeType argsbody; |
| 8827 |
MOZ_TRY_VAR(argsbody, handler_.newParamsBody(wholeInitializerPos)); |
| 8828 |
handler_.setFunctionFormalParametersAndBody(funNode, argsbody); |
| 8829 |
funbox->setArgCount(0); |
| 8830 |
|
| 8831 |
NameNodeType thisName; |
| 8832 |
MOZ_TRY_VAR(thisName, newThisName()); |
| 8833 |
|
| 8834 |
// Build `this.field` expression. |
| 8835 |
ThisLiteralType propAssignThis; |
| 8836 |
MOZ_TRY_VAR(propAssignThis, |
| 8837 |
handler_.newThisLiteral(wholeInitializerPos, thisName)); |
| 8838 |
|
| 8839 |
Node propAssignFieldAccess; |
| 8840 |
uint32_t indexValue; |
| 8841 |
if (!propAtom) { |
| 8842 |
// See BytecodeEmitter::emitCreateFieldKeys for an explanation of what |
| 8843 |
// .fieldKeys means and its purpose. |
| 8844 |
NameNodeType fieldKeysName; |
| 8845 |
if (isStatic) { |
| 8846 |
MOZ_TRY_VAR( |
| 8847 |
fieldKeysName, |
| 8848 |
newInternalDotName( |
| 8849 |
TaggedParserAtomIndex::WellKnown::dot_staticFieldKeys_())); |
| 8850 |
} else { |
| 8851 |
MOZ_TRY_VAR(fieldKeysName, |
| 8852 |
newInternalDotName( |
| 8853 |
TaggedParserAtomIndex::WellKnown::dot_fieldKeys_())); |
| 8854 |
} |
| 8855 |
if (!fieldKeysName) { |
| 8856 |
return errorResult(); |
| 8857 |
} |
| 8858 |
|
| 8859 |
double fieldKeyIndex; |
| 8860 |
if (isStatic) { |
| 8861 |
fieldKeyIndex = classInitializedMembers.staticFieldKeys++; |
| 8862 |
} else { |
| 8863 |
fieldKeyIndex = classInitializedMembers.instanceFieldKeys++; |
| 8864 |
} |
| 8865 |
Node fieldKeyIndexNode; |
| 8866 |
MOZ_TRY_VAR(fieldKeyIndexNode, |
| 8867 |
handler_.newNumber(fieldKeyIndex, DecimalPoint::NoDecimal, |
| 8868 |
wholeInitializerPos)); |
| 8869 |
|
| 8870 |
Node fieldKeyValue; |
| 8871 |
MOZ_TRY_VAR(fieldKeyValue, |
| 8872 |
handler_.newPropertyByValue(fieldKeysName, fieldKeyIndexNode, |
| 8873 |
wholeInitializerPos.end)); |
| 8874 |
|
| 8875 |
MOZ_TRY_VAR(propAssignFieldAccess, |
| 8876 |
handler_.newPropertyByValue(propAssignThis, fieldKeyValue, |
| 8877 |
wholeInitializerPos.end)); |
| 8878 |
} else if (handler_.isPrivateName(propName)) { |
| 8879 |
// It would be nice if we could tweak this here such that only if |
| 8880 |
// HasHeritage::Yes we end up emitting CheckPrivateField, but otherwise we |
| 8881 |
// emit InitElem -- this is an optimization to minimize HasOwn checks |
| 8882 |
// in InitElem for classes without heritage. |
| 8883 |
// |
| 8884 |
// Further tweaking would be to ultimately only do CheckPrivateField for the |
| 8885 |
// -first- field in a derived class, which would suffice to match the |
| 8886 |
// semantic check. |
| 8887 |
|
| 8888 |
NameNodeType privateNameNode; |
| 8889 |
MOZ_TRY_VAR(privateNameNode, privateNameReference(propAtom)); |
| 8890 |
|
| 8891 |
MOZ_TRY_VAR(propAssignFieldAccess, |
| 8892 |
handler_.newPrivateMemberAccess(propAssignThis, privateNameNode, |
| 8893 |
wholeInitializerPos.end)); |
| 8894 |
} else if (this->parserAtoms().isIndex(propAtom, &indexValue)) { |
| 8895 |
MOZ_TRY_VAR(propAssignFieldAccess, |
| 8896 |
handler_.newPropertyByValue(propAssignThis, propName, |
| 8897 |
wholeInitializerPos.end)); |
| 8898 |
} else { |
| 8899 |
NameNodeType propAssignName; |
| 8900 |
MOZ_TRY_VAR(propAssignName, |
| 8901 |
handler_.newPropertyName(propAtom, wholeInitializerPos)); |
| 8902 |
|
| 8903 |
MOZ_TRY_VAR(propAssignFieldAccess, |
| 8904 |
handler_.newPropertyAccess(propAssignThis, propAssignName)); |
| 8905 |
} |
| 8906 |
|
| 8907 |
// Synthesize an property init. |
| 8908 |
BinaryNodeType initializerPropInit; |
| 8909 |
MOZ_TRY_VAR(initializerPropInit, |
| 8910 |
handler_.newInitExpr(propAssignFieldAccess, initializerExpr)); |
| 8911 |
|
| 8912 |
UnaryNodeType exprStatement; |
| 8913 |
MOZ_TRY_VAR(exprStatement, handler_.newExprStatement( |
| 8914 |
initializerPropInit, wholeInitializerPos.end)); |
| 8915 |
|
| 8916 |
ListNodeType statementList; |
| 8917 |
MOZ_TRY_VAR(statementList, handler_.newStatementList(wholeInitializerPos)); |
| 8918 |
handler_.addStatementToList(statementList, exprStatement); |
| 8919 |
|
| 8920 |
bool canSkipLazyClosedOverBindings = handler_.reuseClosedOverBindings(); |
| 8921 |
if (!pc_->declareFunctionThis(usedNames_, canSkipLazyClosedOverBindings)) { |
| 8922 |
return errorResult(); |
| 8923 |
} |
| 8924 |
if (!pc_->declareNewTarget(usedNames_, canSkipLazyClosedOverBindings)) { |
| 8925 |
return errorResult(); |
| 8926 |
} |
| 8927 |
|
| 8928 |
// Set the function's body to the field assignment. |
| 8929 |
LexicalScopeNodeType initializerBody; |
| 8930 |
MOZ_TRY_VAR(initializerBody, |
| 8931 |
finishLexicalScope(pc_->varScope(), statementList, |
| 8932 |
ScopeKind::FunctionLexical)); |
| 8933 |
|
| 8934 |
handler_.setFunctionBody(funNode, initializerBody); |
| 8935 |
|
| 8936 |
if (pc_->superScopeNeedsHomeObject()) { |
| 8937 |
funbox->setNeedsHomeObject(); |
| 8938 |
} |
| 8939 |
|
| 8940 |
if (!finishFunction()) { |
| 8941 |
return errorResult(); |
| 8942 |
} |
| 8943 |
|
| 8944 |
if (!leaveInnerFunction(outerpc)) { |
| 8945 |
return errorResult(); |
| 8946 |
} |
| 8947 |
|
| 8948 |
return funNode; |
| 8949 |
} |
| 8950 |
|
| 8951 |
template <class ParseHandler, typename Unit> |
| 8952 |
typename ParseHandler::FunctionNodeResult |
| 8953 |
GeneralParser<ParseHandler, Unit>::synthesizePrivateMethodInitializer( |
| 8954 |
TaggedParserAtomIndex propAtom, AccessorType accessorType, |
| 8955 |
TokenPos propNamePos) { |
| 8956 |
if (!abortIfSyntaxParser()) { |
| 8957 |
return errorResult(); |
| 8958 |
} |
| 8959 |
|
| 8960 |
// Synthesize a name for the lexical variable that will store the |
| 8961 |
// accessor body. |
| 8962 |
StringBuilder storedMethodName(fc_); |
| 8963 |
if (!storedMethodName.append(this->parserAtoms(), propAtom)) { |
| 8964 |
return errorResult(); |
| 8965 |
} |
| 8966 |
if (!storedMethodName.append( |
| 8967 |
accessorType == AccessorType::Getter ? ".getter" : ".setter")) { |
| 8968 |
return errorResult(); |
| 8969 |
} |
| 8970 |
auto storedMethodProp = |
| 8971 |
storedMethodName.finishParserAtom(this->parserAtoms(), fc_); |
| 8972 |
if (!storedMethodProp) { |
| 8973 |
return errorResult(); |
| 8974 |
} |
| 8975 |
if (!noteDeclaredName(storedMethodProp, DeclarationKind::Synthetic, pos())) { |
| 8976 |
return errorResult(); |
| 8977 |
} |
| 8978 |
|
| 8979 |
return privateMethodInitializer(propNamePos, propAtom, storedMethodProp); |
| 8980 |
} |
| 8981 |
|
| 8982 |
#ifdef ENABLE_DECORATORS |
| 8983 |
template <class ParseHandler, typename Unit> |
| 8984 |
typename ParseHandler::FunctionNodeResult |
| 8985 |
GeneralParser<ParseHandler, Unit>::synthesizeAddInitializerFunction( |
| 8986 |
TaggedParserAtomIndex initializers, YieldHandling yieldHandling) { |
| 8987 |
if (!abortIfSyntaxParser()) { |
| 8988 |
return errorResult(); |
| 8989 |
} |
| 8990 |
|
| 8991 |
// TODO: Add support for static and class extra initializers, see bug 1868220 |
| 8992 |
// and bug 1868221. |
| 8993 |
MOZ_ASSERT( |
| 8994 |
initializers == |
| 8995 |
TaggedParserAtomIndex::WellKnown::dot_instanceExtraInitializers_()); |
| 8996 |
|
| 8997 |
TokenPos propNamePos = pos(); |
| 8998 |
|
| 8999 |
// Synthesize an addInitializer function that can be used to append to |
| 9000 |
// .initializers |
| 9001 |
FunctionSyntaxKind syntaxKind = FunctionSyntaxKind::Statement; |
| 9002 |
FunctionAsyncKind asyncKind = FunctionAsyncKind::SyncFunction; |
| 9003 |
GeneratorKind generatorKind = GeneratorKind::NotGenerator; |
| 9004 |
bool isSelfHosting = options().selfHostingMode; |
| 9005 |
FunctionFlags flags = |
| 9006 |
InitialFunctionFlags(syntaxKind, generatorKind, asyncKind, isSelfHosting); |
| 9007 |
|
| 9008 |
FunctionNodeType funNode; |
| 9009 |
MOZ_TRY_VAR(funNode, handler_.newFunction(syntaxKind, propNamePos)); |
| 9010 |
|
| 9011 |
Directives directives(true); |
| 9012 |
FunctionBox* funbox = |
| 9013 |
newFunctionBox(funNode, TaggedParserAtomIndex::null(), flags, |
| 9014 |
propNamePos.begin, directives, generatorKind, asyncKind); |
| 9015 |
if (!funbox) { |
| 9016 |
return errorResult(); |
| 9017 |
} |
| 9018 |
funbox->initWithEnclosingParseContext(pc_, syntaxKind); |
| 9019 |
|
| 9020 |
ParseContext* outerpc = pc_; |
| 9021 |
SourceParseContext funpc(this, funbox, /* newDirectives = */ nullptr); |
| 9022 |
if (!funpc.init()) { |
| 9023 |
return errorResult(); |
| 9024 |
} |
| 9025 |
pc_->functionScope().useAsVarScope(pc_); |
| 9026 |
|
| 9027 |
// Takes a single parameter, `initializer`. |
| 9028 |
ParamsBodyNodeType params; |
| 9029 |
MOZ_TRY_VAR(params, handler_.newParamsBody(propNamePos)); |
| 9030 |
|
| 9031 |
handler_.setFunctionFormalParametersAndBody(funNode, params); |
| 9032 |
|
| 9033 |
constexpr bool disallowDuplicateParams = true; |
| 9034 |
bool duplicatedParam = false; |
| 9035 |
if (!notePositionalFormalParameter( |
| 9036 |
funNode, TaggedParserAtomIndex::WellKnown::initializer(), pos().begin, |
| 9037 |
disallowDuplicateParams, &duplicatedParam)) { |
| 9038 |
return null(); |
| 9039 |
} |
| 9040 |
MOZ_ASSERT(!duplicatedParam); |
| 9041 |
MOZ_ASSERT(pc_->positionalFormalParameterNames().length() == 1); |
| 9042 |
|
| 9043 |
funbox->setLength(1); |
| 9044 |
funbox->setArgCount(1); |
| 9045 |
setFunctionStartAtCurrentToken(funbox); |
| 9046 |
|
| 9047 |
// Like private method initializers, the addInitializer method is not created |
| 9048 |
// with a body of synthesized AST nodes. Instead, the body is left empty and |
| 9049 |
// the initializer is synthesized at the bytecode level. See |
| 9050 |
// DecoratorEmitter::emitCreateAddInitializerFunction. |
| 9051 |
ListNodeType stmtList; |
| 9052 |
MOZ_TRY_VAR(stmtList, handler_.newStatementList(propNamePos)); |
| 9053 |
|
| 9054 |
if (!noteUsedName(initializers)) { |
| 9055 |
return null(); |
| 9056 |
} |
| 9057 |
|
| 9058 |
bool canSkipLazyClosedOverBindings = handler_.reuseClosedOverBindings(); |
| 9059 |
if (!pc_->declareFunctionThis(usedNames_, canSkipLazyClosedOverBindings)) { |
| 9060 |
return null(); |
| 9061 |
} |
| 9062 |
if (!pc_->declareNewTarget(usedNames_, canSkipLazyClosedOverBindings)) { |
| 9063 |
return null(); |
| 9064 |
} |
| 9065 |
|
| 9066 |
LexicalScopeNodeType addInitializerBody; |
| 9067 |
MOZ_TRY_VAR(addInitializerBody, |
| 9068 |
finishLexicalScope(pc_->varScope(), stmtList, |
| 9069 |
ScopeKind::FunctionLexical)); |
| 9070 |
handler_.setBeginPosition(addInitializerBody, stmtList); |
| 9071 |
handler_.setEndPosition(addInitializerBody, stmtList); |
| 9072 |
handler_.setFunctionBody(funNode, addInitializerBody); |
| 9073 |
|
| 9074 |
// Set field-initializer lambda boundary to start at property name and end |
| 9075 |
// after method body. |
| 9076 |
setFunctionStartAtPosition(funbox, propNamePos); |
| 9077 |
setFunctionEndFromCurrentToken(funbox); |
| 9078 |
|
| 9079 |
if (!finishFunction()) { |
| 9080 |
return errorResult(); |
| 9081 |
} |
| 9082 |
|
| 9083 |
if (!leaveInnerFunction(outerpc)) { |
| 9084 |
return errorResult(); |
| 9085 |
} |
| 9086 |
|
| 9087 |
return funNode; |
| 9088 |
} |
| 9089 |
|
| 9090 |
template <class ParseHandler, typename Unit> |
| 9091 |
typename ParseHandler::ClassMethodResult |
| 9092 |
GeneralParser<ParseHandler, Unit>::synthesizeAccessor( |
| 9093 |
Node propName, TokenPos propNamePos, TaggedParserAtomIndex propAtom, |
| 9094 |
TaggedParserAtomIndex privateStateNameAtom, bool isStatic, |
| 9095 |
FunctionSyntaxKind syntaxKind, |
| 9096 |
ClassInitializedMembers& classInitializedMembers) { |
| 9097 |
// Decorators Proposal |
| 9098 |
// https://arai-a.github.io/ecma262-compare/?pr=2417&id=sec-makeautoaccessorgetter |
| 9099 |
// The abstract operation MakeAutoAccessorGetter takes arguments homeObject |
| 9100 |
// (an Object), name (a property key or Private Name), and privateStateName (a |
| 9101 |
// Private Name) and returns a function object. |
| 9102 |
// |
| 9103 |
// https://arai-a.github.io/ecma262-compare/?pr=2417&id=sec-makeautoaccessorsetter |
| 9104 |
// The abstract operation MakeAutoAccessorSetter takes arguments homeObject |
| 9105 |
// (an Object), name (a property key or Private Name), and privateStateName (a |
| 9106 |
// Private Name) and returns a function object. |
| 9107 |
if (!abortIfSyntaxParser()) { |
| 9108 |
return errorResult(); |
| 9109 |
} |
| 9110 |
|
| 9111 |
AccessorType accessorType = syntaxKind == FunctionSyntaxKind::Getter |
| 9112 |
? AccessorType::Getter |
| 9113 |
: AccessorType::Setter; |
| 9114 |
|
| 9115 |
mozilla::Maybe<FunctionNodeType> initializerIfPrivate = Nothing(); |
| 9116 |
if (!isStatic && handler_.isPrivateName(propName)) { |
| 9117 |
classInitializedMembers.privateAccessors++; |
| 9118 |
FunctionNodeType initializerNode; |
| 9119 |
MOZ_TRY_VAR(initializerNode, synthesizePrivateMethodInitializer( |
| 9120 |
propAtom, accessorType, propNamePos)); |
| 9121 |
initializerIfPrivate = Some(initializerNode); |
| 9122 |
handler_.setPrivateNameKind(propName, PrivateNameKind::GetterSetter); |
| 9123 |
} |
| 9124 |
|
| 9125 |
// https://arai-a.github.io/ecma262-compare/?pr=2417&id=sec-makeautoaccessorgetter |
| 9126 |
// 2. Let getter be CreateBuiltinFunction(getterClosure, 0, "get", « »). |
| 9127 |
// |
| 9128 |
// https://arai-a.github.io/ecma262-compare/?pr=2417&id=sec-makeautoaccessorsetter |
| 9129 |
// 2. Let setter be CreateBuiltinFunction(setterClosure, 1, "set", « »). |
| 9130 |
StringBuilder storedMethodName(fc_); |
| 9131 |
if (!storedMethodName.append(accessorType == AccessorType::Getter ? "get" |
| 9132 |
: "set")) { |
| 9133 |
return errorResult(); |
| 9134 |
} |
| 9135 |
TaggedParserAtomIndex funNameAtom = |
| 9136 |
storedMethodName.finishParserAtom(this->parserAtoms(), fc_); |
| 9137 |
|
| 9138 |
FunctionNodeType funNode; |
| 9139 |
MOZ_TRY_VAR(funNode, |
| 9140 |
synthesizeAccessorBody(funNameAtom, propNamePos, |
| 9141 |
privateStateNameAtom, syntaxKind)); |
| 9142 |
|
| 9143 |
// https://arai-a.github.io/ecma262-compare/?pr=2417&id=sec-makeautoaccessorgetter |
| 9144 |
// 3. Perform MakeMethod(getter, homeObject). |
| 9145 |
// 4. Return getter. |
| 9146 |
// |
| 9147 |
// https://arai-a.github.io/ecma262-compare/?pr=2417&id=sec-makeautoaccessorsetter |
| 9148 |
// 3. Perform MakeMethod(setter, homeObject). |
| 9149 |
// 4. Return setter. |
| 9150 |
return handler_.newClassMethodDefinition( |
| 9151 |
propName, funNode, accessorType, isStatic, initializerIfPrivate, null()); |
| 9152 |
} |
| 9153 |
|
| 9154 |
template <class ParseHandler, typename Unit> |
| 9155 |
typename ParseHandler::FunctionNodeResult |
| 9156 |
GeneralParser<ParseHandler, Unit>::synthesizeAccessorBody( |
| 9157 |
TaggedParserAtomIndex funNameAtom, TokenPos propNamePos, |
| 9158 |
TaggedParserAtomIndex propNameAtom, FunctionSyntaxKind syntaxKind) { |
| 9159 |
if (!abortIfSyntaxParser()) { |
| 9160 |
return errorResult(); |
| 9161 |
} |
| 9162 |
|
| 9163 |
FunctionAsyncKind asyncKind = FunctionAsyncKind::SyncFunction; |
| 9164 |
GeneratorKind generatorKind = GeneratorKind::NotGenerator; |
| 9165 |
bool isSelfHosting = options().selfHostingMode; |
| 9166 |
FunctionFlags flags = |
| 9167 |
InitialFunctionFlags(syntaxKind, generatorKind, asyncKind, isSelfHosting); |
| 9168 |
|
| 9169 |
// Create the top-level function node. |
| 9170 |
FunctionNodeType funNode; |
| 9171 |
MOZ_TRY_VAR(funNode, handler_.newFunction(syntaxKind, propNamePos)); |
| 9172 |
|
| 9173 |
// Create the FunctionBox and link it to the function object. |
| 9174 |
Directives directives(true); |
| 9175 |
FunctionBox* funbox = |
| 9176 |
newFunctionBox(funNode, funNameAtom, flags, propNamePos.begin, directives, |
| 9177 |
generatorKind, asyncKind); |
| 9178 |
if (!funbox) { |
| 9179 |
return errorResult(); |
| 9180 |
} |
| 9181 |
funbox->initWithEnclosingParseContext(pc_, syntaxKind); |
| 9182 |
funbox->setSyntheticFunction(); |
| 9183 |
|
| 9184 |
// Push a SourceParseContext on to the stack. |
| 9185 |
ParseContext* outerpc = pc_; |
| 9186 |
SourceParseContext funpc(this, funbox, /* newDirectives = */ nullptr); |
| 9187 |
if (!funpc.init()) { |
| 9188 |
return errorResult(); |
| 9189 |
} |
| 9190 |
|
| 9191 |
pc_->functionScope().useAsVarScope(pc_); |
| 9192 |
|
| 9193 |
// The function we synthesize is located at the field with the |
| 9194 |
// accessor. |
| 9195 |
setFunctionStartAtCurrentToken(funbox); |
| 9196 |
setFunctionEndFromCurrentToken(funbox); |
| 9197 |
|
| 9198 |
// Create a ListNode for the parameters + body |
| 9199 |
ParamsBodyNodeType paramsbody; |
| 9200 |
MOZ_TRY_VAR(paramsbody, handler_.newParamsBody(propNamePos)); |
| 9201 |
handler_.setFunctionFormalParametersAndBody(funNode, paramsbody); |
| 9202 |
|
| 9203 |
if (syntaxKind == FunctionSyntaxKind::Getter) { |
| 9204 |
funbox->setArgCount(0); |
| 9205 |
} else { |
| 9206 |
funbox->setArgCount(1); |
| 9207 |
} |
| 9208 |
|
| 9209 |
// Build `this` expression to access the privateStateName for use in the |
| 9210 |
// operations to create the getter and setter below. |
| 9211 |
NameNodeType thisName; |
| 9212 |
MOZ_TRY_VAR(thisName, newThisName()); |
| 9213 |
|
| 9214 |
ThisLiteralType propThis; |
| 9215 |
MOZ_TRY_VAR(propThis, handler_.newThisLiteral(propNamePos, thisName)); |
| 9216 |
|
| 9217 |
NameNodeType privateNameNode; |
| 9218 |
MOZ_TRY_VAR(privateNameNode, privateNameReference(propNameAtom)); |
| 9219 |
|
| 9220 |
Node propFieldAccess; |
| 9221 |
MOZ_TRY_VAR(propFieldAccess, handler_.newPrivateMemberAccess( |
| 9222 |
propThis, privateNameNode, propNamePos.end)); |
| 9223 |
|
| 9224 |
Node accessorBody; |
| 9225 |
if (syntaxKind == FunctionSyntaxKind::Getter) { |
| 9226 |
// Decorators Proposal |
| 9227 |
// https://arai-a.github.io/ecma262-compare/?pr=2417&id=sec-makeautoaccessorgetter |
| 9228 |
// 1. Let getterClosure be a new Abstract Closure with no parameters that |
| 9229 |
// captures privateStateName and performs the following steps when called: |
| 9230 |
// 1.a. Let o be the this value. |
| 9231 |
// 1.b. Return ? PrivateGet(privateStateName, o). |
| 9232 |
MOZ_TRY_VAR(accessorBody, |
| 9233 |
handler_.newReturnStatement(propFieldAccess, propNamePos)); |
| 9234 |
} else { |
| 9235 |
// Decorators Proposal |
| 9236 |
// https://arai-a.github.io/ecma262-compare/?pr=2417&id=sec-makeautoaccessorsetter |
| 9237 |
// The abstract operation MakeAutoAccessorSetter takes arguments homeObject |
| 9238 |
// (an Object), name (a property key or Private Name), and privateStateName |
| 9239 |
// (a Private Name) and returns a function object. |
| 9240 |
// 1. Let setterClosure be a new Abstract Closure with parameters (value) |
| 9241 |
// that captures privateStateName and performs the following steps when |
| 9242 |
// called: |
| 9243 |
// 1.a. Let o be the this value. |
| 9244 |
notePositionalFormalParameter(funNode, |
| 9245 |
TaggedParserAtomIndex::WellKnown::value(), |
| 9246 |
/* pos = */ 0, false, |
| 9247 |
/* duplicatedParam = */ nullptr); |
| 9248 |
|
| 9249 |
Node initializerExpr; |
| 9250 |
MOZ_TRY_VAR(initializerExpr, |
| 9251 |
handler_.newName(TaggedParserAtomIndex::WellKnown::value(), |
| 9252 |
propNamePos)); |
| 9253 |
|
| 9254 |
// 1.b. Perform ? PrivateSet(privateStateName, o, value). |
| 9255 |
Node assignment; |
| 9256 |
MOZ_TRY_VAR(assignment, |
| 9257 |
handler_.newAssignment(ParseNodeKind::AssignExpr, |
| 9258 |
propFieldAccess, initializerExpr)); |
| 9259 |
|
| 9260 |
MOZ_TRY_VAR(accessorBody, |
| 9261 |
handler_.newExprStatement(assignment, propNamePos.end)); |
| 9262 |
|
| 9263 |
// 1.c. Return undefined. |
| 9264 |
} |
| 9265 |
|
| 9266 |
ListNodeType statementList; |
| 9267 |
MOZ_TRY_VAR(statementList, handler_.newStatementList(propNamePos)); |
| 9268 |
handler_.addStatementToList(statementList, accessorBody); |
| 9269 |
|
| 9270 |
bool canSkipLazyClosedOverBindings = handler_.reuseClosedOverBindings(); |
| 9271 |
if (!pc_->declareFunctionThis(usedNames_, canSkipLazyClosedOverBindings)) { |
| 9272 |
return errorResult(); |
| 9273 |
} |
| 9274 |
if (!pc_->declareNewTarget(usedNames_, canSkipLazyClosedOverBindings)) { |
| 9275 |
return errorResult(); |
| 9276 |
} |
| 9277 |
|
| 9278 |
LexicalScopeNodeType initializerBody; |
| 9279 |
MOZ_TRY_VAR(initializerBody, |
| 9280 |
finishLexicalScope(pc_->varScope(), statementList, |
| 9281 |
ScopeKind::FunctionLexical)); |
| 9282 |
|
| 9283 |
handler_.setFunctionBody(funNode, initializerBody); |
| 9284 |
|
| 9285 |
if (pc_->superScopeNeedsHomeObject()) { |
| 9286 |
funbox->setNeedsHomeObject(); |
| 9287 |
} |
| 9288 |
|
| 9289 |
if (!finishFunction()) { |
| 9290 |
return errorResult(); |
| 9291 |
} |
| 9292 |
|
| 9293 |
if (!leaveInnerFunction(outerpc)) { |
| 9294 |
return errorResult(); |
| 9295 |
} |
| 9296 |
|
| 9297 |
return funNode; |
| 9298 |
} |
| 9299 |
|
| 9300 |
#endif |
| 9301 |
|
| 9302 |
bool ParserBase::nextTokenContinuesLetDeclaration(TokenKind next) { |
| 9303 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Let)); |
| 9304 |
MOZ_ASSERT(anyChars.nextToken().type == next); |
| 9305 |
|
| 9306 |
TokenStreamShared::verifyConsistentModifier(TokenStreamShared::SlashIsDiv, |
| 9307 |
anyChars.nextToken()); |
| 9308 |
|
| 9309 |
// Destructuring continues a let declaration. |
| 9310 |
if (next == TokenKind::LeftBracket || next == TokenKind::LeftCurly) { |
| 9311 |
return true; |
| 9312 |
} |
| 9313 |
|
| 9314 |
// A "let" edge case deserves special comment. Consider this: |
| 9315 |
// |
| 9316 |
// let // not an ASI opportunity |
| 9317 |
// let; |
| 9318 |
// |
| 9319 |
// Static semantics in §13.3.1.1 turn a LexicalDeclaration that binds |
| 9320 |
// "let" into an early error. Does this retroactively permit ASI so |
| 9321 |
// that we should parse this as two ExpressionStatements? No. ASI |
| 9322 |
// resolves during parsing. Static semantics only apply to the full |
| 9323 |
// parse tree with ASI applied. No backsies! |
| 9324 |
|
| 9325 |
// Otherwise a let declaration must have a name. |
| 9326 |
return TokenKindIsPossibleIdentifier(next); |
| 9327 |
} |
| 9328 |
|
| 9329 |
template <class ParseHandler, typename Unit> |
| 9330 |
typename ParseHandler::DeclarationListNodeResult |
| 9331 |
GeneralParser<ParseHandler, Unit>::variableStatement( |
| 9332 |
YieldHandling yieldHandling) { |
| 9333 |
DeclarationListNodeType vars; |
| 9334 |
MOZ_TRY_VAR(vars, declarationList(yieldHandling, ParseNodeKind::VarStmt)); |
| 9335 |
if (!matchOrInsertSemicolon()) { |
| 9336 |
return errorResult(); |
| 9337 |
} |
| 9338 |
return vars; |
| 9339 |
} |
| 9340 |
|
| 9341 |
template <class ParseHandler, typename Unit> |
| 9342 |
typename ParseHandler::NodeResult GeneralParser<ParseHandler, Unit>::statement( |
| 9343 |
YieldHandling yieldHandling) { |
| 9344 |
MOZ_ASSERT(checkOptionsCalled_); |
| 9345 |
|
| 9346 |
AutoCheckRecursionLimit recursion(this->fc_); |
| 9347 |
if (!recursion.check(this->fc_)) { |
| 9348 |
return errorResult(); |
| 9349 |
} |
| 9350 |
|
| 9351 |
TokenKind tt; |
| 9352 |
if (!tokenStream.getToken(&tt, TokenStream::SlashIsRegExp)) { |
| 9353 |
return errorResult(); |
| 9354 |
} |
| 9355 |
|
| 9356 |
switch (tt) { |
| 9357 |
// BlockStatement[?Yield, ?Return] |
| 9358 |
case TokenKind::LeftCurly: |
| 9359 |
return blockStatement(yieldHandling); |
| 9360 |
|
| 9361 |
// VariableStatement[?Yield] |
| 9362 |
case TokenKind::Var: |
| 9363 |
return variableStatement(yieldHandling); |
| 9364 |
|
| 9365 |
// EmptyStatement |
| 9366 |
case TokenKind::Semi: |
| 9367 |
return handler_.newEmptyStatement(pos()); |
| 9368 |
|
| 9369 |
// ExpressionStatement[?Yield]. |
| 9370 |
|
| 9371 |
case TokenKind::Yield: { |
| 9372 |
// Don't use a ternary operator here due to obscure linker issues |
| 9373 |
// around using static consts in the arms of a ternary. |
| 9374 |
Modifier modifier; |
| 9375 |
if (yieldExpressionsSupported()) { |
| 9376 |
modifier = TokenStream::SlashIsRegExp; |
| 9377 |
} else { |
| 9378 |
modifier = TokenStream::SlashIsDiv; |
| 9379 |
} |
| 9380 |
|
| 9381 |
TokenKind next; |
| 9382 |
if (!tokenStream.peekToken(&next, modifier)) { |
| 9383 |
return errorResult(); |
| 9384 |
} |
| 9385 |
|
| 9386 |
if (next == TokenKind::Colon) { |
| 9387 |
return labeledStatement(yieldHandling); |
| 9388 |
} |
| 9389 |
|
| 9390 |
return expressionStatement(yieldHandling); |
| 9391 |
} |
| 9392 |
|
| 9393 |
default: { |
| 9394 |
// If we encounter an await in a module, and the module is not marked |
| 9395 |
// as async, mark the module as async. |
| 9396 |
if (tt == TokenKind::Await && !pc_->isAsync()) { |
| 9397 |
if (pc_->atModuleTopLevel()) { |
| 9398 |
if (!options().topLevelAwait) { |
| 9399 |
error(JSMSG_TOP_LEVEL_AWAIT_NOT_SUPPORTED); |
| 9400 |
return errorResult(); |
| 9401 |
} |
| 9402 |
pc_->sc()->asModuleContext()->setIsAsync(); |
| 9403 |
MOZ_ASSERT(pc_->isAsync()); |
| 9404 |
} |
| 9405 |
} |
| 9406 |
|
| 9407 |
// Avoid getting next token with SlashIsDiv. |
| 9408 |
if (tt == TokenKind::Await && pc_->isAsync()) { |
| 9409 |
return expressionStatement(yieldHandling); |
| 9410 |
} |
| 9411 |
|
| 9412 |
if (!TokenKindIsPossibleIdentifier(tt)) { |
| 9413 |
return expressionStatement(yieldHandling); |
| 9414 |
} |
| 9415 |
|
| 9416 |
TokenKind next; |
| 9417 |
if (!tokenStream.peekToken(&next)) { |
| 9418 |
return errorResult(); |
| 9419 |
} |
| 9420 |
|
| 9421 |
// |let| here can only be an Identifier, not a declaration. Give nicer |
| 9422 |
// errors for declaration-looking typos. |
| 9423 |
if (tt == TokenKind::Let) { |
| 9424 |
bool forbiddenLetDeclaration = false; |
| 9425 |
|
| 9426 |
if (next == TokenKind::LeftBracket) { |
| 9427 |
// Enforce ExpressionStatement's 'let [' lookahead restriction. |
| 9428 |
forbiddenLetDeclaration = true; |
| 9429 |
} else if (next == TokenKind::LeftCurly || |
| 9430 |
TokenKindIsPossibleIdentifier(next)) { |
| 9431 |
// 'let {' and 'let foo' aren't completely forbidden, if ASI |
| 9432 |
// causes 'let' to be the entire Statement. But if they're |
| 9433 |
// same-line, we can aggressively give a better error message. |
| 9434 |
// |
| 9435 |
// Note that this ignores 'yield' as TokenKind::Yield: we'll handle it |
| 9436 |
// correctly but with a worse error message. |
| 9437 |
TokenKind nextSameLine; |
| 9438 |
if (!tokenStream.peekTokenSameLine(&nextSameLine)) { |
| 9439 |
return errorResult(); |
| 9440 |
} |
| 9441 |
|
| 9442 |
MOZ_ASSERT(TokenKindIsPossibleIdentifier(nextSameLine) || |
| 9443 |
nextSameLine == TokenKind::LeftCurly || |
| 9444 |
nextSameLine == TokenKind::Eol); |
| 9445 |
|
| 9446 |
forbiddenLetDeclaration = nextSameLine != TokenKind::Eol; |
| 9447 |
} |
| 9448 |
|
| 9449 |
if (forbiddenLetDeclaration) { |
| 9450 |
error(JSMSG_FORBIDDEN_AS_STATEMENT, "lexical declarations"); |
| 9451 |
return errorResult(); |
| 9452 |
} |
| 9453 |
} else if (tt == TokenKind::Async) { |
| 9454 |
// Peek only on the same line: ExpressionStatement's lookahead |
| 9455 |
// restriction is phrased as |
| 9456 |
// |
| 9457 |
// [lookahead ∉ { '{', |
| 9458 |
// function, |
| 9459 |
// async [no LineTerminator here] function, |
| 9460 |
// class, |
| 9461 |
// let '[' }] |
| 9462 |
// |
| 9463 |
// meaning that code like this is valid: |
| 9464 |
// |
| 9465 |
// if (true) |
| 9466 |
// async // ASI opportunity |
| 9467 |
// function clownshoes() {} |
| 9468 |
TokenKind maybeFunction; |
| 9469 |
if (!tokenStream.peekTokenSameLine(&maybeFunction)) { |
| 9470 |
return errorResult(); |
| 9471 |
} |
| 9472 |
|
| 9473 |
if (maybeFunction == TokenKind::Function) { |
| 9474 |
error(JSMSG_FORBIDDEN_AS_STATEMENT, "async function declarations"); |
| 9475 |
return errorResult(); |
| 9476 |
} |
| 9477 |
|
| 9478 |
// Otherwise this |async| begins an ExpressionStatement or is a |
| 9479 |
// label name. |
| 9480 |
} |
| 9481 |
|
| 9482 |
// NOTE: It's unfortunately allowed to have a label named 'let' in |
| 9483 |
// non-strict code. 💯 |
| 9484 |
if (next == TokenKind::Colon) { |
| 9485 |
return labeledStatement(yieldHandling); |
| 9486 |
} |
| 9487 |
|
| 9488 |
return expressionStatement(yieldHandling); |
| 9489 |
} |
| 9490 |
|
| 9491 |
case TokenKind::New: |
| 9492 |
return expressionStatement(yieldHandling, PredictInvoked); |
| 9493 |
|
| 9494 |
// IfStatement[?Yield, ?Return] |
| 9495 |
case TokenKind::If: |
| 9496 |
return ifStatement(yieldHandling); |
| 9497 |
|
| 9498 |
// BreakableStatement[?Yield, ?Return] |
| 9499 |
// |
| 9500 |
// BreakableStatement[Yield, Return]: |
| 9501 |
// IterationStatement[?Yield, ?Return] |
| 9502 |
// SwitchStatement[?Yield, ?Return] |
| 9503 |
case TokenKind::Do: |
| 9504 |
return doWhileStatement(yieldHandling); |
| 9505 |
|
| 9506 |
case TokenKind::While: |
| 9507 |
return whileStatement(yieldHandling); |
| 9508 |
|
| 9509 |
case TokenKind::For: |
| 9510 |
return forStatement(yieldHandling); |
| 9511 |
|
| 9512 |
case TokenKind::Switch: |
| 9513 |
return switchStatement(yieldHandling); |
| 9514 |
|
| 9515 |
// ContinueStatement[?Yield] |
| 9516 |
case TokenKind::Continue: |
| 9517 |
return continueStatement(yieldHandling); |
| 9518 |
|
| 9519 |
// BreakStatement[?Yield] |
| 9520 |
case TokenKind::Break: |
| 9521 |
return breakStatement(yieldHandling); |
| 9522 |
|
| 9523 |
// [+Return] ReturnStatement[?Yield] |
| 9524 |
case TokenKind::Return: |
| 9525 |
// The Return parameter is only used here, and the effect is easily |
| 9526 |
// detected this way, so don't bother passing around an extra parameter |
| 9527 |
// everywhere. |
| 9528 |
if (!pc_->allowReturn()) { |
| 9529 |
error(JSMSG_BAD_RETURN_OR_YIELD, "return"); |
| 9530 |
return errorResult(); |
| 9531 |
} |
| 9532 |
return returnStatement(yieldHandling); |
| 9533 |
|
| 9534 |
// WithStatement[?Yield, ?Return] |
| 9535 |
case TokenKind::With: |
| 9536 |
return withStatement(yieldHandling); |
| 9537 |
|
| 9538 |
// LabelledStatement[?Yield, ?Return] |
| 9539 |
// This is really handled by default and TokenKind::Yield cases above. |
| 9540 |
|
| 9541 |
// ThrowStatement[?Yield] |
| 9542 |
case TokenKind::Throw: |
| 9543 |
return throwStatement(yieldHandling); |
| 9544 |
|
| 9545 |
// TryStatement[?Yield, ?Return] |
| 9546 |
case TokenKind::Try: |
| 9547 |
return tryStatement(yieldHandling); |
| 9548 |
|
| 9549 |
// DebuggerStatement |
| 9550 |
case TokenKind::Debugger: |
| 9551 |
return debuggerStatement(); |
| 9552 |
|
| 9553 |
// |function| is forbidden by lookahead restriction (unless as child |
| 9554 |
// statement of |if| or |else|, but Parser::consequentOrAlternative |
| 9555 |
// handles that). |
| 9556 |
case TokenKind::Function: |
| 9557 |
error(JSMSG_FORBIDDEN_AS_STATEMENT, "function declarations"); |
| 9558 |
return errorResult(); |
| 9559 |
|
| 9560 |
// |class| is also forbidden by lookahead restriction. |
| 9561 |
case TokenKind::Class: |
| 9562 |
error(JSMSG_FORBIDDEN_AS_STATEMENT, "classes"); |
| 9563 |
return errorResult(); |
| 9564 |
|
| 9565 |
// ImportDeclaration (only inside modules) |
| 9566 |
case TokenKind::Import: |
| 9567 |
return importDeclarationOrImportExpr(yieldHandling); |
| 9568 |
|
| 9569 |
// ExportDeclaration (only inside modules) |
| 9570 |
case TokenKind::Export: |
| 9571 |
return exportDeclaration(); |
| 9572 |
|
| 9573 |
// Miscellaneous error cases arguably better caught here than elsewhere. |
| 9574 |
|
| 9575 |
case TokenKind::Catch: |
| 9576 |
error(JSMSG_CATCH_WITHOUT_TRY); |
| 9577 |
return errorResult(); |
| 9578 |
|
| 9579 |
case TokenKind::Finally: |
| 9580 |
error(JSMSG_FINALLY_WITHOUT_TRY); |
| 9581 |
return errorResult(); |
| 9582 |
|
| 9583 |
// NOTE: default case handled in the ExpressionStatement section. |
| 9584 |
} |
| 9585 |
} |
| 9586 |
|
| 9587 |
template <class ParseHandler, typename Unit> |
| 9588 |
typename ParseHandler::NodeResult |
| 9589 |
GeneralParser<ParseHandler, Unit>::statementListItem( |
| 9590 |
YieldHandling yieldHandling, bool canHaveDirectives /* = false */) { |
| 9591 |
MOZ_ASSERT(checkOptionsCalled_); |
| 9592 |
|
| 9593 |
AutoCheckRecursionLimit recursion(this->fc_); |
| 9594 |
if (!recursion.check(this->fc_)) { |
| 9595 |
return errorResult(); |
| 9596 |
} |
| 9597 |
|
| 9598 |
TokenKind tt; |
| 9599 |
if (!tokenStream.getToken(&tt, TokenStream::SlashIsRegExp)) { |
| 9600 |
return errorResult(); |
| 9601 |
} |
| 9602 |
|
| 9603 |
switch (tt) { |
| 9604 |
// BlockStatement[?Yield, ?Return] |
| 9605 |
case TokenKind::LeftCurly: |
| 9606 |
return blockStatement(yieldHandling); |
| 9607 |
|
| 9608 |
// VariableStatement[?Yield] |
| 9609 |
case TokenKind::Var: |
| 9610 |
return variableStatement(yieldHandling); |
| 9611 |
|
| 9612 |
// EmptyStatement |
| 9613 |
case TokenKind::Semi: |
| 9614 |
return handler_.newEmptyStatement(pos()); |
| 9615 |
|
| 9616 |
// ExpressionStatement[?Yield]. |
| 9617 |
// |
| 9618 |
// These should probably be handled by a single ExpressionStatement |
| 9619 |
// function in a default, not split up this way. |
| 9620 |
case TokenKind::String: |
| 9621 |
if (!canHaveDirectives && |
| 9622 |
anyChars.currentToken().atom() == |
| 9623 |
TaggedParserAtomIndex::WellKnown::use_asm_()) { |
| 9624 |
if (!warning(JSMSG_USE_ASM_DIRECTIVE_FAIL)) { |
| 9625 |
return errorResult(); |
| 9626 |
} |
| 9627 |
} |
| 9628 |
return expressionStatement(yieldHandling); |
| 9629 |
|
| 9630 |
case TokenKind::Yield: { |
| 9631 |
// Don't use a ternary operator here due to obscure linker issues |
| 9632 |
// around using static consts in the arms of a ternary. |
| 9633 |
Modifier modifier; |
| 9634 |
if (yieldExpressionsSupported()) { |
| 9635 |
modifier = TokenStream::SlashIsRegExp; |
| 9636 |
} else { |
| 9637 |
modifier = TokenStream::SlashIsDiv; |
| 9638 |
} |
| 9639 |
|
| 9640 |
TokenKind next; |
| 9641 |
if (!tokenStream.peekToken(&next, modifier)) { |
| 9642 |
return errorResult(); |
| 9643 |
} |
| 9644 |
|
| 9645 |
if (next == TokenKind::Colon) { |
| 9646 |
return labeledStatement(yieldHandling); |
| 9647 |
} |
| 9648 |
|
| 9649 |
return expressionStatement(yieldHandling); |
| 9650 |
} |
| 9651 |
|
| 9652 |
default: { |
| 9653 |
// If we encounter an await in a module, and the module is not marked |
| 9654 |
// as async, mark the module as async. |
| 9655 |
if (tt == TokenKind::Await && !pc_->isAsync()) { |
| 9656 |
if (pc_->atModuleTopLevel()) { |
| 9657 |
if (!options().topLevelAwait) { |
| 9658 |
error(JSMSG_TOP_LEVEL_AWAIT_NOT_SUPPORTED); |
| 9659 |
return errorResult(); |
| 9660 |
} |
| 9661 |
pc_->sc()->asModuleContext()->setIsAsync(); |
| 9662 |
MOZ_ASSERT(pc_->isAsync()); |
| 9663 |
} |
| 9664 |
} |
| 9665 |
|
| 9666 |
if (tt == TokenKind::Await && pc_->isAsync()) { |
| 9667 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 9668 |
if (options().explicitResourceManagement()) { |
| 9669 |
// Try finding evidence of a AwaitUsingDeclaration the syntax for |
| 9670 |
// which |
| 9671 |
// would be: |
| 9672 |
// await [no LineTerminator here] using [no LineTerminator here] |
| 9673 |
// identifier |
| 9674 |
|
| 9675 |
TokenKind nextTokUsing = TokenKind::Eof; |
| 9676 |
// Scan with regex modifier because when its await expression, `/` |
| 9677 |
// should be treated as a regexp. |
| 9678 |
if (!tokenStream.peekTokenSameLine(&nextTokUsing, |
| 9679 |
TokenStream::SlashIsRegExp)) { |
| 9680 |
return errorResult(); |
| 9681 |
} |
| 9682 |
|
| 9683 |
if (nextTokUsing == TokenKind::Using && |
| 9684 |
this->pc_->isUsingSyntaxAllowed()) { |
| 9685 |
tokenStream.consumeKnownToken(nextTokUsing, |
| 9686 |
TokenStream::SlashIsRegExp); |
| 9687 |
TokenKind nextTokIdentifier = TokenKind::Eof; |
| 9688 |
// Here we can use the Div modifier because if the next token is |
| 9689 |
// using then a `/` as the next token can only be considered a |
| 9690 |
// division. |
| 9691 |
if (!tokenStream.peekTokenSameLine(&nextTokIdentifier)) { |
| 9692 |
return errorResult(); |
| 9693 |
} |
| 9694 |
if (TokenKindIsPossibleIdentifier(nextTokIdentifier)) { |
| 9695 |
return lexicalDeclaration(yieldHandling, |
| 9696 |
DeclarationKind::AwaitUsing); |
| 9697 |
} |
| 9698 |
anyChars.ungetToken(); // put back using. |
| 9699 |
} |
| 9700 |
} |
| 9701 |
#endif |
| 9702 |
return expressionStatement(yieldHandling); |
| 9703 |
} |
| 9704 |
|
| 9705 |
if (!TokenKindIsPossibleIdentifier(tt)) { |
| 9706 |
return expressionStatement(yieldHandling); |
| 9707 |
} |
| 9708 |
|
| 9709 |
TokenKind next; |
| 9710 |
if (!tokenStream.peekToken(&next)) { |
| 9711 |
return errorResult(); |
| 9712 |
} |
| 9713 |
|
| 9714 |
if (tt == TokenKind::Let && nextTokenContinuesLetDeclaration(next)) { |
| 9715 |
return lexicalDeclaration(yieldHandling, DeclarationKind::Let); |
| 9716 |
} |
| 9717 |
|
| 9718 |
if (tt == TokenKind::Async) { |
| 9719 |
TokenKind nextSameLine = TokenKind::Eof; |
| 9720 |
if (!tokenStream.peekTokenSameLine(&nextSameLine)) { |
| 9721 |
return errorResult(); |
| 9722 |
} |
| 9723 |
if (nextSameLine == TokenKind::Function) { |
| 9724 |
uint32_t toStringStart = pos().begin; |
| 9725 |
tokenStream.consumeKnownToken(TokenKind::Function); |
| 9726 |
return functionStmt(toStringStart, yieldHandling, NameRequired, |
| 9727 |
FunctionAsyncKind::AsyncFunction); |
| 9728 |
} |
| 9729 |
} |
| 9730 |
|
| 9731 |
if (next == TokenKind::Colon) { |
| 9732 |
return labeledStatement(yieldHandling); |
| 9733 |
} |
| 9734 |
|
| 9735 |
return expressionStatement(yieldHandling); |
| 9736 |
} |
| 9737 |
|
| 9738 |
case TokenKind::New: |
| 9739 |
return expressionStatement(yieldHandling, PredictInvoked); |
| 9740 |
|
| 9741 |
// IfStatement[?Yield, ?Return] |
| 9742 |
case TokenKind::If: |
| 9743 |
return ifStatement(yieldHandling); |
| 9744 |
|
| 9745 |
// BreakableStatement[?Yield, ?Return] |
| 9746 |
// |
| 9747 |
// BreakableStatement[Yield, Return]: |
| 9748 |
// IterationStatement[?Yield, ?Return] |
| 9749 |
// SwitchStatement[?Yield, ?Return] |
| 9750 |
case TokenKind::Do: |
| 9751 |
return doWhileStatement(yieldHandling); |
| 9752 |
|
| 9753 |
case TokenKind::While: |
| 9754 |
return whileStatement(yieldHandling); |
| 9755 |
|
| 9756 |
case TokenKind::For: |
| 9757 |
return forStatement(yieldHandling); |
| 9758 |
|
| 9759 |
case TokenKind::Switch: |
| 9760 |
return switchStatement(yieldHandling); |
| 9761 |
|
| 9762 |
// ContinueStatement[?Yield] |
| 9763 |
case TokenKind::Continue: |
| 9764 |
return continueStatement(yieldHandling); |
| 9765 |
|
| 9766 |
// BreakStatement[?Yield] |
| 9767 |
case TokenKind::Break: |
| 9768 |
return breakStatement(yieldHandling); |
| 9769 |
|
| 9770 |
// [+Return] ReturnStatement[?Yield] |
| 9771 |
case TokenKind::Return: |
| 9772 |
// The Return parameter is only used here, and the effect is easily |
| 9773 |
// detected this way, so don't bother passing around an extra parameter |
| 9774 |
// everywhere. |
| 9775 |
if (!pc_->allowReturn()) { |
| 9776 |
error(JSMSG_BAD_RETURN_OR_YIELD, "return"); |
| 9777 |
return errorResult(); |
| 9778 |
} |
| 9779 |
return returnStatement(yieldHandling); |
| 9780 |
|
| 9781 |
// WithStatement[?Yield, ?Return] |
| 9782 |
case TokenKind::With: |
| 9783 |
return withStatement(yieldHandling); |
| 9784 |
|
| 9785 |
// LabelledStatement[?Yield, ?Return] |
| 9786 |
// This is really handled by default and TokenKind::Yield cases above. |
| 9787 |
|
| 9788 |
// ThrowStatement[?Yield] |
| 9789 |
case TokenKind::Throw: |
| 9790 |
return throwStatement(yieldHandling); |
| 9791 |
|
| 9792 |
// TryStatement[?Yield, ?Return] |
| 9793 |
case TokenKind::Try: |
| 9794 |
return tryStatement(yieldHandling); |
| 9795 |
|
| 9796 |
// DebuggerStatement |
| 9797 |
case TokenKind::Debugger: |
| 9798 |
return debuggerStatement(); |
| 9799 |
|
| 9800 |
// Declaration[Yield]: |
| 9801 |
|
| 9802 |
// HoistableDeclaration[?Yield, ~Default] |
| 9803 |
case TokenKind::Function: |
| 9804 |
return functionStmt(pos().begin, yieldHandling, NameRequired); |
| 9805 |
|
| 9806 |
// DecoratorList[?Yield, ?Await] opt ClassDeclaration[?Yield, ~Default] |
| 9807 |
#ifdef ENABLE_DECORATORS |
| 9808 |
case TokenKind::At: |
| 9809 |
return classDefinition(yieldHandling, ClassStatement, NameRequired); |
| 9810 |
#endif |
| 9811 |
|
| 9812 |
case TokenKind::Class: |
| 9813 |
return classDefinition(yieldHandling, ClassStatement, NameRequired); |
| 9814 |
|
| 9815 |
// LexicalDeclaration[In, ?Yield] |
| 9816 |
// LetOrConst BindingList[?In, ?Yield] |
| 9817 |
case TokenKind::Const: |
| 9818 |
// [In] is the default behavior, because for-loops specially parse |
| 9819 |
// their heads to handle |in| in this situation. |
| 9820 |
return lexicalDeclaration(yieldHandling, DeclarationKind::Const); |
| 9821 |
|
| 9822 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 9823 |
case TokenKind::Using: { |
| 9824 |
TokenKind nextTok = TokenKind::Eol; |
| 9825 |
if (!tokenStream.peekTokenSameLine(&nextTok)) { |
| 9826 |
return errorResult(); |
| 9827 |
} |
| 9828 |
if (!options().explicitResourceManagement() || |
| 9829 |
!TokenKindIsPossibleIdentifier(nextTok) || |
| 9830 |
!this->pc_->isUsingSyntaxAllowed()) { |
| 9831 |
if (!tokenStream.peekToken(&nextTok)) { |
| 9832 |
return errorResult(); |
| 9833 |
} |
| 9834 |
// labelled statement could be like using\n:\nexpr |
| 9835 |
if (nextTok == TokenKind::Colon) { |
| 9836 |
return labeledStatement(yieldHandling); |
| 9837 |
} |
| 9838 |
return expressionStatement(yieldHandling); |
| 9839 |
} |
| 9840 |
return lexicalDeclaration(yieldHandling, DeclarationKind::Using); |
| 9841 |
} |
| 9842 |
#endif |
| 9843 |
|
| 9844 |
// ImportDeclaration (only inside modules) |
| 9845 |
case TokenKind::Import: |
| 9846 |
return importDeclarationOrImportExpr(yieldHandling); |
| 9847 |
|
| 9848 |
// ExportDeclaration (only inside modules) |
| 9849 |
case TokenKind::Export: |
| 9850 |
return exportDeclaration(); |
| 9851 |
|
| 9852 |
// Miscellaneous error cases arguably better caught here than elsewhere. |
| 9853 |
|
| 9854 |
case TokenKind::Catch: |
| 9855 |
error(JSMSG_CATCH_WITHOUT_TRY); |
| 9856 |
return errorResult(); |
| 9857 |
|
| 9858 |
case TokenKind::Finally: |
| 9859 |
error(JSMSG_FINALLY_WITHOUT_TRY); |
| 9860 |
return errorResult(); |
| 9861 |
|
| 9862 |
// NOTE: default case handled in the ExpressionStatement section. |
| 9863 |
} |
| 9864 |
} |
| 9865 |
|
| 9866 |
template <class ParseHandler, typename Unit> |
| 9867 |
typename ParseHandler::NodeResult GeneralParser<ParseHandler, Unit>::expr( |
| 9868 |
InHandling inHandling, YieldHandling yieldHandling, |
| 9869 |
TripledotHandling tripledotHandling, |
| 9870 |
PossibleError* possibleError /* = nullptr */, |
| 9871 |
InvokedPrediction invoked /* = PredictUninvoked */) { |
| 9872 |
Node pn; |
| 9873 |
MOZ_TRY_VAR(pn, assignExpr(inHandling, yieldHandling, tripledotHandling, |
| 9874 |
possibleError, invoked)); |
| 9875 |
|
| 9876 |
bool matched; |
| 9877 |
if (!tokenStream.matchToken(&matched, TokenKind::Comma, |
| 9878 |
TokenStream::SlashIsRegExp)) { |
| 9879 |
return errorResult(); |
| 9880 |
} |
| 9881 |
if (!matched) { |
| 9882 |
return pn; |
| 9883 |
} |
| 9884 |
|
| 9885 |
ListNodeType seq; |
| 9886 |
MOZ_TRY_VAR(seq, handler_.newCommaExpressionList(pn)); |
| 9887 |
while (true) { |
| 9888 |
// Trailing comma before the closing parenthesis is valid in an arrow |
| 9889 |
// function parameters list: `(a, b, ) => body`. Check if we are |
| 9890 |
// directly under CoverParenthesizedExpressionAndArrowParameterList, |
| 9891 |
// and the next two tokens are closing parenthesis and arrow. If all |
| 9892 |
// are present allow the trailing comma. |
| 9893 |
if (tripledotHandling == TripledotAllowed) { |
| 9894 |
TokenKind tt; |
| 9895 |
if (!tokenStream.peekToken(&tt, TokenStream::SlashIsRegExp)) { |
| 9896 |
return errorResult(); |
| 9897 |
} |
| 9898 |
|
| 9899 |
if (tt == TokenKind::RightParen) { |
| 9900 |
tokenStream.consumeKnownToken(TokenKind::RightParen, |
| 9901 |
TokenStream::SlashIsRegExp); |
| 9902 |
|
| 9903 |
if (!tokenStream.peekToken(&tt)) { |
| 9904 |
return errorResult(); |
| 9905 |
} |
| 9906 |
if (tt != TokenKind::Arrow) { |
| 9907 |
error(JSMSG_UNEXPECTED_TOKEN, "expression", |
| 9908 |
TokenKindToDesc(TokenKind::RightParen)); |
| 9909 |
return errorResult(); |
| 9910 |
} |
| 9911 |
|
| 9912 |
anyChars.ungetToken(); // put back right paren |
| 9913 |
break; |
| 9914 |
} |
| 9915 |
} |
| 9916 |
|
| 9917 |
// Additional calls to assignExpr should not reuse the possibleError |
| 9918 |
// which had been passed into the function. Otherwise we would lose |
| 9919 |
// information needed to determine whether or not we're dealing with |
| 9920 |
// a non-recoverable situation. |
| 9921 |
PossibleError possibleErrorInner(*this); |
| 9922 |
MOZ_TRY_VAR(pn, assignExpr(inHandling, yieldHandling, tripledotHandling, |
| 9923 |
&possibleErrorInner)); |
| 9924 |
|
| 9925 |
if (!possibleError) { |
| 9926 |
// Report any pending expression error. |
| 9927 |
if (!possibleErrorInner.checkForExpressionError()) { |
| 9928 |
return errorResult(); |
| 9929 |
} |
| 9930 |
} else { |
| 9931 |
possibleErrorInner.transferErrorsTo(possibleError); |
| 9932 |
} |
| 9933 |
|
| 9934 |
handler_.addList(seq, pn); |
| 9935 |
|
| 9936 |
if (!tokenStream.matchToken(&matched, TokenKind::Comma, |
| 9937 |
TokenStream::SlashIsRegExp)) { |
| 9938 |
return errorResult(); |
| 9939 |
} |
| 9940 |
if (!matched) { |
| 9941 |
break; |
| 9942 |
} |
| 9943 |
} |
| 9944 |
return seq; |
| 9945 |
} |
| 9946 |
|
| 9947 |
static ParseNodeKind BinaryOpTokenKindToParseNodeKind(TokenKind tok) { |
| 9948 |
MOZ_ASSERT(TokenKindIsBinaryOp(tok)); |
| 9949 |
return ParseNodeKind(size_t(ParseNodeKind::BinOpFirst) + |
| 9950 |
(size_t(tok) - size_t(TokenKind::BinOpFirst))); |
| 9951 |
} |
| 9952 |
|
| 9953 |
// This list must be kept in the same order in several places: |
| 9954 |
// - The binary operators in ParseNode.h , |
| 9955 |
// - the binary operators in TokenKind.h |
| 9956 |
// - the JSOp code list in BytecodeEmitter.cpp |
| 9957 |
static const int PrecedenceTable[] = { |
| 9958 |
1, /* ParseNodeKind::Coalesce */ |
| 9959 |
2, /* ParseNodeKind::Or */ |
| 9960 |
3, /* ParseNodeKind::And */ |
| 9961 |
4, /* ParseNodeKind::BitOr */ |
| 9962 |
5, /* ParseNodeKind::BitXor */ |
| 9963 |
6, /* ParseNodeKind::BitAnd */ |
| 9964 |
7, /* ParseNodeKind::StrictEq */ |
| 9965 |
7, /* ParseNodeKind::Eq */ |
| 9966 |
7, /* ParseNodeKind::StrictNe */ |
| 9967 |
7, /* ParseNodeKind::Ne */ |
| 9968 |
8, /* ParseNodeKind::Lt */ |
| 9969 |
8, /* ParseNodeKind::Le */ |
| 9970 |
8, /* ParseNodeKind::Gt */ |
| 9971 |
8, /* ParseNodeKind::Ge */ |
| 9972 |
8, /* ParseNodeKind::InstanceOf */ |
| 9973 |
8, /* ParseNodeKind::In */ |
| 9974 |
8, /* ParseNodeKind::PrivateIn */ |
| 9975 |
9, /* ParseNodeKind::Lsh */ |
| 9976 |
9, /* ParseNodeKind::Rsh */ |
| 9977 |
9, /* ParseNodeKind::Ursh */ |
| 9978 |
10, /* ParseNodeKind::Add */ |
| 9979 |
10, /* ParseNodeKind::Sub */ |
| 9980 |
11, /* ParseNodeKind::Star */ |
| 9981 |
11, /* ParseNodeKind::Div */ |
| 9982 |
11, /* ParseNodeKind::Mod */ |
| 9983 |
12 /* ParseNodeKind::Pow */ |
| 9984 |
}; |
| 9985 |
|
| 9986 |
static const int PRECEDENCE_CLASSES = 12; |
| 9987 |
|
| 9988 |
static int Precedence(ParseNodeKind pnk) { |
| 9989 |
// Everything binds tighter than ParseNodeKind::Limit, because we want |
| 9990 |
// to reduce all nodes to a single node when we reach a token that is not |
| 9991 |
// another binary operator. |
| 9992 |
if (pnk == ParseNodeKind::Limit) { |
| 9993 |
return 0; |
| 9994 |
} |
| 9995 |
|
| 9996 |
MOZ_ASSERT(pnk >= ParseNodeKind::BinOpFirst); |
| 9997 |
MOZ_ASSERT(pnk <= ParseNodeKind::BinOpLast); |
| 9998 |
return PrecedenceTable[size_t(pnk) - size_t(ParseNodeKind::BinOpFirst)]; |
| 9999 |
} |
| 10000 |
|
| 10001 |
enum class EnforcedParentheses : uint8_t { CoalesceExpr, AndOrExpr, None }; |
| 10002 |
|
| 10003 |
template <class ParseHandler, typename Unit> |
| 10004 |
MOZ_ALWAYS_INLINE typename ParseHandler::NodeResult |
| 10005 |
GeneralParser<ParseHandler, Unit>::orExpr(InHandling inHandling, |
| 10006 |
YieldHandling yieldHandling, |
| 10007 |
TripledotHandling tripledotHandling, |
| 10008 |
PossibleError* possibleError, |
| 10009 |
InvokedPrediction invoked) { |
| 10010 |
// Shift-reduce parser for the binary operator part of the JS expression |
| 10011 |
// syntax. |
| 10012 |
|
| 10013 |
// Conceptually there's just one stack, a stack of pairs (lhs, op). |
| 10014 |
// It's implemented using two separate arrays, though. |
| 10015 |
Node nodeStack[PRECEDENCE_CLASSES]; |
| 10016 |
ParseNodeKind kindStack[PRECEDENCE_CLASSES]; |
| 10017 |
int depth = 0; |
| 10018 |
Node pn; |
| 10019 |
EnforcedParentheses unparenthesizedExpression = EnforcedParentheses::None; |
| 10020 |
for (;;) { |
| 10021 |
MOZ_TRY_VAR( |
| 10022 |
pn, unaryExpr(yieldHandling, tripledotHandling, possibleError, invoked, |
| 10023 |
PrivateNameHandling::PrivateNameAllowed)); |
| 10024 |
|
| 10025 |
// If a binary operator follows, consume it and compute the |
| 10026 |
// corresponding operator. |
| 10027 |
TokenKind tok; |
| 10028 |
if (!tokenStream.getToken(&tok)) { |
| 10029 |
return errorResult(); |
| 10030 |
} |
| 10031 |
|
| 10032 |
// Ensure that if we have a private name lhs we are legally constructing a |
| 10033 |
// `#x in obj` expessions: |
| 10034 |
if (handler_.isPrivateName(pn)) { |
| 10035 |
if (tok != TokenKind::In || inHandling != InAllowed) { |
| 10036 |
error(JSMSG_ILLEGAL_PRIVATE_NAME); |
| 10037 |
return errorResult(); |
| 10038 |
} |
| 10039 |
} |
| 10040 |
|
| 10041 |
ParseNodeKind pnk; |
| 10042 |
if (tok == TokenKind::In ? inHandling == InAllowed |
| 10043 |
: TokenKindIsBinaryOp(tok)) { |
| 10044 |
// We're definitely not in a destructuring context, so report any |
| 10045 |
// pending expression error now. |
| 10046 |
if (possibleError && !possibleError->checkForExpressionError()) { |
| 10047 |
return errorResult(); |
| 10048 |
} |
| 10049 |
|
| 10050 |
bool isErgonomicBrandCheck = false; |
| 10051 |
switch (tok) { |
| 10052 |
// Report an error for unary expressions on the LHS of **. |
| 10053 |
case TokenKind::Pow: |
| 10054 |
if (handler_.isUnparenthesizedUnaryExpression(pn)) { |
| 10055 |
error(JSMSG_BAD_POW_LEFTSIDE); |
| 10056 |
return errorResult(); |
| 10057 |
} |
| 10058 |
break; |
| 10059 |
|
| 10060 |
case TokenKind::Or: |
| 10061 |
case TokenKind::And: |
| 10062 |
// In the case that the `??` is on the left hand side of the |
| 10063 |
// expression: Disallow Mixing of ?? and other logical operators (|| |
| 10064 |
// and &&) unless one expression is parenthesized |
| 10065 |
if (unparenthesizedExpression == EnforcedParentheses::CoalesceExpr) { |
| 10066 |
error(JSMSG_BAD_COALESCE_MIXING); |
| 10067 |
return errorResult(); |
| 10068 |
} |
| 10069 |
// If we have not detected a mixing error at this point, record that |
| 10070 |
// we have an unparenthesized expression, in case we have one later. |
| 10071 |
unparenthesizedExpression = EnforcedParentheses::AndOrExpr; |
| 10072 |
break; |
| 10073 |
|
| 10074 |
case TokenKind::Coalesce: |
| 10075 |
if (unparenthesizedExpression == EnforcedParentheses::AndOrExpr) { |
| 10076 |
error(JSMSG_BAD_COALESCE_MIXING); |
| 10077 |
return errorResult(); |
| 10078 |
} |
| 10079 |
// If we have not detected a mixing error at this point, record that |
| 10080 |
// we have an unparenthesized expression, in case we have one later. |
| 10081 |
unparenthesizedExpression = EnforcedParentheses::CoalesceExpr; |
| 10082 |
break; |
| 10083 |
|
| 10084 |
case TokenKind::In: |
| 10085 |
// if the LHS is a private name, and the operator is In, |
| 10086 |
// ensure we're construcing an ergonomic brand check of |
| 10087 |
// '#x in y', rather than having a higher precedence operator |
| 10088 |
// like + cause a different reduction, such as |
| 10089 |
// 1 + #x in y. |
| 10090 |
if (handler_.isPrivateName(pn)) { |
| 10091 |
if (depth > 0 && Precedence(kindStack[depth - 1]) >= |
| 10092 |
Precedence(ParseNodeKind::InExpr)) { |
| 10093 |
error(JSMSG_INVALID_PRIVATE_NAME_PRECEDENCE); |
| 10094 |
return errorResult(); |
| 10095 |
} |
| 10096 |
|
| 10097 |
isErgonomicBrandCheck = true; |
| 10098 |
} |
| 10099 |
break; |
| 10100 |
|
| 10101 |
default: |
| 10102 |
// do nothing in other cases |
| 10103 |
break; |
| 10104 |
} |
| 10105 |
|
| 10106 |
if (isErgonomicBrandCheck) { |
| 10107 |
pnk = ParseNodeKind::PrivateInExpr; |
| 10108 |
} else { |
| 10109 |
pnk = BinaryOpTokenKindToParseNodeKind(tok); |
| 10110 |
} |
| 10111 |
|
| 10112 |
} else { |
| 10113 |
tok = TokenKind::Eof; |
| 10114 |
pnk = ParseNodeKind::Limit; |
| 10115 |
} |
| 10116 |
|
| 10117 |
// From this point on, destructuring defaults are definitely an error. |
| 10118 |
possibleError = nullptr; |
| 10119 |
|
| 10120 |
// If pnk has precedence less than or equal to another operator on the |
| 10121 |
// stack, reduce. This combines nodes on the stack until we form the |
| 10122 |
// actual lhs of pnk. |
| 10123 |
// |
| 10124 |
// The >= in this condition works because it is appendOrCreateList's |
| 10125 |
// job to decide if the operator in question is left- or |
| 10126 |
// right-associative, and build the corresponding tree. |
| 10127 |
while (depth > 0 && Precedence(kindStack[depth - 1]) >= Precedence(pnk)) { |
| 10128 |
depth--; |
| 10129 |
ParseNodeKind combiningPnk = kindStack[depth]; |
| 10130 |
MOZ_TRY_VAR(pn, handler_.appendOrCreateList(combiningPnk, |
| 10131 |
nodeStack[depth], pn, pc_)); |
| 10132 |
} |
| 10133 |
|
| 10134 |
if (pnk == ParseNodeKind::Limit) { |
| 10135 |
break; |
| 10136 |
} |
| 10137 |
|
| 10138 |
nodeStack[depth] = pn; |
| 10139 |
kindStack[depth] = pnk; |
| 10140 |
depth++; |
| 10141 |
MOZ_ASSERT(depth <= PRECEDENCE_CLASSES); |
| 10142 |
} |
| 10143 |
|
| 10144 |
anyChars.ungetToken(); |
| 10145 |
|
| 10146 |
// Had the next token been a Div, we would have consumed it. So there's no |
| 10147 |
// ambiguity if we later (after ASI) re-get this token with SlashIsRegExp. |
| 10148 |
anyChars.allowGettingNextTokenWithSlashIsRegExp(); |
| 10149 |
|
| 10150 |
MOZ_ASSERT(depth == 0); |
| 10151 |
return pn; |
| 10152 |
} |
| 10153 |
|
| 10154 |
template <class ParseHandler, typename Unit> |
| 10155 |
MOZ_ALWAYS_INLINE typename ParseHandler::NodeResult |
| 10156 |
GeneralParser<ParseHandler, Unit>::condExpr(InHandling inHandling, |
| 10157 |
YieldHandling yieldHandling, |
| 10158 |
TripledotHandling tripledotHandling, |
| 10159 |
PossibleError* possibleError, |
| 10160 |
InvokedPrediction invoked) { |
| 10161 |
Node condition; |
| 10162 |
MOZ_TRY_VAR(condition, orExpr(inHandling, yieldHandling, tripledotHandling, |
| 10163 |
possibleError, invoked)); |
| 10164 |
|
| 10165 |
bool matched; |
| 10166 |
if (!tokenStream.matchToken(&matched, TokenKind::Hook, |
| 10167 |
TokenStream::SlashIsInvalid)) { |
| 10168 |
return errorResult(); |
| 10169 |
} |
| 10170 |
if (!matched) { |
| 10171 |
return condition; |
| 10172 |
} |
| 10173 |
|
| 10174 |
Node thenExpr; |
| 10175 |
MOZ_TRY_VAR(thenExpr, |
| 10176 |
assignExpr(InAllowed, yieldHandling, TripledotProhibited)); |
| 10177 |
|
| 10178 |
if (!mustMatchToken(TokenKind::Colon, JSMSG_COLON_IN_COND)) { |
| 10179 |
return errorResult(); |
| 10180 |
} |
| 10181 |
|
| 10182 |
Node elseExpr; |
| 10183 |
MOZ_TRY_VAR(elseExpr, |
| 10184 |
assignExpr(inHandling, yieldHandling, TripledotProhibited)); |
| 10185 |
|
| 10186 |
return handler_.newConditional(condition, thenExpr, elseExpr); |
| 10187 |
} |
| 10188 |
|
| 10189 |
template <class ParseHandler, typename Unit> |
| 10190 |
typename ParseHandler::NodeResult GeneralParser<ParseHandler, Unit>::assignExpr( |
| 10191 |
InHandling inHandling, YieldHandling yieldHandling, |
| 10192 |
TripledotHandling tripledotHandling, |
| 10193 |
PossibleError* possibleError /* = nullptr */, |
| 10194 |
InvokedPrediction invoked /* = PredictUninvoked */) { |
| 10195 |
AutoCheckRecursionLimit recursion(this->fc_); |
| 10196 |
if (!recursion.check(this->fc_)) { |
| 10197 |
return errorResult(); |
| 10198 |
} |
| 10199 |
|
| 10200 |
// It's very common at this point to have a "detectably simple" expression, |
| 10201 |
// i.e. a name/number/string token followed by one of the following tokens |
| 10202 |
// that obviously isn't part of an expression: , ; : ) ] } |
| 10203 |
// |
| 10204 |
// (In Parsemark this happens 81.4% of the time; in code with large |
| 10205 |
// numeric arrays, such as some Kraken benchmarks, it happens more often.) |
| 10206 |
// |
| 10207 |
// In such cases, we can avoid the full expression parsing route through |
| 10208 |
// assignExpr(), condExpr(), orExpr(), unaryExpr(), memberExpr(), and |
| 10209 |
// primaryExpr(). |
| 10210 |
|
| 10211 |
TokenKind firstToken; |
| 10212 |
if (!tokenStream.getToken(&firstToken, TokenStream::SlashIsRegExp)) { |
| 10213 |
return errorResult(); |
| 10214 |
} |
| 10215 |
|
| 10216 |
TokenPos exprPos = pos(); |
| 10217 |
|
| 10218 |
bool endsExpr; |
| 10219 |
|
| 10220 |
// This only handles identifiers that *never* have special meaning anywhere |
| 10221 |
// in the language. Contextual keywords, reserved words in strict mode, |
| 10222 |
// and other hard cases are handled outside this fast path. |
| 10223 |
if (firstToken == TokenKind::Name) { |
| 10224 |
if (!tokenStream.nextTokenEndsExpr(&endsExpr)) { |
| 10225 |
return errorResult(); |
| 10226 |
} |
| 10227 |
if (endsExpr) { |
| 10228 |
TaggedParserAtomIndex name = identifierReference(yieldHandling); |
| 10229 |
if (!name) { |
| 10230 |
return errorResult(); |
| 10231 |
} |
| 10232 |
|
| 10233 |
return identifierReference(name); |
| 10234 |
} |
| 10235 |
} |
| 10236 |
|
| 10237 |
if (firstToken == TokenKind::Number) { |
| 10238 |
if (!tokenStream.nextTokenEndsExpr(&endsExpr)) { |
| 10239 |
return errorResult(); |
| 10240 |
} |
| 10241 |
if (endsExpr) { |
| 10242 |
return newNumber(anyChars.currentToken()); |
| 10243 |
} |
| 10244 |
} |
| 10245 |
|
| 10246 |
if (firstToken == TokenKind::String) { |
| 10247 |
if (!tokenStream.nextTokenEndsExpr(&endsExpr)) { |
| 10248 |
return errorResult(); |
| 10249 |
} |
| 10250 |
if (endsExpr) { |
| 10251 |
return stringLiteral(); |
| 10252 |
} |
| 10253 |
} |
| 10254 |
|
| 10255 |
if (firstToken == TokenKind::Yield && yieldExpressionsSupported()) { |
| 10256 |
return yieldExpression(inHandling); |
| 10257 |
} |
| 10258 |
|
| 10259 |
bool maybeAsyncArrow = false; |
| 10260 |
if (firstToken == TokenKind::Async) { |
| 10261 |
TokenKind nextSameLine = TokenKind::Eof; |
| 10262 |
if (!tokenStream.peekTokenSameLine(&nextSameLine)) { |
| 10263 |
return errorResult(); |
| 10264 |
} |
| 10265 |
|
| 10266 |
if (TokenKindIsPossibleIdentifier(nextSameLine)) { |
| 10267 |
maybeAsyncArrow = true; |
| 10268 |
} |
| 10269 |
} |
| 10270 |
|
| 10271 |
anyChars.ungetToken(); |
| 10272 |
|
| 10273 |
// Save the tokenizer state in case we find an arrow function and have to |
| 10274 |
// rewind. |
| 10275 |
Position start(tokenStream); |
| 10276 |
auto ghostToken = this->compilationState_.getPosition(); |
| 10277 |
|
| 10278 |
PossibleError possibleErrorInner(*this); |
| 10279 |
Node lhs; |
| 10280 |
TokenKind tokenAfterLHS; |
| 10281 |
bool isArrow; |
| 10282 |
if (maybeAsyncArrow) { |
| 10283 |
tokenStream.consumeKnownToken(TokenKind::Async, TokenStream::SlashIsRegExp); |
| 10284 |
|
| 10285 |
TokenKind tokenAfterAsync; |
| 10286 |
if (!tokenStream.getToken(&tokenAfterAsync)) { |
| 10287 |
return errorResult(); |
| 10288 |
} |
| 10289 |
MOZ_ASSERT(TokenKindIsPossibleIdentifier(tokenAfterAsync)); |
| 10290 |
|
| 10291 |
// Check yield validity here. |
| 10292 |
TaggedParserAtomIndex name = bindingIdentifier(yieldHandling); |
| 10293 |
if (!name) { |
| 10294 |
return errorResult(); |
| 10295 |
} |
| 10296 |
|
| 10297 |
if (!tokenStream.peekToken(&tokenAfterLHS, TokenStream::SlashIsRegExp)) { |
| 10298 |
return errorResult(); |
| 10299 |
} |
| 10300 |
|
| 10301 |
isArrow = tokenAfterLHS == TokenKind::Arrow; |
| 10302 |
|
| 10303 |
// |async [no LineTerminator] of| without being followed by => is only |
| 10304 |
// possible in for-await-of loops, e.g. |for await (async of [])|. Pretend |
| 10305 |
// the |async| token was parsed an identifier reference and then proceed |
| 10306 |
// with the rest of this function. |
| 10307 |
if (!isArrow) { |
| 10308 |
anyChars.ungetToken(); // unget the binding identifier |
| 10309 |
|
| 10310 |
// The next token is guaranteed to never be a Div (, because it's an |
| 10311 |
// identifier), so it's okay to re-get the token with SlashIsRegExp. |
| 10312 |
anyChars.allowGettingNextTokenWithSlashIsRegExp(); |
| 10313 |
|
| 10314 |
TaggedParserAtomIndex asyncName = identifierReference(yieldHandling); |
| 10315 |
if (!asyncName) { |
| 10316 |
return errorResult(); |
| 10317 |
} |
| 10318 |
|
| 10319 |
MOZ_TRY_VAR(lhs, identifierReference(asyncName)); |
| 10320 |
} |
| 10321 |
} else { |
| 10322 |
MOZ_TRY_VAR(lhs, condExpr(inHandling, yieldHandling, tripledotHandling, |
| 10323 |
&possibleErrorInner, invoked)); |
| 10324 |
|
| 10325 |
// Use SlashIsRegExp here because the ConditionalExpression parsed above |
| 10326 |
// could be the entirety of this AssignmentExpression, and then ASI |
| 10327 |
// permits this token to be a regular expression. |
| 10328 |
if (!tokenStream.peekToken(&tokenAfterLHS, TokenStream::SlashIsRegExp)) { |
| 10329 |
return errorResult(); |
| 10330 |
} |
| 10331 |
|
| 10332 |
isArrow = tokenAfterLHS == TokenKind::Arrow; |
| 10333 |
} |
| 10334 |
|
| 10335 |
if (isArrow) { |
| 10336 |
// Rewind to reparse as an arrow function. |
| 10337 |
// |
| 10338 |
// Note: We do not call CompilationState::rewind here because parsing |
| 10339 |
// during delazification will see the same rewind and need the same sequence |
| 10340 |
// of inner functions to skip over. |
| 10341 |
// Instead, we mark inner functions as "ghost". |
| 10342 |
// |
| 10343 |
// See GHOST_FUNCTION in FunctionFlags.h for more details. |
| 10344 |
tokenStream.rewind(start); |
| 10345 |
this->compilationState_.markGhost(ghostToken); |
| 10346 |
|
| 10347 |
TokenKind next; |
| 10348 |
if (!tokenStream.getToken(&next, TokenStream::SlashIsRegExp)) { |
| 10349 |
return errorResult(); |
| 10350 |
} |
| 10351 |
TokenPos startPos = pos(); |
| 10352 |
uint32_t toStringStart = startPos.begin; |
| 10353 |
anyChars.ungetToken(); |
| 10354 |
|
| 10355 |
FunctionAsyncKind asyncKind = FunctionAsyncKind::SyncFunction; |
| 10356 |
|
| 10357 |
if (next == TokenKind::Async) { |
| 10358 |
tokenStream.consumeKnownToken(next, TokenStream::SlashIsRegExp); |
| 10359 |
|
| 10360 |
TokenKind nextSameLine = TokenKind::Eof; |
| 10361 |
if (!tokenStream.peekTokenSameLine(&nextSameLine)) { |
| 10362 |
return errorResult(); |
| 10363 |
} |
| 10364 |
|
| 10365 |
// The AsyncArrowFunction production are |
| 10366 |
// async [no LineTerminator here] AsyncArrowBindingIdentifier ... |
| 10367 |
// async [no LineTerminator here] ArrowFormalParameters ... |
| 10368 |
if (TokenKindIsPossibleIdentifier(nextSameLine) || |
| 10369 |
nextSameLine == TokenKind::LeftParen) { |
| 10370 |
asyncKind = FunctionAsyncKind::AsyncFunction; |
| 10371 |
} else { |
| 10372 |
anyChars.ungetToken(); |
| 10373 |
} |
| 10374 |
} |
| 10375 |
|
| 10376 |
FunctionSyntaxKind syntaxKind = FunctionSyntaxKind::Arrow; |
| 10377 |
FunctionNodeType funNode; |
| 10378 |
MOZ_TRY_VAR(funNode, handler_.newFunction(syntaxKind, startPos)); |
| 10379 |
|
| 10380 |
return functionDefinition(funNode, toStringStart, inHandling, yieldHandling, |
| 10381 |
TaggedParserAtomIndex::null(), syntaxKind, |
| 10382 |
GeneratorKind::NotGenerator, asyncKind); |
| 10383 |
} |
| 10384 |
|
| 10385 |
MOZ_ALWAYS_TRUE( |
| 10386 |
tokenStream.getToken(&tokenAfterLHS, TokenStream::SlashIsRegExp)); |
| 10387 |
|
| 10388 |
ParseNodeKind kind; |
| 10389 |
switch (tokenAfterLHS) { |
| 10390 |
case TokenKind::Assign: |
| 10391 |
kind = ParseNodeKind::AssignExpr; |
| 10392 |
break; |
| 10393 |
case TokenKind::AddAssign: |
| 10394 |
kind = ParseNodeKind::AddAssignExpr; |
| 10395 |
break; |
| 10396 |
case TokenKind::SubAssign: |
| 10397 |
kind = ParseNodeKind::SubAssignExpr; |
| 10398 |
break; |
| 10399 |
case TokenKind::CoalesceAssign: |
| 10400 |
kind = ParseNodeKind::CoalesceAssignExpr; |
| 10401 |
break; |
| 10402 |
case TokenKind::OrAssign: |
| 10403 |
kind = ParseNodeKind::OrAssignExpr; |
| 10404 |
break; |
| 10405 |
case TokenKind::AndAssign: |
| 10406 |
kind = ParseNodeKind::AndAssignExpr; |
| 10407 |
break; |
| 10408 |
case TokenKind::BitOrAssign: |
| 10409 |
kind = ParseNodeKind::BitOrAssignExpr; |
| 10410 |
break; |
| 10411 |
case TokenKind::BitXorAssign: |
| 10412 |
kind = ParseNodeKind::BitXorAssignExpr; |
| 10413 |
break; |
| 10414 |
case TokenKind::BitAndAssign: |
| 10415 |
kind = ParseNodeKind::BitAndAssignExpr; |
| 10416 |
break; |
| 10417 |
case TokenKind::LshAssign: |
| 10418 |
kind = ParseNodeKind::LshAssignExpr; |
| 10419 |
break; |
| 10420 |
case TokenKind::RshAssign: |
| 10421 |
kind = ParseNodeKind::RshAssignExpr; |
| 10422 |
break; |
| 10423 |
case TokenKind::UrshAssign: |
| 10424 |
kind = ParseNodeKind::UrshAssignExpr; |
| 10425 |
break; |
| 10426 |
case TokenKind::MulAssign: |
| 10427 |
kind = ParseNodeKind::MulAssignExpr; |
| 10428 |
break; |
| 10429 |
case TokenKind::DivAssign: |
| 10430 |
kind = ParseNodeKind::DivAssignExpr; |
| 10431 |
break; |
| 10432 |
case TokenKind::ModAssign: |
| 10433 |
kind = ParseNodeKind::ModAssignExpr; |
| 10434 |
break; |
| 10435 |
case TokenKind::PowAssign: |
| 10436 |
kind = ParseNodeKind::PowAssignExpr; |
| 10437 |
break; |
| 10438 |
|
| 10439 |
default: |
| 10440 |
MOZ_ASSERT(!anyChars.isCurrentTokenAssignment()); |
| 10441 |
if (!possibleError) { |
| 10442 |
if (!possibleErrorInner.checkForExpressionError()) { |
| 10443 |
return errorResult(); |
| 10444 |
} |
| 10445 |
} else { |
| 10446 |
possibleErrorInner.transferErrorsTo(possibleError); |
| 10447 |
} |
| 10448 |
|
| 10449 |
anyChars.ungetToken(); |
| 10450 |
return lhs; |
| 10451 |
} |
| 10452 |
|
| 10453 |
// Verify the left-hand side expression doesn't have a forbidden form. |
| 10454 |
if (handler_.isUnparenthesizedDestructuringPattern(lhs)) { |
| 10455 |
if (kind != ParseNodeKind::AssignExpr) { |
| 10456 |
error(JSMSG_BAD_DESTRUCT_ASS); |
| 10457 |
return errorResult(); |
| 10458 |
} |
| 10459 |
|
| 10460 |
if (!possibleErrorInner.checkForDestructuringErrorOrWarning()) { |
| 10461 |
return errorResult(); |
| 10462 |
} |
| 10463 |
} else if (handler_.isName(lhs)) { |
| 10464 |
if (const char* chars = nameIsArgumentsOrEval(lhs)) { |
| 10465 |
// |chars| is "arguments" or "eval" here. |
| 10466 |
if (!strictModeErrorAt(exprPos.begin, JSMSG_BAD_STRICT_ASSIGN, chars)) { |
| 10467 |
return errorResult(); |
| 10468 |
} |
| 10469 |
} |
| 10470 |
} else if (handler_.isArgumentsLength(lhs)) { |
| 10471 |
pc_->sc()->setIneligibleForArgumentsLength(); |
| 10472 |
} else if (handler_.isPropertyOrPrivateMemberAccess(lhs)) { |
| 10473 |
// Permitted: no additional testing/fixup needed. |
| 10474 |
} else if (handler_.isFunctionCall(lhs)) { |
| 10475 |
// We don't have to worry about backward compatibility issues with the new |
| 10476 |
// compound assignment operators, so we always throw here. Also that way we |
| 10477 |
// don't have to worry if |f() &&= expr| should always throw an error or |
| 10478 |
// only if |f()| returns true. |
| 10479 |
if (kind == ParseNodeKind::CoalesceAssignExpr || |
| 10480 |
kind == ParseNodeKind::OrAssignExpr || |
| 10481 |
kind == ParseNodeKind::AndAssignExpr) { |
| 10482 |
errorAt(exprPos.begin, JSMSG_BAD_LEFTSIDE_OF_ASS); |
| 10483 |
return errorResult(); |
| 10484 |
} |
| 10485 |
|
| 10486 |
if (!strictModeErrorAt(exprPos.begin, JSMSG_BAD_LEFTSIDE_OF_ASS)) { |
| 10487 |
return errorResult(); |
| 10488 |
} |
| 10489 |
|
| 10490 |
if (possibleError) { |
| 10491 |
possibleError->setPendingDestructuringErrorAt(exprPos, |
| 10492 |
JSMSG_BAD_DESTRUCT_TARGET); |
| 10493 |
} |
| 10494 |
} else { |
| 10495 |
errorAt(exprPos.begin, JSMSG_BAD_LEFTSIDE_OF_ASS); |
| 10496 |
return errorResult(); |
| 10497 |
} |
| 10498 |
|
| 10499 |
if (!possibleErrorInner.checkForExpressionError()) { |
| 10500 |
return errorResult(); |
| 10501 |
} |
| 10502 |
|
| 10503 |
Node rhs; |
| 10504 |
MOZ_TRY_VAR(rhs, assignExpr(inHandling, yieldHandling, TripledotProhibited)); |
| 10505 |
|
| 10506 |
return handler_.newAssignment(kind, lhs, rhs); |
| 10507 |
} |
| 10508 |
|
| 10509 |
template <class ParseHandler> |
| 10510 |
const char* PerHandlerParser<ParseHandler>::nameIsArgumentsOrEval(Node node) { |
| 10511 |
MOZ_ASSERT(handler_.isName(node), |
| 10512 |
"must only call this function on known names"); |
| 10513 |
|
| 10514 |
if (handler_.isEvalName(node)) { |
| 10515 |
return "eval"; |
| 10516 |
} |
| 10517 |
if (handler_.isArgumentsName(node)) { |
| 10518 |
return "arguments"; |
| 10519 |
} |
| 10520 |
return nullptr; |
| 10521 |
} |
| 10522 |
|
| 10523 |
template <class ParseHandler, typename Unit> |
| 10524 |
bool GeneralParser<ParseHandler, Unit>::checkIncDecOperand( |
| 10525 |
Node operand, uint32_t operandOffset) { |
| 10526 |
if (handler_.isName(operand)) { |
| 10527 |
if (const char* chars = nameIsArgumentsOrEval(operand)) { |
| 10528 |
if (!strictModeErrorAt(operandOffset, JSMSG_BAD_STRICT_ASSIGN, chars)) { |
| 10529 |
return false; |
| 10530 |
} |
| 10531 |
} |
| 10532 |
} else if (handler_.isArgumentsLength(operand)) { |
| 10533 |
pc_->sc()->setIneligibleForArgumentsLength(); |
| 10534 |
} else if (handler_.isPropertyOrPrivateMemberAccess(operand)) { |
| 10535 |
// Permitted: no additional testing/fixup needed. |
| 10536 |
} else if (handler_.isFunctionCall(operand)) { |
| 10537 |
// Assignment to function calls is forbidden in ES6. We're still |
| 10538 |
// somewhat concerned about sites using this in dead code, so forbid it |
| 10539 |
// only in strict mode code. |
| 10540 |
if (!strictModeErrorAt(operandOffset, JSMSG_BAD_INCOP_OPERAND)) { |
| 10541 |
return false; |
| 10542 |
} |
| 10543 |
} else { |
| 10544 |
errorAt(operandOffset, JSMSG_BAD_INCOP_OPERAND); |
| 10545 |
return false; |
| 10546 |
} |
| 10547 |
return true; |
| 10548 |
} |
| 10549 |
|
| 10550 |
template <class ParseHandler, typename Unit> |
| 10551 |
typename ParseHandler::UnaryNodeResult |
| 10552 |
GeneralParser<ParseHandler, Unit>::unaryOpExpr(YieldHandling yieldHandling, |
| 10553 |
ParseNodeKind kind, |
| 10554 |
uint32_t begin) { |
| 10555 |
Node kid; |
| 10556 |
MOZ_TRY_VAR(kid, unaryExpr(yieldHandling, TripledotProhibited)); |
| 10557 |
return handler_.newUnary(kind, begin, kid); |
| 10558 |
} |
| 10559 |
|
| 10560 |
template <class ParseHandler, typename Unit> |
| 10561 |
typename ParseHandler::NodeResult |
| 10562 |
GeneralParser<ParseHandler, Unit>::optionalExpr( |
| 10563 |
YieldHandling yieldHandling, TripledotHandling tripledotHandling, |
| 10564 |
TokenKind tt, PossibleError* possibleError /* = nullptr */, |
| 10565 |
InvokedPrediction invoked /* = PredictUninvoked */) { |
| 10566 |
AutoCheckRecursionLimit recursion(this->fc_); |
| 10567 |
if (!recursion.check(this->fc_)) { |
| 10568 |
return errorResult(); |
| 10569 |
} |
| 10570 |
|
| 10571 |
uint32_t begin = pos().begin; |
| 10572 |
|
| 10573 |
Node lhs; |
| 10574 |
MOZ_TRY_VAR(lhs, |
| 10575 |
memberExpr(yieldHandling, tripledotHandling, tt, |
| 10576 |
/* allowCallSyntax = */ true, possibleError, invoked)); |
| 10577 |
|
| 10578 |
if (!tokenStream.peekToken(&tt, TokenStream::SlashIsDiv)) { |
| 10579 |
return errorResult(); |
| 10580 |
} |
| 10581 |
|
| 10582 |
if (tt != TokenKind::OptionalChain) { |
| 10583 |
return lhs; |
| 10584 |
} |
| 10585 |
|
| 10586 |
while (true) { |
| 10587 |
if (!tokenStream.getToken(&tt)) { |
| 10588 |
return errorResult(); |
| 10589 |
} |
| 10590 |
|
| 10591 |
if (tt == TokenKind::Eof) { |
| 10592 |
anyChars.ungetToken(); |
| 10593 |
break; |
| 10594 |
} |
| 10595 |
|
| 10596 |
Node nextMember; |
| 10597 |
if (tt == TokenKind::OptionalChain) { |
| 10598 |
if (!tokenStream.getToken(&tt)) { |
| 10599 |
return errorResult(); |
| 10600 |
} |
| 10601 |
if (TokenKindIsPossibleIdentifierName(tt)) { |
| 10602 |
MOZ_TRY_VAR(nextMember, |
| 10603 |
memberPropertyAccess(lhs, OptionalKind::Optional)); |
| 10604 |
} else if (tt == TokenKind::PrivateName) { |
| 10605 |
MOZ_TRY_VAR(nextMember, |
| 10606 |
memberPrivateAccess(lhs, OptionalKind::Optional)); |
| 10607 |
} else if (tt == TokenKind::LeftBracket) { |
| 10608 |
MOZ_TRY_VAR(nextMember, memberElemAccess(lhs, yieldHandling, |
| 10609 |
OptionalKind::Optional)); |
| 10610 |
} else if (tt == TokenKind::LeftParen) { |
| 10611 |
MOZ_TRY_VAR(nextMember, |
| 10612 |
memberCall(tt, lhs, yieldHandling, possibleError, |
| 10613 |
OptionalKind::Optional)); |
| 10614 |
} else { |
| 10615 |
error(JSMSG_NAME_AFTER_DOT); |
| 10616 |
return errorResult(); |
| 10617 |
} |
| 10618 |
} else if (tt == TokenKind::Dot) { |
| 10619 |
if (!tokenStream.getToken(&tt)) { |
| 10620 |
return errorResult(); |
| 10621 |
} |
| 10622 |
if (TokenKindIsPossibleIdentifierName(tt)) { |
| 10623 |
MOZ_TRY_VAR(nextMember, memberPropertyAccess(lhs)); |
| 10624 |
} else if (tt == TokenKind::PrivateName) { |
| 10625 |
MOZ_TRY_VAR(nextMember, memberPrivateAccess(lhs)); |
| 10626 |
} else { |
| 10627 |
error(JSMSG_NAME_AFTER_DOT); |
| 10628 |
return errorResult(); |
| 10629 |
} |
| 10630 |
} else if (tt == TokenKind::LeftBracket) { |
| 10631 |
MOZ_TRY_VAR(nextMember, memberElemAccess(lhs, yieldHandling)); |
| 10632 |
} else if (tt == TokenKind::LeftParen) { |
| 10633 |
MOZ_TRY_VAR(nextMember, |
| 10634 |
memberCall(tt, lhs, yieldHandling, possibleError)); |
| 10635 |
} else if (tt == TokenKind::TemplateHead || |
| 10636 |
tt == TokenKind::NoSubsTemplate) { |
| 10637 |
error(JSMSG_BAD_OPTIONAL_TEMPLATE); |
| 10638 |
return errorResult(); |
| 10639 |
} else { |
| 10640 |
anyChars.ungetToken(); |
| 10641 |
break; |
| 10642 |
} |
| 10643 |
|
| 10644 |
MOZ_ASSERT(nextMember); |
| 10645 |
lhs = nextMember; |
| 10646 |
} |
| 10647 |
|
| 10648 |
return handler_.newOptionalChain(begin, lhs); |
| 10649 |
} |
| 10650 |
|
| 10651 |
template <class ParseHandler, typename Unit> |
| 10652 |
typename ParseHandler::NodeResult GeneralParser<ParseHandler, Unit>::unaryExpr( |
| 10653 |
YieldHandling yieldHandling, TripledotHandling tripledotHandling, |
| 10654 |
PossibleError* possibleError /* = nullptr */, |
| 10655 |
InvokedPrediction invoked /* = PredictUninvoked */, |
| 10656 |
PrivateNameHandling privateNameHandling /* = PrivateNameProhibited */) { |
| 10657 |
AutoCheckRecursionLimit recursion(this->fc_); |
| 10658 |
if (!recursion.check(this->fc_)) { |
| 10659 |
return errorResult(); |
| 10660 |
} |
| 10661 |
|
| 10662 |
TokenKind tt; |
| 10663 |
if (!tokenStream.getToken(&tt, TokenStream::SlashIsRegExp)) { |
| 10664 |
return errorResult(); |
| 10665 |
} |
| 10666 |
uint32_t begin = pos().begin; |
| 10667 |
switch (tt) { |
| 10668 |
case TokenKind::Void: |
| 10669 |
return unaryOpExpr(yieldHandling, ParseNodeKind::VoidExpr, begin); |
| 10670 |
case TokenKind::Not: |
| 10671 |
return unaryOpExpr(yieldHandling, ParseNodeKind::NotExpr, begin); |
| 10672 |
case TokenKind::BitNot: |
| 10673 |
return unaryOpExpr(yieldHandling, ParseNodeKind::BitNotExpr, begin); |
| 10674 |
case TokenKind::Add: |
| 10675 |
return unaryOpExpr(yieldHandling, ParseNodeKind::PosExpr, begin); |
| 10676 |
case TokenKind::Sub: |
| 10677 |
return unaryOpExpr(yieldHandling, ParseNodeKind::NegExpr, begin); |
| 10678 |
|
| 10679 |
case TokenKind::TypeOf: { |
| 10680 |
// The |typeof| operator is specially parsed to distinguish its |
| 10681 |
// application to a name, from its application to a non-name |
| 10682 |
// expression: |
| 10683 |
// |
| 10684 |
// // Looks up the name, doesn't find it and so evaluates to |
| 10685 |
// // "undefined". |
| 10686 |
// assertEq(typeof nonExistentName, "undefined"); |
| 10687 |
// |
| 10688 |
// // Evaluates expression, triggering a runtime ReferenceError for |
| 10689 |
// // the undefined name. |
| 10690 |
// typeof (1, nonExistentName); |
| 10691 |
Node kid; |
| 10692 |
MOZ_TRY_VAR(kid, unaryExpr(yieldHandling, TripledotProhibited)); |
| 10693 |
|
| 10694 |
return handler_.newTypeof(begin, kid); |
| 10695 |
} |
| 10696 |
|
| 10697 |
case TokenKind::Inc: |
| 10698 |
case TokenKind::Dec: { |
| 10699 |
TokenKind tt2; |
| 10700 |
if (!tokenStream.getToken(&tt2, TokenStream::SlashIsRegExp)) { |
| 10701 |
return errorResult(); |
| 10702 |
} |
| 10703 |
|
| 10704 |
uint32_t operandOffset = pos().begin; |
| 10705 |
Node operand; |
| 10706 |
MOZ_TRY_VAR(operand, |
| 10707 |
optionalExpr(yieldHandling, TripledotProhibited, tt2)); |
| 10708 |
if (!checkIncDecOperand(operand, operandOffset)) { |
| 10709 |
return errorResult(); |
| 10710 |
} |
| 10711 |
ParseNodeKind pnk = (tt == TokenKind::Inc) |
| 10712 |
? ParseNodeKind::PreIncrementExpr |
| 10713 |
: ParseNodeKind::PreDecrementExpr; |
| 10714 |
return handler_.newUpdate(pnk, begin, operand); |
| 10715 |
} |
| 10716 |
case TokenKind::PrivateName: { |
| 10717 |
if (privateNameHandling == PrivateNameHandling::PrivateNameAllowed) { |
| 10718 |
TaggedParserAtomIndex field = anyChars.currentName(); |
| 10719 |
return privateNameReference(field); |
| 10720 |
} |
| 10721 |
error(JSMSG_INVALID_PRIVATE_NAME_IN_UNARY_EXPR); |
| 10722 |
return errorResult(); |
| 10723 |
} |
| 10724 |
|
| 10725 |
case TokenKind::Delete: { |
| 10726 |
uint32_t exprOffset; |
| 10727 |
if (!tokenStream.peekOffset(&exprOffset, TokenStream::SlashIsRegExp)) { |
| 10728 |
return errorResult(); |
| 10729 |
} |
| 10730 |
|
| 10731 |
Node expr; |
| 10732 |
MOZ_TRY_VAR(expr, unaryExpr(yieldHandling, TripledotProhibited)); |
| 10733 |
|
| 10734 |
// Per spec, deleting most unary expressions is valid -- it simply |
| 10735 |
// returns true -- except for two cases: |
| 10736 |
// 1. `var x; ...; delete x` is a syntax error in strict mode. |
| 10737 |
// 2. Private fields cannot be deleted. |
| 10738 |
if (handler_.isName(expr)) { |
| 10739 |
if (!strictModeErrorAt(exprOffset, JSMSG_DEPRECATED_DELETE_OPERAND)) { |
| 10740 |
return errorResult(); |
| 10741 |
} |
| 10742 |
|
| 10743 |
pc_->sc()->setBindingsAccessedDynamically(); |
| 10744 |
} |
| 10745 |
|
| 10746 |
if (handler_.isPrivateMemberAccess(expr)) { |
| 10747 |
errorAt(exprOffset, JSMSG_PRIVATE_DELETE); |
| 10748 |
return errorResult(); |
| 10749 |
} |
| 10750 |
|
| 10751 |
return handler_.newDelete(begin, expr); |
| 10752 |
} |
| 10753 |
case TokenKind::Await: { |
| 10754 |
// If we encounter an await in a module, mark it as async. |
| 10755 |
if (!pc_->isAsync() && pc_->sc()->isModule()) { |
| 10756 |
if (!options().topLevelAwait) { |
| 10757 |
error(JSMSG_TOP_LEVEL_AWAIT_NOT_SUPPORTED); |
| 10758 |
return errorResult(); |
| 10759 |
} |
| 10760 |
pc_->sc()->asModuleContext()->setIsAsync(); |
| 10761 |
MOZ_ASSERT(pc_->isAsync()); |
| 10762 |
} |
| 10763 |
|
| 10764 |
if (pc_->isAsync()) { |
| 10765 |
if (inParametersOfAsyncFunction()) { |
| 10766 |
error(JSMSG_AWAIT_IN_PARAMETER); |
| 10767 |
return errorResult(); |
| 10768 |
} |
| 10769 |
Node kid; |
| 10770 |
MOZ_TRY_VAR(kid, unaryExpr(yieldHandling, tripledotHandling, |
| 10771 |
possibleError, invoked)); |
| 10772 |
pc_->lastAwaitOffset = begin; |
| 10773 |
return handler_.newAwaitExpression(begin, kid); |
| 10774 |
} |
| 10775 |
} |
| 10776 |
|
| 10777 |
[[fallthrough]]; |
| 10778 |
|
| 10779 |
default: { |
| 10780 |
Node expr; |
| 10781 |
MOZ_TRY_VAR(expr, optionalExpr(yieldHandling, tripledotHandling, tt, |
| 10782 |
possibleError, invoked)); |
| 10783 |
|
| 10784 |
/* Don't look across a newline boundary for a postfix incop. */ |
| 10785 |
if (!tokenStream.peekTokenSameLine(&tt)) { |
| 10786 |
return errorResult(); |
| 10787 |
} |
| 10788 |
|
| 10789 |
if (tt != TokenKind::Inc && tt != TokenKind::Dec) { |
| 10790 |
return expr; |
| 10791 |
} |
| 10792 |
|
| 10793 |
tokenStream.consumeKnownToken(tt); |
| 10794 |
if (!checkIncDecOperand(expr, begin)) { |
| 10795 |
return errorResult(); |
| 10796 |
} |
| 10797 |
|
| 10798 |
ParseNodeKind pnk = (tt == TokenKind::Inc) |
| 10799 |
? ParseNodeKind::PostIncrementExpr |
| 10800 |
: ParseNodeKind::PostDecrementExpr; |
| 10801 |
return handler_.newUpdate(pnk, begin, expr); |
| 10802 |
} |
| 10803 |
} |
| 10804 |
} |
| 10805 |
|
| 10806 |
template <class ParseHandler, typename Unit> |
| 10807 |
typename ParseHandler::NodeResult |
| 10808 |
GeneralParser<ParseHandler, Unit>::assignExprWithoutYieldOrAwait( |
| 10809 |
YieldHandling yieldHandling) { |
| 10810 |
uint32_t startYieldOffset = pc_->lastYieldOffset; |
| 10811 |
uint32_t startAwaitOffset = pc_->lastAwaitOffset; |
| 10812 |
|
| 10813 |
Node res; |
| 10814 |
MOZ_TRY_VAR(res, assignExpr(InAllowed, yieldHandling, TripledotProhibited)); |
| 10815 |
|
| 10816 |
if (pc_->lastYieldOffset != startYieldOffset) { |
| 10817 |
errorAt(pc_->lastYieldOffset, JSMSG_YIELD_IN_PARAMETER); |
| 10818 |
return errorResult(); |
| 10819 |
} |
| 10820 |
if (pc_->lastAwaitOffset != startAwaitOffset) { |
| 10821 |
errorAt(pc_->lastAwaitOffset, JSMSG_AWAIT_IN_PARAMETER); |
| 10822 |
return errorResult(); |
| 10823 |
} |
| 10824 |
return res; |
| 10825 |
} |
| 10826 |
|
| 10827 |
template <class ParseHandler, typename Unit> |
| 10828 |
typename ParseHandler::ListNodeResult |
| 10829 |
GeneralParser<ParseHandler, Unit>::argumentList( |
| 10830 |
YieldHandling yieldHandling, bool* isSpread, |
| 10831 |
PossibleError* possibleError /* = nullptr */) { |
| 10832 |
ListNodeType argsList; |
| 10833 |
MOZ_TRY_VAR(argsList, handler_.newArguments(pos())); |
| 10834 |
|
| 10835 |
bool matched; |
| 10836 |
if (!tokenStream.matchToken(&matched, TokenKind::RightParen, |
| 10837 |
TokenStream::SlashIsRegExp)) { |
| 10838 |
return errorResult(); |
| 10839 |
} |
| 10840 |
if (matched) { |
| 10841 |
handler_.setEndPosition(argsList, pos().end); |
| 10842 |
return argsList; |
| 10843 |
} |
| 10844 |
|
| 10845 |
while (true) { |
| 10846 |
bool spread = false; |
| 10847 |
uint32_t begin = 0; |
| 10848 |
if (!tokenStream.matchToken(&matched, TokenKind::TripleDot, |
| 10849 |
TokenStream::SlashIsRegExp)) { |
| 10850 |
return errorResult(); |
| 10851 |
} |
| 10852 |
if (matched) { |
| 10853 |
spread = true; |
| 10854 |
begin = pos().begin; |
| 10855 |
*isSpread = true; |
| 10856 |
} |
| 10857 |
|
| 10858 |
Node argNode; |
| 10859 |
MOZ_TRY_VAR(argNode, assignExpr(InAllowed, yieldHandling, |
| 10860 |
TripledotProhibited, possibleError)); |
| 10861 |
if (spread) { |
| 10862 |
MOZ_TRY_VAR(argNode, handler_.newSpread(begin, argNode)); |
| 10863 |
} |
| 10864 |
|
| 10865 |
handler_.addList(argsList, argNode); |
| 10866 |
|
| 10867 |
bool matched; |
| 10868 |
if (!tokenStream.matchToken(&matched, TokenKind::Comma, |
| 10869 |
TokenStream::SlashIsRegExp)) { |
| 10870 |
return errorResult(); |
| 10871 |
} |
| 10872 |
if (!matched) { |
| 10873 |
break; |
| 10874 |
} |
| 10875 |
|
| 10876 |
TokenKind tt; |
| 10877 |
if (!tokenStream.peekToken(&tt, TokenStream::SlashIsRegExp)) { |
| 10878 |
return errorResult(); |
| 10879 |
} |
| 10880 |
if (tt == TokenKind::RightParen) { |
| 10881 |
break; |
| 10882 |
} |
| 10883 |
} |
| 10884 |
|
| 10885 |
if (!mustMatchToken(TokenKind::RightParen, JSMSG_PAREN_AFTER_ARGS)) { |
| 10886 |
return errorResult(); |
| 10887 |
} |
| 10888 |
|
| 10889 |
handler_.setEndPosition(argsList, pos().end); |
| 10890 |
return argsList; |
| 10891 |
} |
| 10892 |
|
| 10893 |
bool ParserBase::checkAndMarkSuperScope() { |
| 10894 |
if (!pc_->sc()->allowSuperProperty()) { |
| 10895 |
return false; |
| 10896 |
} |
| 10897 |
|
| 10898 |
pc_->setSuperScopeNeedsHomeObject(); |
| 10899 |
return true; |
| 10900 |
} |
| 10901 |
|
| 10902 |
template <class ParseHandler, typename Unit> |
| 10903 |
bool GeneralParser<ParseHandler, Unit>::computeErrorMetadata( |
| 10904 |
ErrorMetadata* err, const ErrorReportMixin::ErrorOffset& offset) const { |
| 10905 |
if (offset.is<ErrorReportMixin::Current>()) { |
| 10906 |
return tokenStream.computeErrorMetadata(err, AsVariant(pos().begin)); |
| 10907 |
} |
| 10908 |
return tokenStream.computeErrorMetadata(err, offset); |
| 10909 |
} |
| 10910 |
|
| 10911 |
template <class ParseHandler, typename Unit> |
| 10912 |
typename ParseHandler::NodeResult GeneralParser<ParseHandler, Unit>::memberExpr( |
| 10913 |
YieldHandling yieldHandling, TripledotHandling tripledotHandling, |
| 10914 |
TokenKind tt, bool allowCallSyntax, PossibleError* possibleError, |
| 10915 |
InvokedPrediction invoked) { |
| 10916 |
MOZ_ASSERT(anyChars.isCurrentTokenType(tt)); |
| 10917 |
|
| 10918 |
Node lhs; |
| 10919 |
|
| 10920 |
AutoCheckRecursionLimit recursion(this->fc_); |
| 10921 |
if (!recursion.check(this->fc_)) { |
| 10922 |
return errorResult(); |
| 10923 |
} |
| 10924 |
|
| 10925 |
/* Check for new expression first. */ |
| 10926 |
if (tt == TokenKind::New) { |
| 10927 |
uint32_t newBegin = pos().begin; |
| 10928 |
// Make sure this wasn't a |new.target| in disguise. |
| 10929 |
NewTargetNodeType newTarget; |
| 10930 |
if (!tryNewTarget(&newTarget)) { |
| 10931 |
return errorResult(); |
| 10932 |
} |
| 10933 |
if (newTarget) { |
| 10934 |
lhs = newTarget; |
| 10935 |
} else { |
| 10936 |
// Gotten by tryNewTarget |
| 10937 |
tt = anyChars.currentToken().type; |
| 10938 |
Node ctorExpr; |
| 10939 |
MOZ_TRY_VAR(ctorExpr, |
| 10940 |
memberExpr(yieldHandling, TripledotProhibited, tt, |
| 10941 |
/* allowCallSyntax = */ false, |
| 10942 |
/* possibleError = */ nullptr, PredictInvoked)); |
| 10943 |
|
| 10944 |
// If we have encountered an optional chain, in the form of `new |
| 10945 |
// ClassName?.()` then we need to throw, as this is disallowed by the |
| 10946 |
// spec. |
| 10947 |
bool optionalToken; |
| 10948 |
if (!tokenStream.matchToken(&optionalToken, TokenKind::OptionalChain)) { |
| 10949 |
return errorResult(); |
| 10950 |
} |
| 10951 |
if (optionalToken) { |
| 10952 |
errorAt(newBegin, JSMSG_BAD_NEW_OPTIONAL); |
| 10953 |
return errorResult(); |
| 10954 |
} |
| 10955 |
|
| 10956 |
bool matched; |
| 10957 |
if (!tokenStream.matchToken(&matched, TokenKind::LeftParen)) { |
| 10958 |
return errorResult(); |
| 10959 |
} |
| 10960 |
|
| 10961 |
bool isSpread = false; |
| 10962 |
ListNodeType args; |
| 10963 |
if (matched) { |
| 10964 |
MOZ_TRY_VAR(args, argumentList(yieldHandling, &isSpread)); |
| 10965 |
} else { |
| 10966 |
MOZ_TRY_VAR(args, handler_.newArguments(pos())); |
| 10967 |
} |
| 10968 |
|
| 10969 |
if (!args) { |
| 10970 |
return errorResult(); |
| 10971 |
} |
| 10972 |
|
| 10973 |
MOZ_TRY_VAR( |
| 10974 |
lhs, handler_.newNewExpression(newBegin, ctorExpr, args, isSpread)); |
| 10975 |
} |
| 10976 |
} else if (tt == TokenKind::Super) { |
| 10977 |
NameNodeType thisName; |
| 10978 |
MOZ_TRY_VAR(thisName, newThisName()); |
| 10979 |
MOZ_TRY_VAR(lhs, handler_.newSuperBase(thisName, pos())); |
| 10980 |
} else if (tt == TokenKind::Import) { |
| 10981 |
MOZ_TRY_VAR(lhs, importExpr(yieldHandling, allowCallSyntax)); |
| 10982 |
} else { |
| 10983 |
MOZ_TRY_VAR(lhs, primaryExpr(yieldHandling, tripledotHandling, tt, |
| 10984 |
possibleError, invoked)); |
| 10985 |
} |
| 10986 |
|
| 10987 |
MOZ_ASSERT_IF(handler_.isSuperBase(lhs), |
| 10988 |
anyChars.isCurrentTokenType(TokenKind::Super)); |
| 10989 |
|
| 10990 |
while (true) { |
| 10991 |
if (!tokenStream.getToken(&tt)) { |
| 10992 |
return errorResult(); |
| 10993 |
} |
| 10994 |
if (tt == TokenKind::Eof) { |
| 10995 |
anyChars.ungetToken(); |
| 10996 |
break; |
| 10997 |
} |
| 10998 |
|
| 10999 |
Node nextMember; |
| 11000 |
if (tt == TokenKind::Dot) { |
| 11001 |
if (!tokenStream.getToken(&tt)) { |
| 11002 |
return errorResult(); |
| 11003 |
} |
| 11004 |
|
| 11005 |
if (TokenKindIsPossibleIdentifierName(tt)) { |
| 11006 |
MOZ_TRY_VAR(nextMember, memberPropertyAccess(lhs)); |
| 11007 |
} else if (tt == TokenKind::PrivateName) { |
| 11008 |
MOZ_TRY_VAR(nextMember, memberPrivateAccess(lhs)); |
| 11009 |
} else { |
| 11010 |
error(JSMSG_NAME_AFTER_DOT); |
| 11011 |
return errorResult(); |
| 11012 |
} |
| 11013 |
} else if (tt == TokenKind::LeftBracket) { |
| 11014 |
MOZ_TRY_VAR(nextMember, memberElemAccess(lhs, yieldHandling)); |
| 11015 |
} else if ((allowCallSyntax && tt == TokenKind::LeftParen) || |
| 11016 |
tt == TokenKind::TemplateHead || |
| 11017 |
tt == TokenKind::NoSubsTemplate) { |
| 11018 |
if (handler_.isSuperBase(lhs)) { |
| 11019 |
if (!pc_->sc()->allowSuperCall()) { |
| 11020 |
error(JSMSG_BAD_SUPERCALL); |
| 11021 |
return errorResult(); |
| 11022 |
} |
| 11023 |
|
| 11024 |
if (tt != TokenKind::LeftParen) { |
| 11025 |
error(JSMSG_BAD_SUPER); |
| 11026 |
return errorResult(); |
| 11027 |
} |
| 11028 |
|
| 11029 |
MOZ_TRY_VAR(nextMember, memberSuperCall(lhs, yieldHandling)); |
| 11030 |
|
| 11031 |
if (!noteUsedName( |
| 11032 |
TaggedParserAtomIndex::WellKnown::dot_initializers_())) { |
| 11033 |
return errorResult(); |
| 11034 |
} |
| 11035 |
#ifdef ENABLE_DECORATORS |
| 11036 |
if (!noteUsedName(TaggedParserAtomIndex::WellKnown:: |
| 11037 |
dot_instanceExtraInitializers_())) { |
| 11038 |
return null(); |
| 11039 |
} |
| 11040 |
#endif |
| 11041 |
} else { |
| 11042 |
MOZ_TRY_VAR(nextMember, |
| 11043 |
memberCall(tt, lhs, yieldHandling, possibleError)); |
| 11044 |
} |
| 11045 |
} else { |
| 11046 |
anyChars.ungetToken(); |
| 11047 |
if (handler_.isSuperBase(lhs)) { |
| 11048 |
break; |
| 11049 |
} |
| 11050 |
return lhs; |
| 11051 |
} |
| 11052 |
|
| 11053 |
lhs = nextMember; |
| 11054 |
} |
| 11055 |
|
| 11056 |
if (handler_.isSuperBase(lhs)) { |
| 11057 |
error(JSMSG_BAD_SUPER); |
| 11058 |
return errorResult(); |
| 11059 |
} |
| 11060 |
|
| 11061 |
return lhs; |
| 11062 |
} |
| 11063 |
|
| 11064 |
template <class ParseHandler, typename Unit> |
| 11065 |
typename ParseHandler::NodeResult |
| 11066 |
GeneralParser<ParseHandler, Unit>::decoratorExpr(YieldHandling yieldHandling, |
| 11067 |
TokenKind tt) { |
| 11068 |
MOZ_ASSERT(anyChars.isCurrentTokenType(tt)); |
| 11069 |
|
| 11070 |
AutoCheckRecursionLimit recursion(this->fc_); |
| 11071 |
if (!recursion.check(this->fc_)) { |
| 11072 |
return errorResult(); |
| 11073 |
} |
| 11074 |
|
| 11075 |
if (tt == TokenKind::LeftParen) { |
| 11076 |
// DecoratorParenthesizedExpression |
| 11077 |
Node expr; |
| 11078 |
MOZ_TRY_VAR(expr, exprInParens(InAllowed, yieldHandling, TripledotAllowed, |
| 11079 |
/* possibleError*/ nullptr)); |
| 11080 |
if (!mustMatchToken(TokenKind::RightParen, JSMSG_PAREN_AFTER_DECORATOR)) { |
| 11081 |
return errorResult(); |
| 11082 |
} |
| 11083 |
|
| 11084 |
return handler_.parenthesize(expr); |
| 11085 |
} |
| 11086 |
|
| 11087 |
if (!TokenKindIsPossibleIdentifier(tt)) { |
| 11088 |
error(JSMSG_DECORATOR_NAME_EXPECTED); |
| 11089 |
return errorResult(); |
| 11090 |
} |
| 11091 |
|
| 11092 |
TaggedParserAtomIndex name = identifierReference(yieldHandling); |
| 11093 |
if (!name) { |
| 11094 |
return errorResult(); |
| 11095 |
} |
| 11096 |
|
| 11097 |
Node lhs; |
| 11098 |
MOZ_TRY_VAR(lhs, identifierReference(name)); |
| 11099 |
|
| 11100 |
while (true) { |
| 11101 |
if (!tokenStream.getToken(&tt)) { |
| 11102 |
return errorResult(); |
| 11103 |
} |
| 11104 |
if (tt == TokenKind::Eof) { |
| 11105 |
anyChars.ungetToken(); |
| 11106 |
break; |
| 11107 |
} |
| 11108 |
|
| 11109 |
Node nextMember; |
| 11110 |
if (tt == TokenKind::Dot) { |
| 11111 |
if (!tokenStream.getToken(&tt)) { |
| 11112 |
return errorResult(); |
| 11113 |
} |
| 11114 |
|
| 11115 |
if (TokenKindIsPossibleIdentifierName(tt)) { |
| 11116 |
MOZ_TRY_VAR(nextMember, memberPropertyAccess(lhs)); |
| 11117 |
} else if (tt == TokenKind::PrivateName) { |
| 11118 |
MOZ_TRY_VAR(nextMember, memberPrivateAccess(lhs)); |
| 11119 |
} else { |
| 11120 |
error(JSMSG_NAME_AFTER_DOT); |
| 11121 |
return errorResult(); |
| 11122 |
} |
| 11123 |
} else if (tt == TokenKind::LeftParen) { |
| 11124 |
MOZ_TRY_VAR(nextMember, memberCall(tt, lhs, yieldHandling, |
| 11125 |
/* possibleError */ nullptr)); |
| 11126 |
lhs = nextMember; |
| 11127 |
// This is a `DecoratorCallExpression` and it's defined at the top level |
| 11128 |
// of `Decorator`, no other `DecoratorMemberExpression` is allowed to |
| 11129 |
// follow after the arguments. |
| 11130 |
break; |
| 11131 |
} else { |
| 11132 |
anyChars.ungetToken(); |
| 11133 |
break; |
| 11134 |
} |
| 11135 |
|
| 11136 |
lhs = nextMember; |
| 11137 |
} |
| 11138 |
|
| 11139 |
return lhs; |
| 11140 |
} |
| 11141 |
|
| 11142 |
template <class ParseHandler> |
| 11143 |
inline typename ParseHandler::NameNodeResult |
| 11144 |
PerHandlerParser<ParseHandler>::newName(TaggedParserAtomIndex name) { |
| 11145 |
return newName(name, pos()); |
| 11146 |
} |
| 11147 |
|
| 11148 |
template <class ParseHandler> |
| 11149 |
inline typename ParseHandler::NameNodeResult |
| 11150 |
PerHandlerParser<ParseHandler>::newName(TaggedParserAtomIndex name, |
| 11151 |
TokenPos pos) { |
| 11152 |
if (name == TaggedParserAtomIndex::WellKnown::arguments()) { |
| 11153 |
this->pc_->numberOfArgumentsNames++; |
| 11154 |
} |
| 11155 |
return handler_.newName(name, pos); |
| 11156 |
} |
| 11157 |
|
| 11158 |
template <class ParseHandler> |
| 11159 |
inline typename ParseHandler::NameNodeResult |
| 11160 |
PerHandlerParser<ParseHandler>::newPrivateName(TaggedParserAtomIndex name) { |
| 11161 |
return handler_.newPrivateName(name, pos()); |
| 11162 |
} |
| 11163 |
|
| 11164 |
template <class ParseHandler, typename Unit> |
| 11165 |
typename ParseHandler::NodeResult |
| 11166 |
GeneralParser<ParseHandler, Unit>::memberPropertyAccess( |
| 11167 |
Node lhs, OptionalKind optionalKind /* = OptionalKind::NonOptional */) { |
| 11168 |
MOZ_ASSERT(TokenKindIsPossibleIdentifierName(anyChars.currentToken().type) || |
| 11169 |
anyChars.currentToken().type == TokenKind::PrivateName); |
| 11170 |
TaggedParserAtomIndex field = anyChars.currentName(); |
| 11171 |
if (handler_.isSuperBase(lhs) && !checkAndMarkSuperScope()) { |
| 11172 |
error(JSMSG_BAD_SUPERPROP, "property"); |
| 11173 |
return errorResult(); |
| 11174 |
} |
| 11175 |
|
| 11176 |
NameNodeType name; |
| 11177 |
MOZ_TRY_VAR(name, handler_.newPropertyName(field, pos())); |
| 11178 |
|
| 11179 |
if (optionalKind == OptionalKind::Optional) { |
| 11180 |
MOZ_ASSERT(!handler_.isSuperBase(lhs)); |
| 11181 |
return handler_.newOptionalPropertyAccess(lhs, name); |
| 11182 |
} |
| 11183 |
|
| 11184 |
if (handler_.isArgumentsName(lhs) && handler_.isLengthName(name)) { |
| 11185 |
MOZ_ASSERT(pc_->numberOfArgumentsNames > 0); |
| 11186 |
pc_->numberOfArgumentsNames--; |
| 11187 |
// Currently when resuming Generators don't get their argument length set |
| 11188 |
// in the interpreter frame (see InterpreterStack::resumeGeneratorCallFrame, |
| 11189 |
// and its call to initCallFrame). |
| 11190 |
if (pc_->isGeneratorOrAsync()) { |
| 11191 |
pc_->sc()->setIneligibleForArgumentsLength(); |
| 11192 |
} |
| 11193 |
return handler_.newArgumentsLength(lhs, name); |
| 11194 |
} |
| 11195 |
|
| 11196 |
return handler_.newPropertyAccess(lhs, name); |
| 11197 |
} |
| 11198 |
|
| 11199 |
template <class ParseHandler, typename Unit> |
| 11200 |
typename ParseHandler::NodeResult |
| 11201 |
GeneralParser<ParseHandler, Unit>::memberPrivateAccess( |
| 11202 |
Node lhs, OptionalKind optionalKind /* = OptionalKind::NonOptional */) { |
| 11203 |
MOZ_ASSERT(anyChars.currentToken().type == TokenKind::PrivateName); |
| 11204 |
|
| 11205 |
TaggedParserAtomIndex field = anyChars.currentName(); |
| 11206 |
// Cannot access private fields on super. |
| 11207 |
if (handler_.isSuperBase(lhs)) { |
| 11208 |
error(JSMSG_BAD_SUPERPRIVATE); |
| 11209 |
return errorResult(); |
| 11210 |
} |
| 11211 |
|
| 11212 |
NameNodeType privateName; |
| 11213 |
MOZ_TRY_VAR(privateName, privateNameReference(field)); |
| 11214 |
|
| 11215 |
if (optionalKind == OptionalKind::Optional) { |
| 11216 |
MOZ_ASSERT(!handler_.isSuperBase(lhs)); |
| 11217 |
return handler_.newOptionalPrivateMemberAccess(lhs, privateName, pos().end); |
| 11218 |
} |
| 11219 |
return handler_.newPrivateMemberAccess(lhs, privateName, pos().end); |
| 11220 |
} |
| 11221 |
|
| 11222 |
template <class ParseHandler, typename Unit> |
| 11223 |
typename ParseHandler::NodeResult |
| 11224 |
GeneralParser<ParseHandler, Unit>::memberElemAccess( |
| 11225 |
Node lhs, YieldHandling yieldHandling, |
| 11226 |
OptionalKind optionalKind /* = OptionalKind::NonOptional */) { |
| 11227 |
MOZ_ASSERT(anyChars.currentToken().type == TokenKind::LeftBracket); |
| 11228 |
Node propExpr; |
| 11229 |
MOZ_TRY_VAR(propExpr, expr(InAllowed, yieldHandling, TripledotProhibited)); |
| 11230 |
|
| 11231 |
if (!mustMatchToken(TokenKind::RightBracket, JSMSG_BRACKET_IN_INDEX)) { |
| 11232 |
return errorResult(); |
| 11233 |
} |
| 11234 |
|
| 11235 |
if (handler_.isSuperBase(lhs) && !checkAndMarkSuperScope()) { |
| 11236 |
error(JSMSG_BAD_SUPERPROP, "member"); |
| 11237 |
return errorResult(); |
| 11238 |
} |
| 11239 |
if (optionalKind == OptionalKind::Optional) { |
| 11240 |
MOZ_ASSERT(!handler_.isSuperBase(lhs)); |
| 11241 |
return handler_.newOptionalPropertyByValue(lhs, propExpr, pos().end); |
| 11242 |
} |
| 11243 |
return handler_.newPropertyByValue(lhs, propExpr, pos().end); |
| 11244 |
} |
| 11245 |
|
| 11246 |
template <class ParseHandler, typename Unit> |
| 11247 |
typename ParseHandler::NodeResult |
| 11248 |
GeneralParser<ParseHandler, Unit>::memberSuperCall( |
| 11249 |
Node lhs, YieldHandling yieldHandling) { |
| 11250 |
MOZ_ASSERT(anyChars.currentToken().type == TokenKind::LeftParen); |
| 11251 |
// Despite the fact that it's impossible to have |super()| in a |
| 11252 |
// generator, we still inherit the yieldHandling of the |
| 11253 |
// memberExpression, per spec. Curious. |
| 11254 |
bool isSpread = false; |
| 11255 |
ListNodeType args; |
| 11256 |
MOZ_TRY_VAR(args, argumentList(yieldHandling, &isSpread)); |
| 11257 |
|
| 11258 |
CallNodeType superCall; |
| 11259 |
MOZ_TRY_VAR(superCall, handler_.newSuperCall(lhs, args, isSpread)); |
| 11260 |
|
| 11261 |
// |super()| implicitly reads |new.target|. |
| 11262 |
if (!noteUsedName(TaggedParserAtomIndex::WellKnown::dot_newTarget_())) { |
| 11263 |
return errorResult(); |
| 11264 |
} |
| 11265 |
|
| 11266 |
NameNodeType thisName; |
| 11267 |
MOZ_TRY_VAR(thisName, newThisName()); |
| 11268 |
|
| 11269 |
return handler_.newSetThis(thisName, superCall); |
| 11270 |
} |
| 11271 |
|
| 11272 |
template <class ParseHandler, typename Unit> |
| 11273 |
typename ParseHandler::NodeResult GeneralParser<ParseHandler, Unit>::memberCall( |
| 11274 |
TokenKind tt, Node lhs, YieldHandling yieldHandling, |
| 11275 |
PossibleError* possibleError /* = nullptr */, |
| 11276 |
OptionalKind optionalKind /* = OptionalKind::NonOptional */) { |
| 11277 |
if (options().selfHostingMode && |
| 11278 |
(handler_.isPropertyOrPrivateMemberAccess(lhs) || |
| 11279 |
handler_.isOptionalPropertyOrPrivateMemberAccess(lhs))) { |
| 11280 |
error(JSMSG_SELFHOSTED_METHOD_CALL); |
| 11281 |
return errorResult(); |
| 11282 |
} |
| 11283 |
|
| 11284 |
MOZ_ASSERT(tt == TokenKind::LeftParen || tt == TokenKind::TemplateHead || |
| 11285 |
tt == TokenKind::NoSubsTemplate, |
| 11286 |
"Unexpected token kind for member call"); |
| 11287 |
|
| 11288 |
JSOp op = JSOp::Call; |
| 11289 |
bool maybeAsyncArrow = false; |
| 11290 |
if (tt == TokenKind::LeftParen && optionalKind == OptionalKind::NonOptional) { |
| 11291 |
if (handler_.isAsyncKeyword(lhs)) { |
| 11292 |
// |async (| can be the start of an async arrow |
| 11293 |
// function, so we need to defer reporting possible |
| 11294 |
// errors from destructuring syntax. To give better |
| 11295 |
// error messages, we only allow the AsyncArrowHead |
| 11296 |
// part of the CoverCallExpressionAndAsyncArrowHead |
| 11297 |
// syntax when the initial name is "async". |
| 11298 |
maybeAsyncArrow = true; |
| 11299 |
} else if (handler_.isEvalName(lhs)) { |
| 11300 |
// Select the right Eval op and flag pc_ as having a |
| 11301 |
// direct eval. |
| 11302 |
op = pc_->sc()->strict() ? JSOp::StrictEval : JSOp::Eval; |
| 11303 |
pc_->sc()->setBindingsAccessedDynamically(); |
| 11304 |
pc_->sc()->setHasDirectEval(); |
| 11305 |
|
| 11306 |
// In non-strict mode code, direct calls to eval can |
| 11307 |
// add variables to the call object. |
| 11308 |
if (pc_->isFunctionBox() && !pc_->sc()->strict()) { |
| 11309 |
pc_->functionBox()->setFunHasExtensibleScope(); |
| 11310 |
} |
| 11311 |
|
| 11312 |
// If we're in a method, mark the method as requiring |
| 11313 |
// support for 'super', since direct eval code can use |
| 11314 |
// it. (If we're not in a method, that's fine, so |
| 11315 |
// ignore the return value.) |
| 11316 |
checkAndMarkSuperScope(); |
| 11317 |
} |
| 11318 |
} |
| 11319 |
|
| 11320 |
if (tt == TokenKind::LeftParen) { |
| 11321 |
bool isSpread = false; |
| 11322 |
PossibleError* asyncPossibleError = |
| 11323 |
maybeAsyncArrow ? possibleError : nullptr; |
| 11324 |
ListNodeType args; |
| 11325 |
MOZ_TRY_VAR(args, |
| 11326 |
argumentList(yieldHandling, &isSpread, asyncPossibleError)); |
| 11327 |
if (isSpread) { |
| 11328 |
if (op == JSOp::Eval) { |
| 11329 |
op = JSOp::SpreadEval; |
| 11330 |
} else if (op == JSOp::StrictEval) { |
| 11331 |
op = JSOp::StrictSpreadEval; |
| 11332 |
} else { |
| 11333 |
op = JSOp::SpreadCall; |
| 11334 |
} |
| 11335 |
} |
| 11336 |
|
| 11337 |
if (optionalKind == OptionalKind::Optional) { |
| 11338 |
return handler_.newOptionalCall(lhs, args, op); |
| 11339 |
} |
| 11340 |
return handler_.newCall(lhs, args, op); |
| 11341 |
} |
| 11342 |
|
| 11343 |
ListNodeType args; |
| 11344 |
MOZ_TRY_VAR(args, handler_.newArguments(pos())); |
| 11345 |
|
| 11346 |
if (!taggedTemplate(yieldHandling, args, tt)) { |
| 11347 |
return errorResult(); |
| 11348 |
} |
| 11349 |
|
| 11350 |
if (optionalKind == OptionalKind::Optional) { |
| 11351 |
error(JSMSG_BAD_OPTIONAL_TEMPLATE); |
| 11352 |
return errorResult(); |
| 11353 |
} |
| 11354 |
|
| 11355 |
return handler_.newTaggedTemplate(lhs, args, op); |
| 11356 |
} |
| 11357 |
|
| 11358 |
template <class ParseHandler, typename Unit> |
| 11359 |
bool GeneralParser<ParseHandler, Unit>::checkLabelOrIdentifierReference( |
| 11360 |
TaggedParserAtomIndex ident, uint32_t offset, YieldHandling yieldHandling, |
| 11361 |
TokenKind hint /* = TokenKind::Limit */) { |
| 11362 |
TokenKind tt; |
| 11363 |
if (hint == TokenKind::Limit) { |
| 11364 |
tt = ReservedWordTokenKind(ident); |
| 11365 |
} else { |
| 11366 |
// All non-reserved word kinds are folded into TokenKind::Limit in |
| 11367 |
// ReservedWordTokenKind and the following code. |
| 11368 |
if (hint == TokenKind::Name || hint == TokenKind::PrivateName) { |
| 11369 |
hint = TokenKind::Limit; |
| 11370 |
} |
| 11371 |
MOZ_ASSERT(hint == ReservedWordTokenKind(ident), |
| 11372 |
"hint doesn't match actual token kind"); |
| 11373 |
tt = hint; |
| 11374 |
} |
| 11375 |
|
| 11376 |
if (!pc_->sc()->allowArguments() && |
| 11377 |
ident == TaggedParserAtomIndex::WellKnown::arguments()) { |
| 11378 |
error(JSMSG_BAD_ARGUMENTS); |
| 11379 |
return false; |
| 11380 |
} |
| 11381 |
|
| 11382 |
if (tt == TokenKind::Limit) { |
| 11383 |
// Either TokenKind::Name or TokenKind::PrivateName |
| 11384 |
return true; |
| 11385 |
} |
| 11386 |
if (TokenKindIsContextualKeyword(tt)) { |
| 11387 |
if (tt == TokenKind::Yield) { |
| 11388 |
if (yieldHandling == YieldIsKeyword) { |
| 11389 |
errorAt(offset, JSMSG_RESERVED_ID, "yield"); |
| 11390 |
return false; |
| 11391 |
} |
| 11392 |
if (pc_->sc()->strict()) { |
| 11393 |
if (!strictModeErrorAt(offset, JSMSG_RESERVED_ID, "yield")) { |
| 11394 |
return false; |
| 11395 |
} |
| 11396 |
} |
| 11397 |
return true; |
| 11398 |
} |
| 11399 |
if (tt == TokenKind::Await) { |
| 11400 |
if (awaitIsKeyword() || awaitIsDisallowed()) { |
| 11401 |
errorAt(offset, JSMSG_RESERVED_ID, "await"); |
| 11402 |
return false; |
| 11403 |
} |
| 11404 |
return true; |
| 11405 |
} |
| 11406 |
if (pc_->sc()->strict()) { |
| 11407 |
if (tt == TokenKind::Let) { |
| 11408 |
if (!strictModeErrorAt(offset, JSMSG_RESERVED_ID, "let")) { |
| 11409 |
return false; |
| 11410 |
} |
| 11411 |
return true; |
| 11412 |
} |
| 11413 |
if (tt == TokenKind::Static) { |
| 11414 |
if (!strictModeErrorAt(offset, JSMSG_RESERVED_ID, "static")) { |
| 11415 |
return false; |
| 11416 |
} |
| 11417 |
return true; |
| 11418 |
} |
| 11419 |
} |
| 11420 |
return true; |
| 11421 |
} |
| 11422 |
if (TokenKindIsStrictReservedWord(tt)) { |
| 11423 |
if (pc_->sc()->strict()) { |
| 11424 |
if (!strictModeErrorAt(offset, JSMSG_RESERVED_ID, |
| 11425 |
ReservedWordToCharZ(tt))) { |
| 11426 |
return false; |
| 11427 |
} |
| 11428 |
} |
| 11429 |
return true; |
| 11430 |
} |
| 11431 |
if (TokenKindIsKeyword(tt) || TokenKindIsReservedWordLiteral(tt)) { |
| 11432 |
errorAt(offset, JSMSG_INVALID_ID, ReservedWordToCharZ(tt)); |
| 11433 |
return false; |
| 11434 |
} |
| 11435 |
if (TokenKindIsFutureReservedWord(tt)) { |
| 11436 |
errorAt(offset, JSMSG_RESERVED_ID, ReservedWordToCharZ(tt)); |
| 11437 |
return false; |
| 11438 |
} |
| 11439 |
MOZ_ASSERT_UNREACHABLE("Unexpected reserved word kind."); |
| 11440 |
return false; |
| 11441 |
} |
| 11442 |
|
| 11443 |
template <class ParseHandler, typename Unit> |
| 11444 |
bool GeneralParser<ParseHandler, Unit>::checkBindingIdentifier( |
| 11445 |
TaggedParserAtomIndex ident, uint32_t offset, YieldHandling yieldHandling, |
| 11446 |
TokenKind hint /* = TokenKind::Limit */) { |
| 11447 |
if (pc_->sc()->strict()) { |
| 11448 |
if (ident == TaggedParserAtomIndex::WellKnown::arguments()) { |
| 11449 |
if (!strictModeErrorAt(offset, JSMSG_BAD_STRICT_ASSIGN, "arguments")) { |
| 11450 |
return false; |
| 11451 |
} |
| 11452 |
return true; |
| 11453 |
} |
| 11454 |
|
| 11455 |
if (ident == TaggedParserAtomIndex::WellKnown::eval()) { |
| 11456 |
if (!strictModeErrorAt(offset, JSMSG_BAD_STRICT_ASSIGN, "eval")) { |
| 11457 |
return false; |
| 11458 |
} |
| 11459 |
return true; |
| 11460 |
} |
| 11461 |
} |
| 11462 |
|
| 11463 |
return checkLabelOrIdentifierReference(ident, offset, yieldHandling, hint); |
| 11464 |
} |
| 11465 |
|
| 11466 |
template <class ParseHandler, typename Unit> |
| 11467 |
TaggedParserAtomIndex |
| 11468 |
GeneralParser<ParseHandler, Unit>::labelOrIdentifierReference( |
| 11469 |
YieldHandling yieldHandling) { |
| 11470 |
// ES 2017 draft 12.1.1. |
| 11471 |
// StringValue of IdentifierName normalizes any Unicode escape sequences |
| 11472 |
// in IdentifierName hence such escapes cannot be used to write an |
| 11473 |
// Identifier whose code point sequence is the same as a ReservedWord. |
| 11474 |
// |
| 11475 |
// Use const ParserName* instead of TokenKind to reflect the normalization. |
| 11476 |
|
| 11477 |
// Unless the name contains escapes, we can reuse the current TokenKind |
| 11478 |
// to determine if the name is a restricted identifier. |
| 11479 |
TokenKind hint = !anyChars.currentNameHasEscapes(this->parserAtoms()) |
| 11480 |
? anyChars.currentToken().type |
| 11481 |
: TokenKind::Limit; |
| 11482 |
TaggedParserAtomIndex ident = anyChars.currentName(); |
| 11483 |
if (!checkLabelOrIdentifierReference(ident, pos().begin, yieldHandling, |
| 11484 |
hint)) { |
| 11485 |
return TaggedParserAtomIndex::null(); |
| 11486 |
} |
| 11487 |
return ident; |
| 11488 |
} |
| 11489 |
|
| 11490 |
template <class ParseHandler, typename Unit> |
| 11491 |
TaggedParserAtomIndex GeneralParser<ParseHandler, Unit>::bindingIdentifier( |
| 11492 |
YieldHandling yieldHandling) { |
| 11493 |
TokenKind hint = !anyChars.currentNameHasEscapes(this->parserAtoms()) |
| 11494 |
? anyChars.currentToken().type |
| 11495 |
: TokenKind::Limit; |
| 11496 |
TaggedParserAtomIndex ident = anyChars.currentName(); |
| 11497 |
if (!checkBindingIdentifier(ident, pos().begin, yieldHandling, hint)) { |
| 11498 |
return TaggedParserAtomIndex::null(); |
| 11499 |
} |
| 11500 |
return ident; |
| 11501 |
} |
| 11502 |
|
| 11503 |
template <class ParseHandler> |
| 11504 |
typename ParseHandler::NameNodeResult |
| 11505 |
PerHandlerParser<ParseHandler>::identifierReference( |
| 11506 |
TaggedParserAtomIndex name) { |
| 11507 |
NameNodeType id; |
| 11508 |
MOZ_TRY_VAR(id, newName(name)); |
| 11509 |
|
| 11510 |
if (!noteUsedName(name)) { |
| 11511 |
return errorResult(); |
| 11512 |
} |
| 11513 |
|
| 11514 |
return id; |
| 11515 |
} |
| 11516 |
|
| 11517 |
template <class ParseHandler> |
| 11518 |
typename ParseHandler::NameNodeResult |
| 11519 |
PerHandlerParser<ParseHandler>::privateNameReference( |
| 11520 |
TaggedParserAtomIndex name) { |
| 11521 |
NameNodeType id; |
| 11522 |
MOZ_TRY_VAR(id, newPrivateName(name)); |
| 11523 |
|
| 11524 |
if (!noteUsedName(name, NameVisibility::Private, Some(pos()))) { |
| 11525 |
return errorResult(); |
| 11526 |
} |
| 11527 |
|
| 11528 |
return id; |
| 11529 |
} |
| 11530 |
|
| 11531 |
template <class ParseHandler> |
| 11532 |
typename ParseHandler::NameNodeResult |
| 11533 |
PerHandlerParser<ParseHandler>::stringLiteral() { |
| 11534 |
return handler_.newStringLiteral(anyChars.currentToken().atom(), pos()); |
| 11535 |
} |
| 11536 |
|
| 11537 |
template <class ParseHandler> |
| 11538 |
typename ParseHandler::NodeResult |
| 11539 |
PerHandlerParser<ParseHandler>::noSubstitutionTaggedTemplate() { |
| 11540 |
if (anyChars.hasInvalidTemplateEscape()) { |
| 11541 |
anyChars.clearInvalidTemplateEscape(); |
| 11542 |
return handler_.newRawUndefinedLiteral(pos()); |
| 11543 |
} |
| 11544 |
|
| 11545 |
return handler_.newTemplateStringLiteral(anyChars.currentToken().atom(), |
| 11546 |
pos()); |
| 11547 |
} |
| 11548 |
|
| 11549 |
template <class ParseHandler, typename Unit> |
| 11550 |
typename ParseHandler::NameNodeResult |
| 11551 |
GeneralParser<ParseHandler, Unit>::noSubstitutionUntaggedTemplate() { |
| 11552 |
if (!tokenStream.checkForInvalidTemplateEscapeError()) { |
| 11553 |
return errorResult(); |
| 11554 |
} |
| 11555 |
|
| 11556 |
return handler_.newTemplateStringLiteral(anyChars.currentToken().atom(), |
| 11557 |
pos()); |
| 11558 |
} |
| 11559 |
|
| 11560 |
template <typename Unit> |
| 11561 |
FullParseHandler::RegExpLiteralResult |
| 11562 |
Parser<FullParseHandler, Unit>::newRegExp() { |
| 11563 |
MOZ_ASSERT(!options().selfHostingMode); |
| 11564 |
|
| 11565 |
// Create the regexp and check its syntax. |
| 11566 |
const auto& chars = tokenStream.getCharBuffer(); |
| 11567 |
mozilla::Range<const char16_t> range(chars.begin(), chars.length()); |
| 11568 |
RegExpFlags flags = anyChars.currentToken().regExpFlags(); |
| 11569 |
|
| 11570 |
uint32_t offset = anyChars.currentToken().pos.begin; |
| 11571 |
uint32_t line; |
| 11572 |
JS::LimitedColumnNumberOneOrigin column; |
| 11573 |
tokenStream.computeLineAndColumn(offset, &line, &column); |
| 11574 |
|
| 11575 |
if (!handler_.reuseRegexpSyntaxParse()) { |
| 11576 |
// Verify that the Regexp will syntax parse when the time comes to |
| 11577 |
// instantiate it. If we have already done a syntax parse, we can |
| 11578 |
// skip this. |
| 11579 |
if (!irregexp::CheckPatternSyntax( |
| 11580 |
this->alloc_, this->fc_->stackLimit(), anyChars, range, flags, |
| 11581 |
Some(line), Some(JS::ColumnNumberOneOrigin(column)))) { |
| 11582 |
return errorResult(); |
| 11583 |
} |
| 11584 |
} |
| 11585 |
|
| 11586 |
auto atom = |
| 11587 |
this->parserAtoms().internChar16(fc_, chars.begin(), chars.length()); |
| 11588 |
if (!atom) { |
| 11589 |
return errorResult(); |
| 11590 |
} |
| 11591 |
// RegExp patterm must be atomized. |
| 11592 |
this->parserAtoms().markUsedByStencil(atom, ParserAtom::Atomize::Yes); |
| 11593 |
|
| 11594 |
RegExpIndex index(this->compilationState_.regExpData.length()); |
| 11595 |
if (uint32_t(index) >= TaggedScriptThingIndex::IndexLimit) { |
| 11596 |
ReportAllocationOverflow(fc_); |
| 11597 |
return errorResult(); |
| 11598 |
} |
| 11599 |
if (!this->compilationState_.regExpData.emplaceBack(atom, flags)) { |
| 11600 |
js::ReportOutOfMemory(this->fc_); |
| 11601 |
return errorResult(); |
| 11602 |
} |
| 11603 |
|
| 11604 |
return handler_.newRegExp(index, pos()); |
| 11605 |
} |
| 11606 |
|
| 11607 |
template <typename Unit> |
| 11608 |
SyntaxParseHandler::RegExpLiteralResult |
| 11609 |
Parser<SyntaxParseHandler, Unit>::newRegExp() { |
| 11610 |
MOZ_ASSERT(!options().selfHostingMode); |
| 11611 |
|
| 11612 |
// Only check the regexp's syntax, but don't create a regexp object. |
| 11613 |
const auto& chars = tokenStream.getCharBuffer(); |
| 11614 |
RegExpFlags flags = anyChars.currentToken().regExpFlags(); |
| 11615 |
|
| 11616 |
uint32_t offset = anyChars.currentToken().pos.begin; |
| 11617 |
uint32_t line; |
| 11618 |
JS::LimitedColumnNumberOneOrigin column; |
| 11619 |
tokenStream.computeLineAndColumn(offset, &line, &column); |
| 11620 |
|
| 11621 |
mozilla::Range<const char16_t> source(chars.begin(), chars.length()); |
| 11622 |
if (!irregexp::CheckPatternSyntax(this->alloc_, this->fc_->stackLimit(), |
| 11623 |
anyChars, source, flags, Some(line), |
| 11624 |
Some(JS::ColumnNumberOneOrigin(column)))) { |
| 11625 |
return errorResult(); |
| 11626 |
} |
| 11627 |
|
| 11628 |
return handler_.newRegExp(SyntaxParseHandler::Node::NodeGeneric, pos()); |
| 11629 |
} |
| 11630 |
|
| 11631 |
template <class ParseHandler, typename Unit> |
| 11632 |
typename ParseHandler::RegExpLiteralResult |
| 11633 |
GeneralParser<ParseHandler, Unit>::newRegExp() { |
| 11634 |
return asFinalParser()->newRegExp(); |
| 11635 |
} |
| 11636 |
|
| 11637 |
template <typename Unit> |
| 11638 |
FullParseHandler::BigIntLiteralResult |
| 11639 |
Parser<FullParseHandler, Unit>::newBigInt() { |
| 11640 |
// The token's charBuffer contains the DecimalIntegerLiteral or |
| 11641 |
// NonDecimalIntegerLiteral production, and as such does not include the |
| 11642 |
// BigIntLiteralSuffix (the trailing "n"). Note that NonDecimalIntegerLiteral |
| 11643 |
// productions start with 0[bBoOxX], indicating binary/octal/hex. |
| 11644 |
const auto& chars = tokenStream.getCharBuffer(); |
| 11645 |
if (chars.length() > UINT32_MAX) { |
| 11646 |
ReportAllocationOverflow(fc_); |
| 11647 |
return errorResult(); |
| 11648 |
} |
| 11649 |
|
| 11650 |
BigIntIndex index(this->bigInts().length()); |
| 11651 |
if (uint32_t(index) >= TaggedScriptThingIndex::IndexLimit) { |
| 11652 |
ReportAllocationOverflow(fc_); |
| 11653 |
return errorResult(); |
| 11654 |
} |
| 11655 |
if (!this->bigInts().emplaceBack()) { |
| 11656 |
js::ReportOutOfMemory(this->fc_); |
| 11657 |
return errorResult(); |
| 11658 |
} |
| 11659 |
|
| 11660 |
if (!this->bigInts()[index].init(this->fc_, this->stencilAlloc(), chars)) { |
| 11661 |
return errorResult(); |
| 11662 |
} |
| 11663 |
|
| 11664 |
// Should the operations below fail, the buffer held by data will |
| 11665 |
// be cleaned up by the CompilationState destructor. |
| 11666 |
return handler_.newBigInt(index, pos()); |
| 11667 |
} |
| 11668 |
|
| 11669 |
template <typename Unit> |
| 11670 |
SyntaxParseHandler::BigIntLiteralResult |
| 11671 |
Parser<SyntaxParseHandler, Unit>::newBigInt() { |
| 11672 |
// The tokenizer has already checked the syntax of the bigint. |
| 11673 |
|
| 11674 |
return handler_.newBigInt(); |
| 11675 |
} |
| 11676 |
|
| 11677 |
template <class ParseHandler, typename Unit> |
| 11678 |
typename ParseHandler::BigIntLiteralResult |
| 11679 |
GeneralParser<ParseHandler, Unit>::newBigInt() { |
| 11680 |
return asFinalParser()->newBigInt(); |
| 11681 |
} |
| 11682 |
|
| 11683 |
// |exprPossibleError| is the PossibleError state within |expr|, |
| 11684 |
// |possibleError| is the surrounding PossibleError state. |
| 11685 |
template <class ParseHandler, typename Unit> |
| 11686 |
bool GeneralParser<ParseHandler, Unit>::checkDestructuringAssignmentTarget( |
| 11687 |
Node expr, TokenPos exprPos, PossibleError* exprPossibleError, |
| 11688 |
PossibleError* possibleError, TargetBehavior behavior) { |
| 11689 |
// Report any pending expression error if we're definitely not in a |
| 11690 |
// destructuring context or the possible destructuring target is a |
| 11691 |
// property accessor. |
| 11692 |
if (!possibleError || handler_.isPropertyOrPrivateMemberAccess(expr)) { |
| 11693 |
return exprPossibleError->checkForExpressionError(); |
| 11694 |
} |
| 11695 |
|
| 11696 |
// |expr| may end up as a destructuring assignment target, so we need to |
| 11697 |
// validate it's either a name or can be parsed as a nested destructuring |
| 11698 |
// pattern. Property accessors are also valid assignment targets, but |
| 11699 |
// those are already handled above. |
| 11700 |
|
| 11701 |
exprPossibleError->transferErrorsTo(possibleError); |
| 11702 |
|
| 11703 |
// Return early if a pending destructuring error is already present. |
| 11704 |
if (possibleError->hasPendingDestructuringError()) { |
| 11705 |
return true; |
| 11706 |
} |
| 11707 |
|
| 11708 |
if (handler_.isName(expr)) { |
| 11709 |
checkDestructuringAssignmentName(handler_.asNameNode(expr), exprPos, |
| 11710 |
possibleError); |
| 11711 |
return true; |
| 11712 |
} |
| 11713 |
|
| 11714 |
if (handler_.isUnparenthesizedDestructuringPattern(expr)) { |
| 11715 |
if (behavior == TargetBehavior::ForbidAssignmentPattern) { |
| 11716 |
possibleError->setPendingDestructuringErrorAt(exprPos, |
| 11717 |
JSMSG_BAD_DESTRUCT_TARGET); |
| 11718 |
} |
| 11719 |
return true; |
| 11720 |
} |
| 11721 |
|
| 11722 |
// Parentheses are forbidden around destructuring *patterns* (but allowed |
| 11723 |
// around names). Use our nicer error message for parenthesized, nested |
| 11724 |
// patterns if nested destructuring patterns are allowed. |
| 11725 |
if (handler_.isParenthesizedDestructuringPattern(expr) && |
| 11726 |
behavior != TargetBehavior::ForbidAssignmentPattern) { |
| 11727 |
possibleError->setPendingDestructuringErrorAt(exprPos, |
| 11728 |
JSMSG_BAD_DESTRUCT_PARENS); |
| 11729 |
} else { |
| 11730 |
possibleError->setPendingDestructuringErrorAt(exprPos, |
| 11731 |
JSMSG_BAD_DESTRUCT_TARGET); |
| 11732 |
} |
| 11733 |
|
| 11734 |
return true; |
| 11735 |
} |
| 11736 |
|
| 11737 |
template <class ParseHandler, typename Unit> |
| 11738 |
void GeneralParser<ParseHandler, Unit>::checkDestructuringAssignmentName( |
| 11739 |
NameNodeType name, TokenPos namePos, PossibleError* possibleError) { |
| 11740 |
#ifdef DEBUG |
| 11741 |
// GCC 8.0.1 crashes if this is a one-liner. |
| 11742 |
bool isName = handler_.isName(name); |
| 11743 |
MOZ_ASSERT(isName); |
| 11744 |
#endif |
| 11745 |
|
| 11746 |
// Return early if a pending destructuring error is already present. |
| 11747 |
if (possibleError->hasPendingDestructuringError()) { |
| 11748 |
return; |
| 11749 |
} |
| 11750 |
|
| 11751 |
if (handler_.isArgumentsLength(name)) { |
| 11752 |
pc_->sc()->setIneligibleForArgumentsLength(); |
| 11753 |
} |
| 11754 |
|
| 11755 |
if (pc_->sc()->strict()) { |
| 11756 |
if (handler_.isArgumentsName(name)) { |
| 11757 |
if (pc_->sc()->strict()) { |
| 11758 |
possibleError->setPendingDestructuringErrorAt( |
| 11759 |
namePos, JSMSG_BAD_STRICT_ASSIGN_ARGUMENTS); |
| 11760 |
} else { |
| 11761 |
possibleError->setPendingDestructuringWarningAt( |
| 11762 |
namePos, JSMSG_BAD_STRICT_ASSIGN_ARGUMENTS); |
| 11763 |
} |
| 11764 |
return; |
| 11765 |
} |
| 11766 |
|
| 11767 |
if (handler_.isEvalName(name)) { |
| 11768 |
if (pc_->sc()->strict()) { |
| 11769 |
possibleError->setPendingDestructuringErrorAt( |
| 11770 |
namePos, JSMSG_BAD_STRICT_ASSIGN_EVAL); |
| 11771 |
} else { |
| 11772 |
possibleError->setPendingDestructuringWarningAt( |
| 11773 |
namePos, JSMSG_BAD_STRICT_ASSIGN_EVAL); |
| 11774 |
} |
| 11775 |
return; |
| 11776 |
} |
| 11777 |
} |
| 11778 |
} |
| 11779 |
|
| 11780 |
template <class ParseHandler, typename Unit> |
| 11781 |
bool GeneralParser<ParseHandler, Unit>::checkDestructuringAssignmentElement( |
| 11782 |
Node expr, TokenPos exprPos, PossibleError* exprPossibleError, |
| 11783 |
PossibleError* possibleError) { |
| 11784 |
// ES2018 draft rev 0719f44aab93215ed9a626b2f45bd34f36916834 |
| 11785 |
// 12.15.5 Destructuring Assignment |
| 11786 |
// |
| 11787 |
// AssignmentElement[Yield, Await]: |
| 11788 |
// DestructuringAssignmentTarget[?Yield, ?Await] |
| 11789 |
// DestructuringAssignmentTarget[?Yield, ?Await] Initializer[+In, |
| 11790 |
// ?Yield, |
| 11791 |
// ?Await] |
| 11792 |
|
| 11793 |
// If |expr| is an assignment element with an initializer expression, its |
| 11794 |
// destructuring assignment target was already validated in assignExpr(). |
| 11795 |
// Otherwise we need to check that |expr| is a valid destructuring target. |
| 11796 |
if (handler_.isUnparenthesizedAssignment(expr)) { |
| 11797 |
// Report any pending expression error if we're definitely not in a |
| 11798 |
// destructuring context. |
| 11799 |
if (!possibleError) { |
| 11800 |
return exprPossibleError->checkForExpressionError(); |
| 11801 |
} |
| 11802 |
|
| 11803 |
exprPossibleError->transferErrorsTo(possibleError); |
| 11804 |
return true; |
| 11805 |
} |
| 11806 |
return checkDestructuringAssignmentTarget(expr, exprPos, exprPossibleError, |
| 11807 |
possibleError); |
| 11808 |
} |
| 11809 |
|
| 11810 |
template <class ParseHandler, typename Unit> |
| 11811 |
typename ParseHandler::ListNodeResult |
| 11812 |
GeneralParser<ParseHandler, Unit>::arrayInitializer( |
| 11813 |
YieldHandling yieldHandling, PossibleError* possibleError) { |
| 11814 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::LeftBracket)); |
| 11815 |
|
| 11816 |
uint32_t begin = pos().begin; |
| 11817 |
ListNodeType literal; |
| 11818 |
MOZ_TRY_VAR(literal, handler_.newArrayLiteral(begin)); |
| 11819 |
|
| 11820 |
TokenKind tt; |
| 11821 |
if (!tokenStream.getToken(&tt, TokenStream::SlashIsRegExp)) { |
| 11822 |
return errorResult(); |
| 11823 |
} |
| 11824 |
|
| 11825 |
if (tt == TokenKind::RightBracket) { |
| 11826 |
/* |
| 11827 |
* Mark empty arrays as non-constant, since we cannot easily |
| 11828 |
* determine their type. |
| 11829 |
*/ |
| 11830 |
handler_.setListHasNonConstInitializer(literal); |
| 11831 |
} else { |
| 11832 |
anyChars.ungetToken(); |
| 11833 |
|
| 11834 |
for (uint32_t index = 0;; index++) { |
| 11835 |
if (index >= NativeObject::MAX_DENSE_ELEMENTS_COUNT) { |
| 11836 |
error(JSMSG_ARRAY_INIT_TOO_BIG); |
| 11837 |
return errorResult(); |
| 11838 |
} |
| 11839 |
|
| 11840 |
TokenKind tt; |
| 11841 |
if (!tokenStream.peekToken(&tt, TokenStream::SlashIsRegExp)) { |
| 11842 |
return errorResult(); |
| 11843 |
} |
| 11844 |
if (tt == TokenKind::RightBracket) { |
| 11845 |
break; |
| 11846 |
} |
| 11847 |
|
| 11848 |
if (tt == TokenKind::Comma) { |
| 11849 |
tokenStream.consumeKnownToken(TokenKind::Comma, |
| 11850 |
TokenStream::SlashIsRegExp); |
| 11851 |
if (!handler_.addElision(literal, pos())) { |
| 11852 |
return errorResult(); |
| 11853 |
} |
| 11854 |
continue; |
| 11855 |
} |
| 11856 |
|
| 11857 |
if (tt == TokenKind::TripleDot) { |
| 11858 |
tokenStream.consumeKnownToken(TokenKind::TripleDot, |
| 11859 |
TokenStream::SlashIsRegExp); |
| 11860 |
uint32_t begin = pos().begin; |
| 11861 |
|
| 11862 |
TokenPos innerPos; |
| 11863 |
if (!tokenStream.peekTokenPos(&innerPos, TokenStream::SlashIsRegExp)) { |
| 11864 |
return errorResult(); |
| 11865 |
} |
| 11866 |
|
| 11867 |
PossibleError possibleErrorInner(*this); |
| 11868 |
Node inner; |
| 11869 |
MOZ_TRY_VAR(inner, |
| 11870 |
assignExpr(InAllowed, yieldHandling, TripledotProhibited, |
| 11871 |
&possibleErrorInner)); |
| 11872 |
if (!checkDestructuringAssignmentTarget( |
| 11873 |
inner, innerPos, &possibleErrorInner, possibleError)) { |
| 11874 |
return errorResult(); |
| 11875 |
} |
| 11876 |
|
| 11877 |
if (!handler_.addSpreadElement(literal, begin, inner)) { |
| 11878 |
return errorResult(); |
| 11879 |
} |
| 11880 |
} else { |
| 11881 |
TokenPos elementPos; |
| 11882 |
if (!tokenStream.peekTokenPos(&elementPos, |
| 11883 |
TokenStream::SlashIsRegExp)) { |
| 11884 |
return errorResult(); |
| 11885 |
} |
| 11886 |
|
| 11887 |
PossibleError possibleErrorInner(*this); |
| 11888 |
Node element; |
| 11889 |
MOZ_TRY_VAR(element, |
| 11890 |
assignExpr(InAllowed, yieldHandling, TripledotProhibited, |
| 11891 |
&possibleErrorInner)); |
| 11892 |
if (!checkDestructuringAssignmentElement( |
| 11893 |
element, elementPos, &possibleErrorInner, possibleError)) { |
| 11894 |
return errorResult(); |
| 11895 |
} |
| 11896 |
handler_.addArrayElement(literal, element); |
| 11897 |
} |
| 11898 |
|
| 11899 |
bool matched; |
| 11900 |
if (!tokenStream.matchToken(&matched, TokenKind::Comma, |
| 11901 |
TokenStream::SlashIsRegExp)) { |
| 11902 |
return errorResult(); |
| 11903 |
} |
| 11904 |
if (!matched) { |
| 11905 |
break; |
| 11906 |
} |
| 11907 |
|
| 11908 |
if (tt == TokenKind::TripleDot && possibleError) { |
| 11909 |
possibleError->setPendingDestructuringErrorAt(pos(), |
| 11910 |
JSMSG_REST_WITH_COMMA); |
| 11911 |
} |
| 11912 |
} |
| 11913 |
|
| 11914 |
if (!mustMatchToken( |
| 11915 |
TokenKind::RightBracket, [this, begin](TokenKind actual) { |
| 11916 |
this->reportMissingClosing(JSMSG_BRACKET_AFTER_LIST, |
| 11917 |
JSMSG_BRACKET_OPENED, begin); |
| 11918 |
})) { |
| 11919 |
return errorResult(); |
| 11920 |
} |
| 11921 |
} |
| 11922 |
|
| 11923 |
handler_.setEndPosition(literal, pos().end); |
| 11924 |
return literal; |
| 11925 |
} |
| 11926 |
|
| 11927 |
template <class ParseHandler, typename Unit> |
| 11928 |
typename ParseHandler::NodeResult |
| 11929 |
GeneralParser<ParseHandler, Unit>::propertyName( |
| 11930 |
YieldHandling yieldHandling, PropertyNameContext propertyNameContext, |
| 11931 |
const Maybe<DeclarationKind>& maybeDecl, ListNodeType propList, |
| 11932 |
TaggedParserAtomIndex* propAtomOut) { |
| 11933 |
// PropertyName[Yield, Await]: |
| 11934 |
// LiteralPropertyName |
| 11935 |
// ComputedPropertyName[?Yield, ?Await] |
| 11936 |
// |
| 11937 |
// LiteralPropertyName: |
| 11938 |
// IdentifierName |
| 11939 |
// StringLiteral |
| 11940 |
// NumericLiteral |
| 11941 |
TokenKind ltok = anyChars.currentToken().type; |
| 11942 |
|
| 11943 |
*propAtomOut = TaggedParserAtomIndex::null(); |
| 11944 |
switch (ltok) { |
| 11945 |
case TokenKind::Number: { |
| 11946 |
auto numAtom = NumberToParserAtom(fc_, this->parserAtoms(), |
| 11947 |
anyChars.currentToken().number()); |
| 11948 |
if (!numAtom) { |
| 11949 |
return errorResult(); |
| 11950 |
} |
| 11951 |
*propAtomOut = numAtom; |
| 11952 |
return newNumber(anyChars.currentToken()); |
| 11953 |
} |
| 11954 |
|
| 11955 |
case TokenKind::BigInt: { |
| 11956 |
Node biNode; |
| 11957 |
MOZ_TRY_VAR(biNode, newBigInt()); |
| 11958 |
return handler_.newSyntheticComputedName(biNode, pos().begin, pos().end); |
| 11959 |
} |
| 11960 |
case TokenKind::String: { |
| 11961 |
auto str = anyChars.currentToken().atom(); |
| 11962 |
*propAtomOut = str; |
| 11963 |
uint32_t index; |
| 11964 |
if (this->parserAtoms().isIndex(str, &index)) { |
| 11965 |
return handler_.newNumber(index, NoDecimal, pos()); |
| 11966 |
} |
| 11967 |
return stringLiteral(); |
| 11968 |
} |
| 11969 |
|
| 11970 |
case TokenKind::LeftBracket: |
| 11971 |
return computedPropertyName(yieldHandling, maybeDecl, propertyNameContext, |
| 11972 |
propList); |
| 11973 |
|
| 11974 |
case TokenKind::PrivateName: { |
| 11975 |
if (propertyNameContext != PropertyNameContext::PropertyNameInClass) { |
| 11976 |
error(JSMSG_ILLEGAL_PRIVATE_FIELD); |
| 11977 |
return errorResult(); |
| 11978 |
} |
| 11979 |
|
| 11980 |
TaggedParserAtomIndex propName = anyChars.currentName(); |
| 11981 |
*propAtomOut = propName; |
| 11982 |
return privateNameReference(propName); |
| 11983 |
} |
| 11984 |
|
| 11985 |
default: { |
| 11986 |
if (!TokenKindIsPossibleIdentifierName(ltok)) { |
| 11987 |
error(JSMSG_UNEXPECTED_TOKEN, "property name", TokenKindToDesc(ltok)); |
| 11988 |
return errorResult(); |
| 11989 |
} |
| 11990 |
|
| 11991 |
TaggedParserAtomIndex name = anyChars.currentName(); |
| 11992 |
*propAtomOut = name; |
| 11993 |
return handler_.newObjectLiteralPropertyName(name, pos()); |
| 11994 |
} |
| 11995 |
} |
| 11996 |
} |
| 11997 |
|
| 11998 |
// True if `kind` can be the first token of a PropertyName. |
| 11999 |
static bool TokenKindCanStartPropertyName(TokenKind tt) { |
| 12000 |
return TokenKindIsPossibleIdentifierName(tt) || tt == TokenKind::String || |
| 12001 |
tt == TokenKind::Number || tt == TokenKind::LeftBracket || |
| 12002 |
tt == TokenKind::BigInt || tt == TokenKind::PrivateName; |
| 12003 |
} |
| 12004 |
|
| 12005 |
template <class ParseHandler, typename Unit> |
| 12006 |
typename ParseHandler::NodeResult |
| 12007 |
GeneralParser<ParseHandler, Unit>::propertyOrMethodName( |
| 12008 |
YieldHandling yieldHandling, PropertyNameContext propertyNameContext, |
| 12009 |
const Maybe<DeclarationKind>& maybeDecl, ListNodeType propList, |
| 12010 |
PropertyType* propType, TaggedParserAtomIndex* propAtomOut) { |
| 12011 |
// We're parsing an object literal, class, or destructuring pattern; |
| 12012 |
// propertyNameContext tells which one. This method parses any of the |
| 12013 |
// following, storing the corresponding PropertyType in `*propType` to tell |
| 12014 |
// the caller what we parsed: |
| 12015 |
// |
| 12016 |
// async [no LineTerminator here] PropertyName |
| 12017 |
// ==> PropertyType::AsyncMethod |
| 12018 |
// async [no LineTerminator here] * PropertyName |
| 12019 |
// ==> PropertyType::AsyncGeneratorMethod |
| 12020 |
// * PropertyName ==> PropertyType::GeneratorMethod |
| 12021 |
// get PropertyName ==> PropertyType::Getter |
| 12022 |
// set PropertyName ==> PropertyType::Setter |
| 12023 |
// accessor PropertyName ==> PropertyType::FieldWithAccessor |
| 12024 |
// PropertyName : ==> PropertyType::Normal |
| 12025 |
// PropertyName ==> see below |
| 12026 |
// |
| 12027 |
// In the last case, where there's not a `:` token to consume, we peek at |
| 12028 |
// (but don't consume) the next token to decide how to set `*propType`. |
| 12029 |
// |
| 12030 |
// `,` or `}` ==> PropertyType::Shorthand |
| 12031 |
// `(` ==> PropertyType::Method |
| 12032 |
// `=`, not in a class ==> PropertyType::CoverInitializedName |
| 12033 |
// '=', in a class ==> PropertyType::Field |
| 12034 |
// any token, in a class ==> PropertyType::Field (ASI) |
| 12035 |
// |
| 12036 |
// The caller must check `*propType` and throw if whatever we parsed isn't |
| 12037 |
// allowed here (for example, a getter in a destructuring pattern). |
| 12038 |
// |
| 12039 |
// This method does *not* match `static` (allowed in classes) or `...` |
| 12040 |
// (allowed in object literals and patterns). The caller must take care of |
| 12041 |
// those before calling this method. |
| 12042 |
|
| 12043 |
TokenKind ltok; |
| 12044 |
if (!tokenStream.getToken(<ok, TokenStream::SlashIsInvalid)) { |
| 12045 |
return errorResult(); |
| 12046 |
} |
| 12047 |
|
| 12048 |
MOZ_ASSERT(ltok != TokenKind::RightCurly, |
| 12049 |
"caller should have handled TokenKind::RightCurly"); |
| 12050 |
|
| 12051 |
// Accept `async` and/or `*`, indicating an async or generator method; |
| 12052 |
// or `get` or `set` or `accessor`, indicating an accessor. |
| 12053 |
bool isGenerator = false; |
| 12054 |
bool isAsync = false; |
| 12055 |
bool isGetter = false; |
| 12056 |
bool isSetter = false; |
| 12057 |
#ifdef ENABLE_DECORATORS |
| 12058 |
bool hasAccessor = false; |
| 12059 |
#endif |
| 12060 |
|
| 12061 |
if (ltok == TokenKind::Async) { |
| 12062 |
// `async` is also a PropertyName by itself (it's a conditional keyword), |
| 12063 |
// so peek at the next token to see if we're really looking at a method. |
| 12064 |
TokenKind tt = TokenKind::Eof; |
| 12065 |
if (!tokenStream.peekTokenSameLine(&tt)) { |
| 12066 |
return errorResult(); |
| 12067 |
} |
| 12068 |
if (TokenKindCanStartPropertyName(tt) || tt == TokenKind::Mul) { |
| 12069 |
isAsync = true; |
| 12070 |
tokenStream.consumeKnownToken(tt); |
| 12071 |
ltok = tt; |
| 12072 |
} |
| 12073 |
} |
| 12074 |
|
| 12075 |
if (ltok == TokenKind::Mul) { |
| 12076 |
isGenerator = true; |
| 12077 |
if (!tokenStream.getToken(<ok)) { |
| 12078 |
return errorResult(); |
| 12079 |
} |
| 12080 |
} |
| 12081 |
|
| 12082 |
if (!isAsync && !isGenerator && |
| 12083 |
(ltok == TokenKind::Get || ltok == TokenKind::Set)) { |
| 12084 |
// We have parsed |get| or |set|. Look for an accessor property |
| 12085 |
// name next. |
| 12086 |
TokenKind tt; |
| 12087 |
if (!tokenStream.peekToken(&tt)) { |
| 12088 |
return errorResult(); |
| 12089 |
} |
| 12090 |
if (TokenKindCanStartPropertyName(tt)) { |
| 12091 |
tokenStream.consumeKnownToken(tt); |
| 12092 |
isGetter = (ltok == TokenKind::Get); |
| 12093 |
isSetter = (ltok == TokenKind::Set); |
| 12094 |
} |
| 12095 |
} |
| 12096 |
|
| 12097 |
#ifdef ENABLE_DECORATORS |
| 12098 |
if (!isGenerator && !isAsync && propertyNameContext == PropertyNameInClass && |
| 12099 |
ltok == TokenKind::Accessor) { |
| 12100 |
MOZ_ASSERT(!isGetter && !isSetter); |
| 12101 |
TokenKind tt; |
| 12102 |
if (!tokenStream.peekTokenSameLine(&tt)) { |
| 12103 |
return errorResult(); |
| 12104 |
} |
| 12105 |
|
| 12106 |
// The target rule is `accessor [no LineTerminator here] |
| 12107 |
// ClassElementName[?Yield, ?Await] Initializer[+In, ?Yield, ?Await]opt` |
| 12108 |
if (TokenKindCanStartPropertyName(tt)) { |
| 12109 |
tokenStream.consumeKnownToken(tt); |
| 12110 |
hasAccessor = true; |
| 12111 |
} |
| 12112 |
} |
| 12113 |
#endif |
| 12114 |
|
| 12115 |
Node propName; |
| 12116 |
MOZ_TRY_VAR(propName, propertyName(yieldHandling, propertyNameContext, |
| 12117 |
maybeDecl, propList, propAtomOut)); |
| 12118 |
|
| 12119 |
// Grab the next token following the property/method name. |
| 12120 |
// (If this isn't a colon, we're going to either put it back or throw.) |
| 12121 |
TokenKind tt; |
| 12122 |
if (!tokenStream.getToken(&tt)) { |
| 12123 |
return errorResult(); |
| 12124 |
} |
| 12125 |
|
| 12126 |
if (tt == TokenKind::Colon) { |
| 12127 |
if (isGenerator || isAsync || isGetter || isSetter |
| 12128 |
#ifdef ENABLE_DECORATORS |
| 12129 |
|| hasAccessor |
| 12130 |
#endif |
| 12131 |
) { |
| 12132 |
error(JSMSG_BAD_PROP_ID); |
| 12133 |
return errorResult(); |
| 12134 |
} |
| 12135 |
*propType = PropertyType::Normal; |
| 12136 |
return propName; |
| 12137 |
} |
| 12138 |
|
| 12139 |
if (propertyNameContext != PropertyNameInClass && |
| 12140 |
TokenKindIsPossibleIdentifierName(ltok) && |
| 12141 |
(tt == TokenKind::Comma || tt == TokenKind::RightCurly || |
| 12142 |
tt == TokenKind::Assign)) { |
| 12143 |
#ifdef ENABLE_DECORATORS |
| 12144 |
MOZ_ASSERT(!hasAccessor); |
| 12145 |
#endif |
| 12146 |
if (isGenerator || isAsync || isGetter || isSetter) { |
| 12147 |
error(JSMSG_BAD_PROP_ID); |
| 12148 |
return errorResult(); |
| 12149 |
} |
| 12150 |
|
| 12151 |
anyChars.ungetToken(); |
| 12152 |
*propType = tt == TokenKind::Assign ? PropertyType::CoverInitializedName |
| 12153 |
: PropertyType::Shorthand; |
| 12154 |
return propName; |
| 12155 |
} |
| 12156 |
|
| 12157 |
if (tt == TokenKind::LeftParen) { |
| 12158 |
anyChars.ungetToken(); |
| 12159 |
|
| 12160 |
#ifdef ENABLE_DECORATORS |
| 12161 |
if (hasAccessor) { |
| 12162 |
error(JSMSG_BAD_PROP_ID); |
| 12163 |
return errorResult(); |
| 12164 |
} |
| 12165 |
#endif |
| 12166 |
|
| 12167 |
if (isGenerator && isAsync) { |
| 12168 |
*propType = PropertyType::AsyncGeneratorMethod; |
| 12169 |
} else if (isGenerator) { |
| 12170 |
*propType = PropertyType::GeneratorMethod; |
| 12171 |
} else if (isAsync) { |
| 12172 |
*propType = PropertyType::AsyncMethod; |
| 12173 |
} else if (isGetter) { |
| 12174 |
*propType = PropertyType::Getter; |
| 12175 |
} else if (isSetter) { |
| 12176 |
*propType = PropertyType::Setter; |
| 12177 |
} else { |
| 12178 |
*propType = PropertyType::Method; |
| 12179 |
} |
| 12180 |
return propName; |
| 12181 |
} |
| 12182 |
|
| 12183 |
if (propertyNameContext == PropertyNameInClass) { |
| 12184 |
if (isGenerator || isAsync || isGetter || isSetter) { |
| 12185 |
error(JSMSG_BAD_PROP_ID); |
| 12186 |
return errorResult(); |
| 12187 |
} |
| 12188 |
anyChars.ungetToken(); |
| 12189 |
#ifdef ENABLE_DECORATORS |
| 12190 |
if (!hasAccessor) { |
| 12191 |
*propType = PropertyType::Field; |
| 12192 |
} else { |
| 12193 |
*propType = PropertyType::FieldWithAccessor; |
| 12194 |
} |
| 12195 |
#else |
| 12196 |
*propType = PropertyType::Field; |
| 12197 |
#endif |
| 12198 |
return propName; |
| 12199 |
} |
| 12200 |
|
| 12201 |
error(JSMSG_COLON_AFTER_ID); |
| 12202 |
return errorResult(); |
| 12203 |
} |
| 12204 |
|
| 12205 |
template <class ParseHandler, typename Unit> |
| 12206 |
typename ParseHandler::UnaryNodeResult |
| 12207 |
GeneralParser<ParseHandler, Unit>::computedPropertyName( |
| 12208 |
YieldHandling yieldHandling, const Maybe<DeclarationKind>& maybeDecl, |
| 12209 |
PropertyNameContext propertyNameContext, ListNodeType literal) { |
| 12210 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::LeftBracket)); |
| 12211 |
|
| 12212 |
uint32_t begin = pos().begin; |
| 12213 |
|
| 12214 |
if (maybeDecl) { |
| 12215 |
if (*maybeDecl == DeclarationKind::FormalParameter) { |
| 12216 |
pc_->functionBox()->hasParameterExprs = true; |
| 12217 |
} |
| 12218 |
} else if (propertyNameContext == |
| 12219 |
PropertyNameContext::PropertyNameInLiteral) { |
| 12220 |
handler_.setListHasNonConstInitializer(literal); |
| 12221 |
} |
| 12222 |
|
| 12223 |
Node assignNode; |
| 12224 |
MOZ_TRY_VAR(assignNode, |
| 12225 |
assignExpr(InAllowed, yieldHandling, TripledotProhibited)); |
| 12226 |
|
| 12227 |
if (!mustMatchToken(TokenKind::RightBracket, JSMSG_COMP_PROP_UNTERM_EXPR)) { |
| 12228 |
return errorResult(); |
| 12229 |
} |
| 12230 |
return handler_.newComputedName(assignNode, begin, pos().end); |
| 12231 |
} |
| 12232 |
|
| 12233 |
template <class ParseHandler, typename Unit> |
| 12234 |
typename ParseHandler::ListNodeResult |
| 12235 |
GeneralParser<ParseHandler, Unit>::objectLiteral(YieldHandling yieldHandling, |
| 12236 |
PossibleError* possibleError) { |
| 12237 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::LeftCurly)); |
| 12238 |
|
| 12239 |
uint32_t openedPos = pos().begin; |
| 12240 |
|
| 12241 |
ListNodeType literal; |
| 12242 |
MOZ_TRY_VAR(literal, handler_.newObjectLiteral(pos().begin)); |
| 12243 |
|
| 12244 |
bool seenPrototypeMutation = false; |
| 12245 |
bool seenCoverInitializedName = false; |
| 12246 |
Maybe<DeclarationKind> declKind = Nothing(); |
| 12247 |
TaggedParserAtomIndex propAtom; |
| 12248 |
for (;;) { |
| 12249 |
TokenKind tt; |
| 12250 |
if (!tokenStream.peekToken(&tt)) { |
| 12251 |
return errorResult(); |
| 12252 |
} |
| 12253 |
if (tt == TokenKind::RightCurly) { |
| 12254 |
break; |
| 12255 |
} |
| 12256 |
|
| 12257 |
if (tt == TokenKind::TripleDot) { |
| 12258 |
tokenStream.consumeKnownToken(TokenKind::TripleDot); |
| 12259 |
uint32_t begin = pos().begin; |
| 12260 |
|
| 12261 |
TokenPos innerPos; |
| 12262 |
if (!tokenStream.peekTokenPos(&innerPos, TokenStream::SlashIsRegExp)) { |
| 12263 |
return errorResult(); |
| 12264 |
} |
| 12265 |
|
| 12266 |
PossibleError possibleErrorInner(*this); |
| 12267 |
Node inner; |
| 12268 |
MOZ_TRY_VAR(inner, assignExpr(InAllowed, yieldHandling, |
| 12269 |
TripledotProhibited, &possibleErrorInner)); |
| 12270 |
if (!checkDestructuringAssignmentTarget( |
| 12271 |
inner, innerPos, &possibleErrorInner, possibleError, |
| 12272 |
TargetBehavior::ForbidAssignmentPattern)) { |
| 12273 |
return errorResult(); |
| 12274 |
} |
| 12275 |
if (!handler_.addSpreadProperty(literal, begin, inner)) { |
| 12276 |
return errorResult(); |
| 12277 |
} |
| 12278 |
} else { |
| 12279 |
TokenPos namePos = anyChars.nextToken().pos; |
| 12280 |
|
| 12281 |
PropertyType propType; |
| 12282 |
Node propName; |
| 12283 |
MOZ_TRY_VAR(propName, propertyOrMethodName( |
| 12284 |
yieldHandling, PropertyNameInLiteral, declKind, |
| 12285 |
literal, &propType, &propAtom)); |
| 12286 |
|
| 12287 |
if (propType == PropertyType::Normal) { |
| 12288 |
TokenPos exprPos; |
| 12289 |
if (!tokenStream.peekTokenPos(&exprPos, TokenStream::SlashIsRegExp)) { |
| 12290 |
return errorResult(); |
| 12291 |
} |
| 12292 |
|
| 12293 |
PossibleError possibleErrorInner(*this); |
| 12294 |
Node propExpr; |
| 12295 |
MOZ_TRY_VAR(propExpr, |
| 12296 |
assignExpr(InAllowed, yieldHandling, TripledotProhibited, |
| 12297 |
&possibleErrorInner)); |
| 12298 |
|
| 12299 |
if (!checkDestructuringAssignmentElement( |
| 12300 |
propExpr, exprPos, &possibleErrorInner, possibleError)) { |
| 12301 |
return errorResult(); |
| 12302 |
} |
| 12303 |
|
| 12304 |
if (propAtom == TaggedParserAtomIndex::WellKnown::proto_()) { |
| 12305 |
if (seenPrototypeMutation) { |
| 12306 |
// Directly report the error when we're definitely not |
| 12307 |
// in a destructuring context. |
| 12308 |
if (!possibleError) { |
| 12309 |
errorAt(namePos.begin, JSMSG_DUPLICATE_PROTO_PROPERTY); |
| 12310 |
return errorResult(); |
| 12311 |
} |
| 12312 |
|
| 12313 |
// Otherwise delay error reporting until we've |
| 12314 |
// determined whether or not we're destructuring. |
| 12315 |
possibleError->setPendingExpressionErrorAt( |
| 12316 |
namePos, JSMSG_DUPLICATE_PROTO_PROPERTY); |
| 12317 |
} |
| 12318 |
seenPrototypeMutation = true; |
| 12319 |
|
| 12320 |
// This occurs *only* if we observe PropertyType::Normal! |
| 12321 |
// Only |__proto__: v| mutates [[Prototype]]. Getters, |
| 12322 |
// setters, method/generator definitions, computed |
| 12323 |
// property name versions of all of these, and shorthands |
| 12324 |
// do not. |
| 12325 |
if (!handler_.addPrototypeMutation(literal, namePos.begin, |
| 12326 |
propExpr)) { |
| 12327 |
return errorResult(); |
| 12328 |
} |
| 12329 |
} else { |
| 12330 |
BinaryNodeType propDef; |
| 12331 |
MOZ_TRY_VAR(propDef, |
| 12332 |
handler_.newPropertyDefinition(propName, propExpr)); |
| 12333 |
|
| 12334 |
handler_.addPropertyDefinition(literal, propDef); |
| 12335 |
} |
| 12336 |
} else if (propType == PropertyType::Shorthand) { |
| 12337 |
/* |
| 12338 |
* Support, e.g., |({x, y} = o)| as destructuring shorthand |
| 12339 |
* for |({x: x, y: y} = o)|, and |var o = {x, y}| as |
| 12340 |
* initializer shorthand for |var o = {x: x, y: y}|. |
| 12341 |
*/ |
| 12342 |
TaggedParserAtomIndex name = identifierReference(yieldHandling); |
| 12343 |
if (!name) { |
| 12344 |
return errorResult(); |
| 12345 |
} |
| 12346 |
|
| 12347 |
NameNodeType nameExpr; |
| 12348 |
MOZ_TRY_VAR(nameExpr, identifierReference(name)); |
| 12349 |
|
| 12350 |
if (possibleError) { |
| 12351 |
checkDestructuringAssignmentName(nameExpr, namePos, possibleError); |
| 12352 |
} |
| 12353 |
|
| 12354 |
if (!handler_.addShorthand(literal, handler_.asNameNode(propName), |
| 12355 |
nameExpr)) { |
| 12356 |
return errorResult(); |
| 12357 |
} |
| 12358 |
} else if (propType == PropertyType::CoverInitializedName) { |
| 12359 |
/* |
| 12360 |
* Support, e.g., |({x=1, y=2} = o)| as destructuring |
| 12361 |
* shorthand with default values, as per ES6 12.14.5 |
| 12362 |
*/ |
| 12363 |
TaggedParserAtomIndex name = identifierReference(yieldHandling); |
| 12364 |
if (!name) { |
| 12365 |
return errorResult(); |
| 12366 |
} |
| 12367 |
|
| 12368 |
Node lhs; |
| 12369 |
MOZ_TRY_VAR(lhs, identifierReference(name)); |
| 12370 |
|
| 12371 |
tokenStream.consumeKnownToken(TokenKind::Assign); |
| 12372 |
|
| 12373 |
if (!seenCoverInitializedName) { |
| 12374 |
// "shorthand default" or "CoverInitializedName" syntax is |
| 12375 |
// only valid in the case of destructuring. |
| 12376 |
seenCoverInitializedName = true; |
| 12377 |
|
| 12378 |
if (!possibleError) { |
| 12379 |
// Destructuring defaults are definitely not allowed |
| 12380 |
// in this object literal, because of something the |
| 12381 |
// caller knows about the preceding code. For example, |
| 12382 |
// maybe the preceding token is an operator: |
| 12383 |
// |x + {y=z}|. |
| 12384 |
error(JSMSG_COLON_AFTER_ID); |
| 12385 |
return errorResult(); |
| 12386 |
} |
| 12387 |
|
| 12388 |
// Here we set a pending error so that later in the parse, |
| 12389 |
// once we've determined whether or not we're |
| 12390 |
// destructuring, the error can be reported or ignored |
| 12391 |
// appropriately. |
| 12392 |
possibleError->setPendingExpressionErrorAt(pos(), |
| 12393 |
JSMSG_COLON_AFTER_ID); |
| 12394 |
} |
| 12395 |
|
| 12396 |
if (const char* chars = nameIsArgumentsOrEval(lhs)) { |
| 12397 |
// |chars| is "arguments" or "eval" here. |
| 12398 |
if (!strictModeErrorAt(namePos.begin, JSMSG_BAD_STRICT_ASSIGN, |
| 12399 |
chars)) { |
| 12400 |
return errorResult(); |
| 12401 |
} |
| 12402 |
} |
| 12403 |
|
| 12404 |
if (handler_.isArgumentsLength(lhs)) { |
| 12405 |
pc_->sc()->setIneligibleForArgumentsLength(); |
| 12406 |
} |
| 12407 |
|
| 12408 |
Node rhs; |
| 12409 |
MOZ_TRY_VAR(rhs, |
| 12410 |
assignExpr(InAllowed, yieldHandling, TripledotProhibited)); |
| 12411 |
|
| 12412 |
BinaryNodeType propExpr; |
| 12413 |
MOZ_TRY_VAR(propExpr, handler_.newAssignment(ParseNodeKind::AssignExpr, |
| 12414 |
lhs, rhs)); |
| 12415 |
|
| 12416 |
if (!handler_.addPropertyDefinition(literal, propName, propExpr)) { |
| 12417 |
return errorResult(); |
| 12418 |
} |
| 12419 |
} else { |
| 12420 |
TaggedParserAtomIndex funName; |
| 12421 |
bool hasStaticName = |
| 12422 |
!anyChars.isCurrentTokenType(TokenKind::RightBracket) && propAtom; |
| 12423 |
if (hasStaticName) { |
| 12424 |
funName = propAtom; |
| 12425 |
|
| 12426 |
if (propType == PropertyType::Getter || |
| 12427 |
propType == PropertyType::Setter) { |
| 12428 |
funName = prefixAccessorName(propType, propAtom); |
| 12429 |
if (!funName) { |
| 12430 |
return errorResult(); |
| 12431 |
} |
| 12432 |
} |
| 12433 |
} |
| 12434 |
|
| 12435 |
FunctionNodeType funNode; |
| 12436 |
MOZ_TRY_VAR(funNode, |
| 12437 |
methodDefinition(namePos.begin, propType, funName)); |
| 12438 |
|
| 12439 |
AccessorType atype = ToAccessorType(propType); |
| 12440 |
if (!handler_.addObjectMethodDefinition(literal, propName, funNode, |
| 12441 |
atype)) { |
| 12442 |
return errorResult(); |
| 12443 |
} |
| 12444 |
|
| 12445 |
if (possibleError) { |
| 12446 |
possibleError->setPendingDestructuringErrorAt( |
| 12447 |
namePos, JSMSG_BAD_DESTRUCT_TARGET); |
| 12448 |
} |
| 12449 |
} |
| 12450 |
} |
| 12451 |
|
| 12452 |
bool matched; |
| 12453 |
if (!tokenStream.matchToken(&matched, TokenKind::Comma, |
| 12454 |
TokenStream::SlashIsInvalid)) { |
| 12455 |
return errorResult(); |
| 12456 |
} |
| 12457 |
if (!matched) { |
| 12458 |
break; |
| 12459 |
} |
| 12460 |
if (tt == TokenKind::TripleDot && possibleError) { |
| 12461 |
possibleError->setPendingDestructuringErrorAt(pos(), |
| 12462 |
JSMSG_REST_WITH_COMMA); |
| 12463 |
} |
| 12464 |
} |
| 12465 |
|
| 12466 |
if (!mustMatchToken( |
| 12467 |
TokenKind::RightCurly, [this, openedPos](TokenKind actual) { |
| 12468 |
this->reportMissingClosing(JSMSG_CURLY_AFTER_LIST, |
| 12469 |
JSMSG_CURLY_OPENED, openedPos); |
| 12470 |
})) { |
| 12471 |
return errorResult(); |
| 12472 |
} |
| 12473 |
|
| 12474 |
handler_.setEndPosition(literal, pos().end); |
| 12475 |
return literal; |
| 12476 |
} |
| 12477 |
|
| 12478 |
template <class ParseHandler, typename Unit> |
| 12479 |
typename ParseHandler::FunctionNodeResult |
| 12480 |
GeneralParser<ParseHandler, Unit>::methodDefinition( |
| 12481 |
uint32_t toStringStart, PropertyType propType, |
| 12482 |
TaggedParserAtomIndex funName) { |
| 12483 |
FunctionSyntaxKind syntaxKind; |
| 12484 |
switch (propType) { |
| 12485 |
case PropertyType::Getter: |
| 12486 |
syntaxKind = FunctionSyntaxKind::Getter; |
| 12487 |
break; |
| 12488 |
|
| 12489 |
case PropertyType::Setter: |
| 12490 |
syntaxKind = FunctionSyntaxKind::Setter; |
| 12491 |
break; |
| 12492 |
|
| 12493 |
case PropertyType::Method: |
| 12494 |
case PropertyType::GeneratorMethod: |
| 12495 |
case PropertyType::AsyncMethod: |
| 12496 |
case PropertyType::AsyncGeneratorMethod: |
| 12497 |
syntaxKind = FunctionSyntaxKind::Method; |
| 12498 |
break; |
| 12499 |
|
| 12500 |
case PropertyType::Constructor: |
| 12501 |
syntaxKind = FunctionSyntaxKind::ClassConstructor; |
| 12502 |
break; |
| 12503 |
|
| 12504 |
case PropertyType::DerivedConstructor: |
| 12505 |
syntaxKind = FunctionSyntaxKind::DerivedClassConstructor; |
| 12506 |
break; |
| 12507 |
|
| 12508 |
default: |
| 12509 |
MOZ_CRASH("unexpected property type"); |
| 12510 |
} |
| 12511 |
|
| 12512 |
GeneratorKind generatorKind = (propType == PropertyType::GeneratorMethod || |
| 12513 |
propType == PropertyType::AsyncGeneratorMethod) |
| 12514 |
? GeneratorKind::Generator |
| 12515 |
: GeneratorKind::NotGenerator; |
| 12516 |
|
| 12517 |
FunctionAsyncKind asyncKind = (propType == PropertyType::AsyncMethod || |
| 12518 |
propType == PropertyType::AsyncGeneratorMethod) |
| 12519 |
? FunctionAsyncKind::AsyncFunction |
| 12520 |
: FunctionAsyncKind::SyncFunction; |
| 12521 |
|
| 12522 |
YieldHandling yieldHandling = GetYieldHandling(generatorKind); |
| 12523 |
|
| 12524 |
FunctionNodeType funNode; |
| 12525 |
MOZ_TRY_VAR(funNode, handler_.newFunction(syntaxKind, pos())); |
| 12526 |
|
| 12527 |
return functionDefinition(funNode, toStringStart, InAllowed, yieldHandling, |
| 12528 |
funName, syntaxKind, generatorKind, asyncKind); |
| 12529 |
} |
| 12530 |
|
| 12531 |
template <class ParseHandler, typename Unit> |
| 12532 |
bool GeneralParser<ParseHandler, Unit>::tryNewTarget( |
| 12533 |
NewTargetNodeType* newTarget) { |
| 12534 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::New)); |
| 12535 |
|
| 12536 |
*newTarget = null(); |
| 12537 |
|
| 12538 |
NullaryNodeType newHolder; |
| 12539 |
MOZ_TRY_VAR_OR_RETURN(newHolder, handler_.newPosHolder(pos()), false); |
| 12540 |
|
| 12541 |
uint32_t begin = pos().begin; |
| 12542 |
|
| 12543 |
// |new| expects to look for an operand, so we will honor that. |
| 12544 |
TokenKind next; |
| 12545 |
if (!tokenStream.getToken(&next, TokenStream::SlashIsRegExp)) { |
| 12546 |
return false; |
| 12547 |
} |
| 12548 |
|
| 12549 |
// Don't unget the token, since lookahead cannot handle someone calling |
| 12550 |
// getToken() with a different modifier. Callers should inspect |
| 12551 |
// currentToken(). |
| 12552 |
if (next != TokenKind::Dot) { |
| 12553 |
return true; |
| 12554 |
} |
| 12555 |
|
| 12556 |
if (!tokenStream.getToken(&next)) { |
| 12557 |
return false; |
| 12558 |
} |
| 12559 |
if (next != TokenKind::Target) { |
| 12560 |
error(JSMSG_UNEXPECTED_TOKEN, "target", TokenKindToDesc(next)); |
| 12561 |
return false; |
| 12562 |
} |
| 12563 |
|
| 12564 |
if (!pc_->sc()->allowNewTarget()) { |
| 12565 |
errorAt(begin, JSMSG_BAD_NEWTARGET); |
| 12566 |
return false; |
| 12567 |
} |
| 12568 |
|
| 12569 |
NullaryNodeType targetHolder; |
| 12570 |
MOZ_TRY_VAR_OR_RETURN(targetHolder, handler_.newPosHolder(pos()), false); |
| 12571 |
|
| 12572 |
NameNodeType newTargetName; |
| 12573 |
MOZ_TRY_VAR_OR_RETURN(newTargetName, newNewTargetName(), false); |
| 12574 |
|
| 12575 |
MOZ_TRY_VAR_OR_RETURN( |
| 12576 |
*newTarget, handler_.newNewTarget(newHolder, targetHolder, newTargetName), |
| 12577 |
false); |
| 12578 |
|
| 12579 |
return true; |
| 12580 |
} |
| 12581 |
|
| 12582 |
template <class ParseHandler, typename Unit> |
| 12583 |
typename ParseHandler::BinaryNodeResult |
| 12584 |
GeneralParser<ParseHandler, Unit>::importExpr(YieldHandling yieldHandling, |
| 12585 |
bool allowCallSyntax) { |
| 12586 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::Import)); |
| 12587 |
|
| 12588 |
NullaryNodeType importHolder; |
| 12589 |
MOZ_TRY_VAR(importHolder, handler_.newPosHolder(pos())); |
| 12590 |
|
| 12591 |
TokenKind next; |
| 12592 |
if (!tokenStream.getToken(&next)) { |
| 12593 |
return errorResult(); |
| 12594 |
} |
| 12595 |
|
| 12596 |
if (next == TokenKind::Dot) { |
| 12597 |
if (!tokenStream.getToken(&next)) { |
| 12598 |
return errorResult(); |
| 12599 |
} |
| 12600 |
if (next != TokenKind::Meta) { |
| 12601 |
error(JSMSG_UNEXPECTED_TOKEN, "meta", TokenKindToDesc(next)); |
| 12602 |
return errorResult(); |
| 12603 |
} |
| 12604 |
|
| 12605 |
if (parseGoal() != ParseGoal::Module) { |
| 12606 |
errorAt(pos().begin, JSMSG_IMPORT_META_OUTSIDE_MODULE); |
| 12607 |
return errorResult(); |
| 12608 |
} |
| 12609 |
|
| 12610 |
NullaryNodeType metaHolder; |
| 12611 |
MOZ_TRY_VAR(metaHolder, handler_.newPosHolder(pos())); |
| 12612 |
|
| 12613 |
return handler_.newImportMeta(importHolder, metaHolder); |
| 12614 |
} |
| 12615 |
|
| 12616 |
if (next == TokenKind::LeftParen && allowCallSyntax) { |
| 12617 |
Node arg; |
| 12618 |
MOZ_TRY_VAR(arg, assignExpr(InAllowed, yieldHandling, TripledotProhibited)); |
| 12619 |
|
| 12620 |
if (!tokenStream.peekToken(&next, TokenStream::SlashIsRegExp)) { |
| 12621 |
return errorResult(); |
| 12622 |
} |
| 12623 |
|
| 12624 |
Node optionalArg; |
| 12625 |
if (options().importAttributes()) { |
| 12626 |
if (next == TokenKind::Comma) { |
| 12627 |
tokenStream.consumeKnownToken(TokenKind::Comma, |
| 12628 |
TokenStream::SlashIsRegExp); |
| 12629 |
|
| 12630 |
if (!tokenStream.peekToken(&next, TokenStream::SlashIsRegExp)) { |
| 12631 |
return errorResult(); |
| 12632 |
} |
| 12633 |
|
| 12634 |
if (next != TokenKind::RightParen) { |
| 12635 |
MOZ_TRY_VAR(optionalArg, assignExpr(InAllowed, yieldHandling, |
| 12636 |
TripledotProhibited)); |
| 12637 |
|
| 12638 |
if (!tokenStream.peekToken(&next, TokenStream::SlashIsRegExp)) { |
| 12639 |
return errorResult(); |
| 12640 |
} |
| 12641 |
|
| 12642 |
if (next == TokenKind::Comma) { |
| 12643 |
tokenStream.consumeKnownToken(TokenKind::Comma, |
| 12644 |
TokenStream::SlashIsRegExp); |
| 12645 |
} |
| 12646 |
} else { |
| 12647 |
MOZ_TRY_VAR(optionalArg, |
| 12648 |
handler_.newPosHolder(TokenPos(pos().end, pos().end))); |
| 12649 |
} |
| 12650 |
} else { |
| 12651 |
MOZ_TRY_VAR(optionalArg, |
| 12652 |
handler_.newPosHolder(TokenPos(pos().end, pos().end))); |
| 12653 |
} |
| 12654 |
} else { |
| 12655 |
MOZ_TRY_VAR(optionalArg, |
| 12656 |
handler_.newPosHolder(TokenPos(pos().end, pos().end))); |
| 12657 |
} |
| 12658 |
|
| 12659 |
if (!mustMatchToken(TokenKind::RightParen, JSMSG_PAREN_AFTER_ARGS)) { |
| 12660 |
return errorResult(); |
| 12661 |
} |
| 12662 |
|
| 12663 |
Node spec; |
| 12664 |
MOZ_TRY_VAR(spec, handler_.newCallImportSpec(arg, optionalArg)); |
| 12665 |
|
| 12666 |
return handler_.newCallImport(importHolder, spec); |
| 12667 |
} |
| 12668 |
|
| 12669 |
error(JSMSG_UNEXPECTED_TOKEN_NO_EXPECT, TokenKindToDesc(next)); |
| 12670 |
return errorResult(); |
| 12671 |
} |
| 12672 |
|
| 12673 |
template <class ParseHandler, typename Unit> |
| 12674 |
typename ParseHandler::NodeResult |
| 12675 |
GeneralParser<ParseHandler, Unit>::primaryExpr( |
| 12676 |
YieldHandling yieldHandling, TripledotHandling tripledotHandling, |
| 12677 |
TokenKind tt, PossibleError* possibleError, InvokedPrediction invoked) { |
| 12678 |
MOZ_ASSERT(anyChars.isCurrentTokenType(tt)); |
| 12679 |
AutoCheckRecursionLimit recursion(this->fc_); |
| 12680 |
if (!recursion.check(this->fc_)) { |
| 12681 |
return errorResult(); |
| 12682 |
} |
| 12683 |
|
| 12684 |
switch (tt) { |
| 12685 |
case TokenKind::Function: |
| 12686 |
return functionExpr(pos().begin, invoked, |
| 12687 |
FunctionAsyncKind::SyncFunction); |
| 12688 |
|
| 12689 |
case TokenKind::Class: |
| 12690 |
return classDefinition(yieldHandling, ClassExpression, NameRequired); |
| 12691 |
|
| 12692 |
case TokenKind::LeftBracket: |
| 12693 |
return arrayInitializer(yieldHandling, possibleError); |
| 12694 |
|
| 12695 |
case TokenKind::LeftCurly: |
| 12696 |
return objectLiteral(yieldHandling, possibleError); |
| 12697 |
|
| 12698 |
#ifdef ENABLE_DECORATORS |
| 12699 |
case TokenKind::At: |
| 12700 |
return classDefinition(yieldHandling, ClassExpression, NameRequired); |
| 12701 |
#endif |
| 12702 |
|
| 12703 |
case TokenKind::LeftParen: { |
| 12704 |
TokenKind next; |
| 12705 |
if (!tokenStream.peekToken(&next, TokenStream::SlashIsRegExp)) { |
| 12706 |
return errorResult(); |
| 12707 |
} |
| 12708 |
|
| 12709 |
if (next == TokenKind::RightParen) { |
| 12710 |
// Not valid expression syntax, but this is valid in an arrow function |
| 12711 |
// with no params: `() => body`. |
| 12712 |
tokenStream.consumeKnownToken(TokenKind::RightParen, |
| 12713 |
TokenStream::SlashIsRegExp); |
| 12714 |
|
| 12715 |
if (!tokenStream.peekToken(&next)) { |
| 12716 |
return errorResult(); |
| 12717 |
} |
| 12718 |
if (next != TokenKind::Arrow) { |
| 12719 |
error(JSMSG_UNEXPECTED_TOKEN, "expression", |
| 12720 |
TokenKindToDesc(TokenKind::RightParen)); |
| 12721 |
return errorResult(); |
| 12722 |
} |
| 12723 |
|
| 12724 |
// Now just return something that will allow parsing to continue. |
| 12725 |
// It doesn't matter what; when we reach the =>, we will rewind and |
| 12726 |
// reparse the whole arrow function. See Parser::assignExpr. |
| 12727 |
return handler_.newNullLiteral(pos()); |
| 12728 |
} |
| 12729 |
|
| 12730 |
// Pass |possibleError| to support destructuring in arrow parameters. |
| 12731 |
Node expr; |
| 12732 |
MOZ_TRY_VAR(expr, exprInParens(InAllowed, yieldHandling, TripledotAllowed, |
| 12733 |
possibleError)); |
| 12734 |
if (!mustMatchToken(TokenKind::RightParen, JSMSG_PAREN_IN_PAREN)) { |
| 12735 |
return errorResult(); |
| 12736 |
} |
| 12737 |
return handler_.parenthesize(expr); |
| 12738 |
} |
| 12739 |
|
| 12740 |
case TokenKind::TemplateHead: |
| 12741 |
return templateLiteral(yieldHandling); |
| 12742 |
|
| 12743 |
case TokenKind::NoSubsTemplate: |
| 12744 |
return noSubstitutionUntaggedTemplate(); |
| 12745 |
|
| 12746 |
case TokenKind::String: |
| 12747 |
return stringLiteral(); |
| 12748 |
|
| 12749 |
default: { |
| 12750 |
if (!TokenKindIsPossibleIdentifier(tt)) { |
| 12751 |
error(JSMSG_UNEXPECTED_TOKEN, "expression", TokenKindToDesc(tt)); |
| 12752 |
return errorResult(); |
| 12753 |
} |
| 12754 |
|
| 12755 |
if (tt == TokenKind::Async) { |
| 12756 |
TokenKind nextSameLine = TokenKind::Eof; |
| 12757 |
if (!tokenStream.peekTokenSameLine(&nextSameLine)) { |
| 12758 |
return errorResult(); |
| 12759 |
} |
| 12760 |
|
| 12761 |
if (nextSameLine == TokenKind::Function) { |
| 12762 |
uint32_t toStringStart = pos().begin; |
| 12763 |
tokenStream.consumeKnownToken(TokenKind::Function); |
| 12764 |
return functionExpr(toStringStart, PredictUninvoked, |
| 12765 |
FunctionAsyncKind::AsyncFunction); |
| 12766 |
} |
| 12767 |
} |
| 12768 |
|
| 12769 |
TaggedParserAtomIndex name = identifierReference(yieldHandling); |
| 12770 |
if (!name) { |
| 12771 |
return errorResult(); |
| 12772 |
} |
| 12773 |
|
| 12774 |
return identifierReference(name); |
| 12775 |
} |
| 12776 |
|
| 12777 |
case TokenKind::RegExp: |
| 12778 |
return newRegExp(); |
| 12779 |
|
| 12780 |
case TokenKind::Number: |
| 12781 |
return newNumber(anyChars.currentToken()); |
| 12782 |
|
| 12783 |
case TokenKind::BigInt: |
| 12784 |
return newBigInt(); |
| 12785 |
|
| 12786 |
case TokenKind::True: |
| 12787 |
return handler_.newBooleanLiteral(true, pos()); |
| 12788 |
case TokenKind::False: |
| 12789 |
return handler_.newBooleanLiteral(false, pos()); |
| 12790 |
case TokenKind::This: { |
| 12791 |
NameNodeType thisName = null(); |
| 12792 |
if (pc_->sc()->hasFunctionThisBinding()) { |
| 12793 |
MOZ_TRY_VAR(thisName, newThisName()); |
| 12794 |
} |
| 12795 |
return handler_.newThisLiteral(pos(), thisName); |
| 12796 |
} |
| 12797 |
case TokenKind::Null: |
| 12798 |
return handler_.newNullLiteral(pos()); |
| 12799 |
|
| 12800 |
case TokenKind::TripleDot: { |
| 12801 |
// This isn't valid expression syntax, but it's valid in an arrow |
| 12802 |
// function as a trailing rest param: `(a, b, ...rest) => body`. Check |
| 12803 |
// if it's directly under |
| 12804 |
// CoverParenthesizedExpressionAndArrowParameterList, and check for a |
| 12805 |
// name, closing parenthesis, and arrow, and allow it only if all are |
| 12806 |
// present. |
| 12807 |
if (tripledotHandling != TripledotAllowed) { |
| 12808 |
error(JSMSG_UNEXPECTED_TOKEN, "expression", TokenKindToDesc(tt)); |
| 12809 |
return errorResult(); |
| 12810 |
} |
| 12811 |
|
| 12812 |
TokenKind next; |
| 12813 |
if (!tokenStream.getToken(&next)) { |
| 12814 |
return errorResult(); |
| 12815 |
} |
| 12816 |
|
| 12817 |
if (next == TokenKind::LeftBracket || next == TokenKind::LeftCurly) { |
| 12818 |
// Validate, but don't store the pattern right now. The whole arrow |
| 12819 |
// function is reparsed in functionFormalParametersAndBody(). |
| 12820 |
MOZ_TRY(destructuringDeclaration(DeclarationKind::CoverArrowParameter, |
| 12821 |
yieldHandling, next)); |
| 12822 |
} else { |
| 12823 |
// This doesn't check that the provided name is allowed, e.g. if |
| 12824 |
// the enclosing code is strict mode code, any of "let", "yield", |
| 12825 |
// or "arguments" should be prohibited. Argument-parsing code |
| 12826 |
// handles that. |
| 12827 |
if (!TokenKindIsPossibleIdentifier(next)) { |
| 12828 |
error(JSMSG_UNEXPECTED_TOKEN, "rest argument name", |
| 12829 |
TokenKindToDesc(next)); |
| 12830 |
return errorResult(); |
| 12831 |
} |
| 12832 |
} |
| 12833 |
|
| 12834 |
if (!tokenStream.getToken(&next)) { |
| 12835 |
return errorResult(); |
| 12836 |
} |
| 12837 |
if (next != TokenKind::RightParen) { |
| 12838 |
error(JSMSG_UNEXPECTED_TOKEN, "closing parenthesis", |
| 12839 |
TokenKindToDesc(next)); |
| 12840 |
return errorResult(); |
| 12841 |
} |
| 12842 |
|
| 12843 |
if (!tokenStream.peekToken(&next)) { |
| 12844 |
return errorResult(); |
| 12845 |
} |
| 12846 |
if (next != TokenKind::Arrow) { |
| 12847 |
// Advance the scanner for proper error location reporting. |
| 12848 |
tokenStream.consumeKnownToken(next); |
| 12849 |
error(JSMSG_UNEXPECTED_TOKEN, "'=>' after argument list", |
| 12850 |
TokenKindToDesc(next)); |
| 12851 |
return errorResult(); |
| 12852 |
} |
| 12853 |
|
| 12854 |
anyChars.ungetToken(); // put back right paren |
| 12855 |
|
| 12856 |
// Return an arbitrary expression node. See case TokenKind::RightParen |
| 12857 |
// above. |
| 12858 |
return handler_.newNullLiteral(pos()); |
| 12859 |
} |
| 12860 |
} |
| 12861 |
} |
| 12862 |
|
| 12863 |
template <class ParseHandler, typename Unit> |
| 12864 |
typename ParseHandler::NodeResult |
| 12865 |
GeneralParser<ParseHandler, Unit>::exprInParens( |
| 12866 |
InHandling inHandling, YieldHandling yieldHandling, |
| 12867 |
TripledotHandling tripledotHandling, |
| 12868 |
PossibleError* possibleError /* = nullptr */) { |
| 12869 |
MOZ_ASSERT(anyChars.isCurrentTokenType(TokenKind::LeftParen)); |
| 12870 |
return expr(inHandling, yieldHandling, tripledotHandling, possibleError, |
| 12871 |
PredictInvoked); |
| 12872 |
} |
| 12873 |
|
| 12874 |
template class PerHandlerParser<FullParseHandler>; |
| 12875 |
template class PerHandlerParser<SyntaxParseHandler>; |
| 12876 |
template class GeneralParser<FullParseHandler, Utf8Unit>; |
| 12877 |
template class GeneralParser<SyntaxParseHandler, Utf8Unit>; |
| 12878 |
template class GeneralParser<FullParseHandler, char16_t>; |
| 12879 |
template class GeneralParser<SyntaxParseHandler, char16_t>; |
| 12880 |
template class Parser<FullParseHandler, Utf8Unit>; |
| 12881 |
template class Parser<SyntaxParseHandler, Utf8Unit>; |
| 12882 |
template class Parser<FullParseHandler, char16_t>; |
| 12883 |
template class Parser<SyntaxParseHandler, char16_t>; |
| 12884 |
|
| 12885 |
} // namespace js::frontend |