Skip to content

Commit d4c66c8

Browse files
committed
add codegen tests
1 parent 0e798f0 commit d4c66c8

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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 JExtractSwiftLib
16+
import Testing
17+
18+
@Suite
19+
struct JNIClosureTests {
20+
let source =
21+
"""
22+
public func emptyClosure(closure: () -> ()) {}
23+
public func closureWithArgumentsAndReturn(closure: (Int64, Bool) -> Int64) {}
24+
"""
25+
26+
@Test
27+
func emptyClosure_javaBindings() throws {
28+
try assertOutput(input: source, .jni, .java, expectedChunks: [
29+
"""
30+
public static class emptyClosure {
31+
@FunctionalInterface
32+
public interface closure {
33+
void apply();
34+
}
35+
}
36+
""",
37+
"""
38+
/**
39+
* Downcall to Swift:
40+
* {@snippet lang=swift :
41+
* public func emptyClosure(closure: () -> ())
42+
* }
43+
*/
44+
public static void emptyClosure(com.example.swift.SwiftModule.emptyClosure.closure closure) {
45+
SwiftModule.$emptyClosure(closure);
46+
}
47+
""",
48+
"""
49+
private static native void $emptyClosure(com.example.swift.SwiftModule.emptyClosure.closure closure);
50+
"""
51+
])
52+
}
53+
54+
@Test
55+
func emptyClosure_swiftThunks() throws {
56+
try assertOutput(
57+
input: source,
58+
.jni,
59+
.swift,
60+
detectChunkByInitialLines: 1,
61+
expectedChunks: [
62+
"""
63+
@_cdecl("Java_com_example_swift_SwiftModule__00024emptyClosure__Lcom_example_swift_SwiftModule_00024emptyClosure_00024closure_2")
64+
func Java_com_example_swift_SwiftModule__00024emptyClosure__Lcom_example_swift_SwiftModule_00024emptyClosure_00024closure_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, closure: jobject) {
65+
SwiftModule.emptyClosure(closure: {
66+
let class$ = environment!.interface.GetObjectClass(environment, closure)
67+
let methodID$ = environment!.interface.GetMethodID(environment, class$, "apply", "()V")!
68+
let arguments$: [jvalue] = []
69+
environment!.interface.CallVoidMethodA(environment, closure, methodID$, arguments$)
70+
}
71+
)
72+
}
73+
"""
74+
]
75+
)
76+
}
77+
78+
@Test
79+
func closureWithArgumentsAndReturn_javaBindings() throws {
80+
try assertOutput(input: source, .jni, .java, expectedChunks: [
81+
"""
82+
public static class closureWithArgumentsAndReturn {
83+
@FunctionalInterface
84+
public interface closure {
85+
long apply(long _0, boolean _1);
86+
}
87+
}
88+
""",
89+
"""
90+
/**
91+
* Downcall to Swift:
92+
* {@snippet lang=swift :
93+
* public func closureWithArgumentsAndReturn(closure: (Int64, Bool) -> Int64)
94+
* }
95+
*/
96+
public static void closureWithArgumentsAndReturn(com.example.swift.SwiftModule.closureWithArgumentsAndReturn.closure closure) {
97+
SwiftModule.$closureWithArgumentsAndReturn(closure);
98+
}
99+
""",
100+
"""
101+
private static native void $closureWithArgumentsAndReturn(com.example.swift.SwiftModule.closureWithArgumentsAndReturn.closure closure);
102+
"""
103+
])
104+
}
105+
106+
@Test
107+
func closureWithArgumentsAndReturn_swiftThunks() throws {
108+
try assertOutput(
109+
input: source,
110+
.jni,
111+
.swift,
112+
detectChunkByInitialLines: 1,
113+
expectedChunks: [
114+
"""
115+
@_cdecl("Java_com_example_swift_SwiftModule__00024closureWithArgumentsAndReturn__Lcom_example_swift_SwiftModule_00024closureWithArgumentsAndReturn_00024closure_2")
116+
func Java_com_example_swift_SwiftModule__00024closureWithArgumentsAndReturn__Lcom_example_swift_SwiftModule_00024closureWithArgumentsAndReturn_00024closure_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, closure: jobject) {
117+
SwiftModule.closureWithArgumentsAndReturn(closure: { _0, _1 in
118+
let class$ = environment!.interface.GetObjectClass(environment, closure)
119+
let methodID$ = environment!.interface.GetMethodID(environment, class$, "apply", "(JZ)J")!
120+
let arguments$: [jvalue] = [_0.getJValue(in: environment!), _1.getJValue(in: environment!)]
121+
return Int64(fromJNI: environment!.interface.CallLongMethodA(environment, closure, methodID$, arguments$), in: environment!)
122+
}
123+
)
124+
}
125+
"""
126+
]
127+
)
128+
}
129+
}

0 commit comments

Comments
 (0)