Skip to content

[jextract] Rework translation #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,11 @@ public class MySwiftLibraryTest {
@Test
void call_helloWorld() {
MySwiftLibrary.helloWorld();

assertNotNull(MySwiftLibrary.helloWorld$address());
}

@Test
void call_globalTakeInt() {
MySwiftLibrary.globalTakeInt(12);

assertNotNull(MySwiftLibrary.globalTakeInt$address());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public class MySwiftClass {

public var counter: Int32 = 0

public static func factory(len: Int, cap: Int) -> MySwiftClass {
return MySwiftClass(len: len, cap: cap)
}

public func voidMethod() {
p("")
}
Expand Down
2 changes: 1 addition & 1 deletion Samples/SwiftKitSampleApp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def jextract = tasks.register("jextract", Exec) {

workingDir = layout.projectDirectory
commandLine "swift"
args("package", "jextract", "-v", "--log-level", "info") // TODO: pass log level from Gradle build
args("package", "jextract", "-v", "--log-level", "debug") // TODO: pass log level from Gradle build
}

// Add the java-swift generated Java sources
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@

// Import swift-extract generated sources

import com.example.swift.MySwiftLibrary;
import com.example.swift.MySwiftClass;

// Import javakit/swiftkit support libraries

import org.swift.swiftkit.SwiftArena;
import org.swift.swiftkit.SwiftKit;
import org.swift.swiftkit.SwiftValueWitnessTable;

public class HelloJava2Swift {

Expand All @@ -40,21 +37,33 @@ static void examples() {

MySwiftLibrary.globalTakeInt(1337);

long cnt = MySwiftLibrary.globalWriteString("String from Java");

SwiftKit.trace("count = " + cnt);

MySwiftLibrary.globalCallMeRunnable(() -> {
SwiftKit.trace("running runnable");
});

// Example of using an arena; MyClass.deinit is run at end of scope
try (var arena = SwiftArena.ofConfined()) {
MySwiftClass obj = new MySwiftClass(2222, 7777, arena);
MySwiftClass obj = new MySwiftClass(2222, 7777, arena);

// just checking retains/releases work
SwiftKit.trace("retainCount = " + SwiftKit.retainCount(obj));
SwiftKit.retain(obj);
SwiftKit.trace("retainCount = " + SwiftKit.retainCount(obj));
SwiftKit.release(obj);
SwiftKit.trace("retainCount = " + SwiftKit.retainCount(obj));

// just checking retains/releases work
SwiftKit.trace("retainCount = " + SwiftKit.retainCount(obj));
SwiftKit.retain(obj);
SwiftKit.trace("retainCount = " + SwiftKit.retainCount(obj));
SwiftKit.release(obj);
SwiftKit.trace("retainCount = " + SwiftKit.retainCount(obj));
obj.setCounter(12);
SwiftKit.trace("obj.counter = " + obj.getCounter());

obj.voidMethod();
obj.takeIntMethod(42);
obj.voidMethod();
obj.takeIntMethod(42);

MySwiftStruct swiftValue = new MySwiftStruct(2222, 1111, arena);
SwiftKit.trace("swiftValue.capacity = " + swiftValue.getCapacity());
}

System.out.println("DONE.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,11 @@ public class MySwiftLibraryTest {
@Test
void call_helloWorld() {
MySwiftLibrary.helloWorld();

assertNotNull(MySwiftLibrary.helloWorld$address());
}

@Test
void call_globalTakeInt() {
MySwiftLibrary.globalTakeInt(12);

assertNotNull(MySwiftLibrary.globalTakeInt$address());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,14 +619,7 @@ extension LoweredFunctionSignature {

@_spi(Testing)
public func cFunctionDecl(cName: String) throws -> CFunction {
return CFunction(
resultType: try CType(cdeclType: self.result.cdeclResultType),
name: cName,
parameters: try self.allLoweredParameters.map {
try CParameter(name: $0.parameterName, type: CType(cdeclType: $0.type).parameterDecay)
},
isVariadic: false
)
try CFunction(cdeclSignature: self.cdeclSignature, cName: cName)
}
}

Expand Down
9 changes: 9 additions & 0 deletions Sources/JExtractSwift/CTypes/CType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,12 @@ extension CType {
}
}
}

extension CType {
var isVoid: Bool {
return switch self {
case .void: true
default: false
}
}
}
15 changes: 8 additions & 7 deletions Sources/JExtractSwift/CodePrinter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ public struct CodePrinter {
}
}

public mutating func printTypeDecl(
_ text: Any,
public mutating func printBraceBlock(
_ header: Any,
function: String = #function,
file: String = #fileID,
line: UInt = #line,
body: (inout CodePrinter) -> ()
) {
print("\(text) {")
body: (inout CodePrinter) throws -> ()
) rethrows {
print("\(header) {")
indent()
body(&self)
try body(&self)
outdent()
print("}", .sloc, function: function, file: file, line: line)
}
Expand Down Expand Up @@ -145,9 +145,10 @@ public struct CodePrinter {

// TODO: remove this in real mode, this just helps visually while working on it
public mutating func printSeparator(_ text: String) {
// TODO: actually use the indentationDepth
assert(!text.contains(where: \.isNewline))
print(
"""

// ==== --------------------------------------------------
// \(text)

Expand Down
33 changes: 0 additions & 33 deletions Sources/JExtractSwift/Convenience/SwiftSyntax+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,6 @@ extension ImplicitlyUnwrappedOptionalTypeSyntax {
}
}

extension SyntaxProtocol {

var asNominalTypeKind: NominalTypeKind {
if isClass {
.class
} else if isActor {
.actor
} else if isStruct {
.struct
} else if isEnum {
.enum
} else {
fatalError("Unknown nominal kind: \(self)")
}
}

var isClass: Bool {
return self.is(ClassDeclSyntax.self)
}

var isActor: Bool {
return self.is(ActorDeclSyntax.self)
}

var isEnum: Bool {
return self.is(EnumDeclSyntax.self)
}

var isStruct: Bool {
return self.is(StructDeclSyntax.self)
}
}

extension DeclModifierSyntax {
var isAccessControl: Bool {
switch self.name.tokenKind {
Expand Down
101 changes: 0 additions & 101 deletions Sources/JExtractSwift/ImportedDecls+Printing.swift

This file was deleted.

Loading