Skip to content

Commit 87ad32c

Browse files
authored
Merge branch 'master' into support-deref-in-func
2 parents 0ae17e6 + 6cf0edb commit 87ad32c

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

expr_test.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -1329,7 +1329,9 @@ func TestExpr(t *testing.T) {
13291329
}
13301330

13311331
func TestExpr_error(t *testing.T) {
1332-
env := mock.Env{}
1332+
env := mock.Env{
1333+
ArrayOfAny: []any{1, "2", 3, true},
1334+
}
13331335

13341336
tests := []struct {
13351337
code string
@@ -1341,6 +1343,12 @@ func TestExpr_error(t *testing.T) {
13411343
| filter(1..9, # > 9)[0]
13421344
| ...................^`,
13431345
},
1346+
{
1347+
`ArrayOfAny[-7]`,
1348+
`index out of range: -3 (array length is 4) (1:11)
1349+
| ArrayOfAny[-7]
1350+
| ..........^`,
1351+
},
13441352
}
13451353

13461354
for _, tt := range tests {

vm/runtime/runtime.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ func Fetch(from, i any) any {
3535
switch v.Kind() {
3636
case reflect.Array, reflect.Slice, reflect.String:
3737
index := ToInt(i)
38+
l := v.Len()
3839
if index < 0 {
39-
index = v.Len() + index
40+
index = l + index
41+
}
42+
if index < 0 || index >= l {
43+
panic(fmt.Sprintf("index out of range: %v (array length is %v)", index, l))
4044
}
4145
value := v.Index(index)
4246
if value.IsValid() {

0 commit comments

Comments
 (0)