Repository navigation
fix(runtime): convert Java char values as UTF-16 code units - #2140
Open
edusperoni wants to merge 3 commits into
Open
edusperoni wants to merge 3 commits into
edusperoni wants to merge 3 commits into
Conversation
Several JS-to-Java char conversions went through UTF-8 and kept a single
byte, so a non-ASCII value reached Java wrong: char("é") as a method
argument arrived as 195 and char("€") as 226, the same happened for char
fields and for char() values returned from JS callbacks, and a JS array
converted to char[] or a char[] element set reinterpreted the first two
UTF-8 bytes as one UTF-16 unit before taking a byte of it ("é" became
234, "€" 232). Reading a char[] element handed V8 one UTF-8 byte and
produced U+FFFD.
A plain JS string passed to a non-overloaded char parameter was
converted to a java.lang.String whose local reference was then read
through the jvalue's char member, so even "x" arrived as an arbitrary
value.
These paths now share ArgConverter::ToJavaChar, which takes the first
UTF-16 code unit of the JS string (U+0000 for an empty string, the high
surrogate for a character outside the BMP), and the char[] element read
uses jcharToV8String like the method-return, field-get and callback
argument paths already did. Affected: char() cast arguments, plain
string arguments to char parameters, JS array to char[], char[] element
get and set, char field set, and char() values returned from JS
callbacks.
GetJavaField escapes every result from its EscapableHandleScope except the boolean one, which it returned as a handle created inside the closed scope. V8's Boolean::New returns a root-table slot today, so the handle stays valid in practice, but the API does not promise that; escape it like the other branches.
…d reads Specs pass U+00E9, U+20AC, U+4E2D, a lone high surrogate, an ASCII control and ASCII through every char path: char() and plain string arguments, char return values, JS array to char[], char[] element get and set, instance and static char fields, and a char() value returned from a JS callback. Java-side values are read back as ints and the element read uses a Java-created char[], so a broken direction cannot be masked by the opposite one. They also pin the empty-string (U+0000) and multi-unit (first code unit) behaviour, and read instance and static boolean fields.
|
Note Currently processing new changes in this PR. This may take a few minutes, please wait...
|
edusperoni
marked this pull request as ready for review
November 21, 2026 18:41
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Description
Fixes Java
charconversion for non-ASCII values, and escapes one un-escaped handle inFieldAccessor::GetJavaField.Bug 1:
charvalues went through UTF-8Several JS→Java
charconversions turned the JS string into UTF-8 and kept a single byte, so any non-ASCII value reached Java wrong. Observed onmainwith the new specs:char("é")/char("€")method argument (JsArgConverter,CastType::Char)charfield set, instance and static (FieldAccessor::SetJavaField)char()returned from a JS callback (JsArgToArrayConverter,CastType::Char)char()Ã/âfrom the argument bug, and the returnedchar()again kept the first UTF-8 byte)char[]argument (JsArgConverter::ConvertJavaScriptArray)["é", "€", "中", "\ud83d"]char[]element set (ArrayElementAccessor::SetArrayElement)char[]element get (ArrayElementAccessor::GetArrayElement)'é','€'charparameter (JsArgConverter::ConvertJavaScriptString)"x","é",""jstringlocal reference, read throughjvalue.c)The array paths reinterpreted the first two UTF-8 bytes as one UTF-16 unit and then took a byte of its modified UTF-8. The element get handed V8 one UTF-8 byte.
Fix. All of these now go through a new
ArgConverter::ToJavaChar, which takes the first UTF-16 code unit of the JS string. An empty string gives U+0000, which is what the field setter already gave. A character outside the BMP gives its high surrogate. Thechar[]element get usesjcharToV8String. No new errors are thrown.Checked and already correct, so unchanged:
charmethod return valuescharfield getchararguments of Java→JS callbackscharreturn type (TryGetPrimitiveReturnBits)Uint16Array→char[]Behaviour change to review. A plain string passed to a non-overloaded
charparameter now yields its first code unit, where it used to pass an arbitrary value. Overloaded methods are unaffected because overload resolution never matches aStringtochar. If a"Cannot convert string to C"error is preferred instead, that is a small change inConvertJavaScriptString.Bug 2: boolean field read returned an un-escaped handle
GetJavaFieldescapes every result from itsEscapableHandleScopeexcept thebooleanbranch, which returned a handle created inside the closed scope. V8'sBoolean::Newcurrently returns a root-table slot (v8-primitive.h:True()/False()useGetRootSlot), so this does not dangle in practice. It is now escaped like the sibling branches, because the API does not guarantee root slots. The boolean field spec passes before and after, so it is coverage rather than a regression test.Does your commit message include the wording below to reference a specific issue in this repo?
No linked issue; found by a code audit.
Related Pull Requests
None.
Does your pull request have unit tests?
Yes. There is a new
describeintestPrimitiveTypeConversion.jswith Java helpers added tocom.tns.tests.CharConversionTest.What the specs cover
char()and plain-string arguments,charreturn values, JS array →char[],char[]element get and set, instance and staticcharfields, and achar()returned from a JS callback.char[], so a broken direction cannot be masked by the opposite one.booleanfields.Results (full suite,
runtestsAndVerifyResults, arm64 emulator):mainwith the new specs: 9 new specs failed (41 expectations), and nothing else failed. The plain-string path was probed separately; the values are in the table above.Summary by CodeRabbit
charvalues now convert consistently using UTF-16 code units, including non-ASCII characters and surrogate pairs. Empty strings map to NUL, and longer strings use their first code unit.