Skip to content

Commit 28f3471

Browse files
Concurrency: Use LazyThreadLocal without @pw syntax
Unfortunately, `@LazyThreadLocal static var` is considered as concurrency-unsafe in Swift 6 mode even though the underlying PW storage is read-only and concurrency-safe. Also Swift bans `static let` with `@propertyWrapper` syntax, so we need to use `LazyThreadLocal` directly. See the discussion in the Swift forum: https://forums.swift.org/t/static-property-wrappers-and-strict-concurrency-in-5-10/70116/27
1 parent 5148e4f commit 28f3471

File tree

5 files changed

+15
-23
lines changed

5 files changed

+15
-23
lines changed

Sources/JavaScriptKit/BasicObjects/JSArray.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
/// class](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)
33
/// that exposes its properties in a type-safe and Swifty way.
44
public class JSArray: JSBridgedClass {
5-
public static var constructor: JSFunction? { _constructor }
6-
@LazyThreadLocal(initialize: { JSObject.global.Array.function })
7-
private static var _constructor: JSFunction?
5+
public static var constructor: JSFunction? { _constructor.wrappedValue }
6+
private static let _constructor = LazyThreadLocal(initialize: { JSObject.global.Array.function })
87

98
static func isArray(_ object: JSObject) -> Bool {
109
constructor!.isArray!(object).boolean!

Sources/JavaScriptKit/BasicObjects/JSDate.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
*/
99
public final class JSDate: JSBridgedClass {
1010
/// The constructor function used to create new `Date` objects.
11-
public static var constructor: JSFunction? { _constructor }
12-
@LazyThreadLocal(initialize: { JSObject.global.Date.function })
13-
private static var _constructor: JSFunction?
11+
public static var constructor: JSFunction? { _constructor.wrappedValue }
12+
private static let _constructor = LazyThreadLocal(initialize: { JSObject.global.Date.function })
1413

1514
/// The underlying JavaScript `Date` object.
1615
public let jsObject: JSObject

Sources/JavaScriptKit/BasicObjects/JSError.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
*/
55
public final class JSError: Error, JSBridgedClass {
66
/// The constructor function used to create new JavaScript `Error` objects.
7-
public static var constructor: JSFunction? { _constructor }
8-
@LazyThreadLocal(initialize: { JSObject.global.Error.function })
9-
private static var _constructor: JSFunction?
7+
public static var constructor: JSFunction? { _constructor.wrappedValue }
8+
private static let _constructor = LazyThreadLocal(initialize: { JSObject.global.Error.function })
109

1110
/// The underlying JavaScript `Error` object.
1211
public let jsObject: JSObject

Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift

+4-6
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,17 @@ func valueForBitWidth<T>(typeName: String, bitWidth: Int, when32: T) -> T {
143143
}
144144

145145
extension Int: TypedArrayElement {
146-
public static var typedArrayClass: JSFunction { _typedArrayClass }
147-
@LazyThreadLocal(initialize: {
146+
public static var typedArrayClass: JSFunction { _typedArrayClass.wrappedValue }
147+
private static let _typedArrayClass = LazyThreadLocal(initialize: {
148148
valueForBitWidth(typeName: "Int", bitWidth: Int.bitWidth, when32: JSObject.global.Int32Array).function!
149149
})
150-
private static var _typedArrayClass: JSFunction
151150
}
152151

153152
extension UInt: TypedArrayElement {
154-
public static var typedArrayClass: JSFunction { _typedArrayClass }
155-
@LazyThreadLocal(initialize: {
153+
public static var typedArrayClass: JSFunction { _typedArrayClass.wrappedValue }
154+
private static let _typedArrayClass = LazyThreadLocal(initialize: {
156155
valueForBitWidth(typeName: "UInt", bitWidth: Int.bitWidth, when32: JSObject.global.Uint32Array).function!
157156
})
158-
private static var _typedArrayClass: JSFunction
159157
}
160158

161159
extension Int8: TypedArrayElement {

Sources/JavaScriptKit/FundamentalObjects/JSObject.swift

+5-8
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ import _CJavaScriptKit
2424
/// reference counting system.
2525
@dynamicMemberLookup
2626
public class JSObject: Equatable {
27-
internal static var constructor: JSFunction { _constructor }
28-
@LazyThreadLocal(initialize: { JSObject.global.Object.function! })
29-
internal static var _constructor: JSFunction
27+
internal static var constructor: JSFunction { _constructor.wrappedValue }
28+
private static let _constructor = LazyThreadLocal(initialize: { JSObject.global.Object.function! })
3029

3130
@_spi(JSObject_id)
3231
public var id: JavaScriptObjectRef
@@ -206,12 +205,10 @@ public class JSObject: Equatable {
206205

207206
/// A `JSObject` of the global scope object.
208207
/// This allows access to the global properties and global names by accessing the `JSObject` returned.
209-
public static var global: JSObject { return _global }
210-
211-
@LazyThreadLocal(initialize: {
212-
return JSObject(id: _JS_Predef_Value_Global)
208+
public static var global: JSObject { return _global.wrappedValue }
209+
private static let _global = LazyThreadLocal(initialize: {
210+
JSObject(id: _JS_Predef_Value_Global)
213211
})
214-
private static var _global: JSObject
215212

216213
deinit {
217214
assertOnOwnerThread(hint: "deinitializing")

0 commit comments

Comments
 (0)