Script
[<Interface>]
type Script =
abstract member cachedData: NonSharedBuffer option with get, set
abstract member cachedDataProduced: bool option with get, set
abstract member cachedDataRejected: bool option with get, set
abstract member sourceMapURL: string option with get, set
static member Create (runInContext: Func<Context,RunningScriptOptions option,obj>, runInNewContext: Func<U2<float,Context> option,RunningScriptInNewContextOptions option,obj>, runInThisContext: RunningScriptOptions option -> obj, createCachedData: unit -> NonSharedBuffer, ?cachedDataProduced: bool, ?cachedDataRejected: bool, ?cachedData: NonSharedBuffer, ?sourceMapURL: string) : Script
abstract member createCachedData () : NonSharedBuffer
abstract member runInContext (contextifiedObject: Context, ?options: RunningScriptOptions) : obj
abstract member runInNewContext (?contextObject: U2<float,Context>, ?options: RunningScriptInNewContextOptions) : obj
abstract member runInThisContext (?options: RunningScriptOptions) : obj| Assembly | Xantham.Fable.Node |
| Attributes | [<Interface>] |
Instances of the vm.Script class contain precompiled scripts that can be executed in specific contexts.
@since v0.3.1
Properties
abstract member cachedData: NonSharedBuffer option with get, setabstract member cachedDataProduced: bool option with get, set
abstract member cachedDataProduced: bool option with get, set@deprecated in favor of script.createCachedData()
abstract member cachedDataRejected: bool option with get, set
abstract member cachedDataRejected: bool option with get, setWhen cachedData is supplied to create the vm.Script, this value will be set to either true or false depending on acceptance of the data by V8. Otherwise the value is undefined.
@since v5.7.0
abstract member sourceMapURL: string option with get, set
abstract member sourceMapURL: string option with get, setWhen the script is compiled from a source that contains a source map magic comment, this property will be set to the URL of the source map.
import vm from 'node:vm';
const script = new vm.Script(`
function myFunc() {}
//# sourceMappingURL=sourcemap.json
`);
console.log(script.sourceMapURL);
// Prints: sourcemap.json
@since v19.1.0, v18.13.0
Methods
static member Create (runInContext: Func<Context,RunningScriptOptions option,obj>, runInNewContext: Func<U2<float,Context> option,RunningScriptInNewContextOptions option,obj>, runInThisContext: RunningScriptOptions option -> obj, createCachedData: unit -> NonSharedBuffer, ?cachedDataProduced: bool, ?cachedDataRejected: bool, ?cachedData: NonSharedBuffer, ?sourceMapURL: string) : Scriptabstract member createCachedData () : NonSharedBuffer
abstract member createCachedData () : NonSharedBufferCreates a code cache that can be used with the Script constructor's cachedData option. Returns a Buffer. This method may be called at any time and any number of times.
The code cache of the Script doesn't contain any JavaScript observable states. The code cache is safe to be saved along side the script source and used to construct new Script instances multiple times.
Functions in the Script source can be marked as lazily compiled and they are not compiled at construction of the Script. These functions are going to be compiled when they are invoked the first time. The code cache serializes the metadata that V8 currently knows about the Script that it can use to speed up future compilations.
const script = new vm.Script(`
function add(a, b) {
return a + b;
}
const x = add(1, 2);
`);
const cacheWithoutAdd = script.createCachedData();
// In `cacheWithoutAdd` the function `add()` is marked for full compilation
// upon invocation.
script.runInThisContext();
const cacheWithAdd = script.createCachedData();
// `cacheWithAdd` contains fully compiled function `add()`.
@since v10.6.0
abstract member runInContext (contextifiedObject: Context, ?options: RunningScriptOptions) : obj
abstract member runInContext (contextifiedObject: Context, ?options: RunningScriptOptions) : objRuns the compiled code contained by the vm.Script object within the given contextifiedObject and returns the result. Running code does not have access to local scope.
The following example compiles code that increments a global variable, sets the value of another global variable, then execute the code multiple times. The globals are contained in the context object.
import vm from 'node:vm';
const context = {
animal: 'cat',
count: 2,
};
const script = new vm.Script('count += 1; name = "kitty";');
vm.createContext(context);
for (let i = 0; i < 10; ++i) {
script.runInContext(context);
}
console.log(context);
// Prints: { animal: 'cat', count: 12, name: 'kitty' }
Using the timeout or breakOnSigint options will result in new event loops and corresponding threads being started, which have a non-zero performance overhead.
@since v0.3.1
| Parameter | |
|---|---|
contextifiedObject | A contextified object as returned by the vm.createContext() method. |
Returns. the result of the very last statement executed in the script.
abstract member runInNewContext (?contextObject: U2<float,Context>, ?options: RunningScriptInNewContextOptions) : obj
abstract member runInNewContext (?contextObject: U2<float,Context>, ?options: RunningScriptInNewContextOptions) : objThis method is a shortcut to script.runInContext(vm.createContext(options), options). It does several things at once:
- Creates a new context.
- If
contextObjectis an object, contextifies it with the new context. IfcontextObjectis undefined, creates a new object and contextifies it. IfcontextObjectisvm.constants.DONT_CONTEXTIFY, don't contextify anything. - Runs the compiled code contained by the
vm.Scriptobject within the created context. The code does not have access to the scope in which this method is called. - Returns the result.
The following example compiles code that sets a global variable, then executes the code multiple times in different contexts. The globals are set on and contained within each individual context.
const vm = require('node:vm');
const script = new vm.Script('globalVar = "set"');
const contexts = [{}, {}, {}];
contexts.forEach((context) => {
script.runInNewContext(context);
});
console.log(contexts);
// Prints: [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }]
// This would throw if the context is created from a contextified object.
// vm.constants.DONT_CONTEXTIFY allows creating contexts with ordinary
// global objects that can be frozen.
const freezeScript = new vm.Script('Object.freeze(globalThis); globalThis;');
const frozenContext = freezeScript.runInNewContext(vm.constants.DONT_CONTEXTIFY);
@since v0.3.1
| Parameter | |
|---|---|
contextObject | Either vm.constants.DONT_CONTEXTIFY or an object that will be contextified. If undefined, an empty contextified object will be created for backwards compatibility. |
Returns. the result of the very last statement executed in the script.
abstract member runInThisContext (?options: RunningScriptOptions) : obj
abstract member runInThisContext (?options: RunningScriptOptions) : objRuns the compiled code contained by the vm.Script within the context of the current global object. Running code does not have access to local scope, but does have access to the current global object.
The following example compiles code that increments a global variable then executes that code multiple times:
import vm from 'node:vm';
global.globalVar = 0;
const script = new vm.Script('globalVar += 1', { filename: 'myfile.vm' });
for (let i = 0; i < 1000; ++i) {
script.runInThisContext();
}
console.log(globalVar);
// 1000
@since v0.3.1
Returns. the result of the very last statement executed in the script.