Repository navigation
[JSC] Test hooks for a termination inside a module load: $vm.callWithTerminationAtTrapCheck(), $vm.moduleLoaderLoadSync(), setModuleLoaderFetchHook() #975
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
Open
robobun
wants to merge
3
commits into
main
Choose a base branch
from
robobun/85266cfa/termination-at-trap-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| //@ runDefault("--useDollarVM=1") | ||
|
|
||
| // $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"); | ||
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,2 @@ | ||
| globalThis.loadSyncLog.push("dep"); | ||
| export const dep = 40; |
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,3 @@ | ||
| import { dep } from "./dep.js"; | ||
| globalThis.loadSyncLog.push("root"); | ||
| export const value = dep + 1; |
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,2 @@ | ||
| import "./dep.js"; | ||
| throw new RangeError("from the module"); |
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,2 @@ | ||
| await 1; | ||
| export const late = 1; |
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,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; | ||
|
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
18
JSTests/stress/vm-termination-at-trap-check-with-watchdog.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,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"); |
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.