Skip to content

Commit f88678d

Browse files
committed
[JSC] Import and export declarations should not redeclare a var in module code
https://bugs.webkit.org/show_bug.cgi?id=327927 Reviewed by Yusuke Suzuki. It is a Syntax Error if a LexicallyDeclaredName of a module also occurs in its VarDeclaredNames [1], but JSC accepted the following when the var came first. var x; import { y as x } from "./m.js"; var x; export let x; var x; export class x { } Debug builds then hit ASSERT(symbolTableEntry.isNull()) in BytecodeGenerator::instantiateLexicalVariables. Parser::declareVariable compares a lexical name with vars only when m_statementDepth is 1, and import and export declarations were parsed at depth 0. This patch parses them at depth 1, the depth of top-level statements, by incrementing it in parseImportDeclaration and parseExportDeclaration as parseStatementListItem does. Setting the depth to 1 only for exported function declarations is no longer needed. [1]: https://tc39.es/ecma262/#sec-module-semantics-static-semantics-early-errors Tests: JSTests/stress/bigint-to-string-divide-and-conquer-large.js JSTests/stress/modules-syntax-error-import-and-export-declarations-after-var.js * JSTests/stress/bigint-to-string-divide-and-conquer-large.js: Added. (shouldBe): * JSTests/stress/modules-syntax-error-import-and-export-declarations-after-var.js: Added. (shouldThrowModuleSyntaxError): (export.x): (export.async x): (export.default.x): (export.default.async x): (const.declaration.of.declarations.const.statement.of.f): (export.default): (export.C): (export.let.y): * JSTests/stress/modules-syntax-error-with-names.js: * Source/JavaScriptCore/parser/Parser.cpp: (JSC::Parser<LexerType>::parseImportDeclaration): (JSC::Parser<LexerType>::parseExportDeclaration): Canonical link: https://commits.webkit.org/324223@main
1 parent 5d5248a commit f88678d

4 files changed

Lines changed: 96 additions & 22 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function shouldBe(actual, expected, message) {
2+
if (actual !== expected)
3+
throw new Error(`${message}: expected ${expected.slice(0, 40)}... (${expected.length}) but got ${actual.slice(0, 40)}... (${actual.length})`);
4+
}
5+
6+
function check(radix, widths) {
7+
const r = BigInt(radix);
8+
const last = (radix - 1).toString(radix);
9+
let x = 0n;
10+
let expected = "";
11+
for (let i = 0; i < widths.length; i++) {
12+
const power = r ** BigInt(widths[i]);
13+
if (i & 1) {
14+
x = x * power + 1n;
15+
expected += "0".repeat(widths[i] - 1) + "1";
16+
} else {
17+
x = x * power + power - 1n;
18+
expected += last.repeat(widths[i]);
19+
}
20+
}
21+
shouldBe(x.toString(radix), expected, `radix ${radix}, ${widths}`);
22+
}
23+
24+
check(10, [100000, 60001, 19, 77777, 1, 77202]);
25+
check(3, [40, 70000, 12345, 1, 50000, 28614]);
26+
check(24, [30000, 13, 9987, 16000]);
27+
check(36, [1, 49999, 25000, 12, 24988]);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
function shouldThrowModuleSyntaxError(source) {
2+
let error;
3+
try {
4+
checkModuleSyntax(source);
5+
} catch (e) {
6+
error = e;
7+
}
8+
if (!String(error).startsWith("SyntaxError"))
9+
throw new Error(`\`${source}\` did not throw a SyntaxError: ${String(error)}`);
10+
}
11+
12+
const declarations = [
13+
`import x from "./m.js";`,
14+
`import * as x from "./m.js";`,
15+
`import { x } from "./m.js";`,
16+
`import { y as x } from "./m.js";`,
17+
`export let x;`,
18+
`export let [x] = [];`,
19+
`export const x = 1;`,
20+
`export class x { }`,
21+
`export function x() { }`,
22+
`export async function x() { }`,
23+
`export default class x { }`,
24+
`export default function x() { }`,
25+
`export default async function x() { }`,
26+
];
27+
28+
for (const declaration of declarations) {
29+
for (const statement of [`var x;`, `{ var x; }`]) {
30+
shouldThrowModuleSyntaxError(`${statement} ${declaration}`);
31+
shouldThrowModuleSyntaxError(`${declaration} ${statement}`);
32+
}
33+
for (const statement of [`function f() { var x; }`, `{ let x; }`]) {
34+
checkModuleSyntax(`${statement} ${declaration}`);
35+
checkModuleSyntax(`${declaration} ${statement}`);
36+
}
37+
}
38+
39+
const notDeclaringX = [
40+
`import { x as y } from "./m.js";`,
41+
`export { x };`,
42+
`export * as x from "./m.js";`,
43+
`export { y as x } from "./m.js";`,
44+
`export default x;`,
45+
`export default (class x { });`,
46+
`export default function () { let x; }`,
47+
`export class C { static { let x; } }`,
48+
`export let y = x;`,
49+
`export let { x: y } = {};`,
50+
`export let y = function x() { };`,
51+
];
52+
53+
for (const declaration of notDeclaringX) {
54+
checkModuleSyntax(`var x; ${declaration}`);
55+
checkModuleSyntax(`${declaration} var x;`);
56+
}

