cocos2dx Handwriting JS binding C + +

Source: Internet
Author: User

These two days of continuous access to JS binding C + + a lot of articles, there are manual and self-initiative two ways.

Originally wanted to use their own initiative to bind, but the NDK has not been downloaded ..... Just give it to me.

The following summarizes the implementation of manual binding:

Altogether three steps: 1. Write the original C + + class (typically placed in your own definition class library)

2. Write the corresponding binding code of the member function in C + + (in the manual_binding directory established in the class library of your own definition)

3. Register the functions that are bound (add a function to the AppDelegate.cpp)

4. Write JS code test results

1. Original C + + class:

Test.h#include "Cocos2d.h" Using_ns_cc;class Testbang{public:testbang (); ~testbang ();};

Test.cpp#include "Test.h" Testbang::testbang () {Cclog ("testmyfirstbinding ctor");} Testbang::~testbang () {Cclog ("testmyfirstbinding Destroy");}

2. The corresponding binding function

  jsb_test.h#ifndef testjavascript_jsb_test_h#define testjavascript_jsb_test_h#include "jsapi.h" #include " Jsfriendapi.h "void Register_jsb_test (jscontext* cx, jsobject* Global); #endif

Jsb_test.cpp#include "Jsb_test.h" #include "ScriptingCore.h" #include "test.h"//using_ns_cc_ext; Jsclass *js_test_class; Jsobject *js_test_prototype; Jsbool js_test (Jscontext *cx, uint32_t argc, Jsval *vp) {if (argc = = 0) {//Call C + + constructor test            bang* cobj = new Testbang ();            Jsobject *obj = Js_newobject (CX, Js_test_class, Js_test_prototype, NULL);            Js_set_rval (CX, VP, Object_to_jsval (obj)); Constructs a JS-side object.            The CObj actual object is deposited js_proxy_t* p = jsb_new_proxy (CObj, obj);            Js_addnamedobjectroot (CX, &p->obj, "ty_test");        return js_true;        } js_reporterror (CX, "wrong number of ARGUMENTS:ARGC, was expecting%d. ARGC must is 0", argc); return js_true;} Virtual Machine garbage collection when the callback function, the first parameter represents the runtime, the second is garbage collected JS object void Js_test_finalize (Jsfreeop *fop, Jsobject *obj) {cclog ("jsbindings : Finalizing JS Object%p (ty_tcp) ", obj);} ingress void Register_jsb_test (Jscontext *cx, Jsobject *global) {Js_test_class = (Jsclass *) calloc (1, sizeof (Jsclass));    Js_test_class->name = "Ty_test";    Js_test_class->addproperty = js_propertystub;    Js_test_class->delproperty = js_propertystub;    Js_test_class->getproperty = js_propertystub;    Js_test_class->setproperty = js_strictpropertystub;    Js_test_class->enumerate = js_enumeratestub;    Js_test_class->resolve = js_resolvestub;    Js_test_class->convert = js_convertstub;    Js_test_class->finalize = js_test_finalize;        Js_test_class->flags = Jsclass_has_reserved_slots (2); To register the properties of static Jspropertyspec properties[] = {//script layer itself by callback to set the current connection state, here is not set//{"Curstate", 0, Jsprop_ Enumerate | jsprop_permanent |        Jsprop_shared, Jsop_wrapper (js_tuyoo_tcpsocket_get_curstate), NULL}, {0, 0, 0, 0, 0}};        instance function static Jsfunctionspec funcs[] = {Js_fs_end};        class function static Jsfunctionspec st_funcs[] = {Js_fs_end};  Js_test_prototype = Js_initclass (                                        CX, Global, NULL,                                          Js_test_class,//Jsclass class Js_test, 0,//constructor in virtual machine                                          Properties, Funcs,    NULL,//No static properties St_funcs);        jsobject* obj = js_newobject (cx, NULL, NULL, NULL); This corresponds to a JS constructor. Use the new Ty_tcp () method in JS to make use of this native class//jsobject* Jsclassobj = Jsval_to_object (anonevaluate (CX, Global, "(function () {R Eturn ty_tcp;        })()"));    Register to global variable Jsbool found; Js_setpropertyattributes (CX, Global, "ty_tcp", Jsprop_enumerate | Jsprop_readonly, &found);}

3. Register binding function

Appdelegate.cpp#include "Manual_bindings/jsb_test.h"
BOOL Appdelegate::applicationdidfinishlaunching () Sc->addregistercallback (register_jsb_test);


4. Write the JS code test

var testobj = new Ty_test ();

Output: testmyfirstbinding ctor


cocos2dx Handwriting JS binding C + +

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.