package node; /** The `node:vm` module enables compiling and running code within V8 Virtual Machine contexts. **The `node:vm` module is not a security** **mechanism. Do not use it to run untrusted code.** JavaScript code can be compiled and run immediately or compiled, saved, and run later. A common use case is to run the code in a different V8 Context. This means invoked code has a different global object than the invoking code. One can provide the context by `contextifying` an object. The invoked code treats any property in the context like a global variable. Any changes to global variables caused by the invoked code are reflected in the context object. ```js const vm = require('node:vm'); const x = 1; const context = { x: 2 }; vm.createContext(context); // Contextify the object. const code = 'x += 40; var y = 17;'; // `x` and `y` are global variables in the context. // Initially, x has the value 2 because that is the value of context.x. vm.runInContext(code, context); console.log(context.x); // 42 console.log(context.y); // 17 console.log(x); // 1; y is not defined. ``` **/ @:jsRequire("vm") @valueModuleOnly extern class Vm { /** If given a `contextObject`, the `vm.createContext()` method will `prepare that object` so that it can be used in calls to {@link runInContext} or `script.runInContext()`. Inside such scripts, the `contextObject` will be the global object, retaining all of its existing properties but also having the built-in objects and functions any standard [global object](https://es5.github.io/#x15.1) has. Outside of scripts run by the vm module, global variables will remain unchanged. ```js const vm = require('node:vm'); global.globalVar = 3; const context = { globalVar: 1 }; vm.createContext(context); vm.runInContext('globalVar *= 2;', context); console.log(context); // Prints: { globalVar: 2 } console.log(global.globalVar); // Prints: 3 ``` If `contextObject` is omitted (or passed explicitly as `undefined`), a new, empty `contextified` object will be returned. The `vm.createContext()` method is primarily useful for creating a single context that can be used to run multiple scripts. For instance, if emulating a web browser, the method can be used to create a single context representing a window's global object, then run all `