forked from swiftwasm/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSArrayRef.swift
40 lines (32 loc) · 955 Bytes
/
JSArrayRef.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class JSArrayRef {
static let classObject = JSObjectRef.global.Array.function!
static func isArray(_ object: JSObjectRef) -> Bool {
classObject.isArray.function!(object).boolean!
}
let ref: JSObjectRef
public init?(_ ref: JSObjectRef) {
guard Self.isArray(ref) else { return nil }
self.ref = ref
}
}
extension JSArrayRef: Sequence {
public typealias Element = JSValue
public func makeIterator() -> Iterator {
Iterator(ref: ref)
}
public class Iterator: IteratorProtocol {
let ref: JSObjectRef
var index = 0
init(ref: JSObjectRef) {
self.ref = ref
}
public func next() -> Element? {
defer { index += 1 }
guard index < Int(ref.length.number!) else {
return nil
}
let value = ref.get(index)
return value.isNull ? nil : value
}
}
}