Skip to content

Commit 1245f75

Browse files
committed
Start modeling the TranslatedType -> ForeignValueLayout mapping
1 parent c99e58f commit 1245f75

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

Sources/JExtractSwift/ImportedDecls.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
189189
cCompatibleConvention: .indirect,
190190
originalSwiftType: "\(raw: parentName.swiftTypeName)",
191191
cCompatibleSwiftType: "UnsafeRawPointer",
192-
cCompatibleJavaMemoryLayout: .memorySegment,
192+
cCompatibleJavaMemoryLayout: .heapObject,
193193
javaType: .javaForeignMemorySegment
194194
).importedTypeName
195195
)
@@ -204,7 +204,7 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
204204
cCompatibleConvention: .indirect,
205205
originalSwiftType: "\(raw: parentName.swiftTypeName)",
206206
cCompatibleSwiftType: "UnsafeRawPointer",
207-
cCompatibleJavaMemoryLayout: .memorySegment,
207+
cCompatibleJavaMemoryLayout: .heapObject,
208208
javaType: .javaForeignMemorySegment
209209
).importedTypeName
210210
)

Sources/JExtractSwift/JavaConstants/ForeignValueLayouts.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public struct ForeignValueLayout: CustomStringConvertible {
5454
}
5555

5656
extension ForeignValueLayout {
57-
public static let SwiftSelf = Self(inlineComment: "Self", javaConstant: "SWIFT_SELF")
5857
public static let SwiftPointer = Self(javaConstant: "SWIFT_POINTER")
5958

6059
public static let SwiftBool = Self(javaConstant: "SWIFT_BOOL")
@@ -63,6 +62,7 @@ extension ForeignValueLayout {
6362
public static let SwiftInt64 = Self(javaConstant: "SWIFT_INT64")
6463
public static let SwiftInt32 = Self(javaConstant: "SWIFT_INT32")
6564
public static let SwiftInt16 = Self(javaConstant: "SWIFT_INT16")
65+
public static let SwiftUInt16 = Self(javaConstant: "SWIFT_UINT16")
6666
public static let SwiftInt8 = Self(javaConstant: "SWIFT_INT8")
6767

6868
public static let SwiftFloat = Self(javaConstant: "SWIFT_FLOAT")

Sources/JExtractSwift/TranslatedType.swift

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extension Swift2JavaVisitor {
3333
cCompatibleConvention: .direct,
3434
originalSwiftType: type,
3535
cCompatibleSwiftType: "@convention(c) () -> Void",
36-
cCompatibleJavaMemoryLayout: .memorySegment,
36+
cCompatibleJavaMemoryLayout: .cFunction,
3737
javaType: .javaLangRunnable
3838
)
3939
}
@@ -134,7 +134,7 @@ extension Swift2JavaVisitor {
134134
cCompatibleConvention: .direct,
135135
originalSwiftType: type,
136136
cCompatibleSwiftType: "UnsafeMutableRawPointer",
137-
cCompatibleJavaMemoryLayout: .memorySegment,
137+
cCompatibleJavaMemoryLayout: .heapObject,
138138
javaType: .javaForeignMemorySegment
139139
)
140140
}
@@ -155,7 +155,7 @@ extension Swift2JavaVisitor {
155155
cCompatibleConvention: .indirect,
156156
originalSwiftType: type,
157157
cCompatibleSwiftType: "UnsafeMutablePointer<\(type)>",
158-
cCompatibleJavaMemoryLayout: .memorySegment,
158+
cCompatibleJavaMemoryLayout: .heapObject,
159159
javaType: .class(package: nil, name: name)
160160
)
161161
}
@@ -235,15 +235,49 @@ extension TranslatedType {
235235
}
236236
}
237237

238+
/// Describes the C-compatible layout as it should be referenced from Java.
238239
enum CCompatibleJavaMemoryLayout {
239-
/// A primitive Java type.
240+
/// A primitive Java type that has a direct counterpart in C.
240241
case primitive(JavaType)
241242

242243
/// The Swift "Int" type, which may be either a Java int (32-bit platforms) or
243244
/// Java long (64-bit platforms).
244245
case int
245246

246-
case memorySegment
247+
/// A Swift heap object, which is treated as a pointer for interoperability
248+
/// purposes but must be retained/released to keep it alive.
249+
case heapObject
250+
251+
/// A C function pointer. In Swift, this will be a @convention(c) function.
252+
/// In Java, a downcall handle to a function.
253+
case cFunction
254+
}
255+
256+
extension TranslatedType {
257+
/// Determine the foreign value layout to use for the translated type with
258+
/// the Java Foreign Function and Memory API.
259+
var foreignValueLayout: ForeignValueLayout {
260+
switch cCompatibleJavaMemoryLayout {
261+
case .primitive(let javaType):
262+
switch javaType {
263+
case .boolean: return .SwiftBool
264+
case .byte: return .SwiftInt8
265+
case .char: return .SwiftUInt16
266+
case .short: return .SwiftInt16
267+
case .int: return .SwiftInt32
268+
case .long: return .SwiftInt64
269+
case .float: return .SwiftFloat
270+
case .double: return .SwiftDouble
271+
case .array, .class, .void: fatalError("Not a primitive type")
272+
}
273+
274+
case .int:
275+
return .SwiftInt
276+
277+
case .heapObject, .cFunction:
278+
return .SwiftPointer
279+
}
280+
}
247281
}
248282

249283
enum TypeTranslationError: Error {

0 commit comments

Comments
 (0)