Skip to content

Commit 93ebae9

Browse files
committed
refactoring
1 parent e9eafeb commit 93ebae9

File tree

10 files changed

+593
-122
lines changed

10 files changed

+593
-122
lines changed

Plugins/JExtractSwiftPlugin/JExtractSwiftPlugin.swift

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ struct JExtractSwiftBuildToolPlugin: SwiftJavaPluginProtocol, BuildToolPlugin {
8787
let (moduleName, configFile) = moduleAndConfigFile
8888
return [
8989
"--depends-on",
90-
"\(configFile.path(percentEncoded: false))"
90+
"\(moduleName)=\(configFile.path(percentEncoded: false))"
9191
]
9292
}
9393
arguments += dependentConfigFilesArguments
@@ -239,18 +239,23 @@ struct JExtractSwiftBuildToolPlugin: SwiftJavaPluginProtocol, BuildToolPlugin {
239239
let singleSwiftFileOutputName = "WrapJavaGenerated.swift"
240240

241241
// In the end we can run wrap-java on the previous inputs
242+
var wrapJavaArguments = [
243+
"wrap-java",
244+
"--swift-module", sourceModule.name,
245+
"--output-directory", outputSwiftDirectory.path(percentEncoded: false),
246+
"--config", swiftJavaConfigURL.path(percentEncoded: false),
247+
"--cp", swiftKitCoreClassPath.path(percentEncoded: false),
248+
"--single-swift-file-output", singleSwiftFileOutputName
249+
]
250+
251+
// Add any dependent config files as arguments
252+
wrapJavaArguments += dependentConfigFilesArguments
253+
242254
commands += [
243255
.buildCommand(
244256
displayName: "Wrap compiled Java sources using wrap-java",
245257
executable: toolURL,
246-
arguments: [
247-
"wrap-java",
248-
"--swift-module", sourceModule.name,
249-
"--output-directory", outputSwiftDirectory.path(percentEncoded: false),
250-
"--config", swiftJavaConfigURL.path(percentEncoded: false),
251-
"--cp", swiftKitCoreClassPath.path(percentEncoded: false),
252-
"--single-swift-file-output", singleSwiftFileOutputName,
253-
],
258+
arguments: wrapJavaArguments,
254259
inputFiles: [swiftJavaConfigURL, swiftKitCoreClassPath],
255260
outputFiles: [outputSwiftDirectory.appending(path: singleSwiftFileOutputName)]
256261
)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 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 SwiftJava
16+
17+
public protocol CallbackProtocol {
18+
func withBool(_ input: Bool) -> Bool
19+
func withInt8(_ input: Int8) -> Int8
20+
func withUInt16(_ input: UInt16) -> UInt16
21+
func withInt16(_ input: Int16) -> Int16
22+
func withInt32(_ input: Int32) -> Int32
23+
func withInt64(_ input: Int64) -> Int64
24+
func withFloat(_ input: Float) -> Float
25+
func withDouble(_ input: Double) -> Double
26+
func withString(_ input: String) -> String
27+
func withVoid()
28+
func withObject(_ input: MySwiftClass) -> MySwiftClass
29+
func withOptionalObject(_ input: MySwiftClass?) -> Optional<MySwiftClass>
30+
}
31+
32+
public struct CallbackOutput {
33+
public let bool: Bool
34+
public let int8: Int8
35+
public let uint16: UInt16
36+
public let int16: Int16
37+
public let int32: Int32
38+
public let int64: Int64
39+
public let _float: Float
40+
public let _double: Double
41+
public let string: String
42+
public let object: MySwiftClass
43+
public let optionalObject: MySwiftClass?
44+
}
45+
46+
public func outputCallbacks(
47+
_ callbacks: some CallbackProtocol,
48+
bool: Bool,
49+
int8: Int8,
50+
uint16: UInt16,
51+
int16: Int16,
52+
int32: Int32,
53+
int64: Int64,
54+
_float: Float,
55+
_double: Double,
56+
string: String,
57+
object: MySwiftClass,
58+
optionalObject: MySwiftClass?
59+
) -> CallbackOutput {
60+
return CallbackOutput(
61+
bool: callbacks.withBool(bool),
62+
int8: callbacks.withInt8(int8),
63+
uint16: callbacks.withUInt16(uint16),
64+
int16: callbacks.withInt16(int16),
65+
int32: callbacks.withInt32(int32),
66+
int64: callbacks.withInt64(int64),
67+
_float: callbacks.withFloat(_float),
68+
_double: callbacks.withDouble(_double),
69+
string: callbacks.withString(string),
70+
object: callbacks.withObject(object),
71+
optionalObject: callbacks.withOptionalObject(optionalObject)
72+
)
73+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
package com.example.swift;
16+
17+
import org.junit.jupiter.api.Test;
18+
import org.swift.swiftkit.core.SwiftArena;
19+
import org.swift.swiftkit.core.annotations.Unsigned;
20+
21+
import java.util.Optional;
22+
23+
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
import static org.junit.jupiter.api.Assertions.assertTrue;
25+
26+
public class ProtocolCallbacksTest {
27+
static class JavaCallbacks implements CallbackProtocol {
28+
@Override
29+
public boolean withBool(boolean input) {
30+
return input;
31+
}
32+
33+
@Override
34+
public byte withInt8(byte input) {
35+
return input;
36+
}
37+
38+
@Override
39+
public @Unsigned char withUInt16(char input) {
40+
return input;
41+
}
42+
43+
@Override
44+
public short withInt16(short input) {
45+
return input;
46+
}
47+
48+
@Override
49+
public int withInt32(int input) {
50+
return input;
51+
}
52+
53+
@Override
54+
public long withInt64(long input) {
55+
return input;
56+
}
57+
58+
@Override
59+
public float withFloat(float input) {
60+
return input;
61+
}
62+
63+
@Override
64+
public double withDouble(double input) {
65+
return input;
66+
}
67+
68+
@Override
69+
public String withString(String input) {
70+
return input;
71+
}
72+
73+
@Override
74+
public void withVoid() {}
75+
76+
@Override
77+
public MySwiftClass withObject(MySwiftClass input) {
78+
return input;
79+
}
80+
81+
@Override
82+
public Optional<MySwiftClass> withOptionalObject(Optional<MySwiftClass> input) {
83+
return input;
84+
}
85+
}
86+
87+
@Test
88+
void primitiveCallbacks() {
89+
try (var arena = SwiftArena.ofConfined()) {
90+
JavaCallbacks callbacks = new JavaCallbacks();
91+
var object = MySwiftClass.init(5, 3, arena);
92+
var optionalObject = Optional.of(MySwiftClass.init(10, 10, arena));
93+
var output = MySwiftLibrary.outputCallbacks(callbacks, true, (byte) 1, (char) 16, (short) 16, (int) 32, 64L, 1.34f, 1.34, "Hello from Java!", object, optionalObject, arena);
94+
95+
assertEquals(1, output.getInt8());
96+
assertEquals(16, output.getUint16());
97+
assertEquals(16, output.getInt16());
98+
assertEquals(32, output.getInt32());
99+
assertEquals(64, output.getInt64());
100+
assertEquals(1.34f, output.get_float());
101+
assertEquals(1.34, output.get_double());
102+
assertEquals("Hello from Java!", output.getString());
103+
assertEquals(5, output.getObject(arena).getX());
104+
assertEquals(3, output.getObject(arena).getY());
105+
106+
var optionalObjectOutput = output.getOptionalObject(arena);
107+
assertTrue(optionalObjectOutput.isPresent());
108+
assertEquals(10, optionalObjectOutput.get().getX());
109+
}
110+
}
111+
}

Sources/JExtractSwiftLib/ImportedDecls.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,12 @@ extension ImportedFunc {
260260
}
261261
}
262262
}
263+
264+
extension ImportedNominalType: Hashable {
265+
public func hash(into hasher: inout Hasher) {
266+
hasher.combine(ObjectIdentifier(self))
267+
}
268+
public static func == (lhs: ImportedNominalType, rhs: ImportedNominalType) -> Bool {
269+
return lhs === rhs
270+
}
271+
}

0 commit comments

Comments
 (0)