-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vm: allow dynamic import with a referrer realm
A referrer can be a Script Record, a Cyclic Module Record, or a Realm Record as defined in https://tc39.es/ecma262/#sec-HostLoadImportedModule. Add support for dynamic import calls with a realm as the referrer and allow specifying an `importModuleDynamically` callback in `vm.createContext`. PR-URL: #50360 Refs: #49726 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
- Loading branch information
1 parent
2f86d50
commit 773cfa5
Showing
9 changed files
with
159 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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,70 @@ | ||
// Flags: --experimental-vm-modules | ||
import * as common from '../common/index.mjs'; | ||
import assert from 'node:assert'; | ||
import { Script, SourceTextModule, createContext } from 'node:vm'; | ||
|
||
async function test() { | ||
const foo = new SourceTextModule('export const a = 1;'); | ||
await foo.link(common.mustNotCall()); | ||
await foo.evaluate(); | ||
|
||
const ctx = createContext({}, { | ||
importModuleDynamically: common.mustCall((specifier, wrap) => { | ||
assert.strictEqual(specifier, 'foo'); | ||
assert.strictEqual(wrap, ctx); | ||
return foo; | ||
}, 2), | ||
}); | ||
{ | ||
const s = new Script('Promise.resolve("import(\'foo\')").then(eval)', { | ||
importModuleDynamically: common.mustNotCall(), | ||
}); | ||
|
||
const result = s.runInContext(ctx); | ||
assert.strictEqual(await result, foo.namespace); | ||
} | ||
|
||
{ | ||
const m = new SourceTextModule('globalThis.fooResult = Promise.resolve("import(\'foo\')").then(eval)', { | ||
context: ctx, | ||
importModuleDynamically: common.mustNotCall(), | ||
}); | ||
await m.link(common.mustNotCall()); | ||
await m.evaluate(); | ||
assert.strictEqual(await ctx.fooResult, foo.namespace); | ||
delete ctx.fooResult; | ||
} | ||
} | ||
|
||
async function testMissing() { | ||
const ctx = createContext({}); | ||
{ | ||
const s = new Script('Promise.resolve("import(\'foo\')").then(eval)', { | ||
importModuleDynamically: common.mustNotCall(), | ||
}); | ||
|
||
const result = s.runInContext(ctx); | ||
await assert.rejects(result, { | ||
code: 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING', | ||
}); | ||
} | ||
|
||
{ | ||
const m = new SourceTextModule('globalThis.fooResult = Promise.resolve("import(\'foo\')").then(eval)', { | ||
context: ctx, | ||
importModuleDynamically: common.mustNotCall(), | ||
}); | ||
await m.link(common.mustNotCall()); | ||
await m.evaluate(); | ||
|
||
await assert.rejects(ctx.fooResult, { | ||
code: 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING', | ||
}); | ||
delete ctx.fooResult; | ||
} | ||
} | ||
|
||
await Promise.all([ | ||
test(), | ||
testMissing(), | ||
]).then(common.mustCall()); |