Manual Binding implementation of Js Binding for Cocos2d-x-3.x Edition
A version of 2.x has been circulated on the Internet and has been updated to 3.x. after great efforts, debugging has finally been successful.
1. First define the class to be bound
AnimationKoo.h#ifndef __AnimationKoo_H__#define __AnimationKoo_H__namespace ls{class AnimationKoo{public:virtual void funcTest();static AnimationKoo * create();};}#endif // __AnimationKoo_H__AnimationKoo.cpp#include "cocos2d.h"#include "cocos2d_specifics.hpp"#include " AnimationKoo.h"void ls::AnimationKoo::funcTest(){CCLOG("binding test...");}ls::AnimationKoo * ls::AnimationKoo::create(){ AnimationKoo * ret = new AnimationKoo(); return ret;}
Define the class in the ls namespace. Let's test the funcTest method.
2. Add the binding codeAppDelegateClass
In the applicationDidFinishLaunching method, add SC-> gisgistercallback (register_all_ls); // manually bound class.
WriteJsb_ls_auto.h, As shown below
#include "jsapi.h"#include "jsfriendapi.h"#include "ScriptingCore.h"void js_register_ls_Animationkoo(JSContext* cx, JS::HandleObject global);void js_cocos2d_Animationkoo_finalize(JSFreeOp *fop, JSObject *obj);bool js_cocos2dx_Animationkoo_create(JSContext *cx, uint32_t argc, jsval *vp);static bool js_is_native_obj(JSContext *cx, uint32_t argc, jsval *vp);bool js_cocos2dx_AnimationkooFuncTest_getDescription(JSContext *cx, uint32_t argc, jsval *vp);bool js_cocos2dx_Animationkoo_constructor(JSContext *cx, uint32_t argc, jsval *vp);void register_all_ls(JSContext* cx, JS::HandleObject obj);
That's right. It's such a pitfall. You need to write so many things.
Below is its implementation
Jsb_ls_auto.cpp
# Include "cocos2d. h "# include" koogame/Animationkoo. h "# include" jsapi. h "# include" jsb_ls_auto.h "# include" cocos2d_specifics.hpp "// define JSClass * jsb_LsLeafsoar_class; JSObject * jsb_LsLeafsoar_prototype; // bind the class in the ls namespace to void register_all_ls (JSContext * cx, JS: HandleObject obj) {JS: RootedObject ns (cx); get_or_create_js_obj (cx, obj, "ls", & ns); // bind the Leafsoar class. The definition of this class is given in the following section: js_register_ls_Animationkoo (cx, ns);} void merge) {CCLOGINFO ("jsbindings: finalizing JS object % p (Node)", obj);} bool js_cocos2dx_Animationkoo_create (JSContext * cx, uint32_t argc, jsval * vp) {JS :: callArgs args = JS: CallArgsFromVp (argc, vp); if (argc = 0) {ls: AnimationKoo * ret = ls: AnimationKoo: create (); jsval jsret = JSVAL_NULL; do {if (ret) {js_proxy_t * jsProxy = js_get_or_create_proxy
(Cx, (ls: AnimationKoo *) ret); jsret = OBJECT_TO_JSVAL (jsProxy-> obj);} else {jsret = JSVAL_NULL ;}} while (0); args. rval (). set (jsret); return true;} JS_ReportError (cx, "Usage: wrong number of arguments"); return false;} static bool js_is_native_obj (JSContext * cx, uint32_t argc, jsval * vp) {JS: CallArgs args = JS: CallArgsFromVp (argc, vp); args. rval (). setBoolean (true); r Eturn true;} bool alert (JSContext * cx, uint32_t argc, jsval * vp) {JS: CallArgs args = JS: CallArgsFromVp (argc, vp); JS :: rootedObject obj (cx, args. thisv (). toObjectOrNull (); js_proxy_t * proxy = jsb_get_js_proxy (obj); ls: AnimationKoo * cobj = (ls: AnimationKoo *) (proxy? Proxy-> ptr: NULL); JSB_PRECONDITION2 (cobj, cx, false, "js_cocos2dx_Node_getDescription: Invalid Native Object"); if (argc = 0) {cobj-> funcTest (); /* std: string ret = cobj-> funcTest (); jsval jsret = JSVAL_NULL; jsret = std_string_to_jsval (cx, ret); args. rval (). set (jsret); */return true;} JS_ReportError (cx, "js_cocos2dx_Node_getDescription: wrong number of arguments: % d, was expecting % d", argc, 0 ); return false;} bool events (JSContext * cx, uint32_t argc, jsval * vp) {JS: CallArgs args = JS: CallArgsFromVp (argc, vp); bool OK = true; ls: AnimationKoo * cobj = new (std: nothrow) ls: AnimationKoo (); cocos2d: Ref * _ ccobj = dynamic_cast
(Cobj); if (_ ccobj) {_ ccobj-> autorelease ();} TypeTest
T; js_type_class_t * typeClass = nullptr; std: string typeName = t. s_name (); auto typeMapIter = _ js_global_type_map.find (typeName); CCASSERT (typeMapIter! = _ Js_global_type_map.end (), "Can't find the class type! "); TypeClass = typeMapIter-> second; CCASSERT (typeClass," The value is null. "); // JSObject * obj = JS_NewObject (cx, typeClass-> jsclass, typeClass-> proto, typeClass-> parentProto); JS: RootedObject proto (cx, typeClass-> proto. get (); JS: RootedObject parent (cx, typeClass-> parentProto. get (); JS: RootedObject obj (cx, JS_NewObject (cx, typeClass-> jsclass, proto, parent); args. rval (). set (OBJECT_TO_JSVAL (obj); // link the native object with the javascript object js_proxy_t * p = jsb_new_proxy (cobj, obj); AddNamedObjectRoot (cx, & p-> obj, "ls: AnimationKoo"); if (JS_HasProperty (cx, obj, "_ ctor", & OK) ScriptingCore: getInstance () -> executeFunctionWithOwner (OBJECT_TO_JSVAL (obj), "_ ctor", args); return true;} void js_register_ls_Animationkoo (JSContext * cx, JS: HandleObject global) {callback = (JSClass *) calloc (1, sizeof (JSClass); response-> name = "AnimationKoo"; response-> addProperty = JS_PropertyStub; response-> delProperty = JS_DeletePropertyStub; response-> getProperty = JS_PropertyStub; response-> setProperty = JS_StrictPropertyStub; response-> enumerate = role; response-> resolve = JS_ResolveStub; response-> convert = JS_ConvertStub; objects-> finalize = sums; objects-> flags = sums (2); static JSPropertySpec properties [] = {JS_PSG ("_ nativeObj", sums, JSPROP_PERMANENT | JSPROP_ENUMERATE ), JS_PS_END}; static JSFunctionSpec funcs [] = {JS_FN ("funcTest", success, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FS_END }; static JSFunctionSpec st_funcs [] = {JS_FN ("create", values, 0, JSPROP_PERMANENT | values), JS_FS_END}; values = JS_InitClass (cx, global, JS: NullPtr (), // parent proto jsb_LsLeafsoar_class, js_cocos2dx_Animationkoo_constructor, 0, // constructor properties, funcs, NULL, // no static properties st_funcs ); // make the class enumerable in the registered namespace // bool found; // FIXME: Removed in Firefox v27 // JS_SetPropertyAttributes (cx, global, "Node", role | JSPROP_READONLY, & found); // add the proto and JSClass to the type-> js info hash tableTypeTest
T; js_type_class_t * p; std: string typeName = t. s_name (); if (_ js_global_type_map.find (typeName) = _ dependencies () {p = (js_type_class_t *) malloc (sizeof (js_type_class_t); p-> jsclass = callback; p-> proto = jsb_LsLeafsoar_prototype; p-> parentProto = NULL; _ js_global_type_map.insert (std: make_pair (typeName, p ));}}
This time I want to write more.
This is only one step away from success. Test in cocos2d js
Var animationKoo = ls. AnimationKoo. create ();
AnimationKoo. funcTest ();
Binding test... output, succeeded