Skip to content

jextract: support typed unsafepointer/unsafebufferpointer in FFM and JNI generators - #904

Open
Tatenda-k wants to merge 2 commits into
swiftlang:mainfrom
Tatenda-k:jextract-pointer-support
Open

Tatenda-k wants to merge 2 commits into
swiftlang:mainfrom
Tatenda-k:jextract-pointer-support

Conversation

@Tatenda-k

@Tatenda-k Tatenda-k commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Added support for case .unsafePointer, .unsafeMutablePointer, case .unsafePointer, .unsafeMutablePointer, and case .unsafeBufferPointer, .unsafeMutableBufferPointer for FFM and JNI.
#840

@Tatenda-k
Tatenda-k requested a review from ktoso as a code owner August 22, 2026 23:28
Comment thread Tests/JExtractSwiftTests/MethodImportTests.swift Outdated
Comment thread SwiftKitCore/src/main/java/org/swift/swiftkit/core/SwiftBufferPointer.java Outdated
Comment thread SwiftKitCore/src/main/java/org/swift/swiftkit/core/SwiftBufferPointer.java Outdated

@ktoso ktoso left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty good work! Almost there 👍

Please add runtime tests by adding functions returning such buffers to Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary and then adding tests in Java using them in Samples/SwiftJavaExtractJNISampleApp/src/test/java

@ktoso

ktoso commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

Also same tests in FFM since we claim to support it now

Comment thread Tests/JExtractSwiftTests/JNI/JNIPointerTests.swift Outdated
Comment thread Package.swift Outdated
public func bufferCount(data: UnsafeRawBufferPointer) -> Int64 {
Int64(data.count)
}
// snippet.end

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would break existing snippets

@Tatenda-k
Tatenda-k force-pushed the jextract-pointer-support branch 2 times, most recently from ba02d62 to eb8a4cf Compare August 30, 2026 16:38
@Tatenda-k
Tatenda-k requested a review from ktoso August 30, 2026 16:39
@Tatenda-k
Tatenda-k force-pushed the jextract-pointer-support branch 4 times, most recently from 00a8cc5 to a720acb Compare September 2, 2026 01:36
@Tatenda-k

Copy link
Copy Markdown
Contributor Author

Sorry about the pr run fails. I currently have a windows, and I'm working on setting up my wsl so I can run github actions on my machine

@ktoso

ktoso commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

No problem, happy to rerun. You may have more success trying a docker container hmmm

… FFM and JNI generators

fixed test for UnsafeBufferPointer
@Tatenda-k
Tatenda-k force-pushed the jextract-pointer-support branch from 663e446 to 290356a Compare September 20, 2026 00:07
@Tatenda-k

Copy link
Copy Markdown
Contributor Author

@ktoso I believe my PR's macos ci failure (no such module 'XCTest') isn't related to my changes. I have seen the same fail in some other prs too. It looks like it is being addressed in #923. I will rebase and retry when it lands.

expectedChunks: [
"""
do {
let baseAddressBits$ = Int64(Int(bitPattern: SwiftModule.globalReturnUnsafeBufferPointer().baseAddress))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We call SwiftModule.globalReturnUnsafeBufferPointer here and

do {
let baseAddressBits$ = Int64(Int(bitPattern: SwiftModule.globalReturnUnsafeBufferPointer().baseAddress))
environment.interface.SetLongField(environment, resultOut, _JNIMethodIDCache.SwiftUnsafeBufferPointer.baseAddress, baseAddressBits$.getJNIValue(in: environment))
let countBits$ = Int64(SwiftModule.globalReturnUnsafeBufferPointer().count)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here again SwiftModule.globalReturnUnsafeBufferPointer

this is not ok, the global function may have side effects so we call it twice here potentially causing some effect twice when it should be once

printer.print(
"""
let baseAddressBits$ = Int64(Int(bitPattern: \(inner).baseAddress))
environment.interface.SetLongField(environment, \(outArgumentName), _JNIMethodIDCache.SwiftUnsafeBufferPointer.baseAddress, baseAddressBits$.getJNIValue(in: environment))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are reusing the same ID but they may be mutable or not, we must use SwiftUnsafeMutableBufferPointer if it was mutable

let baseAddressBits$ = Int64(Int(bitPattern: \(inner).baseAddress))
environment.interface.SetLongField(environment, \(outArgumentName), _JNIMethodIDCache.SwiftUnsafeBufferPointer.baseAddress, baseAddressBits$.getJNIValue(in: environment))
let countBits$ = Int64(\(inner).count)
environment.interface.SetLongField(environment, \(outArgumentName), _JNIMethodIDCache.SwiftUnsafeBufferPointer.count, countBits$.getJNIValue(in: environment))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, needs to account for Mutable

}
return ""

case .bufferPointerIndirectReturn(let inner, let outArgumentName):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add an isMutable here and use it to determine the IDs below

let javaBufferType: JavaType = isMutable ? .swiftUnsafeMutableBufferPointer : .swiftUnsafeBufferPointer
return NativeResult(
javaType: .void,
conversion: .bufferPointerIndirectReturn(.placeholder, outArgumentName: resultName + "Out"),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this must pass along the isMutable: isMutable

@ktoso

ktoso commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

Found a few bits I'm concerned about, can you have a look please @Tatenda-k ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants