The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / src / main.js @ 8a8e9df7
History | View | Annotate | Download (4.78 KB)
1 |
// module search paths
|
---|---|
2 |
node.includes = ["."];
|
3 |
|
4 |
// This is useful for dealing with raw encodings.
|
5 |
Array.prototype.encodeUtf8 = function () { |
6 |
return String.fromCharCode.apply(String, this); |
7 |
} |
8 |
|
9 |
node.path = new function () { |
10 |
this.join = function () { |
11 |
var joined = ""; |
12 |
for (var i = 0; i < arguments.length; i++) { |
13 |
var part = arguments[i].toString(); |
14 |
if (i === 0) { |
15 |
part = part.replace(/\/*$/, "/"); |
16 |
} else if (i === arguments.length - 1) { |
17 |
part = part.replace(/^\/*/, ""); |
18 |
} else {
|
19 |
part = part.replace(/^\/*/, "") |
20 |
.replace(/\/*$/, "/"); |
21 |
} |
22 |
joined += part; |
23 |
} |
24 |
return joined;
|
25 |
}; |
26 |
|
27 |
this.dirname = function (path) { |
28 |
if (path.charAt(0) !== "/") |
29 |
path = "./" + path;
|
30 |
var parts = path.split("/"); |
31 |
return parts.slice(0, parts.length-1).join("/"); |
32 |
}; |
33 |
}; |
34 |
|
35 |
// Namespace for module loading functionality
|
36 |
(function () {
|
37 |
function findScript(base_directory, name, callback) { |
38 |
// in the future this function will be more complicated
|
39 |
if (name.charAt(0) == "/") |
40 |
throw "absolute module paths are not yet supported."; |
41 |
|
42 |
var filename = node.path.join(base_directory, name) + ".js"; |
43 |
|
44 |
File.exists(filename, function (status) {
|
45 |
callback(status ? filename : null);
|
46 |
}); |
47 |
} |
48 |
|
49 |
// Constructor for submodule.
|
50 |
// "name" is like a path but without .js. e.g. "database/mysql"
|
51 |
// "target" is an object into which the submodule will be loaded.
|
52 |
function Sub (name, target) { |
53 |
this.name = name;
|
54 |
this.target = target;
|
55 |
|
56 |
this.load = function (base_directory, callback) { |
57 |
//node.debug("sub.load from <" + base_directory + "> " + this.toString());
|
58 |
findScript(base_directory, name, function (filename) {
|
59 |
if (filename === null) { |
60 |
stderr.puts("Cannot find a script matching: " + name);
|
61 |
process.exit(1);
|
62 |
} |
63 |
loadScript(filename, target, callback); |
64 |
}); |
65 |
}; |
66 |
|
67 |
this.toString = function () { |
68 |
return "[sub name=" + name + " target=" + target.toString() + "]"; |
69 |
} |
70 |
} |
71 |
|
72 |
function Scaffold (source, filename, module) { |
73 |
// wrap the source in a strange function
|
74 |
var source = "function (__filename) {" |
75 |
+ " var onLoad;"
|
76 |
+ " var exports = this;"
|
77 |
+ " var require = this.__require;"
|
78 |
+ " var include = this.__include;"
|
79 |
+ source |
80 |
+ " this.__onLoad = onLoad;"
|
81 |
+ "};"
|
82 |
; |
83 |
// returns the function
|
84 |
var compiled = node.compile(source, filename);
|
85 |
|
86 |
if (module.__onLoad) {
|
87 |
//node.debug("<"+ filename+"> has onload! this is bad");
|
88 |
} |
89 |
|
90 |
module.__subs = []; |
91 |
module.__require = function (name) { |
92 |
var target = {};
|
93 |
module.__subs.push(new Sub(name, target));
|
94 |
return target;
|
95 |
} |
96 |
module.__include = function (name) { |
97 |
module.__subs.push(new Sub(name, module));
|
98 |
} |
99 |
// execute the script of interest
|
100 |
compiled.apply(module, [filename]); |
101 |
|
102 |
// The module still needs to have its submodules loaded.
|
103 |
this.filename = filename;
|
104 |
this.module = module;
|
105 |
this.subs = module.__subs;
|
106 |
this.onLoad = module.__onLoad;
|
107 |
|
108 |
// remove these references so they don't get exported.
|
109 |
delete module.__subs;
|
110 |
delete module.__onLoad;
|
111 |
delete module.__require;
|
112 |
delete module.__include;
|
113 |
} |
114 |
|
115 |
function loadScript (filename, target, callback) { |
116 |
File.cat(filename, function (status, content) {
|
117 |
if (status != 0) { |
118 |
stderr.puts("Error reading " + filename + ": " + File.strerror(status)); |
119 |
process.exit(1);
|
120 |
} |
121 |
|
122 |
var scaffold = new Scaffold(content, filename, target); |
123 |
|
124 |
//node.debug("after scaffold <" + filename + ">");
|
125 |
|
126 |
function finish() { |
127 |
//node.debug("finish 1 load <" + filename + ">");
|
128 |
if (scaffold.onLoad instanceof Function) { |
129 |
//node.debug("calling onLoad for <" + filename + ">");
|
130 |
scaffold.onLoad(); |
131 |
} |
132 |
//node.debug("finish 2 load <" + filename + ">");
|
133 |
|
134 |
if (callback instanceof Function) |
135 |
callback(); |
136 |
} |
137 |
|
138 |
// Each time require() or include() was called inside the script
|
139 |
// a key/value was added to scaffold.__subs.
|
140 |
// Now we loop though each one and recursively load each.
|
141 |
if (scaffold.subs.length == 0) { |
142 |
finish(); |
143 |
} else {
|
144 |
var ncomplete = 0; |
145 |
for (var i = 0; i < scaffold.subs.length; i++) { |
146 |
var sub = scaffold.subs[i];
|
147 |
sub.load(node.path.dirname(filename), function () {
|
148 |
ncomplete += 1;
|
149 |
//node.debug("<" + filename + "> ncomplete = " + ncomplete.toString() + " scaffold.subs.length = " + scaffold.subs.length.toString());
|
150 |
if (ncomplete === scaffold.subs.length)
|
151 |
finish(); |
152 |
}); |
153 |
} |
154 |
} |
155 |
}); |
156 |
} |
157 |
|
158 |
loadScript(ARGV[1], this); |
159 |
})(); |
160 |
|