|
| 1 | +'use strict'; |
| 2 | +/** |
| 3 | + * This test verifies that dynamic import in an indirect eval without JS stacks. |
| 4 | + * In this case, the referrer for the dynamic import will be null and the main |
| 5 | + * context default loader in createContext() resolves to cwd. |
| 6 | + * |
| 7 | + * Caveat: this test can be unstable if the loader internals are changed and performs |
| 8 | + * microtasks with a JS stack (e.g. with CallbackScope). In this case, the |
| 9 | + * referrer will be resolved to the top JS stack frame `node:internal/process/task_queues.js`. |
| 10 | + * This is due to the implementation detail of how V8 finds the referrer for a dynamic import |
| 11 | + * call. |
| 12 | + */ |
| 13 | + |
| 14 | +const common = require('../common'); |
| 15 | + |
| 16 | +// Can't process.chdir() in worker. |
| 17 | +const { isMainThread } = require('worker_threads'); |
| 18 | + |
| 19 | +if (!isMainThread) { |
| 20 | + common.skip('This test only works on a main thread'); |
| 21 | +} |
| 22 | + |
| 23 | +const tmpdir = require('../common/tmpdir'); |
| 24 | +const fixtures = require('../common/fixtures'); |
| 25 | +const fs = require('node:fs'); |
| 26 | +const { |
| 27 | + Script, |
| 28 | + createContext, |
| 29 | + constants: { USE_MAIN_CONTEXT_DEFAULT_LOADER }, |
| 30 | +} = require('node:vm'); |
| 31 | +const assert = require('node:assert'); |
| 32 | + |
| 33 | +common.expectWarning('ExperimentalWarning', |
| 34 | + 'vm.USE_MAIN_CONTEXT_DEFAULT_LOADER is an experimental feature and might change at any time'); |
| 35 | +assert( |
| 36 | + !process.execArgv.includes('--experimental-vm-modules'), |
| 37 | + 'This test must be run without --experimental-vm-modules'); |
| 38 | +assert.strictEqual(typeof USE_MAIN_CONTEXT_DEFAULT_LOADER, 'symbol'); |
| 39 | + |
| 40 | +async function main() { |
| 41 | + tmpdir.refresh(); |
| 42 | + process.chdir(tmpdir.path); |
| 43 | + |
| 44 | + // |
| 45 | + { |
| 46 | + const options = { |
| 47 | + importModuleDynamically: USE_MAIN_CONTEXT_DEFAULT_LOADER, |
| 48 | + }; |
| 49 | + const ctx = createContext({}, options); |
| 50 | + const s = new Script('Promise.resolve("import(\'./message.mjs\')").then(eval)', { |
| 51 | + importModuleDynamically: common.mustNotCall(), |
| 52 | + }); |
| 53 | + await assert.rejects(s.runInContext(ctx), { code: 'ERR_MODULE_NOT_FOUND' }); |
| 54 | + } |
| 55 | + |
| 56 | + const moduleUrl = fixtures.fileURL('es-modules', 'message.mjs'); |
| 57 | + fs.copyFileSync(moduleUrl, tmpdir.resolve('message.mjs')); |
| 58 | + { |
| 59 | + const options = { |
| 60 | + importModuleDynamically: USE_MAIN_CONTEXT_DEFAULT_LOADER, |
| 61 | + }; |
| 62 | + const ctx = createContext({}, options); |
| 63 | + const moduleUrl = fixtures.fileURL('es-modules', 'message.mjs'); |
| 64 | + const namespace = await import(moduleUrl.href); |
| 65 | + const script = new Script('Promise.resolve("import(\'./message.mjs\')").then(eval)', { |
| 66 | + importModuleDynamically: common.mustNotCall(), |
| 67 | + }); |
| 68 | + const result = await script.runInContext(ctx); |
| 69 | + assert.deepStrictEqual(result, namespace); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +main().catch(common.mustNotCall()); |
0 commit comments