Permalink
Browse files

Add support for asynchronous anonymous modules

Start on support for vanilla Rhino
  • Loading branch information...
kriszyp committed Sep 18, 2010
1 parent d141f4b commit 972f605637d90cc7c331aa761c7f9a8a39048f37
Showing with 72 additions and 24 deletions.
  1. +72 −24 lib/nodules.js
View
@@ -1,12 +1,23 @@
-var Unzip = require("./nodules-utils/unzip").Unzip,
- promiseModule = require("./nodules-utils/promise"),
- when = promiseModule.when,
- system = require("./nodules-utils/process"),
- print = system.print,
- zipInflate = require("./nodules-utils/inflate").zipInflate,
- paths = require.paths,
- defaultRequire = require,
- modules = {},
+(function(global){
+// create compile function for different platforms
+var compile = typeof process === "object" ?
+ function(source, name){
+ return process.compile("(" + source + ")", name);
+ } :
+ typeof Packages === "object" ?
+ function(source, name){
+ return Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(global, source, name, 1, null);
+ } : eval;
+
+
+if(typeof require == "undefined"){
+ // presumably this would only happen from a direct start in rhino
+ var args = global.arguments;
+ // bootstrap require
+ require = makeRequire("file://" + args[0]);
+ require.paths = [];
+}
+var modules = {},
factories = {},
waitingOn = 0,
inFlight = {},
@@ -18,9 +29,14 @@ var Unzip = require("./nodules-utils/unzip").Unzip,
filePathMappings = [],
defaultPath = "",
main = null,
- defaultMap = {
- "http://github.com/([^/]+)/}([^/]+)/raw/([^/]+)/(.*)": "zip:http://github.com/$1/$2/zipball/$3!/$4"
- },
+ Unzip = require("./nodules-utils/unzip").Unzip,
+ promiseModule = require("./nodules-utils/promise"),
+ when = promiseModule.when,
+ system = require("./nodules-utils/process"),
+ print = system.print,
+ zipInflate = require("./nodules-utils/inflate").zipInflate,
+ paths = require.paths,
+ defaultRequire = require,
allKnownOverlays = {npm: true, narwhal: true, rhino: true, node: true};
if(typeof process === "undefined"){
@@ -114,9 +130,12 @@ packages[""] = exports;
exports.mappings = [];
exports.mappings.defaultPath = "";
+exports.forBrowser = function(){
+ return new EnginePackage("browser");
+};
exports.forEngine = function(engine){
return new EnginePackage(engine);
-}
+};
exports.ensure = makeRequire("").ensure;
exports.runAsMain = function(uri){
@@ -532,6 +551,44 @@ exports.load = function(uri, require){
function createFactory(uri, source){
try{
factories[uri] = compile("function(require, exports, module, __filename, __dirname, Worker, SharedWorker){" + source + "\n;return exports;}", uri);
+ if(source.indexOf("require.def") > -1){
+ // looks like it is an Aynchronous module definition module
+ factories[uri]({def: function(id, dependencies, factory){
+ if(!factory){
+ factory = dependencies;
+ if(!factory){
+ factory = id;
+ id = null;
+ }
+ dependencies = null;
+
+ }
+ if(typeof id == "object"){
+ dependencies = id;
+ id = null;
+ }
+ if(typeof id == "string"){
+ if(uri.indexOf(id) == -1){
+ throw new Error("Can't set another module");
+ }
+ }
+ if(dependencies){
+ makeRequire(uri).ensure(dependencies.filter(function(dep){
+ return !(dep in {require:true, exports:true, module: true});
+ }));
+ }
+ factories[uri] = function(require, exports, module){
+ return factory.apply(exports, dependencies ? dependencies.map(function(id){
+ switch(id){
+ case "require": return require;
+ case "exports" : return exports;
+ case "module" : return module;
+ default: return require(id);
+ }
+ }) : arguments);
+ };
+ }});
+ }
}catch(e){
factories[uri] = function(){
throw new Error(e.message + " in " + uri);
@@ -786,16 +843,6 @@ function cache(handler, writeBack){
};
-// create compile function for different platforms
-var compile = typeof process === "object" ?
- function(source, name){
- return process.compile("(" + source + ")", name);
- } :
- typeof Packages === "object" ?
- function(source, name){
- return Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(global, source, name, 1, null);
- } : eval;
-
if(typeof process == "undefined"){
system.args.unshift(null);
}
@@ -815,4 +862,5 @@ if(require.main == module){
if(typeof process === "undefined"){
require("event-loop").enterEventLoop();
}
-}
+}
+})(this);

0 comments on commit 972f605

Please sign in to comment.