Skip to content

Commit

Permalink
Make JNI string conversions behave like ObjC string conversions
Browse files Browse the repository at this point in the history
Return empty string if conversion fails
  • Loading branch information
jb-gcx committed Jun 29, 2024
1 parent 9a74ee9 commit b65a7b1
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions support-lib/jni/djinni_support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,12 @@ using WcharConverter = std::wstring_convert<std::codecvt_utf16<wchar_t, 0x10ffff
using Utf8Converter = std::wstring_convert<std::codecvt_utf8_utf16<char16_t, 0x10ffff, std::codecvt_mode::little_endian>, char16_t>;

jstring jniStringFromWString(JNIEnv * env, const std::wstring & str) {
std::string u16 = WcharConverter{}.to_bytes(str);
std::string u16{};
try {
u16 = WcharConverter{}.to_bytes(str);
} catch (...) {
// Conversion error - empty string will be returned
}
jstring res = env->NewString(reinterpret_cast<const jchar*>(u16.data()), u16.size()/sizeof(jchar));
DJINNI_ASSERT(res, env);
return res;
Expand All @@ -457,7 +462,12 @@ std::wstring jniWStringFromString(JNIEnv * env, const jstring jstr) {
}

jstring jniStringFromUTF8(JNIEnv * env, const std::string & str) {
std::u16string u16 = Utf8Converter{}.from_bytes(str);
std::u16string u16{};
try {
u16 = Utf8Converter{}.from_bytes(str);
} catch (...) {
// Conversion error - empty string will be returned
}
jstring res = env->NewString(reinterpret_cast<const jchar*>(u16.data()), u16.size());
DJINNI_ASSERT(res, env);
return res;
Expand Down

0 comments on commit b65a7b1

Please sign in to comment.