-
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathJSFunction.swift
More file actions
378 lines (339 loc) · 13.2 KB
/
JSFunction.swift
File metadata and controls
378 lines (339 loc) · 13.2 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import _CJavaScriptKit
/// `JSFunction` represents a function in JavaScript and supports new object instantiation.
/// This type can be callable as a function using `callAsFunction`.
///
/// e.g.
/// ```swift
/// let alert: JSFunction = JSObject.global.alert.function!
/// // Call `JSFunction` as a function
/// alert("Hello, world")
/// ```
///
/// - Note: In a future version, JSFunction will be unified with JSObject.
/// Consider using JSObject directly for new code, as all objects in JavaScript
/// can potentially be callable. JSFunction functionality is now available on JSObject.
public class JSFunction: JSObject {
#if !hasFeature(Embedded)
/// Call this function with given `arguments` and binding given `this` as context.
/// - Parameters:
/// - this: The value to be passed as the `this` parameter to this function.
/// - arguments: Arguments to be passed to this function.
/// - Returns: The result of this call.
@discardableResult
override public func callAsFunction(this: JSObject, arguments: [ConvertibleToJSValue]) -> JSValue {
invokeNonThrowingJSFunction(arguments: arguments, this: this).jsValue
}
/// Call this function with given `arguments`.
/// - Parameters:
/// - arguments: Arguments to be passed to this function.
/// - Returns: The result of this call.
@discardableResult
override public func callAsFunction(arguments: [ConvertibleToJSValue]) -> JSValue {
invokeNonThrowingJSFunction(arguments: arguments).jsValue
}
/// A variadic arguments version of `callAsFunction`.
@discardableResult
override public func callAsFunction(this: JSObject, _ arguments: ConvertibleToJSValue...) -> JSValue {
self.callAsFunction(this: this, arguments: arguments)
}
/// A variadic arguments version of `callAsFunction`.
@discardableResult
override public func callAsFunction(_ arguments: ConvertibleToJSValue...) -> JSValue {
self.callAsFunction(arguments: arguments)
}
/// Instantiate an object from this function as a constructor.
///
/// Guaranteed to return an object because either:
///
/// - a. the constructor explicitly returns an object, or
/// - b. the constructor returns nothing, which causes JS to return the `this` value, or
/// - c. the constructor returns undefined, null or a non-object, in which case JS also returns `this`.
///
/// - Parameter arguments: Arguments to be passed to this constructor function.
/// - Returns: A new instance of this constructor.
override public func new(arguments: [ConvertibleToJSValue]) -> JSObject {
arguments.withRawJSValues { rawValues in
rawValues.withUnsafeBufferPointer { bufferPointer in
JSObject(id: swjs_call_new(self.id, bufferPointer.baseAddress!, Int32(bufferPointer.count)))
}
}
}
/// A variadic arguments version of `new`.
override public func new(_ arguments: ConvertibleToJSValue...) -> JSObject {
new(arguments: arguments)
}
/// A modifier to call this function as a throwing function
///
///
/// ```javascript
/// function validateAge(age) {
/// if (age < 0) {
/// throw new Error("Invalid age");
/// }
/// }
/// ```
///
/// ```swift
/// let validateAge = JSObject.global.validateAge.function!
/// try validateAge.throws(20)
/// ```
public var `throws`: JSThrowingFunction {
JSThrowingFunction(self)
}
#endif
@discardableResult
override public func callAsFunction(arguments: [JSValue]) -> JSValue {
invokeNonThrowingJSFunction(arguments: arguments).jsValue
}
override public func new(arguments: [JSValue]) -> JSObject {
arguments.withRawJSValues { rawValues in
rawValues.withUnsafeBufferPointer { bufferPointer in
JSObject(id: swjs_call_new(self.id, bufferPointer.baseAddress!, Int32(bufferPointer.count)))
}
}
}
@available(*, unavailable, message: "Please use JSClosure instead")
public static func from(_: @escaping ([JSValue]) -> JSValue) -> JSFunction {
fatalError("unavailable")
}
override public var jsValue: JSValue {
.object(self)
}
}
#if hasFeature(Embedded)
// Overloads of `callAsFunction(ConvertibleToJSValue...) -> JSValue`
// for 0 through 7 arguments for Embedded Swift.
//
// These are required because the `ConvertibleToJSValue...` version is not
// available in Embedded Swift due to lack of support for existentials.
//
// Once Embedded Swift supports parameter packs/variadic generics, we can
// replace all variants with a single method each that takes a generic pack.
extension JSFunction {
@discardableResult
override public func callAsFunction(this: JSObject) -> JSValue {
invokeNonThrowingJSFunction(arguments: [], this: this).jsValue
}
@discardableResult
override public func callAsFunction(this: JSObject, _ arg0: some ConvertibleToJSValue) -> JSValue {
invokeNonThrowingJSFunction(arguments: [arg0.jsValue], this: this).jsValue
}
@discardableResult
override public func callAsFunction(
this: JSObject,
_ arg0: some ConvertibleToJSValue,
_ arg1: some ConvertibleToJSValue
) -> JSValue {
invokeNonThrowingJSFunction(arguments: [arg0.jsValue, arg1.jsValue], this: this).jsValue
}
@discardableResult
override public func callAsFunction(
this: JSObject,
_ arg0: some ConvertibleToJSValue,
_ arg1: some ConvertibleToJSValue,
_ arg2: some ConvertibleToJSValue
) -> JSValue {
invokeNonThrowingJSFunction(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue], this: this).jsValue
}
@discardableResult
override public func callAsFunction(
this: JSObject,
_ arg0: some ConvertibleToJSValue,
_ arg1: some ConvertibleToJSValue,
_ arg2: some ConvertibleToJSValue,
_ arg3: some ConvertibleToJSValue
) -> JSValue {
invokeNonThrowingJSFunction(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue], this: this)
.jsValue
}
@discardableResult
override public func callAsFunction(
this: JSObject,
_ arg0: some ConvertibleToJSValue,
_ arg1: some ConvertibleToJSValue,
_ arg2: some ConvertibleToJSValue,
_ arg3: some ConvertibleToJSValue,
_ arg4: some ConvertibleToJSValue
) -> JSValue {
invokeNonThrowingJSFunction(
arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue, arg4.jsValue],
this: this
).jsValue
}
@discardableResult
override public func callAsFunction(
this: JSObject,
_ arg0: some ConvertibleToJSValue,
_ arg1: some ConvertibleToJSValue,
_ arg2: some ConvertibleToJSValue,
_ arg3: some ConvertibleToJSValue,
_ arg4: some ConvertibleToJSValue,
_ arg5: some ConvertibleToJSValue
) -> JSValue {
invokeNonThrowingJSFunction(
arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue, arg4.jsValue, arg5.jsValue],
this: this
).jsValue
}
@discardableResult
override public func callAsFunction(
this: JSObject,
_ arg0: some ConvertibleToJSValue,
_ arg1: some ConvertibleToJSValue,
_ arg2: some ConvertibleToJSValue,
_ arg3: some ConvertibleToJSValue,
_ arg4: some ConvertibleToJSValue,
_ arg5: some ConvertibleToJSValue,
_ arg6: some ConvertibleToJSValue
) -> JSValue {
invokeNonThrowingJSFunction(
arguments: [
arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue, arg4.jsValue, arg5.jsValue, arg6.jsValue,
],
this: this
).jsValue
}
@discardableResult
override public func callAsFunction(this: JSObject, arguments: [JSValue]) -> JSValue {
invokeNonThrowingJSFunction(arguments: arguments, this: this).jsValue
}
@discardableResult
override public func callAsFunction() -> JSValue {
invokeNonThrowingJSFunction(arguments: []).jsValue
}
@discardableResult
override public func callAsFunction(_ arg0: some ConvertibleToJSValue) -> JSValue {
invokeNonThrowingJSFunction(arguments: [arg0.jsValue]).jsValue
}
@discardableResult
override public func callAsFunction(
_ arg0: some ConvertibleToJSValue,
_ arg1: some ConvertibleToJSValue
) -> JSValue {
invokeNonThrowingJSFunction(arguments: [arg0.jsValue, arg1.jsValue]).jsValue
}
@discardableResult
override public func callAsFunction(
_ arg0: some ConvertibleToJSValue,
_ arg1: some ConvertibleToJSValue,
_ arg2: some ConvertibleToJSValue
) -> JSValue {
invokeNonThrowingJSFunction(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue]).jsValue
}
@discardableResult
override public func callAsFunction(
_ arg0: some ConvertibleToJSValue,
_ arg1: some ConvertibleToJSValue,
_ arg2: some ConvertibleToJSValue,
_ arg3: some ConvertibleToJSValue
) -> JSValue {
invokeNonThrowingJSFunction(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue]).jsValue
}
@discardableResult
override public func callAsFunction(
_ arg0: some ConvertibleToJSValue,
_ arg1: some ConvertibleToJSValue,
_ arg2: some ConvertibleToJSValue,
_ arg3: some ConvertibleToJSValue,
_ arg4: some ConvertibleToJSValue
) -> JSValue {
invokeNonThrowingJSFunction(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue, arg4.jsValue])
.jsValue
}
@discardableResult
override public func callAsFunction(
_ arg0: some ConvertibleToJSValue,
_ arg1: some ConvertibleToJSValue,
_ arg2: some ConvertibleToJSValue,
_ arg3: some ConvertibleToJSValue,
_ arg4: some ConvertibleToJSValue,
_ arg5: some ConvertibleToJSValue
) -> JSValue {
invokeNonThrowingJSFunction(arguments: [
arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue, arg4.jsValue, arg5.jsValue,
]).jsValue
}
@discardableResult
override public func callAsFunction(
_ arg0: some ConvertibleToJSValue,
_ arg1: some ConvertibleToJSValue,
_ arg2: some ConvertibleToJSValue,
_ arg3: some ConvertibleToJSValue,
_ arg4: some ConvertibleToJSValue,
_ arg5: some ConvertibleToJSValue,
_ arg6: some ConvertibleToJSValue
) -> JSValue {
invokeNonThrowingJSFunction(arguments: [
arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue, arg4.jsValue, arg5.jsValue, arg6.jsValue,
]).jsValue
}
override public func new() -> JSObject {
new(arguments: [])
}
override public func new(_ arg0: some ConvertibleToJSValue) -> JSObject {
new(arguments: [arg0.jsValue])
}
override public func new(
_ arg0: some ConvertibleToJSValue,
_ arg1: some ConvertibleToJSValue
) -> JSObject {
new(arguments: [arg0.jsValue, arg1.jsValue])
}
override public func new(
_ arg0: some ConvertibleToJSValue,
_ arg1: some ConvertibleToJSValue,
_ arg2: some ConvertibleToJSValue
) -> JSObject {
new(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue])
}
override public func new(
_ arg0: some ConvertibleToJSValue,
_ arg1: some ConvertibleToJSValue,
_ arg2: some ConvertibleToJSValue,
_ arg3: some ConvertibleToJSValue
) -> JSObject {
new(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue])
}
override public func new(
_ arg0: some ConvertibleToJSValue,
_ arg1: some ConvertibleToJSValue,
_ arg2: some ConvertibleToJSValue,
_ arg3: some ConvertibleToJSValue,
_ arg4: some ConvertibleToJSValue
) -> JSObject {
new(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue, arg4.jsValue])
}
override public func new(
_ arg0: some ConvertibleToJSValue,
_ arg1: some ConvertibleToJSValue,
_ arg2: some ConvertibleToJSValue,
_ arg3: some ConvertibleToJSValue,
_ arg4: some ConvertibleToJSValue,
_ arg5: some ConvertibleToJSValue
) -> JSObject {
new(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue, arg4.jsValue, arg5.jsValue])
}
override public func new(
_ arg0: some ConvertibleToJSValue,
_ arg1: some ConvertibleToJSValue,
_ arg2: some ConvertibleToJSValue,
_ arg3: some ConvertibleToJSValue,
_ arg4: some ConvertibleToJSValue,
_ arg5: some ConvertibleToJSValue,
_ arg6: some ConvertibleToJSValue
) -> JSObject {
new(arguments: [
arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue, arg4.jsValue, arg5.jsValue, arg6.jsValue,
])
}
}
#endif
internal struct JavaScriptValueKindAndFlags {
static var errorBit: UInt32 { 1 << 31 }
let kind: JavaScriptValueKind
let isException: Bool
init(bitPattern: UInt32) {
self.kind = JavaScriptValueKind(rawValue: bitPattern & ~Self.errorBit)!
self.isException = (bitPattern & Self.errorBit) != 0
}
}