38
44
startup.processKillAndExit();
39
45
startup.processSignalHandlers();
47
startup.processChannel();
41
49
startup.removedMethods();
43
51
startup.resolveArgv0();
45
if (startup.runThirdPartyMain()) {
49
if (startup.runDebugger()) {
53
if (startup.runScript()) {
57
if (startup.runEval()) {
53
// There are various modes that Node can run in. The most common two
54
// are running from a script and running the REPL - but there are a few
55
// others like the debugger or running --eval arguments. Here we decide
56
// which mode we run in.
58
if (NativeModule.exists('_third_party_main')) {
59
// To allow people to extend Node in different ways, this hook allows
60
// one to drop a file lib/_third_party_main.js into the build
61
// directory which will be executed instead of Node's normal loading.
62
process.nextTick(function() {
63
NativeModule.require('_third_party_main');
66
} else if (process.argv[1] == 'debug') {
67
// Start the debugger agent
68
var d = NativeModule.require('_debugger');
71
} else if (process._eval != null) {
72
// User passed '-e' or '--eval' arguments to Node.
73
var Module = NativeModule.require('module');
74
var path = NativeModule.require('path');
75
var cwd = process.cwd();
77
var module = new Module('eval');
78
module.filename = path.join(cwd, 'eval');
79
module.paths = Module._nodeModulePaths(cwd);
80
var result = module._compile('return eval(process._eval)', 'eval');
81
if (process._print_eval) console.log(result);
82
} else if (process.argv[1]) {
83
// make process.argv[1] into a full path
84
var path = NativeModule.require('path');
85
process.argv[1] = path.resolve(process.argv[1]);
87
// If this is a worker in cluster mode, start up the communiction
89
if (process.env.NODE_WORKER_ID) {
90
var cluster = NativeModule.require('cluster');
91
cluster._startWorker();
94
var Module = NativeModule.require('module');
95
// REMOVEME: nextTick should not be necessary. This hack to get
96
// test/simple/test-exception-handler2.js working.
97
// Main entry point into most programs:
98
process.nextTick(Module.runMain);
101
var Module = NativeModule.require('module');
103
// If stdin is a TTY.
104
if (NativeModule.require('tty').isatty(0)) {
106
var repl = Module.requireRepl().start('> ', null, null, true);
109
// Read all of stdin - execute it.
110
process.stdin.resume();
111
process.stdin.setEncoding('utf8');
114
process.stdin.on('data', function(d) {
118
process.stdin.on('end', function() {
119
new Module()._compile(code, '[stdin]');
64
125
startup.globalVariables = function() {
211
function errnoException(errorno, syscall) {
212
// TODO make this more compatible with ErrnoException from src/node.cc
213
// Once all of Node is using this function the ErrnoException from
214
// src/node.cc should be removed.
215
var e = new Error(syscall + ' ' + errorno);
216
e.errno = e.code = errorno;
221
function createWritableStdioStream(fd) {
223
var tty_wrap = process.binding('tty_wrap');
225
// Note stream._type is used for test-module-load-list.js
227
switch (tty_wrap.guessHandleType(fd)) {
229
var tty = NativeModule.require('tty');
230
stream = new tty.WriteStream(fd);
231
stream._type = 'tty';
233
// Hack to have stream not keep the event loop alive.
234
// See https://github.com/joyent/node/issues/1726
235
if (stream._handle && stream._handle.unref) {
236
stream._handle.unref();
241
var fs = NativeModule.require('fs');
242
stream = new fs.SyncWriteStream(fd);
247
var net = NativeModule.require('net');
248
stream = new net.Stream(fd);
250
// FIXME Should probably have an option in net.Stream to create a
251
// stream from an existing fd which is writable only. But for now
252
// we'll just add this hack and set the `readable` member to false.
253
// Test: ./node test/fixtures/echo.js < /etc/passwd
254
stream.readable = false;
255
stream._type = 'pipe';
257
// FIXME Hack to have stream not keep the event loop alive.
258
// See https://github.com/joyent/node/issues/1726
259
if (stream._handle && stream._handle.unref) {
260
stream._handle.unref();
265
// Probably an error on in uv_guess_handle()
266
throw new Error('Implement me. Unknown stream file type!');
269
// For supporting legacy API we put the FD here.
272
stream._isStdio = true;
146
277
startup.processStdio = function() {
147
var binding = process.binding('stdio'),
148
net = NativeModule.require('net'),
149
fs = NativeModule.require('fs'),
150
tty = NativeModule.require('tty');
154
var fd = binding.stdoutFD;
156
if (binding.isatty(fd)) {
157
process.stdout = new tty.WriteStream(fd);
158
} else if (binding.isStdoutBlocking()) {
159
process.stdout = new fs.WriteStream(null, {fd: fd});
161
process.stdout = new net.Stream(fd);
162
// FIXME Should probably have an option in net.Stream to create a
163
// stream from an existing fd which is writable only. But for now
164
// we'll just add this hack and set the `readable` member to false.
165
// Test: ./node test/fixtures/echo.js < /etc/passwd
166
process.stdout.readable = false;
171
var events = NativeModule.require('events');
172
var stderr = process.stderr = new events.EventEmitter();
173
stderr.writable = true;
174
stderr.readable = false;
175
stderr.write = process.binding('stdio').writeError;
176
stderr.end = stderr.destroy = stderr.destroySoon = function() { };
180
var fd = binding.openStdin();
182
if (binding.isatty(fd)) {
183
process.stdin = new tty.ReadStream(fd);
184
} else if (binding.isStdinBlocking()) {
185
process.stdin = new fs.ReadStream(null, {fd: fd});
187
process.stdin = new net.Stream(fd);
188
process.stdin.readable = true;
278
var stdin, stdout, stderr;
280
process.__defineGetter__('stdout', function() {
281
if (stdout) return stdout;
282
stdout = createWritableStdioStream(1);
283
stdout.destroy = stdout.destroySoon = function(er) {
284
er = er || new Error('process.stdout cannot be closed.');
285
stdout.emit('error', er);
290
process.__defineGetter__('stderr', function() {
291
if (stderr) return stderr;
292
stderr = createWritableStdioStream(2);
293
stderr.destroy = stderr.destroySoon = function(er) {
294
er = er || new Error('process.stderr cannot be closed.');
295
stderr.emit('error', er);
300
process.__defineGetter__('stdin', function() {
301
if (stdin) return stdin;
303
var tty_wrap = process.binding('tty_wrap');
306
switch (tty_wrap.guessHandleType(fd)) {
308
var tty = NativeModule.require('tty');
309
stdin = new tty.ReadStream(fd);
313
var fs = NativeModule.require('fs');
314
stdin = new fs.ReadStream(null, {fd: fd});
318
var net = NativeModule.require('net');
319
stdin = new net.Stream(fd);
320
stdin.readable = true;
324
// Probably an error on in uv_guess_handle()
325
throw new Error('Implement me. Unknown stdin file type!');
328
// For supporting legacy API we put the FD here.
331
// stdin starts out life in a paused state, but node doesn't
332
// know yet. Call pause() explicitly to unref() it.
191
338
process.openStdin = function() {
192
339
process.stdin.resume();
197
344
startup.processKillAndExit = function() {
198
347
process.exit = function(code) {
199
process.emit('exit', code || 0);
350
process.emit('exit', code || 0);
200
352
process.reallyExit(code || 0);
203
355
process.kill = function(pid, sig) {
204
358
// preserve null signal
206
process._kill(pid, 0);
360
r = process._kill(pid, 0);
208
362
sig = sig || 'SIGTERM';
209
363
if (startup.lazyConstants()[sig]) {
210
process._kill(pid, startup.lazyConstants()[sig]);
364
r = process._kill(pid, startup.lazyConstants()[sig]);
212
366
throw new Error('Unknown signal: ' + sig);
371
throw errnoException(errno, 'kill');
218
376
startup.processSignalHandlers = function() {
219
377
// Load events module in order to access prototype elements on process like
220
378
// process.addListener.
221
var events = NativeModule.require('events');
222
379
var signalWatchers = {};
223
380
var addListener = process.addListener;
224
381
var removeListener = process.removeListener;
304
startup.runThirdPartyMain = function() {
305
// To allow people to extend Node in different ways, this hook allows
306
// one to drop a file lib/_third_party_main.js into the build directory
307
// which will be executed instead of Node's normal loading.
308
if (!NativeModule.exists('_third_party_main')) {
312
process.nextTick(function() {
313
NativeModule.require('_third_party_main');
318
startup.runDebugger = function() {
319
if (!(process.argv[1] == 'debug')) {
323
// Start the debugger agent
324
var d = NativeModule.require('_debugger');
329
startup.runScript = function() {
330
if (!process.argv[1]) {
334
// make process.argv[1] into a full path
335
if (!(/^http:\/\//).exec(process.argv[1])) {
336
var path = NativeModule.require('path');
337
process.argv[1] = path.resolve(process.argv[1]);
340
var Module = NativeModule.require('module');
342
// REMOVEME: nextTick should not be necessary. This hack to get
343
// test/simple/test-exception-handler2.js working.
344
process.nextTick(Module.runMain);
349
startup.runEval = function() {
351
if (!process._eval) {
355
var Module = NativeModule.require('module');
356
var path = NativeModule.require('path');
357
var cwd = process.cwd();
359
var module = new Module('eval');
360
module.filename = path.join(cwd, 'eval');
361
module.paths = Module._nodeModulePaths(cwd);
362
var rv = module._compile('return eval(process._eval)', 'eval');
367
startup.runRepl = function() {
368
var Module = NativeModule.require('module');
370
Module.requireRepl().start();
374
479
// Below you find a minimal module system, which is used to load the node
375
480
// core modules found in lib/*.js. All core modules are compiled into the
376
481
// node binary, so they can be loaded faster.
378
var Script = process.binding('evals').Script;
483
var Script = process.binding('evals').NodeScript;
379
484
var runInThisContext = Script.runInThisContext;
381
486
function NativeModule(id) {