Improve argument type checking.

This commit is contained in:
James Roseborough
2008-07-24 01:28:52 +00:00
parent 45e60c4b21
commit 022202e578
3 changed files with 23 additions and 14 deletions

View File

@@ -184,7 +184,7 @@ public class PackageLib extends LFunction {
public static void module(LuaState vm) {
LString modname = vm.checklstring(2);
int n = vm.gettop();
LValue value = LOADED.get(modname);
LValue value = LOADED.luaGetTable(vm, modname);
LTable module;
if ( ! value.isTable() ) { /* not found? */

View File

@@ -140,16 +140,24 @@ end
local function invoke( name, arglist )
local s,c = pcall(lookup, name)
if not s then return s,c end
return pcall(c, unpack(arglist))
local f = function(...)
local u = unpack
local t = { c(...) }
return u(t)
end
return pcall(f, unpack(arglist))
end
-- messages, banners
local _print = print
local _tostring = tostring
local _find = string.find
function banner(name)
print( '====== '..tostring(name)..' ======' )
_print( '====== '.._tostring(name)..' ======' )
end
local function subbanner(name)
print( '--- '..tostring(name) )
_print( '--- '.._tostring(name) )
end
local function pack(s,...)
@@ -164,12 +172,12 @@ function checkallpass( name, typesets, typesonly )
local s,r = pack( invoke( name, v ) )
if s then
if typesonly then
print( ok, sig, types(r) )
_print( ok, sig, types(r) )
else
print( ok, sig, values(r) )
_print( ok, sig, values(r) )
end
else
print( fail, sig, values(r) )
_print( fail, sig, values(r) )
end
end
end
@@ -178,18 +186,18 @@ end
-- ignore error messages
function checkallerrors( name, typesets, template )
subbanner('checkallerrors')
template = tostring(template)
template = _tostring(template)
for i,v in arglists(typesets) do
local sig = signature(name,v)
local s,e = invoke( name, v )
if not s then
if string.find(e, template, 1, true) then
print( ok, sig, '...'..template..'...' )
if _find(e, template, 1, true) then
_print( ok, sig, '...'..template..'...' )
else
print( badmsg, sig, "template='"..template.."' actual='"..e.."'" )
_print( badmsg, sig, "template='"..template.."' actual='"..e.."'" )
end
else
print( needcheck, sig, e )
_print( needcheck, sig, e )
end
end
end

View File

@@ -23,8 +23,9 @@ function new(a)
end
-- basic weak-reference table test
local weak = newtable{ new('one'), new('two'), new('three'), new('four'), a=new('aaa'), b=new('bbb'), c=new('ccc'), d=new('ddd') }
local strong = { weak[1], weak[3], a=weak.a, c=weak.c }
local strong = { new('one'), new('two'), a=new('aaa'), c=new('ccc') }
local weak = newtable{ strong[1], new('three'), strong[2], new('four'),
a=new('aaa'), b=new('bbb'), c=new('ccc'), d=new('ddd') }
print( 'before, weak:', eles(weak) )
print( 'before, strong:', eles(strong) )
print( 'gc', pcall( collectgarbage, "collect" ) )