Skip to content

Commit

Permalink
Merge pull request #133 from szeiger/wip/fix-array-bounds
Browse files Browse the repository at this point in the history
Fix upper array bounds handling
  • Loading branch information
szeiger authored Oct 21, 2021
2 parents 7df2dcf + 384e3ba commit 02af3e6
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sjsonnet/src/sjsonnet/Evaluator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ class Evaluator(resolver: CachedResolver,
val pos = e.pos
(visitExpr(e.value), visitExpr(e.index)) match {
case (v: Val.Arr, i: Val.Num) =>
if (i.value > v.length) Error.fail(s"array bounds error: ${i.value} not within [0, ${v.length})", pos)
val int = i.value.toInt
if (int != i.value) Error.fail("array index was not integer: " + i.value, pos)
if (int >= v.length) Error.fail(s"array bounds error: ${int} not within [0, ${v.length})", pos)
v.force(int)
case (v: Val.Str, i: Val.Num) => Val.Str(pos, new String(Array(v.value(i.value.toInt))))
case (v: Val.Obj, i: Val.Str) =>
Expand Down
2 changes: 1 addition & 1 deletion sjsonnet/test/src-jvm-native/sjsonnet/ErrorTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ object ErrorTests extends TestSuite{
|""".stripMargin
)
test("array_large_index") - check(
"""sjsonnet.Error: array bounds error: 1.8446744073709552E19 not within [0, 3)
"""sjsonnet.Error: array index was not integer: 1.8446744073709552E19
| at [Lookup].(sjsonnet/test/resources/test_suite/error.array_large_index.jsonnet:17:10)
|""".stripMargin
)
Expand Down
3 changes: 3 additions & 0 deletions sjsonnet/test/src/sjsonnet/EvaluatorTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ object EvaluatorTests extends TestSuite{
test("arrays") {
eval("[1, [2, 3], 4][1][0]") ==> ujson.Num(2)
eval("([1, 2, 3] + [4, 5, 6])[3]") ==> ujson.Num(4)
evalErr("[][0]") ==>
"""sjsonnet.Error: array bounds error: 0 not within [0, 0)
|at [Lookup].(:1:3)""".stripMargin
}
test("functions") {
eval("(function(x) x)(1)") ==> ujson.Num(1)
Expand Down

0 comments on commit 02af3e6

Please sign in to comment.