From 9d35005ffeba14937e4870ce4699734587159abb Mon Sep 17 00:00:00 2001 From: Max Kuznetsov Date: Sat, 24 Feb 2024 13:21:20 +0000 Subject: [PATCH] Throw a runtime error when trying to slice an unsupported type (#442) --- vm.go | 2 ++ vm_test.go | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/vm.go b/vm.go index 2f8f6fc..2e0240b 100644 --- a/vm.go +++ b/vm.go @@ -534,6 +534,8 @@ func (v *VM) run() { } v.stack[v.sp] = val v.sp++ + default: + v.err = fmt.Errorf("not indexable: %s", left.TypeName()) } case parser.OpCall: numArgs := int(v.curInsts[v.ip+1]) diff --git a/vm_test.go b/vm_test.go index 8c98b5a..49c93de 100644 --- a/vm_test.go +++ b/vm_test.go @@ -3634,6 +3634,12 @@ func TestSpread(t *testing.T) { "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( t *testing.T, input string,