Skip to content
Open
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
3 changes: 3 additions & 0 deletions JSTests/BUN-TEST-DIFFERENCES.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ Not upstream's: a sync neither brings nor changes them. Most are named `stress/b
| `stress/bun-return-address-with-wrong-signature-is-not-returned-to.js` | with `ENABLE(RETURN_ADDRESS_SIGNING)` a function does not return once a bit of the signature of its saved return address was flipped, in each of the four tiers: the run must crash. Where nothing is signed the test crashes by itself |
| the fr-CA block of `stress/intl-date-pattern-includes-literal-text.js` (the file is upstream's) | keeps the file's coverage on CLDR 49, where fr's pattern no longer has literal text: an `h` inside literal text is not mistaken for the hour field |
| the last block of `stress/regexp-test-sticky-first-char-filter.js` (the file is upstream's; nothing of upstream's text changes) | the first-character filter of a sticky `test()` comes from the first sets of the alternatives (`yarr/YarrFirstFollowSets.*`): behind a leading lookbehind, forward reference or lookahead, and in groups nested more than eight deep, a byte that passes the filter still reaches the matcher |
| `stress/vm-termination-at-trap-check.js` and `-with-watchdog.js` | `$vm.callWithTerminationAtTrapCheck(fn, n)` requests a termination at the n-th trap check of a call and withdraws it when the call ends: each larger n lets the call get at least as far, native code is stopped too, no termination is pending before that check (`Atomics.wait()` goes to its timeout), each trap check counts once, none counts while terminations are deferred, a time limit around the call or inside its function still ends what it limits, and nothing is left pending. A VM with a watchdog and script that runs with terminations deferred are refused. The test hook for what a termination leaves behind in the middle of an operation |
| `stress/module-loader-load-sync.js` | `$vm.moduleLoaderLoadSync({ loader }, specifier)` is `JSModuleLoader::loadModuleSync()`, the load of `require()` of an ES module in Bun: the graph has run when the call returns, a load that `import()` began is taken over, what the module throws is thrown, and a top-level await is a `TypeError` |
| `stress/shell-module-loader-fetch-hook.js` | `setModuleLoaderFetchHook(fn)` of the shell: `fn(key)` runs before the file of a module is read, a string that it returns is the text of the module, an object makes a module without text whose exports are read when the graph is about to be linked (a deferred synthetic module, `SyntheticSourceProvider::createDeferred()`), the hook is asked for each fetch of a JavaScript module, and what it throws is what the load fails with. Tests use it for modules without files and for script inside the fetch step of a load |

`jsc` cannot compile a program ahead of time by itself (the compiler's input comes from the front end, in Bun's repository), so
the programs that test ahead-of-time compilation are in Bun's `test/aot`.
Expand Down
59 changes: 59 additions & 0 deletions JSTests/stress/module-loader-load-sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//@ runDefault("--useDollarVM=1")
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

// $vm.moduleLoaderLoadSync({ loader }, specifier): JSModuleLoader::loadModuleSync(), the load that `require()` of an
// ES module makes in Bun. The graph is loaded, linked and evaluated when the call returns.

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

function shouldThrow(fn, constructor, message, what) {
try {
fn();
} catch (error) {
if (error instanceof constructor && error.message == message)
return;
throw new Error(`${what}: threw ${error}`);
}
throw new Error(`${what}: did not throw`);
}

const directory = "./resources/module-loader-load-sync/";
globalThis.loadSyncLog = [];

// The bodies have run, in the order of the graph, when the call returns.
const loader = $vm.createModuleLoader();
const namespace = $vm.moduleLoaderLoadSync(loader, directory + "root.js");
shouldBe(loadSyncLog.join(), "dep,root", "the bodies that ran before the call returned");
shouldBe(namespace.value, 41, "the namespace object");

// A second load, synchronous or not, answers with the module that is there.
shouldBe($vm.moduleLoaderLoadSync(loader, directory + "root.js"), namespace, "the second synchronous load");
shouldBe($vm.moduleLoaderLoadSync(loader, directory + "dep.js").dep, 40, "a synchronous load of the dependency");
let imported;
$vm.moduleLoaderImport(loader, directory + "root.js").then(result => { imported = result; });
drainMicrotasks();
shouldBe(imported, namespace, "import() after the synchronous load");
shouldBe(loadSyncLog.join(), "dep,root", "the bodies ran once");

// A module that import() has begun to load is taken over: the call does not wait for the jobs of that load.
{
const loader = $vm.createModuleLoader();
let imported;
$vm.moduleLoaderImport(loader, directory + "root.js").then(result => { imported = result; });
const namespace = $vm.moduleLoaderLoadSync(loader, directory + "root.js");
shouldBe(namespace.value, 41, "a synchronous load of a module that import() is loading");
shouldBe(imported, undefined, "the import() has not been answered yet");
drainMicrotasks();
shouldBe(imported, namespace, "the import() that was first");
shouldBe(loadSyncLog.join(), "dep,root,dep,root", "the bodies ran once for the second loader");
}

// What the module throws is thrown by the call.
shouldThrow(() => $vm.moduleLoaderLoadSync($vm.createModuleLoader(), directory + "throws.js"), RangeError, "from the module", "a module that throws");

// A module with a top-level await does not finish inside the call.
shouldThrow(() => $vm.moduleLoaderLoadSync($vm.createModuleLoader(), directory + "waits.js"), TypeError, "the load of the module did not finish", "a module that waits");

shouldThrow(() => $vm.moduleLoaderLoadSync({ }, directory + "root.js"), TypeError, "expected the result of $vm.createModuleLoader()", "not a loader");
2 changes: 2 additions & 0 deletions JSTests/stress/resources/module-loader-load-sync/dep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
globalThis.loadSyncLog.push("dep");
export const dep = 40;
3 changes: 3 additions & 0 deletions JSTests/stress/resources/module-loader-load-sync/root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { dep } from "./dep.js";
globalThis.loadSyncLog.push("root");
export const value = dep + 1;
2 changes: 2 additions & 0 deletions JSTests/stress/resources/module-loader-load-sync/throws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import "./dep.js";
throw new RangeError("from the module");
2 changes: 2 additions & 0 deletions JSTests/stress/resources/module-loader-load-sync/waits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
await 1;
export const late = 1;
125 changes: 125 additions & 0 deletions JSTests/stress/shell-module-loader-fetch-hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
//@ runDefault("--useDollarVM=1")

// setModuleLoaderFetchHook(fn): the shell calls fn(key) before it reads the file of a module. A string is the text of
// the module. An object makes a module without text, whose exports are the properties of the object. What fn throws is
// thrown by the fetch hook of the loader. So a test can make modules without files, and can run script inside the
// fetch step of a load.

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

function importThrough(loader, path) {
const load = { state: "pending", value: undefined };
$vm.moduleLoaderImport(loader, path).then(namespace => {
load.value = namespace;
load.state = "fulfilled";
}, error => {
load.value = error;
load.state = "rejected";
});
drainMicrotasks();
return load;
}

const made = "./shell-module-loader-fetch-hook/";
const files = "./resources/module-loader-load-sync/";
globalThis.loadSyncLog = [];
const keys = [];
const failure = new RangeError("from the hook");
let inner;
const syncLoader = $vm.createModuleLoader();
setModuleLoaderFetchHook(key => {
keys.push(key.slice(key.lastIndexOf("/") + 1));
if (key.endsWith("/made-root.js"))
return `import { value } from "./made-dep.js"; export default value + 1;`;
if (key.endsWith("/made-dep.js"))
return `export const value = 40;`;
if (key.endsWith("/throws.js"))
throw failure;
if (key.endsWith("/imports-object.js"))
return `import { answer, late } from "./object.js"; import { more } from "./more.js"; export default answer + late + more;`;
if (key.endsWith("/more.js"))
return `export const more = 0;`;
if (key.endsWith("/sync-root.js"))
return `import "./sync-begins-import.js"; import { value } from "./sync-dep.js"; export default value;`;
if (key.endsWith("/sync-begins-import.js")) {
// An import() of a module that the synchronous load has asked for already.
$vm.moduleLoaderImport(syncLoader, made + "sync-dep.js").then(() => { }, () => { });
return `export { };`;
}
if (key.endsWith("/sync-dep.js"))
return `export const value = "made";`;
if (key.endsWith("/object.js")) {
return {
answer: 41,
get late() {
keys.push("late is read");
return 1;
},
};
}
if (key.endsWith("/loads.js")) {
// A load of its own, inside the fetch step of this one.
inner = $vm.moduleLoaderLoadSync($vm.createModuleLoader(), made + "made-root.js").default;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return `export default ${inner} + 1;`;
}
});

// Modules that the hook makes. There are no such files.
let load = importThrough($vm.createModuleLoader(), made + "made-root.js");
shouldBe(load.state, "fulfilled", "a module that the hook makes");
shouldBe(load.value.default, 41, "the value of a module that the hook makes");
shouldBe(keys.join(), "made-root.js,made-dep.js", "the keys that the hook was asked for");

// Where the hook returns neither a string nor an object, the file is read.
load = importThrough($vm.createModuleLoader(), files + "dep.js");
shouldBe(load.state, "fulfilled", "a module of a file");
shouldBe(load.value.dep, 40, "the value of a module of a file");

// What the hook throws is what the load fails with.
load = importThrough($vm.createModuleLoader(), made + "throws.js");
shouldBe(load.state, "rejected", "a module whose fetch throws");
shouldBe(load.value, failure, "what the load of that module fails with");

// An object makes a module without text. The shell reads the exports when the graph is about to be linked, which is
// behind the fetch of each module of the graph.
keys.length = 0;
load = importThrough($vm.createModuleLoader(), made + "imports-object.js");
shouldBe(load.state, "fulfilled", "a module that imports a module without text");
shouldBe(load.value.default, 42, "the value of a module that imports a module without text");
shouldBe(keys.join(), "imports-object.js,object.js,more.js,late is read", "the exports of a module without text are read once, behind each fetch");
load = importThrough($vm.createModuleLoader(), made + "object.js");
shouldBe(load.state, "fulfilled", "a module without text");
shouldBe(load.value.answer, 41, "an export of a module without text");

// The hook runs script: here a synchronous load through another loader, where the build has that function.
if ($vm.moduleLoaderLoadSync) {
load = importThrough($vm.createModuleLoader(), made + "loads.js");
shouldBe(load.state, "fulfilled", "a module whose fetch loads another");
shouldBe(inner, 41, "the load inside the hook");
shouldBe(load.value.default, 42, "the value of a module whose fetch loads another");
}

// The hook is asked for each fetch of a JavaScript module, also where the loader passes the type of the module along:
// the import() inside this synchronous load is such a fetch.
if ($vm.moduleLoaderLoadSync) {
keys.length = 0;
shouldBe($vm.moduleLoaderLoadSync(syncLoader, made + "sync-root.js").default, "made", "a synchronous load with an import() inside its fetch step");
shouldBe(keys.filter(key => key == "sync-dep.js").length >= 1, true, "the hook was asked for the module of the import()");
drainMicrotasks();
}

// Without a function the shell reads files again.
setModuleLoaderFetchHook();
load = importThrough($vm.createModuleLoader(), made + "made-root.js");
shouldBe(load.state, "rejected", "a module that no hook makes");

let threw = false;
try {
setModuleLoaderFetchHook(1);
} catch (error) {
threw = error instanceof TypeError;
}
shouldBe(threw, true, "a hook that is not a function");
18 changes: 18 additions & 0 deletions JSTests/stress/vm-termination-at-trap-check-with-watchdog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//@ runDefault("--useDollarVM=1", "--useJIT=0", "--watchdog=600000")

// $vm.callWithTerminationAtTrapCheck() counts trap checks with the trap bit of the watchdog. A VM that has a watchdog
// refuses the call: the watchdog would get none of its checks while the count runs.

let error;
let started = false;
try {
$vm.callWithTerminationAtTrapCheck(() => { started = true; }, 1);
} catch (e) {
error = e;
}
if (!(error instanceof TypeError) || error.message != "$vm.callWithTerminationAtTrapCheck() needs a VM without a watchdog")
throw new Error(`expected the TypeError of the refusal, got ${error}`);
if (started)
throw new Error("the function started");
if ($vm.hasPendingTermination())
throw new Error("something is still pending");
Loading
Loading