Debian Codegraph @

engine/tolua/tolua_event.cpp

raw
1 /* tolua: event functions
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 <stdio.h>
15
16 #include "tolua++.h"
17
18 /* Store at ubox
19 * It stores, creating the corresponding table if needed,
20 * the pair key/value in the corresponding ubox table
21 */
22 static void storeatubox (lua_State* L, int lo)
23 {
24 #ifdef LUA_VERSION_NUM
25 lua_getfenv(L, lo);
26 if (lua_rawequal(L, -1, TOLUA_NOPEER)) {
27 lua_pop(L, 1);
28 lua_newtable(L);
29 lua_pushvalue(L, -1);
30 lua_setfenv(L, lo); /* stack: k,v,table */
31 };
32 lua_insert(L, -3);
33 lua_settable(L, -3); /* on lua 5.1, we trade the "tolua_peers" lookup for a settable call */
34 lua_pop(L, 1);
35 #else
36 /* stack: key value (to be stored) */
37 lua_pushstring(L,"tolua_peers");
38 lua_rawget(L,LUA_REGISTRYINDEX); /* stack: k v ubox */
39 lua_pushvalue(L,lo);
40 lua_rawget(L,-2); /* stack: k v ubox ubox[u] */
41 if (!lua_istable(L,-1))
42 {
43 lua_pop(L,1); /* stack: k v ubox */
44 lua_newtable(L); /* stack: k v ubox table */
45 lua_pushvalue(L,1);
46 lua_pushvalue(L,-2); /* stack: k v ubox table u table */
47 lua_rawset(L,-4); /* stack: k v ubox ubox[u]=table */
48 }
49 lua_insert(L,-4); /* put table before k */
50 lua_pop(L,1); /* pop ubox */
51 lua_rawset(L,-3); /* store at table */
52 lua_pop(L,1); /* pop ubox[u] */
53 #endif
54 }
55
56 /* Module index function
57 */
58 static int module_index_event (lua_State* L)
59 {
60 lua_pushstring(L,".get");
61 lua_rawget(L,-3);
62 if (lua_istable(L,-1))
63 {
64 lua_pushvalue(L,2); /* key */
65 lua_rawget(L,-2);
66 if (lua_iscfunction(L,-1))
67 {
68 lua_call(L,0,1);
69 return 1;
70 }
71 else if (lua_istable(L,-1))
72 return 1;
73 }
74 /* call old index meta event */
75 if (lua_getmetatable(L,1))
76 {
77 lua_pushstring(L,"__index");
78 lua_rawget(L,-2);
79 lua_pushvalue(L,1);
80 lua_pushvalue(L,2);
81 if (lua_isfunction(L,-1))
82 {
83 lua_call(L,2,1);
84 return 1;
85 }
86 else if (lua_istable(L,-1))
87 {
88 lua_gettable(L,-3);
89 return 1;
90 }
91 }
92 lua_pushnil(L);
93 return 1;
94 }
95
96 /* Module newindex function
97 */
98 static int module_newindex_event (lua_State* L)
99 {
100 lua_pushstring(L,".set");
101 lua_rawget(L,-4);
102 if (lua_istable(L,-1))
103 {
104 lua_pushvalue(L,2); /* key */
105 lua_rawget(L,-2);
106 if (lua_iscfunction(L,-1))
107 {
108 lua_pushvalue(L,1); /* only to be compatible with non-static vars */
109 lua_pushvalue(L,3); /* value */
110 lua_call(L,2,0);
111 return 0;
112 }
113 }
114 /* call old newindex meta event */
115 if (lua_getmetatable(L,1) && lua_getmetatable(L,-1))
116 {
117 lua_pushstring(L,"__newindex");
118 lua_rawget(L,-2);
119 if (lua_isfunction(L,-1))
120 {
121 lua_pushvalue(L,1);
122 lua_pushvalue(L,2);
123 lua_pushvalue(L,3);
124 lua_call(L,3,0);
125 }
126 }
127 lua_settop(L,3);
128 lua_rawset(L,-3);
129 return 0;
130 }
131
132 /* Class index function
133 * If the object is a userdata (ie, an object), it searches the field in
134 * the alternative table stored in the corresponding "ubox" table.
135 */
136 static int class_index_event (lua_State* L)
137 {
138 int t = lua_type(L,1);
139 if (t == LUA_TUSERDATA)
140 {
141 /* Access alternative table */
142 #ifdef LUA_VERSION_NUM /* new macro on version 5.1 */
143 lua_getfenv(L,1);
144 if (!lua_rawequal(L, -1, TOLUA_NOPEER)) {
145 lua_pushvalue(L, 2); /* key */
146 lua_gettable(L, -2); /* on lua 5.1, we trade the "tolua_peers" lookup for a gettable call */
147 if (!lua_isnil(L, -1))
148 return 1;
149 };
150 #else
151 lua_pushstring(L,"tolua_peers");
152 lua_rawget(L,LUA_REGISTRYINDEX); /* stack: obj key ubox */
153 lua_pushvalue(L,1);
154 lua_rawget(L,-2); /* stack: obj key ubox ubox[u] */
155 if (lua_istable(L,-1))
156 {
157 lua_pushvalue(L,2); /* key */
158 lua_rawget(L,-2); /* stack: obj key ubox ubox[u] value */
159 if (!lua_isnil(L,-1))
160 return 1;
161 }
162 #endif
163 lua_settop(L,2); /* stack: obj key */
164 /* Try metatables */
165 lua_pushvalue(L,1); /* stack: obj key obj */
166 while (lua_getmetatable(L,-1))
167 { /* stack: obj key obj mt */
168 lua_remove(L,-2); /* stack: obj key mt */
169 if (lua_isnumber(L,2)) /* check if key is a numeric value */
170 {
171 /* try operator[] */
172 lua_pushstring(L,".geti");
173 lua_rawget(L,-2); /* stack: obj key mt func */
174 if (lua_isfunction(L,-1))
175 {
176 lua_pushvalue(L,1);
177 lua_pushvalue(L,2);
178 lua_call(L,2,1);
179 return 1;
180 }
181 }
182 else
183 {
184 lua_pushvalue(L,2); /* stack: obj key mt key */
185 lua_rawget(L,-2); /* stack: obj key mt value */
186 if (!lua_isnil(L,-1))
187 return 1;
188 else
189 lua_pop(L,1);
190 /* try C/C++ variable */
191 lua_pushstring(L,".get");
192 lua_rawget(L,-2); /* stack: obj key mt tget */
193 if (lua_istable(L,-1))
194 {
195 lua_pushvalue(L,2);
196 lua_rawget(L,-2); /* stack: obj key mt value */
197 if (lua_iscfunction(L,-1))
198 {
199 lua_pushvalue(L,1);
200 lua_pushvalue(L,2);
201 lua_call(L,2,1);
202 return 1;
203 }
204 else if (lua_istable(L,-1))
205 {
206 /* deal with array: create table to be returned and cache it in ubox */
207 void* u = *((void**)lua_touserdata(L,1));
208 lua_newtable(L); /* stack: obj key mt value table */
209 lua_pushstring(L,".self");
210 lua_pushlightuserdata(L,u);
211 lua_rawset(L,-3); /* store usertype in ".self" */
212 lua_insert(L,-2); /* stack: obj key mt table value */
213 lua_setmetatable(L,-2); /* set stored value as metatable */
214 lua_pushvalue(L,-1); /* stack: obj key met table table */
215 lua_pushvalue(L,2); /* stack: obj key mt table table key */
216 lua_insert(L,-2); /* stack: obj key mt table key table */
217 storeatubox(L,1); /* stack: obj key mt table */
218 return 1;
219 }
220 }
221 }
222 lua_settop(L,3);
223 }
224 lua_pushnil(L);
225 return 1;
226 }
227 else if (t== LUA_TTABLE)
228 {
229 module_index_event(L);
230 return 1;
231 }
232 lua_pushnil(L);
233 return 1;
234 }
235
236 /* Newindex function
237 * It first searches for a C/C++ varaible to be set.
238 * Then, it either stores it in the alternative ubox table (in the case it is
239 * an object) or in the own table (that represents the class or module).
240 */
241 static int class_newindex_event (lua_State* L)
242 {
243 int t = lua_type(L,1);
244 if (t == LUA_TUSERDATA)
245 {
246 /* Try accessing a C/C++ variable to be set */
247 lua_getmetatable(L,1);
248 while (lua_istable(L,-1)) /* stack: t k v mt */
249 {
250 if (lua_isnumber(L,2)) /* check if key is a numeric value */
251 {
252 /* try operator[] */
253 lua_pushstring(L,".seti");
254 lua_rawget(L,-2); /* stack: obj key mt func */
255 if (lua_isfunction(L,-1))
256 {
257 lua_pushvalue(L,1);
258 lua_pushvalue(L,2);
259 lua_pushvalue(L,3);
260 lua_call(L,3,0);
261 return 0;
262 }
263 }
264 else
265 {
266 lua_pushstring(L,".set");
267 lua_rawget(L,-2); /* stack: t k v mt tset */
268 if (lua_istable(L,-1))
269 {
270 lua_pushvalue(L,2);
271 lua_rawget(L,-2); /* stack: t k v mt tset func */
272 if (lua_iscfunction(L,-1))
273 {
274 lua_pushvalue(L,1);
275 lua_pushvalue(L,3);
276 lua_call(L,2,0);
277 return 0;
278 }
279 lua_pop(L,1); /* stack: t k v mt tset */
280 }
281 lua_pop(L,1); /* stack: t k v mt */
282 if (!lua_getmetatable(L,-1)) /* stack: t k v mt mt */
283 lua_pushnil(L);
284 lua_remove(L,-2); /* stack: t k v mt */
285 }
286 }
287 lua_settop(L,3); /* stack: t k v */
288
289 /* then, store as a new field */
290 storeatubox(L,1);
291 }
292 else if (t== LUA_TTABLE)
293 {
294 module_newindex_event(L);
295 }
296 return 0;
297 }
298
299 static int class_call_event(lua_State* L) {
300
301 if (lua_istable(L, 1)) {
302 lua_pushstring(L, ".call");
303 lua_rawget(L, 1);
304 if (lua_isfunction(L, -1)) {
305
306 lua_insert(L, 1);
307 lua_call(L, lua_gettop(L)-1, 1);
308
309 return 1;
310 };
311 };
312 tolua_error(L,"Attempt to call a non-callable object.",NULL);
313 return 0;
314 };
315
316 static int do_operator (lua_State* L, const char* op)
317 {
318 if (lua_isuserdata(L,1))
319 {
320 /* Try metatables */
321 lua_pushvalue(L,1); /* stack: op1 op2 */
322 while (lua_getmetatable(L,-1))
323 { /* stack: op1 op2 op1 mt */
324 lua_remove(L,-2); /* stack: op1 op2 mt */
325 lua_pushstring(L,op); /* stack: op1 op2 mt key */
326 lua_rawget(L,-2); /* stack: obj key mt func */
327 if (lua_isfunction(L,-1))
328 {
329 lua_pushvalue(L,1);
330 lua_pushvalue(L,2);
331 lua_call(L,2,1);
332 return 1;
333 }
334 lua_settop(L,3);
335 }
336 }
337 tolua_error(L,"Attempt to perform operation on an invalid operand",NULL);
338 return 0;
339 }
340
341 static int class_add_event (lua_State* L)
342 {
343 return do_operator(L,".add");
344 }
345
346 static int class_sub_event (lua_State* L)
347 {
348 return do_operator(L,".sub");
349 }
350
351 static int class_mul_event (lua_State* L)
352 {
353 return do_operator(L,".mul");
354 }
355
356 static int class_div_event (lua_State* L)
357 {
358 return do_operator(L,".div");
359 }
360
361 static int class_lt_event (lua_State* L)
362 {
363 return do_operator(L,".lt");
364 }
365
366 static int class_le_event (lua_State* L)
367 {
368 return do_operator(L,".le");
369 }
370
371 static int class_eq_event (lua_State* L)
372 {
373 /* copying code from do_operator here to return false when no operator is found */
374 if (lua_isuserdata(L,1))
375 {
376 /* Try metatables */
377 lua_pushvalue(L,1); /* stack: op1 op2 */
378 while (lua_getmetatable(L,-1))
379 { /* stack: op1 op2 op1 mt */
380 lua_remove(L,-2); /* stack: op1 op2 mt */
381 lua_pushstring(L,".eq"); /* stack: op1 op2 mt key */
382 lua_rawget(L,-2); /* stack: obj key mt func */
383 if (lua_isfunction(L,-1))
384 {
385 lua_pushvalue(L,1);
386 lua_pushvalue(L,2);
387 lua_call(L,2,1);
388 return 1;
389 }
390 lua_settop(L,3);
391 }
392 }
393
394 lua_settop(L, 3);
395 lua_pushboolean(L, 0);
396 return 1;
397 }
398
399 /*
400 static int class_gc_event (lua_State* L)
401 {
402 void* u = *((void**)lua_touserdata(L,1));
403 fprintf(stderr, "collecting: looking at %p\n", u);
404 lua_pushstring(L,"tolua_gc");
405 lua_rawget(L,LUA_REGISTRYINDEX);
406 lua_pushlightuserdata(L,u);
407 lua_rawget(L,-2);
408 if (lua_isfunction(L,-1))
409 {
410 lua_pushvalue(L,1);
411 lua_call(L,1,0);
412 lua_pushlightuserdata(L,u);
413 lua_pushnil(L);
414 lua_rawset(L,-3);
415 }
416 lua_pop(L,2);
417 return 0;
418 }
419 */
420 TOLUA_API int class_gc_event (lua_State* L)
421 {
422 void* u = *((void**)lua_touserdata(L,1));
423 int top;
424 /*fprintf(stderr, "collecting: looking at %p\n", u);*/
425 /*
426 lua_pushstring(L,"tolua_gc");
427 lua_rawget(L,LUA_REGISTRYINDEX);
428 */
429 lua_pushvalue(L, lua_upvalueindex(1));
430 lua_pushlightuserdata(L,u);
431 lua_rawget(L,-2); /* stack: gc umt */
432 lua_getmetatable(L,1); /* stack: gc umt mt */
433 /*fprintf(stderr, "checking type\n");*/
434 top = lua_gettop(L);
435 if (tolua_fast_isa(L,top,top-1, lua_upvalueindex(2))) /* make sure we collect correct type */
436 {
437 /*fprintf(stderr, "Found type!\n");*/
438 /* get gc function */
439 lua_pushliteral(L,".collector");
440 lua_rawget(L,-2); /* stack: gc umt mt collector */
441 if (lua_isfunction(L,-1)) {
442 /*fprintf(stderr, "Found .collector!\n");*/
443 }
444 else {
445 lua_pop(L,1);
446 /*fprintf(stderr, "Using default cleanup\n");*/
447 lua_pushcfunction(L,tolua_default_collect);
448 }
449
450 lua_pushvalue(L,1); /* stack: gc umt mt collector u */
451 lua_call(L,1,0);
452
453 lua_pushlightuserdata(L,u); /* stack: gc umt mt u */
454 lua_pushnil(L); /* stack: gc umt mt u nil */
455 lua_rawset(L,-5); /* stack: gc umt mt */
456 }
457 lua_pop(L,3);
458 return 0;
459 }
460
461
462 /* Register module events
463 * It expects the metatable on the top of the stack
464 */
465 TOLUA_API void tolua_moduleevents (lua_State* L)
466 {
467 lua_pushstring(L,"__index");
468 lua_pushcfunction(L,module_index_event);
469 lua_rawset(L,-3);
470 lua_pushstring(L,"__newindex");
471 lua_pushcfunction(L,module_newindex_event);
472 lua_rawset(L,-3);
473 }
474
475 /* Check if the object on the top has a module metatable
476 */
477 TOLUA_API int tolua_ismodulemetatable (lua_State* L)
478 {
479 int r = 0;
480 if (lua_getmetatable(L,-1))
481 {
482 lua_pushstring(L,"__index");
483 lua_rawget(L,-2);
484 r = (lua_tocfunction(L,-1) == module_index_event);
485 lua_pop(L,2);
486 }
487 return r;
488 }
489
490 /* Register class events
491 * It expects the metatable on the top of the stack
492 */
493 TOLUA_API void tolua_classevents (lua_State* L)
494 {
495 lua_pushstring(L,"__index");
496 lua_pushcfunction(L,class_index_event);
497 lua_rawset(L,-3);
498 lua_pushstring(L,"__newindex");
499 lua_pushcfunction(L,class_newindex_event);
500 lua_rawset(L,-3);
501
502 lua_pushstring(L,"__add");
503 lua_pushcfunction(L,class_add_event);
504 lua_rawset(L,-3);
505 lua_pushstring(L,"__sub");
506 lua_pushcfunction(L,class_sub_event);
507 lua_rawset(L,-3);
508 lua_pushstring(L,"__mul");
509 lua_pushcfunction(L,class_mul_event);
510 lua_rawset(L,-3);
511 lua_pushstring(L,"__div");
512 lua_pushcfunction(L,class_div_event);
513 lua_rawset(L,-3);
514
515 lua_pushstring(L,"__lt");
516 lua_pushcfunction(L,class_lt_event);
517 lua_rawset(L,-3);
518 lua_pushstring(L,"__le");
519 lua_pushcfunction(L,class_le_event);
520 lua_rawset(L,-3);
521 lua_pushstring(L,"__eq");
522 lua_pushcfunction(L,class_eq_event);
523 lua_rawset(L,-3);
524
525 lua_pushstring(L,"__call");
526 lua_pushcfunction(L,class_call_event);
527 lua_rawset(L,-3);
528
529 lua_pushstring(L,"__gc");
530 lua_pushstring(L, "tolua_gc_event");
531 lua_rawget(L, LUA_REGISTRYINDEX);
532 /*lua_pushcfunction(L,class_gc_event);*/
533 lua_rawset(L,-3);
534 }
535