Merge pull request #13186 from jianglong0156/addEmotionTest

fix getStringUTFChars bug
This commit is contained in:
pandamicro 2015-08-05 10:19:19 +08:00
commit 8cdcade07a
11 changed files with 124 additions and 57 deletions

View File

@ -155,6 +155,17 @@ bool UTF16ToUTF8(const std::u16string& utf16, std::string& outUtf8)
return llvm::convertUTF16ToUTF8String(utf16, outUtf8); return llvm::convertUTF16ToUTF8String(utf16, outUtf8);
} }
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
CC_DLL bool getUTFCharsFromJavaEnv(JNIEnv* env, jstring srcjStr, std::string& outUtf8)
{
const unsigned short * unicodeChar = ( const unsigned short *)env->GetStringChars(srcjStr, nullptr);
const std::u16string unicodeStr((const char16_t *)unicodeChar);
bool flag = UTF16ToUTF8(unicodeStr, outUtf8);
env->ReleaseStringChars(srcjStr, unicodeChar);
return flag;
}
#endif
std::vector<char16_t> getChar16VectorFromUTF16String(const std::u16string& utf16) std::vector<char16_t> getChar16VectorFromUTF16String(const std::u16string& utf16)
{ {
std::vector<char16_t> ret; std::vector<char16_t> ret;

View File

@ -30,6 +30,10 @@
#include <vector> #include <vector>
#include <string> #include <string>
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "platform/android/jni/JniHelper.h"
#endif
NS_CC_BEGIN NS_CC_BEGIN
namespace StringUtils { namespace StringUtils {
@ -68,6 +72,10 @@ CC_DLL bool UTF8ToUTF16(const std::string& utf8, std::u16string& outUtf16);
*/ */
CC_DLL bool UTF16ToUTF8(const std::u16string& utf16, std::string& outUtf8); CC_DLL bool UTF16ToUTF8(const std::u16string& utf16, std::string& outUtf8);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
CC_DLL bool getUTFCharsFromJavaEnv(JNIEnv* env, jstring srcjStr, std::string& outUtf8);
#endif
/** /**
* @brief Trims the unicode spaces at the end of char16_t vector. * @brief Trims the unicode spaces at the end of char16_t vector.
*/ */

View File

@ -35,6 +35,8 @@
#include "platform/CCFileUtils.h" #include "platform/CCFileUtils.h"
#include "platform/android/jni/JniHelper.h" #include "platform/android/jni/JniHelper.h"
#include "base/ccUTF8.h"
NS_CC_BEGIN NS_CC_BEGIN
namespace network { namespace network {
@ -579,17 +581,13 @@ private:
{ {
return nullptr; return nullptr;
} }
char *ret = nullptr;
const char* str = nullptr; std::string strValue = "";
char* ret = nullptr; if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(env, jstr, strValue))
str = env->GetStringUTFChars(jstr, nullptr);
if (nullptr != str)
{ {
ret = strdup(str); strValue = "";
} }
ret = strdup(strValue.c_str());
env->ReleaseStringUTFChars(jstr, str);
return ret; return ret;
} }
@ -715,7 +713,10 @@ void HttpClient::processResponse(HttpResponse* response, char* responseMessage)
} }
free(contentInfo); free(contentInfo);
strcpy(responseMessage, urlConnection.getResponseMessage()); char *messageInfo = urlConnection.getResponseMessage();
strcpy(responseMessage, messageInfo);
free(messageInfo);
urlConnection.disconnect(); urlConnection.disconnect();
// write data to HttpResponse // write data to HttpResponse

View File

@ -29,6 +29,7 @@ THE SOFTWARE.
#include "../CCApplication.h" #include "../CCApplication.h"
#include "platform/CCFileUtils.h" #include "platform/CCFileUtils.h"
#include <jni.h> #include <jni.h>
#include "base/ccUTF8.h"
using namespace cocos2d; using namespace cocos2d;
@ -59,10 +60,11 @@ std::string getStringWithEllipsisJni(const char* text, float width, float fontSi
} }
jstring retFromJava = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, stringArg1, width, fontSize); jstring retFromJava = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, stringArg1, width, fontSize);
const char* str = t.env->GetStringUTFChars(retFromJava, 0); if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(t.env, retFromJava, ret))
ret = str; {
ret = "";
}
t.env->ReleaseStringUTFChars(retFromJava, str);
t.env->DeleteLocalRef(stringArg1); t.env->DeleteLocalRef(stringArg1);
t.env->DeleteLocalRef(t.classID); t.env->DeleteLocalRef(t.classID);
} }

