1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
class
JSCallbackWrapper:
public
cocos2d::Object {
:
JSCallbackWrapper();
virtual
~JSCallbackWrapper();
void
setJSCallbackFunc(jsval obj);
setJSCallbackThis(jsval thisObj);
setJSExtraData(jsval data);
const
jsval& getJSCallbackFunc()
;
jsval& getJSCallbackThis()
;
jsval& getJSExtraData()
;
protected
:
jsval _jsCallback;
jsval _jsThisObj;
jsval _extraData;
};
|
// cc.CallFunc.create( func,this,[data])
// cc.CallFunc.create( func )
static
JSBool js_callFunc(JSContext *cx,uint32_t argc,jsval *vp)
{
if
(argc >= 1 && argc <= 3) {
jsval *argv = JS_ARGV(cx,vp);
std::shared_ptr<JSCallbackWrapper> tmpCobj(
new
JSCallbackWrapper());
tmpCobj->setJSCallbackFunc(argv[0]);
(argc >= 2) {
tmpCobj->setJSCallbackThis(argv[1]);
}
(argc == 3) {
tmpCobj->setJSExtraData(argv[2]);
}
CallFuncN *ret = CallFuncN::create([=](Node* sender){
jsval& jsvalThis = tmpCobj->getJSCallbackThis();
jsval& jsvalCallback = tmpCobj->getJSCallbackFunc();
jsval& jsvalExtraData = tmpCobj->getJSExtraData();
bool
hasExtraData = !JSVAL_IS_VOID(jsvalExtraData);
JSObject* thisObj = JSVAL_IS_VOID(jsvalThis) ? nullptr : JSVAL_TO_OBJECT(jsvalThis);
JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET
js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::Node>(cx,sender);
jsval retval;
(jsvalCallback != JSVAL_VOID)
{
(hasExtraData)
{
jsval valArr[2];
valArr[0] = OBJECT_TO_JSVAL(proxy->obj);
valArr[1] = jsvalExtraData;
JS_AddValueRoot(cx,valArr);
JS_CallFunctionValue(cx,thisObj,jsvalCallback,2,valArr,&retval);
JS_RemoveValueRoot(cx,valArr);
}
else
{
jsval senderVal = OBJECT_TO_JSVAL(proxy->obj);
}
}
// I think the JSCallFuncWrapper isn't needed.
});
js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::CallFunc>(cx,ret);
JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(proxy->obj));
JS_SetReservedSlot(proxy->obj,argv[0]);
(argc > 1) {
}
// if(argc == 3) {
// JS_SetReservedSlot(proxy->obj,argv[2]);
// }
return
JS_TRUE;
}
JS_ReportError(cx,
"Invalid number of arguments"
);
JS_FALSE;
}