Skip to content

Commit f83a84c

Browse files
Expose failable initializer
1 parent fab45e1 commit f83a84c

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,19 @@ public class JSTypedArray<Element>: JSValueConvertible, ExpressibleByArrayLitera
2424
}
2525
}
2626

27-
public init(_ object: JSObject) {
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 }
2834
self.ref = object
2935
}
3036

3137
public convenience init(length: Int) {
3238
let jsObject = Element.typedArrayClass.new(length)
33-
self.init(jsObject)
39+
self.init(unsafe: jsObject)
3440
}
3541

3642
required public convenience init(arrayLiteral elements: Element...) {
@@ -42,7 +48,7 @@ public class JSTypedArray<Element>: JSValueConvertible, ExpressibleByArrayLitera
4248
array.withUnsafeBufferPointer { ptr in
4349
_create_typed_array(Element.typedArrayKind, ptr.baseAddress!, Int32(array.count), &resultObj)
4450
}
45-
self.init(JSObject(id: resultObj))
51+
self.init(unsafe: JSObject(id: resultObj))
4652
}
4753

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

0 commit comments

Comments
 (0)