Skip to content

Commit 9fcc003

Browse files
committed
jextract/ffm: make sure uint arrays are marked @unsigned
1 parent 91ff2c5 commit 9fcc003

File tree

8 files changed

+41
-34
lines changed

8 files changed

+41
-34
lines changed

Sources/JExtractSwiftLib/Common/TypeAnnotations.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@ import SwiftJavaConfigurationShared
1818
/// Determine if the given type needs any extra annotations that should be included
1919
/// in Java sources when the corresponding Java type is rendered.
2020
func getTypeAnnotations(swiftType: SwiftType, config: Configuration) -> [JavaAnnotation] {
21-
if swiftType.isUnsignedInteger, config.effectiveUnsignedNumbersMode == .annotate {
22-
return [JavaAnnotation.unsigned]
21+
if config.effectiveUnsignedNumbersMode == .annotate {
22+
switch swiftType {
23+
case .array(let wrapped) where wrapped.isUnsignedInteger:
24+
return [JavaAnnotation.unsigned]
25+
case _ where swiftType.isUnsignedInteger:
26+
return [JavaAnnotation.unsigned]
27+
default:
28+
break
29+
}
2330
}
2431

2532
return []

Sources/JExtractSwiftLib/FFM/CDeclLowering/FFMSwift2JavaGenerator+FunctionLowering.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,11 @@ struct CdeclLowering {
208208
return LoweredParameter(
209209
cdeclParameters: [
210210
SwiftParameter(
211-
convention: .byValue, parameterName: "\(parameterName)_pointer",
211+
convention: .byValue, parameterName: "\(parameterName)$pointer",
212212
type: isMutable ? knownTypes.unsafeMutableRawPointer : knownTypes.unsafeRawPointer
213213
),
214214
SwiftParameter(
215-
convention: .byValue, parameterName: "\(parameterName)_count",
215+
convention: .byValue, parameterName: "\(parameterName)$count",
216216
type: knownTypes.int
217217
),
218218
], conversion: .initialize(
@@ -237,11 +237,11 @@ struct CdeclLowering {
237237
cdeclParameters: [
238238
SwiftParameter(
239239
convention: .byValue,
240-
parameterName: "\(parameterName)_pointer",
240+
parameterName: "\(parameterName)$pointer",
241241
type: .optional(isMutable ? knownTypes.unsafeMutableRawPointer : knownTypes.unsafeRawPointer)
242242
),
243243
SwiftParameter(
244-
convention: .byValue, parameterName: "\(parameterName)_count",
244+
convention: .byValue, parameterName: "\(parameterName)$count",
245245
type: knownTypes.int
246246
)
247247
],
@@ -355,8 +355,8 @@ struct CdeclLowering {
355355
]
356356

357357
// Create parameter names with consistent naming convention
358-
let pointerParameterName = "\(parameterName)_pointer"
359-
let countParameterName = "\(parameterName)_count"
358+
let pointerParameterName = "\(parameterName)$pointer"
359+
let countParameterName = "\(parameterName)$count"
360360

361361
// Build C declaration parameters for pointer and count
362362
let cdeclParameters = [
@@ -542,12 +542,12 @@ struct CdeclLowering {
542542
cdeclParameters: [
543543
SwiftParameter(
544544
convention: .byValue,
545-
parameterName: "\(parameterName)_pointer",
545+
parameterName: "\(parameterName)$pointer",
546546
type: .optional(isMutable ? knownTypes.unsafeMutableRawPointer : knownTypes.unsafeRawPointer)
547547
),
548548
SwiftParameter(
549549
convention: .byValue,
550-
parameterName: "\(parameterName)_count",
550+
parameterName: "\(parameterName)$count",
551551
type: knownTypes.int
552552
),
553553
],
@@ -632,24 +632,24 @@ struct CdeclLowering {
632632
cdeclOutParameters: [
633633
SwiftParameter(
634634
convention: .byValue,
635-
parameterName: "\(outParameterName)_pointer",
635+
parameterName: "\(outParameterName)$pointer",
636636
type: knownTypes.unsafeMutablePointer(
637637
.optional(isMutable ? knownTypes.unsafeMutableRawPointer : knownTypes.unsafeRawPointer)
638638
)
639639
),
640640
SwiftParameter(
641641
convention: .byValue,
642-
parameterName: "\(outParameterName)_count",
642+
parameterName: "\(outParameterName)$count",
643643
type: knownTypes.unsafeMutablePointer(knownTypes.int)
644644
),
645645
],
646646
conversion: .aggregate([
647647
.populatePointer(
648-
name: "\(outParameterName)_pointer",
648+
name: "\(outParameterName)$pointer",
649649
to: .member(.placeholder, member: "baseAddress")
650650
),
651651
.populatePointer(
652-
name: "\(outParameterName)_count",
652+
name: "\(outParameterName)$count",
653653
to: .member(.placeholder, member: "count")
654654
)
655655
], name: outParameterName)

Sources/JExtractSwiftLib/FFM/ConversionStep.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ enum ConversionStep: Equatable {
9999
return "\(raw: placeholder)"
100100

101101
case .explodedComponent(let step, component: let component):
102-
return step.asExprSyntax(placeholder: "\(placeholder)_\(component)", bodyItems: &bodyItems)
102+
return step.asExprSyntax(placeholder: "\(placeholder)$\(component)", bodyItems: &bodyItems)
103103

104104
case .unsafeCastPointer(let step, swiftType: let swiftType):
105105
let untypedExpr = step.asExprSyntax(placeholder: placeholder, bodyItems: &bodyItems)

Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator+JavaTranslation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ extension FFMSwift2JavaGenerator {
479479
case .array(let wrapped) where wrapped == knownTypes.uint8:
480480
return TranslatedParameter(
481481
javaParameters: [
482-
JavaParameter(name: parameterName, type: .array(.byte)),
482+
JavaParameter(name: parameterName, type: .array(.byte), annotations: parameterAnnotations),
483483
],
484484
conversion:
485485
.commaSeparated([

Tests/JExtractSwiftTests/ByteArrayTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ final class ByteArrayTests {
3434
"""
3535
/**
3636
* {@snippet lang=c :
37-
* void swiftjava_SwiftModule_acceptArray_array(const void *array_pointer, ptrdiff_t array_count)
37+
* void swiftjava_SwiftModule_acceptArray_array(const void *array$pointer, ptrdiff_t array$count)
3838
* }
3939
*/
4040
private static class swiftjava_SwiftModule_acceptArray_array {
4141
private static final FunctionDescriptor DESC = FunctionDescriptor.ofVoid(
42-
/* array_pointer: */SwiftValueLayout.SWIFT_POINTER,
43-
/* array_count: */SwiftValueLayout.SWIFT_INT
42+
/* array$pointer: */SwiftValueLayout.SWIFT_POINTER,
43+
/* array$count: */SwiftValueLayout.SWIFT_INT
4444
);
4545
""",
4646
"""
@@ -50,7 +50,7 @@ final class ByteArrayTests {
5050
* public func acceptArray(array: [UInt8])
5151
* }
5252
*/
53-
public static void acceptArray(byte[] array) {
53+
public static void acceptArray(@Unsigned byte[] array) {
5454
try(var arena$ = Arena.ofConfined()) {
5555
swiftjava_SwiftModule_acceptArray_array.call(arena$.allocateFrom(ValueLayout.JAVA_BYTE, array), array.length);
5656
}
@@ -61,8 +61,8 @@ final class ByteArrayTests {
6161
[
6262
"""
6363
@_cdecl("swiftjava_SwiftModule_acceptArray_array")
64-
public func swiftjava_SwiftModule_acceptArray_array(_ array_pointer: UnsafeRawPointer, _ array_count: Int) {
65-
acceptArray(array: [UInt8](UnsafeRawBufferPointer(start: array_pointer, count: array_count)))
64+
public func swiftjava_SwiftModule_acceptArray_array(_ array$pointer: UnsafeRawPointer, _ array$count: Int) {
65+
acceptArray(array: [UInt8](UnsafeRawBufferPointer(start: array$pointer, count: array$count)))
6666
}
6767
"""
6868
]

Tests/JExtractSwiftTests/DataImportTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,8 @@ final class DataImportTests {
345345
void apply(java.lang.foreign.MemorySegment _0);
346346
}
347347
private static MemorySegment $toUpcallStub(body fi, Arena arena) {
348-
return swiftjava_SwiftModule_Data_withUnsafeBytes__.$body.toUpcallStub((_0_pointer, _0_count) -> {
349-
fi.apply(_0_pointer.reinterpret(_0_count));
348+
return swiftjava_SwiftModule_Data_withUnsafeBytes__.$body.toUpcallStub((_0$pointer, _0$count) -> {
349+
fi.apply(_0$pointer.reinterpret(_0$count));
350350
}, arena);
351351
}
352352
}

Tests/JExtractSwiftTests/FuncCallbackImportTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ final class FuncCallbackImportTests {
318318
long apply(java.lang.foreign.MemorySegment _0);
319319
}
320320
private static MemorySegment $toUpcallStub(body fi, Arena arena) {
321-
return swiftjava___FakeModule_withBuffer_body.$body.toUpcallStub((_0_pointer, _0_count) -> {
322-
return fi.apply(_0_pointer.reinterpret(_0_count));
321+
return swiftjava___FakeModule_withBuffer_body.$body.toUpcallStub((_0$pointer, _0$count) -> {
322+
return fi.apply(_0$pointer.reinterpret(_0$count));
323323
}, arena);
324324
}
325325
}

Tests/JExtractSwiftTests/FunctionLoweringTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ final class FunctionLoweringTests {
2525
""",
2626
expectedCDecl: """
2727
@_cdecl("c_f")
28-
public func c_f(_ x: Int, _ y: Float, _ z_pointer: UnsafeRawPointer, _ z_count: Int) {
29-
f(x: x, y: y, z: UnsafeBufferPointer<Bool>(start: z_pointer.assumingMemoryBound(to: Bool.self), count: z_count))
28+
public func c_f(_ x: Int, _ y: Float, _ z$pointer: UnsafeRawPointer, _ z$count: Int) {
29+
f(x: x, y: y, z: UnsafeBufferPointer<Bool>(start: z$pointer.assumingMemoryBound(to: Bool.self), count: z$count))
3030
}
3131
""",
32-
expectedCFunction: "void c_f(ptrdiff_t x, float y, const void *z_pointer, ptrdiff_t z_count)"
32+
expectedCFunction: "void c_f(ptrdiff_t x, float y, const void *z$pointer, ptrdiff_t z$count)"
3333
)
3434
}
3535

@@ -323,13 +323,13 @@ final class FunctionLoweringTests {
323323
""",
324324
expectedCDecl: """
325325
@_cdecl("c_swapRawBufferPointer")
326-
public func c_swapRawBufferPointer(_ buffer_pointer: UnsafeRawPointer?, _ buffer_count: Int, _ _result_pointer: UnsafeMutablePointer<UnsafeMutableRawPointer?>, _ _result_count: UnsafeMutablePointer<Int>) {
327-
let _result = swapRawBufferPointer(buffer: UnsafeRawBufferPointer(start: buffer_pointer, count: buffer_count))
328-
_result_pointer.initialize(to: _result.baseAddress)
329-
_result_count.initialize(to: _result.count)
326+
public func c_swapRawBufferPointer(_ buffer$pointer: UnsafeRawPointer?, _ buffer$count: Int, _ _result$pointer: UnsafeMutablePointer<UnsafeMutableRawPointer?>, _ _result$count: UnsafeMutablePointer<Int>) {
327+
let _result = swapRawBufferPointer(buffer: UnsafeRawBufferPointer(start: buffer$pointer, count: buffer$count))
328+
_result$pointer.initialize(to: _result.baseAddress)
329+
_result$count.initialize(to: _result.count)
330330
}
331331
""",
332-
expectedCFunction: "void c_swapRawBufferPointer(const void *buffer_pointer, ptrdiff_t buffer_count, void **_result_pointer, ptrdiff_t *_result_count)"
332+
expectedCFunction: "void c_swapRawBufferPointer(const void *buffer$pointer, ptrdiff_t buffer$count, void **_result$pointer, ptrdiff_t *_result$count)"
333333
)
334334
}
335335

0 commit comments

Comments
 (0)