|
| 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 JNIJavaKitTests { |
| 20 | + let source = |
| 21 | + """ |
| 22 | + public func function(javaLong: JavaLong, javaInteger: JavaInteger, int: Int64) {} |
| 23 | + """ |
| 24 | + |
| 25 | + let classLookupTable = [ |
| 26 | + "JavaLong": "java.lang.Long", |
| 27 | + "JavaInteger": "java.lang.Integer" |
| 28 | + ] |
| 29 | + |
| 30 | + @Test |
| 31 | + func function_javaBindings() throws { |
| 32 | + try assertOutput( |
| 33 | + input: source, |
| 34 | + .jni, |
| 35 | + .java, |
| 36 | + javaClassLookupTable: classLookupTable, |
| 37 | + expectedChunks: [ |
| 38 | + """ |
| 39 | + /** |
| 40 | + * Downcall to Swift: |
| 41 | + * {@snippet lang=swift : |
| 42 | + * public func function(javaLong: JavaLong, javaInteger: JavaInteger, int: Int64) |
| 43 | + * } |
| 44 | + */ |
| 45 | + public static void function(java.lang.Long javaLong, java.lang.Integer javaInteger, long int) { |
| 46 | + SwiftModule.$function(javaLong, javaInteger, int); |
| 47 | + } |
| 48 | + """, |
| 49 | + """ |
| 50 | + private static native void $function(java.lang.Long javaLong, java.lang.Integer javaInteger, long int); |
| 51 | + """ |
| 52 | + ] |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + func function_swiftThunks() throws { |
| 58 | + try assertOutput( |
| 59 | + input: source, |
| 60 | + .jni, |
| 61 | + .swift, |
| 62 | + detectChunkByInitialLines: 1, |
| 63 | + javaClassLookupTable: classLookupTable, |
| 64 | + expectedChunks: [ |
| 65 | + """ |
| 66 | + @_cdecl("Java_com_example_swift_SwiftModule__00024function__Ljava_lang_Long_2Ljava_lang_Integer_2J") |
| 67 | + func Java_com_example_swift_SwiftModule__00024function__Ljava_lang_Long_2Ljava_lang_Integer_2J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, javaLong: jobject, javaInteger: jobject, int: jlong) { |
| 68 | + SwiftModule.function(javaLong: JavaLong(javaThis: javaLong, environment: environment!), javaInteger: JavaInteger(javaThis: javaInteger, environment: environment!), int: Int64(fromJNI: int, in: environment!)) |
| 69 | + } |
| 70 | + """ |
| 71 | + ] |
| 72 | + ) |
| 73 | + } |
| 74 | +} |
0 commit comments