Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
[debugger] requireConnection() returns bool, break UI
Browse files Browse the repository at this point in the history
Stepping commands will overwrite output of previous step command
  • Loading branch information
indutny authored and ry committed Sep 14, 2011
1 parent 57388d8 commit 9fb1868
Showing 1 changed file with 67 additions and 50 deletions.
117 changes: 67 additions & 50 deletions lib/_debugger.js
Expand Up @@ -626,18 +626,12 @@ var helpMessage = 'Commands: ' + commands.join(', ');


function SourceUnderline(sourceText, position) {
if (!sourceText) return;

var wrapper = require('module').wrapper[0];
if (sourceText.indexOf(wrapper) === 0) {
sourceText = sourceText.slice(wrapper.length);
position -= wrapper.length;
}
if (!sourceText) return '';

// Create an underline with a caret pointing to the source position. If the
// source contains a tab character the underline will have a tab character in
// the same place otherwise the underline will have a space character.
var underline = '';
var underline = ' ';
for (var i = 0; i < position; i++) {
if (sourceText[i] == '\t') {
underline += '\t';
Expand All @@ -648,7 +642,7 @@ function SourceUnderline(sourceText, position) {
underline += '^';

// Return the source line text with the underline beneath.
return sourceText + '\n' + underline;
return underline;
}


Expand Down Expand Up @@ -751,8 +745,10 @@ Interface.prototype.resume = function(silent) {

// Output
Interface.prototype.print = function(text) {
process.stdout.cursorTo(0);
process.stdout.clearLine(1);
if (process.stdout.isTTY) {
process.stdout.cursorTo(0);
process.stdout.clearLine(1);
}
process.stdout.write(typeof text === 'string' ? text : util.inspect(text));
process.stdout.write('\n');
};
Expand All @@ -775,17 +771,45 @@ Interface.prototype.handleBreak = function(r) {
this.pause();

this.client.currentSourceLine = r.sourceLine;
this.client.currentSourceLineText = r.sourceLineText;
this.client.currentSourceColumn = r.sourceColumn;
this.client.currentFrame = 0;
this.client.currentScript = r.script.name;

this.print(SourceInfo(r) + '\n' +
SourceUnderline(r.sourceLineText, r.sourceColumn));
if (process.stdout.isTTY) {
var step = {
'next': true,
'step': true,
'out': true,
'n': true,
's': true,
'o': true
},
history = this.repl.rli.history;

// If current cmd is 'step' and previous was too
// Clear previous lines and overwrite them
if (step[history[0]] && step[history[1]]) {
for (var i = 0; i < 8; i++) {
process.stdout.clearLine(0);
process.stdout.moveCursor(0, -1);
}
process.stdout.clearLine(0);
}
}

this.print(SourceInfo(r));
this.list(2);
this.resume();
};


Interface.prototype.requireConnection = function() {
if (!this.client) this.error('App isn\'t running... Try `run` instead');
if (!this.client) {
this.error('App isn\'t running... Try `run` instead');
return false;
}
return true;
};

Interface.prototype.controlEval = function(code, context, filename, callback) {
Expand Down Expand Up @@ -896,7 +920,8 @@ Interface.prototype.restart = function() {

// Print version
Interface.prototype.version = function() {
this.requireConnection();
if (!this.requireConnection()) return;

var self = this;

this.pause();
Expand All @@ -908,12 +933,13 @@ Interface.prototype.version = function() {

// List source code
Interface.prototype.list = function() {
this.requireConnection();
if (!this.requireConnection()) return;

var self = this,
client = this.client,
from = client.currentSourceLine - 5,
to = client.currentSourceLine + 5;
delta = arguments[0] || 5,
from = client.currentSourceLine - delta + 1,
to = client.currentSourceLine + delta + 1;

self.pause();
client.reqSource(from, to, function(res) {
Expand All @@ -939,6 +965,8 @@ Interface.prototype.list = function() {
}
pointer += '>';
self.print(pointer + ' ' + lines[i]);
self.print(SourceUnderline(client.currentSourceLineText,
client.currentSourceColumn));
} else {
self.print(leftPad(lineno) + ' ' + lines[i]);
}
Expand All @@ -949,7 +977,7 @@ Interface.prototype.list = function() {

// Print backtrace
Interface.prototype.backtrace = function() {
this.requireConnection();
if (!this.requireConnection()) return;

var self = this,
client = this.client;
Expand Down Expand Up @@ -984,7 +1012,7 @@ Interface.prototype.backtrace = function() {

// argument full tells if it should display internal node scripts or not
Interface.prototype.scripts = function(displayNatives) {
this.requireConnection();
if (!this.requireConnection()) return;

var client = this.client;
var scripts = [];
Expand All @@ -1010,7 +1038,7 @@ Interface.prototype.scripts = function(displayNatives) {

// Continue execution of script
Interface.prototype.cont = function() {
this.requireConnection();
if (!this.requireConnection()) return;
this.pause();

var self = this;
Expand All @@ -1020,43 +1048,31 @@ Interface.prototype.cont = function() {
};


// Jump to next command
Interface.prototype.next = function() {
this.requireConnection();
// Step commands generator
Interface.stepGenerator = function(type, count) {
return function() {
if (!this.requireConnection()) return;

this.pause();
var self = this;

var self = this;
this.client.step('next', 1, function(res) {
self.resume();
});
self.pause();
self.client.step(type, count, function(res) {
self.resume();
});
};
};


// Step in
Interface.prototype.step = function() {
this.requireConnection();
// Jump to next command
Interface.prototype.next = Interface.stepGenerator('next', 1);

this.pause();

var self = this;
this.client.step('in', 1, function(res) {
self.resume();
});
};
// Step in
Interface.prototype.step = Interface.stepGenerator('in', 1);


// Step out
Interface.prototype.out = function() {
this.requireConnection();

this.pause();

var self = this;
this.client.step('out', 1, function(res) {
self.resume();
});
};
Interface.prototype.out = Interface.stepGenerator('out', 1);


// Add breakpoint
Expand All @@ -1083,7 +1099,8 @@ Interface.prototype.setBreakpoint = function(script, line, condition) {

// Show breakpoints
Interface.prototype.breakpoints = function() {
this.requireConnection();
if (!this.requireConnection()) return;

this.pause();
var self = this;
this.client.listbreakpoints(function(res) {
Expand All @@ -1106,7 +1123,7 @@ Interface.prototype.kill = function() {

// Activate debug repl
Interface.prototype.repl = function() {
this.requireConnection();
if (!this.requireConnection()) return;

var self = this;

Expand Down

0 comments on commit 9fb1868

Please sign in to comment.