jextract: support typed unsafepointer/unsafebufferpointer in FFM and JNI generators - #904
jextract: support typed unsafepointer/unsafebufferpointer in FFM and JNI generators#904Tatenda-k wants to merge 1 commit into
Conversation
| */ | ||
| public static java.lang.foreign.MemorySegment globalReturnUnsafeBufferPointer() { | ||
| try(var arena$ = Arena.ofConfined()) { | ||
| MemorySegment result$_pointer = arena$.allocate(SwiftValueLayout.SWIFT_POINTER); |
There was a problem hiding this comment.
please format this a bit better
| parameter: JavaParameter(name: parameterName, type: .javaForeignMemorySegment), | ||
| conversion: .commaSeparated([ | ||
| .placeholder, | ||
| .constant("\(parameterName).byteSize() / \(elementLayout.description).byteSize()"), |
There was a problem hiding this comment.
| .constant("\(parameterName).byteSize() / \(elementLayout.description).byteSize()"), | |
| // Calculate the element count: buffer size / element size | |
| .constant("\(parameterName).byteSize() / \(elementLayout.description).byteSize()"), |
| * Corresponds to Swift's {@code UnsafeBufferPointer<T>} and | ||
| * {@code UnsafeMutableBufferPointer<T>}. | ||
| */ | ||
| public final class SwiftBufferPointer { |
There was a problem hiding this comment.
This must be unsafe, we're not keeping the memory alive so it could be misleading to drop the Unsafe -- please let's name it
| public final class SwiftBufferPointer { | |
| public final class SwiftUnsafeBufferPointer { |
There was a problem hiding this comment.
I think we should mirror the Mutable as well -- make a new type that is SwiftUnsafeMutableBufferPointer and we should import as it when possible
|
|
||
| @Override | ||
| public String toString() { | ||
| return "SwiftBufferPointer(baseAddress=0x" + Long.toHexString(baseAddress) + ", count=" + count + ")"; |
ktoso
left a comment
There was a problem hiding this comment.
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
|
Also same tests in FFM since we claim to support it now |
| outParameters: [.init(name: resultName, type: .swiftBufferPointer, allocation: .new)], | ||
| conversion: .constant(resultName), | ||
| ) | ||
| //taenda todo: add support for UnsafeRawBufferPointer and UnsafeMutableRawBufferPointer |
There was a problem hiding this comment.
let's address this TODO please
| @Test | ||
| func returnUnsafeRawBufferPointer_javaBindings() throws { | ||
| try assertOutput( | ||
| dump: true, |
There was a problem hiding this comment.
please don't commit with dump: true, it'll be very noisy in CI
| SwiftBufferPointer o = (SwiftBufferPointer) other; | ||
| return this.baseAddress == o.baseAddress && this.count == o.count; | ||
| } | ||
|
|
There was a problem hiding this comment.
So this is imported but isn't usable in Java, we should probably offer an toByteBuffer() here wdyt?
Added support for
case .unsafePointer, .unsafeMutablePointer,case .unsafePointer, .unsafeMutablePointer, andcase .unsafeBufferPointer, .unsafeMutableBufferPointerfor FFM and JNI.#840