xref: /third_party/node/doc/api/vm.html (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci<!DOCTYPE html>
21cb0ef41Sopenharmony_ci<html lang="en">
31cb0ef41Sopenharmony_ci<head>
41cb0ef41Sopenharmony_ci  <meta charset="utf-8">
51cb0ef41Sopenharmony_ci  <meta name="viewport" content="width=device-width">
61cb0ef41Sopenharmony_ci  <meta name="nodejs.org:node-version" content="v18.20.1">
71cb0ef41Sopenharmony_ci  <title>VM (executing JavaScript) | Node.js v18.20.1 Documentation</title>
81cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
91cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="assets/style.css">
101cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="assets/hljs.css">
111cb0ef41Sopenharmony_ci  <link rel="canonical" href="https://nodejs.org/api/vm.html">
121cb0ef41Sopenharmony_ci  <script async defer src="assets/api.js" type="text/javascript"></script>
131cb0ef41Sopenharmony_ci  <style>@media(max-width:462px){.with-30-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:702px){.with-60-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style>
141cb0ef41Sopenharmony_ci</head>
151cb0ef41Sopenharmony_ci<body class="alt apidoc" id="api-section-vm">
161cb0ef41Sopenharmony_ci  <div id="content" class="clearfix">
171cb0ef41Sopenharmony_ci    <div id="column2" class="interior">
181cb0ef41Sopenharmony_ci      <div id="intro" class="interior">
191cb0ef41Sopenharmony_ci        <a href="/" title="Go back to the home page">
201cb0ef41Sopenharmony_ci          Node.js
211cb0ef41Sopenharmony_ci        </a>
221cb0ef41Sopenharmony_ci      </div>
231cb0ef41Sopenharmony_ci      <ul>
241cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
251cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
261cb0ef41Sopenharmony_ci</ul>
271cb0ef41Sopenharmony_ci<hr class="line">
281cb0ef41Sopenharmony_ci<ul>
291cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
301cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
311cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
321cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
331cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
341cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
351cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
361cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
371cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
381cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
391cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li>
401cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
411cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
421cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
431cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
441cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
451cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li>
461cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li>
471cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li>
481cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li>
491cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li>
501cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li>
511cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li>
521cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
531cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li>
541cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
551cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
561cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
571cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
581cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
591cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
601cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li>
611cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li>
621cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li>
631cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
641cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
651cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li>
661cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
671cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
681cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li>
691cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li>
701cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li>
711cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
721cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li>
731cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
741cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li>
751cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li>
761cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
771cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
781cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li>
791cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
801cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li>
811cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li>
821cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li>
831cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm active">VM</a></li>
841cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
851cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
861cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
871cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
881cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
891cb0ef41Sopenharmony_ci</ul>
901cb0ef41Sopenharmony_ci<hr class="line">
911cb0ef41Sopenharmony_ci<ul>
921cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
931cb0ef41Sopenharmony_ci</ul>
941cb0ef41Sopenharmony_ci    </div>
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci    <div id="column1" data-id="vm" class="interior">
971cb0ef41Sopenharmony_ci      <header class="header">
981cb0ef41Sopenharmony_ci        <div class="header-container">
991cb0ef41Sopenharmony_ci          <h1>Node.js v18.20.1 documentation</h1>
1001cb0ef41Sopenharmony_ci          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
1011cb0ef41Sopenharmony_ci            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
1021cb0ef41Sopenharmony_ci              <path fill="none" d="M0 0h24v24H0z" />
1031cb0ef41Sopenharmony_ci              <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/>
1041cb0ef41Sopenharmony_ci              <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/>
1051cb0ef41Sopenharmony_ci            </svg>
1061cb0ef41Sopenharmony_ci            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
1071cb0ef41Sopenharmony_ci              <path d="M0 0h24v24H0z" fill="none" />
1081cb0ef41Sopenharmony_ci              <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/>
1091cb0ef41Sopenharmony_ci            </svg>
1101cb0ef41Sopenharmony_ci          </button>
1111cb0ef41Sopenharmony_ci        </div>
1121cb0ef41Sopenharmony_ci        <div id="gtoc">
1131cb0ef41Sopenharmony_ci          <ul>
1141cb0ef41Sopenharmony_ci            <li class="pinned-header">Node.js v18.20.1</li>
1151cb0ef41Sopenharmony_ci            
1161cb0ef41Sopenharmony_ci    <li class="picker-header">
1171cb0ef41Sopenharmony_ci      <a href="#">
1181cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
1191cb0ef41Sopenharmony_ci        Table of contents
1201cb0ef41Sopenharmony_ci      </a>
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci      <div class="picker"><div class="toc"><ul>
1231cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#vm-executing-javascript">VM (executing JavaScript)</a></span>
1241cb0ef41Sopenharmony_ci<ul>
1251cb0ef41Sopenharmony_ci<li><a href="#class-vmscript">Class: <code>vm.Script</code></a>
1261cb0ef41Sopenharmony_ci<ul>
1271cb0ef41Sopenharmony_ci<li><a href="#new-vmscriptcode-options"><code>new vm.Script(code[, options])</code></a></li>
1281cb0ef41Sopenharmony_ci<li><a href="#scriptcacheddatarejected"><code>script.cachedDataRejected</code></a></li>
1291cb0ef41Sopenharmony_ci<li><a href="#scriptcreatecacheddata"><code>script.createCachedData()</code></a></li>
1301cb0ef41Sopenharmony_ci<li><a href="#scriptrunincontextcontextifiedobject-options"><code>script.runInContext(contextifiedObject[, options])</code></a></li>
1311cb0ef41Sopenharmony_ci<li><a href="#scriptruninnewcontextcontextobject-options"><code>script.runInNewContext([contextObject[, options]])</code></a></li>
1321cb0ef41Sopenharmony_ci<li><a href="#scriptruninthiscontextoptions"><code>script.runInThisContext([options])</code></a></li>
1331cb0ef41Sopenharmony_ci<li><a href="#scriptsourcemapurl"><code>script.sourceMapURL</code></a></li>
1341cb0ef41Sopenharmony_ci</ul>
1351cb0ef41Sopenharmony_ci</li>
1361cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#class-vmmodule">Class: <code>vm.Module</code></a></span>
1371cb0ef41Sopenharmony_ci<ul>
1381cb0ef41Sopenharmony_ci<li><a href="#moduledependencyspecifiers"><code>module.dependencySpecifiers</code></a></li>
1391cb0ef41Sopenharmony_ci<li><a href="#moduleerror"><code>module.error</code></a></li>
1401cb0ef41Sopenharmony_ci<li><a href="#moduleevaluateoptions"><code>module.evaluate([options])</code></a></li>
1411cb0ef41Sopenharmony_ci<li><a href="#moduleidentifier"><code>module.identifier</code></a></li>
1421cb0ef41Sopenharmony_ci<li><a href="#modulelinklinker"><code>module.link(linker)</code></a></li>
1431cb0ef41Sopenharmony_ci<li><a href="#modulenamespace"><code>module.namespace</code></a></li>
1441cb0ef41Sopenharmony_ci<li><a href="#modulestatus"><code>module.status</code></a></li>
1451cb0ef41Sopenharmony_ci</ul>
1461cb0ef41Sopenharmony_ci</li>
1471cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#class-vmsourcetextmodule">Class: <code>vm.SourceTextModule</code></a></span>
1481cb0ef41Sopenharmony_ci<ul>
1491cb0ef41Sopenharmony_ci<li><a href="#new-vmsourcetextmodulecode-options"><code>new vm.SourceTextModule(code[, options])</code></a></li>
1501cb0ef41Sopenharmony_ci<li><a href="#sourcetextmodulecreatecacheddata"><code>sourceTextModule.createCachedData()</code></a></li>
1511cb0ef41Sopenharmony_ci</ul>
1521cb0ef41Sopenharmony_ci</li>
1531cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#class-vmsyntheticmodule">Class: <code>vm.SyntheticModule</code></a></span>
1541cb0ef41Sopenharmony_ci<ul>
1551cb0ef41Sopenharmony_ci<li><a href="#new-vmsyntheticmoduleexportnames-evaluatecallback-options"><code>new vm.SyntheticModule(exportNames, evaluateCallback[, options])</code></a></li>
1561cb0ef41Sopenharmony_ci<li><a href="#syntheticmodulesetexportname-value"><code>syntheticModule.setExport(name, value)</code></a></li>
1571cb0ef41Sopenharmony_ci</ul>
1581cb0ef41Sopenharmony_ci</li>
1591cb0ef41Sopenharmony_ci<li><a href="#vmcompilefunctioncode-params-options"><code>vm.compileFunction(code[, params[, options]])</code></a></li>
1601cb0ef41Sopenharmony_ci<li><a href="#vmcreatecontextcontextobject-options"><code>vm.createContext([contextObject[, options]])</code></a></li>
1611cb0ef41Sopenharmony_ci<li><a href="#vmiscontextobject"><code>vm.isContext(object)</code></a></li>
1621cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#vmmeasurememoryoptions"><code>vm.measureMemory([options])</code></a></span></li>
1631cb0ef41Sopenharmony_ci<li><a href="#vmrunincontextcode-contextifiedobject-options"><code>vm.runInContext(code, contextifiedObject[, options])</code></a></li>
1641cb0ef41Sopenharmony_ci<li><a href="#vmruninnewcontextcode-contextobject-options"><code>vm.runInNewContext(code[, contextObject[, options]])</code></a></li>
1651cb0ef41Sopenharmony_ci<li><a href="#vmruninthiscontextcode-options"><code>vm.runInThisContext(code[, options])</code></a></li>
1661cb0ef41Sopenharmony_ci<li><a href="#example-running-an-http-server-within-a-vm">Example: Running an HTTP server within a VM</a></li>
1671cb0ef41Sopenharmony_ci<li><a href="#what-does-it-mean-to-contextify-an-object">What does it mean to "contextify" an object?</a></li>
1681cb0ef41Sopenharmony_ci<li><a href="#timeout-interactions-with-asynchronous-tasks-and-promises">Timeout interactions with asynchronous tasks and Promises</a></li>
1691cb0ef41Sopenharmony_ci</ul>
1701cb0ef41Sopenharmony_ci</li>
1711cb0ef41Sopenharmony_ci</ul></div></div>
1721cb0ef41Sopenharmony_ci    </li>
1731cb0ef41Sopenharmony_ci  
1741cb0ef41Sopenharmony_ci            
1751cb0ef41Sopenharmony_ci    <li class="picker-header">
1761cb0ef41Sopenharmony_ci      <a href="#">
1771cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
1781cb0ef41Sopenharmony_ci        Index
1791cb0ef41Sopenharmony_ci      </a>
1801cb0ef41Sopenharmony_ci
1811cb0ef41Sopenharmony_ci      <div class="picker"><ul>
1821cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
1831cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
1841cb0ef41Sopenharmony_ci
1851cb0ef41Sopenharmony_ci      <li>
1861cb0ef41Sopenharmony_ci        <a href="index.html">Index</a>
1871cb0ef41Sopenharmony_ci      </li>
1881cb0ef41Sopenharmony_ci    </ul>
1891cb0ef41Sopenharmony_ci  
1901cb0ef41Sopenharmony_ci<hr class="line">
1911cb0ef41Sopenharmony_ci<ul>
1921cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
1931cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
1941cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
1951cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
1961cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
1971cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
1981cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
1991cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
2001cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
2011cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
2021cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li>
2031cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
2041cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
2051cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
2061cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
2071cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
2081cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li>
2091cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li>
2101cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li>
2111cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li>
2121cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li>
2131cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li>
2141cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li>
2151cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
2161cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li>
2171cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
2181cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
2191cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
2201cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
2211cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
2221cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
2231cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li>
2241cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li>
2251cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li>
2261cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
2271cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
2281cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li>
2291cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
2301cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
2311cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li>
2321cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li>
2331cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li>
2341cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
2351cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li>
2361cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
2371cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li>
2381cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li>
2391cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
2401cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
2411cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li>
2421cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
2431cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li>
2441cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li>
2451cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li>
2461cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm active">VM</a></li>
2471cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
2481cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
2491cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
2501cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
2511cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
2521cb0ef41Sopenharmony_ci</ul>
2531cb0ef41Sopenharmony_ci<hr class="line">
2541cb0ef41Sopenharmony_ci<ul>
2551cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
2561cb0ef41Sopenharmony_ci</ul></div>
2571cb0ef41Sopenharmony_ci    </li>
2581cb0ef41Sopenharmony_ci  
2591cb0ef41Sopenharmony_ci            
2601cb0ef41Sopenharmony_ci    <li class="picker-header">
2611cb0ef41Sopenharmony_ci      <a href="#">
2621cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
2631cb0ef41Sopenharmony_ci        Other versions
2641cb0ef41Sopenharmony_ci      </a>
2651cb0ef41Sopenharmony_ci      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/vm.html">21.x</a></li>
2661cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v20.x/api/vm.html">20.x <b>LTS</b></a></li>
2671cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v19.x/api/vm.html">19.x</a></li>
2681cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v18.x/api/vm.html">18.x <b>LTS</b></a></li>
2691cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v17.x/api/vm.html">17.x</a></li>
2701cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v16.x/api/vm.html">16.x</a></li>
2711cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v15.x/api/vm.html">15.x</a></li>
2721cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v14.x/api/vm.html">14.x</a></li>
2731cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v13.x/api/vm.html">13.x</a></li>
2741cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v12.x/api/vm.html">12.x</a></li>
2751cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v11.x/api/vm.html">11.x</a></li>
2761cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v10.x/api/vm.html">10.x</a></li>
2771cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v9.x/api/vm.html">9.x</a></li>
2781cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v8.x/api/vm.html">8.x</a></li>
2791cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v7.x/api/vm.html">7.x</a></li>
2801cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v6.x/api/vm.html">6.x</a></li>
2811cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v5.x/api/vm.html">5.x</a></li>
2821cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v4.x/api/vm.html">4.x</a></li>
2831cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/vm.html">0.12.x</a></li>
2841cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/vm.html">0.10.x</a></li></ol></div>
2851cb0ef41Sopenharmony_ci    </li>
2861cb0ef41Sopenharmony_ci  
2871cb0ef41Sopenharmony_ci            <li class="picker-header">
2881cb0ef41Sopenharmony_ci              <a href="#">
2891cb0ef41Sopenharmony_ci                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
2901cb0ef41Sopenharmony_ci                Options
2911cb0ef41Sopenharmony_ci              </a>
2921cb0ef41Sopenharmony_ci        
2931cb0ef41Sopenharmony_ci              <div class="picker">
2941cb0ef41Sopenharmony_ci                <ul>
2951cb0ef41Sopenharmony_ci                  <li>
2961cb0ef41Sopenharmony_ci                    <a href="all.html">View on single page</a>
2971cb0ef41Sopenharmony_ci                  </li>
2981cb0ef41Sopenharmony_ci                  <li>
2991cb0ef41Sopenharmony_ci                    <a href="vm.json">View as JSON</a>
3001cb0ef41Sopenharmony_ci                  </li>
3011cb0ef41Sopenharmony_ci                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/vm.md">Edit on GitHub</a></li>    
3021cb0ef41Sopenharmony_ci                </ul>
3031cb0ef41Sopenharmony_ci              </div>
3041cb0ef41Sopenharmony_ci            </li>
3051cb0ef41Sopenharmony_ci          </ul>
3061cb0ef41Sopenharmony_ci        </div>
3071cb0ef41Sopenharmony_ci        <hr>
3081cb0ef41Sopenharmony_ci      </header>
3091cb0ef41Sopenharmony_ci
3101cb0ef41Sopenharmony_ci      <details id="toc" open><summary>Table of contents</summary><ul>
3111cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#vm-executing-javascript">VM (executing JavaScript)</a></span>
3121cb0ef41Sopenharmony_ci<ul>
3131cb0ef41Sopenharmony_ci<li><a href="#class-vmscript">Class: <code>vm.Script</code></a>
3141cb0ef41Sopenharmony_ci<ul>
3151cb0ef41Sopenharmony_ci<li><a href="#new-vmscriptcode-options"><code>new vm.Script(code[, options])</code></a></li>
3161cb0ef41Sopenharmony_ci<li><a href="#scriptcacheddatarejected"><code>script.cachedDataRejected</code></a></li>
3171cb0ef41Sopenharmony_ci<li><a href="#scriptcreatecacheddata"><code>script.createCachedData()</code></a></li>
3181cb0ef41Sopenharmony_ci<li><a href="#scriptrunincontextcontextifiedobject-options"><code>script.runInContext(contextifiedObject[, options])</code></a></li>
3191cb0ef41Sopenharmony_ci<li><a href="#scriptruninnewcontextcontextobject-options"><code>script.runInNewContext([contextObject[, options]])</code></a></li>
3201cb0ef41Sopenharmony_ci<li><a href="#scriptruninthiscontextoptions"><code>script.runInThisContext([options])</code></a></li>
3211cb0ef41Sopenharmony_ci<li><a href="#scriptsourcemapurl"><code>script.sourceMapURL</code></a></li>
3221cb0ef41Sopenharmony_ci</ul>
3231cb0ef41Sopenharmony_ci</li>
3241cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#class-vmmodule">Class: <code>vm.Module</code></a></span>
3251cb0ef41Sopenharmony_ci<ul>
3261cb0ef41Sopenharmony_ci<li><a href="#moduledependencyspecifiers"><code>module.dependencySpecifiers</code></a></li>
3271cb0ef41Sopenharmony_ci<li><a href="#moduleerror"><code>module.error</code></a></li>
3281cb0ef41Sopenharmony_ci<li><a href="#moduleevaluateoptions"><code>module.evaluate([options])</code></a></li>
3291cb0ef41Sopenharmony_ci<li><a href="#moduleidentifier"><code>module.identifier</code></a></li>
3301cb0ef41Sopenharmony_ci<li><a href="#modulelinklinker"><code>module.link(linker)</code></a></li>
3311cb0ef41Sopenharmony_ci<li><a href="#modulenamespace"><code>module.namespace</code></a></li>
3321cb0ef41Sopenharmony_ci<li><a href="#modulestatus"><code>module.status</code></a></li>
3331cb0ef41Sopenharmony_ci</ul>
3341cb0ef41Sopenharmony_ci</li>
3351cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#class-vmsourcetextmodule">Class: <code>vm.SourceTextModule</code></a></span>
3361cb0ef41Sopenharmony_ci<ul>
3371cb0ef41Sopenharmony_ci<li><a href="#new-vmsourcetextmodulecode-options"><code>new vm.SourceTextModule(code[, options])</code></a></li>
3381cb0ef41Sopenharmony_ci<li><a href="#sourcetextmodulecreatecacheddata"><code>sourceTextModule.createCachedData()</code></a></li>
3391cb0ef41Sopenharmony_ci</ul>
3401cb0ef41Sopenharmony_ci</li>
3411cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#class-vmsyntheticmodule">Class: <code>vm.SyntheticModule</code></a></span>
3421cb0ef41Sopenharmony_ci<ul>
3431cb0ef41Sopenharmony_ci<li><a href="#new-vmsyntheticmoduleexportnames-evaluatecallback-options"><code>new vm.SyntheticModule(exportNames, evaluateCallback[, options])</code></a></li>
3441cb0ef41Sopenharmony_ci<li><a href="#syntheticmodulesetexportname-value"><code>syntheticModule.setExport(name, value)</code></a></li>
3451cb0ef41Sopenharmony_ci</ul>
3461cb0ef41Sopenharmony_ci</li>
3471cb0ef41Sopenharmony_ci<li><a href="#vmcompilefunctioncode-params-options"><code>vm.compileFunction(code[, params[, options]])</code></a></li>
3481cb0ef41Sopenharmony_ci<li><a href="#vmcreatecontextcontextobject-options"><code>vm.createContext([contextObject[, options]])</code></a></li>
3491cb0ef41Sopenharmony_ci<li><a href="#vmiscontextobject"><code>vm.isContext(object)</code></a></li>
3501cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#vmmeasurememoryoptions"><code>vm.measureMemory([options])</code></a></span></li>
3511cb0ef41Sopenharmony_ci<li><a href="#vmrunincontextcode-contextifiedobject-options"><code>vm.runInContext(code, contextifiedObject[, options])</code></a></li>
3521cb0ef41Sopenharmony_ci<li><a href="#vmruninnewcontextcode-contextobject-options"><code>vm.runInNewContext(code[, contextObject[, options]])</code></a></li>
3531cb0ef41Sopenharmony_ci<li><a href="#vmruninthiscontextcode-options"><code>vm.runInThisContext(code[, options])</code></a></li>
3541cb0ef41Sopenharmony_ci<li><a href="#example-running-an-http-server-within-a-vm">Example: Running an HTTP server within a VM</a></li>
3551cb0ef41Sopenharmony_ci<li><a href="#what-does-it-mean-to-contextify-an-object">What does it mean to "contextify" an object?</a></li>
3561cb0ef41Sopenharmony_ci<li><a href="#timeout-interactions-with-asynchronous-tasks-and-promises">Timeout interactions with asynchronous tasks and Promises</a></li>
3571cb0ef41Sopenharmony_ci</ul>
3581cb0ef41Sopenharmony_ci</li>
3591cb0ef41Sopenharmony_ci</ul></details>
3601cb0ef41Sopenharmony_ci
3611cb0ef41Sopenharmony_ci      <div id="apicontent">
3621cb0ef41Sopenharmony_ci        <h2>VM (executing JavaScript)<span><a class="mark" href="#vm-executing-javascript" id="vm-executing-javascript">#</a></span><a aria-hidden="true" class="legacy" id="vm_vm_executing_javascript"></a></h2>
3631cb0ef41Sopenharmony_ci
3641cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p>
3651cb0ef41Sopenharmony_ci
3661cb0ef41Sopenharmony_ci<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/vm.js">lib/vm.js</a></p>
3671cb0ef41Sopenharmony_ci<p>The <code>node:vm</code> module enables compiling and running code within V8 Virtual
3681cb0ef41Sopenharmony_ciMachine contexts.</p>
3691cb0ef41Sopenharmony_ci<p><strong class="critical">The <code>node:vm</code> module is not a security
3701cb0ef41Sopenharmony_cimechanism. Do not use it to run untrusted code.</strong></p>
3711cb0ef41Sopenharmony_ci<p>JavaScript code can be compiled and run immediately or
3721cb0ef41Sopenharmony_cicompiled, saved, and run later.</p>
3731cb0ef41Sopenharmony_ci<p>A common use case is to run the code in a different V8 Context. This means
3741cb0ef41Sopenharmony_ciinvoked code has a different global object than the invoking code.</p>
3751cb0ef41Sopenharmony_ci<p>One can provide the context by <a href="#what-does-it-mean-to-contextify-an-object"><em>contextifying</em></a> an
3761cb0ef41Sopenharmony_ciobject. The invoked code treats any property in the context like a
3771cb0ef41Sopenharmony_ciglobal variable. Any changes to global variables caused by the invoked
3781cb0ef41Sopenharmony_cicode are reflected in the context object.</p>
3791cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:vm'</span>);
3801cb0ef41Sopenharmony_ci
3811cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> x = <span class="hljs-number">1</span>;
3821cb0ef41Sopenharmony_ci
3831cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> context = { <span class="hljs-attr">x</span>: <span class="hljs-number">2</span> };
3841cb0ef41Sopenharmony_civm.<span class="hljs-title function_">createContext</span>(context); <span class="hljs-comment">// Contextify the object.</span>
3851cb0ef41Sopenharmony_ci
3861cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> code = <span class="hljs-string">'x += 40; var y = 17;'</span>;
3871cb0ef41Sopenharmony_ci<span class="hljs-comment">// `x` and `y` are global variables in the context.</span>
3881cb0ef41Sopenharmony_ci<span class="hljs-comment">// Initially, x has the value 2 because that is the value of context.x.</span>
3891cb0ef41Sopenharmony_civm.<span class="hljs-title function_">runInContext</span>(code, context);
3901cb0ef41Sopenharmony_ci
3911cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(context.<span class="hljs-property">x</span>); <span class="hljs-comment">// 42</span>
3921cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(context.<span class="hljs-property">y</span>); <span class="hljs-comment">// 17</span>
3931cb0ef41Sopenharmony_ci
3941cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(x); <span class="hljs-comment">// 1; y is not defined.</span></code> <button class="copy-button">copy</button></pre>
3951cb0ef41Sopenharmony_ci<section><h3>Class: <code>vm.Script</code><span><a class="mark" href="#class-vmscript" id="class-vmscript">#</a></span><a aria-hidden="true" class="legacy" id="vm_class_vm_script"></a></h3>
3961cb0ef41Sopenharmony_ci<div class="api_metadata">
3971cb0ef41Sopenharmony_ci<span>Added in: v0.3.1</span>
3981cb0ef41Sopenharmony_ci</div>
3991cb0ef41Sopenharmony_ci<p>Instances of the <code>vm.Script</code> class contain precompiled scripts that can be
4001cb0ef41Sopenharmony_ciexecuted in specific contexts.</p>
4011cb0ef41Sopenharmony_ci<h4><code>new vm.Script(code[, options])</code><span><a class="mark" href="#new-vmscriptcode-options" id="new-vmscriptcode-options">#</a></span><a aria-hidden="true" class="legacy" id="vm_new_vm_script_code_options"></a></h4>
4021cb0ef41Sopenharmony_ci<div class="api_metadata">
4031cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
4041cb0ef41Sopenharmony_ci<table>
4051cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
4061cb0ef41Sopenharmony_ci<tr><td>v17.0.0, v16.12.0</td>
4071cb0ef41Sopenharmony_ci<td><p>Added support for import attributes to the <code>importModuleDynamically</code> parameter.</p></td></tr>
4081cb0ef41Sopenharmony_ci<tr><td>v10.6.0</td>
4091cb0ef41Sopenharmony_ci<td><p>The <code>produceCachedData</code> is deprecated in favour of <code>script.createCachedData()</code>.</p></td></tr>
4101cb0ef41Sopenharmony_ci<tr><td>v5.7.0</td>
4111cb0ef41Sopenharmony_ci<td><p>The <code>cachedData</code> and <code>produceCachedData</code> options are supported now.</p></td></tr>
4121cb0ef41Sopenharmony_ci<tr><td>v0.3.1</td>
4131cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.3.1</span></p></td></tr>
4141cb0ef41Sopenharmony_ci</tbody></table>
4151cb0ef41Sopenharmony_ci</details>
4161cb0ef41Sopenharmony_ci</div>
4171cb0ef41Sopenharmony_ci<ul>
4181cb0ef41Sopenharmony_ci<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The JavaScript code to compile.</li>
4191cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a>
4201cb0ef41Sopenharmony_ci<ul>
4211cb0ef41Sopenharmony_ci<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Specifies the filename used in stack traces produced
4221cb0ef41Sopenharmony_ciby this script. <strong>Default:</strong> <code>'evalmachine.&#x3C;anonymous>'</code>.</li>
4231cb0ef41Sopenharmony_ci<li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the line number offset that is displayed
4241cb0ef41Sopenharmony_ciin stack traces produced by this script. <strong>Default:</strong> <code>0</code>.</li>
4251cb0ef41Sopenharmony_ci<li><code>columnOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the first-line column number offset that
4261cb0ef41Sopenharmony_ciis displayed in stack traces produced by this script. <strong>Default:</strong> <code>0</code>.</li>
4271cb0ef41Sopenharmony_ci<li><code>cachedData</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> Provides an optional <code>Buffer</code> or
4281cb0ef41Sopenharmony_ci<code>TypedArray</code>, or <code>DataView</code> with V8's code cache data for the supplied
4291cb0ef41Sopenharmony_cisource. When supplied, the <code>cachedDataRejected</code> value will be set to
4301cb0ef41Sopenharmony_cieither <code>true</code> or <code>false</code> depending on acceptance of the data by V8.</li>
4311cb0ef41Sopenharmony_ci<li><code>produceCachedData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code> and no <code>cachedData</code> is present, V8
4321cb0ef41Sopenharmony_ciwill attempt to produce code cache data for <code>code</code>. Upon success, a
4331cb0ef41Sopenharmony_ci<code>Buffer</code> with V8's code cache data will be produced and stored in the
4341cb0ef41Sopenharmony_ci<code>cachedData</code> property of the returned <code>vm.Script</code> instance.
4351cb0ef41Sopenharmony_ciThe <code>cachedDataProduced</code> value will be set to either <code>true</code> or <code>false</code>
4361cb0ef41Sopenharmony_cidepending on whether code cache data is produced successfully.
4371cb0ef41Sopenharmony_ciThis option is <strong>deprecated</strong> in favor of <code>script.createCachedData()</code>.
4381cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li>
4391cb0ef41Sopenharmony_ci<li><code>importModuleDynamically</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Called during evaluation of this module
4401cb0ef41Sopenharmony_ciwhen <code>import()</code> is called. If this option is not specified, calls to
4411cb0ef41Sopenharmony_ci<code>import()</code> will reject with <a href="errors.html#err_vm_dynamic_import_callback_missing"><code>ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING</code></a>.
4421cb0ef41Sopenharmony_ciThis option is part of the experimental modules API. We do not recommend
4431cb0ef41Sopenharmony_ciusing it in a production environment. If <code>--experimental-vm-modules</code> isn't
4441cb0ef41Sopenharmony_ciset, this callback will be ignored and calls to <code>import()</code> will reject with
4451cb0ef41Sopenharmony_ci<a href="errors.html#err_vm_dynamic_import_callback_missing_flag"><code>ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG</code></a>.
4461cb0ef41Sopenharmony_ci<ul>
4471cb0ef41Sopenharmony_ci<li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> specifier passed to <code>import()</code></li>
4481cb0ef41Sopenharmony_ci<li><code>script</code> <a href="vm.html#class-vmscript" class="type">&#x3C;vm.Script></a></li>
4491cb0ef41Sopenharmony_ci<li><code>importAttributes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> The <code>"with"</code> value passed to the
4501cb0ef41Sopenharmony_ci<a href="https://tc39.es/proposal-import-attributes/#sec-evaluate-import-call"><code>optionsExpression</code></a> optional parameter, or an empty object if no value
4511cb0ef41Sopenharmony_ciwas provided.</li>
4521cb0ef41Sopenharmony_ci<li>Returns: <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects" class="type">&#x3C;Module Namespace Object></a> | <a href="vm.html#class-vmmodule" class="type">&#x3C;vm.Module></a> Returning a <code>vm.Module</code> is
4531cb0ef41Sopenharmony_cirecommended in order to take advantage of error tracking, and to avoid
4541cb0ef41Sopenharmony_ciissues with namespaces that contain <code>then</code> function exports.</li>
4551cb0ef41Sopenharmony_ci</ul>
4561cb0ef41Sopenharmony_ci</li>
4571cb0ef41Sopenharmony_ci</ul>
4581cb0ef41Sopenharmony_ci</li>
4591cb0ef41Sopenharmony_ci</ul>
4601cb0ef41Sopenharmony_ci<p>If <code>options</code> is a string, then it specifies the filename.</p>
4611cb0ef41Sopenharmony_ci<p>Creating a new <code>vm.Script</code> object compiles <code>code</code> but does not run it. The
4621cb0ef41Sopenharmony_cicompiled <code>vm.Script</code> can be run later multiple times. The <code>code</code> is not bound to
4631cb0ef41Sopenharmony_ciany global object; rather, it is bound before each run, just for that run.</p>
4641cb0ef41Sopenharmony_ci<h4><code>script.cachedDataRejected</code><span><a class="mark" href="#scriptcacheddatarejected" id="scriptcacheddatarejected">#</a></span><a aria-hidden="true" class="legacy" id="vm_script_cacheddatarejected"></a></h4>
4651cb0ef41Sopenharmony_ci<div class="api_metadata">
4661cb0ef41Sopenharmony_ci<span>Added in: v5.7.0</span>
4671cb0ef41Sopenharmony_ci</div>
4681cb0ef41Sopenharmony_ci<ul>
4691cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
4701cb0ef41Sopenharmony_ci</ul>
4711cb0ef41Sopenharmony_ci<p>When <code>cachedData</code> is supplied to create the <code>vm.Script</code>, this value will be set
4721cb0ef41Sopenharmony_cito either <code>true</code> or <code>false</code> depending on acceptance of the data by V8.
4731cb0ef41Sopenharmony_ciOtherwise the value is <code>undefined</code>.</p>
4741cb0ef41Sopenharmony_ci<h4><code>script.createCachedData()</code><span><a class="mark" href="#scriptcreatecacheddata" id="scriptcreatecacheddata">#</a></span><a aria-hidden="true" class="legacy" id="vm_script_createcacheddata"></a></h4>
4751cb0ef41Sopenharmony_ci<div class="api_metadata">
4761cb0ef41Sopenharmony_ci<span>Added in: v10.6.0</span>
4771cb0ef41Sopenharmony_ci</div>
4781cb0ef41Sopenharmony_ci<ul>
4791cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
4801cb0ef41Sopenharmony_ci</ul>
4811cb0ef41Sopenharmony_ci<p>Creates a code cache that can be used with the <code>Script</code> constructor's
4821cb0ef41Sopenharmony_ci<code>cachedData</code> option. Returns a <code>Buffer</code>. This method may be called at any
4831cb0ef41Sopenharmony_citime and any number of times.</p>
4841cb0ef41Sopenharmony_ci<p>The code cache of the <code>Script</code> doesn't contain any JavaScript observable
4851cb0ef41Sopenharmony_cistates. The code cache is safe to be saved along side the script source and
4861cb0ef41Sopenharmony_ciused to construct new <code>Script</code> instances multiple times.</p>
4871cb0ef41Sopenharmony_ci<p>Functions in the <code>Script</code> source can be marked as lazily compiled and they are
4881cb0ef41Sopenharmony_cinot compiled at construction of the <code>Script</code>. These functions are going to be
4891cb0ef41Sopenharmony_cicompiled when they are invoked the first time. The code cache serializes the
4901cb0ef41Sopenharmony_cimetadata that V8 currently knows about the <code>Script</code> that it can use to speed up
4911cb0ef41Sopenharmony_cifuture compilations.</p>
4921cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> script = <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_">Script</span>(<span class="hljs-string">`
4931cb0ef41Sopenharmony_cifunction add(a, b) {
4941cb0ef41Sopenharmony_ci  return a + b;
4951cb0ef41Sopenharmony_ci}
4961cb0ef41Sopenharmony_ci
4971cb0ef41Sopenharmony_ciconst x = add(1, 2);
4981cb0ef41Sopenharmony_ci`</span>);
4991cb0ef41Sopenharmony_ci
5001cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> cacheWithoutAdd = script.<span class="hljs-title function_">createCachedData</span>();
5011cb0ef41Sopenharmony_ci<span class="hljs-comment">// In `cacheWithoutAdd` the function `add()` is marked for full compilation</span>
5021cb0ef41Sopenharmony_ci<span class="hljs-comment">// upon invocation.</span>
5031cb0ef41Sopenharmony_ci
5041cb0ef41Sopenharmony_ciscript.<span class="hljs-title function_">runInThisContext</span>();
5051cb0ef41Sopenharmony_ci
5061cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> cacheWithAdd = script.<span class="hljs-title function_">createCachedData</span>();
5071cb0ef41Sopenharmony_ci<span class="hljs-comment">// `cacheWithAdd` contains fully compiled function `add()`.</span></code> <button class="copy-button">copy</button></pre>
5081cb0ef41Sopenharmony_ci<h4><code>script.runInContext(contextifiedObject[, options])</code><span><a class="mark" href="#scriptrunincontextcontextifiedobject-options" id="scriptrunincontextcontextifiedobject-options">#</a></span><a aria-hidden="true" class="legacy" id="vm_script_runincontext_contextifiedobject_options"></a></h4>
5091cb0ef41Sopenharmony_ci<div class="api_metadata">
5101cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
5111cb0ef41Sopenharmony_ci<table>
5121cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
5131cb0ef41Sopenharmony_ci<tr><td>v6.3.0</td>
5141cb0ef41Sopenharmony_ci<td><p>The <code>breakOnSigint</code> option is supported now.</p></td></tr>
5151cb0ef41Sopenharmony_ci<tr><td>v0.3.1</td>
5161cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.3.1</span></p></td></tr>
5171cb0ef41Sopenharmony_ci</tbody></table>
5181cb0ef41Sopenharmony_ci</details>
5191cb0ef41Sopenharmony_ci</div>
5201cb0ef41Sopenharmony_ci<ul>
5211cb0ef41Sopenharmony_ci<li><code>contextifiedObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> A <a href="#what-does-it-mean-to-contextify-an-object">contextified</a> object as returned by the
5221cb0ef41Sopenharmony_ci<code>vm.createContext()</code> method.</li>
5231cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
5241cb0ef41Sopenharmony_ci<ul>
5251cb0ef41Sopenharmony_ci<li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, if an <a href="errors.html#class-error"><code>Error</code></a> occurs
5261cb0ef41Sopenharmony_ciwhile compiling the <code>code</code>, the line of code causing the error is attached
5271cb0ef41Sopenharmony_cito the stack trace. <strong>Default:</strong> <code>true</code>.</li>
5281cb0ef41Sopenharmony_ci<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Specifies the number of milliseconds to execute <code>code</code>
5291cb0ef41Sopenharmony_cibefore terminating execution. If execution is terminated, an <a href="errors.html#class-error"><code>Error</code></a>
5301cb0ef41Sopenharmony_ciwill be thrown. This value must be a strictly positive integer.</li>
5311cb0ef41Sopenharmony_ci<li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, receiving <code>SIGINT</code>
5321cb0ef41Sopenharmony_ci(<kbd>Ctrl</kbd>+<kbd>C</kbd>) will terminate execution and throw an
5331cb0ef41Sopenharmony_ci<a href="errors.html#class-error"><code>Error</code></a>. Existing handlers for the event that have been attached via
5341cb0ef41Sopenharmony_ci<code>process.on('SIGINT')</code> are disabled during script execution, but continue to
5351cb0ef41Sopenharmony_ciwork after that. <strong>Default:</strong> <code>false</code>.</li>
5361cb0ef41Sopenharmony_ci</ul>
5371cb0ef41Sopenharmony_ci</li>
5381cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> the result of the very last statement executed in the script.</li>
5391cb0ef41Sopenharmony_ci</ul>
5401cb0ef41Sopenharmony_ci<p>Runs the compiled code contained by the <code>vm.Script</code> object within the given
5411cb0ef41Sopenharmony_ci<code>contextifiedObject</code> and returns the result. Running code does not have access
5421cb0ef41Sopenharmony_cito local scope.</p>
5431cb0ef41Sopenharmony_ci<p>The following example compiles code that increments a global variable, sets
5441cb0ef41Sopenharmony_cithe value of another global variable, then execute the code multiple times.
5451cb0ef41Sopenharmony_ciThe globals are contained in the <code>context</code> object.</p>
5461cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:vm'</span>);
5471cb0ef41Sopenharmony_ci
5481cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> context = {
5491cb0ef41Sopenharmony_ci  <span class="hljs-attr">animal</span>: <span class="hljs-string">'cat'</span>,
5501cb0ef41Sopenharmony_ci  <span class="hljs-attr">count</span>: <span class="hljs-number">2</span>,
5511cb0ef41Sopenharmony_ci};
5521cb0ef41Sopenharmony_ci
5531cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> script = <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_">Script</span>(<span class="hljs-string">'count += 1; name = "kitty";'</span>);
5541cb0ef41Sopenharmony_ci
5551cb0ef41Sopenharmony_civm.<span class="hljs-title function_">createContext</span>(context);
5561cb0ef41Sopenharmony_ci<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &#x3C; <span class="hljs-number">10</span>; ++i) {
5571cb0ef41Sopenharmony_ci  script.<span class="hljs-title function_">runInContext</span>(context);
5581cb0ef41Sopenharmony_ci}
5591cb0ef41Sopenharmony_ci
5601cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(context);
5611cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: { animal: 'cat', count: 12, name: 'kitty' }</span></code> <button class="copy-button">copy</button></pre>
5621cb0ef41Sopenharmony_ci<p>Using the <code>timeout</code> or <code>breakOnSigint</code> options will result in new event loops
5631cb0ef41Sopenharmony_ciand corresponding threads being started, which have a non-zero performance
5641cb0ef41Sopenharmony_cioverhead.</p>
5651cb0ef41Sopenharmony_ci<h4><code>script.runInNewContext([contextObject[, options]])</code><span><a class="mark" href="#scriptruninnewcontextcontextobject-options" id="scriptruninnewcontextcontextobject-options">#</a></span><a aria-hidden="true" class="legacy" id="vm_script_runinnewcontext_contextobject_options"></a></h4>
5661cb0ef41Sopenharmony_ci<div class="api_metadata">
5671cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
5681cb0ef41Sopenharmony_ci<table>
5691cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
5701cb0ef41Sopenharmony_ci<tr><td>v14.6.0</td>
5711cb0ef41Sopenharmony_ci<td><p>The <code>microtaskMode</code> option is supported now.</p></td></tr>
5721cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
5731cb0ef41Sopenharmony_ci<td><p>The <code>contextCodeGeneration</code> option is supported now.</p></td></tr>
5741cb0ef41Sopenharmony_ci<tr><td>v6.3.0</td>
5751cb0ef41Sopenharmony_ci<td><p>The <code>breakOnSigint</code> option is supported now.</p></td></tr>
5761cb0ef41Sopenharmony_ci<tr><td>v0.3.1</td>
5771cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.3.1</span></p></td></tr>
5781cb0ef41Sopenharmony_ci</tbody></table>
5791cb0ef41Sopenharmony_ci</details>
5801cb0ef41Sopenharmony_ci</div>
5811cb0ef41Sopenharmony_ci<ul>
5821cb0ef41Sopenharmony_ci<li><code>contextObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> An object that will be <a href="#what-does-it-mean-to-contextify-an-object">contextified</a>. If
5831cb0ef41Sopenharmony_ci<code>undefined</code>, a new object will be created.</li>
5841cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
5851cb0ef41Sopenharmony_ci<ul>
5861cb0ef41Sopenharmony_ci<li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, if an <a href="errors.html#class-error"><code>Error</code></a> occurs
5871cb0ef41Sopenharmony_ciwhile compiling the <code>code</code>, the line of code causing the error is attached
5881cb0ef41Sopenharmony_cito the stack trace. <strong>Default:</strong> <code>true</code>.</li>
5891cb0ef41Sopenharmony_ci<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Specifies the number of milliseconds to execute <code>code</code>
5901cb0ef41Sopenharmony_cibefore terminating execution. If execution is terminated, an <a href="errors.html#class-error"><code>Error</code></a>
5911cb0ef41Sopenharmony_ciwill be thrown. This value must be a strictly positive integer.</li>
5921cb0ef41Sopenharmony_ci<li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, receiving <code>SIGINT</code>
5931cb0ef41Sopenharmony_ci(<kbd>Ctrl</kbd>+<kbd>C</kbd>) will terminate execution and throw an
5941cb0ef41Sopenharmony_ci<a href="errors.html#class-error"><code>Error</code></a>. Existing handlers for the event that have been attached via
5951cb0ef41Sopenharmony_ci<code>process.on('SIGINT')</code> are disabled during script execution, but continue to
5961cb0ef41Sopenharmony_ciwork after that. <strong>Default:</strong> <code>false</code>.</li>
5971cb0ef41Sopenharmony_ci<li><code>contextName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Human-readable name of the newly created context.
5981cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'VM Context i'</code>, where <code>i</code> is an ascending numerical index of
5991cb0ef41Sopenharmony_cithe created context.</li>
6001cb0ef41Sopenharmony_ci<li><code>contextOrigin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <a href="https://developer.mozilla.org/en-US/docs/Glossary/Origin">Origin</a> corresponding to the newly
6011cb0ef41Sopenharmony_cicreated context for display purposes. The origin should be formatted like a
6021cb0ef41Sopenharmony_ciURL, but with only the scheme, host, and port (if necessary), like the
6031cb0ef41Sopenharmony_civalue of the <a href="url.html#urlorigin"><code>url.origin</code></a> property of a <a href="url.html#class-url"><code>URL</code></a> object. Most notably,
6041cb0ef41Sopenharmony_cithis string should omit the trailing slash, as that denotes a path.
6051cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>''</code>.</li>
6061cb0ef41Sopenharmony_ci<li><code>contextCodeGeneration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
6071cb0ef41Sopenharmony_ci<ul>
6081cb0ef41Sopenharmony_ci<li><code>strings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If set to false any calls to <code>eval</code> or function
6091cb0ef41Sopenharmony_ciconstructors (<code>Function</code>, <code>GeneratorFunction</code>, etc) will throw an
6101cb0ef41Sopenharmony_ci<code>EvalError</code>. <strong>Default:</strong> <code>true</code>.</li>
6111cb0ef41Sopenharmony_ci<li><code>wasm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If set to false any attempt to compile a WebAssembly
6121cb0ef41Sopenharmony_cimodule will throw a <code>WebAssembly.CompileError</code>. <strong>Default:</strong> <code>true</code>.</li>
6131cb0ef41Sopenharmony_ci</ul>
6141cb0ef41Sopenharmony_ci</li>
6151cb0ef41Sopenharmony_ci<li><code>microtaskMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> If set to <code>afterEvaluate</code>, microtasks (tasks
6161cb0ef41Sopenharmony_cischeduled through <code>Promise</code>s and <code>async function</code>s) will be run immediately
6171cb0ef41Sopenharmony_ciafter the script has run. They are included in the <code>timeout</code> and
6181cb0ef41Sopenharmony_ci<code>breakOnSigint</code> scopes in that case.</li>
6191cb0ef41Sopenharmony_ci</ul>
6201cb0ef41Sopenharmony_ci</li>
6211cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> the result of the very last statement executed in the script.</li>
6221cb0ef41Sopenharmony_ci</ul>
6231cb0ef41Sopenharmony_ci<p>First contextifies the given <code>contextObject</code>, runs the compiled code contained
6241cb0ef41Sopenharmony_ciby the <code>vm.Script</code> object within the created context, and returns the result.
6251cb0ef41Sopenharmony_ciRunning code does not have access to local scope.</p>
6261cb0ef41Sopenharmony_ci<p>The following example compiles code that sets a global variable, then executes
6271cb0ef41Sopenharmony_cithe code multiple times in different contexts. The globals are set on and
6281cb0ef41Sopenharmony_cicontained within each individual <code>context</code>.</p>
6291cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:vm'</span>);
6301cb0ef41Sopenharmony_ci
6311cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> script = <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_">Script</span>(<span class="hljs-string">'globalVar = "set"'</span>);
6321cb0ef41Sopenharmony_ci
6331cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> contexts = [{}, {}, {}];
6341cb0ef41Sopenharmony_cicontexts.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">context</span>) =></span> {
6351cb0ef41Sopenharmony_ci  script.<span class="hljs-title function_">runInNewContext</span>(context);
6361cb0ef41Sopenharmony_ci});
6371cb0ef41Sopenharmony_ci
6381cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(contexts);
6391cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }]</span></code> <button class="copy-button">copy</button></pre>
6401cb0ef41Sopenharmony_ci<h4><code>script.runInThisContext([options])</code><span><a class="mark" href="#scriptruninthiscontextoptions" id="scriptruninthiscontextoptions">#</a></span><a aria-hidden="true" class="legacy" id="vm_script_runinthiscontext_options"></a></h4>
6411cb0ef41Sopenharmony_ci<div class="api_metadata">
6421cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
6431cb0ef41Sopenharmony_ci<table>
6441cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
6451cb0ef41Sopenharmony_ci<tr><td>v6.3.0</td>
6461cb0ef41Sopenharmony_ci<td><p>The <code>breakOnSigint</code> option is supported now.</p></td></tr>
6471cb0ef41Sopenharmony_ci<tr><td>v0.3.1</td>
6481cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.3.1</span></p></td></tr>
6491cb0ef41Sopenharmony_ci</tbody></table>
6501cb0ef41Sopenharmony_ci</details>
6511cb0ef41Sopenharmony_ci</div>
6521cb0ef41Sopenharmony_ci<ul>
6531cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
6541cb0ef41Sopenharmony_ci<ul>
6551cb0ef41Sopenharmony_ci<li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, if an <a href="errors.html#class-error"><code>Error</code></a> occurs
6561cb0ef41Sopenharmony_ciwhile compiling the <code>code</code>, the line of code causing the error is attached
6571cb0ef41Sopenharmony_cito the stack trace. <strong>Default:</strong> <code>true</code>.</li>
6581cb0ef41Sopenharmony_ci<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Specifies the number of milliseconds to execute <code>code</code>
6591cb0ef41Sopenharmony_cibefore terminating execution. If execution is terminated, an <a href="errors.html#class-error"><code>Error</code></a>
6601cb0ef41Sopenharmony_ciwill be thrown. This value must be a strictly positive integer.</li>
6611cb0ef41Sopenharmony_ci<li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, receiving <code>SIGINT</code>
6621cb0ef41Sopenharmony_ci(<kbd>Ctrl</kbd>+<kbd>C</kbd>) will terminate execution and throw an
6631cb0ef41Sopenharmony_ci<a href="errors.html#class-error"><code>Error</code></a>. Existing handlers for the event that have been attached via
6641cb0ef41Sopenharmony_ci<code>process.on('SIGINT')</code> are disabled during script execution, but continue to
6651cb0ef41Sopenharmony_ciwork after that. <strong>Default:</strong> <code>false</code>.</li>
6661cb0ef41Sopenharmony_ci</ul>
6671cb0ef41Sopenharmony_ci</li>
6681cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> the result of the very last statement executed in the script.</li>
6691cb0ef41Sopenharmony_ci</ul>
6701cb0ef41Sopenharmony_ci<p>Runs the compiled code contained by the <code>vm.Script</code> within the context of the
6711cb0ef41Sopenharmony_cicurrent <code>global</code> object. Running code does not have access to local scope, but
6721cb0ef41Sopenharmony_ci<em>does</em> have access to the current <code>global</code> object.</p>
6731cb0ef41Sopenharmony_ci<p>The following example compiles code that increments a <code>global</code> variable then
6741cb0ef41Sopenharmony_ciexecutes that code multiple times:</p>
6751cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:vm'</span>);
6761cb0ef41Sopenharmony_ci
6771cb0ef41Sopenharmony_ci<span class="hljs-variable language_">global</span>.<span class="hljs-property">globalVar</span> = <span class="hljs-number">0</span>;
6781cb0ef41Sopenharmony_ci
6791cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> script = <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_">Script</span>(<span class="hljs-string">'globalVar += 1'</span>, { <span class="hljs-attr">filename</span>: <span class="hljs-string">'myfile.vm'</span> });
6801cb0ef41Sopenharmony_ci
6811cb0ef41Sopenharmony_ci<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &#x3C; <span class="hljs-number">1000</span>; ++i) {
6821cb0ef41Sopenharmony_ci  script.<span class="hljs-title function_">runInThisContext</span>();
6831cb0ef41Sopenharmony_ci}
6841cb0ef41Sopenharmony_ci
6851cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(globalVar);
6861cb0ef41Sopenharmony_ci
6871cb0ef41Sopenharmony_ci<span class="hljs-comment">// 1000</span></code> <button class="copy-button">copy</button></pre>
6881cb0ef41Sopenharmony_ci<h4><code>script.sourceMapURL</code><span><a class="mark" href="#scriptsourcemapurl" id="scriptsourcemapurl">#</a></span><a aria-hidden="true" class="legacy" id="vm_script_sourcemapurl"></a></h4>
6891cb0ef41Sopenharmony_ci<div class="api_metadata">
6901cb0ef41Sopenharmony_ci<span>Added in: v18.13.0</span>
6911cb0ef41Sopenharmony_ci</div>
6921cb0ef41Sopenharmony_ci<ul>
6931cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
6941cb0ef41Sopenharmony_ci</ul>
6951cb0ef41Sopenharmony_ci<p>When the script is compiled from a source that contains a source map magic
6961cb0ef41Sopenharmony_cicomment, this property will be set to the URL of the source map.</p>
6971cb0ef41Sopenharmony_ci
6981cb0ef41Sopenharmony_ci<pre class="with-30-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> vm <span class="hljs-keyword">from</span> <span class="hljs-string">'node:vm'</span>;
6991cb0ef41Sopenharmony_ci
7001cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> script = <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_">Script</span>(<span class="hljs-string">`
7011cb0ef41Sopenharmony_cifunction myFunc() {}
7021cb0ef41Sopenharmony_ci//# sourceMappingURL=sourcemap.json
7031cb0ef41Sopenharmony_ci`</span>);
7041cb0ef41Sopenharmony_ci
7051cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(script.<span class="hljs-property">sourceMapURL</span>);
7061cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: sourcemap.json</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:vm'</span>);
7071cb0ef41Sopenharmony_ci
7081cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> script = <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_">Script</span>(<span class="hljs-string">`
7091cb0ef41Sopenharmony_cifunction myFunc() {}
7101cb0ef41Sopenharmony_ci//# sourceMappingURL=sourcemap.json
7111cb0ef41Sopenharmony_ci`</span>);
7121cb0ef41Sopenharmony_ci
7131cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(script.<span class="hljs-property">sourceMapURL</span>);
7141cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: sourcemap.json</span></code><button class="copy-button">copy</button></pre>
7151cb0ef41Sopenharmony_ci</section><section><h3>Class: <code>vm.Module</code><span><a class="mark" href="#class-vmmodule" id="class-vmmodule">#</a></span><a aria-hidden="true" class="legacy" id="vm_class_vm_module"></a></h3>
7161cb0ef41Sopenharmony_ci<div class="api_metadata">
7171cb0ef41Sopenharmony_ci<span>Added in: v13.0.0, v12.16.0</span>
7181cb0ef41Sopenharmony_ci</div>
7191cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
7201cb0ef41Sopenharmony_ci<p>This feature is only available with the <code>--experimental-vm-modules</code> command
7211cb0ef41Sopenharmony_ciflag enabled.</p>
7221cb0ef41Sopenharmony_ci<p>The <code>vm.Module</code> class provides a low-level interface for using
7231cb0ef41Sopenharmony_ciECMAScript modules in VM contexts. It is the counterpart of the <code>vm.Script</code>
7241cb0ef41Sopenharmony_ciclass that closely mirrors <a href="https://www.ecma-international.org/ecma-262/#sec-abstract-module-records">Module Record</a>s as defined in the ECMAScript
7251cb0ef41Sopenharmony_cispecification.</p>
7261cb0ef41Sopenharmony_ci<p>Unlike <code>vm.Script</code> however, every <code>vm.Module</code> object is bound to a context from
7271cb0ef41Sopenharmony_ciits creation. Operations on <code>vm.Module</code> objects are intrinsically asynchronous,
7281cb0ef41Sopenharmony_ciin contrast with the synchronous nature of <code>vm.Script</code> objects. The use of
7291cb0ef41Sopenharmony_ci'async' functions can help with manipulating <code>vm.Module</code> objects.</p>
7301cb0ef41Sopenharmony_ci<p>Using a <code>vm.Module</code> object requires three distinct steps: creation/parsing,
7311cb0ef41Sopenharmony_cilinking, and evaluation. These three steps are illustrated in the following
7321cb0ef41Sopenharmony_ciexample.</p>
7331cb0ef41Sopenharmony_ci<p>This implementation lies at a lower level than the <a href="esm.html#modules-ecmascript-modules">ECMAScript Module
7341cb0ef41Sopenharmony_ciloader</a>. There is also no way to interact with the Loader yet, though
7351cb0ef41Sopenharmony_cisupport is planned.</p>
7361cb0ef41Sopenharmony_ci
7371cb0ef41Sopenharmony_ci<pre class="with-30-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> vm <span class="hljs-keyword">from</span> <span class="hljs-string">'node:vm'</span>;
7381cb0ef41Sopenharmony_ci
7391cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> contextifiedObject = vm.<span class="hljs-title function_">createContext</span>({
7401cb0ef41Sopenharmony_ci  <span class="hljs-attr">secret</span>: <span class="hljs-number">42</span>,
7411cb0ef41Sopenharmony_ci  <span class="hljs-attr">print</span>: <span class="hljs-variable language_">console</span>.<span class="hljs-property">log</span>,
7421cb0ef41Sopenharmony_ci});
7431cb0ef41Sopenharmony_ci
7441cb0ef41Sopenharmony_ci<span class="hljs-comment">// Step 1</span>
7451cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span>
7461cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create a Module by constructing a new `vm.SourceTextModule` object. This</span>
7471cb0ef41Sopenharmony_ci<span class="hljs-comment">// parses the provided source text, throwing a `SyntaxError` if anything goes</span>
7481cb0ef41Sopenharmony_ci<span class="hljs-comment">// wrong. By default, a Module is created in the top context. But here, we</span>
7491cb0ef41Sopenharmony_ci<span class="hljs-comment">// specify `contextifiedObject` as the context this Module belongs to.</span>
7501cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span>
7511cb0ef41Sopenharmony_ci<span class="hljs-comment">// Here, we attempt to obtain the default export from the module "foo", and</span>
7521cb0ef41Sopenharmony_ci<span class="hljs-comment">// put it into local binding "secret".</span>
7531cb0ef41Sopenharmony_ci
7541cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> bar = <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_">SourceTextModule</span>(<span class="hljs-string">`
7551cb0ef41Sopenharmony_ci  import s from 'foo';
7561cb0ef41Sopenharmony_ci  s;
7571cb0ef41Sopenharmony_ci  print(s);
7581cb0ef41Sopenharmony_ci`</span>, { <span class="hljs-attr">context</span>: contextifiedObject });
7591cb0ef41Sopenharmony_ci
7601cb0ef41Sopenharmony_ci<span class="hljs-comment">// Step 2</span>
7611cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span>
7621cb0ef41Sopenharmony_ci<span class="hljs-comment">// "Link" the imported dependencies of this Module to it.</span>
7631cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span>
7641cb0ef41Sopenharmony_ci<span class="hljs-comment">// The provided linking callback (the "linker") accepts two arguments: the</span>
7651cb0ef41Sopenharmony_ci<span class="hljs-comment">// parent module (`bar` in this case) and the string that is the specifier of</span>
7661cb0ef41Sopenharmony_ci<span class="hljs-comment">// the imported module. The callback is expected to return a Module that</span>
7671cb0ef41Sopenharmony_ci<span class="hljs-comment">// corresponds to the provided specifier, with certain requirements documented</span>
7681cb0ef41Sopenharmony_ci<span class="hljs-comment">// in `module.link()`.</span>
7691cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span>
7701cb0ef41Sopenharmony_ci<span class="hljs-comment">// If linking has not started for the returned Module, the same linker</span>
7711cb0ef41Sopenharmony_ci<span class="hljs-comment">// callback will be called on the returned Module.</span>
7721cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span>
7731cb0ef41Sopenharmony_ci<span class="hljs-comment">// Even top-level Modules without dependencies must be explicitly linked. The</span>
7741cb0ef41Sopenharmony_ci<span class="hljs-comment">// callback provided would never be called, however.</span>
7751cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span>
7761cb0ef41Sopenharmony_ci<span class="hljs-comment">// The link() method returns a Promise that will be resolved when all the</span>
7771cb0ef41Sopenharmony_ci<span class="hljs-comment">// Promises returned by the linker resolve.</span>
7781cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span>
7791cb0ef41Sopenharmony_ci<span class="hljs-comment">// Note: This is a contrived example in that the linker function creates a new</span>
7801cb0ef41Sopenharmony_ci<span class="hljs-comment">// "foo" module every time it is called. In a full-fledged module system, a</span>
7811cb0ef41Sopenharmony_ci<span class="hljs-comment">// cache would probably be used to avoid duplicated modules.</span>
7821cb0ef41Sopenharmony_ci
7831cb0ef41Sopenharmony_ci<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">linker</span>(<span class="hljs-params">specifier, referencingModule</span>) {
7841cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (specifier === <span class="hljs-string">'foo'</span>) {
7851cb0ef41Sopenharmony_ci    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_">SourceTextModule</span>(<span class="hljs-string">`
7861cb0ef41Sopenharmony_ci      // The "secret" variable refers to the global variable we added to
7871cb0ef41Sopenharmony_ci      // "contextifiedObject" when creating the context.
7881cb0ef41Sopenharmony_ci      export default secret;
7891cb0ef41Sopenharmony_ci    `</span>, { <span class="hljs-attr">context</span>: referencingModule.<span class="hljs-property">context</span> });
7901cb0ef41Sopenharmony_ci
7911cb0ef41Sopenharmony_ci    <span class="hljs-comment">// Using `contextifiedObject` instead of `referencingModule.context`</span>
7921cb0ef41Sopenharmony_ci    <span class="hljs-comment">// here would work as well.</span>
7931cb0ef41Sopenharmony_ci  }
7941cb0ef41Sopenharmony_ci  <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">`Unable to resolve dependency: <span class="hljs-subst">${specifier}</span>`</span>);
7951cb0ef41Sopenharmony_ci}
7961cb0ef41Sopenharmony_ci<span class="hljs-keyword">await</span> bar.<span class="hljs-title function_">link</span>(linker);
7971cb0ef41Sopenharmony_ci
7981cb0ef41Sopenharmony_ci<span class="hljs-comment">// Step 3</span>
7991cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span>
8001cb0ef41Sopenharmony_ci<span class="hljs-comment">// Evaluate the Module. The evaluate() method returns a promise which will</span>
8011cb0ef41Sopenharmony_ci<span class="hljs-comment">// resolve after the module has finished evaluating.</span>
8021cb0ef41Sopenharmony_ci
8031cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints 42.</span>
8041cb0ef41Sopenharmony_ci<span class="hljs-keyword">await</span> bar.evaluate();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:vm'</span>);
8051cb0ef41Sopenharmony_ci
8061cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> contextifiedObject = vm.<span class="hljs-title function_">createContext</span>({
8071cb0ef41Sopenharmony_ci  <span class="hljs-attr">secret</span>: <span class="hljs-number">42</span>,
8081cb0ef41Sopenharmony_ci  <span class="hljs-attr">print</span>: <span class="hljs-variable language_">console</span>.<span class="hljs-property">log</span>,
8091cb0ef41Sopenharmony_ci});
8101cb0ef41Sopenharmony_ci
8111cb0ef41Sopenharmony_ci(<span class="hljs-keyword">async</span> () => {
8121cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Step 1</span>
8131cb0ef41Sopenharmony_ci  <span class="hljs-comment">//</span>
8141cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Create a Module by constructing a new `vm.SourceTextModule` object. This</span>
8151cb0ef41Sopenharmony_ci  <span class="hljs-comment">// parses the provided source text, throwing a `SyntaxError` if anything goes</span>
8161cb0ef41Sopenharmony_ci  <span class="hljs-comment">// wrong. By default, a Module is created in the top context. But here, we</span>
8171cb0ef41Sopenharmony_ci  <span class="hljs-comment">// specify `contextifiedObject` as the context this Module belongs to.</span>
8181cb0ef41Sopenharmony_ci  <span class="hljs-comment">//</span>
8191cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Here, we attempt to obtain the default export from the module "foo", and</span>
8201cb0ef41Sopenharmony_ci  <span class="hljs-comment">// put it into local binding "secret".</span>
8211cb0ef41Sopenharmony_ci
8221cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> bar = <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_">SourceTextModule</span>(<span class="hljs-string">`
8231cb0ef41Sopenharmony_ci    import s from 'foo';
8241cb0ef41Sopenharmony_ci    s;
8251cb0ef41Sopenharmony_ci    print(s);
8261cb0ef41Sopenharmony_ci  `</span>, { <span class="hljs-attr">context</span>: contextifiedObject });
8271cb0ef41Sopenharmony_ci
8281cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Step 2</span>
8291cb0ef41Sopenharmony_ci  <span class="hljs-comment">//</span>
8301cb0ef41Sopenharmony_ci  <span class="hljs-comment">// "Link" the imported dependencies of this Module to it.</span>
8311cb0ef41Sopenharmony_ci  <span class="hljs-comment">//</span>
8321cb0ef41Sopenharmony_ci  <span class="hljs-comment">// The provided linking callback (the "linker") accepts two arguments: the</span>
8331cb0ef41Sopenharmony_ci  <span class="hljs-comment">// parent module (`bar` in this case) and the string that is the specifier of</span>
8341cb0ef41Sopenharmony_ci  <span class="hljs-comment">// the imported module. The callback is expected to return a Module that</span>
8351cb0ef41Sopenharmony_ci  <span class="hljs-comment">// corresponds to the provided specifier, with certain requirements documented</span>
8361cb0ef41Sopenharmony_ci  <span class="hljs-comment">// in `module.link()`.</span>
8371cb0ef41Sopenharmony_ci  <span class="hljs-comment">//</span>
8381cb0ef41Sopenharmony_ci  <span class="hljs-comment">// If linking has not started for the returned Module, the same linker</span>
8391cb0ef41Sopenharmony_ci  <span class="hljs-comment">// callback will be called on the returned Module.</span>
8401cb0ef41Sopenharmony_ci  <span class="hljs-comment">//</span>
8411cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Even top-level Modules without dependencies must be explicitly linked. The</span>
8421cb0ef41Sopenharmony_ci  <span class="hljs-comment">// callback provided would never be called, however.</span>
8431cb0ef41Sopenharmony_ci  <span class="hljs-comment">//</span>
8441cb0ef41Sopenharmony_ci  <span class="hljs-comment">// The link() method returns a Promise that will be resolved when all the</span>
8451cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Promises returned by the linker resolve.</span>
8461cb0ef41Sopenharmony_ci  <span class="hljs-comment">//</span>
8471cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Note: This is a contrived example in that the linker function creates a new</span>
8481cb0ef41Sopenharmony_ci  <span class="hljs-comment">// "foo" module every time it is called. In a full-fledged module system, a</span>
8491cb0ef41Sopenharmony_ci  <span class="hljs-comment">// cache would probably be used to avoid duplicated modules.</span>
8501cb0ef41Sopenharmony_ci
8511cb0ef41Sopenharmony_ci  <span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">linker</span>(<span class="hljs-params">specifier, referencingModule</span>) {
8521cb0ef41Sopenharmony_ci    <span class="hljs-keyword">if</span> (specifier === <span class="hljs-string">'foo'</span>) {
8531cb0ef41Sopenharmony_ci      <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_">SourceTextModule</span>(<span class="hljs-string">`
8541cb0ef41Sopenharmony_ci        // The "secret" variable refers to the global variable we added to
8551cb0ef41Sopenharmony_ci        // "contextifiedObject" when creating the context.
8561cb0ef41Sopenharmony_ci        export default secret;
8571cb0ef41Sopenharmony_ci      `</span>, { <span class="hljs-attr">context</span>: referencingModule.<span class="hljs-property">context</span> });
8581cb0ef41Sopenharmony_ci
8591cb0ef41Sopenharmony_ci      <span class="hljs-comment">// Using `contextifiedObject` instead of `referencingModule.context`</span>
8601cb0ef41Sopenharmony_ci      <span class="hljs-comment">// here would work as well.</span>
8611cb0ef41Sopenharmony_ci    }
8621cb0ef41Sopenharmony_ci    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">`Unable to resolve dependency: <span class="hljs-subst">${specifier}</span>`</span>);
8631cb0ef41Sopenharmony_ci  }
8641cb0ef41Sopenharmony_ci  <span class="hljs-keyword">await</span> bar.<span class="hljs-title function_">link</span>(linker);
8651cb0ef41Sopenharmony_ci
8661cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Step 3</span>
8671cb0ef41Sopenharmony_ci  <span class="hljs-comment">//</span>
8681cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Evaluate the Module. The evaluate() method returns a promise which will</span>
8691cb0ef41Sopenharmony_ci  <span class="hljs-comment">// resolve after the module has finished evaluating.</span>
8701cb0ef41Sopenharmony_ci
8711cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Prints 42.</span>
8721cb0ef41Sopenharmony_ci  <span class="hljs-keyword">await</span> bar.evaluate();
8731cb0ef41Sopenharmony_ci})();</code><button class="copy-button">copy</button></pre>
8741cb0ef41Sopenharmony_ci<h4><code>module.dependencySpecifiers</code><span><a class="mark" href="#moduledependencyspecifiers" id="moduledependencyspecifiers">#</a></span><a aria-hidden="true" class="legacy" id="vm_module_dependencyspecifiers"></a></h4>
8751cb0ef41Sopenharmony_ci<ul>
8761cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
8771cb0ef41Sopenharmony_ci</ul>
8781cb0ef41Sopenharmony_ci<p>The specifiers of all dependencies of this module. The returned array is frozen
8791cb0ef41Sopenharmony_cito disallow any changes to it.</p>
8801cb0ef41Sopenharmony_ci<p>Corresponds to the <code>[[RequestedModules]]</code> field of <a href="https://tc39.es/ecma262/#sec-cyclic-module-records">Cyclic Module Record</a>s in
8811cb0ef41Sopenharmony_cithe ECMAScript specification.</p>
8821cb0ef41Sopenharmony_ci<h4><code>module.error</code><span><a class="mark" href="#moduleerror" id="moduleerror">#</a></span><a aria-hidden="true" class="legacy" id="vm_module_error"></a></h4>
8831cb0ef41Sopenharmony_ci<ul>
8841cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
8851cb0ef41Sopenharmony_ci</ul>
8861cb0ef41Sopenharmony_ci<p>If the <code>module.status</code> is <code>'errored'</code>, this property contains the exception
8871cb0ef41Sopenharmony_cithrown by the module during evaluation. If the status is anything else,
8881cb0ef41Sopenharmony_ciaccessing this property will result in a thrown exception.</p>
8891cb0ef41Sopenharmony_ci<p>The value <code>undefined</code> cannot be used for cases where there is not a thrown
8901cb0ef41Sopenharmony_ciexception due to possible ambiguity with <code>throw undefined;</code>.</p>
8911cb0ef41Sopenharmony_ci<p>Corresponds to the <code>[[EvaluationError]]</code> field of <a href="https://tc39.es/ecma262/#sec-cyclic-module-records">Cyclic Module Record</a>s
8921cb0ef41Sopenharmony_ciin the ECMAScript specification.</p>
8931cb0ef41Sopenharmony_ci<h4><code>module.evaluate([options])</code><span><a class="mark" href="#moduleevaluateoptions" id="moduleevaluateoptions">#</a></span><a aria-hidden="true" class="legacy" id="vm_module_evaluate_options"></a></h4>
8941cb0ef41Sopenharmony_ci<ul>
8951cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
8961cb0ef41Sopenharmony_ci<ul>
8971cb0ef41Sopenharmony_ci<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Specifies the number of milliseconds to evaluate
8981cb0ef41Sopenharmony_cibefore terminating execution. If execution is interrupted, an <a href="errors.html#class-error"><code>Error</code></a>
8991cb0ef41Sopenharmony_ciwill be thrown. This value must be a strictly positive integer.</li>
9001cb0ef41Sopenharmony_ci<li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, receiving <code>SIGINT</code>
9011cb0ef41Sopenharmony_ci(<kbd>Ctrl</kbd>+<kbd>C</kbd>) will terminate execution and throw an
9021cb0ef41Sopenharmony_ci<a href="errors.html#class-error"><code>Error</code></a>. Existing handlers for the event that have been attached via
9031cb0ef41Sopenharmony_ci<code>process.on('SIGINT')</code> are disabled during script execution, but continue to
9041cb0ef41Sopenharmony_ciwork after that. <strong>Default:</strong> <code>false</code>.</li>
9051cb0ef41Sopenharmony_ci</ul>
9061cb0ef41Sopenharmony_ci</li>
9071cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
9081cb0ef41Sopenharmony_ci</ul>
9091cb0ef41Sopenharmony_ci<p>Evaluate the module.</p>
9101cb0ef41Sopenharmony_ci<p>This must be called after the module has been linked; otherwise it will reject.
9111cb0ef41Sopenharmony_ciIt could be called also when the module has already been evaluated, in which
9121cb0ef41Sopenharmony_cicase it will either do nothing if the initial evaluation ended in success
9131cb0ef41Sopenharmony_ci(<code>module.status</code> is <code>'evaluated'</code>) or it will re-throw the exception that the
9141cb0ef41Sopenharmony_ciinitial evaluation resulted in (<code>module.status</code> is <code>'errored'</code>).</p>
9151cb0ef41Sopenharmony_ci<p>This method cannot be called while the module is being evaluated
9161cb0ef41Sopenharmony_ci(<code>module.status</code> is <code>'evaluating'</code>).</p>
9171cb0ef41Sopenharmony_ci<p>Corresponds to the <a href="https://tc39.es/ecma262/#sec-moduleevaluation">Evaluate() concrete method</a> field of <a href="https://tc39.es/ecma262/#sec-cyclic-module-records">Cyclic Module
9181cb0ef41Sopenharmony_ciRecord</a>s in the ECMAScript specification.</p>
9191cb0ef41Sopenharmony_ci<h4><code>module.identifier</code><span><a class="mark" href="#moduleidentifier" id="moduleidentifier">#</a></span><a aria-hidden="true" class="legacy" id="vm_module_identifier"></a></h4>
9201cb0ef41Sopenharmony_ci<ul>
9211cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
9221cb0ef41Sopenharmony_ci</ul>
9231cb0ef41Sopenharmony_ci<p>The identifier of the current module, as set in the constructor.</p>
9241cb0ef41Sopenharmony_ci<h4><code>module.link(linker)</code><span><a class="mark" href="#modulelinklinker" id="modulelinklinker">#</a></span><a aria-hidden="true" class="legacy" id="vm_module_link_linker"></a></h4>
9251cb0ef41Sopenharmony_ci<div class="api_metadata">
9261cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
9271cb0ef41Sopenharmony_ci<table>
9281cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
9291cb0ef41Sopenharmony_ci<tr><td>v18.19.0</td>
9301cb0ef41Sopenharmony_ci<td><p>The option <code>extra.assert</code> is renamed to <code>extra.attributes</code>. The former name is still provided for backward compatibility.</p></td></tr>
9311cb0ef41Sopenharmony_ci</tbody></table>
9321cb0ef41Sopenharmony_ci</details>
9331cb0ef41Sopenharmony_ci</div>
9341cb0ef41Sopenharmony_ci<ul>
9351cb0ef41Sopenharmony_ci<li><code>linker</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
9361cb0ef41Sopenharmony_ci<ul>
9371cb0ef41Sopenharmony_ci<li>
9381cb0ef41Sopenharmony_ci<p><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The specifier of the requested module:</p>
9391cb0ef41Sopenharmony_ci<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> foo <span class="hljs-keyword">from</span> <span class="hljs-string">'foo'</span>;
9401cb0ef41Sopenharmony_ci<span class="hljs-comment">//              ^^^^^ the module specifier</span></code> <button class="copy-button">copy</button></pre>
9411cb0ef41Sopenharmony_ci</li>
9421cb0ef41Sopenharmony_ci<li>
9431cb0ef41Sopenharmony_ci<p><code>referencingModule</code> <a href="vm.html#class-vmmodule" class="type">&#x3C;vm.Module></a> The <code>Module</code> object <code>link()</code> is called on.</p>
9441cb0ef41Sopenharmony_ci</li>
9451cb0ef41Sopenharmony_ci<li>
9461cb0ef41Sopenharmony_ci<p><code>extra</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></p>
9471cb0ef41Sopenharmony_ci<ul>
9481cb0ef41Sopenharmony_ci<li><code>attributes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> The data from the attribute:
9491cb0ef41Sopenharmony_ci<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> foo <span class="hljs-keyword">from</span> <span class="hljs-string">'foo'</span> <span class="hljs-keyword">with</span> { <span class="hljs-attr">name</span>: <span class="hljs-string">'value'</span> };
9501cb0ef41Sopenharmony_ci<span class="hljs-comment">//                           ^^^^^^^^^^^^^^^^^ the attribute</span></code> <button class="copy-button">copy</button></pre>
9511cb0ef41Sopenharmony_ciPer ECMA-262, hosts are expected to trigger an error if an
9521cb0ef41Sopenharmony_ciunsupported attribute is present.</li>
9531cb0ef41Sopenharmony_ci<li><code>assert</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> Alias for <code>extra.attributes</code>.</li>
9541cb0ef41Sopenharmony_ci</ul>
9551cb0ef41Sopenharmony_ci</li>
9561cb0ef41Sopenharmony_ci<li>
9571cb0ef41Sopenharmony_ci<p>Returns: <a href="vm.html#class-vmmodule" class="type">&#x3C;vm.Module></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a></p>
9581cb0ef41Sopenharmony_ci</li>
9591cb0ef41Sopenharmony_ci</ul>
9601cb0ef41Sopenharmony_ci</li>
9611cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a></li>
9621cb0ef41Sopenharmony_ci</ul>
9631cb0ef41Sopenharmony_ci<p>Link module dependencies. This method must be called before evaluation, and
9641cb0ef41Sopenharmony_cican only be called once per module.</p>
9651cb0ef41Sopenharmony_ci<p>The function is expected to return a <code>Module</code> object or a <code>Promise</code> that
9661cb0ef41Sopenharmony_cieventually resolves to a <code>Module</code> object. The returned <code>Module</code> must satisfy the
9671cb0ef41Sopenharmony_cifollowing two invariants:</p>
9681cb0ef41Sopenharmony_ci<ul>
9691cb0ef41Sopenharmony_ci<li>It must belong to the same context as the parent <code>Module</code>.</li>
9701cb0ef41Sopenharmony_ci<li>Its <code>status</code> must not be <code>'errored'</code>.</li>
9711cb0ef41Sopenharmony_ci</ul>
9721cb0ef41Sopenharmony_ci<p>If the returned <code>Module</code>'s <code>status</code> is <code>'unlinked'</code>, this method will be
9731cb0ef41Sopenharmony_cirecursively called on the returned <code>Module</code> with the same provided <code>linker</code>
9741cb0ef41Sopenharmony_cifunction.</p>
9751cb0ef41Sopenharmony_ci<p><code>link()</code> returns a <code>Promise</code> that will either get resolved when all linking
9761cb0ef41Sopenharmony_ciinstances resolve to a valid <code>Module</code>, or rejected if the linker function either
9771cb0ef41Sopenharmony_cithrows an exception or returns an invalid <code>Module</code>.</p>
9781cb0ef41Sopenharmony_ci<p>The linker function roughly corresponds to the implementation-defined
9791cb0ef41Sopenharmony_ci<a href="https://tc39.es/ecma262/#sec-hostresolveimportedmodule">HostResolveImportedModule</a> abstract operation in the ECMAScript
9801cb0ef41Sopenharmony_cispecification, with a few key differences:</p>
9811cb0ef41Sopenharmony_ci<ul>
9821cb0ef41Sopenharmony_ci<li>The linker function is allowed to be asynchronous while
9831cb0ef41Sopenharmony_ci<a href="https://tc39.es/ecma262/#sec-hostresolveimportedmodule">HostResolveImportedModule</a> is synchronous.</li>
9841cb0ef41Sopenharmony_ci</ul>
9851cb0ef41Sopenharmony_ci<p>The actual <a href="https://tc39.es/ecma262/#sec-hostresolveimportedmodule">HostResolveImportedModule</a> implementation used during module
9861cb0ef41Sopenharmony_cilinking is one that returns the modules linked during linking. Since at
9871cb0ef41Sopenharmony_cithat point all modules would have been fully linked already, the
9881cb0ef41Sopenharmony_ci<a href="https://tc39.es/ecma262/#sec-hostresolveimportedmodule">HostResolveImportedModule</a> implementation is fully synchronous per
9891cb0ef41Sopenharmony_cispecification.</p>
9901cb0ef41Sopenharmony_ci<p>Corresponds to the <a href="https://tc39.es/ecma262/#sec-moduledeclarationlinking">Link() concrete method</a> field of <a href="https://tc39.es/ecma262/#sec-cyclic-module-records">Cyclic Module
9911cb0ef41Sopenharmony_ciRecord</a>s in the ECMAScript specification.</p>
9921cb0ef41Sopenharmony_ci<h4><code>module.namespace</code><span><a class="mark" href="#modulenamespace" id="modulenamespace">#</a></span><a aria-hidden="true" class="legacy" id="vm_module_namespace"></a></h4>
9931cb0ef41Sopenharmony_ci<ul>
9941cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
9951cb0ef41Sopenharmony_ci</ul>
9961cb0ef41Sopenharmony_ci<p>The namespace object of the module. This is only available after linking
9971cb0ef41Sopenharmony_ci(<code>module.link()</code>) has completed.</p>
9981cb0ef41Sopenharmony_ci<p>Corresponds to the <a href="https://tc39.es/ecma262/#sec-getmodulenamespace">GetModuleNamespace</a> abstract operation in the ECMAScript
9991cb0ef41Sopenharmony_cispecification.</p>
10001cb0ef41Sopenharmony_ci<h4><code>module.status</code><span><a class="mark" href="#modulestatus" id="modulestatus">#</a></span><a aria-hidden="true" class="legacy" id="vm_module_status"></a></h4>
10011cb0ef41Sopenharmony_ci<ul>
10021cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
10031cb0ef41Sopenharmony_ci</ul>
10041cb0ef41Sopenharmony_ci<p>The current status of the module. Will be one of:</p>
10051cb0ef41Sopenharmony_ci<ul>
10061cb0ef41Sopenharmony_ci<li>
10071cb0ef41Sopenharmony_ci<p><code>'unlinked'</code>: <code>module.link()</code> has not yet been called.</p>
10081cb0ef41Sopenharmony_ci</li>
10091cb0ef41Sopenharmony_ci<li>
10101cb0ef41Sopenharmony_ci<p><code>'linking'</code>: <code>module.link()</code> has been called, but not all Promises returned
10111cb0ef41Sopenharmony_ciby the linker function have been resolved yet.</p>
10121cb0ef41Sopenharmony_ci</li>
10131cb0ef41Sopenharmony_ci<li>
10141cb0ef41Sopenharmony_ci<p><code>'linked'</code>: The module has been linked successfully, and all of its
10151cb0ef41Sopenharmony_cidependencies are linked, but <code>module.evaluate()</code> has not yet been called.</p>
10161cb0ef41Sopenharmony_ci</li>
10171cb0ef41Sopenharmony_ci<li>
10181cb0ef41Sopenharmony_ci<p><code>'evaluating'</code>: The module is being evaluated through a <code>module.evaluate()</code> on
10191cb0ef41Sopenharmony_ciitself or a parent module.</p>
10201cb0ef41Sopenharmony_ci</li>
10211cb0ef41Sopenharmony_ci<li>
10221cb0ef41Sopenharmony_ci<p><code>'evaluated'</code>: The module has been successfully evaluated.</p>
10231cb0ef41Sopenharmony_ci</li>
10241cb0ef41Sopenharmony_ci<li>
10251cb0ef41Sopenharmony_ci<p><code>'errored'</code>: The module has been evaluated, but an exception was thrown.</p>
10261cb0ef41Sopenharmony_ci</li>
10271cb0ef41Sopenharmony_ci</ul>
10281cb0ef41Sopenharmony_ci<p>Other than <code>'errored'</code>, this status string corresponds to the specification's
10291cb0ef41Sopenharmony_ci<a href="https://tc39.es/ecma262/#sec-cyclic-module-records">Cyclic Module Record</a>'s <code>[[Status]]</code> field. <code>'errored'</code> corresponds to
10301cb0ef41Sopenharmony_ci<code>'evaluated'</code> in the specification, but with <code>[[EvaluationError]]</code> set to a
10311cb0ef41Sopenharmony_civalue that is not <code>undefined</code>.</p>
10321cb0ef41Sopenharmony_ci</section><section><h3>Class: <code>vm.SourceTextModule</code><span><a class="mark" href="#class-vmsourcetextmodule" id="class-vmsourcetextmodule">#</a></span><a aria-hidden="true" class="legacy" id="vm_class_vm_sourcetextmodule"></a></h3>
10331cb0ef41Sopenharmony_ci<div class="api_metadata">
10341cb0ef41Sopenharmony_ci<span>Added in: v9.6.0</span>
10351cb0ef41Sopenharmony_ci</div>
10361cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
10371cb0ef41Sopenharmony_ci<p>This feature is only available with the <code>--experimental-vm-modules</code> command
10381cb0ef41Sopenharmony_ciflag enabled.</p>
10391cb0ef41Sopenharmony_ci<ul>
10401cb0ef41Sopenharmony_ci<li>Extends: <a href="vm.html#class-vmmodule" class="type">&#x3C;vm.Module></a></li>
10411cb0ef41Sopenharmony_ci</ul>
10421cb0ef41Sopenharmony_ci<p>The <code>vm.SourceTextModule</code> class provides the <a href="https://tc39.es/ecma262/#sec-source-text-module-records">Source Text Module Record</a> as
10431cb0ef41Sopenharmony_cidefined in the ECMAScript specification.</p>
10441cb0ef41Sopenharmony_ci<h4><code>new vm.SourceTextModule(code[, options])</code><span><a class="mark" href="#new-vmsourcetextmodulecode-options" id="new-vmsourcetextmodulecode-options">#</a></span><a aria-hidden="true" class="legacy" id="vm_new_vm_sourcetextmodule_code_options"></a></h4>
10451cb0ef41Sopenharmony_ci<div class="api_metadata">
10461cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
10471cb0ef41Sopenharmony_ci<table>
10481cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
10491cb0ef41Sopenharmony_ci<tr><td>v17.0.0, v16.12.0</td>
10501cb0ef41Sopenharmony_ci<td><p>Added support for import attributes to the <code>importModuleDynamically</code> parameter.</p></td></tr>
10511cb0ef41Sopenharmony_ci</tbody></table>
10521cb0ef41Sopenharmony_ci</details>
10531cb0ef41Sopenharmony_ci</div>
10541cb0ef41Sopenharmony_ci<ul>
10551cb0ef41Sopenharmony_ci<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> JavaScript Module code to parse</li>
10561cb0ef41Sopenharmony_ci<li><code>options</code>
10571cb0ef41Sopenharmony_ci<ul>
10581cb0ef41Sopenharmony_ci<li><code>identifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> String used in stack traces.
10591cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'vm:module(i)'</code> where <code>i</code> is a context-specific ascending
10601cb0ef41Sopenharmony_ciindex.</li>
10611cb0ef41Sopenharmony_ci<li><code>cachedData</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> Provides an optional <code>Buffer</code> or
10621cb0ef41Sopenharmony_ci<code>TypedArray</code>, or <code>DataView</code> with V8's code cache data for the supplied
10631cb0ef41Sopenharmony_cisource. The <code>code</code> must be the same as the module from which this
10641cb0ef41Sopenharmony_ci<code>cachedData</code> was created.</li>
10651cb0ef41Sopenharmony_ci<li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> The <a href="#what-does-it-mean-to-contextify-an-object">contextified</a> object as returned by the
10661cb0ef41Sopenharmony_ci<code>vm.createContext()</code> method, to compile and evaluate this <code>Module</code> in.
10671cb0ef41Sopenharmony_ciIf no context is specified, the module is evaluated in the current
10681cb0ef41Sopenharmony_ciexecution context.</li>
10691cb0ef41Sopenharmony_ci<li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Specifies the line number offset that is displayed
10701cb0ef41Sopenharmony_ciin stack traces produced by this <code>Module</code>. <strong>Default:</strong> <code>0</code>.</li>
10711cb0ef41Sopenharmony_ci<li><code>columnOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Specifies the first-line column number offset that
10721cb0ef41Sopenharmony_ciis displayed in stack traces produced by this <code>Module</code>. <strong>Default:</strong> <code>0</code>.</li>
10731cb0ef41Sopenharmony_ci<li><code>initializeImportMeta</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Called during evaluation of this <code>Module</code>
10741cb0ef41Sopenharmony_cito initialize the <code>import.meta</code>.
10751cb0ef41Sopenharmony_ci<ul>
10761cb0ef41Sopenharmony_ci<li><code>meta</code> <a href="esm.html#importmeta" class="type">&#x3C;import.meta></a></li>
10771cb0ef41Sopenharmony_ci<li><code>module</code> <a href="vm.html#class-vmsourcetextmodule" class="type">&#x3C;vm.SourceTextModule></a></li>
10781cb0ef41Sopenharmony_ci</ul>
10791cb0ef41Sopenharmony_ci</li>
10801cb0ef41Sopenharmony_ci<li><code>importModuleDynamically</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Called during evaluation of this module
10811cb0ef41Sopenharmony_ciwhen <code>import()</code> is called. If this option is not specified, calls to
10821cb0ef41Sopenharmony_ci<code>import()</code> will reject with <a href="errors.html#err_vm_dynamic_import_callback_missing"><code>ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING</code></a>.
10831cb0ef41Sopenharmony_ciIf <code>--experimental-vm-modules</code> isn't set, this callback will be ignored
10841cb0ef41Sopenharmony_ciand calls to <code>import()</code> will reject with
10851cb0ef41Sopenharmony_ci<a href="errors.html#err_vm_dynamic_import_callback_missing_flag"><code>ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG</code></a>.
10861cb0ef41Sopenharmony_ci<ul>
10871cb0ef41Sopenharmony_ci<li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> specifier passed to <code>import()</code></li>
10881cb0ef41Sopenharmony_ci<li><code>module</code> <a href="vm.html#class-vmmodule" class="type">&#x3C;vm.Module></a></li>
10891cb0ef41Sopenharmony_ci<li><code>importAttributes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> The <code>"assert"</code> value passed to the
10901cb0ef41Sopenharmony_ci<a href="https://tc39.es/proposal-import-attributes/#sec-evaluate-import-call"><code>optionsExpression</code></a> optional parameter, or an empty object if no value
10911cb0ef41Sopenharmony_ciwas provided.</li>
10921cb0ef41Sopenharmony_ci<li>Returns: <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects" class="type">&#x3C;Module Namespace Object></a> | <a href="vm.html#class-vmmodule" class="type">&#x3C;vm.Module></a> Returning a <code>vm.Module</code> is
10931cb0ef41Sopenharmony_cirecommended in order to take advantage of error tracking, and to avoid
10941cb0ef41Sopenharmony_ciissues with namespaces that contain <code>then</code> function exports.</li>
10951cb0ef41Sopenharmony_ci</ul>
10961cb0ef41Sopenharmony_ci</li>
10971cb0ef41Sopenharmony_ci</ul>
10981cb0ef41Sopenharmony_ci</li>
10991cb0ef41Sopenharmony_ci</ul>
11001cb0ef41Sopenharmony_ci<p>Creates a new <code>SourceTextModule</code> instance.</p>
11011cb0ef41Sopenharmony_ci<p>Properties assigned to the <code>import.meta</code> object that are objects may
11021cb0ef41Sopenharmony_ciallow the module to access information outside the specified <code>context</code>. Use
11031cb0ef41Sopenharmony_ci<code>vm.runInContext()</code> to create objects in a specific context.</p>
11041cb0ef41Sopenharmony_ci
11051cb0ef41Sopenharmony_ci<pre class="with-60-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> vm <span class="hljs-keyword">from</span> <span class="hljs-string">'node:vm'</span>;
11061cb0ef41Sopenharmony_ci
11071cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> contextifiedObject = vm.<span class="hljs-title function_">createContext</span>({ <span class="hljs-attr">secret</span>: <span class="hljs-number">42</span> });
11081cb0ef41Sopenharmony_ci
11091cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> <span class="hljs-variable language_">module</span> = <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_">SourceTextModule</span>(
11101cb0ef41Sopenharmony_ci  <span class="hljs-string">'Object.getPrototypeOf(import.meta.prop).secret = secret;'</span>,
11111cb0ef41Sopenharmony_ci  {
11121cb0ef41Sopenharmony_ci    <span class="hljs-title function_">initializeImportMeta</span>(<span class="hljs-params">meta</span>) {
11131cb0ef41Sopenharmony_ci      <span class="hljs-comment">// Note: this object is created in the top context. As such,</span>
11141cb0ef41Sopenharmony_ci      <span class="hljs-comment">// Object.getPrototypeOf(import.meta.prop) points to the</span>
11151cb0ef41Sopenharmony_ci      <span class="hljs-comment">// Object.prototype in the top context rather than that in</span>
11161cb0ef41Sopenharmony_ci      <span class="hljs-comment">// the contextified object.</span>
11171cb0ef41Sopenharmony_ci      meta.<span class="hljs-property">prop</span> = {};
11181cb0ef41Sopenharmony_ci    },
11191cb0ef41Sopenharmony_ci  });
11201cb0ef41Sopenharmony_ci<span class="hljs-comment">// Since module has no dependencies, the linker function will never be called.</span>
11211cb0ef41Sopenharmony_ci<span class="hljs-keyword">await</span> <span class="hljs-variable language_">module</span>.<span class="hljs-title function_">link</span>(<span class="hljs-function">() =></span> {});
11221cb0ef41Sopenharmony_ci<span class="hljs-keyword">await</span> <span class="hljs-variable language_">module</span>.evaluate();
11231cb0ef41Sopenharmony_ci
11241cb0ef41Sopenharmony_ci<span class="hljs-comment">// Now, Object.prototype.secret will be equal to 42.</span>
11251cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span>
11261cb0ef41Sopenharmony_ci<span class="hljs-comment">// To fix this problem, replace</span>
11271cb0ef41Sopenharmony_ci<span class="hljs-comment">//     meta.prop = {};</span>
11281cb0ef41Sopenharmony_ci<span class="hljs-comment">// above with</span>
11291cb0ef41Sopenharmony_ci<span class="hljs-comment">//     meta.prop = vm.runInContext('{}', contextifiedObject);</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:vm'</span>);
11301cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> contextifiedObject = vm.<span class="hljs-title function_">createContext</span>({ <span class="hljs-attr">secret</span>: <span class="hljs-number">42</span> });
11311cb0ef41Sopenharmony_ci(<span class="hljs-keyword">async</span> () => {
11321cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> <span class="hljs-variable language_">module</span> = <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_">SourceTextModule</span>(
11331cb0ef41Sopenharmony_ci    <span class="hljs-string">'Object.getPrototypeOf(import.meta.prop).secret = secret;'</span>,
11341cb0ef41Sopenharmony_ci    {
11351cb0ef41Sopenharmony_ci      <span class="hljs-title function_">initializeImportMeta</span>(<span class="hljs-params">meta</span>) {
11361cb0ef41Sopenharmony_ci        <span class="hljs-comment">// Note: this object is created in the top context. As such,</span>
11371cb0ef41Sopenharmony_ci        <span class="hljs-comment">// Object.getPrototypeOf(import.meta.prop) points to the</span>
11381cb0ef41Sopenharmony_ci        <span class="hljs-comment">// Object.prototype in the top context rather than that in</span>
11391cb0ef41Sopenharmony_ci        <span class="hljs-comment">// the contextified object.</span>
11401cb0ef41Sopenharmony_ci        meta.<span class="hljs-property">prop</span> = {};
11411cb0ef41Sopenharmony_ci      },
11421cb0ef41Sopenharmony_ci    });
11431cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Since module has no dependencies, the linker function will never be called.</span>
11441cb0ef41Sopenharmony_ci  <span class="hljs-keyword">await</span> <span class="hljs-variable language_">module</span>.<span class="hljs-title function_">link</span>(<span class="hljs-function">() =></span> {});
11451cb0ef41Sopenharmony_ci  <span class="hljs-keyword">await</span> <span class="hljs-variable language_">module</span>.evaluate();
11461cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Now, Object.prototype.secret will be equal to 42.</span>
11471cb0ef41Sopenharmony_ci  <span class="hljs-comment">//</span>
11481cb0ef41Sopenharmony_ci  <span class="hljs-comment">// To fix this problem, replace</span>
11491cb0ef41Sopenharmony_ci  <span class="hljs-comment">//     meta.prop = {};</span>
11501cb0ef41Sopenharmony_ci  <span class="hljs-comment">// above with</span>
11511cb0ef41Sopenharmony_ci  <span class="hljs-comment">//     meta.prop = vm.runInContext('{}', contextifiedObject);</span>
11521cb0ef41Sopenharmony_ci})();</code><button class="copy-button">copy</button></pre>
11531cb0ef41Sopenharmony_ci<h4><code>sourceTextModule.createCachedData()</code><span><a class="mark" href="#sourcetextmodulecreatecacheddata" id="sourcetextmodulecreatecacheddata">#</a></span><a aria-hidden="true" class="legacy" id="vm_sourcetextmodule_createcacheddata"></a></h4>
11541cb0ef41Sopenharmony_ci<div class="api_metadata">
11551cb0ef41Sopenharmony_ci<span>Added in: v13.7.0, v12.17.0</span>
11561cb0ef41Sopenharmony_ci</div>
11571cb0ef41Sopenharmony_ci<ul>
11581cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
11591cb0ef41Sopenharmony_ci</ul>
11601cb0ef41Sopenharmony_ci<p>Creates a code cache that can be used with the <code>SourceTextModule</code> constructor's
11611cb0ef41Sopenharmony_ci<code>cachedData</code> option. Returns a <code>Buffer</code>. This method may be called any number
11621cb0ef41Sopenharmony_ciof times before the module has been evaluated.</p>
11631cb0ef41Sopenharmony_ci<p>The code cache of the <code>SourceTextModule</code> doesn't contain any JavaScript
11641cb0ef41Sopenharmony_ciobservable states. The code cache is safe to be saved along side the script
11651cb0ef41Sopenharmony_cisource and used to construct new <code>SourceTextModule</code> instances multiple times.</p>
11661cb0ef41Sopenharmony_ci<p>Functions in the <code>SourceTextModule</code> source can be marked as lazily compiled
11671cb0ef41Sopenharmony_ciand they are not compiled at construction of the <code>SourceTextModule</code>. These
11681cb0ef41Sopenharmony_cifunctions are going to be compiled when they are invoked the first time. The
11691cb0ef41Sopenharmony_cicode cache serializes the metadata that V8 currently knows about the
11701cb0ef41Sopenharmony_ci<code>SourceTextModule</code> that it can use to speed up future compilations.</p>
11711cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// Create an initial module</span>
11721cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> <span class="hljs-variable language_">module</span> = <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_">SourceTextModule</span>(<span class="hljs-string">'const a = 1;'</span>);
11731cb0ef41Sopenharmony_ci
11741cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create cached data from this module</span>
11751cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> cachedData = <span class="hljs-variable language_">module</span>.<span class="hljs-title function_">createCachedData</span>();
11761cb0ef41Sopenharmony_ci
11771cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create a new module using the cached data. The code must be the same.</span>
11781cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> module2 = <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_">SourceTextModule</span>(<span class="hljs-string">'const a = 1;'</span>, { cachedData });</code> <button class="copy-button">copy</button></pre>
11791cb0ef41Sopenharmony_ci</section><section><h3>Class: <code>vm.SyntheticModule</code><span><a class="mark" href="#class-vmsyntheticmodule" id="class-vmsyntheticmodule">#</a></span><a aria-hidden="true" class="legacy" id="vm_class_vm_syntheticmodule"></a></h3>
11801cb0ef41Sopenharmony_ci<div class="api_metadata">
11811cb0ef41Sopenharmony_ci<span>Added in: v13.0.0, v12.16.0</span>
11821cb0ef41Sopenharmony_ci</div>
11831cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
11841cb0ef41Sopenharmony_ci<p>This feature is only available with the <code>--experimental-vm-modules</code> command
11851cb0ef41Sopenharmony_ciflag enabled.</p>
11861cb0ef41Sopenharmony_ci<ul>
11871cb0ef41Sopenharmony_ci<li>Extends: <a href="vm.html#class-vmmodule" class="type">&#x3C;vm.Module></a></li>
11881cb0ef41Sopenharmony_ci</ul>
11891cb0ef41Sopenharmony_ci<p>The <code>vm.SyntheticModule</code> class provides the <a href="https://heycam.github.io/webidl/#synthetic-module-records">Synthetic Module Record</a> as
11901cb0ef41Sopenharmony_cidefined in the WebIDL specification. The purpose of synthetic modules is to
11911cb0ef41Sopenharmony_ciprovide a generic interface for exposing non-JavaScript sources to ECMAScript
11921cb0ef41Sopenharmony_cimodule graphs.</p>
11931cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:vm'</span>);
11941cb0ef41Sopenharmony_ci
11951cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> source = <span class="hljs-string">'{ "a": 1 }'</span>;
11961cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> <span class="hljs-variable language_">module</span> = <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_">SyntheticModule</span>([<span class="hljs-string">'default'</span>], <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) {
11971cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> obj = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">parse</span>(source);
11981cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">setExport</span>(<span class="hljs-string">'default'</span>, obj);
11991cb0ef41Sopenharmony_ci});
12001cb0ef41Sopenharmony_ci
12011cb0ef41Sopenharmony_ci<span class="hljs-comment">// Use `module` in linking...</span></code> <button class="copy-button">copy</button></pre>
12021cb0ef41Sopenharmony_ci<h4><code>new vm.SyntheticModule(exportNames, evaluateCallback[, options])</code><span><a class="mark" href="#new-vmsyntheticmoduleexportnames-evaluatecallback-options" id="new-vmsyntheticmoduleexportnames-evaluatecallback-options">#</a></span><a aria-hidden="true" class="legacy" id="vm_new_vm_syntheticmodule_exportnames_evaluatecallback_options"></a></h4>
12031cb0ef41Sopenharmony_ci<div class="api_metadata">
12041cb0ef41Sopenharmony_ci<span>Added in: v13.0.0, v12.16.0</span>
12051cb0ef41Sopenharmony_ci</div>
12061cb0ef41Sopenharmony_ci<ul>
12071cb0ef41Sopenharmony_ci<li><code>exportNames</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> Array of names that will be exported from the
12081cb0ef41Sopenharmony_cimodule.</li>
12091cb0ef41Sopenharmony_ci<li><code>evaluateCallback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Called when the module is evaluated.</li>
12101cb0ef41Sopenharmony_ci<li><code>options</code>
12111cb0ef41Sopenharmony_ci<ul>
12121cb0ef41Sopenharmony_ci<li><code>identifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> String used in stack traces.
12131cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'vm:module(i)'</code> where <code>i</code> is a context-specific ascending
12141cb0ef41Sopenharmony_ciindex.</li>
12151cb0ef41Sopenharmony_ci<li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> The <a href="#what-does-it-mean-to-contextify-an-object">contextified</a> object as returned by the
12161cb0ef41Sopenharmony_ci<code>vm.createContext()</code> method, to compile and evaluate this <code>Module</code> in.</li>
12171cb0ef41Sopenharmony_ci</ul>
12181cb0ef41Sopenharmony_ci</li>
12191cb0ef41Sopenharmony_ci</ul>
12201cb0ef41Sopenharmony_ci<p>Creates a new <code>SyntheticModule</code> instance.</p>
12211cb0ef41Sopenharmony_ci<p>Objects assigned to the exports of this instance may allow importers of
12221cb0ef41Sopenharmony_cithe module to access information outside the specified <code>context</code>. Use
12231cb0ef41Sopenharmony_ci<code>vm.runInContext()</code> to create objects in a specific context.</p>
12241cb0ef41Sopenharmony_ci<h4><code>syntheticModule.setExport(name, value)</code><span><a class="mark" href="#syntheticmodulesetexportname-value" id="syntheticmodulesetexportname-value">#</a></span><a aria-hidden="true" class="legacy" id="vm_syntheticmodule_setexport_name_value"></a></h4>
12251cb0ef41Sopenharmony_ci<div class="api_metadata">
12261cb0ef41Sopenharmony_ci<span>Added in: v13.0.0, v12.16.0</span>
12271cb0ef41Sopenharmony_ci</div>
12281cb0ef41Sopenharmony_ci<ul>
12291cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Name of the export to set.</li>
12301cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> The value to set the export to.</li>
12311cb0ef41Sopenharmony_ci</ul>
12321cb0ef41Sopenharmony_ci<p>This method is used after the module is linked to set the values of exports. If
12331cb0ef41Sopenharmony_ciit is called before the module is linked, an <a href="errors.html#err_vm_module_status"><code>ERR_VM_MODULE_STATUS</code></a> error
12341cb0ef41Sopenharmony_ciwill be thrown.</p>
12351cb0ef41Sopenharmony_ci
12361cb0ef41Sopenharmony_ci<pre class="with-30-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> vm <span class="hljs-keyword">from</span> <span class="hljs-string">'node:vm'</span>;
12371cb0ef41Sopenharmony_ci
12381cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> m = <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_">SyntheticModule</span>([<span class="hljs-string">'x'</span>], <span class="hljs-function">() =></span> {
12391cb0ef41Sopenharmony_ci  m.<span class="hljs-title function_">setExport</span>(<span class="hljs-string">'x'</span>, <span class="hljs-number">1</span>);
12401cb0ef41Sopenharmony_ci});
12411cb0ef41Sopenharmony_ci
12421cb0ef41Sopenharmony_ci<span class="hljs-keyword">await</span> m.<span class="hljs-title function_">link</span>(<span class="hljs-function">() =></span> {});
12431cb0ef41Sopenharmony_ci<span class="hljs-keyword">await</span> m.evaluate();
12441cb0ef41Sopenharmony_ci
12451cb0ef41Sopenharmony_ciassert.<span class="hljs-title function_">strictEqual</span>(m.<span class="hljs-property">namespace</span>.<span class="hljs-property">x</span>, <span class="hljs-number">1</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:vm'</span>);
12461cb0ef41Sopenharmony_ci(<span class="hljs-keyword">async</span> () => {
12471cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> m = <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_">SyntheticModule</span>([<span class="hljs-string">'x'</span>], <span class="hljs-function">() =></span> {
12481cb0ef41Sopenharmony_ci    m.<span class="hljs-title function_">setExport</span>(<span class="hljs-string">'x'</span>, <span class="hljs-number">1</span>);
12491cb0ef41Sopenharmony_ci  });
12501cb0ef41Sopenharmony_ci  <span class="hljs-keyword">await</span> m.<span class="hljs-title function_">link</span>(<span class="hljs-function">() =></span> {});
12511cb0ef41Sopenharmony_ci  <span class="hljs-keyword">await</span> m.evaluate();
12521cb0ef41Sopenharmony_ci  assert.<span class="hljs-title function_">strictEqual</span>(m.<span class="hljs-property">namespace</span>.<span class="hljs-property">x</span>, <span class="hljs-number">1</span>);
12531cb0ef41Sopenharmony_ci})();</code><button class="copy-button">copy</button></pre>
12541cb0ef41Sopenharmony_ci</section><section><h3><code>vm.compileFunction(code[, params[, options]])</code><span><a class="mark" href="#vmcompilefunctioncode-params-options" id="vmcompilefunctioncode-params-options">#</a></span><a aria-hidden="true" class="legacy" id="vm_vm_compilefunction_code_params_options"></a></h3>
12551cb0ef41Sopenharmony_ci<div class="api_metadata">
12561cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
12571cb0ef41Sopenharmony_ci<table>
12581cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
12591cb0ef41Sopenharmony_ci<tr><td>v18.15.0</td>
12601cb0ef41Sopenharmony_ci<td><p>The return value now includes <code>cachedDataRejected</code> with the same semantics as the <code>vm.Script</code> version if the <code>cachedData</code> option was passed.</p></td></tr>
12611cb0ef41Sopenharmony_ci<tr><td>v17.0.0, v16.12.0</td>
12621cb0ef41Sopenharmony_ci<td><p>Added support for import attributes to the <code>importModuleDynamically</code> parameter.</p></td></tr>
12631cb0ef41Sopenharmony_ci<tr><td>v15.9.0</td>
12641cb0ef41Sopenharmony_ci<td><p>Added <code>importModuleDynamically</code> option again.</p></td></tr>
12651cb0ef41Sopenharmony_ci<tr><td>v14.3.0</td>
12661cb0ef41Sopenharmony_ci<td><p>Removal of <code>importModuleDynamically</code> due to compatibility issues.</p></td></tr>
12671cb0ef41Sopenharmony_ci<tr><td>v14.1.0, v13.14.0</td>
12681cb0ef41Sopenharmony_ci<td><p>The <code>importModuleDynamically</code> option is now supported.</p></td></tr>
12691cb0ef41Sopenharmony_ci<tr><td>v10.10.0</td>
12701cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.10.0</span></p></td></tr>
12711cb0ef41Sopenharmony_ci</tbody></table>
12721cb0ef41Sopenharmony_ci</details>
12731cb0ef41Sopenharmony_ci</div>
12741cb0ef41Sopenharmony_ci<ul>
12751cb0ef41Sopenharmony_ci<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The body of the function to compile.</li>
12761cb0ef41Sopenharmony_ci<li><code>params</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> An array of strings containing all parameters for the
12771cb0ef41Sopenharmony_cifunction.</li>
12781cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
12791cb0ef41Sopenharmony_ci<ul>
12801cb0ef41Sopenharmony_ci<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Specifies the filename used in stack traces produced
12811cb0ef41Sopenharmony_ciby this script. <strong>Default:</strong> <code>''</code>.</li>
12821cb0ef41Sopenharmony_ci<li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the line number offset that is displayed
12831cb0ef41Sopenharmony_ciin stack traces produced by this script. <strong>Default:</strong> <code>0</code>.</li>
12841cb0ef41Sopenharmony_ci<li><code>columnOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the first-line column number offset that
12851cb0ef41Sopenharmony_ciis displayed in stack traces produced by this script. <strong>Default:</strong> <code>0</code>.</li>
12861cb0ef41Sopenharmony_ci<li><code>cachedData</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> Provides an optional <code>Buffer</code> or
12871cb0ef41Sopenharmony_ci<code>TypedArray</code>, or <code>DataView</code> with V8's code cache data for the supplied
12881cb0ef41Sopenharmony_cisource. This must be produced by a prior call to <a href="#vmcompilefunctioncode-params-options"><code>vm.compileFunction()</code></a>
12891cb0ef41Sopenharmony_ciwith the same <code>code</code> and <code>params</code>.</li>
12901cb0ef41Sopenharmony_ci<li><code>produceCachedData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Specifies whether to produce new cache data.
12911cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li>
12921cb0ef41Sopenharmony_ci<li><code>parsingContext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> The <a href="#what-does-it-mean-to-contextify-an-object">contextified</a> object in which the said
12931cb0ef41Sopenharmony_cifunction should be compiled in.</li>
12941cb0ef41Sopenharmony_ci<li><code>contextExtensions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object[]></a> An array containing a collection of context
12951cb0ef41Sopenharmony_ciextensions (objects wrapping the current scope) to be applied while
12961cb0ef41Sopenharmony_cicompiling. <strong>Default:</strong> <code>[]</code>.</li>
12971cb0ef41Sopenharmony_ci<li><code>importModuleDynamically</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Called during evaluation of this module
12981cb0ef41Sopenharmony_ciwhen <code>import()</code> is called. If this option is not specified, calls to
12991cb0ef41Sopenharmony_ci<code>import()</code> will reject with <a href="errors.html#err_vm_dynamic_import_callback_missing"><code>ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING</code></a>.
13001cb0ef41Sopenharmony_ciThis option is part of the experimental modules API, and should not be
13011cb0ef41Sopenharmony_ciconsidered stable.  If <code>--experimental-vm-modules</code> isn't
13021cb0ef41Sopenharmony_ciset, this callback will be ignored and calls to <code>import()</code> will reject with
13031cb0ef41Sopenharmony_ci<a href="errors.html#err_vm_dynamic_import_callback_missing_flag"><code>ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG</code></a>.
13041cb0ef41Sopenharmony_ci<ul>
13051cb0ef41Sopenharmony_ci<li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> specifier passed to <code>import()</code></li>
13061cb0ef41Sopenharmony_ci<li><code>function</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
13071cb0ef41Sopenharmony_ci<li><code>importAttributes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> The <code>"with"</code> value passed to the
13081cb0ef41Sopenharmony_ci<a href="https://tc39.es/proposal-import-attributes/#sec-evaluate-import-call"><code>optionsExpression</code></a> optional parameter, or an empty object if no value
13091cb0ef41Sopenharmony_ciwas provided.</li>
13101cb0ef41Sopenharmony_ci<li>Returns: <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects" class="type">&#x3C;Module Namespace Object></a> | <a href="vm.html#class-vmmodule" class="type">&#x3C;vm.Module></a> Returning a <code>vm.Module</code> is
13111cb0ef41Sopenharmony_cirecommended in order to take advantage of error tracking, and to avoid
13121cb0ef41Sopenharmony_ciissues with namespaces that contain <code>then</code> function exports.</li>
13131cb0ef41Sopenharmony_ci</ul>
13141cb0ef41Sopenharmony_ci</li>
13151cb0ef41Sopenharmony_ci</ul>
13161cb0ef41Sopenharmony_ci</li>
13171cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
13181cb0ef41Sopenharmony_ci</ul>
13191cb0ef41Sopenharmony_ci<p>Compiles the given code into the provided context (if no context is
13201cb0ef41Sopenharmony_cisupplied, the current context is used), and returns it wrapped inside a
13211cb0ef41Sopenharmony_cifunction with the given <code>params</code>.</p>
13221cb0ef41Sopenharmony_ci</section><section><h3><code>vm.createContext([contextObject[, options]])</code><span><a class="mark" href="#vmcreatecontextcontextobject-options" id="vmcreatecontextcontextobject-options">#</a></span><a aria-hidden="true" class="legacy" id="vm_vm_createcontext_contextobject_options"></a></h3>
13231cb0ef41Sopenharmony_ci<div class="api_metadata">
13241cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
13251cb0ef41Sopenharmony_ci<table>
13261cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
13271cb0ef41Sopenharmony_ci<tr><td>v14.6.0</td>
13281cb0ef41Sopenharmony_ci<td><p>The <code>microtaskMode</code> option is supported now.</p></td></tr>
13291cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
13301cb0ef41Sopenharmony_ci<td><p>The first argument can no longer be a function.</p></td></tr>
13311cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
13321cb0ef41Sopenharmony_ci<td><p>The <code>codeGeneration</code> option is supported now.</p></td></tr>
13331cb0ef41Sopenharmony_ci<tr><td>v0.3.1</td>
13341cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.3.1</span></p></td></tr>
13351cb0ef41Sopenharmony_ci</tbody></table>
13361cb0ef41Sopenharmony_ci</details>
13371cb0ef41Sopenharmony_ci</div>
13381cb0ef41Sopenharmony_ci<ul>
13391cb0ef41Sopenharmony_ci<li><code>contextObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
13401cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
13411cb0ef41Sopenharmony_ci<ul>
13421cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Human-readable name of the newly created context.
13431cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'VM Context i'</code>, where <code>i</code> is an ascending numerical index of
13441cb0ef41Sopenharmony_cithe created context.</li>
13451cb0ef41Sopenharmony_ci<li><code>origin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <a href="https://developer.mozilla.org/en-US/docs/Glossary/Origin">Origin</a> corresponding to the newly created
13461cb0ef41Sopenharmony_cicontext for display purposes. The origin should be formatted like a URL,
13471cb0ef41Sopenharmony_cibut with only the scheme, host, and port (if necessary), like the value of
13481cb0ef41Sopenharmony_cithe <a href="url.html#urlorigin"><code>url.origin</code></a> property of a <a href="url.html#class-url"><code>URL</code></a> object. Most notably, this
13491cb0ef41Sopenharmony_cistring should omit the trailing slash, as that denotes a path.
13501cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>''</code>.</li>
13511cb0ef41Sopenharmony_ci<li><code>codeGeneration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
13521cb0ef41Sopenharmony_ci<ul>
13531cb0ef41Sopenharmony_ci<li><code>strings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If set to false any calls to <code>eval</code> or function
13541cb0ef41Sopenharmony_ciconstructors (<code>Function</code>, <code>GeneratorFunction</code>, etc) will throw an
13551cb0ef41Sopenharmony_ci<code>EvalError</code>. <strong>Default:</strong> <code>true</code>.</li>
13561cb0ef41Sopenharmony_ci<li><code>wasm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If set to false any attempt to compile a WebAssembly
13571cb0ef41Sopenharmony_cimodule will throw a <code>WebAssembly.CompileError</code>. <strong>Default:</strong> <code>true</code>.</li>
13581cb0ef41Sopenharmony_ci</ul>
13591cb0ef41Sopenharmony_ci</li>
13601cb0ef41Sopenharmony_ci<li><code>microtaskMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> If set to <code>afterEvaluate</code>, microtasks (tasks
13611cb0ef41Sopenharmony_cischeduled through <code>Promise</code>s and <code>async function</code>s) will be run immediately
13621cb0ef41Sopenharmony_ciafter a script has run through <a href="#scriptrunincontextcontextifiedobject-options"><code>script.runInContext()</code></a>.
13631cb0ef41Sopenharmony_ciThey are included in the <code>timeout</code> and <code>breakOnSigint</code> scopes in that case.</li>
13641cb0ef41Sopenharmony_ci</ul>
13651cb0ef41Sopenharmony_ci</li>
13661cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> contextified object.</li>
13671cb0ef41Sopenharmony_ci</ul>
13681cb0ef41Sopenharmony_ci<p>If given a <code>contextObject</code>, the <code>vm.createContext()</code> method will <a href="#what-does-it-mean-to-contextify-an-object">prepare
13691cb0ef41Sopenharmony_cithat object</a> so that it can be used in calls to
13701cb0ef41Sopenharmony_ci<a href="#vmrunincontextcode-contextifiedobject-options"><code>vm.runInContext()</code></a> or <a href="#scriptrunincontextcontextifiedobject-options"><code>script.runInContext()</code></a>. Inside such scripts,
13711cb0ef41Sopenharmony_cithe <code>contextObject</code> will be the global object, retaining all of its existing
13721cb0ef41Sopenharmony_ciproperties but also having the built-in objects and functions any standard
13731cb0ef41Sopenharmony_ci<a href="https://es5.github.io/#x15.1">global object</a> has. Outside of scripts run by the vm module, global variables
13741cb0ef41Sopenharmony_ciwill remain unchanged.</p>
13751cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:vm'</span>);
13761cb0ef41Sopenharmony_ci
13771cb0ef41Sopenharmony_ci<span class="hljs-variable language_">global</span>.<span class="hljs-property">globalVar</span> = <span class="hljs-number">3</span>;
13781cb0ef41Sopenharmony_ci
13791cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> context = { <span class="hljs-attr">globalVar</span>: <span class="hljs-number">1</span> };
13801cb0ef41Sopenharmony_civm.<span class="hljs-title function_">createContext</span>(context);
13811cb0ef41Sopenharmony_ci
13821cb0ef41Sopenharmony_civm.<span class="hljs-title function_">runInContext</span>(<span class="hljs-string">'globalVar *= 2;'</span>, context);
13831cb0ef41Sopenharmony_ci
13841cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(context);
13851cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: { globalVar: 2 }</span>
13861cb0ef41Sopenharmony_ci
13871cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable language_">global</span>.<span class="hljs-property">globalVar</span>);
13881cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 3</span></code> <button class="copy-button">copy</button></pre>
13891cb0ef41Sopenharmony_ci<p>If <code>contextObject</code> is omitted (or passed explicitly as <code>undefined</code>), a new,
13901cb0ef41Sopenharmony_ciempty <a href="#what-does-it-mean-to-contextify-an-object">contextified</a> object will be returned.</p>
13911cb0ef41Sopenharmony_ci<p>The <code>vm.createContext()</code> method is primarily useful for creating a single
13921cb0ef41Sopenharmony_cicontext that can be used to run multiple scripts. For instance, if emulating a
13931cb0ef41Sopenharmony_ciweb browser, the method can be used to create a single context representing a
13941cb0ef41Sopenharmony_ciwindow's global object, then run all <code>&#x3C;script></code> tags together within that
13951cb0ef41Sopenharmony_cicontext.</p>
13961cb0ef41Sopenharmony_ci<p>The provided <code>name</code> and <code>origin</code> of the context are made visible through the
13971cb0ef41Sopenharmony_ciInspector API.</p>
13981cb0ef41Sopenharmony_ci</section><section><h3><code>vm.isContext(object)</code><span><a class="mark" href="#vmiscontextobject" id="vmiscontextobject">#</a></span><a aria-hidden="true" class="legacy" id="vm_vm_iscontext_object"></a></h3>
13991cb0ef41Sopenharmony_ci<div class="api_metadata">
14001cb0ef41Sopenharmony_ci<span>Added in: v0.11.7</span>
14011cb0ef41Sopenharmony_ci</div>
14021cb0ef41Sopenharmony_ci<ul>
14031cb0ef41Sopenharmony_ci<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
14041cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
14051cb0ef41Sopenharmony_ci</ul>
14061cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the given <code>object</code> object has been <a href="#what-does-it-mean-to-contextify-an-object">contextified</a> using
14071cb0ef41Sopenharmony_ci<a href="#vmcreatecontextcontextobject-options"><code>vm.createContext()</code></a>.</p>
14081cb0ef41Sopenharmony_ci</section><section><h3><code>vm.measureMemory([options])</code><span><a class="mark" href="#vmmeasurememoryoptions" id="vmmeasurememoryoptions">#</a></span><a aria-hidden="true" class="legacy" id="vm_vm_measurememory_options"></a></h3>
14091cb0ef41Sopenharmony_ci<div class="api_metadata">
14101cb0ef41Sopenharmony_ci<span>Added in: v13.10.0</span>
14111cb0ef41Sopenharmony_ci</div>
14121cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
14131cb0ef41Sopenharmony_ci<p>Measure the memory known to V8 and used by all contexts known to the
14141cb0ef41Sopenharmony_cicurrent V8 isolate, or the main context.</p>
14151cb0ef41Sopenharmony_ci<ul>
14161cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> Optional.
14171cb0ef41Sopenharmony_ci<ul>
14181cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Either <code>'summary'</code> or <code>'detailed'</code>. In summary mode,
14191cb0ef41Sopenharmony_cionly the memory measured for the main context will be returned. In
14201cb0ef41Sopenharmony_cidetailed mode, the memory measured for all contexts known to the
14211cb0ef41Sopenharmony_cicurrent V8 isolate will be returned.
14221cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'summary'</code></li>
14231cb0ef41Sopenharmony_ci<li><code>execution</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Either <code>'default'</code> or <code>'eager'</code>. With default
14241cb0ef41Sopenharmony_ciexecution, the promise will not resolve until after the next scheduled
14251cb0ef41Sopenharmony_cigarbage collection starts, which may take a while (or never if the program
14261cb0ef41Sopenharmony_ciexits before the next GC). With eager execution, the GC will be started
14271cb0ef41Sopenharmony_ciright away to measure the memory.
14281cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'default'</code></li>
14291cb0ef41Sopenharmony_ci</ul>
14301cb0ef41Sopenharmony_ci</li>
14311cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> If the memory is successfully measured, the promise will
14321cb0ef41Sopenharmony_ciresolve with an object containing information about the memory usage.
14331cb0ef41Sopenharmony_ciOtherwise it will be rejected with an <code>ERR_CONTEXT_NOT_INITIALIZED</code> error.</li>
14341cb0ef41Sopenharmony_ci</ul>
14351cb0ef41Sopenharmony_ci<p>The format of the object that the returned Promise may resolve with is
14361cb0ef41Sopenharmony_cispecific to the V8 engine and may change from one version of V8 to the next.</p>
14371cb0ef41Sopenharmony_ci<p>The returned result is different from the statistics returned by
14381cb0ef41Sopenharmony_ci<code>v8.getHeapSpaceStatistics()</code> in that <code>vm.measureMemory()</code> measure the
14391cb0ef41Sopenharmony_cimemory reachable by each V8 specific contexts in the current instance of
14401cb0ef41Sopenharmony_cithe V8 engine, while the result of <code>v8.getHeapSpaceStatistics()</code> measure
14411cb0ef41Sopenharmony_cithe memory occupied by each heap space in the current V8 instance.</p>
14421cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:vm'</span>);
14431cb0ef41Sopenharmony_ci<span class="hljs-comment">// Measure the memory used by the main context.</span>
14441cb0ef41Sopenharmony_civm.<span class="hljs-title function_">measureMemory</span>({ <span class="hljs-attr">mode</span>: <span class="hljs-string">'summary'</span> })
14451cb0ef41Sopenharmony_ci  <span class="hljs-comment">// This is the same as vm.measureMemory()</span>
14461cb0ef41Sopenharmony_ci  .<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">result</span>) =></span> {
14471cb0ef41Sopenharmony_ci    <span class="hljs-comment">// The current format is:</span>
14481cb0ef41Sopenharmony_ci    <span class="hljs-comment">// {</span>
14491cb0ef41Sopenharmony_ci    <span class="hljs-comment">//   total: {</span>
14501cb0ef41Sopenharmony_ci    <span class="hljs-comment">//      jsMemoryEstimate: 2418479, jsMemoryRange: [ 2418479, 2745799 ]</span>
14511cb0ef41Sopenharmony_ci    <span class="hljs-comment">//    }</span>
14521cb0ef41Sopenharmony_ci    <span class="hljs-comment">// }</span>
14531cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(result);
14541cb0ef41Sopenharmony_ci  });
14551cb0ef41Sopenharmony_ci
14561cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> context = vm.<span class="hljs-title function_">createContext</span>({ <span class="hljs-attr">a</span>: <span class="hljs-number">1</span> });
14571cb0ef41Sopenharmony_civm.<span class="hljs-title function_">measureMemory</span>({ <span class="hljs-attr">mode</span>: <span class="hljs-string">'detailed'</span>, <span class="hljs-attr">execution</span>: <span class="hljs-string">'eager'</span> })
14581cb0ef41Sopenharmony_ci  .<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">result</span>) =></span> {
14591cb0ef41Sopenharmony_ci    <span class="hljs-comment">// Reference the context here so that it won't be GC'ed</span>
14601cb0ef41Sopenharmony_ci    <span class="hljs-comment">// until the measurement is complete.</span>
14611cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(context.<span class="hljs-property">a</span>);
14621cb0ef41Sopenharmony_ci    <span class="hljs-comment">// {</span>
14631cb0ef41Sopenharmony_ci    <span class="hljs-comment">//   total: {</span>
14641cb0ef41Sopenharmony_ci    <span class="hljs-comment">//     jsMemoryEstimate: 2574732,</span>
14651cb0ef41Sopenharmony_ci    <span class="hljs-comment">//     jsMemoryRange: [ 2574732, 2904372 ]</span>
14661cb0ef41Sopenharmony_ci    <span class="hljs-comment">//   },</span>
14671cb0ef41Sopenharmony_ci    <span class="hljs-comment">//   current: {</span>
14681cb0ef41Sopenharmony_ci    <span class="hljs-comment">//     jsMemoryEstimate: 2438996,</span>
14691cb0ef41Sopenharmony_ci    <span class="hljs-comment">//     jsMemoryRange: [ 2438996, 2768636 ]</span>
14701cb0ef41Sopenharmony_ci    <span class="hljs-comment">//   },</span>
14711cb0ef41Sopenharmony_ci    <span class="hljs-comment">//   other: [</span>
14721cb0ef41Sopenharmony_ci    <span class="hljs-comment">//     {</span>
14731cb0ef41Sopenharmony_ci    <span class="hljs-comment">//       jsMemoryEstimate: 135736,</span>
14741cb0ef41Sopenharmony_ci    <span class="hljs-comment">//       jsMemoryRange: [ 135736, 465376 ]</span>
14751cb0ef41Sopenharmony_ci    <span class="hljs-comment">//     }</span>
14761cb0ef41Sopenharmony_ci    <span class="hljs-comment">//   ]</span>
14771cb0ef41Sopenharmony_ci    <span class="hljs-comment">// }</span>
14781cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(result);
14791cb0ef41Sopenharmony_ci  });</code> <button class="copy-button">copy</button></pre>
14801cb0ef41Sopenharmony_ci</section><section><h3><code>vm.runInContext(code, contextifiedObject[, options])</code><span><a class="mark" href="#vmrunincontextcode-contextifiedobject-options" id="vmrunincontextcode-contextifiedobject-options">#</a></span><a aria-hidden="true" class="legacy" id="vm_vm_runincontext_code_contextifiedobject_options"></a></h3>
14811cb0ef41Sopenharmony_ci<div class="api_metadata">
14821cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
14831cb0ef41Sopenharmony_ci<table>
14841cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
14851cb0ef41Sopenharmony_ci<tr><td>v17.0.0, v16.12.0</td>
14861cb0ef41Sopenharmony_ci<td><p>Added support for import attributes to the <code>importModuleDynamically</code> parameter.</p></td></tr>
14871cb0ef41Sopenharmony_ci<tr><td>v6.3.0</td>
14881cb0ef41Sopenharmony_ci<td><p>The <code>breakOnSigint</code> option is supported now.</p></td></tr>
14891cb0ef41Sopenharmony_ci<tr><td>v0.3.1</td>
14901cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.3.1</span></p></td></tr>
14911cb0ef41Sopenharmony_ci</tbody></table>
14921cb0ef41Sopenharmony_ci</details>
14931cb0ef41Sopenharmony_ci</div>
14941cb0ef41Sopenharmony_ci<ul>
14951cb0ef41Sopenharmony_ci<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The JavaScript code to compile and run.</li>
14961cb0ef41Sopenharmony_ci<li><code>contextifiedObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> The <a href="#what-does-it-mean-to-contextify-an-object">contextified</a> object that will be used
14971cb0ef41Sopenharmony_cias the <code>global</code> when the <code>code</code> is compiled and run.</li>
14981cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a>
14991cb0ef41Sopenharmony_ci<ul>
15001cb0ef41Sopenharmony_ci<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Specifies the filename used in stack traces produced
15011cb0ef41Sopenharmony_ciby this script. <strong>Default:</strong> <code>'evalmachine.&#x3C;anonymous>'</code>.</li>
15021cb0ef41Sopenharmony_ci<li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the line number offset that is displayed
15031cb0ef41Sopenharmony_ciin stack traces produced by this script. <strong>Default:</strong> <code>0</code>.</li>
15041cb0ef41Sopenharmony_ci<li><code>columnOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the first-line column number offset that
15051cb0ef41Sopenharmony_ciis displayed in stack traces produced by this script. <strong>Default:</strong> <code>0</code>.</li>
15061cb0ef41Sopenharmony_ci<li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, if an <a href="errors.html#class-error"><code>Error</code></a> occurs
15071cb0ef41Sopenharmony_ciwhile compiling the <code>code</code>, the line of code causing the error is attached
15081cb0ef41Sopenharmony_cito the stack trace. <strong>Default:</strong> <code>true</code>.</li>
15091cb0ef41Sopenharmony_ci<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Specifies the number of milliseconds to execute <code>code</code>
15101cb0ef41Sopenharmony_cibefore terminating execution. If execution is terminated, an <a href="errors.html#class-error"><code>Error</code></a>
15111cb0ef41Sopenharmony_ciwill be thrown. This value must be a strictly positive integer.</li>
15121cb0ef41Sopenharmony_ci<li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, receiving <code>SIGINT</code>
15131cb0ef41Sopenharmony_ci(<kbd>Ctrl</kbd>+<kbd>C</kbd>) will terminate execution and throw an
15141cb0ef41Sopenharmony_ci<a href="errors.html#class-error"><code>Error</code></a>. Existing handlers for the event that have been attached via
15151cb0ef41Sopenharmony_ci<code>process.on('SIGINT')</code> are disabled during script execution, but continue to
15161cb0ef41Sopenharmony_ciwork after that. <strong>Default:</strong> <code>false</code>.</li>
15171cb0ef41Sopenharmony_ci<li><code>cachedData</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> Provides an optional <code>Buffer</code> or
15181cb0ef41Sopenharmony_ci<code>TypedArray</code>, or <code>DataView</code> with V8's code cache data for the supplied
15191cb0ef41Sopenharmony_cisource.</li>
15201cb0ef41Sopenharmony_ci<li><code>importModuleDynamically</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Called during evaluation of this module
15211cb0ef41Sopenharmony_ciwhen <code>import()</code> is called. If this option is not specified, calls to
15221cb0ef41Sopenharmony_ci<code>import()</code> will reject with <a href="errors.html#err_vm_dynamic_import_callback_missing"><code>ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING</code></a>.
15231cb0ef41Sopenharmony_ciThis option is part of the experimental modules API. We do not recommend
15241cb0ef41Sopenharmony_ciusing it in a production environment.  If <code>--experimental-vm-modules</code> isn't
15251cb0ef41Sopenharmony_ciset, this callback will be ignored and calls to <code>import()</code> will reject with
15261cb0ef41Sopenharmony_ci<a href="errors.html#err_vm_dynamic_import_callback_missing_flag"><code>ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG</code></a>.
15271cb0ef41Sopenharmony_ci<ul>
15281cb0ef41Sopenharmony_ci<li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> specifier passed to <code>import()</code></li>
15291cb0ef41Sopenharmony_ci<li><code>script</code> <a href="vm.html#class-vmscript" class="type">&#x3C;vm.Script></a></li>
15301cb0ef41Sopenharmony_ci<li><code>importAttributes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> The <code>"with"</code> value passed to the
15311cb0ef41Sopenharmony_ci<a href="https://tc39.es/proposal-import-attributes/#sec-evaluate-import-call"><code>optionsExpression</code></a> optional parameter, or an empty object if no value
15321cb0ef41Sopenharmony_ciwas provided.</li>
15331cb0ef41Sopenharmony_ci<li>Returns: <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects" class="type">&#x3C;Module Namespace Object></a> | <a href="vm.html#class-vmmodule" class="type">&#x3C;vm.Module></a> Returning a <code>vm.Module</code> is
15341cb0ef41Sopenharmony_cirecommended in order to take advantage of error tracking, and to avoid
15351cb0ef41Sopenharmony_ciissues with namespaces that contain <code>then</code> function exports.</li>
15361cb0ef41Sopenharmony_ci</ul>
15371cb0ef41Sopenharmony_ci</li>
15381cb0ef41Sopenharmony_ci</ul>
15391cb0ef41Sopenharmony_ci</li>
15401cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> the result of the very last statement executed in the script.</li>
15411cb0ef41Sopenharmony_ci</ul>
15421cb0ef41Sopenharmony_ci<p>The <code>vm.runInContext()</code> method compiles <code>code</code>, runs it within the context of
15431cb0ef41Sopenharmony_cithe <code>contextifiedObject</code>, then returns the result. Running code does not have
15441cb0ef41Sopenharmony_ciaccess to the local scope. The <code>contextifiedObject</code> object <em>must</em> have been
15451cb0ef41Sopenharmony_cipreviously <a href="#what-does-it-mean-to-contextify-an-object">contextified</a> using the <a href="#vmcreatecontextcontextobject-options"><code>vm.createContext()</code></a> method.</p>
15461cb0ef41Sopenharmony_ci<p>If <code>options</code> is a string, then it specifies the filename.</p>
15471cb0ef41Sopenharmony_ci<p>The following example compiles and executes different scripts using a single
15481cb0ef41Sopenharmony_ci<a href="#what-does-it-mean-to-contextify-an-object">contextified</a> object:</p>
15491cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:vm'</span>);
15501cb0ef41Sopenharmony_ci
15511cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> contextObject = { <span class="hljs-attr">globalVar</span>: <span class="hljs-number">1</span> };
15521cb0ef41Sopenharmony_civm.<span class="hljs-title function_">createContext</span>(contextObject);
15531cb0ef41Sopenharmony_ci
15541cb0ef41Sopenharmony_ci<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &#x3C; <span class="hljs-number">10</span>; ++i) {
15551cb0ef41Sopenharmony_ci  vm.<span class="hljs-title function_">runInContext</span>(<span class="hljs-string">'globalVar *= 2;'</span>, contextObject);
15561cb0ef41Sopenharmony_ci}
15571cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(contextObject);
15581cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: { globalVar: 1024 }</span></code> <button class="copy-button">copy</button></pre>
15591cb0ef41Sopenharmony_ci</section><section><h3><code>vm.runInNewContext(code[, contextObject[, options]])</code><span><a class="mark" href="#vmruninnewcontextcode-contextobject-options" id="vmruninnewcontextcode-contextobject-options">#</a></span><a aria-hidden="true" class="legacy" id="vm_vm_runinnewcontext_code_contextobject_options"></a></h3>
15601cb0ef41Sopenharmony_ci<div class="api_metadata">
15611cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
15621cb0ef41Sopenharmony_ci<table>
15631cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
15641cb0ef41Sopenharmony_ci<tr><td>v17.0.0, v16.12.0</td>
15651cb0ef41Sopenharmony_ci<td><p>Added support for import attributes to the <code>importModuleDynamically</code> parameter.</p></td></tr>
15661cb0ef41Sopenharmony_ci<tr><td>v14.6.0</td>
15671cb0ef41Sopenharmony_ci<td><p>The <code>microtaskMode</code> option is supported now.</p></td></tr>
15681cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
15691cb0ef41Sopenharmony_ci<td><p>The <code>contextCodeGeneration</code> option is supported now.</p></td></tr>
15701cb0ef41Sopenharmony_ci<tr><td>v6.3.0</td>
15711cb0ef41Sopenharmony_ci<td><p>The <code>breakOnSigint</code> option is supported now.</p></td></tr>
15721cb0ef41Sopenharmony_ci<tr><td>v0.3.1</td>
15731cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.3.1</span></p></td></tr>
15741cb0ef41Sopenharmony_ci</tbody></table>
15751cb0ef41Sopenharmony_ci</details>
15761cb0ef41Sopenharmony_ci</div>
15771cb0ef41Sopenharmony_ci<ul>
15781cb0ef41Sopenharmony_ci<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The JavaScript code to compile and run.</li>
15791cb0ef41Sopenharmony_ci<li><code>contextObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> An object that will be <a href="#what-does-it-mean-to-contextify-an-object">contextified</a>. If
15801cb0ef41Sopenharmony_ci<code>undefined</code>, a new object will be created.</li>
15811cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a>
15821cb0ef41Sopenharmony_ci<ul>
15831cb0ef41Sopenharmony_ci<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Specifies the filename used in stack traces produced
15841cb0ef41Sopenharmony_ciby this script. <strong>Default:</strong> <code>'evalmachine.&#x3C;anonymous>'</code>.</li>
15851cb0ef41Sopenharmony_ci<li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the line number offset that is displayed
15861cb0ef41Sopenharmony_ciin stack traces produced by this script. <strong>Default:</strong> <code>0</code>.</li>
15871cb0ef41Sopenharmony_ci<li><code>columnOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the first-line column number offset that
15881cb0ef41Sopenharmony_ciis displayed in stack traces produced by this script. <strong>Default:</strong> <code>0</code>.</li>
15891cb0ef41Sopenharmony_ci<li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, if an <a href="errors.html#class-error"><code>Error</code></a> occurs
15901cb0ef41Sopenharmony_ciwhile compiling the <code>code</code>, the line of code causing the error is attached
15911cb0ef41Sopenharmony_cito the stack trace. <strong>Default:</strong> <code>true</code>.</li>
15921cb0ef41Sopenharmony_ci<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Specifies the number of milliseconds to execute <code>code</code>
15931cb0ef41Sopenharmony_cibefore terminating execution. If execution is terminated, an <a href="errors.html#class-error"><code>Error</code></a>
15941cb0ef41Sopenharmony_ciwill be thrown. This value must be a strictly positive integer.</li>
15951cb0ef41Sopenharmony_ci<li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, receiving <code>SIGINT</code>
15961cb0ef41Sopenharmony_ci(<kbd>Ctrl</kbd>+<kbd>C</kbd>) will terminate execution and throw an
15971cb0ef41Sopenharmony_ci<a href="errors.html#class-error"><code>Error</code></a>. Existing handlers for the event that have been attached via
15981cb0ef41Sopenharmony_ci<code>process.on('SIGINT')</code> are disabled during script execution, but continue to
15991cb0ef41Sopenharmony_ciwork after that. <strong>Default:</strong> <code>false</code>.</li>
16001cb0ef41Sopenharmony_ci<li><code>contextName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Human-readable name of the newly created context.
16011cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'VM Context i'</code>, where <code>i</code> is an ascending numerical index of
16021cb0ef41Sopenharmony_cithe created context.</li>
16031cb0ef41Sopenharmony_ci<li><code>contextOrigin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <a href="https://developer.mozilla.org/en-US/docs/Glossary/Origin">Origin</a> corresponding to the newly
16041cb0ef41Sopenharmony_cicreated context for display purposes. The origin should be formatted like a
16051cb0ef41Sopenharmony_ciURL, but with only the scheme, host, and port (if necessary), like the
16061cb0ef41Sopenharmony_civalue of the <a href="url.html#urlorigin"><code>url.origin</code></a> property of a <a href="url.html#class-url"><code>URL</code></a> object. Most notably,
16071cb0ef41Sopenharmony_cithis string should omit the trailing slash, as that denotes a path.
16081cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>''</code>.</li>
16091cb0ef41Sopenharmony_ci<li><code>contextCodeGeneration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
16101cb0ef41Sopenharmony_ci<ul>
16111cb0ef41Sopenharmony_ci<li><code>strings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If set to false any calls to <code>eval</code> or function
16121cb0ef41Sopenharmony_ciconstructors (<code>Function</code>, <code>GeneratorFunction</code>, etc) will throw an
16131cb0ef41Sopenharmony_ci<code>EvalError</code>. <strong>Default:</strong> <code>true</code>.</li>
16141cb0ef41Sopenharmony_ci<li><code>wasm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If set to false any attempt to compile a WebAssembly
16151cb0ef41Sopenharmony_cimodule will throw a <code>WebAssembly.CompileError</code>. <strong>Default:</strong> <code>true</code>.</li>
16161cb0ef41Sopenharmony_ci</ul>
16171cb0ef41Sopenharmony_ci</li>
16181cb0ef41Sopenharmony_ci<li><code>cachedData</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> Provides an optional <code>Buffer</code> or
16191cb0ef41Sopenharmony_ci<code>TypedArray</code>, or <code>DataView</code> with V8's code cache data for the supplied
16201cb0ef41Sopenharmony_cisource.</li>
16211cb0ef41Sopenharmony_ci<li><code>importModuleDynamically</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Called during evaluation of this module
16221cb0ef41Sopenharmony_ciwhen <code>import()</code> is called. If this option is not specified, calls to
16231cb0ef41Sopenharmony_ci<code>import()</code> will reject with <a href="errors.html#err_vm_dynamic_import_callback_missing"><code>ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING</code></a>.
16241cb0ef41Sopenharmony_ciThis option is part of the experimental modules API. We do not recommend
16251cb0ef41Sopenharmony_ciusing it in a production environment. If <code>--experimental-vm-modules</code> isn't
16261cb0ef41Sopenharmony_ciset, this callback will be ignored and calls to <code>import()</code> will reject with
16271cb0ef41Sopenharmony_ci<a href="errors.html#err_vm_dynamic_import_callback_missing_flag"><code>ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG</code></a>.
16281cb0ef41Sopenharmony_ci<ul>
16291cb0ef41Sopenharmony_ci<li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> specifier passed to <code>import()</code></li>
16301cb0ef41Sopenharmony_ci<li><code>script</code> <a href="vm.html#class-vmscript" class="type">&#x3C;vm.Script></a></li>
16311cb0ef41Sopenharmony_ci<li><code>importAttributes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> The <code>"with"</code> value passed to the
16321cb0ef41Sopenharmony_ci<a href="https://tc39.es/proposal-import-attributes/#sec-evaluate-import-call"><code>optionsExpression</code></a> optional parameter, or an empty object if no value
16331cb0ef41Sopenharmony_ciwas provided.</li>
16341cb0ef41Sopenharmony_ci<li>Returns: <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects" class="type">&#x3C;Module Namespace Object></a> | <a href="vm.html#class-vmmodule" class="type">&#x3C;vm.Module></a> Returning a <code>vm.Module</code> is
16351cb0ef41Sopenharmony_cirecommended in order to take advantage of error tracking, and to avoid
16361cb0ef41Sopenharmony_ciissues with namespaces that contain <code>then</code> function exports.</li>
16371cb0ef41Sopenharmony_ci</ul>
16381cb0ef41Sopenharmony_ci</li>
16391cb0ef41Sopenharmony_ci<li><code>microtaskMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> If set to <code>afterEvaluate</code>, microtasks (tasks
16401cb0ef41Sopenharmony_cischeduled through <code>Promise</code>s and <code>async function</code>s) will be run immediately
16411cb0ef41Sopenharmony_ciafter the script has run. They are included in the <code>timeout</code> and
16421cb0ef41Sopenharmony_ci<code>breakOnSigint</code> scopes in that case.</li>
16431cb0ef41Sopenharmony_ci</ul>
16441cb0ef41Sopenharmony_ci</li>
16451cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> the result of the very last statement executed in the script.</li>
16461cb0ef41Sopenharmony_ci</ul>
16471cb0ef41Sopenharmony_ci<p>The <code>vm.runInNewContext()</code> first contextifies the given <code>contextObject</code> (or
16481cb0ef41Sopenharmony_cicreates a new <code>contextObject</code> if passed as <code>undefined</code>), compiles the <code>code</code>,
16491cb0ef41Sopenharmony_ciruns it within the created context, then returns the result. Running code
16501cb0ef41Sopenharmony_cidoes not have access to the local scope.</p>
16511cb0ef41Sopenharmony_ci<p>If <code>options</code> is a string, then it specifies the filename.</p>
16521cb0ef41Sopenharmony_ci<p>The following example compiles and executes code that increments a global
16531cb0ef41Sopenharmony_civariable and sets a new one. These globals are contained in the <code>contextObject</code>.</p>
16541cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:vm'</span>);
16551cb0ef41Sopenharmony_ci
16561cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> contextObject = {
16571cb0ef41Sopenharmony_ci  <span class="hljs-attr">animal</span>: <span class="hljs-string">'cat'</span>,
16581cb0ef41Sopenharmony_ci  <span class="hljs-attr">count</span>: <span class="hljs-number">2</span>,
16591cb0ef41Sopenharmony_ci};
16601cb0ef41Sopenharmony_ci
16611cb0ef41Sopenharmony_civm.<span class="hljs-title function_">runInNewContext</span>(<span class="hljs-string">'count += 1; name = "kitty"'</span>, contextObject);
16621cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(contextObject);
16631cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: { animal: 'cat', count: 3, name: 'kitty' }</span></code> <button class="copy-button">copy</button></pre>
16641cb0ef41Sopenharmony_ci</section><section><h3><code>vm.runInThisContext(code[, options])</code><span><a class="mark" href="#vmruninthiscontextcode-options" id="vmruninthiscontextcode-options">#</a></span><a aria-hidden="true" class="legacy" id="vm_vm_runinthiscontext_code_options"></a></h3>
16651cb0ef41Sopenharmony_ci<div class="api_metadata">
16661cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
16671cb0ef41Sopenharmony_ci<table>
16681cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
16691cb0ef41Sopenharmony_ci<tr><td>v17.0.0, v16.12.0</td>
16701cb0ef41Sopenharmony_ci<td><p>Added support for import attributes to the <code>importModuleDynamically</code> parameter.</p></td></tr>
16711cb0ef41Sopenharmony_ci<tr><td>v6.3.0</td>
16721cb0ef41Sopenharmony_ci<td><p>The <code>breakOnSigint</code> option is supported now.</p></td></tr>
16731cb0ef41Sopenharmony_ci<tr><td>v0.3.1</td>
16741cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.3.1</span></p></td></tr>
16751cb0ef41Sopenharmony_ci</tbody></table>
16761cb0ef41Sopenharmony_ci</details>
16771cb0ef41Sopenharmony_ci</div>
16781cb0ef41Sopenharmony_ci<ul>
16791cb0ef41Sopenharmony_ci<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The JavaScript code to compile and run.</li>
16801cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a>
16811cb0ef41Sopenharmony_ci<ul>
16821cb0ef41Sopenharmony_ci<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Specifies the filename used in stack traces produced
16831cb0ef41Sopenharmony_ciby this script. <strong>Default:</strong> <code>'evalmachine.&#x3C;anonymous>'</code>.</li>
16841cb0ef41Sopenharmony_ci<li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the line number offset that is displayed
16851cb0ef41Sopenharmony_ciin stack traces produced by this script. <strong>Default:</strong> <code>0</code>.</li>
16861cb0ef41Sopenharmony_ci<li><code>columnOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the first-line column number offset that
16871cb0ef41Sopenharmony_ciis displayed in stack traces produced by this script. <strong>Default:</strong> <code>0</code>.</li>
16881cb0ef41Sopenharmony_ci<li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, if an <a href="errors.html#class-error"><code>Error</code></a> occurs
16891cb0ef41Sopenharmony_ciwhile compiling the <code>code</code>, the line of code causing the error is attached
16901cb0ef41Sopenharmony_cito the stack trace. <strong>Default:</strong> <code>true</code>.</li>
16911cb0ef41Sopenharmony_ci<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Specifies the number of milliseconds to execute <code>code</code>
16921cb0ef41Sopenharmony_cibefore terminating execution. If execution is terminated, an <a href="errors.html#class-error"><code>Error</code></a>
16931cb0ef41Sopenharmony_ciwill be thrown. This value must be a strictly positive integer.</li>
16941cb0ef41Sopenharmony_ci<li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, receiving <code>SIGINT</code>
16951cb0ef41Sopenharmony_ci(<kbd>Ctrl</kbd>+<kbd>C</kbd>) will terminate execution and throw an
16961cb0ef41Sopenharmony_ci<a href="errors.html#class-error"><code>Error</code></a>. Existing handlers for the event that have been attached via
16971cb0ef41Sopenharmony_ci<code>process.on('SIGINT')</code> are disabled during script execution, but continue to
16981cb0ef41Sopenharmony_ciwork after that. <strong>Default:</strong> <code>false</code>.</li>
16991cb0ef41Sopenharmony_ci<li><code>cachedData</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> Provides an optional <code>Buffer</code> or
17001cb0ef41Sopenharmony_ci<code>TypedArray</code>, or <code>DataView</code> with V8's code cache data for the supplied
17011cb0ef41Sopenharmony_cisource.</li>
17021cb0ef41Sopenharmony_ci<li><code>importModuleDynamically</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Called during evaluation of this module
17031cb0ef41Sopenharmony_ciwhen <code>import()</code> is called. If this option is not specified, calls to
17041cb0ef41Sopenharmony_ci<code>import()</code> will reject with <a href="errors.html#err_vm_dynamic_import_callback_missing"><code>ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING</code></a>.
17051cb0ef41Sopenharmony_ciThis option is part of the experimental modules API. We do not recommend
17061cb0ef41Sopenharmony_ciusing it in a production environment. If <code>--experimental-vm-modules</code> isn't
17071cb0ef41Sopenharmony_ciset, this callback will be ignored and calls to <code>import()</code> will reject with
17081cb0ef41Sopenharmony_ci<a href="errors.html#err_vm_dynamic_import_callback_missing_flag"><code>ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG</code></a>.
17091cb0ef41Sopenharmony_ci<ul>
17101cb0ef41Sopenharmony_ci<li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> specifier passed to <code>import()</code></li>
17111cb0ef41Sopenharmony_ci<li><code>script</code> <a href="vm.html#class-vmscript" class="type">&#x3C;vm.Script></a></li>
17121cb0ef41Sopenharmony_ci<li><code>importAttributes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> The <code>"with"</code> value passed to the
17131cb0ef41Sopenharmony_ci<a href="https://tc39.es/proposal-import-attributes/#sec-evaluate-import-call"><code>optionsExpression</code></a> optional parameter, or an empty object if no value
17141cb0ef41Sopenharmony_ciwas provided.</li>
17151cb0ef41Sopenharmony_ci<li>Returns: <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects" class="type">&#x3C;Module Namespace Object></a> | <a href="vm.html#class-vmmodule" class="type">&#x3C;vm.Module></a> Returning a <code>vm.Module</code> is
17161cb0ef41Sopenharmony_cirecommended in order to take advantage of error tracking, and to avoid
17171cb0ef41Sopenharmony_ciissues with namespaces that contain <code>then</code> function exports.</li>
17181cb0ef41Sopenharmony_ci</ul>
17191cb0ef41Sopenharmony_ci</li>
17201cb0ef41Sopenharmony_ci</ul>
17211cb0ef41Sopenharmony_ci</li>
17221cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> the result of the very last statement executed in the script.</li>
17231cb0ef41Sopenharmony_ci</ul>
17241cb0ef41Sopenharmony_ci<p><code>vm.runInThisContext()</code> compiles <code>code</code>, runs it within the context of the
17251cb0ef41Sopenharmony_cicurrent <code>global</code> and returns the result. Running code does not have access to
17261cb0ef41Sopenharmony_cilocal scope, but does have access to the current <code>global</code> object.</p>
17271cb0ef41Sopenharmony_ci<p>If <code>options</code> is a string, then it specifies the filename.</p>
17281cb0ef41Sopenharmony_ci<p>The following example illustrates using both <code>vm.runInThisContext()</code> and
17291cb0ef41Sopenharmony_cithe JavaScript <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval"><code>eval()</code></a> function to run the same code:</p>
17301cb0ef41Sopenharmony_ci<!-- eslint-disable prefer-const -->
17311cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:vm'</span>);
17321cb0ef41Sopenharmony_ci<span class="hljs-keyword">let</span> localVar = <span class="hljs-string">'initial value'</span>;
17331cb0ef41Sopenharmony_ci
17341cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> vmResult = vm.<span class="hljs-title function_">runInThisContext</span>(<span class="hljs-string">'localVar = "vm";'</span>);
17351cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`vmResult: '<span class="hljs-subst">${vmResult}</span>', localVar: '<span class="hljs-subst">${localVar}</span>'`</span>);
17361cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: vmResult: 'vm', localVar: 'initial value'</span>
17371cb0ef41Sopenharmony_ci
17381cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> evalResult = <span class="hljs-built_in">eval</span>(<span class="hljs-string">'localVar = "eval";'</span>);
17391cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`evalResult: '<span class="hljs-subst">${evalResult}</span>', localVar: '<span class="hljs-subst">${localVar}</span>'`</span>);
17401cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: evalResult: 'eval', localVar: 'eval'</span></code> <button class="copy-button">copy</button></pre>
17411cb0ef41Sopenharmony_ci<p>Because <code>vm.runInThisContext()</code> does not have access to the local scope,
17421cb0ef41Sopenharmony_ci<code>localVar</code> is unchanged. In contrast, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval"><code>eval()</code></a> <em>does</em> have access to the
17431cb0ef41Sopenharmony_cilocal scope, so the value <code>localVar</code> is changed. In this way
17441cb0ef41Sopenharmony_ci<code>vm.runInThisContext()</code> is much like an <a href="https://es5.github.io/#x10.4.2">indirect <code>eval()</code> call</a>, e.g.
17451cb0ef41Sopenharmony_ci<code>(0,eval)('code')</code>.</p>
17461cb0ef41Sopenharmony_ci</section><section><h3>Example: Running an HTTP server within a VM<span><a class="mark" href="#example-running-an-http-server-within-a-vm" id="example-running-an-http-server-within-a-vm">#</a></span><a aria-hidden="true" class="legacy" id="vm_example_running_an_http_server_within_a_vm"></a></h3>
17471cb0ef41Sopenharmony_ci<p>When using either <a href="#scriptruninthiscontextoptions"><code>script.runInThisContext()</code></a> or
17481cb0ef41Sopenharmony_ci<a href="#vmruninthiscontextcode-options"><code>vm.runInThisContext()</code></a>, the code is executed within the current V8 global
17491cb0ef41Sopenharmony_cicontext. The code passed to this VM context will have its own isolated scope.</p>
17501cb0ef41Sopenharmony_ci<p>In order to run a simple web server using the <code>node:http</code> module the code passed
17511cb0ef41Sopenharmony_cito the context must either call <code>require('node:http')</code> on its own, or have a
17521cb0ef41Sopenharmony_cireference to the <code>node:http</code> module passed to it. For instance:</p>
17531cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-meta">'use strict'</span>;
17541cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> vm = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:vm'</span>);
17551cb0ef41Sopenharmony_ci
17561cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> code = <span class="hljs-string">`
17571cb0ef41Sopenharmony_ci((require) => {
17581cb0ef41Sopenharmony_ci  const http = require('node:http');
17591cb0ef41Sopenharmony_ci
17601cb0ef41Sopenharmony_ci  http.createServer((request, response) => {
17611cb0ef41Sopenharmony_ci    response.writeHead(200, { 'Content-Type': 'text/plain' });
17621cb0ef41Sopenharmony_ci    response.end('Hello World\\n');
17631cb0ef41Sopenharmony_ci  }).listen(8124);
17641cb0ef41Sopenharmony_ci
17651cb0ef41Sopenharmony_ci  console.log('Server running at http://127.0.0.1:8124/');
17661cb0ef41Sopenharmony_ci})`</span>;
17671cb0ef41Sopenharmony_ci
17681cb0ef41Sopenharmony_civm.<span class="hljs-title function_">runInThisContext</span>(code)(<span class="hljs-built_in">require</span>);</code> <button class="copy-button">copy</button></pre>
17691cb0ef41Sopenharmony_ci<p>The <code>require()</code> in the above case shares the state with the context it is
17701cb0ef41Sopenharmony_cipassed from. This may introduce risks when untrusted code is executed, e.g.
17711cb0ef41Sopenharmony_cialtering objects in the context in unwanted ways.</p>
17721cb0ef41Sopenharmony_ci</section><section><h3>What does it mean to "contextify" an object?<span><a class="mark" href="#what-does-it-mean-to-contextify-an-object" id="what-does-it-mean-to-contextify-an-object">#</a></span><a aria-hidden="true" class="legacy" id="vm_what_does_it_mean_to_contextify_an_object"></a></h3>
17731cb0ef41Sopenharmony_ci<p>All JavaScript executed within Node.js runs within the scope of a "context".
17741cb0ef41Sopenharmony_ciAccording to the <a href="https://v8.dev/docs/embed#contexts">V8 Embedder's Guide</a>:</p>
17751cb0ef41Sopenharmony_ci<blockquote>
17761cb0ef41Sopenharmony_ci<p>In V8, a context is an execution environment that allows separate, unrelated,
17771cb0ef41Sopenharmony_ciJavaScript applications to run in a single instance of V8. You must explicitly
17781cb0ef41Sopenharmony_cispecify the context in which you want any JavaScript code to be run.</p>
17791cb0ef41Sopenharmony_ci</blockquote>
17801cb0ef41Sopenharmony_ci<p>When the method <code>vm.createContext()</code> is called, the <code>contextObject</code> argument
17811cb0ef41Sopenharmony_ci(or a newly-created object if <code>contextObject</code> is <code>undefined</code>) is associated
17821cb0ef41Sopenharmony_ciinternally with a new instance of a V8 Context. This V8 Context provides the
17831cb0ef41Sopenharmony_ci<code>code</code> run using the <code>node:vm</code> module's methods with an isolated global
17841cb0ef41Sopenharmony_cienvironment within which it can operate. The process of creating the V8 Context
17851cb0ef41Sopenharmony_ciand associating it with the <code>contextObject</code> is what this document refers to as
17861cb0ef41Sopenharmony_ci"contextifying" the object.</p>
17871cb0ef41Sopenharmony_ci</section><section><h3>Timeout interactions with asynchronous tasks and Promises<span><a class="mark" href="#timeout-interactions-with-asynchronous-tasks-and-promises" id="timeout-interactions-with-asynchronous-tasks-and-promises">#</a></span><a aria-hidden="true" class="legacy" id="vm_timeout_interactions_with_asynchronous_tasks_and_promises"></a></h3>
17881cb0ef41Sopenharmony_ci<p><code>Promise</code>s and <code>async function</code>s can schedule tasks run by the JavaScript
17891cb0ef41Sopenharmony_ciengine asynchronously. By default, these tasks are run after all JavaScript
17901cb0ef41Sopenharmony_cifunctions on the current stack are done executing.
17911cb0ef41Sopenharmony_ciThis allows escaping the functionality of the <code>timeout</code> and
17921cb0ef41Sopenharmony_ci<code>breakOnSigint</code> options.</p>
17931cb0ef41Sopenharmony_ci<p>For example, the following code executed by <code>vm.runInNewContext()</code> with a
17941cb0ef41Sopenharmony_citimeout of 5 milliseconds schedules an infinite loop to run after a promise
17951cb0ef41Sopenharmony_ciresolves. The scheduled loop is never interrupted by the timeout:</p>
17961cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:vm'</span>);
17971cb0ef41Sopenharmony_ci
17981cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">loop</span>(<span class="hljs-params"></span>) {
17991cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'entering loop'</span>);
18001cb0ef41Sopenharmony_ci  <span class="hljs-keyword">while</span> (<span class="hljs-number">1</span>) <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>());
18011cb0ef41Sopenharmony_ci}
18021cb0ef41Sopenharmony_ci
18031cb0ef41Sopenharmony_civm.<span class="hljs-title function_">runInNewContext</span>(
18041cb0ef41Sopenharmony_ci  <span class="hljs-string">'Promise.resolve().then(() => loop());'</span>,
18051cb0ef41Sopenharmony_ci  { loop, <span class="hljs-variable language_">console</span> },
18061cb0ef41Sopenharmony_ci  { <span class="hljs-attr">timeout</span>: <span class="hljs-number">5</span> },
18071cb0ef41Sopenharmony_ci);
18081cb0ef41Sopenharmony_ci<span class="hljs-comment">// This is printed *before* 'entering loop' (!)</span>
18091cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'done executing'</span>);</code> <button class="copy-button">copy</button></pre>
18101cb0ef41Sopenharmony_ci<p>This can be addressed by passing <code>microtaskMode: 'afterEvaluate'</code> to the code
18111cb0ef41Sopenharmony_cithat creates the <code>Context</code>:</p>
18121cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:vm'</span>);
18131cb0ef41Sopenharmony_ci
18141cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">loop</span>(<span class="hljs-params"></span>) {
18151cb0ef41Sopenharmony_ci  <span class="hljs-keyword">while</span> (<span class="hljs-number">1</span>) <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>());
18161cb0ef41Sopenharmony_ci}
18171cb0ef41Sopenharmony_ci
18181cb0ef41Sopenharmony_civm.<span class="hljs-title function_">runInNewContext</span>(
18191cb0ef41Sopenharmony_ci  <span class="hljs-string">'Promise.resolve().then(() => loop());'</span>,
18201cb0ef41Sopenharmony_ci  { loop, <span class="hljs-variable language_">console</span> },
18211cb0ef41Sopenharmony_ci  { <span class="hljs-attr">timeout</span>: <span class="hljs-number">5</span>, <span class="hljs-attr">microtaskMode</span>: <span class="hljs-string">'afterEvaluate'</span> },
18221cb0ef41Sopenharmony_ci);</code> <button class="copy-button">copy</button></pre>
18231cb0ef41Sopenharmony_ci<p>In this case, the microtask scheduled through <code>promise.then()</code> will be run
18241cb0ef41Sopenharmony_cibefore returning from <code>vm.runInNewContext()</code>, and will be interrupted
18251cb0ef41Sopenharmony_ciby the <code>timeout</code> functionality. This applies only to code running in a
18261cb0ef41Sopenharmony_ci<code>vm.Context</code>, so e.g. <a href="#vmruninthiscontextcode-options"><code>vm.runInThisContext()</code></a> does not take this option.</p>
18271cb0ef41Sopenharmony_ci<p>Promise callbacks are entered into the microtask queue of the context in which
18281cb0ef41Sopenharmony_cithey were created. For example, if <code>() => loop()</code> is replaced with just <code>loop</code>
18291cb0ef41Sopenharmony_ciin the above example, then <code>loop</code> will be pushed into the global microtask
18301cb0ef41Sopenharmony_ciqueue, because it is a function from the outer (main) context, and thus will
18311cb0ef41Sopenharmony_cialso be able to escape the timeout.</p>
18321cb0ef41Sopenharmony_ci<p>If asynchronous scheduling functions such as <code>process.nextTick()</code>,
18331cb0ef41Sopenharmony_ci<code>queueMicrotask()</code>, <code>setTimeout()</code>, <code>setImmediate()</code>, etc. are made available
18341cb0ef41Sopenharmony_ciinside a <code>vm.Context</code>, functions passed to them will be added to global queues,
18351cb0ef41Sopenharmony_ciwhich are shared by all contexts. Therefore, callbacks passed to those functions
18361cb0ef41Sopenharmony_ciare not controllable through the timeout either.</p></section>
18371cb0ef41Sopenharmony_ci        <!-- API END -->
18381cb0ef41Sopenharmony_ci      </div>
18391cb0ef41Sopenharmony_ci    </div>
18401cb0ef41Sopenharmony_ci  </div>
18411cb0ef41Sopenharmony_ci</body>
18421cb0ef41Sopenharmony_ci</html>
1843