forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySwiftLibrary.swift
More file actions
142 lines (113 loc) · 3.44 KB
/
MySwiftLibrary.swift
File metadata and controls
142 lines (113 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
// This is a "plain Swift" file containing various types of declarations,
// that is exported to Java by using the `jextract-swift` tool.
//
// No annotations are necessary on the Swift side to perform the export.
import Foundation
#if os(Linux)
import Glibc
#else
import Darwin.C
#endif
public func helloWorld() {
}
public func globalTakeInt(i: Int) {
}
public func globalMakeInt() -> Int {
42
}
public func globalWriteString(string: String) -> Int {
string.count
}
public func globalTakeIntInt(i: Int, j: Int) {
}
public func globalCallMeRunnable(run: () -> Void) {
run()
}
public func globalReceiveRawBuffer(buf: UnsafeRawBufferPointer) -> Int {
buf.count
}
public var globalBuffer: UnsafeRawBufferPointer = UnsafeRawBufferPointer(
UnsafeMutableRawBufferPointer.allocate(byteCount: 124, alignment: 1)
)
public func globalReceiveReturnData(data: Data) -> Data {
Data(data)
}
public func withBuffer(body: (UnsafeRawBufferPointer) -> Void) {
body(globalBuffer)
}
public func getArray() -> [UInt8] {
[1, 2, 3]
}
// Tuple round-trips for jextract FFM (see `FFMTupleTest` in the sample app).
public func ffmTupleReturnPair() -> (Int32, Int64) {
(42, 43)
}
public func ffmTupleSumPair(_ arg: (Int32, Int64)) -> Int64 {
Int64(arg.0) + arg.1
}
public func ffmTupleLabeledPair() -> (x: Int32, y: Int32) {
(x: 10, y: 20)
}
public func sumAllByteArrayElements(actuallyAnArray: UnsafeRawPointer, count: Int) -> Int {
let bufferPointer = UnsafeRawBufferPointer(start: actuallyAnArray, count: count)
let array = Array(bufferPointer)
return Int(array.reduce(0, { partialResult, element in partialResult + element }))
}
public func sumAllByteArrayElements(array: [UInt8]) -> Int {
Int(array.reduce(0, { partialResult, element in partialResult + element }))
}
public func returnSwiftArray() -> [UInt8] {
[1, 2, 3, 4]
}
public func withArray(body: ([UInt8]) -> Void) {
body([1, 2, 3])
}
public func globalReceiveSomeDataProtocol(data: some DataProtocol) -> Int {
p(Array(data).description)
return data.count
}
public func globalReceiveOptional(o1: Int?, o2: (some DataProtocol)?) -> Int {
switch (o1, o2) {
case (nil, nil):
return 0
case (let v1?, nil):
return 1
case (nil, let v2?):
return 2
case (let v1?, let v2?):
return 3
}
}
// ==== -----------------------------------------------------------------------
// MARK: Overloaded functions
public func globalOverloaded(a: Int) {
p("globalOverloaded(a: \(a))")
}
public func globalOverloaded(b: Int) {
p("globalOverloaded(b: \(b))")
}
// ==== Internal helpers
func p(_ msg: String, file: String = #fileID, line: UInt = #line, function: String = #function) {
print("[swift][\(file):\(line)](\(function)) \(msg)")
fflush(stdout)
}
#if os(Linux)
// FIXME: why do we need this workaround?
@_silgen_name("_objc_autoreleaseReturnValue")
public func _objc_autoreleaseReturnValue(a: Any) {}
@_silgen_name("objc_autoreleaseReturnValue")
public func objc_autoreleaseReturnValue(a: Any) {}
#endif