Skip to content

Commit e9158ab

Browse files
Stop use of global variable as a object cache
Instead, use `LazyThreadLocal`
1 parent 115ca29 commit e9158ab

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

Sources/JavaScriptKit/BasicObjects/JSArray.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ extension JSArray: RandomAccessCollection {
9393
}
9494
}
9595

96-
private let alwaysTrue = JSClosure { _ in .boolean(true) }
96+
private let alwaysTrue = LazyThreadLocal(initialize: { JSClosure { _ in .boolean(true) } })
9797
private func getObjectValuesLength(_ object: JSObject) -> Int {
98-
let values = object.filter!(alwaysTrue).object!
98+
let values = object.filter!(alwaysTrue.wrappedValue).object!
9999
return Int(values.length.number!)
100100
}
101101

Sources/JavaScriptKit/ConvertibleToJSValue.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ extension JSObject: JSValueCompatible {
8585
// from `JSFunction`
8686
}
8787

88-
private let objectConstructor = JSObject.global.Object.function!
89-
private let arrayConstructor = JSObject.global.Array.function!
88+
private let _objectConstructor = LazyThreadLocal(initialize: { JSObject.global.Object.function! })
89+
private var objectConstructor: JSFunction { _objectConstructor.wrappedValue }
90+
private let _arrayConstructor = LazyThreadLocal(initialize: { JSObject.global.Array.function! })
91+
private var arrayConstructor: JSFunction { _arrayConstructor.wrappedValue }
9092

9193
#if !hasFeature(Embedded)
9294
extension Dictionary where Value == ConvertibleToJSValue, Key == String {
@@ -296,4 +298,4 @@ extension Array where Element == ConvertibleToJSValue {
296298
return _withRawJSValues(self, 0, &_results, body)
297299
}
298300
}
299-
#endif
301+
#endif

Sources/JavaScriptKit/FundamentalObjects/JSSymbol.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import _CJavaScriptKit
22

3-
private let Symbol = JSObject.global.Symbol.function!
3+
private let _Symbol = LazyThreadLocal(initialize: { JSObject.global.Symbol.function! })
4+
private var Symbol: JSFunction { _Symbol.wrappedValue }
45

56
/// A wrapper around [the JavaScript `Symbol`
67
/// class](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol)

0 commit comments

Comments
 (0)