@@ -9,37 +9,40 @@ 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> : JSValueConvertible , ExpressibleByArrayLiteral where Element: TypedArrayElement {
13
+ let ref : JSObject
14
+ public func jsValue( ) -> JSValue {
15
+ . object( ref)
16
+ }
17
+
13
18
public subscript( _ index: Int ) -> Element {
14
19
get {
15
- return Element . construct ( from: getJSValue ( this: self , index: Int32 ( index) ) ) !
20
+ return Element . construct ( from: getJSValue ( this: ref , index: Int32 ( index) ) ) !
16
21
}
17
22
set {
18
- setJSValue ( this: self , index: Int32 ( index) , value: newValue. jsValue ( ) )
23
+ setJSValue ( this: ref , index: Int32 ( index) , value: newValue. jsValue ( ) )
19
24
}
20
25
}
26
+
27
+ public init ( _ object: JSObject ) {
28
+ self . ref = object
29
+ }
21
30
22
- public init ( length: Int ) {
31
+ public convenience init ( length: Int ) {
23
32
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)
33
+ self . init ( jsObject)
31
34
}
32
35
33
36
required public convenience init ( arrayLiteral elements: Element ... ) {
34
37
self . init ( elements)
35
38
}
36
39
37
- public init ( _ array: [ Element ] ) {
40
+ public convenience init ( _ array: [ Element ] ) {
38
41
var resultObj = JavaScriptObjectRef ( )
39
42
array. withUnsafeBufferPointer { ptr in
40
43
_create_typed_array ( Element . typedArrayKind, ptr. baseAddress!, Int32 ( array. count) , & resultObj)
41
44
}
42
- super . init ( id: resultObj)
45
+ self . init ( JSObject ( id: resultObj) )
43
46
}
44
47
45
48
public convenience init ( _ stride: StrideTo < Element > ) where Element: Strideable {
0 commit comments