-
Notifications
You must be signed in to change notification settings - Fork 59
/
checks.lua
59 lines (53 loc) · 1.98 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
--------------------------------------------------------------------------------
-- Copyright (c) 2006-2013 Fabien Fleutot and others.
--
-- All rights reserved.
--
-- This program and the accompanying materials are made available
-- under the terms of the Eclipse Public License v1.0 which
-- accompanies this distribution, and is available at
-- http://www.eclipse.org/legal/epl-v10.html
--
-- This program and the accompanying materials are also made available
-- under the terms of the MIT public license which accompanies this
-- distribution, and is available at http://www.lua.org/license.html
--
-- Contributors:
-- Fabien Fleutot - API and implementation
--
--------------------------------------------------------------------------------
-- Alternative implementation of checks() in Lua. Slower than
-- the C counterpart, but no compilation/porting concerns.
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(name, 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
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