Skip to content

Commit ba3316e

Browse files
committed
Update JSTypedArray.swift
1 parent be1f916 commit ba3316e

File tree

1 file changed

+10
-26
lines changed

1 file changed

+10
-26
lines changed

Diff for: Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift

+10-26
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,24 @@ public protocol TypedArrayElement: JSValueConvertible, JSValueConstructible {
99
static var typedArrayClass: JSFunction { get }
1010
}
1111

12-
public class JSTypedArray<Element>: JSValueConvertible, ExpressibleByArrayLiteral where Element: TypedArrayElement {
13-
let ref: JSObject
14-
public func jsValue() -> JSValue {
15-
.object(ref)
16-
}
17-
12+
public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral where Element: TypedArrayElement {
13+
public static var classRef: JSFunction { Element.typedArrayClass }
14+
public var objectRef: JSObject
1815
public subscript(_ index: Int) -> Element {
1916
get {
20-
return Element.construct(from: getJSValue(this: ref, index: Int32(index)))!
17+
return Element.construct(from: objectRef[index])!
2118
}
2219
set {
23-
setJSValue(this: ref, index: Int32(index), value: newValue.jsValue())
20+
self.objectRef[index] = newValue.jsValue()
2421
}
2522
}
2623

27-
// This private initializer assumes that the passed object is TypedArray
28-
private init(unsafe object: JSObject) {
29-
self.ref = object
30-
}
31-
32-
public init?(_ object: JSObject) {
33-
guard object.isInstanceOf(Element.typedArrayClass) else { return nil }
34-
self.ref = object
35-
}
36-
37-
public convenience init(length: Int) {
38-
let jsObject = Element.typedArrayClass.new(length)
39-
self.init(unsafe: jsObject)
24+
public init(length: Int) {
25+
objectRef = Element.typedArrayClass.new(length)
4026
}
4127

42-
public init?(objectRef jsObject: JSObjectRef) {
43-
guard jsObject.isInstanceOf(Element.typedArrayClass) else { return nil }
44-
_retain(jsObject.id)
45-
super.init(id: jsObject.id)
28+
required public init(withCompatibleObject jsObject: JSObject) {
29+
objectRef = jsObject
4630
}
4731

4832
required public convenience init(arrayLiteral elements: Element...) {
@@ -54,7 +38,7 @@ public class JSTypedArray<Element>: JSValueConvertible, ExpressibleByArrayLitera
5438
array.withUnsafeBufferPointer { ptr in
5539
_create_typed_array(Element.typedArrayKind, ptr.baseAddress!, Int32(array.count), &resultObj)
5640
}
57-
self.init(unsafe: JSObject(id: resultObj))
41+
self.init(withCompatibleObject: JSObject(id: resultObj))
5842
}
5943

6044
public convenience init(_ stride: StrideTo<Element>) where Element: Strideable {

0 commit comments

Comments
 (0)