Skip to content

Commit 6eca948

Browse files
committed
Move constants out of process object
1 parent 3def66a commit 6eca948

File tree

6 files changed

+57
-63
lines changed

6 files changed

+57
-63
lines changed

TODO

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
Test on Linux's /proc/sys/kernel/hostname
2121
- Ruby-like Process#detach (is that possible?)
2222
- stderr isn't flushing on exit
23-
- Pull constants output process.
2423
- ReadStream should not use an offset in calls to fs.read
2524
(so that it can pull in files larger than 2G)
2625
- fs.readFile and fs.readFileSync need to not stat and prealloc a buffer

lib/child_process.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var inherits = require('sys').inherits;
22
var EventEmitter = require('events').EventEmitter;
33
var Stream = require('net').Stream;
44
var InternalChildProcess = process.binding('child_process').ChildProcess;
5+
var constants;
56

67

78
var spawn = exports.spawn = function (path, args /*, options OR env, customFds */) {
@@ -158,7 +159,10 @@ inherits(ChildProcess, EventEmitter);
158159

159160

160161
ChildProcess.prototype.kill = function (sig) {
161-
return this._internal.kill(sig);
162+
if (!constants) constants = process.binding("constants");
163+
sig = sig || 'SIGTERM';
164+
if (!constants[sig]) throw new Error("Unknown signal: " + sig);
165+
return this._internal.kill(constants[sig]);
162166
};
163167

164168

lib/fs.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var events = require('events');
33
var Buffer = require('buffer').Buffer;
44

55
var binding = process.binding('fs');
6+
var constants = process.binding('constants');
67
var fs = exports;
78

89
var kMinPoolSpace = 128;
@@ -11,35 +12,35 @@ var kPoolSize = 40 * 1024;
1112
fs.Stats = binding.Stats;
1213

1314
fs.Stats.prototype._checkModeProperty = function (property) {
14-
return ((this.mode & process.S_IFMT) === property);
15+
return ((this.mode & constants.S_IFMT) === property);
1516
};
1617

1718
fs.Stats.prototype.isDirectory = function () {
18-
return this._checkModeProperty(process.S_IFDIR);
19+
return this._checkModeProperty(constants.S_IFDIR);
1920
};
2021

2122
fs.Stats.prototype.isFile = function () {
22-
return this._checkModeProperty(process.S_IFREG);
23+
return this._checkModeProperty(constants.S_IFREG);
2324
};
2425

2526
fs.Stats.prototype.isBlockDevice = function () {
26-
return this._checkModeProperty(process.S_IFBLK);
27+
return this._checkModeProperty(constants.S_IFBLK);
2728
};
2829

2930
fs.Stats.prototype.isCharacterDevice = function () {
30-
return this._checkModeProperty(process.S_IFCHR);
31+
return this._checkModeProperty(constants.S_IFCHR);
3132
};
3233

3334
fs.Stats.prototype.isSymbolicLink = function () {
34-
return this._checkModeProperty(process.S_IFLNK);
35+
return this._checkModeProperty(constants.S_IFLNK);
3536
};
3637

3738
fs.Stats.prototype.isFIFO = function () {
38-
return this._checkModeProperty(process.S_IFIFO);
39+
return this._checkModeProperty(constants.S_IFIFO);
3940
};
4041

4142
fs.Stats.prototype.isSocket = function () {
42-
return this._checkModeProperty(process.S_IFSOCK);
43+
return this._checkModeProperty(constants.S_IFSOCK);
4344
};
4445

4546
fs.readFile = function (path, encoding_, callback) {
@@ -48,7 +49,7 @@ fs.readFile = function (path, encoding_, callback) {
4849
var callback = (typeof(callback_) == 'function' ? callback_ : noop);
4950
binding.stat(path, function (err, stat) {
5051
if (err) { callback(err); return; }
51-
binding.open(path, process.O_RDONLY, 0666, function (err, fd) {
52+
binding.open(path, constants.O_RDONLY, 0666, function (err, fd) {
5253
if (err) { callback(err); return; }
5354
var size = stat.size;
5455
var buffer = new Buffer(size);
@@ -91,7 +92,7 @@ fs.readFile = function (path, encoding_, callback) {
9192
};
9293

9394
fs.readFileSync = function (path, encoding) {
94-
var fd = fs.openSync(path, process.O_RDONLY, 0666);
95+
var fd = fs.openSync(path, constants.O_RDONLY, 0666);
9596
var stat = fs.statSync(path);
9697
var buffer = new Buffer(stat.size);
9798
var nread = 0;
@@ -117,12 +118,12 @@ function stringToFlags(flag) {
117118
return flag;
118119
}
119120
switch (flag) {
120-
case "r": return process.O_RDONLY;
121-
case "r+": return process.O_RDWR;
122-
case "w": return process.O_CREAT | process.O_TRUNC | process.O_WRONLY;
123-
case "w+": return process.O_CREAT | process.O_TRUNC | process.O_RDWR;
124-
case "a": return process.O_APPEND | process.O_CREAT | process.O_WRONLY;
125-
case "a+": return process.O_APPEND | process.O_CREAT | process.O_RDWR;
121+
case "r": return constants.O_RDONLY;
122+
case "r+": return constants.O_RDWR;
123+
case "w": return constants.O_CREAT | constants.O_TRUNC | constants.O_WRONLY;
124+
case "w+": return constants.O_CREAT | constants.O_TRUNC | constants.O_RDWR;
125+
case "a": return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY;
126+
case "a+": return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR;
126127
default: throw new Error("Unknown file open flag: " + flag);
127128
}
128129
}

src/node.cc

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,40 +1237,23 @@ v8::Handle<v8::Value> MemoryUsage(const v8::Arguments& args) {
12371237
}
12381238

12391239

1240-
v8::Handle<v8::Value> Kill(const v8::Arguments& args) {
1240+
Handle<Value> Kill(const Arguments& args) {
12411241
HandleScope scope;
12421242

1243-
if (args.Length() < 1 || !args[0]->IsNumber()) {
1243+
if (args.Length() != 2 || !args[0]->IsNumber() || !args[1]->IsNumber()) {
12441244
return ThrowException(Exception::Error(String::New("Bad argument.")));
12451245
}
12461246

12471247
pid_t pid = args[0]->IntegerValue();
1248-
1249-
int sig = SIGTERM;
1250-
1251-
if (args.Length() >= 2) {
1252-
if (args[1]->IsNumber()) {
1253-
sig = args[1]->Int32Value();
1254-
} else if (args[1]->IsString()) {
1255-
Local<String> signame = args[1]->ToString();
1256-
1257-
Local<Value> sig_v = process->Get(signame);
1258-
if (!sig_v->IsNumber()) {
1259-
return ThrowException(Exception::Error(String::New("Unknown signal")));
1260-
}
1261-
sig = sig_v->Int32Value();
1262-
}
1263-
}
1264-
1248+
int sig = args[1]->Int32Value();
12651249
int r = kill(pid, sig);
12661250

1267-
if (r != 0) {
1268-
return ThrowException(Exception::Error(String::New(strerror(errno))));
1269-
}
1251+
if (r != 0) return ThrowException(ErrnoException(errno, "kill"));
12701252

12711253
return Undefined();
12721254
}
12731255

1256+
12741257
typedef void (*extInit)(Handle<Object> exports);
12751258

12761259
// DLOpen is node.dlopen(). Used to load 'module.node' dynamically shared
@@ -1483,11 +1466,17 @@ static Handle<Value> Binding(const Arguments& args) {
14831466

14841467
if (binding_cache->Has(module)) {
14851468
exports = binding_cache->Get(module)->ToObject();
1486-
}
1487-
else if ((modp = get_builtin_module(*module_v)) != NULL) {
1469+
1470+
} else if ((modp = get_builtin_module(*module_v)) != NULL) {
14881471
exports = Object::New();
14891472
modp->register_func(exports);
14901473
binding_cache->Set(module, exports);
1474+
1475+
} else if (!strcmp(*module_v, "constants")) {
1476+
exports = Object::New();
1477+
DefineConstants(exports);
1478+
binding_cache->Set(module, exports);
1479+
14911480
} else if (!strcmp(*module_v, "natives")) {
14921481
exports = Object::New();
14931482
// Explicitly define native sources.
@@ -1516,6 +1505,7 @@ static Handle<Value> Binding(const Arguments& args) {
15161505
exports->Set(String::New("string_decoder"), String::New(native_string_decoder));
15171506
binding_cache->Set(module, exports);
15181507
} else {
1508+
15191509
return ThrowException(Exception::Error(String::New("No such module")));
15201510
}
15211511

@@ -1639,7 +1629,7 @@ static void Load(int argc, char *argv[]) {
16391629

16401630
NODE_SET_METHOD(process, "umask", Umask);
16411631
NODE_SET_METHOD(process, "dlopen", DLOpen);
1642-
NODE_SET_METHOD(process, "kill", Kill);
1632+
NODE_SET_METHOD(process, "_kill", Kill);
16431633
NODE_SET_METHOD(process, "memoryUsage", MemoryUsage);
16441634

16451635
NODE_SET_METHOD(process, "binding", Binding);
@@ -1655,7 +1645,6 @@ static void Load(int argc, char *argv[]) {
16551645
//IdleWatcher::Initialize(process); // idle_watcher.cc
16561646
Timer::Initialize(process); // timer.cc
16571647
// coverity[stack_use_callee]
1658-
DefineConstants(process); // constants.cc
16591648

16601649
// Compile, execute the src/node.js file. (Which was included as static C
16611650
// string in node_natives.h. 'natve_node' is the string containing that

src/node.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ function requireNative (id) {
9898
// process.addListener.
9999
var events = requireNative('events');
100100

101+
var constants; // lazy loaded.
101102

102103
// Signal Handlers
103104
(function() {
@@ -106,28 +107,29 @@ var events = requireNative('events');
106107
var removeListener = process.removeListener;
107108

108109
function isSignal (event) {
109-
return event.slice(0, 3) === 'SIG' && process.hasOwnProperty(event);
110-
};
110+
if (!constants) constants = process.binding("constants");
111+
return event.slice(0, 3) === 'SIG' && constants[event];
112+
}
111113

112114
// Wrap addListener for the special signal types
113115
process.on = process.addListener = function (type, listener) {
114116
var ret = addListener.apply(this, arguments);
115117
if (isSignal(type)) {
116118
if (!signalWatchers.hasOwnProperty(type)) {
117-
var b = process.binding('signal_watcher'),
118-
w = new b.SignalWatcher(process[type]);
119-
w.callback = function () {
120-
process.emit(type);
121-
}
119+
if (!constants) constants = process.binding("constants");
120+
var b = process.binding('signal_watcher');
121+
var w = new b.SignalWatcher(constants[type]);
122+
w.callback = function () { process.emit(type); };
122123
signalWatchers[type] = w;
123124
w.start();
125+
124126
} else if (this.listeners(type).length === 1) {
125127
signalWatchers[event].start();
126128
}
127129
}
128130

129131
return ret;
130-
}
132+
};
131133

132134
process.removeListener = function (type, listener) {
133135
var ret = removeListener.apply(this, arguments);
@@ -140,7 +142,7 @@ var events = requireNative('events');
140142
}
141143

142144
return ret;
143-
}
145+
};
144146
})();
145147

146148
// Timers
@@ -304,6 +306,13 @@ process.exit = function (code) {
304306
process.reallyExit(code);
305307
};
306308

309+
process.kill = function (pid, sig) {
310+
if (!constants) constants = process.binding("constants");
311+
sig = sig || 'SIGTERM';
312+
if (!constants[sig]) throw new Error("Unknown signal: " + sig);
313+
process._kill(pid, constants[sig]);
314+
};
315+
307316

308317
// Module System
309318
var module = (function () {

src/node_child_process.cc

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,8 @@ Handle<Value> ChildProcess::Kill(const Arguments& args) {
170170
if (args.Length() > 0) {
171171
if (args[0]->IsNumber()) {
172172
sig = args[0]->Int32Value();
173-
} else if (args[0]->IsString()) {
174-
Local<String> signame = args[0]->ToString();
175-
Local<Object> process = v8::Context::GetCurrent()->Global();
176-
Local<Object> node_obj = process->Get(String::NewSymbol("process"))->ToObject();
177-
178-
Local<Value> sig_v = node_obj->Get(signame);
179-
if (!sig_v->IsNumber()) {
180-
return ThrowException(Exception::Error(String::New("Unknown signal")));
181-
}
182-
sig = sig_v->Int32Value();
173+
} else {
174+
return ThrowException(Exception::Error(String::New("Bad argument.")));
183175
}
184176
}
185177

0 commit comments

Comments
 (0)