1 /* tolua: event functions
2 ** Support code for Lua bindings.
3 ** Written by Waldemar Celes
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.
20 * It stores, creating the corresponding table if needed,
21 * the pair key/value in the corresponding ubox table
23 static void storeatubox (lua_State* L, int lo)
25 #ifdef LUA_VERSION_NUM
27 if (lua_rawequal(L, -1, TOLUA_NOPEER)) {
31 lua_setfenv(L, lo); /* stack: k,v,table */
34 lua_settable(L, -3); /* on lua 5.1, we trade the "tolua_peers" lookup for a settable call */
37 /* stack: key value (to be stored) */
38 lua_pushstring(L,"tolua_peers");
39 lua_rawget(L,LUA_REGISTRYINDEX); /* stack: k v ubox */
41 lua_rawget(L,-2); /* stack: k v ubox ubox[u] */
42 if (!lua_istable(L,-1))
44 lua_pop(L,1); /* stack: k v ubox */
45 lua_newtable(L); /* stack: k v ubox table */
47 lua_pushvalue(L,-2); /* stack: k v ubox table u table */
48 lua_rawset(L,-4); /* stack: k v ubox ubox[u]=table */
50 lua_insert(L,-4); /* put table before k */
51 lua_pop(L,1); /* pop ubox */
52 lua_rawset(L,-3); /* store at table */
53 lua_pop(L,1); /* pop ubox[u] */
57 /* Module index function
59 static int module_index_event (lua_State* L)
61 lua_pushstring(L,".get");
63 if (lua_istable(L,-1))
65 lua_pushvalue(L,2); /* key */
67 if (lua_iscfunction(L,-1))
72 else if (lua_istable(L,-1))
75 /* call old index meta event */
76 if (lua_getmetatable(L,1))
78 lua_pushstring(L,"__index");
82 if (lua_isfunction(L,-1))
87 else if (lua_istable(L,-1))
97 /* Module newindex function
99 static int module_newindex_event (lua_State* L)
101 lua_pushstring(L,".set");
103 if (lua_istable(L,-1))
105 lua_pushvalue(L,2); /* key */
107 if (lua_iscfunction(L,-1))
109 lua_pushvalue(L,1); /* only to be compatible with non-static vars */
110 lua_pushvalue(L,3); /* value */
115 /* call old newindex meta event */
116 if (lua_getmetatable(L,1) && lua_getmetatable(L,-1))
118 lua_pushstring(L,"__newindex");
120 if (lua_isfunction(L,-1))
133 /* Class index function
134 * If the object is a userdata (ie, an object), it searches the field in
135 * the alternative table stored in the corresponding "ubox" table.
137 static int class_index_event (lua_State* L)
139 int t = lua_type(L,1);
140 if (t == LUA_TUSERDATA)
142 /* Access alternative table */
143 #ifdef LUA_VERSION_NUM /* new macro on version 5.1 */
145 if (!lua_rawequal(L, -1, TOLUA_NOPEER)) {
146 lua_pushvalue(L, 2); /* key */
147 lua_gettable(L, -2); /* on lua 5.1, we trade the "tolua_peers" lookup for a gettable call */
148 if (!lua_isnil(L, -1))
152 lua_pushstring(L,"tolua_peers");
153 lua_rawget(L,LUA_REGISTRYINDEX); /* stack: obj key ubox */
155 lua_rawget(L,-2); /* stack: obj key ubox ubox[u] */
156 if (lua_istable(L,-1))
158 lua_pushvalue(L,2); /* key */
159 lua_rawget(L,-2); /* stack: obj key ubox ubox[u] value */
160 if (!lua_isnil(L,-1))
164 lua_settop(L,2); /* stack: obj key */
166 lua_pushvalue(L,1); /* stack: obj key obj */
167 while (lua_getmetatable(L,-1))
168 { /* stack: obj key obj mt */
169 lua_remove(L,-2); /* stack: obj key mt */
170 if (lua_isnumber(L,2)) /* check if key is a numeric value */
173 lua_pushstring(L,".geti");
174 lua_rawget(L,-2); /* stack: obj key mt func */
175 if (lua_isfunction(L,-1))
185 lua_pushvalue(L,2); /* stack: obj key mt key */
186 lua_rawget(L,-2); /* stack: obj key mt value */
187 if (!lua_isnil(L,-1))
191 /* try C/C++ variable */
192 lua_pushstring(L,".get");
193 lua_rawget(L,-2); /* stack: obj key mt tget */
194 if (lua_istable(L,-1))
197 lua_rawget(L,-2); /* stack: obj key mt value */
198 if (lua_iscfunction(L,-1))
205 else if (lua_istable(L,-1))
207 /* deal with array: create table to be returned and cache it in ubox */
208 void* u = *((void**)lua_touserdata(L,1));
209 lua_newtable(L); /* stack: obj key mt value table */
210 lua_pushstring(L,".self");
211 lua_pushlightuserdata(L,u);
212 lua_rawset(L,-3); /* store usertype in ".self" */
213 lua_insert(L,-2); /* stack: obj key mt table value */
214 lua_setmetatable(L,-2); /* set stored value as metatable */
215 lua_pushvalue(L,-1); /* stack: obj key met table table */
216 lua_pushvalue(L,2); /* stack: obj key mt table table key */
217 lua_insert(L,-2); /* stack: obj key mt table key table */
218 storeatubox(L,1); /* stack: obj key mt table */
228 else if (t== LUA_TTABLE)
230 module_index_event(L);
238 * It first searches for a C/C++ varaible to be set.
239 * Then, it either stores it in the alternative ubox table (in the case it is
240 * an object) or in the own table (that represents the class or module).
242 static int class_newindex_event (lua_State* L)
244 int t = lua_type(L,1);
245 if (t == LUA_TUSERDATA)
247 /* Try accessing a C/C++ variable to be set */
248 lua_getmetatable(L,1);
249 while (lua_istable(L,-1)) /* stack: t k v mt */
251 if (lua_isnumber(L,2)) /* check if key is a numeric value */
254 lua_pushstring(L,".seti");
255 lua_rawget(L,-2); /* stack: obj key mt func */
256 if (lua_isfunction(L,-1))
267 lua_pushstring(L,".set");
268 lua_rawget(L,-2); /* stack: t k v mt tset */
269 if (lua_istable(L,-1))
272 lua_rawget(L,-2); /* stack: t k v mt tset func */
273 if (lua_iscfunction(L,-1))
280 lua_pop(L,1); /* stack: t k v mt tset */
282 lua_pop(L,1); /* stack: t k v mt */
283 if (!lua_getmetatable(L,-1)) /* stack: t k v mt mt */
285 lua_remove(L,-2); /* stack: t k v mt */
288 lua_settop(L,3); /* stack: t k v */
290 /* then, store as a new field */
293 else if (t== LUA_TTABLE)
295 module_newindex_event(L);
300 static int class_call_event(lua_State* L) {
302 if (lua_istable(L, 1)) {
303 lua_pushstring(L, ".call");
305 if (lua_isfunction(L, -1)) {
308 lua_call(L, lua_gettop(L)-1, 1);
313 tolua_error(L,"Attempt to call a non-callable object.",NULL);
317 static int do_operator (lua_State* L, const char* op)
319 if (lua_isuserdata(L,1))
322 lua_pushvalue(L,1); /* stack: op1 op2 */
323 while (lua_getmetatable(L,-1))
324 { /* stack: op1 op2 op1 mt */
325 lua_remove(L,-2); /* stack: op1 op2 mt */
326 lua_pushstring(L,op); /* stack: op1 op2 mt key */
327 lua_rawget(L,-2); /* stack: obj key mt func */
328 if (lua_isfunction(L,-1))
338 tolua_error(L,"Attempt to perform operation on an invalid operand",NULL);
342 static int class_add_event (lua_State* L)
344 return do_operator(L,".add");
347 static int class_sub_event (lua_State* L)
349 return do_operator(L,".sub");
352 static int class_mul_event (lua_State* L)
354 return do_operator(L,".mul");
357 static int class_div_event (lua_State* L)
359 return do_operator(L,".div");
362 static int class_lt_event (lua_State* L)
364 return do_operator(L,".lt");
367 static int class_le_event (lua_State* L)
369 return do_operator(L,".le");
372 static int class_eq_event (lua_State* L)
374 /* copying code from do_operator here to return false when no operator is found */
375 if (lua_isuserdata(L,1))
378 lua_pushvalue(L,1); /* stack: op1 op2 */
379 while (lua_getmetatable(L,-1))
380 { /* stack: op1 op2 op1 mt */
381 lua_remove(L,-2); /* stack: op1 op2 mt */
382 lua_pushstring(L,".eq"); /* stack: op1 op2 mt key */
383 lua_rawget(L,-2); /* stack: obj key mt func */
384 if (lua_isfunction(L,-1))
396 lua_pushboolean(L, 0);
401 static int class_gc_event (lua_State* L)
403 void* u = *((void**)lua_touserdata(L,1));
404 fprintf(stderr, "collecting: looking at %p\n", u);
405 lua_pushstring(L,"tolua_gc");
406 lua_rawget(L,LUA_REGISTRYINDEX);
407 lua_pushlightuserdata(L,u);
409 if (lua_isfunction(L,-1))
413 lua_pushlightuserdata(L,u);
421 TOLUA_API int class_gc_event (lua_State* L)
423 void* u = *((void**)lua_touserdata(L,1));
425 /*fprintf(stderr, "collecting: looking at %p\n", u);*/
427 lua_pushstring(L,"tolua_gc");
428 lua_rawget(L,LUA_REGISTRYINDEX);
430 lua_pushvalue(L, lua_upvalueindex(1));
431 lua_pushlightuserdata(L,u);
432 lua_rawget(L,-2); /* stack: gc umt */
433 lua_getmetatable(L,1); /* stack: gc umt mt */
434 /*fprintf(stderr, "checking type\n");*/
436 if (tolua_fast_isa(L,top,top-1, lua_upvalueindex(2))) /* make sure we collect correct type */
438 /*fprintf(stderr, "Found type!\n");*/
439 /* get gc function */
440 lua_pushliteral(L,".collector");
441 lua_rawget(L,-2); /* stack: gc umt mt collector */
442 if (lua_isfunction(L,-1)) {
443 /*fprintf(stderr, "Found .collector!\n");*/
447 /*fprintf(stderr, "Using default cleanup\n");*/
448 lua_pushcfunction(L,tolua_default_collect);
451 lua_pushvalue(L,1); /* stack: gc umt mt collector u */
454 lua_pushlightuserdata(L,u); /* stack: gc umt mt u */
455 lua_pushnil(L); /* stack: gc umt mt u nil */
456 lua_rawset(L,-5); /* stack: gc umt mt */
463 /* Register module events
464 * It expects the metatable on the top of the stack
466 TOLUA_API void tolua_moduleevents (lua_State* L)
468 lua_pushstring(L,"__index");
469 lua_pushcfunction(L,module_index_event);
471 lua_pushstring(L,"__newindex");
472 lua_pushcfunction(L,module_newindex_event);
476 /* Check if the object on the top has a module metatable
478 TOLUA_API int tolua_ismodulemetatable (lua_State* L)
481 if (lua_getmetatable(L,-1))
483 lua_pushstring(L,"__index");
485 r = (lua_tocfunction(L,-1) == module_index_event);
491 /* Register class events
492 * It expects the metatable on the top of the stack
494 TOLUA_API void tolua_classevents (lua_State* L)
496 lua_pushstring(L,"__index");
497 lua_pushcfunction(L,class_index_event);
499 lua_pushstring(L,"__newindex");
500 lua_pushcfunction(L,class_newindex_event);
503 lua_pushstring(L,"__add");
504 lua_pushcfunction(L,class_add_event);
506 lua_pushstring(L,"__sub");
507 lua_pushcfunction(L,class_sub_event);
509 lua_pushstring(L,"__mul");
510 lua_pushcfunction(L,class_mul_event);
512 lua_pushstring(L,"__div");
513 lua_pushcfunction(L,class_div_event);
516 lua_pushstring(L,"__lt");
517 lua_pushcfunction(L,class_lt_event);
519 lua_pushstring(L,"__le");
520 lua_pushcfunction(L,class_le_event);
522 lua_pushstring(L,"__eq");
523 lua_pushcfunction(L,class_eq_event);
526 lua_pushstring(L,"__call");
527 lua_pushcfunction(L,class_call_event);
530 lua_pushstring(L,"__gc");
531 lua_pushstring(L, "tolua_gc_event");
532 lua_rawget(L, LUA_REGISTRYINDEX);
533 /*lua_pushcfunction(L,class_gc_event);*/