Cocos2d-js 3.0 RC0 manually bound C ++ call js, js call C ++ jsbinding

Source: Internet
Author: User

Cocos2d-js 3.0 RC0 manually bound C ++ call js, js call C ++ jsbinding
1. JS calls C ++ 3.0 to write this binding, which is quite simple, just like ANE calls java, A JSContext, A jsval, use the c ++ and js variable conversion functions provided by cocos2d to complete the conversion. Cocos2d-js originally defined the code style: copy the code SC-> addRegisterCallback (MinXmlHttpRequest: _ js_register); SC-> addRegisterCallback (register_jsb_websocket); SC-> addRegisterCallback (callback ); # if (CC_TARGET_PLATFORM = CC_PLATFORM_ANDROID) SC-> addRegisterCallback (JavascriptJavaBridge: _ js_register); # endif SC-> addRegisterCallback (callback); SC-> start (); copy the code and follow this style to add a function: register_jsb_kenko _ All. This is a global function. Copy the code 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, uint32_t argc, JS:: Value * vp); void Merge (JSContext * cx, JSObject * obj); # endif copy the code to copy the code jsb_kenko_auto.cpp # include "jsb_kenko_auto.h" # include "cocos2d_specifics.hp P "std: string OS _info () {CCLOG (" it's c ++ OS _info here "); return" OS _info ";} bool jsb_ OS _info (JSContext * cx, uint32_t argc, JS: Value * vp) {jsval ret = std_string_to_jsval (cx, OS _info (); JS_SET_RVAL (cx, vp, ret); return true;} void trim (JSContext * cx, JSObject * obj) {JS_DefineFunction (cx, obj, "osInfo", jsb_ OS _info, 0, 0 ); // generate a js global function named osInfo} copy the code and put both the h and cpp files to AppDelegate. cpp In the same region. The above c ++ code will generate the corresponding js interface in the spidermonkey runtime environment. Therefore, we do not need to write the corresponding js interface by ourselves. Then you can try writing js Code. The running result shows that the js call is successful and the return value is obtained. Copy the cc code. game. onStart = function () {cc. view. setDesignResolutionSize (800,450, cc. resolutionPolicy. SHOW_ALL); cc. view. resizeWithBrowserSize (true); cc. director. runScene (new MainScene (); cc. log ("js get from c ++:" + osInfo () ;}; cc. game. run (); the key to copying Code 2 C ++ callback is to use the method provided by ScriptingCore to call js. First, let's take a look at the source code of ScriptingCore. ExecuteFunctionWithOwner can call c ++ objects and js objects like cc. sprite without further research. This example shows how to make a global call. EvalString is no stranger to any front-end development. After all, it is not a browser. We can use this function to eliminate all kinds of messy security issues. Copy the code/** @ brief Execute a scripted global function. @ brief The function shocould not take any parameters and shocould return an integer. @ param 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 parseConfig (ConfigType 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 handle (void * nativeObj, const char * name, JSObject * obj); bool executeFunctionWithOwner (jsval owner, const char * name, uint32_t argc = 0, jsval * vp = NULL, jsval * retVal = NULL); void callback (jsval thisObj, jsval callback, uint32_t argc = 0, jsval * vp = NULL, jsval * retVal = NULL ); /*** will eval the specified string * @ param string The string with the javascript code to be evaluated * @ param 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); copy the code to modify jsb_kenko_auto.cpp: copy the Code # 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, uint32_t argc, JS: Value * vp) {CCLOG ("it's c ++ testCallback here"); JSContext * jc = ScriptingCore :: getInstance ()-> getGlobalContext (); // The annotation part is suitable for calling objects. // reference: http://www.tairan.com/archives/4902 // Jsval v [2]; // v [0] = int32_to_jsval (jc, 32); // v [1] = int32_to_jsval (jc, 12 ); // implement callback through the method encapsulated by ScriptingCore, which can help us save a lot of research details. // js_proxy_t * p = jsb_get_native_proxy (); // return ScriptingCore: getInstance () -> executeFunctionWithOwner (OBJECT_TO_JSVAL (p-> obj), "cpp_callback", 2, v); // 2 indicates the number of parameters, v is the parameter list // find a method that is more suitable for global functions. jsval ret; return ScriptingCore: getInstance ()-> evalString ("cpp_callback (2, 3)", & ret) ;} Bool jsb_ OS _info (JSContext * cx, uint32_t argc, JS: Value * vp) {jsval ret = std_string_to_jsval (cx, OS _info (); JS_SET_RVAL (cx, vp, ret); return true;} void evaluate (JSContext * cx, JSObject * obj) {JS_DefineFunction (cx, obj, "osInfo", jsb_ OS _info, 0, 0); JS_DefineFunction (cx, obj, "test_cpp_callback", jsb_callback, 0, 0);} copy the code and add a global function on the js side to call c ++. Copy the cc code. game. onStart = function () {cc. view. setDesignResolutionSize (800,450, cc. resolutionPolicy. SHOW_ALL); cc. view. resizeWithBrowserSize (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);} 3 various variable conversion functions are available in js_manual_conversions.h. Only a part is listed below. Copy the code bool kernel (JSContext * cx, jsval vp, unsigned short * ret); bool jsval_to_int32 (JSContext * cx, jsval vp, int32_t * ret); bool kernel (JSContext * cx, jsval vp, uint32_t * ret); bool kernel (JSContext * cx, jsval vp, uint16_t * ret); bool jsval_to_long (JSContext * cx, jsval vp, long * out ); bool jsval_to_ulong (JSContext * cx, jsval vp, unsigned long * out); bool destroy (JSContext * cx, jsval v, long * ret); bool jsval_to_std_string (JSContext * cx, jsval v, std: string * ret); jsval kernel (JSContext * cx, int32_t l); jsval kernel (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 trim (JSContext * cx, long v ); jsval std_string_to_jsval (JSContext * cx, const std: string & v );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.