-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
vm: make vm.Module.evaluate() conditionally synchronous #60205
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
joyeecheung
wants to merge
2
commits into
nodejs:main
Choose a base branch
from
joyeecheung:vm-evaluate
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.
+474
−30
Open
Changes from all commits
Commits
Show all changes
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
'use strict'; | ||
|
||
const vm = require('vm'); | ||
const common = require('../common.js'); | ||
const assert = require('assert'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
stage: ['all', 'compile', 'link', 'instantiate', 'evaluate'], | ||
type: ['sync', 'async'], | ||
n: [1000], | ||
}, { | ||
flags: ['--experimental-vm-modules'], | ||
}); | ||
|
||
function main({ stage, type, n }) { | ||
const arr = []; | ||
if (stage === 'all' || stage === 'compile') { | ||
bench.start(); | ||
} | ||
|
||
for (let i = 0; i < n; i++) { | ||
let source = `export const value${i} = 1;`; | ||
if (type === 'async') { | ||
source += `\nawait Promise.resolve();\n`; | ||
} | ||
const m = new vm.SourceTextModule(source); | ||
arr.push(m); | ||
} | ||
|
||
if (stage === 'compile') { | ||
bench.end(n); | ||
return; | ||
} | ||
|
||
if (stage === 'link') { | ||
bench.start(); | ||
} | ||
|
||
for (let i = 0; i < n; i++) { | ||
arr[i].linkRequests([]); | ||
} | ||
|
||
if (stage === 'link') { | ||
bench.end(n); | ||
return; | ||
} | ||
|
||
if (stage === 'instantiate') { | ||
bench.start(); | ||
} | ||
|
||
for (let i = 0; i < n; i++) { | ||
arr[i].instantiate(); | ||
} | ||
|
||
if (stage === 'instantiate') { | ||
bench.end(n); | ||
return; | ||
} | ||
|
||
if (stage === 'evaluate') { | ||
bench.start(); | ||
} | ||
|
||
function finalize() { | ||
bench.end(n); | ||
for (let i = 0; i < n; i++) { | ||
assert.strictEqual(arr[i].namespace[`value${i}`], 1); | ||
} | ||
} | ||
|
||
if (type === 'sync') { | ||
for (let i = 0; i < n; i++) { | ||
arr[i].evaluate(); | ||
} | ||
finalize(); | ||
} else { | ||
const promises = []; | ||
for (let i = 0; i < n; i++) { | ||
promises.push(arr[i].evaluate()); | ||
} | ||
Promise.all(promises).then(finalize); | ||
} | ||
} |
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
167 changes: 167 additions & 0 deletions
167
test/parallel/test-vm-module-evaluate-source-text-module.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,167 @@ | ||
// Flags: --experimental-vm-modules | ||
'use strict'; | ||
|
||
// This tests the result of evaluating a vm.SourceTextModule. | ||
const common = require('../common'); | ||
|
||
const assert = require('assert'); | ||
// To make testing easier we just use the public inspect API. If the output format | ||
// changes, update this test accordingly. | ||
const { inspect } = require('util'); | ||
const vm = require('vm'); | ||
|
||
globalThis.callCount = {}; | ||
common.allowGlobals(globalThis.callCount); | ||
|
||
// Synchronous error during evaluation results in a synchronously rejected promise. | ||
{ | ||
globalThis.callCount.syncError = 0; | ||
const mod = new vm.SourceTextModule(` | ||
globalThis.callCount.syncError++; | ||
throw new Error("synchronous source text module"); | ||
export const a = 1; | ||
`); | ||
mod.linkRequests([]); | ||
mod.instantiate(); | ||
const promise = mod.evaluate(); | ||
assert.strictEqual(globalThis.callCount.syncError, 1); | ||
assert.match(inspect(promise), /rejected/); | ||
joyeecheung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert(mod.error, 'Expected mod.error to be set'); | ||
assert.strictEqual(mod.error.message, 'synchronous source text module'); | ||
|
||
promise.catch(common.mustCall((err) => { | ||
assert.strictEqual(err, mod.error); | ||
// Calling evaluate() again results in the same rejection synchronously. | ||
const promise2 = mod.evaluate(); | ||
assert.match(inspect(promise2), /rejected/); | ||
promise2.catch(common.mustCall((err2) => { | ||
assert.strictEqual(err, err2); | ||
// The module is only evaluated once. | ||
assert.strictEqual(globalThis.callCount.syncError, 1); | ||
})); | ||
})); | ||
} | ||
|
||
// Successful evaluation of a module without top-level await results in a | ||
// promise synchronously resolved to undefined. | ||
{ | ||
globalThis.callCount.syncNamedExports = 0; | ||
const mod = new vm.SourceTextModule(` | ||
globalThis.callCount.syncNamedExports++; | ||
export const a = 1, b = 2; | ||
`); | ||
mod.linkRequests([]); | ||
mod.instantiate(); | ||
const promise = mod.evaluate(); | ||
assert.match(inspect(promise), /Promise { undefined }/); | ||
assert.strictEqual(mod.namespace.a, 1); | ||
assert.strictEqual(mod.namespace.b, 2); | ||
assert.strictEqual(globalThis.callCount.syncNamedExports, 1); | ||
promise.then(common.mustCall((value) => { | ||
assert.strictEqual(value, undefined); | ||
|
||
// Calling evaluate() again results in the same resolved promise synchronously. | ||
const promise2 = mod.evaluate(); | ||
assert.match(inspect(promise2), /Promise { undefined }/); | ||
assert.strictEqual(mod.namespace.a, 1); | ||
assert.strictEqual(mod.namespace.b, 2); | ||
promise2.then(common.mustCall((value) => { | ||
assert.strictEqual(value, undefined); | ||
// The module is only evaluated once. | ||
assert.strictEqual(globalThis.callCount.syncNamedExports, 1); | ||
})); | ||
})); | ||
} | ||
|
||
{ | ||
globalThis.callCount.syncDefaultExports = 0; | ||
// Modules with either named and default exports have the same behaviors. | ||
const mod = new vm.SourceTextModule(` | ||
globalThis.callCount.syncDefaultExports++; | ||
export default 42; | ||
`); | ||
mod.linkRequests([]); | ||
mod.instantiate(); | ||
const promise = mod.evaluate(); | ||
assert.match(inspect(promise), /Promise { undefined }/); | ||
assert.strictEqual(mod.namespace.default, 42); | ||
assert.strictEqual(globalThis.callCount.syncDefaultExports, 1); | ||
|
||
promise.then(common.mustCall((value) => { | ||
assert.strictEqual(value, undefined); | ||
|
||
// Calling evaluate() again results in the same resolved promise synchronously. | ||
const promise2 = mod.evaluate(); | ||
assert.match(inspect(promise2), /Promise { undefined }/); | ||
assert.strictEqual(mod.namespace.default, 42); | ||
promise2.then(common.mustCall((value) => { | ||
assert.strictEqual(value, undefined); | ||
// The module is only evaluated once. | ||
assert.strictEqual(globalThis.callCount.syncDefaultExports, 1); | ||
})); | ||
})); | ||
} | ||
|
||
// Successful evaluation of a module with top-level await results in a promise | ||
// that is fulfilled asynchronously with undefined. | ||
{ | ||
globalThis.callCount.asyncEvaluation = 0; | ||
const mod = new vm.SourceTextModule(` | ||
globalThis.callCount.asyncEvaluation++; | ||
await Promise.resolve(); | ||
export const a = 1; | ||
`); | ||
mod.linkRequests([]); | ||
mod.instantiate(); | ||
const promise = mod.evaluate(); | ||
assert.match(inspect(promise), /<pending>/); | ||
// Accessing the namespace before the promise is fulfilled throws ReferenceError. | ||
assert.throws(() => mod.namespace.a, { name: 'ReferenceError' }); | ||
joyeecheung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert.strictEqual(globalThis.callCount.asyncEvaluation, 1); | ||
promise.then(common.mustCall((value) => { | ||
assert.strictEqual(value, undefined); | ||
assert.strictEqual(globalThis.callCount.asyncEvaluation, 1); | ||
|
||
// Calling evaluate() again results in a promise synchronously resolved to undefined. | ||
const promise2 = mod.evaluate(); | ||
assert.match(inspect(promise2), /Promise { undefined }/); | ||
assert.strictEqual(mod.namespace.a, 1); | ||
promise2.then(common.mustCall((value) => { | ||
assert.strictEqual(value, undefined); | ||
// The module is only evaluated once. | ||
assert.strictEqual(globalThis.callCount.asyncEvaluation, 1); | ||
})); | ||
})); | ||
} | ||
|
||
// Rejection of a top-level await promise results in a promise that is | ||
// rejected asynchronously with the same reason. | ||
{ | ||
globalThis.callCount.asyncRejection = 0; | ||
const mod = new vm.SourceTextModule(` | ||
globalThis.callCount.asyncRejection++; | ||
await Promise.reject(new Error("asynchronous source text module")); | ||
export const a = 1; | ||
`); | ||
mod.linkRequests([]); | ||
mod.instantiate(); | ||
const promise = mod.evaluate(); | ||
assert.match(inspect(promise), /<pending>/); | ||
// Accessing the namespace before the promise is fulfilled throws ReferenceError. | ||
assert.throws(() => mod.namespace.a, { name: 'ReferenceError' }); | ||
promise.catch(common.mustCall((err) => { | ||
assert.strictEqual(err, mod.error); | ||
assert.strictEqual(err.message, 'asynchronous source text module'); | ||
assert.strictEqual(globalThis.callCount.asyncRejection, 1); | ||
|
||
// Calling evaluate() again results in a promise synchronously rejected | ||
// with the same reason. | ||
const promise2 = mod.evaluate(); | ||
assert.match(inspect(promise2), /rejected/); | ||
promise2.catch(common.mustCall((err2) => { | ||
assert.strictEqual(err, err2); | ||
// The module is only evaluated once. | ||
assert.strictEqual(globalThis.callCount.asyncRejection, 1); | ||
})); | ||
})); | ||
} |
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.