1 JS调用C++
sc->addRegisterCallback(MinXmlHttpRequest::_js_register);
sc->addRegisterCallback(register_jsb_websocket);
sc->addRegisterCallback(register_jsb_socketio);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
sc->addRegisterCallback(JavascriptJavaBridge::_js_register);
#endif
sc->addRegisterCallback(register_jsb_kenko_all);
sc->start();
jsb_kenko_auto.h
#ifndef jsb_jsb_kenko_auto_h
#define jsb_jsb_kenko_auto_h
#include "cocos2d.h"
std::string os_info();
bool jsb_os_info(
jscontext *cx,uint32_t argc,JS::Value *vp);
bool jsb_callback(
jscontext *cx,JS::Value *vp);
void register_jsb_kenko_all(
jscontext* cx,JSObject* obj);
#endif
jsb_kenko_auto.cpp
#include "jsb_kenko_auto.h"
#include "cocos2d_specifics.hpp"
std::string os_info() {
cclOG("it's c++ os_info here");
return "os_info";
}
bool jsb_os_info(
jscontext *cx,JS::Value *vp) {
jsval ret = std_string_to_jsval(cx,os_info());
JS_SET_RVAL(cx,vp,ret);
return true;
}
void register_jsb_kenko_all(
jscontext *cx,JSObject *obj) {
JS_DefineFunction(cx,obj,"osInfo",jsb_os_info,0); //
生成名为osInfo的js全局
函数
}
把h和cpp
文件都放到AppDelegate.cpp同
一个地方。上述的c++
代码会在spidermonkey运行环境中
生成相应的js接口,所以,我们不需要自己额外写对应的js接口。
然后就可以写js
代码试试了。从运行结果可以看到,js
调用成功,并
获取到返回值。
cc.game.onStart = function(){
cc.view.setDesignResolutionSize(800,450,cc.ResolutionPolicy.SHOW_ALL);
cc.view.resizeWith
browserSize(true);
cc.director.runScene(new MainScene());
cc.log("js get from c++: " + osInfo());
};
cc.game.run();
2 C++回调
executeFunctionWithOwner可以实现类似cc.sprite之类的c++对象和js对象的
调用,没有深究。这里演示的是如何做全局
调用。
evalString对任何
一个前端开发来说都不会太陌生,毕竟这里不是
浏览器,排除各种乱七八糟的安全问题,我们直接用这个
函数。
/**
@brief Execute a scripted global function.
@brief The function should not take any p
arameters and should return an integer.
@p
aram functionName String object holding the name of the function,in the global script environment,that is to be executed.
@return The integer value returned from the script function.
*/
virtual int executeGlobalFunction(const char* functionName) { return 0; }
virtual int sendEvent(cocos2d::ScriptEvent* message) override;
virtual bool parseCon
fig(Con
figType type,const std::string& str) override;
virtual bool handleAssert(const char *msg) { return false; }
virtual void setCalledFromScript(bool callFromScript) { _callFromScript = callFromScript; };
virtual bool isCalledFromScript() { return _callFromScript; };
bool executeFunctionWithObjectData(void* nativeObj,const char *name,JSObject *obj);
bool executeFunctionWithOwner(jsval owner,uint32_t argc = 0,jsval* vp = NULL,jsval* retVal = NULL);
void executeJSFunctionWithThisObj(jsval thisObj,jsval callback,jsval* retVal = NULL);
/**
* will eval the specified string
* @p
aram string The string with the javascript code to be evaluated
* @p
aram outVal The jsval that will hold the return value of the evaluation.
* Can be NULL.
*/
bool evalString(const char *string,jsval *outVal,const char *filename = NULL,
jscontext* cx = NULL,JSObject* global = NULL);
#include "jsb_kenko_auto.h"
#include "cocos2d_specifics.hpp"
std::string os_info() {
cclOG("it's c++ os_info here");
return "os_info";
}
bool jsb_callback(
jscontext *cx,JS::Value *vp) {
cclOG("it's c++ testCallback here");
// 参考:http://www.tairan.com/archives/4902
//jsval v[2];
//v[0] = int32_to_jsval(jc,32);
//v[1] = int32_to_jsval(jc,12);
// 通过 Scriptin
gcore 封装好的
方法实现回调,可以帮助我们节省很多细节上的研究
//js_proxy_t * p = jsb_get_native_proxy();
//return Scriptin
gcore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj),"cpp_callback",2,v); //2是参数个数,v是参数列表
jsval ret;
return Scriptin
gcore::getInstance()->evalString("cpp_callback(2,3)",&ret);
}
JS_DefineFunction(cx,"test_cpp_callback",jsb_callback,0);
}
cc.game.onStart = function(){
cc.view.setDesignResolutionSize(800,cc.ResolutionPolicy.SHOW_ALL);
cc.view.resizeWith
browserSize(true);
cc.director.runScene(new MainScene());
cc.log("js get from c++: " + osInfo());
test_cpp_callback();
};
cc.game.run();
function cpp_callback(a,b) {
cc.log("cpp return two integer: " + a + " " + b);
}
都在js_manual_conversions.h这里了,真是应有尽有。下边只列出一部分。
bool jsval_to_ushort(
jscontext *cx,jsval vp,unsigned short *ret );
bool jsval_to_int32(
jscontext *cx,int32_t *ret );
bool jsval_to_uint32(
jscontext *cx,uint32_t *ret );
bool jsval_to_uint16(
jscontext *cx,uint16_t *ret );
bool jsval_to_long(
jscontext *cx,long *out);
bool jsval_to_ulong(
jscontext *cx,unsigned long *out);
bool jsval_to_long_long(
jscontext *cx,jsval v,long long* ret);
bool jsval_to_std_string(
jscontext *cx,std::string* ret);
jsval int32_to_jsval(
jscontext *cx,int32_t l);
jsval uint32_to_jsval(
jscontext *cx,uint32_t number );
jsval ushort_to_jsval(
jscontext *cx,unsigned short number );
jsval long_to_jsval(
jscontext *cx,long number );
jsval ulong_to_jsval(
jscontext* cx,unsigned long v);
jsval long_long_to_jsval(
jscontext* cx,long long v);
jsval std_string_to_jsval(
jscontext* cx,const std::string& v);