diff --git a/runtime/lib.go b/runtime/lib.go index f1cba5f0..d2967532 100644 --- a/runtime/lib.go +++ b/runtime/lib.go @@ -83,8 +83,10 @@ func SetIndex(t *Thread, coll Value, idx Value, val Value) error { if isTable { // No need to call SetTableCheck t.SetTable(tbl, idx, val) + return nil } - return nil + return fmt.Errorf( + "attempt to index %s value without __newindex", coll.TypeName()) } if _, ok := metaNewIndex.TryTable(); ok { coll = metaNewIndex diff --git a/runtime/lib_test.go b/runtime/lib_test.go index 9674bb48..5ebacf32 100644 --- a/runtime/lib_test.go +++ b/runtime/lib_test.go @@ -75,3 +75,18 @@ func TestRuntime_CompileAndLoadLuaChunkOrExp(t *testing.T) { }) } } + +// TestSetIndexNoNewIndex tests setting new indices of values without +// a __newindex metamethod. +func TestSetIndexNoNewIndex(t *testing.T) { + r := New(os.Stdout) + intValue := IntValue(42) + meta := NewTable() + udValue := UserDataValue(NewUserData([]int{}, meta)) + if err := SetIndex(r.MainThread(), intValue, intValue, intValue); err == nil { + t.Error("expected error indexing int value") + } + if err := SetIndex(r.MainThread(), udValue, intValue, intValue); err == nil { + t.Error("expected error indexing userdata value") + } +}