View File

@ -8,6 +8,8 @@
#include "JniHelper.h" #include "JniHelper.h"
#include <jni.h> #include <jni.h>
#include "base/ccUTF8.h"
using namespace cocos2d; using namespace cocos2d;
extern "C" { extern "C" {
@ -33,9 +35,13 @@ extern "C" {
} }
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInsertText(JNIEnv* env, jobject thiz, jstring text) { JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInsertText(JNIEnv* env, jobject thiz, jstring text) {
const char* pszText = env->GetStringUTFChars(text, NULL); std::string strValue = "";
if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(env, text, strValue))
{
strValue = "";
}
const char* pszText = strValue.c_str();
cocos2d::IMEDispatcher::sharedDispatcher()->dispatchInsertText(pszText, strlen(pszText)); cocos2d::IMEDispatcher::sharedDispatcher()->dispatchInsertText(pszText, strlen(pszText));
env->ReleaseStringUTFChars(text, pszText);
} }
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeDeleteBackward(JNIEnv* env, jobject thiz) { JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeDeleteBackward(JNIEnv* env, jobject thiz) {

View File

@ -27,6 +27,8 @@ THE SOFTWARE.
#include <string.h> #include <string.h>
#include <pthread.h> #include <pthread.h>
#include "base/ccUTF8.h"
#define LOG_TAG "JniHelper" #define LOG_TAG "JniHelper"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
@ -265,14 +267,16 @@ namespace cocos2d {
JNIEnv *env = JniHelper::getEnv(); JNIEnv *env = JniHelper::getEnv();
if (!env) { if (!env) {
return nullptr; return "";
} }
const char* chars = env->GetStringUTFChars(jstr, nullptr); std::string strValue = "";
std::string ret(chars); if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(env, jstr, strValue))
env->ReleaseStringUTFChars(jstr, chars); {
strValue = "";
}
return ret; return strValue;
} }
} //namespace cocos2d } //namespace cocos2d

View File

