-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathJavaPrimitive.swift
More file actions
247 lines (194 loc) · 5.42 KB
/
Copy pathJavaPrimitive.swift
File metadata and controls
247 lines (194 loc) · 5.42 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//
// Created by Andriy Druk on 24.01.2020.
//
import Foundation
import java_swift
public typealias JavaBoolean = jboolean
public typealias JavaByte = jbyte
public typealias JavaShort = jshort
public typealias JavaInt = jint
public typealias JavaLong = jlong
#if arch(arm)
// Looks like calling convention for ARM32 is broken: probably soft-float vs hard-float
// https://android.googlesource.com/platform/ndk/+/master/docs/HardFloatAbi.md
// We will replace jfloat with 4-byte jint bit pattern
public typealias JavaFloat = jint
#else
public typealias JavaFloat = jfloat
#endif
#if arch(arm)
// We will replace jdouble with 8-byte jlong bit pattern
public typealias JavaDouble = jlong
#else
public typealias JavaDouble = jdouble
#endif
extension Bool {
public init(fromJavaPrimitive javaPrimitive: JavaBoolean) {
self.init(javaPrimitive == JNI_TRUE)
}
public func javaPrimitive() throws -> JavaBoolean {
return jboolean(self ? JNI_TRUE : JNI_FALSE)
}
}
extension Int {
public init(fromJavaPrimitive javaPrimitive: JavaInt) {
self.init(javaPrimitive)
}
public func javaPrimitive(codingPath: [CodingKey] = []) throws -> JavaInt {
if self < Int(Int32.min) || self > Int(Int32.max) {
let errorDescription = "Not enough bits to represent Int"
let context = EncodingError.Context(codingPath: codingPath, debugDescription: errorDescription)
throw EncodingError.invalidValue(self, context)
}
return jint(self)
}
}
extension Int8 {
public init(fromJavaPrimitive javaPrimitive: JavaByte) {
self.init(javaPrimitive)
}
public func javaPrimitive() throws -> JavaByte {
return jbyte(self)
}
}
extension Int16 {
public init(fromJavaPrimitive javaPrimitive: JavaShort) {
self.init(javaPrimitive)
}
public func javaPrimitive() throws -> JavaShort {
return jshort(self)
}
}
extension Int32 {
public init(fromJavaPrimitive javaPrimitive: JavaInt) {
self.init(javaPrimitive)
}
public func javaPrimitive() throws -> JavaInt {
return jint(self)
}
}
extension Int64 {
public init(fromJavaPrimitive javaPrimitive: JavaLong) {
self.init(javaPrimitive)
}
public func javaPrimitive() throws -> JavaLong {
return jlong(self)
}
}
extension UInt {
public init(fromJavaPrimitive javaPrimitive: JavaInt) {
self.init(UInt32(bitPattern: javaPrimitive))
}
public func javaPrimitive(codingPath: [CodingKey] = []) throws -> JavaInt {
if self < UInt(UInt32.min) || self > UInt(UInt32.max) {
let errorDescription = "Not enough bits to represent UInt"
let context = EncodingError.Context(codingPath: codingPath, debugDescription: errorDescription)
throw EncodingError.invalidValue(self, context)
}
return jint(bitPattern: UInt32(self))
}
}
extension UInt8 {
public init(fromJavaPrimitive javaPrimitive: JavaByte) {
self.init(bitPattern: javaPrimitive)
}
public func javaPrimitive() throws -> JavaByte {
return jbyte(bitPattern: self)
}
}
extension UInt16 {
public init(fromJavaPrimitive javaPrimitive: JavaShort) {
self.init(bitPattern: javaPrimitive)
}
public func javaPrimitive() throws -> JavaShort {
return jshort(bitPattern: self)
}
}
extension UInt32 {
public init(fromJavaPrimitive javaPrimitive: JavaInt) {
self.init(bitPattern: javaPrimitive)
}
public func javaPrimitive() throws -> JavaInt {
return jint(bitPattern: self)
}
}
extension UInt64 {
public init(fromJavaPrimitive javaPrimitive: JavaLong) {
self.init(bitPattern: javaPrimitive)
}
public func javaPrimitive() throws -> JavaLong {
return jlong(bitPattern: self)
}
}
extension Float {
public init(fromJavaPrimitive javaPrimitive: jfloat) {
self.init(javaPrimitive)
}
#if arch(arm)
public init(fromJavaPrimitive javaPrimitive: jint) {
self.init(bitPattern: UInt32(bitPattern: Int32(javaPrimitive)))
}
#endif
public func javaPrimitive() throws -> JavaFloat {
#if arch(arm)
return jint(Int32(bitPattern: bitPattern))
#else
return self
#endif
}
}
extension Double {
public init(fromJavaPrimitive javaPrimitive: jdouble) {
self.init(javaPrimitive)
}
#if arch(arm)
public init(fromJavaPrimitive javaPrimitive: jlong) {
self.init(bitPattern: UInt64(javaPrimitive))
}
#endif
public func javaPrimitive() throws -> JavaDouble {
#if arch(arm)
return jlong(bitPattern: bitPattern)
#else
return self
#endif
}
}
extension JavaBoolean {
public static func defaultValue() -> JavaBoolean {
return jboolean(JNI_FALSE)
}
}
extension JavaByte {
public static func defaultValue() -> JavaByte {
return 0
}
}
extension JavaShort {
public static func defaultValue() -> JavaShort {
return 0
}
}
extension JavaInt {
public static func defaultValue() -> JavaInt {
return 0
}
}
extension JavaLong {
public static func defaultValue() -> JavaLong {
return 0
}
}
#if arch(arm)
#else
extension JavaFloat {
public static func defaultValue() -> JavaFloat {
return 0
}
}
extension JavaDouble {
public static func defaultValue() -> JavaDouble {
return 0
}
}
#endif