/
arguments.lua
296 lines (256 loc) · 7.23 KB
/
arguments.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
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
local select, tostring, assert, type, error
= select, tostring, assert, type, error
--------------------------------------------------------------------------------
local run_plain_assert = function(a, b, c)
assert(type(a) == "number")
assert(type(b) == "boolean")
assert(type(c) == "string")
end
--------------------------------------------------------------------------------
local run_assert_is
do
local make_assert_is = function(typename)
return function(v, msg)
if type(v) == typename then
return v
end
error(
(msg or "assertion failed")
.. ": expected `" .. typename .. "', got `"
.. type(v) .. "'",
3
)
end
end
local assert_is_number = make_assert_is("number")
local assert_is_boolean = make_assert_is("boolean")
local assert_is_string = make_assert_is("string")
run_assert_is = function(a, b, c)
assert_is_number(a)
assert_is_boolean(b)
assert_is_string(c)
end
end
--------------------------------------------------------------------------------
local run_arguments_select_simple
do
local arguments_select = function(...)
local nargs = select("#", ...)
for i = 1, nargs, 2 do
local expected_type, value = select(i, ...)
if type(value) ~= expected_type then
error(
"bad argument #" .. ((i + 1) / 2)
.. " type: expected `" .. expected_type
.. "', got `" .. type(value) .. "'",
3
)
end
end
end
run_arguments_select_simple = function(a, b, c)
arguments_select(
"number", a,
"boolean", b,
"string", c
)
end
end
--------------------------------------------------------------------------------
local run_arguments_recursive_simple
do
-- Simplified lua-nucleo version, equivalent to the others.
local function impl(arg_n, expected_type, value, ...)
-- Points error on function, calling function which calls *arguments()
if type(value) ~= expected_type then
error(
"argument #"..arg_n..": expected `"..tostring(expected_type)
.. "', got `"..type(value).."'",
3 + arg_n
)
end
-- If have at least one more type, check it
return ((...) ~= nil) and impl(arg_n + 1, ...) or true
end
local arguments_recursive = function(...)
local nargs = select('#', ...)
return (nargs > 0)
and impl(1, ...)
or true
end
run_arguments_recursive_simple = function(a, b, c)
arguments_recursive(
"number", a,
"boolean", b,
"string", c
)
end
end
--------------------------------------------------------------------------------
local run_arguments_recursive_lua_nucleo
do
-- Taken directly from lua-nucleo
local lua51_types =
{
["nil"] = true;
["boolean"] = true;
["number"] = true;
["string"] = true;
["table"] = true;
["function"] = true;
["thread"] = true;
["userdata"] = true;
}
local function impl(is_optional, arg_n, expected_type, value, ...)
-- Points error on function, calling function which calls *arguments()
if type(value) ~= expected_type then
if not lua51_types[expected_type] then
error(
"argument #"..arg_n..": bad expected type `"..tostring(expected_type).."'",
3 + arg_n
)
end
if not is_optional or value ~= nil then
error(
(is_optional and "optional" or "")
.. "argument #"..arg_n..": expected `"..tostring(expected_type)
.. "', got `"..type(value).."'",
3 + arg_n
)
end
end
-- If have at least one more type, check it
return ((...) ~= nil) and impl(is_optional, arg_n + 1, ...) or true
end
local arguments_recursive = function(...)
local nargs = select('#', ...)
return (nargs > 0)
and (
(nargs % 2 == 0)
and impl(false, 1, ...) -- Not optional
or error("arguments: bad call, dangling argument detected")
)
or true
end
run_arguments_recursive_lua_nucleo = function(a, b, c)
arguments_recursive(
"number", a,
"boolean", b,
"string", c
)
end
end
--------------------------------------------------------------------------------
-- TODO: Add a version with full-blown validation.
local run_arguments_unroll_simple
do
-- TODO: Put a code-generation metatable over cache
-- and pre-populate it for cases with (1-10) * 2 arguments.
-- If __index sees odd number, it should crash
-- with dangling argument error.
local arguments_cache =
{
[6] = function(t1, v1, t2, v2, t3, v3)
if type(v1) ~= t1 then
error(
"argument #1: expected `"..tostring(t1)
.. "', got `"..type(v1).."'",
4
)
end
if type(v2) ~= t2 then
error(
"argument #2: expected `"..tostring(t2)
.. "', got `"..type(v2).."'",
4
)
end
if type(v3) ~= t3 then
error(
"argument #3: expected `"..tostring(t3)
.. "', got `"..type(v3).."'",
4
)
end
end;
}
local arguments = function(...)
local n = select("#", ...)
-- Assuming cache is pre-populated for all possible use-cases
return assert(arguments_cache[n])(...)
end
run_arguments_unroll_simple = function(a, b, c)
arguments(
"number", a,
"boolean", b,
"string", c
)
end
end
--------------------------------------------------------------------------------
local run_arguments_hardcoded_simple
do
-- Not much real-word meaning, just for comparison with
-- run_arguments_unroll_simple.
local hardcoded_arguments_6 = function(t1, v1, t2, v2, t3, v3)
if type(v1) ~= t1 then
error(
"argument #1: expected `"..tostring(t1)
.. "', got `"..type(v1).."'",
2
)
end
if type(v2) ~= t2 then
error(
"argument #2: expected `"..tostring(t2)
.. "', got `"..type(v2).."'",
2
)
end
if type(v3) ~= t3 then
error(
"argument #3: expected `"..tostring(t3)
.. "', got `"..type(v3).."'",
2
)
end
end
run_arguments_hardcoded_simple = function(a, b, c)
hardcoded_arguments_6(
"number", a,
"boolean", b,
"string", c
)
end
end
--------------------------------------------------------------------------------
local bench = { }
bench.plain_assert = function()
run_plain_assert(42, true, "aaa")
end
bench.assert_is = function()
run_assert_is(42, true, "aaa")
end
bench.assert_is_alloc = function()
-- Imitating args table allocation.
-- Needed to compensate plain Lua interpreter
-- compatibility mode.
local a, b, c = { }, { }, { }
run_assert_is(42, true, "aaa")
end
bench.args_select_simple = function()
run_arguments_select_simple(42, true, "aaa")
end
bench.args_recursive_simp = function()
run_arguments_recursive_simple(42, true, "aaa")
end
bench.args_recursive_ln = function()
run_arguments_recursive_lua_nucleo(42, true, "aaa")
end
bench.args_unroll_simple = function()
run_arguments_unroll_simple(42, true, "aaa")
end
bench.args_hard_simple = function()
run_arguments_hardcoded_simple(42, true, "aaa")
end
return bench