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 / node.js @ ffbbc465
History | View | Annotate | Download (7.45 KB)
1 |
(function (process) {
|
---|---|
2 |
|
3 |
process.global.process = process; |
4 |
process.global.global = process.global; |
5 |
global.GLOBAL = global; |
6 |
|
7 |
/** deprecation errors ************************************************/
|
8 |
|
9 |
function removed (reason) { |
10 |
return function () { |
11 |
throw new Error(reason) |
12 |
} |
13 |
} |
14 |
|
15 |
GLOBAL.__module = removed("'__module' has been renamed to 'module'");
|
16 |
GLOBAL.include = removed("include(module) has been removed. Use require(module)");
|
17 |
GLOBAL.puts = removed("puts() has moved. Use require('sys') to bring it back.");
|
18 |
GLOBAL.print = removed("print() has moved. Use require('sys') to bring it back.");
|
19 |
GLOBAL.p = removed("p() has moved. Use require('sys') to bring it back.");
|
20 |
process.debug = removed("process.debug() has moved. Use require('sys') to bring it back.");
|
21 |
process.error = removed("process.error() has moved. Use require('sys') to bring it back.");
|
22 |
process.watchFile = removed("process.watchFile() has moved to fs.watchFile()");
|
23 |
process.unwatchFile = removed("process.unwatchFile() has moved to fs.unwatchFile()");
|
24 |
process.mixin = removed('process.mixin() has been removed.');
|
25 |
process.createChildProcess = removed("childProcess API has changed. See doc/api.txt.");
|
26 |
process.inherits = removed("process.inherits() has moved to sys.inherits.");
|
27 |
|
28 |
process.assert = function (x, msg) { |
29 |
if (!x) throw new Error(msg || "assertion error"); |
30 |
}; |
31 |
|
32 |
var writeError = process.binding('stdio').writeError; |
33 |
|
34 |
var evalcxMsg;
|
35 |
process.evalcx = function () { |
36 |
if (!evalcxMsg) {
|
37 |
writeError(evalcxMsg = |
38 |
"process.evalcx is deprecated. Use Script.runInNewContext instead.\n");
|
39 |
} |
40 |
return process.binding('evals').Script |
41 |
.runInNewContext.apply(null, arguments); |
42 |
}; |
43 |
|
44 |
// nextTick()
|
45 |
|
46 |
var nextTickQueue = [];
|
47 |
|
48 |
process._tickCallback = function () { |
49 |
for (var l = nextTickQueue.length; l; l--) { |
50 |
nextTickQueue.shift()(); |
51 |
} |
52 |
}; |
53 |
|
54 |
process.nextTick = function (callback) { |
55 |
nextTickQueue.push(callback); |
56 |
process._needTickCallback(); |
57 |
}; |
58 |
|
59 |
// Module System
|
60 |
var module = {}
|
61 |
process.compile("(function (exports) {"
|
62 |
+ process.binding("natives").module
|
63 |
+ "\n})", "module")(module); |
64 |
|
65 |
// TODO: make sure that event module gets loaded here once it's
|
66 |
// factored out of module.js
|
67 |
// module.require("events");
|
68 |
|
69 |
// Signal Handlers
|
70 |
(function() {
|
71 |
var signalWatchers = {};
|
72 |
addListener = process.addListener, |
73 |
removeListener = process.removeListener; |
74 |
|
75 |
function isSignal (event) { |
76 |
return event.slice(0, 3) === 'SIG' && process.hasOwnProperty(event); |
77 |
}; |
78 |
|
79 |
// Wrap addListener for the special signal types
|
80 |
process.on = process.addListener = function (type, listener) { |
81 |
var ret = addListener.apply(this, arguments); |
82 |
if (isSignal(type)) {
|
83 |
if (!signalWatchers.hasOwnProperty(type)) {
|
84 |
var b = process.binding('signal_watcher'), |
85 |
w = new b.SignalWatcher(process[type]);
|
86 |
w.callback = function () { |
87 |
process.emit(type); |
88 |
} |
89 |
signalWatchers[type] = w; |
90 |
w.start(); |
91 |
} else if (this.listeners(type).length === 1) { |
92 |
signalWatchers[event].start(); |
93 |
} |
94 |
} |
95 |
|
96 |
return ret;
|
97 |
} |
98 |
|
99 |
process.removeListener = function (type, listener) { |
100 |
var ret = removeListener.apply(this, arguments); |
101 |
if (isSignal(type)) {
|
102 |
process.assert(signalWatchers.hasOwnProperty(type)); |
103 |
|
104 |
if (this.listeners(type).length === 0) { |
105 |
signalWatchers[type].stop(); |
106 |
} |
107 |
} |
108 |
|
109 |
return ret;
|
110 |
} |
111 |
})(); |
112 |
|
113 |
// Timers
|
114 |
function addTimerListener (callback) { |
115 |
var timer = this; |
116 |
// Special case the no param case to avoid the extra object creation.
|
117 |
if (arguments.length > 2) { |
118 |
var args = Array.prototype.slice.call(arguments, 2); |
119 |
timer.callback = function () { callback.apply(timer, args); }; |
120 |
} else {
|
121 |
timer.callback = callback; |
122 |
} |
123 |
} |
124 |
|
125 |
global.setTimeout = function (callback, after) { |
126 |
var timer = new process.Timer(); |
127 |
addTimerListener.apply(timer, arguments);
|
128 |
timer.start(after, 0);
|
129 |
return timer;
|
130 |
}; |
131 |
|
132 |
global.setInterval = function (callback, repeat) { |
133 |
var timer = new process.Timer(); |
134 |
addTimerListener.apply(timer, arguments);
|
135 |
timer.start(repeat, repeat); |
136 |
return timer;
|
137 |
}; |
138 |
|
139 |
global.clearTimeout = function (timer) { |
140 |
if (timer instanceof process.Timer) { |
141 |
timer.stop(); |
142 |
} |
143 |
}; |
144 |
|
145 |
global.clearInterval = global.clearTimeout; |
146 |
|
147 |
var stdout;
|
148 |
process.__defineGetter__('stdout', function () { |
149 |
if (stdout) return stdout; |
150 |
|
151 |
var binding = process.binding('stdio'), |
152 |
net = module.requireNative('net'),
|
153 |
fs = module.requireNative('fs'),
|
154 |
fd = binding.stdoutFD; |
155 |
|
156 |
if (binding.isStdoutBlocking()) {
|
157 |
stdout = new fs.WriteStream(null, {fd: fd}); |
158 |
} else {
|
159 |
stdout = new net.Stream(fd);
|
160 |
// FIXME Should probably have an option in net.Stream to create a stream from
|
161 |
// an existing fd which is writable only. But for now we'll just add
|
162 |
// this hack and set the `readable` member to false.
|
163 |
// Test: ./node test/fixtures/echo.js < /etc/passwd
|
164 |
stdout.readable = false;
|
165 |
} |
166 |
|
167 |
return stdout;
|
168 |
}); |
169 |
|
170 |
var stdin;
|
171 |
process.openStdin = function () { |
172 |
if (stdin) return stdin; |
173 |
|
174 |
var binding = process.binding('stdio'), |
175 |
net = module.requireNative('net'),
|
176 |
fs = module.requireNative('fs'),
|
177 |
fd = binding.openStdin(); |
178 |
|
179 |
if (binding.isStdinBlocking()) {
|
180 |
stdin = new fs.ReadStream(null, {fd: fd}); |
181 |
} else {
|
182 |
stdin = new net.Stream(fd);
|
183 |
stdin.readable = true;
|
184 |
} |
185 |
|
186 |
stdin.resume(); |
187 |
|
188 |
return stdin;
|
189 |
}; |
190 |
|
191 |
|
192 |
// console object
|
193 |
var formatRegExp = /%[sdj]/g; |
194 |
function format (f) { |
195 |
if (typeof f !== 'string') { |
196 |
var objects = [], sys = module.requireNative('sys'); |
197 |
for (var i = 0; i < arguments.length; i++) { |
198 |
objects.push(sys.inspect(arguments[i]));
|
199 |
} |
200 |
return objects.join(' '); |
201 |
} |
202 |
|
203 |
|
204 |
var i = 1; |
205 |
var args = arguments; |
206 |
return String(f).replace(formatRegExp, function (x) { |
207 |
switch (x) {
|
208 |
case '%s': return args[i++]; |
209 |
case '%d': return +args[i++]; |
210 |
case '%j': return JSON.stringify(args[i++]); |
211 |
default:
|
212 |
return x;
|
213 |
} |
214 |
}); |
215 |
} |
216 |
|
217 |
global.console = {}; |
218 |
|
219 |
global.console.log = function () { |
220 |
process.stdout.write(format.apply(this, arguments) + '\n'); |
221 |
}; |
222 |
|
223 |
global.console.info = global.console.log; |
224 |
|
225 |
global.console.warn = function () { |
226 |
writeError(format.apply(this, arguments) + '\n'); |
227 |
}; |
228 |
|
229 |
global.console.error = global.console.warn; |
230 |
|
231 |
global.console.dir = function(object){ |
232 |
var sys = module.requireNative('sys'); |
233 |
process.stdout.write(sys.inspect(object) + '\n');
|
234 |
} |
235 |
|
236 |
global.console.assert = function(expression){ |
237 |
if(!expression){
|
238 |
var arr = Array.prototype.slice.call(arguments, 1); |
239 |
process.assert(false, format.apply(this, arr)); |
240 |
} |
241 |
} |
242 |
|
243 |
global.Buffer = module.requireNative('buffer').Buffer;
|
244 |
|
245 |
process.exit = function (code) { |
246 |
process.emit("exit");
|
247 |
process.reallyExit(code); |
248 |
}; |
249 |
|
250 |
var cwd = process.cwd();
|
251 |
var path = module.requireNative('path'); |
252 |
|
253 |
// Make process.argv[0] and process.argv[1] into full paths.
|
254 |
if (process.argv[0].indexOf('/') > 0) { |
255 |
process.argv[0] = path.join(cwd, process.argv[0]); |
256 |
} |
257 |
|
258 |
if (process.argv[1]) { |
259 |
if (process.argv[1].charAt(0) != "/" && !(/^http:\/\//).exec(process.argv[1])) { |
260 |
process.argv[1] = path.join(cwd, process.argv[1]); |
261 |
} |
262 |
|
263 |
module.runMain(); |
264 |
} else {
|
265 |
// No arguments, run the repl
|
266 |
var repl = module.requireNative('repl'); |
267 |
console.log("Type '.help' for options.");
|
268 |
repl.start(); |
269 |
} |
270 |
|
271 |
// All our arguments are loaded. We've evaluated all of the scripts. We
|
272 |
// might even have created TCP servers. Now we enter the main eventloop. If
|
273 |
// there are no watchers on the loop (except for the ones that were
|
274 |
// ev_unref'd) then this function exits. As long as there are active
|
275 |
// watchers, it blocks.
|
276 |
process.loop(); |
277 |
|
278 |
process.emit("exit");
|
279 |
|
280 |
}); |