1
- public class JSArray {
2
- static let classObject = JSObject . global. Array. function!
1
+ public class JSArray : JSBridgedClass {
2
+ public static let constructor = JSObject . global. Array. function!
3
3
4
4
static func isArray( _ object: JSObject ) -> Bool {
5
- classObject . isArray!( object) . boolean!
5
+ constructor . isArray!( object) . boolean!
6
6
}
7
7
8
- let ref : JSObject
8
+ public let jsObject : JSObject
9
9
10
- public init ? ( _ ref: JSObject ) {
11
- guard Self . isArray ( ref) else { return nil }
12
- self . ref = ref
10
+ public required convenience init ? ( from value: JSValue ) {
11
+ guard let object = value. object else { return nil }
12
+ self . init ( object)
13
+ }
14
+
15
+ public convenience init ? ( _ jsObject: JSObject ) {
16
+ guard Self . isArray ( jsObject) else { return nil }
17
+ self . init ( withCompatibleObject: jsObject)
18
+ }
19
+ public required init ( withCompatibleObject jsObject: JSObject ) {
20
+ self . jsObject = jsObject
13
21
}
14
22
}
15
23
16
24
extension JSArray : RandomAccessCollection {
17
25
public typealias Element = JSValue
18
26
19
27
public func makeIterator( ) -> Iterator {
20
- Iterator ( ref : ref )
28
+ Iterator ( jsObject : jsObject )
21
29
}
22
30
23
31
public class Iterator : IteratorProtocol {
24
- let ref : JSObject
32
+ let jsObject : JSObject
25
33
var index = 0
26
- init ( ref : JSObject ) {
27
- self . ref = ref
34
+ init ( jsObject : JSObject ) {
35
+ self . jsObject = jsObject
28
36
}
29
37
30
38
public func next( ) -> Element ? {
31
39
let currentIndex = index
32
- guard currentIndex < Int ( ref . length. number!) else {
40
+ guard currentIndex < Int ( jsObject . length. number!) else {
33
41
return nil
34
42
}
35
43
index += 1
36
- guard ref . hasOwnProperty!( currentIndex) . boolean! else {
44
+ guard jsObject . hasOwnProperty!( currentIndex) . boolean! else {
37
45
return next ( )
38
46
}
39
- let value = ref [ currentIndex]
47
+ let value = jsObject [ currentIndex]
40
48
return value
41
49
}
42
50
}
43
51
44
52
public subscript( position: Int ) -> JSValue {
45
- ref [ position]
53
+ jsObject [ position]
46
54
}
47
55
48
56
public var startIndex : Int { 0 }
@@ -62,14 +70,14 @@ extension JSArray: RandomAccessCollection {
62
70
/// array.count // 2
63
71
/// ```
64
72
public var length : Int {
65
- return Int ( ref . length. number!)
73
+ Int ( jsObject . length. number!)
66
74
}
67
75
68
76
/// The number of elements in that array **not** including empty hole.
69
77
/// Note that `count` syncs with the number that `Iterator` can iterate.
70
78
/// See also: `JSArray.length`
71
79
public var count : Int {
72
- return getObjectValuesLength ( ref )
80
+ getObjectValuesLength ( jsObject )
73
81
}
74
82
}
75
83
0 commit comments