forked from zobnin/aiolauncher_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checks.lua
42 lines (36 loc) · 1.21 KB
/
checks.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
-- Copyright (c) 2006-2013 Fabien Fleutot and others.
--
-- All rights reserved.
checkers = { }
local function check_one(expected, val)
if type(val)==expected then return true end
local mt = getmetatable(val)
if mt and mt.__type==expected then return true end
local f = checkers[expected]
if f and f(val) then return true end
return false
end
local function check_many(_, expected, val)
if expected=='?' then return true
elseif expected=='!' then return (val~=nil)
elseif type(expected) ~= 'string' then
error 'strings expected by checks()'
elseif val==nil and expected :sub(1,1) == '?' then return true end
for one in expected :gmatch "[^|?]+" do
if check_one(one, val) then return true end
end
return false
end
local function checks(...)
for i, arg in ipairs{...} do
local name, val = debug.getlocal(2, i)
local success = check_many(name, arg, val)
if not success then
local fname = debug.getinfo(2, 'n').name
local fmt = "bad argument #%d to '%s' (%s expected, got %s)"
local msg = string.format(fmt, i, fname or "?", arg, type(val))
error(msg, 3)
end
end
end
return checks