Debian Codegraph @

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

raw
1 /* tolua: functions to push C values.
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 "lauxlib.h"
17
18 #include <stdlib.h>
19
20 TOLUA_API void tolua_pushvalue (lua_State* L, int lo)
21 {
22 lua_pushvalue(L,lo);
23 }
24
25 TOLUA_API void tolua_pushboolean (lua_State* L, int value)
26 {
27 lua_pushboolean(L,value);
28 }
29
30 TOLUA_API void tolua_pushnumber (lua_State* L, lua_Number value)
31 {
32 lua_pushnumber(L,value);
33 }
34
35 TOLUA_API void tolua_pushstring (lua_State* L, const char* value)
36 {
37 if (value == NULL)
38 lua_pushnil(L);
39 else
40 lua_pushstring(L,value);
41 }
42
43 TOLUA_API void tolua_pushuserdata (lua_State* L, void* value)
44 {
45 if (value == NULL)
46 lua_pushnil(L);
47 else
48 lua_pushlightuserdata(L,value);
49 }
50
51 TOLUA_API void tolua_pushusertype (lua_State* L, void* value, const char* type)
52 {
53 if (value == NULL)
54 lua_pushnil(L);
55 else
56 {
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)) {
61 lua_pop(L, 1);
62 lua_pushstring(L, "tolua_ubox");
63 lua_rawget(L, LUA_REGISTRYINDEX);
64 };
65 lua_pushlightuserdata(L,value);
66 lua_rawget(L,-2); /* stack: mt ubox ubox[u] */
67 if (lua_isnil(L,-1))
68 {
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 */
79
80 #ifdef LUA_VERSION_NUM
81 lua_pushvalue(L, TOLUA_NOPEER);
82 #if LUA_VERSION_NUM > 501
83 lua_setuservalue(L, -2);
84 #else
85 lua_setfenv(L, -2);
86 #endif
87 #endif
88 }
89 else
90 {
91 /* check the need of updating the metatable to a more specialized class */
92 lua_insert(L,-2); /* stack: mt ubox[u] ubox */
93 lua_pop(L,1); /* stack: mt ubox[u] */
94 lua_pushstring(L,"tolua_super");
95 lua_rawget(L,LUA_REGISTRYINDEX); /* stack: mt ubox[u] super */
96 lua_getmetatable(L,-2); /* stack: mt ubox[u] super mt */
97 lua_rawget(L,-2); /* stack: mt ubox[u] super super[mt] */
98 if (lua_istable(L,-1))
99 {
100 lua_pushstring(L,type); /* stack: mt ubox[u] super super[mt] type */
101 lua_rawget(L,-2); /* stack: mt ubox[u] super super[mt] flag */
102 if (lua_toboolean(L,-1) == 1) /* if true */
103 {
104 lua_pop(L,3); /* mt ubox[u]*/
105 lua_remove(L, -2);
106 return;
107 }
108 }
109 /* type represents a more specilized type */
110 /*luaL_getmetatable(L,type); // stack: mt ubox[u] super super[mt] flag mt */
111 lua_pushvalue(L, -5); /* stack: mt ubox[u] super super[mt] flag mt */
112 lua_setmetatable(L,-5); /* stack: mt ubox[u] super super[mt] flag */
113 lua_pop(L,3); /* stack: mt ubox[u] */
114 }
115 lua_remove(L, -2); /* stack: ubox[u]*/
116 }
117 }
118
119 TOLUA_API void tolua_pushusertype_and_takeownership (lua_State* L, void* value, const char* type)
120 {
121 tolua_pushusertype(L,value,type);
122 tolua_register_gc(L,lua_gettop(L));
123 }
124
125 TOLUA_API void tolua_pushfieldvalue (lua_State* L, int lo, int index, int v)
126 {
127 lua_pushnumber(L,index);
128 lua_pushvalue(L,v);
129 lua_settable(L,lo);
130 }
131
132 TOLUA_API void tolua_pushfieldboolean (lua_State* L, int lo, int index, int v)
133 {
134 lua_pushnumber(L,index);
135 lua_pushboolean(L,v);
136 lua_settable(L,lo);
137 }
138
139
140 TOLUA_API void tolua_pushfieldnumber (lua_State* L, int lo, int index, lua_Number v)
141 {
142 lua_pushnumber(L,index);
143 tolua_pushnumber(L,v);
144 lua_settable(L,lo);
145 }
146
147 TOLUA_API void tolua_pushfieldstring (lua_State* L, int lo, int index, const char* v)
148 {
149 lua_pushnumber(L,index);
150 tolua_pushstring(L,v);
151 lua_settable(L,lo);
152 }
153
154 TOLUA_API void tolua_pushfielduserdata (lua_State* L, int lo, int index, void* v)
155 {
156 lua_pushnumber(L,index);
157 tolua_pushuserdata(L,v);
158 lua_settable(L,lo);
159 }
160
161 TOLUA_API void tolua_pushfieldusertype (lua_State* L, int lo, int index, void* v, const char* type)
162 {
163 lua_pushnumber(L,index);
164 tolua_pushusertype(L,v,type);
165 lua_settable(L,lo);
166 }
167
168 TOLUA_API void tolua_pushfieldusertype_and_takeownership (lua_State* L, int lo, int index, void* v, const char* type)
169 {
170 lua_pushnumber(L,index);
171 tolua_pushusertype(L,v,type);
172 tolua_register_gc(L,lua_gettop(L));
173 lua_settable(L,lo);
174 }
175