Move constants out of process object

This commit is contained in:
Ryan Dahl 2010-09-16 23:13:03 -07:00
parent 3def66ac3b
commit 6eca948ca2
6 changed files with 57 additions and 63 deletions

1
TODO
View File

@ -20,7 +20,6 @@
Test on Linux's /proc/sys/kernel/hostname Test on Linux's /proc/sys/kernel/hostname
- Ruby-like Process#detach (is that possible?) - Ruby-like Process#detach (is that possible?)
- stderr isn't flushing on exit - stderr isn't flushing on exit
- Pull constants output process.
- ReadStream should not use an offset in calls to fs.read - ReadStream should not use an offset in calls to fs.read
(so that it can pull in files larger than 2G) (so that it can pull in files larger than 2G)
- fs.readFile and fs.readFileSync need to not stat and prealloc a buffer - fs.readFile and fs.readFileSync need to not stat and prealloc a buffer

View File

@ -2,6 +2,7 @@ var inherits = require('sys').inherits;
var EventEmitter = require('events').EventEmitter; var EventEmitter = require('events').EventEmitter;
var Stream = require('net').Stream; var Stream = require('net').Stream;
var InternalChildProcess = process.binding('child_process').ChildProcess; var InternalChildProcess = process.binding('child_process').ChildProcess;
var constants;
var spawn = exports.spawn = function (path, args /*, options OR env, customFds */) { var spawn = exports.spawn = function (path, args /*, options OR env, customFds */) {
@ -158,7 +159,10 @@ inherits(ChildProcess, EventEmitter);
ChildProcess.prototype.kill = function (sig) { 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]);
}; };

View File

