[Cocos2d-x from c ++ to js] 15: puppet Constructor

Source: Internet
Author: User

In the previous article, we used Sprite as an example to analyze the registration function. However, we seem to have omitted a point, that is, the constructor. Because the Cocos2d-x in the C ++ layer uses workshop functions to generate objects, rather than constructors. Therefore, in the JS-Layer Code, corresponding mechanisms are also required to handle this issue.


Let's take a look at jsb_cocos2dx_auto.hpp.

extern JSClass  *jsb_cocos2d_Sprite_class;extern JSObject *jsb_cocos2d_Sprite_prototype;JSBool js_cocos2dx_Sprite_constructor(JSContext *cx, uint32_t argc, jsval *vp);void js_cocos2dx_Sprite_finalize(JSContext *cx, JSObject *obj);void js_register_cocos2dx_Sprite(JSContext *cx, JSObject *global);void register_all_cocos2dx(JSContext* cx, JSObject* obj);


This declares several important objects and functions. JSClass objects and prototype objects, registration functions, and self-implemented finalize Stub. However, we found that the js_cocos2dx_Sprite_constructor constructor has no corresponding implementation code, just a declaration.


It should be noted that, based on the prototype inheritance of JS, We need to input a constructor when generating the jsb_cocos2d_Sprite_prototype prototype, and the constructor is not implemented, so how did he do it?


In the js_register_cocos2dx_Sprite function, view the code that generates the prototype jsb_cocos2d_Sprite_prototype:

jsb_cocos2d_Sprite_prototype = JS_InitClass(    cx, global,    jsb_cocos2d_Node_prototype,    jsb_cocos2d_Sprite_class,    dummy_constructor<cocos2d::Sprite>, 0, // no constructor    properties,    funcs,    NULL, // no static properties    st_funcs);

Note that the fifth parameter is a template function dummy_constructor <cocos2d: Sprite>, which literally means a dummy constructor.


Let's take a look at the template function definition.

template<class T>static JSBool dummy_constructor(JSContext *cx, uint32_t argc, jsval *vp) {    JS::RootedValue initializing(cx);    JSBool isNewValid = JS_TRUE;    JSObject* global = ScriptingCore::getInstance()->getGlobalObject();    isNewValid = JS_GetProperty(cx, global, "initializing", &initializing) && JSVAL_TO_BOOLEAN(initializing);    if (isNewValid)    {        TypeTest<T> 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 *_tmp = JS_NewObject(cx, typeClass->jsclass, typeClass->proto, typeClass->parentProto);        JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(_tmp));        return JS_TRUE;    }    JS_ReportError(cx, "Don't use `new cc.XXX`, please use `cc.XXX.create` instead! ");    return JS_FALSE;}

This function first uses the JS: RootedValue type to determine whether the GlobalObject object initialization is complete. JS: The specific principle of RootedValue does not need to be further explored. You only need to know that this is a memory management method of the SpiderMonkey engine.


Then I used a very interesting technique. I used a template class TypeTest <T> t to retrieve the corresponding type name. This is a good way to write the code without damaging the function signature, so that the function can match the parameter type of JS_InitClass and obtain the required information in different contexts. Let's take a look at the implementation of TypeTest, which is of great reference in many cases!

template< typename DERIVED >class TypeTest{public:    static const char* s_name()    {        // return id unique for DERIVED        // ALWAYS VALID BUT STRING, NOT INT - BUT VALID AND CROSS-PLATFORM/CROSS-VERSION COMPATBLE        // AS FAR AS YOU KEEP THE CLASS NAME        return typeid( DERIVED ).name();    }};


Finally, we query the corresponding type in _ js_global_type_map, obtain the corresponding parameters to call the JS_NewObject function, generate the corresponding object, and set it as the return value.







This article is from the "Old G hut" blog, please be sure to keep this source http://4137613.blog.51cto.com/4127613/1357886

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.