@ -27,6 +27,7 @@
#include "spidermonkey_specifics.h" #include "spidermonkey_specifics.h"
#include "ScriptingCore.h" #include "ScriptingCore.h"
#include "js_manual_conversions.h" #include "js_manual_conversions.h"
#include "base/ccUTF8.h"
#define LOG_TAG "CCJavascriptJavaBridge" #define LOG_TAG "CCJavascriptJavaBridge"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
@ -38,10 +39,14 @@ extern "C" {
JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxJavascriptJavaBridge_evalString JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxJavascriptJavaBridge_evalString
(JNIEnv *env, jclass cls, jstring value) (JNIEnv *env, jclass cls, jstring value)
{ {
const char *_value = env->GetStringUTFChars(value, NULL);
ScriptingCore::getInstance()->evalString(_value,NULL);
env->ReleaseStringUTFChars(value, _value);
std::string strValue = "";
if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(env, value, strValue))
{
CCLOG("Cocos2dxJavascriptJavaBridge_evalString error, invalid string code");
return 0;
}
ScriptingCore::getInstance()->evalString(strValue.c_str(), nullptr);
return 1; return 1;
} }
@ -79,9 +84,13 @@ bool JavascriptJavaBridge::CallInfo::execute(void)
case TypeString: case TypeString:
m_retjstring = (jstring)m_env->CallStaticObjectMethod(m_classID, m_methodID); m_retjstring = (jstring)m_env->CallStaticObjectMethod(m_classID, m_methodID);
const char *stringBuff = m_env->GetStringUTFChars(m_retjstring, 0); std::string strValue = "";
m_ret.stringValue = new string(stringBuff); if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(m_env, m_retjstring, strValue))
m_env->ReleaseStringUTFChars(m_retjstring, stringBuff); {
strValue = "";
}
m_ret.stringValue = new string(strValue);
break; break;
} }
@ -119,9 +128,12 @@ bool JavascriptJavaBridge::CallInfo::executeWithArgs(jvalue *args)
case TypeString: case TypeString:
m_retjstring = (jstring)m_env->CallStaticObjectMethodA(m_classID, m_methodID, args); m_retjstring = (jstring)m_env->CallStaticObjectMethodA(m_classID, m_methodID, args);
const char *stringBuff = m_env->GetStringUTFChars(m_retjstring, 0); std::string strValue = "";
m_ret.stringValue = new string(stringBuff); if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(m_env, m_retjstring, strValue))
m_env->ReleaseStringUTFChars(m_retjstring, stringBuff); {
strValue = "";
}
m_ret.stringValue = new string(strValue);
break; break;
} }

View File

@ -2,6 +2,7 @@
#include "CCLuaJavaBridge.h" #include "CCLuaJavaBridge.h"
#include "platform/android/jni/JniHelper.h" #include "platform/android/jni/JniHelper.h"
#include <android/log.h> #include <android/log.h>
#include "base/ccUTF8.h"
#define LOG_TAG "luajc" #define LOG_TAG "luajc"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
@ -40,9 +41,12 @@ bool LuaJavaBridge::CallInfo::execute(void)
case TypeString: case TypeString:
m_retjs = (jstring)m_env->CallStaticObjectMethod(m_classID, m_methodID); m_retjs = (jstring)m_env->CallStaticObjectMethod(m_classID, m_methodID);
const char *stringBuff = m_env->GetStringUTFChars(m_retjs, 0); std::string strValue = "";
m_ret.stringValue = new string(stringBuff); if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(m_env, m_retjs, strValue))
m_env->ReleaseStringUTFChars(m_retjs, stringBuff); {
strValue = "";
}
m_ret.stringValue = new string(strValue);
break; break;
} }
@ -80,9 +84,12 @@ bool LuaJavaBridge::CallInfo::executeWithArgs(jvalue *args)
case TypeString: case TypeString:
m_retjs = (jstring)m_env->CallStaticObjectMethodA(m_classID, m_methodID, args); m_retjs = (jstring)m_env->CallStaticObjectMethodA(m_classID, m_methodID, args);
const char *stringBuff = m_env->GetStringUTFChars(m_retjs, 0); std::string strValue = "";
m_ret.stringValue = new string(stringBuff); if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(m_env, m_retjs, strValue))
m_env->ReleaseStringUTFChars(m_retjs, stringBuff); {
strValue = "";
}
m_ret.stringValue = new string(strValue);
break; break;
} }

View File

