Node.js vm.runInContext() Method - GeeksforGeeks

11 Oct, 2021
The vm.runInContext() method is used to compile the code. It runs the code inside the context of the contextifiedObject and then returns the output. Moreover, the running code have no access to the local scope and, the contextifiedObject object be contextified formerly using vm.createContext() method.
Syntax:
vm.runInContext( code, contextifiedObject, options )
Parameters: This method accept three parameters as mentioned above and described below:
code: It is the JavaScript code to compile and run.
contextifiedObject: It is used as the global when the code is compiled and run.
options: It is optional parameter that holds Object or string and if it is a string, then it defines the filename which returns string.It holds the following parameters:filename: It holds a string. It specifies the filename that is used in the stack traces which are generated by this script. Its by default value is ‘evalmachine.anonymous’.lineOffset: It holds a number that specifies the offset of the line number that is shown in the stack traces which is generated by this script. Its default value is 0.columnOffset: It holds a number that specifies the offset of the column number that is shown in the stack traces which are generated by this script. Its by default value is 0.displayErrors: It holds a Boolean i.e. true if an error is thrown while compiling the code and the line of code because of which an error is thrown is linked to the stack trace. Its by default value is true.timeout: It holds an integer that specifies the number of milliseconds taken in order to execute the stated code before ending the execution. However, if an execution is closed then an error will occur. And the value for this must be a positive integer absolutely.breakOnSigint: It holds a Boolean. If its true, then the execution will be stopped as soon as SIGINT i.e, (Ctrl+C) is provided. And if the execution is stopped then an error is thrown. Its by default value is false.cachedData: It holds a Buffer, TypedArray, or a DataView. It gives an optional Buffer or TypedArray, or DataView for the use of supplied source with the help of V8’s code cache data. After that, the value of the cachedDataRejected can either be set to true or false. It depends upon the reception of data by V8.produceCachedData: It holds a Boolean. If its true and cachedData is no more available then V8 tries to output code cache data for the code. If this is accomplished then a buffer with V8’s code cache data is generated and then its consequently stored in the cachedData of the vm.Script instance that is being returned. Moreover, the cachedDataProduced value is either set to true or false depending upon the code cache data. However, this option is deprecated and script.createCachedData() is used instead. Its by default value is false.importModuleDynamically: It holds a function which is called while evaluating this module when import() method is called. And if this option is not stated then the calls to import() will be rejected with an error.It holds following parameters:specifier: It holds a string. It is the specifier passed to the import() method.module: It holds vm.Module. It returns either Module Namespace Object or vm.Module.
filename: It holds a string. It specifies the filename that is used in the stack traces which are generated by this script. Its by default value is ‘evalmachine.anonymous’.lineOffset: It holds a number that specifies the offset of the line number that is shown in the stack traces which is generated by this script. Its default value is 0.columnOffset: It holds a number that specifies the offset of the column number that is shown in the stack traces which are generated by this script. Its by default value is 0.displayErrors: It holds a Boolean i.e. true if an error is thrown while compiling the code and the line of code because of which an error is thrown is linked to the stack trace. Its by default value is true.timeout: It holds an integer that specifies the number of milliseconds taken in order to execute the stated code before ending the execution. However, if an execution is closed then an error will occur. And the value for this must be a positive integer absolutely.breakOnSigint: It holds a Boolean. If its true, then the execution will be stopped as soon as SIGINT i.e, (Ctrl+C) is provided. And if the execution is stopped then an error is thrown. Its by default value is false.cachedData: It holds a Buffer, TypedArray, or a DataView. It gives an optional Buffer or TypedArray, or DataView for the use of supplied source with the help of V8’s code cache data. After that, the value of the cachedDataRejected can either be set to true or false. It depends upon the reception of data by V8.produceCachedData: It holds a Boolean. If its true and cachedData is no more available then V8 tries to output code cache data for the code. If this is accomplished then a buffer with V8’s code cache data is generated and then its consequently stored in the cachedData of the vm.Script instance that is being returned. Moreover, the cachedDataProduced value is either set to true or false depending upon the code cache data. However, this option is deprecated and script.createCachedData() is used instead. Its by default value is false.importModuleDynamically: It holds a function which is called while evaluating this module when import() method is called. And if this option is not stated then the calls to import() will be rejected with an error.It holds following parameters:specifier: It holds a string. It is the specifier passed to the import() method.module: It holds vm.Module. It returns either Module Namespace Object or vm.Module.
filename: It holds a string. It specifies the filename that is used in the stack traces which are generated by this script. Its by default value is ‘evalmachine.anonymous’.
lineOffset: It holds a number that specifies the offset of the line number that is shown in the stack traces which is generated by this script. Its default value is 0.
columnOffset: It holds a number that specifies the offset of the column number that is shown in the stack traces which are generated by this script. Its by default value is 0.
displayErrors: It holds a Boolean i.e. true if an error is thrown while compiling the code and the line of code because of which an error is thrown is linked to the stack trace. Its by default value is true.
timeout: It holds an integer that specifies the number of milliseconds taken in order to execute the stated code before ending the execution. However, if an execution is closed then an error will occur. And the value for this must be a positive integer absolutely.
breakOnSigint: It holds a Boolean. If its true, then the execution will be stopped as soon as SIGINT i.e, (Ctrl+C) is provided. And if the execution is stopped then an error is thrown. Its by default value is false.
cachedData: It holds a Buffer, TypedArray, or a DataView. It gives an optional Buffer or TypedArray, or DataView for the use of supplied source with the help of V8’s code cache data. After that, the value of the cachedDataRejected can either be set to true or false. It depends upon the reception of data by V8.
produceCachedData: It holds a Boolean. If its true and cachedData is no more available then V8 tries to output code cache data for the code. If this is accomplished then a buffer with V8’s code cache data is generated and then its consequently stored in the cachedData of the vm.Script instance that is being returned. Moreover, the cachedDataProduced value is either set to true or false depending upon the code cache data. However, this option is deprecated and script.createCachedData() is used instead. Its by default value is false.
importModuleDynamically: It holds a function which is called while evaluating this module when import() method is called. And if this option is not stated then the calls to import() will be rejected with an error.It holds following parameters:specifier: It holds a string. It is the specifier passed to the import() method.module: It holds vm.Module. It returns either Module Namespace Object or vm.Module.
specifier: It holds a string. It is the specifier passed to the import() method.module: It holds vm.Module. It returns either Module Namespace Object or vm.Module.
specifier: It holds a string. It is the specifier passed to the import() method.
module: It holds vm.Module. It returns either Module Namespace Object or vm.Module.
Return Value: It returns the result of the very last statement executed in the script.
Below examples illustrate the use of vm.runInContext() method in Node.js:
Example 1:
// Node.js program to demonstrate the     // vm.runInContext() method  // Including util and vm moduleconst util = require('util');const vm = require('vm');  // Defining Context objectconst contextobj = { count: 8 }  // Contextifying stated object// using createContext methodvm.createContext(contextobj);  // Compiling code by using runInContext// method with its parametervm.runInContext('count *= 4;', contextobj, 'file.vm');  // Displays outputconsole.log("The output is: ", contextobj);
Output:
The output is:  { count: 32 }

Here, the count is 32 in output as (8*4 = 32).
Example 2:
// Node.js program to demonstrate the     // vm.runInContext() method  // Including util and vm moduleconst util = require('util');const vm = require('vm');  // Creating objectcobj = {         name_of_school: 'M.B.V',         number_of_students: 10    },  // Contexifying objectcontext = vm.createContext(cobj);  // Calling runInContext methodvm.runInContext('number_of_students *= 3;',             cobj, 'myfile.vm');  // Displays objectconsole.log(cobj);
Output:
{ name_of_school: 'M.B.V', number_of_students: 30 }

Reference: https://nodejs.org/api/vm.html#vm_vm_runincontext_code_contextifiedobject_options
Node.js-vm-module
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Node.js fs.readFileSync() Method
How to update Node.js and NPM to next version ?
Node.js fs.writeFile() Method
How to update NPM ?
Difference between promise and async await in Node.js
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript