diff --git a/.gitignore b/.gitignore index c1866f55e..4f8aecf97 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ dist node_modules .DS_Store -/.build +.build /Packages /*.xcodeproj xcuserdata/ diff --git a/IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift b/IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift index 140cc0bd3..60806e671 100644 --- a/IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift +++ b/IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift @@ -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 { diff --git a/Sources/JavaScriptKit/JSArrayRef.swift b/Sources/JavaScriptKit/JSArrayRef.swift index 05f46bbec..d4abc7e66 100644 --- a/Sources/JavaScriptKit/JSArrayRef.swift +++ b/Sources/JavaScriptKit/JSArrayRef.swift @@ -14,7 +14,7 @@ public class JSArrayRef { } } -extension JSArrayRef: Sequence { +extension JSArrayRef: RandomAccessCollection { public typealias Element = JSValue public func makeIterator() -> Iterator { @@ -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 } }