COCOS2D-JS 3.0 RC0 Manually binding C + + calls Js,js calling C + + jsbinding

Source: Internet
Author: User

Reference: http://www.tairan.com/archives/4902

Reference article is version 2.x, for 3.0 may not fit, did not delve into.

Code: https://github.com/kenkozheng/cocos2d-js/tree/master/jsbinding (Cpp_js%20js_cpp)

1 JS call C + +

3.0 Write this binding is relatively simple, with Ane call Java, a jscontext, a jsval, using the cocos2d provided by the C + + and JS variable conversion function to do a good conversion.

COCOS2D-JS originally defined the code style:

    Sc->Addregistercallback (minxmlhttprequest::_js_register);     SC--Addregistercallback (register_jsb_websocket);     SC--Addregistercallback (Register_jsb_socketio)         ; #if     SC--Addregistercallback (Javascriptjavabridge::_js_register)     ; #endif         SC--Addregistercallback (Register_jsb_kenko_all);         SC->start ();

We also follow this style by adding a function: Register_jsb_kenko_all, which is a global function.

#define jsb_jsb_kenko_auto_h"cocos2d.h"std::string BOOL Jsb_os_info (Jscontext *cx, uint32_t argc, Js::value *bool jsb_   Callback (Jscontext *cx, uint32_t argc, Js::value *void register_jsb_kenko_all (jscontext* CX, jsobject* obj); #endif
Jsb_kenko_auto.cpp#include"jsb_kenko_auto.h"#include"cocos2d_specifics.hpp"std::stringOs_info () {Cclog ("it ' s C + + Os_info here"); return "Os_info"; } BOOLJsb_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; } voidRegister_jsb_kenko_all (Jscontext *cx, Jsobject *obj) {js_definefunction (CX, obj,"OsInfo", Jsb_os_info,0,0); Generate a JS global function named Osinfo
}

Put the H and CPP files in the same place as the AppDelegate.cpp. The above C + + code will generate the corresponding JS interface in the SpiderMonkey runtime environment, so we do not need to write the corresponding JS interface.

Then you can write the JS code to try. From the running result you can see that the JS call succeeds and gets the return value.

Cc.game.onStart = function () {     cc.view.setDesignResolutionSize (), CC. Resolutionpolicy.show_all);     Cc.view.resizeWithBrowserSize (true);     Cc.director.runScene (new  mainscene ());         Cc.log ("" + OsInfo ()); Cc.game.run ();

2 C + + callbacks

The key is to invoke JS using the method provided by Scriptingcore. First look at the source code of Scriptingcore, there are some ways to use.

Executefunctionwithowner can implement the call of C + + objects and JS objects like Cc.sprite. Here's how to make a global call.

Evalstring for any of the front-end development is not too unfamiliar, after all, this is not a browser, excluding a variety of chaotic security issues, we directly use this function.

/** @brief Execute a scripted global function.      @brief the function should not take any parameters and should return an integer. @param functionname String Object holding the name of the function, in the global script environment, which is to be execut      Ed.      @return The integer value returned from the script function. */     Virtual intExecuteglobalfunction (Const Char* functionname) {return 0; } Virtual intSendevent (cocos2d::scriptevent* message)Override; Virtual BOOLParseconfig (Configtype type,ConstSTD::string& str)Override; Virtual BOOLHandleassert (Const Char*MSG) {return false; } Virtual voidSetcalledfromscript (BOOLCallfromscript) {_callfromscript =Callfromscript;}; Virtual BOOLIscalledfromscript () {return_callfromscript;}; BOOLExecutefunctionwithobjectdata (void* Nativeobj,Const Char*name, Jsobject *obj); BOOLExecutefunctionwithowner (jsval owner,Const Char*name, uint32_t argc =0, jsval* VP = NULL, jsval* retVal =NULL); voidExecutejsfunctionwiththisobj (Jsval thisobj, Jsval callback, uint32_t argc =0, jsval* VP = NULL, jsval* retVal =NULL); /** would eval the specified string * @param string The string with the JavaScript code to be evaluated      * @param outval The jsval that would hold the return value of the evaluation.      * Can be NULL. */     BOOLEvalstring (Const Char*string, Jsval *outval,Const Char*filename = null, jscontext* CX = null, jsobject*Global= NULL);

Modify Jsb_kenko_auto.cpp:

#include"jsb_kenko_auto.h"#include"cocos2d_specifics.hpp"std::stringOs_info () {Cclog ("it ' s C + + Os_info here"); return "Os_info"; }BOOLJsb_callback (Jscontext *cx, uint32_t argc, Js::value *VP) {Cclog ("it ' s C + + Testcallback here"); Jscontext* JC = Scriptingcore::getinstance ()Getglobalcontext (); //the comment section is appropriate for an object-aware invocation//Reference:http://www.tairan.com/archives/4902     //Jsval v[2]; //v[0] = Int32_to_jsval (JC, 32); //v[1] = Int32_to_jsval (JC, 12); //The implementation of callbacks through the Scriptingcore packaged method can help us save a lot of detail on the research//js_proxy_t * p = jsb_get_native_proxy (); //return Scriptingcore::getinstance ()->executefunctionwithowner (Object_to_jsval (p->obj), "Cpp_callback",        2, V); //2 is the number of arguments, and V is the parameter list//find a way to better fit the global functionjsval ret; returnScriptingcore::getinstance ()->evalstring ("cpp_callback (2,3)", &ret); }   BOOLJsb_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; } voidRegister_jsb_kenko_all (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); }

Add a global function to the C + + call on the JS side accordingly.

Cc.game.onStart =function () {cc.view.setDesignResolutionSize ( -, the, CC.     Resolutionpolicy.show_all); Cc.view.resizeWithBrowserSize (true); Cc.director.runScene (NewMainscene ()); Cc.log ("JS get from C + +:"+osInfo ()); Test_cpp_callback (); }; Cc.game.run (); function Cpp_callback (A, b) {Cc.log ("cpp return the integer:"+ A +" "+b); }

Look at the results of the output:

  

3 various variable conversion functions

It's all in the js_manual_conversions.h. Only part of the list is listed below.

BOOLJsval_to_ushort (Jscontext *cx, Jsval VP, unsigned Short*ret); BOOLJsval_to_int32 (Jscontext *cx, Jsval VP, int32_t *ret); BOOLJsval_to_uint32 (Jscontext *cx, Jsval VP, uint32_t *ret); BOOLJsval_to_uint16 (Jscontext *cx, Jsval VP, uint16_t *ret); BOOLJsval_to_long (Jscontext *cx, VP of Jsval,Long* out); BOOLJsval_to_ulong (Jscontext *cx, Jsval VP, unsignedLong* out); BOOLJsval_to_long_long (Jscontext *cx, Jsval V,Long Long*ret); BOOLJsval_to_std_string (Jscontext *cx, Jsval V, 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 ShortNumber ); Jsval Long_to_jsval (Jscontext*CX,LongNumber ); Jsval Ulong_to_jsval (Jscontext* CX, unsignedLongv); Jsval Long_long_to_jsval (Jscontext* CX,Long Longv); Jsval Std_string_to_jsval (Jscontext* CX,ConstSTD::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.