Skip to content

Commit a05e798

Browse files
Avoid sharing JSObject by global variables
1 parent 45206f7 commit a05e798

File tree

9 files changed

+53
-35
lines changed

9 files changed

+53
-35
lines changed

Diff for: Sources/JavaScriptBigIntSupport/Int64+I64.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import JavaScriptKit
22

33
extension UInt64: JavaScriptKit.ConvertibleToJSValue, JavaScriptKit.TypedArrayElement {
4-
public static var typedArrayClass = JSObject.global.BigUint64Array.function!
4+
public static var typedArrayClass: JSFunction { JSObject.global.BigUint64Array.function! }
55

66
public var jsValue: JSValue { .bigInt(JSBigInt(unsigned: self)) }
77
}
88

99
extension Int64: JavaScriptKit.ConvertibleToJSValue, JavaScriptKit.TypedArrayElement {
10-
public static var typedArrayClass = JSObject.global.BigInt64Array.function!
10+
public static var typedArrayClass: JSFunction { JSObject.global.BigInt64Array.function! }
1111

1212
public var jsValue: JSValue { .bigInt(JSBigInt(self)) }
1313
}

Diff for: Sources/JavaScriptKit/BasicObjects/JSArray.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
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 let constructor = JSObject.global.Array.function
5+
public static var constructor: JSFunction? { _constructor }
6+
@LazyThreadLocal(initialize: { JSObject.global.Array.function })
7+
private static var _constructor: JSFunction?
68

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

Diff for: Sources/JavaScriptKit/BasicObjects/JSDate.swift

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

1315
/// The underlying JavaScript `Date` object.
1416
public let jsObject: JSObject

Diff for: Sources/JavaScriptKit/BasicObjects/JSError.swift

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

911
/// The underlying JavaScript `Error` object.
1012
public let jsObject: JSObject

Diff for: Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift

+20-11
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
4747
/// - Parameter array: The array that will be copied to create a new instance of TypedArray
4848
public convenience init(_ array: [Element]) {
4949
let jsArrayRef = array.withUnsafeBufferPointer { ptr in
50-
swjs_create_typed_array(Self.constructor!.id, ptr.baseAddress, Int32(array.count))
50+
// Retain the constructor function to avoid it being released before calling `swjs_create_typed_array`
51+
withExtendedLifetime(Self.constructor!) { ctor in
52+
swjs_create_typed_array(ctor.id, ptr.baseAddress, Int32(array.count))
53+
}
5154
}
5255
self.init(unsafelyWrapping: JSObject(id: jsArrayRef))
5356
}
@@ -140,21 +143,27 @@ func valueForBitWidth<T>(typeName: String, bitWidth: Int, when32: T) -> T {
140143
}
141144

142145
extension Int: TypedArrayElement {
143-
public static var typedArrayClass: JSFunction =
146+
public static var typedArrayClass: JSFunction { _typedArrayClass }
147+
@LazyThreadLocal(initialize: {
144148
valueForBitWidth(typeName: "Int", bitWidth: Int.bitWidth, when32: JSObject.global.Int32Array).function!
149+
})
150+
private static var _typedArrayClass: JSFunction
145151
}
146152

147153
extension UInt: TypedArrayElement {
148-
public static var typedArrayClass: JSFunction =
154+
public static var typedArrayClass: JSFunction { _typedArrayClass }
155+
@LazyThreadLocal(initialize: {
149156
valueForBitWidth(typeName: "UInt", bitWidth: Int.bitWidth, when32: JSObject.global.Uint32Array).function!
157+
})
158+
private static var _typedArrayClass: JSFunction
150159
}
151160

152161
extension Int8: TypedArrayElement {
153-
public static var typedArrayClass = JSObject.global.Int8Array.function!
162+
public static var typedArrayClass: JSFunction { JSObject.global.Int8Array.function! }
154163
}
155164

156165
extension UInt8: TypedArrayElement {
157-
public static var typedArrayClass = JSObject.global.Uint8Array.function!
166+
public static var typedArrayClass: JSFunction { JSObject.global.Uint8Array.function! }
158167
}
159168

160169
/// A wrapper around [the JavaScript `Uint8ClampedArray`
@@ -165,26 +174,26 @@ public class JSUInt8ClampedArray: JSTypedArray<UInt8> {
165174
}
166175

167176
extension Int16: TypedArrayElement {
168-
public static var typedArrayClass = JSObject.global.Int16Array.function!
177+
public static var typedArrayClass: JSFunction { JSObject.global.Int16Array.function! }
169178
}
170179

171180
extension UInt16: TypedArrayElement {
172-
public static var typedArrayClass = JSObject.global.Uint16Array.function!
181+
public static var typedArrayClass: JSFunction { JSObject.global.Uint16Array.function! }
173182
}
174183

175184
extension Int32: TypedArrayElement {
176-
public static var typedArrayClass = JSObject.global.Int32Array.function!
185+
public static var typedArrayClass: JSFunction { JSObject.global.Int32Array.function! }
177186
}
178187

