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); |
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); |
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; } |
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(); } }; |