Skip to content

Instantly share code, notes, and snippets.

@kanru

kanru/repl.js

Created Jan 10, 2012
Embed
What would you like to do?
JS repl
const repl = "JS> ";
(function Repl() {
let output;
let reader = {
onInputStreamReady : function(input) {
var sin = Cc['@mozilla.org/scriptableinputstream;1']
.createInstance(Ci.nsIScriptableInputStream);
sin.init(input);
try {
val = eval(sin.read(sin.available()));
ret = val ? val.toString()+'\n' : "undefined\n";
output.write(ret, ret.length);
} catch (e) {
output.write(e, e.length);
}
output.write(repl, repl.length);
var tm = Cc["@mozilla.org/thread-manager;1"].getService();
input.asyncWait(reader, 0, 0, tm.mainThread);
}
}
let listener = {
onSocketAccepted: function(serverSocket, clientSocket) {
dump("Accepted connection on "+clientSocket.host + '\n');
input = clientSocket.openInputStream(0, 0, 0)
.QueryInterface(Ci.nsIAsyncInputStream);
output = clientSocket.openOutputStream(Ci.nsITransport.OPEN_BLOCKING, 0, 0);
output.write(repl, repl.length);
var tm = Cc["@mozilla.org/thread-manager;1"].getService();
input.asyncWait(reader, 0, 0, tm.mainThread);
}
}
var serverSocket = Cc['@mozilla.org/network/server-socket;1']
.createInstance(Ci.nsIServerSocket);
serverSocket.init(31415, true, 5);
dump("Opened socket on " + serverSocket.port + '\n');
serverSocket.asyncListen(listener);
})();
@kanru

This comment has been minimized.

Copy link
Owner Author

@kanru kanru commented Jan 11, 2012

Put into shell.js and |adb forward tcp:31415 tcp:31415|

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.