Debian Codegraph @

engine/tolua/tolua_map.cpp

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