179188
extension UInt32: TypedArrayElement {
180-
public static var typedArrayClass = JSObject.global.Uint32Array.function!
189+
public static var typedArrayClass: JSFunction { JSObject.global.Uint32Array.function! }
181190
}
182191

183192
extension Float32: TypedArrayElement {
184-
public static var typedArrayClass = JSObject.global.Float32Array.function!
193+
public static var typedArrayClass: JSFunction { JSObject.global.Float32Array.function! }
185194
}
186195

187196
extension Float64: TypedArrayElement {
188-
public static var typedArrayClass = JSObject.global.Float64Array.function!
197+
public static var typedArrayClass: JSFunction { JSObject.global.Float64Array.function! }
189198
}
190199
#endif

Diff for: Sources/JavaScriptKit/FundamentalObjects/JSBigInt.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import _CJavaScriptKit
22

3-
private let constructor = JSObject.global.BigInt.function!
3+
private var constructor: JSFunction { JSObject.global.BigInt.function! }
44

55
/// A wrapper around [the JavaScript `BigInt`
66
/// class](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)
@@ -30,9 +30,9 @@ public final class JSBigInt: JSObject {
3030

3131
public func clamped(bitSize: Int, signed: Bool) -> JSBigInt {
3232
if signed {
33-
return constructor.asIntN!(bitSize, self).bigInt!
33+
return constructor.asIntN(bitSize, self).bigInt!
3434
} else {
35-
return constructor.asUintN!(bitSize, self).bigInt!
35+
return constructor.asUintN(bitSize, self).bigInt!
3636
}
3737
}
3838
}

Diff for: Sources/JavaScriptKit/FundamentalObjects/JSObject.swift

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ import _CJavaScriptKit
2222
/// reference counting system.
2323
@dynamicMemberLookup
2424
public class JSObject: Equatable {
25+
internal static var constructor: JSFunction { _constructor }
26+
@LazyThreadLocal(initialize: { JSObject.global.Object.function! })
27+
internal static var _constructor: JSFunction
28+
2529
@_spi(JSObject_id)
2630
public var id: JavaScriptObjectRef
2731

Diff for: Sources/JavaScriptKit/FundamentalObjects/JSSymbol.swift

+13-13
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,17 @@ public class JSSymbol: JSObject {
4747
}
4848

4949
extension JSSymbol {
50-
public static let asyncIterator: JSSymbol! = Symbol.asyncIterator.symbol
51-
public static let hasInstance: JSSymbol! = Symbol.hasInstance.symbol
52-
public static let isConcatSpreadable: JSSymbol! = Symbol.isConcatSpreadable.symbol
53-
public static let iterator: JSSymbol! = Symbol.iterator.symbol
54-
public static let match: JSSymbol! = Symbol.match.symbol
55-
public static let matchAll: JSSymbol! = Symbol.matchAll.symbol
56-
public static let replace: JSSymbol! = Symbol.replace.symbol
57-
public static let search: JSSymbol! = Symbol.search.symbol
58-
public static let species: JSSymbol! = Symbol.species.symbol
59-
public static let split: JSSymbol! = Symbol.split.symbol
60-
public static let toPrimitive: JSSymbol! = Symbol.toPrimitive.symbol
61-
public static let toStringTag: JSSymbol! = Symbol.toStringTag.symbol
62-
public static let unscopables: JSSymbol! = Symbol.unscopables.symbol
50+
public static var asyncIterator: JSSymbol! { Symbol.asyncIterator.symbol }
51+
public static var hasInstance: JSSymbol! { Symbol.hasInstance.symbol }
52+
public static var isConcatSpreadable: JSSymbol! { Symbol.isConcatSpreadable.symbol }
53+
public static var iterator: JSSymbol! { Symbol.iterator.symbol }
54+
public static var match: JSSymbol! { Symbol.match.symbol }
55+
public static var matchAll: JSSymbol! { Symbol.matchAll.symbol }
56+
public static var replace: JSSymbol! { Symbol.replace.symbol }
57+
public static var search: JSSymbol! { Symbol.search.symbol }
58+
public static var species: JSSymbol! { Symbol.species.symbol }
59+
public static var split: JSSymbol! { Symbol.split.symbol }
60+
public static var toPrimitive: JSSymbol! { Symbol.toPrimitive.symbol }
61+
public static var toStringTag: JSSymbol! { Symbol.toStringTag.symbol }
62+
public static var unscopables: JSSymbol! { Symbol.unscopables.symbol }
6363
}

Diff for: Sources/JavaScriptKit/JSValueDecoder.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ private struct _Decoder: Decoder {
3535
}
3636

3737
private enum Object {
38-
static let ref = JSObject.global.Object.function!
3938
static func keys(_ object: JSObject) -> [String] {
40-
let keys = ref.keys!(object).array!
39+
let keys = JSObject.constructor.keys!(object).array!
4140
return keys.map { $0.string! }
4241
}
4342
}
@@ -249,4 +248,4 @@ public class JSValueDecoder {
249248
return try T(from: decoder)
250249
}
251250
}
252-
#endif
251+
#endif

0 commit comments

Comments
 (0)