Debian Codegraph @

engine/tolua/tolua_is.cpp

raw
1 /* tolua: functions to check types.
2 ** Support code for Lua bindings.
3 ** Written by Waldemar Celes
4 ** TeCGraf/PUC-Rio
5 ** Apr 2003
6 */
7
8 /* This code is free software; you can redistribute it and/or modify it.
9 ** The software provided hereunder is on an "as is" basis, and
10 ** the author has no obligation to provide maintenance, support, updates,
11 ** enhancements, or modifications.
12 */
13
14 #include "tolua++.h"
15 #include "lauxlib.h"
16
17 #include <stdlib.h>
18 #include <string.h>
19
20 /* a fast check if a is b, without parameter validation
21 i.e. if b is equal to a or a superclass of a. */
22 TOLUA_API int tolua_fast_isa(lua_State *L, int mt_indexa, int mt_indexb, int super_index)
23 {
24 int result;
25 if (lua_rawequal(L,mt_indexa,mt_indexb))
26 result = 1;
27 else
28 {
29 if (super_index) {
30 lua_pushvalue(L, super_index);
31 } else {
32 lua_pushliteral(L,"tolua_super");
33 lua_rawget(L,LUA_REGISTRYINDEX); /* stack: super */
34 };
35 lua_pushvalue(L,mt_indexa); /* stack: super mta */
36 lua_rawget(L,-2); /* stack: super super[mta] */
37 lua_pushvalue(L,mt_indexb); /* stack: super super[mta] mtb */
38 lua_rawget(L,LUA_REGISTRYINDEX); /* stack: super super[mta] typenameB */
39 lua_rawget(L,-2); /* stack: super super[mta] bool */
40 result = lua_toboolean(L,-1);
41 lua_pop(L,3);
42 }
43 return result;
44 }
45
46 /* Push and returns the corresponding object typename */
47 TOLUA_API const char* tolua_typename (lua_State* L, int lo)
48 {
49 int tag = lua_type(L,lo);
50 if (tag == LUA_TNONE)
51 lua_pushstring(L,"[no object]");
52 else if (tag != LUA_TUSERDATA && tag != LUA_TTABLE)
53 lua_pushstring(L,lua_typename(L,tag));
54 else if (tag == LUA_TUSERDATA)
55 {
56 if (!lua_getmetatable(L,lo))
57 lua_pushstring(L,lua_typename(L,tag));
58 else
59 {
60 lua_rawget(L,LUA_REGISTRYINDEX);
61 if (!lua_isstring(L,-1))
62 {
63 lua_pop(L,1);
64 lua_pushstring(L,"[undefined]");
65 }
66 }
67 }
68 else /* is table */
69 {
70 lua_pushvalue(L,lo);
71 lua_rawget(L,LUA_REGISTRYINDEX);
72 if (!lua_isstring(L,-1))
73 {
74 lua_pop(L,1);
75 lua_pushstring(L,"table");
76 }
77 else
78 {
79 lua_pushstring(L,"class ");
80 lua_insert(L,-2);
81 lua_concat(L,2);
82 }
83 }
84 return lua_tostring(L,-1);
85 }
86
87 TOLUA_API void tolua_error (lua_State* L, const char* msg, tolua_Error* err)
88 {
89 if (msg[0] == '#')
90 {
91 const char* expected = err->type;
92 const char* provided = tolua_typename(L,err->index);
93 if (msg[1]=='f')
94 {
95 int narg = err->index;
96 if (err->array)
97 luaL_error(L,"%s\n argument #%d is array of '%s'; array of '%s' expected.\n",
98 msg+2,narg,provided,expected);
99 else
100 luaL_error(L,"%s\n argument #%d is '%s'; '%s' expected.\n",
101 msg+2,narg,provided,expected);
102 }
103 else if (msg[1]=='v')
104 {
105 if (err->array)
106 luaL_error(L,"%s\n value is array of '%s'; array of '%s' expected.\n",
107 msg+2,provided,expected);
108 else
109 luaL_error(L,"%s\n value is '%s'; '%s' expected.\n",
110 msg+2,provided,expected);
111 }
112 }
113 else
114 luaL_error(L,msg);
115 }
116
117 /* the equivalent of lua_is* for usertable */
118 static int lua_isusertable (lua_State* L, int lo, const char* type)
119 {
120 int r = 0;
121 if (lo < 0) lo = lua_gettop(L)+lo+1;
122 lua_pushvalue(L,lo);
123 lua_rawget(L,LUA_REGISTRYINDEX); /* get registry[t] */
124 if (lua_isstring(L,-1))
125 {
126 r = strcmp(lua_tostring(L,-1),type)==0;
127 if (!r)
128 {
129 /* try const */
130 lua_pushstring(L,"const ");
131 lua_insert(L,-2);
132 lua_concat(L,2);
133 r = lua_isstring(L,-1) && strcmp(lua_tostring(L,-1),type)==0;
134 }
135 }
136 lua_pop(L, 1);
137 return r;
138 }
139
140 int push_table_instance(lua_State* L, int lo) {
141
142 if (lua_istable(L, lo)) {
143
144 lua_pushstring(L, ".c_instance");
145 lua_gettable(L, lo);
146 if (lua_isuserdata(L, -1)) {
147
148 lua_replace(L, lo);
149 return 1;
150 } else {
151
152 lua_pop(L, 1);
153 return 0;
154 };
155 } else {
156 return 0;
157 };
158
159 return 0;
160 };
161
162 /* the equivalent of lua_is* for usertype */
163 static int lua_isusertype (lua_State* L, int lo, const char* type)
164 {
165 if (!lua_isuserdata(L,lo)) {
166 if (!push_table_instance(L, lo)) {
167 return 0;
168 };
169 };
170 {
171 /* check if it is of the same type */
172 int r;
173 const char *tn;
174 if (lua_getmetatable(L,lo)) /* if metatable? */
175 {
176 lua_rawget(L,LUA_REGISTRYINDEX); /* get registry[mt] */
177 tn = lua_tostring(L,-1);
178 r = tn && (strcmp(tn,type) == 0);
179 lua_pop(L, 1);
180 if (r)
181 return 1;
182 else
183 {
184 /* check if it is a specialized class */
185 lua_pushstring(L,"tolua_super");
186 lua_rawget(L,LUA_REGISTRYINDEX); /* get super */
187 lua_getmetatable(L,lo);
188 lua_rawget(L,-2); /* get super[mt] */
189 if (lua_istable(L,-1))
190 {
191 int b;
192 lua_pushstring(L,type);
193 lua_rawget(L,-2); /* get super[mt][type] */
194 b = lua_toboolean(L,-1);
195 lua_pop(L,3);
196 if (b)
197 return 1;
198 }
199 }
200 }
201 }
202 return 0;
203 }
204
205 TOLUA_API int tolua_isnoobj (lua_State* L, int lo, tolua_Error* err)
206 {
207 if (lua_gettop(L)<abs(lo))
208 return 1;
209 err->index = lo;
210 err->array = 0;
211 err->type = "[no object]";
212 return 0;
213 }
214
215 TOLUA_API int tolua_isboolean (lua_State* L, int lo, int def, tolua_Error* err)
216 {
217 if (def && lua_gettop(L)<abs(lo))
218 return 1;
219 if (lua_isnil(L,lo) || lua_isboolean(L,lo))
220 return 1;
221 err->index = lo;
222 err->array = 0;
223 err->type = "boolean";
224 return 0;
225 }
226
227 TOLUA_API int tolua_isnumber (lua_State* L, int lo, int def, tolua_Error* err)
228 {
229 if (def && lua_gettop(L)<abs(lo))
230 return 1;
231 if (lua_isnumber(L,lo))
232 return 1;
233 err->index = lo;
234 err->array = 0;
235 err->type = "number";
236 return 0;
237 }
238
239 TOLUA_API int tolua_isstring (lua_State* L, int lo, int def, tolua_Error* err)
240 {
241 if (def && lua_gettop(L)<abs(lo))
242 return 1;
243 if (lua_isnil(L,lo) || lua_isstring(L,lo))
244 return 1;
245 err->index = lo;
246 err->array = 0;
247 err->type = "string";
248 return 0;
249 }
250
251 TOLUA_API int tolua_istable (lua_State* L, int lo, int def, tolua_Error* err)
252 {
253 if (def && lua_gettop(L)<abs(lo))
254 return 1;
255 if (lua_istable(L,lo))
256 return 1;
257 err->index = lo;
258 err->array = 0;
259 err->type = "table";
260 return 0;
261 }
262
263 TOLUA_API int tolua_isusertable (lua_State* L, int lo, const char* type, int def, tolua_Error* err)
264 {
265 if (def && lua_gettop(L)<abs(lo))
266 return 1;
267 if (lua_isusertable(L,lo,type))
268 return 1;
269 err->index = lo;
270 err->array = 0;
271 err->type = type;
272 return 0;
273 }
274
275
276 TOLUA_API int tolua_isuserdata (lua_State* L, int lo, int def, tolua_Error* err)
277 {
278 if (def && lua_gettop(L)<abs(lo))
279 return 1;
280 if (lua_isnil(L,lo) || lua_isuserdata(L,lo))
281 return 1;
282 err->index = lo;
283 err->array = 0;
284 err->type = "userdata";
285 return 0;
286 }
287
288 TOLUA_API int tolua_isvaluenil (lua_State* L, int lo, tolua_Error* err) {
289
290 if (lua_gettop(L)<abs(lo))
291 return 0; /* somebody else should chack this */
292 if (!lua_isnil(L, lo))
293 return 0;
294
295 err->index = lo;
296 err->array = 0;
297 err->type = "value";
298 return 1;
299 };
300
301 TOLUA_API int tolua_isvalue (lua_State* L, int lo, int def, tolua_Error* err)
302 {
303 if (def || abs(lo)<=lua_gettop(L)) /* any valid index */
304 return 1;
305 err->index = lo;
306 err->array = 0;
307 err->type = "value";
308 return 0;
309 }
310
311 TOLUA_API int tolua_isusertype (lua_State* L, int lo, const char* type, int def, tolua_Error* err)
312 {
313 if (def && lua_gettop(L)<abs(lo))
314 return 1;
315 if (lua_isnil(L,lo) || lua_isusertype(L,lo,type))
316 return 1;
317 err->index = lo;
318 err->array = 0;
319 err->type = type;
320 return 0;
321 }
322
323 TOLUA_API int tolua_isvaluearray
324 (lua_State* L, int lo, int dim, int def, tolua_Error* err)
325 {
326 if (!tolua_istable(L,lo,def,err))
327 return 0;
328 else
329 return 1;
330 }
331
332 TOLUA_API int tolua_isbooleanarray
333 (lua_State* L, int lo, int dim, int def, tolua_Error* err)
334 {
335 if (!tolua_istable(L,lo,def,err))
336 return 0;
337 else
338 {
339 int i;
340 for (i=1; i<=dim; ++i)
341 {
342 lua_pushnumber(L,i);
343 lua_gettable(L,lo);
344 if (!(lua_isnil(L,-1) || lua_isboolean(L,-1)) &&
345 !(def && lua_isnil(L,-1))
346 )
347 {
348 err->index = lo;
349 err->array = 1;
350 err->type = "boolean";
351 return 0;
352 }
353 lua_pop(L,1);
354 }
355 }
356 return 1;
357 }
358
359 TOLUA_API int tolua_isnumberarray
360 (lua_State* L, int lo, int dim, int def, tolua_Error* err)
361 {
362 if (!tolua_istable(L,lo,def,err))
363 return 0;
364 else
365 {
366 int i;
367 for (i=1; i<=dim; ++i)
368 {
369 lua_pushnumber(L,i);
370 lua_gettable(L,lo);
371 if (!lua_isnumber(L,-1) &&
372 !(def && lua_isnil(L,-1))
373 )
374 {
375 err->index = lo;
376 err->array = 1;
377 err->type = "number";
378 return 0;
379 }
380 lua_pop(L,1);
381 }
382 }
383 return 1;
384 }
385
386 TOLUA_API int tolua_isstringarray
387 (lua_State* L, int lo, int dim, int def, tolua_Error* err)
388 {
389 if (!tolua_istable(L,lo,def,err))
390 return 0;
391 else
392 {
393 int i;
394 for (i=1; i<=dim; ++i)
395 {
396 lua_pushnumber(L,i);
397 lua_gettable(L,lo);
398 if (!(lua_isnil(L,-1) || lua_isstring(L,-1)) &&
399 !(def && lua_isnil(L,-1))
400 )
401 {
402 err->index = lo;
403 err->array = 1;
404 err->type = "string";
405 return 0;
406 }
407 lua_pop(L,1);
408 }
409 }
410 return 1;
411 }
412
413 TOLUA_API int tolua_istablearray
414 (lua_State* L, int lo, int dim, int def, tolua_Error* err)
415 {
416 if (!tolua_istable(L,lo,def,err))
417 return 0;
418 else
419 {
420 int i;
421 for (i=1; i<=dim; ++i)
422 {
423 lua_pushnumber(L,i);
424 lua_gettable(L,lo);
425 if (! lua_istable(L,-1) &&
426 !(def && lua_isnil(L,-1))
427 )
428 {
429 err->index = lo;
430 err->array = 1;
431 err->type = "table";
432 return 0;
433 }
434 lua_pop(L,1);
435 }
436 }
437 return 1;
438 }
439
440 TOLUA_API int tolua_isuserdataarray
441 (lua_State* L, int lo, int dim, int def, tolua_Error* err)
442 {
443 if (!tolua_istable(L,lo,def,err))
444 return 0;
445 else
446 {
447 int i;
448 for (i=1; i<=dim; ++i)
449 {
450 lua_pushnumber(L,i);
451 lua_gettable(L,lo);
452 if (!(lua_isnil(L,-1) || lua_isuserdata(L,-1)) &&
453 !(def && lua_isnil(L,-1))
454 )
455 {
456 err->index = lo;
457 err->array = 1;
458 err->type = "userdata";
459 return 0;
460 }
461 lua_pop(L,1);
462 }
463 }
464 return 1;
465 }
466
467 TOLUA_API int tolua_isusertypearray
468 (lua_State* L, int lo, const char* type, int dim, int def, tolua_Error* err)
469 {
470 if (!tolua_istable(L,lo,def,err))
471 return 0;
472 else
473 {
474 int i;
475 for (i=1; i<=dim; ++i)
476 {
477 lua_pushnumber(L,i);
478 lua_gettable(L,lo);
479 if (!(lua_isnil(L,-1) || lua_isuserdata(L,-1)) &&
480 !(def && lua_isnil(L,-1))
481 )
482 {
483 err->index = lo;
484 err->type = type;
485 err->array = 1;
486 return 0;
487 }
488 lua_pop(L,1);
489 }
490 }
491 return 1;
492 }
493
494 #if 0
495 int tolua_isbooleanfield
496 (lua_State* L, int lo, int i, int def, tolua_Error* err)
497 {
498 lua_pushnumber(L,i);
499 lua_gettable(L,lo);
500 if (!(lua_isnil(L,-1) || lua_isboolean(L,-1)) &&
501 !(def && lua_isnil(L,-1))
502 )
503 {
504 err->index = lo;
505 err->array = 1;
506 err->type = "boolean";
507 return 0;
508 }
509 lua_pop(L,1);
510 return 1;
511 }
512
513 int tolua_isnumberfield
514 (lua_State* L, int lo, int i, int def, tolua_Error* err)
515 {
516 lua_pushnumber(L,i);
517 lua_gettable(L,lo);
518 if (!lua_isnumber(L,-1) &&
519 !(def && lua_isnil(L,-1))
520 )
521 {
522 err->index = lo;
523 err->array = 1;
524 err->type = "number";
525 return 0;
526 }
527 lua_pop(L,1);
528 return 1;
529 }
530
531 int tolua_isstringfield
532 (lua_State* L, int lo, int i, int def, tolua_Error* err)
533 {
534 lua_pushnumber(L,i);
535 lua_gettable(L,lo);
536 if (!(lua_isnil(L,-1) || lua_isstring(L,-1)) &&
537 !(def && lua_isnil(L,-1))
538 )
539 {
540 err->index = lo;
541 err->array = 1;
542 err->type = "string";
543 return 0;
544 }
545 lua_pop(L,1);
546 return 1;
547 }
548
549 int tolua_istablefield
550 (lua_State* L, int lo, int i, int def, tolua_Error* err)
551 {
552 lua_pushnumber(L,i+1);
553 lua_gettable(L,lo);
554 if (! lua_istable(L,-1) &&
555 !(def && lua_isnil(L,-1))
556 )
557 {
558 err->index = lo;
559 err->array = 1;
560 err->type = "table";
561 return 0;
562 }
563 lua_pop(L,1);
564 }
565
566 int tolua_isusertablefield
567 (lua_State* L, int lo, const char* type, int i, int def, tolua_Error* err)
568 {
569 lua_pushnumber(L,i);
570 lua_gettable(L,lo);
571 if (! lua_isusertable(L,-1,type) &&
572 !(def && lua_isnil(L,-1))
573 )
574 {
575 err->index = lo;
576 err->array = 1;
577 err->type = type;
578 return 0;
579 }
580 lua_pop(L,1);
581 return 1;
582 }
583
584 int tolua_isuserdatafield
585 (lua_State* L, int lo, int i, int def, tolua_Error* err)
586 {
587 lua_pushnumber(L,i);
588 lua_gettable(L,lo);
589 if (!(lua_isnil(L,-1) || lua_isuserdata(L,-1)) &&
590 !(def && lua_isnil(L,-1))
591 )
592 {
593 err->index = lo;
594 err->array = 1;
595 err->type = "userdata";
596 return 0;
597 }
598 lua_pop(L,1);
599 return 1;
600 }
601
602 int tolua_isusertypefield
603 (lua_State* L, int lo, const char* type, int i, int def, tolua_Error* err)
604 {
605 lua_pushnumber(L,i);
606 lua_gettable(L,lo);
607 if (!(lua_isnil(L,-1) || lua_isusertype(L,-1,type)) &&
608 !(def && lua_isnil(L,-1))
609 )
610 {
611 err->index = lo;
612 err->type = type;
613 err->array = 1;
614 return 0;
615 }
616 lua_pop(L,1);
617 return 1;
618 }
619
620 #endif