Home >Web Front-end >JS Tutorial >Detailed explanation of VM module in NodeJs_node.js

Detailed explanation of VM module in NodeJs_node.js

WBOY
WBOYOriginal
2016-05-16 16:00:512104browse

What is VM?

The VM module is the core module in NodeJS, supporting the require method and the operating mechanism of NodeJS. Sometimes we may also use VM templates to do some special things.

Through VM, JS can be compiled and run immediately or compiled, saved, and run later.
The VM module contains three commonly used methods for creating an independently running sandbox system, as follows:
vm.runInThisContext(code, filename);

This method is used to create an independent sandbox running space. The code within the code can access external global objects, but cannot access other variables

And the code is shared internally and externally

Copy code The code is as follows:

var vm = require("vm");

var p = 5;
global.p = 11;

vm.runInThisContext("console.log('ok', p)");//Display 11
under global vm.runInThisContext("console.log(global)"); // Display global

console.log(p);// Display 5
vm.runInContext(code, sandBox);

This method is used to create an independent sandbox running space. sandBox will be passed as a global variable into the code, but there is no global variable

The sandBox requirement is the sandBox created by the vm.createContext() method

Copy code The code is as follows:

var vm = require("vm");
var util = require("util");

var window = {
p: 2,
vm: vm,
console: console,
​ require: require
};

var p = 5;

global.p = 11;

vm.createContext(window);
vm.runInContext('p = 3;console.log(typeof global);', window); // global is undefined

console.log(window.p);// is changed to 3

console.log(util.inspect(window));
vm.runInNewContext(code, sandbox, opt);

This method should be the same as runInContext, but without the step of creating sandBox

Compare

More complex situations
What will happen if runInThisContext is executed in runInContext? Whose global object does runInThisContext access?

How will the following code be executed?

Copy code The code is as follows:

var vm = require("vm");
var util = require("util");

var window = {
p: 2,
vm: vm,
console: console,
​ require: require
};

window.global = window;

var p = 5;

global.p = 11;

vm.runInNewContext('p = 3;console.log(typeof global);require('vm').runInThisContext("console.log(p)");', window);

The code inside runInThisContext can access external global objects, but there is actually no global object outside (although there is, it is not essentially a global object). Just remember that runInThisContext can only access the top global object.

The execution results are as follows

Copy code The code is as follows:

object (global exists)
11 (top global p)
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn