@@ -47,7 +47,10 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
47
47
/// - Parameter array: The array that will be copied to create a new instance of TypedArray
48
48
public convenience init ( _ array: [ Element ] ) {
49
49
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
+ }
51
54
}
52
55
self . init ( unsafelyWrapping: JSObject ( id: jsArrayRef) )
53
56
}
@@ -140,21 +143,27 @@ func valueForBitWidth<T>(typeName: String, bitWidth: Int, when32: T) -> T {
140
143
}
141
144
142
145
extension Int : TypedArrayElement {
143
- public static var typedArrayClass : JSFunction =
146
+ public static var typedArrayClass : JSFunction { _typedArrayClass }
147
+ @LazyThreadLocal ( initialize: {
144
148
valueForBitWidth ( typeName: " Int " , bitWidth: Int . bitWidth, when32: JSObject . global. Int32Array) . function!
149
+ } )
150
+ private static var _typedArrayClass : JSFunction
145
151
}
146
152
147
153
extension UInt : TypedArrayElement {
148
- public static var typedArrayClass : JSFunction =
154
+ public static var typedArrayClass : JSFunction { _typedArrayClass }
155
+ @LazyThreadLocal ( initialize: {
149
156
valueForBitWidth ( typeName: " UInt " , bitWidth: Int . bitWidth, when32: JSObject . global. Uint32Array) . function!
157
+ } )
158
+ private static var _typedArrayClass : JSFunction
150
159
}
151
160
152
161
extension Int8 : TypedArrayElement {
153
- public static var typedArrayClass = JSObject . global. Int8Array. function!
162
+ public static var typedArrayClass : JSFunction { JSObject . global. Int8Array. function! }
154
163
}
155
164
156
165
extension UInt8 : TypedArrayElement {
157
- public static var typedArrayClass = JSObject . global. Uint8Array. function!
166
+ public static var typedArrayClass : JSFunction { JSObject . global. Uint8Array. function! }
158
167
}
159
168
160
169
/// A wrapper around [the JavaScript `Uint8ClampedArray`
@@ -165,26 +174,26 @@ public class JSUInt8ClampedArray: JSTypedArray<UInt8> {
165
174
}
166
175
167
176
extension Int16 : TypedArrayElement {
168
- public static var typedArrayClass = JSObject . global. Int16Array. function!
177
+ public static var typedArrayClass : JSFunction { JSObject . global. Int16Array. function! }
169
178
}
170
179
171
180
extension UInt16 : TypedArrayElement {
172
- public static var typedArrayClass = JSObject . global. Uint16Array. function!
181
+ public static var typedArrayClass : JSFunction { JSObject . global. Uint16Array. function! }
173
182
}
174
183
175
184
extension Int32 : TypedArrayElement {
176
- public static var typedArrayClass = JSObject . global. Int32Array. function!
185
+ public static var typedArrayClass : JSFunction { JSObject . global. Int32Array. function! }
177
186
}
178
187
179
188
extension UInt32 : TypedArrayElement {
180
- public static var typedArrayClass = JSObject . global. Uint32Array. function!
189
+ public static var typedArrayClass : JSFunction { JSObject . global. Uint32Array. function! }
181
190
}
182
191
183
192
extension Float32 : TypedArrayElement {
184
- public static var typedArrayClass = JSObject . global. Float32Array. function!
193
+ public static var typedArrayClass : JSFunction { JSObject . global. Float32Array. function! }
185
194
}
186
195
187
196
extension Float64 : TypedArrayElement {
188
- public static var typedArrayClass = JSObject . global. Float64Array. function!
197
+ public static var typedArrayClass : JSFunction { JSObject . global. Float64Array. function! }
189
198
}
190
199
#endif
0 commit comments