From 0e7cb5da168ad3dfe66d3547744b8a67b1bd2447 Mon Sep 17 00:00:00 2001 From: Wenhai Lin Date: Thu, 7 Apr 2016 11:33:04 +0800 Subject: [PATCH] no more uthash --- native/cocos/audio/apple/AudioCache.mm | 6 +- native/cocos/editor-support/spine/Json.c | 2 +- .../auto/jsb_cocos2dx_audioengine_auto.cpp | 16 +- .../js-bindings/auto/jsb_cocos2dx_auto.cpp | 16 +- .../js-bindings/manual/ScriptingCore.cpp | 173 +++++++++++------- .../js-bindings/manual/ScriptingCore.h | 13 +- .../js-bindings/manual/cocos2d_specifics.cpp | 21 +-- .../js-bindings/manual/cocos2d_specifics.hpp | 3 +- .../manual/component/CCComponentJS.cpp | 2 +- .../jsb_cocos2dx_extension_manual.cpp | 5 +- .../manual/network/jsb_socketio.cpp | 3 +- .../manual/network/jsb_websocket.cpp | 7 +- .../manual/spidermonkey_specifics.h | 33 +--- 13 files changed, 156 insertions(+), 144 deletions(-) diff --git a/native/cocos/audio/apple/AudioCache.mm b/native/cocos/audio/apple/AudioCache.mm index 3f3c255af3..1bee292f1d 100644 --- a/native/cocos/audio/apple/AudioCache.mm +++ b/native/cocos/audio/apple/AudioCache.mm @@ -36,10 +36,10 @@ #define PCMDATA_CACHEMAXSIZE 1048576 -typedef ALvoid AL_APIENTRY (*alBufferDataStaticProcPtr) (const ALint bid, ALenum format, ALvoid* data, ALsizei size, ALsizei freq); -static ALvoid alBufferDataStaticProc(const ALint bid, ALenum format, ALvoid* data, ALsizei size, ALsizei freq) +typedef ALvoid AL_APIENTRY (*alBufferDataStaticProcPtr) (const ALint bid, ALenum format, ALvoid* data, ALsizei size, ALsizei freq); +static ALvoid alBufferDataStaticProc(const ALint bid, ALenum format, ALvoid* data, ALsizei size, ALsizei freq) { - static alBufferDataStaticProcPtr proc = NULL; + static alBufferDataStaticProcPtr proc = NULL; if (proc == NULL){ proc = (alBufferDataStaticProcPtr) alcGetProcAddress(NULL, (const ALCchar*) "alBufferDataStatic"); diff --git a/native/cocos/editor-support/spine/Json.c b/native/cocos/editor-support/spine/Json.c index f422f71b9d..be3c9538b1 100644 --- a/native/cocos/editor-support/spine/Json.c +++ b/native/cocos/editor-support/spine/Json.c @@ -68,7 +68,7 @@ static int Json_strcasecmp (const char* s1, const char* s2) { else if (s1 == s2) return 0; /* both are null */ else - return 1; /* s2 is nul s1 is not */ + return 1; /* s2 is null s1 is not */ } } diff --git a/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_audioengine_auto.cpp b/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_audioengine_auto.cpp index 4771a23d6b..6aa8ec14e4 100644 --- a/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_audioengine_auto.cpp +++ b/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_audioengine_auto.cpp @@ -151,20 +151,14 @@ bool js_cocos2dx_audioengine_AudioProfile_constructor(JSContext *cx, uint32_t ar void js_cocos2d_experimental_AudioProfile_finalize(JSFreeOp *fop, JSObject *obj) { CCLOGINFO("jsbindings: finalizing JS object %p (AudioProfile)", obj); - js_proxy_t* nproxy; - js_proxy_t* jsproxy; JSContext *cx = ScriptingCore::getInstance()->getGlobalContext(); JS::RootedObject jsobj(cx, obj); - jsproxy = jsb_get_js_proxy(jsobj); - if (jsproxy) { - cocos2d::experimental::AudioProfile *nobj = static_cast(jsproxy->ptr); - nproxy = jsb_get_native_proxy(jsproxy->ptr); + auto proxy = jsb_get_js_proxy(jsobj); + if (proxy) { + cocos2d::experimental::AudioProfile *nobj = static_cast(proxy->ptr); - if (nobj) { - jsb_remove_proxy(nproxy, jsproxy); - delete nobj; - } - else jsb_remove_proxy(nullptr, jsproxy); + jsb_remove_proxy(proxy); + delete nobj; } } void js_register_cocos2dx_audioengine_AudioProfile(JSContext *cx, JS::HandleObject global) { diff --git a/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_auto.cpp b/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_auto.cpp index 8c19091c61..69b1dd0cb8 100644 --- a/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_auto.cpp +++ b/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_auto.cpp @@ -41743,20 +41743,14 @@ bool js_cocos2dx_ParticleData_constructor(JSContext *cx, uint32_t argc, jsval *v void js_cocos2d_ParticleData_finalize(JSFreeOp *fop, JSObject *obj) { CCLOGINFO("jsbindings: finalizing JS object %p (ParticleData)", obj); - js_proxy_t* nproxy; - js_proxy_t* jsproxy; JSContext *cx = ScriptingCore::getInstance()->getGlobalContext(); JS::RootedObject jsobj(cx, obj); - jsproxy = jsb_get_js_proxy(jsobj); - if (jsproxy) { - cocos2d::ParticleData *nobj = static_cast(jsproxy->ptr); - nproxy = jsb_get_native_proxy(jsproxy->ptr); + auto proxy = jsb_get_js_proxy(jsobj); + if (proxy) { + cocos2d::ParticleData *nobj = static_cast(proxy->ptr); - if (nobj) { - jsb_remove_proxy(nproxy, jsproxy); - delete nobj; - } - else jsb_remove_proxy(nullptr, jsproxy); + jsb_remove_proxy(proxy); + delete nobj; } } void js_register_cocos2dx_ParticleData(JSContext *cx, JS::HandleObject global) { diff --git a/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp b/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp index ee8f31bbe9..f676931033 100644 --- a/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp +++ b/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp @@ -70,7 +70,7 @@ #define TRACE_DEBUGGER_SERVER(...) CCLOG(__VA_ARGS__) #else #define TRACE_DEBUGGER_SERVER(...) -#endif // #if DEBUG +#endif // #if COCOS2D_DEBUG #define BYTE_CODE_FILE_EXT ".jsc" @@ -87,9 +87,9 @@ static uint32_t s_nestedLoopLevel = 0; // server entry point for the bg thread static void serverEntryPoint(unsigned int port); -js_proxy_t *_native_js_global_ht = NULL; -js_proxy_t *_js_native_global_ht = NULL; std::unordered_map _js_global_type_map; +static std::unordered_map _native_js_global_map; +static std::unordered_map _js_native_global_map; static char *_js_log_buf = NULL; @@ -215,18 +215,18 @@ static void unRootObject(JSContext *cx, JS::Heap *obj) { RemoveObjectRoot(cx, obj); } -void removeJSObject(JSContext* cx, void* nativeObj) +void removeJSObject(JSContext* cx, cocos2d::Ref* nativeObj) { - js_proxy_t* nproxy; - js_proxy_t* jsproxy; - - nproxy = jsb_get_native_proxy(nativeObj); - if (nproxy) { - JS::RootedObject jsobj(cx, nproxy->obj); - jsproxy = jsb_get_js_proxy(jsobj); - RemoveObjectRoot(cx, &jsproxy->obj); - jsb_remove_proxy(nproxy, jsproxy); + auto proxy = jsb_get_native_proxy(nativeObj); + if (proxy) + { + RemoveObjectRoot(cx, &proxy->obj); + // remove the proxy here, since this was a "stack" object, not heap + // when js_finalize will be called, it will fail, but + // the correct solution is to have a new finalize for event + jsb_remove_proxy(proxy); } + else CCLOG("removeJSObject: BUG: cannot find native object = %p", nativeObj); } void ScriptingCore::executeJSFunctionWithThisObj(JS::HandleValue thisObj, JS::HandleValue callback) @@ -519,19 +519,20 @@ void ScriptingCore::addRegisterCallback(sc_register_sth callback) { registrationList.push_back(callback); } -void ScriptingCore::removeAllRoots(JSContext *cx) { - js_proxy_t *current, *tmp; - HASH_ITER(hh, _js_native_global_ht, current, tmp) { - RemoveObjectRoot(cx, ¤t->obj); - HASH_DEL(_js_native_global_ht, current); - free(current); +void ScriptingCore::removeAllRoots(JSContext *cx) +{ + // Native -> JS. No need to free "second" + _native_js_global_map.clear(); + + // JS -> Native: free "second" and "unroot" it. + auto it_js = _js_native_global_map.begin(); + while (it_js != _js_native_global_map.end()) + { + JS::RemoveObjectRoot(cx, &it_js->second->obj); + free(it_js->second); + it_js = _js_native_global_map.erase(it_js); } - HASH_ITER(hh, _native_js_global_ht, current, tmp) { - HASH_DEL(_native_js_global_ht, current); - free(current); - } - HASH_CLEAR(hh, _js_native_global_ht); - HASH_CLEAR(hh, _native_js_global_ht); + _js_native_global_map.clear(); } // Just a wrapper around JSPrincipals that allows static construction. @@ -898,22 +899,14 @@ bool ScriptingCore::log(JSContext* cx, uint32_t argc, jsval *vp) return true; } -void ScriptingCore::removeScriptObjectByObject(Ref* pObj) +void ScriptingCore::removeScriptObjectByObject(cocos2d::Ref* nativeObj) { - js_proxy_t* nproxy; - js_proxy_t* jsproxy; - void *ptr = (void*)pObj; - nproxy = jsb_get_native_proxy(ptr); - if (nproxy) - { + auto proxy = jsb_get_native_proxy(nativeObj); + if (proxy) + { JSContext *cx = getGlobalContext(); - JS::RootedObject jsobj(cx, nproxy->obj); - jsproxy = jsb_get_js_proxy(jsobj); - if (jsproxy) - { - RemoveObjectRoot(cx, &jsproxy->obj); - jsb_remove_proxy(nproxy, jsproxy); - } + JS::RemoveObjectRoot(cx, &proxy->obj); + jsb_remove_proxy(proxy); } } @@ -992,7 +985,7 @@ bool ScriptingCore::removeRootJS(JSContext *cx, uint32_t argc, jsval *vp) JS::CallArgs args = JS::CallArgsFromVp(argc, vp); JS::Heap o(args.get(0).toObjectOrNull()); if (o != nullptr) { - RemoveObjectRoot(cx, &o); + JS::RemoveObjectRoot(cx, &o); } return true; } @@ -1932,48 +1925,88 @@ bool jsb_get_reserved_slot(JSObject *obj, uint32_t idx, jsval& ret) return true; } -js_proxy_t* jsb_new_proxy(void* nativeObj, JS::HandleObject jsObj) +js_proxy_t* jsb_new_proxy(void* nativeObj, JS::HandleObject jsHandle) { - js_proxy_t* p = nullptr; - JSObject* ptr = jsObj.get(); - do { - p = (js_proxy_t *)malloc(sizeof(js_proxy_t)); - assert(p); - js_proxy_t* nativeObjJsObjtmp = NULL; - HASH_FIND_PTR(_native_js_global_ht, &nativeObj, nativeObjJsObjtmp); - assert(!nativeObjJsObjtmp); - p->ptr = nativeObj; - p->obj = ptr; - HASH_ADD_PTR(_native_js_global_ht, ptr, p); - p = (js_proxy_t *)malloc(sizeof(js_proxy_t)); - assert(p); - nativeObjJsObjtmp = NULL; - HASH_FIND_PTR(_js_native_global_ht, &ptr, nativeObjJsObjtmp); - assert(!nativeObjJsObjtmp); - p->ptr = nativeObj; - p->obj = ptr; - HASH_ADD_PTR(_js_native_global_ht, obj, p); - } while (0); - return p; + js_proxy_t* proxy = nullptr; + JSObject* jsObj = jsHandle.get(); + if (nativeObj && jsObj) + { + // native to JS index + proxy = (js_proxy_t *)malloc(sizeof(js_proxy_t)); + CC_ASSERT(proxy && "not enough memory"); + + CC_ASSERT(_native_js_global_map.find(nativeObj) == _native_js_global_map.end() && "Native Key should not be present"); + // If native proxy doesn't exist, and js proxy exist, means previous js object in this location have already been released. + // In some circumstances, js object may be released without calling its finalizer, so the proxy haven't been removed. + // For ex: var seq = cc.sequence(moveBy, cc.callFunc(this.callback, this)); + // In this code, cc.callFunc is created in parameter, and directly released after the native function call, the finalizer won't be triggered. + // The current solution keep the game running with a warning because it may cause memory leak as the native object may have been retained. + auto existJSProxy = _js_native_global_map.find(jsObj); + if (existJSProxy != _js_native_global_map.end()) { +#if COCOS2D_DEBUG + CCLOG("jsbindings: Failed to remove proxy for native object: %p, force removing it, but it may cause memory leak", existJSProxy->second->ptr); +#endif + jsb_remove_proxy(existJSProxy->second); + } + + proxy->ptr = nativeObj; + proxy->obj = jsObj; + proxy->_jsobj = jsObj; + + // One Proxy in two entries + _native_js_global_map[nativeObj] = proxy; + _js_native_global_map[jsObj] = proxy; + } + else + CCLOG("jsb_new_proxy: Invalid keys"); + + return proxy; } js_proxy_t* jsb_get_native_proxy(void* nativeObj) { - js_proxy_t* p = nullptr; - JS_GET_PROXY(p, nativeObj); - return p; + auto search = _native_js_global_map.find(nativeObj); + if(search != _native_js_global_map.end()) + return search->second; + return nullptr; } js_proxy_t* jsb_get_js_proxy(JS::HandleObject jsObj) { - js_proxy_t* p = nullptr; - JSObject* ptr = jsObj.get(); - JS_GET_NATIVE_PROXY(p, ptr); - return p; + auto search = _js_native_global_map.find(jsObj); + if(search != _js_native_global_map.end()) + return search->second; + return nullptr; } void jsb_remove_proxy(js_proxy_t* nativeProxy, js_proxy_t* jsProxy) { - JS_REMOVE_PROXY(nativeProxy, jsProxy); + js_proxy_t* proxy = nativeProxy ? nativeProxy : jsProxy; + jsb_remove_proxy(proxy); } +void jsb_remove_proxy(js_proxy_t* proxy) +{ + void* nativeKey = proxy->ptr; + JSObject* jsKey = proxy->_jsobj; + + CC_ASSERT(nativeKey && "Invalid nativeKey"); + CC_ASSERT(jsKey && "Invalid JSKey"); + + auto it_nat = _native_js_global_map.find(nativeKey); + auto it_js = _js_native_global_map.find(jsKey); + + if (it_nat != _native_js_global_map.end()) + { + _native_js_global_map.erase(it_nat); + } + else CCLOG("jsb_remove_proxy: failed. Native key not found"); + + if (it_js != _js_native_global_map.end()) + { + // Free it once, since we only have one proxy alloced entry + free(it_js->second); + _js_native_global_map.erase(it_js); + } + else CCLOG("jsb_remove_proxy: failed. JS key not found"); +} diff --git a/native/cocos/scripting/js-bindings/manual/ScriptingCore.h b/native/cocos/scripting/js-bindings/manual/ScriptingCore.h index 2e9fc84c0c..99efc2808e 100644 --- a/native/cocos/scripting/js-bindings/manual/ScriptingCore.h +++ b/native/cocos/scripting/js-bindings/manual/ScriptingCore.h @@ -535,11 +535,22 @@ js_type_class_t *jsb_register_class(JSContext *cx, JSClass *jsClass, JS::HandleO return p; } +/** creates two new proxies: one associated with the nativeObj, + and another one associated with the JsObj */ js_proxy_t* jsb_new_proxy(void* nativeObj, JS::HandleObject jsObj); + +/** returns the proxy associated with the Native* */ js_proxy_t* jsb_get_native_proxy(void* nativeObj); + +/** returns the proxy associated with the JSObject* */ js_proxy_t* jsb_get_js_proxy(JS::HandleObject jsObj); + +/** deprecated: use jsb_remove_proxy(js_proxy_t* proxy) instead */ void jsb_remove_proxy(js_proxy_t* nativeProxy, js_proxy_t* jsProxy); +/** removes both the native and js proxies */ +void jsb_remove_proxy(js_proxy_t* proxy); + template jsval getJSObject(JSContext* cx, T* nativeObj) { @@ -551,7 +562,7 @@ jsval getJSObject(JSContext* cx, T* nativeObj) return proxy ? OBJECT_TO_JSVAL(proxy->obj) : JSVAL_NULL; } -void removeJSObject(JSContext* cx, void* nativeObj); +void removeJSObject(JSContext* cx, cocos2d::Ref* nativeObj); #endif /* __SCRIPTING_CORE_H__ */ diff --git a/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp b/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp index 53ce82b524..73bfee9d84 100644 --- a/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp +++ b/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp @@ -2480,7 +2480,7 @@ bool js_cocos2dx_ActionInterval_repeat(JSContext *cx, uint32_t argc, jsval *vp) cocos2d::Repeat* action = cocos2d::Repeat::create(cobj, timesInt); // Unbind current proxy binding JS::RemoveObjectRoot(cx, &proxy->obj); - jsb_remove_proxy(jsb_get_native_proxy(cobj), proxy); + jsb_remove_proxy(proxy); // Rebind js obj with new action js_proxy_t* newProxy = jsb_new_proxy(action, obj); JS::AddNamedObjectRoot(cx, &newProxy->obj, "cocos2d::Repeat"); @@ -2505,7 +2505,7 @@ bool js_cocos2dx_ActionInterval_repeatForever(JSContext *cx, uint32_t argc, jsva cocos2d::RepeatForever* action = cocos2d::RepeatForever::create(cobj); // Unbind current proxy binding JS::RemoveObjectRoot(cx, &proxy->obj); - jsb_remove_proxy(jsb_get_native_proxy(cobj), proxy); + jsb_remove_proxy(proxy); // Rebind js obj with new action js_proxy_t* newProxy = jsb_new_proxy(action, obj); JS::AddNamedObjectRoot(cx, &newProxy->obj, "cocos2d::RepeatForever"); @@ -2540,7 +2540,7 @@ bool js_cocos2dx_ActionInterval_speed(JSContext *cx, uint32_t argc, jsval *vp) cocos2d::Speed* action = cocos2d::Speed::create(cobj, speed); // Unbind current proxy binding JS::RemoveObjectRoot(cx, &proxy->obj); - jsb_remove_proxy(jsb_get_native_proxy(cobj), proxy); + jsb_remove_proxy(proxy); // Rebind js obj with new action js_proxy_t* newProxy = jsb_new_proxy(action, obj); JS::AddNamedObjectRoot(cx, &newProxy->obj, "cocos2d::Speed"); @@ -2734,7 +2734,7 @@ bool js_cocos2dx_ActionInterval_easing(JSContext *cx, uint32_t argc, jsval *vp) // Unbind current proxy binding JS::RemoveObjectRoot(cx, &proxy->obj); - jsb_remove_proxy(jsb_get_native_proxy(cobj), proxy); + jsb_remove_proxy(proxy); // Rebind js obj with new action js_proxy_t* newProxy = jsb_new_proxy(currentAction, obj); JS::AddNamedObjectRoot(cx, &newProxy->obj, "cocos2d::EaseAction"); @@ -5289,20 +5289,17 @@ bool js_cocos2dx_PolygonInfo_constructor(JSContext *cx, uint32_t argc, jsval *vp void js_cocos2d_PolygonInfo_finalize(JSFreeOp *fop, JSObject *obj) { CCLOGINFO("jsbindings: finalizing JS object %p (PolygonInfo)", obj); - js_proxy_t* nproxy; - js_proxy_t* jsproxy; + JSContext *cx = ScriptingCore::getInstance()->getGlobalContext(); JS::RootedObject jsobj(cx, obj); - jsproxy = jsb_get_js_proxy(jsobj); - if (jsproxy) + auto proxy = jsb_get_js_proxy(jsobj); + if (proxy) { - nproxy = jsb_get_native_proxy(jsproxy->ptr); - - cocos2d::PolygonInfo *nobj = static_cast(nproxy->ptr); + auto nobj = static_cast(proxy->ptr); if (nobj) delete nobj; - jsb_remove_proxy(nproxy, jsproxy); + jsb_remove_proxy(proxy); } } diff --git a/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.hpp b/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.hpp index 77d542b17f..c3d274834c 100644 --- a/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.hpp +++ b/native/cocos/scripting/js-bindings/manual/cocos2d_specifics.hpp @@ -105,8 +105,7 @@ inline js_type_class_t *js_get_type_from_native(T* native_obj) { */ template inline js_proxy_t *js_get_or_create_proxy(JSContext *cx, T *native_obj) { - js_proxy_t *proxy; - HASH_FIND_PTR(_native_js_global_ht, &native_obj, proxy); + auto proxy = jsb_get_native_proxy(native_obj); if (!proxy) { js_type_class_t *typeProxy = js_get_type_from_native(native_obj); // Return NULL if can't find its type rather than making an assert. diff --git a/native/cocos/scripting/js-bindings/manual/component/CCComponentJS.cpp b/native/cocos/scripting/js-bindings/manual/component/CCComponentJS.cpp index e7242a7130..4b4a30df9d 100644 --- a/native/cocos/scripting/js-bindings/manual/component/CCComponentJS.cpp +++ b/native/cocos/scripting/js-bindings/manual/component/CCComponentJS.cpp @@ -84,7 +84,7 @@ ComponentJS::ComponentJS(const std::string& scriptFileName) // Unbind current proxy binding js_proxy_t* jsproxy = js_get_or_create_proxy(cx, this); JS::RemoveObjectRoot(cx, &jsproxy->obj); - jsb_remove_proxy(jsb_get_native_proxy(this), jsproxy); + jsb_remove_proxy(jsproxy); // link the native object with the javascript object jsb_new_proxy(this, jsObj->ref()); diff --git a/native/cocos/scripting/js-bindings/manual/extension/jsb_cocos2dx_extension_manual.cpp b/native/cocos/scripting/js-bindings/manual/extension/jsb_cocos2dx_extension_manual.cpp index b035d839ac..3af69c8174 100644 --- a/native/cocos/scripting/js-bindings/manual/extension/jsb_cocos2dx_extension_manual.cpp +++ b/native/cocos/scripting/js-bindings/manual/extension/jsb_cocos2dx_extension_manual.cpp @@ -559,11 +559,10 @@ public: virtual void onEvent(Ref *controlButton, Control::EventType event) { - js_proxy_t * p; - JS_GET_PROXY(p, controlButton); + js_proxy_t* p = jsb_get_native_proxy(controlButton); if (!p) { - log("Failed to get proxy for control button"); + log("Failed to get proxy for control button %p", controlButton); return; } diff --git a/native/cocos/scripting/js-bindings/manual/network/jsb_socketio.cpp b/native/cocos/scripting/js-bindings/manual/network/jsb_socketio.cpp index 381e489782..216757e986 100644 --- a/native/cocos/scripting/js-bindings/manual/network/jsb_socketio.cpp +++ b/native/cocos/scripting/js-bindings/manual/network/jsb_socketio.cpp @@ -164,8 +164,7 @@ bool js_cocos2dx_SocketIO_connect(JSContext* cx, uint32_t argc, jsval* vp) if (ret) { // link the native object with the javascript object - js_proxy_t *p; - HASH_FIND_PTR(_native_js_global_ht, &ret, p); + js_proxy_t *p = jsb_get_native_proxy(ret); if(!p) { //previous connection not found, create a new one diff --git a/native/cocos/scripting/js-bindings/manual/network/jsb_websocket.cpp b/native/cocos/scripting/js-bindings/manual/network/jsb_websocket.cpp index 2792129ffa..cdbb3271e8 100644 --- a/native/cocos/scripting/js-bindings/manual/network/jsb_websocket.cpp +++ b/native/cocos/scripting/js-bindings/manual/network/jsb_websocket.cpp @@ -150,10 +150,9 @@ public: JS::RootedValue args(cx, OBJECT_TO_JSVAL(jsobj)); ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(_JSDelegate.ref()), "onclose", 1, args.address()); - JS::RootedObject wsobj(cx, p->obj); - js_proxy_t* jsproxy = jsb_get_js_proxy(wsobj); - JS::RemoveObjectRoot(cx, &jsproxy->obj); - jsb_remove_proxy(p, jsproxy); + auto copy = &p->obj; + jsb_remove_proxy(p); + JS::RemoveObjectRoot(cx, copy); // Delete WebSocket instance CC_SAFE_DELETE(ws); // Delete self at last while websocket was closed. diff --git a/native/cocos/scripting/js-bindings/manual/spidermonkey_specifics.h b/native/cocos/scripting/js-bindings/manual/spidermonkey_specifics.h index 9583794f39..9bb26ed5ac 100644 --- a/native/cocos/scripting/js-bindings/manual/spidermonkey_specifics.h +++ b/native/cocos/scripting/js-bindings/manual/spidermonkey_specifics.h @@ -25,19 +25,23 @@ #include "spidermonkey/jsapi.h" #include "spidermonkey/jsfriendapi.h" -#include "base/uthash.h" #include "mozilla/Maybe.h" #include +// Probably we can get rid of this struct, but since a lot of code +// depends on it, we cannot remove it easily typedef struct js_proxy { + /** the native object. usually a pointer to cocos2d::Ref, but could be a pointer to another object */ void *ptr; + /** the JS object, as a heap object. Required by GC best practices */ JS::Heap obj; - UT_hash_handle hh; + /** This is the raw pointer. The same as the "obj", but 'raw'. This is needed + because under certain circumstances JS::RemoveRootObject will be called on "obj" + and "obj" will became NULL. Which is not Ok if we need to use "obj" later for other stuff + */ + JSObject* _jsobj; } js_proxy_t; -extern js_proxy_t *_native_js_global_ht; -extern js_proxy_t *_js_native_global_ht; - typedef struct js_type_class { JSClass *jsclass; mozilla::Maybe proto; @@ -53,28 +57,12 @@ public: static const char* s_name() { // return id unique for DERIVED - // ALWAYS VALID BUT STRING, NOT INT - BUT VALID AND CROSS-PLATFORM/CROSS-VERSION COMPATBLE + // ALWAYS VALID BUT STRING, NOT INT - BUT VALID AND CROSS-PLATFORM/CROSS-VERSION COMPATIBLE // AS FAR AS YOU KEEP THE CLASS NAME return typeid( DERIVED ).name(); } }; -#define JS_GET_PROXY(p, native_obj) \ -do { \ - HASH_FIND_PTR(_native_js_global_ht, &native_obj, p); \ -} while (0) - -#define JS_GET_NATIVE_PROXY(p, js_obj) \ -do { \ - HASH_FIND_PTR(_js_native_global_ht, &js_obj, p); \ -} while (0) - -#define JS_REMOVE_PROXY(nproxy, jsproxy) \ -do { \ - if (nproxy) { HASH_DEL(_native_js_global_ht, nproxy); free(nproxy); } \ - if (jsproxy) { HASH_DEL(_js_native_global_ht, jsproxy); free(jsproxy); } \ -} while (0) - #define TEST_NATIVE_OBJECT(cx, native_obj) \ if (!native_obj) { \ JS_ReportError(cx, "Invalid Native Object"); \ @@ -82,4 +70,3 @@ if (!native_obj) { \ } #endif -