@@ -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
142145extension 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
147153extension 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
152161extension Int8 : TypedArrayElement {
153- public static var typedArrayClass = JSObject . global. Int8Array. function!
162+ public static var typedArrayClass : JSFunction { JSObject . global. Int8Array. function! }
154163}
155164
156165extension 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
167176extension Int16 : TypedArrayElement {
168- public static var typedArrayClass = JSObject . global. Int16Array. function!
177+ public static var typedArrayClass : JSFunction { JSObject . global. Int16Array. function! }
169178}
170179
171180extension UInt16 : TypedArrayElement {
172- public static var typedArrayClass = JSObject . global. Uint16Array. function!
181+ public static var typedArrayClass : JSFunction { JSObject . global. Uint16Array. function! }
173182}
174183
175184extension Int32 : TypedArrayElement {
176- public static var typedArrayClass = JSObject . global. Int32Array. function!
185+ public static var typedArrayClass : JSFunction { JSObject . global. Int32Array. function! }
177186}
178187
179188extension UInt32 : TypedArrayElement {
180- public static var typedArrayClass = JSObject . global. Uint32Array. function!
189+ public static var typedArrayClass : JSFunction { JSObject . global. Uint32Array. function! }
181190}
182191
183192extension Float32 : TypedArrayElement {
184- public static var typedArrayClass = JSObject . global. Float32Array. function!
193+ public static var typedArrayClass : JSFunction { JSObject . global. Float32Array. function! }
185194}
186195
187196extension Float64 : TypedArrayElement {
188- public static var typedArrayClass = JSObject . global. Float64Array. function!
197+ public static var typedArrayClass : JSFunction { JSObject . global. Float64Array. function! }
189198}
190199#endif
0 commit comments