1 /* tolua: functions to push C values.
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 TOLUA_API void tolua_pushvalue (lua_State* L, int lo)
25 TOLUA_API void tolua_pushboolean (lua_State* L, int value)
27 lua_pushboolean(L,value);
30 TOLUA_API void tolua_pushnumber (lua_State* L, lua_Number value)
32 lua_pushnumber(L,value);
35 TOLUA_API void tolua_pushstring (lua_State* L, const char* value)
40 lua_pushstring(L,value);
43 TOLUA_API void tolua_pushuserdata (lua_State* L, void* value)
48 lua_pushlightuserdata(L,value);
51 TOLUA_API void tolua_pushusertype (lua_State* L, void* value, const char* type)
57 luaL_getmetatable(L, type);
58 lua_pushstring(L,"tolua_ubox");
59 lua_rawget(L,-2); /* stack: mt ubox */
60 if (lua_isnil(L, -1)) {
62 lua_pushstring(L, "tolua_ubox");
63 lua_rawget(L, LUA_REGISTRYINDEX);
65 lua_pushlightuserdata(L,value);
66 lua_rawget(L,-2); /* stack: mt ubox ubox[u] */
69 lua_pop(L,1); /* stack: mt ubox */
70 lua_pushlightuserdata(L,value);
71 *(void**)lua_newuserdata(L,sizeof(void *)) = value; /* stack: mt ubox u newud */
72 lua_pushvalue(L,-1); /* stack: mt ubox u newud newud */
73 lua_insert(L,-4); /* stack: mt newud ubox u newud */
74 lua_rawset(L,-3); /* stack: mt newud ubox */
75 lua_pop(L,1); /* stack: mt newud */
76 /*luaL_getmetatable(L,type);*/
77 lua_pushvalue(L, -2); /* stack: mt newud mt */
78 lua_setmetatable(L,-2); /* stack: mt newud */
80 #ifdef LUA_VERSION_NUM
81 lua_pushvalue(L, TOLUA_NOPEER);
87 /* check the need of updating the metatable to a more specialized class */
88 lua_insert(L,-2); /* stack: mt ubox[u] ubox */
89 lua_pop(L,1); /* stack: mt ubox[u] */
90 lua_pushstring(L,"tolua_super");
91 lua_rawget(L,LUA_REGISTRYINDEX); /* stack: mt ubox[u] super */
92 lua_getmetatable(L,-2); /* stack: mt ubox[u] super mt */
93 lua_rawget(L,-2); /* stack: mt ubox[u] super super[mt] */
94 if (lua_istable(L,-1))
96 lua_pushstring(L,type); /* stack: mt ubox[u] super super[mt] type */
97 lua_rawget(L,-2); /* stack: mt ubox[u] super super[mt] flag */
98 if (lua_toboolean(L,-1) == 1) /* if true */
100 lua_pop(L,3); /* mt ubox[u]*/
105 /* type represents a more specilized type */
106 /*luaL_getmetatable(L,type); // stack: mt ubox[u] super super[mt] flag mt */
107 lua_pushvalue(L, -5); /* stack: mt ubox[u] super super[mt] flag mt */
108 lua_setmetatable(L,-5); /* stack: mt ubox[u] super super[mt] flag */
109 lua_pop(L,3); /* stack: mt ubox[u] */
111 lua_remove(L, -2); /* stack: ubox[u]*/
115 TOLUA_API void tolua_pushusertype_and_takeownership (lua_State* L, void* value, const char* type)
117 tolua_pushusertype(L,value,type);
118 tolua_register_gc(L,lua_gettop(L));
121 TOLUA_API void tolua_pushfieldvalue (lua_State* L, int lo, int index, int v)
123 lua_pushnumber(L,index);
128 TOLUA_API void tolua_pushfieldboolean (lua_State* L, int lo, int index, int v)
130 lua_pushnumber(L,index);
131 lua_pushboolean(L,v);
136 TOLUA_API void tolua_pushfieldnumber (lua_State* L, int lo, int index, lua_Number v)
138 lua_pushnumber(L,index);
139 tolua_pushnumber(L,v);
143 TOLUA_API void tolua_pushfieldstring (lua_State* L, int lo, int index, const char* v)
145 lua_pushnumber(L,index);
146 tolua_pushstring(L,v);
150 TOLUA_API void tolua_pushfielduserdata (lua_State* L, int lo, int index, void* v)
152 lua_pushnumber(L,index);
153 tolua_pushuserdata(L,v);
157 TOLUA_API void tolua_pushfieldusertype (lua_State* L, int lo, int index, void* v, const char* type)
159 lua_pushnumber(L,index);
160 tolua_pushusertype(L,v,type);
164 TOLUA_API void tolua_pushfieldusertype_and_takeownership (lua_State* L, int lo, int index, void* v, const char* type)
166 lua_pushnumber(L,index);
167 tolua_pushusertype(L,v,type);
168 tolua_register_gc(L,lua_gettop(L));