Skip to content

Commit ccc2543

Browse files
committed
Update Class Layout to use Runtime Metadata
1 parent c769b1a commit ccc2543

File tree

2 files changed

+74
-7
lines changed

2 files changed

+74
-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.printParts("")
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: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
// MANGLED NAME: $s14MySwiftLibrary0aB5ClassCMa
32+
public class MySwiftClass {
33+
// MANGLED NAME: $s14MySwiftLibrary0aB5ClassC3len3capACSi_SitcfC
34+
public init(len: Swift.Int, cap: Swift.Int)
35+
36+
// MANGLED NAME: $s14MySwiftLibrary0aB5ClassC19helloMemberFunctionyyF
37+
public func helloMemberFunction()
38+
39+
public func makeInt() -> Int
40+
41+
@objc deinit
42+
}
43+
"""
44+
45+
@Test("Import: class layout")
46+
func class_layout() throws {
47+
let st = Swift2JavaTranslator(
48+
javaPackage: "com.example.swift",
49+
swiftModuleName: "__FakeModule"
50+
)
51+
52+
try assertOutput(st, input: class_interfaceFile, .java, expectedChunks: [
53+
"""
54+
public static final SwiftAnyType TYPE_METADATA =
55+
new SwiftAnyType(SwiftKit.swiftjava.getType("__FakeModule", "MySwiftClass"));
56+
public final SwiftAnyType $swiftType() {
57+
return TYPE_METADATA;
58+
}
59+
60+
61+
private static final GroupLayout $LAYOUT = (GroupLayout)SwiftValueWitnessTable.layoutOfSwiftType(TYPE_METADATA.$memorySegment());
62+
public final GroupLayout $layout() {
63+
return $LAYOUT;
64+
}
65+
"""
66+
])
67+
}
68+
}

0 commit comments

Comments
 (0)