Debian Codegraph @

3rdparty/toluapp/src/lib/tolua_map.c

raw
1 /* tolua: functions to map features
2 ** Support code for Lua bindings.
3 ** Written by Waldemar Celes
4 ** TeCGraf/PUC-Rio
5 ** Apr 2003
6 ** $Id: $
7 */
8
9 /* This code is free software; you can redistribute it and/or modify it.
10 ** The software provided hereunder is on an "as is" basis, and
11 ** the author has no obligation to provide maintenance, support, updates,
12 ** enhancements, or modifications.
13 */
14
15 #include "tolua++.h"
16 #include "tolua_event.h"
17 #include "lauxlib.h"
18
19 #include <string.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <math.h>
23
24 /* Create metatable
25 * Create and register new metatable
26 */
27 static int tolua_newmetatable(lua_State *L, const char *name)
28 {
29 int r = luaL_newmetatable(L, name);
30
31 #ifdef LUA_VERSION_NUM /* only lua 5.1 */
32 if (r)
33 {
34 lua_pushvalue(L, -1);
35 lua_pushstring(L, name);
36 lua_settable(L, LUA_REGISTRYINDEX); /* reg[mt] = type_name */
37 }
38 #endif
39
40 if (r)
41 tolua_classevents(L); /* set meta events */
42 lua_pop(L, 1);
43 return r;
44 }
45
46 /* Map super classes
47 * It sets 'name' as being also a 'base', mapping all super classes of 'base' in 'name'
48 */
49 static void mapsuper(lua_State *L, const char *name, const char *base)
50 {
51 /* push registry.super */
52 lua_pushstring(L, "tolua_super");
53 lua_rawget(L, LUA_REGISTRYINDEX); /* stack: super */
54 luaL_getmetatable(L, name); /* stack: super mt */
55 lua_rawget(L, -2); /* stack: super table */
56 if (lua_isnil(L, -1))
57 {
58 /* create table */
59 lua_pop(L, 1);
60 lua_newtable(L); /* stack: super table */
61 luaL_getmetatable(L, name); /* stack: super table mt */
62 lua_pushvalue(L, -2); /* stack: super table mt table */
63 lua_rawset(L, -4); /* stack: super table */
64 }
65
66 /* set base as super class */
67 lua_pushstring(L, base);
68 lua_pushboolean(L, 1);
69 lua_rawset(L, -3); /* stack: super table */
70
71 /* set all super class of base as super class of name */
72 luaL_getmetatable(L, base); /* stack: super table base_mt */
73 lua_rawget(L, -3); /* stack: super table base_table */
74 if (lua_istable(L, -1))
75 {
76 /* traverse base table */
77 lua_pushnil(L); /* first key */
78 while (lua_next(L, -2) != 0)
79 {
80 /* stack: ... base_table key value */
81 lua_pushvalue(L, -2); /* stack: ... base_table key value key */
82 lua_insert(L, -2); /* stack: ... base_table key key value */
83 lua_rawset(L, -5); /* stack: ... base_table key */
84 }
85 }
86 lua_pop(L, 3); /* stack: <empty> */
87 }
88
89 /* creates a 'tolua_ubox' table for base clases, and
90 // expects the metatable and base metatable on the stack */
91 static void set_ubox(lua_State *L)
92 {
93
94 /* mt basemt */
95 if (!lua_isnil(L, -1))
96 {
97 lua_pushstring(L, "tolua_ubox");
98 lua_rawget(L, -2);
99 }
100 else
101 {
102 lua_pushnil(L);
103 }
104 /* mt basemt base_ubox */
105 if (!lua_isnil(L, -1))
106 {
107 lua_pushstring(L, "tolua_ubox");
108 lua_insert(L, -2);
109 /* mt basemt key ubox */
110 lua_rawset(L, -4);
111 /* (mt with ubox) basemt */
112 }
113 else
114 {
115 /* mt basemt nil */
116 lua_pop(L, 1);
117 lua_pushstring(L, "tolua_ubox");
118 lua_newtable(L);
119 /* make weak value metatable for ubox table to allow userdata to be
120 garbage-collected */
121 lua_newtable(L);
122 lua_pushliteral(L, "__mode");
123 lua_pushliteral(L, "v");
124 lua_rawset(L, -3); /* stack: string ubox mt */
125 lua_setmetatable(L, -2); /* stack:mt basemt string ubox */
126 lua_rawset(L, -4);
127 }
128 }
129
130 /* Map inheritance
131 * It sets 'name' as derived from 'base' by setting 'base' as metatable of 'name'
132 */
133 static void mapinheritance(lua_State *L, const char *name, const char *base)
134 {
135 /* set metatable inheritance */
136 luaL_getmetatable(L, name);
137
138 if (base && *base)
139 luaL_getmetatable(L, base);
140 else
141 {
142
143 if (lua_getmetatable(L, -1))
144 { /* already has a mt, we don't overwrite it */
145 lua_pop(L, 2);
146 return;
147 }
148 luaL_getmetatable(L, "tolua_commonclass");
149 }
150
151 set_ubox(L);
152
153 lua_setmetatable(L, -2);
154 lua_pop(L, 1);
155 }
156
157 /* Object type
158 */
159 static int tolua_bnd_type(lua_State *L)
160 {
161 tolua_typename(L, lua_gettop(L));
162 return 1;
163 }
164
165 /* Take ownership
166 */
167 static int tolua_bnd_takeownership(lua_State *L)
168 {
169 int success = 0;
170 if (lua_isuserdata(L, 1))
171 {
172 if (lua_getmetatable(L, 1)) /* if metatable? */
173 {
174 lua_pop(L, 1); /* clear metatable off stack */
175 /* force garbage collection to avoid C to reuse a to-be-collected address */
176 #ifdef LUA_VERSION_NUM
177 lua_gc(L, LUA_GCCOLLECT, 0);
178 #else
179 lua_setgcthreshold(L, 0);
180 #endif
181
182 success = tolua_register_gc(L, 1);
183 }
184 }
185 lua_pushboolean(L, success != 0);
186 return 1;
187 }
188
189 /* Release ownership
190 */
191 static int tolua_bnd_releaseownership(lua_State *L)
192 {
193 int done = 0;
194 if (lua_isuserdata(L, 1))
195 {
196 void *u = *((void **)lua_touserdata(L, 1));
197 /* force garbage collection to avoid releasing a to-be-collected address */
198 #ifdef LUA_VERSION_NUM
199 lua_gc(L, LUA_GCCOLLECT, 0);
200 #else
201 lua_setgcthreshold(L, 0);
202 #endif
203 lua_pushstring(L, "tolua_gc");
204 lua_rawget(L, LUA_REGISTRYINDEX);
205 lua_pushlightuserdata(L, u);
206 lua_rawget(L, -2);
207 lua_getmetatable(L, 1);
208 if (lua_rawequal(L, -1, -2)) /* check that we are releasing the correct type */
209 {
210 lua_pushlightuserdata(L, u);
211 lua_pushnil(L);
212 lua_rawset(L, -5);
213 done = 1;
214 }
215 }
216 lua_pushboolean(L, done != 0);
217 return 1;
218 }
219
220 /* Type casting
221 */
222 static int tolua_bnd_cast(lua_State *L)
223 {
224
225 /* // old code
226 void* v = tolua_tousertype(L,1,NULL);
227 const char* s = tolua_tostring(L,2,NULL);
228 if (v && s)
229 tolua_pushusertype(L,v,s);
230 else
231 lua_pushnil(L);
232 return 1;
233 */
234
235 void *v;
236 const char *s;
237 if (lua_islightuserdata(L, 1))
238 {
239 v = tolua_touserdata(L, 1, NULL);
240 }
241 else
242 {
243 v = tolua_tousertype(L, 1, 0);
244 }
245
246 s = tolua_tostring(L, 2, NULL);
247 if (v && s)
248 tolua_pushusertype(L, v, s);
249 else
250 lua_pushnil(L);
251 return 1;
252 }
253
254 /* Inheritance
255 */
256 static int tolua_bnd_inherit(lua_State *L)
257 {
258
259 /* stack: lua object, c object */
260 lua_pushstring(L, ".c_instance");
261 lua_pushvalue(L, -2);
262 lua_rawset(L, -4);
263 /* l_obj[".c_instance"] = c_obj */
264
265 return 0;
266 }
267
268 #ifdef LUA_VERSION_NUM /* lua 5.1 */
269 static int tolua_bnd_setpeer(lua_State *L)
270 {
271
272 /* stack: userdata, table */
273 if (!lua_isuserdata(L, -2))
274 {
275 lua_pushstring(L, "Invalid argument #1 to setpeer: userdata expected.");
276 lua_error(L);
277 }
278
279 if (lua_isnil(L, -1))
280 {
281
282 lua_pop(L, 1);
283 lua_pushvalue(L, TOLUA_NOPEER);
284 }
285 #if LUA_VERSION_NUM > 501
286 lua_setuservalue(L, -2);
287 #else
288 lua_setfenv(L, -2);
289 #endif
290
291 return 0;
292 }
293
294 static int tolua_bnd_getpeer(lua_State *L)
295 {
296
297 /* stack: userdata */
298 #if LUA_VERSION_NUM > 501
299 lua_getuservalue(L, -1);
300 #else
301 lua_getfenv(L, -1);
302 #endif
303 if (lua_rawequal(L, -1, TOLUA_NOPEER))
304 {
305 lua_pop(L, 1);
306 lua_pushnil(L);
307 }
308 return 1;
309 }
310 #endif
311
312 /* static int class_gc_event (lua_State* L); */
313
314 TOLUA_API void tolua_open(lua_State *L)
315 {
316 int top = lua_gettop(L);
317 lua_pushstring(L, "tolua_opened");
318 lua_rawget(L, LUA_REGISTRYINDEX);
319 if (!lua_isboolean(L, -1))
320 {
321 lua_pushstring(L, "tolua_opened");
322 lua_pushboolean(L, 1);
323 lua_rawset(L, LUA_REGISTRYINDEX);
324
325 #ifndef LUA_VERSION_NUM /* only prior to lua 5.1 */
326 /* create peer object table */
327 lua_pushstring(L, "tolua_peers");
328 lua_newtable(L);
329 /* make weak key metatable for peers indexed by userdata object */
330 lua_newtable(L);
331 lua_pushliteral(L, "__mode");
332 lua_pushliteral(L, "k");
333 lua_rawset(L, -3); /* stack: string peers mt */
334 lua_setmetatable(L, -2); /* stack: string peers */
335 lua_rawset(L, LUA_REGISTRYINDEX);
336 #endif
337
338 /* create object ptr -> udata mapping table */
339 lua_pushstring(L, "tolua_ubox");
340 lua_newtable(L);
341 /* make weak value metatable for ubox table to allow userdata to be
342 garbage-collected */
343 lua_newtable(L);
344 lua_pushliteral(L, "__mode");
345 lua_pushliteral(L, "v");
346 lua_rawset(L, -3); /* stack: string ubox mt */
347 lua_setmetatable(L, -2); /* stack: string ubox */
348 lua_rawset(L, LUA_REGISTRYINDEX);
349
350 lua_pushstring(L, "tolua_super");
351 lua_newtable(L);
352 lua_rawset(L, LUA_REGISTRYINDEX);
353 lua_pushstring(L, "tolua_gc");
354 lua_newtable(L);
355 lua_rawset(L, LUA_REGISTRYINDEX);
356
357 /* create gc_event closure */
358 lua_pushstring(L, "tolua_gc_event");
359 lua_pushstring(L, "tolua_gc");
360 lua_rawget(L, LUA_REGISTRYINDEX);
361 lua_pushstring(L, "tolua_super");
362 lua_rawget(L, LUA_REGISTRYINDEX);
363 lua_pushcclosure(L, class_gc_event, 2);
364 lua_rawset(L, LUA_REGISTRYINDEX);
365
366 tolua_newmetatable(L, "tolua_commonclass");
367
368 tolua_module(L, NULL, 0);
369 tolua_beginmodule(L, NULL);
370 tolua_module(L, "tolua", 0);
371 tolua_beginmodule(L, "tolua");
372 tolua_function(L, "type", tolua_bnd_type);
373 tolua_function(L, "takeownership", tolua_bnd_takeownership);
374 tolua_function(L, "releaseownership", tolua_bnd_releaseownership);
375 tolua_function(L, "cast", tolua_bnd_cast);
376 tolua_function(L, "inherit", tolua_bnd_inherit);
377 #ifdef LUA_VERSION_NUM /* lua 5.1 */
378 tolua_function(L, "setpeer", tolua_bnd_setpeer);
379 tolua_function(L, "getpeer", tolua_bnd_getpeer);
380 #endif
381
382 tolua_endmodule(L);
383 tolua_endmodule(L);
384 }
385 lua_settop(L, top);
386 }
387
388 /* Copy a C object
389 */
390 TOLUA_API void *tolua_copy(lua_State *L, void *value, unsigned int size)
391 {
392 void *clone = (void *)malloc(size);
393 if (clone)
394 memcpy(clone, value, size);
395 else
396 tolua_error(L, "insuficient memory", NULL);
397 return clone;
398 }
399
400 /* Default collect function
401 */
402 TOLUA_API int tolua_default_collect(lua_State *tolua_S)
403 {
404 void *self = tolua_tousertype(tolua_S, 1, 0);
405 free(self);
406 return 0;
407 }
408
409 /* Do clone
410 */
411 TOLUA_API int tolua_register_gc(lua_State *L, int lo)
412 {
413 int success = 1;
414 void *value = *(void **)lua_touserdata(L, lo);
415 lua_pushstring(L, "tolua_gc");
416 lua_rawget(L, LUA_REGISTRYINDEX);
417 lua_pushlightuserdata(L, value);
418 lua_rawget(L, -2);
419 if (!lua_isnil(L, -1)) /* make sure that object is not already owned */
420 success = 0;
421 else
422 {
423 lua_pushlightuserdata(L, value);
424 lua_getmetatable(L, lo);
425 lua_rawset(L, -4);
426 }
427 lua_pop(L, 2);
428 return success;
429 }
430
431 /* Register a usertype
432 * It creates the correspoding metatable in the registry, for both 'type' and 'const type'.
433 * It maps 'const type' as being also a 'type'
434 */
435 TOLUA_API void tolua_usertype(lua_State *L, const char *type)
436 {
437 char ctype[128] = "const ";
438 strncat(ctype, type, 120);
439
440 /* create both metatables */
441 if (tolua_newmetatable(L, ctype) && tolua_newmetatable(L, type))
442 mapsuper(L, type, ctype); /* 'type' is also a 'const type' */
443 }
444
445 /* Begin module
446 * It pushes the module (or class) table on the stack
447 */
448 TOLUA_API void tolua_beginmodule(lua_State *L, const char *name)
449 {
450 if (name)
451 {
452 lua_pushstring(L, name);
453 lua_rawget(L, -2);
454 }
455 else
456 #if LUA_VERSION_NUM > 501
457 lua_pushglobaltable(L);
458 #else
459 lua_pushvalue(L, LUA_GLOBALSINDEX);
460 #endif
461 }
462
463 /* End module
464 * It pops the module (or class) from the stack
465 */
466 TOLUA_API void tolua_endmodule(lua_State *L)
467 {
468 lua_pop(L, 1);
469 }
470
471 /* Map module
472 * It creates a new module
473 */
474 #if 1
475 TOLUA_API void tolua_module(lua_State *L, const char *name, int hasvar)
476 {
477 if (name)
478 {
479 /* tolua module */
480 lua_pushstring(L, name);
481 lua_rawget(L, -2);
482 if (!lua_istable(L, -1)) /* check if module already exists */
483 {
484 lua_pop(L, 1);
485 lua_newtable(L);
486 lua_pushstring(L, name);
487 lua_pushvalue(L, -2);
488 lua_rawset(L, -4); /* assing module into module */
489 }
490 }
491 else
492 {
493 /* global table */
494 #if LUA_VERSION_NUM > 501
495 lua_pushglobaltable(L);
496 #else
497 lua_pushvalue(L, LUA_GLOBALSINDEX);
498 #endif
499 }
500 if (hasvar)
501 {
502 if (!tolua_ismodulemetatable(L)) /* check if it already has a module metatable */
503 {
504 /* create metatable to get/set C/C++ variable */
505 lua_newtable(L);
506 tolua_moduleevents(L);
507 if (lua_getmetatable(L, -2))
508 lua_setmetatable(L, -2); /* set old metatable as metatable of metatable */
509 lua_setmetatable(L, -2);
510 }
511 }
512 lua_pop(L, 1); /* pop module */
513 }
514 #else
515 TOLUA_API void tolua_module(lua_State *L, const char *name, int hasvar)
516 {
517 if (name)
518 {
519 /* tolua module */
520 lua_pushstring(L, name);
521 lua_newtable(L);
522 }
523 else
524 {
525 /* global table */
526 #if LUA_VERSION_NUM > 501
527 lua_pushglobaltable(L);
528 #else
529 lua_pushvalue(L, LUA_GLOBALSINDEX);
530 #endif
531 }
532 if (hasvar)
533 {
534 /* create metatable to get/set C/C++ variable */
535 lua_newtable(L);
536 tolua_moduleevents(L);
537 if (lua_getmetatable(L, -2))
538 lua_setmetatable(L, -2); /* set old metatable as metatable of metatable */
539 lua_setmetatable(L, -2);
540 }
541 if (name)
542 lua_rawset(L, -3); /* assing module into module */
543 else
544 lua_pop(L, 1); /* pop global table */
545 }
546 #endif
547
548 static void push_collector(lua_State *L, const char *type, lua_CFunction col)
549 {
550
551 /* push collector function, but only if it's not NULL, or if there's no
552 collector already */
553 if (!col)
554 return;
555 luaL_getmetatable(L, type);
556 lua_pushstring(L, ".collector");
557 /*
558 if (!col) {
559 lua_pushvalue(L, -1);
560 lua_rawget(L, -3);
561 if (!lua_isnil(L, -1)) {
562 lua_pop(L, 3);
563 return;
564 }
565 lua_pop(L, 1);
566 }
567 // */
568 lua_pushcfunction(L, col);
569
570 lua_rawset(L, -3);
571 lua_pop(L, 1);
572 }
573
574 /* Map C class
575 * It maps a C class, setting the appropriate inheritance and super classes.
576 */
577 TOLUA_API void tolua_cclass(lua_State *L, const char *lname, const char *name, const char *base, lua_CFunction col)
578 {
579 char cname[128] = "const ";
580 char cbase[128] = "const ";
581 strncat(cname, name, 120);
582 strncat(cbase, base, 120);
583
584 mapinheritance(L, name, base);
585 mapinheritance(L, cname, name);
586
587 mapsuper(L, cname, cbase);
588 mapsuper(L, name, base);
589
590 lua_pushstring(L, lname);
591
592 push_collector(L, name, col);
593 /*
594 luaL_getmetatable(L,name);
595 lua_pushstring(L,".collector");
596 lua_pushcfunction(L,col);
597
598 lua_rawset(L,-3);
599 */
600
601 luaL_getmetatable(L, name);
602 lua_rawset(L, -3); /* assign class metatable to module */
603
604 /* now we also need to store the collector table for the const
605 instances of the class */
606 push_collector(L, cname, col);
607 /*
608 luaL_getmetatable(L,cname);
609 lua_pushstring(L,".collector");
610 lua_pushcfunction(L,col);
611 lua_rawset(L,-3);
612 lua_pop(L,1);
613 */
614 }
615
616 /* Add base
617 * It adds additional base classes to a class (for multiple inheritance)
618 * (not for now)
619 TOLUA_API void tolua_addbase(lua_State* L, char* name, char* base) {
620
621 char cname[128] = "const ";
622 char cbase[128] = "const ";
623 strncat(cname,name,120);
624 strncat(cbase,base,120);
625
626 mapsuper(L,cname,cbase);
627 mapsuper(L,name,base);
628 }
629 */
630
631 /* Map function
632 * It assigns a function into the current module (or class)
633 */
634 TOLUA_API void tolua_function(lua_State *L, const char *name, lua_CFunction func)
635 {
636 lua_pushstring(L, name);
637 lua_pushcfunction(L, func);
638 lua_rawset(L, -3);
639 }
640
641 /* sets the __call event for the class (expects the class' main table on top) */
642 /* never really worked :(
643 TOLUA_API void tolua_set_call_event(lua_State* L, lua_CFunction func, char* type) {
644
645 lua_getmetatable(L, -1);
646 //luaL_getmetatable(L, type);
647 lua_pushstring(L,"__call");
648 lua_pushcfunction(L,func);
649 lua_rawset(L,-3);
650 lua_pop(L, 1);
651 }
652 */
653
654 /* Map constant number
655 * It assigns a constant number into the current module (or class)
656 */
657 TOLUA_API void tolua_constant(lua_State *L, const char *name, lua_Number value)
658 {
659 lua_pushstring(L, name);
660 tolua_pushnumber(L, value);
661 lua_rawset(L, -3);
662 }
663
664 /* Map variable
665 * It assigns a variable into the current module (or class)
666 */
667 TOLUA_API void tolua_variable(lua_State *L, const char *name, lua_CFunction get, lua_CFunction set)
668 {
669 /* get func */
670 lua_pushstring(L, ".get");
671 lua_rawget(L, -2);
672 if (!lua_istable(L, -1))
673 {
674 /* create .get table, leaving it at the top */
675 lua_pop(L, 1);
676 lua_newtable(L);
677 lua_pushstring(L, ".get");
678 lua_pushvalue(L, -2);
679 lua_rawset(L, -4);
680 }
681 lua_pushstring(L, name);
682 lua_pushcfunction(L, get);
683 lua_rawset(L, -3); /* store variable */
684 lua_pop(L, 1); /* pop .get table */
685
686 /* set func */
687 if (set)
688 {
689 lua_pushstring(L, ".set");
690 lua_rawget(L, -2);
691 if (!lua_istable(L, -1))
692 {
693 /* create .set table, leaving it at the top */
694 lua_pop(L, 1);
695 lua_newtable(L);
696 lua_pushstring(L, ".set");
697 lua_pushvalue(L, -2);
698 lua_rawset(L, -4);
699 }
700 lua_pushstring(L, name);
701 lua_pushcfunction(L, set);
702 lua_rawset(L, -3); /* store variable */
703 lua_pop(L, 1); /* pop .set table */
704 }
705 }
706
707 /* Access const array
708 * It reports an error when trying to write into a const array
709 */
710 static int const_array(lua_State *L)
711 {
712 luaL_error(L, "value of const array cannot be changed");
713 return 0;
714 }
715
716 /* Map an array
717 * It assigns an array into the current module (or class)
718 */
719 TOLUA_API void tolua_array(lua_State *L, const char *name, lua_CFunction get, lua_CFunction set)
720 {
721 lua_pushstring(L, ".get");
722 lua_rawget(L, -2);
723 if (!lua_istable(L, -1))
724 {
725 /* create .get table, leaving it at the top */
726 lua_pop(L, 1);
727 lua_newtable(L);
728 lua_pushstring(L, ".get");
729 lua_pushvalue(L, -2);
730 lua_rawset(L, -4);
731 }
732 lua_pushstring(L, name);
733
734 lua_newtable(L); /* create array metatable */
735 lua_pushvalue(L, -1);
736 lua_setmetatable(L, -2); /* set the own table as metatable (for modules) */
737 lua_pushstring(L, "__index");
738 lua_pushcfunction(L, get);
739 lua_rawset(L, -3);
740 lua_pushstring(L, "__newindex");
741 lua_pushcfunction(L, set ? set : const_array);
742 lua_rawset(L, -3);
743
744 lua_rawset(L, -3); /* store variable */
745 lua_pop(L, 1); /* pop .get table */
746 }
747
748 TOLUA_API void tolua_dobuffer(lua_State *L, char *B, unsigned int size, const char *name)
749 {
750
751 #ifdef LUA_VERSION_NUM /* lua 5.1 */
752 #pragma GCC diagnostic push
753 #pragma GCC diagnostic ignored "-Wunused-value"
754 luaL_loadbuffer(L, B, size, name) || lua_pcall(L, 0, 0, 0);
755 #pragma GCC diagnostic pop
756 #else
757 lua_dobuffer(L, B, size, name);
758 #endif
759 }