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拷贝过去发现可以了,不知道为啥

相关推荐
崔庆才丨静觅4 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby60615 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了5 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅5 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅6 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅6 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment6 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅6 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊6 小时前
jwt介绍
前端
爱敲代码的小鱼7 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax