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+ final class OptionalImportTests {
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: $fake
32+ public func globalGetStringOptional() -> String?
33+
34+ // MANGLED NAME: $fake
35+ public func globalGetIntOptional() -> Int?
36+
37+ // FIXME: Hack to allow us to translate " String " , even though it's not
38+ // actually available
39+ // MANGLED NAME: $ss
40+ public class String {
41+ }
42+ """
43+
44+ @Test ( " Import: public func globalGetIntOptional() -> Int? " )
45+ func globalGetIntOptional( ) throws {
46+ let st = Swift2JavaTranslator (
47+ javaPackage: " com.example.swift " ,
48+ swiftModuleName: " __FakeModule "
49+ )
50+ st. log. logLevel = . warning
51+
52+ try st. analyze ( swiftInterfacePath: " /fake/Fake.swiftinterface " , text: class_interfaceFile)
53+
54+ let funcDecl = st. importedGlobalFuncs. first {
55+ $0. baseIdentifier == " globalGetIntOptional "
56+ } !
57+
58+ let output = CodePrinter . toString { printer in
59+ st. printFuncDowncallMethod ( & printer, decl: funcDecl, selfVariant: nil )
60+ }
61+
62+ assertOutput (
63+ output,
64+ expected:
65+ """
66+ /**
67+ * Downcall to Swift:
68+ * {@snippet lang=swift :
69+ * public func globalGetIntOptional() -> Int?
70+ * }
71+ */
72+ public static java.util.OptionalLong globalGetIntOptional() {
73+ var mh$ = globalGetIntOptional.HANDLE;
74+ try {
75+ if (TRACE_DOWNCALLS) {
76+ traceDowncall();
77+ }
78+ return (java.util.OptionalLong) mh$.invokeExact();
79+ } catch (Throwable ex$) {
80+ throw new AssertionError( " should not reach here " , ex$);
81+ }
82+ }
83+ """
84+ )
85+ }
86+
87+ @Test ( " Import: public func globalGetStringOptional() -> String? " )
88+ func globalGetStringOptional( ) throws {
89+ let st = Swift2JavaTranslator (
90+ javaPackage: " com.example.swift " ,
91+ swiftModuleName: " __FakeModule "
92+ )
93+ st. log. logLevel = . warning
94+
95+ try st. analyze ( swiftInterfacePath: " /fake/Fake.swiftinterface " , text: class_interfaceFile)
96+
97+ let funcDecl = st. importedGlobalFuncs. first {
98+ $0. baseIdentifier == " globalGetStringOptional "
99+ } !
100+
101+ let output = CodePrinter . toString { printer in
102+ st. printFuncDowncallMethod ( & printer, decl: funcDecl, selfVariant: nil )
103+ }
104+
105+ assertOutput (
106+ output,
107+ expected:
108+ """
109+ /**
110+ * Downcall to Swift:
111+ * {@snippet lang=swift :
112+ * public func globalGetStringOptional() -> String?
113+ * }
114+ */
115+ public static java.util.Optional<String> globalGetStringOptional() {
116+ var mh$ = globalGetStringOptional.HANDLE;
117+ try {
118+ if (TRACE_DOWNCALLS) {
119+ traceDowncall();
120+ }
121+ return (java.util.Optional<String>) mh$.invokeExact();
122+ } catch (Throwable ex$) {
123+ throw new AssertionError( " should not reach here " , ex$);
124+ }
125+ }
126+ """
127+ )
128+ }
129+ }
0 commit comments