Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// The parameters of an arrow function in a generator are parsed with [+Yield]: `yield` is not an identifier in them, and a
// YieldExpression is an early error. That holds for an arrow function in the parameters of another arrow function too.
//
// The parser first reads "( ... )" as an expression. To see that it is a parameter list it parses it again, in a scope
// that is never a generator, and an arrow function in there goes to the source provider cache with `yield` as an
// identifier. The parse that knows about the generator then skipped that arrow function from the cache, so all of the
// code below that must throw was accepted. It was always rejected with --useSourceProviderCache=false, and the messages
// here are the ones that run gives.

function shouldBe(actual, expected, what) {
if (actual !== expected)
throw new Error(`${what}: expected ${String(expected)} but got ${String(actual)}`);
}

function shouldThrowSyntaxError(source, message) {
let error;
try {
(0, eval)(source);
} catch (e) {
error = e;
}
if (!(error instanceof SyntaxError))
throw new Error(`${source}: expected a SyntaxError but got ${String(error)}`);
if (message !== undefined)
shouldBe(error.message, message, source);
}

function shouldParse(source) {
try {
(0, eval)(source);
} catch (e) {
if (e instanceof SyntaxError)
throw new Error(`${source}: ${String(e)}`);
}
}

const parameterName = "Cannot use 'yield' as a parameter name in a generator function.";
const yieldExpression = "Unexpected keyword 'yield'. Cannot use yield expression out of generator.";
const shorthand = "Cannot use abbreviated destructuring syntax for keyword 'yield'.";

// [text before the expression, text after it]. The messages below are those of the first one.
const generators = [
["(function* () { (", "); })"],
["(function* () { return ", "; })"],
["(function* (p = ", ") { })"],
["function* g() { (", "); }"],
["({ *g() { (", "); } })"],
["({ *g(p = ", ") { } })"],
["(class { *g() { (", "); } })", "strict"],
["(class { static *g(p = ", ") { } })", "strict"],
["(async function* () { (", "); })"],
["(async function* (p = ", ") { })"],
["(function* () { { let q; (", "); } })"],
["(function* (p = function* () { (", "); }) { })"],
];

// [expression, message, true if the expression is not valid where `yield` is an identifier either]
const invalid = [
// The arrow function is in the parameters of another arrow function.
["(a = (yield) => 1) => a", parameterName],
["(a = (b = yield) => b) => a", yieldExpression],
["(a = (b = yield 1) => b) => a", "Unexpected number '1'. Expected ')' to end a compound expression.", true],
["(a = (yield = 1) => 1) => a", parameterName],
["(a = (...yield) => 1) => a", parameterName],
["(a = ([yield]) => 1) => a", parameterName],
["(a = ({ yield }) => 1) => a", shorthand],
["(a = ({ b = yield }) => 1) => a", yieldExpression],
["(a = async (yield) => 1) => a", parameterName],
["(a = async (b = yield) => b) => a", yieldExpression],
["(b = (yield) => 1, c) => b", parameterName],
["(c, b = (yield) => 1) => b", parameterName],
["({ a = (yield) => 1 }) => a", parameterName],
["({ k: a = (b = yield) => b }) => a", yieldExpression],
["([a = (yield) => 1]) => a", parameterName],
["(...[a = (yield) => 1]) => a", parameterName],
["async (a = (yield) => 1) => a", parameterName],
["async ({ a = (b = yield) => b }) => a", yieldExpression],
// Deeper.
["(a = (b = (yield) => 1) => b) => a", parameterName],
["(a = (b = (c = yield) => c) => b) => a", yieldExpression],
["(a = (b = (c = (d = (yield) => 1) => d) => c) => b) => a", parameterName],
["({ x = 1 }, a = (b = (c = yield) => c) => b) => a", yieldExpression],
// "( ... )" is not a parameter list, but the parser has to try that to find out. The enclosing pattern is parsed twice.
["[a = (b = (yield) => 1)] = []", parameterName],
["[a = ({ [(yield) => 1]: 1 })] = []", parameterName],
["[a = (b = (c = yield) => c)] = []", yieldExpression],
// One arrow function. These were rejected before, and the messages are the same.
["(yield) => 1", parameterName],
["(a = yield) => a", yieldExpression],
["({ a = yield }) => a", yieldExpression],
["(...[a = yield]) => a", yieldExpression],
];

for (const [before, after] of generators) {
for (const [expression, message] of invalid)
shouldThrowSyntaxError(before + expression + after, before === generators[0][0] ? message : undefined);
}

