mirror of https://github.com/nodejs/node.git
Move constants out of process object
This commit is contained in:
parent
3def66ac3b
commit
6eca948ca2
1
TODO
1
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
|
||||
|
|
|
@ -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]);
|
||||
};
|
||||
|
||||
|
||||
|
|
33
lib/fs.js
33
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);
|
||||
}
|
||||
}
|
||||
|
|
41
src/node.cc
41
src/node.cc
|
@ -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;
|
||||
|
||||
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<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 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<Object> exports);
|
||||
|
||||
// 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)) {
|
||||
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<Value> 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
|
||||
|
|
27
src/node.js
27
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 () {
|
||||
|
|
|
@ -170,16 +170,8 @@ Handle<Value> ChildProcess::Kill(const Arguments& args) {
|
|||
if (args.Length() > 0) {
|
||||
if (args[0]->IsNumber()) {
|
||||
sig = args[0]->Int32Value();
|
||||
} else if (args[0]->IsString()) {
|
||||
Local<String> signame = args[0]->ToString();
|
||||
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();
|
||||
} else {
|
||||
return ThrowException(Exception::Error(String::New("Bad argument.")));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue