cocos2dx中javascript绑定c++

本文详细介绍了一种通过四步骤实现C++类与JavaScript交互的方法:编写原始C++类、编写成员函数绑定代码、注册绑定函数及编写测试JS代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面总结一下手动绑定的实现过程 : 

一共三步 : 



绑定所需的spidermonkey目录如上

1 . 写原始 C++ 类 ( 一般放在自定义类库里 )

2.  用 C++ 逐个写 成员函数对应 的 绑定代码 ( 在自定义类库中建立的manual_binding文件夹里)

3.  注册所绑定过的函数( 在AppDelegate.cpp中 添加注册函数 )

4.  写js代码测试效果

1 . 原始C++类 :

  1. //test.h  
  2. #include "cocos2d.h"  
  3. USING_NS_CC;  
  4. class testbang  
  5. {  
  6. public:  
  7.     testbang();  
  8.     ~testbang();  
  9.       
  10. };  

  1. //test.cpp  
  2. #include "test.h"  
  3.   
  4. testbang::testbang()  
  5. {  
  6.     CCLog("testmyfirstbinding ctor ");  
  7. }  
  8. testbang::~testbang()  
  9. {  
  10.     CCLog("testmyfirstbinding destroy");  
  11. }  

2.对应的绑定函数


  1. //  jsb_test.h  
  2. #ifndef TestJavascript_jsb_test_h  
  3. #define TestJavascript_jsb_test_h  
  4.   
  5. #include "jsapi.h"  
  6. #include "jsfriendapi.h"  
  7.   
  8.   
  9. void register_jsb_test(JSContext* cx, JSObject* global);  
  10.   
  11. #endif  

  1. //jsb_test.cpp  
  2. #include "jsb_test.h"  
  3. #include "ScriptingCore.h"  
  4. #include "test.h"  
  5.   
  6. //USING_NS_CC_EXT;  
  7.   
  8. JSClass  *js_test_class;  
  9. JSObject *js_test_prototype;  
  10.   
  11. JSBool js_test(JSContext *cx, uint32_t argc, jsval *vp)  
  12. {  
  13.         if (argc == 0) {  
  14.             // 调用 C++ 构造函数  
  15.             testbang* cobj = new testbang();  
  16.             JSObject *obj = JS_NewObject(cx, js_test_class, js_test_prototype, NULL);  
  17.             JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));  
  18.             // 构造 js 端对象,将 cobj 实际对象存入  
  19.             js_proxy_t* p = jsb_new_proxy(cobj, obj);  
  20.             JS_AddNamedObjectRoot(cx, &p->obj, "TY_test");  
  21.             return JS_TRUE;  
  22.         }  
  23.         JS_ReportError(cx, "wrong number of arguments: argc, was expecting %d. argc must be 0 ", argc);  
  24.         return JS_TRUE;  
  25. }  
  26. // 虚拟机垃圾回收时的回调函数,第一个参数代表runtime,第二个是被垃圾回收的js对象  
  27. void js_test_finalize(JSFreeOp *fop, JSObject *obj) {  
  28.     CCLOG("jsbindings: finalizing JS object %p (TY_TCP)", obj);  
  29. }  
  30. // 入口  
  31. void register_jsb_test(JSContext *cx, JSObject *global) {  
  32.   
  33.     js_test_class = (JSClass *)calloc(1, sizeof(JSClass));  
  34.     js_test_class->name = "TY_test";  
  35.     js_test_class->addProperty = JS_PropertyStub;  
  36.     js_test_class->delProperty = JS_PropertyStub;  
  37.     js_test_class->getProperty = JS_PropertyStub;  
  38.     js_test_class->setProperty = JS_StrictPropertyStub;  
  39.     js_test_class->enumerate = JS_EnumerateStub;  
  40.     js_test_class->resolve = JS_ResolveStub;  
  41.     js_test_class->convert = JS_ConvertStub;  
  42.     js_test_class->finalize = js_test_finalize;  
  43.     js_test_class->flags = JSCLASS_HAS_RESERVED_SLOTS(2);  
  44.       
  45.     // 要注册的属性  
  46.     static JSPropertySpec properties[] = {  
  47.         // 脚本层自己通过回调来设定当前的连接状态,这里就不设置了  
  48. //        {"curState", 0, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_SHARED, JSOP_WRAPPER(js_tuyoo_TCPSocket_get_curState), NULL},  
  49.         {0, 0, 0, 0, 0}  
  50.     };  
  51.       
  52.     // 实例函数  
  53.     static JSFunctionSpec funcs[] = {  
  54.   
  55.         JS_FS_END  
  56.     };  
  57.       
  58.     // 类函数  
  59.     static JSFunctionSpec st_funcs[] = {  
  60.         JS_FS_END  
  61.     };  
  62.       
  63.     js_test_prototype = JS_InitClass(  
  64.                                           cx, global,  
  65.                                           NULL,  
  66.                                           js_test_class, // 虚拟机内的JSClass类  
  67.                                           js_test, 0, // 构造函数  
  68.                                           properties,  
  69.                                           funcs,  
  70.                                           NULL, // no static properties  
  71.                                           st_funcs);  
  72.       
  73. //    JSObject* obj = JS_NewObject(cx, NULL, NULL, NULL);  
  74.       
  75.     // 这个对应一个js的构造函数,在js中使用 new TY_TCP()的方式来使用这个native的类  
  76. //    JSObject* jsclassObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return TY_TCP; })()"));  
  77.       
  78.     // 注册到全局变量中  
  79.     JSBool found;  
  80.     JS_SetPropertyAttributes(cx, global, "TY_TCP", JSPROP_ENUMERATE | JSPROP_READONLY, &found);  

3 . 注册绑定函数


  1. //AppDelegate.cpp  
  2. #include "manual_bindings/jsb_test.h" 

  1. //bool AppDelegate::applicationDidFinishLaunching()  
  2. sc->addRegisterCallback(register_jsb_test); 

4 . 写js代码测试


var testobj = new TY_test(); 


输出 :testmyfirstbinding ctor


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值