forked from victorliu/S4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua_named_arg.c
251 lines (237 loc) · 7.42 KB
/
lua_named_arg.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
#include "lua_named_arg.h"
#include <lualib.h>
#include <lauxlib.h>
#include <string.h>
#include <stdio.h>
int lua_named_arg_error(lua_State *L, int narg, const char *name, const char *extramsg){
lua_Debug ar;
if(NULL == name){ return luaL_argerror(L, narg, extramsg); }
if(!lua_getstack(L, 0, &ar)){ /* no stack frame? */
return luaL_error(L, "bad argument " LUA_QS " (%s)", name, extramsg);
}
lua_getinfo(L, "n", &ar);
if(0 == strcmp(ar.namewhat, "method")){
narg--; /* do not count `self' */
if(0 == narg){ /* error is in the self argument itself? */
return luaL_error(L, "calling " LUA_QS " on bad self (%s)", ar.name, extramsg);
}
}
if(NULL == ar.name){
ar.name = "?";
}
return luaL_error(L, "bad argument " LUA_QS " to " LUA_QS " (%s)", name, ar.name, extramsg);
}
// Parses the object at stack index `index' as a 2-vector, placing the values into `v'
// If the object was a named argument, then an optional `argname' may be provided for
// more helpful error messages.
int lua_arg_parse_vec2(lua_State *L, int index, const char *argname, double *v){
int i;
if(!lua_istable(L, index) || 2 != lua_rawlen(L, index)){
return lua_named_arg_error(L, index, argname, "expected argument type: vec2");
}
for(i = 0; i < 2; ++i){
lua_rawgeti(L, index, i+1);
if(!lua_isnumber(L, -1)){
return lua_named_arg_error(L, index, argname, "argument of type vec2 does not contain numbers");
}
v[i] = lua_tonumber(L, -1);
lua_pop(L, 1);
}
return 0;
}
int lua_arg_parse_vec3(lua_State *L, int index, const char *argname, double *v){
int i;
if(!lua_istable(L, index) || 3 != lua_rawlen(L, index)){
return lua_named_arg_error(L, index, argname, "expected type: vec2");
}
for(i = 0; i < 3; ++i){
lua_rawgeti(L, index, i+1);
if(!lua_isnumber(L, -1)){
return lua_named_arg_error(L, index, argname, "argument of type vec2 does not contain numbers");
}
v[i] = lua_tonumber(L, -1);
lua_pop(L, 1);
}
return 0;
}
#define lua_arg_parse_ctensor3_err() \
lua_named_arg_error(L, index, argname, "expected type: scalar, complex, or ctensor3")
int lua_arg_parse_ctensor3(lua_State *L, int index, const char *argname, double *m){
int i, j, k, l, ikind = 0;
for(i = 0; i < 18; ++i){ m[i] = 0.; }
if(lua_isnumber(L, index)){
m[0] = lua_tonumber(L, index);
m[8] = m[0];
m[16] = m[0];
return 0;
}else if(!lua_istable(L, index)){
return lua_arg_parse_ctensor3_err();
}
if(index < 0){ index += 1+lua_gettop(L); }
lua_len(L, index); l = lua_tointeger(L, -1); lua_pop(L, 1);
if(2 == l){ /* complex number */
for(k = 0; k < 2; ++k){
lua_pushinteger(L, k+1);
lua_gettable(L, index);
if(!lua_isnumber(L, -1)){
return lua_named_arg_error(L, index, argname, "non-numeric value in what looks like a complex number");
}
m[k] = lua_tonumber(L, -1);
lua_pop(L, 1);
}
m[8] = m[0];
m[9] = m[1];
m[16] = m[0];
m[17] = m[1];
return 0;
}else if(3 != l){
return lua_arg_parse_ctensor3_err();
}
/* Now we should expect a 3 vector or a 3x3 matrix */
/* Iterate over the rows */
for(i = 0; i < 3; ++i){
lua_pushinteger(L, i+1);
lua_gettable(L, index);
if(0 == ikind){
if(lua_isnumber(L, -1)){
ikind = 1;
}else if(!lua_istable(L, -1)){
return lua_arg_parse_ctensor3_err();
}else{
lua_len(L, -1); l = lua_tointeger(L, -1); lua_pop(L, 1);
if(2 == l){
ikind = 2;
}else if(3 == l){
ikind = 3;
}else{
return lua_arg_parse_ctensor3_err();
}
}
}
if(1 == ikind){ /* real diagonal (3-vector) */
if(!lua_isnumber(L, -1)){ return lua_arg_parse_ctensor3_err(); }
m[8*i] = lua_tonumber(L, -1);
}else if(2 == ikind){ /* complex diagonal (3-cvector) */
if(!lua_istable(L, -1)){ return lua_arg_parse_ctensor3_err(); }
lua_len(L, -1); l = lua_tointeger(L, -1); lua_pop(L, 1);
if(2 != l){ return lua_arg_parse_ctensor3_err(); }
for(k = 0; k < 2; ++k){
lua_pushinteger(L, k+1);
lua_gettable(L, -2);
m[8*i+k] = lua_tonumber(L, -1);
lua_pop(L, 1);
}
}else if(3 == ikind){ /* 3x3 matrix */
int jkind = 0;
if(!lua_istable(L, -1)){ return lua_arg_parse_ctensor3_err(); }
lua_len(L, -1); l = lua_tointeger(L, -1); lua_pop(L, 1);
if(3 != l){ return lua_arg_parse_ctensor3_err(); }
for(j = 0; j < 3; ++j){
lua_pushinteger(L, j+1);
lua_gettable(L, -2);
if(0 == jkind){
if(lua_isnumber(L, -1)){
jkind = 1;
}else if(!lua_istable(L, -1)){
return lua_arg_parse_ctensor3_err();
}else{
lua_len(L, -1); l = lua_tointeger(L, -1); lua_pop(L, 1);
if(2 == l){
jkind = 2;
}else{
return lua_arg_parse_ctensor3_err();
}
}
}
if(1 == jkind){ /* real diagonal (3-vector) */
if(!lua_isnumber(L, -1)){ return lua_arg_parse_ctensor3_err(); }
m[(i+j*3)*2] = lua_tonumber(L, -1);
}else if(2 == jkind){ /* complex diagonal (3-cvector) */
if(!lua_istable(L, -1)){ return lua_arg_parse_ctensor3_err(); }
lua_len(L, -1); l = lua_tointeger(L, -1); lua_pop(L, 1);
if(2 != l){ return lua_arg_parse_ctensor3_err(); }
for(k = 0; k < 2; ++k){
lua_pushinteger(L, k+1);
lua_gettable(L, -2);
m[k+(i+j*3)*2] = lua_tonumber(L, -1);
lua_pop(L, 1);
}
}
lua_pop(L, 1);
}
}
lua_pop(L, 1);
i++;
}
return 0;
}
/* Checks to make sure there is a table of named arguments at stack index `index'. */
int lua_named_arg_check(lua_State *L, int index, int nreq, int nopt, const char *args[]){
int i;
if(!lua_istable(L, index)){
return luaL_argerror(L, index, "expected named arguments");
}
lua_pushnil(L);
while(lua_next(L, index) != 0){
/* key at index -2 and value at index -1 */
const char *key;
int found = 0;
if(!lua_isstring(L, -2)){
return luaL_argerror(L, index, "non-string key in named argument table");
}
key = lua_tostring(L, -2);
for(i = 0; i < nreq+nopt; ++i){
if(NULL != args[i] && 0 == strcmp(key, args[i])){
args[i] = NULL;
found = 1;
break;
}
}
if(!found){
return luaL_error(L, "unexpected argument: " LUA_QS, key);
}
lua_pop(L, 1); /* remove value, keep key for next iteration */
}
for(i = 0; i < nreq; ++i){
if(NULL != args[i]){
return luaL_error(L, "missing argument: " LUA_QS, args[i]);
}
}
return 0;
}
/* For a table of named arguments at stack index `index', returns a string with
* argument name `argname'.
*/
int lua_named_arg_get_string(lua_State *L, int index, const char *argname, const char **value){
lua_getfield(L, index, argname);
if(!lua_isstring(L, -1)){
return lua_named_arg_error(L, index, argname, "expected type: string");
}
*value = lua_tostring(L, -1);
lua_pop(L, 1);
return 0;
}
int lua_named_arg_get_number(lua_State *L, int index, const char *argname, double *value){
lua_getfield(L, index, argname);
if(!lua_isnumber(L, -1)){
return lua_named_arg_error(L, index, argname, "expected type: number");
}
*value = lua_tonumber(L, -1);
lua_pop(L, 1);
return 0;
}
int lua_named_arg_exists(lua_State *L, int index, const char *argname){
int ret;
lua_getfield(L, index, argname);
ret = !lua_isnil(L, -1);
lua_pop(L, 1);
return ret;
}
int lua_named_arg_get(lua_State *L, int index, const char *argname){
lua_getfield(L, index, argname);
if(lua_isnil(L, -1)){
lua_pop(L, 1);
return 0;
}
return 1;
}