diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2009-10-13 13:26:00 +0200 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2009-10-13 19:55:28 +0200 |
commit | 2b8ab7e24f2abc92474f44aad796b263582ab5d4 (patch) | |
tree | 6509a256352e278b46fe5b9e57d9d10e2f0254fa /lib/sys.js | |
parent | 0329468e739161e0b4af6648c588b17d70490571 (diff) | |
download | android-node-v8-2b8ab7e24f2abc92474f44aad796b263582ab5d4.tar.gz android-node-v8-2b8ab7e24f2abc92474f44aad796b263582ab5d4.tar.bz2 android-node-v8-2b8ab7e24f2abc92474f44aad796b263582ab5d4.zip |
utils.js links to sys.js instead of other way around
Diffstat (limited to 'lib/sys.js')
-rw-r--r--[l---------] | lib/sys.js | 70 |
1 files changed, 69 insertions, 1 deletions
diff --git a/lib/sys.js b/lib/sys.js index f59ba89fef..f85bb15fdb 120000..100644 --- a/lib/sys.js +++ b/lib/sys.js @@ -1 +1,69 @@ -utils.js
\ No newline at end of file +exports.print = function (x) { + node.stdio.write(x); +}; + +exports.puts = function (x) { + node.stdio.write(x.toString() + "\n"); +}; + +exports.debug = function (x) { + node.stdio.writeError("DEBUG: " + x.toString() + "\n"); +}; + +exports.error = function (x) { + node.stdio.writeError(x.toString() + "\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 + */ +exports.inspect = function (value) { + if (value === 0) return "0"; + if (value === false) return "false"; + if (value === "") return '""'; + if (typeof(value) == "function") return "[Function]"; + if (value === undefined) return; + + try { + return JSON.stringify(value); + } catch (e) { + // TODO make this recusrive and do a partial JSON output of object. + if (e.message.search("circular")) { + return "[Circular Object]"; + } else { + throw e; + } + } +}; + +exports.p = function (x) { + exports.error(exports.inspect(x)); +}; + +exports.exec = function (command) { + var child = node.createChildProcess("/bin/sh", ["-c", command]); + var stdout = ""; + var stderr = ""; + var promise = new node.Promise(); + + child.addListener("output", function (chunk) { + if (chunk) stdout += chunk; + }); + + child.addListener("error", function (chunk) { + if (chunk) stderr += chunk; + }); + + child.addListener("exit", function (code) { + if (code == 0) { + promise.emitSuccess(stdout, stderr); + } else { + promise.emitError(code, stdout, stderr); + } + }); + + return promise; +}; |