diff --git a/native/cocos/bindings/jswrapper/napi/Class.cpp b/native/cocos/bindings/jswrapper/napi/Class.cpp index 0a36750a04..fe9ad54698 100644 --- a/native/cocos/bindings/jswrapper/napi/Class.cpp +++ b/native/cocos/bindings/jswrapper/napi/Class.cpp @@ -80,8 +80,10 @@ bool Class::init(const std::string &clsName, Object *parent, Object *parentProto } napi_value Class::_defaultCtor(napi_env env, napi_callback_info info) { - LOGE("check default ctor called"); - return nullptr; + napi_value _this; + napi_status status; + NODE_API_CALL(status, env, napi_get_cb_info(env, info, nullptr, nullptr, &_this, NULL)); + return _this; } void Class::defineProperty(const char* name, napi_callback g, napi_callback s) { diff --git a/native/cocos/bindings/jswrapper/napi/Object.cpp b/native/cocos/bindings/jswrapper/napi/Object.cpp index b8d8ec34d7..e2e15e04a8 100644 --- a/native/cocos/bindings/jswrapper/napi/Object.cpp +++ b/native/cocos/bindings/jswrapper/napi/Object.cpp @@ -35,11 +35,17 @@ namespace se { std::unique_ptr> __objectMap; // Currently, the value `void*` is always nullptr -Object::Object() {} +Object::Object(): _objRef(this) {} Object::~Object() { + if (!_destructInFinalizer && _cls != nullptr) { + napi_remove_wrap(_env, _objRef.getValue(_env), nullptr); + } if (__objectMap) { __objectMap->erase(this); } + + delete _privateObject; + _privateObject = nullptr; } Object* Object::createObjectWithClass(Class* cls) { @@ -463,7 +469,7 @@ bool Object::init(napi_env env, napi_value js_object, Class* cls) { assert(env); _cls = cls; _env = env; - _objRef.initWeakref(env, js_object); + _objRef.init(env, js_object); if (__objectMap) { assert(__objectMap->find(this) == __objectMap->end()); @@ -526,10 +532,9 @@ void Object::setPrivateObject(PrivateObjectBase* data) { //issue https://github.com/nodejs/node/issues/23999 auto tmpThis = _objRef.getValue(_env); //_objRef.deleteRef(); - napi_ref result = nullptr; NODE_API_CALL(status, _env, napi_wrap(_env, tmpThis, this, weakCallback, - (void*)this /* finalize_hint */, &result)); + (void*)this /* finalize_hint */, nullptr)); } bool Object::attachObject(Object* obj) { @@ -665,6 +670,7 @@ void Object::weakCallback(napi_env env, void* nativeObject, void* finalizeHint / seObj->_getClass()->_getFinalizeFunction()(env, finalizeHint, finalizeHint); } } + seObj->_destructInFinalizer = true; seObj->decRef(); } } @@ -730,4 +736,50 @@ void Object::clearPrivateData(bool clearMapping) { } } +ObjectRef::ObjectRef(Object *parent) +: _parent(parent) { + +} + +ObjectRef::~ObjectRef() { + deleteRef(); +} + + +napi_value ObjectRef::getValue(napi_env env) const { + napi_value result; + napi_status status; + NODE_API_CALL(status, env, napi_get_reference_value(env, _ref, &result)); + assert(status == napi_ok); + assert(result != nullptr); + return result; +} + +void ObjectRef::init(napi_env env, napi_value obj) { + assert(_ref == nullptr); + _obj = obj; + _env = env; + napi_create_reference(env, obj, 1, &_ref); +} + +void ObjectRef::incRef(napi_env env) { + napi_reference_ref(env, _ref, nullptr); +} + +void ObjectRef::decRef(napi_env env) { + napi_reference_unref(env, _ref, nullptr); +} + +void ObjectRef::deleteRef() { + if (!_ref) { + return; + } + if(!_parent->_destructInFinalizer) { + // Similar to jsvm, please read the comments inside jsvm. + napi_reference_ref(_env, _ref, nullptr); + } + napi_delete_reference(_env, _ref); + _ref = nullptr; +} + } // namespace se diff --git a/native/cocos/bindings/jswrapper/napi/Object.h b/native/cocos/bindings/jswrapper/napi/Object.h index b94c1f6677..cb7cba712a 100644 --- a/native/cocos/bindings/jswrapper/napi/Object.h +++ b/native/cocos/bindings/jswrapper/napi/Object.h @@ -37,66 +37,20 @@ namespace se { class Class; class ObjectRef { private: - napi_ref _ref = nullptr; - int _refCounts = 0; - napi_env _env = nullptr; - napi_value _obj = nullptr; + napi_ref _ref{nullptr}; + napi_env _env{nullptr}; + napi_value _obj{nullptr}; + Object* _parent{nullptr}; public: - ~ObjectRef() { - deleteRef(); - } - napi_value getValue(napi_env env) const { - napi_value result; - napi_status status; - NODE_API_CALL(status, env, napi_get_reference_value(env, _ref, &result)); - assert(status == napi_ok); - assert(result != nullptr); - return result; - } - void initWeakref(napi_env env, napi_value obj) { - assert(_ref == nullptr); - _obj = obj; - _env = env; - napi_create_reference(env, obj, 0, &_ref); - } - void setWeakref(napi_env env, napi_ref ref) { - assert(_ref == nullptr); - _ref = ref; - } - void initStrongRef(napi_env env, napi_value obj) { - assert(_ref == nullptr); - _refCounts = 1; - _obj = obj; - napi_create_reference(env, obj, _refCounts, &_ref); - _env = env; - } - void incRef(napi_env env) { - assert(_refCounts == 0); - if (_refCounts == 0) { - uint32_t result = 0; - _refCounts = 1; - napi_reference_ref(env, _ref, &result); - } - } - void decRef(napi_env env) { - assert(_refCounts == 1); - uint32_t result = 0; - if (_refCounts > 0) { - _refCounts--; - if (_refCounts == 0) { - napi_reference_unref(env, _ref, &result); - } - } - } - void deleteRef() { - _refCounts = 0; - if (!_ref) { - return; - } - napi_delete_reference(_env, _ref); - _ref = nullptr; - } + ObjectRef(Object *parent); + ~ObjectRef(); + + napi_value getValue(napi_env env) const; + void init(napi_env env, napi_value obj); + void incRef(napi_env env); + void decRef(napi_env env); + void deleteRef(); }; class Object; @@ -481,6 +435,7 @@ private: ObjectRef _objRef; napi_finalize _finalizeCb = nullptr; bool _clearMappingInFinalizer = true; + bool _destructInFinalizer = false; void *_privateData = nullptr; PrivateObjectBase *_privateObject = nullptr; napi_env _env = nullptr; @@ -488,6 +443,7 @@ private: uint32_t _rootCount = 0; bool _onCleaingPrivateData = false; + friend class ObjectRef; friend class ScriptEngine; }; }; // namespace se \ No newline at end of file