Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 22 additions & 8 deletions lexer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let source, pos, end;
const STACK_DEPTH = 2048;
let openTokenDepth,
templateDepth,
lastTokenPos,
Expand Down Expand Up @@ -37,6 +38,20 @@ const Import = 0;
const ExportAssign = 1;
const ExportStar = 2;

/** @param {number} tokenPos */
function pushOpenToken (tokenPos) {
if (openTokenDepth === STACK_DEPTH)
throw new Error('Maximum nesting depth exceeded.');
openTokenPosStack[openTokenDepth++] = tokenPos;
}

function pushTemplate () {
if (templateStackDepth === STACK_DEPTH || openTokenDepth === STACK_DEPTH)
throw new Error('Maximum nesting depth exceeded.');
templateStack[templateStackDepth++] = templateDepth;
templateDepth = ++openTokenDepth;
}

function parseCJS (source, name = '@') {
resetState();
try {
Expand Down Expand Up @@ -125,7 +140,7 @@ function parseSource (cjsSource) {
pos += 23;
if (source.charCodeAt(pos) === 40/*(*/) {
pos++;
openTokenPosStack[openTokenDepth++] = lastTokenPos;
pushOpenToken(lastTokenPos);
if (tryParseRequire(Import) && keywordStart(startPos)) {
tryBacktrackAddStarExportBinding(startPos - 1);
}
Expand All @@ -136,7 +151,7 @@ function parseSource (cjsSource) {
if (source.startsWith('Star', pos))
pos += 4;
if (source.charCodeAt(pos) === 40/*(*/) {
openTokenPosStack[openTokenDepth++] = lastTokenPos;
pushOpenToken(lastTokenPos);
if (source.charCodeAt(pos + 1) === 114/*r*/) {
pos++;
tryParseRequire(ExportStar);
Expand Down Expand Up @@ -170,17 +185,17 @@ function parseSource (cjsSource) {
tryParseObjectDefineOrKeys(openTokenDepth === 0);
break;
case 40/*(*/:
openTokenPosStack[openTokenDepth++] = lastTokenPos;
pushOpenToken(lastTokenPos);
break;
case 41/*)*/:
if (openTokenDepth === 0)
throw new Error('Unexpected closing bracket.');
openTokenDepth--;
break;
case 123/*{*/:
openClassPosStack[openTokenDepth] = nextBraceIsClass;
pushOpenToken(lastTokenPos);
openClassPosStack[openTokenDepth - 1] = nextBraceIsClass;
nextBraceIsClass = false;
openTokenPosStack[openTokenDepth++] = lastTokenPos;
break;
case 125/*}*/:
if (openTokenDepth === 0)
Expand Down Expand Up @@ -1324,7 +1339,7 @@ function throwIfImportStatement () {
switch (ch) {
// dynamic import
case 40/*(*/:
openTokenPosStack[openTokenDepth++] = startPos;
pushOpenToken(startPos);
return;
// import.meta
case 46/*.*/:
Expand Down Expand Up @@ -1382,8 +1397,7 @@ function templateString () {
const ch = source.charCodeAt(pos);
if (ch === 36/*$*/ && source.charCodeAt(pos + 1) === 123/*{*/) {
pos++;
templateStack[templateStackDepth++] = templateDepth;
templateDepth = ++openTokenDepth;
pushTemplate();
return;
}
if (ch === 96/*`*/)
Expand Down
Binary file modified lib/lexer.wasm
Binary file not shown.
Loading
Loading