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) { public static void module(LuaState vm) {
LString modname = vm.checklstring(2); LString modname = vm.checklstring(2);
int n = vm.gettop(); int n = vm.gettop();
LValue value = LOADED.get(modname); LValue value = LOADED.luaGetTable(vm, modname);
LTable module; LTable module;
if ( ! value.isTable() ) { /* not found? */ if ( ! value.isTable() ) { /* not found? */

View File

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

View File

@@ -23,8 +23,9 @@ function new(a)
end end
-- basic weak-reference table test -- 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 = { new('one'), new('two'), a=new('aaa'), c=new('ccc') }
local strong = { weak[1], weak[3], a=weak.a, c=weak.c } 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, weak:', eles(weak) )
print( 'before, strong:', eles(strong) ) print( 'before, strong:', eles(strong) )
print( 'gc', pcall( collectgarbage, "collect" ) ) print( 'gc', pcall( collectgarbage, "collect" ) )