// `yield` is an identifier in the body of an arrow function, in a function that is not an arrow function, and in an arrow
// function that is in one of those.
const valid = [
"(a = (b) => yield) => a",
"(a = (b) => { var yield; }) => a",
"(a = (b) => { (yield) => 1; }) => a",
"(a = (b) => (c = (yield) => 1) => c) => a",
"(a = function (yield) { yield; }) => a",
"(a = function (b = yield) { }) => a",
"(a = function () { (b = (yield) => 1) => b; }) => a",
"(a = function* () { yield 1; }) => a",
"(a = { yield: 1 }.yield) => a",
"({ x = 1 }, a = (b = (c = 1) => c) => b) => a",
"[a = (b = (c = 1) => c)] = []",
];

for (const [before, after, strict] of generators) {
// `yield` is a reserved word in strict mode code.
if (strict)
continue;
for (const expression of valid)
shouldParse(before + expression + after);
}

// Outside a generator `yield` is an identifier in sloppy mode, at every level.
for (const [expression, , neverValid] of invalid) {
if (neverValid)
continue;
shouldParse(`(function () { (${expression}); })`);
shouldParse(`(function* () { () => { (${expression}); }; })`);
shouldParse(`(function* () { function f() { (${expression}); } })`);
shouldThrowSyntaxError(`(function () { "use strict"; (${expression}); })`);
}

