Skip to content
Related Articles

Related Articles

Node.js | vm.runInContext() Method
  • Last Updated : 27 Mar, 2020

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:
    1. 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’.
    2. 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.
    3. 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.
    4. 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.
    5. 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.
    6. 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.
    7. 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.
    8. 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.
    9. 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:



      1. specifier: It holds a string. It is the specifier passed to the import() method.
      2. 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 module
const util = require('util');
const vm = require('vm');
  
// Defining Context object
const contextobj = { count: 8 }
  
// Contextifying stated object
// using createContext method
vm.createContext(contextobj);
  
// Compiling code by using runInContext
// method with its parameter
vm.runInContext('count *= 4;', contextobj, 'file.vm');
  
// Displays output
console.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 module
const util = require('util');
const vm = require('vm');
  
// Creating object
cobj = {
         name_of_school: 'M.B.V',
         number_of_students: 10
    },
  
// Contexifying object
context = vm.createContext(cobj);
  
// Calling runInContext method
vm.runInContext('number_of_students *= 3;',
             cobj, 'myfile.vm');
  
// Displays object
console.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

My Personal Notes arrow_drop_up
Recommended Articles
Page :