From 0b057318a44ea866f3310aea5ad6631a9da8fc43 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Mon, 4 Sep 2017 13:46:37 +0800 Subject: [PATCH] Avoid useless function check in executeFunctionWithOwner and useless js parameter construction in websocket --- .../js-bindings/manual/ScriptingCore.cpp | 33 ++-- .../manual/network/jsb_websocket.cpp | 147 ++++++++++-------- 2 files changed, 96 insertions(+), 84 deletions(-) diff --git a/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp b/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp index 4587b3edec..c2bdc31560 100644 --- a/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp +++ b/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp @@ -1670,24 +1670,25 @@ bool ScriptingCore::executeFunctionWithOwner(JS::HandleValue owner, const char * JS::RootedValue ownerval(cx, owner); JS::RootedObject obj(cx); if (ownerval.isObject()) - obj = ownerval.toObjectOrNull(); - - do { - if (JS_HasProperty(cx, obj, name, &hasFunc) && hasFunc) { - if (!JS_GetProperty(cx, obj, name, &funcVal)) { - break; + obj = ownerval.toObjectOrNull(); + do + { + if (JS_HasProperty(cx, obj, name, &hasFunc) && hasFunc) { + if (!JS_GetProperty(cx, obj, name, &funcVal)) { + break; + } + if (funcVal.isNullOrUndefined()) { + break; + } + + bRet = JS_CallFunctionValue(cx, obj, funcVal, args, retVal); + if (JS_IsExceptionPending(cx)) { + handlePendingException(cx); + } } - if (funcVal.isNullOrUndefined()) { - break; - } - - bRet = JS_CallFunctionValue(cx, obj, funcVal, args, retVal); - if (JS_IsExceptionPending(cx)) { - handlePendingException(cx); - } - } - }while(0); + } while(0); + } return bRet; } diff --git a/native/cocos/scripting/js-bindings/manual/network/jsb_websocket.cpp b/native/cocos/scripting/js-bindings/manual/network/jsb_websocket.cpp index 032225fd69..cc1e5985d0 100644 --- a/native/cocos/scripting/js-bindings/manual/network/jsb_websocket.cpp +++ b/native/cocos/scripting/js-bindings/manual/network/jsb_websocket.cpp @@ -83,23 +83,25 @@ public: return; JSContext* cx = ScriptingCore::getInstance()->getGlobalContext(); - - // Set the protocol which server selects. - JS::RootedValue jsprotocol(cx); - std_string_to_jsval(cx, ws->getProtocol(), &jsprotocol); - JS::RootedObject wsObj(cx, p->obj); - JS_SetProperty(cx, wsObj, "protocol", jsprotocol); - - JS::RootedObject jsobj(cx, JS_NewPlainObject(cx)); - JS::RootedValue vp(cx); - c_string_to_jsval(cx, "open", &vp); - JS_SetProperty(cx, jsobj, "type", vp); - - JS::RootedValue jsobjVal(cx, JS::ObjectOrNullValue(jsobj)); - JS::HandleValueArray args(jsobjVal); JS::RootedValue owner(cx, JS::ObjectOrNullValue(_JSDelegate)); - - ScriptingCore::getInstance()->executeFunctionWithOwner(owner, "onopen", args); + if (owner.isObject()) + { + // Set the protocol which server selects. + JS::RootedValue jsprotocol(cx); + std_string_to_jsval(cx, ws->getProtocol(), &jsprotocol); + JS::RootedObject wsObj(cx, p->obj); + JS_SetProperty(cx, wsObj, "protocol", jsprotocol); + + JS::RootedObject jsobj(cx, JS_NewPlainObject(cx)); + JS::RootedValue vp(cx); + c_string_to_jsval(cx, "open", &vp); + JS_SetProperty(cx, jsobj, "type", vp); + + JS::RootedValue jsobjVal(cx, JS::ObjectOrNullValue(jsobj)); + JS::HandleValueArray args(jsobjVal); + + ScriptingCore::getInstance()->executeFunctionWithOwner(owner, "onopen", args); + } } virtual void onMessage(WebSocket* ws, const WebSocket::Data& data) @@ -111,46 +113,49 @@ public: return; JSContext* cx = ScriptingCore::getInstance()->getGlobalContext(); - JS::RootedObject jsobj(cx, JS_NewPlainObject(cx)); - JS::RootedValue vp(cx); - c_string_to_jsval(cx, "message", &vp); - JS_SetProperty(cx, jsobj, "type", vp); - - JS::RootedValue arg(cx, JS::ObjectOrNullValue(jsobj)); - bool flag; - if (data.isBinary) - {// data is binary - JS::RootedObject buffer(cx, JS_NewArrayBuffer(cx, static_cast(data.len))); - if (data.len > 0) - { - uint8_t* bufdata = JS_GetArrayBufferData(buffer, &flag, JS::AutoCheckCannotGC()); - memcpy((void*)bufdata, (void*)data.bytes, data.len); - } - JS::RootedValue dataVal(cx, JS::ObjectOrNullValue(buffer)); - JS_SetProperty(cx, jsobj, "data", dataVal); - } - else - {// data is string - JS::RootedValue dataVal(cx); - if (strlen(data.bytes) == 0 && data.len > 0) - {// String with 0x00 prefix - dataVal = JS::StringValue(JS_NewStringCopyN(cx, data.bytes, data.len)); + JS::RootedValue delegate(cx, JS::ObjectOrNullValue(_JSDelegate)); + if (delegate.isObject()) + { + JS::RootedObject jsobj(cx, JS_NewPlainObject(cx)); + JS::RootedValue vp(cx); + c_string_to_jsval(cx, "message", &vp); + JS_SetProperty(cx, jsobj, "type", vp); + + JS::RootedValue arg(cx, JS::ObjectOrNullValue(jsobj)); + bool flag; + if (data.isBinary) + {// data is binary + JS::RootedObject buffer(cx, JS_NewArrayBuffer(cx, static_cast(data.len))); + if (data.len > 0) + { + uint8_t* bufdata = JS_GetArrayBufferData(buffer, &flag, JS::AutoCheckCannotGC()); + memcpy((void*)bufdata, (void*)data.bytes, data.len); + } + JS::RootedValue dataVal(cx, JS::ObjectOrNullValue(buffer)); + JS_SetProperty(cx, jsobj, "data", dataVal); } else - {// Normal string - c_string_to_jsval(cx, data.bytes, &dataVal); + {// data is string + JS::RootedValue dataVal(cx); + if (strlen(data.bytes) == 0 && data.len > 0) + {// String with 0x00 prefix + dataVal = JS::StringValue(JS_NewStringCopyN(cx, data.bytes, data.len)); + } + else + {// Normal string + c_string_to_jsval(cx, data.bytes, &dataVal); + } + if (dataVal.isNullOrUndefined()) + { + ws->closeAsync(); + return; + } + JS_SetProperty(cx, jsobj, "data", dataVal); } - if (dataVal.isNullOrUndefined()) - { - ws->closeAsync(); - return; - } - JS_SetProperty(cx, jsobj, "data", dataVal); - } - JS::RootedValue delegate(cx, JS::ObjectOrNullValue(_JSDelegate)); - JS::HandleValueArray args(arg); - ScriptingCore::getInstance()->executeFunctionWithOwner(delegate, "onmessage", args); + JS::HandleValueArray args(arg); + ScriptingCore::getInstance()->executeFunctionWithOwner(delegate, "onmessage", args); + } } virtual void onClose(WebSocket* ws) @@ -161,15 +166,18 @@ public: if (cocos2d::Director::getInstance() != nullptr && cocos2d::Director::getInstance()->getRunningScene() && cocos2d::ScriptEngineManager::getInstance() != nullptr) { JSContext* cx = ScriptingCore::getInstance()->getGlobalContext(); - JS::RootedObject jsobj(cx, JS_NewPlainObject(cx)); - JS::RootedValue vp(cx); - c_string_to_jsval(cx, "close", &vp); - JS_SetProperty(cx, jsobj, "type", vp); - JS::RootedValue delegate(cx, JS::ObjectOrNullValue(_JSDelegate)); - JS::RootedValue arg(cx, JS::ObjectOrNullValue(jsobj)); - JS::HandleValueArray args(arg); - ScriptingCore::getInstance()->executeFunctionWithOwner(delegate, "onclose", args); + if (delegate.isObject()) + { + JS::RootedObject jsobj(cx, JS_NewPlainObject(cx)); + JS::RootedValue vp(cx); + c_string_to_jsval(cx, "close", &vp); + JS_SetProperty(cx, jsobj, "type", vp); + + JS::RootedValue arg(cx, JS::ObjectOrNullValue(jsobj)); + JS::HandleValueArray args(arg); + ScriptingCore::getInstance()->executeFunctionWithOwner(delegate, "onclose", args); + } } JS_SetPrivate(p->obj, nullptr); } @@ -186,15 +194,18 @@ public: return; JSContext* cx = ScriptingCore::getInstance()->getGlobalContext(); - JS::RootedObject jsobj(cx, JS_NewPlainObject(cx)); - JS::RootedValue vp(cx); - c_string_to_jsval(cx, "error", &vp); - JS_SetProperty(cx, jsobj, "type", vp); - JS::RootedValue delegate(cx, JS::ObjectOrNullValue(_JSDelegate)); - JS::RootedValue arg(cx, JS::ObjectOrNullValue(jsobj)); - JS::HandleValueArray args(arg); - ScriptingCore::getInstance()->executeFunctionWithOwner(delegate, "onerror", args); + if (delegate.isObject()) + { + JS::RootedObject jsobj(cx, JS_NewPlainObject(cx)); + JS::RootedValue vp(cx); + c_string_to_jsval(cx, "error", &vp); + JS_SetProperty(cx, jsobj, "type", vp); + + JS::RootedValue arg(cx, JS::ObjectOrNullValue(jsobj)); + JS::HandleValueArray args(arg); + ScriptingCore::getInstance()->executeFunctionWithOwner(delegate, "onerror", args); + } } void setJSDelegate(JS::HandleObject pJSDelegate)