{
  "source": "vm.markdown",
  "modules": [
    {
      "textRaw": "执行 JavaScript",
      "name": "vm",
      "desc": "<pre><code>稳定度: 3 - 稳定</code></pre>\n<p>You can access this module with:\n\n</p>\n<p>你可以这样引入此模块：\n\n</p>\n<pre><code>var vm = require(&apos;vm&apos;);</code></pre>\n<p>JavaScript code can be compiled and run immediately or compiled, saved, and run\nlater.\n\n</p>\n<p>JavaScript 代码可以被编译并立即执行，也可以在编译后保存，留到稍后执行。\n\n</p>\n",
      "methods": [
        {
          "textRaw": "vm.runInThisContext(code, [options])",
          "type": "method",
          "name": "runInThisContext",
          "desc": "<p><code>vm.runInThisContext()</code> compiles <code>code</code>, runs it and returns the result. Running\ncode does not have access to local scope, but does have access to the current\n<code>global</code> object.\n\n</p>\n<p><code>vm.runInThisContext()</code> 对 <code>code</code> 进行编译、运行并返回结果。\n 被运行的代码没有对本地作用域 (local scope) 的访问权限，但是可以访问当前的 <code>global</code> 对象。\n\n</p>\n<p>Example of using <code>vm.runInThisContext</code> and <code>eval</code> to run the same code:\n\n</p>\n<p>使用 <code>vm.runInThisContext</code> 和 <code>eval</code> 分别执行相同的代码：\n\n</p>\n<pre><code>// vmResult: &apos;vm&apos;, localVar: &apos;initial value&apos;\n// evalResult: &apos;eval&apos;, localVar: &apos;eval&apos;</code></pre>\n<p><code>vm.runInThisContext</code> does not have access to the local scope, so <code>localVar</code> is\nunchanged. <code>eval</code> does have access to the local scope, so <code>localVar</code> is changed.\n\n</p>\n<p><code>vm.runInThisContext</code> 无法访问本地作用域，因此 <code>localVar</code> 没有被改变。\n<code>eval</code> 可以访问本地作用域，因此 <code>localVar</code> 被改变。\n\n</p>\n<p>In this way <code>vm.runInThisContext</code> is much like an [indirect <code>eval</code> call][1],\ne.g. <code>(0,eval)(&apos;code&apos;)</code>. However, it also has the following additional options:\n\n</p>\n<p>这种情况下 <code>vm.runInThisContext</code> 可以看作一种 [间接的 <code>eval</code> 调用][1]，\n如 <code>(0,eval)(&apos;code&apos;)</code>。但是 <code>vm.runInThisContext</code> 也提供下面几个额外的参数：\n\n</p>\n<ul>\n<li><code>filename</code>: allows you to control the filename that shows up in any stack\ntraces produced.</li>\n<li><code>displayErrors</code>: whether or not to print any errors to stderr, with the\nline of code that caused them highlighted, before throwing an exception.\nWill capture both syntax errors from compiling <code>code</code> and runtime errors\nthrown by executing the compiled code. Defaults to <code>true</code>.</li>\n<li><p><code>timeout</code>: a number of milliseconds to execute <code>code</code> before terminating\nexecution. If execution is terminated, an <code>Error</code> will be thrown.</p>\n</li>\n<li><p><code>filename</code>: 允许您更改显示在站追踪 (stack trace) 中的文件名</p>\n</li>\n<li><code>displayErrors</code>: 是否在抛出异常前输出带高亮错误代码行的错误信息到 stderr。\n将会捕捉所有在编译 <code>code</code> 的过程中产生的语法错误以及执行过程中产生的运行时错误。\n默认为 <code>true</code></li>\n<li><code>timeout</code>: 以毫秒为单位规定 <code>code</code> 允许执行的时间。在执行过程中被终止时会有 <code>Error</code> 抛出。</li>\n</ul>\n",
          "signatures": [
            {
              "params": [
                {
                  "name": "code"
                },
                {
                  "name": "options",
                  "optional": true
                }
              ]
            }
          ]
        },
        {
          "textRaw": "vm.createContext([sandbox])",
          "type": "method",
          "name": "createContext",
          "desc": "<p>If given a <code>sandbox</code> object, will &quot;contextify&quot; that sandbox so that it can be\nused in calls to <code>vm.runInContext</code> or <code>script.runInContext</code>. Inside scripts run\nas such, <code>sandbox</code> will be the global object, retaining all its existing\nproperties but also having the built-in objects and functions any standard\n[global object][2] has. Outside of scripts run by the vm module, <code>sandbox</code> will\nbe unchanged.\n\n</p>\n<p>如提供 <code>sandbox</code> 对象则将沙箱 (sandbox) 对象 “上下文化 (contextify)” 供 <code>vm.runInContext</code> 或者 <code>script.runInContext</code> 使用。\n以此方式运行的脚本将以 <code>sandbox</code> 作为全局对象，该对象将在保留其所有的属性的基础上拥有标准 [全局对象][2] 所拥有的内置对象和函数。\n在由 vm 模块运行的脚本之外的地方 <code>sandbox</code> 将不会被改变。\n\n</p>\n<p>If not given a sandbox object, returns a new, empty contextified sandbox object\nyou can use.\n\n</p>\n<p>如果没有提供沙箱对象，则返回一个新建的、没有任何对象被上下文化的可用沙箱。\n\n</p>\n<p>This function is useful for creating a sandbox that can be used to run multiple\nscripts, e.g. if you were emulating a web browser it could be used to create a\nsingle sandbox representing a window&apos;s global object, then run all <code>&lt;script&gt;</code>\ntags together inside that sandbox.\n\n</p>\n<p>此函数可用于创建可执行多个脚本的沙箱，\n比如，在模拟浏览器的时候可以使用该函数创建一个用于表示 window 全局对象的沙箱，\n并将所有 <code>&lt;script&gt;</code> 标签放入沙箱执行。\n\n</p>\n",
          "signatures": [
            {
              "params": [
                {
                  "name": "sandbox",
                  "optional": true
                }
              ]
            }
          ]
        },
        {
          "textRaw": "vm.isContext(sandbox)",
          "type": "method",
          "name": "isContext",
          "desc": "<p>Returns whether or not a sandbox object has been contextified by calling\n<code>vm.createContext</code> on it.\n\n</p>\n<p>返回沙箱对象是否已经通过 <code>vm.createContext</code> 上下文化 (contextified)\n\n</p>\n",
          "signatures": [
            {
              "params": [
                {
                  "name": "sandbox"
                }
              ]
            }
          ]
        },
        {
          "textRaw": "vm.runInContext(code, contextifiedSandbox, [options])",
          "type": "method",
          "name": "runInContext",
          "desc": "<p><code>vm.runInContext</code> compiles <code>code</code>, then runs it in <code>contextifiedSandbox</code> and\nreturns the result. Running code does not have access to local scope. The\n<code>contextifiedSandbox</code> object must have been previously contextified via\n<code>vm.createContext</code>; it will be used as the global object for <code>code</code>.\n\n</p>\n<p><code>vm.runInContext</code> 编译 <code>code</code> 放入 <code>contextifiedSandbox</code> 执行并返回执行结果。\n被执行的代码对 本地作用域 (local scope) 没有访问权。\n<code>contextifiedSandbox</code> 必须在使用前通过 <code>vm.createContext</code> 上下文化，用作 <code>code</code> 的全局对象。\n\n</p>\n<p><code>vm.runInContext</code> takes the same options as <code>vm.runInThisContext</code>.\n\n</p>\n<p><code>vm.runInContext</code> 使用与 <code>vm.runInThisContext</code> 相同的 选项 (<code>options</code>)\n\n</p>\n<p>Example: compile and execute differnt scripts in a single existing context.\n\n</p>\n<p>示例：在同一个上下文中编译并执行不同的脚本\n\n</p>\n<pre><code>// { globalVar: 1024 }</code></pre>\n<p>Note that running untrusted code is a tricky business requiring great care.\n<code>vm.runInContext</code> is quite useful, but safely running untrusted code requires a\nseparate process.\n\n</p>\n<p>执行不可信代码 (untrusted code) 是一件充满技巧而且需要非常小心的工作。\n<code>vm.runInContext</code> 十分好用，但是安全地运行不可信代码还需要将这些代码放入单独的进程里面执行。\n\n</p>\n",
          "signatures": [
            {
              "params": [
                {
                  "name": "code"
                },
                {
                  "name": "contextifiedSandbox"
                },
                {
                  "name": "options",
                  "optional": true
                }
              ]
            }
          ]
        },
        {
          "textRaw": "vm.runInNewContext(code, [sandbox], [options])",
          "type": "method",
          "name": "runInNewContext",
          "desc": "<p><code>vm.runInNewContext</code> compiles <code>code</code>, contextifies <code>sandbox</code> if passed or\ncreates a new contextified sandbox if it&apos;s omitted, and then runs the code with\nthe sandbox as the global object and returns the result.\n\n</p>\n<p><code>vm.runInNewContext</code> 首先编译 <code>code</code>，若提供 <code>sandbox</code> 则将 <code>sandbox</code> 上下文化，若未提供则创建一个新的沙箱并上下文化，\n然后将代码放入沙箱作为全局对象的上下文内执行并返回结果。\n\n</p>\n<p><code>vm.runInNewContext</code> takes the same options as <code>vm.runInThisContext</code>.\n\n</p>\n<p><code>vm.runInNewContext</code> 使用与 <code>vm.runInThisContext</code> 相同的 选项 (<code>options</code>)\n\n</p>\n<p>Example: compile and execute code that increments a global variable and sets a\nnew one. These globals are contained in the sandbox.\n\n</p>\n<p>示例: 编译并执行一段“自增一个全局变量然后创建一个全局变量”的代码。这些被操作的全局变量会被保存在沙箱中。\n\n</p>\n<pre><code>// { animal: &apos;cat&apos;, count: 3, name: &apos;kitty&apos; }</code></pre>\n<p>Note that running untrusted code is a tricky business requiring great care.\n<code>vm.runInNewContext</code> is quite useful, but safely running untrusted code requires\na separate process.\n\n</p>\n<p>执行不可信代码 (untrusted code) 是一件充满技巧而且需要非常小心的工作。\n<code>vm.runInNewContext</code> 十分好用，但是安全地运行不可信代码还需要将这些代码放入单独的进程里面执行。\n\n</p>\n",
          "signatures": [
            {
              "params": [
                {
                  "name": "code"
                },
                {
                  "name": "sandbox",
                  "optional": true
                },
                {
                  "name": "options",
                  "optional": true
                }
              ]
            }
          ]
        }
      ],
      "modules": [
        {
          "textRaw": "类: Script",
          "name": "类:_script",
          "desc": "<p>A class for holding precompiled scripts, and running them in specific sandboxes.\n\n</p>\n<p>用于存放预编译脚本的类，可将预编译代码放入沙箱执行。\n\n</p>\n",
          "methods": [
            {
              "textRaw": "new vm.Script(code, options)",
              "type": "method",
              "name": "Script",
              "desc": "<p>Creating a new <code>Script</code> compiles <code>code</code> but does not run it. Instead, the\ncreated <code>vm.Script</code> object represents this compiled code. This script can be run\nlater many times using methods below. The returned script is not bound to any\nglobal object. It is bound before each run, just for that run.\n\n</p>\n<p>创建一个新的 <code>Script</code> 用于编译 <code>code</code> 但是不执行。使用被创建的 <code>vm.Script</code> 用来表示完成编译的代码。\n这份可以在后面的代码中执行多次。\n返回的脚本是未绑定任何全局对象 (上下文 context) 的，全局对象仅在每一次执行的时候被绑定，执行结束后即释放绑定。\n\n</p>\n<p>The options when creating a script are:\n\n</p>\n<p>创建脚本的选项 (option) 有：\n\n</p>\n<ul>\n<li><code>filename</code>: allows you to control the filename that shows up in any stack\ntraces produced from this script.</li>\n<li><p><code>displayErrors</code>: whether or not to print any errors to stderr, with the\nline of code that caused them highlighted, before throwing an exception.\nApplies only to syntax errors compiling the code; errors while running the\ncode are controlled by the options to the script&apos;s methods.</p>\n</li>\n<li><p><code>filename</code>: 允许您更改显示在站追踪 (stack trace) 中的文件名</p>\n</li>\n<li><code>displayErrors</code>: 是否在抛出异常前输出带高亮错误代码行的错误信息到 stderr。\n仅捕捉所有在编译过程中产生的语法错误（运行时错误由运行脚本选项控制）。</li>\n</ul>\n",
              "signatures": [
                {
                  "params": [
                    {
                      "name": "code"
                    },
                    {
                      "name": "options"
                    }
                  ]
                }
              ]
            },
            {
              "textRaw": "script.runInThisContext([options])",
              "type": "method",
              "name": "runInThisContext",
              "desc": "<p>Similar to <code>vm.runInThisContext</code> but a method of a precompiled <code>Script</code> object.\n<code>script.runInThisContext</code> runs <code>script</code>&apos;s compiled code and returns the result.\nRunning code does not have access to local scope, but does have access to the\ncurrent <code>global</code> object.\n\n</p>\n<p>类似 <code>vm.runInThisContext</code> 只是作为预编译的 <code>Script</code> 对象方法。\n<code>script.runInThisContext</code> 执行被编译的 <code>script</code> 并返回结果。\n被运行的代码没有对本地作用域 (local scope) 的访问权限，但是可以访问当前的 <code>global</code> 对象。\n\n</p>\n<p>Example of using <code>script.runInThisContext</code> to compile code once and run it\nmultiple times:\n\n</p>\n<p>示例: 使用 <code>script.runInThisContext</code> 编译代码并多次执行: \n\n</p>\n<pre><code>// 1000</code></pre>\n<p>The options for running a script are:\n\n</p>\n<p>运行脚本的选项 (option) 有: \n\n</p>\n<ul>\n<li><code>displayErrors</code>: whether or not to print any runtime errors to stderr, with\nthe line of code that caused them highlighted, before throwing an exception.\nApplies only to runtime errors executing the code; it is impossible to create\na <code>Script</code> instance with syntax errors, as the constructor will throw.</li>\n<li><p><code>timeout</code>: a number of milliseconds to execute the script before terminating\nexecution. If execution is terminated, an <code>Error</code> will be thrown.</p>\n</li>\n<li><p><code>displayErrors</code>: 是否在抛出异常前输出带高亮错误代码行的错误信息到 stderr。\n仅捕捉所有执行过程中产生的运行时错误（语法错误会在 <code>Script</code> 示例创建时就发生，因此不可能创建出带语法错误的 <code>Script</code> 对象）。</p>\n</li>\n<li><code>timeout</code>: 以毫秒为单位规定 <code>code</code> 允许执行的时间。在执行过程中被终止时会有 <code>Error</code> 抛出。</li>\n</ul>\n",
              "signatures": [
                {
                  "params": [
                    {
                      "name": "options",
                      "optional": true
                    }
                  ]
                }
              ]
            },
            {
              "textRaw": "script.runInContext(contextifiedSandbox, [options])",
              "type": "method",
              "name": "runInContext",
              "desc": "<p>Similar to <code>vm.runInContext</code> but a method of a precompiled <code>Script</code> object.\n<code>script.runInContext</code> runs <code>script</code>&apos;s compiled code in <code>contextifiedSandbox</code>\nand returns the result. Running code does not have access to local scope.\n\n</p>\n<p>类似 <code>vm.runInContext</code> 只是作为预编译的 <code>Script</code> 对象方法。\n<code>script.runInContext</code> 在 <code>contextifiedSandbox</code> 中执行 <code>script</code> 编译出的代码，并返回结果。\n被运行的代码没有对本地作用域 (local scope) 的访问权限。\n\n</p>\n<p><code>script.runInContext</code> takes the same options as <code>script.runInThisContext</code>.\n\n</p>\n<p><code>script.runInContext</code> 使用与 <code>script.runInThisContext</code> 相同的 选项 (option)。\n\n</p>\n<p>Example: compile code that increments a global variable and sets one, then\nexecute the code multiple times. These globals are contained in the sandbox.\n\n</p>\n<p>示例: 编译一段“自增一个全局变量然后创建一个全局变量”的代码，然后多次执行此代码，\n被操作的全局变量会被保存在沙箱中。\n\n</p>\n<pre><code>// { animal: &apos;cat&apos;, count: 12, name: &apos;kitty&apos; }</code></pre>\n<p>Note that running untrusted code is a tricky business requiring great care.\n<code>script.runInContext</code> is quite useful, but safely running untrusted code\nrequires a separate process.\n\n</p>\n<p>执行不可信代码 (untrusted code) 是一件充满技巧而且需要非常小心的工作。\n<code>script.runInContext</code> 十分好用，但是安全地运行不可信代码还需要将这些代码放入单独的进程里面执行。\n\n</p>\n",
              "signatures": [
                {
                  "params": [
                    {
                      "name": "contextifiedSandbox"
                    },
                    {
                      "name": "options",
                      "optional": true
                    }
                  ]
                }
              ]
            },
            {
              "textRaw": "script.runInNewContext([sandbox], [options])",
              "type": "method",
              "name": "runInNewContext",
              "desc": "<p>Similar to <code>vm.runInNewContext</code> but a method of a precompiled <code>Script</code> object.\n<code>script.runInNewContext</code> contextifies <code>sandbox</code> if passed or creates a new\ncontextified sandbox if it&apos;s omitted, and then runs <code>script</code>&apos;s compiled code\nwith the sandbox as the global object and returns the result. Running code does\nnot have access to local scope.\n\n</p>\n<p>类似 <code>vm.runInNewContext</code> 但是作为预编译的 <code>Script</code> 对象方法。\n若提供 <code>sandbox</code> 则 <code>script.runInNewContext</code> 将 <code>sandbox</code> 上下文化，若未提供，则创建一个新的上下文化的沙箱，\n然后将代码放入沙箱作为全局对象的上下文内执行并返回结果。\n\n</p>\n<p><code>script.runInNewContext</code> takes the same options as <code>script.runInThisContext</code>.\n\n</p>\n<p><code>script.runInNewContext</code> 使用与 <code>script.runInThisContext</code> 相同的 选项 (option)。\n\n</p>\n<p>Example: compile code that sets a global variable, then execute the code\nmultiple times in different contexts. These globals are set on and contained in\nthe sandboxes.\n\n</p>\n<p>示例: 编译一段“写入一个全局变量”的代码，然后将代码放入不同的上下文 (context) 执行，这些被操作的全局变量会被保存在沙箱中。\n\n</p>\n<pre><code>// [{ globalVar: &apos;set&apos; }, { globalVar: &apos;set&apos; }, { globalVar: &apos;set&apos; }]</code></pre>\n<p>Note that running untrusted code is a tricky business requiring great care.\n<code>script.runInNewContext</code> is quite useful, but safely running untrusted code\nrequires a separate process.\n\n\n</p>\n<p>执行不可信代码 (untrusted code) 是一件充满技巧而且需要非常小心的工作。\n<code>script.runInNewContext</code> 十分好用，但是安全地运行不可信代码还需要将这些代码放入单独的进程里面执行。</p>\n",
              "signatures": [
                {
                  "params": [
                    {
                      "name": "sandbox",
                      "optional": true
                    },
                    {
                      "name": "options",
                      "optional": true
                    }
                  ]
                }
              ]
            }
          ],
          "type": "module",
          "displayName": "类: Script"
        }
      ],
      "type": "module",
      "displayName": "vm"
    }
  ]
}