@@ -9,43 +9,36 @@ public protocol TypedArrayElement: JSValueConvertible, JSValueConstructible {
9
9
static var typedArrayClass : JSFunctionRef { get }
10
10
}
11
11
12
- public class JSTypedArray < Element> : JSObjectRef , ExpressibleByArrayLiteral where Element: TypedArrayElement {
12
+ public class JSTypedArray < Element> : JSBridgedClass , ExpressibleByArrayLiteral where Element: TypedArrayElement {
13
+ public static var classRef : JSFunctionRef { Element . typedArrayClass }
14
+ public var objectRef : JSObjectRef
13
15
public subscript( _ index: Int ) -> Element {
14
16
get {
15
- return Element . construct ( from: getJSValue ( this : self , index: Int32 ( index ) ) ) !
17
+ return Element . construct ( from: objectRef [ index] ) !
16
18
}
17
19
set {
18
- setJSValue ( this : self , index: Int32 ( index ) , value : newValue. jsValue ( ) )
20
+ self . objectRef [ index] = newValue. jsValue ( )
19
21
}
20
22
}
21
23
22
24
public init ( length: Int ) {
23
- let jsObject = Element . typedArrayClass. new ( length)
24
- // _retain is necessary here because the JSObjectRef we used to create the array
25
- // goes out of scope and is deinitialized when this init() returns, causing
26
- // the JS side to decrement the object's reference count. JSTypedArray will also
27
- // call _release() when deinitialized because it inherits from JSObjectRef, so this
28
- // will not leak memory.
29
- _retain ( jsObject. id)
30
- super. init ( id: jsObject. id)
25
+ objectRef = Element . typedArrayClass. new ( length)
31
26
}
32
27
33
- public init ? ( objectRef jsObject: JSObjectRef ) {
34
- guard jsObject. isInstanceOf ( Element . typedArrayClass) else { return nil }
35
- _retain ( jsObject. id)
36
- super. init ( id: jsObject. id)
28
+ required public init ( withCompatibleObject jsObject: JSObjectRef ) {
29
+ objectRef = jsObject
37
30
}
38
31
39
32
required public convenience init ( arrayLiteral elements: Element ... ) {
40
33
self . init ( elements)
41
34
}
42
35
43
- public init ( _ array: [ Element ] ) {
36
+ public convenience init ( _ array: [ Element ] ) {
44
37
var resultObj = JavaScriptObjectRef ( )
45
38
array. withUnsafeBufferPointer { ptr in
46
39
_create_typed_array ( Element . typedArrayKind, ptr. baseAddress!, Int32 ( array. count) , & resultObj)
47
40
}
48
- super . init ( id: resultObj)
41
+ self . init ( withCompatibleObject : JSObjectRef ( id: resultObj) )
49
42
}
50
43
51
44
public convenience init ( _ stride: StrideTo < Element > ) where Element: Strideable {
0 commit comments