@ -5,6 +5,8 @@
#include "CCLuaJavaBridge.h" #include "CCLuaJavaBridge.h"
#include "base/ccUTF8.h"
#define LOG_TAG "Cocos2dxLuaJavaBridge_java" #define LOG_TAG "Cocos2dxLuaJavaBridge_java"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
@ -13,20 +15,23 @@ extern "C" {
JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge_callLuaFunctionWithString JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge_callLuaFunctionWithString
(JNIEnv *env, jclass cls, jint functionId, jstring value) (JNIEnv *env, jclass cls, jint functionId, jstring value)
{ {
const char *value_ = env->GetStringUTFChars(value, 0); std::string strValue = "";
int ret = LuaJavaBridge::callLuaFunctionById(functionId, value_); if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(env, value, strValue))
env->ReleaseStringUTFChars(value, value_); {
strValue = "";
}
int ret = LuaJavaBridge::callLuaFunctionById(functionId, strValue.c_str());
return ret; return ret;
} }
JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge_callLuaGlobalFunctionWithString JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge_callLuaGlobalFunctionWithString
(JNIEnv *env, jclass cls, jstring luaFunctionName, jstring value) (JNIEnv *env, jclass cls, jstring luaFunctionName, jstring value)
{ {
const char *luaFunctionName_ = env->GetStringUTFChars(luaFunctionName, 0); std::string functionNameStr = "";
const char *value_ = env->GetStringUTFChars(value, 0); std::string valueStr = "";
int ret = LuaJavaBridge::callLuaGlobalFunction(luaFunctionName_, value_); cocos2d::StringUtils::getUTFCharsFromJavaEnv(env, luaFunctionName, functionNameStr);
env->ReleaseStringUTFChars(luaFunctionName, luaFunctionName_); cocos2d::StringUtils::getUTFCharsFromJavaEnv(env, value, valueStr);
env->ReleaseStringUTFChars(value, value_); int ret = LuaJavaBridge::callLuaGlobalFunction(functionNameStr.c_str(), valueStr.c_str());
return ret; return ret;
} }

View File

