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