博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【cocos2d-x从c++到js】傀儡构造函数
阅读量:1969 次
发布时间:2019-04-27

本文共 2758 字,大约阅读时间需要 9 分钟。

上篇我们以Sprite为例,分析了注册函数。但其中我们似乎遗漏了一个地方,那就是构造函数。因为Cocos2d-x在C++层使用的是工场函数来生成对象,而不是构造函数。所以在JS层代码中,也需要有相应的对应机制来处理这件事。


看一下jsb_cocos2dx_auto.hpp

1
2
3
4
5
6
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);


这声明了几个重要的对象和函数。JSClass对象和原型对象、注册函数、自己实现的finalize的Stub等。但是我们发现js_cocos2dx_Sprite_constructor构造函数并没有对应的实现代码,仅仅是一个声明而已。


需要注意的是,根据JS的原型继承,我们在生成jsb_cocos2d_Sprite_prototype原型时,需要传入一个构造函数,而构造函数js_cocos2dx_Sprite_constructor又是未实现的,那么他是如何做到的呢?


在js_register_cocos2dx_Sprite函数中查看生成jsb_cocos2d_Sprite_prototype原型的代码:

1
2
3
4
5
6
7
8
9
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);

注意到第五个参数是一个模板函数dummy_constructor<cocos2d::Sprite>,字面意思是傀儡构造函数。


看一下这个模板函数的定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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;
}

这个函数首先使用了JS::RootedValue类型的量来判断GlobalObject对象是否初始化完毕。JS::RootedValue具体的原理暂时不用深究,你只需要知道这是SpiderMonkey引擎的一种内存管理方式即可。


然后使用了一个非常有趣的技巧,用一个模板类TypeTest<T> t,取出对应的类型名。这是一个很不错的写法,能够不破坏函数签名,使得函数能够匹配JS_InitClass的参数类型,又能够在不同的上下文中里面获得需要的信息。我们看一下TypeTest的实现,这种写法在很多时候有很大的借鉴意义!

1
2
3
4
5
6
7
8
9
10
11
12
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();
    
}
};


最后我们在_js_global_type_map里查询对应的类型,取出相应的参数来调用JS_NewObject函数,生成对应的对象并设置为返回值。

转载地址:http://fzypf.baihongyu.com/

你可能感兴趣的文章
tf callbacks
查看>>
keras、tf、numpy实现logloss对比
查看>>
Ubuntu20.04安装微信
查看>>
Restful风格的使用
查看>>
Swagger基础入门整合SpringBoot
查看>>
MyBatisPlus简单入门(SpringBoot)
查看>>
攻防世界web进阶区NewsCenter详解
查看>>
攻防世界web进阶PHP2详解
查看>>
如何解决词达人问题(新)
查看>>
攻防世界web进阶区surpersqli详解
查看>>
攻防世界web进阶区easytornado详解
查看>>
攻防世界web进阶区web2详解
查看>>
xss-labs详解(上)1-10
查看>>
xss-labs详解(下)11-20
查看>>
攻防世界web进阶区ics-05详解
查看>>
攻防世界web进阶区FlatScience详解
查看>>
攻防世界web进阶区ics-04详解
查看>>
攻防世界web进阶区Cat详解
查看>>
攻防世界web进阶区bug详解
查看>>
攻防世界web进阶区ics-07详解
查看>>