summaryrefslogtreecommitdiffstats
path: root/lib/tolua++/src/lib/tolua_push.c
blob: 73a5f6ec0bc2db54d3b4233dbf41c7a09a48c21b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* tolua: functions to push C values.
** Support code for Lua bindings.
** Written by Waldemar Celes
** TeCGraf/PUC-Rio
** Apr 2003
** $Id: $
*/

/* This code is free software; you can redistribute it and/or modify it.
** The software provided hereunder is on an "as is" basis, and
** the author has no obligation to provide maintenance, support, updates,
** enhancements, or modifications.
*/

#include "../../include/tolua++.h"
#include "../../../lua/src/lauxlib.h"

#include <stdlib.h>
#include <assert.h>

TOLUA_API void tolua_pushvalue (lua_State* L, int lo)
{
 lua_pushvalue(L,lo);
}

TOLUA_API void tolua_pushboolean (lua_State* L, int value)
{
 lua_pushboolean(L,value);
}

TOLUA_API void tolua_pushnumber (lua_State* L, lua_Number value)
{
 lua_pushnumber(L,value);
}

TOLUA_API void tolua_pushstring (lua_State* L, const char* value)
{
 if (value == NULL)
  lua_pushnil(L);
 else
  lua_pushstring(L,value);
}

TOLUA_API void tolua_pushuserdata (lua_State* L, void* value)
{
 if (value == NULL)
  lua_pushnil(L);
 else
  lua_pushlightuserdata(L,value);
}

TOLUA_API void tolua_pushusertype (lua_State* L, void* value, const char* type)
{
 if (value == NULL)
  lua_pushnil(L);
 else
 {
  luaL_getmetatable(L, type);
  assert(!lua_isnil(L, -1));  /* Failure here means that the usertype is unknown to ToLua. Check what type you're pushing. */
  lua_pushstring(L,"tolua_ubox");
  lua_rawget(L,-2);        /* stack: mt ubox */
  if (lua_isnil(L, -1))
  {
   lua_pop(L, 1);
   lua_pushstring(L, "tolua_ubox");
   lua_rawget(L, LUA_REGISTRYINDEX);
  };
  lua_pushlightuserdata(L,value);
  lua_rawget(L,-2);                       /* stack: mt ubox ubox[u] */
  if (lua_isnil(L,-1))
  {
   lua_pop(L,1);                          /* stack: mt ubox */
   lua_pushlightuserdata(L,value);
   *(void**)lua_newuserdata(L,sizeof(void *)) = value;   /* stack: mt ubox u newud */
   lua_pushvalue(L,-1);                   /* stack: mt ubox u newud newud */
   lua_insert(L,-4);                      /* stack: mt newud ubox u newud */
   lua_rawset(L,-3);                      /* stack: mt newud ubox */
   lua_pop(L,1);                          /* stack: mt newud */
   /*luaL_getmetatable(L,type);*/
   lua_pushvalue(L, -2);			/* stack: mt newud mt */
   lua_setmetatable(L,-2);			/* stack: mt newud */

   #ifdef LUA_VERSION_NUM
   lua_pushvalue(L, TOLUA_NOPEER);
   lua_setfenv(L, -2);
   #endif
  }
  else
  {
   /* check the need of updating the metatable to a more specialized class */
   lua_insert(L,-2);                       /* stack: mt ubox[u] ubox */
   lua_pop(L,1);                           /* stack: mt ubox[u] */
   lua_pushstring(L,"tolua_super");
   lua_rawget(L,LUA_REGISTRYINDEX);        /* stack: mt ubox[u] super */
   lua_getmetatable(L,-2);                 /* stack: mt ubox[u] super mt */
   lua_rawget(L,-2);                       /* stack: mt ubox[u] super super[mt] */
			if (lua_istable(L,-1))
			{
				lua_pushstring(L,type);                 /* stack: mt ubox[u] super super[mt] type */
				lua_rawget(L,-2);                       /* stack: mt ubox[u] super super[mt] flag */
				if (lua_toboolean(L,-1) == 1)   /* if true */
				{
					lua_pop(L,3);	/* mt ubox[u]*/
					lua_remove(L, -2);
					return;
				}
			}
			/* type represents a more specilized type */
			/*luaL_getmetatable(L,type);             // stack: mt ubox[u] super super[mt] flag mt */
			lua_pushvalue(L, -5);					/* stack: mt ubox[u] super super[mt] flag mt */
			lua_setmetatable(L,-5);                /* stack: mt ubox[u] super super[mt] flag */
			lua_pop(L,3);                          /* stack: mt ubox[u] */
  }
  lua_remove(L, -2);	/* stack: ubox[u]*/
 }
}

TOLUA_API void tolua_pushusertype_and_takeownership (lua_State* L, void* value, const char* type)
{
	tolua_pushusertype(L,value,type);
	tolua_register_gc(L,lua_gettop(L));
}

TOLUA_API void tolua_pushfieldvalue (lua_State* L, int lo, int index, int v)
{
 lua_pushnumber(L,index);
 lua_pushvalue(L,v);
 lua_settable(L,lo);
}

TOLUA_API void tolua_pushfieldboolean (lua_State* L, int lo, int index, int v)
{
 lua_pushnumber(L,index);
 lua_pushboolean(L,v);
 lua_settable(L,lo);
}


TOLUA_API void tolua_pushfieldnumber (lua_State* L, int lo, int index, lua_Number v)
{
 lua_pushnumber(L,index);
 tolua_pushnumber(L,v);
 lua_settable(L,lo);
}

TOLUA_API void tolua_pushfieldstring (lua_State* L, int lo, int index, const char* v)
{
 lua_pushnumber(L,index);
 tolua_pushstring(L,v);
 lua_settable(L,lo);
}

TOLUA_API void tolua_pushfielduserdata (lua_State* L, int lo, int index, void* v)
{
 lua_pushnumber(L,index);
 tolua_pushuserdata(L,v);
 lua_settable(L,lo);
}

TOLUA_API void tolua_pushfieldusertype (lua_State* L, int lo, int index, void* v, const char* type)
{
 lua_pushnumber(L,index);
 tolua_pushusertype(L,v,type);
 lua_settable(L,lo);
}

TOLUA_API void tolua_pushfieldusertype_and_takeownership (lua_State* L, int lo, int index, void* v, const char* type)
{
 lua_pushnumber(L,index);
 tolua_pushusertype(L,v,type);
	tolua_register_gc(L,lua_gettop(L));
 lua_settable(L,lo);
}