summaryrefslogtreecommitdiff
path: root/src/lua/tolua_rg.c
blob: 4337e9f9b127d6f24957fc39aecaef841b24b1c3 (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* tolua: register functions
** Support code for Lua bindings.
** Written by Waldemar Celes
** TeCGraf/PUC-Rio
** Jul 1998
** $Id: tolua_rg.c,v 1.2 2001/11/26 23:00:27 darkgod Exp $
*/

/* 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 <stdio.h>

#include "tolua.h"
#include "tolua_rg.h"
#include "tolua_tm.h"
#include "tolua_tt.h"

void tolua_globalvar (lua_State* L, char* name, lua_CFunction get, lua_CFunction set)
{
 lua_newtable(L);
 lua_pushstring(L,".get");
 lua_pushcfunction(L,get);
 lua_settable(L,-3);
 if (set)
 {
  lua_pushstring(L,".set");
  lua_pushcfunction(L,set);
  lua_settable(L,-3);
 }
 lua_pushvalue(L,-1);  /* duplicate top */
 lua_setglobal(L,name);
 toluaI_tm_global(L,lua_gettop(L));
 lua_pop(L,1);
}

static int toluaI_const_global_array (lua_State* L)
{
 lua_error(L,"value of const array cannot be changed");
 return 0;
}


void tolua_globalarray (lua_State* L,char* name, lua_CFunction get, lua_CFunction set)
{
 int tag = lua_newtag(L);
 lua_newtable(L);
 lua_settag(L,tag);
 lua_setglobal(L,name);
 
 lua_pushcfunction(L,get);
 lua_settagmethod(L,tag,"gettable"); 
 if (set)
  lua_pushcfunction(L,set);
 else
  lua_pushcfunction(L,toluaI_const_global_array);
 lua_settagmethod(L,tag,"settable");
}

void tolua_tablevar 
(lua_State* L, char* table, char* name, lua_CFunction get, lua_CFunction set)
{
 lua_getglobal(L,table);

 lua_pushstring(L,".get");
 lua_gettable(L,-2);
 lua_pushstring(L,name);
 lua_pushcfunction(L,get);
 lua_settable(L,-3);
 lua_pop(L,1);
 if (set)
 {
  lua_pushstring(L,".set");
  lua_gettable(L,-2);
  lua_pushstring(L,name);
  lua_pushcfunction(L,set);
  lua_settable(L,-3);
  lua_pop(L,1);
 }

 lua_pop(L,1);
}

static int toluaI_get_array (lua_State* L)
{
 void* self = tolua_getuserdata(L,1,0);
 const char* field = tolua_getstring(L,2,0);

 if (!field)
  tolua_error(L,"invalid 'field' in accessing array");
 if (!self)
 {
  static char msg[BUFSIZ];
  sprintf(msg,"invalid 'self' in accessing array '%s'",field);
  tolua_error(L,msg);
 }
 toluaI_getregistry(L,"tolua_tbl_itype");
 lua_pushnumber(L,lua_tag(L,1));
 lua_gettable(L,-2);
 lua_getglobal(L,lua_tostring(L,-1));
 lua_pushstring(L,".array");
 lua_gettable(L,-2);             
 lua_pushvalue(L,2);    /* field */
 lua_gettable(L,-2);
 lua_pushstring(L,".self");
 lua_pushvalue(L,1);    /* self */
 lua_rawset(L,-3);
 return 1;
}

static int toluaI_const_array (lua_State* L)
{
 lua_error(L,"value of const field cannot be changed");
 return 0;
}

void tolua_tablearray
(lua_State* L, char* table, char* name, lua_CFunction get, lua_CFunction set)
{
 int tag = lua_newtag(L);
 lua_getglobal(L,table);
 lua_pushstring(L,".array");
 lua_rawget(L,-2);
 lua_pushstring(L,name);
 lua_newtable(L);
 lua_settag(L,tag);
 lua_settable(L,-3);
 lua_pop(L,2);

 lua_pushcfunction(L,get);
 lua_settagmethod(L,tag,"gettable");
 if (set)
  lua_pushcfunction(L,set);
 else
  lua_pushcfunction(L,toluaI_const_array);
 lua_settagmethod(L,tag,"settable");

 tolua_tablevar(L,table,name,toluaI_get_array,NULL);
}

void tolua_module (lua_State* L, char* name)
{
 lua_getglobal(L,name);
 if (!lua_istable(L,-1))
 {
  lua_newtable(L);
  lua_pushstring(L,".get");
  lua_newtable(L);
  lua_settable(L,-3);
  lua_pushstring(L,".set");
  lua_newtable(L);
  lua_settable(L,-3);
  lua_pushvalue(L,-1);  /* duplicate top */
  lua_setglobal(L,name);
  toluaI_tm_module(L,lua_gettop(L));
  lua_pop(L,1);
 }
 lua_pop(L,1);
}

void tolua_cclass (lua_State* L, char* name, char* base)
{
 int t;
 lua_newtable(L);
 lua_pushstring(L,".get");
 lua_newtable(L);
 lua_settable(L,-3);
 lua_pushstring(L,".set");
 lua_newtable(L);
 lua_settable(L,-3);
 lua_pushstring(L,".array");
 lua_newtable(L);
 lua_settable(L,-3);
 if (*base != 0)
 {
  lua_pushstring(L,".base");
  lua_getglobal(L,base);
  lua_rawset(L,-3);
 }
 lua_pushvalue(L,-1);  /* duplicate top */
 lua_setglobal(L,name);
 t = lua_gettop(L); 
 toluaI_tm_class(L,t,name);
 toluaI_tt_class(L,t,name,base);
 lua_pop(L,1);
}


void tolua_function (lua_State* L, char* parent, char* name, lua_CFunction func)
{
 if (parent==NULL)
 {
  lua_pushcfunction(L,func);
  lua_setglobal(L,name);
 }
 else
 {
  lua_getglobal(L,parent);
  lua_pushstring(L,name);
  lua_pushcfunction(L,func);
  lua_settable(L,-3);
  lua_pop(L,1);
 }
}

void tolua_constant (lua_State* L, char* parent, char* name, long value)
{
 if (parent==NULL)
 {
  lua_pushnumber(L,value);
  lua_setglobal(L,name);
 }
 else
 {
  lua_getglobal(L,parent);
  lua_pushstring(L,name);
  lua_pushnumber(L,value);
  lua_settable(L,-3);
  lua_pop(L,1);
 }
}

void toluaI_setregistry (lua_State* L, char* field)
{
 lua_getregistry(L);
 lua_insert(L,-2);
 lua_pushstring(L,field);
 lua_insert(L,-2);
 lua_settable(L,-3);
 lua_pop(L,1);
}

void toluaI_getregistry (lua_State* L, char* field)
{
 lua_getregistry(L);
 lua_pushstring(L,field);
 lua_gettable(L,-2);
 lua_insert(L,-2);
 lua_pop(L,1);
}