2010-01-15 21:46:08 +01:00
|
|
|
var events = require('events');
|
|
|
|
|
2009-10-13 13:26:00 +02:00
|
|
|
exports.print = function (x) {
|
2009-10-29 23:34:10 +01:00
|
|
|
process.stdio.write(x);
|
2009-10-13 13:26:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.puts = function (x) {
|
2009-11-28 12:15:37 +01:00
|
|
|
process.stdio.write(x + "\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) {
|
2009-11-28 12:15:37 +01:00
|
|
|
process.stdio.writeError(x + "\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
|
|
|
|
*/
|
|
|
|
exports.inspect = function (value) {
|
2009-12-01 11:20:14 +01:00
|
|
|
return formatter(value, '', []);
|
2009-10-13 13:26:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.p = function (x) {
|
|
|
|
exports.error(exports.inspect(x));
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* A recursive function to format an object - used by inspect.
|
|
|
|
*
|
|
|
|
* @param {Object} value
|
|
|
|
* the value to format
|
|
|
|
* @param {String} indent
|
|
|
|
* the indent level of any nested objects, since they are formatted over
|
|
|
|
* more than one line
|
|
|
|
* @param {Array} parents
|
|
|
|
* contains all objects above the current one in the heirachy, used to
|
|
|
|
* prevent getting stuck in a loop on circular references
|
|
|
|
*/
|
|
|
|
var formatter = function(value, indent, parents) {
|
|
|
|
switch(typeof(value)) {
|
2009-12-07 05:44:41 +01:00
|
|
|
case 'string': return JSON.stringify(value);
|
2009-12-01 11:20:14 +01:00
|
|
|
case 'number': return '' + value;
|
|
|
|
case 'function': return '[Function]';
|
|
|
|
case 'boolean': return '' + value;
|
|
|
|
case 'undefined': return 'undefined';
|
|
|
|
case 'object':
|
|
|
|
if (value == null) return 'null';
|
|
|
|
if (parents.indexOf(value) >= 0) return '[Circular]';
|
|
|
|
parents.push(value);
|
|
|
|
|
2009-12-31 18:41:35 +01:00
|
|
|
if (value instanceof Array && Object.keys(value).length === value.length) {
|
2009-12-01 11:20:14 +01:00
|
|
|
return formatObject(value, indent, parents, '[]', function(x, f) {
|
|
|
|
return f(value[x]);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return formatObject(value, indent, parents, '{}', function(x, f) {
|
2010-01-03 01:25:09 +01:00
|
|
|
var child;
|
|
|
|
if (value.__lookupGetter__(x)) {
|
|
|
|
if (value.__lookupSetter__(x)) {
|
2010-01-03 21:04:35 +01:00
|
|
|
child = "[Getter/Setter]";
|
2010-01-03 01:25:09 +01:00
|
|
|
} else {
|
2010-01-03 21:04:35 +01:00
|
|
|
child = "[Getter]";
|
2010-01-03 01:25:09 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (value.__lookupSetter__(x)) {
|
2010-01-03 21:04:35 +01:00
|
|
|
child = "[Setter]";
|
2010-01-03 01:25:09 +01:00
|
|
|
} else {
|
|
|
|
child = f(value[x]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return f(x) + ': ' + child;
|
2009-12-01 11:20:14 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
default:
|
|
|
|
throw('inspect unimplemented for ' + typeof(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for formatting either an array or an object, used internally by formatter
|
|
|
|
*/
|
|
|
|
var formatObject = function(obj, indent, parents, parenthesis, entryFormatter) {
|
|
|
|
var buffer = parenthesis[0];
|
|
|
|
var values = [];
|
2010-01-03 01:25:09 +01:00
|
|
|
var x;
|
2009-12-01 11:20:14 +01:00
|
|
|
|
|
|
|
var localFormatter = function(value) {
|
|
|
|
return formatter(value, indent + ' ', parents);
|
|
|
|
};
|
|
|
|
for (x in obj) {
|
|
|
|
values.push(indent + ' ' + entryFormatter(x, localFormatter));
|
|
|
|
}
|
|
|
|
if (values.length > 0) {
|
|
|
|
buffer += "\n" + values.join(",\n") + "\n" + indent;
|
|
|
|
}
|
|
|
|
buffer += parenthesis[1];
|
|
|
|
return buffer;
|
|
|
|
}
|