From 73e45e92e4d91cd0a8ef24fd64764893187b4818 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 17 Aug 2017 18:28:37 +0800 Subject: [PATCH 01/11] Adds MappingUtils file, box2d works again. --- .../physics/CCPhysicsContactListener.cpp | 20 +++- .../physics/CCPhysicsContactListener.h | 5 + .../js-bindings/auto/jsb_box2d_auto.cpp | 40 +++---- .../auto/jsb_cocos2dx_audioengine_auto.cpp | 8 +- .../auto/jsb_cocos2dx_dragonbones_auto.cpp | 8 +- .../auto/jsb_cocos2dx_network_auto.cpp | 10 +- .../js-bindings/auto/jsb_creator_auto.cpp | 46 ++++---- .../js-bindings/jswrapper/MappingUtils.cpp | 111 ++++++++++++++++++ .../js-bindings/jswrapper/MappingUtils.hpp | 55 +++++++++ .../scripting/js-bindings/jswrapper/SeApi.h | 1 + .../jswrapper/chakracore/HelperMacros.h | 25 ---- .../jswrapper/chakracore/Object.cpp | 41 +++---- .../jswrapper/chakracore/Object.hpp | 6 - .../jswrapper/chakracore/ScriptEngine.cpp | 25 ++-- .../jswrapper/chakracore/Utils.cpp | 3 +- .../js-bindings/jswrapper/jsc/HelperMacros.h | 25 ---- .../js-bindings/jswrapper/jsc/Object.cpp | 30 +++-- .../js-bindings/jswrapper/jsc/Object.hpp | 5 - .../jswrapper/jsc/ScriptEngine.cpp | 25 ++-- .../js-bindings/jswrapper/jsc/Utils.cpp | 3 +- .../js-bindings/jswrapper/sm/HelperMacros.h | 25 ---- .../js-bindings/jswrapper/sm/Object.cpp | 26 ++-- .../js-bindings/jswrapper/sm/Object.hpp | 5 - .../js-bindings/jswrapper/sm/ScriptEngine.cpp | 33 +++--- .../js-bindings/jswrapper/sm/ScriptEngine.hpp | 2 + .../js-bindings/jswrapper/sm/Utils.cpp | 3 +- .../js-bindings/jswrapper/v8/HelperMacros.h | 18 --- .../js-bindings/jswrapper/v8/Object.cpp | 28 ++--- .../js-bindings/jswrapper/v8/Object.hpp | 4 - .../js-bindings/jswrapper/v8/ScriptEngine.cpp | 23 ++-- .../js-bindings/jswrapper/v8/Utils.cpp | 3 +- .../js-bindings/manual/jsb_box2d_manual.cpp | 39 +++--- .../manual/jsb_cocos2dx_manual.cpp | 8 +- .../js-bindings/manual/jsb_conversions.hpp | 34 +++--- .../manual/jsb_dragonbones_manual.cpp | 4 +- .../js-bindings/manual/jsb_global.cpp | 14 ++- .../scripting/js-bindings/manual/jsb_node.cpp | 4 +- .../js-bindings/manual/jsb_socketio.cpp | 4 +- .../js-bindings/manual/jsb_websocket.cpp | 16 +-- .../js-bindings/proj.android/Android.mk | 2 + 40 files changed, 436 insertions(+), 351 deletions(-) create mode 100644 native/cocos/scripting/js-bindings/jswrapper/MappingUtils.cpp create mode 100644 native/cocos/scripting/js-bindings/jswrapper/MappingUtils.hpp diff --git a/native/cocos/editor-support/creator/physics/CCPhysicsContactListener.cpp b/native/cocos/editor-support/creator/physics/CCPhysicsContactListener.cpp index 645eef1f48..c901ba3bfd 100644 --- a/native/cocos/editor-support/creator/physics/CCPhysicsContactListener.cpp +++ b/native/cocos/editor-support/creator/physics/CCPhysicsContactListener.cpp @@ -4,12 +4,26 @@ using namespace cocos2d; namespace creator { - -PhysicsContactListener::PhysicsContactListener() + +std::vector PhysicsContactListener::__allInstances; + +const std::vector& PhysicsContactListener::getAllInstances() { + return __allInstances; } -PhysicsContactListener::~PhysicsContactListener() { +PhysicsContactListener::PhysicsContactListener() +{ + __allInstances.push_back(this); +} + +PhysicsContactListener::~PhysicsContactListener() +{ + auto iter = std::find(__allInstances.begin(), __allInstances.end(), this); + if (iter != __allInstances.end()) + { + __allInstances.erase(iter); + } } void PhysicsContactListener::setBeginContact(const std::function& callback) diff --git a/native/cocos/editor-support/creator/physics/CCPhysicsContactListener.h b/native/cocos/editor-support/creator/physics/CCPhysicsContactListener.h index dea536cb33..21df27bb7d 100644 --- a/native/cocos/editor-support/creator/physics/CCPhysicsContactListener.h +++ b/native/cocos/editor-support/creator/physics/CCPhysicsContactListener.h @@ -38,6 +38,9 @@ namespace creator { class CC_DLL PhysicsContactListener : public b2ContactListener { public: + + static const std::vector& getAllInstances(); + PhysicsContactListener(); virtual ~PhysicsContactListener(); @@ -65,6 +68,8 @@ protected: PhysicsContactImpulse _impulse; std::unordered_map _contactMap; + + static std::vector __allInstances; }; } diff --git a/native/cocos/scripting/js-bindings/auto/jsb_box2d_auto.cpp b/native/cocos/scripting/js-bindings/auto/jsb_box2d_auto.cpp index 07766e26b8..501bfa2db2 100644 --- a/native/cocos/scripting/js-bindings/auto/jsb_box2d_auto.cpp +++ b/native/cocos/scripting/js-bindings/auto/jsb_box2d_auto.cpp @@ -682,7 +682,7 @@ static bool js_box2dclasses_b2CircleShape_constructor(se::State& s) { b2CircleShape* cobj = new (std::nothrow) b2CircleShape(); s.thisObject()->setPrivateData(cobj); - se::__nonRefNativeObjectCreatedByCtorMap.emplace(cobj, true); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); return true; } SE_BIND_CTOR(js_box2dclasses_b2CircleShape_constructor, __jsb_b2CircleShape_class, js_b2CircleShape_finalize) @@ -694,10 +694,10 @@ extern se::Object* __jsb_b2Shape_proto; static bool js_b2CircleShape_finalize(se::State& s) { cocos2d::log("jsbindings: finalizing JS object %p (b2CircleShape)", s.nativeThisObject()); - auto iter = se::__nonRefNativeObjectCreatedByCtorMap.find(s.nativeThisObject()); - if (iter != se::__nonRefNativeObjectCreatedByCtorMap.end()) + auto iter = se::NonRefNativePtrCreatedByCtorMap::find(s.nativeThisObject()); + if (iter != se::NonRefNativePtrCreatedByCtorMap::end()) { - se::__nonRefNativeObjectCreatedByCtorMap.erase(iter); + se::NonRefNativePtrCreatedByCtorMap::erase(iter); b2CircleShape* cobj = (b2CircleShape*)s.nativeThisObject(); delete cobj; } @@ -900,7 +900,7 @@ static bool js_box2dclasses_b2EdgeShape_constructor(se::State& s) { b2EdgeShape* cobj = new (std::nothrow) b2EdgeShape(); s.thisObject()->setPrivateData(cobj); - se::__nonRefNativeObjectCreatedByCtorMap.emplace(cobj, true); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); return true; } SE_BIND_CTOR(js_box2dclasses_b2EdgeShape_constructor, __jsb_b2EdgeShape_class, js_b2EdgeShape_finalize) @@ -912,10 +912,10 @@ extern se::Object* __jsb_b2Shape_proto; static bool js_b2EdgeShape_finalize(se::State& s) { cocos2d::log("jsbindings: finalizing JS object %p (b2EdgeShape)", s.nativeThisObject()); - auto iter = se::__nonRefNativeObjectCreatedByCtorMap.find(s.nativeThisObject()); - if (iter != se::__nonRefNativeObjectCreatedByCtorMap.end()) + auto iter = se::NonRefNativePtrCreatedByCtorMap::find(s.nativeThisObject()); + if (iter != se::NonRefNativePtrCreatedByCtorMap::end()) { - se::__nonRefNativeObjectCreatedByCtorMap.erase(iter); + se::NonRefNativePtrCreatedByCtorMap::erase(iter); b2EdgeShape* cobj = (b2EdgeShape*)s.nativeThisObject(); delete cobj; } @@ -1168,7 +1168,7 @@ static bool js_box2dclasses_b2ChainShape_constructor(se::State& s) { b2ChainShape* cobj = new (std::nothrow) b2ChainShape(); s.thisObject()->setPrivateData(cobj); - se::__nonRefNativeObjectCreatedByCtorMap.emplace(cobj, true); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); return true; } SE_BIND_CTOR(js_box2dclasses_b2ChainShape_constructor, __jsb_b2ChainShape_class, js_b2ChainShape_finalize) @@ -1180,10 +1180,10 @@ extern se::Object* __jsb_b2Shape_proto; static bool js_b2ChainShape_finalize(se::State& s) { cocos2d::log("jsbindings: finalizing JS object %p (b2ChainShape)", s.nativeThisObject()); - auto iter = se::__nonRefNativeObjectCreatedByCtorMap.find(s.nativeThisObject()); - if (iter != se::__nonRefNativeObjectCreatedByCtorMap.end()) + auto iter = se::NonRefNativePtrCreatedByCtorMap::find(s.nativeThisObject()); + if (iter != se::NonRefNativePtrCreatedByCtorMap::end()) { - se::__nonRefNativeObjectCreatedByCtorMap.erase(iter); + se::NonRefNativePtrCreatedByCtorMap::erase(iter); b2ChainShape* cobj = (b2ChainShape*)s.nativeThisObject(); delete cobj; } @@ -1422,7 +1422,7 @@ static bool js_box2dclasses_b2PolygonShape_constructor(se::State& s) { b2PolygonShape* cobj = new (std::nothrow) b2PolygonShape(); s.thisObject()->setPrivateData(cobj); - se::__nonRefNativeObjectCreatedByCtorMap.emplace(cobj, true); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); return true; } SE_BIND_CTOR(js_box2dclasses_b2PolygonShape_constructor, __jsb_b2PolygonShape_class, js_b2PolygonShape_finalize) @@ -1434,10 +1434,10 @@ extern se::Object* __jsb_b2Shape_proto; static bool js_b2PolygonShape_finalize(se::State& s) { cocos2d::log("jsbindings: finalizing JS object %p (b2PolygonShape)", s.nativeThisObject()); - auto iter = se::__nonRefNativeObjectCreatedByCtorMap.find(s.nativeThisObject()); - if (iter != se::__nonRefNativeObjectCreatedByCtorMap.end()) + auto iter = se::NonRefNativePtrCreatedByCtorMap::find(s.nativeThisObject()); + if (iter != se::NonRefNativePtrCreatedByCtorMap::end()) { - se::__nonRefNativeObjectCreatedByCtorMap.erase(iter); + se::NonRefNativePtrCreatedByCtorMap::erase(iter); b2PolygonShape* cobj = (b2PolygonShape*)s.nativeThisObject(); delete cobj; } @@ -3907,7 +3907,7 @@ static bool js_box2dclasses_b2World_constructor(se::State& s) SE_PRECONDITION2(ok, false, "js_box2dclasses_b2World_constructor : Error processing arguments"); b2World* cobj = new (std::nothrow) b2World(arg0); s.thisObject()->setPrivateData(cobj); - se::__nonRefNativeObjectCreatedByCtorMap.emplace(cobj, true); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); return true; } SE_BIND_CTOR(js_box2dclasses_b2World_constructor, __jsb_b2World_class, js_b2World_finalize) @@ -3918,10 +3918,10 @@ SE_BIND_CTOR(js_box2dclasses_b2World_constructor, __jsb_b2World_class, js_b2Worl static bool js_b2World_finalize(se::State& s) { cocos2d::log("jsbindings: finalizing JS object %p (b2World)", s.nativeThisObject()); - auto iter = se::__nonRefNativeObjectCreatedByCtorMap.find(s.nativeThisObject()); - if (iter != se::__nonRefNativeObjectCreatedByCtorMap.end()) + auto iter = se::NonRefNativePtrCreatedByCtorMap::find(s.nativeThisObject()); + if (iter != se::NonRefNativePtrCreatedByCtorMap::end()) { - se::__nonRefNativeObjectCreatedByCtorMap.erase(iter); + se::NonRefNativePtrCreatedByCtorMap::erase(iter); b2World* cobj = (b2World*)s.nativeThisObject(); delete cobj; } 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 2e1b3fe4bf..56bc3c2f66 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 @@ -96,7 +96,7 @@ static bool js_cocos2dx_audioengine_AudioProfile_constructor(se::State& s) { cocos2d::experimental::AudioProfile* cobj = new (std::nothrow) cocos2d::experimental::AudioProfile(); s.thisObject()->setPrivateData(cobj); - se::__nonRefNativeObjectCreatedByCtorMap.emplace(cobj, true); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); return true; } SE_BIND_CTOR(js_cocos2dx_audioengine_AudioProfile_constructor, __jsb_cocos2d_experimental_AudioProfile_class, js_cocos2d_experimental_AudioProfile_finalize) @@ -107,10 +107,10 @@ SE_BIND_CTOR(js_cocos2dx_audioengine_AudioProfile_constructor, __jsb_cocos2d_exp static bool js_cocos2d_experimental_AudioProfile_finalize(se::State& s) { cocos2d::log("jsbindings: finalizing JS object %p (cocos2d::experimental::AudioProfile)", s.nativeThisObject()); - auto iter = se::__nonRefNativeObjectCreatedByCtorMap.find(s.nativeThisObject()); - if (iter != se::__nonRefNativeObjectCreatedByCtorMap.end()) + auto iter = se::NonRefNativePtrCreatedByCtorMap::find(s.nativeThisObject()); + if (iter != se::NonRefNativePtrCreatedByCtorMap::end()) { - se::__nonRefNativeObjectCreatedByCtorMap.erase(iter); + se::NonRefNativePtrCreatedByCtorMap::erase(iter); cocos2d::experimental::AudioProfile* cobj = (cocos2d::experimental::AudioProfile*)s.nativeThisObject(); delete cobj; } diff --git a/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_dragonbones_auto.cpp b/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_dragonbones_auto.cpp index 4527bab2ef..d23f8e16d7 100644 --- a/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_dragonbones_auto.cpp +++ b/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_dragonbones_auto.cpp @@ -4860,7 +4860,7 @@ static bool js_cocos2dx_dragonbones_CCFactory_constructor(se::State& s) { dragonBones::CCFactory* cobj = new (std::nothrow) dragonBones::CCFactory(); s.thisObject()->setPrivateData(cobj); - se::__nonRefNativeObjectCreatedByCtorMap.emplace(cobj, true); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); return true; } SE_BIND_CTOR(js_cocos2dx_dragonbones_CCFactory_constructor, __jsb_dragonBones_CCFactory_class, js_dragonBones_CCFactory_finalize) @@ -4872,10 +4872,10 @@ extern se::Object* __jsb_dragonBones_BaseFactory_proto; static bool js_dragonBones_CCFactory_finalize(se::State& s) { cocos2d::log("jsbindings: finalizing JS object %p (dragonBones::CCFactory)", s.nativeThisObject()); - auto iter = se::__nonRefNativeObjectCreatedByCtorMap.find(s.nativeThisObject()); - if (iter != se::__nonRefNativeObjectCreatedByCtorMap.end()) + auto iter = se::NonRefNativePtrCreatedByCtorMap::find(s.nativeThisObject()); + if (iter != se::NonRefNativePtrCreatedByCtorMap::end()) { - se::__nonRefNativeObjectCreatedByCtorMap.erase(iter); + se::NonRefNativePtrCreatedByCtorMap::erase(iter); dragonBones::CCFactory* cobj = (dragonBones::CCFactory*)s.nativeThisObject(); delete cobj; } diff --git a/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_network_auto.cpp b/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_network_auto.cpp index a4621f3ac4..e4fd500384 100644 --- a/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_network_auto.cpp +++ b/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_network_auto.cpp @@ -205,7 +205,7 @@ static bool js_cocos2dx_network_Downloader_constructor(se::State& s) if (!ok) { ok = true; break; } cocos2d::network::Downloader* cobj = new (std::nothrow) cocos2d::network::Downloader(arg0); s.thisObject()->setPrivateData(cobj); - se::__nonRefNativeObjectCreatedByCtorMap.emplace(cobj, true); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); return true; } } while(false); @@ -213,7 +213,7 @@ static bool js_cocos2dx_network_Downloader_constructor(se::State& s) if (argc == 0) { cocos2d::network::Downloader* cobj = new (std::nothrow) cocos2d::network::Downloader(); s.thisObject()->setPrivateData(cobj); - se::__nonRefNativeObjectCreatedByCtorMap.emplace(cobj, true); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); return true; } } while(false); @@ -228,10 +228,10 @@ SE_BIND_CTOR(js_cocos2dx_network_Downloader_constructor, __jsb_cocos2d_network_D static bool js_cocos2d_network_Downloader_finalize(se::State& s) { cocos2d::log("jsbindings: finalizing JS object %p (cocos2d::network::Downloader)", s.nativeThisObject()); - auto iter = se::__nonRefNativeObjectCreatedByCtorMap.find(s.nativeThisObject()); - if (iter != se::__nonRefNativeObjectCreatedByCtorMap.end()) + auto iter = se::NonRefNativePtrCreatedByCtorMap::find(s.nativeThisObject()); + if (iter != se::NonRefNativePtrCreatedByCtorMap::end()) { - se::__nonRefNativeObjectCreatedByCtorMap.erase(iter); + se::NonRefNativePtrCreatedByCtorMap::erase(iter); cocos2d::network::Downloader* cobj = (cocos2d::network::Downloader*)s.nativeThisObject(); delete cobj; } diff --git a/native/cocos/scripting/js-bindings/auto/jsb_creator_auto.cpp b/native/cocos/scripting/js-bindings/auto/jsb_creator_auto.cpp index 6b8a511a5a..39c4e95dc3 100644 --- a/native/cocos/scripting/js-bindings/auto/jsb_creator_auto.cpp +++ b/native/cocos/scripting/js-bindings/auto/jsb_creator_auto.cpp @@ -1584,7 +1584,7 @@ static bool js_creator_PhysicsDebugDraw_constructor(se::State& s) { creator::PhysicsDebugDraw* cobj = new (std::nothrow) creator::PhysicsDebugDraw(); s.thisObject()->setPrivateData(cobj); - se::__nonRefNativeObjectCreatedByCtorMap.emplace(cobj, true); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); return true; } SE_BIND_CTOR(js_creator_PhysicsDebugDraw_constructor, __jsb_creator_PhysicsDebugDraw_class, js_creator_PhysicsDebugDraw_finalize) @@ -1596,10 +1596,10 @@ extern se::Object* __jsb_b2Draw_proto; static bool js_creator_PhysicsDebugDraw_finalize(se::State& s) { cocos2d::log("jsbindings: finalizing JS object %p (creator::PhysicsDebugDraw)", s.nativeThisObject()); - auto iter = se::__nonRefNativeObjectCreatedByCtorMap.find(s.nativeThisObject()); - if (iter != se::__nonRefNativeObjectCreatedByCtorMap.end()) + auto iter = se::NonRefNativePtrCreatedByCtorMap::find(s.nativeThisObject()); + if (iter != se::NonRefNativePtrCreatedByCtorMap::end()) { - se::__nonRefNativeObjectCreatedByCtorMap.erase(iter); + se::NonRefNativePtrCreatedByCtorMap::erase(iter); creator::PhysicsDebugDraw* cobj = (creator::PhysicsDebugDraw*)s.nativeThisObject(); delete cobj; } @@ -1751,7 +1751,7 @@ static bool js_creator_PhysicsWorldManifoldWrapper_constructor(se::State& s) { creator::PhysicsWorldManifoldWrapper* cobj = new (std::nothrow) creator::PhysicsWorldManifoldWrapper(); s.thisObject()->setPrivateData(cobj); - se::__nonRefNativeObjectCreatedByCtorMap.emplace(cobj, true); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); return true; } SE_BIND_CTOR(js_creator_PhysicsWorldManifoldWrapper_constructor, __jsb_creator_PhysicsWorldManifoldWrapper_class, js_creator_PhysicsWorldManifoldWrapper_finalize) @@ -1988,7 +1988,7 @@ static bool js_creator_PhysicsManifoldWrapper_constructor(se::State& s) { creator::PhysicsManifoldWrapper* cobj = new (std::nothrow) creator::PhysicsManifoldWrapper(); s.thisObject()->setPrivateData(cobj); - se::__nonRefNativeObjectCreatedByCtorMap.emplace(cobj, true); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); return true; } SE_BIND_CTOR(js_creator_PhysicsManifoldWrapper_constructor, __jsb_creator_PhysicsManifoldWrapper_class, js_creator_PhysicsManifoldWrapper_finalize) @@ -2128,7 +2128,7 @@ static bool js_creator_PhysicsUtils_constructor(se::State& s) { creator::PhysicsUtils* cobj = new (std::nothrow) creator::PhysicsUtils(); s.thisObject()->setPrivateData(cobj); - se::__nonRefNativeObjectCreatedByCtorMap.emplace(cobj, true); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); return true; } SE_BIND_CTOR(js_creator_PhysicsUtils_constructor, __jsb_creator_PhysicsUtils_class, js_creator_PhysicsUtils_finalize) @@ -2139,10 +2139,10 @@ SE_BIND_CTOR(js_creator_PhysicsUtils_constructor, __jsb_creator_PhysicsUtils_cla static bool js_creator_PhysicsUtils_finalize(se::State& s) { cocos2d::log("jsbindings: finalizing JS object %p (creator::PhysicsUtils)", s.nativeThisObject()); - auto iter = se::__nonRefNativeObjectCreatedByCtorMap.find(s.nativeThisObject()); - if (iter != se::__nonRefNativeObjectCreatedByCtorMap.end()) + auto iter = se::NonRefNativePtrCreatedByCtorMap::find(s.nativeThisObject()); + if (iter != se::NonRefNativePtrCreatedByCtorMap::end()) { - se::__nonRefNativeObjectCreatedByCtorMap.erase(iter); + se::NonRefNativePtrCreatedByCtorMap::erase(iter); creator::PhysicsUtils* cobj = (creator::PhysicsUtils*)s.nativeThisObject(); delete cobj; } @@ -2239,7 +2239,7 @@ static bool js_creator_PhysicsContactImpulse_constructor(se::State& s) { creator::PhysicsContactImpulse* cobj = new (std::nothrow) creator::PhysicsContactImpulse(); s.thisObject()->setPrivateData(cobj); - se::__nonRefNativeObjectCreatedByCtorMap.emplace(cobj, true); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); return true; } SE_BIND_CTOR(js_creator_PhysicsContactImpulse_constructor, __jsb_creator_PhysicsContactImpulse_class, js_creator_PhysicsContactImpulse_finalize) @@ -2319,7 +2319,7 @@ static bool js_creator_PhysicsContactListener_constructor(se::State& s) { creator::PhysicsContactListener* cobj = new (std::nothrow) creator::PhysicsContactListener(); s.thisObject()->setPrivateData(cobj); - se::__nonRefNativeObjectCreatedByCtorMap.emplace(cobj, true); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); return true; } SE_BIND_CTOR(js_creator_PhysicsContactListener_constructor, __jsb_creator_PhysicsContactListener_class, js_creator_PhysicsContactListener_finalize) @@ -2331,10 +2331,10 @@ extern se::Object* __jsb_b2ContactListener_proto; static bool js_creator_PhysicsContactListener_finalize(se::State& s) { cocos2d::log("jsbindings: finalizing JS object %p (creator::PhysicsContactListener)", s.nativeThisObject()); - auto iter = se::__nonRefNativeObjectCreatedByCtorMap.find(s.nativeThisObject()); - if (iter != se::__nonRefNativeObjectCreatedByCtorMap.end()) + auto iter = se::NonRefNativePtrCreatedByCtorMap::find(s.nativeThisObject()); + if (iter != se::NonRefNativePtrCreatedByCtorMap::end()) { - se::__nonRefNativeObjectCreatedByCtorMap.erase(iter); + se::NonRefNativePtrCreatedByCtorMap::erase(iter); creator::PhysicsContactListener* cobj = (creator::PhysicsContactListener*)s.nativeThisObject(); delete cobj; } @@ -2415,7 +2415,7 @@ static bool js_creator_PhysicsAABBQueryCallback_constructor(se::State& s) { creator::PhysicsAABBQueryCallback* cobj = new (std::nothrow) creator::PhysicsAABBQueryCallback(); s.thisObject()->setPrivateData(cobj); - se::__nonRefNativeObjectCreatedByCtorMap.emplace(cobj, true); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); return true; } SE_BIND_CTOR(js_creator_PhysicsAABBQueryCallback_constructor, __jsb_creator_PhysicsAABBQueryCallback_class, js_creator_PhysicsAABBQueryCallback_finalize) @@ -2427,10 +2427,10 @@ extern se::Object* __jsb_b2QueryCallback_proto; static bool js_creator_PhysicsAABBQueryCallback_finalize(se::State& s) { cocos2d::log("jsbindings: finalizing JS object %p (creator::PhysicsAABBQueryCallback)", s.nativeThisObject()); - auto iter = se::__nonRefNativeObjectCreatedByCtorMap.find(s.nativeThisObject()); - if (iter != se::__nonRefNativeObjectCreatedByCtorMap.end()) + auto iter = se::NonRefNativePtrCreatedByCtorMap::find(s.nativeThisObject()); + if (iter != se::NonRefNativePtrCreatedByCtorMap::end()) { - se::__nonRefNativeObjectCreatedByCtorMap.erase(iter); + se::NonRefNativePtrCreatedByCtorMap::erase(iter); creator::PhysicsAABBQueryCallback* cobj = (creator::PhysicsAABBQueryCallback*)s.nativeThisObject(); delete cobj; } @@ -2519,7 +2519,7 @@ static bool js_creator_PhysicsRayCastCallback_constructor(se::State& s) { creator::PhysicsRayCastCallback* cobj = new (std::nothrow) creator::PhysicsRayCastCallback(); s.thisObject()->setPrivateData(cobj); - se::__nonRefNativeObjectCreatedByCtorMap.emplace(cobj, true); + se::NonRefNativePtrCreatedByCtorMap::emplace(cobj); return true; } SE_BIND_CTOR(js_creator_PhysicsRayCastCallback_constructor, __jsb_creator_PhysicsRayCastCallback_class, js_creator_PhysicsRayCastCallback_finalize) @@ -2531,10 +2531,10 @@ extern se::Object* __jsb_b2RayCastCallback_proto; static bool js_creator_PhysicsRayCastCallback_finalize(se::State& s) { cocos2d::log("jsbindings: finalizing JS object %p (creator::PhysicsRayCastCallback)", s.nativeThisObject()); - auto iter = se::__nonRefNativeObjectCreatedByCtorMap.find(s.nativeThisObject()); - if (iter != se::__nonRefNativeObjectCreatedByCtorMap.end()) + auto iter = se::NonRefNativePtrCreatedByCtorMap::find(s.nativeThisObject()); + if (iter != se::NonRefNativePtrCreatedByCtorMap::end()) { - se::__nonRefNativeObjectCreatedByCtorMap.erase(iter); + se::NonRefNativePtrCreatedByCtorMap::erase(iter); creator::PhysicsRayCastCallback* cobj = (creator::PhysicsRayCastCallback*)s.nativeThisObject(); delete cobj; } diff --git a/native/cocos/scripting/js-bindings/jswrapper/MappingUtils.cpp b/native/cocos/scripting/js-bindings/jswrapper/MappingUtils.cpp new file mode 100644 index 0000000000..99a9533fc6 --- /dev/null +++ b/native/cocos/scripting/js-bindings/jswrapper/MappingUtils.cpp @@ -0,0 +1,111 @@ +// +// MappingUtils.cpp +// cocos2d_js_bindings +// +// Created by James Chen on 8/17/17. +// +// + +#include "MappingUtils.hpp" + +namespace se { + +// NativePtrToObjectMap +NativePtrToObjectMap::Map NativePtrToObjectMap::__nativePtrToObjectMap; + +void NativePtrToObjectMap::emplace(void* nativeObj, Object* seObj) +{ + __nativePtrToObjectMap.emplace(nativeObj, seObj); +} + +NativePtrToObjectMap::Map::iterator NativePtrToObjectMap::find(void* nativeObj) +{ + return __nativePtrToObjectMap.find(nativeObj); +} + +NativePtrToObjectMap::Map::iterator NativePtrToObjectMap::erase(Map::iterator iter) +{ + return __nativePtrToObjectMap.erase(iter); +} + +void NativePtrToObjectMap::erase(void* nativeObj) +{ + __nativePtrToObjectMap.erase(nativeObj); +} + +void NativePtrToObjectMap::clear() +{ + __nativePtrToObjectMap.clear(); +} + +size_t NativePtrToObjectMap::size() +{ + return __nativePtrToObjectMap.size(); +} + +const NativePtrToObjectMap::Map& NativePtrToObjectMap::instance() +{ + return __nativePtrToObjectMap; +} + +NativePtrToObjectMap::Map::iterator NativePtrToObjectMap::begin() +{ + return __nativePtrToObjectMap.begin(); +} + +NativePtrToObjectMap::Map::iterator NativePtrToObjectMap::end() +{ + return __nativePtrToObjectMap.end(); +} + +// NonRefNativePtrCreatedByCtorMap +NonRefNativePtrCreatedByCtorMap::Map NonRefNativePtrCreatedByCtorMap::__nonRefNativeObjectCreatedByCtorMap; + +void NonRefNativePtrCreatedByCtorMap::emplace(void* nativeObj) +{ + __nonRefNativeObjectCreatedByCtorMap.emplace(nativeObj, true); +} + +NonRefNativePtrCreatedByCtorMap::Map::iterator NonRefNativePtrCreatedByCtorMap::find(void* nativeObj) +{ + return __nonRefNativeObjectCreatedByCtorMap.find(nativeObj); +} + +NonRefNativePtrCreatedByCtorMap::Map::iterator NonRefNativePtrCreatedByCtorMap::erase(Map::iterator iter) +{ + return __nonRefNativeObjectCreatedByCtorMap.erase(iter); +} + +void NonRefNativePtrCreatedByCtorMap::erase(void* nativeObj) +{ + __nonRefNativeObjectCreatedByCtorMap.erase(nativeObj); +} + +void NonRefNativePtrCreatedByCtorMap::clear() +{ + __nonRefNativeObjectCreatedByCtorMap.clear(); +} + +size_t NonRefNativePtrCreatedByCtorMap::size() +{ + return __nonRefNativeObjectCreatedByCtorMap.size(); +} + +const NonRefNativePtrCreatedByCtorMap::Map& NonRefNativePtrCreatedByCtorMap::instance() +{ + return __nonRefNativeObjectCreatedByCtorMap; +} + +NonRefNativePtrCreatedByCtorMap::Map::iterator NonRefNativePtrCreatedByCtorMap::begin() +{ + return __nonRefNativeObjectCreatedByCtorMap.begin(); +} + +NonRefNativePtrCreatedByCtorMap::Map::iterator NonRefNativePtrCreatedByCtorMap::end() +{ + return __nonRefNativeObjectCreatedByCtorMap.end(); +} + + + +} // namespace se { diff --git a/native/cocos/scripting/js-bindings/jswrapper/MappingUtils.hpp b/native/cocos/scripting/js-bindings/jswrapper/MappingUtils.hpp new file mode 100644 index 0000000000..ad5fc9ee8d --- /dev/null +++ b/native/cocos/scripting/js-bindings/jswrapper/MappingUtils.hpp @@ -0,0 +1,55 @@ +#pragma once + +#include + +namespace se { + + class Object; + + class NativePtrToObjectMap + { + public: + // key: native ptr, value: se::Object + using Map = std::unordered_map; + + static Map::iterator find(void* nativeObj); + static Map::iterator erase(Map::iterator iter); + static void erase(void* nativeObj); + static void clear(); + static size_t size(); + + static const Map& instance(); + + static Map::iterator begin(); + static Map::iterator end(); + + private: + static void emplace(void* nativeObj, Object* seObj); + static Map __nativePtrToObjectMap; + + friend class Object; + }; + + class NonRefNativePtrCreatedByCtorMap + { + public: + // key: native ptr, value: non-ref object created by ctor + using Map = std::unordered_map; + + static void emplace(void* nativeObj); + static Map::iterator find(void* nativeObj); + static Map::iterator erase(Map::iterator iter); + static void erase(void* nativeObj); + static void clear(); + static size_t size(); + + static const Map& instance(); + + static Map::iterator begin(); + static Map::iterator end(); + + private: + static Map __nonRefNativeObjectCreatedByCtorMap; + }; + +} // namespace se { diff --git a/native/cocos/scripting/js-bindings/jswrapper/SeApi.h b/native/cocos/scripting/js-bindings/jswrapper/SeApi.h index c421c56868..15a27525e7 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/SeApi.h +++ b/native/cocos/scripting/js-bindings/jswrapper/SeApi.h @@ -22,3 +22,4 @@ #include "Object.hpp" #include "State.hpp" #include "HandleObject.hpp" +#include "MappingUtils.hpp" diff --git a/native/cocos/scripting/js-bindings/jswrapper/chakracore/HelperMacros.h b/native/cocos/scripting/js-bindings/jswrapper/chakracore/HelperMacros.h index bf38d9f1f3..19e0706d8d 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/chakracore/HelperMacros.h +++ b/native/cocos/scripting/js-bindings/jswrapper/chakracore/HelperMacros.h @@ -32,13 +32,6 @@ se::State state(nativeThisObject, args); \ ret = funcName(state); \ se::internal::seToJsValue(state.rval(), &_jsRet); \ - for (auto& v : args) \ - { \ - if (v.isObject()) \ - { \ - v.toObject()->unroot(); \ - } \ - } \ return _jsRet; \ } @@ -82,13 +75,6 @@ _found = thisObject->getProperty("_ctor", &_property); \ if (_found) _property.toObject()->call(args, thisObject); \ } \ - for (auto& v : args) \ - { \ - if (v.isObject()) \ - { \ - v.toObject()->unroot(); \ - } \ - } \ return _jsRet; \ } @@ -112,13 +98,6 @@ _found = thisObject->getProperty("_ctor", &_property); \ if (_found) _property.toObject()->call(args, thisObject); \ } \ - for (auto& v : args) \ - { \ - if (v.isObject()) \ - { \ - v.toObject()->unroot(); \ - } \ - } \ return _jsRet; \ } @@ -149,10 +128,6 @@ args.push_back(std::move(data)); \ se::State state(nativeThisObject, args); \ ret = funcName(state); \ - if (args[0].isObject() && args[0].toObject()->isRooted()) \ - { \ - args[0].toObject()->unroot(); \ - } \ return JS_INVALID_REFERENCE; \ } diff --git a/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.cpp b/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.cpp index b6fa52ea20..96a3137fc2 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.cpp @@ -1,17 +1,14 @@ #include "Object.hpp" -#include "Utils.hpp" -#include "Class.hpp" -#include "ScriptEngine.hpp" #ifdef SCRIPT_ENGINE_CHAKRACORE +#include "Utils.hpp" +#include "Class.hpp" +#include "ScriptEngine.hpp" +#include "../MappingUtils.hpp" + namespace se { - // key: native ptr, value: se::Object - std::unordered_map __nativePtrToObjectMap; - // key: native ptr, value: non-ref object created by ctor - std::unordered_map __nonRefNativeObjectCreatedByCtorMap; - Object::Object() : _cls(nullptr) , _obj(JS_INVALID_REFERENCE) @@ -105,8 +102,8 @@ namespace se { Object* Object::getObjectWithPtr(void* ptr) { Object* obj = nullptr; - auto iter = __nativePtrToObjectMap.find(ptr); - if (iter != __nativePtrToObjectMap.end()) + auto iter = NativePtrToObjectMap::find(ptr); + if (iter != NativePtrToObjectMap::end()) { obj = iter->second; obj->addRef(); @@ -156,10 +153,10 @@ namespace se { if (nativeObject != nullptr) { - auto iter = __nativePtrToObjectMap.find(nativeObject); - if (iter != __nativePtrToObjectMap.end()) + auto iter = NativePtrToObjectMap::find(nativeObject); + if (iter != NativePtrToObjectMap::end()) { - __nativePtrToObjectMap.erase(iter); + NativePtrToObjectMap::erase(iter); } } } @@ -182,8 +179,8 @@ namespace se { void Object::cleanup() { ScriptEngine::getInstance()->addAfterCleanupHook([](){ - __nativePtrToObjectMap.clear(); - __nonRefNativeObjectCreatedByCtorMap.clear(); + NativePtrToObjectMap::clear(); + NonRefNativePtrCreatedByCtorMap::clear(); }); } @@ -524,9 +521,9 @@ namespace se { void Object::setPrivateData(void* data) { assert(_privateData == nullptr); - assert(__nativePtrToObjectMap.find(data) == __nativePtrToObjectMap.end()); + assert(NativePtrToObjectMap::find(data) == NativePtrToObjectMap::end()); internal::setPrivate(_obj, data, _finalizeCb); - __nativePtrToObjectMap.emplace(data, this); + NativePtrToObjectMap::emplace(data, this); _privateData = data; } @@ -535,7 +532,7 @@ namespace se { if (_privateData != nullptr) { void* data = getPrivateData(); - __nativePtrToObjectMap.erase(data); + NativePtrToObjectMap::erase(data); internal::clearPrivate(_obj); _privateData = nullptr; } @@ -568,8 +565,12 @@ namespace se { --_rootCount; if (_rootCount == 0) { - unsigned int count = 0; - _CHECK(JsRelease(_obj, &count)); + // Don't unprotect if it's in cleanup, otherwise, it will trigger crash. + if (!ScriptEngine::getInstance()->_isInCleanup) + { + unsigned int count = 0; + _CHECK(JsRelease(_obj, &count)); + } } } } diff --git a/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.hpp b/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.hpp index 828020e9fa..d49da94926 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.hpp @@ -83,12 +83,6 @@ namespace se { friend class ScriptEngine; }; - - // key: native ptr, value: se::Object - extern std::unordered_map __nativePtrToObjectMap; - // key: native ptr, value: non-ref object created by ctor - extern std::unordered_map __nonRefNativeObjectCreatedByCtorMap; - } // namespace se { #endif // SCRIPT_ENGINE_CHAKRACORE diff --git a/native/cocos/scripting/js-bindings/jswrapper/chakracore/ScriptEngine.cpp b/native/cocos/scripting/js-bindings/jswrapper/chakracore/ScriptEngine.cpp index 4b2b6d7e0a..4e24ed3e30 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/chakracore/ScriptEngine.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/chakracore/ScriptEngine.cpp @@ -1,10 +1,11 @@ #include "ScriptEngine.hpp" +#ifdef SCRIPT_ENGINE_CHAKRACORE + #include "Object.hpp" #include "Class.hpp" #include "Utils.hpp" - -#ifdef SCRIPT_ENGINE_CHAKRACORE +#include "../MappingUtils.hpp" namespace se { @@ -256,9 +257,9 @@ namespace se { void ScriptEngine::gc() { - LOGD("GC begin ..., (Native -> JS map) count: %d\n", (int)__nativePtrToObjectMap.size()); + LOGD("GC begin ..., (Native -> JS map) count: %d\n", (int)NativePtrToObjectMap::size()); _CHECK(JsCollectGarbage(_rt)); - LOGD("GC end ..., (Native -> JS map) count: %d\n", (int)__nativePtrToObjectMap.size()); + LOGD("GC end ..., (Native -> JS map) count: %d\n", (int)NativePtrToObjectMap::size()); } void ScriptEngine::clearException() @@ -324,14 +325,14 @@ namespace se { void ScriptEngine::_retainScriptObject(void* owner, void* target) { - auto iterOwner = __nativePtrToObjectMap.find(owner); - if (iterOwner == __nativePtrToObjectMap.end()) + auto iterOwner = NativePtrToObjectMap::find(owner); + if (iterOwner == NativePtrToObjectMap::end()) { return; } - auto iterTarget = __nativePtrToObjectMap.find(target); - if (iterTarget == __nativePtrToObjectMap.end()) + auto iterTarget = NativePtrToObjectMap::find(target); + if (iterTarget == NativePtrToObjectMap::end()) { return; } @@ -342,14 +343,14 @@ namespace se { void ScriptEngine::_releaseScriptObject(void* owner, void* target) { - auto iterOwner = __nativePtrToObjectMap.find(owner); - if (iterOwner == __nativePtrToObjectMap.end()) + auto iterOwner = NativePtrToObjectMap::find(owner); + if (iterOwner == NativePtrToObjectMap::end()) { return; } - auto iterTarget = __nativePtrToObjectMap.find(target); - if (iterTarget == __nativePtrToObjectMap.end()) + auto iterTarget = NativePtrToObjectMap::find(target); + if (iterTarget == NativePtrToObjectMap::end()) { return; } diff --git a/native/cocos/scripting/js-bindings/jswrapper/chakracore/Utils.cpp b/native/cocos/scripting/js-bindings/jswrapper/chakracore/Utils.cpp index 0912dfb327..e3dd1fb31c 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/chakracore/Utils.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/chakracore/Utils.cpp @@ -126,8 +126,7 @@ namespace se { { obj = Object::_createJSObject(nullptr, jsValue); } - obj->root(); // Root it here since it will be unrooted in at the end of SE_BIND_FUNC - data->setObject(obj); + data->setObject(obj, true); obj->release(); } else diff --git a/native/cocos/scripting/js-bindings/jswrapper/jsc/HelperMacros.h b/native/cocos/scripting/js-bindings/jswrapper/jsc/HelperMacros.h index e92d0db9ce..6de5c887c2 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/jsc/HelperMacros.h +++ b/native/cocos/scripting/js-bindings/jswrapper/jsc/HelperMacros.h @@ -33,13 +33,6 @@ se::State state(nativeThisObject, args); \ ret = funcName(state); \ se::internal::seToJsValue(_cx, state.rval(), &_jsRet); \ - for (auto& v : args) \ - { \ - if (v.isObject()) \ - { \ - v.toObject()->unroot(); \ - } \ - } \ } \ return _jsRet; \ } @@ -85,13 +78,6 @@ _found = thisObject->getProperty("_ctor", &_property); \ if (_found) _property.toObject()->call(args, thisObject); \ } \ - for (auto& v : args) \ - { \ - if (v.isObject()) \ - { \ - v.toObject()->unroot(); \ - } \ - } \ return JSValueToObject(_cx, _jsRet, nullptr); \ } @@ -114,13 +100,6 @@ _found = thisObject->getProperty("_ctor", &_property); \ if (_found) _property.toObject()->call(args, thisObject); \ } \ - for (auto& v : args) \ - { \ - if (v.isObject()) \ - { \ - v.toObject()->unroot(); \ - } \ - } \ return _jsRet; \ } @@ -158,10 +137,6 @@ args.push_back(std::move(data)); \ se::State state(nativeThisObject, args); \ ret = funcName(state); \ - if (args[0].isObject() && args[0].toObject()->isRooted()) \ - { \ - args[0].toObject()->unroot(); \ - } \ } \ return _jsRet; \ } diff --git a/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.cpp b/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.cpp index aa671bb619..cfc05b2e9b 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.cpp @@ -5,14 +5,10 @@ #include "Utils.hpp" #include "Class.hpp" #include "ScriptEngine.hpp" +#include "../MappingUtils.hpp" namespace se { - // key: native ptr, value: se::Object - std::unordered_map __nativePtrToObjectMap; - // key: native ptr, value: non-ref object created by ctor - std::unordered_map __nonRefNativeObjectCreatedByCtorMap; - namespace { JSContextRef __cx = nullptr; } @@ -109,8 +105,8 @@ namespace se { Object* Object::getObjectWithPtr(void* ptr) { Object* obj = nullptr; - auto iter = __nativePtrToObjectMap.find(ptr); - if (iter != __nativePtrToObjectMap.end()) + auto iter = NativePtrToObjectMap::find(ptr); + if (iter != NativePtrToObjectMap::end()) { obj = iter->second; obj->addRef(); @@ -158,10 +154,10 @@ namespace se { if (nativeObj != nullptr) { - auto iter = __nativePtrToObjectMap.find(nativeObj); - if (iter != __nativePtrToObjectMap.end()) + auto iter = NativePtrToObjectMap::find(nativeObj); + if (iter != NativePtrToObjectMap::end()) { - __nativePtrToObjectMap.erase(iter); + NativePtrToObjectMap::erase(iter); } } else @@ -489,9 +485,9 @@ namespace se { void Object::setPrivateData(void* data) { assert(_privateData == nullptr); - assert(__nativePtrToObjectMap.find(data) == __nativePtrToObjectMap.end()); + assert(NativePtrToObjectMap::find(data) == NativePtrToObjectMap::end()); internal::setPrivate(_obj, data, _finalizeCb); - __nativePtrToObjectMap.emplace(data, this); + NativePtrToObjectMap::emplace(data, this); _privateData = data; } @@ -500,7 +496,7 @@ namespace se { if (_privateData != nullptr) { void* data = getPrivateData(); - __nativePtrToObjectMap.erase(data); + NativePtrToObjectMap::erase(data); internal::clearPrivate(_obj); _privateData = nullptr; } @@ -514,8 +510,8 @@ namespace se { void Object::cleanup() { ScriptEngine::getInstance()->addAfterCleanupHook([](){ - __nativePtrToObjectMap.clear(); - __nonRefNativeObjectCreatedByCtorMap.clear(); + NativePtrToObjectMap::clear(); + NonRefNativePtrCreatedByCtorMap::clear(); __cx = nullptr; }); } @@ -546,7 +542,9 @@ namespace se { --_rootCount; if (_rootCount == 0) { - JSValueUnprotect(__cx, _obj); + // Don't unprotect if it's in cleanup, otherwise, it will trigger crash. + if (!ScriptEngine::getInstance()->_isInCleanup) + JSValueUnprotect(__cx, _obj); } } } diff --git a/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.hpp b/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.hpp index dc336c6448..ebac21892b 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.hpp @@ -318,11 +318,6 @@ namespace se { friend class AutoHandleScope; }; - // key: native ptr, value: se::Object - extern std::unordered_map __nativePtrToObjectMap; - // key: native ptr, value: non-ref object created by ctor - extern std::unordered_map __nonRefNativeObjectCreatedByCtorMap; - } // namespace se { #endif // SCRIPT_ENGINE_JSC diff --git a/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.cpp b/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.cpp index a60ea5673d..e30196c786 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.cpp @@ -5,6 +5,7 @@ #include "Object.hpp" #include "Class.hpp" #include "Utils.hpp" +#include "../MappingUtils.hpp" extern "C" JS_EXPORT void JSSynchronousGarbageCollectForDebugging(JSContextRef); @@ -29,10 +30,10 @@ namespace se { JSValueRef __forceGC(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { - LOGD("GC begin ..., (Native -> JS map) count: %d\n", (int)__nativePtrToObjectMap.size()); + LOGD("GC begin ..., (Native -> JS map) count: %d\n", (int)NativePtrToObjectMap::size()); // JSGarbageCollect(ctx); JSSynchronousGarbageCollectForDebugging(ctx); - LOGD("GC end ..., (Native -> JS map) count: %d\n", (int)__nativePtrToObjectMap.size()); + LOGD("GC end ..., (Native -> JS map) count: %d\n", (int)NativePtrToObjectMap::size()); return JSValueMakeUndefined(ctx); } @@ -279,10 +280,10 @@ namespace se { void ScriptEngine::gc() { - LOGD("GC begin ..., (Native -> JS map) count: %d\n", (int)__nativePtrToObjectMap.size()); + LOGD("GC begin ..., (Native -> JS map) count: %d\n", (int)NativePtrToObjectMap::size()); // JSGarbageCollect(_cx); JSSynchronousGarbageCollectForDebugging(_cx); - LOGD("GC end ..., (Native -> JS map) count: %d\n", (int)__nativePtrToObjectMap.size()); + LOGD("GC end ..., (Native -> JS map) count: %d\n", (int)NativePtrToObjectMap::size()); } bool ScriptEngine::executeScriptBuffer(const char *script, ssize_t length, Value *data, const char *fileName) @@ -344,14 +345,14 @@ namespace se { void ScriptEngine::_retainScriptObject(void* owner, void* target) { - auto iterOwner = __nativePtrToObjectMap.find(owner); - if (iterOwner == __nativePtrToObjectMap.end()) + auto iterOwner = NativePtrToObjectMap::find(owner); + if (iterOwner == NativePtrToObjectMap::end()) { return; } - auto iterTarget = __nativePtrToObjectMap.find(target); - if (iterTarget == __nativePtrToObjectMap.end()) + auto iterTarget = NativePtrToObjectMap::find(target); + if (iterTarget == NativePtrToObjectMap::end()) { return; } @@ -362,14 +363,14 @@ namespace se { void ScriptEngine::_releaseScriptObject(void* owner, void* target) { - auto iterOwner = __nativePtrToObjectMap.find(owner); - if (iterOwner == __nativePtrToObjectMap.end()) + auto iterOwner = NativePtrToObjectMap::find(owner); + if (iterOwner == NativePtrToObjectMap::end()) { return; } - auto iterTarget = __nativePtrToObjectMap.find(target); - if (iterTarget == __nativePtrToObjectMap.end()) + auto iterTarget = NativePtrToObjectMap::find(target); + if (iterTarget == NativePtrToObjectMap::end()) { return; } diff --git a/native/cocos/scripting/js-bindings/jswrapper/jsc/Utils.cpp b/native/cocos/scripting/js-bindings/jswrapper/jsc/Utils.cpp index 6b5e854163..cac9351a3c 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/jsc/Utils.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/jsc/Utils.cpp @@ -159,8 +159,7 @@ namespace se { { obj = Object::_createJSObject(nullptr, jsobj); } - obj->root(); // Root it here since it will be unrooted in at the end of SE_BIND_FUNC - data->setObject(obj); + data->setObject(obj, true); obj->release(); } } diff --git a/native/cocos/scripting/js-bindings/jswrapper/sm/HelperMacros.h b/native/cocos/scripting/js-bindings/jswrapper/sm/HelperMacros.h index 9c8dc892ee..bcb16efe0a 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/sm/HelperMacros.h +++ b/native/cocos/scripting/js-bindings/jswrapper/sm/HelperMacros.h @@ -26,13 +26,6 @@ se::State state(nativeThisObject, args); \ ret = funcName(state); \ se::internal::setReturnValue(_cx, state.rval(), _argv); \ - for (auto& v : args) \ - { \ - if (v.isObject()) \ - { \ - v.toObject()->unroot(); \ - } \ - } \ return ret; \ } @@ -69,13 +62,6 @@ _found = thisObject->getProperty("_ctor", &_property); \ if (_found) _property.toObject()->call(args, thisObject); \ } \ - for (auto& v : args) \ - { \ - if (v.isObject()) \ - { \ - v.toObject()->unroot(); \ - } \ - } \ return ret; \ } @@ -99,13 +85,6 @@ _found = thisObject->getProperty("_ctor", &_property); \ if (_found) _property.toObject()->call(args, thisObject); \ } \ - for (auto& v : args) \ - { \ - if (v.isObject()) \ - { \ - v.toObject()->unroot(); \ - } \ - } \ return ret; \ } @@ -139,10 +118,6 @@ args.push_back(std::move(data)); \ se::State state(nativeThisObject, args); \ ret = funcName(state); \ - if (args[0].isObject() && args[0].toObject()->isRooted()) \ - { \ - args[0].toObject()->unroot(); \ - } \ return ret; \ } diff --git a/native/cocos/scripting/js-bindings/jswrapper/sm/Object.cpp b/native/cocos/scripting/js-bindings/jswrapper/sm/Object.cpp index 08daca607b..b53ded1b6b 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/sm/Object.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/sm/Object.cpp @@ -1,16 +1,14 @@ #include "Object.hpp" -#include "Utils.hpp" -#include "Class.hpp" -#include "ScriptEngine.hpp" #ifdef SCRIPT_ENGINE_SM +#include "Utils.hpp" +#include "Class.hpp" +#include "ScriptEngine.hpp" +#include "../MappingUtils.hpp" + namespace se { - // key: native ptr, value: se::Object - std::unordered_map __nativePtrToObjectMap; - // key: native ptr, value: non-ref object created by ctor - std::unordered_map __nonRefNativeObjectCreatedByCtorMap; std::unordered_map __objectMap; // Currently, the value `void*` is always nullptr namespace { @@ -94,8 +92,8 @@ namespace se { Object* Object::getObjectWithPtr(void* ptr) { Object* obj = nullptr; - auto iter = __nativePtrToObjectMap.find(ptr); - if (iter != __nativePtrToObjectMap.end()) + auto iter = NativePtrToObjectMap::find(ptr); + if (iter != NativePtrToObjectMap::end()) { obj = iter->second; obj->addRef(); @@ -374,12 +372,12 @@ namespace se { void Object::setPrivateData(void* data) { assert(_privateData == nullptr); - assert(__nativePtrToObjectMap.find(data) == __nativePtrToObjectMap.end()); + assert(NativePtrToObjectMap::find(data) == NativePtrToObjectMap::end()); assert(_cls != nullptr); JS::RootedObject obj(__cx, _getJSObject()); internal::setPrivate(__cx, obj, data, _finalizeCb); - __nativePtrToObjectMap.emplace(data, this); + NativePtrToObjectMap::emplace(data, this); _privateData = data; } @@ -388,7 +386,7 @@ namespace se { if (_privateData != nullptr) { void* data = getPrivateData(); - __nativePtrToObjectMap.erase(data); + NativePtrToObjectMap::erase(data); JS::RootedObject obj(__cx, _getJSObject()); internal::clearPrivate(__cx, obj); _privateData = nullptr; @@ -410,8 +408,8 @@ namespace se { ScriptEngine::getInstance()->addAfterCleanupHook([](){ __objectMap.clear(); - __nativePtrToObjectMap.clear(); - __nonRefNativeObjectCreatedByCtorMap.clear(); + NativePtrToObjectMap::clear(); + NonRefNativePtrCreatedByCtorMap::clear(); __cx = nullptr; }); } diff --git a/native/cocos/scripting/js-bindings/jswrapper/sm/Object.hpp b/native/cocos/scripting/js-bindings/jswrapper/sm/Object.hpp index 0aaedfebe0..b85a7861fd 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/sm/Object.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/sm/Object.hpp @@ -93,11 +93,6 @@ namespace se { friend class ScriptEngine; }; - - // key: native ptr, value: se::Object - extern std::unordered_map __nativePtrToObjectMap; - // key: native ptr, value: non-ref object created by ctor - extern std::unordered_map __nonRefNativeObjectCreatedByCtorMap; extern std::unordered_map __objectMap; // Currently, the value `void*` is always nullptr } // namespace se { diff --git a/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.cpp b/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.cpp index 63b83530cb..83ab9101a7 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.cpp @@ -1,10 +1,11 @@ #include "ScriptEngine.hpp" +#ifdef SCRIPT_ENGINE_SM + #include "Object.hpp" #include "Class.hpp" #include "Utils.hpp" - -#ifdef SCRIPT_ENGINE_SM +#include "../MappingUtils.hpp" namespace se { @@ -98,11 +99,11 @@ namespace se { if (status == JSGC_BEGIN) { ScriptEngine::getInstance()->_setInGC(true); - LOGD("on_garbage_collect: begin, Native -> JS map count: %d, all objects: %d\n", (int)__nativePtrToObjectMap.size(), (int)__objectMap.size()); + LOGD("on_garbage_collect: begin, Native -> JS map count: %d, all objects: %d\n", (int)NativePtrToObjectMap::size(), (int)__objectMap.size()); } else if (status == JSGC_END) { - LOGD("on_garbage_collect: end, Native -> JS map count: %d, all objects: %d\n", (int)__nativePtrToObjectMap.size(), (int)__objectMap.size()); + LOGD("on_garbage_collect: end, Native -> JS map count: %d, all objects: %d\n", (int)NativePtrToObjectMap::size(), (int)__objectMap.size()); ScriptEngine::getInstance()->_setInGC(false); } } @@ -231,8 +232,8 @@ namespace se { bool isInCleanup = getInstance()->_isInCleanup; bool isIterUpdated = false; Object* obj = nullptr; - auto iter = __nativePtrToObjectMap.begin(); - while (iter != __nativePtrToObjectMap.end()) + auto iter = NativePtrToObjectMap::begin(); + while (iter != NativePtrToObjectMap::end()) { obj = iter->second; isIterUpdated = false; @@ -241,7 +242,7 @@ namespace se { if (obj->updateAfterGC(data)) { obj->release(); - iter = __nativePtrToObjectMap.erase(iter); + iter = NativePtrToObjectMap::erase(iter); isIterUpdated = true; } } @@ -249,7 +250,7 @@ namespace se { { obj->unprotect(); obj->release(); - iter = __nativePtrToObjectMap.erase(iter); + iter = NativePtrToObjectMap::erase(iter); isIterUpdated = true; } @@ -483,14 +484,14 @@ namespace se { void ScriptEngine::_retainScriptObject(void* owner, void* target) { - auto iterOwner = __nativePtrToObjectMap.find(owner); - if (iterOwner == __nativePtrToObjectMap.end()) + auto iterOwner = NativePtrToObjectMap::find(owner); + if (iterOwner == NativePtrToObjectMap::end()) { return; } - auto iterTarget = __nativePtrToObjectMap.find(target); - if (iterTarget == __nativePtrToObjectMap.end()) + auto iterTarget = NativePtrToObjectMap::find(target); + if (iterTarget == NativePtrToObjectMap::end()) { return; } @@ -501,14 +502,14 @@ namespace se { void ScriptEngine::_releaseScriptObject(void* owner, void* target) { - auto iterOwner = __nativePtrToObjectMap.find(owner); - if (iterOwner == __nativePtrToObjectMap.end()) + auto iterOwner = NativePtrToObjectMap::find(owner); + if (iterOwner == NativePtrToObjectMap::end()) { return; } - auto iterTarget = __nativePtrToObjectMap.find(target); - if (iterTarget == __nativePtrToObjectMap.end()) + auto iterTarget = NativePtrToObjectMap::find(target); + if (iterTarget == NativePtrToObjectMap::end()) { return; } diff --git a/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.hpp b/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.hpp index 3ca1bf43c4..02a6275a7a 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.hpp @@ -57,6 +57,8 @@ namespace se { void clearException(); + const std::chrono::steady_clock::time_point& getStartTime() const { return _startTime; } + void _retainScriptObject(void* owner, void* target); void _releaseScriptObject(void* owner, void* target); diff --git a/native/cocos/scripting/js-bindings/jswrapper/sm/Utils.cpp b/native/cocos/scripting/js-bindings/jswrapper/sm/Utils.cpp index 9191431dcc..f1ddf0f890 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/sm/Utils.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/sm/Utils.cpp @@ -136,8 +136,7 @@ namespace se { { object = Object::_createJSObject(nullptr, jsval.toObjectOrNull()); } - object->root(); // Root it here since it will be unrooted in at the end of SE_BIND_FUNC - v->setObject(object); + v->setObject(object, true); object->release(); } else if (jsval.isNull()) diff --git a/native/cocos/scripting/js-bindings/jswrapper/v8/HelperMacros.h b/native/cocos/scripting/js-bindings/jswrapper/v8/HelperMacros.h index 4e11a31d58..4cd55ff001 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/v8/HelperMacros.h +++ b/native/cocos/scripting/js-bindings/jswrapper/v8/HelperMacros.h @@ -32,13 +32,6 @@ se::State state(nativeThisObject, args); \ ret = funcName(state); \ se::internal::setReturnValue(state.rval(), _v8args); \ - for (auto& v : args) \ - { \ - if (v.isObject()) \ - { \ - v.toObject()->unroot(); \ - } \ - } \ } #define SE_BIND_FINALIZE_FUNC(funcName) \ @@ -73,13 +66,6 @@ bool _found = false; \ _found = thisObject->getProperty("_ctor", &_property); \ if (_found) _property.toObject()->call(args, thisObject); \ - for (auto& v : args) \ - { \ - if (v.isObject()) \ - { \ - v.toObject()->unroot(); \ - } \ - } \ } #define SE_BIND_SUB_CLS_CTOR SE_BIND_CTOR @@ -111,10 +97,6 @@ args.push_back(std::move(data)); \ se::State state(nativeThisObject, args); \ ret = funcName(state); \ - if (args[0].isObject() && args[0].toObject()->isRooted()) \ - { \ - args[0].toObject()->unroot(); \ - } \ } diff --git a/native/cocos/scripting/js-bindings/jswrapper/v8/Object.cpp b/native/cocos/scripting/js-bindings/jswrapper/v8/Object.cpp index 420c1c87a3..b5771551a9 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/v8/Object.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/v8/Object.cpp @@ -4,13 +4,10 @@ #include "Utils.hpp" #include "Class.hpp" #include "ScriptEngine.hpp" +#include "../MappingUtils.hpp" namespace se { - // key: native ptr, value: se::Object - std::unordered_map __nativePtrToObjectMap; - // key: native ptr, value: non-ref object created by ctor - std::unordered_map __nonRefNativeObjectCreatedByCtorMap; std::unordered_map __objectMap; // Currently, the value `void*` is always nullptr namespace { @@ -46,8 +43,8 @@ namespace se { if (nativeObj == nullptr) return; - auto iter = __nativePtrToObjectMap.find(nativeObj); - if (iter != __nativePtrToObjectMap.end()) + auto iter = NativePtrToObjectMap::find(nativeObj); + if (iter != NativePtrToObjectMap::end()) { Object* obj = iter->second; if (obj->_finalizeCb != nullptr) @@ -61,7 +58,7 @@ namespace se { obj->_getClass()->_finalizeFunc(nativeObj); } obj->release(); - __nativePtrToObjectMap.erase(iter); + NativePtrToObjectMap::erase(iter); } else { @@ -82,7 +79,8 @@ namespace se { Object* obj = nullptr; Class* cls = nullptr; - for (const auto& e : __nativePtrToObjectMap) + const auto& nativePtrToObjectMap = NativePtrToObjectMap::instance(); + for (const auto& e : nativePtrToObjectMap) { nativeObj = e.first; obj = e.second; @@ -110,8 +108,8 @@ namespace se { obj->release(); } - __nativePtrToObjectMap.clear(); - __nonRefNativeObjectCreatedByCtorMap.clear(); + NativePtrToObjectMap::clear(); + NonRefNativePtrCreatedByCtorMap::clear(); std::vector toReleaseObjects; for (const auto& e : __objectMap) @@ -146,8 +144,8 @@ namespace se { Object* Object::getObjectWithPtr(void* ptr) { Object* obj = nullptr; - auto iter = __nativePtrToObjectMap.find(ptr); - if (iter != __nativePtrToObjectMap.end()) + auto iter = NativePtrToObjectMap::find(ptr); + if (iter != NativePtrToObjectMap::end()) { obj = iter->second; obj->addRef(); @@ -340,9 +338,9 @@ namespace se { void Object::setPrivateData(void* data) { assert(_privateData == nullptr); - assert(__nativePtrToObjectMap.find(data) == __nativePtrToObjectMap.end()); + assert(NativePtrToObjectMap::find(data) == NativePtrToObjectMap::end()); internal::setPrivate(__isolate, _obj, data, &_internalData); - __nativePtrToObjectMap.emplace(data, this); + NativePtrToObjectMap::emplace(data, this); _privateData = data; } @@ -360,7 +358,7 @@ namespace se { if (_privateData != nullptr) { void* data = getPrivateData(); - __nativePtrToObjectMap.erase(data); + NativePtrToObjectMap::erase(data); internal::clearPrivate(__isolate, _obj); _privateData = nullptr; } diff --git a/native/cocos/scripting/js-bindings/jswrapper/v8/Object.hpp b/native/cocos/scripting/js-bindings/jswrapper/v8/Object.hpp index 07096d97d6..26e611f58a 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/v8/Object.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/v8/Object.hpp @@ -95,10 +95,6 @@ namespace se { friend class ScriptEngine; }; - // key: native ptr, value: se::Object - extern std::unordered_map __nativePtrToObjectMap; - // key: native ptr, value: non-ref object created by ctor - extern std::unordered_map __nonRefNativeObjectCreatedByCtorMap; extern std::unordered_map __objectMap; // Currently, the value `void*` is always nullptr } // namespace se { diff --git a/native/cocos/scripting/js-bindings/jswrapper/v8/ScriptEngine.cpp b/native/cocos/scripting/js-bindings/jswrapper/v8/ScriptEngine.cpp index 924336e1dc..e170c23a3c 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/v8/ScriptEngine.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/v8/ScriptEngine.cpp @@ -5,6 +5,7 @@ #include "Object.hpp" #include "Class.hpp" #include "Utils.hpp" +#include "../MappingUtils.hpp" #if SE_ENABLE_INSPECTOR #include "inspector_agent.h" @@ -286,7 +287,7 @@ namespace se { void ScriptEngine::gc() { - LOGD("GC begin ..., (js->native map) size: %d, all objects: %d\n", (int)__nativePtrToObjectMap.size(), (int)__objectMap.size()); + LOGD("GC begin ..., (js->native map) size: %d, all objects: %d\n", (int)NativePtrToObjectMap::size(), (int)__objectMap.size()); const double kLongIdlePauseInSeconds = 1.0; _isolate->ContextDisposedNotification(); _isolate->IdleNotificationDeadline(_platform->MonotonicallyIncreasingTime() + kLongIdlePauseInSeconds); @@ -294,7 +295,7 @@ namespace se { // garbage and will therefore also invoke all weak callbacks of actually // unreachable persistent handles. _isolate->LowMemoryNotification(); - LOGD("GC end ..., (js->native map) size: %d, all objects: %d\n", (int)__nativePtrToObjectMap.size(), (int)__objectMap.size()); + LOGD("GC end ..., (js->native map) size: %d, all objects: %d\n", (int)NativePtrToObjectMap::size(), (int)__objectMap.size()); } bool ScriptEngine::isInGC() @@ -352,20 +353,20 @@ namespace se { } } - assert(success); +// assert(success); return success; } void ScriptEngine::_retainScriptObject(void* owner, void* target) { - auto iterOwner = __nativePtrToObjectMap.find(owner); - if (iterOwner == __nativePtrToObjectMap.end()) + auto iterOwner = NativePtrToObjectMap::find(owner); + if (iterOwner == NativePtrToObjectMap::end()) { return; } - auto iterTarget = __nativePtrToObjectMap.find(target); - if (iterTarget == __nativePtrToObjectMap.end()) + auto iterTarget = NativePtrToObjectMap::find(target); + if (iterTarget == NativePtrToObjectMap::end()) { return; } @@ -377,14 +378,14 @@ namespace se { void ScriptEngine::_releaseScriptObject(void* owner, void* target) { - auto iterOwner = __nativePtrToObjectMap.find(owner); - if (iterOwner == __nativePtrToObjectMap.end()) + auto iterOwner = NativePtrToObjectMap::find(owner); + if (iterOwner == NativePtrToObjectMap::end()) { return; } - auto iterTarget = __nativePtrToObjectMap.find(target); - if (iterTarget == __nativePtrToObjectMap.end()) + auto iterTarget = NativePtrToObjectMap::find(target); + if (iterTarget == NativePtrToObjectMap::end()) { return; } diff --git a/native/cocos/scripting/js-bindings/jswrapper/v8/Utils.cpp b/native/cocos/scripting/js-bindings/jswrapper/v8/Utils.cpp index 5ba2fef38d..5056df7099 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/v8/Utils.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/v8/Utils.cpp @@ -108,8 +108,7 @@ namespace se { { obj = Object::_createJSObject(nullptr, jsObj.ToLocalChecked()); } - obj->root(); // Root it here since it will be unrooted in at the end of SE_BIND_FUNC - v->setObject(obj); + v->setObject(obj, true); obj->release(); } else diff --git a/native/cocos/scripting/js-bindings/manual/jsb_box2d_manual.cpp b/native/cocos/scripting/js-bindings/manual/jsb_box2d_manual.cpp index bfc637a6c2..fce5ebf14e 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_box2d_manual.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_box2d_manual.cpp @@ -5,6 +5,8 @@ #include "cocos/scripting/js-bindings/manual/jsb_helper.hpp" #include "cocos/scripting/js-bindings/auto/jsb_box2d_auto.hpp" +#include "cocos/editor-support/creator/physics/CCPhysicsContactListener.h" + bool seval_to_b2BodyDef(const se::Value& v, b2BodyDef* ret) { static b2BodyDef ZERO; @@ -540,7 +542,7 @@ bool array_of_b2Fixture_to_seval(const std::vector& fixtures, se::Va se::Value tmp; int i = 0; - bool ok = false; + bool ok = true; for (const auto& e : fixtures) { ok = native_ptr_to_rooted_seval(e, &tmp); @@ -553,11 +555,11 @@ bool array_of_b2Fixture_to_seval(const std::vector& fixtures, se::Va } if (ok) - ret->setObject(obj); + ret->setObject(obj, true); else ret->setUndefined(); - return true; + return ok; } bool array_of_b2Vec2_to_seval(const std::vector& vs, se::Value* ret) @@ -566,7 +568,7 @@ bool array_of_b2Vec2_to_seval(const std::vector& vs, se::Value* ret) se::Value tmp; int i = 0; - bool ok = false; + bool ok = true; for (const auto& e : vs) { ok = b2Vec2_to_seval(e, &tmp); @@ -579,11 +581,11 @@ bool array_of_b2Vec2_to_seval(const std::vector& vs, se::Value* ret) } if (ok) - ret->setObject(obj); + ret->setObject(obj, true); else ret->setUndefined(); - return true; + return ok; } static bool js_box2dclasses_b2Shape_SetRadius(se::State& s) @@ -841,8 +843,8 @@ static bool js_box2dclasses_b2Body_GetUserData(se::State& s) b2Body* cobj = (b2Body *)s.nativeThisObject(); void* data = cobj->GetUserData(); - auto iter = se::__nativePtrToObjectMap.find(data); - if (iter != se::__nativePtrToObjectMap.end()) + auto iter = se::NativePtrToObjectMap::find(data); + if (iter != se::NativePtrToObjectMap::end()) { s.rval().setObject(iter->second); } @@ -1077,17 +1079,17 @@ bool register_all_box2d_manual(se::Object* obj) se::ScriptEngine::getInstance()->clearException(); - b2SetObjectDestroyNotifier([](void* obj, const char* type){ + b2SetObjectDestroyNotifier([](void* obj, b2ObjectType type, const char* typeName){ - std::string typeName = type; + std::string typeNameStr = typeName; auto cleanup = [=](){ se::AutoHandleScope hs; se::ScriptEngine::getInstance()->clearException(); - auto iter = se::__nativePtrToObjectMap.find(obj); - if (iter != se::__nativePtrToObjectMap.end()) + auto iter = se::NativePtrToObjectMap::find(obj); + if (iter != se::NativePtrToObjectMap::end()) { -// CCLOG("%s, %p was recycled!", typeName.c_str(), obj); +// CCLOG("%s, %p was recycled!", typeNameStr.c_str(), obj); se::Object* seObj = iter->second; seObj->clearPrivateData(); seObj->unroot(); @@ -1095,7 +1097,7 @@ bool register_all_box2d_manual(se::Object* obj) } else { -// CCLOG("Didn't find %s, %p in map", typeName.c_str(), obj); +// CCLOG("Didn't find %s, %p in map", typeNameStr.c_str(), obj); // assert(false); } }; @@ -1108,6 +1110,15 @@ bool register_all_box2d_manual(se::Object* obj) { CleanupTask::pushTaskToAutoReleasePool(cleanup); } + + if (type == b2ObjectType::FIXTURE) + { + const auto& instances = creator::PhysicsContactListener::getAllInstances(); + for (auto listener : instances) + { + listener->unregisterContactFixture(reinterpret_cast(obj)); + } + } }); return true; diff --git a/native/cocos/scripting/js-bindings/manual/jsb_cocos2dx_manual.cpp b/native/cocos/scripting/js-bindings/manual/jsb_cocos2dx_manual.cpp index 024c3aa326..ffc6d5c2e0 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_cocos2dx_manual.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_cocos2dx_manual.cpp @@ -338,7 +338,7 @@ static bool invokeJSMouseCallback(EventListenerMouse* listener, const char* func SE_PRECONDITION2(ok, false, "invokeJSMouseCallback convert arg1 failed!"); argArr.push_back(std::move(arg1Val)); - assert(se::__nativePtrToObjectMap.find(arg1) != se::__nativePtrToObjectMap.end()); + assert(se::NativePtrToObjectMap::find(arg1) != se::NativePtrToObjectMap::end()); ok = funcVal.toObject()->call(argArr, listenerObj, retVal); SE_PRECONDITION2(ok, false, "invokeJSMouseCallback call function failed!"); @@ -447,8 +447,8 @@ static bool invokeJSTouchOneByOneCallback(EventListenerTouchOneByOne* listener, SE_PRECONDITION_ERROR_BREAK(ok, "invokeJSTouchOneByOneCallback convert arg2 failed!"); argArr.push_back(std::move(arg2Val)); - assert(se::__nativePtrToObjectMap.find(touch) != se::__nativePtrToObjectMap.end()); - assert(se::__nativePtrToObjectMap.find(event) != se::__nativePtrToObjectMap.end()); + assert(se::NativePtrToObjectMap::find(touch) != se::NativePtrToObjectMap::end()); + assert(se::NativePtrToObjectMap::find(event) != se::NativePtrToObjectMap::end()); ok = funcVal.toObject()->call(argArr, listenerObj, retVal); SE_PRECONDITION_ERROR_BREAK(ok, "invokeJSTouchOneByOneCallback call function failed!"); @@ -779,7 +779,7 @@ static bool js_EventListenerCustom_create(se::State& s) ret->init(eventName, [ret, funcVal](EventCustom* event){ - if (se::__nativePtrToObjectMap.find(ret) == se::__nativePtrToObjectMap.end()) + if (se::NativePtrToObjectMap::find(ret) == se::NativePtrToObjectMap::end()) { CCLOGERROR("can't find se::Object with native ptr: %p", ret); return; diff --git a/native/cocos/scripting/js-bindings/manual/jsb_conversions.hpp b/native/cocos/scripting/js-bindings/manual/jsb_conversions.hpp index d0bc3b4829..f491036442 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_conversions.hpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_conversions.hpp @@ -209,8 +209,8 @@ bool recreate_seval_by_native_ptr(typename std::enable_ifsecond; seObj->clearPrivateData(); @@ -235,8 +235,8 @@ bool native_ptr_to_seval(typename std::enable_if(v); @@ -273,8 +273,8 @@ bool native_ptr_to_rooted_seval(typename std::enable_if(v); assert(cls != nullptr); @@ -314,10 +314,10 @@ bool native_ptr_to_seval(typename std::enable_ifsetObject(obj, true); @@ -352,8 +352,8 @@ bool native_ptr_to_rooted_seval(typename std::enable_if } se::Object* obj = nullptr; - auto iter = se::__nativePtrToObjectMap.find(v); - if (iter == se::__nativePtrToObjectMap.end()) + auto iter = se::NativePtrToObjectMap::find(v); + if (iter == se::NativePtrToObjectMap::end()) { // If we couldn't find native object in map, then the native object is created from native code. e.g. TMXLayer::getTileAt - // CCLOGWARN("WARNING: Ref type: (%s) isn't catched!", typeid(*v).name()); +// CCLOGWARN("WARNING: Ref type: (%s) isn't catched!", typeid(*v).name()); se::Class* cls = JSBClassType::findClass(v); assert(cls != nullptr); obj = se::Object::createObjectWithClass(cls); @@ -432,10 +432,10 @@ bool native_ptr_to_seval(typename std::enable_if } se::Object* obj = nullptr; - auto iter = se::__nativePtrToObjectMap.find(v); - if (iter == se::__nativePtrToObjectMap.end()) + auto iter = se::NativePtrToObjectMap::find(v); + if (iter == se::NativePtrToObjectMap::end()) { // If we couldn't find native object in map, then the native object is created from native code. e.g. TMXLayer::getTileAt - // CCLOGWARN("WARNING: Ref type: (%s) isn't catched!", typeid(*v).name()); +// CCLOGWARN("WARNING: Ref type: (%s) isn't catched!", typeid(*v).name()); assert(cls != nullptr); obj = se::Object::createObjectWithClass(cls); ret->setObject(obj, true); diff --git a/native/cocos/scripting/js-bindings/manual/jsb_dragonbones_manual.cpp b/native/cocos/scripting/js-bindings/manual/jsb_dragonbones_manual.cpp index 83230b2c81..7c0d261970 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_dragonbones_manual.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_dragonbones_manual.cpp @@ -423,8 +423,8 @@ bool register_all_dragonbones_manual(se::Object* obj) se::AutoHandleScope hs; se::ScriptEngine::getInstance()->clearException(); - auto iter = se::__nativePtrToObjectMap.find(obj); - if (iter != se::__nativePtrToObjectMap.end()) + auto iter = se::NativePtrToObjectMap::find(obj); + if (iter != se::NativePtrToObjectMap::end()) { // CCLOG("%s: %p was recycled!", typeName.c_str(), obj); se::Object* seObj = iter->second; diff --git a/native/cocos/scripting/js-bindings/manual/jsb_global.cpp b/native/cocos/scripting/js-bindings/manual/jsb_global.cpp index ac9ef1f27a..f48fa795a9 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_global.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_global.cpp @@ -441,7 +441,7 @@ SE_BIND_FUNC(jsc_garbageCollect) static bool jsc_dumpNativePtrToSeObjectMap(se::State& s) { - cocos2d::log(">>> total: %d, Dump (native -> jsobj) map begin", (int)se::__nativePtrToObjectMap.size()); + cocos2d::log(">>> total: %d, Dump (native -> jsobj) map begin", (int)se::NativePtrToObjectMap::size()); struct NamePtrStruct { @@ -451,7 +451,7 @@ static bool jsc_dumpNativePtrToSeObjectMap(se::State& s) std::vector namePtrArray; - for (const auto& e : se::__nativePtrToObjectMap) + for (const auto& e : se::NativePtrToObjectMap::instance()) { se::Object* jsobj = e.second; assert(jsobj->_getClass() != nullptr); @@ -478,7 +478,7 @@ static bool jsc_dumpNativePtrToSeObjectMap(se::State& s) { cocos2d::log("%s: %p", e.name, e.ptr); } - cocos2d::log(">>> total: %d, Dump (native -> jsobj) map end", (int)se::__nativePtrToObjectMap.size()); + cocos2d::log(">>> total: %d, nonRefMap: %d, Dump (native -> jsobj) map end", (int)se::NativePtrToObjectMap::size(), (int)se::NonRefNativePtrCreatedByCtorMap::size()); return true; } SE_BIND_FUNC(jsc_dumpNativePtrToSeObjectMap) @@ -598,7 +598,8 @@ static bool JSB_isObjectValid(se::State& s) { void* nativePtr = nullptr; seval_to_native_ptr(args[0], &nativePtr); - return nativePtr != nullptr; + s.rval().setBoolean(nativePtr != nullptr); + return true; } SE_REPORT_ERROR("Invalid number of arguments: %d. Expecting: 1", argc); @@ -628,8 +629,9 @@ static bool getOrCreatePlainObject_r(const char* name, se::Object* parent, se::O static bool js_performance_now(se::State& s) { - assert(false); - //FIXME: +//FIXME: auto now = std::chrono::steady_clock::now(); +// auto micro = std::chrono::duration_cast(now - se::ScriptEngine::getInstance()->getStartTime()).count(); +// s.rval().setNumber((double)micro * 0.001); return true; } SE_BIND_FUNC(js_performance_now) diff --git a/native/cocos/scripting/js-bindings/manual/jsb_node.cpp b/native/cocos/scripting/js-bindings/manual/jsb_node.cpp index 7241c8b7a9..fecfb046f2 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_node.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_node.cpp @@ -1258,8 +1258,8 @@ static void cleanupAllSchedulesForTarget(Node* node, se::Object* jsThis) static bool onReceiveNodeEvent(void* node, se::ScriptEngine::NodeEventType type) { - auto iter = se::__nativePtrToObjectMap.find(node); - if (iter == se::__nativePtrToObjectMap.end()) + auto iter = se::NativePtrToObjectMap::find(node); + if (iter == se::NativePtrToObjectMap::end()) return false; se::ScriptEngine::getInstance()->clearException(); diff --git a/native/cocos/scripting/js-bindings/manual/jsb_socketio.cpp b/native/cocos/scripting/js-bindings/manual/jsb_socketio.cpp index fa657e38cb..b9daeb2885 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_socketio.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_socketio.cpp @@ -60,8 +60,8 @@ public: if (cocos2d::Director::getInstance() == nullptr || cocos2d::ScriptEngineManager::getInstance() == nullptr) return; - auto iter = se::__nativePtrToObjectMap.find(client); //FIXME: client probably be a new value with the same address as the old one, it may cause undefined result. - if (iter == se::__nativePtrToObjectMap.end()) + auto iter = se::NativePtrToObjectMap::find(client); //FIXME: client probably be a new value with the same address as the old one, it may cause undefined result. + if (iter == se::NativePtrToObjectMap::end()) return; se::Value dataVal; diff --git a/native/cocos/scripting/js-bindings/manual/jsb_websocket.cpp b/native/cocos/scripting/js-bindings/manual/jsb_websocket.cpp index 40c27de750..47025008a4 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_websocket.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_websocket.cpp @@ -62,8 +62,8 @@ public: if (cocos2d::Director::getInstance() == nullptr || cocos2d::ScriptEngineManager::getInstance() == nullptr) return; - auto iter = se::__nativePtrToObjectMap.find(ws); - if (iter == se::__nativePtrToObjectMap.end()) + auto iter = se::NativePtrToObjectMap::find(ws); + if (iter == se::NativePtrToObjectMap::end()) return; se::Object* wsObj = iter->second; @@ -93,8 +93,8 @@ public: if (cocos2d::Director::getInstance() == nullptr || cocos2d::ScriptEngineManager::getInstance() == nullptr) return; - auto iter = se::__nativePtrToObjectMap.find(ws); - if (iter == se::__nativePtrToObjectMap.end()) + auto iter = se::NativePtrToObjectMap::find(ws); + if (iter == se::NativePtrToObjectMap::end()) return; se::Object* wsObj = iter->second; @@ -152,10 +152,10 @@ public: if (cocos2d::Director::getInstance() == nullptr || cocos2d::ScriptEngineManager::getInstance() == nullptr) return; - auto iter = se::__nativePtrToObjectMap.find(ws); + auto iter = se::NativePtrToObjectMap::find(ws); do { - if (iter == se::__nativePtrToObjectMap.end()) + if (iter == se::NativePtrToObjectMap::end()) { CCLOGINFO("WebSocket js instance was destroyted, don't need to invoke onclose callback!"); break; @@ -192,8 +192,8 @@ public: if (cocos2d::Director::getInstance() == nullptr || cocos2d::ScriptEngineManager::getInstance() == nullptr) return; - auto iter = se::__nativePtrToObjectMap.find(ws); - if (iter == se::__nativePtrToObjectMap.end()) + auto iter = se::NativePtrToObjectMap::find(ws); + if (iter == se::NativePtrToObjectMap::end()) return; se::Object* wsObj = iter->second; diff --git a/native/cocos/scripting/js-bindings/proj.android/Android.mk b/native/cocos/scripting/js-bindings/proj.android/Android.mk index b2faa6e427..f39fe9d0cc 100644 --- a/native/cocos/scripting/js-bindings/proj.android/Android.mk +++ b/native/cocos/scripting/js-bindings/proj.android/Android.mk @@ -12,6 +12,7 @@ LOCAL_SRC_FILES := \ ../jswrapper/State.cpp \ ../jswrapper/Value.cpp \ ../jswrapper/HandleObject.cpp \ + ../jswrapper/MappingUtils.cpp \ ../jswrapper/sm/Class.cpp \ ../jswrapper/sm/ScriptEngine.cpp \ ../jswrapper/sm/Object.cpp \ @@ -42,6 +43,7 @@ LOCAL_SRC_FILES := \ ../auto/jsb_cocos2dx_spine_auto.cpp \ ../auto/jsb_cocos2dx_ui_auto.cpp \ ../auto/jsb_creator_auto.cpp \ + ../manual/BaseJSAction.cpp \ ../manual/JavaScriptJavaBridge.cpp \ ../manual/ScriptingCore.cpp \ ../manual/jsb_helper.cpp \ From b2ff7855014c4bcecf7325c04863c25644405492 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 17 Aug 2017 18:29:31 +0800 Subject: [PATCH 02/11] updates bindings-generator. --- native/tools/bindings-generator | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/native/tools/bindings-generator b/native/tools/bindings-generator index 74c5788646..9fa88819c7 160000 --- a/native/tools/bindings-generator +++ b/native/tools/bindings-generator @@ -1 +1 @@ -Subproject commit 74c578864606f29aa002ba20935ae7dd7f856342 +Subproject commit 9fa88819c776ccf866905533183dfd9dc181db75 From 2fab54c6126c147b16ca12bd95ab977ec36c8f47 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 18 Aug 2017 14:26:24 +0800 Subject: [PATCH 03/11] fix ios crash after closing app in task manager. --- .../js-bindings/jswrapper/MappingUtils.cpp | 75 ++++++++++++++----- .../js-bindings/jswrapper/MappingUtils.hpp | 10 ++- .../jswrapper/chakracore/Object.cpp | 5 ++ .../jswrapper/chakracore/ScriptEngine.cpp | 36 +++++++-- .../jswrapper/chakracore/ScriptEngine.hpp | 20 +++++ .../js-bindings/jswrapper/config.hpp | 4 +- .../js-bindings/jswrapper/jsc/Object.cpp | 5 ++ .../jswrapper/jsc/ScriptEngine.cpp | 36 +++++++-- .../jswrapper/jsc/ScriptEngine.hpp | 23 ++++++ .../js-bindings/jswrapper/sm/Object.cpp | 5 ++ .../js-bindings/jswrapper/sm/ScriptEngine.cpp | 37 +++++++-- .../js-bindings/jswrapper/sm/ScriptEngine.hpp | 18 +++++ .../js-bindings/jswrapper/v8/ScriptEngine.cpp | 38 ++++++++-- .../js-bindings/jswrapper/v8/ScriptEngine.hpp | 21 ++++++ .../js-bindings/manual/jsb_box2d_manual.cpp | 6 +- .../js-bindings/manual/jsb_classtype.cpp | 21 +++++- .../js-bindings/manual/jsb_classtype.hpp | 18 +++-- .../js-bindings/manual/jsb_global.cpp | 6 +- .../manual/jsb_module_register.cpp | 9 ++- 19 files changed, 334 insertions(+), 59 deletions(-) diff --git a/native/cocos/scripting/js-bindings/jswrapper/MappingUtils.cpp b/native/cocos/scripting/js-bindings/jswrapper/MappingUtils.cpp index 99a9533fc6..6894ff7b6e 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/MappingUtils.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/MappingUtils.cpp @@ -11,99 +11,134 @@ namespace se { // NativePtrToObjectMap -NativePtrToObjectMap::Map NativePtrToObjectMap::__nativePtrToObjectMap; +NativePtrToObjectMap::Map* NativePtrToObjectMap::__nativePtrToObjectMap = nullptr; + +bool NativePtrToObjectMap::init() +{ + if (__nativePtrToObjectMap == nullptr) + __nativePtrToObjectMap = new (std::nothrow) NativePtrToObjectMap::Map(); + + return __nativePtrToObjectMap != nullptr; +} + +void NativePtrToObjectMap::destroy() +{ + if (__nativePtrToObjectMap != nullptr) + { + delete __nativePtrToObjectMap; + __nativePtrToObjectMap = nullptr; + } +} void NativePtrToObjectMap::emplace(void* nativeObj, Object* seObj) { - __nativePtrToObjectMap.emplace(nativeObj, seObj); + __nativePtrToObjectMap->emplace(nativeObj, seObj); } NativePtrToObjectMap::Map::iterator NativePtrToObjectMap::find(void* nativeObj) { - return __nativePtrToObjectMap.find(nativeObj); + return __nativePtrToObjectMap->find(nativeObj); } NativePtrToObjectMap::Map::iterator NativePtrToObjectMap::erase(Map::iterator iter) { - return __nativePtrToObjectMap.erase(iter); + return __nativePtrToObjectMap->erase(iter); } void NativePtrToObjectMap::erase(void* nativeObj) { - __nativePtrToObjectMap.erase(nativeObj); + __nativePtrToObjectMap->erase(nativeObj); } void NativePtrToObjectMap::clear() { - __nativePtrToObjectMap.clear(); + __nativePtrToObjectMap->clear(); } size_t NativePtrToObjectMap::size() { - return __nativePtrToObjectMap.size(); + return __nativePtrToObjectMap->size(); } const NativePtrToObjectMap::Map& NativePtrToObjectMap::instance() { - return __nativePtrToObjectMap; + return *__nativePtrToObjectMap; } NativePtrToObjectMap::Map::iterator NativePtrToObjectMap::begin() { - return __nativePtrToObjectMap.begin(); + return __nativePtrToObjectMap->begin(); } NativePtrToObjectMap::Map::iterator NativePtrToObjectMap::end() { - return __nativePtrToObjectMap.end(); + return __nativePtrToObjectMap->end(); } // NonRefNativePtrCreatedByCtorMap -NonRefNativePtrCreatedByCtorMap::Map NonRefNativePtrCreatedByCtorMap::__nonRefNativeObjectCreatedByCtorMap; + +NonRefNativePtrCreatedByCtorMap::Map* NonRefNativePtrCreatedByCtorMap::__nonRefNativeObjectCreatedByCtorMap = nullptr; + +bool NonRefNativePtrCreatedByCtorMap::init() +{ + if (__nonRefNativeObjectCreatedByCtorMap == nullptr) + __nonRefNativeObjectCreatedByCtorMap = new (std::nothrow) NonRefNativePtrCreatedByCtorMap::Map(); + + return __nonRefNativeObjectCreatedByCtorMap != nullptr; +} + +void NonRefNativePtrCreatedByCtorMap::destroy() +{ + if (__nonRefNativeObjectCreatedByCtorMap != nullptr) + { + delete __nonRefNativeObjectCreatedByCtorMap; + __nonRefNativeObjectCreatedByCtorMap = nullptr; + } +} void NonRefNativePtrCreatedByCtorMap::emplace(void* nativeObj) { - __nonRefNativeObjectCreatedByCtorMap.emplace(nativeObj, true); + __nonRefNativeObjectCreatedByCtorMap->emplace(nativeObj, true); } NonRefNativePtrCreatedByCtorMap::Map::iterator NonRefNativePtrCreatedByCtorMap::find(void* nativeObj) { - return __nonRefNativeObjectCreatedByCtorMap.find(nativeObj); + return __nonRefNativeObjectCreatedByCtorMap->find(nativeObj); } NonRefNativePtrCreatedByCtorMap::Map::iterator NonRefNativePtrCreatedByCtorMap::erase(Map::iterator iter) { - return __nonRefNativeObjectCreatedByCtorMap.erase(iter); + return __nonRefNativeObjectCreatedByCtorMap->erase(iter); } void NonRefNativePtrCreatedByCtorMap::erase(void* nativeObj) { - __nonRefNativeObjectCreatedByCtorMap.erase(nativeObj); + __nonRefNativeObjectCreatedByCtorMap->erase(nativeObj); } void NonRefNativePtrCreatedByCtorMap::clear() { - __nonRefNativeObjectCreatedByCtorMap.clear(); + __nonRefNativeObjectCreatedByCtorMap->clear(); } size_t NonRefNativePtrCreatedByCtorMap::size() { - return __nonRefNativeObjectCreatedByCtorMap.size(); + return __nonRefNativeObjectCreatedByCtorMap->size(); } const NonRefNativePtrCreatedByCtorMap::Map& NonRefNativePtrCreatedByCtorMap::instance() { - return __nonRefNativeObjectCreatedByCtorMap; + return *__nonRefNativeObjectCreatedByCtorMap; } NonRefNativePtrCreatedByCtorMap::Map::iterator NonRefNativePtrCreatedByCtorMap::begin() { - return __nonRefNativeObjectCreatedByCtorMap.begin(); + return __nonRefNativeObjectCreatedByCtorMap->begin(); } NonRefNativePtrCreatedByCtorMap::Map::iterator NonRefNativePtrCreatedByCtorMap::end() { - return __nonRefNativeObjectCreatedByCtorMap.end(); + return __nonRefNativeObjectCreatedByCtorMap->end(); } diff --git a/native/cocos/scripting/js-bindings/jswrapper/MappingUtils.hpp b/native/cocos/scripting/js-bindings/jswrapper/MappingUtils.hpp index ad5fc9ee8d..d5f8edd680 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/MappingUtils.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/MappingUtils.hpp @@ -12,6 +12,9 @@ namespace se { // key: native ptr, value: se::Object using Map = std::unordered_map; + static bool init(); + static void destroy(); + static Map::iterator find(void* nativeObj); static Map::iterator erase(Map::iterator iter); static void erase(void* nativeObj); @@ -25,7 +28,7 @@ namespace se { private: static void emplace(void* nativeObj, Object* seObj); - static Map __nativePtrToObjectMap; + static Map* __nativePtrToObjectMap; friend class Object; }; @@ -36,6 +39,9 @@ namespace se { // key: native ptr, value: non-ref object created by ctor using Map = std::unordered_map; + static bool init(); + static void destroy(); + static void emplace(void* nativeObj); static Map::iterator find(void* nativeObj); static Map::iterator erase(Map::iterator iter); @@ -49,7 +55,7 @@ namespace se { static Map::iterator end(); private: - static Map __nonRefNativeObjectCreatedByCtorMap; + static Map* __nonRefNativeObjectCreatedByCtorMap; }; } // namespace se { diff --git a/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.cpp b/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.cpp index 96a3137fc2..91041e532b 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/chakracore/Object.cpp @@ -179,6 +179,11 @@ namespace se { void Object::cleanup() { ScriptEngine::getInstance()->addAfterCleanupHook([](){ + const auto& instance = NativePtrToObjectMap::instance(); + for (const auto& e : instance) + { + e.second->release(); + } NativePtrToObjectMap::clear(); NonRefNativePtrCreatedByCtorMap::clear(); }); diff --git a/native/cocos/scripting/js-bindings/jswrapper/chakracore/ScriptEngine.cpp b/native/cocos/scripting/js-bindings/jswrapper/chakracore/ScriptEngine.cpp index 4e24ed3e30..8b5a305f5b 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/chakracore/ScriptEngine.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/chakracore/ScriptEngine.cpp @@ -55,11 +55,6 @@ namespace se { if (__instance == nullptr) { __instance = new ScriptEngine(); - if (!__instance->init()) - { - delete __instance; - __instance = nullptr; - } } return __instance; @@ -86,10 +81,19 @@ namespace se { { LOGD("Initializing ChakraCore ... \n"); + for (const auto& hook : _beforeInitHookArray) + { + hook(); + } + _beforeInitHookArray.clear(); + _CHECK(JsCreateRuntime(JsRuntimeAttributeNone, nullptr, &_rt)); _CHECK(JsCreateContext(_rt, &_cx)); _CHECK(JsSetCurrentContext(_cx)); + NativePtrToObjectMap::init(); + NonRefNativePtrCreatedByCtorMap::init(); + // Set up ES6 Promise // if (JsSetPromiseContinuationCallback(PromiseContinuationCallback, &taskQueue) != JsNoError) @@ -110,6 +114,12 @@ namespace se { _isValid = true; + for (const auto& hook : _afterInitHookArray) + { + hook(); + } + _afterInitHookArray.clear(); + return true; } @@ -151,6 +161,9 @@ namespace se { } _afterCleanupHookArray.clear(); _isInCleanup = false; + + NativePtrToObjectMap::destroy(); + NonRefNativePtrCreatedByCtorMap::destroy(); } std::string ScriptEngine::formatException(JsValueRef exception) @@ -211,6 +224,16 @@ namespace se { return _globalObj; } + void ScriptEngine::addBeforeInitHook(const std::function& hook) + { + _beforeInitHookArray.push_back(hook); + } + + void ScriptEngine::addAfterInitHook(const std::function& hook) + { + _afterInitHookArray.push_back(hook); + } + void ScriptEngine::addBeforeCleanupHook(const std::function& hook) { _beforeCleanupHookArray.push_back(hook); @@ -229,6 +252,9 @@ namespace se { bool ScriptEngine::start() { + if (!init()) + return false; + bool ok = false; _startTime = std::chrono::steady_clock::now(); diff --git a/native/cocos/scripting/js-bindings/jswrapper/chakracore/ScriptEngine.hpp b/native/cocos/scripting/js-bindings/jswrapper/chakracore/ScriptEngine.hpp index d447f4305f..c9ecbb45bd 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/chakracore/ScriptEngine.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/chakracore/ScriptEngine.hpp @@ -40,6 +40,21 @@ namespace se { bool init(); void cleanup(); + + /** + * @brief Adds a hook function before initializing script engine. + * @param[in] hook A hook function to be invoked before initializing script engine. + * @note Multiple hook functions could be added, they will be invoked by the order of adding. + */ + void addBeforeInitHook(const std::function& hook); + + /** + * @brief Adds a hook function after initializing script engine. + * @param[in] hook A hook function to be invoked before initializing script engine. + * @note Multiple hook functions could be added, they will be invoked by the order of adding. + */ + void addAfterInitHook(const std::function& hook); + void addBeforeCleanupHook(const std::function& hook); void addAfterCleanupHook(const std::function& hook); @@ -53,6 +68,7 @@ namespace se { bool isValid() { return _isValid; } void clearException(); + const std::chrono::steady_clock::time_point& getStartTime() const { return _startTime; } void _retainScriptObject(void* owner, void* target); void _releaseScriptObject(void* owner, void* target); @@ -86,6 +102,10 @@ namespace se { std::vector _registerCallbackArray; std::chrono::steady_clock::time_point _startTime; + + std::vector> _beforeInitHookArray; + std::vector> _afterInitHookArray; + std::vector> _beforeCleanupHookArray; std::vector> _afterCleanupHookArray; diff --git a/native/cocos/scripting/js-bindings/jswrapper/config.hpp b/native/cocos/scripting/js-bindings/jswrapper/config.hpp index ca4dc2e0e3..9b0bf9aebd 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/config.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/config.hpp @@ -1,8 +1,8 @@ #pragma once //#define SCRIPT_ENGINE_SM -//#define SCRIPT_ENGINE_V8 -#define SCRIPT_ENGINE_JSC +#define SCRIPT_ENGINE_V8 +//#define SCRIPT_ENGINE_JSC //#define SCRIPT_ENGINE_CHAKRACORE #define SE_ENABLE_INSPECTOR 0 diff --git a/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.cpp b/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.cpp index cfc05b2e9b..59d7c486be 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/jsc/Object.cpp @@ -510,6 +510,11 @@ namespace se { void Object::cleanup() { ScriptEngine::getInstance()->addAfterCleanupHook([](){ + const auto& instance = NativePtrToObjectMap::instance(); + for (const auto& e : instance) + { + e.second->release(); + } NativePtrToObjectMap::clear(); NonRefNativePtrCreatedByCtorMap::clear(); __cx = nullptr; diff --git a/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.cpp b/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.cpp index e30196c786..b8f1f48d2a 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.cpp @@ -69,11 +69,6 @@ namespace se { if (__instance == nullptr) { __instance = new ScriptEngine(); - if (!__instance->init()) - { - delete __instance; - __instance = nullptr; - } } return __instance; @@ -99,6 +94,12 @@ namespace se { cleanup(); LOGD("Initializing JavaScriptCore \n"); + for (const auto& hook : _beforeInitHookArray) + { + hook(); + } + _beforeInitHookArray.clear(); + _cx = JSGlobalContextCreate(nullptr); if (nullptr == _cx) @@ -108,6 +109,9 @@ namespace se { JSGlobalContextSetName(_cx, ctxName); JSStringRelease(ctxName); + NativePtrToObjectMap::init(); + NonRefNativePtrCreatedByCtorMap::init(); + internal::setContext(_cx); Class::setContext(_cx); Object::setContext(_cx); @@ -134,6 +138,12 @@ namespace se { _isValid = true; + for (const auto& hook : _afterInitHookArray) + { + hook(); + } + _afterInitHookArray.clear(); + return true; } @@ -175,6 +185,9 @@ namespace se { } _afterCleanupHookArray.clear(); _isInCleanup = false; + + NativePtrToObjectMap::destroy(); + NonRefNativePtrCreatedByCtorMap::destroy(); LOGD("ScriptEngine::cleanup end ..."); } @@ -244,6 +257,16 @@ namespace se { return _globalObj; } + void ScriptEngine::addBeforeInitHook(const std::function& hook) + { + _beforeInitHookArray.push_back(hook); + } + + void ScriptEngine::addAfterInitHook(const std::function& hook) + { + _afterInitHookArray.push_back(hook); + } + void ScriptEngine::addBeforeCleanupHook(const std::function& hook) { _beforeCleanupHookArray.push_back(hook); @@ -262,6 +285,9 @@ namespace se { bool ScriptEngine::start() { + if (!init()) + return false; + bool ok = false; _startTime = std::chrono::steady_clock::now(); diff --git a/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.hpp b/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.hpp index 9dfd0d2394..cc3cb64194 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.hpp @@ -76,6 +76,20 @@ namespace se { */ bool init(); + /** + * @brief Adds a hook function before initializing script engine. + * @param[in] hook A hook function to be invoked before initializing script engine. + * @note Multiple hook functions could be added, they will be invoked by the order of adding. + */ + void addBeforeInitHook(const std::function& hook); + + /** + * @brief Adds a hook function after initializing script engine. + * @param[in] hook A hook function to be invoked before initializing script engine. + * @note Multiple hook functions could be added, they will be invoked by the order of adding. + */ + void addAfterInitHook(const std::function& hook); + /** * @brief Cleanups script engine. * @note This method will removes all objects in JavaScript VM even whose are rooted, then shutdown JavaScript VMf. @@ -128,6 +142,11 @@ namespace se { */ void clearException(); + /** + * @brief Gets the start time of script engine. + */ + const std::chrono::steady_clock::time_point& getStartTime() const { return _startTime; } + // Private API used in wrapper void _retainScriptObject(void* owner, void* target); void _releaseScriptObject(void* owner, void* target); @@ -167,6 +186,10 @@ namespace se { std::vector _registerCallbackArray; std::chrono::steady_clock::time_point _startTime; + + std::vector> _beforeInitHookArray; + std::vector> _afterInitHookArray; + std::vector> _beforeCleanupHookArray; std::vector> _afterCleanupHookArray; diff --git a/native/cocos/scripting/js-bindings/jswrapper/sm/Object.cpp b/native/cocos/scripting/js-bindings/jswrapper/sm/Object.cpp index b53ded1b6b..90832de673 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/sm/Object.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/sm/Object.cpp @@ -408,6 +408,11 @@ namespace se { ScriptEngine::getInstance()->addAfterCleanupHook([](){ __objectMap.clear(); + const auto& instance = NativePtrToObjectMap::instance(); + for (const auto& e : instance) + { + e.second->release(); + } NativePtrToObjectMap::clear(); NonRefNativePtrCreatedByCtorMap::clear(); __cx = nullptr; diff --git a/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.cpp b/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.cpp index 83ab9101a7..0ffdc4390a 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.cpp @@ -191,11 +191,6 @@ namespace se { if (__instance == nullptr) { __instance = new ScriptEngine(); - if (!__instance->init()) - { - delete __instance; - __instance = nullptr; - } } return __instance; @@ -263,11 +258,20 @@ namespace se { { LOGD("Initializing SpiderMonkey \n"); + for (const auto& hook : _beforeInitHookArray) + { + hook(); + } + _beforeInitHookArray.clear(); + _cx = JS_NewContext(JS::DefaultHeapMaxBytes); if (nullptr == _cx) return false; + NativePtrToObjectMap::init(); + NonRefNativePtrCreatedByCtorMap::init(); + Class::setContext(_cx); Object::setContext(_cx); @@ -347,6 +351,12 @@ namespace se { _isValid = true; + for (const auto& hook : _afterInitHookArray) + { + hook(); + } + _afterInitHookArray.clear(); + return true; } @@ -402,6 +412,10 @@ namespace se { } _afterCleanupHookArray.clear(); _isInCleanup = false; + + + NativePtrToObjectMap::destroy(); + NonRefNativePtrCreatedByCtorMap::destroy(); } void ScriptEngine::addBeforeCleanupHook(const std::function& hook) @@ -414,6 +428,16 @@ namespace se { _afterCleanupHookArray.push_back(hook); } + void ScriptEngine::addBeforeInitHook(const std::function& hook) + { + _beforeInitHookArray.push_back(hook); + } + + void ScriptEngine::addAfterInitHook(const std::function& hook) + { + _afterInitHookArray.push_back(hook); + } + bool ScriptEngine::isInGC() { return _isInGC; @@ -437,6 +461,9 @@ namespace se { bool ScriptEngine::start() { + if (!init()) + return false; + bool ok = false; _startTime = std::chrono::steady_clock::now(); diff --git a/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.hpp b/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.hpp index 02a6275a7a..15bf2bcdc8 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.hpp @@ -41,6 +41,20 @@ namespace se { bool init(); void cleanup(); + /** + * @brief Adds a hook function before initializing script engine. + * @param[in] hook A hook function to be invoked before initializing script engine. + * @note Multiple hook functions could be added, they will be invoked by the order of adding. + */ + void addBeforeInitHook(const std::function& hook); + + /** + * @brief Adds a hook function after initializing script engine. + * @param[in] hook A hook function to be invoked before initializing script engine. + * @note Multiple hook functions could be added, they will be invoked by the order of adding. + */ + void addAfterInitHook(const std::function& hook); + void addBeforeCleanupHook(const std::function& hook); void addAfterCleanupHook(const std::function& hook); @@ -91,6 +105,10 @@ namespace se { std::vector _registerCallbackArray; std::chrono::steady_clock::time_point _startTime; + + std::vector> _beforeInitHookArray; + std::vector> _afterInitHookArray; + std::vector> _beforeCleanupHookArray; std::vector> _afterCleanupHookArray; }; diff --git a/native/cocos/scripting/js-bindings/jswrapper/v8/ScriptEngine.cpp b/native/cocos/scripting/js-bindings/jswrapper/v8/ScriptEngine.cpp index e170c23a3c..17a8f0e81f 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/v8/ScriptEngine.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/v8/ScriptEngine.cpp @@ -96,11 +96,6 @@ namespace se { if (__instance == nullptr) { __instance = new ScriptEngine(); - if (!__instance->init()) - { - delete __instance; - __instance = nullptr; - } } return __instance; @@ -146,6 +141,12 @@ namespace se { { LOGD("Initializing V8\n"); + for (const auto& hook : _beforeInitHookArray) + { + hook(); + } + _beforeInitHookArray.clear(); + assert(_allocator == nullptr); _allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator(); // Create a new Isolate and make it the current one. @@ -163,6 +164,9 @@ namespace se { _context.Reset(_isolate, v8::Context::New(_isolate)); _context.Get(_isolate)->Enter(); + NativePtrToObjectMap::init(); + NonRefNativePtrCreatedByCtorMap::init(); + Class::setIsolate(_isolate); Object::setIsolate(_isolate); @@ -194,6 +198,12 @@ namespace se { _isValid = true; + for (const auto& hook : _afterInitHookArray) + { + hook(); + } + _afterInitHookArray.clear(); + return _isValid; } @@ -243,6 +253,10 @@ namespace se { hook(); } _afterCleanupHookArray.clear(); + + + NativePtrToObjectMap::destroy(); + NonRefNativePtrCreatedByCtorMap::destroy(); } Object* ScriptEngine::getGlobalObject() const @@ -250,6 +264,16 @@ namespace se { return _globalObj; } + void ScriptEngine::addBeforeInitHook(const std::function& hook) + { + _beforeInitHookArray.push_back(hook); + } + + void ScriptEngine::addAfterInitHook(const std::function& hook) + { + _afterInitHookArray.push_back(hook); + } + void ScriptEngine::addBeforeCleanupHook(const std::function& hook) { _beforeCleanupHookArray.push_back(hook); @@ -268,6 +292,10 @@ namespace se { bool ScriptEngine::start() { + if (!init()) + return false; + + se::AutoHandleScope hs; bool ok = false; _startTime = std::chrono::steady_clock::now(); diff --git a/native/cocos/scripting/js-bindings/jswrapper/v8/ScriptEngine.hpp b/native/cocos/scripting/js-bindings/jswrapper/v8/ScriptEngine.hpp index 5bc7d35383..0885e3fe09 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/v8/ScriptEngine.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/v8/ScriptEngine.hpp @@ -60,6 +60,21 @@ namespace se { bool init(); void cleanup(); + + /** + * @brief Adds a hook function before initializing script engine. + * @param[in] hook A hook function to be invoked before initializing script engine. + * @note Multiple hook functions could be added, they will be invoked by the order of adding. + */ + void addBeforeInitHook(const std::function& hook); + + /** + * @brief Adds a hook function after initializing script engine. + * @param[in] hook A hook function to be invoked before initializing script engine. + * @note Multiple hook functions could be added, they will be invoked by the order of adding. + */ + void addAfterInitHook(const std::function& hook); + void addBeforeCleanupHook(const std::function& hook); void addAfterCleanupHook(const std::function& hook); @@ -74,6 +89,8 @@ namespace se { void clearException(); + const std::chrono::steady_clock::time_point& getStartTime() const { return _startTime; } + void _retainScriptObject(void* owner, void* target); void _releaseScriptObject(void* owner, void* target); @@ -111,6 +128,10 @@ namespace se { std::vector _registerCallbackArray; std::chrono::steady_clock::time_point _startTime; + + std::vector> _beforeInitHookArray; + std::vector> _afterInitHookArray; + std::vector> _beforeCleanupHookArray; std::vector> _afterCleanupHookArray; diff --git a/native/cocos/scripting/js-bindings/manual/jsb_box2d_manual.cpp b/native/cocos/scripting/js-bindings/manual/jsb_box2d_manual.cpp index fce5ebf14e..4cff6dbb6b 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_box2d_manual.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_box2d_manual.cpp @@ -1082,7 +1082,11 @@ bool register_all_box2d_manual(se::Object* obj) b2SetObjectDestroyNotifier([](void* obj, b2ObjectType type, const char* typeName){ std::string typeNameStr = typeName; - auto cleanup = [=](){ + auto cleanup = [obj, typeNameStr](){ + + if (!se::ScriptEngine::getInstance()->isValid()) + return; + se::AutoHandleScope hs; se::ScriptEngine::getInstance()->clearException(); diff --git a/native/cocos/scripting/js-bindings/manual/jsb_classtype.cpp b/native/cocos/scripting/js-bindings/manual/jsb_classtype.cpp index 63f7db0acf..c4c63cb2df 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_classtype.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_classtype.cpp @@ -1,8 +1,25 @@ #include "jsb_classtype.hpp" -std::unordered_map JSBClassType::__jsbClassTypeMap; +JSBClassType::Map* JSBClassType::__jsbClassTypeMap = nullptr; + +bool JSBClassType::init() +{ + if (__jsbClassTypeMap == nullptr) + __jsbClassTypeMap = new (std::nothrow) Map(); + + return __jsbClassTypeMap != nullptr; +} + +void JSBClassType::destroy() +{ + if (__jsbClassTypeMap != nullptr) + { + delete __jsbClassTypeMap; + __jsbClassTypeMap = nullptr; + } +} void JSBClassType::cleanup() { - __jsbClassTypeMap.clear(); + __jsbClassTypeMap->clear(); } diff --git a/native/cocos/scripting/js-bindings/manual/jsb_classtype.hpp b/native/cocos/scripting/js-bindings/manual/jsb_classtype.hpp index 37af75ec44..ebe417bb9d 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_classtype.hpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_classtype.hpp @@ -7,12 +7,15 @@ class JSBClassType { public: + static bool init(); + static void destroy(); + template static void registerClass(se::Class* cls) { const char* typeName = typeid(T).name(); - assert(__jsbClassTypeMap.find(typeName) == __jsbClassTypeMap.end()); - __jsbClassTypeMap.emplace(typeName, cls); + assert(__jsbClassTypeMap->find(typeName) == __jsbClassTypeMap->end()); + __jsbClassTypeMap->emplace(typeName, cls); } template @@ -20,12 +23,12 @@ public: { bool found = false; std::string typeName = typeid(*nativeObj).name(); - auto iter = __jsbClassTypeMap.find(typeName); - if (iter == __jsbClassTypeMap.end()) + auto iter = __jsbClassTypeMap->find(typeName); + if (iter == __jsbClassTypeMap->end()) { typeName = typeid(T).name(); - iter = __jsbClassTypeMap.find(typeName); - if (iter != __jsbClassTypeMap.end()) + iter = __jsbClassTypeMap->find(typeName); + if (iter != __jsbClassTypeMap->end()) { found = true; } @@ -40,5 +43,6 @@ public: static void cleanup(); private: - static std::unordered_map __jsbClassTypeMap; + using Map = std::unordered_map; + static Map* __jsbClassTypeMap; }; diff --git a/native/cocos/scripting/js-bindings/manual/jsb_global.cpp b/native/cocos/scripting/js-bindings/manual/jsb_global.cpp index f48fa795a9..13c53bc260 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_global.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_global.cpp @@ -629,9 +629,9 @@ static bool getOrCreatePlainObject_r(const char* name, se::Object* parent, se::O static bool js_performance_now(se::State& s) { -//FIXME: auto now = std::chrono::steady_clock::now(); -// auto micro = std::chrono::duration_cast(now - se::ScriptEngine::getInstance()->getStartTime()).count(); -// s.rval().setNumber((double)micro * 0.001); + auto now = std::chrono::steady_clock::now(); + auto micro = std::chrono::duration_cast(now - se::ScriptEngine::getInstance()->getStartTime()).count(); + s.rval().setNumber((double)micro * 0.001); return true; } SE_BIND_FUNC(js_performance_now) diff --git a/native/cocos/scripting/js-bindings/manual/jsb_module_register.cpp b/native/cocos/scripting/js-bindings/manual/jsb_module_register.cpp index ee2b4a7419..9a5288540e 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_module_register.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_module_register.cpp @@ -48,6 +48,7 @@ using namespace cocos2d; bool jsb_run_script(const std::string& filePath) { + se::AutoHandleScope hs; Data data = FileUtils::getInstance()->getDataFromFile(filePath); if (data.isNull()) return false; @@ -70,12 +71,16 @@ static bool run_boot_script(se::Object* global) bool jsb_register_all_modules() { se::ScriptEngine* se = se::ScriptEngine::getInstance(); + + se->addBeforeInitHook([](){ + JSBClassType::init(); + }); + se->addBeforeCleanupHook([se](){ se->gc(); PoolManager::getInstance()->getCurrentPool()->clear(); se->gc(); PoolManager::getInstance()->getCurrentPool()->clear(); - CCLOG("test"); }); se->addRegisterCallback(jsb_register_global_variables); @@ -128,8 +133,8 @@ bool jsb_register_all_modules() se->addRegisterCallback(run_boot_script); se->addAfterCleanupHook([](){ - JSBClassType::cleanup(); PoolManager::getInstance()->getCurrentPool()->clear(); + JSBClassType::destroy(); }); return true; } From ce7c78a857fb2b71a0b3e95c3f0166ed1839021d Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 18 Aug 2017 14:30:39 +0800 Subject: [PATCH 04/11] updates external and bindings-generator. --- native/external/config.json | 2 +- native/tools/bindings-generator | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/native/external/config.json b/native/external/config.json index 6dba110a17..fe7df4a796 100644 --- a/native/external/config.json +++ b/native/external/config.json @@ -1,5 +1,5 @@ { - "version": "v3-deps-38", + "version": "v3-deps-39", "zip_file_size": "114318593", "repo_name": "cocos2d-x-lite-external", "repo_parent": "https://github.com/cocos-creator/" diff --git a/native/tools/bindings-generator b/native/tools/bindings-generator index 9fa88819c7..b79c709ab5 160000 --- a/native/tools/bindings-generator +++ b/native/tools/bindings-generator @@ -1 +1 @@ -Subproject commit 9fa88819c776ccf866905533183dfd9dc181db75 +Subproject commit b79c709ab518b124027e3b881289834bd7b2f49e From 7c889aa691e633996527a875642339fc9c6a3795 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 18 Aug 2017 14:34:04 +0800 Subject: [PATCH 05/11] updates SDKManager --- native/extensions/anysdk/js-bindings/SDKManager.cpp | 11 ++++++----- native/extensions/anysdk/js-bindings/SDKManager.h | 5 +---- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/native/extensions/anysdk/js-bindings/SDKManager.cpp b/native/extensions/anysdk/js-bindings/SDKManager.cpp index dc186779c4..f62b1bae71 100644 --- a/native/extensions/anysdk/js-bindings/SDKManager.cpp +++ b/native/extensions/anysdk/js-bindings/SDKManager.cpp @@ -2,7 +2,7 @@ using namespace anysdk::framework; -SDKManager* SDKManager::_pInstance = NULL; +SDKManager* SDKManager::_pInstance = nullptr; SDKManager::SDKManager() { @@ -10,12 +10,13 @@ SDKManager::SDKManager() SDKManager::~SDKManager() { - _pAgent->unloadAllPlugins(); + AgentManager::getInstance()->unloadAllPlugins(); + AgentManager::end(); } SDKManager* SDKManager::getInstance() { - if (_pInstance == NULL) { + if (_pInstance == nullptr) { _pInstance = new SDKManager(); } return _pInstance; @@ -23,10 +24,10 @@ SDKManager* SDKManager::getInstance() void SDKManager::purge() { - if (_pInstance) + if (_pInstance != nullptr) { delete _pInstance; - _pInstance = NULL; + _pInstance = nullptr; } } diff --git a/native/extensions/anysdk/js-bindings/SDKManager.h b/native/extensions/anysdk/js-bindings/SDKManager.h index c2e83c9dbd..4072b0a74b 100644 --- a/native/extensions/anysdk/js-bindings/SDKManager.h +++ b/native/extensions/anysdk/js-bindings/SDKManager.h @@ -2,8 +2,7 @@ #define __SDK_MANAGER_H__ #include "AgentManager.h" -using namespace anysdk::framework; -using namespace std; + class SDKManager { public: @@ -17,8 +16,6 @@ private: virtual ~SDKManager(); static SDKManager* _pInstance; - - AgentManager* _pAgent; }; #endif From 717face5fb5a82401042b0b2518d52b52dc98f96 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 18 Aug 2017 14:39:07 +0800 Subject: [PATCH 06/11] Updates xcode project --- .../project.pbxproj | 92 ++++++------------- 1 file changed, 28 insertions(+), 64 deletions(-) diff --git a/native/cocos/scripting/js-bindings/proj.ios_mac/cocos2d_js_bindings.xcodeproj/project.pbxproj b/native/cocos/scripting/js-bindings/proj.ios_mac/cocos2d_js_bindings.xcodeproj/project.pbxproj index b68500ce64..c6bdce35a3 100644 --- a/native/cocos/scripting/js-bindings/proj.ios_mac/cocos2d_js_bindings.xcodeproj/project.pbxproj +++ b/native/cocos/scripting/js-bindings/proj.ios_mac/cocos2d_js_bindings.xcodeproj/project.pbxproj @@ -23,6 +23,14 @@ 1A159D511F13216F00558E38 /* jsb_socketio.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A159D4E1F13216F00558E38 /* jsb_socketio.cpp */; }; 1A159D521F13216F00558E38 /* jsb_socketio.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1A159D4F1F13216F00558E38 /* jsb_socketio.hpp */; }; 1A159D531F13216F00558E38 /* jsb_socketio.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1A159D4F1F13216F00558E38 /* jsb_socketio.hpp */; }; + 1A210D941F456821006CCBAE /* MappingUtils.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A210D921F456821006CCBAE /* MappingUtils.cpp */; }; + 1A210D951F456821006CCBAE /* MappingUtils.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A210D921F456821006CCBAE /* MappingUtils.cpp */; }; + 1A210D961F456821006CCBAE /* MappingUtils.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1A210D931F456821006CCBAE /* MappingUtils.hpp */; }; + 1A210D971F456821006CCBAE /* MappingUtils.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1A210D931F456821006CCBAE /* MappingUtils.hpp */; }; + 1A210DE01F46C2D3006CCBAE /* libjs_static.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 1A210DDE1F46C2D3006CCBAE /* libjs_static.a */; }; + 1A210DE11F46C2D3006CCBAE /* libmozglue.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 1A210DDF1F46C2D3006CCBAE /* libmozglue.a */; }; + 1A210DE41F46C2DF006CCBAE /* libjs_static.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 1A210DE21F46C2DF006CCBAE /* libjs_static.a */; }; + 1A210DE51F46C2DF006CCBAE /* libmozglue.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 1A210DE31F46C2DF006CCBAE /* libmozglue.a */; }; 1A5F228A1F0DDD6B0008BF09 /* jsb_dragonbones_manual.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A5F22881F0DDD6B0008BF09 /* jsb_dragonbones_manual.cpp */; }; 1A5F228B1F0DDD6B0008BF09 /* jsb_dragonbones_manual.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A5F22881F0DDD6B0008BF09 /* jsb_dragonbones_manual.cpp */; }; 1A5F228C1F0DDD6B0008BF09 /* jsb_dragonbones_manual.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1A5F22891F0DDD6B0008BF09 /* jsb_dragonbones_manual.hpp */; }; @@ -77,8 +85,6 @@ 1AB5E62C18D05BC80088DAA4 /* jsb_cocos2dx_ui_auto.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AB5E62918D05BC80088DAA4 /* jsb_cocos2dx_ui_auto.cpp */; }; 1AB5E62D18D05BC80088DAA4 /* jsb_cocos2dx_ui_auto.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AB5E62A18D05BC80088DAA4 /* jsb_cocos2dx_ui_auto.hpp */; }; 1AB5E62E18D05BC80088DAA4 /* jsb_cocos2dx_ui_auto.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AB5E62A18D05BC80088DAA4 /* jsb_cocos2dx_ui_auto.hpp */; }; - 1AC0650A1F00A62A009EC214 /* libmozglue.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 1AC065091F00A62A009EC214 /* libmozglue.a */; }; - 1AC0650C1F00A926009EC214 /* JavaScriptCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1AC0650B1F00A926009EC214 /* JavaScriptCore.framework */; }; 1AD4AE671F0F920700A823D8 /* jsb_websocket.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AD4AE651F0F920700A823D8 /* jsb_websocket.cpp */; }; 1AD4AE681F0F920700A823D8 /* jsb_websocket.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AD4AE651F0F920700A823D8 /* jsb_websocket.cpp */; }; 1AD4AE691F0F920700A823D8 /* jsb_websocket.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AD4AE661F0F920700A823D8 /* jsb_websocket.hpp */; }; @@ -261,9 +267,6 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ - 1A1051061F14C0B600D4322C /* libjs_static.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libjs_static.a; path = "../../../../../SpiderMonkey/build-osx/release-dist/sdk/lib/libjs_static.a"; sourceTree = ""; }; - 1A1051071F14C0B600D4322C /* libmozglue.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libmozglue.a; path = "../../../../../SpiderMonkey/build-osx/release-dist/sdk/lib/libmozglue.a"; sourceTree = ""; }; - 1A10510D1F14CB4000D4322C /* libjs_static.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libjs_static.a; path = ../../../../external/mac/libs/libjs_static.a; sourceTree = ""; }; 1A119E2F18BDF19200352BAA /* jsb_cocos2dx_auto.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_cocos2dx_auto.cpp; sourceTree = ""; }; 1A119E3018BDF19200352BAA /* jsb_cocos2dx_auto.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = jsb_cocos2dx_auto.hpp; sourceTree = ""; }; 1A119E3518BDF19200352BAA /* jsb_cocos2dx_extension_auto.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_cocos2dx_extension_auto.cpp; sourceTree = ""; }; @@ -272,8 +275,12 @@ 1A119E3C18BDF19200352BAA /* jsb_cocos2dx_spine_auto.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = jsb_cocos2dx_spine_auto.hpp; sourceTree = ""; }; 1A159D4E1F13216F00558E38 /* jsb_socketio.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_socketio.cpp; sourceTree = ""; }; 1A159D4F1F13216F00558E38 /* jsb_socketio.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = jsb_socketio.hpp; sourceTree = ""; }; - 1A4E8E801F00CB0200F30872 /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = System/Library/Frameworks/JavaScriptCore.framework; sourceTree = SDKROOT; }; - 1A4E8E821F00E84C00F30872 /* libChakraCoreStatic.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libChakraCoreStatic.a; path = ../../../../../ChakraCore/out/Debug/lib/libChakraCoreStatic.a; sourceTree = ""; }; + 1A210D921F456821006CCBAE /* MappingUtils.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MappingUtils.cpp; sourceTree = ""; }; + 1A210D931F456821006CCBAE /* MappingUtils.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = MappingUtils.hpp; sourceTree = ""; }; + 1A210DDE1F46C2D3006CCBAE /* libjs_static.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libjs_static.a; path = ../../../../external/ios/libs/libjs_static.a; sourceTree = ""; }; + 1A210DDF1F46C2D3006CCBAE /* libmozglue.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libmozglue.a; path = ../../../../external/ios/libs/libmozglue.a; sourceTree = ""; }; + 1A210DE21F46C2DF006CCBAE /* libjs_static.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libjs_static.a; path = ../../../../external/mac/libs/libjs_static.a; sourceTree = ""; }; + 1A210DE31F46C2DF006CCBAE /* libmozglue.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libmozglue.a; path = ../../../../external/mac/libs/libmozglue.a; sourceTree = ""; }; 1A5410A418B785A10016A3AF /* libjscocos2d Mac.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libjscocos2d Mac.a"; sourceTree = BUILT_PRODUCTS_DIR; }; 1A5410A518B785A10016A3AF /* libjscocos2d iOS.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libjscocos2d iOS.a"; sourceTree = BUILT_PRODUCTS_DIR; }; 1A5F22881F0DDD6B0008BF09 /* jsb_dragonbones_manual.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_dragonbones_manual.cpp; sourceTree = ""; }; @@ -284,14 +291,6 @@ 1A5F229B1F0E37B20008BF09 /* jsb_creator_manual.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = jsb_creator_manual.hpp; sourceTree = ""; }; 1A6293EE1F44128300A07B77 /* BaseJSAction.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BaseJSAction.cpp; sourceTree = ""; }; 1A6293EF1F44128300A07B77 /* BaseJSAction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BaseJSAction.h; sourceTree = ""; }; - 1A62FCAA1F282DDA0002E38C /* libbmalloc.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libbmalloc.a; path = "../../../../../JavaScriptCore/build-osx/lib/libbmalloc.a"; sourceTree = ""; }; - 1A62FCAB1F282DDA0002E38C /* libJavaScriptCore.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libJavaScriptCore.a; path = "../../../../../JavaScriptCore/build-osx/lib/libJavaScriptCore.a"; sourceTree = ""; }; - 1A62FCAC1F282DDA0002E38C /* libWTF.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libWTF.a; path = "../../../../../JavaScriptCore/build-osx/lib/libWTF.a"; sourceTree = ""; }; - 1A62FCAD1F282DDA0002E38C /* libbmalloc.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libbmalloc.a; path = "../../../../../JavaScriptCore/build-osx/lib/libbmalloc.a"; sourceTree = ""; }; - 1A62FCAE1F282DDA0002E38C /* libJavaScriptCore.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libJavaScriptCore.a; path = "../../../../../JavaScriptCore/build-osx/lib/libJavaScriptCore.a"; sourceTree = ""; }; - 1A62FCAF1F282DDA0002E38C /* libWTF.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libWTF.a; path = "../../../../../JavaScriptCore/build-osx/lib/libWTF.a"; sourceTree = ""; }; - 1A62FCB01F282DDB0002E38C /* libJavaScriptCore.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libJavaScriptCore.a; path = "../../../../../JavaScriptCore/build-osx/lib/libJavaScriptCore.a"; sourceTree = ""; }; - 1A62FCB11F282DDB0002E38C /* libbmalloc.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libbmalloc.a; path = "../../../../../JavaScriptCore/build-osx/lib/libbmalloc.a"; sourceTree = ""; }; 1A6B8FB11F3BF7E800BC83DC /* SHA1.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SHA1.cpp; sourceTree = ""; }; 1A6B8FB21F3BF7E800BC83DC /* SHA1.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SHA1.h; sourceTree = ""; }; 1A7759C11F340FB10012A210 /* inspector_agent.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = inspector_agent.cc; sourceTree = ""; }; @@ -314,30 +313,14 @@ 1A7759F41F3421B90012A210 /* node.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = node.cc; sourceTree = ""; }; 1A7759F51F3421B90012A210 /* node.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = node.h; sourceTree = ""; }; 1A7759FA1F3460BB0012A210 /* node_mutex.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = node_mutex.h; sourceTree = ""; }; - 1A7759FD1F3467990012A210 /* libuv.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libuv.a; path = ../../../../external/mac/libs/libuv.a; sourceTree = ""; }; - 1A7759FF1F346A090012A210 /* libinspector.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libinspector.a; path = ../../../../external/mac/libs/libinspector.a; sourceTree = ""; }; 1A81B41C1F137BFE008C38F8 /* JavaScriptObjCBridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JavaScriptObjCBridge.h; sourceTree = ""; }; 1A81B41D1F137BFE008C38F8 /* JavaScriptObjCBridge.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = JavaScriptObjCBridge.mm; sourceTree = ""; }; 1A81B4221F149E7B008C38F8 /* jsb_module_register.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_module_register.cpp; sourceTree = ""; }; 1A81B4231F149E7B008C38F8 /* jsb_module_register.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = jsb_module_register.hpp; sourceTree = ""; }; - 1A8D25531F3AB432002CC0A8 /* libv8_builtins_generators.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libv8_builtins_generators.a; path = ../../../../external/mac/libs/libv8_builtins_generators.a; sourceTree = ""; }; - 1A8D25541F3AB432002CC0A8 /* libv8_builtins_setup.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libv8_builtins_setup.a; path = ../../../../external/mac/libs/libv8_builtins_setup.a; sourceTree = ""; }; 1A9F0F9B1F30235C00A499E1 /* jsb_helper.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_helper.cpp; sourceTree = ""; }; 1A9F0F9C1F30235C00A499E1 /* jsb_helper.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = jsb_helper.hpp; sourceTree = ""; }; - 1AB064771EF9647F0061E1AF /* libbmalloc.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libbmalloc.a; path = ../../../../../JavaScriptCore/build/lib/libbmalloc.a; sourceTree = ""; }; - 1AB064781EF9647F0061E1AF /* libJavaScriptCore.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libJavaScriptCore.a; path = ../../../../../JavaScriptCore/build/lib/libJavaScriptCore.a; sourceTree = ""; }; - 1AB064791EF9647F0061E1AF /* libWTF.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libWTF.a; path = ../../../../../JavaScriptCore/build/lib/libWTF.a; sourceTree = ""; }; 1AB5E62918D05BC80088DAA4 /* jsb_cocos2dx_ui_auto.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_cocos2dx_ui_auto.cpp; sourceTree = ""; }; 1AB5E62A18D05BC80088DAA4 /* jsb_cocos2dx_ui_auto.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = jsb_cocos2dx_ui_auto.hpp; sourceTree = ""; }; - 1AB649FC1F035628000C28A7 /* libv8_base.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libv8_base.a; path = ../../../../external/mac/libs/libv8_base.a; sourceTree = ""; }; - 1AB649FD1F035628000C28A7 /* libv8_libbase.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libv8_libbase.a; path = ../../../../external/mac/libs/libv8_libbase.a; sourceTree = ""; }; - 1AB649FE1F035628000C28A7 /* libv8_libplatform.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libv8_libplatform.a; path = ../../../../external/mac/libs/libv8_libplatform.a; sourceTree = ""; }; - 1AB649FF1F035628000C28A7 /* libv8_libsampler.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libv8_libsampler.a; path = ../../../../external/mac/libs/libv8_libsampler.a; sourceTree = ""; }; - 1AB64A001F035628000C28A7 /* libv8_nosnapshot.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libv8_nosnapshot.a; path = ../../../../external/mac/libs/libv8_nosnapshot.a; sourceTree = ""; }; - 1AC065091F00A62A009EC214 /* libmozglue.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libmozglue.a; path = ../../../../external/ios/libs/libmozglue.a; sourceTree = ""; }; - 1AC0650B1F00A926009EC214 /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS10.3.sdk/System/Library/Frameworks/JavaScriptCore.framework; sourceTree = DEVELOPER_DIR; }; - 1AD4AE5F1F0F88E400A823D8 /* libicui18n.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libicui18n.a; path = ../../../../../../../../usr/local/Cellar/icu4c/58.2/lib/libicui18n.a; sourceTree = ""; }; - 1AD4AE601F0F88E400A823D8 /* libicuuc.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libicuuc.a; path = ../../../../../../../../usr/local/Cellar/icu4c/58.2/lib/libicuuc.a; sourceTree = ""; }; 1AD4AE651F0F920700A823D8 /* jsb_websocket.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_websocket.cpp; sourceTree = ""; }; 1AD4AE661F0F920700A823D8 /* jsb_websocket.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = jsb_websocket.hpp; sourceTree = ""; }; 1AF82D411F2EE1C20002C5A0 /* HandleObject.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HandleObject.cpp; sourceTree = ""; }; @@ -415,7 +398,6 @@ 1AFD36BF1EF116190097DBB1 /* jsb_xmlhttprequest.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = jsb_xmlhttprequest.hpp; sourceTree = ""; }; 1AFD36C01EF116190097DBB1 /* ScriptingCore.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ScriptingCore.cpp; sourceTree = ""; }; 1AFD36C11EF116190097DBB1 /* ScriptingCore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ScriptingCore.h; sourceTree = ""; }; - 1AFD37031EF116CB0097DBB1 /* libmozglue.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libmozglue.a; path = ../../../../external/mac/libs/libmozglue.a; sourceTree = ""; }; 4BE089E01ADF965E00D65D4B /* jsb_cocos2dx_experimental_webView_auto.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_cocos2dx_experimental_webView_auto.cpp; sourceTree = ""; }; 4BE089E11ADF965E00D65D4B /* jsb_cocos2dx_experimental_webView_auto.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = jsb_cocos2dx_experimental_webView_auto.hpp; sourceTree = ""; }; 4BE089EA1ADF967400D65D4B /* jsb_cocos2dx_experimental_webView_manual.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = jsb_cocos2dx_experimental_webView_manual.h; sourceTree = ""; }; @@ -438,6 +420,8 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 1A210DE41F46C2DF006CCBAE /* libjs_static.a in Frameworks */, + 1A210DE51F46C2DF006CCBAE /* libmozglue.a in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -445,8 +429,8 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 1AC0650C1F00A926009EC214 /* JavaScriptCore.framework in Frameworks */, - 1AC0650A1F00A62A009EC214 /* libmozglue.a in Frameworks */, + 1A210DE01F46C2D3006CCBAE /* libjs_static.a in Frameworks */, + 1A210DE11F46C2D3006CCBAE /* libmozglue.a in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -557,6 +541,8 @@ 1AFD36331EF115100097DBB1 /* Value.hpp */, 1AF82D411F2EE1C20002C5A0 /* HandleObject.cpp */, 1AF82D421F2EE1C20002C5A0 /* HandleObject.hpp */, + 1A210D921F456821006CCBAE /* MappingUtils.cpp */, + 1A210D931F456821006CCBAE /* MappingUtils.hpp */, ); name = jswrapper; path = ../jswrapper; @@ -661,36 +647,10 @@ 1AFD37021EF116CA0097DBB1 /* Frameworks */ = { isa = PBXGroup; children = ( - 1A8D25531F3AB432002CC0A8 /* libv8_builtins_generators.a */, - 1A8D25541F3AB432002CC0A8 /* libv8_builtins_setup.a */, - 1A7759FF1F346A090012A210 /* libinspector.a */, - 1A7759FD1F3467990012A210 /* libuv.a */, - 1A62FCB11F282DDB0002E38C /* libbmalloc.a */, - 1A62FCB01F282DDB0002E38C /* libJavaScriptCore.a */, - 1A62FCAD1F282DDA0002E38C /* libbmalloc.a */, - 1A62FCAE1F282DDA0002E38C /* libJavaScriptCore.a */, - 1A62FCAF1F282DDA0002E38C /* libWTF.a */, - 1A62FCAA1F282DDA0002E38C /* libbmalloc.a */, - 1A62FCAB1F282DDA0002E38C /* libJavaScriptCore.a */, - 1A62FCAC1F282DDA0002E38C /* libWTF.a */, - 1A10510D1F14CB4000D4322C /* libjs_static.a */, - 1A1051061F14C0B600D4322C /* libjs_static.a */, - 1A1051071F14C0B600D4322C /* libmozglue.a */, - 1AD4AE5F1F0F88E400A823D8 /* libicui18n.a */, - 1AD4AE601F0F88E400A823D8 /* libicuuc.a */, - 1AB649FC1F035628000C28A7 /* libv8_base.a */, - 1AB649FD1F035628000C28A7 /* libv8_libbase.a */, - 1AB649FE1F035628000C28A7 /* libv8_libplatform.a */, - 1AB649FF1F035628000C28A7 /* libv8_libsampler.a */, - 1AB64A001F035628000C28A7 /* libv8_nosnapshot.a */, - 1A4E8E821F00E84C00F30872 /* libChakraCoreStatic.a */, - 1A4E8E801F00CB0200F30872 /* JavaScriptCore.framework */, - 1AC0650B1F00A926009EC214 /* JavaScriptCore.framework */, - 1AC065091F00A62A009EC214 /* libmozglue.a */, - 1AB064771EF9647F0061E1AF /* libbmalloc.a */, - 1AB064781EF9647F0061E1AF /* libJavaScriptCore.a */, - 1AB064791EF9647F0061E1AF /* libWTF.a */, - 1AFD37031EF116CB0097DBB1 /* libmozglue.a */, + 1A210DE21F46C2DF006CCBAE /* libjs_static.a */, + 1A210DE31F46C2DF006CCBAE /* libmozglue.a */, + 1A210DDE1F46C2D3006CCBAE /* libjs_static.a */, + 1A210DDF1F46C2D3006CCBAE /* libmozglue.a */, ); name = Frameworks; sourceTree = ""; @@ -705,6 +665,7 @@ 4DFA4C121CBCC49000E3B736 /* jsb_creator_auto.hpp in Headers */, 1AFD365C1EF115100097DBB1 /* SeApi.h in Headers */, 1AFD36961EF115100097DBB1 /* ScriptEngine.hpp in Headers */, + 1A210D961F456821006CCBAE /* MappingUtils.hpp in Headers */, 1AFD36C81EF116190097DBB1 /* jsb_classtype.hpp in Headers */, 1AFD36881EF115100097DBB1 /* Class.hpp in Headers */, 1AFD36441EF115100097DBB1 /* SeApi.h in Headers */, @@ -796,6 +757,7 @@ 1AFD36D51EF116190097DBB1 /* jsb_conversions.hpp in Headers */, 1AFD36C91EF116190097DBB1 /* jsb_classtype.hpp in Headers */, 1AFD36351EF115100097DBB1 /* Base.h in Headers */, + 1A210D971F456821006CCBAE /* MappingUtils.hpp in Headers */, 1AFD36931EF115100097DBB1 /* ObjectWrap.h in Headers */, 1AFD36D11EF116190097DBB1 /* jsb_cocos2dx_manual.hpp in Headers */, 1AFD36711EF115100097DBB1 /* HelperMacros.h in Headers */, @@ -977,6 +939,7 @@ BA4F59841E28DCD3003F5096 /* jsb_cocos2dx_network_auto.cpp in Sources */, 1A7759DB1F34105B0012A210 /* node_debug_options.cc in Sources */, 1AFD36461EF115100097DBB1 /* Utils.cpp in Sources */, + 1A210D941F456821006CCBAE /* MappingUtils.cpp in Sources */, 1A5F22961F0E306C0008BF09 /* jsb_box2d_manual.cpp in Sources */, 1A7759F01F341D6E0012A210 /* env.cc in Sources */, 1A7759D51F340FB10012A210 /* inspector_socket.cc in Sources */, @@ -1012,6 +975,7 @@ 1AB5E62C18D05BC80088DAA4 /* jsb_cocos2dx_ui_auto.cpp in Sources */, 1AFD36651EF115100097DBB1 /* Ref.cpp in Sources */, BA4F59851E28DCD3003F5096 /* jsb_cocos2dx_network_auto.cpp in Sources */, + 1A210D951F456821006CCBAE /* MappingUtils.cpp in Sources */, 1A119E9C18BDF19200352BAA /* jsb_cocos2dx_spine_auto.cpp in Sources */, 1A5F22971F0E306C0008BF09 /* jsb_box2d_manual.cpp in Sources */, 1AFD36C71EF116190097DBB1 /* jsb_classtype.cpp in Sources */, From e58401f0faaeed398a9f50712b8034fb4923d294 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 18 Aug 2017 14:44:44 +0800 Subject: [PATCH 07/11] updates template --- .../frameworks/runtime-src/Classes/AppDelegate.cpp | 6 ------ .../runtime-src/proj.ios_mac/ios/AppController.mm | 11 ++++++++++- .../frameworks/runtime-src/Classes/AppDelegate.cpp | 6 ------ .../runtime-src/proj.ios_mac/ios/AppController.mm | 12 +++++++++++- .../frameworks/runtime-src/Classes/AppDelegate.cpp | 6 ------ .../runtime-src/proj.ios_mac/ios/AppController.mm | 11 ++++++++++- 6 files changed, 31 insertions(+), 21 deletions(-) diff --git a/native/templates/js-template-binary/frameworks/runtime-src/Classes/AppDelegate.cpp b/native/templates/js-template-binary/frameworks/runtime-src/Classes/AppDelegate.cpp index be204540a6..0a3b74df34 100755 --- a/native/templates/js-template-binary/frameworks/runtime-src/Classes/AppDelegate.cpp +++ b/native/templates/js-template-binary/frameworks/runtime-src/Classes/AppDelegate.cpp @@ -74,13 +74,7 @@ bool AppDelegate::applicationDidFinishLaunching() ScriptEngineManager::getInstance()->setScriptEngine(sc); se::ScriptEngine* se = se::ScriptEngine::getInstance(); - if (!se->isValid()) - { - CCLOGERROR("Unable to Initialize se::ScriptEngine."); - return false; - } - se::AutoHandleScope hs; jsb_register_all_modules(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && PACKAGE_AS diff --git a/native/templates/js-template-binary/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm b/native/templates/js-template-binary/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm index 8061f93565..844c029ed3 100755 --- a/native/templates/js-template-binary/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm +++ b/native/templates/js-template-binary/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm @@ -39,10 +39,14 @@ using namespace cocos2d; #pragma mark Application lifecycle // cocos2d application instance -static AppDelegate s_sharedApplication; +static AppDelegate* s_sharedApplication = nullptr; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + if (s_sharedApplication == nullptr) + { + s_sharedApplication = new (std::nothrow) AppDelegate(); + } cocos2d::Application *app = cocos2d::Application::getInstance(); // Initialize the GLView attributes @@ -127,6 +131,11 @@ static AppDelegate s_sharedApplication; Called when the application is about to terminate. See also applicationDidEnterBackground:. */ + if (s_sharedApplication != nullptr) + { + delete s_sharedApplication; + s_sharedApplication = nullptr; + } } diff --git a/native/templates/js-template-default/frameworks/runtime-src/Classes/AppDelegate.cpp b/native/templates/js-template-default/frameworks/runtime-src/Classes/AppDelegate.cpp index be204540a6..0a3b74df34 100755 --- a/native/templates/js-template-default/frameworks/runtime-src/Classes/AppDelegate.cpp +++ b/native/templates/js-template-default/frameworks/runtime-src/Classes/AppDelegate.cpp @@ -74,13 +74,7 @@ bool AppDelegate::applicationDidFinishLaunching() ScriptEngineManager::getInstance()->setScriptEngine(sc); se::ScriptEngine* se = se::ScriptEngine::getInstance(); - if (!se->isValid()) - { - CCLOGERROR("Unable to Initialize se::ScriptEngine."); - return false; - } - se::AutoHandleScope hs; jsb_register_all_modules(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && PACKAGE_AS diff --git a/native/templates/js-template-default/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm b/native/templates/js-template-default/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm index 8061f93565..c25b9cca82 100755 --- a/native/templates/js-template-default/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm +++ b/native/templates/js-template-default/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm @@ -39,10 +39,14 @@ using namespace cocos2d; #pragma mark Application lifecycle // cocos2d application instance -static AppDelegate s_sharedApplication; +static AppDelegate* s_sharedApplication = nullptr; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + if (s_sharedApplication == nullptr) + { + s_sharedApplication = new (std::nothrow) AppDelegate(); + } cocos2d::Application *app = cocos2d::Application::getInstance(); // Initialize the GLView attributes @@ -127,6 +131,12 @@ static AppDelegate s_sharedApplication; Called when the application is about to terminate. See also applicationDidEnterBackground:. */ + + if (s_sharedApplication != nullptr) + { + delete s_sharedApplication; + s_sharedApplication = nullptr; + } } diff --git a/native/templates/js-template-link/frameworks/runtime-src/Classes/AppDelegate.cpp b/native/templates/js-template-link/frameworks/runtime-src/Classes/AppDelegate.cpp index be204540a6..0a3b74df34 100755 --- a/native/templates/js-template-link/frameworks/runtime-src/Classes/AppDelegate.cpp +++ b/native/templates/js-template-link/frameworks/runtime-src/Classes/AppDelegate.cpp @@ -74,13 +74,7 @@ bool AppDelegate::applicationDidFinishLaunching() ScriptEngineManager::getInstance()->setScriptEngine(sc); se::ScriptEngine* se = se::ScriptEngine::getInstance(); - if (!se->isValid()) - { - CCLOGERROR("Unable to Initialize se::ScriptEngine."); - return false; - } - se::AutoHandleScope hs; jsb_register_all_modules(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && PACKAGE_AS diff --git a/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm b/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm index 8061f93565..844c029ed3 100755 --- a/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm +++ b/native/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm @@ -39,10 +39,14 @@ using namespace cocos2d; #pragma mark Application lifecycle // cocos2d application instance -static AppDelegate s_sharedApplication; +static AppDelegate* s_sharedApplication = nullptr; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + if (s_sharedApplication == nullptr) + { + s_sharedApplication = new (std::nothrow) AppDelegate(); + } cocos2d::Application *app = cocos2d::Application::getInstance(); // Initialize the GLView attributes @@ -127,6 +131,11 @@ static AppDelegate s_sharedApplication; Called when the application is about to terminate. See also applicationDidEnterBackground:. */ + if (s_sharedApplication != nullptr) + { + delete s_sharedApplication; + s_sharedApplication = nullptr; + } } From c82048bdeaafce687a345083fd26ab44e2aff5ff Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 18 Aug 2017 15:32:25 +0800 Subject: [PATCH 08/11] Updates Android.mk --- .../cocos/scripting/js-bindings/proj.android/Android.mk | 4 ++-- .../anysdk/js-bindings/jsb_anysdk_basic_conversions.h | 9 +-------- native/external/config.json | 2 +- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/native/cocos/scripting/js-bindings/proj.android/Android.mk b/native/cocos/scripting/js-bindings/proj.android/Android.mk index f39fe9d0cc..d397e38b19 100644 --- a/native/cocos/scripting/js-bindings/proj.android/Android.mk +++ b/native/cocos/scripting/js-bindings/proj.android/Android.mk @@ -91,8 +91,8 @@ LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../manual \ LOCAL_EXPORT_LDLIBS := -lz LOCAL_STATIC_LIBRARIES := cocos2dx_static -LOCAL_STATIC_LIBRARIES += spidermonkey_static spidermonkey_glue_static -LOCAL_WHOLE_STATIC_LIBRARIES += v8_base_static v8_inspector_static v8_libplatform_static v8_libbase_static v8_libsampler_static v8_nosnapshot_static v8_builtins_setup_static v8_builtins_generators_static uv_static +# LOCAL_STATIC_LIBRARIES += spidermonkey_static +LOCAL_STATIC_LIBRARIES += v8_static include $(BUILD_STATIC_LIBRARY) #============================================================== diff --git a/native/extensions/anysdk/js-bindings/jsb_anysdk_basic_conversions.h b/native/extensions/anysdk/js-bindings/jsb_anysdk_basic_conversions.h index 9b466dc4c7..61851edbf2 100644 --- a/native/extensions/anysdk/js-bindings/jsb_anysdk_basic_conversions.h +++ b/native/extensions/anysdk/js-bindings/jsb_anysdk_basic_conversions.h @@ -1,5 +1,4 @@ -#ifndef __JS_BASIC_CONVERSIONS_H__ -#define __JS_BASIC_CONVERSIONS_H__ +#pragma once #include "cocos/scripting/js-bindings/jswrapper/SeApi.h" @@ -11,12 +10,6 @@ #include "ProtocolUser.h" #include "ProtocolREC.h" -#ifndef CCLOGINFO -#define CCLOGINFO(...) -#endif - // to native bool seval_to_TVideoInfo(const se::Value& v, anysdk::framework::TVideoInfo* ret); - -#endif /* __JS_ANYSDK_CONVERSIONS_H__ */ diff --git a/native/external/config.json b/native/external/config.json index fe7df4a796..6e9c81810e 100644 --- a/native/external/config.json +++ b/native/external/config.json @@ -1,5 +1,5 @@ { - "version": "v3-deps-39", + "version": "v3-deps-40", "zip_file_size": "114318593", "repo_name": "cocos2d-x-lite-external", "repo_parent": "https://github.com/cocos-creator/" From 41eae72bd81e12c51ac1b59029597d194a9c3258 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 18 Aug 2017 16:03:04 +0800 Subject: [PATCH 09/11] Adds global variable 'window' for jsc and cleanup. --- .../js-bindings/jswrapper/chakracore/ScriptEngine.cpp | 1 + .../cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.cpp | 3 ++- .../cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.cpp | 1 + .../cocos/scripting/js-bindings/jswrapper/v8/ScriptEngine.cpp | 1 + native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp | 1 - 5 files changed, 5 insertions(+), 2 deletions(-) diff --git a/native/cocos/scripting/js-bindings/jswrapper/chakracore/ScriptEngine.cpp b/native/cocos/scripting/js-bindings/jswrapper/chakracore/ScriptEngine.cpp index 8b5a305f5b..badf0d1268 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/chakracore/ScriptEngine.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/chakracore/ScriptEngine.cpp @@ -79,6 +79,7 @@ namespace se { bool ScriptEngine::init() { + cleanup(); LOGD("Initializing ChakraCore ... \n"); for (const auto& hook : _beforeInitHookArray) diff --git a/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.cpp b/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.cpp index b8f1f48d2a..e600aec837 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/jsc/ScriptEngine.cpp @@ -123,6 +123,7 @@ namespace se { _globalObj = Object::_createJSObject(nullptr, globalObj); _globalObj->root(); + _globalObj->setProperty("window", se::Value(_globalObj)); JSStringRef propertyName = JSStringCreateWithUTF8CString("log"); JSObjectSetProperty(_cx, globalObj, propertyName, JSObjectMakeFunctionWithCallback(_cx, propertyName, __log), kJSPropertyAttributeReadOnly, nullptr); @@ -188,7 +189,7 @@ namespace se { NativePtrToObjectMap::destroy(); NonRefNativePtrCreatedByCtorMap::destroy(); - LOGD("ScriptEngine::cleanup end ..."); + LOGD("ScriptEngine::cleanup end ...\n"); } std::string ScriptEngine::_formatException(JSValueRef exception) diff --git a/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.cpp b/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.cpp index 0ffdc4390a..d165f8b752 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.cpp @@ -256,6 +256,7 @@ namespace se { bool ScriptEngine::init() { + cleanup(); LOGD("Initializing SpiderMonkey \n"); for (const auto& hook : _beforeInitHookArray) diff --git a/native/cocos/scripting/js-bindings/jswrapper/v8/ScriptEngine.cpp b/native/cocos/scripting/js-bindings/jswrapper/v8/ScriptEngine.cpp index 17a8f0e81f..24902d35c2 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/v8/ScriptEngine.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/v8/ScriptEngine.cpp @@ -139,6 +139,7 @@ namespace se { bool ScriptEngine::init() { + cleanup(); LOGD("Initializing V8\n"); for (const auto& hook : _beforeInitHookArray) diff --git a/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp b/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp index a93ad485d1..2fb4b143e4 100644 --- a/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp +++ b/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp @@ -194,6 +194,5 @@ int ScriptingCore::handleNodeEvent(void* data) void ScriptingCore::restartVM() { se::ScriptEngine::getInstance()->cleanup(); - se::ScriptEngine::getInstance()->init(); Application::getInstance()->applicationDidFinishLaunching(); } From 5d01973c259fb619b86b448b17770c91ec8f0bac Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 18 Aug 2017 16:05:15 +0800 Subject: [PATCH 10/11] Updates config.json. --- native/external/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/native/external/config.json b/native/external/config.json index 6e9c81810e..8dafb05d9d 100644 --- a/native/external/config.json +++ b/native/external/config.json @@ -1,5 +1,5 @@ { - "version": "v3-deps-40", + "version": "v3-deps-41", "zip_file_size": "114318593", "repo_name": "cocos2d-x-lite-external", "repo_parent": "https://github.com/cocos-creator/" From a5279993b984b0493ad99e0f8333a00280395307 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 18 Aug 2017 16:01:38 +0800 Subject: [PATCH 11/11] updates project configuration for win32. --- .../js-bindings/proj.win32/libjscocos2d.vcxproj | 4 ++++ .../proj.win32/libjscocos2d.vcxproj.filters | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/native/cocos/scripting/js-bindings/proj.win32/libjscocos2d.vcxproj b/native/cocos/scripting/js-bindings/proj.win32/libjscocos2d.vcxproj index de9542a49e..e1d502802d 100644 --- a/native/cocos/scripting/js-bindings/proj.win32/libjscocos2d.vcxproj +++ b/native/cocos/scripting/js-bindings/proj.win32/libjscocos2d.vcxproj @@ -38,6 +38,7 @@ + @@ -56,6 +57,7 @@ + @@ -87,6 +89,7 @@ + @@ -114,6 +117,7 @@ + diff --git a/native/cocos/scripting/js-bindings/proj.win32/libjscocos2d.vcxproj.filters b/native/cocos/scripting/js-bindings/proj.win32/libjscocos2d.vcxproj.filters index 79f5b4ad41..4dbe9d02cd 100644 --- a/native/cocos/scripting/js-bindings/proj.win32/libjscocos2d.vcxproj.filters +++ b/native/cocos/scripting/js-bindings/proj.win32/libjscocos2d.vcxproj.filters @@ -155,6 +155,12 @@ jswrapper\v8 + + jswrapper + + + manual + @@ -325,5 +331,11 @@ jswrapper\v8 + + jswrapper + + + manual + \ No newline at end of file