diff --git a/native/cocos/scripting/js-bindings/jswrapper/State.hpp b/native/cocos/scripting/js-bindings/jswrapper/State.hpp index e98af30f2a..281e38f7d3 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/State.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/State.hpp @@ -6,7 +6,7 @@ namespace se { class Object; - class State + class State final { public: State(); diff --git a/native/cocos/scripting/js-bindings/jswrapper/Value.cpp b/native/cocos/scripting/js-bindings/jswrapper/Value.cpp index e076f4a4c0..23fab51d09 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/Value.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/Value.cpp @@ -10,115 +10,136 @@ namespace se { Value::Value() : _type(Type::Undefined) + , _autoRootUnroot(false) { + memset(&_u, 0, sizeof(_u)); } Value::Value(Type type) : _type(Type::Undefined) + , _autoRootUnroot(false) { + memset(&_u, 0, sizeof(_u)); reset(type); } Value::Value(const Value& v) : _type(Type::Undefined) + , _autoRootUnroot(false) { *this = v; } Value::Value(Value&& v) : _type(Type::Undefined) + , _autoRootUnroot(false) { *this = std::move(v); } Value::Value(bool v) : _type(Type::Undefined) + , _autoRootUnroot(false) { setBoolean(v); } Value::Value(int8_t v) : _type(Type::Undefined) + , _autoRootUnroot(false) { setInt8(v); } Value::Value(uint8_t v) : _type(Type::Undefined) + , _autoRootUnroot(false) { setUint8(v); } Value::Value(int32_t v) : _type(Type::Undefined) + , _autoRootUnroot(false) { setInt32(v); } Value::Value(uint32_t v) : _type(Type::Undefined) + , _autoRootUnroot(false) { setUint32(v); } Value::Value(int16_t v) : _type(Type::Undefined) + , _autoRootUnroot(false) { setInt16(v); } Value::Value(uint16_t v) : _type(Type::Undefined) + , _autoRootUnroot(false) { setUint16(v); } Value::Value(long v) : _type(Type::Undefined) + , _autoRootUnroot(false) { setLong(v); } Value::Value(unsigned long v) : _type(Type::Undefined) + , _autoRootUnroot(false) { setUlong(v); } Value::Value(float v) : _type(Type::Undefined) + , _autoRootUnroot(false) { setFloat(v); } Value::Value(double v) : _type(Type::Undefined) + , _autoRootUnroot(false) { setNumber(v); } Value::Value(const char* v) : _type(Type::Undefined) + , _autoRootUnroot(false) { setString(v); } Value::Value(const std::string& v) : _type(Type::Undefined) + , _autoRootUnroot(false) { setString(v); } - Value::Value(Object* o) + Value::Value(Object* o, bool autoRootUnroot/* = false*/) : _type(Type::Undefined) + , _autoRootUnroot(false) { - setObject(o); + setObject(o, autoRootUnroot); } - Value::Value(const HandleObject& o) + Value::Value(const HandleObject& o, bool autoRootUnroot/* = false*/) : _type(Type::Undefined) + , _autoRootUnroot(false) { - setObject(o); + setObject(o, autoRootUnroot); } Value::~Value() @@ -134,7 +155,11 @@ namespace se { switch (_type) { case Type::Null: + case Type::Undefined: + { + memset(&_u, 0, sizeof(_u)); break; + } case Type::Number: _u._number = v._u._number; break; @@ -146,7 +171,7 @@ namespace se { break; case Type::Object: { - setObject(v._u._object); + setObject(v._u._object, v._autoRootUnroot); } break; default: @@ -164,7 +189,11 @@ namespace se { switch (_type) { case Type::Null: + case Type::Undefined: + { + memset(&_u, 0, sizeof(_u)); break; + } case Type::Number: _u._number = v._u._number; break; @@ -176,12 +205,18 @@ namespace se { break; case Type::Object: { - if (_u._object != nullptr) + if (_u._object != nullptr) // When old value is also an Object, reset will take no effect, therefore, _u._object may not be nullptr. { + if (_autoRootUnroot) + { + _u._object->unroot(); + } _u._object->release(); } _u._object = v._u._object; - v._u._object = nullptr; + _autoRootUnroot = v._autoRootUnroot; + v._u._object = nullptr; // Reset to nullptr here to avoid 'release' operation in v.reset(Type::Undefined) since it's a move operation here. + v._autoRootUnroot = false; } break; default: @@ -193,35 +228,35 @@ namespace se { return *this; } - Value& Value::operator=(bool v) - { - setBoolean(v); - return *this; - } - - Value& Value::operator=(double v) - { - setNumber(v); - return *this; - } - - Value& Value::operator=(const std::string& v) - { - setString(v); - return *this; - } - - Value& Value::operator=(Object* o) - { - setObject(o); - return *this; - } - - Value& Value::operator=(const HandleObject& o) - { - setObject(o); - return *this; - } +// Value& Value::operator=(bool v) +// { +// setBoolean(v); +// return *this; +// } +// +// Value& Value::operator=(double v) +// { +// setNumber(v); +// return *this; +// } +// +// Value& Value::operator=(const std::string& v) +// { +// setString(v); +// return *this; +// } +// +// Value& Value::operator=(Object* o) +// { +// setObject(o); +// return *this; +// } +// +// Value& Value::operator=(const HandleObject& o) +// { +// setObject(o); +// return *this; +// } void Value::setUndefined() { @@ -318,30 +353,54 @@ namespace se { *_u._string = v; } - void Value::setObject(Object* object) + void Value::setObject(Object* object, bool autoRootUnroot/* = false*/) { - if (object != nullptr) - reset(Type::Object); - else + if (object == nullptr) + { reset(Type::Null); + return; + } + + if (_type != Type::Object) + { + reset(Type::Object); + } if (_u._object != object) { - if (_u._object != nullptr) + if (object != nullptr) { + object->addRef(); + if (autoRootUnroot) + { + object->root(); + } + } + + if (_u._object != nullptr) // When old value is also an Object, reset will take no effect, therefore, _u._object may not be nullptr. + { + if (_autoRootUnroot) + { + _u._object->unroot(); + } _u._object->release(); } _u._object = object; - if (_u._object != nullptr) + _autoRootUnroot = autoRootUnroot; + } + else + { + _autoRootUnroot = autoRootUnroot; + if (_autoRootUnroot) { - _u._object->addRef(); + _u._object->root(); } } } - void Value::setObject(const HandleObject& o) + void Value::setObject(const HandleObject& o, bool autoRootUnroot/* = false*/) { - setObject(o.get()); + setObject(o.get(), autoRootUnroot); } int8_t Value::toInt8() const @@ -469,8 +528,20 @@ namespace se { delete _u._string; break; case Type::Object: - SAFE_RELEASE(_u._object); + { + if (_u._object != nullptr) + { + if (_autoRootUnroot) + { + _u._object->unroot(); + } + _u._object->release(); + _u._object = nullptr; + } + + _autoRootUnroot = false; break; + } default: break; } diff --git a/native/cocos/scripting/js-bindings/jswrapper/Value.hpp b/native/cocos/scripting/js-bindings/jswrapper/Value.hpp index e15fec84ae..7d5b2040f0 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/Value.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/Value.hpp @@ -9,7 +9,7 @@ namespace se { class Object; - class Value + class Value final { public: enum class Type @@ -42,19 +42,19 @@ namespace se { explicit Value(double v); explicit Value(const char* v); explicit Value(const std::string& v); - explicit Value(Object* o); - explicit Value(const HandleObject& o); + explicit Value(Object* o, bool autoRootUnroot = false); + explicit Value(const HandleObject& o, bool autoRootUnroot = false); ~Value(); Value& operator=(const Value& v); Value& operator=(Value&& v); - Value& operator=(bool v); - Value& operator=(double v); - Value& operator=(const char* v); - Value& operator=(const std::string& v); - Value& operator=(Object* o); - Value& operator=(const HandleObject& o); +// Value& operator=(bool v); +// Value& operator=(double v); +// Value& operator=(const char* v); +// Value& operator=(const std::string& v); +// Value& operator=(Object* o); +// Value& operator=(const HandleObject& o); void setUndefined(); void setNull(); @@ -71,8 +71,8 @@ namespace se { void setNumber(double v); void setString(const char* v); void setString(const std::string& v); - void setObject(Object* o); - void setObject(const HandleObject& o); + void setObject(Object* o, bool autoRootUnroot = false); + void setObject(const HandleObject& o, bool autoRootUnroot = false); int8_t toInt8() const; uint8_t toUint8() const; @@ -112,6 +112,8 @@ namespace se { std::string* _string; Object* _object; } _u; + + bool _autoRootUnroot; }; using ValueArray = std::vector; diff --git a/native/cocos/scripting/js-bindings/jswrapper/chakracore/Class.hpp b/native/cocos/scripting/js-bindings/jswrapper/chakracore/Class.hpp index ee22cba9e9..07717801ef 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/chakracore/Class.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/chakracore/Class.hpp @@ -10,7 +10,7 @@ namespace se { class Object; - class Class + class Class final { private: Class(); diff --git a/native/cocos/scripting/js-bindings/jswrapper/chakracore/HelperMacros.h b/native/cocos/scripting/js-bindings/jswrapper/chakracore/HelperMacros.h index 9413fdb943..cc497f8f09 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/chakracore/HelperMacros.h +++ b/native/cocos/scripting/js-bindings/jswrapper/chakracore/HelperMacros.h @@ -34,7 +34,7 @@ se::internal::seToJsValue(state.rval(), &_jsRet); \ for (auto& v : args) \ { \ - if (v.isObject() && v.toObject()->isRooted()) \ + if (v.isObject()) \ { \ v.toObject()->unroot(); \ } \ @@ -81,7 +81,7 @@ } \ for (auto& v : args) \ { \ - if (v.isObject() && v.toObject()->isRooted()) \ + if (v.isObject()) \ { \ v.toObject()->unroot(); \ } \ @@ -111,7 +111,7 @@ } \ for (auto& v : args) \ { \ - if (v.isObject() && v.toObject()->isRooted()) \ + if (v.isObject()) \ { \ v.toObject()->unroot(); \ } \ diff --git a/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.hpp b/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.hpp index 8ae9462c49..8fea200a6a 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.hpp @@ -12,7 +12,7 @@ namespace se { class Class; - class Object : public Ref + class Object final : public Ref { private: Object(); diff --git a/native/cocos/scripting/js-bindings/jswrapper/chakracore/ScriptEngine.hpp b/native/cocos/scripting/js-bindings/jswrapper/chakracore/ScriptEngine.hpp index a9cd32fb77..c870893955 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/chakracore/ScriptEngine.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/chakracore/ScriptEngine.hpp @@ -22,7 +22,7 @@ namespace se { ~AutoHandleScope() {} }; - class ScriptEngine + class ScriptEngine final { private: ScriptEngine(); diff --git a/native/cocos/scripting/js-bindings/jswrapper/jsc/Class.cpp b/native/cocos/scripting/js-bindings/jswrapper/jsc/Class.cpp index 79475450e0..141a6d83f6 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/jsc/Class.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/jsc/Class.cpp @@ -109,11 +109,18 @@ namespace se { ScriptEngine::getInstance()->getGlobalObject()->getProperty("Function", &functionCtor); ctorObj->setProperty("constructor", functionCtor); + JSValueRef exception = nullptr; for (const auto& staticfunc : _staticFuncs) { JSStringRef name = JSStringCreateWithUTF8CString(staticfunc.name); JSObjectRef func = JSObjectMakeFunctionWithCallback(__cx, nullptr, staticfunc.callAsFunction); - JSObjectSetProperty(__cx, jsCtor, name, func, kJSPropertyAttributeNone, nullptr); + + exception = nullptr; + JSObjectSetProperty(__cx, jsCtor, name, func, kJSPropertyAttributeNone, &exception); + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + } JSStringRelease(name); } @@ -122,12 +129,24 @@ namespace se { bool exist = JSObjectHasProperty(__cx, jsCtor, prototypeName); if (exist) { - prototypeObj = JSObjectGetProperty(__cx, jsCtor, prototypeName, nullptr); + exception = nullptr; + prototypeObj = JSObjectGetProperty(__cx, jsCtor, prototypeName, &exception); + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + } } JSStringRelease(prototypeName); assert(prototypeObj != nullptr); - _proto = Object::_createJSObject(this, JSValueToObject(__cx, prototypeObj, nullptr)); + exception = nullptr; + JSObjectRef protoJSObj = JSValueToObject(__cx, prototypeObj, &exception); + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + } + + _proto = Object::_createJSObject(this, protoJSObj); _proto->root(); // reset constructor @@ -184,21 +203,6 @@ namespace se { return true; } -// JSObjectRef Class::_createJSObject(const std::string &clsName, Class** outCls) -// { -// auto iter = __clsMap.find(clsName); -// if (iter == __clsMap.end()) -// { -// *outCls = nullptr; -// return nullptr; -// } -// -// Class* thiz = iter->second; -// *outCls = thiz; -// -// return _createJSObjectWithClass(thiz); -// } - JSObjectRef Class::_createJSObjectWithClass(Class* cls) { return JSObjectMake(__cx, cls->_jsCls, nullptr); diff --git a/native/cocos/scripting/js-bindings/jswrapper/jsc/Class.hpp b/native/cocos/scripting/js-bindings/jswrapper/jsc/Class.hpp index 90f307f347..54943a9fc2 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/jsc/Class.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/jsc/Class.hpp @@ -10,7 +10,7 @@ namespace se { class Object; - class Class + class Class final { private: Class(); diff --git a/native/cocos/scripting/js-bindings/jswrapper/jsc/HelperMacros.h b/native/cocos/scripting/js-bindings/jswrapper/jsc/HelperMacros.h index edafb60c77..e8abc9117f 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/jsc/HelperMacros.h +++ b/native/cocos/scripting/js-bindings/jswrapper/jsc/HelperMacros.h @@ -33,7 +33,7 @@ se::internal::seToJsValue(_cx, state.rval(), &_jsRet); \ for (auto& v : args) \ { \ - if (v.isObject() && v.toObject()->isRooted()) \ + if (v.isObject()) \ { \ v.toObject()->unroot(); \ } \ @@ -53,8 +53,8 @@ se::State state(nativeThisObject); \ se::Object* _thisObject = state.thisObject(); \ if (_thisObject) _thisObject->_cleanup(nativeThisObject); \ - JSObjectSetPrivate(_obj, nullptr); \ ret = funcName(state); \ + JSObjectSetPrivate(_obj, nullptr); \ SAFE_RELEASE(_thisObject); \ } \ se->_setInGC(false); \ @@ -84,7 +84,7 @@ } \ for (auto& v : args) \ { \ - if (v.isObject() && v.toObject()->isRooted()) \ + if (v.isObject()) \ { \ v.toObject()->unroot(); \ } \ @@ -113,7 +113,7 @@ } \ for (auto& v : args) \ { \ - if (v.isObject() && v.toObject()->isRooted()) \ + if (v.isObject()) \ { \ v.toObject()->unroot(); \ } \ diff --git a/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.cpp b/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.cpp index d68dcf81ad..90ff6d566a 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.cpp @@ -9,6 +9,7 @@ namespace se { std::unordered_map __nativePtrToObjectMap; + std::unordered_map __nonRefNativeObjectCreatedByCtorMap; namespace { JSContextRef __cx = nullptr; @@ -37,7 +38,13 @@ namespace se { Object* Object::createArrayObject(size_t length) { - JSObjectRef jsObj = JSObjectMakeArray(__cx, 0, nullptr, nullptr); + JSValueRef exception = nullptr; + JSObjectRef jsObj = JSObjectMakeArray(__cx, 0, nullptr, &exception); + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + return nullptr; + } Object* obj = _createJSObject(nullptr, jsObj); return obj; } @@ -51,7 +58,13 @@ namespace se { { void* copiedData = malloc(byteLength); memcpy(copiedData, data, byteLength); - JSObjectRef jsobj = JSObjectMakeArrayBufferWithBytesNoCopy(__cx, copiedData, byteLength, myJSTypedArrayBytesDeallocator, nullptr, nullptr); + JSValueRef exception = nullptr; + JSObjectRef jsobj = JSObjectMakeArrayBufferWithBytesNoCopy(__cx, copiedData, byteLength, myJSTypedArrayBytesDeallocator, nullptr, &exception); + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + return nullptr; + } Object* obj = Object::_createJSObject(nullptr, jsobj); return obj; } @@ -60,7 +73,13 @@ namespace se { { void* copiedData = malloc(byteLength); memcpy(copiedData, data, byteLength); - JSObjectRef jsobj = JSObjectMakeTypedArrayWithBytesNoCopy(__cx, kJSTypedArrayTypeUint8Array, copiedData, byteLength, myJSTypedArrayBytesDeallocator, nullptr, nullptr); + JSValueRef exception = nullptr; + JSObjectRef jsobj = JSObjectMakeTypedArrayWithBytesNoCopy(__cx, kJSTypedArrayTypeUint8Array, copiedData, byteLength, myJSTypedArrayBytesDeallocator, nullptr, &exception); + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + return nullptr; + } Object* obj = Object::_createJSObject(nullptr, jsobj); return obj; } @@ -73,7 +92,14 @@ namespace se { if (ret != nullptr) { - obj = Object::_createJSObject(nullptr, JSValueToObject(__cx, ret, nullptr)); + JSValueRef exception = nullptr; + JSObjectRef jsobj = JSValueToObject(__cx, ret, &exception); + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + return nullptr; + } + obj = Object::_createJSObject(nullptr, jsobj); } return obj; } @@ -166,7 +192,13 @@ namespace se { if (exist) { - JSValueRef jsValue = JSObjectGetProperty(__cx, _obj, jsName, nullptr); + JSValueRef exception = nullptr; + JSValueRef jsValue = JSObjectGetProperty(__cx, _obj, jsName, &exception); + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + return false; + } internal::jsToSeValue(__cx, jsValue, data); } @@ -207,7 +239,14 @@ namespace se { jsValue = JSValueMakeUndefined(__cx); } - JSObjectSetProperty(__cx, obj, jsName, jsValue, kJSPropertyAttributeNone, nullptr); + JSValueRef exception = nullptr; + JSObjectSetProperty(__cx, obj, jsName, jsValue, kJSPropertyAttributeNone, &exception); + + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + } + JSStringRelease(jsName); } @@ -250,12 +289,7 @@ namespace se { // Function call failed, try to output exception if (exception != nullptr) { - std::string exceptionStr = se::ScriptEngine::getInstance()->_formatException(exception); - if (!exceptionStr.empty()) - { - LOGD("%s\n", exceptionStr.c_str()); - } - se::ScriptEngine::getInstance()->clearException(); + ScriptEngine::getInstance()->_clearException(exception); } return false; @@ -264,8 +298,13 @@ namespace se { bool Object::defineFunction(const char* funcName, JSObjectCallAsFunctionCallback func) { JSStringRef jsName = JSStringCreateWithUTF8CString(funcName); - JSObjectRef jsFunc = JSObjectMakeFunctionWithCallback(__cx, nullptr, func); - JSObjectSetProperty(__cx, _obj, jsName, jsFunc, kJSPropertyAttributeNone, nullptr); + JSObjectRef jsFunc = JSObjectMakeFunctionWithCallback(__cx, jsName, func); + JSValueRef exception = nullptr; + JSObjectSetProperty(__cx, _obj, jsName, jsFunc, kJSPropertyAttributeNone, &exception); + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + } JSStringRelease(jsName); return true; } @@ -275,7 +314,16 @@ namespace se { assert(isArray()); assert(length != nullptr); JSStringRef key = JSStringCreateWithUTF8CString("length"); - JSValueRef v = JSObjectGetProperty(__cx, _obj, key, nullptr); + JSValueRef exception = nullptr; + JSValueRef v = JSObjectGetProperty(__cx, _obj, key, &exception); + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + JSStringRelease(key); + *length = 0; + return false; + } + assert(JSValueIsNumber(__cx, v)); double len = JSValueToNumber(__cx, v, nullptr); JSStringRelease(key); @@ -288,7 +336,15 @@ namespace se { { assert(isArray()); assert(data != nullptr); - JSValueRef v = JSObjectGetPropertyAtIndex(__cx, _obj, index, nullptr); + JSValueRef exception = nullptr; + JSValueRef v = JSObjectGetPropertyAtIndex(__cx, _obj, index, &exception); + + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + return false; + } + internal::jsToSeValue(__cx, v, data); return true; @@ -300,7 +356,14 @@ namespace se { JSValueRef v; internal::seToJsValue(__cx, data, &v); - JSObjectSetPropertyAtIndex(__cx, _obj, index, v, nullptr); + JSValueRef exception = nullptr; + JSObjectSetPropertyAtIndex(__cx, _obj, index, v, &exception); + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + return false; + } + return true; } @@ -335,8 +398,21 @@ namespace se { bool Object::getTypedArrayData(uint8_t** ptr, size_t* length) const { assert(isTypedArray()); - *length = JSObjectGetTypedArrayByteLength(__cx, _obj, nullptr); - *ptr = (uint8_t*)JSObjectGetTypedArrayBytesPtr(__cx, _obj, nullptr); + JSValueRef exception = nullptr; + *length = JSObjectGetTypedArrayByteLength(__cx, _obj, &exception); + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + return false; + } + + *ptr = (uint8_t*)JSObjectGetTypedArrayBytesPtr(__cx, _obj, &exception); + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + return false; + } + return (*ptr != nullptr); } @@ -361,7 +437,13 @@ namespace se { bool Object::isArrayBuffer() const { - JSTypedArrayType type = JSValueGetTypedArrayType(__cx, _obj, nullptr); + JSValueRef exception = nullptr; + JSTypedArrayType type = JSValueGetTypedArrayType(__cx, _obj, &exception); + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + return false; + } return type == kJSTypedArrayTypeArrayBuffer; } @@ -369,8 +451,21 @@ namespace se { { assert(ptr && length); assert(isArrayBuffer()); - *length = JSObjectGetArrayBufferByteLength(__cx, _obj, nullptr); - *ptr = (uint8_t*)JSObjectGetArrayBufferBytesPtr(__cx, _obj, nullptr); + JSValueRef exception = nullptr; + *length = JSObjectGetArrayBufferByteLength(__cx, _obj, &exception); + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + return false; + } + + *ptr = (uint8_t*)JSObjectGetArrayBufferBytesPtr(__cx, _obj, &exception); + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + return false; + } + return (*ptr != nullptr); } @@ -408,6 +503,7 @@ namespace se { { ScriptEngine::getInstance()->addAfterCleanupHook([](){ __nativePtrToObjectMap.clear(); + __nonRefNativeObjectCreatedByCtorMap.clear(); __cx = nullptr; }); } diff --git a/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.hpp b/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.hpp index 8d7b0b978b..c43302d73e 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.hpp @@ -12,7 +12,7 @@ namespace se { class Class; - class Object : public Ref + class Object final : public Ref { private: Object(); @@ -87,6 +87,7 @@ namespace se { }; extern std::unordered_map __nativePtrToObjectMap; + extern std::unordered_map __nonRefNativeObjectCreatedByCtorMap; } // namespace se { diff --git a/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.cpp b/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.cpp index 87e63c30e7..9e8b6d9f65 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.cpp @@ -211,6 +211,18 @@ namespace se { return ret; } + void ScriptEngine::_clearException(JSValueRef exception) + { + if (exception != nullptr) + { + std::string exceptionStr = _formatException(exception); + if (!exceptionStr.empty()) + { + LOGD("%s\n", exceptionStr.c_str()); + } + } + } + bool ScriptEngine::isInGC() { return _isInGC; diff --git a/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.hpp b/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.hpp index b39666a982..3ea6a15720 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.hpp @@ -23,7 +23,7 @@ namespace se { ~AutoHandleScope(); }; - class ScriptEngine + class ScriptEngine final { private: ScriptEngine(); @@ -73,6 +73,7 @@ namespace se { bool _setNodeEventListener(NodeEventListener listener); std::string _formatException(JSValueRef exception); + void _clearException(JSValueRef exception); JSContextRef _getContext() const { return _cx; } private: diff --git a/native/cocos/scripting/js-bindings/jswrapper/jsc/Utils.cpp b/native/cocos/scripting/js-bindings/jswrapper/jsc/Utils.cpp index ade5891b8f..422dea37cc 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/jsc/Utils.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/jsc/Utils.cpp @@ -120,7 +120,16 @@ namespace se { } else if (JSValueIsNumber(cx, jsValue)) { - data->setNumber(JSValueToNumber(cx, jsValue, nullptr)); + JSValueRef exception = nullptr; + double number = JSValueToNumber(cx, jsValue, &exception); + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + } + else + { + data->setNumber(number); + } } else if (JSValueIsBoolean(cx, jsValue)) { @@ -134,9 +143,18 @@ namespace se { } else if (JSValueIsObject(cx, jsValue)) { - HandleObject obj(Object::_createJSObject(nullptr, JSValueToObject(cx, jsValue, nullptr))); - obj->root(); - data->setObject(obj); + JSValueRef exception = nullptr; + JSObjectRef jsobj = JSValueToObject(cx, jsValue, &exception); + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + } + else + { + HandleObject obj(Object::_createJSObject(nullptr, jsobj)); + obj->root(); + data->setObject(obj); + } } else { @@ -179,8 +197,17 @@ namespace se { void forceConvertJsValueToStdString(JSContextRef cx, JSValueRef jsval, std::string* ret) { - JSStringRef jsstr = JSValueToStringCopy(cx, jsval, nullptr); - jsStringToStdString(cx, jsstr, ret); + JSValueRef exception = nullptr; + JSStringRef jsstr = JSValueToStringCopy(cx, jsval, &exception); + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + ret->clear(); + } + else + { + jsStringToStdString(cx, jsstr, ret); + } JSStringRelease(jsstr); } @@ -227,7 +254,12 @@ namespace se { assert(ok); JSStringRef key = JSStringCreateWithUTF8CString(KEY_PRIVATE_DATA); - JSObjectSetProperty(__cx, obj, key, privateObj->_getJSObject(), kJSPropertyAttributeDontEnum, nullptr); + JSValueRef exception = nullptr; + JSObjectSetProperty(__cx, obj, key, privateObj->_getJSObject(), kJSPropertyAttributeDontEnum, &exception); + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + } JSStringRelease(key); } @@ -241,10 +273,26 @@ namespace se { bool found = JSObjectHasProperty(__cx, obj, key); if (found) { - JSValueRef privateDataVal = JSObjectGetProperty(__cx, obj, key, nullptr); - internal::PrivateData* privateData = (internal::PrivateData*)JSObjectGetPrivate(JSValueToObject(__cx, privateDataVal, nullptr)); - assert(privateData != nullptr); - data = privateData->data; + JSValueRef exception = nullptr; + JSValueRef privateDataVal = JSObjectGetProperty(__cx, obj, key, &exception); + do + { + if (exception != nullptr) + break; + + JSObjectRef jsobj = JSValueToObject(__cx, privateDataVal, &exception); + if (exception != nullptr) + break; + + internal::PrivateData* privateData = (internal::PrivateData*)JSObjectGetPrivate(jsobj); + assert(privateData != nullptr); + data = privateData->data; + } while(false); + + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + } } JSStringRelease(key); return data; @@ -262,13 +310,28 @@ namespace se { JSStringRef key = JSStringCreateWithUTF8CString(KEY_PRIVATE_DATA); //FIXME: cache the key string if (JSObjectHasProperty(__cx, obj, key)) { - JSValueRef value = JSObjectGetProperty(__cx, obj, key, nullptr); - JSObjectRef propertyObj = JSValueToObject(__cx, value, nullptr); - internal::PrivateData* privateData = (internal::PrivateData*)JSObjectGetPrivate(propertyObj); - free(privateData); - JSObjectSetPrivate(propertyObj, nullptr); - bool ok = JSObjectDeleteProperty(__cx, obj, key, nullptr); - assert(ok); + JSValueRef exception = nullptr; + do + { + JSValueRef value = JSObjectGetProperty(__cx, obj, key, &exception); + if (exception != nullptr) + break; + + JSObjectRef propertyObj = JSValueToObject(__cx, value, &exception); + if (exception != nullptr) + break; + + internal::PrivateData* privateData = (internal::PrivateData*)JSObjectGetPrivate(propertyObj); + free(privateData); + JSObjectSetPrivate(propertyObj, nullptr); + bool ok = JSObjectDeleteProperty(__cx, obj, key, nullptr); + assert(ok); + } while (false); + + if (exception != nullptr) + { + ScriptEngine::getInstance()->_clearException(exception); + } } JSStringRelease(key); diff --git a/native/cocos/scripting/js-bindings/jswrapper/sm/Class.hpp b/native/cocos/scripting/js-bindings/jswrapper/sm/Class.hpp index c4ff9b6c7b..0ed86e3688 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/sm/Class.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/sm/Class.hpp @@ -10,7 +10,7 @@ namespace se { class Object; - class Class + class Class final { private: Class(); diff --git a/native/cocos/scripting/js-bindings/jswrapper/sm/HelperMacros.h b/native/cocos/scripting/js-bindings/jswrapper/sm/HelperMacros.h index d198192d96..2affdc010f 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/sm/HelperMacros.h +++ b/native/cocos/scripting/js-bindings/jswrapper/sm/HelperMacros.h @@ -28,7 +28,7 @@ se::internal::setReturnValue(_cx, state.rval(), _argv); \ for (auto& v : args) \ { \ - if (v.isObject() && v.toObject()->isRooted()) \ + if (v.isObject()) \ { \ v.toObject()->unroot(); \ } \ @@ -71,7 +71,7 @@ } \ for (auto& v : args) \ { \ - if (v.isObject() && v.toObject()->isRooted()) \ + if (v.isObject()) \ { \ v.toObject()->unroot(); \ } \ @@ -101,7 +101,7 @@ } \ for (auto& v : args) \ { \ - if (v.isObject() && v.toObject()->isRooted()) \ + if (v.isObject()) \ { \ v.toObject()->unroot(); \ } \ diff --git a/native/cocos/scripting/js-bindings/jswrapper/sm/Object.hpp b/native/cocos/scripting/js-bindings/jswrapper/sm/Object.hpp index 250b75db39..e297f12936 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/sm/Object.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/sm/Object.hpp @@ -12,7 +12,7 @@ namespace se { class Class; - class Object : public Ref + class Object final : public Ref { private: Object(); diff --git a/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.hpp b/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.hpp index 9a5d584d99..e985f1ab8a 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.hpp @@ -23,7 +23,7 @@ namespace se { ~AutoHandleScope(); }; - class ScriptEngine + class ScriptEngine final { private: ScriptEngine(); diff --git a/native/cocos/scripting/js-bindings/jswrapper/v8/Class.hpp b/native/cocos/scripting/js-bindings/jswrapper/v8/Class.hpp index 424a2c99d9..02cf54f571 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/v8/Class.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/v8/Class.hpp @@ -10,7 +10,7 @@ namespace se { class Object; - class Class + class Class final { private: Class(); diff --git a/native/cocos/scripting/js-bindings/jswrapper/v8/HelperMacros.h b/native/cocos/scripting/js-bindings/jswrapper/v8/HelperMacros.h index b0d213639d..b97e586ec4 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/v8/HelperMacros.h +++ b/native/cocos/scripting/js-bindings/jswrapper/v8/HelperMacros.h @@ -34,7 +34,7 @@ se::internal::setReturnValue(state.rval(), _v8args); \ for (auto& v : args) \ { \ - if (v.isObject() && v.toObject()->isRooted()) \ + if (v.isObject()) \ { \ v.toObject()->unroot(); \ } \ @@ -73,7 +73,7 @@ if (_found) _property.toObject()->call(args, thisObject); \ for (auto& v : args) \ { \ - if (v.isObject() && v.toObject()->isRooted()) \ + if (v.isObject()) \ { \ v.toObject()->unroot(); \ } \ diff --git a/native/cocos/scripting/js-bindings/jswrapper/v8/Object.hpp b/native/cocos/scripting/js-bindings/jswrapper/v8/Object.hpp index b895b0c8cf..dedb79b1c7 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/v8/Object.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/v8/Object.hpp @@ -17,7 +17,7 @@ namespace se { struct PrivateData; } - class Object : public Ref + class Object final : public Ref { private: Object(); diff --git a/native/cocos/scripting/js-bindings/jswrapper/v8/ScriptEngine.hpp b/native/cocos/scripting/js-bindings/jswrapper/v8/ScriptEngine.hpp index 81299e3c71..23867a5b82 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/v8/ScriptEngine.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/v8/ScriptEngine.hpp @@ -47,7 +47,7 @@ namespace se { v8::HandleScope _handleScope; }; - class ScriptEngine + class ScriptEngine final { private: ScriptEngine();