zoukankan      html  css  js  c++  java
  • node.js 动态执行脚本

    node.js最近新增了虚拟机模块,其实也不能说是新增的,只是把一些内部接口暴露出来罢了,从2.x就有了。我们可以从node / src / node.js看到这些代码:

    
    var Script = process.binding('evals').NodeScript;
    var runInThisContext = Script.runInThisContext;
    
      NativeModule.wrap = function(script) {
        return NativeModule.wrapper[0] + script + NativeModule.wrapper[1];
      };
    
      NativeModule.wrapper = [
        '(function (exports, require, module, __filename, __dirname) { ',
        '\n});'
      ];
    
      NativeModule.prototype.compile = function() {
        var source = NativeModule.getSource(this.id);
        source = NativeModule.wrap(source);
    
        var fn = runInThisContext(source, this.filename, true);
        fn(this.exports, NativeModule.require, this, this.filename);
    
        this.loaded = true;
      };
    
    
    

    其中的Script对象,就与require('vm')返回的对象很相似,而实质上,vm模块就是对Script对象的封装。

    var Script = process.binding('evals').NodeScript;
    console.log(Script)
    /**
    { [Function: NodeScript]
      createContext: [Function],
      runInContext: [Function],
      runInThisContext: [Function],
      runInNewContext: [Function] }
    
    */
    
    
    console.log(require('vm'))
    { Script: 
       { [Function: NodeScript]
         createContext: [Function],
         runInContext: [Function],
         runInThisContext: [Function],
         runInNewContext: [Function] },
      createScript: [Function],
      createContext: [Function],
      runInContext: [Function],
      runInThisContext: [Function],
      runInNewContext: [Function] }
    

    其中,runInThisContext 相当于一个全新的环境中执行代码,不会影响当前作用域的对象。而runInNewContext与runInContext则能指定是上下文对象,区别是一个普通对象或一个context对象。换言之,runInNewContext与runInContext能局部影响当前作用域的对象。要与当前环境完全进行交互的话,就需要用到危险的eval。在node.js自带的加载体系中,显然没有这样的勇气,使用的是runInThisContext。并且在这之前做了许多工作,如把用户的JS文件里面的内容再包一层( NativeModule.wrap),还有其他凌散操作,加之是同步操作,实际上是一种效率很糟的加载方式。唯一的好处是,使用了同步,让代码编写起来简单多了。

    在github中,已有人对这几种动态执行脚本的方法进行性能比较:

    var vm = require('vm'),
      code = 'var square = n * n;',
      fn = new Function('n', code),
      script = vm.createScript(code),
      sandbox;
    
    n = 5;
    sandbox = { n: n };
    
    benchmark = function(title, funk) {
      var end, i, start;
      start = new Date;
      for (i = 0; i < 5000; i++) {
        funk();
      }
      end = new Date;
      console.log(title + ': ' + (end - start) + 'ms');
    }
    
    var ctx = vm.createContext(sandbox);
    benchmark('vm.runInThisContext',     function() { vm.runInThisContext(code); });
    benchmark('vm.runInNewContext',      function() { vm.runInNewContext(code, sandbox); });
    benchmark('script.runInThisContext', function() { script.runInThisContext(); });
    benchmark('script.runInNewContext',  function() { script.runInNewContext(sandbox); });
    benchmark('script.runInContext',  function() { script.runInContext(ctx); });
    benchmark('fn',                      function() { fn(n); });
    /**
    vm.runInThisContext: 212ms
    vm.runInNewContext: 2222ms
    script.runInThisContext: 6ms
    script.runInNewContext: 1876ms
    script.runInContext: 44ms
    fn: 0ms
    
    */
    

    由此可见,还是v8自带的方法Function完胜!

  • 相关阅读:
    通过kinaba对es进行crud
    安装启动kibana
    es安装启动
    IP网络层
    Java 高级文本处理
    django17
    邮件问题
    django16
    django15
    django14
  • 原文地址:https://www.cnblogs.com/rubylouvre/p/2262521.html
Copyright © 2011-2022 走看看