📄 debug-delay.js.svn-base
字号:
request = this.commandToJSONRequest(command); response = this.processDebugJSONRequest(request); text_result = this.responseToText(response); running = this.isRunning(response); return { "request" : request, "response" : response, "text_result" : text_result, "running" : running };}// Converts a text command to a JSON request.DebugCommandProcessor.prototype.commandToJSONRequest = function(cmd_line) { // If the wery first character is a { assume that a JSON request have been // entered as a command. Converting that to a JSON request is trivial. if (cmd_line && cmd_line.length > 0 && cmd_line.charAt(0) == '{') { return cmd_line; } // Trim string for leading and trailing whitespace. cmd_line = cmd_line.replace(/^\s+|\s+$/g, ""); // Find the command. var pos = cmd_line.indexOf(" "); var cmd; var args; if (pos == -1) { cmd = cmd_line; args = ""; } else { cmd = cmd_line.slice(0, pos); args = cmd_line.slice(pos).replace(/^\s+|\s+$/g, ""); } // Switch on command. if (cmd == 'continue' || cmd == 'c') { return this.continueCommandToJSONRequest_(args); } else if (cmd == 'step' || cmd == 's') { return this.stepCommandToJSONRequest_(args); } else if (cmd == 'backtrace' || cmd == 'bt') { return this.backtraceCommandToJSONRequest_(args); } else if (cmd == 'frame' || cmd == 'f') { return this.frameCommandToJSONRequest_(args); } else if (cmd == 'print' || cmd == 'p') { return this.printCommandToJSONRequest_(args); } else if (cmd == 'source') { return this.sourceCommandToJSONRequest_(args); } else if (cmd == 'scripts') { return this.scriptsCommandToJSONRequest_(args); } else if (cmd[0] == '{') { return cmd_line; } else { throw new Error('Unknown command "' + cmd + '"'); }};// Create a JSON request for the continue command.DebugCommandProcessor.prototype.continueCommandToJSONRequest_ = function(args) { var request = this.createRequest('continue'); return request.toJSONProtocol();};// Create a JSON request for the step command.DebugCommandProcessor.prototype.stepCommandToJSONRequest_ = function(args) { // Requesting a step is through the continue command with additional // arguments. var request = this.createRequest('continue'); request.arguments = {}; // Process arguments if any. if (args && args.length > 0) { args = args.split(/\s*[ ]+\s*/g); if (args.length > 2) { throw new Error('Invalid step arguments.'); } if (args.length > 0) { // Get step count argument if any. if (args.length == 2) { request.arguments.stepcount = %ToNumber(args[1]); } // Get the step action. if (args[0] == 'in' || args[0] == 'i') { request.arguments.stepaction = 'in'; } else if (args[0] == 'min' || args[0] == 'm') { request.arguments.stepaction = 'min'; } else if (args[0] == 'next' || args[0] == 'n') { request.arguments.stepaction = 'next'; } else if (args[0] == 'out' || args[0] == 'o') { request.arguments.stepaction = 'out'; } else { throw new Error('Invalid step argument "' + args[0] + '".'); } } } else { // Default is step next. request.arguments.stepaction = 'next'; } return request.toJSONProtocol();};// Create a JSON request for the backtrace command.DebugCommandProcessor.prototype.backtraceCommandToJSONRequest_ = function(args) { // Build a backtrace request from the text command. var request = this.createRequest('backtrace'); args = args.split(/\s*[ ]+\s*/g); if (args.length == 2) { request.arguments = {}; request.arguments.fromFrame = %ToNumber(args[0]); request.arguments.toFrame = %ToNumber(args[1]) + 1; } return request.toJSONProtocol();};// Create a JSON request for the frame command.DebugCommandProcessor.prototype.frameCommandToJSONRequest_ = function(args) { // Build a frame request from the text command. var request = this.createRequest('frame'); args = args.split(/\s*[ ]+\s*/g); if (args.length > 0 && args[0].length > 0) { request.arguments = {}; request.arguments.number = args[0]; } return request.toJSONProtocol();};// Create a JSON request for the print command.DebugCommandProcessor.prototype.printCommandToJSONRequest_ = function(args) { // Build a evaluate request from the text command. var request = this.createRequest('evaluate'); if (args.length == 0) { throw new Error('Missing expression.'); } request.arguments = {}; request.arguments.expression = args; return request.toJSONProtocol();};// Create a JSON request for the source command.DebugCommandProcessor.prototype.sourceCommandToJSONRequest_ = function(args) { // Build a evaluate request from the text command. var request = this.createRequest('source'); // Default is one line before and two lines after current location. var before = 1; var after = 2; // Parse the arguments. args = args.split(/\s*[ ]+\s*/g); if (args.length > 1 && args[0].length > 0 && args[1].length > 0) { before = %ToNumber(args[0]); after = %ToNumber(args[1]); } else if (args.length > 0 && args[0].length > 0) { after = %ToNumber(args[0]); } // Request source arround current source location. request.arguments = {}; request.arguments.fromLine = this.exec_state_.frame().sourceLine() - before; if (request.arguments.fromLine < 0) { request.arguments.fromLine = 0 } request.arguments.toLine = this.exec_state_.frame().sourceLine() + after + 1; return request.toJSONProtocol();};// Create a JSON request for the scripts command.DebugCommandProcessor.prototype.scriptsCommandToJSONRequest_ = function(args) { // Build a evaluate request from the text command. var request = this.createRequest('scripts'); // Process arguments if any. if (args && args.length > 0) { args = args.split(/\s*[ ]+\s*/g); if (args.length > 1) { throw new Error('Invalid scripts arguments.'); } request.arguments = {}; if (args[0] == 'natives') { request.arguments.types = ScriptTypeFlag(Debug.ScriptType.Native); } else if (args[0] == 'extensions') { request.arguments.types = ScriptTypeFlag(Debug.ScriptType.Extension); } else if (args[0] == 'all') { request.arguments.types = ScriptTypeFlag(Debug.ScriptType.Normal) | ScriptTypeFlag(Debug.ScriptType.Native) | ScriptTypeFlag(Debug.ScriptType.Extension); } else { throw new Error('Invalid argument "' + args[0] + '".'); } } return request.toJSONProtocol();};// Convert a JSON response to text for display in a text based debugger.DebugCommandProcessor.prototype.responseToText = function(json_response) { try { // Convert the JSON string to an object. response = %CompileString('(' + json_response + ')', 0, false)(); if (!response.success) { return response.message; } if (response.command == 'backtrace') { var body = response.body; var result = 'Frames #' + body.fromFrame + ' to #' + (body.toFrame - 1) + ' of ' + body.totalFrames + '\n'; for (i = 0; i < body.frames.length; i++) { if (i != 0) result += '\n'; result += body.frames[i].text; } return result; } else if (response.command == 'frame') { return SourceUnderline(response.body.sourceLineText, response.body.column); } else if (response.command == 'evaluate') { return response.body.text; } else if (response.command == 'source') { // Get the source from the response. var source = response.body.source; // Get rid of last line terminator. var remove_count = 0; if (source[source.length - 1] == '\n') remove_count++; if (source[source.length - 2] == '\r') remove_count++; if (remove_count > 0) source = source.substring(0, source.length - remove_count); return source; } else if (response.command == 'scripts') { var result = ''; for (i = 0; i < response.body.length; i++) { if (i != 0) result += '\n'; if (response.body[i].name) { result += response.body[i].name; } else { result += '[unnamed] '; var sourceStart = response.body[i].sourceStart; if (sourceStart.length > 40) { sourceStart = sourceStart.substring(0, 37) + '...'; } result += sourceStart; } result += ' (lines: '; result += response.body[i].sourceLines; result += ', length: '; result += response.body[i].sourceLength; if (response.body[i].type == Debug.ScriptType.Native) { result += ', native'; } else if (response.body[i].type == Debug.ScriptType.Extension) { result += ', extension'; } result += ')'; } return result; } } catch (e) { return 'Error: "' + %ToString(e) + '" formatting response'; }};function SourceUnderline(source_text, position) { if (IS_UNDEFINED(source_text)) { 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 = ''; for (var i = 0; i < position; i++) { if (source_text[i] == '\t') { underline += '\t'; } else { underline += ' '; } } underline += '^'; // Return the source line text with the underline beneath. return source_text + '\n' + underline;};function FrameSourceUnderline(frame) { var location = frame.sourceLocation(); if (location) { return SourceUnderline(location.sourceText(), location.position - location.start); }};function RequestPacket(command) { this.seq = 0; this.type = 'request'; this.command = command;};RequestPacket.prototype.toJSONProtocol = function() { // Encode the protocol header. var json = '{'; json += '"seq":' + this.seq; json += ',"type":"' + this.type + '"'; if (this.command) { json += ',"command":' + StringToJSON_(this.command); } if (this.arguments) { json += ',"arguments":'; // Encode the arguments part. if (this.arguments.toJSONProtocol) { json += this.arguments.toJSONProtocol() } else { json += SimpleObjectToJSON_(this.arguments); } } json += '}'; return json;}DebugCommandProcessor.prototype.createRequest = function(command) { return new RequestPacket(command);};function ResponsePacket(request) { // Build the initial response from the request. this.seq = next_response_seq++; this.type = 'response'; if (request) this.request_seq = request.seq; if (request) this.command = request.command; this.success = true; this.running = false;};ResponsePacket.prototype.failed = function(message) { this.success = false; this.message = message;}ResponsePacket.prototype.toJSONProtocol = function() { // Encode the protocol header. var json = '{'; json += '"seq":' + this.seq; if (this.request_seq) { json += ',"request_seq":' + this.request_seq; } json += ',"type":"' + this.type + '"'; if (this.command) { json += ',"command":' + StringToJSON_(this.command); } if (this.success) { json += ',"success":' + this.success; } else { json += ',"success":false'; } if (this.body) { json += ',"body":'; // Encode the body part. if (this.body.toJSONProtocol) { json += this.body.toJSONProtocol(true); } else if (this.body instanceof Array) { json += '['; for (var i = 0; i < this.body.length; i++) { if (i != 0) json += ','; if (this.body[i].toJSONProtocol) { json += this.body[i].toJSONProtocol(true) } else { json += SimpleObjectToJSON_(this.body[i]); } } json += ']'; } else { json += SimpleObjectToJSON_(this.body); } } if (this.message) { json += ',"message":' + StringToJSON_(this.message) ; } if (this.running) { json += ',"running":true'; } else { json += ',"running":false'; } json += '}'; return json;}DebugCommandProcessor.prototype.createResponse = function(request) { return new ResponsePacket(request);};DebugCommandProcessor.prototype.processDebugJSONRequest = function(json_request, stopping) { var request; // Current request. var response; // Generated response. try { try { // Convert the JSON string to an object. request = %CompileString('(' + json_request + ')', 0, false)(); // Create an initial response. response = this.createResponse(request); if (!request.type) { throw new Error('Type not specified'); } if (request.type != 'request') { throw new Error("Illegal type '" + request.type + "' in request"); } if (!request.command) { throw new Error('Command not specified'); } if (request.command == 'continue') { this.continueRequest_(request, response); } else if (request.command == 'break') { this.breakRequest_(request, response); } else if (request.command == 'setbreakpoint') { this.setBreakPointRequest_(request, response); } else if (request.command == 'changebreakpoint') { this.changeBreakPointRequest_(request, response); } else if (request.command == 'clearbreakpoint') { this.clearBreakPointRequest_(request, response); } else if (request.command == 'backtrace') { this.backtraceRequest_(request, response); } else if (request.command == 'frame') { this.frameRequest_(request, response); } else if (request.command == 'evaluate') { this.evaluateRequest_(request, response); } else if (request.command == 'source') { this.sourceRequest_(request, response); } else if (request.command == 'scripts') { this.scriptsRequest_(request, response); } else { throw new Error('Unknown command "' + request.command + '" in request'); } } catch (e) { // If there is no response object created one (without command). if (!response) { response = this.createResponse(); } response.success = false; response.message = %ToString(e); } // Return the response as a JSON encoded string. try { // Set the running state to what indicated. if (!IS_UNDEFINED(stopping)) { response.running = !stopping; } return response.toJSONProtocol(); } catch (e) { // Failed to generate response - return generic error. return '{"seq":' + response.seq + ',' + '"request_seq":' + request.seq + ',' + '"type":"response",' + '"success":false,' + '"message":"Internal error: ' + %ToString(e) + '"}'; } } catch (e) { // Failed in one of the catch blocks above - most generic error. return '{"seq":0,"type":"response","success":false,"message":"Internal error"}'; }};DebugCommandProcessor.prototype.continueRequest_ = function(request, response) { // Check for arguments for continue. if (request.arguments) { var count = 1; var action = Debug.StepAction.StepIn; // Pull out arguments. var stepaction = request.arguments.stepaction; var stepcount = request.arguments.stepcount; // Get the stepcount argument if any. if (stepcount) { count = %ToNumber(stepcount); if (count < 0) { throw new Error('Invalid stepcount argument "' + stepcount + '".');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -