Improve table lib error reporting.

This commit is contained in:
James Roseborough
2010-05-12 03:48:21 +00:00
parent 536b27330d
commit cd4cb03525
2 changed files with 22 additions and 17 deletions

View File

@@ -21,6 +21,9 @@
******************************************************************************/ ******************************************************************************/
package org.luaj.vm2.lib; package org.luaj.vm2.lib;
import org.luaj.vm.LNil;
import org.luaj.vm.LTable;
import org.luaj.vm.LValue;
import org.luaj.vm2.LuaTable; import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue; import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs; import org.luaj.vm2.Varargs;
@@ -74,7 +77,9 @@ public class TableLib extends OneArgFunction {
return NONE; return NONE;
} }
case 3: { // "sort" (table [, comp]) -> void case 3: { // "sort" (table [, comp]) -> void
args.checktable(1).sort( args.optvalue(2,NIL) ); LuaTable table = args.checktable(1);
LuaValue compare = (args.isnoneornil(2)? NIL: args.checkfunction(2));
table.sort( compare );
return NONE; return NONE;
} }
case 4: { // (table, func) -> void case 4: { // (table, func) -> void

View File

@@ -16,40 +16,40 @@ checkallpass('table.concat',{somestringstable})
checkallpass('table.concat',{somestringstable,somesep}) checkallpass('table.concat',{somestringstable,somesep})
checkallpass('table.concat',{somestringstable,{'-'},somei}) checkallpass('table.concat',{somestringstable,{'-'},somei})
checkallpass('table.concat',{somestringstable,{'-'},{2},somej}) checkallpass('table.concat',{somestringstable,{'-'},{2},somej})
checkallerrors('table.concat',{notatable},'bad argument #1') checkallerrors('table.concat',{notatable},'bad argument')
checkallerrors('table.concat',{somenonstringtable},'table contains non-strings') checkallerrors('table.concat',{somenonstringtable},'boolean')
checkallerrors('table.concat',{somestringstable,notasep},'bad argument #2') checkallerrors('table.concat',{somestringstable,notasep},'bad argument')
checkallerrors('table.concat',{somestringstable,{'-'},notij},'bad argument #3') checkallerrors('table.concat',{somestringstable,{'-'},notij},'bad argument')
checkallerrors('table.concat',{somestringstable,{'-'},{2},notij},'bad argument #4') checkallerrors('table.concat',{somestringstable,{'-'},{2},notij},'bad argument')
-- table.insert -- table.insert
banner('table.insert') banner('table.insert')
checkallpass('table.insert',{sometable,notanil}) checkallpass('table.insert',{sometable,notanil})
checkallpass('table.insert',{sometable,somei,notanil}) checkallpass('table.insert',{sometable,somei,notanil})
checkallerrors('table.insert',{notatable,somestring},'bad argument #1') checkallerrors('table.insert',{notatable,somestring},'bad argument')
checkallerrors('table.insert',{sometable,notij,notanil},'bad argument #2') checkallerrors('table.insert',{sometable,notij,notanil},'bad argument')
-- table.maxn -- table.maxn
banner('table.maxn') banner('table.maxn')
checkallpass('table.maxn',{sometable}) checkallpass('table.maxn',{sometable})
checkallerrors('table.maxn',{notatable},'bad argument #1') checkallerrors('table.maxn',{notatable},'bad argument')
-- table.remove -- table.remove
banner('table.remove') banner('table.remove')
checkallpass('table.remove',{sometable}) checkallpass('table.remove',{sometable})
checkallpass('table.remove',{sometable,somei}) checkallpass('table.remove',{sometable,somei})
checkallerrors('table.remove',{notatable},'bad argument #1') checkallerrors('table.remove',{notatable},'bad argument')
checkallerrors('table.remove',{notatable,somei},'bad argument #1') checkallerrors('table.remove',{notatable,somei},'bad argument')
checkallerrors('table.remove',{sometable,notij},'bad argument #2') checkallerrors('table.remove',{sometable,notij},'bad argument')
-- table.sort -- table.sort
local somecomp = {nil,afunction} local somecomp = {nil,afunction,n=2}
local notacomp = {astring,anumber,aboolean,atable} local notacomp = {astring,anumber,aboolean,atable}
banner('table.sort') banner('table.sort')
checkallpass('table.sort',{somestringstable,somecomp}) checkallpass('table.sort',{somestringstable,somecomp})
checkallerrors('table.sort',{sometable},'attempt to compare') checkallerrors('table.sort',{sometable},'attempt to')
checkallerrors('table.sort',{notatable,somecomp},'bad argument #1') checkallerrors('table.sort',{notatable,somecomp},'bad argument')
checkallerrors('table.sort',{sometable,notacomp},'bad argument #2') checkallerrors('table.sort',{sometable,notacomp},'bad argument')
-- table get -- table get
banner('table_get - tbl[key]') banner('table_get - tbl[key]')
@@ -61,6 +61,6 @@ banner('table_set - tbl[key]=val')
function table_set(tbl,key,val) tbl[key]=val end function table_set(tbl,key,val) tbl[key]=val end
function table_set_nil_key(tbl,val) tbl[nil]=val end function table_set_nil_key(tbl,val) tbl[nil]=val end
checkallpass('table_set',{sometable,notanil,anylua}) checkallpass('table_set',{sometable,notanil,anylua})
checkallerrors('table_set_nil_key',{sometable,anylua},'table index is nil') checkallerrors('table_set_nil_key',{sometable,anylua},'table index')