cocos v8引擎调试js

creator打包的windows程序运行后直接js报错了,使用creator编辑器的模拟器是正常的,打算调试一下。

都是使用的v8执行的js,electron有个调试选项inspect-brk,可以让程序在js的第一行断下,理论上creator打包的windows程序也可以。

  • build\jsb-default\frameworks\cocos2d-x\cocos\scripting\js-bindings\jswrapper\v8\ScriptEngine.cpp

设置调试参数_isWaitForConnect

c++ 复制代码
bool AppDelegate::applicationDidFinishLaunching()
{
#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
    //开启调试,使用的是localhost:6086,   ↓ 这个参数用来控制是否断下,当为true的时候,程序会挂起,直到调试器附加到应用程序
    jsb_enable_debugger("0.0.0.0", 6086, true);
#endif
}

bool jsb_enable_debugger(const std::string& debuggerServerAddr, uint32_t port, bool isWaitForConnect)
{
    auto se = se::ScriptEngine::getInstance();
    se->enableDebugger(debuggerServerAddr.c_str(), port, isWaitForConnect);
}
void ScriptEngine::enableDebugger(const std::string& serverAddr, uint32_t port, bool isWait)
{
    _debuggerServerAddr = serverAddr;
    _debuggerServerPort = port;
    _isWaitForConnect = isWait; // 这个参数
}
    

使用之前的调试配置

c++ 复制代码
bool ScriptEngine::start()
{
    if (isDebuggerEnabled())
    {
#if SE_ENABLE_INSPECTOR
        // V8 inspector stuff, most code are taken from NodeJS.
        _isolateData = node::CreateIsolateData(_isolate, uv_default_loop());
        _env = node::CreateEnvironment(_isolateData, _context.Get(_isolate), 0, nullptr, 0, nullptr);

        node::DebugOptions options;
        // 调试配置,其实就是在设置 break_first_line_ 
        options.set_wait_for_connect(_isWaitForConnect);// the program will be hung up until debug attach if _isWaitForConnect = true
        options.set_inspector_enabled(true);
        options.set_port((int)_debuggerServerPort);
        options.set_host_name(_debuggerServerAddr.c_str());
        bool ok = _env->inspector_agent()->Start(_platform, "", options);
        assert(ok);
#endif
}

打开chrome://inspect/#devices , 配置6086端口,稍等一会就会看到inpsect

调试器启动后,并没有断在第一行,直接运行起来了。

c++ 复制代码
void set_wait_for_connect(bool wait) { break_first_line_ = wait; }
bool wait_for_connect() const { return break_first_line_; }
DebugOptions::DebugOptions() :
                               inspector_enabled_(false),
                               deprecated_debug_(false),
                               break_first_line_(false),
                               host_name_("127.0.0.1"), port_(-1) { }
bool DebugOptions::ParseOption(const char* argv0, const std::string& option) {
  bool has_argument = false;
  std::string option_name;
  std::string argument;

  auto pos = option.find("=");
  if (pos == std::string::npos) {
    option_name = option;
  } else {
    option_name = option.substr(0, pos);
    argument = option.substr(pos + 1);

    if (argument.length() > 0)
      has_argument = true;
    else
      argument.clear();
  }

  // Note that --debug-port and --debug-brk in conjunction with --inspect
  // work but are undocumented.
  // --debug is no longer valid.
  // Ref: https://github.com/nodejs/node/issues/12630
  // Ref: https://github.com/nodejs/node/pull/12949
  if (option_name == "--inspect") {
    inspector_enabled_ = true;
  } else if (option_name == "--debug") {
    deprecated_debug_ = true;
  } else if (option_name == "--inspect-brk") {
    inspector_enabled_ = true;
    break_first_line_ = true;
  } else if (option_name == "--debug-brk") {
    break_first_line_ = true;
    deprecated_debug_ = true;
  } else if (option_name == "--debug-port" ||
             option_name == "--inspect-port") {
    if (!has_argument) {
      SE_LOGE("%s: %s requires an argument\n",
              argv0, option.c_str());
      exit(9);
    }
  } else {
    return false;
  }

#if !HAVE_INSPECTOR
  if (inspector_enabled_) {
    SE_LOGE("Inspector support is not available with this Node.js build\n");
  }
  inspector_enabled_ = false;
  return false;
#endif

  // argument can be specified for *any* option to specify host:port
  if (has_argument) {
    std::pair<std::string, int> host_port = split_host_port(argument);
    if (!host_port.first.empty()) {
      host_name_ = host_port.first;
    }
    if (host_port.second >= 0) {
      port_ = host_port.second;
    }
  }

  return true;
}

v8 demo

c++ 复制代码
bool ScriptEngine::init(){
    v8::Isolate::CreateParams create_params;
    create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
    _isolate = v8::Isolate::New(create_params);
    v8::HandleScope hs(_isolate);
    _isolate->Enter();
}

问题

可能无法找到,和chrome版本有关系

console.log(process.versions.v8);

creator的native在启动的时候会打印v8版本

D/jswrapper (485): Initializing V8, version: 8.0.426.16

npm install -g v8-inspector

yarn global add v8-inspector

v8-inspector --debug-port=7878 --debug-host=192.168.1.29

调试细节

编辑器可以设置等待调试,很明显只有debug版本才有这个功能

这里使用的是不一定debug版本,因为你可以配置release,cocos2d_debug是1同样能达到相同的效果

如何找到config.json

来源

生成事件里面

bash 复制代码
if exist "$(ProjectDir)..\..\..\..\..\simulator\win32\config.json" 
copy 
    "$(ProjectDir)..\..\..\..\..\simulator\win32\config.json" 
    "$(OutDir)config.json" 

从这里获取到最原始的配置,目标目录就是exe的工作目录,所以能够找到

creator模拟器使用的是Creator\2.4.10\resources\cocos2d-x\simulator\win32

当我把编译好的模拟器放到这个目录后,发现无法调试了,

通过生成事件将编译的exe、dll拷贝过去发现可以了,不知道为啥

相关推荐
MiyueFE17 分钟前
14 个逻辑驱动的 UI 设计技巧,助您改善任何界面
前端·设计
啃火龙果的兔子21 分钟前
前端单元测试覆盖率工具有哪些,分别有什么优缺点
前端·单元测试
「、皓子~1 小时前
后台管理系统的诞生 - 利用AI 1天完成整个后台管理系统的微服务后端+前端
前端·人工智能·微服务·小程序·go·ai编程·ai写作
就改了1 小时前
Ajax——在OA系统提升性能的局部刷新
前端·javascript·ajax
凌冰_1 小时前
Ajax 入门
前端·javascript·ajax
京东零售技术1 小时前
京东小程序JS API仓颉改造实践
前端
老A技术联盟1 小时前
从小白入门,基于Cursor开发一个前端小程序之Cursor 编程实践与案例分析
前端·小程序
风铃喵游1 小时前
构建引擎: 打造小程序编译器
前端·小程序·架构
sunbyte1 小时前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | ThemeClock(主题时钟)
前端·javascript·css·vue.js·前端框架·tailwindcss
小飞悟2 小时前
🎯 什么是模块化?CommonJS 和 ES6 Modules 到底有什么区别?小白也能看懂
前端·javascript·设计