From 3597ce7c434432674084de07b4174a47bdfcba86 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Fri, 3 Jun 2016 11:01:06 +0800 Subject: [PATCH] Fix scheduler callback target lost issue --- .../js-bindings/manual/cocos2d_specifics.cpp | 63 +++++++++---------- .../js-bindings/manual/cocos2d_specifics.hpp | 2 - 2 files changed, 28 insertions(+), 37 deletions(-) diff --git a/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp b/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp index 9cecd48df8..a638609c9b 100644 --- a/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp +++ b/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp @@ -734,7 +734,26 @@ JSCallbackWrapper::JSCallbackWrapper(JS::HandleValue owner) JSCallbackWrapper::~JSCallbackWrapper() { - reset(); + JSContext* cx = ScriptingCore::getInstance()->getGlobalContext(); + JS::RootedValue ownerVal(cx, _owner); + if (!ownerVal.isNullOrUndefined()) + { + JS::RootedValue target(cx, _jsCallback); + if (!target.isNullOrUndefined()) + { + js_remove_object_reference(ownerVal, target); + } + target.set(_jsThisObj); + if (!target.isNullOrUndefined()) + { + js_remove_object_reference(ownerVal, target); + } + target.set(_extraData); + if (!target.isNullOrUndefined()) + { + js_remove_object_reference(ownerVal, target); + } + } } void JSCallbackWrapper::setJSCallbackFunc(JS::HandleValue func) { @@ -806,33 +825,6 @@ const jsval JSCallbackWrapper::getJSExtraData() const return _extraData; } -void JSCallbackWrapper::reset() -{ - JSContext* cx = ScriptingCore::getInstance()->getGlobalContext(); - JS::RootedValue ownerVal(cx, _owner); - if (!ownerVal.isNullOrUndefined()) - { - JS::RootedValue target(cx, _jsCallback); - if (!target.isNullOrUndefined()) - { - js_remove_object_reference(ownerVal, target); - } - target.set(_jsThisObj); - if (!target.isNullOrUndefined()) - { - js_remove_object_reference(ownerVal, target); - } - target.set(_extraData); - if (!target.isNullOrUndefined()) - { - js_remove_object_reference(ownerVal, target); - } - } - _jsCallback = JS::NullValue(); - _jsThisObj = JS::NullValue(); - _extraData = JS::NullValue(); -} - // cc.CallFunc.create( func, this, [data]) // cc.CallFunc.create( func ) static bool js_callFunc(JSContext *cx, uint32_t argc, jsval *vp) @@ -1225,7 +1217,6 @@ void JSScheduleWrapper::removeTargetForJSObject(JS::HandleObject jsTargetObj, JS free(removed); } } - target->reset(); dump(); CCLOGINFO("removeTargetForJSObject end"); } @@ -1272,12 +1263,14 @@ void JSScheduleWrapper::scheduleFunc(float dt) JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET JS::RootedValue callback(cx, getJSCallbackFunc()); - if(!callback.isNullOrUndefined()) { - JS::HandleValueArray args = JS::HandleValueArray::fromMarkedLocation(1, &data); - JS::RootedValue retval(cx); - JS::RootedValue targetVal(cx, getJSCallbackThis()); - if (!targetVal.isNullOrUndefined()) { - JS::RootedObject target(cx, targetVal.toObjectOrNull()); + if(!callback.isNullOrUndefined()) + { + auto exist = JSScheduleWrapper::getTargetForSchedule(callback); + if (exist) + { + JS::HandleValueArray args = JS::HandleValueArray::fromMarkedLocation(1, &data); + JS::RootedValue retval(cx); + JS::RootedObject target(cx, getJSCallbackThis().toObjectOrNull()); JS_CallFunctionValue(cx, target, callback, args, &retval); } } diff --git a/cocos/scripting/js-bindings/manual/cocos2d_specifics.hpp b/cocos/scripting/js-bindings/manual/cocos2d_specifics.hpp index 8cf8cbfc0f..f83b56bf56 100644 --- a/cocos/scripting/js-bindings/manual/cocos2d_specifics.hpp +++ b/cocos/scripting/js-bindings/manual/cocos2d_specifics.hpp @@ -148,8 +148,6 @@ public: const jsval getJSCallbackFunc() const; const jsval getJSCallbackThis() const; const jsval getJSExtraData() const; - - void reset(); protected: JS::Heap _owner; JS::Heap _jsCallback;