[英]How do I provide source-level debugging for a language like VS does for TypeScript?
I understand the concept of using source maps with JavaScript to make debugging easier with minified scripts, etc. What I don't get is how source-level debugging works like it does for TypeScript in Visual Studio ( http://blogs.msdn.com/b/typescript/archive/2012/11/15/announcing-typescript-0-8-1.aspx ). 我了解将源映射与JavaScript结合使用以使调试更便捷,使用最小化的脚本等的概念。我无法理解的是源代码级调试的工作方式,就像在Visual Studio中为TypeScript所做的一样( http://blogs.msdn。 com / b / typescript / archive / 2012/11/15 / anonuncing-typescript-0-8-1.aspx )。
For example, if I create my own language that compiles/translates to JavaScript, how do I interface with browsers to provide this kind of source-level debugging? 例如,如果我创建自己的语言来编译/翻译为JavaScript,那么如何与浏览器交互以提供这种源代码级调试? Is there a standard protocol for this? 为此有标准协议吗? How does Visual Studio do it? Visual Studio如何做到这一点?
Update 更新
To clarify even more, let's say I invent a language called Caffeinated Beverage Script that compiles to JavaScript. 为了进一步说明,假设我发明了一种称为Caffeinated Beverage Script的语言,该语言可编译为JavaScript。 I build an IDE for my language and I want to be able to set breakpoints, step through code, inspect variables, etc. in my IDE while the JavaScript runs in a browser. 我为自己的语言构建了一个IDE,并且希望能够在JavaScript在浏览器中运行时在IDE中设置断点,单步执行代码,检查变量等。 How do I get my IDE to communicate with the browser on this level? 如何在此级别上使我的IDE与浏览器通信?
This is admittedly a bad answer, but I figure its better than nothing. 公认这是一个错误的答案,但我认为它总比没有好。 If I were in your shoes, here is what I would do: 如果我穿上你的鞋子,这是我会做的:
Now its obviously possible to communicate from the browser to the IDE using some kind of plugin, but I don't like that for at least 2 reasons: I don't enjoy coding in strongly-typed languages, and any code I produce will be inherently browser-specific. 现在,显然可以使用某种插件从浏览器与IDE进行通信了,但是至少有两个原因,我不喜欢这样做:我不喜欢使用强类型语言编写代码,而我产生的任何代码都会本质上是特定于浏览器的。 Although its not the lightest weight solution, XHR in blocking mode can get the job done. 尽管它不是最轻的解决方案,但XHR处于阻塞模式可以完成工作。
The IDE would host a http server that sends the CORS header . IDE将托管一个HTTP服务器,该服务器发送CORS标头 。 The compiled source would contain something like this: 编译后的源将包含以下内容:
var ___, __INTERRUPT_SIGNAL = {};
(function() {
var oXhr = new XMLHttpRequest();
oXhr.open('POST', 'http://localhost/debugging_port', false); // Force XHR to work synchronously
___ = function(nLineNumber) {
var sState = 'line_number='+nLineNumber;
for(var nOffset = 0; nOffset < arguments.length; nOffset++) sState += '&i=' + escape(arguments[nOffset]);
oXhr.send(sState);
if(oXhr.status !== 200) return ___.apply(this, arguments);
var sCommand = oXhr.responseText;
if(sCommand === 'step_into') {
return;
}else if(sCommand === 'step_out') {
throw __INTERRUPT_SIGNAL;
}else{
return ___.apply(this, arguments);
}
};
})();
Now when the Caffeinated Beverage Script compiler compiles the source, it should place calls to the _ function at every line break of the source. 现在,当Caffeinated Beverage Script编译器编译源代码时,它将在源代码的每个换行符处调用_函数。 For example, 例如,
int nCheese = 0;
char cLetter = 'Q';
customClass productZeroQ = nCheese * cLetter;
might compile to something like 可能会编译成类似
var $0xA1, $0xA2, $0xA3;
___(9, $0xA1, $0xA2, $0xA3);
$0xA1 = 0;
___(10, $0xA1, $0xA2, $0xA3);
$0xA2 = 'Q';
___(11, $0xA1, $0xA2, $0xA3);
try {
$0xA3 = $0x34.$0x21($0xA1, $0xA2);
}catch(oSignal){
if(oSignal !== ___INTERRUPT_SIGNAL) throw oSignal;
}
XHR will block execution while the IDE decides to whether to step_out or step_forward. 当IDE决定是step_out还是step_forward时,XHR将阻止执行。 This could be extended to include the stack tracing, but I don't feel like writing a novel. 可以将其扩展为包括堆栈跟踪,但是我不喜欢写小说。
I know it's ugly, but I hope this helps a bit! 我知道这很丑陋,但希望能有所帮助!
You might consider WebKit's remote debugging API: 您可能考虑使用WebKit的远程调试API:
https://developers.google.com/chrome-developer-tools/docs/protocol/1.0/index https://developers.google.com/chrome-developer-tools/docs/protocol/1.0/index
I believe that's what Sublime Web Inspector uses. 我相信这就是Sublime Web Inspector所使用的。
https://github.com/sokolovstas/SublimeWebInspector https://github.com/sokolovstas/SublimeWebInspector
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.