Skip to content

Commit bb33176

Browse files
jrosen081ktoso
andauthored
Update Class Layout to use Runtime Metadata (#190)
Co-authored-by: Konrad `ktoso` Malawski <konrad.malawski@project13.pl>
1 parent 2cf42ce commit bb33176

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

Sources/JExtractSwift/Swift2JavaTranslator+Printing.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ extension Swift2JavaTranslator {
205205
)
206206
printer.print("")
207207

208+
// Layout of the class
209+
printClassMemoryLayout(&printer, decl)
210+
211+
printer.print("")
212+
208213
// Initializers
209214
for initDecl in decl.initializers {
210215
printClassConstructors(&printer, initDecl)
@@ -264,9 +269,6 @@ extension Swift2JavaTranslator {
264269
printClassConstants(printer: &printer)
265270
printTypeMappingDecls(&printer)
266271

267-
// Layout of the class
268-
printClassMemoryLayout(&printer, decl)
269-
270272
body(&printer)
271273
}
272274
}
@@ -384,12 +386,9 @@ extension Swift2JavaTranslator {
384386
}
385387

386388
private func printClassMemoryLayout(_ printer: inout CodePrinter, _ decl: ImportedNominalType) {
387-
// TODO: make use of the swift runtime to get the layout
388389
printer.print(
389390
"""
390-
private static final GroupLayout $LAYOUT = MemoryLayout.structLayout(
391-
SWIFT_POINTER
392-
).withName("\(decl.swiftTypeName)");
391+
private static final GroupLayout $LAYOUT = (GroupLayout) SwiftValueWitnessTable.layoutOfSwiftType(TYPE_METADATA.$memorySegment());
393392
394393
public final GroupLayout $layout() {
395394
return $LAYOUT;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 JExtractSwift
16+
import Testing
17+
18+
struct ClassPrintingTests {
19+
let class_interfaceFile =
20+
"""
21+
// swift-interface-format-version: 1.0
22+
// swift-compiler-version: Apple Swift version 6.0 effective-5.10 (swiftlang-6.0.0.7.6 clang-1600.0.24.1)
23+
// swift-module-flags: -target arm64-apple-macosx15.0 -enable-objc-interop -enable-library-evolution -module-name MySwiftLibrary
24+
import Darwin.C
25+
import Darwin
26+
import Swift
27+
import _Concurrency
28+
import _StringProcessing
29+
import _SwiftConcurrencyShims
30+
31+
public class MySwiftClass {
32+
public init(len: Swift.Int, cap: Swift.Int)
33+
34+
public func helloMemberFunction()
35+
36+
public func makeInt() -> Int
37+
38+
@objc deinit
39+
}
40+
"""
41+
42+
@Test("Import: class layout")
43+
func class_layout() throws {
44+
let st = Swift2JavaTranslator(
45+
javaPackage: "com.example.swift",
46+
swiftModuleName: "__FakeModule"
47+
)
48+
49+
try assertOutput(st, input: class_interfaceFile, .java, expectedChunks: [
50+
"""
51+
public static final SwiftAnyType TYPE_METADATA =
52+
new SwiftAnyType(SwiftKit.swiftjava.getType("__FakeModule", "MySwiftClass"));
53+
public final SwiftAnyType $swiftType() {
54+
return TYPE_METADATA;
55+
}
56+
57+
58+
private static final GroupLayout $LAYOUT = (GroupLayout) SwiftValueWitnessTable.layoutOfSwiftType(TYPE_METADATA.$memorySegment());
59+
public final GroupLayout $layout() {
60+
return $LAYOUT;
61+
}
62+
"""
63+
])
64+
}
65+
}

0 commit comments

Comments
 (0)