‎JSTests/stress/modules-syntax-error-with-names.js‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -228,28 +228,24 @@ export { a as Cocoa };
228228
export class Cocoa { }
229229
`, `SyntaxError: Cannot export a duplicate class name: 'Cocoa'.:5`);
230230

231-
// FIXME: These tests also should be passed. But now, var and lexical declared names can be co-exist on Script / Module top level scope.
232-
// This will be fixed when this issue is fixed for Script environment.
233-
// http://www.ecma-international.org/ecma-262/6.0/#sec-scripts-static-semantics-early-errors
234-
// http://www.ecma-international.org/ecma-262/6.0/#sec-module-semantics-static-semantics-early-errors
235231
checkModuleSyntaxError(String.raw`
236232
import A from "Cocoa"
237233
var A = 20;
238234
`, `SyntaxError: Cannot declare a var variable that shadows a let/const/class variable: 'A'.:3`);
239235

240-
// checkModuleSyntaxError(String.raw`
241-
// var A = 20;
242-
// import A from "Cocoa"
243-
// `, ``);
236+
checkModuleSyntaxError(String.raw`
237+
var A = 20;
238+
import A from "Cocoa"
239+
`, `SyntaxError: Cannot declare an imported binding name twice: 'A'.:3`);
244240

245241
checkModuleSyntaxError(String.raw`
246242
import A from "Cocoa"
247243
var A = 20;
248244
`, `SyntaxError: Cannot declare a var variable that shadows a let/const/class variable: 'A'.:3`);
249245

250-
// checkModuleSyntaxError(String.raw`
251-
// var A = 20;
252-
// import A from "Cocoa"
253-
// `, ``);
246+
checkModuleSyntaxError(String.raw`
247+
var A = 20;
248+
import A from "Cocoa"
249+
`, `SyntaxError: Cannot declare an imported binding name twice: 'A'.:3`);
254250

255251

‎Source/JavaScriptCore/parser/Parser.cpp‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3764,6 +3764,8 @@ template <typename LexerType>
37643764
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseImportDeclaration(TreeBuilder& context)
37653765
{
37663766
// http://www.ecma-international.org/ecma-262/6.0/#sec-imports
3767+
DepthManager statementDepth(&m_statementDepth);
3768+
m_statementDepth++;
37673769
ASSERT(match(IMPORT));
37683770
JSTokenLocation importLocation(tokenLocation());
37693771
next();
@@ -3902,6 +3904,8 @@ template <typename LexerType>
39023904
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseExportDeclaration(TreeBuilder& context)
39033905
{
39043906
// http://www.ecma-international.org/ecma-262/6.0/#sec-exports
3907+
DepthManager statementDepth(&m_statementDepth);
3908+
m_statementDepth++;
39053909
ASSERT(match(EXPORT_));
39063910
JSTokenLocation exportLocation(tokenLocation());
39073911
next();
@@ -3997,17 +4001,13 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseExportDeclara
39974001
if (isFunctionOrClassDeclaration) {
39984002
if (startsWithFunction) {
39994003
ASSERT(match(FUNCTION));
4000-
DepthManager statementDepth(&m_statementDepth);
4001-
m_statementDepth = 1;
40024004
result = parseFunctionDeclaration(context, FunctionDeclarationType::Declaration, ExportType::NotExported, DeclarationDefaultContext::ExportDefault);
40034005
} else if (match(CLASSTOKEN)) {
40044006
result = parseClassDeclaration(context, ExportType::NotExported, DeclarationDefaultContext::ExportDefault);
40054007
} else {
40064008
ASSERT(matchContextualKeyword(m_vm.propertyNames->async));
40074009
unsigned functionStart = m_token.m_startPosition;
40084010
next();
4009-
DepthManager statementDepth(&m_statementDepth);
4010-
m_statementDepth = 1;
40114011
result = parseAsyncFunctionDeclaration(context, functionStart, ExportType::NotExported, DeclarationDefaultContext::ExportDefault);
40124012
}
40134013
} else {
@@ -4123,12 +4123,9 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseExportDeclara
41234123
result = parseVariableDeclaration(context, DeclarationType::LetDeclaration, ExportType::Exported);
41244124
break;
41254125

4126-
case FUNCTION: {
4127-
DepthManager statementDepth(&m_statementDepth);
4128-
m_statementDepth = 1;
4126+
case FUNCTION:
41294127
result = parseFunctionDeclaration(context, FunctionDeclarationType::Declaration, ExportType::Exported);
41304128
break;
4131-
}
41324129

41334130
case CLASSTOKEN:
41344131
result = parseClassDeclaration(context, ExportType::Exported);
@@ -4139,8 +4136,6 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseExportDeclara
41394136
unsigned functionStart = m_token.m_startPosition;
41404137
next();
41414138
semanticFailIfFalse(match(FUNCTION) && !m_lexer->hasLineTerminatorBeforeToken(), "Expected 'function' keyword following 'async' keyword with no preceding line terminator");
4142-
DepthManager statementDepth(&m_statementDepth);
4143-
m_statementDepth = 1;
41444139
result = parseAsyncFunctionDeclaration(context, functionStart, ExportType::Exported);
41454140
break;
41464141
}

0 commit comments

Comments
 (0)