C++ 如何让Chrome开发工具显示JavaScript源代码
我稍微修改了C++ 如何让Chrome开发工具显示JavaScript源代码,c++,v8,embedded-v8,C++,V8,Embedded V8,我稍微修改了helloworld.cc示例,从d8导入了一些代码。然后,使用和,我向程序添加了一个WebSocket服务器。 此外,我还为inspector协议后端添加了一个简单的实现 现在,当我启动程序,然后使用Chrome导航到Chrome时-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9002,我从CDT收到以下消息: {"id":1,"method":"Profi
helloworld.cc
示例,从d8
导入了一些代码。然后,使用和,我向程序添加了一个WebSocket服务器。
此外,我还为inspector协议后端添加了一个简单的实现
现在,当我启动程序,然后使用Chrome导航到Chrome时-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9002
,我从CDT收到以下消息:
{"id":1,"method":"Profiler.enable"}
对于witch,响应是:
{"id":1,"result":{}}
然后
对于这一个,将发送通知和响应:
{"method":"Runtime.executionContextCreated",
"params":{"context":{"id":1,"origin":"","name":"MyApplication"}}}
{"id":2,"result":{}}
然后:
同样,将通知和响应发送回前端:
{"method":"Debugger.scriptParsed",
"params":{
"scriptId":"4","url":"func_add.js","startLine":0,
"startColumn":0,"endLine":0,"endColumn":35,
"executionContextId":1,"hash":"365568ee6245be1376631dbf20e7de9d42c9adf1",
"isLiveEdit":false,"sourceMapURL":"","hasSourceURL":false,
"isModule":false,"length":35
}
}
{"id":3,"result":{"debuggerId":"(DC239109305DBEF825A955524584A826)"}}
目前,我不会把从前端收到的其他消息和发送的响应添加到问题中。
最后一次交流是:
{"id":7,"method":"Runtime.runIfWaitingForDebugger"}
{"id":7,"result":{}}
我的问题:在CDT中,Sources
选项卡是空的(因此,我无法尝试设置断点)
在V8中插入JS的代码:
const char * pszScript = "function add( a, b) { return a+b; }";
v8::Local<v8::String> source =
v8::String::NewFromUtf8(isolate, pszScript, v8::NewStringType::kNormal).ToLocalChecked();
v8::Local<v8::String> name =
v8::String::NewFromUtf8(isolate, "func_add.js", v8::NewStringType::kNormal).ToLocalChecked();
ExecuteString( isolate, source, name );
我想我做错了什么,因为我应该能够在CDT中看到一些
func_add.js
源代码,内容是function add(a,b){return a+b;}
根本不检查源代码,我记得在这个用例中有一些不好的时候
尝试向源参数添加协议。
CDT需要任何协议文件、http、https来创建源代码树。
它还将使用此uri请求映射或任何其他与源代码相关的内容
v8::Local<v8::String> name =
v8::String::NewFromUtf8(isolate, "file://func_add.js", v8::NewStringType::kNormal).ToLocalChecked();
ExecuteString( isolate, source, name );
虽然实现非常简单,但有很多原因导致代码无法显示。
希望能有所帮助。谢谢!是的,
contextCreated
调用在我的代码中;只需复制/粘贴代码:-)。将尝试“file://”提示并通知您。顺便说一句,你的“嵌入者立场”页面是某种互联网奇迹,谢谢你。一些新闻:我用“CDT对CDT”来查看WebSocket流量。我的答案/通知似乎正确,json解析成功。仍然没有“Debugger.getScriptSource”请求(带或不带“file://”前缀)您以前可能已经尝试过了,但我通篇介绍了:并清理了大部分内容,只是为了获得我想要的最原始的示例。TaskRunner结构是必须的。方法quit/runMessageLoopOnPause将在消息循环上同步重新进入(例如,开发工具在源代码顶部填充变量信息等)。这是最棘手的部分。就我而言,这在嵌入式android应用程序上运行得很好。我可能也需要重新阅读这篇文章并对此进行反思。当我能够在CDT中看到我的JS源代码并能够设置断点时,我会这样做……你会在这里找到WebSocket流量:一些进展。我的“文件://“测试。现在该文件在CDT中可见。您知道在编译没有真实文件的scriptlet时,我必须使用什么格式良好的“源代码”吗?”?
const char * pszScript = "function add( a, b) { return a+b; }";
v8::Local<v8::String> source =
v8::String::NewFromUtf8(isolate, pszScript, v8::NewStringType::kNormal).ToLocalChecked();
v8::Local<v8::String> name =
v8::String::NewFromUtf8(isolate, "func_add.js", v8::NewStringType::kNormal).ToLocalChecked();
ExecuteString( isolate, source, name );
bool ExecuteString(v8::Isolate* isolate, v8::Local<v8::String> source,
v8::Local<v8::Value> name) {
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Context::Scope context_scope(context);
v8::TryCatch try_catch(isolate);
try_catch.SetVerbose(true);
v8::MaybeLocal<v8::Value> maybe_result;
bool success = true;
v8::ScriptOrigin origin(name);
v8::ScriptCompiler::Source script_source(source, origin);
v8::MaybeLocal<v8::Script> maybe_script;
maybe_script = v8::ScriptCompiler::Compile(context, &script_source);
v8::Local<v8::Script> script;
if (!maybe_script.ToLocal(&script)) {
// Print errors that happened during compilation.
ReportException(isolate, &try_catch);
return false;
}
maybe_result = script->Run(context);
v8::Local<v8::Value> result;
if (!maybe_result.ToLocal(&result)) {
// Print errors that happened during execution.
ReportException(isolate, &try_catch);
return false;
}
if (!result->IsUndefined()) {
// If all went well and the result wasn't undefined then print
// the returned value.
v8::String::Utf8Value str(isolate, result);
fwrite(*str, sizeof(**str), str.length(), stdout);
printf("\n");
} else {
printf("undefined\n");
}
return success;
}
v8::Local<v8::String> name =
v8::String::NewFromUtf8(isolate, "file://func_add.js", v8::NewStringType::kNormal).ToLocalChecked();
ExecuteString( isolate, source, name );
inspector_->contextCreated(
v8_inspector::V8ContextInfo(context, 1, v8_inspector::StringView(
reinterpret_cast<const uint8_t *>("ABCD"), 4)));
{"id":8,"method":"Debugger.getScriptSource","params":{"scriptId":"7"}}