Throw error when attempting to set a table value with a nil key

This commit is contained in:
James Roseborough
2008-09-19 17:57:03 +00:00
parent a0b1aef0b1
commit 9c19535631
4 changed files with 62 additions and 3 deletions

View File

@@ -948,6 +948,8 @@ public class LuaState extends Lua {
/** Get a key from a table using full metatable processing */
public void luaV_settable(LValue table, LValue key, LValue val) {
if ( key.isNil() )
this.error("table index is nil");
LValue h=LNil.NIL,t=table;
for ( int loop=0; loop<MAXTAGLOOP; loop++ ) {
if ( t.isTable() ) {

View File

@@ -50,3 +50,17 @@ checkallpass('table.sort',{somestringstable,somecomp})
checkallerrors('table.sort',{sometable},'attempt to compare')
checkallerrors('table.sort',{notatable,somecomp},'bad argument #1')
checkallerrors('table.sort',{sometable,notacomp},'bad argument #2')
-- table get
banner('table_get - tbl[key]')
function table_get(tbl,key) return tbl[key] end
checkallpass('table_get',{sometable,anylua})
-- table set
banner('table_set - tbl[key]=val')
function table_set(tbl,key,val) tbl[key]=val end
function table_set_nil_key(tbl,val) tbl[nil]=val end
checkallpass('table_set',{sometable,notanil,anylua})
checkallerrors('table_set_nil_key',{sometable,anylua},'table index is nil')

View File

@@ -5,7 +5,6 @@ table.insert(t,4,'eight');
table.insert(t,7,'nine');
table.insert(t,10,'ten'); print( #t )
-- concat
print( '-- concat tests' )
function tryconcat(t)
@@ -167,3 +166,47 @@ print( # { 'abc', 'def', 'ghi', 0 } ) -- should be 4 !
print( nil and 'T' or 'F' ) -- should be 'F'
print( false and 'T' or 'F' ) -- should be 'F'
print( 0 and 'T' or 'F' ) -- should be 'T'
-- basic table operation tests
local dummyfunc = function(t,...)
print( 'metatable call args', type(t), ...)
return 'dummy'
end
local makeloud = function(t)
return setmetatable(t,{
__index=function(t,k)
print( '__index', type(t), k )
return rawset(t,k)
end,
__newindex=function(t,k,v)
print( '__newindex', type(t), k, v )
rawset(t,k,v)
end})
end
local tests = {
{'basic table', {}},
{'function metatable on __index', setmetatable({},{__index=dummyfunc})},
{'function metatable on __newindex', setmetatable({},{__newindex=dummyfunc})},
{'plain metatable on __index', setmetatable({},makeloud({}))},
{'plain metatable on __newindex', setmetatable({},makeloud({}))},
}
for i,test in ipairs(tests) do
local testname = test[1]
local testtable = test[2]
print( '------ basic table tests on '..testname..' '..type(testtable) )
print( 't[1]=2', pcall( function() testtable[1]=2 end ) )
print( 't[1]', pcall( function() return testtable[1] end ) )
print( 't[1]=nil', pcall( function() testtable[1]=nil end ) )
print( 't[1]', pcall( function() return testtable[1] end ) )
print( 't["a"]="b"', pcall( function() testtable["a"]="b" end ) )
print( 't["a"],t.a', pcall( function() return testtable["a"],testtable.a end ) )
print( 't.a="c"', pcall( function() testtable.a="c" end ) )
print( 't["a"],t.a', pcall( function() return testtable["a"],testtable.a end ) )
print( 't.a=nil', pcall( function() testtable.a=nil end ) )
print( 't["a"],t.a', pcall( function() return testtable["a"],testtable.a end ) )
print( 't[nil]="d"', pcall( function() testtable[nil]="d" end ) )
print( 't[nil]', pcall( function() return testtable[nil] end ) )
print( 't[nil]=nil', pcall( function() testtable[nil]=nil end ) )
print( 't[nil]', pcall( function() return testtable[nil] end ) )
end

View File

@@ -1 +1 @@
version: 0.52
version: 0.53