diff --git a/native/cocos/scripting/js-bindings/jswrapper/chakracore/Class.cpp b/native/cocos/scripting/js-bindings/jswrapper/chakracore/Class.cpp
index bdb1235416..638619ff13 100644
--- a/native/cocos/scripting/js-bindings/jswrapper/chakracore/Class.cpp
+++ b/native/cocos/scripting/js-bindings/jswrapper/chakracore/Class.cpp
@@ -6,6 +6,7 @@
#include "Utils.hpp"
#include "State.hpp"
#include "ScriptEngine.hpp"
+#include "../HandleObject.hpp"
namespace se {
@@ -94,13 +95,14 @@ namespace se {
}
_CHECK(JsCreateNamedFunction(funcName, _ctor, nullptr, &jsConstructor));
- Object* ctorObj = Object::_createJSObject(nullptr, jsConstructor, false);
+ HandleObject ctorObj(Object::_createJSObject(nullptr, jsConstructor));
// create class's prototype and project its member functions
JsValueRef prototype;
_CHECK(JsCreateObject(&prototype));
- Object* prototypeObj = Object::_createJSObject(this, prototype, true);
+ Object* prototypeObj = Object::_createJSObject(this, prototype);
+ prototypeObj->root();
for (const auto& func : _funcs)
{
@@ -134,8 +136,6 @@ namespace se {
_proto = prototypeObj;
_parent->setProperty(_name.c_str(), Value(ctorObj));
- ctorObj->release();
-
return true;
}
diff --git a/native/cocos/scripting/js-bindings/jswrapper/chakracore/HelperMacros.h b/native/cocos/scripting/js-bindings/jswrapper/chakracore/HelperMacros.h
index cc497f8f09..bf38d9f1f3 100644
--- a/native/cocos/scripting/js-bindings/jswrapper/chakracore/HelperMacros.h
+++ b/native/cocos/scripting/js-bindings/jswrapper/chakracore/HelperMacros.h
@@ -47,12 +47,15 @@
{ \
if (nativeThisObject != nullptr) \
{ \
+ auto se = se::ScriptEngine::getInstance(); \
+ se->_setInGC(true); \
bool ret = false; \
se::State state(nativeThisObject); \
se::Object* _thisObject = state.thisObject(); \
if (_thisObject) _thisObject->_cleanup(nativeThisObject); \
ret = funcName(state); \
SAFE_RELEASE(_thisObject); \
+ se->_setInGC(false); \
} \
}
@@ -68,7 +71,7 @@
bool ret = true; \
se::ValueArray args; \
se::internal::jsToSeArgs(argc, _argv+1, &args); \
- se::Object* thisObject = se::Object::createObjectWithClass(cls, false); \
+ se::Object* thisObject = se::Object::createObjectWithClass(cls); \
JsValueRef _jsRet = thisObject->_getJSObject(); \
se::State state(thisObject, args); \
ret = funcName(state); \
@@ -98,7 +101,7 @@
bool ret = true; \
se::ValueArray args; \
se::internal::jsToSeArgs(argc, _argv+1, &args); \
- se::Object* thisObject = se::Object::_createJSObject(cls, _argv[0], false); \
+ se::Object* thisObject = se::Object::_createJSObject(cls, _argv[0]); \
thisObject->_setFinalizeCallback(_SE(finalizeCb)); \
se::State state(thisObject, args); \
ret = funcName(state); \
diff --git a/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.cpp b/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.cpp
index 5a46445694..69f7a40c05 100644
--- a/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.cpp
+++ b/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.cpp
@@ -8,13 +8,13 @@
namespace se {
std::unordered_map __nativePtrToObjectMap;
+ std::unordered_map __nonRefNativeObjectCreatedByCtorMap;
Object::Object()
: _cls(nullptr)
, _obj(JS_INVALID_REFERENCE)
- , _isRooted(false)
- , _isKeepRootedUntilDie(false)
- , _hasPrivateData(false)
+ , _rootCount(0)
+ , _privateData(nullptr)
, _isCleanup(false)
, _finalizeCb(nullptr)
{
@@ -25,31 +25,23 @@ namespace se {
_cleanup();
}
- Object* Object::createPlainObject(bool rooted)
+ Object* Object::createPlainObject()
{
JsValueRef jsobj;
_CHECK(JsCreateObject(&jsobj));
- Object* obj = _createJSObject(nullptr, jsobj, rooted);
+ Object* obj = _createJSObject(nullptr, jsobj);
return obj;
}
-// Object* Object::createObject(const char* clsName, bool rooted)
-// {
-// Class* cls = nullptr;
-// JsValueRef jsObj = Class::_createJSObject(clsName, &cls);
-// Object* obj = _createJSObject(cls, jsObj, rooted);
-// return obj;
-// }
-
- Object* Object::createArrayObject(size_t length, bool rooted)
+ Object* Object::createArrayObject(size_t length)
{
JsValueRef jsObj = JS_INVALID_REFERENCE;
_CHECK(JsCreateArray((unsigned int)length, &jsObj));
- Object* obj = _createJSObject(nullptr, jsObj, rooted);
+ Object* obj = _createJSObject(nullptr, jsObj);
return obj;
}
- Object* Object::createArrayBufferObject(void* data, size_t byteLength, bool rooted)
+ Object* Object::createArrayBufferObject(void* data, size_t byteLength)
{
Object* obj = nullptr;
JsValueRef jsobj;
@@ -59,13 +51,13 @@ namespace se {
if (JsNoError == JsGetArrayBufferStorage(jsobj, &buffer, &bufferLength))
{
memcpy((void*)buffer, data, byteLength);
- obj = Object::_createJSObject(nullptr, jsobj, rooted);
+ obj = Object::_createJSObject(nullptr, jsobj);
}
return obj;
}
- Object* Object::createUint8TypedArray(uint8_t* data, size_t byteLength, bool rooted)
+ Object* Object::createUint8TypedArray(uint8_t* data, size_t byteLength)
{
Object* obj = nullptr;
JsValueRef jsobj;
@@ -77,13 +69,13 @@ namespace se {
if (JsNoError == JsGetTypedArrayStorage(jsobj, &buffer, &bufferLength, &arrType, &elementSize))
{
memcpy((void*)buffer, data, byteLength);
- obj = Object::_createJSObject(nullptr, jsobj, rooted);
+ obj = Object::_createJSObject(nullptr, jsobj);
}
return obj;
}
- Object* Object::createJSONObject(const std::string& jsonStr, bool rooted)
+ Object* Object::createJSONObject(const std::string& jsonStr)
{
bool ok = false;
Object* obj = nullptr;
@@ -102,7 +94,7 @@ namespace se {
args.push_back(Value(jsonStr));
if (parseVal.toObject()->call(args, jsonVal.toObject(), &ret))
{
- obj = Object::_createJSObject(nullptr, ret.toObject(), rooted);
+ obj = Object::_createJSObject(nullptr, ret.toObject());
}
return obj;
@@ -120,21 +112,10 @@ namespace se {
return obj;
}
-// Object* Object::getOrCreateObjectWithPtr(void* ptr, const char* clsName, bool rooted)
-// {
-// Object* obj = getObjectWithPtr(ptr);
-// if (obj == nullptr)
-// {
-// obj = createObject(clsName, rooted);
-// obj->setPrivateData(ptr);
-// }
-// return obj;
-// }
-
- Object* Object::_createJSObject(Class* cls, JsValueRef obj, bool rooted)
+ Object* Object::_createJSObject(Class* cls, JsValueRef obj)
{
Object* ret = new Object();
- if (!ret->init(obj, rooted))
+ if (!ret->init(obj))
{
delete ret;
ret = nullptr;
@@ -144,22 +125,16 @@ namespace se {
return ret;
}
- Object* Object::createObjectWithClass(Class* cls, bool rooted)
+ Object* Object::createObjectWithClass(Class* cls)
{
JsValueRef jsobj = Class::_createJSObjectWithClass(cls);
- Object* obj = Object::_createJSObject(cls, jsobj, rooted);
+ Object* obj = Object::_createJSObject(cls, jsobj);
return obj;
}
- bool Object::init(JsValueRef obj, bool rooted)
+ bool Object::init(JsValueRef obj)
{
_obj = obj;
- _isRooted = rooted;
- if (_isRooted)
- {
- unsigned int count = 0;
- _CHECK(JsAddRef(_obj, &count));
- }
return true;
}
@@ -168,7 +143,7 @@ namespace se {
if (_isCleanup)
return;
- if (_hasPrivateData)
+ if (_privateData != nullptr)
{
if (_obj != nullptr)
{
@@ -188,7 +163,7 @@ namespace se {
}
}
- if (_isRooted)
+ if (_rootCount > 0)
{
// Don't unprotect if it's in cleanup, otherwise, it will trigger crash.
if (!ScriptEngine::getInstance()->_isInCleanup)
@@ -196,6 +171,7 @@ namespace se {
unsigned int count = 0;
_CHECK(JsRelease(_obj, &count));
}
+ _rootCount = 0;
}
_isCleanup = true;
@@ -205,6 +181,7 @@ namespace se {
{
ScriptEngine::getInstance()->addAfterCleanupHook([](){
__nativePtrToObjectMap.clear();
+ __nonRefNativeObjectCreatedByCtorMap.clear();
});
}
@@ -533,48 +510,32 @@ namespace se {
return ret;
}
-// void Object::getAsUint8Array(unsigned char **ptr, unsigned int *length)
-// {
-// assert(false);
-// }
-//
-// void Object::getAsUint16Array(unsigned short **ptr, unsigned int *length)
-// {
-// assert(false);
-// }
-// void Object::getAsUint32Array(unsigned int **ptr, unsigned int *length)
-// {
-// assert(false);
-// }
-//
-// void Object::getAsFloat32Array(float **ptr, unsigned int *length)
-// {
-// assert(false);
-// }
-
-
- void* Object::getPrivateData()
+ void* Object::getPrivateData() const
{
- return internal::getPrivate(_obj);
+ if (_privateData == nullptr)
+ {
+ const_cast