diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2010-11-22 01:20:22 +0300 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2010-11-22 01:20:22 +0300 |
commit | c11f3f8b0fa4378e0080f29a52bbe4c77070bea9 (patch) | |
tree | ccd7777ed2d1cc4c271f3a87eb015179404e8ff8 /src | |
parent | 6063ea62dfef7dff10b850a5bc8258a2d0df30f6 (diff) |
simplify lazy constant loading
Diffstat (limited to 'src')
-rw-r--r-- | src/node.js | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/src/node.js b/src/node.js index aba2eb70445..5baa6aad40f 100644 --- a/src/node.js +++ b/src/node.js @@ -29,6 +29,15 @@ process.assert = function (x, msg) { var writeError = process.binding('stdio').writeError; + +// lazy loaded. +var constants; +function lazyConstants () { + if (!constants) constants = process.binding("constants"); + return constants; +} + + // nextTick() var nextTickQueue = []; @@ -351,8 +360,7 @@ var module = (function () { try { process.mainModule.load(process.argv[1]); } catch (e) { - if (!constants) constants = process.binding("constants"); - if (e.errno == constants.ENOENT) { + if (e.errno == lazyConstants().ENOENT) { console.error("Cannot load '%s'", process.argv[1]); process.exit(1); } else { @@ -369,8 +377,6 @@ var module = (function () { // process.addListener. var events = module.requireNative('events'); -var constants; // lazy loaded. - // Signal Handlers (function() { var signalWatchers = {}; @@ -378,8 +384,7 @@ var constants; // lazy loaded. var removeListener = process.removeListener; function isSignal (event) { - if (!constants) constants = process.binding("constants"); - return event.slice(0, 3) === 'SIG' && constants[event]; + return event.slice(0, 3) === 'SIG' && lazyConstants()[event]; } // Wrap addListener for the special signal types @@ -387,9 +392,8 @@ var constants; // lazy loaded. var ret = addListener.apply(this, arguments); if (isSignal(type)) { if (!signalWatchers.hasOwnProperty(type)) { - if (!constants) constants = process.binding("constants"); var b = process.binding('signal_watcher'); - var w = new b.SignalWatcher(constants[type]); + var w = new b.SignalWatcher(lazyConstants()[type]); w.callback = function () { process.emit(type); }; signalWatchers[type] = w; w.start(); @@ -566,10 +570,9 @@ process.exit = function (code) { }; process.kill = function (pid, sig) { - if (!constants) constants = process.binding("constants"); sig = sig || 'SIGTERM'; - if (!constants[sig]) throw new Error("Unknown signal: " + sig); - process._kill(pid, constants[sig]); + if (!lazyConstants()[sig]) throw new Error("Unknown signal: " + sig); + process._kill(pid, lazyConstants()[sig]); }; |