diff --git a/interpreter.go b/interpreter.go index cc7238c..6d1e995 100644 --- a/interpreter.go +++ b/interpreter.go @@ -392,7 +392,7 @@ func (i *interpreter) evaluate(a ast.Node, tc tailCallStatus) (value, error) { return nil, err } // TODO(https://github.com/google/jsonnet/issues/377): non-integer indexes should be an error - return e.evaluateTailCall(target.elements[int(indexInt.value)], tc) + return target.index(e, int(indexInt.value), tc) case *valueString: indexInt, err := e.getNumber(index) diff --git a/testdata/array_out_of_bounds.golden b/testdata/array_out_of_bounds.golden new file mode 100644 index 0000000..77a8cc8 --- /dev/null +++ b/testdata/array_out_of_bounds.golden @@ -0,0 +1,10 @@ +RUNTIME ERROR: Index 0 out of bounds, not within [0, 0) +------------------------------------------------- + testdata/array_out_of_bounds:1:1-6 $ + +[][0] + +------------------------------------------------- + During evaluation + + diff --git a/testdata/array_out_of_bounds.jsonnet b/testdata/array_out_of_bounds.jsonnet new file mode 100644 index 0000000..8e482eb --- /dev/null +++ b/testdata/array_out_of_bounds.jsonnet @@ -0,0 +1 @@ +[][0] \ No newline at end of file diff --git a/testdata/array_out_of_bounds2.golden b/testdata/array_out_of_bounds2.golden new file mode 100644 index 0000000..01a38f5 --- /dev/null +++ b/testdata/array_out_of_bounds2.golden @@ -0,0 +1,10 @@ +RUNTIME ERROR: Index 3 out of bounds, not within [0, 3) +------------------------------------------------- + testdata/array_out_of_bounds2:1:1-11 $ + +[1,2,3][3] + +------------------------------------------------- + During evaluation + + diff --git a/testdata/array_out_of_bounds2.jsonnet b/testdata/array_out_of_bounds2.jsonnet new file mode 100644 index 0000000..eb557fc --- /dev/null +++ b/testdata/array_out_of_bounds2.jsonnet @@ -0,0 +1 @@ +[1,2,3][3] \ No newline at end of file diff --git a/testdata/array_out_of_bounds3.golden b/testdata/array_out_of_bounds3.golden new file mode 100644 index 0000000..104ab93 --- /dev/null +++ b/testdata/array_out_of_bounds3.golden @@ -0,0 +1,10 @@ +RUNTIME ERROR: Index -1 out of bounds, not within [0, 0) +------------------------------------------------- + testdata/array_out_of_bounds3:1:1-7 $ + +[][-1] + +------------------------------------------------- + During evaluation + + diff --git a/testdata/array_out_of_bounds3.jsonnet b/testdata/array_out_of_bounds3.jsonnet new file mode 100644 index 0000000..06c5222 --- /dev/null +++ b/testdata/array_out_of_bounds3.jsonnet @@ -0,0 +1 @@ +[][-1] \ No newline at end of file diff --git a/testdata/array_out_of_bounds4.golden b/testdata/array_out_of_bounds4.golden new file mode 100644 index 0000000..acf209d --- /dev/null +++ b/testdata/array_out_of_bounds4.golden @@ -0,0 +1,10 @@ +RUNTIME ERROR: Index 42 out of bounds, not within [0, 3) +------------------------------------------------- + testdata/array_out_of_bounds4:1:1-12 $ + +[1,2,3][42] + +------------------------------------------------- + During evaluation + + diff --git a/testdata/array_out_of_bounds4.jsonnet b/testdata/array_out_of_bounds4.jsonnet new file mode 100644 index 0000000..f74735c --- /dev/null +++ b/testdata/array_out_of_bounds4.jsonnet @@ -0,0 +1 @@ +[1,2,3][42] \ No newline at end of file diff --git a/value.go b/value.go index b69a3bb..661cebb 100644 --- a/value.go +++ b/value.go @@ -200,6 +200,13 @@ type valueArray struct { elements []potentialValue } +func (arr *valueArray) index(e *evaluator, index int, tc tailCallStatus) (value, error) { + if 0 <= index && index < arr.length() { + return e.evaluateTailCall(arr.elements[index], tc) + } + return nil, e.Error(fmt.Sprintf("Index %d out of bounds, not within [0, %v)", index, arr.length())) +} + func (arr *valueArray) length() int { return len(arr.elements) }