Throw a runtime error when trying to slice an unsupported type (#442)

This commit is contained in:
Max Kuznetsov 2024-02-24 13:21:20 +00:00 committed by GitHub
parent 18b953c7be
commit 9d35005ffe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 0 deletions

2
vm.go
View file

@ -534,6 +534,8 @@ func (v *VM) run() {
} }
v.stack[v.sp] = val v.stack[v.sp] = val
v.sp++ v.sp++
default:
v.err = fmt.Errorf("not indexable: %s", left.TypeName())
} }
case parser.OpCall: case parser.OpCall:
numArgs := int(v.curInsts[v.ip+1]) numArgs := int(v.curInsts[v.ip+1])

View file

@ -3634,6 +3634,12 @@ func TestSpread(t *testing.T) {
"Runtime Error: wrong number of arguments: want=3, got=2") "Runtime Error: wrong number of arguments: want=3, got=2")
} }
func TestSliceIndex(t *testing.T) {
expectError(t, `undefined[:1]`, nil, "Runtime Error: not indexable")
expectError(t, `123[-1:2]`, nil, "Runtime Error: not indexable")
expectError(t, `{}[:]`, nil, "Runtime Error: not indexable")
}
func expectRun( func expectRun(
t *testing.T, t *testing.T,
input string, input string,