Skip to content

Commit

Permalink
Add bounds checking to list
Browse files Browse the repository at this point in the history
Fixes #417.
  • Loading branch information
simonbyrne committed Feb 9, 2022
1 parent 65cd62e commit 57686bf
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Manifest.toml
deps/deps.jl
docs/build
docs/gh-pages
Expand Down
9 changes: 8 additions & 1 deletion src/methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,15 @@ function iterate(s::RObject{S}, state) where S<:VectorListSxp
state += 1
(RObject(s[state]), state)
end
getindex(r::RObject{S}, I...) where S<:VectorListSxp = RObject(getindex(sexp(r), I...))

Base.checkbounds(::Type{Bool}, r::RObject{S}, i::Integer) where S<:VectorListSxp =
1 <= i <= length(r)
Base.checkbounds(r::RObject{S}, i) where S<:VectorListSxp =
checkbounds(Bool, r, i) ? nothing : throw(BoundsError(r, i))
function getindex(r::RObject{S}, i::Integer) where S<:VectorListSxp
@boundscheck checkbounds(r, i)
RObject(getindex(sexp(r), i))
end

# StrSxp

Expand Down
3 changes: 3 additions & 0 deletions test/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ globalEnv[:y] = RObject([4,5,6])
@test isna(R"list(a=1, b=NA)", 1) == false
@test isna(R"list(a=1, b=NA)", 2) == true

@test R"list(1,2)"[1] isa RObject
@test_throws BoundsError R"list(1,2)"[3]

@test length(R"mtcars") == 11
@test size(R"mtcars") == (32, 11)

Expand Down

0 comments on commit 57686bf

Please sign in to comment.