Throw error when attempting to set a table value with a nil key
This commit is contained in:
@@ -948,6 +948,8 @@ public class LuaState extends Lua {
|
|||||||
|
|
||||||
/** Get a key from a table using full metatable processing */
|
/** Get a key from a table using full metatable processing */
|
||||||
public void luaV_settable(LValue table, LValue key, LValue val) {
|
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;
|
LValue h=LNil.NIL,t=table;
|
||||||
for ( int loop=0; loop<MAXTAGLOOP; loop++ ) {
|
for ( int loop=0; loop<MAXTAGLOOP; loop++ ) {
|
||||||
if ( t.isTable() ) {
|
if ( t.isTable() ) {
|
||||||
|
|||||||
@@ -50,3 +50,17 @@ checkallpass('table.sort',{somestringstable,somecomp})
|
|||||||
checkallerrors('table.sort',{sometable},'attempt to compare')
|
checkallerrors('table.sort',{sometable},'attempt to compare')
|
||||||
checkallerrors('table.sort',{notatable,somecomp},'bad argument #1')
|
checkallerrors('table.sort',{notatable,somecomp},'bad argument #1')
|
||||||
checkallerrors('table.sort',{sometable,notacomp},'bad argument #2')
|
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')
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ table.insert(t,4,'eight');
|
|||||||
table.insert(t,7,'nine');
|
table.insert(t,7,'nine');
|
||||||
table.insert(t,10,'ten'); print( #t )
|
table.insert(t,10,'ten'); print( #t )
|
||||||
|
|
||||||
|
|
||||||
-- concat
|
-- concat
|
||||||
print( '-- concat tests' )
|
print( '-- concat tests' )
|
||||||
function tryconcat(t)
|
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( nil and 'T' or 'F' ) -- should be 'F'
|
||||||
print( false 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'
|
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
|
||||||
@@ -1 +1 @@
|
|||||||
version: 0.52
|
version: 0.53
|
||||||
|
|||||||
Reference in New Issue
Block a user