Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into no-jsobject-methods
Browse files Browse the repository at this point in the history
  • Loading branch information
j-f1 committed Aug 4, 2020
2 parents 774b08b + 19365a1 commit 3c33a92
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,7 +1,7 @@
dist
node_modules
.DS_Store
/.build
.build
/Packages
/*.xcodeproj
xcuserdata/
Expand Down
11 changes: 11 additions & 0 deletions IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift
Expand Up @@ -100,6 +100,17 @@ Array_Iterator: do {
try expectEqual(Array(array), expectedProp_4)
}

Array_RandomAccessCollection: do {
let globalObject1 = getJSValue(this: .global, name: "globalObject1")
let globalObject1Ref = try expectObject(globalObject1)
let prop_4 = getJSValue(this: globalObject1Ref, name: "prop_4")
let array = try expectArray(prop_4)
let expectedProp_4: [JSValue] = [
.number(3), .number(4), .string("str_elm_1"), .number(5),
]
try expectEqual([array[0], array[1], array[2], array[3]], expectedProp_4)
}

Value_Decoder: do {
struct GlobalObject1: Codable {
struct Prop1: Codable {
Expand Down
10 changes: 9 additions & 1 deletion Sources/JavaScriptKit/JSArrayRef.swift
Expand Up @@ -14,7 +14,7 @@ public class JSArrayRef {
}
}

extension JSArrayRef: Sequence {
extension JSArrayRef: RandomAccessCollection {
public typealias Element = JSValue

public func makeIterator() -> Iterator {
Expand All @@ -37,4 +37,12 @@ extension JSArrayRef: Sequence {
return value.isNull ? nil : value
}
}

public subscript(position: Int) -> JSValue {
ref.get(position)
}

public var startIndex: Int { 0 }

public var endIndex: Int { ref.length.number.map(Int.init) ?? 0 }
}

0 comments on commit 3c33a92

Please sign in to comment.