@ -3,6 +3,7 @@ var events = require('events');
var Buffer = require('buffer').Buffer; var Buffer = require('buffer').Buffer;
var binding = process.binding('fs'); var binding = process.binding('fs');
var constants = process.binding('constants');
var fs = exports; var fs = exports;
var kMinPoolSpace = 128; var kMinPoolSpace = 128;
@ -11,35 +12,35 @@ var kPoolSize = 40 * 1024;
fs.Stats = binding.Stats; fs.Stats = binding.Stats;
fs.Stats.prototype._checkModeProperty = function (property) { 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 () { fs.Stats.prototype.isDirectory = function () {
return this._checkModeProperty(process.S_IFDIR); return this._checkModeProperty(constants.S_IFDIR);
}; };
fs.Stats.prototype.isFile = function () { fs.Stats.prototype.isFile = function () {
return this._checkModeProperty(process.S_IFREG); return this._checkModeProperty(constants.S_IFREG);
}; };
fs.Stats.prototype.isBlockDevice = function () { fs.Stats.prototype.isBlockDevice = function () {
return this._checkModeProperty(process.S_IFBLK); return this._checkModeProperty(constants.S_IFBLK);
}; };
fs.Stats.prototype.isCharacterDevice = function () { fs.Stats.prototype.isCharacterDevice = function () {
return this._checkModeProperty(process.S_IFCHR); return this._checkModeProperty(constants.S_IFCHR);
}; };
fs.Stats.prototype.isSymbolicLink = function () { fs.Stats.prototype.isSymbolicLink = function () {
return this._checkModeProperty(process.S_IFLNK); return this._checkModeProperty(constants.S_IFLNK);
}; };
fs.Stats.prototype.isFIFO = function () { fs.Stats.prototype.isFIFO = function () {
return this._checkModeProperty(process.S_IFIFO); return this._checkModeProperty(constants.S_IFIFO);
}; };
fs.Stats.prototype.isSocket = function () { fs.Stats.prototype.isSocket = function () {
return this._checkModeProperty(process.S_IFSOCK); return this._checkModeProperty(constants.S_IFSOCK);
}; };
fs.readFile = function (path, encoding_, callback) { fs.readFile = function (path, encoding_, callback) {
@ -48,7 +49,7 @@ fs.readFile = function (path, encoding_, callback) {
var callback = (typeof(callback_) == 'function' ? callback_ : noop); var callback = (typeof(callback_) == 'function' ? callback_ : noop);
binding.stat(path, function (err, stat) { binding.stat(path, function (err, stat) {
if (err) { callback(err); return; } 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; } if (err) { callback(err); return; }
var size = stat.size; var size = stat.size;
var buffer = new Buffer(size); var buffer = new Buffer(size);
@ -91,7 +92,7 @@ fs.readFile = function (path, encoding_, callback) {
}; };
fs.readFileSync = function (path, encoding) { 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 stat = fs.statSync(path);
var buffer = new Buffer(stat.size); var buffer = new Buffer(stat.size);
var nread = 0; var nread = 0;
@ -117,12 +118,12 @@ function stringToFlags(flag) {
return flag; return flag;
} }
switch (flag) { switch (flag) {
case "r": return process.O_RDONLY; case "r": return constants.O_RDONLY;
case "r+": return process.O_RDWR; case "r+": return constants.O_RDWR;
case "w": return process.O_CREAT | process.O_TRUNC | process.O_WRONLY; case "w": return constants.O_CREAT | constants.O_TRUNC | constants.O_WRONLY;
case "w+": return process.O_CREAT | process.O_TRUNC | process.O_RDWR; case "w+": return constants.O_CREAT | constants.O_TRUNC | constants.O_RDWR;
case "a": return process.O_APPEND | process.O_CREAT | process.O_WRONLY; case "a": return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY;
case "a+": return process.O_APPEND | process.O_CREAT | process.O_RDWR; case "a+": return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR;
default: throw new Error("Unknown file open flag: " + flag); default: throw new Error("Unknown file open flag: " + flag);
} }
} }

View File

@ -1237,40 +1237,23 @@ v8::Handle<v8::Value> MemoryUsage(const v8::Arguments& args) {
} }
v8::Handle<v8::Value> Kill(const v8::Arguments& args) { Handle<Value> Kill(const Arguments& args) {
HandleScope scope; 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."))); return ThrowException(Exception::Error(String::New("Bad argument.")));
} }
pid_t pid = args[0]->IntegerValue(); pid_t pid = args[0]->IntegerValue();
int sig = args[1]->Int32Value();
int sig = SIGTERM;
if (args.Length() >= 2) {
if (args[1]->IsNumber()) {
sig = args[1]->Int32Value();
} else if (args[1]->IsString()) {
Local<String> signame = args[1]->ToString();
Local<Value> sig_v = process->Get(signame);
if (!sig_v->IsNumber()) {
return ThrowException(Exception::Error(String::New("Unknown signal")));
}
sig = sig_v->Int32Value();
}
}
int r = kill(pid, sig); int r = kill(pid, sig);
if (r != 0) { if (r != 0) return ThrowException(ErrnoException(errno, "kill"));
return ThrowException(Exception::Error(String::New(strerror(errno))));
}
return Undefined(); return Undefined();
} }
typedef void (*extInit)(Handle<Object> exports); typedef void (*extInit)(Handle<Object> exports);
// DLOpen is node.dlopen(). Used to load 'module.node' dynamically shared // DLOpen is node.dlopen(). Used to load 'module.node' dynamically shared
@ -1483,11 +1466,17 @@ static Handle<Value> Binding(const Arguments& args) {
if (binding_cache->Has(module)) { if (binding_cache->Has(module)) {
exports = binding_cache->Get(module)->ToObject(); 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(); exports = Object::New();
modp->register_func(exports); modp->register_func(exports);
binding_cache->Set(module, 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")) { } else if (!strcmp(*module_v, "natives")) {
exports = Object::New(); exports = Object::New();
// Explicitly define native sources. // Explicitly define native sources.
@ -1516,6 +1505,7 @@ static Handle<Value> Binding(const Arguments& args) {
exports->Set(String::New("string_decoder"), String::New(native_string_decoder)); exports->Set(String::New("string_decoder"), String::New(native_string_decoder));
binding_cache->Set(module, exports); binding_cache->Set(module, exports);
} else { } else {
return ThrowException(Exception::Error(String::New("No such module"))); 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, "umask", Umask);
NODE_SET_METHOD(process, "dlopen", DLOpen); 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, "memoryUsage", MemoryUsage);
NODE_SET_METHOD(process, "binding", Binding); NODE_SET_METHOD(process, "binding", Binding);
@ -1655,7 +1645,6 @@ static void Load(int argc, char *argv[]) {
//IdleWatcher::Initialize(process); // idle_watcher.cc //IdleWatcher::Initialize(process); // idle_watcher.cc
Timer::Initialize(process); // timer.cc Timer::Initialize(process); // timer.cc
// coverity[stack_use_callee] // coverity[stack_use_callee]
DefineConstants(process); // constants.cc
// Compile, execute the src/node.js file. (Which was included as static C // 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 // string in node_natives.h. 'natve_node' is the string containing that

View File

@ -98,6 +98,7 @@ function requireNative (id) {
// process.addListener. // process.addListener.
var events = requireNative('events'); var events = requireNative('events');
var constants; // lazy loaded.
// Signal Handlers // Signal Handlers
(function() { (function() {
@ -106,28 +107,29 @@ var events = requireNative('events');
var removeListener = process.removeListener; var removeListener = process.removeListener;
function isSignal (event) { 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 // Wrap addListener for the special signal types
process.on = process.addListener = function (type, listener) { process.on = process.addListener = function (type, listener) {
var ret = addListener.apply(this, arguments); var ret = addListener.apply(this, arguments);
if (isSignal(type)) { if (isSignal(type)) {
if (!signalWatchers.hasOwnProperty(type)) { if (!signalWatchers.hasOwnProperty(type)) {
var b = process.binding('signal_watcher'), if (!constants) constants = process.binding("constants");
w = new b.SignalWatcher(process[type]); var b = process.binding('signal_watcher');
w.callback = function () { var w = new b.SignalWatcher(constants[type]);
process.emit(type); w.callback = function () { process.emit(type); };
}
signalWatchers[type] = w; signalWatchers[type] = w;
w.start(); w.start();
} else if (this.listeners(type).length === 1) { } else if (this.listeners(type).length === 1) {
signalWatchers[event].start(); signalWatchers[event].start();
} }
} }
return ret; return ret;
} };
process.removeListener = function (type, listener) { process.removeListener = function (type, listener) {
var ret = removeListener.apply(this, arguments); var ret = removeListener.apply(this, arguments);
@ -140,7 +142,7 @@ var events = requireNative('events');
} }
return ret; return ret;
} };
})(); })();
// Timers // Timers
@ -304,6 +306,13 @@ process.exit = function (code) {
process.reallyExit(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 // Module System
var module = (function () { var module = (function () {

View File

@ -170,16 +170,8 @@ Handle<Value> ChildProcess::Kill(const Arguments& args) {
if (args.Length() > 0) { if (args.Length() > 0) {
if (args[0]->IsNumber()) { if (args[0]->IsNumber()) {
sig = args[0]->Int32Value(); sig = args[0]->Int32Value();
} else if (args[0]->IsString()) { } else {
Local<String> signame = args[0]->ToString(); return ThrowException(Exception::Error(String::New("Bad argument.")));
Local<Object> process = v8::Context::GetCurrent()->Global();
Local<Object> node_obj = process->Get(String::NewSymbol("process"))->ToObject();
Local<Value> sig_v = node_obj->Get(signame);
if (!sig_v->IsNumber()) {
return ThrowException(Exception::Error(String::New("Unknown signal")));
}
sig = sig_v->Int32Value();
} }
} }