2014-01-26 16:14:18
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;
};
|
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// 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.
@H_473_502@// Since an action will be run by a cc.Node,it will be released at the Node::cleanup.
@H_473_502@// By James Chen
@H_473_502@// JSCallFuncWrapper::setTargetForNativeNode(node,(JSCallFuncWrapper *)this);
});
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]);
// }
@H_473_502@// test->execute();
return
JS_TRUE;
}
JS_ReportError(cx,
"Invalid number of arguments"
);
JS_FALSE;
}
10
CallFuncN * CallFuncN::create(
std::function<
(Node*)> &func)
{
auto
ret =
CallFuncN();
(ret && ret->initWithFunction(func) ) {
ret->autorelease();
ret;
}
CC_SAFE_DELETE(ret);
nullptr;
}