143
|
1 |
#include "lua_func.h"
|
|
2 |
#include "error.h"
|
|
3 |
|
|
4 |
#include <lua5.1/lauxlib.h>
|
|
5 |
|
|
6 |
/**
|
|
7 |
* Pushes onto the stack the value at t[i]
|
|
8 |
*/
|
|
9 |
static void lua_getindex (lua_State *L, int t, int i)
|
|
10 |
{
|
|
11 |
lua_pushinteger(L, i);
|
|
12 |
lua_gettable(L, t);
|
|
13 |
}
|
|
14 |
|
|
15 |
/**
|
|
16 |
* Pushes onto the stack either:
|
|
17 |
* * the value at t[name]
|
|
18 |
* * the value at t[index]
|
|
19 |
*
|
|
20 |
* Returns the new index, or 0, if neither could be found
|
|
21 |
*/
|
|
22 |
static int lua_arg_lookup (lua_State *L, int t, const char *name, int index)
|
|
23 |
{
|
|
24 |
// try name
|
|
25 |
lua_getfield(L, t, name);
|
|
26 |
|
|
27 |
if (!lua_isnil(L, -1))
|
|
28 |
return lua_gettop(L);
|
|
29 |
else
|
|
30 |
lua_pop(L, 1);
|
|
31 |
|
|
32 |
// try index
|
|
33 |
lua_getindex(L, t, index);
|
|
34 |
|
|
35 |
if (!lua_isnil(L, -1))
|
|
36 |
return lua_gettop(L);
|
|
37 |
|
|
38 |
else
|
|
39 |
lua_pop(L, 1);
|
|
40 |
|
|
41 |
// not found
|
|
42 |
return 0;
|
|
43 |
}
|
|
44 |
|
|
45 |
static const char *_lua_arg_string (lua_State *L, int index, const char *name, const char *def)
|
|
46 |
{
|
|
47 |
const char *value;
|
|
48 |
|
|
49 |
// use default?
|
|
50 |
if (lua_isnoneornil(L, index) && def != (const char *) LUA_ARG_REQUIRED)
|
|
51 |
return def;
|
|
52 |
|
|
53 |
// value given?
|
|
54 |
if ((value = lua_tostring(L, index)))
|
|
55 |
return value;
|
|
56 |
|
|
57 |
// error
|
|
58 |
luaL_error(L, "missing value for required string argument <%d:%s>", index, name); return NULL;
|
|
59 |
}
|
|
60 |
|
|
61 |
static bool _lua_arg_bool (lua_State *L, int index, const char *name, int def)
|
|
62 |
{
|
|
63 |
(void) name;
|
|
64 |
|
|
65 |
// use default?
|
|
66 |
if (lua_isnoneornil(L, index) && def != LUA_ARG_REQUIRED)
|
|
67 |
return def;
|
|
68 |
|
|
69 |
// value given
|
|
70 |
return lua_toboolean(L, index);
|
|
71 |
}
|
|
72 |
|
|
73 |
/**
|
|
74 |
* Look up the arg index to use for the given index/name.
|
|
75 |
*
|
|
76 |
* If no value is found for the corresponding index, returns zero.
|
|
77 |
*/
|
|
78 |
static int lua_arg_index (lua_State *L, int nargs, int index, const char *name)
|
|
79 |
{
|
|
80 |
// lookup from table?
|
|
81 |
if (name && lua_istable(L, 2)) {
|
|
82 |
// push the value from the field onto the stack
|
|
83 |
lua_getfield(L, 2, name);
|
|
84 |
|
|
85 |
// no named field?
|
|
86 |
if (lua_isnil(L, -1)) {
|
|
87 |
lua_pop(L, 1);
|
|
88 |
|
|
89 |
lua_getindex(L, 2, index - 1);
|
|
90 |
}
|
|
91 |
|
|
92 |
// no index field?
|
|
93 |
if (lua_isnil(L, -1)) {
|
|
94 |
lua_pop(L, 1);
|
|
95 |
|
|
96 |
return 0;
|
|
97 |
}
|
|
98 |
|
|
99 |
// found either a named or indexed arg
|
|
100 |
return lua_gettop(L);
|
|
101 |
|
|
102 |
} else if (index <= nargs) {
|
|
103 |
// use the same index
|
|
104 |
return index;
|
|
105 |
|
|
106 |
} else {
|
|
107 |
// no index
|
|
108 |
return 0;
|
|
109 |
}
|
|
110 |
}
|
|
111 |
|
|
112 |
const char *lua_arg_string (lua_State *L, int nargs, int index, const char *name, const char *def)
|
|
113 |
{
|
|
114 |
return _lua_arg_string(L, lua_arg_index(L, nargs, index, name), name, def);
|
|
115 |
}
|
|
116 |
|
|
117 |
bool lua_arg_bool (lua_State *L, int nargs, int index, const char *name, int def)
|
|
118 |
{
|
|
119 |
return _lua_arg_bool(L, lua_arg_index(L, nargs, index, name), name, def);
|
|
120 |
}
|
|
121 |
|
|
122 |
void lua_args_parse (lua_State *L, const struct lua_func *func, void **obj_ptr, ...)
|
|
123 |
{
|
|
124 |
int argidx = 1, argtbl = 0, idx;
|
|
125 |
const struct lua_func_arg *arg;
|
|
126 |
va_list vargs;
|
|
127 |
|
|
128 |
// first, the obj argument
|
145
|
129 |
if (func->type)
|
|
130 |
*obj_ptr = lua_type_get(L, func->type, argidx++);
|
143
|
131 |
|
|
132 |
// were we given a table of arguments?
|
|
133 |
if (lua_istable(L, argidx))
|
|
134 |
argtbl = argidx++;
|
|
135 |
|
|
136 |
// parse the args
|
|
137 |
va_start(vargs, obj_ptr);
|
|
138 |
|
|
139 |
for (arg = func->args, idx = 1; arg->name && arg->type; arg++, idx++) {
|
|
140 |
int index;
|
|
141 |
|
|
142 |
// map index
|
|
143 |
if (!argtbl)
|
|
144 |
// direct
|
|
145 |
index = argidx++;
|
|
146 |
|
|
147 |
else
|
|
148 |
// lookup from table
|
|
149 |
index = lua_arg_lookup(L, argtbl, arg->name, idx);
|
|
150 |
|
|
151 |
// apply
|
|
152 |
switch (arg->type) {
|
|
153 |
case LUA_ARG_STRING:
|
|
154 |
*va_arg(vargs, const char **) = _lua_arg_string(L, index, arg->name, arg->def.string);
|
|
155 |
break;
|
|
156 |
|
|
157 |
case LUA_ARG_BOOL:
|
|
158 |
*va_arg(vargs, bool *) = _lua_arg_bool(L, index, arg->name, arg->def.boolean);
|
|
159 |
break;
|
|
160 |
|
|
161 |
default:
|
|
162 |
NOT_REACHED();
|
|
163 |
};
|
|
164 |
}
|
|
165 |
|
|
166 |
va_end(vargs);
|
|
167 |
}
|