From 6eca948ca24338743afdfbcadeefa928701d1c07 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 16 Sep 2010 23:13:03 -0700 Subject: [PATCH] Move constants out of process object --- TODO | 1 - lib/child_process.js | 6 +++++- lib/fs.js | 33 ++++++++++++++++--------------- src/node.cc | 41 ++++++++++++++------------------------- src/node.js | 27 +++++++++++++++++--------- src/node_child_process.cc | 12 ++---------- 6 files changed, 57 insertions(+), 63 deletions(-) diff --git a/TODO b/TODO index 14f0fab40f..26cd78ba12 100644 --- a/TODO +++ b/TODO @@ -20,7 +20,6 @@ Test on Linux's /proc/sys/kernel/hostname - Ruby-like Process#detach (is that possible?) - stderr isn't flushing on exit -- Pull constants output process. - ReadStream should not use an offset in calls to fs.read (so that it can pull in files larger than 2G) - fs.readFile and fs.readFileSync need to not stat and prealloc a buffer diff --git a/lib/child_process.js b/lib/child_process.js index fb5c7fd8d7..a8076865cc 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -2,6 +2,7 @@ var inherits = require('sys').inherits; var EventEmitter = require('events').EventEmitter; var Stream = require('net').Stream; var InternalChildProcess = process.binding('child_process').ChildProcess; +var constants; var spawn = exports.spawn = function (path, args /*, options OR env, customFds */) { @@ -158,7 +159,10 @@ inherits(ChildProcess, EventEmitter); ChildProcess.prototype.kill = function (sig) { - return this._internal.kill(sig); + if (!constants) constants = process.binding("constants"); + sig = sig || 'SIGTERM'; + if (!constants[sig]) throw new Error("Unknown signal: " + sig); + return this._internal.kill(constants[sig]); }; diff --git a/lib/fs.js b/lib/fs.js index 8f09235b56..e12a82af04 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -3,6 +3,7 @@ var events = require('events'); var Buffer = require('buffer').Buffer; var binding = process.binding('fs'); +var constants = process.binding('constants'); var fs = exports; var kMinPoolSpace = 128; @@ -11,35 +12,35 @@ var kPoolSize = 40 * 1024; fs.Stats = binding.Stats; fs.Stats.prototype._checkModeProperty = function (property) { - return ((this.mode & process.S_IFMT) === property); + return ((this.mode & constants.S_IFMT) === property); }; fs.Stats.prototype.isDirectory = function () { - return this._checkModeProperty(process.S_IFDIR); + return this._checkModeProperty(constants.S_IFDIR); }; fs.Stats.prototype.isFile = function () { - return this._checkModeProperty(process.S_IFREG); + return this._checkModeProperty(constants.S_IFREG); }; fs.Stats.prototype.isBlockDevice = function () { - return this._checkModeProperty(process.S_IFBLK); + return this._checkModeProperty(constants.S_IFBLK); }; fs.Stats.prototype.isCharacterDevice = function () { - return this._checkModeProperty(process.S_IFCHR); + return this._checkModeProperty(constants.S_IFCHR); }; fs.Stats.prototype.isSymbolicLink = function () { - return this._checkModeProperty(process.S_IFLNK); + return this._checkModeProperty(constants.S_IFLNK); }; fs.Stats.prototype.isFIFO = function () { - return this._checkModeProperty(process.S_IFIFO); + return this._checkModeProperty(constants.S_IFIFO); }; fs.Stats.prototype.isSocket = function () { - return this._checkModeProperty(process.S_IFSOCK); + return this._checkModeProperty(constants.S_IFSOCK); }; fs.readFile = function (path, encoding_, callback) { @@ -48,7 +49,7 @@ fs.readFile = function (path, encoding_, callback) { var callback = (typeof(callback_) == 'function' ? callback_ : noop); binding.stat(path, function (err, stat) { if (err) { callback(err); return; } - binding.open(path, process.O_RDONLY, 0666, function (err, fd) { + binding.open(path, constants.O_RDONLY, 0666, function (err, fd) { if (err) { callback(err); return; } var size = stat.size; var buffer = new Buffer(size); @@ -91,7 +92,7 @@ fs.readFile = function (path, encoding_, callback) { }; fs.readFileSync = function (path, encoding) { - var fd = fs.openSync(path, process.O_RDONLY, 0666); + var fd = fs.openSync(path, constants.O_RDONLY, 0666); var stat = fs.statSync(path); var buffer = new Buffer(stat.size); var nread = 0; @@ -117,12 +118,12 @@ function stringToFlags(flag) { return flag; } switch (flag) { - case "r": return process.O_RDONLY; - case "r+": return process.O_RDWR; - case "w": return process.O_CREAT | process.O_TRUNC | process.O_WRONLY; - case "w+": return process.O_CREAT | process.O_TRUNC | process.O_RDWR; - case "a": return process.O_APPEND | process.O_CREAT | process.O_WRONLY; - case "a+": return process.O_APPEND | process.O_CREAT | process.O_RDWR; + case "r": return constants.O_RDONLY; + case "r+": return constants.O_RDWR; + case "w": return constants.O_CREAT | constants.O_TRUNC | constants.O_WRONLY; + case "w+": return constants.O_CREAT | constants.O_TRUNC | constants.O_RDWR; + case "a": return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY; + case "a+": return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR; default: throw new Error("Unknown file open flag: " + flag); } } diff --git a/src/node.cc b/src/node.cc index 63730c5269..31d9cb6503 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1237,40 +1237,23 @@ v8::Handle MemoryUsage(const v8::Arguments& args) { } -v8::Handle Kill(const v8::Arguments& args) { +Handle Kill(const Arguments& args) { HandleScope scope; - if (args.Length() < 1 || !args[0]->IsNumber()) { + if (args.Length() != 2 || !args[0]->IsNumber() || !args[1]->IsNumber()) { return ThrowException(Exception::Error(String::New("Bad argument."))); } pid_t pid = args[0]->IntegerValue(); - - int sig = SIGTERM; - - if (args.Length() >= 2) { - if (args[1]->IsNumber()) { - sig = args[1]->Int32Value(); - } else if (args[1]->IsString()) { - Local signame = args[1]->ToString(); - - Local sig_v = process->Get(signame); - if (!sig_v->IsNumber()) { - return ThrowException(Exception::Error(String::New("Unknown signal"))); - } - sig = sig_v->Int32Value(); - } - } - + int sig = args[1]->Int32Value(); int r = kill(pid, sig); - if (r != 0) { - return ThrowException(Exception::Error(String::New(strerror(errno)))); - } + if (r != 0) return ThrowException(ErrnoException(errno, "kill")); return Undefined(); } + typedef void (*extInit)(Handle exports); // DLOpen is node.dlopen(). Used to load 'module.node' dynamically shared @@ -1483,11 +1466,17 @@ static Handle Binding(const Arguments& args) { if (binding_cache->Has(module)) { exports = binding_cache->Get(module)->ToObject(); - } - else if ((modp = get_builtin_module(*module_v)) != NULL) { + + } else if ((modp = get_builtin_module(*module_v)) != NULL) { exports = Object::New(); modp->register_func(exports); binding_cache->Set(module, exports); + + } else if (!strcmp(*module_v, "constants")) { + exports = Object::New(); + DefineConstants(exports); + binding_cache->Set(module, exports); + } else if (!strcmp(*module_v, "natives")) { exports = Object::New(); // Explicitly define native sources. @@ -1516,6 +1505,7 @@ static Handle Binding(const Arguments& args) { exports->Set(String::New("string_decoder"), String::New(native_string_decoder)); binding_cache->Set(module, exports); } else { + return ThrowException(Exception::Error(String::New("No such module"))); } @@ -1639,7 +1629,7 @@ static void Load(int argc, char *argv[]) { NODE_SET_METHOD(process, "umask", Umask); NODE_SET_METHOD(process, "dlopen", DLOpen); - NODE_SET_METHOD(process, "kill", Kill); + NODE_SET_METHOD(process, "_kill", Kill); NODE_SET_METHOD(process, "memoryUsage", MemoryUsage); NODE_SET_METHOD(process, "binding", Binding); @@ -1655,7 +1645,6 @@ static void Load(int argc, char *argv[]) { //IdleWatcher::Initialize(process); // idle_watcher.cc Timer::Initialize(process); // timer.cc // coverity[stack_use_callee] - DefineConstants(process); // constants.cc // Compile, execute the src/node.js file. (Which was included as static C // string in node_natives.h. 'natve_node' is the string containing that diff --git a/src/node.js b/src/node.js index 179324bf66..6001ceefa2 100644 --- a/src/node.js +++ b/src/node.js @@ -98,6 +98,7 @@ function requireNative (id) { // process.addListener. var events = requireNative('events'); +var constants; // lazy loaded. // Signal Handlers (function() { @@ -106,28 +107,29 @@ var events = requireNative('events'); var removeListener = process.removeListener; function isSignal (event) { - return event.slice(0, 3) === 'SIG' && process.hasOwnProperty(event); - }; + if (!constants) constants = process.binding("constants"); + return event.slice(0, 3) === 'SIG' && constants[event]; + } // Wrap addListener for the special signal types process.on = process.addListener = function (type, listener) { 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); - } + if (!constants) constants = process.binding("constants"); + var b = process.binding('signal_watcher'); + var w = new b.SignalWatcher(constants[type]); + w.callback = function () { process.emit(type); }; signalWatchers[type] = w; w.start(); + } else if (this.listeners(type).length === 1) { signalWatchers[event].start(); } } return ret; - } + }; process.removeListener = function (type, listener) { var ret = removeListener.apply(this, arguments); @@ -140,7 +142,7 @@ var events = requireNative('events'); } return ret; - } + }; })(); // Timers @@ -304,6 +306,13 @@ process.exit = function (code) { process.reallyExit(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]); +}; + // Module System var module = (function () { diff --git a/src/node_child_process.cc b/src/node_child_process.cc index 2c12d23178..9201a91307 100644 --- a/src/node_child_process.cc +++ b/src/node_child_process.cc @@ -170,16 +170,8 @@ Handle ChildProcess::Kill(const Arguments& args) { if (args.Length() > 0) { if (args[0]->IsNumber()) { sig = args[0]->Int32Value(); - } else if (args[0]->IsString()) { - Local signame = args[0]->ToString(); - Local process = v8::Context::GetCurrent()->Global(); - Local node_obj = process->Get(String::NewSymbol("process"))->ToObject(); - - Local sig_v = node_obj->Get(signame); - if (!sig_v->IsNumber()) { - return ThrowException(Exception::Error(String::New("Unknown signal"))); - } - sig = sig_v->Int32Value(); + } else { + return ThrowException(Exception::Error(String::New("Bad argument."))); } }