我在c++层有个这样的类:
typedef std::function <void(const char* buf)> MY_FUNCTION;
class common {
public:
static void funcA();
static void funcB(MY_FUNCTION func);
};
在对这个类做binding时,funcA生成在jsb_common_auto.cpp中, funcB生成在jsb_common_manual.cpp中。
在jsb_common_manual.cpp中绑定代码如下:
static bool js_cocos2dx_common_funcB_manual(se::State& s)
{
const auto& args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc == 1) {
std::function<void(const char*)> arg0;
do {
if (args[0].isObject() && args[0].toObject()->isFunction())
{
se::Value jsThis(s.thisObject());
se::Value jsFunc(args[0]);
jsFunc.toObject()->root();
// jsThis.toObject()->root();
auto lambda = [=](const char* larg0) -> void {
se::AutoHandleScope hs;
CC_UNUSED bool ok = true;
se::ValueArray args;
args.resize(1);
**ok &= std_string_to_seval(larg0, &args[0]);**
se::Value rval;
se::Object* thisObj = jsThis.isObject() ? jsThis.toObject() : nullptr;
se::Object* funcObj = jsFunc.toObject();
funcObj->call(args, thisObj, &rval);
};
arg0 = lambda;
}
else
{
arg0 = nullptr;
}
} while (false)
;
SE_PRECONDITION2(ok, false, "js_cocos2dx_common_funcB_manual : Error processing arguments");
common::funcB(arg0);
return true;
}
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 1);
return false;
}
SE_BIND_FUNC(js_cocos2dx_common_funcB_manual)
bool register_common(se::Object* obj)
{
auto cls = __jsb_common_class;
cls->defineStaticFunction("funcB", _SE(js_cocos2dx_common_funcB_manual));
// 这句加不加都一样。
// cls->install();
se::ScriptEngine::getInstance()->clearException();
return true;
}
bool register_all_plugin_manual(se::Object* obj)
{
auto global = se::ScriptEngine::getInstance()->getGlobalObject();
se::Object* _pluginObj = nullptr;
getOrCreatePlainObject_plugin("plugin", global, &_pluginObj);
register_common(_pluginObj);
return true;
}
jsb_common_auto.cpp中common的绑定代码如下:
bool js_register_plugin_common(se::Object* obj)
{
auto cls = se::Class::create("common", obj, nullptr, nullptr);
cls->defineStaticFunction("funcA", _SE(js_cocos2dx_common_funcA));
cls->defineFinalizeFunction(_SE(js_common_finalize));
cls->install();
JSBClassType::registerClass<common>(cls);
__jsb_common_proto = cls->getProto();
__jsb_common_class = cls;
se::ScriptEngine::getInstance()->clearException();
return true;
}
在jsb_module_register.cpp中添加下面两句代码:
se->addRegisterCallback(js_register_plugin_common);
se->addRegisterCallback(register_all_plugin_manual);
运行时,报错,common中没有接口funcB. 这是哪里错了?
PS.如果把funcB直接自动 生成到jsb_common_auto.cpp中就可以找到这个接口。。。但是这样生成的代码需要手动改一一些地方才可以执行正确(实在不想每次导出后要去手动改代码。所以一定要放在manual中。)