From 766082134f92d86743a32252bb7a0fdcfa1f6354 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Wed, 11 Jan 2017 11:10:42 +0800 Subject: [PATCH 1/3] Reference JS actions by C++ refs --- native/cocos/2d/CCAction.cpp | 22 +++++++++++++ native/cocos/2d/CCActionInterval.cpp | 48 +++++++++++++++++++++++++++- native/cocos/2d/CCActionManager.cpp | 38 ++++++++++++++++++---- 3 files changed, 101 insertions(+), 7 deletions(-) diff --git a/native/cocos/2d/CCAction.cpp b/native/cocos/2d/CCAction.cpp index 2171ef0279..afc3621fc3 100644 --- a/native/cocos/2d/CCAction.cpp +++ b/native/cocos/2d/CCAction.cpp @@ -132,6 +132,13 @@ bool Speed::initWithAction(ActionInterval *action, float speed) action->retain(); _innerAction = action; +#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS + auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine(); + if (sEngine) + { + sEngine->retainScriptObject(this, _innerAction); + } +#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS _speed = speed; return true; } @@ -186,6 +193,14 @@ void Speed::setInnerAction(ActionInterval *action) { if (_innerAction != action) { +#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS + auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine(); + if (sEngine) + { + sEngine->releaseScriptObject(this, _innerAction); + sEngine->retainScriptObject(this, action); + } +#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS CC_SAFE_RELEASE(_innerAction); _innerAction = action; CC_SAFE_RETAIN(_innerAction); @@ -243,6 +258,13 @@ bool Follow::initWithTargetAndOffset(Node *followedNode, float xOffset,float yOf followedNode->retain(); _followedNode = followedNode; +#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS + auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine(); + if (sEngine) + { + sEngine->retainScriptObject(this, _followedNode); + } +#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS _worldRect = rect; _boundarySet = !rect.equals(Rect::ZERO); _boundaryFullyCovered = false; diff --git a/native/cocos/2d/CCActionInterval.cpp b/native/cocos/2d/CCActionInterval.cpp index d11d92e340..2a759f2bc6 100644 --- a/native/cocos/2d/CCActionInterval.cpp +++ b/native/cocos/2d/CCActionInterval.cpp @@ -287,6 +287,15 @@ bool Sequence::initWithTwoActions(FiniteTimeAction *actionOne, FiniteTimeAction _actions[1] = actionTwo; actionTwo->retain(); + +#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS + auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine(); + if (sEngine) + { + sEngine->retainScriptObject(this, actionOne); + sEngine->retainScriptObject(this, actionTwo); + } +#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS return true; } @@ -448,11 +457,18 @@ bool Repeat::initWithAction(FiniteTimeAction *action, unsigned int times) _times = times; _innerAction = action; action->retain(); +#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS + auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine(); + if (sEngine) + { + sEngine->retainScriptObject(this, _innerAction); + } +#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS _actionInstant = dynamic_cast(action) ? true : false; //an instant action needs to be executed one time less in the update method since it uses startWithTarget to execute the action // minggo: instant action doesn't execute action in Repeat::startWithTarget(), so comment it. -// if (_actionInstant) +// if (_actionInstant) // { // _times -=1; // } @@ -582,6 +598,13 @@ bool RepeatForever::initWithAction(ActionInterval *action) action->retain(); _innerAction = action; +#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS + auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine(); + if (sEngine) + { + sEngine->retainScriptObject(this, _innerAction); + } +#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS return true; } @@ -759,6 +782,15 @@ bool Spawn::initWithTwoActions(FiniteTimeAction *action1, FiniteTimeAction *acti _one->retain(); _two->retain(); + +#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS + auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine(); + if (sEngine) + { + sEngine->retainScriptObject(this, _one); + sEngine->retainScriptObject(this, _two); + } +#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS ret = true; } @@ -2186,6 +2218,13 @@ bool ReverseTime::initWithAction(FiniteTimeAction *action) _other = action; action->retain(); +#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS + auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine(); + if (sEngine) + { + sEngine->retainScriptObject(this, action); + } +#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS return true; } @@ -2469,6 +2508,13 @@ bool TargetedAction::initWithTarget(Node* target, FiniteTimeAction* action) _forcedTarget = target; CC_SAFE_RETAIN(action); _action = action; +#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS + auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine(); + if (sEngine) + { + sEngine->retainScriptObject(this, _action); + } +#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS return true; } return false; diff --git a/native/cocos/2d/CCActionManager.cpp b/native/cocos/2d/CCActionManager.cpp index d01d9decbd..42e3bb1759 100644 --- a/native/cocos/2d/CCActionManager.cpp +++ b/native/cocos/2d/CCActionManager.cpp @@ -97,7 +97,14 @@ void ActionManager::removeActionAtIndex(ssize_t index, tHashElement *element) element->currentAction->retain(); element->currentActionSalvaged = true; } - + +#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS + auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine(); + if (sEngine) + { + sEngine->releaseScriptObject(this, action); + } +#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS ccArrayRemoveObjectAtIndex(element->actions, index, true); // update actionIndex in case we are in tick. looping over the actions @@ -187,12 +194,19 @@ void ActionManager::addAction(Action *action, Node *target, bool paused) HASH_ADD_PTR(_targets, target, element); } - actionAllocWithHashElement(element); + actionAllocWithHashElement(element); - CCASSERT(! ccArrayContainsObject(element->actions, action), "action already be added!"); - ccArrayAppendObject(element->actions, action); + CCASSERT(! ccArrayContainsObject(element->actions, action), "action already be added!"); + ccArrayAppendObject(element->actions, action); +#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS + auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine(); + if (sEngine) + { + sEngine->retainScriptObject(this, action); + } +#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS - action->startWithTarget(target); + action->startWithTarget(target); } // remove @@ -224,7 +238,19 @@ void ActionManager::removeAllActionsFromTarget(Node *target) element->currentAction->retain(); element->currentActionSalvaged = true; } - + +#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS + auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine(); + if (sEngine) + { + auto limit = element->actions->num; + for (int i = 0; i < limit; ++i) + { + Action *action = (Action*)element->actions->arr[i]; + sEngine->releaseScriptObject(this, action); + } + } +#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS ccArrayRemoveAllObjects(element->actions); if (_currentTarget == element) { From 5e710556bbd2906c61dff155a3a84cc1ac6d4ca4 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Wed, 11 Jan 2017 11:12:49 +0800 Subject: [PATCH 2/3] Improve JSCallbackWrapper memory management (avoid crash) --- .../js-bindings/manual/ScriptingCore.cpp | 2 +- .../js-bindings/manual/cocos2d_specifics.cpp | 80 ++++++++++++------- .../js-bindings/manual/cocos2d_specifics.hpp | 1 + 3 files changed, 54 insertions(+), 29 deletions(-) diff --git a/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp b/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp index a55803a71e..80a8480826 100644 --- a/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp +++ b/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp @@ -2437,8 +2437,8 @@ void jsb_ref_rebind(JSContext* cx, JS::HandleObject jsobj, js_proxy_t *proxy, co #else JS::RemoveObjectRoot(cx, &proxy->obj); #endif + jsb_remove_proxy(proxy); - // Rebind js obj with new action js_proxy_t* newProxy = jsb_new_proxy(newRef, jsobj); diff --git a/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp b/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp index 1993700b24..2e4536c22d 100644 --- a/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp +++ b/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp @@ -379,13 +379,13 @@ bool js_cocos2dx_CCSequence_create(JSContext *cx, uint32_t argc, jsval *vp) } } auto ret = new (std::nothrow) cocos2d::Sequence; + js_type_class_t *typeClass = js_get_type_from_native(ret); + // link the native object with the javascript object + JS::RootedObject jsobj(cx, jsb_ref_create_jsobject(cx, ret, typeClass, "cocos2d::Sequence")); + auto ok = ret->init(array); - if (ok) { - js_type_class_t *typeClass = js_get_type_from_native(ret); - // link the native object with the javascript object - JS::RootedObject jsobj(cx, jsb_ref_create_jsobject(cx, ret, typeClass, "cocos2d::Sequence")); args.rval().set(OBJECT_TO_JSVAL(jsobj)); return true; } @@ -417,12 +417,13 @@ bool js_cocos2dx_CCSpawn_create(JSContext *cx, uint32_t argc, jsval *vp) } } auto ret = new (std::nothrow) cocos2d::Spawn; + js_type_class_t *typeClass = js_get_type_from_native(ret); + // link the native object with the javascript object + JS::RootedObject jsobj(cx, jsb_ref_create_jsobject(cx, ret, typeClass, "cocos2d::Spawn")); + auto ok = ret->init(array); if (ok) { - js_type_class_t *typeClass = js_get_type_from_native(ret); - // link the native object with the javascript object - JS::RootedObject jsobj(cx, jsb_ref_create_jsobject(cx, ret, typeClass, "cocos2d::Spawn")); args.rval().set(OBJECT_TO_JSVAL(jsobj)); return true; } @@ -714,6 +715,7 @@ void js_remove_object_root(JS::HandleValue target) } JSCallbackWrapper::JSCallbackWrapper() +: _cppRelated(false) { JSContext* cx = ScriptingCore::getInstance()->getGlobalContext(); _jsCallback = JS::NullValue(); @@ -727,36 +729,57 @@ JSCallbackWrapper::JSCallbackWrapper() } JSCallbackWrapper::JSCallbackWrapper(JS::HandleValue owner) +: _cppRelated(false) { _owner = owner; _jsCallback = JS::NullValue(); _jsThisObj = JS::NullValue(); _extraData = JS::NullValue(); + + JS::RootedObject ownerObj(ScriptingCore::getInstance()->getGlobalContext(), owner.toObjectOrNull()); + js_proxy *t = jsb_get_js_proxy(ownerObj); + if (t) { + _cppRelated = true; + } } JSCallbackWrapper::~JSCallbackWrapper() { ScriptingCore* sc = ScriptingCore::getInstance(); JSContext* cx = sc->getGlobalContext(); + JSAutoCompartment(cx, sc->getGlobalObject()); + JS::RootedValue ownerVal(cx, _owner); - if (!sc->getFinalizing() && !ownerVal.isNullOrUndefined()) + if (sc->getFinalizing() || ownerVal.isNullOrUndefined()) { - JS::RootedValue target(cx, _jsCallback); - if (!target.isNullOrUndefined()) + return; + } + if (_cppRelated) + { + JS::RootedObject ownerObj(cx, ownerVal.toObjectOrNull()); + js_proxy *t = jsb_get_js_proxy(ownerObj); + // Cpp object already released, no need to do the following release anymore, gc will take care of everything + if (t == nullptr) { - 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); + return; } } + + 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) { @@ -841,15 +864,17 @@ static bool js_callFunc(JSContext *cx, uint32_t argc, jsval *vp) JS::RootedObject jsobj(cx, jsb_ref_create_jsobject(cx, ret, typeClass, "cocos2d::CallFuncN")); JS::RootedValue retVal(cx, OBJECT_TO_JSVAL(jsobj)); - std::shared_ptr tmpCobj(new JSCallbackWrapper()); + std::shared_ptr tmpCobj(new JSCallbackWrapper(retVal)); JS::RootedValue callback(cx, args.get(0)); tmpCobj->setJSCallbackFunc(callback); - if(argc >= 2) { + if (argc >= 2) + { JS::RootedValue thisObj(cx, args.get(1)); tmpCobj->setJSCallbackThis(thisObj); } - if(argc >= 3) { + if (argc >= 3) + { JS::RootedValue data(cx, args.get(2)); tmpCobj->setJSExtraData(data); } @@ -1841,7 +1866,7 @@ bool js_CCScheduler_scheduleUpdateForTarget(JSContext *cx, uint32_t argc, jsval if (!bFound) { - tmpCObj = new (std::nothrow) JSScheduleWrapper(); + tmpCObj = new (std::nothrow) JSScheduleWrapper(args.thisv()); tmpCObj->autorelease(); tmpCObj->setJSCallbackThis(args.get(0)); tmpCObj->setJSCallbackFunc(jsUpdateFunc); @@ -1969,7 +1994,7 @@ bool js_CCScheduler_scheduleCallbackForTarget(JSContext *cx, uint32_t argc, jsva if (!bFound) { - tmpCObj = new (std::nothrow) JSScheduleWrapper(); + tmpCObj = new (std::nothrow) JSScheduleWrapper(args.thisv()); tmpCObj->autorelease(); tmpCObj->setJSCallbackThis(args.get(0)); tmpCObj->setJSCallbackFunc(args.get(1)); @@ -2022,7 +2047,7 @@ bool js_CCScheduler_schedule(JSContext *cx, uint32_t argc, jsval *vp) break; } - std::shared_ptr func(new JSFunctionWrapper(cx, targetObj, callbackVal)); + std::shared_ptr func(new JSFunctionWrapper(cx, targetObj, callbackVal, args.thisv())); auto lambda = [=](float larg0) -> void { JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET jsval largv[1]; @@ -2771,7 +2796,6 @@ bool js_cocos2dx_ActionInterval_repeatForever(JSContext *cx, uint32_t argc, jsva if (argc == 0) { cocos2d::RepeatForever* action = new (std::nothrow) cocos2d::RepeatForever; action->initWithAction(cobj); - jsb_ref_rebind(cx, jsobj, proxy, cobj, action, "cocos2d::RepeatForever"); args.rval().set(OBJECT_TO_JSVAL(jsobj)); return true; diff --git a/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.hpp b/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.hpp index ba5ac118be..4bc4738c11 100644 --- a/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.hpp +++ b/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.hpp @@ -172,6 +172,7 @@ protected: JS::Heap _jsCallback; JS::Heap _jsThisObj; JS::Heap _extraData; + bool _cppRelated; }; From 22382e4f2dda405c7ee70c1f823f9ac1fc6fc84e Mon Sep 17 00:00:00 2001 From: pandamicro Date: Wed, 11 Jan 2017 11:13:07 +0800 Subject: [PATCH 3/3] Improve JSFunctionWrapper memory management --- .../manual/js_manual_conversions.cpp | 92 +++++++++++-------- .../manual/js_manual_conversions.h | 4 +- .../manual/jsb_event_dispatcher_manual.cpp | 25 ++++- 3 files changed, 78 insertions(+), 43 deletions(-) diff --git a/native/cocos/scripting/js-bindings/manual/js_manual_conversions.cpp b/native/cocos/scripting/js-bindings/manual/js_manual_conversions.cpp index 66a45dd642..2b50c1d802 100644 --- a/native/cocos/scripting/js-bindings/manual/js_manual_conversions.cpp +++ b/native/cocos/scripting/js-bindings/manual/js_manual_conversions.cpp @@ -101,65 +101,79 @@ const char* JSStringWrapper::get() // JSFunctionWrapper JSFunctionWrapper::JSFunctionWrapper(JSContext* cx, JS::HandleObject jsthis, JS::HandleValue fval) -: _cx(cx) +: _cppRelated(false) +, _cx(cx) { _jsthis = jsthis; _fval = fval; - - JS::RootedObject root(cx); - get_or_create_js_obj("jsb._root", &root); - JS::RootedValue valRoot(cx, OBJECT_TO_JSVAL(root)); - _owner = valRoot; - - if (!valRoot.isNullOrUndefined()) - { - JS::RootedValue thisVal(cx, OBJECT_TO_JSVAL(_jsthis)); - if (!thisVal.isNullOrUndefined()) - { - js_add_object_reference(valRoot, thisVal); - } - JS::RootedValue funcVal(cx, _fval); - if (!funcVal.isNullOrUndefined()) - { - js_add_object_reference(valRoot, funcVal); - } - } + _owner = JS::NullValue(); } JSFunctionWrapper::JSFunctionWrapper(JSContext* cx, JS::HandleObject jsthis, JS::HandleValue fval, JS::HandleValue owner) -: _cx(cx) +: _cppRelated(false) +, _cx(cx) { _jsthis = jsthis; _fval = fval; - _owner = owner; - JS::RootedValue ownerVal(cx, owner); - - JS::RootedValue thisVal(cx, OBJECT_TO_JSVAL(jsthis)); - if (!thisVal.isNullOrUndefined()) - { - js_add_object_reference(ownerVal, thisVal); - } - JS::RootedValue funcVal(cx, _fval); - if (!funcVal.isNullOrUndefined()) - { - js_add_object_reference(ownerVal, funcVal); - } + setOwner(cx, JS::RootedValue(cx, owner)); } JSFunctionWrapper::~JSFunctionWrapper() { + ScriptingCore* sc = ScriptingCore::getInstance(); + JSContext* cx = sc->getGlobalContext(); + JSAutoCompartment(cx, sc->getGlobalObject()); JS::RootedValue ownerVal(_cx, _owner); - if (!ScriptingCore::getInstance()->getFinalizing() && !ownerVal.isNullOrUndefined()) + if (sc->getFinalizing() || ownerVal.isNullOrUndefined()) { - JS::RootedValue thisVal(_cx, OBJECT_TO_JSVAL(_jsthis)); + return; + } + if (_cppRelated) + { + JS::RootedObject ownerObj(cx, ownerVal.toObjectOrNull()); + js_proxy *t = jsb_get_js_proxy(ownerObj); + // Cpp object already released, no need to do the following release anymore, gc will take care of everything + if (t == nullptr) + { + return; + } + } + + JS::RootedValue thisVal(_cx, OBJECT_TO_JSVAL(_jsthis)); + if (!thisVal.isNullOrUndefined()) + { + js_remove_object_reference(ownerVal, thisVal); + } + JS::RootedValue funcVal(_cx, _fval); + if (!funcVal.isNullOrUndefined()) + { + js_remove_object_reference(ownerVal, funcVal); + } +} + +void JSFunctionWrapper::setOwner(JSContext* cx, JS::HandleValue owner) +{ + JSAutoCompartment(cx, ScriptingCore::getInstance()->getGlobalObject()); + JS::RootedValue ownerVal(cx, owner); + if (!owner.isNullOrUndefined()) + { + _owner = owner; + + JS::RootedObject ownerObj(cx, owner.toObjectOrNull()); + js_proxy *t = jsb_get_js_proxy(ownerObj); + if (t) { + _cppRelated = true; + } + + JS::RootedValue thisVal(cx, OBJECT_TO_JSVAL(_jsthis)); if (!thisVal.isNullOrUndefined()) { - js_remove_object_reference(ownerVal, thisVal); + js_add_object_reference(ownerVal, thisVal); } - JS::RootedValue funcVal(_cx, _fval); + JS::RootedValue funcVal(cx, _fval); if (!funcVal.isNullOrUndefined()) { - js_remove_object_reference(ownerVal, funcVal); + js_add_object_reference(ownerVal, funcVal); } } } diff --git a/native/cocos/scripting/js-bindings/manual/js_manual_conversions.h b/native/cocos/scripting/js-bindings/manual/js_manual_conversions.h index c34a06526c..e7e55ece96 100644 --- a/native/cocos/scripting/js-bindings/manual/js_manual_conversions.h +++ b/native/cocos/scripting/js-bindings/manual/js_manual_conversions.h @@ -79,6 +79,7 @@ public: JSFunctionWrapper(JSContext* cx, JS::HandleObject jsthis, JS::HandleValue fval, JS::HandleValue owner); ~JSFunctionWrapper(); + void setOwner(JSContext* cx, JS::HandleValue owner); bool invoke(unsigned int argc, jsval *argv, JS::MutableHandleValue rval); bool invoke(JS::HandleValueArray args, JS::MutableHandleValue rval); private: @@ -86,7 +87,8 @@ private: JS::Heap _jsthis; JS::Heap _fval; JS::Heap _owner; -private: + bool _cppRelated; + CC_DISALLOW_COPY_AND_ASSIGN(JSFunctionWrapper); }; diff --git a/native/cocos/scripting/js-bindings/manual/jsb_event_dispatcher_manual.cpp b/native/cocos/scripting/js-bindings/manual/jsb_event_dispatcher_manual.cpp index 51eae8ede6..3f580f32db 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_event_dispatcher_manual.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_event_dispatcher_manual.cpp @@ -172,11 +172,13 @@ bool js_EventListenerAcceleration_create(JSContext *cx, uint32_t argc, jsval *vp bool ok = true; if (argc == 1) { std::function arg0; + JSFunctionWrapper *wrapper = nullptr; do { if(JS_TypeOfValue(cx, args.get(0)) == JSTYPE_FUNCTION) { JS::RootedObject jstarget(cx, args.thisv().toObjectOrNull()); - std::shared_ptr func(new JSFunctionWrapper(cx, jstarget, args.get(0), args.thisv())); + std::shared_ptr func(new JSFunctionWrapper(cx, jstarget, args.get(0))); + wrapper = func.get(); auto lambda = [=](Acceleration* acc, Event* event) -> void { JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET jsval largv[2]; @@ -205,6 +207,10 @@ bool js_EventListenerAcceleration_create(JSContext *cx, uint32_t argc, jsval *vp auto ret = EventListenerAcceleration::create(arg0); JS::RootedValue jsret(cx, OBJECT_TO_JSVAL(js_get_or_create_jsobject(cx, ret))); + if (wrapper) + { + wrapper->setOwner(cx, jsret); + } args.rval().set(jsret); return true; } @@ -239,12 +245,14 @@ bool js_EventListenerCustom_create(JSContext *cx, uint32_t argc, jsval *vp) if (argc == 2) { std::string arg0; std::function arg1; + JSFunctionWrapper *wrapper = nullptr; ok &= jsval_to_std_string(cx, args.get(0), &arg0); do { if(JS_TypeOfValue(cx, args.get(1)) == JSTYPE_FUNCTION) { JS::RootedObject jstarget(cx, args.thisv().toObjectOrNull()); - std::shared_ptr func(new JSFunctionWrapper(cx, jstarget, args.get(1), args.thisv())); + std::shared_ptr func(new JSFunctionWrapper(cx, jstarget, args.get(1))); + wrapper = func.get(); auto lambda = [=](EventCustom* event) -> void { JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET jsval largv[1]; @@ -273,6 +281,10 @@ bool js_EventListenerCustom_create(JSContext *cx, uint32_t argc, jsval *vp) auto ret = EventListenerCustom::create(arg0, arg1); JS::RootedValue jsret(cx, OBJECT_TO_JSVAL(js_get_or_create_jsobject(cx, ret))); + if (wrapper) + { + wrapper->setOwner(cx, jsret); + } args.rval().set(jsret); return true; } @@ -292,12 +304,14 @@ bool js_EventDispatcher_addCustomEventListener(JSContext *cx, uint32_t argc, jsv if (argc == 2) { std::string arg0; std::function arg1; + JSFunctionWrapper *wrapper = nullptr; ok &= jsval_to_std_string(cx, args.get(0), &arg0); do { if(JS_TypeOfValue(cx, args.get(1)) == JSTYPE_FUNCTION) { JS::RootedObject jstarget(cx, args.thisv().toObjectOrNull()); - std::shared_ptr func(new JSFunctionWrapper(cx, jstarget, args.get(1), args.thisv())); + std::shared_ptr func(new JSFunctionWrapper(cx, jstarget, args.get(1))); + wrapper = func.get(); auto lambda = [=](cocos2d::EventCustom* event) -> void { JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET jsval largv[1]; @@ -326,6 +340,11 @@ bool js_EventDispatcher_addCustomEventListener(JSContext *cx, uint32_t argc, jsv JS::RootedValue jsret(cx); if (ret) { jsret = OBJECT_TO_JSVAL(js_get_or_create_jsobject(cx, ret)); + ScriptingCore::getInstance()->retainScriptObject(cobj, ret); + if (wrapper) + { + wrapper->setOwner(cx, jsret); + } args.rval().set(jsret); } else { jsret = JSVAL_NULL;