-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
/
Copy pathsys.js
251 lines (220 loc) Β· 6.98 KB
/
sys.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
var events = require('events');
exports.print = function () {
for (var i = 0, len = arguments.length; i < len; ++i) {
process.stdout.write(arguments[i]);
}
};
exports.puts = function () {
for (var i = 0, len = arguments.length; i < len; ++i) {
process.stdout.write(arguments[i] + '\n');
}
};
exports.debug = function (x) {
process.binding('stdio').writeError("DEBUG: " + x + "\n");
};
var error = exports.error = function (x) {
for (var i = 0, len = arguments.length; i < len; ++i) {
process.binding('stdio').writeError(arguments[i] + '\n');
}
};
/**
* Echos the value of a value. Trys to print the value out
* in the best way possible given the different types.
*
* @param {Object} value The object to print out
* @param {Boolean} showHidden Flag that shows hidden (not enumerable) properties of objects.
*/
exports.inspect = function (obj, showHidden, depth) {
var seen = [];
function format(value, recurseTimes) {
// Provide a hook for user-specified inspect functions.
if (value && typeof value.inspect === 'function') {
return value.inspect(recurseTimes);
}
// Primitive types cannot have properties
switch (typeof value) {
case 'undefined': return 'undefined';
case 'string': return JSON.stringify(value).replace(/'/g, "\\'").replace(/\\"/g, '"').replace(/(^"|"$)/g, "'");
case 'number': return '' + value;
case 'boolean': return '' + value;
}
// For some reason typeof null is "object", so special case here.
if (value === null) {
return 'null';
}
// Look up the keys of the object.
if (showHidden) {
var keys = Object.getOwnPropertyNames(value).map(function (key) { return '' + key; });
} else {
var keys = Object.keys(value);
}
var visible_keys = Object.keys(value);
// Functions without properties can be shortcutted.
if (typeof value === 'function' && keys.length === 0) {
if (value instanceof RegExp) {
return '' + value;
} else {
return '[Function]';
}
}
// Dates without properties can be shortcutted
if (value instanceof Date && keys.length === 0) {
return value.toUTCString();
}
var base, type, braces;
// Determine the object type
if (value instanceof Array) {
type = 'Array';
braces = ["[", "]"];
} else {
type = 'Object';
braces = ["{", "}"];
}
// Make functions say that they are functions
if (typeof value === 'function') {
base = (value instanceof RegExp) ? ' ' + value : ' [Function]';
} else {
base = "";
}
// Make dates with properties first say the date
if (value instanceof Date) {
base = ' ' + value.toUTCString();
}
seen.push(value);
if (keys.length === 0) {
return braces[0] + base + braces[1];
}
if (recurseTimes < 0) {
if (value instanceof RegExp) {
return '' + value;
} else {
return "[Object]";
}
}
output = keys.map(function (key) {
var name, str;
if (value.__lookupGetter__) {
if (value.__lookupGetter__(key)) {
if (value.__lookupSetter__(key)) {
str = "[Getter/Setter]";
} else {
str = "[Getter]";
}
} else {
if (value.__lookupSetter__(key)) {
str = "[Setter]";
}
}
}
if (visible_keys.indexOf(key) < 0) {
name = "[" + key + "]";
}
if (!str) {
if (seen.indexOf(value[key]) < 0) {
if ( recurseTimes === null) {
str = format(value[key]);
}
else {
str = format(value[key], recurseTimes - 1);
}
if (str.indexOf('\n') > -1) {
if (value instanceof Array) {
str = str.split('\n').map(function(line) {
return ' ' + line;
}).join('\n').substr(2);
}
else {
str = '\n' + str.split('\n').map(function(line) {
return ' ' + line;
}).join('\n');
}
}
} else {
str = '[Circular]';
}
}
if (typeof name === 'undefined') {
if (type === 'Array' && key.match(/^\d+$/)) {
return str;
}
name = JSON.stringify('' + key);
if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
name = name.substr(1, name.length-2);
}
else {
name = name.replace(/'/g, "\\'").replace(/\\"/g, '"').replace(/(^"|"$)/g, "'");
}
}
return name + ": " + str;
});
var numLinesEst = 0;
var length = output.reduce(function(prev, cur) {
numLinesEst++;
if( cur.indexOf('\n') >= 0 ) {
numLinesEst++;
}
return prev + cur.length + 1;
},0);
if (length > 50) {
output = braces[0] + (base === '' ? '' : base + '\n ') + ' ' + output.join('\n, ') + (numLinesEst > 1 ? '\n' : ' ') + braces[1];
}
else {
output = braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
}
return output;
}
return format(obj, (typeof depth === 'undefined' ? 2 : depth));
};
var pWarning;
exports.p = function () {
if (!pWarning) {
pWarning = "sys.p will be removed in future versions of Node. Use sys.puts(sys.inspect()) instead.\n";
exports.error(pWarning);
}
for (var i = 0, len = arguments.length; i < len; ++i) {
error(exports.inspect(arguments[i]));
}
};
function pad (n) {
return n < 10 ? '0' + n.toString(10) : n.toString(10);
}
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
// 26 Feb 16:19:34
function timestamp () {
var d = new Date();
return [ d.getDate()
, months[d.getMonth()]
, [pad(d.getHours()), pad(d.getMinutes()), pad(d.getSeconds())].join(':')
].join(' ');
}
exports.log = function (msg) {
exports.puts(timestamp() + ' - ' + msg.toString());
}
var execWarning;
exports.exec = function () {
if (!execWarning) {
execWarning = 'sys.exec has moved to the "child_process" module. Please update your source code.'
error(execWarning);
}
return require('child_process').exec.apply(this, arguments);
}
/**
* Inherit the prototype methods from one constructor into another.
*
* The Function.prototype.inherits from lang.js rewritten as a standalone
* function (not on Function.prototype). NOTE: If this file is to be loaded
* during bootstrapping this function needs to be revritten using some native
* functions as prototype setup using normal JavaScript does not work as
* expected during bootstrapping (see mirror.js in r114903).
*
* @param {function} ctor Constructor function which needs to inherit the
* prototype
* @param {function} superCtor Constructor function to inherit prototype from
*/
exports.inherits = function (ctor, superCtor) {
var tempCtor = function(){};
tempCtor.prototype = superCtor.prototype;
ctor.super_ = superCtor;
ctor.prototype = new tempCtor();
ctor.prototype.constructor = ctor;
};