2009-04-15 10:08:28 +02:00
|
|
|
// module search paths
|
|
|
|
node.includes = ["."];
|
|
|
|
|
2009-04-17 18:54:29 +02:00
|
|
|
// This is useful for dealing with raw encodings.
|
|
|
|
Array.prototype.encodeUtf8 = function () {
|
|
|
|
return String.fromCharCode.apply(String, this);
|
|
|
|
}
|
|
|
|
|
2009-04-15 10:08:28 +02:00
|
|
|
node.path = new function () {
|
2009-04-20 02:55:08 +02:00
|
|
|
this.join = function () {
|
|
|
|
var joined = "";
|
|
|
|
for (var i = 0; i < arguments.length; i++) {
|
|
|
|
var part = arguments[i].toString();
|
|
|
|
if (i === 0) {
|
|
|
|
part = part.replace(/\/*$/, "/");
|
|
|
|
} else if (i === arguments.length - 1) {
|
|
|
|
part = part.replace(/^\/*/, "");
|
|
|
|
} else {
|
|
|
|
part = part.replace(/^\/*/, "")
|
|
|
|
.replace(/\/*$/, "/");
|
|
|
|
}
|
|
|
|
joined += part;
|
|
|
|
}
|
|
|
|
return joined;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.dirname = function (path) {
|
2009-04-21 13:52:21 +02:00
|
|
|
if (path.charAt(0) !== "/")
|
|
|
|
path = "./" + path;
|
2009-04-20 02:55:08 +02:00
|
|
|
var parts = path.split("/");
|
2009-04-21 13:52:21 +02:00
|
|
|
return parts.slice(0, parts.length-1).join("/");
|
2009-04-20 02:55:08 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2009-04-29 11:00:46 +02:00
|
|
|
// Timers
|
|
|
|
|
|
|
|
function setTimeout (callback, delay) {
|
2009-05-13 21:42:18 +02:00
|
|
|
var timer = new node.Timer(callback, delay, 0);
|
2009-04-29 11:00:46 +02:00
|
|
|
timer.start();
|
|
|
|
return timer;
|
|
|
|
};
|
|
|
|
|
|
|
|
function setInterval (callback, delay) {
|
2009-05-13 21:42:18 +02:00
|
|
|
var timer = new node.Timer(callback, delay, delay);
|
2009-04-29 11:00:46 +02:00
|
|
|
timer.start();
|
|
|
|
return timer;
|
|
|
|
};
|
|
|
|
|
|
|
|
function clearTimeout (timer) {
|
|
|
|
timer.stop();
|
|
|
|
delete timer;
|
|
|
|
};
|
|
|
|
clearInterval = clearTimeout;
|
|
|
|
|
|
|
|
// Modules
|
|
|
|
|
2009-04-20 02:55:08 +02:00
|
|
|
(function () {
|
|
|
|
function findScript(base_directory, name, callback) {
|
|
|
|
// in the future this function will be more complicated
|
|
|
|
if (name.charAt(0) == "/")
|
|
|
|
throw "absolute module paths are not yet supported.";
|
|
|
|
|
|
|
|
var filename = node.path.join(base_directory, name) + ".js";
|
2009-04-21 13:52:21 +02:00
|
|
|
|
2009-05-21 12:49:41 +02:00
|
|
|
node.fs.exists(filename, function (status) {
|
2009-04-20 18:44:30 +02:00
|
|
|
callback(status ? filename : null);
|
2009-04-20 02:55:08 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constructor for submodule.
|
|
|
|
// "name" is like a path but without .js. e.g. "database/mysql"
|
|
|
|
// "target" is an object into which the submodule will be loaded.
|
|
|
|
function Sub (name, target) {
|
|
|
|
this.name = name;
|
|
|
|
this.target = target;
|
|
|
|
|
|
|
|
this.load = function (base_directory, callback) {
|
2009-04-21 14:38:55 +02:00
|
|
|
//node.debug("sub.load from <" + base_directory + "> " + this.toString());
|
2009-04-20 02:55:08 +02:00
|
|
|
findScript(base_directory, name, function (filename) {
|
|
|
|
if (filename === null) {
|
|
|
|
stderr.puts("Cannot find a script matching: " + name);
|
2009-05-25 13:38:36 +02:00
|
|
|
node.exit(1);
|
2009-04-15 10:08:28 +02:00
|
|
|
}
|
2009-04-20 02:55:08 +02:00
|
|
|
loadScript(filename, target, callback);
|
|
|
|
});
|
2009-04-15 10:08:28 +02:00
|
|
|
};
|
|
|
|
|
2009-04-20 02:55:08 +02:00
|
|
|
this.toString = function () {
|
|
|
|
return "[sub name=" + name + " target=" + target.toString() + "]";
|
|
|
|
}
|
|
|
|
}
|
2009-04-15 10:08:28 +02:00
|
|
|
|
2009-04-20 02:55:08 +02:00
|
|
|
function Scaffold (source, filename, module) {
|
|
|
|
// wrap the source in a strange function
|
2009-04-20 18:44:30 +02:00
|
|
|
var source = "function (__filename) {"
|
2009-04-21 16:30:47 +02:00
|
|
|
+ " var onLoad;"
|
2009-04-20 18:44:30 +02:00
|
|
|
+ " var exports = this;"
|
|
|
|
+ " var require = this.__require;"
|
|
|
|
+ " var include = this.__include;"
|
|
|
|
+ source
|
2009-04-21 16:30:47 +02:00
|
|
|
+ " this.__onLoad = onLoad;"
|
2009-04-20 02:55:08 +02:00
|
|
|
+ "};"
|
|
|
|
;
|
|
|
|
// returns the function
|
|
|
|
var compiled = node.compile(source, filename);
|
|
|
|
|
2009-04-21 16:30:47 +02:00
|
|
|
if (module.__onLoad) {
|
2009-04-21 14:38:55 +02:00
|
|
|
//node.debug("<"+ filename+"> has onload! this is bad");
|
2009-04-21 13:52:21 +02:00
|
|
|
}
|
|
|
|
|
2009-04-20 02:55:08 +02:00
|
|
|
module.__subs = [];
|
|
|
|
module.__require = function (name) {
|
|
|
|
var target = {};
|
|
|
|
module.__subs.push(new Sub(name, target));
|
|
|
|
return target;
|
2009-04-15 10:08:28 +02:00
|
|
|
}
|
2009-04-20 02:55:08 +02:00
|
|
|
module.__include = function (name) {
|
|
|
|
module.__subs.push(new Sub(name, module));
|
|
|
|
}
|
|
|
|
// execute the script of interest
|
|
|
|
compiled.apply(module, [filename]);
|
2009-04-15 10:08:28 +02:00
|
|
|
|
2009-04-20 02:55:08 +02:00
|
|
|
// The module still needs to have its submodules loaded.
|
2009-04-21 13:52:21 +02:00
|
|
|
this.filename = filename;
|
2009-04-20 18:44:30 +02:00
|
|
|
this.module = module;
|
|
|
|
this.subs = module.__subs;
|
2009-04-21 16:30:47 +02:00
|
|
|
this.onLoad = module.__onLoad;
|
2009-04-15 10:08:28 +02:00
|
|
|
|
2009-04-20 02:55:08 +02:00
|
|
|
// remove these references so they don't get exported.
|
|
|
|
delete module.__subs;
|
2009-04-21 16:30:47 +02:00
|
|
|
delete module.__onLoad;
|
2009-04-20 02:55:08 +02:00
|
|
|
delete module.__require;
|
|
|
|
delete module.__include;
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadScript (filename, target, callback) {
|
2009-05-26 17:46:56 +02:00
|
|
|
node.fs.cat(filename, "utf8", function (status, content) {
|
2009-04-20 02:55:08 +02:00
|
|
|
if (status != 0) {
|
2009-06-01 12:56:28 +02:00
|
|
|
stderr.puts("Error reading " + filename);
|
2009-05-25 13:38:36 +02:00
|
|
|
node.exit(1);
|
2009-04-20 02:55:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var scaffold = new Scaffold(content, filename, target);
|
|
|
|
|
2009-04-21 14:38:55 +02:00
|
|
|
//node.debug("after scaffold <" + filename + ">");
|
2009-04-21 13:52:21 +02:00
|
|
|
|
2009-04-20 02:55:08 +02:00
|
|
|
function finish() {
|
2009-04-21 14:38:55 +02:00
|
|
|
//node.debug("finish 1 load <" + filename + ">");
|
2009-04-21 16:30:47 +02:00
|
|
|
if (scaffold.onLoad instanceof Function) {
|
2009-04-23 14:22:57 +02:00
|
|
|
//node.debug("calling onLoad for <" + filename + ">");
|
2009-04-21 16:30:47 +02:00
|
|
|
scaffold.onLoad();
|
2009-04-21 13:52:21 +02:00
|
|
|
}
|
2009-04-21 14:38:55 +02:00
|
|
|
//node.debug("finish 2 load <" + filename + ">");
|
2009-04-20 02:55:08 +02:00
|
|
|
|
|
|
|
if (callback instanceof Function)
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Each time require() or include() was called inside the script
|
|
|
|
// a key/value was added to scaffold.__subs.
|
|
|
|
// Now we loop though each one and recursively load each.
|
|
|
|
if (scaffold.subs.length == 0) {
|
|
|
|
finish();
|
|
|
|
} else {
|
2009-04-21 13:52:21 +02:00
|
|
|
var ncomplete = 0;
|
|
|
|
for (var i = 0; i < scaffold.subs.length; i++) {
|
|
|
|
var sub = scaffold.subs[i];
|
2009-04-20 02:55:08 +02:00
|
|
|
sub.load(node.path.dirname(filename), function () {
|
2009-04-21 13:52:21 +02:00
|
|
|
ncomplete += 1;
|
2009-04-21 14:38:55 +02:00
|
|
|
//node.debug("<" + filename + "> ncomplete = " + ncomplete.toString() + " scaffold.subs.length = " + scaffold.subs.length.toString());
|
2009-04-21 13:52:21 +02:00
|
|
|
if (ncomplete === scaffold.subs.length)
|
2009-04-20 18:44:30 +02:00
|
|
|
finish();
|
2009-04-20 02:55:08 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2009-04-15 10:08:28 +02:00
|
|
|
|
2009-04-20 02:55:08 +02:00
|
|
|
loadScript(ARGV[1], this);
|
|
|
|
})();
|