Skip to content

Commit

Permalink
factored front-end requests into Debugger.js and response handling in…
Browse files Browse the repository at this point in the history
…to InspectorFrontendHostStub.js
  • Loading branch information
dannycoates committed Jul 23, 2010
1 parent 6043ef5 commit 61dfe89
Show file tree
Hide file tree
Showing 7 changed files with 365 additions and 325 deletions.
14 changes: 5 additions & 9 deletions bin/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ exports.createSession = function(options) {
var httpServer = http.createServer(staticFile);
var wsServer = ws.createServer(null, httpServer);
var debug = null;

wsServer.on('connection', function(conn) {
if (!debug) {
// first connection
Expand All @@ -62,13 +62,9 @@ exports.createSession = function(options) {
conn.on('message', function(msg) {
debug.request(msg);
});
session.emit('connection', conn);
});

wsServer.on('close', function() {
wsServer = null;
session.close();
});


if (settings.file) {
var flag = '--debug=';
if (options.brk) flag = '--debug-brk=';
Expand Down Expand Up @@ -101,7 +97,7 @@ exports.createSession = function(options) {
}
}
wsServer.listen(settings.webPort);

var session = Object.create(events.EventEmitter.prototype, {
close: {
value: function()
Expand All @@ -113,6 +109,6 @@ exports.createSession = function(options) {
}}});
session.__defineGetter__('webPort', function() { return settings.webPort; });
session.__defineGetter__('debugPort', function() { return settings.debugPort; });

return session;
};
6 changes: 3 additions & 3 deletions front-end/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
<script type="text/javascript" src="utilities.js"></script>
<script type="text/javascript" src="treeoutline.js"></script>
<script type="text/javascript" src="inspector.js"></script>
<script type="text/javascript" src="node/Debugger.js"></script>
<script type="text/javascript" src="node/InspectorBackendStub.js"></script>
<script type="text/javascript" src="node/InspectorFrontendHostStub.js"></script>
<script type="text/javascript" src="node/InspectorFrontendHostStub.js"></script>
<script type="text/javascript" src="Object.js"></script>
<script type="text/javascript" src="Settings.js"></script>
<script type="text/javascript" src="CSSStyleModel.js"></script>
Expand Down Expand Up @@ -132,8 +133,7 @@
<script type="text/javascript" src="HelpScreen.js"></script>
<script type="text/javascript" src="ShortcutsHelp.js"></script>
<script type="text/javascript" src="HAREntry.js"></script>
<script type="text/javascript" src="node/InspectorController.js"></script>
<script type="text/javascript" src="node/Overrides.js"></script>
<script type="text/javascript" src="node/Overrides.js"></script>
</head>
<body class="detached">
<div id="toolbar">
Expand Down
169 changes: 169 additions & 0 deletions front-end/node/Debugger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
WebInspector.nodeDebugger = (function() {
var socket = null;
var seq = 1;
var callbacks = {};
var breakpoints = {};
var listeners = {};

function takeCallId(request_seq) {
var callId = callbacks[request_seq];
delete callbacks[request_seq];
return callId;
}

function dispatch(data) {
var msg = JSON.parse(data);
var cmd = msg.command || msg.event;
msg['callId'] = takeCallId(msg.request_seq);
if (listeners[cmd]) {
listeners[cmd].forEach(function(callback) {
callback.call(this, msg);
});
}
}

function makeRequest(command, params) {
var msg = {
seq: seq++,
type: 'request',
command: command
}
if (params) {
Object.keys(params).forEach(function(key) {
msg[key] = params[key];
});
}
return msg;
}

function sendRequest(command, params, callId) {
var msg = makeRequest(command, params);
if (typeof callId !== 'undefined') {
callbacks[msg.seq] = callId;
}
socket.send(JSON.stringify(msg));
}

var debugr = {
connect: function() {
if (['http:', 'https:'].indexOf(window.location.protocol) > -1) {
var addr = window.location.host;
}
else {
var addr = '127.0.0.1:8080'; //FIXME
}
socket = new WebSocket('ws://' + addr);
socket.onmessage = function(event) {
dispatch(event.data);
};
socket.onclose = function() {
breakpoints = {};
callbacks = {};
WebInspector.debuggerWasDisabled();
WebInspector.panels.scripts.reset();
console.log('socket closed');
};
socket.onopen = function() {
console.log('socket open');
WebInspector.debuggerWasEnabled();
sendRequest('scripts', {
arguments: { includeSource: true, types: 4 }});
};
},
close: function() {
socket.close();
socket = null;
},
on: function(event, callback) {
var list = listeners[event] || [];
list.push(callback);
listeners[event] = list;
},
setBreakpoint: function(callId, sourceID, line, enabled, condition) {
var bp = breakpoints[sourceID + ':' + line];
if(bp) {
sendRequest('changebreakpoint', {
arguments: {
breakpoint: bp,
enabled: enabled,
condition: condition}});
}
else {
sendRequest('setbreakpoint',{
arguments: {
type: 'scriptId',
target: sourceID,
line: line - 1,
enabled: enabled,
condition: condition
}}, {id: sourceID + ':' + line, line: line, callId: callId});
}
},
clearBreakpoint: function(sourceID, line) {
var id = sourceID + ':' + line;
sendRequest('clearbreakpoint', {
arguments: { breakpoint: breakpoints[id] }}, id);
},
listBreakpoints: function() {
sendRequest('listbreakpoints');
},

getBacktrace: function() {
sendRequest('backtrace',{arguments: { inlineRefs: true }});
},
pause: function() {
sendRequest('suspend');
},
resume: function(step) {
if(step) {
var params = {arguments: { stepaction: step }};
}
sendRequest('continue', params);
},
getScope: function(frameId, scopeId, callId) {
sendRequest('scope', {
arguments: {
number: scopeId,
frameNumber: frameId,
inlineRefs:true }},
callId);
},
lookup: function(ref, callId) {
sendRequest('lookup', {
arguments: { handles: [ref], inlineRefs:true }},
callId);
},
evaluate: function(expr, callId, frameId) {
var args = { expression: expr, disable_break: true };
if (frameId != null) {
args.frame = frameId;
args.global = false;
}
else {
args.global = true;
}
sendRequest('evaluate', {arguments: args}, callId);
}
};

debugr.on('setbreakpoint', function(msg) {
breakpoints[msg.callId.id] = msg.body.breakpoint;
});

debugr.on('clearbreakpoint', function(msg) {
delete breakpoints[msg.callId];
});

debugr.on('listbreakpoints', function(msg) {
breakpoints = {};
msg.body.breakpoints.forEach(function(bp) {
if(bp.type === 'scriptId') {
var l = bp.line + 1;
var url = WebInspector.panels.scripts._sourceIDMap[bp.script_id].sourceURL;
breakpoints[bp.script_id + ':' + l] = bp.number;
}
});
});
return debugr;
}());

27 changes: 13 additions & 14 deletions front-end/node/InspectorBackendStub.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,23 +158,22 @@ WebInspector.InspectorBackendStub.prototype = {

enableDebugger: function()
{
WebInspector.InspectorController.initialize();
WebInspector.nodeDebugger.connect();
},

disableDebugger: function()
{
WebInspector.InspectorController.close();
WebInspector.nodeDebugger.close();
},

setBreakpoint: function(callId, sourceID, line, enabled, condition)
{
WebInspector.InspectorController.setBreakpoint(callId, sourceID, line, enabled, condition);
WebInspector.didSetBreakpoint(callId, true, line);
WebInspector.nodeDebugger.setBreakpoint(callId, sourceID, line, enabled, condition);
},

removeBreakpoint: function(sourceID, line)
{
WebInspector.InspectorController.clearBreakpoint(sourceID, line);
WebInspector.nodeDebugger.clearBreakpoint(sourceID, line);
},

activateBreakpoints: function()
Expand All @@ -199,7 +198,7 @@ WebInspector.InspectorBackendStub.prototype = {

pause: function()
{
WebInspector.InspectorController.pause();
WebInspector.nodeDebugger.pause();
},

setPauseOnExceptionsState: function(value)
Expand All @@ -219,7 +218,7 @@ WebInspector.InspectorBackendStub.prototype = {

resume: function()
{
WebInspector.InspectorController.resume();
WebInspector.nodeDebugger.resume();
},

enableProfiler: function()
Expand Down Expand Up @@ -260,17 +259,17 @@ WebInspector.InspectorBackendStub.prototype = {

stepIntoStatement: function()
{
WebInspector.InspectorController.resume('in');
WebInspector.nodeDebugger.resume('in');
},

stepOutOfFunction: function()
{
WebInspector.InspectorController.resume('out');
WebInspector.nodeDebugger.resume('out');
},

stepOverStatement: function()
{
WebInspector.InspectorController.resume('next');
WebInspector.nodeDebugger.resume('next');
},

saveApplicationSettings: function()
Expand Down Expand Up @@ -332,16 +331,16 @@ WebInspector.InspectorBackendStub.prototype = {
WebInspector.Callback.processCallback(arguments[0], props);
}
else {
WebInspector.InspectorController.getScope(id.frameId, id.scopeId, arguments[0]);
WebInspector.nodeDebugger.getScope(id.frameId, id.scopeId, arguments[0]);
}
}
else {
WebInspector.InspectorController.lookup(id, arguments[0]);
WebInspector.nodeDebugger.lookup(id, arguments[0]);
}
break;
case 'evaluate':
var expr = JSON.parse(arguments[3])[0];
WebInspector.InspectorController.evaluate(expr, arguments[0]);
WebInspector.nodeDebugger.evaluate(expr, arguments[0]);
break;
case 'evaluateInCallFrame':
var args = JSON.parse(arguments[3]);
Expand All @@ -353,7 +352,7 @@ WebInspector.InspectorBackendStub.prototype = {
WebInspector.Callback.processCallback(arguments[0], null);
}
else {
WebInspector.InspectorController.evaluate(expr, arguments[0], frameId);
WebInspector.nodeDebugger.evaluate(expr, arguments[0], frameId);
}
break;
default:
Expand Down
Loading

0 comments on commit 61dfe89

Please sign in to comment.