mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-06 03:22:39 +08:00
Separate FinalizeHook for ref objects and non ref objects
This commit is contained in:
@@ -1440,7 +1440,7 @@ bool ScriptingCore::handleTouchesEvent(void* nativeObj, cocos2d::EventTouch::Eve
|
||||
|
||||
for (const auto& touch : touches)
|
||||
{
|
||||
JS::RootedValue jsret(_cx, OBJECT_TO_JSVAL(jsb_get_or_create_weak_jsobject(_cx, touch, typeClassTouch, "cocos2d::Touch")));
|
||||
JS::RootedValue jsret(_cx, OBJECT_TO_JSVAL(jsb_get_or_create_weak_jsobject(_cx, touch, typeClassTouch)));
|
||||
if (!JS_SetElement(_cx, jsretArr, count, jsret))
|
||||
{
|
||||
break;
|
||||
@@ -1453,7 +1453,7 @@ bool ScriptingCore::handleTouchesEvent(void* nativeObj, cocos2d::EventTouch::Eve
|
||||
{
|
||||
jsval dataVal[2];
|
||||
dataVal[0] = OBJECT_TO_JSVAL(jsretArr);
|
||||
dataVal[1] = OBJECT_TO_JSVAL(jsb_get_or_create_weak_jsobject(_cx, event, typeClassEvent, "cocos2d::EventTouch"));
|
||||
dataVal[1] = OBJECT_TO_JSVAL(jsb_get_or_create_weak_jsobject(_cx, event, typeClassEvent));
|
||||
ret = executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), funcName.c_str(), 2, dataVal, jsvalRet);
|
||||
}
|
||||
|
||||
@@ -1461,7 +1461,6 @@ bool ScriptingCore::handleTouchesEvent(void* nativeObj, cocos2d::EventTouch::Eve
|
||||
{
|
||||
removeJSObject(_cx, touch);
|
||||
}
|
||||
|
||||
removeJSObject(_cx, event);
|
||||
|
||||
return ret;
|
||||
@@ -1487,8 +1486,8 @@ bool ScriptingCore::handleTouchEvent(void* nativeObj, cocos2d::EventTouch::Event
|
||||
js_type_class_t *typeClassEvent = js_get_type_from_native<cocos2d::EventTouch>((cocos2d::EventTouch*)event);
|
||||
|
||||
jsval dataVal[2];
|
||||
dataVal[0] = OBJECT_TO_JSVAL(jsb_get_or_create_weak_jsobject(_cx, touch, typeClassTouch, "cocos2d::Touch"));
|
||||
dataVal[1] = OBJECT_TO_JSVAL(jsb_get_or_create_weak_jsobject(_cx, event, typeClassEvent, "cocos2d::EventTouch"));
|
||||
dataVal[0] = OBJECT_TO_JSVAL(jsb_get_or_create_weak_jsobject(_cx, touch, typeClassTouch));
|
||||
dataVal[1] = OBJECT_TO_JSVAL(jsb_get_or_create_weak_jsobject(_cx, event, typeClassEvent));
|
||||
|
||||
ret = executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), funcName.c_str(), 2, dataVal, jsvalRet);
|
||||
}
|
||||
@@ -1516,7 +1515,7 @@ bool ScriptingCore::handleMouseEvent(void* nativeObj, cocos2d::EventMouse::Mouse
|
||||
if (p)
|
||||
{
|
||||
js_type_class_t *typeClass = js_get_type_from_native<cocos2d::EventMouse>((cocos2d::EventMouse*)event);
|
||||
jsval dataVal = OBJECT_TO_JSVAL(jsb_get_or_create_weak_jsobject(_cx, event, typeClass, "cocos2d::EventMouse"));
|
||||
jsval dataVal = OBJECT_TO_JSVAL(jsb_get_or_create_weak_jsobject(_cx, event, typeClass));
|
||||
ret = executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), funcName.c_str(), 1, &dataVal, jsvalRet);
|
||||
|
||||
removeJSObject(_cx, event);
|
||||
@@ -1658,7 +1657,7 @@ int ScriptingCore::executeCustomTouchesEvent(EventTouch::EventCode eventType,
|
||||
{
|
||||
js_type_class_t *typeClass = js_get_type_from_native<cocos2d::Touch>(touch);
|
||||
|
||||
jsval jsret = OBJECT_TO_JSVAL(jsb_get_or_create_weak_jsobject(this->_cx, touch, typeClass, "cocos2d::Touch"));
|
||||
jsval jsret = OBJECT_TO_JSVAL(jsb_get_or_create_weak_jsobject(this->_cx, touch, typeClass));
|
||||
JS::RootedValue jsval(_cx, jsret);
|
||||
if (!JS_SetElement(this->_cx, jsretArr, count, jsval)) {
|
||||
break;
|
||||
@@ -1686,7 +1685,7 @@ int ScriptingCore::executeCustomTouchEvent(EventTouch::EventCode eventType, Touc
|
||||
std::string funcName = getTouchFuncName(eventType);
|
||||
|
||||
js_type_class_t *typeClass = js_get_type_from_native<cocos2d::Touch>(touch);
|
||||
jsval jsTouch = OBJECT_TO_JSVAL(jsb_get_or_create_weak_jsobject(this->_cx, touch, typeClass, "cocos2d::Touch"));
|
||||
jsval jsTouch = OBJECT_TO_JSVAL(jsb_get_or_create_weak_jsobject(this->_cx, touch, typeClass));
|
||||
|
||||
executeFunctionWithOwner(OBJECT_TO_JSVAL(obj), funcName.c_str(), 1, &jsTouch, &retval);
|
||||
|
||||
@@ -1705,7 +1704,7 @@ int ScriptingCore::executeCustomTouchEvent(EventTouch::EventCode eventType,
|
||||
std::string funcName = getTouchFuncName(eventType);
|
||||
|
||||
js_type_class_t *typeClass = js_get_type_from_native<cocos2d::Touch>(touch);
|
||||
jsval jsTouch = OBJECT_TO_JSVAL(jsb_get_or_create_weak_jsobject(this->_cx, touch, typeClass, "cocos2d::Touch"));
|
||||
jsval jsTouch = OBJECT_TO_JSVAL(jsb_get_or_create_weak_jsobject(this->_cx, touch, typeClass));
|
||||
|
||||
executeFunctionWithOwner(OBJECT_TO_JSVAL(obj), funcName.c_str(), 1, &jsTouch, retval);
|
||||
|
||||
@@ -2295,13 +2294,17 @@ JSObject* jsb_create_weak_jsobject(JSContext *cx, void *native, js_type_class_t
|
||||
JS::RootedObject parent(cx, typeClass->parentProto.ref());
|
||||
JS::RootedObject jsObj(cx, JS_NewObject(cx, typeClass->jsclass, proto, parent));
|
||||
auto proxy = jsb_new_proxy(native, jsObj);
|
||||
js_add_FinalizeHook(cx, jsObj, false);
|
||||
|
||||
#if ! CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
||||
JS::AddNamedObjectRoot(cx, &proxy->obj, debug);
|
||||
#else
|
||||
#if COCOS2D_DEBUG > 1
|
||||
CC_UNUSED_PARAM(proxy);
|
||||
// CCLOG("++++++WEAK_REF++++++ Cpp(%s): %p - JS: %p", debug, native, jsObj.get());
|
||||
if (debug != nullptr)
|
||||
{
|
||||
CCLOG("++++++WEAK_REF++++++ Cpp(%s): %p - JS: %p", debug, native, jsObj.get());
|
||||
}
|
||||
#endif // COCOS2D_DEBUG
|
||||
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
||||
return jsObj;
|
||||
@@ -2325,7 +2328,7 @@ JSObject* jsb_ref_get_or_create_jsobject(JSContext *cx, cocos2d::Ref *ref, js_ty
|
||||
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
||||
CC_UNUSED_PARAM(newproxy);
|
||||
ref->retain();
|
||||
js_add_FinalizeHook(cx, jsObj);
|
||||
js_add_FinalizeHook(cx, jsObj, true);
|
||||
#if COCOS2D_DEBUG > 1
|
||||
CCLOG("++++++RETAINED++++++ Cpp(%s): %p - JS: %p", debug, ref, jsObj.get());
|
||||
#endif // COCOS2D_DEBUG
|
||||
@@ -2373,8 +2376,12 @@ JSObject* jsb_get_or_create_weak_jsobject(JSContext *cx, void *native, js_type_c
|
||||
#if ! CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
||||
JS::AddNamedObjectRoot(cx, &proxy->obj, debug);
|
||||
#else
|
||||
js_add_FinalizeHook(cx, jsObj, false);
|
||||
#if COCOS2D_DEBUG > 1
|
||||
// CCLOG("++++++WEAK_REF++++++ Cpp(%s): %p - JS: %p", debug, native, jsObj.get());
|
||||
if (debug != nullptr)
|
||||
{
|
||||
CCLOG("++++++WEAK_REF++++++ Cpp(%s): %p - JS: %p", debug, native, jsObj.get());
|
||||
}
|
||||
#endif // COCOS2D_DEBUG
|
||||
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
||||
return jsObj;
|
||||
@@ -2387,7 +2394,7 @@ void jsb_ref_init(JSContext* cx, JS::Heap<JSObject*> *obj, Ref* ref, const char*
|
||||
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
||||
(void)ref;
|
||||
JS::RootedObject jsObj(cx, *obj);
|
||||
js_add_FinalizeHook(cx, jsObj);
|
||||
js_add_FinalizeHook(cx, jsObj, true);
|
||||
// don't retain it, already retained
|
||||
#if COCOS2D_DEBUG > 1
|
||||
CCLOG("++++++RETAINED++++++ Cpp(%s): %p - JS: %p", debug, ref, jsObj.get());
|
||||
@@ -2407,7 +2414,7 @@ void jsb_ref_autoreleased_init(JSContext* cx, JS::Heap<JSObject*> *obj, Ref* ref
|
||||
(void)obj;
|
||||
ref->retain();
|
||||
JS::RootedObject jsObj(cx, *obj);
|
||||
js_add_FinalizeHook(cx, jsObj);
|
||||
js_add_FinalizeHook(cx, jsObj, true);
|
||||
#else
|
||||
// don't autorelease it, since it is already autoreleased
|
||||
JS::AddNamedObjectRoot(cx, obj, debug);
|
||||
@@ -2429,7 +2436,9 @@ void jsb_ref_rebind(JSContext* cx, JS::HandleObject jsobj, js_proxy_t *proxy, co
|
||||
// Rebind js obj with new action
|
||||
js_proxy_t* newProxy = jsb_new_proxy(newRef, jsobj);
|
||||
|
||||
#if !CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
||||
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
||||
CC_UNUSED_PARAM(newProxy);
|
||||
#else
|
||||
JS::AddNamedObjectRoot(cx, &newProxy->obj, debug);
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user