1899
|
1 /* -----------------------------------------------------------------------------
|
|
2 * luarun.swg
|
|
3 *
|
|
4 * This file contains the runtime support for Lua modules
|
|
5 * and includes code for managing global variables and pointer
|
|
6 * type checking.
|
|
7 * ----------------------------------------------------------------------------- */
|
|
8
|
|
9 #ifdef __cplusplus
|
|
10 extern "C" {
|
|
11 #endif
|
|
12
|
|
13 #include "lua.h"
|
|
14 #include "lauxlib.h"
|
|
15 #include <stdlib.h> /* for malloc */
|
|
16 #include <assert.h> /* for a few sanity tests */
|
|
17
|
|
18 /* -----------------------------------------------------------------------------
|
|
19 * Lua flavors
|
|
20 * ----------------------------------------------------------------------------- */
|
|
21
|
|
22 #define SWIG_LUA_FLAVOR_LUA 1
|
|
23 #define SWIG_LUA_FLAVOR_ELUA 2
|
|
24 #define SWIG_LUA_FLAVOR_ELUAC 3
|
|
25
|
|
26 #if !defined(SWIG_LUA_TARGET)
|
|
27 # error SWIG_LUA_TARGET not defined
|
|
28 #endif
|
|
29
|
|
30 #if (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) || (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUAC)
|
|
31 # define SWIG_LUA_CONSTTAB_INT(B, C) LSTRKEY(B), LNUMVAL(C)
|
|
32 # define SWIG_LUA_CONSTTAB_FLOAT(B, C) LSTRKEY(B), LNUMVAL(C)
|
|
33 # define SWIG_LUA_CONSTTAB_STRING(B, C) LSTRKEY(B), LSTRVAL(C)
|
|
34 # define SWIG_LUA_CONSTTAB_CHAR(B, C) LSTRKEY(B), LNUMVAL(C)
|
|
35 #else /* SWIG_LUA_FLAVOR_LUA */
|
|
36 # define SWIG_LUA_CONSTTAB_INT(B, C) SWIG_LUA_INT, (char *)B, (long)C, 0, 0, 0
|
|
37 # define SWIG_LUA_CONSTTAB_FLOAT(B, C) SWIG_LUA_FLOAT, (char *)B, 0, (double)C, 0, 0
|
|
38 # define SWIG_LUA_CONSTTAB_STRING(B, C) SWIG_LUA_STRING, (char *)B, 0, 0, (void *)C, 0
|
|
39 # define SWIG_LUA_CONSTTAB_CHAR(B, C) SWIG_LUA_CHAR, (char *)B, (long)C, 0, 0, 0
|
|
40 #endif
|
|
41
|
|
42 #if (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) || (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUAC)
|
|
43 # define LRO_STRVAL(v) {{.p = (char *) v}, LUA_TSTRING}
|
|
44 # define LSTRVAL LRO_STRVAL
|
|
45 #endif
|
|
46
|
|
47 /* -----------------------------------------------------------------------------
|
|
48 * compatibility defines
|
|
49 * ----------------------------------------------------------------------------- */
|
|
50
|
|
51 /* History of Lua C API length functions: In Lua 5.0 (and before?)
|
|
52 there was "lua_strlen". In Lua 5.1, this was renamed "lua_objlen",
|
|
53 but a compatibility define of "lua_strlen" was added. In Lua 5.2,
|
|
54 this function was again renamed, to "lua_rawlen" (to emphasize that
|
|
55 it doesn't call the "__len" metamethod), and the compatibility
|
|
56 define of lua_strlen was removed. All SWIG uses have been updated
|
|
57 to "lua_rawlen", and we add our own defines of that here for older
|
|
58 versions of Lua. */
|
|
59 #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 501
|
|
60 # define lua_rawlen lua_strlen
|
|
61 #elif LUA_VERSION_NUM == 501
|
|
62 # define lua_rawlen lua_objlen
|
|
63 #endif
|
|
64
|
|
65
|
|
66 /* lua_pushglobaltable is the recommended "future-proof" way to get
|
|
67 the global table for Lua 5.2 and later. Here we define
|
|
68 lua_pushglobaltable ourselves for Lua versions before 5.2. */
|
|
69 #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 502
|
|
70 # define lua_pushglobaltable(L) lua_pushvalue(L, LUA_GLOBALSINDEX)
|
|
71 #endif
|
|
72
|
|
73
|
|
74 /* --------------------------------------------------------------------------
|
|
75 * Helper functions for error handling
|
|
76 * -------------------------------------------------------------------------- */
|
|
77
|
|
78 /* Push the string STR on the Lua stack, like lua_pushstring, but
|
|
79 prefixed with the the location of the innermost Lua call-point
|
|
80 (as formated by luaL_where). */
|
|
81 SWIGRUNTIME void
|
|
82 SWIG_Lua_pusherrstring (lua_State *L, const char *str)
|
|
83 {
|
|
84 luaL_where (L, 1);
|
|
85 lua_pushstring (L, str);
|
|
86 lua_concat (L, 2);
|
|
87 }
|
|
88
|
|
89 /* Push a formatted string generated from FMT and following args on
|
|
90 the Lua stack, like lua_pushfstring, but prefixed with the the
|
|
91 location of the innermost Lua call-point (as formated by luaL_where). */
|
|
92 SWIGRUNTIME void
|
|
93 SWIG_Lua_pushferrstring (lua_State *L, const char *fmt, ...)
|
|
94 {
|
|
95 va_list argp;
|
|
96 va_start(argp, fmt);
|
|
97 luaL_where(L, 1);
|
|
98 lua_pushvfstring(L, fmt, argp);
|
|
99 va_end(argp);
|
|
100 lua_concat(L, 2);
|
|
101 }
|
|
102
|
|
103
|
|
104 /* -----------------------------------------------------------------------------
|
|
105 * global swig types
|
|
106 * ----------------------------------------------------------------------------- */
|
|
107 /* Constant table */
|
|
108 #define SWIG_LUA_INT 1
|
|
109 #define SWIG_LUA_FLOAT 2
|
|
110 #define SWIG_LUA_STRING 3
|
|
111 #define SWIG_LUA_POINTER 4
|
|
112 #define SWIG_LUA_BINARY 5
|
|
113 #define SWIG_LUA_CHAR 6
|
|
114
|
|
115 /* Structure for variable linking table */
|
|
116 typedef struct {
|
|
117 const char *name;
|
|
118 lua_CFunction get;
|
|
119 lua_CFunction set;
|
|
120 } swig_lua_var_info;
|
|
121
|
|
122 /* Constant information structure */
|
|
123 typedef struct {
|
|
124 int type;
|
|
125 char *name;
|
|
126 long lvalue;
|
|
127 double dvalue;
|
|
128 void *pvalue;
|
|
129 swig_type_info **ptype;
|
|
130 } swig_lua_const_info;
|
|
131
|
|
132 typedef struct {
|
|
133 const char *name;
|
|
134 lua_CFunction method;
|
|
135 } swig_lua_method;
|
|
136
|
|
137 typedef struct {
|
|
138 const char *name;
|
|
139 lua_CFunction getmethod;
|
|
140 lua_CFunction setmethod;
|
|
141 } swig_lua_attribute;
|
|
142
|
|
143 // Can be used to create namespaces. Currently used to
|
|
144 // wrap class static methods/variables/constants
|
|
145 typedef struct {
|
|
146 const char *name;
|
|
147 swig_lua_method *ns_methods;
|
|
148 swig_lua_attribute *ns_attributes;
|
|
149 swig_lua_const_info *ns_constants;
|
|
150 } swig_lua_namespace;
|
|
151
|
|
152 typedef struct swig_lua_class {
|
|
153 const char *name;
|
|
154 swig_type_info **type;
|
|
155 lua_CFunction constructor;
|
|
156 void (*destructor)(void *);
|
|
157 swig_lua_method *methods;
|
|
158 swig_lua_attribute *attributes;
|
|
159 swig_lua_namespace cls_static;
|
|
160 struct swig_lua_class **bases;
|
|
161 const char **base_names;
|
|
162 } swig_lua_class;
|
|
163
|
|
164 /* this is the struct for wrapping all pointers in SwigLua
|
|
165 */
|
|
166 typedef struct {
|
|
167 swig_type_info *type;
|
|
168 int own; /* 1 if owned & must be destroyed */
|
|
169 void *ptr;
|
|
170 } swig_lua_userdata;
|
|
171
|
|
172 /* this is the struct for wrapping arbitrary packed binary data
|
|
173 (currently it is only used for member function pointers)
|
|
174 the data ordering is similar to swig_lua_userdata, but it is currently not possible
|
|
175 to tell the two structures apart within SWIG, other than by looking at the type
|
|
176 */
|
|
177 typedef struct {
|
|
178 swig_type_info *type;
|
|
179 int own; /* 1 if owned & must be destroyed */
|
|
180 char data[1]; /* arbitary amount of data */
|
|
181 } swig_lua_rawdata;
|
|
182
|
|
183 /* Common SWIG API */
|
|
184 #define SWIG_NewPointerObj(L, ptr, type, owner) SWIG_Lua_NewPointerObj(L, (void *)ptr, type, owner)
|
|
185 #define SWIG_ConvertPtr(L,idx, ptr, type, flags) SWIG_Lua_ConvertPtr(L,idx,ptr,type,flags)
|
|
186 #define SWIG_MustGetPtr(L,idx, type,flags, argnum,fnname) SWIG_Lua_MustGetPtr(L,idx, type,flags, argnum,fnname)
|
|
187 /* for C++ member pointers, ie, member methods */
|
|
188 #define SWIG_ConvertMember(L, idx, ptr, sz, ty) SWIG_Lua_ConvertPacked(L, idx, ptr, sz, ty)
|
|
189 #define SWIG_NewMemberObj(L, ptr, sz, type) SWIG_Lua_NewPackedObj(L, ptr, sz, type)
|
|
190
|
|
191 /* Runtime API */
|
|
192 #define SWIG_GetModule(clientdata) SWIG_Lua_GetModule((lua_State*)(clientdata))
|
|
193 #define SWIG_SetModule(clientdata, pointer) SWIG_Lua_SetModule((lua_State*) (clientdata), pointer)
|
|
194 #define SWIG_MODULE_CLIENTDATA_TYPE lua_State*
|
|
195
|
|
196 /* Contract support */
|
|
197 #define SWIG_contract_assert(expr, msg) \
|
|
198 if (!(expr)) { SWIG_Lua_pusherrstring(L, (char *) msg); goto fail; } else
|
|
199
|
|
200
|
|
201 /* helper #defines */
|
|
202 #define SWIG_fail {goto fail;}
|
|
203 #define SWIG_fail_arg(func_name,argnum,type) \
|
|
204 {SWIG_Lua_pushferrstring(L,"Error in %s (arg %d), expected '%s' got '%s'",\
|
|
205 func_name,argnum,type,SWIG_Lua_typename(L,argnum));\
|
|
206 goto fail;}
|
|
207 #define SWIG_fail_ptr(func_name,argnum,type) \
|
|
208 SWIG_fail_arg(func_name,argnum,(type && type->str)?type->str:"void*")
|
|
209 #define SWIG_check_num_args(func_name,a,b) \
|
|
210 if (lua_gettop(L)<a || lua_gettop(L)>b) \
|
|
211 {SWIG_Lua_pushferrstring(L,"Error in %s expected %d..%d args, got %d",func_name,a,b,lua_gettop(L));\
|
|
212 goto fail;}
|
|
213
|
|
214
|
|
215 #define SWIG_Lua_get_table(L,n) \
|
|
216 (lua_pushstring(L, n), lua_rawget(L,-2))
|
|
217
|
|
218 #define SWIG_Lua_add_function(L,n,f) \
|
|
219 (lua_pushstring(L, n), \
|
|
220 lua_pushcfunction(L, f), \
|
|
221 lua_rawset(L,-3))
|
|
222
|
|
223 /* special helper for allowing 'nil' for usertypes */
|
|
224 #define SWIG_isptrtype(L,I) (lua_isuserdata(L,I) || lua_isnil(L,I))
|
|
225
|
|
226 #ifdef __cplusplus
|
|
227 /* Special helper for member function pointers
|
|
228 it gets the address, casts it, then dereferences it */
|
|
229 //#define SWIG_mem_fn_as_voidptr(a) (*((char**)&(a)))
|
|
230 #endif
|
|
231
|
|
232 /* storing/access of swig_module_info */
|
|
233 SWIGRUNTIME swig_module_info *
|
|
234 SWIG_Lua_GetModule(lua_State* L) {
|
|
235 swig_module_info *ret = 0;
|
|
236 lua_pushstring(L,"swig_runtime_data_type_pointer" SWIG_RUNTIME_VERSION SWIG_TYPE_TABLE_NAME);
|
|
237 lua_rawget(L,LUA_REGISTRYINDEX);
|
|
238 if (lua_islightuserdata(L,-1))
|
|
239 ret=(swig_module_info*)lua_touserdata(L,-1);
|
|
240 lua_pop(L,1); /* tidy */
|
|
241 return ret;
|
|
242 }
|
|
243
|
|
244 SWIGRUNTIME void
|
|
245 SWIG_Lua_SetModule(lua_State* L, swig_module_info *module) {
|
|
246 /* add this all into the Lua registry: */
|
|
247 lua_pushstring(L,"swig_runtime_data_type_pointer" SWIG_RUNTIME_VERSION SWIG_TYPE_TABLE_NAME);
|
|
248 lua_pushlightuserdata(L,(void*)module);
|
|
249 lua_rawset(L,LUA_REGISTRYINDEX);
|
|
250 }
|
|
251
|
|
252 /* -----------------------------------------------------------------------------
|
|
253 * global variable support code: modules
|
|
254 * ----------------------------------------------------------------------------- */
|
|
255
|
|
256 /* this function is called when trying to set an immutable.
|
|
257 default action is to print an error.
|
|
258 This can removed with a compile flag SWIGLUA_IGNORE_SET_IMMUTABLE */
|
|
259 SWIGINTERN int SWIG_Lua_set_immutable(lua_State* L)
|
|
260 {
|
|
261 /* there should be 1 param passed in: the new value */
|
|
262 #ifndef SWIGLUA_IGNORE_SET_IMMUTABLE
|
|
263 lua_pop(L,1); /* remove it */
|
|
264 luaL_error(L,"This variable is immutable");
|
|
265 #endif
|
|
266 return 0; /* should not return anything */
|
|
267 }
|
|
268
|
|
269 /* the module.get method used for getting linked data */
|
|
270 SWIGINTERN int SWIG_Lua_module_get(lua_State* L)
|
|
271 {
|
|
272 /* there should be 2 params passed in
|
|
273 (1) table (not the meta table)
|
|
274 (2) string name of the attribute
|
|
275 printf("SWIG_Lua_module_get %p(%s) '%s'\n",
|
|
276 lua_topointer(L,1),lua_typename(L,lua_type(L,1)),
|
|
277 lua_tostring(L,2));
|
|
278 */
|
|
279 /* get the metatable */
|
|
280 #if ((SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) || (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUAC))
|
|
281 assert(lua_isrotable(L,1)); /* just in case */
|
|
282 #else
|
|
283 assert(lua_istable(L,1)); /* default Lua action */
|
|
284 #endif
|
|
285 lua_getmetatable(L,1); /* get the metatable */
|
|
286 #if ((SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) || (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUAC))
|
|
287 assert(lua_isrotable(L,-1)); /* just in case */
|
|
288 #else
|
|
289 assert(lua_istable(L,-1));
|
|
290 #endif
|
|
291 SWIG_Lua_get_table(L,".get"); /* get the .get table */
|
|
292 lua_remove(L,3); /* remove metatable */
|
|
293 #if ((SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) || (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUAC))
|
|
294 if (lua_isrotable(L,-1))
|
|
295 #else
|
|
296 if (lua_istable(L,-1))
|
|
297 #endif
|
|
298 {
|
|
299 /* look for the key in the .get table */
|
|
300 lua_pushvalue(L,2); /* key */
|
|
301 lua_rawget(L,-2);
|
|
302 lua_remove(L,3); /* remove .get */
|
|
303 if (lua_iscfunction(L,-1))
|
|
304 { /* found it so call the fn & return its value */
|
|
305 lua_call(L,0,1);
|
|
306 return 1;
|
|
307 }
|
|
308 lua_pop(L,1); /* remove the top */
|
|
309 }
|
|
310 lua_pop(L,1); /* remove the .get */
|
|
311 lua_pushnil(L); /* return a nil */
|
|
312 return 1;
|
|
313 }
|
|
314
|
|
315 /* the module.set method used for setting linked data */
|
|
316 SWIGINTERN int SWIG_Lua_module_set(lua_State* L)
|
|
317 {
|
|
318 /* there should be 3 params passed in
|
|
319 (1) table (not the meta table)
|
|
320 (2) string name of the attribute
|
|
321 (3) any for the new value
|
|
322 */
|
|
323 /* get the metatable */
|
|
324 #if ((SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) || (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUAC))
|
|
325 assert(lua_isrotable(L,1)); /* just in case */
|
|
326 #else
|
|
327 assert(lua_istable(L,1)); /* default Lua action */
|
|
328 #endif
|
|
329 lua_getmetatable(L,1); /* get the metatable */
|
|
330 #if ((SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) || (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUAC))
|
|
331 assert(lua_isrotable(L,-1)); /* just in case */
|
|
332 #else
|
|
333 assert(lua_istable(L,-1));
|
|
334 #endif
|
|
335 SWIG_Lua_get_table(L,".set"); /* get the .set table */
|
|
336 lua_remove(L,4); /* remove metatable */
|
|
337 #if ((SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) || (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUAC))
|
|
338 if (lua_isrotable(L,-1))
|
|
339 #else
|
|
340 if (lua_istable(L,-1))
|
|
341 #endif
|
|
342 {
|
|
343 /* look for the key in the .set table */
|
|
344 lua_pushvalue(L,2); /* key */
|
|
345 lua_rawget(L,-2);
|
|
346 lua_remove(L,4); /* remove .set */
|
|
347 if (lua_iscfunction(L,-1))
|
|
348 { /* found it so call the fn & return its value */
|
|
349 lua_pushvalue(L,3); /* value */
|
|
350 lua_call(L,1,0);
|
|
351 return 0;
|
|
352 }
|
|
353 #if (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA)
|
|
354 else {
|
|
355 return 0; // Exits stoically if an invalid key is initialized.
|
|
356 }
|
|
357 #endif
|
|
358 }
|
|
359 lua_settop(L,3); /* reset back to start */
|
|
360 /* we now have the table, key & new value, so just set directly */
|
|
361 lua_rawset(L,1); /* add direct */
|
|
362 return 0;
|
|
363 }
|
|
364
|
|
365 #if ((SWIG_LUA_TARGET != SWIG_LUA_FLAVOR_ELUA) && (SWIG_LUA_TARGET != SWIG_LUA_FLAVOR_ELUAC))
|
|
366 /* registering a module in lua. Pushes the module table on the stack. */
|
|
367 SWIGINTERN void SWIG_Lua_module_begin(lua_State* L,const char* name)
|
|
368 {
|
|
369 assert(lua_istable(L,-1)); /* just in case */
|
|
370 lua_pushstring(L,name);
|
|
371 lua_newtable(L); /* the table */
|
|
372 /* add meta table */
|
|
373 lua_newtable(L); /* the meta table */
|
|
374 SWIG_Lua_add_function(L,"__index",SWIG_Lua_module_get);
|
|
375 SWIG_Lua_add_function(L,"__newindex",SWIG_Lua_module_set);
|
|
376 lua_pushstring(L,".get");
|
|
377 lua_newtable(L); /* the .get table */
|
|
378 lua_rawset(L,-3); /* add .get into metatable */
|
|
379 lua_pushstring(L,".set");
|
|
380 lua_newtable(L); /* the .set table */
|
|
381 lua_rawset(L,-3); /* add .set into metatable */
|
|
382 lua_setmetatable(L,-2); /* sets meta table in module */
|
|
383 #ifdef SWIG_LUA_MODULE_GLOBAL
|
|
384 /* If requested, install the module directly into the global namespace. */
|
|
385 lua_rawset(L,-3); /* add module into parent */
|
|
386 SWIG_Lua_get_table(L,name); /* get the table back out */
|
|
387 #else
|
|
388 /* Do not install the module table as global name. The stack top has
|
|
389 the module table with the name below. We pop the top and replace
|
|
390 the name with it. */
|
|
391 lua_replace(L,-2);
|
|
392 #endif
|
|
393 }
|
|
394
|
|
395 /* ending the register */
|
|
396 SWIGINTERN void SWIG_Lua_module_end(lua_State* L)
|
|
397 {
|
|
398 lua_pop(L,1); /* tidy stack (remove module) */
|
|
399 }
|
|
400
|
|
401 /* adding a linked variable to the module */
|
|
402 SWIGINTERN void SWIG_Lua_module_add_variable(lua_State* L,const char* name,lua_CFunction getFn,lua_CFunction setFn)
|
|
403 {
|
|
404 assert(lua_istable(L,-1)); /* just in case */
|
|
405 lua_getmetatable(L,-1); /* get the metatable */
|
|
406 assert(lua_istable(L,-1)); /* just in case */
|
|
407 SWIG_Lua_get_table(L,".get"); /* find the .get table */
|
|
408 assert(lua_istable(L,-1)); /* should be a table: */
|
|
409 SWIG_Lua_add_function(L,name,getFn);
|
|
410 lua_pop(L,1); /* tidy stack (remove table) */
|
|
411 if (setFn) /* if there is a set fn */
|
|
412 {
|
|
413 SWIG_Lua_get_table(L,".set"); /* find the .set table */
|
|
414 assert(lua_istable(L,-1)); /* should be a table: */
|
|
415 SWIG_Lua_add_function(L,name,setFn);
|
|
416 lua_pop(L,1); /* tidy stack (remove table) */
|
|
417 }
|
|
418 lua_pop(L,1); /* tidy stack (remove meta) */
|
|
419 }
|
|
420 #endif
|
|
421
|
|
422 /* adding a function module */
|
|
423 SWIGINTERN void SWIG_Lua_module_add_function(lua_State* L,const char* name,lua_CFunction fn)
|
|
424 {
|
|
425 SWIG_Lua_add_function(L,name,fn);
|
|
426 }
|
|
427
|
|
428 /* -----------------------------------------------------------------------------
|
|
429 * global variable support code: namespaces
|
|
430 * ----------------------------------------------------------------------------- */
|
|
431
|
|
432 SWIGINTERN int SWIG_Lua_namespace_get(lua_State* L)
|
|
433 {
|
|
434 /* there should be 2 params passed in
|
|
435 (1) table (not the meta table)
|
|
436 (2) string name of the attribute
|
|
437 */
|
|
438 assert(lua_istable(L,-2)); /* just in case */
|
|
439 lua_getmetatable(L,-2);
|
|
440 assert(lua_istable(L,-1));
|
|
441 SWIG_Lua_get_table(L,".get"); /* find the .get table */
|
|
442 assert(lua_istable(L,-1));
|
|
443 /* look for the key in the .get table */
|
|
444 lua_pushvalue(L,2); /* key */
|
|
445 lua_rawget(L,-2);
|
|
446 lua_remove(L,-2); /* stack tidy, remove .get table */
|
|
447 if (lua_iscfunction(L,-1))
|
|
448 { /* found it so call the fn & return its value */
|
|
449 lua_call(L,0,1); /* 1 value in (userdata),1 out (result) */
|
|
450 lua_remove(L,-2); /* stack tidy, remove metatable */
|
|
451 return 1;
|
|
452 }
|
|
453 lua_pop(L,1); /* remove whatever was there */
|
|
454 /* ok, so try the .fn table */
|
|
455 SWIG_Lua_get_table(L,".fn"); /* find the .get table */
|
|
456 assert(lua_istable(L,-1)); /* just in case */
|
|
457 lua_pushvalue(L,2); /* key */
|
|
458 lua_rawget(L,-2); /* look for the fn */
|
|
459 lua_remove(L,-2); /* stack tidy, remove .fn table */
|
|
460 if (lua_isfunction(L,-1)) /* note: whether it's a C function or lua function */
|
|
461 { /* found it so return the fn & let lua call it */
|
|
462 lua_remove(L,-2); /* stack tidy, remove metatable */
|
|
463 return 1;
|
|
464 }
|
|
465 lua_pop(L,1); /* remove whatever was there */
|
|
466 return 0;
|
|
467 }
|
|
468
|
|
469 SWIGINTERN int SWIG_Lua_namespace_set(lua_State* L)
|
|
470 {
|
|
471 /* there should be 3 params passed in
|
|
472 (1) table (not the meta table)
|
|
473 (2) string name of the attribute
|
|
474 (3) any for the new value
|
|
475 */
|
|
476
|
|
477 assert(lua_istable(L,1));
|
|
478 lua_getmetatable(L,1); /* get the meta table */
|
|
479 assert(lua_istable(L,-1));
|
|
480
|
|
481 SWIG_Lua_get_table(L,".set"); /* find the .set table */
|
|
482 if (lua_istable(L,-1))
|
|
483 {
|
|
484 /* look for the key in the .set table */
|
|
485 lua_pushvalue(L,2); /* key */
|
|
486 lua_rawget(L,-2);
|
|
487 if (lua_iscfunction(L,-1))
|
|
488 { /* found it so call the fn & return its value */
|
|
489 lua_pushvalue(L,3); /* value */
|
|
490 lua_call(L,1,0);
|
|
491 return 0;
|
|
492 }
|
|
493 lua_pop(L,1); /* remove the value */
|
|
494 }
|
|
495 lua_pop(L,1); /* remove the value .set table */
|
|
496 return 0;
|
|
497 }
|
|
498
|
|
499 SWIGINTERN void SWIG_Lua_InstallConstants(lua_State* L, swig_lua_const_info constants[]); // forward declaration
|
|
500 SWIGINTERN void SWIG_Lua_add_class_variable(lua_State* L,const char* name,lua_CFunction getFn,lua_CFunction setFn); // forward declaration
|
|
501
|
|
502 /* helper function - register namespace methods and attributes into namespace */
|
|
503 SWIGINTERN int SWIG_Lua_add_namespace_details(lua_State* L, swig_lua_namespace* ns)
|
|
504 {
|
|
505 int i = 0;
|
|
506 assert(lua_istable(L,-1));
|
|
507 /* There must be table at the top of the stack */
|
|
508 SWIG_Lua_InstallConstants(L, ns->ns_constants);
|
|
509
|
|
510 lua_getmetatable(L,-1);
|
|
511
|
|
512 /* add fns */
|
|
513 for(i=0;ns->ns_attributes[i].name;i++){
|
|
514 SWIG_Lua_add_class_variable(L,ns->ns_attributes[i].name,ns->ns_attributes[i].getmethod,ns->ns_attributes[i].setmethod);
|
|
515 }
|
|
516
|
|
517 /* add methods to the metatable */
|
|
518 SWIG_Lua_get_table(L,".fn"); /* find the .fn table */
|
|
519 assert(lua_istable(L,-1)); /* just in case */
|
|
520 for(i=0;ns->ns_methods[i].name;i++){
|
|
521 SWIG_Lua_add_function(L,ns->ns_methods[i].name,ns->ns_methods[i].method);
|
|
522 }
|
|
523 lua_pop(L,1);
|
|
524
|
|
525 /* clear stack - remove metatble */
|
|
526 lua_pop(L,1);
|
|
527
|
|
528 }
|
|
529
|
|
530 /* helper function. creates namespace table and add it to module table */
|
|
531 SWIGINTERN int SWIG_Lua_namespace_register(lua_State* L, swig_lua_namespace* ns)
|
|
532 {
|
|
533 assert(lua_istable(L,-1)); /* just in case. This is supposed to be module table */
|
|
534 lua_checkstack(L,5);
|
|
535 lua_pushstring(L, ns->name);
|
|
536 lua_newtable(L); /* namespace itself */
|
|
537 lua_newtable(L); /* metatable for namespace */
|
|
538
|
|
539 /* add a table called ".get" */
|
|
540 lua_pushstring(L,".get");
|
|
541 lua_newtable(L);
|
|
542 lua_rawset(L,-3);
|
|
543 /* add a table called ".set" */
|
|
544 lua_pushstring(L,".set");
|
|
545 lua_newtable(L);
|
|
546 lua_rawset(L,-3);
|
|
547 /* add a table called ".fn" */
|
|
548 lua_pushstring(L,".fn");
|
|
549 lua_newtable(L);
|
|
550 lua_rawset(L,-3);
|
|
551
|
|
552 /* add accessor fns for using the .get,.set&.fn */
|
|
553 SWIG_Lua_add_function(L,"__index",SWIG_Lua_namespace_get);
|
|
554 SWIG_Lua_add_function(L,"__newindex",SWIG_Lua_namespace_set);
|
|
555
|
|
556 lua_setmetatable(L,-2); /* set metatable */
|
|
557 lua_rawset(L,-3); /* add namespace to module table */
|
|
558 }
|
|
559 /* -----------------------------------------------------------------------------
|
|
560 * global variable support code: classes
|
|
561 * ----------------------------------------------------------------------------- */
|
|
562
|
|
563 /* the class.get method, performs the lookup of class attributes */
|
|
564 SWIGINTERN int SWIG_Lua_class_get(lua_State* L)
|
|
565 {
|
|
566 /* there should be 2 params passed in
|
|
567 (1) userdata (not the meta table)
|
|
568 (2) string name of the attribute
|
|
569 */
|
|
570 assert(lua_isuserdata(L,-2)); /* just in case */
|
|
571 lua_getmetatable(L,-2); /* get the meta table */
|
|
572 assert(lua_istable(L,-1)); /* just in case */
|
|
573 SWIG_Lua_get_table(L,".get"); /* find the .get table */
|
|
574 assert(lua_istable(L,-1)); /* just in case */
|
|
575 /* look for the key in the .get table */
|
|
576 lua_pushvalue(L,2); /* key */
|
|
577 lua_rawget(L,-2);
|
|
578 lua_remove(L,-2); /* stack tidy, remove .get table */
|
|
579 if (lua_iscfunction(L,-1))
|
|
580 { /* found it so call the fn & return its value */
|
|
581 lua_pushvalue(L,1); /* the userdata */
|
|
582 lua_call(L,1,1); /* 1 value in (userdata),1 out (result) */
|
|
583 lua_remove(L,-2); /* stack tidy, remove metatable */
|
|
584 return 1;
|
|
585 }
|
|
586 lua_pop(L,1); /* remove whatever was there */
|
|
587 /* ok, so try the .fn table */
|
|
588 SWIG_Lua_get_table(L,".fn"); /* find the .get table */
|
|
589 assert(lua_istable(L,-1)); /* just in case */
|
|
590 lua_pushvalue(L,2); /* key */
|
|
591 lua_rawget(L,-2); /* look for the fn */
|
|
592 lua_remove(L,-2); /* stack tidy, remove .fn table */
|
|
593 if (lua_isfunction(L,-1)) /* note: if its a C function or lua function */
|
|
594 { /* found it so return the fn & let lua call it */
|
|
595 lua_remove(L,-2); /* stack tidy, remove metatable */
|
|
596 return 1;
|
|
597 }
|
|
598 lua_pop(L,1); /* remove whatever was there */
|
|
599 /* NEW: looks for the __getitem() fn
|
|
600 this is a user provided get fn */
|
|
601 SWIG_Lua_get_table(L,"__getitem"); /* find the __getitem fn */
|
|
602 if (lua_iscfunction(L,-1)) /* if its there */
|
|
603 { /* found it so call the fn & return its value */
|
|
604 lua_pushvalue(L,1); /* the userdata */
|
|
605 lua_pushvalue(L,2); /* the parameter */
|
|
606 lua_call(L,2,1); /* 2 value in (userdata),1 out (result) */
|
|
607 lua_remove(L,-2); /* stack tidy, remove metatable */
|
|
608 return 1;
|
|
609 }
|
|
610 return 0; /* sorry not known */
|
|
611 }
|
|
612
|
|
613 /* the class.set method, performs the lookup of class attributes */
|
|
614 SWIGINTERN int SWIG_Lua_class_set(lua_State* L)
|
|
615 {
|
|
616 /* there should be 3 params passed in
|
|
617 (1) table (not the meta table)
|
|
618 (2) string name of the attribute
|
|
619 (3) any for the new value
|
|
620 printf("SWIG_Lua_class_set %p(%s) '%s' %p(%s)\n",
|
|
621 lua_topointer(L,1),lua_typename(L,lua_type(L,1)),
|
|
622 lua_tostring(L,2),
|
|
623 lua_topointer(L,3),lua_typename(L,lua_type(L,3)));*/
|
|
624
|
|
625 assert(lua_isuserdata(L,1)); /* just in case */
|
|
626 lua_getmetatable(L,1); /* get the meta table */
|
|
627 assert(lua_istable(L,-1)); /* just in case */
|
|
628
|
|
629 SWIG_Lua_get_table(L,".set"); /* find the .set table */
|
|
630 if (lua_istable(L,-1))
|
|
631 {
|
|
632 /* look for the key in the .set table */
|
|
633 lua_pushvalue(L,2); /* key */
|
|
634 lua_rawget(L,-2);
|
|
635 if (lua_iscfunction(L,-1))
|
|
636 { /* found it so call the fn & return its value */
|
|
637 lua_pushvalue(L,1); /* userdata */
|
|
638 lua_pushvalue(L,3); /* value */
|
|
639 lua_call(L,2,0);
|
|
640 return 0;
|
|
641 }
|
|
642 lua_pop(L,1); /* remove the value */
|
|
643 }
|
|
644 lua_pop(L,1); /* remove the value .set table */
|
|
645 /* NEW: looks for the __setitem() fn
|
|
646 this is a user provided set fn */
|
|
647 SWIG_Lua_get_table(L,"__setitem"); /* find the fn */
|
|
648 if (lua_iscfunction(L,-1)) /* if its there */
|
|
649 { /* found it so call the fn & return its value */
|
|
650 lua_pushvalue(L,1); /* the userdata */
|
|
651 lua_pushvalue(L,2); /* the parameter */
|
|
652 lua_pushvalue(L,3); /* the value */
|
|
653 lua_call(L,3,0); /* 3 values in ,0 out */
|
|
654 lua_remove(L,-2); /* stack tidy, remove metatable */
|
|
655 return 1;
|
|
656 }
|
|
657 return 0;
|
|
658 }
|
|
659
|
|
660 /* the class.destruct method called by the interpreter */
|
|
661 SWIGINTERN int SWIG_Lua_class_destruct(lua_State* L)
|
|
662 {
|
|
663 /* there should be 1 params passed in
|
|
664 (1) userdata (not the meta table) */
|
|
665 swig_lua_userdata* usr;
|
|
666 swig_lua_class* clss;
|
|
667 assert(lua_isuserdata(L,-1)); /* just in case */
|
|
668 usr=(swig_lua_userdata*)lua_touserdata(L,-1); /* get it */
|
|
669 /* if must be destroyed & has a destructor */
|
|
670 if (usr->own) /* if must be destroyed */
|
|
671 {
|
|
672 clss=(swig_lua_class*)usr->type->clientdata; /* get the class */
|
|
673 if (clss && clss->destructor) /* there is a destroy fn */
|
|
674 {
|
|
675 clss->destructor(usr->ptr); /* bye bye */
|
|
676 }
|
|
677 }
|
|
678 return 0;
|
|
679 }
|
|
680
|
|
681 /* the class.__tostring method called by the interpreter and print */
|
|
682 SWIGINTERN int SWIG_Lua_class_tostring(lua_State* L)
|
|
683 {
|
|
684 /* there should be 1 param passed in
|
|
685 (1) userdata (not the metatable) */
|
|
686 assert(lua_isuserdata(L,1)); /* just in case */
|
|
687 unsigned long userData = (unsigned long)lua_touserdata(L,1); /* get the userdata address for later */
|
|
688 lua_getmetatable(L,1); /* get the meta table */
|
|
689 assert(lua_istable(L,-1)); /* just in case */
|
|
690
|
|
691 lua_getfield(L, -1, ".type");
|
|
692 const char* className = lua_tostring(L, -1);
|
|
693
|
|
694 char output[256];
|
|
695 sprintf(output, "<%s userdata: %lX>", className, userData);
|
|
696
|
|
697 lua_pushstring(L, (const char*)output);
|
|
698 return 1;
|
|
699 }
|
|
700
|
|
701 /* to manually disown some userdata */
|
|
702 SWIGINTERN int SWIG_Lua_class_disown(lua_State* L)
|
|
703 {
|
|
704 /* there should be 1 params passed in
|
|
705 (1) userdata (not the meta table) */
|
|
706 swig_lua_userdata* usr;
|
|
707 assert(lua_isuserdata(L,-1)); /* just in case */
|
|
708 usr=(swig_lua_userdata*)lua_touserdata(L,-1); /* get it */
|
|
709
|
|
710 usr->own = 0; /* clear our ownership */
|
|
711 return 0;
|
|
712 }
|
|
713
|
|
714 /* Constructor proxy. Used when class name entry in module is not class constructor,
|
|
715 but special table instead. */
|
|
716 SWIGINTERN int SWIG_Lua_constructor_proxy(lua_State* L)
|
|
717 {
|
|
718 /* unlimited number of parameters
|
|
719 First one is our proxy table and we should remove it
|
|
720 Other we should pass to real constructor
|
|
721 */
|
|
722 assert(lua_istable(L,1));
|
|
723 lua_pushstring(L,".constructor");
|
|
724 lua_rawget(L,1);
|
|
725 assert(!lua_isnil(L,-1));
|
|
726 lua_replace(L,1); /* replace our table with real constructor */
|
|
727 lua_call(L,lua_gettop(L)-1,1);
|
|
728 return 1;
|
|
729 }
|
|
730
|
|
731 /* gets the swig class registry (or creates it) */
|
|
732 SWIGINTERN void SWIG_Lua_get_class_registry(lua_State* L)
|
|
733 {
|
|
734 /* add this all into the swig registry: */
|
|
735 lua_pushstring(L,"SWIG");
|
|
736 lua_rawget(L,LUA_REGISTRYINDEX); /* get the registry */
|
|
737 if (!lua_istable(L,-1)) /* not there */
|
|
738 { /* must be first time, so add it */
|
|
739 lua_pop(L,1); /* remove the result */
|
|
740 lua_pushstring(L,"SWIG");
|
|
741 lua_newtable(L);
|
|
742 lua_rawset(L,LUA_REGISTRYINDEX);
|
|
743 /* then get it */
|
|
744 lua_pushstring(L,"SWIG");
|
|
745 lua_rawget(L,LUA_REGISTRYINDEX);
|
|
746 }
|
|
747 }
|
|
748
|
|
749 /* helper fn to get the classes metatable from the register */
|
|
750 SWIGINTERN void SWIG_Lua_get_class_metatable(lua_State* L,const char* cname)
|
|
751 {
|
|
752 SWIG_Lua_get_class_registry(L); /* get the registry */
|
|
753 lua_pushstring(L,cname); /* get the name */
|
|
754 lua_rawget(L,-2); /* get it */
|
|
755 lua_remove(L,-2); /* tidy up (remove registry) */
|
|
756 }
|
|
757
|
|
758 /* helper add a variable to a registered class */
|
|
759 SWIGINTERN void SWIG_Lua_add_class_variable(lua_State* L,const char* name,lua_CFunction getFn,lua_CFunction setFn)
|
|
760 {
|
|
761 assert(lua_istable(L,-1)); /* just in case */
|
|
762 SWIG_Lua_get_table(L,".get"); /* find the .get table */
|
|
763 assert(lua_istable(L,-1)); /* just in case */
|
|
764 SWIG_Lua_add_function(L,name,getFn);
|
|
765 lua_pop(L,1); /* tidy stack (remove table) */
|
|
766 if (setFn)
|
|
767 {
|
|
768 SWIG_Lua_get_table(L,".set"); /* find the .set table */
|
|
769 assert(lua_istable(L,-1)); /* just in case */
|
|
770 SWIG_Lua_add_function(L,name,setFn);
|
|
771 lua_pop(L,1); /* tidy stack (remove table) */
|
|
772 }
|
|
773 }
|
|
774
|
|
775 /* helper to recursively add class static details (static attributes, operations and constants) */
|
|
776 SWIGINTERN void SWIG_Lua_add_class_static_details(lua_State* L, swig_lua_class* clss)
|
|
777 {
|
|
778 int i = 0;
|
|
779 /* The class namespace table must be on the top of the stack */
|
|
780 assert(lua_istable(L,-1));
|
|
781 /* call all the base classes first: we can then override these later: */
|
|
782 for(i=0;clss->bases[i];i++)
|
|
783 {
|
|
784 SWIG_Lua_add_class_static_details(L,clss->bases[i]);
|
|
785 }
|
|
786
|
|
787 SWIG_Lua_add_namespace_details(L, &clss->cls_static);
|
|
788 }
|
|
789
|
|
790 /* helper to recursively add class details (attributes & operations) */
|
|
791 SWIGINTERN void SWIG_Lua_add_class_details(lua_State* L,swig_lua_class* clss)
|
|
792 {
|
|
793 int i;
|
|
794 /* call all the base classes first: we can then override these later: */
|
|
795 for(i=0;clss->bases[i];i++)
|
|
796 {
|
|
797 SWIG_Lua_add_class_details(L,clss->bases[i]);
|
|
798 }
|
|
799 /* add fns */
|
|
800 for(i=0;clss->attributes[i].name;i++){
|
|
801 SWIG_Lua_add_class_variable(L,clss->attributes[i].name,clss->attributes[i].getmethod,clss->attributes[i].setmethod);
|
|
802 }
|
|
803 /* add methods to the metatable */
|
|
804 SWIG_Lua_get_table(L,".fn"); /* find the .fn table */
|
|
805 assert(lua_istable(L,-1)); /* just in case */
|
|
806 for(i=0;clss->methods[i].name;i++){
|
|
807 SWIG_Lua_add_function(L,clss->methods[i].name,clss->methods[i].method);
|
|
808 }
|
|
809 lua_pop(L,1); /* tidy stack (remove table) */
|
|
810 /* add operator overloads
|
|
811 these look ANY method which start with "__" and assume they
|
|
812 are operator overloads & add them to the metatable
|
|
813 (this might mess up is someone defines a method __gc (the destructor)*/
|
|
814 for(i=0;clss->methods[i].name;i++){
|
|
815 if (clss->methods[i].name[0]=='_' && clss->methods[i].name[1]=='_'){
|
|
816 SWIG_Lua_add_function(L,clss->methods[i].name,clss->methods[i].method);
|
|
817 }
|
|
818 }
|
|
819 }
|
|
820
|
|
821 /* set up the base classes pointers.
|
|
822 Each class structure has a list of pointers to the base class structures.
|
|
823 This function fills them.
|
|
824 It cannot be done at compile time, as this will not work with hireachies
|
|
825 spread over more than one swig file.
|
|
826 Therefore it must be done at runtime, querying the SWIG type system.
|
|
827 */
|
|
828 SWIGINTERN void SWIG_Lua_init_base_class(lua_State* L,swig_lua_class* clss)
|
|
829 {
|
|
830 int i=0;
|
|
831 swig_module_info* module=SWIG_GetModule(L);
|
|
832 for(i=0;clss->base_names[i];i++)
|
|
833 {
|
|
834 if (clss->bases[i]==0) /* not found yet */
|
|
835 {
|
|
836 /* lookup and cache the base class */
|
|
837 swig_type_info *info = SWIG_TypeQueryModule(module,module,clss->base_names[i]);
|
|
838 if (info) clss->bases[i] = (swig_lua_class *) info->clientdata;
|
|
839 }
|
|
840 }
|
|
841 }
|
|
842
|
|
843 /* Register class static methods,attributes etc as well as constructor proxy */
|
|
844 SWIGINTERN void SWIG_Lua_class_register_static(lua_State* L, swig_lua_class* clss)
|
|
845 {
|
|
846 lua_checkstack(L,5); /* just in case */
|
|
847 assert(lua_istable(L,-1)); /* just in case */
|
|
848 assert(strcmp(clss->name, clss->cls_static.name) == 0); /* in class those 2 must be equal */
|
|
849
|
|
850 SWIG_Lua_namespace_register(L,&clss->cls_static);
|
|
851
|
|
852 SWIG_Lua_get_table(L,clss->name); // Get namespace table back
|
|
853 assert(lua_istable(L,-1)); /* just in case */
|
|
854
|
|
855 /* add its constructor to module with the name of the class
|
|
856 so you can do MyClass(...) as well as new_MyClass(...)
|
|
857 BUT only if a constructor is defined
|
|
858 (this overcomes the problem of pure virtual classes without constructors)*/
|
|
859 if (clss->constructor)
|
|
860 {
|
|
861 SWIG_Lua_add_function(L,".constructor", clss->constructor);
|
|
862 lua_getmetatable(L,-1);
|
|
863 assert(lua_istable(L,-1)); /* just in case */
|
|
864 SWIG_Lua_add_function(L,"__call", SWIG_Lua_constructor_proxy);
|
|
865 lua_pop(L,1);
|
|
866 }
|
|
867
|
|
868 assert(lua_istable(L,-1)); /* just in case */
|
|
869 SWIG_Lua_add_class_static_details(L, clss);
|
|
870
|
|
871 /* clear stack */
|
|
872 lua_pop(L,1);
|
|
873 }
|
|
874
|
|
875 /* performs the entire class registration process */
|
|
876 SWIGINTERN void SWIG_Lua_class_register(lua_State* L,swig_lua_class* clss)
|
|
877 {
|
|
878 SWIG_Lua_class_register_static(L,clss);
|
|
879
|
|
880 SWIG_Lua_get_class_registry(L); /* get the registry */
|
|
881 lua_pushstring(L,clss->name); /* get the name */
|
|
882 lua_newtable(L); /* create the metatable */
|
|
883 /* add string of class name called ".type" */
|
|
884 lua_pushstring(L,".type");
|
|
885 lua_pushstring(L,clss->name);
|
|
886 lua_rawset(L,-3);
|
|
887 /* add a table called ".get" */
|
|
888 lua_pushstring(L,".get");
|
|
889 lua_newtable(L);
|
|
890 lua_rawset(L,-3);
|
|
891 /* add a table called ".set" */
|
|
892 lua_pushstring(L,".set");
|
|
893 lua_newtable(L);
|
|
894 lua_rawset(L,-3);
|
|
895 /* add a table called ".fn" */
|
|
896 lua_pushstring(L,".fn");
|
|
897 lua_newtable(L);
|
|
898 /* add manual disown method */
|
|
899 SWIG_Lua_add_function(L,"__disown",SWIG_Lua_class_disown);
|
|
900 lua_rawset(L,-3);
|
|
901 /* add accessor fns for using the .get,.set&.fn */
|
|
902 SWIG_Lua_add_function(L,"__index",SWIG_Lua_class_get);
|
|
903 SWIG_Lua_add_function(L,"__newindex",SWIG_Lua_class_set);
|
|
904 SWIG_Lua_add_function(L,"__gc",SWIG_Lua_class_destruct);
|
|
905 /* add tostring method for better output */
|
|
906 SWIG_Lua_add_function(L,"__tostring",SWIG_Lua_class_tostring);
|
|
907 /* add it */
|
|
908 lua_rawset(L,-3); /* metatable into registry */
|
|
909 lua_pop(L,1); /* tidy stack (remove registry) */
|
|
910
|
|
911 SWIG_Lua_get_class_metatable(L,clss->name);
|
|
912 SWIG_Lua_add_class_details(L,clss); /* recursive adding of details (atts & ops) */
|
|
913 lua_pop(L,1); /* tidy stack (remove class metatable) */
|
|
914 }
|
|
915
|
|
916 /* -----------------------------------------------------------------------------
|
|
917 * Class/structure conversion fns
|
|
918 * ----------------------------------------------------------------------------- */
|
|
919
|
|
920 /* helper to add metatable to new lua object */
|
|
921 SWIGINTERN void _SWIG_Lua_AddMetatable(lua_State* L,swig_type_info *type)
|
|
922 {
|
|
923 if (type->clientdata) /* there is clientdata: so add the metatable */
|
|
924 {
|
|
925 SWIG_Lua_get_class_metatable(L,((swig_lua_class*)(type->clientdata))->name);
|
|
926 if (lua_istable(L,-1))
|
|
927 {
|
|
928 lua_setmetatable(L,-2);
|
|
929 }
|
|
930 else
|
|
931 {
|
|
932 lua_pop(L,1);
|
|
933 }
|
|
934 }
|
|
935 }
|
|
936
|
|
937 /* pushes a new object into the lua stack */
|
|
938 SWIGRUNTIME void SWIG_Lua_NewPointerObj(lua_State* L,void* ptr,swig_type_info *type, int own)
|
|
939 {
|
|
940 swig_lua_userdata* usr;
|
|
941 if (!ptr){
|
|
942 lua_pushnil(L);
|
|
943 return;
|
|
944 }
|
|
945 usr=(swig_lua_userdata*)lua_newuserdata(L,sizeof(swig_lua_userdata)); /* get data */
|
|
946 usr->ptr=ptr; /* set the ptr */
|
|
947 usr->type=type;
|
|
948 usr->own=own;
|
|
949 #if (SWIG_LUA_TARGET != SWIG_LUA_FLAVOR_ELUAC)
|
|
950 _SWIG_Lua_AddMetatable(L,type); /* add metatable */
|
|
951 #endif
|
|
952 }
|
|
953
|
|
954 /* takes a object from the lua stack & converts it into an object of the correct type
|
|
955 (if possible) */
|
|
956 SWIGRUNTIME int SWIG_Lua_ConvertPtr(lua_State* L,int index,void** ptr,swig_type_info *type,int flags)
|
|
957 {
|
|
958 swig_lua_userdata* usr;
|
|
959 swig_cast_info *cast;
|
|
960 if (lua_isnil(L,index)){*ptr=0; return SWIG_OK;} /* special case: lua nil => NULL pointer */
|
|
961 usr=(swig_lua_userdata*)lua_touserdata(L,index); /* get data */
|
|
962 if (usr)
|
|
963 {
|
|
964 if (flags & SWIG_POINTER_DISOWN) /* must disown the object */
|
|
965 {
|
|
966 usr->own=0;
|
|
967 }
|
|
968 if (!type) /* special cast void*, no casting fn */
|
|
969 {
|
|
970 *ptr=usr->ptr;
|
|
971 return SWIG_OK; /* ok */
|
|
972 }
|
|
973 cast=SWIG_TypeCheckStruct(usr->type,type); /* performs normal type checking */
|
|
974 if (cast)
|
|
975 {
|
|
976 int newmemory = 0;
|
|
977 *ptr=SWIG_TypeCast(cast,usr->ptr,&newmemory);
|
|
978 assert(!newmemory); /* newmemory handling not yet implemented */
|
|
979 return SWIG_OK; /* ok */
|
|
980 }
|
|
981 }
|
|
982 return SWIG_ERROR; /* error */
|
|
983 }
|
|
984
|
|
985 SWIGRUNTIME void* SWIG_Lua_MustGetPtr(lua_State* L,int index,swig_type_info *type,int flags,
|
|
986 int argnum,const char* func_name){
|
|
987 void* result;
|
|
988 if (!SWIG_IsOK(SWIG_ConvertPtr(L,index,&result,type,flags))){
|
|
989 luaL_error (L,"Error in %s, expected a %s at argument number %d\n",
|
|
990 func_name,(type && type->str)?type->str:"void*",argnum);
|
|
991 }
|
|
992 return result;
|
|
993 }
|
|
994
|
|
995 /* pushes a packed userdata. user for member fn pointers only */
|
|
996 SWIGRUNTIME void SWIG_Lua_NewPackedObj(lua_State* L,void* ptr,size_t size,swig_type_info *type)
|
|
997 {
|
|
998 swig_lua_rawdata* raw;
|
|
999 assert(ptr); /* not acceptable to pass in a NULL value */
|
|
1000 raw=(swig_lua_rawdata*)lua_newuserdata(L,sizeof(swig_lua_rawdata)-1+size); /* alloc data */
|
|
1001 raw->type=type;
|
|
1002 raw->own=0;
|
|
1003 memcpy(raw->data,ptr,size); /* copy the data */
|
|
1004 _SWIG_Lua_AddMetatable(L,type); /* add metatable */
|
|
1005 }
|
|
1006
|
|
1007 /* converts a packed userdata. user for member fn pointers only */
|
|
1008 SWIGRUNTIME int SWIG_Lua_ConvertPacked(lua_State* L,int index,void* ptr,size_t size,swig_type_info *type)
|
|
1009 {
|
|
1010 swig_lua_rawdata* raw;
|
|
1011 raw=(swig_lua_rawdata*)lua_touserdata(L,index); /* get data */
|
|
1012 if (!raw) return SWIG_ERROR; /* error */
|
|
1013 if (type==0 || type==raw->type) /* void* or identical type */
|
|
1014 {
|
|
1015 memcpy(ptr,raw->data,size); /* copy it */
|
|
1016 return SWIG_OK; /* ok */
|
|
1017 }
|
|
1018 return SWIG_ERROR; /* error */
|
|
1019 }
|
|
1020
|
|
1021 /* a function to get the typestring of a piece of data */
|
|
1022 SWIGRUNTIME const char *SWIG_Lua_typename(lua_State *L, int tp)
|
|
1023 {
|
|
1024 swig_lua_userdata* usr;
|
|
1025 if (lua_isuserdata(L,tp))
|
|
1026 {
|
|
1027 usr=(swig_lua_userdata*)lua_touserdata(L,tp); /* get data */
|
|
1028 if (usr && usr->type && usr->type->str)
|
|
1029 return usr->type->str;
|
|
1030 return "userdata (unknown type)";
|
|
1031 }
|
|
1032 return lua_typename(L,lua_type(L,tp));
|
|
1033 }
|
|
1034
|
|
1035 /* lua callable function to get the userdata's type */
|
|
1036 SWIGRUNTIME int SWIG_Lua_type(lua_State* L)
|
|
1037 {
|
|
1038 lua_pushstring(L,SWIG_Lua_typename(L,1));
|
|
1039 return 1;
|
|
1040 }
|
|
1041
|
|
1042 /* lua callable function to compare userdata's value
|
|
1043 the issue is that two userdata may point to the same thing
|
|
1044 but to lua, they are different objects */
|
|
1045 SWIGRUNTIME int SWIG_Lua_equal(lua_State* L)
|
|
1046 {
|
|
1047 int result;
|
|
1048 swig_lua_userdata *usr1,*usr2;
|
|
1049 if (!lua_isuserdata(L,1) || !lua_isuserdata(L,2)) /* just in case */
|
|
1050 return 0; /* nil reply */
|
|
1051 usr1=(swig_lua_userdata*)lua_touserdata(L,1); /* get data */
|
|
1052 usr2=(swig_lua_userdata*)lua_touserdata(L,2); /* get data */
|
|
1053 /*result=(usr1->ptr==usr2->ptr && usr1->type==usr2->type); only works if type is the same*/
|
|
1054 result=(usr1->ptr==usr2->ptr);
|
|
1055 lua_pushboolean(L,result);
|
|
1056 return 1;
|
|
1057 }
|
|
1058
|
|
1059 /* -----------------------------------------------------------------------------
|
|
1060 * global variable support code: class/struct typemap functions
|
|
1061 * ----------------------------------------------------------------------------- */
|
|
1062
|
|
1063 #if ((SWIG_LUA_TARGET != SWIG_LUA_FLAVOR_ELUA) && (SWIG_LUA_TARGET != SWIG_LUA_FLAVOR_ELUAC))
|
|
1064 /* Install Constants */
|
|
1065 SWIGINTERN void
|
|
1066 SWIG_Lua_InstallConstants(lua_State* L, swig_lua_const_info constants[]) {
|
|
1067 int i;
|
|
1068 for (i = 0; constants[i].type; i++) {
|
|
1069 switch(constants[i].type) {
|
|
1070 case SWIG_LUA_INT:
|
|
1071 lua_pushstring(L,constants[i].name);
|
|
1072 lua_pushnumber(L,(lua_Number)constants[i].lvalue);
|
|
1073 lua_rawset(L,-3);
|
|
1074 break;
|
|
1075 case SWIG_LUA_FLOAT:
|
|
1076 lua_pushstring(L,constants[i].name);
|
|
1077 lua_pushnumber(L,(lua_Number)constants[i].dvalue);
|
|
1078 lua_rawset(L,-3);
|
|
1079 break;
|
|
1080 case SWIG_LUA_CHAR:
|
|
1081 lua_pushstring(L,constants[i].name);
|
|
1082 lua_pushfstring(L,"%c",(char)constants[i].lvalue);
|
|
1083 lua_rawset(L,-3);
|
|
1084 break;
|
|
1085 case SWIG_LUA_STRING:
|
|
1086 lua_pushstring(L,constants[i].name);
|
|
1087 lua_pushstring(L,(char *) constants[i].pvalue);
|
|
1088 lua_rawset(L,-3);
|
|
1089 break;
|
|
1090 case SWIG_LUA_POINTER:
|
|
1091 lua_pushstring(L,constants[i].name);
|
|
1092 SWIG_NewPointerObj(L,constants[i].pvalue, *(constants[i]).ptype,0);
|
|
1093 lua_rawset(L,-3);
|
|
1094 break;
|
|
1095 case SWIG_LUA_BINARY:
|
|
1096 lua_pushstring(L,constants[i].name);
|
|
1097 SWIG_NewMemberObj(L,constants[i].pvalue,constants[i].lvalue,*(constants[i]).ptype);
|
|
1098 lua_rawset(L,-3);
|
|
1099 break;
|
|
1100 default:
|
|
1101 break;
|
|
1102 }
|
|
1103 }
|
|
1104 }
|
|
1105 #endif
|
|
1106
|
|
1107 /* -----------------------------------------------------------------------------
|
|
1108 * executing lua code from within the wrapper
|
|
1109 * ----------------------------------------------------------------------------- */
|
|
1110
|
|
1111 #ifndef SWIG_DOSTRING_FAIL /* Allows redefining of error function */
|
|
1112 #define SWIG_DOSTRING_FAIL(S) fprintf(stderr,"%s\n",S)
|
|
1113 #endif
|
|
1114 /* Executes a C string in Lua which is a really simple way of calling lua from C
|
|
1115 Unfortunately lua keeps changing its APIs, so we need a conditional compile
|
|
1116 In lua 5.0.X its lua_dostring()
|
|
1117 In lua 5.1.X its luaL_dostring()
|
|
1118 */
|
|
1119 SWIGINTERN int
|
|
1120 SWIG_Lua_dostring(lua_State *L, const char* str) {
|
|
1121 int ok,top;
|
|
1122 if (str==0 || str[0]==0) return 0; /* nothing to do */
|
|
1123 top=lua_gettop(L); /* save stack */
|
|
1124 #if (defined(LUA_VERSION_NUM) && (LUA_VERSION_NUM>=501))
|
|
1125 ok=luaL_dostring(L,str); /* looks like this is lua 5.1.X or later, good */
|
|
1126 #else
|
|
1127 ok=lua_dostring(L,str); /* might be lua 5.0.x, using lua_dostring */
|
|
1128 #endif
|
|
1129 if (ok!=0) {
|
|
1130 SWIG_DOSTRING_FAIL(lua_tostring(L,-1));
|
|
1131 }
|
|
1132 lua_settop(L,top); /* restore the stack */
|
|
1133 return ok;
|
|
1134 }
|
|
1135
|
|
1136 #ifdef __cplusplus
|
|
1137 }
|
|
1138 #endif
|
|
1139
|
|
1140 /* ------------------------------ end luarun.swg ------------------------------ */
|