{
  "source": "doc/api/vm.markdown",
  "modules": [
    {
      "textRaw": "Executing JavaScript",
      "name": "vm",
      "stability": 3,
      "stabilityText": "Stable",
      "desc": "<p>You can access this module with:\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 later.\n\n\n</p>\n",
      "methods": [
        {
          "textRaw": "vm.runInThisContext(code, [filename])",
          "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. <code>filename</code> is optional, it&apos;s used only\nin stack traces.\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<pre><code>var localVar = 123,\n    usingscript, evaled,\n    vm = require(&apos;vm&apos;);\n\nusingscript = vm.runInThisContext(&apos;localVar = 1;&apos;,\n  &apos;myfile.vm&apos;);\nconsole.log(&apos;localVar: &apos; + localVar + &apos;, usingscript: &apos; +\n  usingscript);\nevaled = eval(&apos;localVar = 1;&apos;);\nconsole.log(&apos;localVar: &apos; + localVar + &apos;, evaled: &apos; +\n  evaled);\n\n// localVar: 123, usingscript: 1\n// localVar: 1, evaled: 1</code></pre>\n<p><code>vm.runInThisContext</code> does not have access to the local scope, so <code>localVar</code> is unchanged.\n<code>eval</code> does have access to the local scope, so <code>localVar</code> is changed.\n\n</p>\n<p>In case of syntax error in <code>code</code>, <code>vm.runInThisContext</code> emits the syntax error to stderr\nand throws an exception.\n\n\n</p>\n",
          "signatures": [
            {
              "params": [
                {
                  "name": "code"
                },
                {
                  "name": "filename",
                  "optional": true
                }
              ]
            }
          ]
        },
        {
          "textRaw": "vm.runInNewContext(code, [sandbox], [filename])",
          "type": "method",
          "name": "runInNewContext",
          "desc": "<p><code>vm.runInNewContext</code> compiles <code>code</code>, then runs it in <code>sandbox</code> and returns the\nresult. Running code does not have access to local scope. The object <code>sandbox</code>\nwill be used as the global object for <code>code</code>.\n<code>sandbox</code> and <code>filename</code> are optional, <code>filename</code> is only used in stack traces.\n\n</p>\n<p>Example: compile and execute code that increments a global variable and sets a new one.\nThese globals are contained in the sandbox.\n\n</p>\n<pre><code>var util = require(&apos;util&apos;),\n    vm = require(&apos;vm&apos;),\n    sandbox = {\n      animal: &apos;cat&apos;,\n      count: 2\n    };\n\nvm.runInNewContext(&apos;count += 1; name = &quot;kitty&quot;&apos;, sandbox, &apos;myfile.vm&apos;);\nconsole.log(util.inspect(sandbox));\n\n// { 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.  To prevent accidental\nglobal variable leakage, <code>vm.runInNewContext</code> is quite useful, but safely running untrusted code\nrequires a separate process.\n\n</p>\n<p>In case of syntax error in <code>code</code>, <code>vm.runInNewContext</code> emits the syntax error to stderr\nand throws an exception.\n\n</p>\n",
          "signatures": [
            {
              "params": [
                {
                  "name": "code"
                },
                {
                  "name": "sandbox",
                  "optional": true
                },
                {
                  "name": "filename",
                  "optional": true
                }
              ]
            }
          ]
        },
        {
          "textRaw": "vm.runInContext(code, context, [filename])",
          "type": "method",
          "name": "runInContext",
          "desc": "<p><code>vm.runInContext</code> compiles <code>code</code>, then runs it in <code>context</code> and returns the\nresult. A (V8) context comprises a global object, together with a set of\nbuilt-in objects and functions. Running code does not have access to local scope\nand the global object held within <code>context</code> will be used as the global object\nfor <code>code</code>.\n<code>filename</code> is optional, it&apos;s used only in stack traces.\n\n</p>\n<p>Example: compile and execute code in a existing context.\n\n</p>\n<pre><code>var util = require(&apos;util&apos;),\n    vm = require(&apos;vm&apos;),\n    initSandbox = {\n      animal: &apos;cat&apos;,\n      count: 2\n    },\n    context = vm.createContext(initSandbox);\n\nvm.runInContext(&apos;count += 1; name = &quot;CATT&quot;&apos;, context, &apos;myfile.vm&apos;);\nconsole.log(util.inspect(context));\n\n// { animal: &apos;cat&apos;, count: 3, name: &apos;CATT&apos; }</code></pre>\n<p>Note that <code>createContext</code> will perform a shallow clone of the supplied sandbox object in order to\ninitialise the global object of the freshly constructed context.\n\n</p>\n<p>Note that running untrusted code is a tricky business requiring great care.  To prevent accidental\nglobal variable leakage, <code>vm.runInContext</code> is quite useful, but safely running untrusted code\nrequires a separate process.\n\n</p>\n<p>In case of syntax error in <code>code</code>, <code>vm.runInContext</code> emits the syntax error to stderr\nand throws an exception.\n\n</p>\n",
          "signatures": [
            {
              "params": [
                {
                  "name": "code"
                },
                {
                  "name": "context"
                },
                {
                  "name": "filename",
                  "optional": true
                }
              ]
            }
          ]
        },
        {
          "textRaw": "vm.createContext([initSandbox])",
          "type": "method",
          "name": "createContext",
          "desc": "<p><code>vm.createContext</code> creates a new context which is suitable for use as the 2nd argument of a subsequent\ncall to <code>vm.runInContext</code>. A (V8) context comprises a global object together with a set of\nbuild-in objects and functions. The optional argument <code>initSandbox</code> will be shallow-copied\nto seed the initial contents of the global object used by the context.\n\n</p>\n",
          "signatures": [
            {
              "params": [
                {
                  "name": "initSandbox",
                  "optional": true
                }
              ]
            }
          ]
        },
        {
          "textRaw": "vm.createScript(code, [filename])",
          "type": "method",
          "name": "createScript",
          "desc": "<p><code>createScript</code> compiles <code>code</code> but does not run it. Instead, it returns a\n<code>vm.Script</code> object representing 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. <code>filename</code> is\noptional, it&apos;s only used in stack traces.\n\n</p>\n<p>In case of syntax error in <code>code</code>, <code>createScript</code> prints the syntax error to stderr\nand throws an exception.\n\n\n</p>\n",
          "signatures": [
            {
              "params": [
                {
                  "name": "code"
                },
                {
                  "name": "filename",
                  "optional": true
                }
              ]
            }
          ]
        }
      ],
      "classes": [
        {
          "textRaw": "Class: Script",
          "type": "class",
          "name": "Script",
          "desc": "<p>A class for running scripts.  Returned by vm.createScript.\n\n</p>\n",
          "methods": [
            {
              "textRaw": "script.runInThisContext()",
              "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 the code of <code>script</code> and returns the result.\nRunning code does not have access to local scope, but does have access to the <code>global</code> object\n(v8: in actual context).\n\n</p>\n<p>Example of using <code>script.runInThisContext</code> to compile code once and run it multiple times:\n\n</p>\n<pre><code>var vm = require(&apos;vm&apos;);\n\nglobalVar = 0;\n\nvar script = vm.createScript(&apos;globalVar += 1&apos;, &apos;myfile.vm&apos;);\n\nfor (var i = 0; i &lt; 1000 ; i += 1) {\n  script.runInThisContext();\n}\n\nconsole.log(globalVar);\n\n// 1000</code></pre>\n",
              "signatures": [
                {
                  "params": []
                }
              ]
            },
            {
              "textRaw": "script.runInNewContext([sandbox])",
              "type": "method",
              "name": "runInNewContext",
              "desc": "<p>Similar to <code>vm.runInNewContext</code> a method of a precompiled <code>Script</code> object.\n<code>script.runInNewContext</code> runs the code of <code>script</code> with <code>sandbox</code> as the global object and returns the result.\nRunning code does not have access to local scope. <code>sandbox</code> is optional.\n\n</p>\n<p>Example: compile code that increments a global variable and sets one, then execute this code multiple times.\nThese globals are contained in the sandbox.\n\n</p>\n<pre><code>var util = require(&apos;util&apos;),\n    vm = require(&apos;vm&apos;),\n    sandbox = {\n      animal: &apos;cat&apos;,\n      count: 2\n    };\n\nvar script = vm.createScript(&apos;count += 1; name = &quot;kitty&quot;&apos;, &apos;myfile.vm&apos;);\n\nfor (var i = 0; i &lt; 10 ; i += 1) {\n  script.runInNewContext(sandbox);\n}\n\nconsole.log(util.inspect(sandbox));\n\n// { 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.  To prevent accidental\nglobal variable leakage, <code>script.runInNewContext</code> is quite useful, but safely running untrusted code\nrequires a separate process.\n</p>\n",
              "signatures": [
                {
                  "params": [
                    {
                      "name": "sandbox",
                      "optional": true
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "type": "module",
      "displayName": "vm"
    }
  ]
}
