nanha
2/16/2012 - 3:00 PM

Node's NativeModule

Node's NativeModule

var NativeModule;

/* Hack to get a reference to node's internal NativeModule */
(function(){
  // intercept NativeModule.require's call to process.moduleLoadList.push
  process.moduleLoadList.push = function(){
    // `NativeModule.require('native_module')` returns NativeModule
    NativeModule = arguments.callee.caller('native_module');

    // delete the interceptor and forward normal functionality
    delete process.moduleLoadList.push;
    return Array.prototype.push.apply(process.moduleLoadList, arguments);
  }

  // force an initial call to the interceptor
  require('vm');
})();