Skip to content

Instantly share code, notes, and snippets.

@imbcmdth
Created July 25, 2012 07:07
Show Gist options
  • Save imbcmdth/3174860 to your computer and use it in GitHub Desktop.
Save imbcmdth/3174860 to your computer and use it in GitHub Desktop.
var require = function(require){
var NM = function(){
var src = process.binding('natives');
src = Object.keys(src).reduce(function(ret, key){
ret[key] = src[key];
return ret;
}, {});
var NM = function NativeModule(id) {
this.filename = id + '.js';
this.id = id;
this.exports = {};
this.loaded = false;
}
Object.defineProperty(NM, '_source', { configurable: true, writable: true, value: src });
NM._cache = {};
NM.wrapper = ['(function(){ ', '\n});'];
NM.require = function require(id) {
if (id == 'native_module') return NM;
var cached = NM._cache[id];
if (cached) return cached.exports;
if (!NM.exists(id)) {
throw new Error('No such native module ' + id);
}
var nativeModule = new NM(id);
nativeModule.compile();
return nativeModule.exports;
};
NM.exists = function exists(id) {
return NM._source.hasOwnProperty(id);
};
NM.wrap = function wrap(script) {
return NM.wrapper[0] + script + NM.wrapper[1];
};
NM.prototype.compile = function compile() {
var source = NM._source[this.id];
var compiled = new Function('exports', 'require', 'module', '__filename', '__dirname', 'global', source);
compiled(this.exports, NM.require, this, this.filename, '', window);
this.loaded = true;
NM._cache[this.id] = this;
};
return NM;
}(require);
return function(){
var mod = require('module'),
path = require('path'),
fs = require('fs');
function Module(request, parent){
var filename = mod._resolveFilename(request, parent);
this.cache = parent.cache || {};
var cached = this.cache[filename];
if (cached) {
return cached.exports;
} else if (NM.exists(filename)) {
return NM.require(filename);
}
this.id = this.filename = filename;
this.cache[filename] = this;
this.parent = parent;
this.dirname = path.dirname(this.filename);
this.exports = {};
return this.load();
}
Module.prototype = {
require: function require(request){
return new Module(request, this);
},
resolve: function resolve(request){
return mod._resolveFilename(request, this);
},
load: function load(){
var self = this;
function require(request){ return self.require(request) }
function resolve(request){ return self.resolve(request) };
require.resolve = resolve;
var content = fs.readFileSync(this.filename, 'utf8').replace(/^\#\!.*/, '');
var compiled = new Function('exports', 'require', 'module', '__filename', '__dirname', 'global', content);
var result = compiled.call(this.exports, this.exports, require, this, this.filename, this.dirname, window);
return result === undefined || Object.getOwnPropertyNames(this.exports) > 0 ? this.exports : result;
}
};
var bootstrap = {
__proto__: Module.prototype,
cache : {},
id : module.id,
parent : module.parent,
filename : module.filename,
dirname : path.dirname(module.filename),
exports : module.exports,
paths : module.paths
};
bootstrap.cache[module.id] = module;
var requireHere = function require(request){
return bootstrap.require(request);
};
requireHere.resolve = function resolve(request){
return mod._resolveFilename(request, bootstrap);
};
return requireHere;
}();
}(window.require);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment