Skip to content

Commit

Permalink
Forbid newindex for non-tables without __newindex (#99)
Browse files Browse the repository at this point in the history
Previously, a newindex event on a non-table without a suitable
__newindex entry in its metatable would be silently ignored. This commit
causes an error to be raised.
  • Loading branch information
TheCount authored Feb 15, 2023
1 parent 35f260b commit e0b5347
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion runtime/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions runtime/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit e0b5347

Please sign in to comment.