Repository navigation
wrap-java: translate Java byte[] as [UInt8] - #939
Open
rayman-zhao wants to merge 2 commits into
Open
rayman-zhao wants to merge 2 commits into
rayman-zhao wants to merge 2 commits into
Conversation
A Java byte[] is a byte buffer, and Swift's idiomatic buffer element type is UInt8; the export direction already maps UInt8 to byte and the JNI bridge has a fast path for both element types. Mapping byte[] to [Int8] forced every wrap-java consumer to convert buffers element-type-only, adding an allocation and memcpy per boundary call. Scalar byte stays Int8, matching Java's signed byte. Source-breaking: Swift signatures involving byte[] change from [Int8] to [UInt8]. Regenerated affected wrappers.
JavaClassTranslator read .class bytes via the generated InputStream.readAllBytes() wrapper, which now returns [UInt8]; drop the now-unnecessary bitPattern conversion shim. Update the JavaKitSampleApp array tests to the new [UInt8] signatures and a format nit in JavaTranslator.
Contributor
Author
|
Fixed the CI fallout in 025fec8 — apologies for the noise:
The sample apps and linkage/benchmark checks were cascade failures of the toolchain build error; they should be green now. |
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.
Motivation
A Java
byte[]is a byte buffer, and Swift code that produces or consumes byte buffers (image codecs, crypto, IO) works in[UInt8]. Importingbyte[]as[Int8]forces a redundant type-only conversion at every boundary — e.g. implementations of Javanativemethods that fetch encoded images as[UInt8]must copy into[Int8](one allocation + memcpy per call) purely to satisfy the type system.The runtime already has a fast path bridging
[UInt8]andbyte[]directly viaSetByteArrayRegion(JavaValue+Array.swift), and the export direction already mapsUInt8tobyte(seeJavaType+SwiftNames.swift). This makes the import direction symmetric for arrays.Scalar
bytestaysInt8, matching Java's signedbyte— only arrays change, sincebyte[]is buffer-shaped while a scalar byte is numeric.Modification
JavaTranslator.getSwiftTypeNamenow maps.array(.byte)to[UInt8]instead of composing the scalar mapping."byte[]": [UInt8]entry fromtranslatedClasses— it was unreachable (reflected array classes are named[Band parsed asJavaType, never hitting the string-keyed table); the new special case implements what that entry intended.ByteBuffer,JavaString,InputStream,OutputStream,JavaClassLoader,BufferedInputStream,BitSet,ZipEntry,JarInputStreamflip[Int8]→[UInt8]).testCovariantInJavaNotInSwiftOverride3; addedtestByteArrayTranslatesToUInt8AndScalarByteStaysInt8covering both the array and the scalar mapping.Source compatibility
byte[]change from[Int8]to[UInt8](both JavaKit call sites and implementations of Javanativemethods). The JNI bridge accepts both element types unchanged, so only the Swift-level types move. I judged this acceptable pre-1.0; happy to rework if a different policy is preferred (e.g. keeping[Int8]and doing the flip at a major boundary).Verified locally by regenerating all stdlib wrappers (baseline before the change reproduced committed files byte-for-byte on JDK 25) and by wrapping
java.lang.String/java.lang.reflect.Arrayin struct mode to match the new test expectations. Fullswift testcould not run on Windows locally (thepackage-benchmarkplugin target and repo symlink layout do not build there); relying on CI.