var net = require('net'),
repl = require('repl'),
vm = require('vm'),
- inherits = require('util').inherits,
+ util = require('util'),
+ inherits = util.inherits,
spawn = require('child_process').spawn;
exports.port = 5858;
});
};
+Client.prototype.setBreakpoint = function(req, cb) {
+ var req = {
+ command: 'setbreakpoint',
+ arguments: req
+ };
+
+ this.req(req, function(res) {
+ if (cb) cb(res);
+ });
+};
+
Client.prototype.reqSource = function(from, to, cb) {
var req = {
command: 'source',
};
function defineProperty(key, protoKey) {
- Object.defineProperty(self.repl.context, key, {
- get: proto[protoKey].bind(self),
- enumerable: true
- });
+ // Check arity
+ if (proto[protoKey].length === 0) {
+ Object.defineProperty(self.repl.context, key, {
+ get: proto[protoKey].bind(self),
+ enumerable: true
+ });
+ } else {
+ self.repl.context[key] = proto[protoKey].bind(self);
+ }
};
for (var i in proto) {
Interface.prototype.print = function(text) {
process.stdout.cursorTo(0);
process.stdout.clearLine(1);
- process.stdout.write(text + '\n');
+ process.stdout.write(typeof text === 'string' ? text : util.inspect(text));
+ process.stdout.write('\n');
};
Interface.prototype.childPrint = function(text) {
return chunk;
}).map(function(chunk) {
return '< ' + chunk;
- }).join('\n'));
+ }).join('\n') + '\n');
};
+Interface.prototype.error = function(text) {
+ this.print(text);
+ this.resume();
+};
Interface.prototype.handleBreak = function(r) {
this.pause();
Interface.prototype.requireConnection = function() {
- if (!this.client) throw Error('App isn\'t running... Try `run` instead');
+ if (!this.client) this.error('App isn\'t running... Try `run` instead');
};
Interface.prototype.controlEval = function(code, context, filename, callback) {
// Run script
Interface.prototype.run = function() {
if (this.child) {
- throw Error('App is already running... Try `restart` instead');
+ this.error('App is already running... Try `restart` instead');
} else {
this.trySpawn();
}
// Restart script
Interface.prototype.restart = function() {
- if (!this.child) throw Error('App isn\'t running... Try `run` instead');
+ if (!this.child) return this.error('App isn\'t running... Try `run` instead');
var self = this;
self.pause();
client.reqSource(from, to, function(res) {
+ if (!res.source) return self.error('You can\'t list source code right now');
+
var lines = res.source.split('\n');
for (var i = 0; i < lines.length; i++) {
var lineno = res.fromLine + i + 1;
var self = this;
this.client.reqContinue(function() {
- process.nextTick(function() {
- self.resume();
- });
+ self.resume();
});
};
var self = this;
this.client.step('next', 1, function(res) {
- process.nextTick(function() {
- self.resume();
- });
+ self.resume();
});
};
var self = this;
this.client.step('in', 1, function(res) {
- process.nextTick(function() {
- self.resume();
- });
+ self.resume();
});
};
var self = this;
this.client.step('out', 1, function(res) {
- process.nextTick(function() {
- self.resume();
- });
+ self.resume();
+ });
+};
+
+
+// Add breakpoint
+Interface.prototype.setBreakpoint = function(script, line, condition) {
+ var self = this,
+ req = {
+ type: 'script',
+ target: script,
+ line: line,
+ condition: condition
+ };
+
+ self.pause();
+ self.client.setBreakpoint(req, function(res) {
+ if (res.success) {
+ self.print('ok');
+ } else {
+ self.print(req.message || 'error!');
+ }
+ self.resume();
});
};
this.client.listbreakpoints(function(res) {
if (res.success) {
self.print(res.body);
+ self.resume();
} else {
- throw Error(res.message || 'Some error happened');
+ self.error(res.message || 'Some error happened');
}
- self.resume();
});
};