Module basic module

modulevm

Sandbox module, used to isolate operating environments with different security levels

By establishing an isolation sandbox, you can limit the resources that scripts can access when running, isolate different script execution environments, and customize basic modules for different environments to ensure the security of the overall operating environment.

The following example creates a sandbox that restricts access to only global base modules.assertmodule, and add two custom modules a and b:

1 2 3 4 5 6 7 8
var vm = require('vm'); var sbox = new vm.SandBox({ a: 100, b: 200, assert: require('assert') }); var mod_in_sbox = sbox.require('./path/to/mod');

object

SandBox

CreateSandBoxobject, seeSandBox

1
SandBox vm.SandBox;

Script

CreateScriptobject, seeScript

1
Script vm.Script;

static function

createContext

Create a context object

1 2
static Object vm.createContext(Object contextObject = {}, Object opts = {});

Call parameters:

  • contextObject: Object, specifies the object to be contextualized
  • opts: Object, specifies context options

Return results:

  • Object, returns the context object

isContext

If givenobjectObject is usedvm.createContext() for contextualization, returns true

1
static Boolean vm.isContext(Object contextObject);

Call parameters:

  • contextObject: Object, specifies the object to be checked

Return results:


runInContext

Runs the code specified by code within the given contextualizedObject and returns the result

1 2 3
static Value vm.runInContext(String code, Object contextifiedObject, Object opts = {});

Call parameters:

  • code: String, specifies the script code to be compiled and run
  • contextifiedObject: Object, specifies the runtime context object
  • opts: Object, specify run options

Return results:

  • Value, return the running result

Runs the code specified by code within the given contextualizedObject and returns the result

1 2 3
static Value vm.runInContext(String code, Object contextifiedObject, String filename);

Call parameters:

  • code: String, specifies the script code to be compiled and run
  • contextifiedObject: Object, specifies the runtime context object
  • filename: String, specify the script file name

Return results:

  • Value, return the running result

runInNewContext

Use the given contextObject to run the code specified by code in the created context and return the result.

1 2 3
static Value vm.runInNewContext(String code, Object contextObject = {}, Object opts = {});

Call parameters:

  • code: String, specifies the script code to be compiled and run
  • contextObject: Object, specifies the object to be contextualized
  • opts: Object, specify run options

Return results:

  • Value, return the running result

Use the given contextObject to run the code specified by code in the created context and return the result.

1 2 3
static Value vm.runInNewContext(String code, Object contextObject = {}, String filename);

Call parameters:

  • code: String, specifies the script code to be compiled and run
  • contextObject: Object, specifies the object to be contextualized
  • filename: String, specify the script file name

Return results:

  • Value, return the running result

runInThisContext

Runs the code specified by code within the current context and returns the result

1 2
static Value vm.runInThisContext(String code, Object opts = {});

Call parameters:

  • code: String, specifies the script code to be compiled and run
  • opts: Object, specify run options

Return results:

  • Value, return the running result

Runs the code specified by code within the current context and returns the result

1 2
static Value vm.runInThisContext(String code, String filename);

Call parameters:

  • code: String, specifies the script code to be compiled and run
  • filename: String, specify the script file name

Return results:

  • Value, return the running result