diff --git a/native/cocos/bindings/event/EventDispatcher.cpp b/native/cocos/bindings/event/EventDispatcher.cpp index 667595ccad..63f8ad1230 100644 --- a/native/cocos/bindings/event/EventDispatcher.cpp +++ b/native/cocos/bindings/event/EventDispatcher.cpp @@ -26,6 +26,7 @@ #include #include "cocos/application/ApplicationManager.h" #include "cocos/bindings/event/CustomEventTypes.h" +#include "cocos/bindings/jswrapper/HandleObject.h" #include "cocos/bindings/jswrapper/SeApi.h" #include "cocos/bindings/manual/jsb_global_init.h" #include "cocos/platform/interfaces/modules/ISystemWindow.h" @@ -39,8 +40,17 @@ se::Object *jsMouseEventObj = nullptr; se::Object *jsKeyboardEventObj = nullptr; se::Object *jsControllerEventArray = nullptr; se::Object *jsResizeEventObj = nullptr; -se::Object *jsOrientationEventObj = nullptr; bool inited = false; + +// attach the argument object to the function +void accessCacheArgObj(se::Object *func, se::Value *argObj, const char *cacheKey = "__reusedArgumentObject") { + func->getProperty(cacheKey, argObj); + if (argObj->isUndefined()) { + se::HandleObject argumentObj(se::Object::createPlainObject()); + argObj->setObject(argumentObj); + } +} + } // namespace namespace cc { @@ -97,11 +107,6 @@ void EventDispatcher::destroy() { jsResizeEventObj = nullptr; } - if (jsOrientationEventObj != nullptr) { - jsOrientationEventObj->unroot(); - jsOrientationEventObj->decRef(); - jsOrientationEventObj = nullptr; - } inited = false; tickVal.setUndefined(); } @@ -255,7 +260,7 @@ void EventDispatcher::dispatchControllerEvent(const ControllerEvent &controllerE if (controllerEvent.type == ControllerEvent::Type::HANDLE) { eventName = "onHandleInput"; } - uint32_t controllerIndex = 0; + uint32_t controllerIndex = 0; jsControllerEventArray->setProperty("length", se::Value(static_cast(controllerEvent.controllerInfos.size()))); for (const auto &controller : controllerEvent.controllerInfos) { @@ -264,7 +269,7 @@ void EventDispatcher::dispatchControllerEvent(const ControllerEvent &controllerE se::HandleObject jsButtonInfoList{se::Object::createArrayObject(static_cast(controller->buttonInfos.size()))}; - uint32_t buttonIndex = 0; + uint32_t buttonIndex = 0; for (const auto &buttonInfo : controller->buttonInfos) { se::HandleObject jsButtonInfo{se::Object::createPlainObject()}; jsButtonInfo->setProperty("code", se::Value(static_cast(buttonInfo.key))); @@ -339,18 +344,15 @@ void EventDispatcher::dispatchOrientationChangeEvent(int orientation) { se::AutoHandleScope scope; CC_ASSERT(inited); - if (jsOrientationEventObj == nullptr) { - jsOrientationEventObj = se::Object::createPlainObject(); - jsOrientationEventObj->root(); - } - se::Value func; __jsbObj->getProperty("onOrientationChanged", &func); if (func.isObject() && func.toObject()->isFunction()) { - jsOrientationEventObj->setProperty("orientation", se::Value(orientation)); + se::Value evtObj; + accessCacheArgObj(func.toObject(), &evtObj); + evtObj.toObject()->setProperty("orientation", se::Value(orientation)); se::ValueArray args; - args.emplace_back(se::Value(jsOrientationEventObj)); + args.emplace_back(evtObj); func.toObject()->call(args, nullptr); } } @@ -490,7 +492,7 @@ void EventDispatcher::removeAllEventListeners() { delete node.second; } listeners.clear(); - //start from 1 cuz 0 represents pause and resume + // start from 1 cuz 0 represents pause and resume hashListenerId = 1; }