阿西河

所有教程

公众号
🌙
阿西河前端的公众号

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js script.createContext

      Node.js script.createContext([sandbox[, options]])

      版本历史

      版本变更
      v10.0.0The sandbox option can no longer be a function.
      v10.0.0The codeGeneration option is supported now.
      v0.3.1新增于: v0.3.1
      • sandbox < Object>
      • options < Object>
        • name < string> Human-readable name of the newly created context. Default: ‘VM Context i’, where i is an ascending numerical index of the created context.
        • origin < string> Origin corresponding to the newly created context for display purposes. The origin should be formatted like a URL, but with only the scheme, host, and port (if necessary), like the value of the url.origin property of a URL object. Most notably, this string should omit the trailing slash, as that denotes a path. Default: ‘’.
        • codeGeneration < Object>
          • strings < boolean> If set to false any calls to eval or function constructors (Function, GeneratorFunction, etc) will throw an EvalError. Default: true.
          • wasm < boolean> If set to false any attempt to compile a WebAssembly module will throw a WebAssembly.CompileError. Default: true.

      If given a sandbox object, the vm.createContext() method will prepare that sandbox so that it can be used in calls to vm.runInContext() or script.runInContext(). Inside such scripts, the sandbox object will be the global object, retaining all of its existing properties but also having the built-in objects and functions any standard global object has. Outside of scripts run by the vm module, global variables will remain unchanged.

      const util = require('util');
      const vm = require('vm');
      
      global.globalVar = 3;
      
      const sandbox = { globalVar: 1 };
      vm.createContext(sandbox);
      
      vm.runInContext('globalVar *= 2;', sandbox);
      
      console.log(util.inspect(sandbox)); // { globalVar: 2 }
      
      console.log(util.inspect(globalVar)); // 3
      

      If sandbox is omitted (or passed explicitly as undefined), a new, empty contextified sandbox object will be returned.

      The vm.createContext() method is primarily useful for creating a single sandbox that can be used to run multiple scripts. For instance, if emulating a web browser, the method can be used to create a single sandbox representing a window’s global object, then run all

      目录
      本文目录
      目录