Merge pull request #490 from pandamicro/v1.4

Fix action reference issue
This commit is contained in:
pandamicro
2017-01-11 12:01:59 +08:00
committed by GitHub
9 changed files with 233 additions and 79 deletions
+22
View File
@@ -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;
+47 -1
View File
@@ -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<ActionInstant*>(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;
+32 -6
View File
@@ -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)
{
@@ -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);
@@ -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<cocos2d::Sequence>(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<cocos2d::Sequence>(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<cocos2d::Spawn>(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<cocos2d::Spawn>(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<JSCallbackWrapper> tmpCobj(new JSCallbackWrapper());
std::shared_ptr<JSCallbackWrapper> 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<JSFunctionWrapper> func(new JSFunctionWrapper(cx, targetObj, callbackVal));
std::shared_ptr<JSFunctionWrapper> 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;
@@ -172,6 +172,7 @@ protected:
JS::Heap<JS::Value> _jsCallback;
JS::Heap<JS::Value> _jsThisObj;
JS::Heap<JS::Value> _extraData;
bool _cppRelated;
};
@@ -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);
}
}
}
@@ -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<JSObject*> _jsthis;
JS::Heap<JS::Value> _fval;
JS::Heap<JS::Value> _owner;
private:
bool _cppRelated;
CC_DISALLOW_COPY_AND_ASSIGN(JSFunctionWrapper);
};
@@ -172,11 +172,13 @@ bool js_EventListenerAcceleration_create(JSContext *cx, uint32_t argc, jsval *vp
bool ok = true;
if (argc == 1) {
std::function<void (Acceleration *, Event *)> arg0;
JSFunctionWrapper *wrapper = nullptr;
do {
if(JS_TypeOfValue(cx, args.get(0)) == JSTYPE_FUNCTION)
{
JS::RootedObject jstarget(cx, args.thisv().toObjectOrNull());
std::shared_ptr<JSFunctionWrapper> func(new JSFunctionWrapper(cx, jstarget, args.get(0), args.thisv()));
std::shared_ptr<JSFunctionWrapper> 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<EventListenerAcceleration>(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<void (cocos2d::EventCustom *)> 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<JSFunctionWrapper> func(new JSFunctionWrapper(cx, jstarget, args.get(1), args.thisv()));
std::shared_ptr<JSFunctionWrapper> 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<EventListenerCustom>(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<void (cocos2d::EventCustom *)> 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<JSFunctionWrapper> func(new JSFunctionWrapper(cx, jstarget, args.get(1), args.thisv()));
std::shared_ptr<JSFunctionWrapper> 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<EventListenerCustom>(cx, ret));
ScriptingCore::getInstance()->retainScriptObject(cobj, ret);
if (wrapper)
{
wrapper->setOwner(cx, jsret);
}
args.rval().set(jsret);
} else {
jsret = JSVAL_NULL;