Repository navigation
[JSC] The line starts of a source come with its code, and positions are kept #734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
da7256c
[JSC] Line start table: about one byte per line, an embedder can supp…
dylan-conway 5cdec80
Keep the position in the text, which every source that shares the cod…
dylan-conway 7c233f1
[JSC] The line starts of a source come with its code
dylan-conway 40f2c0f
line-start-table-positions.js: get the line of a syntax error from a …
dylan-conway 4e54cc3
[JSC] U+2028 and U+2029 in a string get to shiftLineTerminator() in e…
dylan-conway 8662e2b
[JSC] The line starts come from a scan of the text when its code is c…
dylan-conway 9e20a36
[JSC] The rest of the last commit: the scan, the table and the tests
dylan-conway 3f5d317
builtin-position-counts-from-the-builtin.js: a build with assertions …
dylan-conway af5518a
[JSC] The lexer notes where the lines start, and nothing scans a sour…
dylan-conway File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
JSTests/stress/builtin-position-counts-from-the-builtin.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| //@ runDefault("--useDollarVM=1") | ||
|
|
||
| // The builtins share one text, and none of them knows of the others: a line and column in a builtin counts from where | ||
| // the builtin starts. Finding it does not read the rest of the text. | ||
|
|
||
| function shouldBe(actual, expected, message) { | ||
| if (actual !== expected) | ||
| throw new Error(`${message}: expected ${expected} but got ${actual}`); | ||
| } | ||
|
|
||
| function thrownBy(f) { | ||
| try { | ||
| f(); | ||
| } catch (e) { | ||
| return e; | ||
| } | ||
| throw new Error("did not throw"); | ||
| } | ||
|
|
||
| // A builtin has no expression info, so an error in it is where its parameters start: after "(function ". | ||
| for (const f of [() => [].reduce(() => { }), () => [].reduceRight(() => { }), () => Array.prototype.map.call(null)]) { | ||
| const error = thrownBy(f); | ||
| shouldBe(`${error.line}:${error.column}`, "1:11", String(f)); | ||
| } | ||
|
|
||
| shouldBe($vm.lineStartTableIsBuilt(Array.prototype.reduce), false, "no table"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| //@ runDefault("--useDollarVM=1") | ||
| //@ runBytecodeCache | ||
| //@ runBytecodeCache("--diskCachePayloadIsPersistentForTesting=1") | ||
|
|
||
| // A source's line start table is made when its code is compiled, and the code has it too, and so does its bytecode: in | ||
| // the second run of the last two configurations this file's text is not read. Either way, asking for a line or column | ||
| // later does not read the source. | ||
|
|
||
| function shouldBe(actual, expected, message) { | ||
| if (actual !== expected) | ||
| throw new Error(`${message}: expected ${expected} but got ${actual}`); | ||
| } | ||
|
|
||
| function declared(a, b) { return a + b; } | ||
|
|
||
| class Klass { | ||
| method() { return 1; } | ||
| } | ||
|
|
||
| shouldBe($vm.lineStartTableIsBuilt(declared), true, "a function of this file"); | ||
| shouldBe($vm.lineStartTableIsBuilt(Klass.prototype.method), true, "a method of this file"); | ||
| shouldBe(new Error().line, 22, "a line of this file"); | ||
|
|
||
| // Each of these has a source of its own. | ||
| const long = `/* ${"x".repeat(1024)} */\n`; | ||
| shouldBe($vm.lineStartTableIsBuilt((0, eval)(long + "(function () { })")), true, "indirect eval"); | ||
| shouldBe($vm.lineStartTableIsBuilt(eval(long + "(function () { })")), true, "direct eval"); | ||
|
|
||
| // The same text again is not compiled again: the code is shared, and the new source gets its table from the code. | ||
| shouldBe($vm.lineStartTableIsBuilt((0, eval)(long + "(function () { })")), true, "indirect eval of the same text"); | ||
|
|
||
| // A table is not worth its cost to a short source, which has none until it is asked for a position. | ||
| const short = (0, eval)("(function () {\n return new Error().line;\n})"); | ||
| shouldBe($vm.lineStartTableIsBuilt(short), false, "a short source"); | ||
| shouldBe(short(), 2, "a line of a short source"); | ||
| shouldBe($vm.lineStartTableIsBuilt(short), true, "a short source that was asked"); | ||
|
|
||
| // The builtins need no table. | ||
| shouldBe($vm.lineStartTableIsBuilt(Array.prototype.map), false, "a builtin"); |
82 changes: 0 additions & 82 deletions
82
JSTests/stress/line-start-table-not-built-by-ordinary-parse.js
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // The line and column of an error come from its source's line start table. This compares them with | ||
| // positions counted here, for every kind of line terminator, for line lengths on both sides of what | ||
| // one, two and three bytes of the table's length encoding hold, and for line counts around its block size. | ||
|
|
||
| function shouldBe(actual, expected, message) { | ||
| if (actual !== expected) | ||
| throw new Error(`${message}: expected ${expected} but got ${actual}`); | ||
| } | ||
|
|
||
| let seed = 12345; | ||
| function random(n) { | ||
| seed = (seed * 1103515245 + 12345) & 0x7fffffff; | ||
| return (seed >>> 12) % n; | ||
| } | ||
|
|
||
| globalThis.seen = []; | ||
| globalThis.at = function (error) { | ||
| seen.push(`${error.line}:${error.column}`); | ||
| }; | ||
|
|
||
| function positionsOf(text) { | ||
| seen.length = 0; | ||
| (0, eval)(text); | ||
| return seen.join(" "); | ||
| } | ||
|
|
||
| // One-based, for ascending offsets, by reading the text one character at a time. | ||
| function positionsAt(text, offsets) { | ||
| const positions = []; | ||
| let line = 1; | ||
| let lineStart = 0; | ||
| let i = 0; | ||
| for (const offset of offsets) { | ||
| for (; i < offset; i++) { | ||
| const c = text[i]; | ||
| if (c === "\r" && text[i + 1] === "\n") | ||
| i++; | ||
| else if (c !== "\n" && c !== "\r" && c !== "\u2028" && c !== "\u2029") | ||
| continue; | ||
| line++; | ||
| lineStart = i + 1; | ||
| } | ||
| positions.push(`${line}:${offset - lineStart + 1}`); | ||
| } | ||
| return positions.join(" "); | ||
| } | ||
|
|
||
| // Where in `at(new Error("..."));` the column of the error is. | ||
| const columnInStatement = Number(positionsOf(`at(new Error(""));`).split(":")[1]) - 1; | ||
|
|
||
| const terminators = { | ||
| "LF": ["\n"], | ||
| "CR LF": ["\r\n"], | ||
| "CR": ["\r"], | ||
| "LF, CR LF and CR": ["\n", "\r\n", "\r"], | ||
| "all five, 16-bit": ["\n", "\r\n", "\r", "\u2028", "\u2029"], | ||
| }; | ||
|
|
||
| const lengths = [100, 300, 16000, 17000, 40000]; | ||
|
|
||
| for (const [name, choices] of Object.entries(terminators)) { | ||
| const terminator = () => choices[random(choices.length)]; | ||
| let text = ""; | ||
| const offsets = []; | ||
| for (let i = 0; i < 150; i++) { | ||
| for (let blank = random(4); blank > 0; blank--) | ||
| text += terminator(); | ||
| text += " ".repeat(random(12)); | ||
| offsets.push(text.length + columnInStatement); | ||
| text += `at(new Error("${"x".repeat(i < lengths.length ? lengths[i] : random(60))}"));` + terminator(); | ||
| } | ||
| const expected = positionsAt(text, offsets); | ||
| shouldBe(positionsOf(text), expected, name); | ||
|
dylan-conway marked this conversation as resolved.
Resolved
|
||
| // Again: the answers are kept. | ||
| shouldBe(positionsOf(text), expected, `${name}, second time`); | ||
| } | ||
|
|
||
| // A line terminator ends a line wherever it is: in a comment, in a template, after a backslash in a string, and, for | ||
| // U+2028 and U+2029, in a string as it is. That holds in the body of a function too, which is only checked for syntax | ||
| // while what is around it is parsed. | ||
| const tokens = [ | ||
| t => `/* a${t}b${t}${t}c */`, | ||
| t => "`a" + t + "${" + t + "1}" + t + "b`;", | ||
| t => `"a\\${t}b";`, | ||
| t => t === "\u2028" || t === "\u2029" ? `"a${t}b"; a = ['${t}', ("\\n${t}${t}")];` : `"ab";`, | ||
| ]; | ||
|
|
||
| for (const [name, choices] of Object.entries(terminators)) { | ||
| for (const t of choices) { | ||
| for (const inFunction of [false, true]) { | ||
| // 16-bit where the terminators of the set are, whichever of them this is. | ||
| let text = (name.includes("16-bit") ? "// \u4e16" + t : "") + "var a;" + t; | ||
| const offsets = []; | ||
| for (let i = 0; i < 40; i++) { | ||
| const token = tokens[i % tokens.length](t); | ||
| text += (inFunction ? `function inner() {${t}${token}${t}}` : token) + t; | ||
| offsets.push(text.length + columnInStatement); | ||
| text += `at(new Error(""));` + t; | ||
| } | ||
| shouldBe(positionsOf(text), positionsAt(text, offsets), `tokens${inFunction ? " in a function" : ""}, ${name}, ${escape(t)}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // The line of a syntax error is asked for before there is any code. | ||
| for (const before of [0, 1, 63, 64, 200]) { | ||
| const line = "var someName = 12345678;\n"; | ||
| // It only parses, and throws a string that ends with the line. The error of eval() has the line of the call, and | ||
| // a program that does not compile is not for the bytecode cache configuration, where every program has to be cached. | ||
| let message; | ||
| try { | ||
| checkModuleSyntax(line.repeat(before) + "var = ;\n" + line.repeat(100)); | ||
| } catch (e) { | ||
| message = e; | ||
| } | ||
| shouldBe(/^SyntaxError: .*:(\d+)$/.exec(message)?.[1], String(before + 1), `a syntax error after ${before} lines`); | ||
| } | ||
|
|
||
| // A first line whose length, terminator included, is the most that one, two and three bytes hold, and one more. | ||
| for (const length of [127, 128, 16383, 16384, 2097151, 2097152]) { | ||
| const text = `"${"x".repeat(length - 4)}";\n` + `at(new Error(""));`; | ||
| shouldBe(text.indexOf("\n") + 1, length, "length of the first line"); | ||
| shouldBe(positionsOf(text), `2:${columnInStatement + 1}`, `after a line of ${length}`); | ||
| } | ||
|
|
||
| for (const lines of [1, 2, 63, 64, 65, 127, 128, 129, 130]) { | ||
| for (const endsWithTerminator of [false, true]) { | ||
| const text = "\n".repeat(lines - 1) + `at(new Error(""));` + (endsWithTerminator ? "\n" : ""); | ||
| shouldBe(positionsOf(text), `${lines}:${columnInStatement + 1}`, `${lines} lines`); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.