11cb0ef41Sopenharmony_ci# VM (executing JavaScript) 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci<!--introduced_in=v0.10.0--> 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci> Stability: 2 - Stable 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci<!--name=vm--> 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci<!-- source_link=lib/vm.js --> 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ciThe `node:vm` module enables compiling and running code within V8 Virtual 121cb0ef41Sopenharmony_ciMachine contexts. 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci<strong class="critical">The `node:vm` module is not a security 151cb0ef41Sopenharmony_cimechanism. Do not use it to run untrusted code.</strong> 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ciJavaScript code can be compiled and run immediately or 181cb0ef41Sopenharmony_cicompiled, saved, and run later. 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ciA common use case is to run the code in a different V8 Context. This means 211cb0ef41Sopenharmony_ciinvoked code has a different global object than the invoking code. 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ciOne can provide the context by [_contextifying_][contextified] an 241cb0ef41Sopenharmony_ciobject. The invoked code treats any property in the context like a 251cb0ef41Sopenharmony_ciglobal variable. Any changes to global variables caused by the invoked 261cb0ef41Sopenharmony_cicode are reflected in the context object. 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci```js 291cb0ef41Sopenharmony_ciconst vm = require('node:vm'); 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ciconst x = 1; 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ciconst context = { x: 2 }; 341cb0ef41Sopenharmony_civm.createContext(context); // Contextify the object. 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ciconst code = 'x += 40; var y = 17;'; 371cb0ef41Sopenharmony_ci// `x` and `y` are global variables in the context. 381cb0ef41Sopenharmony_ci// Initially, x has the value 2 because that is the value of context.x. 391cb0ef41Sopenharmony_civm.runInContext(code, context); 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ciconsole.log(context.x); // 42 421cb0ef41Sopenharmony_ciconsole.log(context.y); // 17 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ciconsole.log(x); // 1; y is not defined. 451cb0ef41Sopenharmony_ci``` 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci## Class: `vm.Script` 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci<!-- YAML 501cb0ef41Sopenharmony_ciadded: v0.3.1 511cb0ef41Sopenharmony_ci--> 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ciInstances of the `vm.Script` class contain precompiled scripts that can be 541cb0ef41Sopenharmony_ciexecuted in specific contexts. 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci### `new vm.Script(code[, options])` 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci<!-- YAML 591cb0ef41Sopenharmony_ciadded: v0.3.1 601cb0ef41Sopenharmony_cichanges: 611cb0ef41Sopenharmony_ci - version: 621cb0ef41Sopenharmony_ci - v17.0.0 631cb0ef41Sopenharmony_ci - v16.12.0 641cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/40249 651cb0ef41Sopenharmony_ci description: Added support for import attributes to the 661cb0ef41Sopenharmony_ci `importModuleDynamically` parameter. 671cb0ef41Sopenharmony_ci - version: v10.6.0 681cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/20300 691cb0ef41Sopenharmony_ci description: The `produceCachedData` is deprecated in favour of 701cb0ef41Sopenharmony_ci `script.createCachedData()`. 711cb0ef41Sopenharmony_ci - version: v5.7.0 721cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/4777 731cb0ef41Sopenharmony_ci description: The `cachedData` and `produceCachedData` options are 741cb0ef41Sopenharmony_ci supported now. 751cb0ef41Sopenharmony_ci--> 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci* `code` {string} The JavaScript code to compile. 781cb0ef41Sopenharmony_ci* `options` {Object|string} 791cb0ef41Sopenharmony_ci * `filename` {string} Specifies the filename used in stack traces produced 801cb0ef41Sopenharmony_ci by this script. **Default:** `'evalmachine.<anonymous>'`. 811cb0ef41Sopenharmony_ci * `lineOffset` {number} Specifies the line number offset that is displayed 821cb0ef41Sopenharmony_ci in stack traces produced by this script. **Default:** `0`. 831cb0ef41Sopenharmony_ci * `columnOffset` {number} Specifies the first-line column number offset that 841cb0ef41Sopenharmony_ci is displayed in stack traces produced by this script. **Default:** `0`. 851cb0ef41Sopenharmony_ci * `cachedData` {Buffer|TypedArray|DataView} Provides an optional `Buffer` or 861cb0ef41Sopenharmony_ci `TypedArray`, or `DataView` with V8's code cache data for the supplied 871cb0ef41Sopenharmony_ci source. When supplied, the `cachedDataRejected` value will be set to 881cb0ef41Sopenharmony_ci either `true` or `false` depending on acceptance of the data by V8. 891cb0ef41Sopenharmony_ci * `produceCachedData` {boolean} When `true` and no `cachedData` is present, V8 901cb0ef41Sopenharmony_ci will attempt to produce code cache data for `code`. Upon success, a 911cb0ef41Sopenharmony_ci `Buffer` with V8's code cache data will be produced and stored in the 921cb0ef41Sopenharmony_ci `cachedData` property of the returned `vm.Script` instance. 931cb0ef41Sopenharmony_ci The `cachedDataProduced` value will be set to either `true` or `false` 941cb0ef41Sopenharmony_ci depending on whether code cache data is produced successfully. 951cb0ef41Sopenharmony_ci This option is **deprecated** in favor of `script.createCachedData()`. 961cb0ef41Sopenharmony_ci **Default:** `false`. 971cb0ef41Sopenharmony_ci * `importModuleDynamically` {Function} Called during evaluation of this module 981cb0ef41Sopenharmony_ci when `import()` is called. If this option is not specified, calls to 991cb0ef41Sopenharmony_ci `import()` will reject with [`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING`][]. 1001cb0ef41Sopenharmony_ci This option is part of the experimental modules API. We do not recommend 1011cb0ef41Sopenharmony_ci using it in a production environment. If `--experimental-vm-modules` isn't 1021cb0ef41Sopenharmony_ci set, this callback will be ignored and calls to `import()` will reject with 1031cb0ef41Sopenharmony_ci [`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG`][]. 1041cb0ef41Sopenharmony_ci * `specifier` {string} specifier passed to `import()` 1051cb0ef41Sopenharmony_ci * `script` {vm.Script} 1061cb0ef41Sopenharmony_ci * `importAttributes` {Object} The `"with"` value passed to the 1071cb0ef41Sopenharmony_ci [`optionsExpression`][] optional parameter, or an empty object if no value 1081cb0ef41Sopenharmony_ci was provided. 1091cb0ef41Sopenharmony_ci * Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is 1101cb0ef41Sopenharmony_ci recommended in order to take advantage of error tracking, and to avoid 1111cb0ef41Sopenharmony_ci issues with namespaces that contain `then` function exports. 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ciIf `options` is a string, then it specifies the filename. 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ciCreating a new `vm.Script` object compiles `code` but does not run it. The 1161cb0ef41Sopenharmony_cicompiled `vm.Script` can be run later multiple times. The `code` is not bound to 1171cb0ef41Sopenharmony_ciany global object; rather, it is bound before each run, just for that run. 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_ci### `script.cachedDataRejected` 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_ci<!-- YAML 1221cb0ef41Sopenharmony_ciadded: v5.7.0 1231cb0ef41Sopenharmony_ci--> 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci* {boolean|undefined} 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ciWhen `cachedData` is supplied to create the `vm.Script`, this value will be set 1281cb0ef41Sopenharmony_cito either `true` or `false` depending on acceptance of the data by V8. 1291cb0ef41Sopenharmony_ciOtherwise the value is `undefined`. 1301cb0ef41Sopenharmony_ci 1311cb0ef41Sopenharmony_ci### `script.createCachedData()` 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_ci<!-- YAML 1341cb0ef41Sopenharmony_ciadded: v10.6.0 1351cb0ef41Sopenharmony_ci--> 1361cb0ef41Sopenharmony_ci 1371cb0ef41Sopenharmony_ci* Returns: {Buffer} 1381cb0ef41Sopenharmony_ci 1391cb0ef41Sopenharmony_ciCreates a code cache that can be used with the `Script` constructor's 1401cb0ef41Sopenharmony_ci`cachedData` option. Returns a `Buffer`. This method may be called at any 1411cb0ef41Sopenharmony_citime and any number of times. 1421cb0ef41Sopenharmony_ci 1431cb0ef41Sopenharmony_ciThe code cache of the `Script` doesn't contain any JavaScript observable 1441cb0ef41Sopenharmony_cistates. The code cache is safe to be saved along side the script source and 1451cb0ef41Sopenharmony_ciused to construct new `Script` instances multiple times. 1461cb0ef41Sopenharmony_ci 1471cb0ef41Sopenharmony_ciFunctions in the `Script` source can be marked as lazily compiled and they are 1481cb0ef41Sopenharmony_cinot compiled at construction of the `Script`. These functions are going to be 1491cb0ef41Sopenharmony_cicompiled when they are invoked the first time. The code cache serializes the 1501cb0ef41Sopenharmony_cimetadata that V8 currently knows about the `Script` that it can use to speed up 1511cb0ef41Sopenharmony_cifuture compilations. 1521cb0ef41Sopenharmony_ci 1531cb0ef41Sopenharmony_ci```js 1541cb0ef41Sopenharmony_ciconst script = new vm.Script(` 1551cb0ef41Sopenharmony_cifunction add(a, b) { 1561cb0ef41Sopenharmony_ci return a + b; 1571cb0ef41Sopenharmony_ci} 1581cb0ef41Sopenharmony_ci 1591cb0ef41Sopenharmony_ciconst x = add(1, 2); 1601cb0ef41Sopenharmony_ci`); 1611cb0ef41Sopenharmony_ci 1621cb0ef41Sopenharmony_ciconst cacheWithoutAdd = script.createCachedData(); 1631cb0ef41Sopenharmony_ci// In `cacheWithoutAdd` the function `add()` is marked for full compilation 1641cb0ef41Sopenharmony_ci// upon invocation. 1651cb0ef41Sopenharmony_ci 1661cb0ef41Sopenharmony_ciscript.runInThisContext(); 1671cb0ef41Sopenharmony_ci 1681cb0ef41Sopenharmony_ciconst cacheWithAdd = script.createCachedData(); 1691cb0ef41Sopenharmony_ci// `cacheWithAdd` contains fully compiled function `add()`. 1701cb0ef41Sopenharmony_ci``` 1711cb0ef41Sopenharmony_ci 1721cb0ef41Sopenharmony_ci### `script.runInContext(contextifiedObject[, options])` 1731cb0ef41Sopenharmony_ci 1741cb0ef41Sopenharmony_ci<!-- YAML 1751cb0ef41Sopenharmony_ciadded: v0.3.1 1761cb0ef41Sopenharmony_cichanges: 1771cb0ef41Sopenharmony_ci - version: v6.3.0 1781cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/6635 1791cb0ef41Sopenharmony_ci description: The `breakOnSigint` option is supported now. 1801cb0ef41Sopenharmony_ci--> 1811cb0ef41Sopenharmony_ci 1821cb0ef41Sopenharmony_ci* `contextifiedObject` {Object} A [contextified][] object as returned by the 1831cb0ef41Sopenharmony_ci `vm.createContext()` method. 1841cb0ef41Sopenharmony_ci* `options` {Object} 1851cb0ef41Sopenharmony_ci * `displayErrors` {boolean} When `true`, if an [`Error`][] occurs 1861cb0ef41Sopenharmony_ci while compiling the `code`, the line of code causing the error is attached 1871cb0ef41Sopenharmony_ci to the stack trace. **Default:** `true`. 1881cb0ef41Sopenharmony_ci * `timeout` {integer} Specifies the number of milliseconds to execute `code` 1891cb0ef41Sopenharmony_ci before terminating execution. If execution is terminated, an [`Error`][] 1901cb0ef41Sopenharmony_ci will be thrown. This value must be a strictly positive integer. 1911cb0ef41Sopenharmony_ci * `breakOnSigint` {boolean} If `true`, receiving `SIGINT` 1921cb0ef41Sopenharmony_ci (<kbd>Ctrl</kbd>+<kbd>C</kbd>) will terminate execution and throw an 1931cb0ef41Sopenharmony_ci [`Error`][]. Existing handlers for the event that have been attached via 1941cb0ef41Sopenharmony_ci `process.on('SIGINT')` are disabled during script execution, but continue to 1951cb0ef41Sopenharmony_ci work after that. **Default:** `false`. 1961cb0ef41Sopenharmony_ci* Returns: {any} the result of the very last statement executed in the script. 1971cb0ef41Sopenharmony_ci 1981cb0ef41Sopenharmony_ciRuns the compiled code contained by the `vm.Script` object within the given 1991cb0ef41Sopenharmony_ci`contextifiedObject` and returns the result. Running code does not have access 2001cb0ef41Sopenharmony_cito local scope. 2011cb0ef41Sopenharmony_ci 2021cb0ef41Sopenharmony_ciThe following example compiles code that increments a global variable, sets 2031cb0ef41Sopenharmony_cithe value of another global variable, then execute the code multiple times. 2041cb0ef41Sopenharmony_ciThe globals are contained in the `context` object. 2051cb0ef41Sopenharmony_ci 2061cb0ef41Sopenharmony_ci```js 2071cb0ef41Sopenharmony_ciconst vm = require('node:vm'); 2081cb0ef41Sopenharmony_ci 2091cb0ef41Sopenharmony_ciconst context = { 2101cb0ef41Sopenharmony_ci animal: 'cat', 2111cb0ef41Sopenharmony_ci count: 2, 2121cb0ef41Sopenharmony_ci}; 2131cb0ef41Sopenharmony_ci 2141cb0ef41Sopenharmony_ciconst script = new vm.Script('count += 1; name = "kitty";'); 2151cb0ef41Sopenharmony_ci 2161cb0ef41Sopenharmony_civm.createContext(context); 2171cb0ef41Sopenharmony_cifor (let i = 0; i < 10; ++i) { 2181cb0ef41Sopenharmony_ci script.runInContext(context); 2191cb0ef41Sopenharmony_ci} 2201cb0ef41Sopenharmony_ci 2211cb0ef41Sopenharmony_ciconsole.log(context); 2221cb0ef41Sopenharmony_ci// Prints: { animal: 'cat', count: 12, name: 'kitty' } 2231cb0ef41Sopenharmony_ci``` 2241cb0ef41Sopenharmony_ci 2251cb0ef41Sopenharmony_ciUsing the `timeout` or `breakOnSigint` options will result in new event loops 2261cb0ef41Sopenharmony_ciand corresponding threads being started, which have a non-zero performance 2271cb0ef41Sopenharmony_cioverhead. 2281cb0ef41Sopenharmony_ci 2291cb0ef41Sopenharmony_ci### `script.runInNewContext([contextObject[, options]])` 2301cb0ef41Sopenharmony_ci 2311cb0ef41Sopenharmony_ci<!-- YAML 2321cb0ef41Sopenharmony_ciadded: v0.3.1 2331cb0ef41Sopenharmony_cichanges: 2341cb0ef41Sopenharmony_ci - version: v14.6.0 2351cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/34023 2361cb0ef41Sopenharmony_ci description: The `microtaskMode` option is supported now. 2371cb0ef41Sopenharmony_ci - version: v10.0.0 2381cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/19016 2391cb0ef41Sopenharmony_ci description: The `contextCodeGeneration` option is supported now. 2401cb0ef41Sopenharmony_ci - version: v6.3.0 2411cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/6635 2421cb0ef41Sopenharmony_ci description: The `breakOnSigint` option is supported now. 2431cb0ef41Sopenharmony_ci--> 2441cb0ef41Sopenharmony_ci 2451cb0ef41Sopenharmony_ci* `contextObject` {Object} An object that will be [contextified][]. If 2461cb0ef41Sopenharmony_ci `undefined`, a new object will be created. 2471cb0ef41Sopenharmony_ci* `options` {Object} 2481cb0ef41Sopenharmony_ci * `displayErrors` {boolean} When `true`, if an [`Error`][] occurs 2491cb0ef41Sopenharmony_ci while compiling the `code`, the line of code causing the error is attached 2501cb0ef41Sopenharmony_ci to the stack trace. **Default:** `true`. 2511cb0ef41Sopenharmony_ci * `timeout` {integer} Specifies the number of milliseconds to execute `code` 2521cb0ef41Sopenharmony_ci before terminating execution. If execution is terminated, an [`Error`][] 2531cb0ef41Sopenharmony_ci will be thrown. This value must be a strictly positive integer. 2541cb0ef41Sopenharmony_ci * `breakOnSigint` {boolean} If `true`, receiving `SIGINT` 2551cb0ef41Sopenharmony_ci (<kbd>Ctrl</kbd>+<kbd>C</kbd>) will terminate execution and throw an 2561cb0ef41Sopenharmony_ci [`Error`][]. Existing handlers for the event that have been attached via 2571cb0ef41Sopenharmony_ci `process.on('SIGINT')` are disabled during script execution, but continue to 2581cb0ef41Sopenharmony_ci work after that. **Default:** `false`. 2591cb0ef41Sopenharmony_ci * `contextName` {string} Human-readable name of the newly created context. 2601cb0ef41Sopenharmony_ci **Default:** `'VM Context i'`, where `i` is an ascending numerical index of 2611cb0ef41Sopenharmony_ci the created context. 2621cb0ef41Sopenharmony_ci * `contextOrigin` {string} [Origin][origin] corresponding to the newly 2631cb0ef41Sopenharmony_ci created context for display purposes. The origin should be formatted like a 2641cb0ef41Sopenharmony_ci URL, but with only the scheme, host, and port (if necessary), like the 2651cb0ef41Sopenharmony_ci value of the [`url.origin`][] property of a [`URL`][] object. Most notably, 2661cb0ef41Sopenharmony_ci this string should omit the trailing slash, as that denotes a path. 2671cb0ef41Sopenharmony_ci **Default:** `''`. 2681cb0ef41Sopenharmony_ci * `contextCodeGeneration` {Object} 2691cb0ef41Sopenharmony_ci * `strings` {boolean} If set to false any calls to `eval` or function 2701cb0ef41Sopenharmony_ci constructors (`Function`, `GeneratorFunction`, etc) will throw an 2711cb0ef41Sopenharmony_ci `EvalError`. **Default:** `true`. 2721cb0ef41Sopenharmony_ci * `wasm` {boolean} If set to false any attempt to compile a WebAssembly 2731cb0ef41Sopenharmony_ci module will throw a `WebAssembly.CompileError`. **Default:** `true`. 2741cb0ef41Sopenharmony_ci * `microtaskMode` {string} If set to `afterEvaluate`, microtasks (tasks 2751cb0ef41Sopenharmony_ci scheduled through `Promise`s and `async function`s) will be run immediately 2761cb0ef41Sopenharmony_ci after the script has run. They are included in the `timeout` and 2771cb0ef41Sopenharmony_ci `breakOnSigint` scopes in that case. 2781cb0ef41Sopenharmony_ci* Returns: {any} the result of the very last statement executed in the script. 2791cb0ef41Sopenharmony_ci 2801cb0ef41Sopenharmony_ciFirst contextifies the given `contextObject`, runs the compiled code contained 2811cb0ef41Sopenharmony_ciby the `vm.Script` object within the created context, and returns the result. 2821cb0ef41Sopenharmony_ciRunning code does not have access to local scope. 2831cb0ef41Sopenharmony_ci 2841cb0ef41Sopenharmony_ciThe following example compiles code that sets a global variable, then executes 2851cb0ef41Sopenharmony_cithe code multiple times in different contexts. The globals are set on and 2861cb0ef41Sopenharmony_cicontained within each individual `context`. 2871cb0ef41Sopenharmony_ci 2881cb0ef41Sopenharmony_ci```js 2891cb0ef41Sopenharmony_ciconst vm = require('node:vm'); 2901cb0ef41Sopenharmony_ci 2911cb0ef41Sopenharmony_ciconst script = new vm.Script('globalVar = "set"'); 2921cb0ef41Sopenharmony_ci 2931cb0ef41Sopenharmony_ciconst contexts = [{}, {}, {}]; 2941cb0ef41Sopenharmony_cicontexts.forEach((context) => { 2951cb0ef41Sopenharmony_ci script.runInNewContext(context); 2961cb0ef41Sopenharmony_ci}); 2971cb0ef41Sopenharmony_ci 2981cb0ef41Sopenharmony_ciconsole.log(contexts); 2991cb0ef41Sopenharmony_ci// Prints: [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }] 3001cb0ef41Sopenharmony_ci``` 3011cb0ef41Sopenharmony_ci 3021cb0ef41Sopenharmony_ci### `script.runInThisContext([options])` 3031cb0ef41Sopenharmony_ci 3041cb0ef41Sopenharmony_ci<!-- YAML 3051cb0ef41Sopenharmony_ciadded: v0.3.1 3061cb0ef41Sopenharmony_cichanges: 3071cb0ef41Sopenharmony_ci - version: v6.3.0 3081cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/6635 3091cb0ef41Sopenharmony_ci description: The `breakOnSigint` option is supported now. 3101cb0ef41Sopenharmony_ci--> 3111cb0ef41Sopenharmony_ci 3121cb0ef41Sopenharmony_ci* `options` {Object} 3131cb0ef41Sopenharmony_ci * `displayErrors` {boolean} When `true`, if an [`Error`][] occurs 3141cb0ef41Sopenharmony_ci while compiling the `code`, the line of code causing the error is attached 3151cb0ef41Sopenharmony_ci to the stack trace. **Default:** `true`. 3161cb0ef41Sopenharmony_ci * `timeout` {integer} Specifies the number of milliseconds to execute `code` 3171cb0ef41Sopenharmony_ci before terminating execution. If execution is terminated, an [`Error`][] 3181cb0ef41Sopenharmony_ci will be thrown. This value must be a strictly positive integer. 3191cb0ef41Sopenharmony_ci * `breakOnSigint` {boolean} If `true`, receiving `SIGINT` 3201cb0ef41Sopenharmony_ci (<kbd>Ctrl</kbd>+<kbd>C</kbd>) will terminate execution and throw an 3211cb0ef41Sopenharmony_ci [`Error`][]. Existing handlers for the event that have been attached via 3221cb0ef41Sopenharmony_ci `process.on('SIGINT')` are disabled during script execution, but continue to 3231cb0ef41Sopenharmony_ci work after that. **Default:** `false`. 3241cb0ef41Sopenharmony_ci* Returns: {any} the result of the very last statement executed in the script. 3251cb0ef41Sopenharmony_ci 3261cb0ef41Sopenharmony_ciRuns the compiled code contained by the `vm.Script` within the context of the 3271cb0ef41Sopenharmony_cicurrent `global` object. Running code does not have access to local scope, but 3281cb0ef41Sopenharmony_ci_does_ have access to the current `global` object. 3291cb0ef41Sopenharmony_ci 3301cb0ef41Sopenharmony_ciThe following example compiles code that increments a `global` variable then 3311cb0ef41Sopenharmony_ciexecutes that code multiple times: 3321cb0ef41Sopenharmony_ci 3331cb0ef41Sopenharmony_ci```js 3341cb0ef41Sopenharmony_ciconst vm = require('node:vm'); 3351cb0ef41Sopenharmony_ci 3361cb0ef41Sopenharmony_ciglobal.globalVar = 0; 3371cb0ef41Sopenharmony_ci 3381cb0ef41Sopenharmony_ciconst script = new vm.Script('globalVar += 1', { filename: 'myfile.vm' }); 3391cb0ef41Sopenharmony_ci 3401cb0ef41Sopenharmony_cifor (let i = 0; i < 1000; ++i) { 3411cb0ef41Sopenharmony_ci script.runInThisContext(); 3421cb0ef41Sopenharmony_ci} 3431cb0ef41Sopenharmony_ci 3441cb0ef41Sopenharmony_ciconsole.log(globalVar); 3451cb0ef41Sopenharmony_ci 3461cb0ef41Sopenharmony_ci// 1000 3471cb0ef41Sopenharmony_ci``` 3481cb0ef41Sopenharmony_ci 3491cb0ef41Sopenharmony_ci### `script.sourceMapURL` 3501cb0ef41Sopenharmony_ci 3511cb0ef41Sopenharmony_ci<!-- YAML 3521cb0ef41Sopenharmony_ciadded: v18.13.0 3531cb0ef41Sopenharmony_ci--> 3541cb0ef41Sopenharmony_ci 3551cb0ef41Sopenharmony_ci* {string|undefined} 3561cb0ef41Sopenharmony_ci 3571cb0ef41Sopenharmony_ciWhen the script is compiled from a source that contains a source map magic 3581cb0ef41Sopenharmony_cicomment, this property will be set to the URL of the source map. 3591cb0ef41Sopenharmony_ci 3601cb0ef41Sopenharmony_ci```mjs 3611cb0ef41Sopenharmony_ciimport vm from 'node:vm'; 3621cb0ef41Sopenharmony_ci 3631cb0ef41Sopenharmony_ciconst script = new vm.Script(` 3641cb0ef41Sopenharmony_cifunction myFunc() {} 3651cb0ef41Sopenharmony_ci//# sourceMappingURL=sourcemap.json 3661cb0ef41Sopenharmony_ci`); 3671cb0ef41Sopenharmony_ci 3681cb0ef41Sopenharmony_ciconsole.log(script.sourceMapURL); 3691cb0ef41Sopenharmony_ci// Prints: sourcemap.json 3701cb0ef41Sopenharmony_ci``` 3711cb0ef41Sopenharmony_ci 3721cb0ef41Sopenharmony_ci```cjs 3731cb0ef41Sopenharmony_ciconst vm = require('node:vm'); 3741cb0ef41Sopenharmony_ci 3751cb0ef41Sopenharmony_ciconst script = new vm.Script(` 3761cb0ef41Sopenharmony_cifunction myFunc() {} 3771cb0ef41Sopenharmony_ci//# sourceMappingURL=sourcemap.json 3781cb0ef41Sopenharmony_ci`); 3791cb0ef41Sopenharmony_ci 3801cb0ef41Sopenharmony_ciconsole.log(script.sourceMapURL); 3811cb0ef41Sopenharmony_ci// Prints: sourcemap.json 3821cb0ef41Sopenharmony_ci``` 3831cb0ef41Sopenharmony_ci 3841cb0ef41Sopenharmony_ci## Class: `vm.Module` 3851cb0ef41Sopenharmony_ci 3861cb0ef41Sopenharmony_ci<!-- YAML 3871cb0ef41Sopenharmony_ciadded: 3881cb0ef41Sopenharmony_ci - v13.0.0 3891cb0ef41Sopenharmony_ci - v12.16.0 3901cb0ef41Sopenharmony_ci--> 3911cb0ef41Sopenharmony_ci 3921cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 3931cb0ef41Sopenharmony_ci 3941cb0ef41Sopenharmony_ciThis feature is only available with the `--experimental-vm-modules` command 3951cb0ef41Sopenharmony_ciflag enabled. 3961cb0ef41Sopenharmony_ci 3971cb0ef41Sopenharmony_ciThe `vm.Module` class provides a low-level interface for using 3981cb0ef41Sopenharmony_ciECMAScript modules in VM contexts. It is the counterpart of the `vm.Script` 3991cb0ef41Sopenharmony_ciclass that closely mirrors [Module Record][]s as defined in the ECMAScript 4001cb0ef41Sopenharmony_cispecification. 4011cb0ef41Sopenharmony_ci 4021cb0ef41Sopenharmony_ciUnlike `vm.Script` however, every `vm.Module` object is bound to a context from 4031cb0ef41Sopenharmony_ciits creation. Operations on `vm.Module` objects are intrinsically asynchronous, 4041cb0ef41Sopenharmony_ciin contrast with the synchronous nature of `vm.Script` objects. The use of 4051cb0ef41Sopenharmony_ci'async' functions can help with manipulating `vm.Module` objects. 4061cb0ef41Sopenharmony_ci 4071cb0ef41Sopenharmony_ciUsing a `vm.Module` object requires three distinct steps: creation/parsing, 4081cb0ef41Sopenharmony_cilinking, and evaluation. These three steps are illustrated in the following 4091cb0ef41Sopenharmony_ciexample. 4101cb0ef41Sopenharmony_ci 4111cb0ef41Sopenharmony_ciThis implementation lies at a lower level than the [ECMAScript Module 4121cb0ef41Sopenharmony_ciloader][]. There is also no way to interact with the Loader yet, though 4131cb0ef41Sopenharmony_cisupport is planned. 4141cb0ef41Sopenharmony_ci 4151cb0ef41Sopenharmony_ci```mjs 4161cb0ef41Sopenharmony_ciimport vm from 'node:vm'; 4171cb0ef41Sopenharmony_ci 4181cb0ef41Sopenharmony_ciconst contextifiedObject = vm.createContext({ 4191cb0ef41Sopenharmony_ci secret: 42, 4201cb0ef41Sopenharmony_ci print: console.log, 4211cb0ef41Sopenharmony_ci}); 4221cb0ef41Sopenharmony_ci 4231cb0ef41Sopenharmony_ci// Step 1 4241cb0ef41Sopenharmony_ci// 4251cb0ef41Sopenharmony_ci// Create a Module by constructing a new `vm.SourceTextModule` object. This 4261cb0ef41Sopenharmony_ci// parses the provided source text, throwing a `SyntaxError` if anything goes 4271cb0ef41Sopenharmony_ci// wrong. By default, a Module is created in the top context. But here, we 4281cb0ef41Sopenharmony_ci// specify `contextifiedObject` as the context this Module belongs to. 4291cb0ef41Sopenharmony_ci// 4301cb0ef41Sopenharmony_ci// Here, we attempt to obtain the default export from the module "foo", and 4311cb0ef41Sopenharmony_ci// put it into local binding "secret". 4321cb0ef41Sopenharmony_ci 4331cb0ef41Sopenharmony_ciconst bar = new vm.SourceTextModule(` 4341cb0ef41Sopenharmony_ci import s from 'foo'; 4351cb0ef41Sopenharmony_ci s; 4361cb0ef41Sopenharmony_ci print(s); 4371cb0ef41Sopenharmony_ci`, { context: contextifiedObject }); 4381cb0ef41Sopenharmony_ci 4391cb0ef41Sopenharmony_ci// Step 2 4401cb0ef41Sopenharmony_ci// 4411cb0ef41Sopenharmony_ci// "Link" the imported dependencies of this Module to it. 4421cb0ef41Sopenharmony_ci// 4431cb0ef41Sopenharmony_ci// The provided linking callback (the "linker") accepts two arguments: the 4441cb0ef41Sopenharmony_ci// parent module (`bar` in this case) and the string that is the specifier of 4451cb0ef41Sopenharmony_ci// the imported module. The callback is expected to return a Module that 4461cb0ef41Sopenharmony_ci// corresponds to the provided specifier, with certain requirements documented 4471cb0ef41Sopenharmony_ci// in `module.link()`. 4481cb0ef41Sopenharmony_ci// 4491cb0ef41Sopenharmony_ci// If linking has not started for the returned Module, the same linker 4501cb0ef41Sopenharmony_ci// callback will be called on the returned Module. 4511cb0ef41Sopenharmony_ci// 4521cb0ef41Sopenharmony_ci// Even top-level Modules without dependencies must be explicitly linked. The 4531cb0ef41Sopenharmony_ci// callback provided would never be called, however. 4541cb0ef41Sopenharmony_ci// 4551cb0ef41Sopenharmony_ci// The link() method returns a Promise that will be resolved when all the 4561cb0ef41Sopenharmony_ci// Promises returned by the linker resolve. 4571cb0ef41Sopenharmony_ci// 4581cb0ef41Sopenharmony_ci// Note: This is a contrived example in that the linker function creates a new 4591cb0ef41Sopenharmony_ci// "foo" module every time it is called. In a full-fledged module system, a 4601cb0ef41Sopenharmony_ci// cache would probably be used to avoid duplicated modules. 4611cb0ef41Sopenharmony_ci 4621cb0ef41Sopenharmony_ciasync function linker(specifier, referencingModule) { 4631cb0ef41Sopenharmony_ci if (specifier === 'foo') { 4641cb0ef41Sopenharmony_ci return new vm.SourceTextModule(` 4651cb0ef41Sopenharmony_ci // The "secret" variable refers to the global variable we added to 4661cb0ef41Sopenharmony_ci // "contextifiedObject" when creating the context. 4671cb0ef41Sopenharmony_ci export default secret; 4681cb0ef41Sopenharmony_ci `, { context: referencingModule.context }); 4691cb0ef41Sopenharmony_ci 4701cb0ef41Sopenharmony_ci // Using `contextifiedObject` instead of `referencingModule.context` 4711cb0ef41Sopenharmony_ci // here would work as well. 4721cb0ef41Sopenharmony_ci } 4731cb0ef41Sopenharmony_ci throw new Error(`Unable to resolve dependency: ${specifier}`); 4741cb0ef41Sopenharmony_ci} 4751cb0ef41Sopenharmony_ciawait bar.link(linker); 4761cb0ef41Sopenharmony_ci 4771cb0ef41Sopenharmony_ci// Step 3 4781cb0ef41Sopenharmony_ci// 4791cb0ef41Sopenharmony_ci// Evaluate the Module. The evaluate() method returns a promise which will 4801cb0ef41Sopenharmony_ci// resolve after the module has finished evaluating. 4811cb0ef41Sopenharmony_ci 4821cb0ef41Sopenharmony_ci// Prints 42. 4831cb0ef41Sopenharmony_ciawait bar.evaluate(); 4841cb0ef41Sopenharmony_ci``` 4851cb0ef41Sopenharmony_ci 4861cb0ef41Sopenharmony_ci```cjs 4871cb0ef41Sopenharmony_ciconst vm = require('node:vm'); 4881cb0ef41Sopenharmony_ci 4891cb0ef41Sopenharmony_ciconst contextifiedObject = vm.createContext({ 4901cb0ef41Sopenharmony_ci secret: 42, 4911cb0ef41Sopenharmony_ci print: console.log, 4921cb0ef41Sopenharmony_ci}); 4931cb0ef41Sopenharmony_ci 4941cb0ef41Sopenharmony_ci(async () => { 4951cb0ef41Sopenharmony_ci // Step 1 4961cb0ef41Sopenharmony_ci // 4971cb0ef41Sopenharmony_ci // Create a Module by constructing a new `vm.SourceTextModule` object. This 4981cb0ef41Sopenharmony_ci // parses the provided source text, throwing a `SyntaxError` if anything goes 4991cb0ef41Sopenharmony_ci // wrong. By default, a Module is created in the top context. But here, we 5001cb0ef41Sopenharmony_ci // specify `contextifiedObject` as the context this Module belongs to. 5011cb0ef41Sopenharmony_ci // 5021cb0ef41Sopenharmony_ci // Here, we attempt to obtain the default export from the module "foo", and 5031cb0ef41Sopenharmony_ci // put it into local binding "secret". 5041cb0ef41Sopenharmony_ci 5051cb0ef41Sopenharmony_ci const bar = new vm.SourceTextModule(` 5061cb0ef41Sopenharmony_ci import s from 'foo'; 5071cb0ef41Sopenharmony_ci s; 5081cb0ef41Sopenharmony_ci print(s); 5091cb0ef41Sopenharmony_ci `, { context: contextifiedObject }); 5101cb0ef41Sopenharmony_ci 5111cb0ef41Sopenharmony_ci // Step 2 5121cb0ef41Sopenharmony_ci // 5131cb0ef41Sopenharmony_ci // "Link" the imported dependencies of this Module to it. 5141cb0ef41Sopenharmony_ci // 5151cb0ef41Sopenharmony_ci // The provided linking callback (the "linker") accepts two arguments: the 5161cb0ef41Sopenharmony_ci // parent module (`bar` in this case) and the string that is the specifier of 5171cb0ef41Sopenharmony_ci // the imported module. The callback is expected to return a Module that 5181cb0ef41Sopenharmony_ci // corresponds to the provided specifier, with certain requirements documented 5191cb0ef41Sopenharmony_ci // in `module.link()`. 5201cb0ef41Sopenharmony_ci // 5211cb0ef41Sopenharmony_ci // If linking has not started for the returned Module, the same linker 5221cb0ef41Sopenharmony_ci // callback will be called on the returned Module. 5231cb0ef41Sopenharmony_ci // 5241cb0ef41Sopenharmony_ci // Even top-level Modules without dependencies must be explicitly linked. The 5251cb0ef41Sopenharmony_ci // callback provided would never be called, however. 5261cb0ef41Sopenharmony_ci // 5271cb0ef41Sopenharmony_ci // The link() method returns a Promise that will be resolved when all the 5281cb0ef41Sopenharmony_ci // Promises returned by the linker resolve. 5291cb0ef41Sopenharmony_ci // 5301cb0ef41Sopenharmony_ci // Note: This is a contrived example in that the linker function creates a new 5311cb0ef41Sopenharmony_ci // "foo" module every time it is called. In a full-fledged module system, a 5321cb0ef41Sopenharmony_ci // cache would probably be used to avoid duplicated modules. 5331cb0ef41Sopenharmony_ci 5341cb0ef41Sopenharmony_ci async function linker(specifier, referencingModule) { 5351cb0ef41Sopenharmony_ci if (specifier === 'foo') { 5361cb0ef41Sopenharmony_ci return new vm.SourceTextModule(` 5371cb0ef41Sopenharmony_ci // The "secret" variable refers to the global variable we added to 5381cb0ef41Sopenharmony_ci // "contextifiedObject" when creating the context. 5391cb0ef41Sopenharmony_ci export default secret; 5401cb0ef41Sopenharmony_ci `, { context: referencingModule.context }); 5411cb0ef41Sopenharmony_ci 5421cb0ef41Sopenharmony_ci // Using `contextifiedObject` instead of `referencingModule.context` 5431cb0ef41Sopenharmony_ci // here would work as well. 5441cb0ef41Sopenharmony_ci } 5451cb0ef41Sopenharmony_ci throw new Error(`Unable to resolve dependency: ${specifier}`); 5461cb0ef41Sopenharmony_ci } 5471cb0ef41Sopenharmony_ci await bar.link(linker); 5481cb0ef41Sopenharmony_ci 5491cb0ef41Sopenharmony_ci // Step 3 5501cb0ef41Sopenharmony_ci // 5511cb0ef41Sopenharmony_ci // Evaluate the Module. The evaluate() method returns a promise which will 5521cb0ef41Sopenharmony_ci // resolve after the module has finished evaluating. 5531cb0ef41Sopenharmony_ci 5541cb0ef41Sopenharmony_ci // Prints 42. 5551cb0ef41Sopenharmony_ci await bar.evaluate(); 5561cb0ef41Sopenharmony_ci})(); 5571cb0ef41Sopenharmony_ci``` 5581cb0ef41Sopenharmony_ci 5591cb0ef41Sopenharmony_ci### `module.dependencySpecifiers` 5601cb0ef41Sopenharmony_ci 5611cb0ef41Sopenharmony_ci* {string\[]} 5621cb0ef41Sopenharmony_ci 5631cb0ef41Sopenharmony_ciThe specifiers of all dependencies of this module. The returned array is frozen 5641cb0ef41Sopenharmony_cito disallow any changes to it. 5651cb0ef41Sopenharmony_ci 5661cb0ef41Sopenharmony_ciCorresponds to the `[[RequestedModules]]` field of [Cyclic Module Record][]s in 5671cb0ef41Sopenharmony_cithe ECMAScript specification. 5681cb0ef41Sopenharmony_ci 5691cb0ef41Sopenharmony_ci### `module.error` 5701cb0ef41Sopenharmony_ci 5711cb0ef41Sopenharmony_ci* {any} 5721cb0ef41Sopenharmony_ci 5731cb0ef41Sopenharmony_ciIf the `module.status` is `'errored'`, this property contains the exception 5741cb0ef41Sopenharmony_cithrown by the module during evaluation. If the status is anything else, 5751cb0ef41Sopenharmony_ciaccessing this property will result in a thrown exception. 5761cb0ef41Sopenharmony_ci 5771cb0ef41Sopenharmony_ciThe value `undefined` cannot be used for cases where there is not a thrown 5781cb0ef41Sopenharmony_ciexception due to possible ambiguity with `throw undefined;`. 5791cb0ef41Sopenharmony_ci 5801cb0ef41Sopenharmony_ciCorresponds to the `[[EvaluationError]]` field of [Cyclic Module Record][]s 5811cb0ef41Sopenharmony_ciin the ECMAScript specification. 5821cb0ef41Sopenharmony_ci 5831cb0ef41Sopenharmony_ci### `module.evaluate([options])` 5841cb0ef41Sopenharmony_ci 5851cb0ef41Sopenharmony_ci* `options` {Object} 5861cb0ef41Sopenharmony_ci * `timeout` {integer} Specifies the number of milliseconds to evaluate 5871cb0ef41Sopenharmony_ci before terminating execution. If execution is interrupted, an [`Error`][] 5881cb0ef41Sopenharmony_ci will be thrown. This value must be a strictly positive integer. 5891cb0ef41Sopenharmony_ci * `breakOnSigint` {boolean} If `true`, receiving `SIGINT` 5901cb0ef41Sopenharmony_ci (<kbd>Ctrl</kbd>+<kbd>C</kbd>) will terminate execution and throw an 5911cb0ef41Sopenharmony_ci [`Error`][]. Existing handlers for the event that have been attached via 5921cb0ef41Sopenharmony_ci `process.on('SIGINT')` are disabled during script execution, but continue to 5931cb0ef41Sopenharmony_ci work after that. **Default:** `false`. 5941cb0ef41Sopenharmony_ci* Returns: {Promise} Fulfills with `undefined` upon success. 5951cb0ef41Sopenharmony_ci 5961cb0ef41Sopenharmony_ciEvaluate the module. 5971cb0ef41Sopenharmony_ci 5981cb0ef41Sopenharmony_ciThis must be called after the module has been linked; otherwise it will reject. 5991cb0ef41Sopenharmony_ciIt could be called also when the module has already been evaluated, in which 6001cb0ef41Sopenharmony_cicase it will either do nothing if the initial evaluation ended in success 6011cb0ef41Sopenharmony_ci(`module.status` is `'evaluated'`) or it will re-throw the exception that the 6021cb0ef41Sopenharmony_ciinitial evaluation resulted in (`module.status` is `'errored'`). 6031cb0ef41Sopenharmony_ci 6041cb0ef41Sopenharmony_ciThis method cannot be called while the module is being evaluated 6051cb0ef41Sopenharmony_ci(`module.status` is `'evaluating'`). 6061cb0ef41Sopenharmony_ci 6071cb0ef41Sopenharmony_ciCorresponds to the [Evaluate() concrete method][] field of [Cyclic Module 6081cb0ef41Sopenharmony_ciRecord][]s in the ECMAScript specification. 6091cb0ef41Sopenharmony_ci 6101cb0ef41Sopenharmony_ci### `module.identifier` 6111cb0ef41Sopenharmony_ci 6121cb0ef41Sopenharmony_ci* {string} 6131cb0ef41Sopenharmony_ci 6141cb0ef41Sopenharmony_ciThe identifier of the current module, as set in the constructor. 6151cb0ef41Sopenharmony_ci 6161cb0ef41Sopenharmony_ci### `module.link(linker)` 6171cb0ef41Sopenharmony_ci 6181cb0ef41Sopenharmony_ci<!-- YAML 6191cb0ef41Sopenharmony_cichanges: 6201cb0ef41Sopenharmony_ci - version: v18.19.0 6211cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/50141 6221cb0ef41Sopenharmony_ci description: The option `extra.assert` is renamed to `extra.attributes`. The 6231cb0ef41Sopenharmony_ci former name is still provided for backward compatibility. 6241cb0ef41Sopenharmony_ci--> 6251cb0ef41Sopenharmony_ci 6261cb0ef41Sopenharmony_ci* `linker` {Function} 6271cb0ef41Sopenharmony_ci * `specifier` {string} The specifier of the requested module: 6281cb0ef41Sopenharmony_ci ```mjs 6291cb0ef41Sopenharmony_ci import foo from 'foo'; 6301cb0ef41Sopenharmony_ci // ^^^^^ the module specifier 6311cb0ef41Sopenharmony_ci ``` 6321cb0ef41Sopenharmony_ci 6331cb0ef41Sopenharmony_ci * `referencingModule` {vm.Module} The `Module` object `link()` is called on. 6341cb0ef41Sopenharmony_ci 6351cb0ef41Sopenharmony_ci * `extra` {Object} 6361cb0ef41Sopenharmony_ci * `attributes` {Object} The data from the attribute: 6371cb0ef41Sopenharmony_ci ```mjs 6381cb0ef41Sopenharmony_ci import foo from 'foo' with { name: 'value' }; 6391cb0ef41Sopenharmony_ci // ^^^^^^^^^^^^^^^^^ the attribute 6401cb0ef41Sopenharmony_ci ``` 6411cb0ef41Sopenharmony_ci Per ECMA-262, hosts are expected to trigger an error if an 6421cb0ef41Sopenharmony_ci unsupported attribute is present. 6431cb0ef41Sopenharmony_ci * `assert` {Object} Alias for `extra.attributes`. 6441cb0ef41Sopenharmony_ci 6451cb0ef41Sopenharmony_ci * Returns: {vm.Module|Promise} 6461cb0ef41Sopenharmony_ci* Returns: {Promise} 6471cb0ef41Sopenharmony_ci 6481cb0ef41Sopenharmony_ciLink module dependencies. This method must be called before evaluation, and 6491cb0ef41Sopenharmony_cican only be called once per module. 6501cb0ef41Sopenharmony_ci 6511cb0ef41Sopenharmony_ciThe function is expected to return a `Module` object or a `Promise` that 6521cb0ef41Sopenharmony_cieventually resolves to a `Module` object. The returned `Module` must satisfy the 6531cb0ef41Sopenharmony_cifollowing two invariants: 6541cb0ef41Sopenharmony_ci 6551cb0ef41Sopenharmony_ci* It must belong to the same context as the parent `Module`. 6561cb0ef41Sopenharmony_ci* Its `status` must not be `'errored'`. 6571cb0ef41Sopenharmony_ci 6581cb0ef41Sopenharmony_ciIf the returned `Module`'s `status` is `'unlinked'`, this method will be 6591cb0ef41Sopenharmony_cirecursively called on the returned `Module` with the same provided `linker` 6601cb0ef41Sopenharmony_cifunction. 6611cb0ef41Sopenharmony_ci 6621cb0ef41Sopenharmony_ci`link()` returns a `Promise` that will either get resolved when all linking 6631cb0ef41Sopenharmony_ciinstances resolve to a valid `Module`, or rejected if the linker function either 6641cb0ef41Sopenharmony_cithrows an exception or returns an invalid `Module`. 6651cb0ef41Sopenharmony_ci 6661cb0ef41Sopenharmony_ciThe linker function roughly corresponds to the implementation-defined 6671cb0ef41Sopenharmony_ci[HostResolveImportedModule][] abstract operation in the ECMAScript 6681cb0ef41Sopenharmony_cispecification, with a few key differences: 6691cb0ef41Sopenharmony_ci 6701cb0ef41Sopenharmony_ci* The linker function is allowed to be asynchronous while 6711cb0ef41Sopenharmony_ci [HostResolveImportedModule][] is synchronous. 6721cb0ef41Sopenharmony_ci 6731cb0ef41Sopenharmony_ciThe actual [HostResolveImportedModule][] implementation used during module 6741cb0ef41Sopenharmony_cilinking is one that returns the modules linked during linking. Since at 6751cb0ef41Sopenharmony_cithat point all modules would have been fully linked already, the 6761cb0ef41Sopenharmony_ci[HostResolveImportedModule][] implementation is fully synchronous per 6771cb0ef41Sopenharmony_cispecification. 6781cb0ef41Sopenharmony_ci 6791cb0ef41Sopenharmony_ciCorresponds to the [Link() concrete method][] field of [Cyclic Module 6801cb0ef41Sopenharmony_ciRecord][]s in the ECMAScript specification. 6811cb0ef41Sopenharmony_ci 6821cb0ef41Sopenharmony_ci### `module.namespace` 6831cb0ef41Sopenharmony_ci 6841cb0ef41Sopenharmony_ci* {Object} 6851cb0ef41Sopenharmony_ci 6861cb0ef41Sopenharmony_ciThe namespace object of the module. This is only available after linking 6871cb0ef41Sopenharmony_ci(`module.link()`) has completed. 6881cb0ef41Sopenharmony_ci 6891cb0ef41Sopenharmony_ciCorresponds to the [GetModuleNamespace][] abstract operation in the ECMAScript 6901cb0ef41Sopenharmony_cispecification. 6911cb0ef41Sopenharmony_ci 6921cb0ef41Sopenharmony_ci### `module.status` 6931cb0ef41Sopenharmony_ci 6941cb0ef41Sopenharmony_ci* {string} 6951cb0ef41Sopenharmony_ci 6961cb0ef41Sopenharmony_ciThe current status of the module. Will be one of: 6971cb0ef41Sopenharmony_ci 6981cb0ef41Sopenharmony_ci* `'unlinked'`: `module.link()` has not yet been called. 6991cb0ef41Sopenharmony_ci 7001cb0ef41Sopenharmony_ci* `'linking'`: `module.link()` has been called, but not all Promises returned 7011cb0ef41Sopenharmony_ci by the linker function have been resolved yet. 7021cb0ef41Sopenharmony_ci 7031cb0ef41Sopenharmony_ci* `'linked'`: The module has been linked successfully, and all of its 7041cb0ef41Sopenharmony_ci dependencies are linked, but `module.evaluate()` has not yet been called. 7051cb0ef41Sopenharmony_ci 7061cb0ef41Sopenharmony_ci* `'evaluating'`: The module is being evaluated through a `module.evaluate()` on 7071cb0ef41Sopenharmony_ci itself or a parent module. 7081cb0ef41Sopenharmony_ci 7091cb0ef41Sopenharmony_ci* `'evaluated'`: The module has been successfully evaluated. 7101cb0ef41Sopenharmony_ci 7111cb0ef41Sopenharmony_ci* `'errored'`: The module has been evaluated, but an exception was thrown. 7121cb0ef41Sopenharmony_ci 7131cb0ef41Sopenharmony_ciOther than `'errored'`, this status string corresponds to the specification's 7141cb0ef41Sopenharmony_ci[Cyclic Module Record][]'s `[[Status]]` field. `'errored'` corresponds to 7151cb0ef41Sopenharmony_ci`'evaluated'` in the specification, but with `[[EvaluationError]]` set to a 7161cb0ef41Sopenharmony_civalue that is not `undefined`. 7171cb0ef41Sopenharmony_ci 7181cb0ef41Sopenharmony_ci## Class: `vm.SourceTextModule` 7191cb0ef41Sopenharmony_ci 7201cb0ef41Sopenharmony_ci<!-- YAML 7211cb0ef41Sopenharmony_ciadded: v9.6.0 7221cb0ef41Sopenharmony_ci--> 7231cb0ef41Sopenharmony_ci 7241cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 7251cb0ef41Sopenharmony_ci 7261cb0ef41Sopenharmony_ciThis feature is only available with the `--experimental-vm-modules` command 7271cb0ef41Sopenharmony_ciflag enabled. 7281cb0ef41Sopenharmony_ci 7291cb0ef41Sopenharmony_ci* Extends: {vm.Module} 7301cb0ef41Sopenharmony_ci 7311cb0ef41Sopenharmony_ciThe `vm.SourceTextModule` class provides the [Source Text Module Record][] as 7321cb0ef41Sopenharmony_cidefined in the ECMAScript specification. 7331cb0ef41Sopenharmony_ci 7341cb0ef41Sopenharmony_ci### `new vm.SourceTextModule(code[, options])` 7351cb0ef41Sopenharmony_ci 7361cb0ef41Sopenharmony_ci<!-- YAML 7371cb0ef41Sopenharmony_cichanges: 7381cb0ef41Sopenharmony_ci - version: 7391cb0ef41Sopenharmony_ci - v17.0.0 7401cb0ef41Sopenharmony_ci - v16.12.0 7411cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/40249 7421cb0ef41Sopenharmony_ci description: Added support for import attributes to the 7431cb0ef41Sopenharmony_ci `importModuleDynamically` parameter. 7441cb0ef41Sopenharmony_ci--> 7451cb0ef41Sopenharmony_ci 7461cb0ef41Sopenharmony_ci* `code` {string} JavaScript Module code to parse 7471cb0ef41Sopenharmony_ci* `options` 7481cb0ef41Sopenharmony_ci * `identifier` {string} String used in stack traces. 7491cb0ef41Sopenharmony_ci **Default:** `'vm:module(i)'` where `i` is a context-specific ascending 7501cb0ef41Sopenharmony_ci index. 7511cb0ef41Sopenharmony_ci * `cachedData` {Buffer|TypedArray|DataView} Provides an optional `Buffer` or 7521cb0ef41Sopenharmony_ci `TypedArray`, or `DataView` with V8's code cache data for the supplied 7531cb0ef41Sopenharmony_ci source. The `code` must be the same as the module from which this 7541cb0ef41Sopenharmony_ci `cachedData` was created. 7551cb0ef41Sopenharmony_ci * `context` {Object} The [contextified][] object as returned by the 7561cb0ef41Sopenharmony_ci `vm.createContext()` method, to compile and evaluate this `Module` in. 7571cb0ef41Sopenharmony_ci If no context is specified, the module is evaluated in the current 7581cb0ef41Sopenharmony_ci execution context. 7591cb0ef41Sopenharmony_ci * `lineOffset` {integer} Specifies the line number offset that is displayed 7601cb0ef41Sopenharmony_ci in stack traces produced by this `Module`. **Default:** `0`. 7611cb0ef41Sopenharmony_ci * `columnOffset` {integer} Specifies the first-line column number offset that 7621cb0ef41Sopenharmony_ci is displayed in stack traces produced by this `Module`. **Default:** `0`. 7631cb0ef41Sopenharmony_ci * `initializeImportMeta` {Function} Called during evaluation of this `Module` 7641cb0ef41Sopenharmony_ci to initialize the `import.meta`. 7651cb0ef41Sopenharmony_ci * `meta` {import.meta} 7661cb0ef41Sopenharmony_ci * `module` {vm.SourceTextModule} 7671cb0ef41Sopenharmony_ci * `importModuleDynamically` {Function} Called during evaluation of this module 7681cb0ef41Sopenharmony_ci when `import()` is called. If this option is not specified, calls to 7691cb0ef41Sopenharmony_ci `import()` will reject with [`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING`][]. 7701cb0ef41Sopenharmony_ci If `--experimental-vm-modules` isn't set, this callback will be ignored 7711cb0ef41Sopenharmony_ci and calls to `import()` will reject with 7721cb0ef41Sopenharmony_ci [`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG`][]. 7731cb0ef41Sopenharmony_ci * `specifier` {string} specifier passed to `import()` 7741cb0ef41Sopenharmony_ci * `module` {vm.Module} 7751cb0ef41Sopenharmony_ci * `importAttributes` {Object} The `"assert"` value passed to the 7761cb0ef41Sopenharmony_ci [`optionsExpression`][] optional parameter, or an empty object if no value 7771cb0ef41Sopenharmony_ci was provided. 7781cb0ef41Sopenharmony_ci * Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is 7791cb0ef41Sopenharmony_ci recommended in order to take advantage of error tracking, and to avoid 7801cb0ef41Sopenharmony_ci issues with namespaces that contain `then` function exports. 7811cb0ef41Sopenharmony_ci 7821cb0ef41Sopenharmony_ciCreates a new `SourceTextModule` instance. 7831cb0ef41Sopenharmony_ci 7841cb0ef41Sopenharmony_ciProperties assigned to the `import.meta` object that are objects may 7851cb0ef41Sopenharmony_ciallow the module to access information outside the specified `context`. Use 7861cb0ef41Sopenharmony_ci`vm.runInContext()` to create objects in a specific context. 7871cb0ef41Sopenharmony_ci 7881cb0ef41Sopenharmony_ci```mjs 7891cb0ef41Sopenharmony_ciimport vm from 'node:vm'; 7901cb0ef41Sopenharmony_ci 7911cb0ef41Sopenharmony_ciconst contextifiedObject = vm.createContext({ secret: 42 }); 7921cb0ef41Sopenharmony_ci 7931cb0ef41Sopenharmony_ciconst module = new vm.SourceTextModule( 7941cb0ef41Sopenharmony_ci 'Object.getPrototypeOf(import.meta.prop).secret = secret;', 7951cb0ef41Sopenharmony_ci { 7961cb0ef41Sopenharmony_ci initializeImportMeta(meta) { 7971cb0ef41Sopenharmony_ci // Note: this object is created in the top context. As such, 7981cb0ef41Sopenharmony_ci // Object.getPrototypeOf(import.meta.prop) points to the 7991cb0ef41Sopenharmony_ci // Object.prototype in the top context rather than that in 8001cb0ef41Sopenharmony_ci // the contextified object. 8011cb0ef41Sopenharmony_ci meta.prop = {}; 8021cb0ef41Sopenharmony_ci }, 8031cb0ef41Sopenharmony_ci }); 8041cb0ef41Sopenharmony_ci// Since module has no dependencies, the linker function will never be called. 8051cb0ef41Sopenharmony_ciawait module.link(() => {}); 8061cb0ef41Sopenharmony_ciawait module.evaluate(); 8071cb0ef41Sopenharmony_ci 8081cb0ef41Sopenharmony_ci// Now, Object.prototype.secret will be equal to 42. 8091cb0ef41Sopenharmony_ci// 8101cb0ef41Sopenharmony_ci// To fix this problem, replace 8111cb0ef41Sopenharmony_ci// meta.prop = {}; 8121cb0ef41Sopenharmony_ci// above with 8131cb0ef41Sopenharmony_ci// meta.prop = vm.runInContext('{}', contextifiedObject); 8141cb0ef41Sopenharmony_ci``` 8151cb0ef41Sopenharmony_ci 8161cb0ef41Sopenharmony_ci```cjs 8171cb0ef41Sopenharmony_ciconst vm = require('node:vm'); 8181cb0ef41Sopenharmony_ciconst contextifiedObject = vm.createContext({ secret: 42 }); 8191cb0ef41Sopenharmony_ci(async () => { 8201cb0ef41Sopenharmony_ci const module = new vm.SourceTextModule( 8211cb0ef41Sopenharmony_ci 'Object.getPrototypeOf(import.meta.prop).secret = secret;', 8221cb0ef41Sopenharmony_ci { 8231cb0ef41Sopenharmony_ci initializeImportMeta(meta) { 8241cb0ef41Sopenharmony_ci // Note: this object is created in the top context. As such, 8251cb0ef41Sopenharmony_ci // Object.getPrototypeOf(import.meta.prop) points to the 8261cb0ef41Sopenharmony_ci // Object.prototype in the top context rather than that in 8271cb0ef41Sopenharmony_ci // the contextified object. 8281cb0ef41Sopenharmony_ci meta.prop = {}; 8291cb0ef41Sopenharmony_ci }, 8301cb0ef41Sopenharmony_ci }); 8311cb0ef41Sopenharmony_ci // Since module has no dependencies, the linker function will never be called. 8321cb0ef41Sopenharmony_ci await module.link(() => {}); 8331cb0ef41Sopenharmony_ci await module.evaluate(); 8341cb0ef41Sopenharmony_ci // Now, Object.prototype.secret will be equal to 42. 8351cb0ef41Sopenharmony_ci // 8361cb0ef41Sopenharmony_ci // To fix this problem, replace 8371cb0ef41Sopenharmony_ci // meta.prop = {}; 8381cb0ef41Sopenharmony_ci // above with 8391cb0ef41Sopenharmony_ci // meta.prop = vm.runInContext('{}', contextifiedObject); 8401cb0ef41Sopenharmony_ci})(); 8411cb0ef41Sopenharmony_ci``` 8421cb0ef41Sopenharmony_ci 8431cb0ef41Sopenharmony_ci### `sourceTextModule.createCachedData()` 8441cb0ef41Sopenharmony_ci 8451cb0ef41Sopenharmony_ci<!-- YAML 8461cb0ef41Sopenharmony_ciadded: 8471cb0ef41Sopenharmony_ci - v13.7.0 8481cb0ef41Sopenharmony_ci - v12.17.0 8491cb0ef41Sopenharmony_ci--> 8501cb0ef41Sopenharmony_ci 8511cb0ef41Sopenharmony_ci* Returns: {Buffer} 8521cb0ef41Sopenharmony_ci 8531cb0ef41Sopenharmony_ciCreates a code cache that can be used with the `SourceTextModule` constructor's 8541cb0ef41Sopenharmony_ci`cachedData` option. Returns a `Buffer`. This method may be called any number 8551cb0ef41Sopenharmony_ciof times before the module has been evaluated. 8561cb0ef41Sopenharmony_ci 8571cb0ef41Sopenharmony_ciThe code cache of the `SourceTextModule` doesn't contain any JavaScript 8581cb0ef41Sopenharmony_ciobservable states. The code cache is safe to be saved along side the script 8591cb0ef41Sopenharmony_cisource and used to construct new `SourceTextModule` instances multiple times. 8601cb0ef41Sopenharmony_ci 8611cb0ef41Sopenharmony_ciFunctions in the `SourceTextModule` source can be marked as lazily compiled 8621cb0ef41Sopenharmony_ciand they are not compiled at construction of the `SourceTextModule`. These 8631cb0ef41Sopenharmony_cifunctions are going to be compiled when they are invoked the first time. The 8641cb0ef41Sopenharmony_cicode cache serializes the metadata that V8 currently knows about the 8651cb0ef41Sopenharmony_ci`SourceTextModule` that it can use to speed up future compilations. 8661cb0ef41Sopenharmony_ci 8671cb0ef41Sopenharmony_ci```js 8681cb0ef41Sopenharmony_ci// Create an initial module 8691cb0ef41Sopenharmony_ciconst module = new vm.SourceTextModule('const a = 1;'); 8701cb0ef41Sopenharmony_ci 8711cb0ef41Sopenharmony_ci// Create cached data from this module 8721cb0ef41Sopenharmony_ciconst cachedData = module.createCachedData(); 8731cb0ef41Sopenharmony_ci 8741cb0ef41Sopenharmony_ci// Create a new module using the cached data. The code must be the same. 8751cb0ef41Sopenharmony_ciconst module2 = new vm.SourceTextModule('const a = 1;', { cachedData }); 8761cb0ef41Sopenharmony_ci``` 8771cb0ef41Sopenharmony_ci 8781cb0ef41Sopenharmony_ci## Class: `vm.SyntheticModule` 8791cb0ef41Sopenharmony_ci 8801cb0ef41Sopenharmony_ci<!-- YAML 8811cb0ef41Sopenharmony_ciadded: 8821cb0ef41Sopenharmony_ci - v13.0.0 8831cb0ef41Sopenharmony_ci - v12.16.0 8841cb0ef41Sopenharmony_ci--> 8851cb0ef41Sopenharmony_ci 8861cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 8871cb0ef41Sopenharmony_ci 8881cb0ef41Sopenharmony_ciThis feature is only available with the `--experimental-vm-modules` command 8891cb0ef41Sopenharmony_ciflag enabled. 8901cb0ef41Sopenharmony_ci 8911cb0ef41Sopenharmony_ci* Extends: {vm.Module} 8921cb0ef41Sopenharmony_ci 8931cb0ef41Sopenharmony_ciThe `vm.SyntheticModule` class provides the [Synthetic Module Record][] as 8941cb0ef41Sopenharmony_cidefined in the WebIDL specification. The purpose of synthetic modules is to 8951cb0ef41Sopenharmony_ciprovide a generic interface for exposing non-JavaScript sources to ECMAScript 8961cb0ef41Sopenharmony_cimodule graphs. 8971cb0ef41Sopenharmony_ci 8981cb0ef41Sopenharmony_ci```js 8991cb0ef41Sopenharmony_ciconst vm = require('node:vm'); 9001cb0ef41Sopenharmony_ci 9011cb0ef41Sopenharmony_ciconst source = '{ "a": 1 }'; 9021cb0ef41Sopenharmony_ciconst module = new vm.SyntheticModule(['default'], function() { 9031cb0ef41Sopenharmony_ci const obj = JSON.parse(source); 9041cb0ef41Sopenharmony_ci this.setExport('default', obj); 9051cb0ef41Sopenharmony_ci}); 9061cb0ef41Sopenharmony_ci 9071cb0ef41Sopenharmony_ci// Use `module` in linking... 9081cb0ef41Sopenharmony_ci``` 9091cb0ef41Sopenharmony_ci 9101cb0ef41Sopenharmony_ci### `new vm.SyntheticModule(exportNames, evaluateCallback[, options])` 9111cb0ef41Sopenharmony_ci 9121cb0ef41Sopenharmony_ci<!-- YAML 9131cb0ef41Sopenharmony_ciadded: 9141cb0ef41Sopenharmony_ci - v13.0.0 9151cb0ef41Sopenharmony_ci - v12.16.0 9161cb0ef41Sopenharmony_ci--> 9171cb0ef41Sopenharmony_ci 9181cb0ef41Sopenharmony_ci* `exportNames` {string\[]} Array of names that will be exported from the 9191cb0ef41Sopenharmony_ci module. 9201cb0ef41Sopenharmony_ci* `evaluateCallback` {Function} Called when the module is evaluated. 9211cb0ef41Sopenharmony_ci* `options` 9221cb0ef41Sopenharmony_ci * `identifier` {string} String used in stack traces. 9231cb0ef41Sopenharmony_ci **Default:** `'vm:module(i)'` where `i` is a context-specific ascending 9241cb0ef41Sopenharmony_ci index. 9251cb0ef41Sopenharmony_ci * `context` {Object} The [contextified][] object as returned by the 9261cb0ef41Sopenharmony_ci `vm.createContext()` method, to compile and evaluate this `Module` in. 9271cb0ef41Sopenharmony_ci 9281cb0ef41Sopenharmony_ciCreates a new `SyntheticModule` instance. 9291cb0ef41Sopenharmony_ci 9301cb0ef41Sopenharmony_ciObjects assigned to the exports of this instance may allow importers of 9311cb0ef41Sopenharmony_cithe module to access information outside the specified `context`. Use 9321cb0ef41Sopenharmony_ci`vm.runInContext()` to create objects in a specific context. 9331cb0ef41Sopenharmony_ci 9341cb0ef41Sopenharmony_ci### `syntheticModule.setExport(name, value)` 9351cb0ef41Sopenharmony_ci 9361cb0ef41Sopenharmony_ci<!-- YAML 9371cb0ef41Sopenharmony_ciadded: 9381cb0ef41Sopenharmony_ci - v13.0.0 9391cb0ef41Sopenharmony_ci - v12.16.0 9401cb0ef41Sopenharmony_ci--> 9411cb0ef41Sopenharmony_ci 9421cb0ef41Sopenharmony_ci* `name` {string} Name of the export to set. 9431cb0ef41Sopenharmony_ci* `value` {any} The value to set the export to. 9441cb0ef41Sopenharmony_ci 9451cb0ef41Sopenharmony_ciThis method is used after the module is linked to set the values of exports. If 9461cb0ef41Sopenharmony_ciit is called before the module is linked, an [`ERR_VM_MODULE_STATUS`][] error 9471cb0ef41Sopenharmony_ciwill be thrown. 9481cb0ef41Sopenharmony_ci 9491cb0ef41Sopenharmony_ci```mjs 9501cb0ef41Sopenharmony_ciimport vm from 'node:vm'; 9511cb0ef41Sopenharmony_ci 9521cb0ef41Sopenharmony_ciconst m = new vm.SyntheticModule(['x'], () => { 9531cb0ef41Sopenharmony_ci m.setExport('x', 1); 9541cb0ef41Sopenharmony_ci}); 9551cb0ef41Sopenharmony_ci 9561cb0ef41Sopenharmony_ciawait m.link(() => {}); 9571cb0ef41Sopenharmony_ciawait m.evaluate(); 9581cb0ef41Sopenharmony_ci 9591cb0ef41Sopenharmony_ciassert.strictEqual(m.namespace.x, 1); 9601cb0ef41Sopenharmony_ci``` 9611cb0ef41Sopenharmony_ci 9621cb0ef41Sopenharmony_ci```cjs 9631cb0ef41Sopenharmony_ciconst vm = require('node:vm'); 9641cb0ef41Sopenharmony_ci(async () => { 9651cb0ef41Sopenharmony_ci const m = new vm.SyntheticModule(['x'], () => { 9661cb0ef41Sopenharmony_ci m.setExport('x', 1); 9671cb0ef41Sopenharmony_ci }); 9681cb0ef41Sopenharmony_ci await m.link(() => {}); 9691cb0ef41Sopenharmony_ci await m.evaluate(); 9701cb0ef41Sopenharmony_ci assert.strictEqual(m.namespace.x, 1); 9711cb0ef41Sopenharmony_ci})(); 9721cb0ef41Sopenharmony_ci``` 9731cb0ef41Sopenharmony_ci 9741cb0ef41Sopenharmony_ci## `vm.compileFunction(code[, params[, options]])` 9751cb0ef41Sopenharmony_ci 9761cb0ef41Sopenharmony_ci<!-- YAML 9771cb0ef41Sopenharmony_ciadded: v10.10.0 9781cb0ef41Sopenharmony_cichanges: 9791cb0ef41Sopenharmony_ci - version: 9801cb0ef41Sopenharmony_ci - v18.15.0 9811cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/46320 9821cb0ef41Sopenharmony_ci description: The return value now includes `cachedDataRejected` 9831cb0ef41Sopenharmony_ci with the same semantics as the `vm.Script` version 9841cb0ef41Sopenharmony_ci if the `cachedData` option was passed. 9851cb0ef41Sopenharmony_ci - version: 9861cb0ef41Sopenharmony_ci - v17.0.0 9871cb0ef41Sopenharmony_ci - v16.12.0 9881cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/40249 9891cb0ef41Sopenharmony_ci description: Added support for import attributes to the 9901cb0ef41Sopenharmony_ci `importModuleDynamically` parameter. 9911cb0ef41Sopenharmony_ci - version: v15.9.0 9921cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35431 9931cb0ef41Sopenharmony_ci description: Added `importModuleDynamically` option again. 9941cb0ef41Sopenharmony_ci - version: v14.3.0 9951cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/33364 9961cb0ef41Sopenharmony_ci description: Removal of `importModuleDynamically` due to compatibility 9971cb0ef41Sopenharmony_ci issues. 9981cb0ef41Sopenharmony_ci - version: 9991cb0ef41Sopenharmony_ci - v14.1.0 10001cb0ef41Sopenharmony_ci - v13.14.0 10011cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/32985 10021cb0ef41Sopenharmony_ci description: The `importModuleDynamically` option is now supported. 10031cb0ef41Sopenharmony_ci--> 10041cb0ef41Sopenharmony_ci 10051cb0ef41Sopenharmony_ci* `code` {string} The body of the function to compile. 10061cb0ef41Sopenharmony_ci* `params` {string\[]} An array of strings containing all parameters for the 10071cb0ef41Sopenharmony_ci function. 10081cb0ef41Sopenharmony_ci* `options` {Object} 10091cb0ef41Sopenharmony_ci * `filename` {string} Specifies the filename used in stack traces produced 10101cb0ef41Sopenharmony_ci by this script. **Default:** `''`. 10111cb0ef41Sopenharmony_ci * `lineOffset` {number} Specifies the line number offset that is displayed 10121cb0ef41Sopenharmony_ci in stack traces produced by this script. **Default:** `0`. 10131cb0ef41Sopenharmony_ci * `columnOffset` {number} Specifies the first-line column number offset that 10141cb0ef41Sopenharmony_ci is displayed in stack traces produced by this script. **Default:** `0`. 10151cb0ef41Sopenharmony_ci * `cachedData` {Buffer|TypedArray|DataView} Provides an optional `Buffer` or 10161cb0ef41Sopenharmony_ci `TypedArray`, or `DataView` with V8's code cache data for the supplied 10171cb0ef41Sopenharmony_ci source. This must be produced by a prior call to [`vm.compileFunction()`][] 10181cb0ef41Sopenharmony_ci with the same `code` and `params`. 10191cb0ef41Sopenharmony_ci * `produceCachedData` {boolean} Specifies whether to produce new cache data. 10201cb0ef41Sopenharmony_ci **Default:** `false`. 10211cb0ef41Sopenharmony_ci * `parsingContext` {Object} The [contextified][] object in which the said 10221cb0ef41Sopenharmony_ci function should be compiled in. 10231cb0ef41Sopenharmony_ci * `contextExtensions` {Object\[]} An array containing a collection of context 10241cb0ef41Sopenharmony_ci extensions (objects wrapping the current scope) to be applied while 10251cb0ef41Sopenharmony_ci compiling. **Default:** `[]`. 10261cb0ef41Sopenharmony_ci * `importModuleDynamically` {Function} Called during evaluation of this module 10271cb0ef41Sopenharmony_ci when `import()` is called. If this option is not specified, calls to 10281cb0ef41Sopenharmony_ci `import()` will reject with [`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING`][]. 10291cb0ef41Sopenharmony_ci This option is part of the experimental modules API, and should not be 10301cb0ef41Sopenharmony_ci considered stable. If `--experimental-vm-modules` isn't 10311cb0ef41Sopenharmony_ci set, this callback will be ignored and calls to `import()` will reject with 10321cb0ef41Sopenharmony_ci [`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG`][]. 10331cb0ef41Sopenharmony_ci * `specifier` {string} specifier passed to `import()` 10341cb0ef41Sopenharmony_ci * `function` {Function} 10351cb0ef41Sopenharmony_ci * `importAttributes` {Object} The `"with"` value passed to the 10361cb0ef41Sopenharmony_ci [`optionsExpression`][] optional parameter, or an empty object if no value 10371cb0ef41Sopenharmony_ci was provided. 10381cb0ef41Sopenharmony_ci * Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is 10391cb0ef41Sopenharmony_ci recommended in order to take advantage of error tracking, and to avoid 10401cb0ef41Sopenharmony_ci issues with namespaces that contain `then` function exports. 10411cb0ef41Sopenharmony_ci* Returns: {Function} 10421cb0ef41Sopenharmony_ci 10431cb0ef41Sopenharmony_ciCompiles the given code into the provided context (if no context is 10441cb0ef41Sopenharmony_cisupplied, the current context is used), and returns it wrapped inside a 10451cb0ef41Sopenharmony_cifunction with the given `params`. 10461cb0ef41Sopenharmony_ci 10471cb0ef41Sopenharmony_ci## `vm.createContext([contextObject[, options]])` 10481cb0ef41Sopenharmony_ci 10491cb0ef41Sopenharmony_ci<!-- YAML 10501cb0ef41Sopenharmony_ciadded: v0.3.1 10511cb0ef41Sopenharmony_cichanges: 10521cb0ef41Sopenharmony_ci - version: v14.6.0 10531cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/34023 10541cb0ef41Sopenharmony_ci description: The `microtaskMode` option is supported now. 10551cb0ef41Sopenharmony_ci - version: v10.0.0 10561cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/19398 10571cb0ef41Sopenharmony_ci description: The first argument can no longer be a function. 10581cb0ef41Sopenharmony_ci - version: v10.0.0 10591cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/19016 10601cb0ef41Sopenharmony_ci description: The `codeGeneration` option is supported now. 10611cb0ef41Sopenharmony_ci--> 10621cb0ef41Sopenharmony_ci 10631cb0ef41Sopenharmony_ci* `contextObject` {Object} 10641cb0ef41Sopenharmony_ci* `options` {Object} 10651cb0ef41Sopenharmony_ci * `name` {string} Human-readable name of the newly created context. 10661cb0ef41Sopenharmony_ci **Default:** `'VM Context i'`, where `i` is an ascending numerical index of 10671cb0ef41Sopenharmony_ci the created context. 10681cb0ef41Sopenharmony_ci * `origin` {string} [Origin][origin] corresponding to the newly created 10691cb0ef41Sopenharmony_ci context for display purposes. The origin should be formatted like a URL, 10701cb0ef41Sopenharmony_ci but with only the scheme, host, and port (if necessary), like the value of 10711cb0ef41Sopenharmony_ci the [`url.origin`][] property of a [`URL`][] object. Most notably, this 10721cb0ef41Sopenharmony_ci string should omit the trailing slash, as that denotes a path. 10731cb0ef41Sopenharmony_ci **Default:** `''`. 10741cb0ef41Sopenharmony_ci * `codeGeneration` {Object} 10751cb0ef41Sopenharmony_ci * `strings` {boolean} If set to false any calls to `eval` or function 10761cb0ef41Sopenharmony_ci constructors (`Function`, `GeneratorFunction`, etc) will throw an 10771cb0ef41Sopenharmony_ci `EvalError`. **Default:** `true`. 10781cb0ef41Sopenharmony_ci * `wasm` {boolean} If set to false any attempt to compile a WebAssembly 10791cb0ef41Sopenharmony_ci module will throw a `WebAssembly.CompileError`. **Default:** `true`. 10801cb0ef41Sopenharmony_ci * `microtaskMode` {string} If set to `afterEvaluate`, microtasks (tasks 10811cb0ef41Sopenharmony_ci scheduled through `Promise`s and `async function`s) will be run immediately 10821cb0ef41Sopenharmony_ci after a script has run through [`script.runInContext()`][]. 10831cb0ef41Sopenharmony_ci They are included in the `timeout` and `breakOnSigint` scopes in that case. 10841cb0ef41Sopenharmony_ci* Returns: {Object} contextified object. 10851cb0ef41Sopenharmony_ci 10861cb0ef41Sopenharmony_ciIf given a `contextObject`, the `vm.createContext()` method will [prepare 10871cb0ef41Sopenharmony_cithat object][contextified] so that it can be used in calls to 10881cb0ef41Sopenharmony_ci[`vm.runInContext()`][] or [`script.runInContext()`][]. Inside such scripts, 10891cb0ef41Sopenharmony_cithe `contextObject` will be the global object, retaining all of its existing 10901cb0ef41Sopenharmony_ciproperties but also having the built-in objects and functions any standard 10911cb0ef41Sopenharmony_ci[global object][] has. Outside of scripts run by the vm module, global variables 10921cb0ef41Sopenharmony_ciwill remain unchanged. 10931cb0ef41Sopenharmony_ci 10941cb0ef41Sopenharmony_ci```js 10951cb0ef41Sopenharmony_ciconst vm = require('node:vm'); 10961cb0ef41Sopenharmony_ci 10971cb0ef41Sopenharmony_ciglobal.globalVar = 3; 10981cb0ef41Sopenharmony_ci 10991cb0ef41Sopenharmony_ciconst context = { globalVar: 1 }; 11001cb0ef41Sopenharmony_civm.createContext(context); 11011cb0ef41Sopenharmony_ci 11021cb0ef41Sopenharmony_civm.runInContext('globalVar *= 2;', context); 11031cb0ef41Sopenharmony_ci 11041cb0ef41Sopenharmony_ciconsole.log(context); 11051cb0ef41Sopenharmony_ci// Prints: { globalVar: 2 } 11061cb0ef41Sopenharmony_ci 11071cb0ef41Sopenharmony_ciconsole.log(global.globalVar); 11081cb0ef41Sopenharmony_ci// Prints: 3 11091cb0ef41Sopenharmony_ci``` 11101cb0ef41Sopenharmony_ci 11111cb0ef41Sopenharmony_ciIf `contextObject` is omitted (or passed explicitly as `undefined`), a new, 11121cb0ef41Sopenharmony_ciempty [contextified][] object will be returned. 11131cb0ef41Sopenharmony_ci 11141cb0ef41Sopenharmony_ciThe `vm.createContext()` method is primarily useful for creating a single 11151cb0ef41Sopenharmony_cicontext that can be used to run multiple scripts. For instance, if emulating a 11161cb0ef41Sopenharmony_ciweb browser, the method can be used to create a single context representing a 11171cb0ef41Sopenharmony_ciwindow's global object, then run all `<script>` tags together within that 11181cb0ef41Sopenharmony_cicontext. 11191cb0ef41Sopenharmony_ci 11201cb0ef41Sopenharmony_ciThe provided `name` and `origin` of the context are made visible through the 11211cb0ef41Sopenharmony_ciInspector API. 11221cb0ef41Sopenharmony_ci 11231cb0ef41Sopenharmony_ci## `vm.isContext(object)` 11241cb0ef41Sopenharmony_ci 11251cb0ef41Sopenharmony_ci<!-- YAML 11261cb0ef41Sopenharmony_ciadded: v0.11.7 11271cb0ef41Sopenharmony_ci--> 11281cb0ef41Sopenharmony_ci 11291cb0ef41Sopenharmony_ci* `object` {Object} 11301cb0ef41Sopenharmony_ci* Returns: {boolean} 11311cb0ef41Sopenharmony_ci 11321cb0ef41Sopenharmony_ciReturns `true` if the given `object` object has been [contextified][] using 11331cb0ef41Sopenharmony_ci[`vm.createContext()`][]. 11341cb0ef41Sopenharmony_ci 11351cb0ef41Sopenharmony_ci## `vm.measureMemory([options])` 11361cb0ef41Sopenharmony_ci 11371cb0ef41Sopenharmony_ci<!-- YAML 11381cb0ef41Sopenharmony_ciadded: v13.10.0 11391cb0ef41Sopenharmony_ci--> 11401cb0ef41Sopenharmony_ci 11411cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 11421cb0ef41Sopenharmony_ci 11431cb0ef41Sopenharmony_ciMeasure the memory known to V8 and used by all contexts known to the 11441cb0ef41Sopenharmony_cicurrent V8 isolate, or the main context. 11451cb0ef41Sopenharmony_ci 11461cb0ef41Sopenharmony_ci* `options` {Object} Optional. 11471cb0ef41Sopenharmony_ci * `mode` {string} Either `'summary'` or `'detailed'`. In summary mode, 11481cb0ef41Sopenharmony_ci only the memory measured for the main context will be returned. In 11491cb0ef41Sopenharmony_ci detailed mode, the memory measured for all contexts known to the 11501cb0ef41Sopenharmony_ci current V8 isolate will be returned. 11511cb0ef41Sopenharmony_ci **Default:** `'summary'` 11521cb0ef41Sopenharmony_ci * `execution` {string} Either `'default'` or `'eager'`. With default 11531cb0ef41Sopenharmony_ci execution, the promise will not resolve until after the next scheduled 11541cb0ef41Sopenharmony_ci garbage collection starts, which may take a while (or never if the program 11551cb0ef41Sopenharmony_ci exits before the next GC). With eager execution, the GC will be started 11561cb0ef41Sopenharmony_ci right away to measure the memory. 11571cb0ef41Sopenharmony_ci **Default:** `'default'` 11581cb0ef41Sopenharmony_ci* Returns: {Promise} If the memory is successfully measured, the promise will 11591cb0ef41Sopenharmony_ci resolve with an object containing information about the memory usage. 11601cb0ef41Sopenharmony_ci Otherwise it will be rejected with an `ERR_CONTEXT_NOT_INITIALIZED` error. 11611cb0ef41Sopenharmony_ci 11621cb0ef41Sopenharmony_ciThe format of the object that the returned Promise may resolve with is 11631cb0ef41Sopenharmony_cispecific to the V8 engine and may change from one version of V8 to the next. 11641cb0ef41Sopenharmony_ci 11651cb0ef41Sopenharmony_ciThe returned result is different from the statistics returned by 11661cb0ef41Sopenharmony_ci`v8.getHeapSpaceStatistics()` in that `vm.measureMemory()` measure the 11671cb0ef41Sopenharmony_cimemory reachable by each V8 specific contexts in the current instance of 11681cb0ef41Sopenharmony_cithe V8 engine, while the result of `v8.getHeapSpaceStatistics()` measure 11691cb0ef41Sopenharmony_cithe memory occupied by each heap space in the current V8 instance. 11701cb0ef41Sopenharmony_ci 11711cb0ef41Sopenharmony_ci```js 11721cb0ef41Sopenharmony_ciconst vm = require('node:vm'); 11731cb0ef41Sopenharmony_ci// Measure the memory used by the main context. 11741cb0ef41Sopenharmony_civm.measureMemory({ mode: 'summary' }) 11751cb0ef41Sopenharmony_ci // This is the same as vm.measureMemory() 11761cb0ef41Sopenharmony_ci .then((result) => { 11771cb0ef41Sopenharmony_ci // The current format is: 11781cb0ef41Sopenharmony_ci // { 11791cb0ef41Sopenharmony_ci // total: { 11801cb0ef41Sopenharmony_ci // jsMemoryEstimate: 2418479, jsMemoryRange: [ 2418479, 2745799 ] 11811cb0ef41Sopenharmony_ci // } 11821cb0ef41Sopenharmony_ci // } 11831cb0ef41Sopenharmony_ci console.log(result); 11841cb0ef41Sopenharmony_ci }); 11851cb0ef41Sopenharmony_ci 11861cb0ef41Sopenharmony_ciconst context = vm.createContext({ a: 1 }); 11871cb0ef41Sopenharmony_civm.measureMemory({ mode: 'detailed', execution: 'eager' }) 11881cb0ef41Sopenharmony_ci .then((result) => { 11891cb0ef41Sopenharmony_ci // Reference the context here so that it won't be GC'ed 11901cb0ef41Sopenharmony_ci // until the measurement is complete. 11911cb0ef41Sopenharmony_ci console.log(context.a); 11921cb0ef41Sopenharmony_ci // { 11931cb0ef41Sopenharmony_ci // total: { 11941cb0ef41Sopenharmony_ci // jsMemoryEstimate: 2574732, 11951cb0ef41Sopenharmony_ci // jsMemoryRange: [ 2574732, 2904372 ] 11961cb0ef41Sopenharmony_ci // }, 11971cb0ef41Sopenharmony_ci // current: { 11981cb0ef41Sopenharmony_ci // jsMemoryEstimate: 2438996, 11991cb0ef41Sopenharmony_ci // jsMemoryRange: [ 2438996, 2768636 ] 12001cb0ef41Sopenharmony_ci // }, 12011cb0ef41Sopenharmony_ci // other: [ 12021cb0ef41Sopenharmony_ci // { 12031cb0ef41Sopenharmony_ci // jsMemoryEstimate: 135736, 12041cb0ef41Sopenharmony_ci // jsMemoryRange: [ 135736, 465376 ] 12051cb0ef41Sopenharmony_ci // } 12061cb0ef41Sopenharmony_ci // ] 12071cb0ef41Sopenharmony_ci // } 12081cb0ef41Sopenharmony_ci console.log(result); 12091cb0ef41Sopenharmony_ci }); 12101cb0ef41Sopenharmony_ci``` 12111cb0ef41Sopenharmony_ci 12121cb0ef41Sopenharmony_ci## `vm.runInContext(code, contextifiedObject[, options])` 12131cb0ef41Sopenharmony_ci 12141cb0ef41Sopenharmony_ci<!-- YAML 12151cb0ef41Sopenharmony_ciadded: v0.3.1 12161cb0ef41Sopenharmony_cichanges: 12171cb0ef41Sopenharmony_ci - version: 12181cb0ef41Sopenharmony_ci - v17.0.0 12191cb0ef41Sopenharmony_ci - v16.12.0 12201cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/40249 12211cb0ef41Sopenharmony_ci description: Added support for import attributes to the 12221cb0ef41Sopenharmony_ci `importModuleDynamically` parameter. 12231cb0ef41Sopenharmony_ci - version: v6.3.0 12241cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/6635 12251cb0ef41Sopenharmony_ci description: The `breakOnSigint` option is supported now. 12261cb0ef41Sopenharmony_ci--> 12271cb0ef41Sopenharmony_ci 12281cb0ef41Sopenharmony_ci* `code` {string} The JavaScript code to compile and run. 12291cb0ef41Sopenharmony_ci* `contextifiedObject` {Object} The [contextified][] object that will be used 12301cb0ef41Sopenharmony_ci as the `global` when the `code` is compiled and run. 12311cb0ef41Sopenharmony_ci* `options` {Object|string} 12321cb0ef41Sopenharmony_ci * `filename` {string} Specifies the filename used in stack traces produced 12331cb0ef41Sopenharmony_ci by this script. **Default:** `'evalmachine.<anonymous>'`. 12341cb0ef41Sopenharmony_ci * `lineOffset` {number} Specifies the line number offset that is displayed 12351cb0ef41Sopenharmony_ci in stack traces produced by this script. **Default:** `0`. 12361cb0ef41Sopenharmony_ci * `columnOffset` {number} Specifies the first-line column number offset that 12371cb0ef41Sopenharmony_ci is displayed in stack traces produced by this script. **Default:** `0`. 12381cb0ef41Sopenharmony_ci * `displayErrors` {boolean} When `true`, if an [`Error`][] occurs 12391cb0ef41Sopenharmony_ci while compiling the `code`, the line of code causing the error is attached 12401cb0ef41Sopenharmony_ci to the stack trace. **Default:** `true`. 12411cb0ef41Sopenharmony_ci * `timeout` {integer} Specifies the number of milliseconds to execute `code` 12421cb0ef41Sopenharmony_ci before terminating execution. If execution is terminated, an [`Error`][] 12431cb0ef41Sopenharmony_ci will be thrown. This value must be a strictly positive integer. 12441cb0ef41Sopenharmony_ci * `breakOnSigint` {boolean} If `true`, receiving `SIGINT` 12451cb0ef41Sopenharmony_ci (<kbd>Ctrl</kbd>+<kbd>C</kbd>) will terminate execution and throw an 12461cb0ef41Sopenharmony_ci [`Error`][]. Existing handlers for the event that have been attached via 12471cb0ef41Sopenharmony_ci `process.on('SIGINT')` are disabled during script execution, but continue to 12481cb0ef41Sopenharmony_ci work after that. **Default:** `false`. 12491cb0ef41Sopenharmony_ci * `cachedData` {Buffer|TypedArray|DataView} Provides an optional `Buffer` or 12501cb0ef41Sopenharmony_ci `TypedArray`, or `DataView` with V8's code cache data for the supplied 12511cb0ef41Sopenharmony_ci source. 12521cb0ef41Sopenharmony_ci * `importModuleDynamically` {Function} Called during evaluation of this module 12531cb0ef41Sopenharmony_ci when `import()` is called. If this option is not specified, calls to 12541cb0ef41Sopenharmony_ci `import()` will reject with [`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING`][]. 12551cb0ef41Sopenharmony_ci This option is part of the experimental modules API. We do not recommend 12561cb0ef41Sopenharmony_ci using it in a production environment. If `--experimental-vm-modules` isn't 12571cb0ef41Sopenharmony_ci set, this callback will be ignored and calls to `import()` will reject with 12581cb0ef41Sopenharmony_ci [`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG`][]. 12591cb0ef41Sopenharmony_ci * `specifier` {string} specifier passed to `import()` 12601cb0ef41Sopenharmony_ci * `script` {vm.Script} 12611cb0ef41Sopenharmony_ci * `importAttributes` {Object} The `"with"` value passed to the 12621cb0ef41Sopenharmony_ci [`optionsExpression`][] optional parameter, or an empty object if no value 12631cb0ef41Sopenharmony_ci was provided. 12641cb0ef41Sopenharmony_ci * Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is 12651cb0ef41Sopenharmony_ci recommended in order to take advantage of error tracking, and to avoid 12661cb0ef41Sopenharmony_ci issues with namespaces that contain `then` function exports. 12671cb0ef41Sopenharmony_ci* Returns: {any} the result of the very last statement executed in the script. 12681cb0ef41Sopenharmony_ci 12691cb0ef41Sopenharmony_ciThe `vm.runInContext()` method compiles `code`, runs it within the context of 12701cb0ef41Sopenharmony_cithe `contextifiedObject`, then returns the result. Running code does not have 12711cb0ef41Sopenharmony_ciaccess to the local scope. The `contextifiedObject` object _must_ have been 12721cb0ef41Sopenharmony_cipreviously [contextified][] using the [`vm.createContext()`][] method. 12731cb0ef41Sopenharmony_ci 12741cb0ef41Sopenharmony_ciIf `options` is a string, then it specifies the filename. 12751cb0ef41Sopenharmony_ci 12761cb0ef41Sopenharmony_ciThe following example compiles and executes different scripts using a single 12771cb0ef41Sopenharmony_ci[contextified][] object: 12781cb0ef41Sopenharmony_ci 12791cb0ef41Sopenharmony_ci```js 12801cb0ef41Sopenharmony_ciconst vm = require('node:vm'); 12811cb0ef41Sopenharmony_ci 12821cb0ef41Sopenharmony_ciconst contextObject = { globalVar: 1 }; 12831cb0ef41Sopenharmony_civm.createContext(contextObject); 12841cb0ef41Sopenharmony_ci 12851cb0ef41Sopenharmony_cifor (let i = 0; i < 10; ++i) { 12861cb0ef41Sopenharmony_ci vm.runInContext('globalVar *= 2;', contextObject); 12871cb0ef41Sopenharmony_ci} 12881cb0ef41Sopenharmony_ciconsole.log(contextObject); 12891cb0ef41Sopenharmony_ci// Prints: { globalVar: 1024 } 12901cb0ef41Sopenharmony_ci``` 12911cb0ef41Sopenharmony_ci 12921cb0ef41Sopenharmony_ci## `vm.runInNewContext(code[, contextObject[, options]])` 12931cb0ef41Sopenharmony_ci 12941cb0ef41Sopenharmony_ci<!-- YAML 12951cb0ef41Sopenharmony_ciadded: v0.3.1 12961cb0ef41Sopenharmony_cichanges: 12971cb0ef41Sopenharmony_ci - version: 12981cb0ef41Sopenharmony_ci - v17.0.0 12991cb0ef41Sopenharmony_ci - v16.12.0 13001cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/40249 13011cb0ef41Sopenharmony_ci description: Added support for import attributes to the 13021cb0ef41Sopenharmony_ci `importModuleDynamically` parameter. 13031cb0ef41Sopenharmony_ci - version: v14.6.0 13041cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/34023 13051cb0ef41Sopenharmony_ci description: The `microtaskMode` option is supported now. 13061cb0ef41Sopenharmony_ci - version: v10.0.0 13071cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/19016 13081cb0ef41Sopenharmony_ci description: The `contextCodeGeneration` option is supported now. 13091cb0ef41Sopenharmony_ci - version: v6.3.0 13101cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/6635 13111cb0ef41Sopenharmony_ci description: The `breakOnSigint` option is supported now. 13121cb0ef41Sopenharmony_ci--> 13131cb0ef41Sopenharmony_ci 13141cb0ef41Sopenharmony_ci* `code` {string} The JavaScript code to compile and run. 13151cb0ef41Sopenharmony_ci* `contextObject` {Object} An object that will be [contextified][]. If 13161cb0ef41Sopenharmony_ci `undefined`, a new object will be created. 13171cb0ef41Sopenharmony_ci* `options` {Object|string} 13181cb0ef41Sopenharmony_ci * `filename` {string} Specifies the filename used in stack traces produced 13191cb0ef41Sopenharmony_ci by this script. **Default:** `'evalmachine.<anonymous>'`. 13201cb0ef41Sopenharmony_ci * `lineOffset` {number} Specifies the line number offset that is displayed 13211cb0ef41Sopenharmony_ci in stack traces produced by this script. **Default:** `0`. 13221cb0ef41Sopenharmony_ci * `columnOffset` {number} Specifies the first-line column number offset that 13231cb0ef41Sopenharmony_ci is displayed in stack traces produced by this script. **Default:** `0`. 13241cb0ef41Sopenharmony_ci * `displayErrors` {boolean} When `true`, if an [`Error`][] occurs 13251cb0ef41Sopenharmony_ci while compiling the `code`, the line of code causing the error is attached 13261cb0ef41Sopenharmony_ci to the stack trace. **Default:** `true`. 13271cb0ef41Sopenharmony_ci * `timeout` {integer} Specifies the number of milliseconds to execute `code` 13281cb0ef41Sopenharmony_ci before terminating execution. If execution is terminated, an [`Error`][] 13291cb0ef41Sopenharmony_ci will be thrown. This value must be a strictly positive integer. 13301cb0ef41Sopenharmony_ci * `breakOnSigint` {boolean} If `true`, receiving `SIGINT` 13311cb0ef41Sopenharmony_ci (<kbd>Ctrl</kbd>+<kbd>C</kbd>) will terminate execution and throw an 13321cb0ef41Sopenharmony_ci [`Error`][]. Existing handlers for the event that have been attached via 13331cb0ef41Sopenharmony_ci `process.on('SIGINT')` are disabled during script execution, but continue to 13341cb0ef41Sopenharmony_ci work after that. **Default:** `false`. 13351cb0ef41Sopenharmony_ci * `contextName` {string} Human-readable name of the newly created context. 13361cb0ef41Sopenharmony_ci **Default:** `'VM Context i'`, where `i` is an ascending numerical index of 13371cb0ef41Sopenharmony_ci the created context. 13381cb0ef41Sopenharmony_ci * `contextOrigin` {string} [Origin][origin] corresponding to the newly 13391cb0ef41Sopenharmony_ci created context for display purposes. The origin should be formatted like a 13401cb0ef41Sopenharmony_ci URL, but with only the scheme, host, and port (if necessary), like the 13411cb0ef41Sopenharmony_ci value of the [`url.origin`][] property of a [`URL`][] object. Most notably, 13421cb0ef41Sopenharmony_ci this string should omit the trailing slash, as that denotes a path. 13431cb0ef41Sopenharmony_ci **Default:** `''`. 13441cb0ef41Sopenharmony_ci * `contextCodeGeneration` {Object} 13451cb0ef41Sopenharmony_ci * `strings` {boolean} If set to false any calls to `eval` or function 13461cb0ef41Sopenharmony_ci constructors (`Function`, `GeneratorFunction`, etc) will throw an 13471cb0ef41Sopenharmony_ci `EvalError`. **Default:** `true`. 13481cb0ef41Sopenharmony_ci * `wasm` {boolean} If set to false any attempt to compile a WebAssembly 13491cb0ef41Sopenharmony_ci module will throw a `WebAssembly.CompileError`. **Default:** `true`. 13501cb0ef41Sopenharmony_ci * `cachedData` {Buffer|TypedArray|DataView} Provides an optional `Buffer` or 13511cb0ef41Sopenharmony_ci `TypedArray`, or `DataView` with V8's code cache data for the supplied 13521cb0ef41Sopenharmony_ci source. 13531cb0ef41Sopenharmony_ci * `importModuleDynamically` {Function} Called during evaluation of this module 13541cb0ef41Sopenharmony_ci when `import()` is called. If this option is not specified, calls to 13551cb0ef41Sopenharmony_ci `import()` will reject with [`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING`][]. 13561cb0ef41Sopenharmony_ci This option is part of the experimental modules API. We do not recommend 13571cb0ef41Sopenharmony_ci using it in a production environment. If `--experimental-vm-modules` isn't 13581cb0ef41Sopenharmony_ci set, this callback will be ignored and calls to `import()` will reject with 13591cb0ef41Sopenharmony_ci [`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG`][]. 13601cb0ef41Sopenharmony_ci * `specifier` {string} specifier passed to `import()` 13611cb0ef41Sopenharmony_ci * `script` {vm.Script} 13621cb0ef41Sopenharmony_ci * `importAttributes` {Object} The `"with"` value passed to the 13631cb0ef41Sopenharmony_ci [`optionsExpression`][] optional parameter, or an empty object if no value 13641cb0ef41Sopenharmony_ci was provided. 13651cb0ef41Sopenharmony_ci * Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is 13661cb0ef41Sopenharmony_ci recommended in order to take advantage of error tracking, and to avoid 13671cb0ef41Sopenharmony_ci issues with namespaces that contain `then` function exports. 13681cb0ef41Sopenharmony_ci * `microtaskMode` {string} If set to `afterEvaluate`, microtasks (tasks 13691cb0ef41Sopenharmony_ci scheduled through `Promise`s and `async function`s) will be run immediately 13701cb0ef41Sopenharmony_ci after the script has run. They are included in the `timeout` and 13711cb0ef41Sopenharmony_ci `breakOnSigint` scopes in that case. 13721cb0ef41Sopenharmony_ci* Returns: {any} the result of the very last statement executed in the script. 13731cb0ef41Sopenharmony_ci 13741cb0ef41Sopenharmony_ciThe `vm.runInNewContext()` first contextifies the given `contextObject` (or 13751cb0ef41Sopenharmony_cicreates a new `contextObject` if passed as `undefined`), compiles the `code`, 13761cb0ef41Sopenharmony_ciruns it within the created context, then returns the result. Running code 13771cb0ef41Sopenharmony_cidoes not have access to the local scope. 13781cb0ef41Sopenharmony_ci 13791cb0ef41Sopenharmony_ciIf `options` is a string, then it specifies the filename. 13801cb0ef41Sopenharmony_ci 13811cb0ef41Sopenharmony_ciThe following example compiles and executes code that increments a global 13821cb0ef41Sopenharmony_civariable and sets a new one. These globals are contained in the `contextObject`. 13831cb0ef41Sopenharmony_ci 13841cb0ef41Sopenharmony_ci```js 13851cb0ef41Sopenharmony_ciconst vm = require('node:vm'); 13861cb0ef41Sopenharmony_ci 13871cb0ef41Sopenharmony_ciconst contextObject = { 13881cb0ef41Sopenharmony_ci animal: 'cat', 13891cb0ef41Sopenharmony_ci count: 2, 13901cb0ef41Sopenharmony_ci}; 13911cb0ef41Sopenharmony_ci 13921cb0ef41Sopenharmony_civm.runInNewContext('count += 1; name = "kitty"', contextObject); 13931cb0ef41Sopenharmony_ciconsole.log(contextObject); 13941cb0ef41Sopenharmony_ci// Prints: { animal: 'cat', count: 3, name: 'kitty' } 13951cb0ef41Sopenharmony_ci``` 13961cb0ef41Sopenharmony_ci 13971cb0ef41Sopenharmony_ci## `vm.runInThisContext(code[, options])` 13981cb0ef41Sopenharmony_ci 13991cb0ef41Sopenharmony_ci<!-- YAML 14001cb0ef41Sopenharmony_ciadded: v0.3.1 14011cb0ef41Sopenharmony_cichanges: 14021cb0ef41Sopenharmony_ci - version: 14031cb0ef41Sopenharmony_ci - v17.0.0 14041cb0ef41Sopenharmony_ci - v16.12.0 14051cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/40249 14061cb0ef41Sopenharmony_ci description: Added support for import attributes to the 14071cb0ef41Sopenharmony_ci `importModuleDynamically` parameter. 14081cb0ef41Sopenharmony_ci - version: v6.3.0 14091cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/6635 14101cb0ef41Sopenharmony_ci description: The `breakOnSigint` option is supported now. 14111cb0ef41Sopenharmony_ci--> 14121cb0ef41Sopenharmony_ci 14131cb0ef41Sopenharmony_ci* `code` {string} The JavaScript code to compile and run. 14141cb0ef41Sopenharmony_ci* `options` {Object|string} 14151cb0ef41Sopenharmony_ci * `filename` {string} Specifies the filename used in stack traces produced 14161cb0ef41Sopenharmony_ci by this script. **Default:** `'evalmachine.<anonymous>'`. 14171cb0ef41Sopenharmony_ci * `lineOffset` {number} Specifies the line number offset that is displayed 14181cb0ef41Sopenharmony_ci in stack traces produced by this script. **Default:** `0`. 14191cb0ef41Sopenharmony_ci * `columnOffset` {number} Specifies the first-line column number offset that 14201cb0ef41Sopenharmony_ci is displayed in stack traces produced by this script. **Default:** `0`. 14211cb0ef41Sopenharmony_ci * `displayErrors` {boolean} When `true`, if an [`Error`][] occurs 14221cb0ef41Sopenharmony_ci while compiling the `code`, the line of code causing the error is attached 14231cb0ef41Sopenharmony_ci to the stack trace. **Default:** `true`. 14241cb0ef41Sopenharmony_ci * `timeout` {integer} Specifies the number of milliseconds to execute `code` 14251cb0ef41Sopenharmony_ci before terminating execution. If execution is terminated, an [`Error`][] 14261cb0ef41Sopenharmony_ci will be thrown. This value must be a strictly positive integer. 14271cb0ef41Sopenharmony_ci * `breakOnSigint` {boolean} If `true`, receiving `SIGINT` 14281cb0ef41Sopenharmony_ci (<kbd>Ctrl</kbd>+<kbd>C</kbd>) will terminate execution and throw an 14291cb0ef41Sopenharmony_ci [`Error`][]. Existing handlers for the event that have been attached via 14301cb0ef41Sopenharmony_ci `process.on('SIGINT')` are disabled during script execution, but continue to 14311cb0ef41Sopenharmony_ci work after that. **Default:** `false`. 14321cb0ef41Sopenharmony_ci * `cachedData` {Buffer|TypedArray|DataView} Provides an optional `Buffer` or 14331cb0ef41Sopenharmony_ci `TypedArray`, or `DataView` with V8's code cache data for the supplied 14341cb0ef41Sopenharmony_ci source. 14351cb0ef41Sopenharmony_ci * `importModuleDynamically` {Function} Called during evaluation of this module 14361cb0ef41Sopenharmony_ci when `import()` is called. If this option is not specified, calls to 14371cb0ef41Sopenharmony_ci `import()` will reject with [`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING`][]. 14381cb0ef41Sopenharmony_ci This option is part of the experimental modules API. We do not recommend 14391cb0ef41Sopenharmony_ci using it in a production environment. If `--experimental-vm-modules` isn't 14401cb0ef41Sopenharmony_ci set, this callback will be ignored and calls to `import()` will reject with 14411cb0ef41Sopenharmony_ci [`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG`][]. 14421cb0ef41Sopenharmony_ci * `specifier` {string} specifier passed to `import()` 14431cb0ef41Sopenharmony_ci * `script` {vm.Script} 14441cb0ef41Sopenharmony_ci * `importAttributes` {Object} The `"with"` value passed to the 14451cb0ef41Sopenharmony_ci [`optionsExpression`][] optional parameter, or an empty object if no value 14461cb0ef41Sopenharmony_ci was provided. 14471cb0ef41Sopenharmony_ci * Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is 14481cb0ef41Sopenharmony_ci recommended in order to take advantage of error tracking, and to avoid 14491cb0ef41Sopenharmony_ci issues with namespaces that contain `then` function exports. 14501cb0ef41Sopenharmony_ci* Returns: {any} the result of the very last statement executed in the script. 14511cb0ef41Sopenharmony_ci 14521cb0ef41Sopenharmony_ci`vm.runInThisContext()` compiles `code`, runs it within the context of the 14531cb0ef41Sopenharmony_cicurrent `global` and returns the result. Running code does not have access to 14541cb0ef41Sopenharmony_cilocal scope, but does have access to the current `global` object. 14551cb0ef41Sopenharmony_ci 14561cb0ef41Sopenharmony_ciIf `options` is a string, then it specifies the filename. 14571cb0ef41Sopenharmony_ci 14581cb0ef41Sopenharmony_ciThe following example illustrates using both `vm.runInThisContext()` and 14591cb0ef41Sopenharmony_cithe JavaScript [`eval()`][] function to run the same code: 14601cb0ef41Sopenharmony_ci 14611cb0ef41Sopenharmony_ci<!-- eslint-disable prefer-const --> 14621cb0ef41Sopenharmony_ci 14631cb0ef41Sopenharmony_ci```js 14641cb0ef41Sopenharmony_ciconst vm = require('node:vm'); 14651cb0ef41Sopenharmony_cilet localVar = 'initial value'; 14661cb0ef41Sopenharmony_ci 14671cb0ef41Sopenharmony_ciconst vmResult = vm.runInThisContext('localVar = "vm";'); 14681cb0ef41Sopenharmony_ciconsole.log(`vmResult: '${vmResult}', localVar: '${localVar}'`); 14691cb0ef41Sopenharmony_ci// Prints: vmResult: 'vm', localVar: 'initial value' 14701cb0ef41Sopenharmony_ci 14711cb0ef41Sopenharmony_ciconst evalResult = eval('localVar = "eval";'); 14721cb0ef41Sopenharmony_ciconsole.log(`evalResult: '${evalResult}', localVar: '${localVar}'`); 14731cb0ef41Sopenharmony_ci// Prints: evalResult: 'eval', localVar: 'eval' 14741cb0ef41Sopenharmony_ci``` 14751cb0ef41Sopenharmony_ci 14761cb0ef41Sopenharmony_ciBecause `vm.runInThisContext()` does not have access to the local scope, 14771cb0ef41Sopenharmony_ci`localVar` is unchanged. In contrast, [`eval()`][] _does_ have access to the 14781cb0ef41Sopenharmony_cilocal scope, so the value `localVar` is changed. In this way 14791cb0ef41Sopenharmony_ci`vm.runInThisContext()` is much like an [indirect `eval()` call][], e.g. 14801cb0ef41Sopenharmony_ci`(0,eval)('code')`. 14811cb0ef41Sopenharmony_ci 14821cb0ef41Sopenharmony_ci## Example: Running an HTTP server within a VM 14831cb0ef41Sopenharmony_ci 14841cb0ef41Sopenharmony_ciWhen using either [`script.runInThisContext()`][] or 14851cb0ef41Sopenharmony_ci[`vm.runInThisContext()`][], the code is executed within the current V8 global 14861cb0ef41Sopenharmony_cicontext. The code passed to this VM context will have its own isolated scope. 14871cb0ef41Sopenharmony_ci 14881cb0ef41Sopenharmony_ciIn order to run a simple web server using the `node:http` module the code passed 14891cb0ef41Sopenharmony_cito the context must either call `require('node:http')` on its own, or have a 14901cb0ef41Sopenharmony_cireference to the `node:http` module passed to it. For instance: 14911cb0ef41Sopenharmony_ci 14921cb0ef41Sopenharmony_ci```js 14931cb0ef41Sopenharmony_ci'use strict'; 14941cb0ef41Sopenharmony_ciconst vm = require('node:vm'); 14951cb0ef41Sopenharmony_ci 14961cb0ef41Sopenharmony_ciconst code = ` 14971cb0ef41Sopenharmony_ci((require) => { 14981cb0ef41Sopenharmony_ci const http = require('node:http'); 14991cb0ef41Sopenharmony_ci 15001cb0ef41Sopenharmony_ci http.createServer((request, response) => { 15011cb0ef41Sopenharmony_ci response.writeHead(200, { 'Content-Type': 'text/plain' }); 15021cb0ef41Sopenharmony_ci response.end('Hello World\\n'); 15031cb0ef41Sopenharmony_ci }).listen(8124); 15041cb0ef41Sopenharmony_ci 15051cb0ef41Sopenharmony_ci console.log('Server running at http://127.0.0.1:8124/'); 15061cb0ef41Sopenharmony_ci})`; 15071cb0ef41Sopenharmony_ci 15081cb0ef41Sopenharmony_civm.runInThisContext(code)(require); 15091cb0ef41Sopenharmony_ci``` 15101cb0ef41Sopenharmony_ci 15111cb0ef41Sopenharmony_ciThe `require()` in the above case shares the state with the context it is 15121cb0ef41Sopenharmony_cipassed from. This may introduce risks when untrusted code is executed, e.g. 15131cb0ef41Sopenharmony_cialtering objects in the context in unwanted ways. 15141cb0ef41Sopenharmony_ci 15151cb0ef41Sopenharmony_ci## What does it mean to "contextify" an object? 15161cb0ef41Sopenharmony_ci 15171cb0ef41Sopenharmony_ciAll JavaScript executed within Node.js runs within the scope of a "context". 15181cb0ef41Sopenharmony_ciAccording to the [V8 Embedder's Guide][]: 15191cb0ef41Sopenharmony_ci 15201cb0ef41Sopenharmony_ci> In V8, a context is an execution environment that allows separate, unrelated, 15211cb0ef41Sopenharmony_ci> JavaScript applications to run in a single instance of V8. You must explicitly 15221cb0ef41Sopenharmony_ci> specify the context in which you want any JavaScript code to be run. 15231cb0ef41Sopenharmony_ci 15241cb0ef41Sopenharmony_ciWhen the method `vm.createContext()` is called, the `contextObject` argument 15251cb0ef41Sopenharmony_ci(or a newly-created object if `contextObject` is `undefined`) is associated 15261cb0ef41Sopenharmony_ciinternally with a new instance of a V8 Context. This V8 Context provides the 15271cb0ef41Sopenharmony_ci`code` run using the `node:vm` module's methods with an isolated global 15281cb0ef41Sopenharmony_cienvironment within which it can operate. The process of creating the V8 Context 15291cb0ef41Sopenharmony_ciand associating it with the `contextObject` is what this document refers to as 15301cb0ef41Sopenharmony_ci"contextifying" the object. 15311cb0ef41Sopenharmony_ci 15321cb0ef41Sopenharmony_ci## Timeout interactions with asynchronous tasks and Promises 15331cb0ef41Sopenharmony_ci 15341cb0ef41Sopenharmony_ci`Promise`s and `async function`s can schedule tasks run by the JavaScript 15351cb0ef41Sopenharmony_ciengine asynchronously. By default, these tasks are run after all JavaScript 15361cb0ef41Sopenharmony_cifunctions on the current stack are done executing. 15371cb0ef41Sopenharmony_ciThis allows escaping the functionality of the `timeout` and 15381cb0ef41Sopenharmony_ci`breakOnSigint` options. 15391cb0ef41Sopenharmony_ci 15401cb0ef41Sopenharmony_ciFor example, the following code executed by `vm.runInNewContext()` with a 15411cb0ef41Sopenharmony_citimeout of 5 milliseconds schedules an infinite loop to run after a promise 15421cb0ef41Sopenharmony_ciresolves. The scheduled loop is never interrupted by the timeout: 15431cb0ef41Sopenharmony_ci 15441cb0ef41Sopenharmony_ci```js 15451cb0ef41Sopenharmony_ciconst vm = require('node:vm'); 15461cb0ef41Sopenharmony_ci 15471cb0ef41Sopenharmony_cifunction loop() { 15481cb0ef41Sopenharmony_ci console.log('entering loop'); 15491cb0ef41Sopenharmony_ci while (1) console.log(Date.now()); 15501cb0ef41Sopenharmony_ci} 15511cb0ef41Sopenharmony_ci 15521cb0ef41Sopenharmony_civm.runInNewContext( 15531cb0ef41Sopenharmony_ci 'Promise.resolve().then(() => loop());', 15541cb0ef41Sopenharmony_ci { loop, console }, 15551cb0ef41Sopenharmony_ci { timeout: 5 }, 15561cb0ef41Sopenharmony_ci); 15571cb0ef41Sopenharmony_ci// This is printed *before* 'entering loop' (!) 15581cb0ef41Sopenharmony_ciconsole.log('done executing'); 15591cb0ef41Sopenharmony_ci``` 15601cb0ef41Sopenharmony_ci 15611cb0ef41Sopenharmony_ciThis can be addressed by passing `microtaskMode: 'afterEvaluate'` to the code 15621cb0ef41Sopenharmony_cithat creates the `Context`: 15631cb0ef41Sopenharmony_ci 15641cb0ef41Sopenharmony_ci```js 15651cb0ef41Sopenharmony_ciconst vm = require('node:vm'); 15661cb0ef41Sopenharmony_ci 15671cb0ef41Sopenharmony_cifunction loop() { 15681cb0ef41Sopenharmony_ci while (1) console.log(Date.now()); 15691cb0ef41Sopenharmony_ci} 15701cb0ef41Sopenharmony_ci 15711cb0ef41Sopenharmony_civm.runInNewContext( 15721cb0ef41Sopenharmony_ci 'Promise.resolve().then(() => loop());', 15731cb0ef41Sopenharmony_ci { loop, console }, 15741cb0ef41Sopenharmony_ci { timeout: 5, microtaskMode: 'afterEvaluate' }, 15751cb0ef41Sopenharmony_ci); 15761cb0ef41Sopenharmony_ci``` 15771cb0ef41Sopenharmony_ci 15781cb0ef41Sopenharmony_ciIn this case, the microtask scheduled through `promise.then()` will be run 15791cb0ef41Sopenharmony_cibefore returning from `vm.runInNewContext()`, and will be interrupted 15801cb0ef41Sopenharmony_ciby the `timeout` functionality. This applies only to code running in a 15811cb0ef41Sopenharmony_ci`vm.Context`, so e.g. [`vm.runInThisContext()`][] does not take this option. 15821cb0ef41Sopenharmony_ci 15831cb0ef41Sopenharmony_ciPromise callbacks are entered into the microtask queue of the context in which 15841cb0ef41Sopenharmony_cithey were created. For example, if `() => loop()` is replaced with just `loop` 15851cb0ef41Sopenharmony_ciin the above example, then `loop` will be pushed into the global microtask 15861cb0ef41Sopenharmony_ciqueue, because it is a function from the outer (main) context, and thus will 15871cb0ef41Sopenharmony_cialso be able to escape the timeout. 15881cb0ef41Sopenharmony_ci 15891cb0ef41Sopenharmony_ciIf asynchronous scheduling functions such as `process.nextTick()`, 15901cb0ef41Sopenharmony_ci`queueMicrotask()`, `setTimeout()`, `setImmediate()`, etc. are made available 15911cb0ef41Sopenharmony_ciinside a `vm.Context`, functions passed to them will be added to global queues, 15921cb0ef41Sopenharmony_ciwhich are shared by all contexts. Therefore, callbacks passed to those functions 15931cb0ef41Sopenharmony_ciare not controllable through the timeout either. 15941cb0ef41Sopenharmony_ci 15951cb0ef41Sopenharmony_ci[Cyclic Module Record]: https://tc39.es/ecma262/#sec-cyclic-module-records 15961cb0ef41Sopenharmony_ci[ECMAScript Module Loader]: esm.md#modules-ecmascript-modules 15971cb0ef41Sopenharmony_ci[Evaluate() concrete method]: https://tc39.es/ecma262/#sec-moduleevaluation 15981cb0ef41Sopenharmony_ci[GetModuleNamespace]: https://tc39.es/ecma262/#sec-getmodulenamespace 15991cb0ef41Sopenharmony_ci[HostResolveImportedModule]: https://tc39.es/ecma262/#sec-hostresolveimportedmodule 16001cb0ef41Sopenharmony_ci[Link() concrete method]: https://tc39.es/ecma262/#sec-moduledeclarationlinking 16011cb0ef41Sopenharmony_ci[Module Record]: https://www.ecma-international.org/ecma-262/#sec-abstract-module-records 16021cb0ef41Sopenharmony_ci[Source Text Module Record]: https://tc39.es/ecma262/#sec-source-text-module-records 16031cb0ef41Sopenharmony_ci[Synthetic Module Record]: https://heycam.github.io/webidl/#synthetic-module-records 16041cb0ef41Sopenharmony_ci[V8 Embedder's Guide]: https://v8.dev/docs/embed#contexts 16051cb0ef41Sopenharmony_ci[`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG`]: errors.md#err_vm_dynamic_import_callback_missing_flag 16061cb0ef41Sopenharmony_ci[`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING`]: errors.md#err_vm_dynamic_import_callback_missing 16071cb0ef41Sopenharmony_ci[`ERR_VM_MODULE_STATUS`]: errors.md#err_vm_module_status 16081cb0ef41Sopenharmony_ci[`Error`]: errors.md#class-error 16091cb0ef41Sopenharmony_ci[`URL`]: url.md#class-url 16101cb0ef41Sopenharmony_ci[`eval()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval 16111cb0ef41Sopenharmony_ci[`optionsExpression`]: https://tc39.es/proposal-import-attributes/#sec-evaluate-import-call 16121cb0ef41Sopenharmony_ci[`script.runInContext()`]: #scriptrunincontextcontextifiedobject-options 16131cb0ef41Sopenharmony_ci[`script.runInThisContext()`]: #scriptruninthiscontextoptions 16141cb0ef41Sopenharmony_ci[`url.origin`]: url.md#urlorigin 16151cb0ef41Sopenharmony_ci[`vm.compileFunction()`]: #vmcompilefunctioncode-params-options 16161cb0ef41Sopenharmony_ci[`vm.createContext()`]: #vmcreatecontextcontextobject-options 16171cb0ef41Sopenharmony_ci[`vm.runInContext()`]: #vmrunincontextcode-contextifiedobject-options 16181cb0ef41Sopenharmony_ci[`vm.runInThisContext()`]: #vmruninthiscontextcode-options 16191cb0ef41Sopenharmony_ci[contextified]: #what-does-it-mean-to-contextify-an-object 16201cb0ef41Sopenharmony_ci[global object]: https://es5.github.io/#x15.1 16211cb0ef41Sopenharmony_ci[indirect `eval()` call]: https://es5.github.io/#x10.4.2 16221cb0ef41Sopenharmony_ci[origin]: https://developer.mozilla.org/en-US/docs/Glossary/Origin 1623