Improve argument type checking.
This commit is contained in:
@@ -18,6 +18,7 @@ import org.luaj.vm.LString;
|
|||||||
import org.luaj.vm.LTable;
|
import org.luaj.vm.LTable;
|
||||||
import org.luaj.vm.LValue;
|
import org.luaj.vm.LValue;
|
||||||
import org.luaj.vm.Lua;
|
import org.luaj.vm.Lua;
|
||||||
|
import org.luaj.vm.LuaErrorException;
|
||||||
import org.luaj.vm.LuaState;
|
import org.luaj.vm.LuaState;
|
||||||
import org.luaj.vm.Platform;
|
import org.luaj.vm.Platform;
|
||||||
|
|
||||||
@@ -118,6 +119,7 @@ public class BaseLib extends LFunction {
|
|||||||
vm.pushnil();
|
vm.pushnil();
|
||||||
vm.pushstring( message );
|
vm.pushstring( message );
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean luaStackCall(LuaState vm) {
|
public boolean luaStackCall(LuaState vm) {
|
||||||
switch ( id ) {
|
switch ( id ) {
|
||||||
case PRINT: {
|
case PRINT: {
|
||||||
@@ -240,7 +242,7 @@ public class BaseLib extends LFunction {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case ERROR: {
|
case ERROR: {
|
||||||
vm.error(vm.checkstring(2), vm.optint(3,1));
|
vm.error(vm.optstring(2,null), vm.optint(3,1));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case ASSERT: {
|
case ASSERT: {
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ public class LuaErrorException extends RuntimeException {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private static String addLineInfo(LuaState vm, String message, int level) {
|
private static String addLineInfo(LuaState vm, String message, int level) {
|
||||||
if ( level < 1 )
|
if ( level < 1 || message == null )
|
||||||
return message;
|
return message;
|
||||||
if ( vm == null ) {
|
if ( vm == null ) {
|
||||||
if ( LThread.running != null )
|
if ( LThread.running != null )
|
||||||
|
|||||||
@@ -401,7 +401,10 @@ public class LuaState extends Lua {
|
|||||||
closeUpVals(oldtop); /* close eventual pending closures */
|
closeUpVals(oldtop); /* close eventual pending closures */
|
||||||
String s = t.getMessage();
|
String s = t.getMessage();
|
||||||
resettop();
|
resettop();
|
||||||
pushstring( s!=null? s: t.toString() );
|
if ( s != null )
|
||||||
|
pushstring( s );
|
||||||
|
else
|
||||||
|
pushnil();
|
||||||
return (t instanceof OutOfMemoryError? LUA_ERRMEM: LUA_ERRRUN);
|
return (t instanceof OutOfMemoryError? LUA_ERRMEM: LUA_ERRRUN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -152,13 +152,16 @@ local function subbanner(name)
|
|||||||
print( '--- '..tostring(name) )
|
print( '--- '..tostring(name) )
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function pack(s,...)
|
||||||
|
return s,{...}
|
||||||
|
end
|
||||||
|
|
||||||
-- check that all combinations of arguments pass
|
-- check that all combinations of arguments pass
|
||||||
function checkallpass( name, typesets, typesonly )
|
function checkallpass( name, typesets, typesonly )
|
||||||
subbanner('checkallpass')
|
subbanner('checkallpass')
|
||||||
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 r = { invoke( name, v ) }
|
local s,r = pack( invoke( name, v ) )
|
||||||
local s = table.remove( r, 1 )
|
|
||||||
if s then
|
if s then
|
||||||
if typesonly then
|
if typesonly then
|
||||||
print( ok, sig, types(r) )
|
print( ok, sig, types(r) )
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ checkallerrors('loadfile', {nonstring}, 'bad argument #1')
|
|||||||
banner('loadstring')
|
banner('loadstring')
|
||||||
checkallpass('loadstring', {{'return'}})
|
checkallpass('loadstring', {{'return'}})
|
||||||
checkallpass('loadstring', {{'return'},{'mychunk'}})
|
checkallpass('loadstring', {{'return'},{'mychunk'}})
|
||||||
checkallpass('loadstring', {{'return a ... b'},{'mychunk'}})
|
checkallpass('loadstring', {{'return a ... b'},{'mychunk'}},true)
|
||||||
checkallerrors('loadstring', {{'return a ... b'},{'mychunk'}},'hello')
|
checkallerrors('loadstring', {{'return a ... b'},{'mychunk'}},'hello')
|
||||||
checkallerrors('loadstring', {notastring,{nil,astring,anumber}}, 'bad argument #1')
|
checkallerrors('loadstring', {notastring,{nil,astring,anumber}}, 'bad argument #1')
|
||||||
checkallerrors('loadstring', {{'return'},{afunction,atable}}, 'bad argument #2')
|
checkallerrors('loadstring', {{'return'},{afunction,atable}}, 'bad argument #2')
|
||||||
|
|||||||
@@ -8,20 +8,20 @@ local notanumber = {nil,astring,aboolean,afunction,atable,athread}
|
|||||||
local nonnumber = {astring,aboolean,afunction,atable}
|
local nonnumber = {astring,aboolean,afunction,atable}
|
||||||
|
|
||||||
local singleargfunctions = {
|
local singleargfunctions = {
|
||||||
'abs', 'acos', 'asin', 'atan', 'ceil', 'cos', 'cosh', 'deg', 'exp', 'floor',
|
'abs', 'acos', 'asin', 'atan', 'cos', 'cosh', 'deg', 'exp', 'floor',
|
||||||
'rad', 'randomseed', 'sin', 'sinh', 'tan', 'tanh', 'frexp',
|
'rad', 'randomseed', 'sin', 'sinh', 'tan', 'tanh', 'frexp',
|
||||||
}
|
}
|
||||||
|
|
||||||
local singleargposdomain = {
|
local singleargposdomain = {
|
||||||
'log', 'log10', 'sqrt',
|
'log', 'log10', 'sqrt', 'ceil',
|
||||||
}
|
}
|
||||||
|
|
||||||
local twoargfunctions = {
|
local twoargfunctions = {
|
||||||
'atan2', 'fmod',
|
'atan2',
|
||||||
}
|
}
|
||||||
|
|
||||||
local twoargsposdomain = {
|
local twoargsposdomain = {
|
||||||
'pow',
|
'pow', 'fmod',
|
||||||
}
|
}
|
||||||
|
|
||||||
-- single argument tests
|
-- single argument tests
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ print( math.sqrt( 9.0 ) )
|
|||||||
print( math.modf( 5.25 ) )
|
print( math.modf( 5.25 ) )
|
||||||
|
|
||||||
local aliases = {
|
local aliases = {
|
||||||
|
['0']='<zero>',
|
||||||
|
['-0']='<zero>',
|
||||||
['nan']='<nan>',
|
['nan']='<nan>',
|
||||||
['inf']='<pos-inf>',
|
['inf']='<pos-inf>',
|
||||||
['-inf']='<neg-inf>',
|
['-inf']='<neg-inf>',
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
|
||||||
|
|
||||||
-- unit tests for module() function
|
-- unit tests for module() function
|
||||||
local ids = {}
|
local ids = {}
|
||||||
local function id(obj)
|
local function id(obj)
|
||||||
@@ -72,3 +74,17 @@ f()
|
|||||||
print( a )
|
print( a )
|
||||||
print( getfenv(f)['a'] )
|
print( getfenv(f)['a'] )
|
||||||
print( a )
|
print( a )
|
||||||
|
|
||||||
|
-- do metatables work with package.loaded and require?
|
||||||
|
print( 'setting metatable for package.loaded' )
|
||||||
|
print( 'package.loaded.mypreload', package.loaded.mypreload )
|
||||||
|
print( 'setmetatable')
|
||||||
|
pcall( setmetatable, package.loaded, { __index={mypreload=12345}})
|
||||||
|
print( 'package.loaded.mypreload', package.loaded.mypreload )
|
||||||
|
print( "require, 'mypreload'", pcall( require, 'mypreload' ) )
|
||||||
|
print( 'package.loaded.mypreload', package.loaded.mypreload )
|
||||||
|
print( 'resetting metatable for package.loaded' )
|
||||||
|
print( 'setmetatable')
|
||||||
|
pcall( setmetatable, package.loaded, nil )
|
||||||
|
print( "require, 'mypreload'", (pcall( require, 'mypreload' )) )
|
||||||
|
print( 'package.loaded.mypreload', package.loaded.mypreload )
|
||||||
|
|||||||
@@ -20,6 +20,13 @@ local function ct(f,a,b,c)
|
|||||||
return f(a,b,c)
|
return f(a,b,c)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- wrap pcall to be more useful in testing
|
||||||
|
local pc = pcall
|
||||||
|
local pcall = function(...)
|
||||||
|
local s,c = pc(...)
|
||||||
|
return s, type(c)
|
||||||
|
end
|
||||||
|
|
||||||
-- lua calls
|
-- lua calls
|
||||||
print( 'lc(22,33,44)', lc(22,33,44) )
|
print( 'lc(22,33,44)', lc(22,33,44) )
|
||||||
print( 'pcall(lc,22,33,44)', pcall(lc,22,33,44) )
|
print( 'pcall(lc,22,33,44)', pcall(lc,22,33,44) )
|
||||||
@@ -50,3 +57,4 @@ for i = 0,4 do
|
|||||||
print( 'pcall(le,i)', i, pcall(le,i) )
|
print( 'pcall(le,i)', i, pcall(le,i) )
|
||||||
print( 'pcall(ge,i)', i, pcall(ge,i) )
|
print( 'pcall(ge,i)', i, pcall(ge,i) )
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user