~james-page/ubuntu/precise/nodejs/0.6.x-merge

« back to all changes in this revision

Viewing changes to src/node.js

  • Committer: James Page
  • Date: 2012-03-30 12:09:16 UTC
  • mfrom: (7.1.23 sid)
  • Revision ID: james.page@canonical.com-20120330120916-40hfu9o00qr5t87b
* Merge from Debian unstable:
  - New upstream release (LP: #892034).
  - This package is x86/arm only. Update control to match
  - d/patches/2009_increase_test_timeout.patch: Increased default test
    timeout from 60 to 120 seconds to support reliable execution of all
    tests on armhf/armel architectures.
  - d/patches/2005_expected_failing_tests.patch: 
    - Allow racey tests to fail: test-cluster-kill-workers,
      test-child-process-fork2 
    - Allow test-fs-watch to fail as LP buildd's don't support
      inotify.
    - Revert all other Ubuntu changes as no longer required.
* Update Standards-Version to 3.9.3.
* Patch wscript to enable build on mipsel arch, libv8 being available.
  Upstream does not support that arch, failure expected.
* test-cluster-kill-workers is expected to fail on armhf,
  Bug#660802 will be closed when test pass.
* test-buffer is expected to fail on armel,
  Bug#660800 will be closed when test pass.
* Add epoch to dependency on libev >= 1:4.11. Closes: bug#658441.
* Remove tools/doc because node-doc-generator has no license for now.
* Add copyright for doc/sh* files (shjs).
* source.lintian-overrides : source-contains-waf-binary tools/node-waf
  it is simply not the case here.
* test-stream-pipe-multi expected to timeout sometimes on busy builds. 
* New upstream release.
* Remove upstream patches.
* test-dgram-pingpong expected to timeout, the test itself is buggy.
* test-buffer expected to fail on armel, allow building package to make
  it easier to find the cause of the failure.
  Closes: bug#639636.
* Expect tests dgram-multicast and broadcast to fail.
  debian/patches/2005_expected_failing_tests.patch
* Drop dpkg-source local-options: Defaults since dpkg-source 1.16.1.
* New upstream release.
* Depend on libev-dev 4.11, see bug#657080.
* Bump dependency on openssl to 1.0.0g.
* Remove useless uv_loop_refcount from libuv,
  refreshed 2009_fix_shared_ev.patch.
* Apply to upstream patches landed after 0.6.10 release,
  to fix debugger repl and http client.
* New upstream release. Closes:bug#650661
* Repackage to remove non-dfsg font files ./deps/npm/html/*/*.ttf
* Remove unneeded bundled dependencies: lighter tarball,
  debian/copyright is easier to maintain.
* Drop unneeded build-dependency on scons.
* Depend on zlib1g, libc-ares, libev.
  Patches done to support building with those shared libs.
* Fix DEB_UPSTREAM_URL in debian/rules, and debian/watch.
* nodejs.pc file for pkgconfig is no more available.
* Build-depend on procps package, a test is using /bin/ps.
* Refreshed debian/patches/2005_expected_failing_tests.patch,
  only for tests that need networking.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
(function(process) {
28
28
  global = this;
29
29
 
 
30
  var EventEmitter;
 
31
 
30
32
  function startup() {
 
33
    EventEmitter = NativeModule.require('events').EventEmitter;
 
34
    process.__proto__ = EventEmitter.prototype;
 
35
    process.EventEmitter = EventEmitter; // process.EventEmitter is deprecated
 
36
 
31
37
    startup.globalVariables();
32
38
    startup.globalTimeouts();
33
39
    startup.globalConsole();
38
44
    startup.processKillAndExit();
39
45
    startup.processSignalHandlers();
40
46
 
 
47
    startup.processChannel();
 
48
 
41
49
    startup.removedMethods();
42
50
 
43
51
    startup.resolveArgv0();
44
52
 
45
 
    if (startup.runThirdPartyMain()) {
46
 
      return;
47
 
    }
48
 
 
49
 
    if (startup.runDebugger()) {
50
 
      return;
51
 
    }
52
 
 
53
 
    if (startup.runScript()) {
54
 
      return;
55
 
    }
56
 
 
57
 
    if (startup.runEval()) {
58
 
      return;
59
 
    }
60
 
 
61
 
    startup.runRepl();
 
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.
 
57
 
 
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');
 
64
      });
 
65
 
 
66
    } else if (process.argv[1] == 'debug') {
 
67
      // Start the debugger agent
 
68
      var d = NativeModule.require('_debugger');
 
69
      d.start();
 
70
 
 
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();
 
76
 
 
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]);
 
86
 
 
87
      // If this is a worker in cluster mode, start up the communiction
 
88
      // channel.
 
89
      if (process.env.NODE_WORKER_ID) {
 
90
        var cluster = NativeModule.require('cluster');
 
91
        cluster._startWorker();
 
92
      }
 
93
 
 
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);
 
99
 
 
100
    } else {
 
101
      var Module = NativeModule.require('module');
 
102
 
 
103
      // If stdin is a TTY.
 
104
      if (NativeModule.require('tty').isatty(0)) {
 
105
        // REPL
 
106
        var repl = Module.requireRepl().start('> ', null, null, true);
 
107
 
 
108
      } else {
 
109
        // Read all of stdin - execute it.
 
110
        process.stdin.resume();
 
111
        process.stdin.setEncoding('utf8');
 
112
 
 
113
        var code = '';
 
114
        process.stdin.on('data', function(d) {
 
115
          code += d;
 
116
        });
 
117
 
 
118
        process.stdin.on('end', function() {
 
119
          new Module()._compile(code, '[stdin]');
 
120
        });
 
121
      }
 
122
    }
62
123
  }
63
124
 
64
125
  startup.globalVariables = function() {
92
153
  };
93
154
 
94
155
  startup.globalConsole = function() {
95
 
    global.console = NativeModule.require('console');
 
156
    global.__defineGetter__('console', function() {
 
157
      return NativeModule.require('console');
 
158
    });
96
159
  };
97
160
 
 
161
 
98
162
  startup._lazyConstants = null;
99
163
 
100
164
  startup.lazyConstants = function() {
121
185
      var l = nextTickQueue.length;
122
186
      if (l === 0) return;
123
187
 
 
188
      var q = nextTickQueue;
 
189
      nextTickQueue = [];
 
190
 
124
191
      try {
125
 
        for (var i = 0; i < l; i++) {
126
 
          nextTickQueue[i]();
127
 
        }
 
192
        for (var i = 0; i < l; i++) q[i]();
128
193
      }
129
194
      catch (e) {
130
 
        nextTickQueue.splice(0, i + 1);
131
195
        if (i + 1 < l) {
 
196
          nextTickQueue = q.slice(i + 1).concat(nextTickQueue);
 
197
        }
 
198
        if (nextTickQueue.length) {
132
199
          process._needTickCallback();
133
200
        }
134
201
        throw e; // process.nextTick error, or 'error' event on first tick
135
202
      }
136
 
 
137
 
      nextTickQueue.splice(0, l);
138
203
    };
139
204
 
140
205
    process.nextTick = function(callback) {
143
208
    };
144
209
  };
145
210
 
 
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;
 
217
    e.syscall = syscall;
 
218
    return e;
 
219
  }
 
220
 
 
221
  function createWritableStdioStream(fd) {
 
222
    var stream;
 
223
    var tty_wrap = process.binding('tty_wrap');
 
224
 
 
225
    // Note stream._type is used for test-module-load-list.js
 
226
 
 
227
    switch (tty_wrap.guessHandleType(fd)) {
 
228
      case 'TTY':
 
229
        var tty = NativeModule.require('tty');
 
230
        stream = new tty.WriteStream(fd);
 
231
        stream._type = 'tty';
 
232
 
 
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();
 
237
        }
 
238
        break;
 
239
 
 
240
      case 'FILE':
 
241
        var fs = NativeModule.require('fs');
 
242
        stream = new fs.SyncWriteStream(fd);
 
243
        stream._type = 'fs';
 
244
        break;
 
245
 
 
246
      case 'PIPE':
 
247
        var net = NativeModule.require('net');
 
248
        stream = new net.Stream(fd);
 
249
 
 
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';
 
256
 
 
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();
 
261
        }
 
262
        break;
 
263
 
 
264
      default:
 
265
        // Probably an error on in uv_guess_handle()
 
266
        throw new Error('Implement me. Unknown stream file type!');
 
267
    }
 
268
 
 
269
    // For supporting legacy API we put the FD here.
 
270
    stream.fd = fd;
 
271
 
 
272
    stream._isStdio = true;
 
273
 
 
274
    return stream;
 
275
  }
 
276
 
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');
151
 
 
152
 
    // process.stdout
153
 
 
154
 
    var fd = binding.stdoutFD;
155
 
 
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});
160
 
    } else {
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;
167
 
    }
168
 
 
169
 
    // process.stderr
170
 
 
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() { };
177
 
 
178
 
    // process.stdin
179
 
 
180
 
    var fd = binding.openStdin();
181
 
 
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});
186
 
    } else {
187
 
      process.stdin = new net.Stream(fd);
188
 
      process.stdin.readable = true;
189
 
    }
 
278
    var stdin, stdout, stderr;
 
279
 
 
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);
 
286
      };
 
287
      return stdout;
 
288
    });
 
289
 
 
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);
 
296
      };
 
297
      return stderr;
 
298
    });
 
299
 
 
300
    process.__defineGetter__('stdin', function() {
 
301
      if (stdin) return stdin;
 
302
 
 
303
      var tty_wrap = process.binding('tty_wrap');
 
304
      var fd = 0;
 
305
 
 
306
      switch (tty_wrap.guessHandleType(fd)) {
 
307
        case 'TTY':
 
308
          var tty = NativeModule.require('tty');
 
309
          stdin = new tty.ReadStream(fd);
 
310
          break;
 
311
 
 
312
        case 'FILE':
 
313
          var fs = NativeModule.require('fs');
 
314
          stdin = new fs.ReadStream(null, {fd: fd});
 
315
          break;
 
316
 
 
317
        case 'PIPE':
 
318
          var net = NativeModule.require('net');
 
319
          stdin = new net.Stream(fd);
 
320
          stdin.readable = true;
 
321
          break;
 
322
 
 
323
        default:
 
324
          // Probably an error on in uv_guess_handle()
 
325
          throw new Error('Implement me. Unknown stdin file type!');
 
326
      }
 
327
 
 
328
      // For supporting legacy API we put the FD here.
 
329
      stdin.fd = fd;
 
330
 
 
331
      // stdin starts out life in a paused state, but node doesn't
 
332
      // know yet.  Call pause() explicitly to unref() it.
 
333
      stdin.pause();
 
334
 
 
335
      return stdin;
 
336
    });
190
337
 
191
338
    process.openStdin = function() {
192
339
      process.stdin.resume();
195
342
  };
196
343
 
197
344
  startup.processKillAndExit = function() {
 
345
    var exiting = false;
 
346
 
198
347
    process.exit = function(code) {
199
 
      process.emit('exit', code || 0);
 
348
      if (!exiting) {
 
349
        exiting = true;
 
350
        process.emit('exit', code || 0);
 
351
      }
200
352
      process.reallyExit(code || 0);
201
353
    };
202
354
 
203
355
    process.kill = function(pid, sig) {
 
356
      var r;
 
357
 
204
358
      // preserve null signal
205
359
      if (0 === sig) {
206
 
        process._kill(pid, 0);
 
360
        r = process._kill(pid, 0);
207
361
      } else {
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]);
211
365
        } else {
212
366
          throw new Error('Unknown signal: ' + sig);
213
367
        }
214
368
      }
 
369
 
 
370
      if (r) {
 
371
        throw errnoException(errno, 'kill');
 
372
      }
215
373
    };
216
374
  };
217
375
 
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;
260
417
    };
261
418
  };
262
419
 
 
420
 
 
421
  startup.processChannel = function() {
 
422
    // If we were spawned with env NODE_CHANNEL_FD then load that up and
 
423
    // start parsing data from that stream.
 
424
    if (process.env.NODE_CHANNEL_FD) {
 
425
      assert(parseInt(process.env.NODE_CHANNEL_FD) >= 0);
 
426
      var cp = NativeModule.require('child_process');
 
427
 
 
428
      // Load tcp_wrap to avoid situation where we might immediately receive
 
429
      // a message.
 
430
      // FIXME is this really necessary?
 
431
      process.binding('tcp_wrap')
 
432
 
 
433
      cp._forkChild();
 
434
      assert(process.send);
 
435
    }
 
436
  }
 
437
 
263
438
  startup._removedProcessMethods = {
264
439
    'assert': 'process.assert() use require("assert").ok() instead',
265
440
    'debug': 'process.debug() use console.error() instead',
268
443
    'unwatchFile': 'process.unwatchFile() has moved to fs.unwatchFile()',
269
444
    'mixin': 'process.mixin() has been removed.',
270
445
    'createChildProcess': 'childProcess API has changed. See doc/api.txt.',
271
 
    'inherits': 'process.inherits() has moved to sys.inherits.',
272
 
    '_byteLength': 'process._byteLength() has moved to Buffer.byteLength',
 
446
    'inherits': 'process.inherits() has moved to util.inherits()',
 
447
    '_byteLength': 'process._byteLength() has moved to Buffer.byteLength'
273
448
  };
274
449
 
275
450
  startup.removedMethods = function() {
301
476
    }
302
477
  };
303
478
 
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')) {
309
 
      return;
310
 
    }
311
 
 
312
 
    process.nextTick(function() {
313
 
      NativeModule.require('_third_party_main');
314
 
    });
315
 
    return true;
316
 
  };
317
 
 
318
 
  startup.runDebugger = function() {
319
 
    if (!(process.argv[1] == 'debug')) {
320
 
      return;
321
 
    }
322
 
 
323
 
    // Start the debugger agent
324
 
    var d = NativeModule.require('_debugger');
325
 
    d.start();
326
 
    return true;
327
 
  };
328
 
 
329
 
  startup.runScript = function() {
330
 
    if (!process.argv[1]) {
331
 
      return;
332
 
    }
333
 
 
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]);
338
 
    }
339
 
 
340
 
    var Module = NativeModule.require('module');
341
 
 
342
 
    // REMOVEME: nextTick should not be necessary. This hack to get
343
 
    // test/simple/test-exception-handler2.js working.
344
 
    process.nextTick(Module.runMain);
345
 
 
346
 
    return true;
347
 
  };
348
 
 
349
 
  startup.runEval = function() {
350
 
    // -e, --eval
351
 
    if (!process._eval) {
352
 
      return;
353
 
    }
354
 
 
355
 
    var Module = NativeModule.require('module');
356
 
    var path = NativeModule.require('path');
357
 
    var cwd = process.cwd();
358
 
 
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');
363
 
    console.log(rv);
364
 
    return true;
365
 
  };
366
 
 
367
 
  startup.runRepl = function() {
368
 
    var Module = NativeModule.require('module');
369
 
    // REPL
370
 
    Module.requireRepl().start();
371
 
  };
372
 
 
373
 
 
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.
377
482
 
378
 
  var Script = process.binding('evals').Script;
 
483
  var Script = process.binding('evals').NodeScript;
379
484
  var runInThisContext = Script.runInThisContext;
380
485
 
381
486
  function NativeModule(id) {
402
507
      throw new Error('No such native module ' + id);
403
508
    }
404
509
 
 
510
    process.moduleLoadList.push('NativeModule ' + id);
 
511
 
405
512
    var nativeModule = new NativeModule(id);
406
513
 
407
514
    nativeModule.compile();