Skip to content

Commit 06c7688

Browse files
committed
add tests and correct char handling for jextractign unsigned params
1 parent bdc5219 commit 06c7688

File tree

9 files changed

+162
-46
lines changed

9 files changed

+162
-46
lines changed

Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator+JavaBindingsPrinting.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ extension FFMSwift2JavaGenerator {
7575
) {
7676
printer.start("private static final FunctionDescriptor DESC = ")
7777

78+
print("C PARAMS = \(parameters)")
79+
7880
let isEmptyParam = parameters.isEmpty
7981
if resultType.isVoid {
8082
printer.print("FunctionDescriptor.ofVoid(", isEmptyParam ? .continue : .newLine)
@@ -87,6 +89,8 @@ extension FFMSwift2JavaGenerator {
8789
}
8890

8991
for (param, isLast) in parameters.withIsLast {
92+
print("PARAM = \(param)")
93+
print("PARAM LAYOUT = \(param.type.foreignValueLayout)")
9094
printer.print("/* \(param.name ?? "_"): */", .continue)
9195
printer.print(param.type.foreignValueLayout, .parameterNewlineSeparator(isLast))
9296
}

Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator+JavaTranslation.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -720,10 +720,10 @@ extension CType {
720720
case .integral(.signed(bits: 32)): return .SwiftInt32
721721
case .integral(.signed(bits: 64)): return .SwiftInt64
722722

723-
case .integral(.unsigned(bits: 8)): return .SwiftInt8
724-
case .integral(.unsigned(bits: 16)): return .SwiftInt16
725-
case .integral(.unsigned(bits: 32)): return .SwiftInt32
726-
case .integral(.unsigned(bits: 64)): return .SwiftInt64
723+
case .integral(.unsigned(bits: 8)): return .SwiftUInt8
724+
case .integral(.unsigned(bits: 16)): return .SwiftUInt16
725+
case .integral(.unsigned(bits: 32)): return .SwiftUInt32
726+
case .integral(.unsigned(bits: 64)): return .SwiftUInt64
727727

728728
case .floating(.double): return .SwiftDouble
729729
case .floating(.float): return .SwiftFloat

Sources/JExtractSwiftLib/FFM/ForeignValueLayouts.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,19 @@ extension ForeignValueLayout {
6767
public static let SwiftBool = Self(javaConstant: "SWIFT_BOOL")
6868

6969
public static let SwiftInt = Self(javaConstant: "SWIFT_INT")
70+
public static let SwiftUInt = Self(javaConstant: "SWIFT_UINT")
71+
7072
public static let SwiftInt64 = Self(javaConstant: "SWIFT_INT64")
73+
public static let SwiftUInt64 = Self(javaConstant: "SWIFT_UINT64")
74+
7175
public static let SwiftInt32 = Self(javaConstant: "SWIFT_INT32")
76+
public static let SwiftUInt32 = Self(javaConstant: "SWIFT_UINT32")
77+
7278
public static let SwiftInt16 = Self(javaConstant: "SWIFT_INT16")
7379
public static let SwiftUInt16 = Self(javaConstant: "SWIFT_UINT16")
80+
7481
public static let SwiftInt8 = Self(javaConstant: "SWIFT_INT8")
82+
public static let SwiftUInt8 = Self(javaConstant: "SWIFT_UINT8")
7583

7684
public static let SwiftFloat = Self(javaConstant: "SWIFT_FLOAT")
7785
public static let SwiftDouble = Self(javaConstant: "SWIFT_DOUBLE")

Sources/JExtractSwiftLib/JavaTypes/JavaType+SwiftKit.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ extension JavaType {
6161
case .nominal(let nominal):
6262
switch nominal.nominalTypeDecl.knownTypeKind {
6363
case .uint8: return swiftkit.primitives.UnsignedByte
64-
case .uint16: return swiftkit.primitives.UnsignedShort
64+
case .uint16: return .char // no wrapper necessary, we can express it as 'char' natively in Java
6565
case .uint32: return swiftkit.primitives.UnsignedInteger
6666
case .uint64: return swiftkit.primitives.UnsignedLong
6767
default: return nil

SwiftKitCore/src/main/java/org/swift/swiftkit/core/primitives/UnsignedNumbers.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
*/
2626
public final class UnsignedNumbers {
2727

28+
@Deprecated(forRemoval = true)
29+
public static int toPrimitive(char value) {
30+
return value; // TODO: remove this, we should not be generating a conversion for 'char'
31+
}
32+
2833
/**
2934
* Returns the primitive {@code int}, value of the passed in {@link UnsignedInteger}.
3035
*/

SwiftKitFFM/src/main/java/org/swift/swiftkit/ffm/SwiftValueLayout.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,19 @@ public static long addressByteSize() {
3434
}
3535

3636
public static final ValueLayout.OfBoolean SWIFT_BOOL = ValueLayout.JAVA_BOOLEAN;
37+
3738
public static final ValueLayout.OfByte SWIFT_INT8 = ValueLayout.JAVA_BYTE;
38-
public static final ValueLayout.OfChar SWIFT_UINT16 = ValueLayout.JAVA_CHAR;
39+
public static final ValueLayout.OfByte SWIFT_UINT8 = SWIFT_INT8;
40+
3941
public static final ValueLayout.OfShort SWIFT_INT16 = ValueLayout.JAVA_SHORT;
42+
public static final ValueLayout.OfChar SWIFT_UINT16 = ValueLayout.JAVA_CHAR;
43+
4044
public static final ValueLayout.OfInt SWIFT_INT32 = ValueLayout.JAVA_INT;
45+
public static final ValueLayout.OfInt SWIFT_UINT32 = SWIFT_INT32;
46+
4147
public static final ValueLayout.OfLong SWIFT_INT64 = ValueLayout.JAVA_LONG;
48+
public static final ValueLayout.OfLong SWIFT_UINT64 = SWIFT_INT64;
49+
4250
public static final ValueLayout.OfFloat SWIFT_FLOAT = ValueLayout.JAVA_FLOAT;
4351
public static final ValueLayout.OfDouble SWIFT_DOUBLE = ValueLayout.JAVA_DOUBLE;
4452

Tests/JExtractSwiftTests/Asserts/TextAssertions.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ func assertOutput(
7979
}
8080
output = printer.finalize()
8181

82+
print("OUTPUT ======")
83+
print(output)
84+
print("==========================")
85+
8286
let gotLines = output.split(separator: "\n").filter { l in
8387
l.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).count > 0
8488
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import JExtractSwiftLib
16+
import Testing
17+
18+
final class UnsignedNumberTests {
19+
20+
@Test("Import: UInt8")
21+
func unsignedByte() throws {
22+
try assertOutput(
23+
input: "public func unsignedByte(_ arg: UInt8)",
24+
.ffm, .java,
25+
detectChunkByInitialLines: 2,
26+
expectedChunks: [
27+
"""
28+
/**
29+
* {@snippet lang=c :
30+
* void swiftjava_SwiftModule_unsignedByte__(uint8_t arg)
31+
* }
32+
*/
33+
private static class swiftjava_SwiftModule_unsignedByte__ {
34+
private static final FunctionDescriptor DESC = FunctionDescriptor.ofVoid(
35+
/* arg: */SwiftValueLayout.SWIFT_UINT8
36+
);
37+
""",
38+
"""
39+
public static void unsignedByte(org.swift.swiftkit.core.primitives.UnsignedByte arg) {
40+
swiftjava_SwiftModule_unsignedByte__.call(UnsignedNumbers.toPrimitive(arg));
41+
}
42+
""",
43+
]
44+
)
45+
}
46+
47+
@Test("Import: UInt16")
48+
func unsignedChar() throws {
49+
try assertOutput(
50+
input: "public func unsignedChar(_ arg: UInt16)",
51+
.ffm, .java,
52+
detectChunkByInitialLines: 2,
53+
expectedChunks: [
54+
"""
55+
/**
56+
* {@snippet lang=c :
57+
* void swiftjava_SwiftModule_unsignedChar__(uint16_t arg)
58+
* }
59+
*/
60+
private static class swiftjava_SwiftModule_unsignedChar__ {
61+
private static final FunctionDescriptor DESC = FunctionDescriptor.ofVoid(
62+
/* arg: */SwiftValueLayout.SWIFT_UINT16
63+
);
64+
""",
65+
"""
66+
public static void unsignedChar(char arg) {
67+
swiftjava_SwiftModule_unsignedChar__.call(UnsignedNumbers.toPrimitive(arg));
68+
}
69+
""",
70+
]
71+
)
72+
}
73+
74+
@Test("Import: UInt32")
75+
func unsignedInt() throws {
76+
try assertOutput(
77+
input: "public func unsignedInt(_ arg: UInt32)",
78+
.ffm, .java,
79+
detectChunkByInitialLines: 2,
80+
expectedChunks: [
81+
"""
82+
/**
83+
* {@snippet lang=c :
84+
* void swiftjava_SwiftModule_unsignedInt__(uint32_t arg)
85+
* }
86+
*/
87+
private static class swiftjava_SwiftModule_unsignedInt__ {
88+
private static final FunctionDescriptor DESC = FunctionDescriptor.ofVoid(
89+
/* arg: */SwiftValueLayout.SWIFT_UINT32
90+
);
91+
""",
92+
"""
93+
public static void unsignedInt(org.swift.swiftkit.core.primitives.UnsignedInteger arg) {
94+
swiftjava_SwiftModule_unsignedInt__.call(UnsignedNumbers.toPrimitive(arg));
95+
}
96+
""",
97+
]
98+
)
99+
}
100+
101+
@Test("Import: UInt64")
102+
func unsignedLong() throws {
103+
try assertOutput(
104+
input: "public func unsignedLong(_ arg: UInt64)",
105+
.ffm, .java,
106+
detectChunkByInitialLines: 2,
107+
expectedChunks: [
108+
"""
109+
/**
110+
* {@snippet lang=c :
111+
* void swiftjava_SwiftModule_unsignedLong__(uint64_t arg)
112+
* }
113+
*/
114+
private static class swiftjava_SwiftModule_unsignedLong__ {
115+
private static final FunctionDescriptor DESC = FunctionDescriptor.ofVoid(
116+
/* arg: */SwiftValueLayout.SWIFT_UINT64
117+
);
118+
""",
119+
"""
120+
public static void unsignedLong(org.swift.swiftkit.core.primitives.UnsignedLong arg) {
121+
swiftjava_SwiftModule_unsignedLong__.call(UnsignedNumbers.toPrimitive(arg));
122+
}
123+
""",
124+
]
125+
)
126+
}
127+
}

Tests/JExtractSwiftTests/UnsignedTests.swift

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)