@ -108,6 +108,13 @@ public class AppActivity extends Cocos2dxActivity {
public static String getUtfStr() { public static String getUtfStr() {
final String utf8Str = "you will see emotion:💝"; final String utf8Str = "you will see emotion:💝";
app.runOnGLThread(new Runnable() {
@Override
public void run() {
Cocos2dxJavascriptJavaBridge.evalString("cc.log(\"" + utf8Str + "\")");
}
});
return utf8Str; return utf8Str;
} }

View File

@ -28,41 +28,45 @@
var ReflectionTestLayer = BaseTestLayer.extend({ var ReflectionTestLayer = BaseTestLayer.extend({
_title:"jsb.reflection", _title:"jsb.reflection",
_subtitle:"call java/objective-c methods in js", _subtitle:"call java/objective-c methods in js",
onRestartCallback:function (sender) {
new ReflectionTestScene().runThisTest();
},
ctor:function() { ctor:function() {
this._super(cc.color(0,0,0,255), cc.color(98,99,117,255)); this._super(cc.color(0,0,0,255), cc.color(98,99,117,255));
var label = new cc.LabelTTF("Show Alert Dialog", "Arial", 35); var resultLabel = new cc.LabelTTF("Show emotion result", "Arial", 20);
resultLabel.setPosition(cc.p(cc.winSize.width / 2, cc.winSize.height - 130));
this.addChild(resultLabel);
var label = new cc.LabelTTF("Show Alert Dialog", "Arial", 30);
var menuItem = new cc.MenuItemLabel(label, function(){ var menuItem = new cc.MenuItemLabel(label, function(){
if(cc.sys.os == cc.sys.OS_ANDROID){ if(cc.sys.os == cc.sys.OS_ANDROID){
jsb.reflection.callStaticMethod("org/cocos2dx/js_tests/AppActivity", "showAlertDialog", "(Ljava/lang/String;Ljava/lang/String;)V", "How are you ?", "I'm great !"); jsb.reflection.callStaticMethod("org/cocos2dx/js_tests/AppActivity", "showAlertDialog", "(Ljava/lang/String;Ljava/lang/String;)V", "How are you ?", "I'm great !");
}else if(cc.sys.os == cc.sys.OS_IOS || cc.sys.os == cc.sys.OS_OSX){ }else if(cc.sys.os == cc.sys.OS_IOS || cc.sys.os == cc.sys.OS_OSX){
var ret = jsb.reflection.callStaticMethod("NativeOcClass","callNativeUIWithTitle:andContent:","cocos2d-js","Yes! you call a Native UI from Reflection"); var ret = jsb.reflection.callStaticMethod("NativeOcClass","callNativeUIWithTitle:andContent:","cocos2d-js","Yes! you call a Native UI from Reflection");
cc.log("ret val is "+ret); resultLabel.setString("ret val is "+ret);
} }
}, this); }, this);
var label2 = new cc.LabelTTF("CallReflectionWithBool", "Arial", 35); var label2 = new cc.LabelTTF("CallReflectionWithBool", "Arial", 30);
var menuItem2 = new cc.MenuItemLabel(label2, function(){ var menuItem2 = new cc.MenuItemLabel(label2, function(){
if(cc.sys.os == cc.sys.OS_ANDROID){ if(cc.sys.os == cc.sys.OS_ANDROID){
jsb.reflection.callStaticMethod("org/cocos2dx/js_tests/AppActivity", "showAlertDialog", "(Ljava/lang/String;Ljava/lang/String;Z)V", "How are you ?", "I'm great !", true); jsb.reflection.callStaticMethod("org/cocos2dx/js_tests/AppActivity", "showAlertDialog", "(Ljava/lang/String;Ljava/lang/String;Z)V", "How are you ?", "I'm great !", true);
}else if(cc.sys.os == cc.sys.OS_IOS || cc.sys.os == cc.sys.OS_OSX){ }else if(cc.sys.os == cc.sys.OS_IOS || cc.sys.os == cc.sys.OS_OSX){
var ret = jsb.reflection.callStaticMethod("NativeOcClass","callNativeUIWithTitle:andContent:addBool:","cocos2d-js","Yes! you call a Native UI from Reflection", true); var ret = jsb.reflection.callStaticMethod("NativeOcClass","callNativeUIWithTitle:andContent:addBool:","cocos2d-js","Yes! you call a Native UI from Reflection", true);
cc.log("ret val is "+ret); resultLabel.setString("ret val is "+ret);
} }
}, this); }, this);
var label3 = new cc.LabelTTF("CallReflectionWithUTF8Str", "Arial", 35); var label3 = new cc.LabelTTF("CallReflectionWithUTF8Str", "Arial", 30);
var menuItem3 = new cc.MenuItemLabel(label3, function(){ var menuItem3 = new cc.MenuItemLabel(label3, function(){
if(cc.sys.os == cc.sys.OS_ANDROID){ if(cc.sys.os == cc.sys.OS_ANDROID){
var ret = jsb.reflection.callStaticMethod("org/cocos2dx/js_tests/AppActivity", "getUtfStr", "()Ljava/lang/String;"); var ret = jsb.reflection.callStaticMethod("org/cocos2dx/js_tests/AppActivity", "getUtfStr", "()Ljava/lang/String;");
jsb.reflection.callStaticMethod("org/cocos2dx/js_tests/AppActivity", "showAlertDialog", resultLabel.setString("result:" + ret);
"(Ljava/lang/String;Ljava/lang/String;)V", "Show Emotion", ret);
cc.log("result:" + ret);
}else if(cc.sys.os == cc.sys.OS_IOS || cc.sys.os == cc.sys.OS_OSX){ }else if(cc.sys.os == cc.sys.OS_IOS || cc.sys.os == cc.sys.OS_OSX){
var ret = "you will see emotion:💝"; var ret = "you will see emotion:💝";
jsb.reflection.callStaticMethod("NativeOcClass","callNativeUIWithTitle:andContent:","Show Emotion", ret); jsb.reflection.callStaticMethod("NativeOcClass","callNativeUIWithTitle:andContent:","Show Emotion", ret);
cc.log("result:" + ret); resultLabel.setString("result:" + ret);
} }
}, this); }, this);
var menu = new cc.Menu(menuItem, menuItem2, menuItem3); var menu = new cc.Menu(menuItem, menuItem2, menuItem3);