forked from swiftwasm/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructAPIs.swift
More file actions
275 lines (223 loc) · 6.04 KB
/
StructAPIs.swift
File metadata and controls
275 lines (223 loc) · 6.04 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import JavaScriptKit
@JS struct PointerFields {
var raw: UnsafeRawPointer
var mutRaw: UnsafeMutableRawPointer
var opaque: OpaquePointer
var ptr: UnsafePointer<UInt8>
var mutPtr: UnsafeMutablePointer<UInt8>
@JS init(
raw: UnsafeRawPointer,
mutRaw: UnsafeMutableRawPointer,
opaque: OpaquePointer,
ptr: UnsafePointer<UInt8>,
mutPtr: UnsafeMutablePointer<UInt8>
) {
self.raw = raw
self.mutRaw = mutRaw
self.opaque = opaque
self.ptr = ptr
self.mutPtr = mutPtr
}
}
@JS func roundTripPointerFields(_ value: PointerFields) -> PointerFields { value }
@JS struct DataPoint {
let x: Double
let y: Double
var label: String
var optCount: Int?
var optFlag: Bool?
@JS init(x: Double, y: Double, label: String, optCount: Int?, optFlag: Bool?) {
self.x = x
self.y = y
self.label = label
self.optCount = optCount
self.optFlag = optFlag
}
}
@JS public struct PublicPoint {
public var x: Int
public var y: Int
@JS public init(x: Int, y: Int) {
self.x = x
self.y = y
}
}
@JS struct Address {
var street: String
var city: String
var zipCode: Int?
}
@JS struct Contact {
var name: String
var age: Int
var address: Address
var email: String?
var secondaryAddress: Address?
}
@JS struct Config {
var name: String
var theme: Theme?
var direction: Direction?
var status: Status
}
@JS struct SessionData {
var id: Int
var owner: Greeter?
}
@JS struct ValidationReport {
var id: Int
var result: APIResult
var status: Status?
var outcome: APIResult?
}
@JS struct AdvancedConfig {
var id: Int
var title: String
var enabled: Bool
var theme: Theme
var status: Status
var result: APIResult?
var metadata: JSObject?
var location: DataPoint?
var defaults: ConfigStruct
var overrideDefaults: ConfigStruct?
}
@JS struct MeasurementConfig {
var precision: Precision
var ratio: Ratio
var optionalPrecision: Precision?
var optionalRatio: Ratio?
}
@JS struct MathOperations {
var baseValue: Double
@JS init(baseValue: Double = 0.0) {
self.baseValue = baseValue
}
@JS func add(a: Double, b: Double = 10.0) -> Double {
return baseValue + a + b
}
@JS func multiply(a: Double, b: Double) -> Double {
return a * b
}
@JS static func subtract(a: Double, b: Double = 5.0) -> Double {
return a - b
}
}
@JS func testStructDefault(
point: DataPoint = DataPoint(x: 1.0, y: 2.0, label: "default", optCount: nil, optFlag: nil)
) -> String {
return "\(point.x),\(point.y),\(point.label)"
}
@JS struct CopyableCart {
var x: Int
var note: String?
@JS static func fromJSObject(_ object: JSObject) -> CopyableCart {
CopyableCart(unsafelyCopying: object)
}
}
@JS func cartToJSObject(_ cart: CopyableCart) -> JSObject {
cart.toJSObject()
}
@JS struct CopyableCartItem {
var sku: String
var quantity: Int
}
@JS struct CopyableNestedCart {
var id: Int
var item: CopyableCartItem
var shippingAddress: Address?
@JS static func fromJSObject(_ object: JSObject) -> CopyableNestedCart {
CopyableNestedCart(unsafelyCopying: object)
}
}
@JS func nestedCartToJSObject(_ cart: CopyableNestedCart) -> JSObject {
cart.toJSObject()
}
@JS struct ConfigStruct {
var name: String
var value: Int
@JS nonisolated(unsafe) static var defaultConfig: String = "production"
@JS static let maxRetries: Int = 3
@JS nonisolated(unsafe) static var timeout: Double = 30.0
@JS static var computedSetting: String {
return "Config: \(defaultConfig)"
}
}
@JS func roundTripDataPoint(_ data: DataPoint) -> DataPoint {
return data
}
@JS public func roundTripPublicPoint(_ point: PublicPoint) -> PublicPoint {
point
}
@JS func roundTripContact(_ contact: Contact) -> Contact {
return contact
}
@JS func roundTripConfig(_ config: Config) -> Config {
return config
}
@JS func roundTripSessionData(_ session: SessionData) -> SessionData {
return session
}
@JS func roundTripValidationReport(_ report: ValidationReport) -> ValidationReport {
return report
}
@JS func roundTripAdvancedConfig(_ config: AdvancedConfig) -> AdvancedConfig {
return config
}
@JS func roundTripMeasurementConfig(_ config: MeasurementConfig) -> MeasurementConfig {
return config
}
@JS func updateValidationReport(_ newResult: APIResult?, _ report: ValidationReport) -> ValidationReport {
return ValidationReport(
id: report.id,
result: newResult ?? report.result,
status: report.status,
outcome: report.outcome
)
}
@JS class Container {
@JS var location: DataPoint
@JS var config: Config?
@JS init(location: DataPoint, config: Config?) {
self.location = location
self.config = config
}
}
@JS func testContainerWithStruct(_ point: DataPoint) -> Container {
return Container(location: point, config: nil)
}
// Struct with JSObject fields
@JS struct JSObjectContainer {
var object: JSObject
var optionalObject: JSObject?
}
@JS func roundTripJSObjectContainer(_ container: JSObjectContainer) -> JSObjectContainer {
return container
}
// Struct with @JSClass fields (Foo is defined in ExportAPITests.swift)
@JS struct FooContainer {
var foo: Foo
var optionalFoo: Foo?
}
@JS func roundTripFooContainer(_ container: FooContainer) -> FooContainer {
return container
}
@JS struct ArrayMembers {
var ints: [Int]
var optStrings: [String]?
@JS func sumValues(_ values: [Int]) -> Int {
values.reduce(0, +)
}
@JS func firstString(_ values: [String]) -> String? {
values.first
}
}
@JS func roundTripArrayMembers(_ value: ArrayMembers) -> ArrayMembers {
value
}
@JS func arrayMembersSum(_ value: ArrayMembers, _ values: [Int]) -> Int {
value.sumValues(values)
}
@JS func arrayMembersFirst(_ value: ArrayMembers, _ values: [String]) -> String? {
value.firstString(values)
}