VM

Материал из support.qbpro.ru
Версия от 17:58, 21 августа 2013; imported>Supportadmin

2 - Unstable

В Node.js, JavaScript код может быть скомпилирован и сразу выполнен или скомпилирован, а запущен позже или скомпилирован, сохранен и еще позже запущен :). Для этого неоходимо включить в код require('vm');.

Внимание

The vm module has many known issues and edge cases. If you run into issues or unexpected behavior, please consult the open issues on GitHub. Some of the biggest problems are described below.

Sandboxes

Аргумент sandbox в vm.runInNewContext и vm.createContext , наряду с аргументом initSandbox в vm.createContext, ведут себя не так, как можно было ожидать, и обычно их поведение варьируется между различными версиями Node.

The key issue to be aware of is that V8 provides no way to directly control the global object used within a context. As a result, while properties of your sandbox object will be available in the context, any properties from the prototypes of the sandbox may not be available. Furthermore, the this expression within the global scope of the context evaluates to the empty object ({}) instead of to your sandbox.

Your sandbox's properties are also not shared directly with the script. Instead, the properties of the sandbox are copied into the context at the beginning of execution, and then after execution, the properties are copied back out in an attempt to propagate any changes.

Globals

Properties of the global object, like Array and String, have different values inside of a context. This means that common expressions like [] instanceof Array or Object.getPrototypeOf([]) === Array.prototype may not produce expected results when used inside of scripts evaluated via the vm module.

Some of these problems have known workarounds listed in the issues for vm on GitHub. for example, Array.isArray works around the example problem with Array.

Example


Методы

createContext([Object initSandbox]) Object vm.createContext() creates a new context which is suitable for use as the second argument of a subsequent call to vm.runInContext().


createScript(String code[, String filename]) vm.Script This script can be run later many times using the other vm methods. In case of syntax error in code, createScript prints the syntax error to stderr and throws an exception.


runInContext(String code, Object context[, String filename]) String vm.runInContext() compiles code, then runs it in context and returns the result.


runInNewContext(String code[, Object sandbox][, String filename]) vm.runInNewContext() compiles code then runs it in sandbox and returns the result. Running code does not have access to local scope. The object sandbox is used as the global object for code. sandbox and filename are optional, and filename is only used in stack traces.


runInThisContext(String code[, String filename]) String vm.runInThisContext() compiles code as if it were loaded from filename, runs it, and returns the result. Running code does not have access to local scope. The filename is optional, and is only used in stack traces.


vm.Script

This object is created as a result of the vm.createScript() method. It represents some compiled code than can be run at a later moment.

Methods

runInNewContext([Object sandbox]) String Similar to vm.runInNewContext(), this is a method of a precompiled Script object.


runInThisContext() String Similar to vm.runInThisContext(), but a method of the precompiled Script object.