CocosCreator 使用JSB调用C++方法时报错

Creator 版本: 3.5.2

使用官方的JSB 自动绑定生成

C++代码

bool RPCClient::connect(const char *host, in_port_t port, long timeout)
{
    printf("this is connect func \n"):       
    return true;
}

JSB代码:(自动生成)

static bool js_pigeon_RPCClient_connect(se::State& s) // NOLINT(readability-identifier-naming)
{
    auto* cobj = SE_THIS_OBJECT<pigeon::RPCClient>(s);
    SE_PRECONDITION2(cobj, false, "js_pigeon_RPCClient_connect : Invalid Native Object");
    const auto& args = s.args();
    size_t argc = args.size();
    CC_UNUSED bool ok = true;
    if (argc == 3) {
        HolderType<const char*, false> arg0 = {};
        HolderType<unsigned short, false> arg1 = {};
        HolderType<long, false> arg2 = {};
        ok &= sevalue_to_native(args[0], &arg0, s.thisObject());
        ok &= sevalue_to_native(args[1], &arg1, s.thisObject());
        ok &= sevalue_to_native(args[2], &arg2, s.thisObject());
        SE_PRECONDITION2(ok, false, "js_pigeon_RPCClient_connect : Error processing arguments");
        bool result = cobj->connect(arg0.value(), arg1.value(), arg2.value());
        ok &= nativevalue_to_se(result, s.rval(), nullptr /*ctx*/);
        SE_PRECONDITION2(ok, false, "js_pigeon_RPCClient_connect : Error processing arguments");
        SE_HOLD_RETURN_VALUE(result, s.thisObject(), s.rval());
        return true;
    }
    SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 3);
    return false;
}
SE_BIND_FUNC(js_pigeon_RPCClient_connect)

ts代码:

let ret = client.connect("192.168.193.70", 8888, 10000);

运行结果截图:

看着像是类型转换的问题?请求帮助,谢谢!!

截图太模糊了,看不清楚

我找人看了下,挺清楚的。是不能点开吗?

报错堆栈:

NetController::NetController new !! 
Can not convert type ???
 - [[ l ]]
Assertion failed: ((std::is_same<T, void>::value)), function sevalue_to_native, file jsb_conversions.h, line 819.
Assertion failed: ((std::is_same<T, void>::value)), function sevalue_to_native, file jsb_conversions.h, line 819.
Assertion failed: ((std::is_same<T, void>::value)), function sevalue_to_native, file jsb_conversions.h, line 819.
Program ended with exit code: 9

点开了,能看见了,

bool RPCClient::connect(const char *host, in_port_t port, long timeout)
{
printf(“this is connect func \n”):
return true;
}
跟断点看 是因为 typescript 函数调用的时候数字被转换为double 类型,但是 C++的接口 参数 需要的是 long类型,所以在sevalue_to_native调用的时候crash了

JSB自动绑定生成的时候 C++ 参数 是否支持long类型?

自动生成的代码改一下试试,改成你传入数值的类型

自动生成的代码是long, 改成double 就好了

参数类型用int试试。。