Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
26 changes: 26 additions & 0 deletions JSTests/stress/builtin-position-counts-from-the-builtin.js
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");
39 changes: 39 additions & 0 deletions JSTests/stress/line-start-table-comes-with-the-code.js
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 JSTests/stress/line-start-table-not-built-by-ordinary-parse.js

This file was deleted.

131 changes: 131 additions & 0 deletions JSTests/stress/line-start-table-positions.js
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--)
Comment thread
dylan-conway marked this conversation as resolved.
Resolved
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);
Comment thread
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`);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ constinit const JSC::BuiltinSourceMetadata s_JSCBuiltinSourceMetadata[JSC::numbe
};
static_assert(2 == JSC::numberOfBuiltinCodes);

constinit const unsigned s_JSCBuiltinSourceStarts[JSC::numberOfBuiltinCodes] = { 0, 336 };


#define DEFINE_BUILTIN_GENERATOR(codeName, functionName, overriddenName, argumentCount) \
JSC::FunctionExecutable* codeName##Generator(JSC::VM& vm) \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ constinit const JSC::BuiltinSourceMetadata s_JSCBuiltinSourceMetadata[JSC::numbe
};
static_assert(4 == JSC::numberOfBuiltinCodes);

constinit const unsigned s_JSCBuiltinSourceStarts[JSC::numberOfBuiltinCodes] = { 0, 762, 1456, 2694 };


#define DEFINE_BUILTIN_GENERATOR(codeName, functionName, overriddenName, argumentCount) \
JSC::FunctionExecutable* codeName##Generator(JSC::VM& vm) \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ constinit const JSC::BuiltinSourceMetadata s_JSCBuiltinSourceMetadata[JSC::numbe
};
static_assert(2 == JSC::numberOfBuiltinCodes);

constinit const unsigned s_JSCBuiltinSourceStarts[JSC::numberOfBuiltinCodes] = { 0, 2046 };


#define DEFINE_BUILTIN_GENERATOR(codeName, functionName, overriddenName, argumentCount) \
JSC::FunctionExecutable* codeName##Generator(JSC::VM& vm) \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ constinit const JSC::BuiltinSourceMetadata s_JSCBuiltinSourceMetadata[JSC::numbe
};
static_assert(2 == JSC::numberOfBuiltinCodes);

constinit const unsigned s_JSCBuiltinSourceStarts[JSC::numberOfBuiltinCodes] = { 0, 71 };


#define DEFINE_BUILTIN_GENERATOR(codeName, functionName, overriddenName, argumentCount) \
JSC::FunctionExecutable* codeName##Generator(JSC::VM& vm) \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def generate_output(self):
data = self.generate_embedded_code_data_for_function(function)
combinedCode += data['originalSource']
data['embeddedSource'] = "s_%sCombinedCode + %d" % (args['namespace'], combinedCodeOffset)
data['combinedCodeOffset'] = combinedCodeOffset
combinedCodeOffset += data['embeddedSourceLength']
function_data.append(data)

Expand Down Expand Up @@ -97,6 +98,8 @@ def generate_source_metadata_table(self, function_data):
lines.append(" /* %(codeName)s */ { %(sourceLength)d, %(parametersStart)d, %(parameterCount)d, %(closeBraceOffsetFromEnd)d, %(isAsyncFunction)s, %(isInStrictContext)s }," % entry)
lines.append("};")
lines.append("static_assert(%d == JSC::numberOfBuiltinCodes);" % len(function_data))
lines.append("")
lines.append("constinit const unsigned s_JSCBuiltinSourceStarts[JSC::numberOfBuiltinCodes] = { %s };" % ", ".join(str(data['combinedCodeOffset']) for data in function_data))
return '\n'.join(lines)

def generate_secondary_header_includes(self):
Expand Down
2 changes: 1 addition & 1 deletion Source/JavaScriptCore/builtins/BuiltinExecutables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ WTF_MAKE_TZONE_ALLOCATED_IMPL(BuiltinExecutables);

BuiltinExecutables::BuiltinExecutables(VM& vm)
: m_vm(vm)
, m_combinedSourceProvider(StringSourceProvider::create(StringImpl::createWithoutCopying({ s_JSCCombinedCode, s_JSCCombinedCodeLength }), { }, String(), SourceTaintedOrigin::Untainted))
, m_combinedSourceProvider(BuiltinsSourceProvider::create(StringImpl::createWithoutCopying({ s_JSCCombinedCode, s_JSCCombinedCodeLength }), s_JSCBuiltinSourceStarts))
{
}

Expand Down
1 change: 1 addition & 0 deletions Source/JavaScriptCore/builtins/BuiltinExecutables.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ struct BuiltinSourceMetadata {

// Emitted by the builtins generator, indexed by BuiltinCodeIndex.
extern constinit const BuiltinSourceMetadata s_JSCBuiltinSourceMetadata[numberOfBuiltinCodes];
extern constinit const unsigned s_JSCBuiltinSourceStarts[numberOfBuiltinCodes]; // in s_JSCCombinedCode

class BuiltinExecutables {
WTF_MAKE_TZONE_ALLOCATED(BuiltinExecutables);
Expand Down
10 changes: 7 additions & 3 deletions Source/JavaScriptCore/bytecode/CodeBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2576,9 +2576,13 @@ void CodeBlock::removeExceptionHandlerForCallSite(DisposableCallSiteIndex callSi
LineColumn CodeBlock::lineColumnForBytecodeIndex(BytecodeIndex bytecodeIndex) const
{
RELEASE_ASSERT(bytecodeIndex.offset() < instructions().size());
auto entry = m_unlinkedCode->expressionInfoForBytecodeIndex(bytecodeIndex);
unsigned divotInProvider = sourceOffset() + entry.divot;
return source().provider()->documentLineColumnForOffset(divotInProvider);
SourceProvider& provider = *source().provider();
return provider.documentLineColumn(m_unlinkedCode->lineColumnInTextForBytecodeIndex(bytecodeIndex, provider, sourceOffset()));
}

LineColumn CodeBlock::lineColumnForBytecodeIndexConcurrently(BytecodeIndex bytecodeIndex) const
{
return source().provider()->documentLineColumnForOffset(expressionInfoForBytecodeIndex(bytecodeIndex).divot);
}

ExpressionInfo::Entry CodeBlock::expressionInfoForBytecodeIndex(BytecodeIndex bytecodeIndex) const
Expand Down
2 changes: 2 additions & 0 deletions Source/JavaScriptCore/bytecode/CodeBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ class CodeBlock : public JSCell {
void removeExceptionHandlerForCallSite(DisposableCallSiteIndex);

LineColumn lineColumnForBytecodeIndex(BytecodeIndex) const;
// For a thread that runs beside the mutator. It keeps nothing, so it decodes the expression info on every call.
LineColumn lineColumnForBytecodeIndexConcurrently(BytecodeIndex) const;
ExpressionInfo::Entry expressionInfoForBytecodeIndex(BytecodeIndex) const;

std::optional<BytecodeIndex> bytecodeIndexFromCallSiteIndex(CallSiteIndex);
Expand Down
Loading
Loading