@@ -26,7 +26,7 @@ public class JSOneshotClosure: JSObject, JSClosureProtocol {
26
26
}
27
27
28
28
// 3. Retain the given body in static storage by `funcRef`.
29
- JSClosure . sharedClosures [ hostFuncRef] = ( self , {
29
+ JSClosure . sharedClosures. wrappedValue [ hostFuncRef] = ( self , {
30
30
defer { self . release ( ) }
31
31
return body ( $0)
32
32
} )
@@ -42,7 +42,7 @@ public class JSOneshotClosure: JSObject, JSClosureProtocol {
42
42
/// Release this function resource.
43
43
/// After calling `release`, calling this function from JavaScript will fail.
44
44
public func release( ) {
45
- JSClosure . sharedClosures [ hostFuncRef] = nil
45
+ JSClosure . sharedClosures. wrappedValue [ hostFuncRef] = nil
46
46
}
47
47
}
48
48
@@ -74,14 +74,8 @@ public class JSClosure: JSFunction, JSClosureProtocol {
74
74
}
75
75
76
76
// Note: Retain the closure object itself also to avoid funcRef conflicts
77
- fileprivate static var sharedClosures : SharedJSClosure {
78
- if let swjs_thread_local_closures {
79
- return Unmanaged < SharedJSClosure > . fromOpaque ( swjs_thread_local_closures) . takeUnretainedValue ( )
80
- } else {
81
- let shared = SharedJSClosure ( )
82
- swjs_thread_local_closures = Unmanaged . passRetained ( shared) . toOpaque ( )
83
- return shared
84
- }
77
+ fileprivate static let sharedClosures = LazyThreadLocal {
78
+ SharedJSClosure ( )
85
79
}
86
80
87
81
private var hostFuncRef : JavaScriptHostFuncRef = 0
@@ -110,7 +104,7 @@ public class JSClosure: JSFunction, JSClosureProtocol {
110
104
}
111
105
112
106
// 3. Retain the given body in static storage by `funcRef`.
113
- Self . sharedClosures [ hostFuncRef] = ( self , body)
107
+ Self . sharedClosures. wrappedValue [ hostFuncRef] = ( self , body)
114
108
}
115
109
116
110
#if compiler(>=5.5) && !hasFeature(Embedded)
@@ -192,7 +186,7 @@ func _call_host_function_impl(
192
186
_ argv: UnsafePointer < RawJSValue > , _ argc: Int32 ,
193
187
_ callbackFuncRef: JavaScriptObjectRef
194
188
) -> Bool {
195
- guard let ( _, hostFunc) = JSClosure . sharedClosures [ hostFuncRef] else {
189
+ guard let ( _, hostFunc) = JSClosure . sharedClosures. wrappedValue [ hostFuncRef] else {
196
190
return true
197
191
}
198
192
let arguments = UnsafeBufferPointer ( start: argv, count: Int ( argc) ) . map { $0. jsValue}
@@ -232,7 +226,7 @@ extension JSClosure {
232
226
233
227
@_cdecl ( " _free_host_function_impl " )
234
228
func _free_host_function_impl( _ hostFuncRef: JavaScriptHostFuncRef ) {
235
- JSClosure . sharedClosures [ hostFuncRef] = nil
229
+ JSClosure . sharedClosures. wrappedValue [ hostFuncRef] = nil
236
230
}
237
231
#endif
238
232
@@ -251,4 +245,4 @@ public func _swjs_call_host_function(
251
245
public func _swjs_free_host_function( _ hostFuncRef: JavaScriptHostFuncRef ) {
252
246
_free_host_function_impl ( hostFuncRef)
253
247
}
254
- #endif
248
+ #endif
0 commit comments