Created
February 2, 2010 23:18
-
-
Save silentrob/293157 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// module.js | |
// This loader is derived from nodes loader. :) | |
// Fixme: path is always absolute ie requre('/math'); | |
// Fixme: bug in loading order, and dependency support is weak | |
// Fixme: using a modified version of filesystem.get to look in the code directory. | |
// - SNIP From: /Applications/Smart.app/Contents/Resources/Image/lib/perl5/site_perl/5.10.0/RSP/Extension/Filesystem.pm | |
/** | |
- in | |
return { | |
'filesystem' => { | |
'get' => sub { | |
my $rsp_path = shift; | |
my $real_path = $tx->host->file( 'web', $rsp_path ); | |
if ( -f $real_path ) { | |
return RSP::JSObject::File->new( $real_path, $rsp_path ); | |
} else { | |
my $real_path = $tx->host->file( 'code', $rsp_path ); | |
if ( -f $real_path ) { | |
return RSP::JSObject::File->new( $real_path, $rsp_path ); | |
} else { | |
RSP::Error->throw("couldn't open file $rsp_path: $!"); | |
} | |
} | |
} | |
} | |
}; | |
**/ | |
function Module (id, parent) { | |
this.id = id; | |
this.exports = {}; | |
this.parent = parent; | |
this.filename = null; | |
}; | |
var moduleCache = {}; | |
function createModule (id, parent) { | |
if (id in moduleCache) { | |
system.console.log("found " + JSON.stringify(id) + " in cache"); | |
return moduleCache[id]; | |
} | |
system.console.log("didn't find " + JSON.stringify(id) + " in cache. creating new module"); | |
var m = new Module(id, parent); | |
moduleCache[id] = m; | |
return m; | |
}; | |
Module.prototype.load = function (filename) { | |
system.console.log("load " + JSON.stringify(filename) + " for module " + JSON.stringify(this.id)); | |
this.filename = filename; | |
var self = this; | |
function require (url) { | |
return loadModule(url, self); | |
} | |
var x = system.filesystem.get(this.filename); | |
var contents = x.contents; | |
// Hack to stop calling itself | |
contents = contents.replace('system.use("org.commonjs.module");',""); | |
var wrapper = "(function (exports, require, module, __filename, __dirname) { " | |
+ contents | |
+ "\n});"; | |
try { | |
var compiledWrapper = eval(wrapper); | |
// compiledWrapper.apply(self.exports, [self.exports, require, self, filename, path.dirname(filename)]); | |
compiledWrapper.apply(self.exports, [self.exports, require, self]); | |
} catch (e) { | |
system.console.log("Yikes something didn't work out"); | |
return; | |
} | |
return self.exports; | |
}; | |
function loadModule (request, parent) { | |
var id, paths; | |
if (request.charAt(0) == "." && (request.charAt(1) == "/" || request.charAt(1) == ".")) { | |
// Relative request | |
} else { | |
// Absolute request | |
id = request; | |
} | |
if (id in moduleCache) { | |
system.console.log("found " + JSON.stringify(id) + " in cache"); | |
// In cache | |
var module = moduleCache[id]; | |
return module.exports; | |
} else { | |
system.console.log("looking for " + JSON.stringify(id) + " in " + JSON.stringify(paths)); | |
// Not in cache | |
var module = createModule(id, parent); | |
module.load(request + ".js"); | |
} | |
}; | |
var bootstrapModule = createModule("bootstrap"); | |
bootstrapModule.load("bootstrap.js"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment