forked from fab13n/checks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checks.c
311 lines (261 loc) · 11.5 KB
/
checks.c
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*******************************************************************************
*
* Copyright (c) 2012 Sierra Wireless 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
*
* Contributors:
*
* Sierra Wireless - initial API and implementation
*
******************************************************************************/
/**
Argument type checking API.
This library declares a `checks()` function and a `checkers` table, which
allow to check the parameters passed to a Lua function in a fast and
unobtrusive way.
`checks (type_1, ..., type_n)`, when called directly inside function
`f`, checks that `f`'s 1st argument conforms to `type_1`, that its 2nd
argument conforms to `type_2`, etc. until `type_n`. Type specifiers
are strings, and if the arguments passed to `f` don't conform to their
specification, a proper error message is produced, pinpointing the
call to `f` as the faulty expression.
Each type description `type_n` must be a string, and can describe:
* the Lua type of an object, such as `"table"`, `"number"` etc.;
* an arbitrary name, which would be stored in the `__type` field of
the argument's metatable;
* a type-checking function, which would be stored in the `checkers`
global table. This table uses type names as keys, test functions
returning Booleans as keys.
Moreover, types can be prefixed with a `"?"`, which makes them
optional. For instance, `"?table"` accepts tables as well as `nil`
values.
A `"?"` alone accepts anything. It is mainly useful as a placeholder,
to skip an argument which doesn't need to be checked.
Finally, several types can be accepted, if their names are
concatenated with a bar `"|"` between them. For instance,
`"table|number"` accepts tables as well as numbers. It can be combined
with the question mark, so `"?table|number"` accepts tables, numbers
and nil values. It is actually equivalent to `"nil|table|number"`.
More formally, let's specify `conform(a, t)`, the property that
argument `a` conforms to the type denoted by `t`. `conform(a,t)` is
true if and only if at least one of the following propositions is
verified:
* `conforms(a, t:match "^(.-)|.*"`
* `t == "?"`
* `t:sub(1, 1) == "?" and (conforms(a, t:sub(2, -1)) or a==nil)`
* `type(a) == t`
* `getmetatable(a) and getmetatable(a).__type == t`
* `checkers[t] and checkers[t](a) is true`
* `conforms(a, t:match "^.-|(.*)")`
The above propositions are listed in the order in which they are
tried by `check`. The higher they appear in the list, the faster
`checks` accepts aconforming argument. For instance,
`checks("number")` is faster than
`checkers.mynumber=function(x) return type(x)=="number" end; checks("mynumber")`.
Usage examples
--------------
require 'checks'
-- Custom checker function --
function checkers.port(p)
return type(p)=='number' and p>0 and p<0x10000
end
-- A new named type --
socket_mt = { __type='socket' }
asocket = setmetatable ({ }, socket_mt)
-- A function that checks its parameters --
function take_socket_then_port_then_maybe_string (sock, port, str)
checks ('socket', 'port', '?string')
end
take_socket_then_port_then_maybe_string (asocket, 1024, "hello")
take_socket_then_port_then_maybe_string (asocket, 1024)
-- A couple of other parameter-checking options --
function take_number_or_string()
checks("number|string")
end
function take_number_or_string_or_nil()
checks("?number|string")
end
function take_anything_followed_by_a_number()
checks("?", "number")
end
-- Catch some incorrect arguments passed to the function --
function must_fail(...)
assert (not pcall (take_socket_then_port_then_maybe_string, ...))
end
must_fail ({ }, 1024, "string") -- 1st argument isn't a socket
must_fail (asocket, -1, "string") -- port number must be 0-0xffff
must_fail (asocket, 1024, { }) -- 3rd argument cannot be a table
@module checks
*/
#include "checks.h"
#include "lauxlib.h"
#include <string.h>
/** Generate and throw an error.
@function [parent=#global] error
@param level stack level where the error must be reported
@param narg indice of the erroneous argument
@param expected name of the expected type
@param got name of the type actually found
@return never returns (throws a Lua error instead) */
static int error(
lua_State *L, int level, int narg,
const char *expected, const char *got) {
lua_Debug ar;
lua_getstack( L, level, & ar);
lua_getinfo( L, "n", & ar);
luaL_where( L, level+1);
lua_pushfstring( L, " bad argument #%d to %s (%s expected, got %s)",
narg, ar.name, expected, got);
lua_concat( L, 2);
#if 0 /* Debugging cruft */
int i;
for(i=0;i<10;i++){
if( ! lua_getstack( L, i, & ar)) break;
lua_getinfo( L, "n", & ar);
printf( "\tat level %d: '%s' / '%s'\n", i, ar.name, ar.namewhat);
}
printf( "\tend of error, level was %d\n\n", level);
#endif
return lua_error( L);
}
#define WITH_SUM_TYPES
#ifdef WITH_SUM_TYPES
/** Return true iff actualType occurs in expecteTypes, the later being
a list of type names separate by '|' chars.
If `WITH_SUM_TYPES` is disabled, the expectedTypes list must have one
element, i.e. no '|' separator character.
@function [parent=#global] matches
@param actualType the type of the tested object
@param expectedTypes the list of types listed as acceptable in `checks()`
for this argument
@return whether `actualType` is listed in `expectedTypes`.
*/
static int matches( const char *actualType, const char *expectedTypes) {
const char *p = expectedTypes;
if( ! strcmp( actualType, expectedTypes)) return 1;
while( 1) {
const char *q = strchr( p, '|');
if( ! q) {
if( p == expectedTypes) return 0; // 1st loop, no '|' at all
else q = p+strlen( expectedTypes); // last loop
}
if( ! strncmp( p, actualType, q-p)) return 1;
if( ! *q) return 0; else p = q+1;
};
}
#else
# define matches( a, b) ( ! strcmp( a, b))
#endif
/***
check whether the calling function's argument have the expected types.
`checks( [level], t_1, ..., t_n)` causes an error if the type of
argument #`i` in stack frame #`level` isn't as described by `t_i`, for
i in `[1...n]`. `level` is optional, it defaults to one (checks the
function immediately calling `checks`).
@function [parent=#global] checks
@param level the number of stack levels to ignore in the error message,
should it be produced. Optional, defaults to 1.
@param varargs one type string per expected argument.
@return nothing on success, throw an error on failure.
*/
static int checks( lua_State *L) {
lua_Debug ar;
int level=1, i=1, r;
if( lua_isnumber( L, 1)) { i = 2; level = lua_tointeger( L, 1); }
r = lua_getstack( L, level, & ar);
if( ! r) luaL_error( L, "checks() must be called within a Lua function");
/* loop for each checked argument in stack frame. */
for( /* i already initialized. */; ! lua_isnoneornil( L, i); i++) {
const char *expectedType = luaL_checkstring( L, i); // -
lua_getlocal( L, & ar, i); // val (value whose type is checked)
/* 1. Check for nil if type is optional. */
if( '?' == expectedType[0]) {
if( ! expectedType[1] /* expectedType == "?". */
|| lua_isnil( L, -1)) { /* actualType == "nil". */
lua_pop( L, 1); continue; // -
}
expectedType++;
}
// if( ! paramName) return error( L, level, i, expectedType, "nothing");
const char *actualType = lua_typename( L, lua_type( L, -1));
/* 2. Check real type. */
if( matches( actualType, expectedType)) {
lua_pop( L, 1); continue; // -
}
/* 3. Check for type name in metatable. */
if( lua_getmetatable( L, -1)) { // val, mt
lua_getfield( L, -1, "__type"); // val, mt, __type?
if( lua_isstring( L, -1)) { // val, mt, __type
if( matches( luaL_checkstring( L, -1), expectedType)) {
lua_pop( L, 3); continue; // -
} else { /* non-matching __type field. */
lua_pop( L, 2); // val
}
} else { /* no __type field. */
lua_pop( L, 2); // val
}
} else { /* no metatable. */ } // val
/* 4. Check for a custom typechecking function. */
lua_getfield( L, LUA_REGISTRYINDEX, "checkers"); // val, checkers
const char *p = expectedType;
while( 1) {
const char *q = strchr( p, '|');
if( ! q) q = p + strlen( p);
lua_pushlstring( L, p, q-p); // val, checkers, expType
lua_gettable( L, -2); // val, checkers, checkers.expType?
if( lua_isfunction( L, -1)) {
lua_pushvalue( L, -3); // val, checkers, checkers.expType, val
r = lua_pcall( L, 1, 1, 0); // val, checkers, result || msg
if( ! r && lua_toboolean( L, -1)) {// val, checkers, result==true
lua_pop( L, 3); // -
break;
} else { // val, checkers, errormsg
lua_pop( L, 1); // val, checkers
}
} else { /* no such custom checker */ // val, checkers, nil
lua_pop( L, 1); // val, checkers
}
if( ! *q) { /* last possible expected type. */
lua_pop( L, 2);
return error( L, level, i, expectedType, actualType);
} else {
p = q+1;
}
} /* for each expected type */
} /* for each i */
return 0;
}
/***
Table of custom type-checkers. This table contain type-checking
functions, indexed by type name. If an argument `a` is expected to be
of type `t`, and neither `type(a)` nor `getmetatable(a).__type` return
`t`, but `checkers[t]` contains a function, this function will be
called, with `a` as its only argument. If the function returns `true`,
then `a` is considered to be of type `t`.
Example
-------
-- Create the type-checking function --
function checkers.positive_number(x)
return type(x)=='number' and x>0
end
-- Use the `positive_number` type-checking function --
function sqrt(x)
checks('positive_number')
return x^(1/2)
end
@type checkers
*/
int luaopen_checks( lua_State *L) {
lua_newtable( L); // checkers
lua_pushvalue( L, -1); // checkers, checkers
lua_setfield( L, LUA_REGISTRYINDEX, "checkers"); // checkers
lua_setglobal( L, "checkers"); // -
lua_pushcfunction( L, checks); // checks
lua_pushvalue( L, -1); // checks, checks
lua_setglobal( L, "checks"); // checks
return 1;
}