2010-01-31 11:13:30 -08:00
|
|
|
(function (process) {
|
2010-01-30 23:22:34 -08:00
|
|
|
|
|
|
|
process.global.process = process;
|
|
|
|
process.global.global = process.global;
|
|
|
|
global.GLOBAL = global;
|
2009-10-31 19:02:30 +01:00
|
|
|
|
2010-01-15 12:46:08 -08:00
|
|
|
/** deprecation errors ************************************************/
|
2009-10-31 19:02:30 +01:00
|
|
|
|
2010-01-20 21:39:10 +01:00
|
|
|
function removed (reason) {
|
|
|
|
return function () {
|
|
|
|
throw new Error(reason)
|
|
|
|
}
|
|
|
|
}
|
2009-10-31 19:02:30 +01:00
|
|
|
|
2010-01-20 21:39:10 +01:00
|
|
|
GLOBAL.__module = removed("'__module' has been renamed to 'module'");
|
2010-03-08 15:08:30 -08:00
|
|
|
GLOBAL.include = removed("include(module) has been removed. Use require(module)");
|
2010-01-20 21:39:10 +01:00
|
|
|
GLOBAL.puts = removed("puts() has moved. Use require('sys') to bring it back.");
|
|
|
|
GLOBAL.print = removed("print() has moved. Use require('sys') to bring it back.");
|
|
|
|
GLOBAL.p = removed("p() has moved. Use require('sys') to bring it back.");
|
|
|
|
process.debug = removed("process.debug() has moved. Use require('sys') to bring it back.");
|
|
|
|
process.error = removed("process.error() has moved. Use require('sys') to bring it back.");
|
2010-03-01 10:42:37 -08:00
|
|
|
process.watchFile = removed("process.watchFile() has moved to fs.watchFile()");
|
|
|
|
process.unwatchFile = removed("process.unwatchFile() has moved to fs.unwatchFile()");
|
2010-04-13 13:44:05 -07:00
|
|
|
process.mixin = removed('process.mixin() has been removed.');
|
2010-03-17 14:00:17 -07:00
|
|
|
process.createChildProcess = removed("childProcess API has changed. See doc/api.txt.");
|
2010-03-01 11:39:35 -08:00
|
|
|
process.inherits = removed("process.inherits() has moved to sys.inherits.");
|
2009-06-30 13:40:00 +02:00
|
|
|
|
2009-11-07 14:37:22 +01:00
|
|
|
process.assert = function (x, msg) {
|
2010-04-28 22:37:09 +02:00
|
|
|
if (!x) throw new Error(msg || "assertion error");
|
2009-11-07 14:37:22 +01:00
|
|
|
};
|
|
|
|
|
2010-06-28 20:47:12 -07:00
|
|
|
var writeError = process.binding('stdio').writeError;
|
|
|
|
|
2010-04-28 22:51:16 +02:00
|
|
|
var evalcxMsg;
|
|
|
|
process.evalcx = function () {
|
|
|
|
if (!evalcxMsg) {
|
2010-06-28 20:47:12 -07:00
|
|
|
writeError(evalcxMsg =
|
2010-04-28 22:51:16 +02:00
|
|
|
"process.evalcx is deprecated. Use Script.runInNewContext instead.\n");
|
|
|
|
}
|
|
|
|
return process.binding('evals').Script
|
|
|
|
.runInNewContext.apply(null, arguments);
|
|
|
|
};
|
2010-03-15 15:11:40 -07:00
|
|
|
|
2010-01-18 10:27:27 -08:00
|
|
|
// nextTick()
|
|
|
|
|
|
|
|
var nextTickQueue = [];
|
|
|
|
|
2010-04-13 15:39:15 -07:00
|
|
|
process._tickCallback = function () {
|
2010-04-28 22:37:09 +02:00
|
|
|
for (var l = nextTickQueue.length; l; l--) {
|
|
|
|
nextTickQueue.shift()();
|
2010-01-18 10:27:27 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
process.nextTick = function (callback) {
|
|
|
|
nextTickQueue.push(callback);
|
2010-04-13 15:39:15 -07:00
|
|
|
process._needTickCallback();
|
2010-01-18 10:27:27 -08:00
|
|
|
};
|
|
|
|
|
2010-04-20 18:22:51 -07:00
|
|
|
// Module System
|
|
|
|
var module = {}
|
|
|
|
process.compile("(function (exports) {"
|
|
|
|
+ process.binding("natives").module
|
|
|
|
+ "\n})", "module")(module);
|
2010-01-18 10:27:27 -08:00
|
|
|
|
2010-04-20 18:22:51 -07:00
|
|
|
// TODO: make sure that event module gets loaded here once it's
|
|
|
|
// factored out of module.js
|
|
|
|
// module.require("events");
|
2010-01-18 10:27:27 -08:00
|
|
|
|
2009-10-14 17:56:12 -04:00
|
|
|
// Signal Handlers
|
2010-05-14 09:59:31 +02:00
|
|
|
(function() {
|
|
|
|
var signalWatchers = {};
|
|
|
|
addListener = process.addListener,
|
|
|
|
removeListener = process.removeListener;
|
|
|
|
|
|
|
|
function isSignal (event) {
|
|
|
|
return event.slice(0, 3) === 'SIG' && process.hasOwnProperty(event);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Wrap addListener for the special signal types
|
2010-07-31 20:13:47 -04:00
|
|
|
process.on = process.addListener = function (type, listener) {
|
2010-05-14 09:59:31 +02:00
|
|
|
var ret = addListener.apply(this, arguments);
|
|
|
|
if (isSignal(type)) {
|
|
|
|
if (!signalWatchers.hasOwnProperty(type)) {
|
|
|
|
var b = process.binding('signal_watcher'),
|
|
|
|
w = new b.SignalWatcher(process[type]);
|
|
|
|
w.callback = function () {
|
|
|
|
process.emit(type);
|
|
|
|
}
|
|
|
|
signalWatchers[type] = w;
|
|
|
|
w.start();
|
|
|
|
} else if (this.listeners(type).length === 1) {
|
|
|
|
signalWatchers[event].start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2009-10-14 17:56:12 -04:00
|
|
|
|
2010-05-14 09:59:31 +02:00
|
|
|
process.removeListener = function (type, listener) {
|
|
|
|
var ret = removeListener.apply(this, arguments);
|
|
|
|
if (isSignal(type)) {
|
|
|
|
process.assert(signalWatchers.hasOwnProperty(type));
|
|
|
|
|
|
|
|
if (this.listeners(type).length === 0) {
|
|
|
|
signalWatchers[type].stop();
|
|
|
|
}
|
|
|
|
}
|
2009-10-14 17:56:12 -04:00
|
|
|
|
2010-05-14 09:59:31 +02:00
|
|
|
return ret;
|
2009-10-31 19:02:30 +01:00
|
|
|
}
|
2010-05-14 09:59:31 +02:00
|
|
|
})();
|
2009-10-14 17:56:12 -04:00
|
|
|
|
2009-06-08 15:30:10 +02:00
|
|
|
// Timers
|
2010-01-19 04:51:50 +11:00
|
|
|
function addTimerListener (callback) {
|
|
|
|
var timer = this;
|
|
|
|
// Special case the no param case to avoid the extra object creation.
|
|
|
|
if (arguments.length > 2) {
|
|
|
|
var args = Array.prototype.slice.call(arguments, 2);
|
2010-03-05 15:31:21 -08:00
|
|
|
timer.callback = function () { callback.apply(timer, args); };
|
2010-01-19 04:51:50 +11:00
|
|
|
} else {
|
2010-03-05 15:31:21 -08:00
|
|
|
timer.callback = callback;
|
2010-01-19 04:51:50 +11:00
|
|
|
}
|
|
|
|
}
|
2009-06-08 15:30:10 +02:00
|
|
|
|
2010-03-05 15:31:21 -08:00
|
|
|
global.setTimeout = function (callback, after) {
|
2009-10-29 23:34:10 +01:00
|
|
|
var timer = new process.Timer();
|
2010-01-19 04:51:50 +11:00
|
|
|
addTimerListener.apply(timer, arguments);
|
2009-06-25 20:25:44 +02:00
|
|
|
timer.start(after, 0);
|
2009-06-08 15:30:10 +02:00
|
|
|
return timer;
|
2009-12-16 17:33:33 -05:00
|
|
|
};
|
2009-06-08 15:30:10 +02:00
|
|
|
|
2010-03-05 15:31:21 -08:00
|
|
|
global.setInterval = function (callback, repeat) {
|
2009-10-29 23:34:10 +01:00
|
|
|
var timer = new process.Timer();
|
2010-01-19 04:51:50 +11:00
|
|
|
addTimerListener.apply(timer, arguments);
|
2009-06-25 20:25:44 +02:00
|
|
|
timer.start(repeat, repeat);
|
2009-06-08 15:30:10 +02:00
|
|
|
return timer;
|
2009-12-16 17:33:33 -05:00
|
|
|
};
|
2009-06-08 15:30:10 +02:00
|
|
|
|
2010-03-05 15:31:21 -08:00
|
|
|
global.clearTimeout = function (timer) {
|
2010-01-03 19:45:17 -05:00
|
|
|
if (timer instanceof process.Timer) {
|
|
|
|
timer.stop();
|
|
|
|
}
|
2009-12-16 17:33:33 -05:00
|
|
|
};
|
2009-06-08 15:30:10 +02:00
|
|
|
|
2010-03-05 15:31:21 -08:00
|
|
|
global.clearInterval = global.clearTimeout;
|
2009-10-31 19:02:30 +01:00
|
|
|
|
2010-03-15 15:11:40 -07:00
|
|
|
var stdout;
|
|
|
|
process.__defineGetter__('stdout', function () {
|
|
|
|
if (stdout) return stdout;
|
2010-04-20 00:22:59 +02:00
|
|
|
|
|
|
|
var binding = process.binding('stdio'),
|
|
|
|
net = module.requireNative('net'),
|
|
|
|
fs = module.requireNative('fs'),
|
|
|
|
fd = binding.stdoutFD;
|
|
|
|
|
|
|
|
if (binding.isStdoutBlocking()) {
|
2010-04-27 18:51:41 -07:00
|
|
|
stdout = new fs.WriteStream(null, {fd: fd});
|
2010-04-20 00:22:59 +02:00
|
|
|
} else {
|
|
|
|
stdout = new net.Stream(fd);
|
2010-04-27 18:45:10 -07:00
|
|
|
// FIXME Should probably have an option in net.Stream to create a stream from
|
|
|
|
// an existing fd which is writable only. But for now we'll just add
|
|
|
|
// this hack and set the `readable` member to false.
|
|
|
|
// Test: ./node test/fixtures/echo.js < /etc/passwd
|
|
|
|
stdout.readable = false;
|
2010-04-20 00:22:59 +02:00
|
|
|
}
|
2010-04-22 14:25:13 -07:00
|
|
|
|
2010-03-15 15:11:40 -07:00
|
|
|
return stdout;
|
|
|
|
});
|
|
|
|
|
|
|
|
var stdin;
|
|
|
|
process.openStdin = function () {
|
|
|
|
if (stdin) return stdin;
|
2010-04-28 01:17:28 +02:00
|
|
|
|
2010-05-20 15:21:40 -07:00
|
|
|
var binding = process.binding('stdio'),
|
|
|
|
net = module.requireNative('net'),
|
|
|
|
fs = module.requireNative('fs'),
|
|
|
|
fd = binding.openStdin();
|
2010-04-28 01:17:28 +02:00
|
|
|
|
2010-04-29 01:07:13 +02:00
|
|
|
if (binding.isStdinBlocking()) {
|
2010-05-20 22:11:26 -07:00
|
|
|
stdin = new fs.ReadStream(null, {fd: fd});
|
|
|
|
} else {
|
2010-04-28 01:17:28 +02:00
|
|
|
stdin = new net.Stream(fd);
|
|
|
|
stdin.readable = true;
|
|
|
|
}
|
|
|
|
|
2010-03-15 15:11:40 -07:00
|
|
|
stdin.resume();
|
2010-04-28 01:17:28 +02:00
|
|
|
|
2010-03-15 15:11:40 -07:00
|
|
|
return stdin;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-06-28 20:47:12 -07:00
|
|
|
// console object
|
2010-07-22 21:26:55 +10:00
|
|
|
var formatRegExp = /%[sdj]/g;
|
2010-06-28 20:47:12 -07:00
|
|
|
function format (f) {
|
2010-08-05 11:38:11 +02:00
|
|
|
if (typeof f !== 'string') {
|
|
|
|
var objects = [], sys = module.requireNative('sys');
|
|
|
|
for (var i = 0; i < arguments.length; i++) {
|
|
|
|
objects.push(sys.inspect(arguments[i]));
|
|
|
|
}
|
|
|
|
return objects.join(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-28 20:47:12 -07:00
|
|
|
var i = 1;
|
|
|
|
var args = arguments;
|
2010-08-05 10:15:22 -07:00
|
|
|
var str = String(f).replace(formatRegExp, function (x) {
|
2010-06-28 20:47:12 -07:00
|
|
|
switch (x) {
|
|
|
|
case '%s': return args[i++];
|
2010-07-22 21:26:55 +10:00
|
|
|
case '%d': return +args[i++];
|
2010-07-13 10:16:33 -07:00
|
|
|
case '%j': return JSON.stringify(args[i++]);
|
2010-06-28 20:47:12 -07:00
|
|
|
default:
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
});
|
2010-08-05 10:15:22 -07:00
|
|
|
for (var len = args.length; i < len; ++i) {
|
|
|
|
str += ' ' + args[i];
|
|
|
|
}
|
|
|
|
return str;
|
2010-06-28 20:47:12 -07:00
|
|
|
}
|
|
|
|
|
2010-06-23 17:40:51 -07:00
|
|
|
global.console = {};
|
|
|
|
|
2010-06-28 20:47:12 -07:00
|
|
|
global.console.log = function () {
|
|
|
|
process.stdout.write(format.apply(this, arguments) + '\n');
|
2010-06-23 17:40:51 -07:00
|
|
|
};
|
|
|
|
|
2010-06-28 20:47:12 -07:00
|
|
|
global.console.info = global.console.log;
|
|
|
|
|
|
|
|
global.console.warn = function () {
|
|
|
|
writeError(format.apply(this, arguments) + '\n');
|
|
|
|
};
|
|
|
|
|
|
|
|
global.console.error = global.console.warn;
|
|
|
|
|
2010-07-07 11:58:33 +02:00
|
|
|
global.console.dir = function(object){
|
|
|
|
var sys = module.requireNative('sys');
|
|
|
|
process.stdout.write(sys.inspect(object) + '\n');
|
2010-08-05 10:32:13 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
var times = {};
|
|
|
|
global.console.time = function(label){
|
|
|
|
times[label] = Date.now();
|
|
|
|
};
|
|
|
|
|
|
|
|
global.console.timeEnd = function(label){
|
|
|
|
var duration = Date.now() - times[label];
|
|
|
|
global.console.log('%s: %dms', label, duration);
|
|
|
|
};
|
2010-07-07 11:58:33 +02:00
|
|
|
|
2010-08-05 10:47:26 -07:00
|
|
|
global.console.trace = function(label){
|
|
|
|
// TODO probably can to do this better with V8's debug object once that is
|
|
|
|
// exposed.
|
|
|
|
var err = new Error;
|
|
|
|
err.name = 'Trace';
|
|
|
|
err.message = label || '';
|
|
|
|
Error.captureStackTrace(err, arguments.callee);
|
|
|
|
console.error(err.stack);
|
|
|
|
};
|
|
|
|
|
2010-07-07 11:58:33 +02:00
|
|
|
global.console.assert = function(expression){
|
|
|
|
if(!expression){
|
|
|
|
var arr = Array.prototype.slice.call(arguments, 1);
|
|
|
|
process.assert(false, format.apply(this, arr));
|
|
|
|
}
|
|
|
|
}
|
2010-06-23 17:40:51 -07:00
|
|
|
|
2010-07-27 10:58:45 -07:00
|
|
|
global.Buffer = module.requireNative('buffer').Buffer;
|
|
|
|
|
2009-09-28 16:50:19 +02:00
|
|
|
process.exit = function (code) {
|
|
|
|
process.emit("exit");
|
2009-10-29 23:34:10 +01:00
|
|
|
process.reallyExit(code);
|
2009-09-28 16:50:19 +02:00
|
|
|
};
|
|
|
|
|
2010-06-07 16:15:41 -07:00
|
|
|
var cwd = process.cwd();
|
|
|
|
var path = module.requireNative('path');
|
2009-09-18 15:45:47 +02:00
|
|
|
|
2010-06-07 16:15:41 -07:00
|
|
|
// Make process.argv[0] and process.argv[1] into full paths.
|
|
|
|
if (process.argv[0].indexOf('/') > 0) {
|
|
|
|
process.argv[0] = path.join(cwd, process.argv[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.argv[1]) {
|
|
|
|
if (process.argv[1].charAt(0) != "/" && !(/^http:\/\//).exec(process.argv[1])) {
|
|
|
|
process.argv[1] = path.join(cwd, process.argv[1]);
|
|
|
|
}
|
2009-10-31 19:02:30 +01:00
|
|
|
|
2010-07-01 11:10:22 -07:00
|
|
|
module.runMain();
|
2010-06-07 16:15:41 -07:00
|
|
|
} else {
|
|
|
|
// No arguments, run the repl
|
|
|
|
var repl = module.requireNative('repl');
|
2010-06-23 17:40:51 -07:00
|
|
|
console.log("Type '.help' for options.");
|
2010-06-07 16:15:41 -07:00
|
|
|
repl.start();
|
|
|
|
}
|
2009-09-07 14:09:18 +02:00
|
|
|
|
2009-11-23 00:59:36 +01:00
|
|
|
// All our arguments are loaded. We've evaluated all of the scripts. We
|
|
|
|
// might even have created TCP servers. Now we enter the main eventloop. If
|
|
|
|
// there are no watchers on the loop (except for the ones that were
|
|
|
|
// ev_unref'd) then this function exits. As long as there are active
|
|
|
|
// watchers, it blocks.
|
|
|
|
process.loop();
|
|
|
|
|
|
|
|
process.emit("exit");
|
|
|
|
|
2010-03-11 22:05:09 -08:00
|
|
|
});
|