Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion external/Java.Interop/build-tools/jnienv-gen/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ abstract class TypeInfo
static readonly Dictionary<string, TypeInfo> types = new Dictionary<string, TypeInfo> () {
{ "jvalue*", new BuiltinTypeInfo ("jvalue*", "JniArgumentValue*") },
{ "jbyte", new BuiltinTypeInfo ("jbyte", "sbyte") },
{ "jchar", new BuiltinTypeInfo ("jchar", "char") },
{ "jchar", new CharTypeInfo ("jchar") },
{ "jchar*", new BuiltinTypeInfo ("jchar*", "char*") },
{ "jshort", new BuiltinTypeInfo ("jshort", "short") },
{ "jsize", new BuiltinTypeInfo ("jsize", "int") },
Expand Down Expand Up @@ -680,6 +680,34 @@ public override string GetManagedToMarshalExpression (string variable)
}
}

class CharTypeInfo : TypeInfo {

public CharTypeInfo (string jni)
: base (jni)
{
}

public override string GetMarshalType (bool isReturn, bool isPinvoke)
{
return isPinvoke ? "ushort" : "char";
}

public override string GetManagedType (bool isReturn, bool isPinvoke)
{
return "char";
}

public override string GetManagedToMarshalExpression (string variable)
{
return $"(ushort) {variable}";
}

public override string[] GetMarshalToManagedStatements (string variable, JniFunction entry)
{
return [$"return (char) {variable};"];
}
}

class StringTypeInfo : TypeInfo {

public StringTypeInfo (string jni)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;

using Android.Runtime;
using Android.Text;

using NUnit.Framework;

Expand All @@ -17,5 +18,34 @@ public void ToLocalJniHandle ()
JNIEnv.DeleteLocalRef (p);
}
}

// JNI jchar is an unsigned 16-bit value; the tests below verify both Java implementations preserve all bits
// through the generated managed char API.
[Test]
public void JavaLangStringCharAtPreservesNonAsciiCharacters ()
{
const string value = "A\u00E4\u0100\u200B\u4E2D\uFFFF";

using (var javaString = new Java.Lang.String (value))
AssertCharAt (javaString, value);
}

[Test]
public void SpannableStringBuilderCharAtPreservesNonAsciiCharacters ()
{
const string value = "A\u00E4\u0100\u200B\u4E2D\uFFFF";

using (var builder = new SpannableStringBuilder (value))
AssertCharAt (builder, value);
}

static void AssertCharAt (Java.Lang.ICharSequence actual, string expected)
{
for (int i = 0; i < expected.Length; i++) {
// The char local verifies the managed projection, while the value verifies the 16-bit JNI ABI.
char actualChar = actual.CharAt (i);
Assert.AreEqual (expected [i], actualChar, $"Character at index {i}: expected U+{(int) expected [i]:X4}, actual U+{(int) actualChar:X4}");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ public void TestMyPaintColor ()
}
}

[Test]
public void CharFieldPreservesNonAsciiCharacters ()
{
const char expected = '\uFFFF';

// Setting Value covers jchar input through SetCharField; reading it back covers GetCharField.
using (var value = new Android.Util.MutableChar ('\0')) {
value.Value = expected;
Assert.AreEqual (expected, value.Value);
}
}

[DllImport ("reuse-threads")]
static extern int rt_register_type_on_new_thread (string java_type_namem, IntPtr class_loader);

Expand Down
Loading