Skip to content

Commit

Permalink
Issue #16: disallow nil table index
Browse files Browse the repository at this point in the history
  • Loading branch information
yuin committed Apr 23, 2015
1 parent c085e92 commit 941c279
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
9 changes: 8 additions & 1 deletion _glua-tests/bugs.lua → _glua-tests/issues.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

-- bug #10
-- issue #10
local function inspect(options)
options = options or {}
return type(options)
Expand All @@ -11,3 +11,10 @@ local function inspect(options)
return type(options)
end
assert(inspect(nil) == "table")

-- issue #16
local ok, msg = pcall(function()
local a = {}
a[nil] = 1
end)
assert(not ok and string.find(msg, "table index is nil", 1, true))
2 changes: 1 addition & 1 deletion script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
const maxMemory = 40

var gluaTests []string = []string{
"bugs.lua",
"issues.lua",
}

var luaTests []string = []string{
Expand Down
15 changes: 7 additions & 8 deletions state.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,10 +755,7 @@ func (ls *LState) setField(obj LValue, key LValue, value LValue) {
tb, istable := curobj.(*LTable)
if istable {
if tb.RawGet(key) != LNil {
if n, ok := key.(LNumber); ok && math.IsNaN(float64(n)) {
ls.RaiseError("table index is NaN")
}
tb.RawSet(key, value)
ls.RawSet(tb, key, value)
return
}
}
Expand All @@ -767,10 +764,7 @@ func (ls *LState) setField(obj LValue, key LValue, value LValue) {
if !istable {
ls.RaiseError("attempt to index a non-table object(%v)", curobj.Type().String())
}
if n, ok := key.(LNumber); ok && math.IsNaN(float64(n)) {
ls.RaiseError("table index is NaN")
}
tb.RawSet(key, value)
ls.RawSet(tb, key, value)
return
}
if metaindex.Type() == LTFunction {
Expand Down Expand Up @@ -1245,6 +1239,11 @@ func (ls *LState) GetField(obj LValue, skey string) LValue {
}

func (ls *LState) RawSet(tb *LTable, key LValue, value LValue) {
if n, ok := key.(LNumber); ok && math.IsNaN(float64(n)) {
ls.RaiseError("table index is NaN")
} else if key == LNil {
ls.RaiseError("table index is nil")
}
tb.RawSet(key, value)
}

Expand Down

0 comments on commit 941c279

Please sign in to comment.