// The arrow functions do what they say, also the ones that the parser had to parse again for the generator.
{
const f = (a = (yield) => yield + 1) => a;
shouldBe(f()(41), 42, "yield as a parameter name");

function* inArrowBody() {
return () => (a = (yield) => yield * 2) => a;
}
shouldBe(inArrowBody().next().value()()(21), 42, "yield as a parameter name in the body of an arrow function in a generator");

// "{ x = 1 }" is not an expression, so the nested arrow functions are first parsed in the scope that is not a generator.
const captures = `(function* (p) {
let local = 10;
const first = yield;
return ({ x = 1 }, a = (b = (c = x + local + p + first) => c) => b) => a;
})`;
for (const dropCache of [false, true]) {
const generator = (0, eval)(captures)(100);
generator.next();
const arrow = generator.next(1000).value;
// A full collection drops the source provider cache. An arrow function is compiled when it is first called, and
// parses its text again then.
if (dropCache)
fullGC();
shouldBe(arrow({ })()(), 1111, "captured variables");
shouldBe(arrow({ x: 2 })()(), 1112, "captured variables");
}

class Base {
name() { return "base"; }
}
class Derived extends Base {
*method() {
return ({ x = 1 }, a = (b = (c = [this.field, arguments[0], super.name(), x]) => c) => b) => a;
}
name() { return "derived"; }
}
const derived = new Derived;
derived.field = "field";
shouldBe(JSON.stringify(derived.method("argument").next().value({ })()()), `["field","argument","base",1]`, "this, arguments and super");

function* withEval(p) {
return ({ x = 1 }, a = (b = (c = eval("x + p")) => c) => b) => a;
}
shouldBe(withEval(41).next().value({ })()(), 42, "eval");
}
12 changes: 11 additions & 1 deletion Source/JavaScriptCore/parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2587,6 +2587,9 @@ template <class TreeBuilder> bool Parser<LexerType>::parseFunctionInfo(TreeBuild

Scope* parentScope = currentScope();

// ArrowParameters[?Yield]: the parameters of an arrow function in a generator are parsed with [+Yield], see below.
const bool parseArrowParametersAsGenerator = SourceParseModeSet(SourceParseMode::ArrowFunctionMode, SourceParseMode::AsyncArrowFunctionMode).contains(mode) && parentScope->isGeneratorFunction();

bool functionNameIsAwait = isPossiblyEscapedAwait(m_token);
const char* isDisallowedAwaitFunctionNameReason = functionNameIsAwait && !canUseIdentifierAwait() ? disallowedIdentifierAwaitReason() : nullptr;

Expand Down Expand Up @@ -2625,6 +2628,12 @@ template <class TreeBuilder> bool Parser<LexerType>::parseFunctionInfo(TreeBuild

// If we know about this function already, we can use the cached info and skip the parser to the end of the function.
if (const SourceProviderCacheItem* cachedInfo = TreeBuilder::CanUseFunctionCache ? findCachedFunctionInfo(parametersStart) : nullptr) {
// isArrowFunctionParameters() parses parameters in a scope that is never a generator, so an arrow function in them is
// parsed, and cached, with `yield` as an identifier. That item does not say that the arrow function is valid in a
// generator. Parse it again here, as without the cache. The new item replaces it.
if (parseArrowParametersAsGenerator && !cachedInfo->arrowParametersParsedAsGenerator)
Comment thread
robobun marked this conversation as resolved.
Resolved
return false;
Comment thread
claude[bot] marked this conversation as resolved.
Resolved

// If we're in a strict context, the cached function info must say it was strict too.
ASSERT(!strictMode() || (cachedInfo->lexicallyScopedFeatures() & StrictModeLexicallyScopedFeature));
JSTokenLocation endLocation;
Expand Down Expand Up @@ -2713,7 +2722,7 @@ template <class TreeBuilder> bool Parser<LexerType>::parseFunctionInfo(TreeBuild
{
// Parse formal parameters with [+Yield] parameterization, in order to ban YieldExpressions
// in ArrowFormalParameters, per ES6 #sec-arrow-function-definitions-static-semantics-early-errors.
Scope::MaybeParseAsGeneratorFunctionForScope parseAsGeneratorFunction(functionScope.scope(), parentScope->isGeneratorFunction());
Scope::MaybeParseAsGeneratorFunctionForScope parseAsGeneratorFunction(functionScope.scope(), parseArrowParametersAsGenerator);
SetForScope overrideAllowAwait(m_parserState.allowAwait, !parentScope->isAsyncFunction() && !isAsyncFunctionParseMode(mode));
parseFunctionParameters(syntaxChecker, functionInfo);
propagateError();
Expand Down Expand Up @@ -2907,6 +2916,7 @@ template <class TreeBuilder> bool Parser<LexerType>::parseFunctionInfo(TreeBuild
parameters.expectedSuperBinding = expectedSuperBinding;
parameters.implementationVisibility = implementationVisibility;
parameters.containsTaggedTemplate = m_seenTaggedTemplateInNonReparsingFunctionMode;
parameters.arrowParametersParsedAsGenerator = parseArrowParametersAsGenerator;
if (functionBodyType == ArrowFunctionBodyExpression) {
parameters.isBodyArrowExpression = true;
parameters.tokenType = m_token.m_type;
Expand Down
3 changes: 2 additions & 1 deletion Source/JavaScriptCore/parser/SourceProviderCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ void SourceProviderCache::clear()

void SourceProviderCache::add(int sourcePosition, std::unique_ptr<SourceProviderCacheItem> item)
{
m_map.add(sourcePosition, WTF::move(item));
// Parser::parseFunctionInfo() parses a function again when it cannot use the item that is here. The new item replaces it.
m_map.set(sourcePosition, WTF::move(item));
}

}
4 changes: 4 additions & 0 deletions Source/JavaScriptCore/parser/SourceProviderCacheItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct SourceProviderCacheItemCreationParameters {
bool needsSuperBinding : 1 { false };
bool isBodyArrowExpression : 1 { false };
bool containsTaggedTemplate : 1 { false };
bool arrowParametersParsedAsGenerator : 1 { false };
// Where the lexer stood after the function's last token, which for a token spanning lines is not lastTokenLine.
unsigned lastTokenEndLine { 0 };
unsigned lastTokenEndLineStartOffset { 0 };
Expand Down Expand Up @@ -123,6 +124,8 @@ class SourceProviderCacheItem final : public TrailingArray<SourceProviderCacheIt
unsigned implementationVisibility : 2; // ImplementationVisibility
bool usesImportMeta : 1 { false };
bool containsTaggedTemplate : 1 { false };
// The parameters of this arrow function were parsed with [+Yield]. If not, nothing says that they are valid in a generator.
bool arrowParametersParsedAsGenerator : 1 { false };
unsigned lastTokenEndLine;
unsigned lastTokenEndLineStartOffset;

Expand Down Expand Up @@ -159,6 +162,7 @@ inline SourceProviderCacheItem::SourceProviderCacheItem(const SourceProviderCach
, implementationVisibility(static_cast<unsigned>(parameters.implementationVisibility))
, usesImportMeta(parameters.usesImportMeta)
, containsTaggedTemplate(parameters.containsTaggedTemplate)
, arrowParametersParsedAsGenerator(parameters.arrowParametersParsedAsGenerator)
, lastTokenEndLine(parameters.lastTokenEndLine)
, lastTokenEndLineStartOffset(parameters.lastTokenEndLineStartOffset)
{
Expand Down
Loading