2010-01-15 21:46:08 +01:00
|
|
|
var events = require('events');
|
|
|
|
|
2010-02-10 02:27:23 +01:00
|
|
|
exports.print = function () {
|
|
|
|
for (var i = 0, len = arguments.length; i < len; ++i) {
|
|
|
|
process.stdio.write(arguments[i]);
|
|
|
|
}
|
2009-10-13 13:26:00 +02:00
|
|
|
};
|
|
|
|
|
2010-02-10 02:27:23 +01:00
|
|
|
exports.puts = function () {
|
|
|
|
for (var i = 0, len = arguments.length; i < len; ++i) {
|
|
|
|
process.stdio.write(arguments[i] + '\n');
|
|
|
|
}
|
2009-10-13 13:26:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.debug = function (x) {
|
2009-11-28 12:15:37 +01:00
|
|
|
process.stdio.writeError("DEBUG: " + x + "\n");
|
2009-10-13 13:26:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.error = function (x) {
|
2010-02-10 02:27:23 +01:00
|
|
|
for (var i = 0, len = arguments.length; i < len; ++i) {
|
|
|
|
process.stdio.writeError(arguments[i] + '\n');
|
|
|
|
}
|
2009-10-13 13:26:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Echos the value of a value. Trys to print the value out
|
|
|
|
* in the best way possible given the different types.
|
2009-12-01 11:20:14 +01:00
|
|
|
*
|
2009-10-13 13:26:00 +02:00
|
|
|
* @param {Object} value The object to print out
|
2010-02-09 17:50:05 +01:00
|
|
|
* @param {Boolean} showHidden Flag that shows hidden (not enumerable) properties of objects.
|
2009-10-13 13:26:00 +02:00
|
|
|
*/
|
2010-02-09 17:50:05 +01:00
|
|
|
exports.inspect = function (obj, showHidden) {
|
|
|
|
var seen = [];
|
|
|
|
function format(value) {
|
|
|
|
// Primitive types cannot have properties
|
|
|
|
switch (typeof value) {
|
|
|
|
case 'undefined': return 'undefined';
|
|
|
|
case 'string': return JSON.stringify(value);
|
|
|
|
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.
|
2010-02-09 18:15:02 +01:00
|
|
|
var keys = showHidden ? Object.getOwnPropertyNames(value).map(function (key) {
|
2010-02-09 17:50:05 +01:00
|
|
|
return '' + key;
|
|
|
|
}) : Object.keys(value);
|
2010-02-09 18:15:02 +01:00
|
|
|
var visible_keys = Object.keys(value);
|
2010-02-09 17:50:05 +01:00
|
|
|
|
|
|
|
// Functions without properties can be shortcutted.
|
|
|
|
if (typeof value === 'function' && keys.length === 0) {
|
|
|
|
if (value instanceof RegExp) {
|
|
|
|
return '' + value;
|
|
|
|
} else {
|
|
|
|
return '[Function]';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-09 18:15:02 +01:00
|
|
|
var base, type, braces;
|
2010-02-09 17:50:05 +01:00
|
|
|
// 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 = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
seen.push(value);
|
|
|
|
|
|
|
|
if (keys.length === 0) {
|
|
|
|
return braces[0] + base + braces[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return braces[0] + base + "\n" + (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) {
|
|
|
|
str = format(value[key]);
|
|
|
|
} else {
|
|
|
|
str = '[Circular]';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (typeof name === 'undefined') {
|
|
|
|
if (type === 'Array' && key.match(/^\d+$/)) {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
name = JSON.stringify('' + key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return name + ": " + str;
|
|
|
|
}).join(",\n")).split("\n").map(function (line) {
|
|
|
|
return ' ' + line;
|
|
|
|
}).join('\n') + "\n" + braces[1];
|
|
|
|
}
|
|
|
|
return format(obj);
|
2009-10-13 13:26:00 +02:00
|
|
|
};
|
|
|
|
|
2010-02-10 02:27:23 +01:00
|
|
|
exports.p = function () {
|
|
|
|
for (var i = 0, len = arguments.length; i < len; ++i) {
|
|
|
|
exports.error(exports.inspect(arguments[i]));
|
|
|
|
}
|
2009-10-13 13:26:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.exec = function (command) {
|
2009-10-29 23:34:10 +01:00
|
|
|
var child = process.createChildProcess("/bin/sh", ["-c", command]);
|
2009-10-13 13:26:00 +02:00
|
|
|
var stdout = "";
|
|
|
|
var stderr = "";
|
2010-01-15 21:46:08 +01:00
|
|
|
var promise = new events.Promise();
|
2009-10-13 13:26:00 +02:00
|
|
|
|
|
|
|
child.addListener("output", function (chunk) {
|
2009-12-01 11:20:14 +01:00
|
|
|
if (chunk) stdout += chunk;
|
2009-10-13 13:26:00 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
child.addListener("error", function (chunk) {
|
2009-12-01 11:20:14 +01:00
|
|
|
if (chunk) stderr += chunk;
|
2009-10-13 13:26:00 +02:00
|
|
|
});
|
2009-12-01 11:20:14 +01:00
|
|
|
|
2009-10-13 13:26:00 +02:00
|
|
|
child.addListener("exit", function (code) {
|
|
|
|
if (code == 0) {
|
|
|
|
promise.emitSuccess(stdout, stderr);
|
|
|
|
} else {
|
|
|
|
promise.emitError(code, stdout, stderr);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
};
|
2009-11-07 14:45:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2009-12-06 11:36:22 +01:00
|
|
|
exports.inherits = process.inherits;
|
2009-12-01 11:20:14 +01:00
|
|
|
|