-
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathJSClosure.swift
More file actions
290 lines (253 loc) · 11.6 KB
/
JSClosure.swift
File metadata and controls
290 lines (253 loc) · 11.6 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
import _CJavaScriptKit
#if hasFeature(Embedded) && os(WASI)
import _Concurrency
#endif
/// `JSClosureProtocol` wraps Swift closure objects for use in JavaScript. Conforming types
/// are responsible for managing the lifetime of the closure they wrap, but can delegate that
/// task to the user by requiring an explicit `release()` call.
public protocol JSClosureProtocol: JSValueCompatible {
/// Release this function resource.
/// After calling `release`, calling this function from JavaScript will fail.
func release()
}
/// `JSOneshotClosure` is a JavaScript function that can be called only once. This class can be used
/// for optimized memory management when compared to the common `JSClosure`.
public class JSOneshotClosure: JSObject, JSClosureProtocol {
private var hostFuncRef: JavaScriptHostFuncRef = 0
public init(_ body: @escaping (sending [JSValue]) -> JSValue, file: String = #fileID, line: UInt32 = #line) {
// 1. Fill `id` as zero at first to access `self` to get `ObjectIdentifier`.
super.init(id: 0)
// 2. Create a new JavaScript function which calls the given Swift function.
hostFuncRef = JavaScriptHostFuncRef(bitPattern: ObjectIdentifier(self))
_id = withExtendedLifetime(JSString(file)) { file in
swjs_create_function(hostFuncRef, line, file.asInternalJSRef())
}
// 3. Retain the given body in static storage by `funcRef`.
JSClosure.sharedClosures.wrappedValue[hostFuncRef] = (
self,
{
defer { self.release() }
return body($0)
}
)
}
@available(*, unavailable, message: "JSOneshotClosure does not support dictionary literal initialization")
public required init(dictionaryLiteral elements: (String, JSValue)...) {
fatalError("JSOneshotClosure does not support dictionary literal initialization")
}
#if compiler(>=5.5) && (!hasFeature(Embedded) || os(WASI))
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public static func async(
_ body: sending @escaping (sending [JSValue]) async throws(JSException) -> JSValue
) -> JSOneshotClosure {
JSOneshotClosure(makeAsyncClosure(body))
}
#endif
/// Release this function resource.
/// After calling `release`, calling this function from JavaScript will fail.
public func release() {
JSClosure.sharedClosures.wrappedValue[hostFuncRef] = nil
}
}
/// `JSClosure` represents a JavaScript function the body of which is written in Swift.
/// This type can be passed as a callback handler to JavaScript functions.
///
/// e.g.
/// ```swift
/// let eventListener = JSClosure { _ in
/// ...
/// return JSValue.undefined
/// }
///
/// button.addEventListener!("click", JSValue.function(eventListener))
/// ...
/// button.removeEventListener!("click", JSValue.function(eventListener))
/// ```
///
public class JSClosure: JSFunction, JSClosureProtocol {
class SharedJSClosure {
// Note: 6.0 compilers built with assertions enabled crash when calling
// `removeValue(forKey:)` on a dictionary with value type containing
// `sending`. Wrap the value type with a struct to avoid the crash.
struct Entry {
let item: (object: JSObject, body: (sending [JSValue]) -> JSValue)
}
private var storage: [JavaScriptHostFuncRef: Entry] = [:]
init() {}
subscript(_ key: JavaScriptHostFuncRef) -> (object: JSObject, body: (sending [JSValue]) -> JSValue)? {
get { storage[key]?.item }
set { storage[key] = newValue.map { Entry(item: $0) } }
}
}
// Note: Retain the closure object itself also to avoid funcRef conflicts
fileprivate static let sharedClosures = LazyThreadLocal {
SharedJSClosure()
}
private var hostFuncRef: JavaScriptHostFuncRef = 0
#if JAVASCRIPTKIT_WITHOUT_WEAKREFS
private var isReleased: Bool = false
#endif
@available(
*,
deprecated,
message:
"This initializer will be removed in the next minor version update. Please use `init(_ body: @escaping ([JSValue]) -> JSValue)` and add `return .undefined` to the end of your closure"
)
@_disfavoredOverload
public convenience init(_ body: @escaping ([JSValue]) -> Void) {
self.init({
body($0)
return .undefined
})
}
public init(_ body: @escaping (sending [JSValue]) -> JSValue, file: String = #fileID, line: UInt32 = #line) {
// 1. Fill `id` as zero at first to access `self` to get `ObjectIdentifier`.
super.init(id: 0)
// 2. Create a new JavaScript function which calls the given Swift function.
hostFuncRef = JavaScriptHostFuncRef(bitPattern: ObjectIdentifier(self))
_id = withExtendedLifetime(JSString(file)) { file in
swjs_create_function(hostFuncRef, line, file.asInternalJSRef())
}
// 3. Retain the given body in static storage by `funcRef`.
Self.sharedClosures.wrappedValue[hostFuncRef] = (self, body)
}
@available(*, unavailable, message: "JSClosure does not support dictionary literal initialization")
public required init(dictionaryLiteral elements: (String, JSValue)...) {
fatalError("JSClosure does not support dictionary literal initialization")
}
#if compiler(>=5.5) && (!hasFeature(Embedded) || os(WASI))
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public static func async(
_ body: @Sendable @escaping (sending [JSValue]) async throws(JSException) -> JSValue
) -> JSClosure {
JSClosure(makeAsyncClosure(body))
}
#endif
#if JAVASCRIPTKIT_WITHOUT_WEAKREFS
deinit {
guard isReleased else {
fatalError("release() must be called on JSClosure objects manually before they are deallocated")
}
}
#endif
}
#if compiler(>=5.5) && (!hasFeature(Embedded) || os(WASI))
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
private func makeAsyncClosure(
_ body: sending @escaping (sending [JSValue]) async throws(JSException) -> JSValue
) -> ((sending [JSValue]) -> JSValue) {
{ arguments in
JSPromise { resolver in
// NOTE: The context is fully transferred to the unstructured task
// isolation but the compiler can't prove it yet, so we need to
// use `@unchecked Sendable` to make it compile with the Swift 6 mode.
struct Context: @unchecked Sendable {
let resolver: (JSPromise.Result) -> Void
let arguments: [JSValue]
let body: (sending [JSValue]) async throws(JSException) -> JSValue
}
let context = Context(resolver: resolver, arguments: arguments, body: body)
Task {
do throws(JSException) {
let result = try await context.body(context.arguments)
context.resolver(.success(result))
} catch {
context.resolver(.failure(error.thrownValue))
}
}
}.jsValue()
}
}
#endif
// MARK: - `JSClosure` mechanism note
//
// 1. Create a thunk in the JavaScript world, which has a reference
// to a Swift closure.
// ┌─────────────────────┬──────────────────────────┐
// │ Swift side │ JavaScript side │
// │ │ │
// │ │ │
// │ │ ┌──────[Thunk]───────┐ │
// │ ┌ ─ ─ ─ ─ ─│─ ─│─ ─ ─ ─ ─ ┐ │ │
// │ ↓ │ │ │ │ │
// │ [Swift Closure] │ │ Host Function ID │ │
// │ │ │ │ │
// │ │ └────────────────────┘ │
// └─────────────────────┴──────────────────────────┘
//
// 2. When the thunk function is invoked, it calls the Swift closure via
// `_call_host_function` and receives the result through a callback.
// ┌─────────────────────┬──────────────────────────┐
// │ Swift side │ JavaScript side │
// │ │ │
// │ │ │
// │ Apply ┌──────[Thunk]───────┐ │
// │ ┌ ─ ─ ─ ─ ─│─ ─│─ ─ ─ ─ ─ ┐ │ │
// │ ↓ │ │ │ │ │
// │ [Swift Closure] │ │ Host Function ID │ │
// │ │ │ │ │ │
// │ │ │ └────────────────────┘ │
// │ │ │ ↑ │
// │ │ Apply │ │
// │ └─[Result]─┼───>[Callback func]─┘ │
// │ │ │
// └─────────────────────┴──────────────────────────┘
/// Returns true if the host function has been already released, otherwise false.
@_cdecl("_call_host_function_impl")
func _call_host_function_impl(
_ hostFuncRef: JavaScriptHostFuncRef,
_ argv: UnsafePointer<RawJSValue>,
_ argc: Int32,
_ callbackFuncRef: JavaScriptObjectRef
) -> Bool {
guard let (_, hostFunc) = JSClosure.sharedClosures.wrappedValue[hostFuncRef] else {
return true
}
var arguments: [JSValue] = []
for i in 0..<Int(argc) {
arguments.append(argv[i].jsValue)
}
let result = hostFunc(arguments)
let callbackFuncRef = JSFunction(id: callbackFuncRef)
_ = callbackFuncRef(result)
return false
}
/// [WeakRefs](https://github.com/tc39/proposal-weakrefs) are already Stage 4,
/// but was added recently enough that older browser versions don’t support it.
/// Build with `-Xswiftc -DJAVASCRIPTKIT_WITHOUT_WEAKREFS` to disable the relevant behavior.
#if JAVASCRIPTKIT_WITHOUT_WEAKREFS
// MARK: - Legacy Closure Types
extension JSClosure {
public func release() {
isReleased = true
Self.sharedClosures.wrappedValue[hostFuncRef] = nil
}
}
@_cdecl("_free_host_function_impl")
func _free_host_function_impl(_ hostFuncRef: JavaScriptHostFuncRef) {}
#else
extension JSClosure {
@available(*, deprecated, message: "JSClosure.release() is no longer necessary")
public func release() {}
}
@_cdecl("_free_host_function_impl")
func _free_host_function_impl(_ hostFuncRef: JavaScriptHostFuncRef) {
JSClosure.sharedClosures.wrappedValue[hostFuncRef] = nil
}
#endif
#if compiler(>=6.0) && hasFeature(Embedded)
// cdecls currently don't work in embedded, and expose for wasm only works >=6.0
@_expose(wasm, "swjs_call_host_function")
public func _swjs_call_host_function(
_ hostFuncRef: JavaScriptHostFuncRef,
_ argv: UnsafePointer<RawJSValue>,
_ argc: Int32,
_ callbackFuncRef: JavaScriptObjectRef
) -> Bool {
_call_host_function_impl(hostFuncRef, argv, argc, callbackFuncRef)
}
@_expose(wasm, "swjs_free_host_function")
public func _swjs_free_host_function(_ hostFuncRef: JavaScriptHostFuncRef) {
_free_host_function_impl(hostFuncRef)
}
#endif