PageRenderTime 78ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Lib/lua/luarun.swg

#
Unknown | 818 lines | 759 code | 59 blank | 0 comment | 0 complexity | 9695de2dff84c7253134a00931cd6012 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  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. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. #include "lua.h"
  12. #include "lauxlib.h"
  13. #include <stdlib.h> /* for malloc */
  14. #include <assert.h> /* for a few sanity tests */
  15. /* -----------------------------------------------------------------------------
  16. * global swig types
  17. * ----------------------------------------------------------------------------- */
  18. /* Constant table */
  19. #define SWIG_LUA_INT 1
  20. #define SWIG_LUA_FLOAT 2
  21. #define SWIG_LUA_STRING 3
  22. #define SWIG_LUA_POINTER 4
  23. #define SWIG_LUA_BINARY 5
  24. #define SWIG_LUA_CHAR 6
  25. /* Structure for variable linking table */
  26. typedef struct {
  27. const char *name;
  28. lua_CFunction get;
  29. lua_CFunction set;
  30. } swig_lua_var_info;
  31. /* Constant information structure */
  32. typedef struct {
  33. int type;
  34. char *name;
  35. long lvalue;
  36. double dvalue;
  37. void *pvalue;
  38. swig_type_info **ptype;
  39. } swig_lua_const_info;
  40. typedef struct {
  41. const char *name;
  42. lua_CFunction method;
  43. } swig_lua_method;
  44. typedef struct {
  45. const char *name;
  46. lua_CFunction getmethod;
  47. lua_CFunction setmethod;
  48. } swig_lua_attribute;
  49. typedef struct swig_lua_class {
  50. const char *name;
  51. swig_type_info **type;
  52. lua_CFunction constructor;
  53. void (*destructor)(void *);
  54. swig_lua_method *methods;
  55. swig_lua_attribute *attributes;
  56. struct swig_lua_class **bases;
  57. const char **base_names;
  58. } swig_lua_class;
  59. /* this is the struct for wrappering all pointers in SwigLua
  60. */
  61. typedef struct {
  62. swig_type_info *type;
  63. int own; /* 1 if owned & must be destroyed */
  64. void *ptr;
  65. } swig_lua_userdata;
  66. /* this is the struct for wrapping arbitary packed binary data
  67. (currently it is only used for member function pointers)
  68. the data ordering is similar to swig_lua_userdata, but it is currently not possible
  69. to tell the two structures apart within SWIG, other than by looking at the type
  70. */
  71. typedef struct {
  72. swig_type_info *type;
  73. int own; /* 1 if owned & must be destroyed */
  74. char data[1]; /* arbitary amount of data */
  75. } swig_lua_rawdata;
  76. /* Common SWIG API */
  77. #define SWIG_NewPointerObj(L, ptr, type, owner) SWIG_Lua_NewPointerObj(L, (void *)ptr, type, owner)
  78. #define SWIG_ConvertPtr(L,idx, ptr, type, flags) SWIG_Lua_ConvertPtr(L,idx,ptr,type,flags)
  79. #define SWIG_MustGetPtr(L,idx, type,flags, argnum,fnname) SWIG_Lua_MustGetPtr(L,idx, type,flags, argnum,fnname)
  80. /* for C++ member pointers, ie, member methods */
  81. #define SWIG_ConvertMember(L, idx, ptr, sz, ty) SWIG_Lua_ConvertPacked(L, idx, ptr, sz, ty)
  82. #define SWIG_NewMemberObj(L, ptr, sz, type) SWIG_Lua_NewPackedObj(L, ptr, sz, type)
  83. /* Runtime API */
  84. #define SWIG_GetModule(clientdata) SWIG_Lua_GetModule((lua_State*)(clientdata))
  85. #define SWIG_SetModule(clientdata, pointer) SWIG_Lua_SetModule((lua_State*) (clientdata), pointer)
  86. #define SWIG_MODULE_CLIENTDATA_TYPE lua_State*
  87. /* Contract support */
  88. #define SWIG_contract_assert(expr, msg) \
  89. if (!(expr)) { lua_pushstring(L, (char *) msg); goto fail; } else
  90. /* helper #defines */
  91. #define SWIG_fail {goto fail;}
  92. #define SWIG_fail_arg(func_name,argnum,type) \
  93. {lua_pushfstring(L,"Error in %s (arg %d), expected '%s' got '%s'",\
  94. func_name,argnum,type,SWIG_Lua_typename(L,argnum));\
  95. goto fail;}
  96. #define SWIG_fail_ptr(func_name,argnum,type) \
  97. SWIG_fail_arg(func_name,argnum,(type && type->str)?type->str:"void*")
  98. #define SWIG_check_num_args(func_name,a,b) \
  99. if (lua_gettop(L)<a || lua_gettop(L)>b) \
  100. {lua_pushfstring(L,"Error in %s expected %d..%d args, got %d",func_name,a,b,lua_gettop(L));\
  101. goto fail;}
  102. #define SWIG_Lua_get_table(L,n) \
  103. (lua_pushstring(L, n), lua_rawget(L,-2))
  104. #define SWIG_Lua_add_function(L,n,f) \
  105. (lua_pushstring(L, n), \
  106. lua_pushcfunction(L, f), \
  107. lua_rawset(L,-3))
  108. /* special helper for allowing 'nil' for usertypes */
  109. #define SWIG_isptrtype(L,I) (lua_isuserdata(L,I) || lua_isnil(L,I))
  110. #ifdef __cplusplus
  111. /* Special helper for member function pointers
  112. it gets the address, casts it, then dereferences it */
  113. //#define SWIG_mem_fn_as_voidptr(a) (*((char**)&(a)))
  114. #endif
  115. /* storing/access of swig_module_info */
  116. SWIGRUNTIME swig_module_info *
  117. SWIG_Lua_GetModule(lua_State* L) {
  118. swig_module_info *ret = 0;
  119. lua_pushstring(L,"swig_runtime_data_type_pointer" SWIG_RUNTIME_VERSION SWIG_TYPE_TABLE_NAME);
  120. lua_rawget(L,LUA_REGISTRYINDEX);
  121. if (lua_islightuserdata(L,-1))
  122. ret=(swig_module_info*)lua_touserdata(L,-1);
  123. lua_pop(L,1); /* tidy */
  124. return ret;
  125. }
  126. SWIGRUNTIME void
  127. SWIG_Lua_SetModule(lua_State* L, swig_module_info *module) {
  128. /* add this all into the Lua registry: */
  129. lua_pushstring(L,"swig_runtime_data_type_pointer" SWIG_RUNTIME_VERSION SWIG_TYPE_TABLE_NAME);
  130. lua_pushlightuserdata(L,(void*)module);
  131. lua_rawset(L,LUA_REGISTRYINDEX);
  132. }
  133. /* -----------------------------------------------------------------------------
  134. * global variable support code: modules
  135. * ----------------------------------------------------------------------------- */
  136. /* this function is called when trying to set an immutable.
  137. default value is to print an error.
  138. This can removed with a compile flag SWIGLUA_IGNORE_SET_IMMUTABLE */
  139. SWIGINTERN int SWIG_Lua_set_immutable(lua_State* L)
  140. {
  141. /* there should be 1 param passed in: the new value */
  142. #ifndef SWIGLUA_IGNORE_SET_IMMUTABLE
  143. lua_pop(L,1); /* remove it */
  144. lua_pushstring(L,"This variable is immutable");
  145. lua_error(L);
  146. #endif
  147. return 0; /* should not return anything */
  148. }
  149. /* the module.get method used for getting linked data */
  150. SWIGINTERN int SWIG_Lua_module_get(lua_State* L)
  151. {
  152. /* there should be 2 params passed in
  153. (1) table (not the meta table)
  154. (2) string name of the attribute
  155. printf("SWIG_Lua_module_get %p(%s) '%s'\n",
  156. lua_topointer(L,1),lua_typename(L,lua_type(L,1)),
  157. lua_tostring(L,2));
  158. */
  159. /* get the metatable */
  160. #if ((SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) || (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUAC))
  161. assert(lua_isrotable(L,1)); /* just in case */
  162. #else
  163. assert(lua_istable(L,1)); /* default Lua action */
  164. #endif
  165. lua_getmetatable(L,1); /* get the metatable */
  166. #if ((SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) || (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUAC))
  167. assert(lua_isrotable(L,-1)); /* just in case */
  168. #else
  169. assert(lua_istable(L,-1));
  170. #endif
  171. SWIG_Lua_get_table(L,".get"); /* get the .get table */
  172. lua_remove(L,3); /* remove metatable */
  173. #if ((SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) || (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUAC))
  174. if (lua_isrotable(L,-1))
  175. #else
  176. if (lua_istable(L,-1))
  177. #endif
  178. {
  179. /* look for the key in the .get table */
  180. lua_pushvalue(L,2); /* key */
  181. lua_rawget(L,-2);
  182. lua_remove(L,3); /* remove .get */
  183. if (lua_iscfunction(L,-1))
  184. { /* found it so call the fn & return its value */
  185. lua_call(L,0,1);
  186. return 1;
  187. }
  188. lua_pop(L,1); /* remove the top */
  189. }
  190. lua_pop(L,1); /* remove the .get */
  191. lua_pushnil(L); /* return a nil */
  192. return 1;
  193. }
  194. /* the module.set method used for setting linked data */
  195. SWIGINTERN int SWIG_Lua_module_set(lua_State* L)
  196. {
  197. /* there should be 3 params passed in
  198. (1) table (not the meta table)
  199. (2) string name of the attribute
  200. (3) any for the new value
  201. */
  202. /* get the metatable */
  203. #if ((SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) || (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUAC))
  204. assert(lua_isrotable(L,1)); /* just in case */
  205. #else
  206. assert(lua_istable(L,1)); /* default Lua action */
  207. #endif
  208. lua_getmetatable(L,1); /* get the metatable */
  209. #if ((SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) || (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUAC))
  210. assert(lua_isrotable(L,-1)); /* just in case */
  211. #else
  212. assert(lua_istable(L,-1));
  213. #endif
  214. SWIG_Lua_get_table(L,".set"); /* get the .set table */
  215. lua_remove(L,4); /* remove metatable */
  216. #if ((SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) || (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUAC))
  217. if (lua_isrotable(L,-1))
  218. #else
  219. if (lua_istable(L,-1))
  220. #endif
  221. {
  222. /* look for the key in the .set table */
  223. lua_pushvalue(L,2); /* key */
  224. lua_rawget(L,-2);
  225. lua_remove(L,4); /* remove .set */
  226. if (lua_iscfunction(L,-1))
  227. { /* found it so call the fn & return its value */
  228. lua_pushvalue(L,3); /* value */
  229. lua_call(L,1,0);
  230. return 0;
  231. }
  232. #if (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA)
  233. else {
  234. return 0; // Exits stoically if an invalid key is initialized.
  235. }
  236. #endif
  237. }
  238. lua_settop(L,3); /* reset back to start */
  239. /* we now have the table, key & new value, so just set directly */
  240. lua_rawset(L,1); /* add direct */
  241. return 0;
  242. }
  243. #if ((SWIG_LUA_TARGET != SWIG_LUA_FLAVOR_ELUA) && (SWIG_LUA_TARGET != SWIG_LUA_FLAVOR_ELUAC))
  244. /* registering a module in lua. Pushes the module table on the stack. */
  245. SWIGINTERN void SWIG_Lua_module_begin(lua_State* L,const char* name)
  246. {
  247. assert(lua_istable(L,-1)); /* just in case */
  248. lua_pushstring(L,name);
  249. lua_newtable(L); /* the table */
  250. /* add meta table */
  251. lua_newtable(L); /* the meta table */
  252. SWIG_Lua_add_function(L,"__index",SWIG_Lua_module_get);
  253. SWIG_Lua_add_function(L,"__newindex",SWIG_Lua_module_set);
  254. lua_pushstring(L,".get");
  255. lua_newtable(L); /* the .get table */
  256. lua_rawset(L,-3); /* add .get into metatable */
  257. lua_pushstring(L,".set");
  258. lua_newtable(L); /* the .set table */
  259. lua_rawset(L,-3); /* add .set into metatable */
  260. lua_setmetatable(L,-2); /* sets meta table in module */
  261. #ifdef SWIG_LUA_MODULE_GLOBAL
  262. /* If requested, install the module directly into the global namespace. */
  263. lua_rawset(L,-3); /* add module into parent */
  264. SWIG_Lua_get_table(L,name); /* get the table back out */
  265. #else
  266. /* Do not install the module table as global name. The stack top has
  267. the module table with the name below. We pop the top and replace
  268. the name with it. */
  269. lua_replace(L,-2);
  270. #endif
  271. }
  272. /* ending the register */
  273. SWIGINTERN void SWIG_Lua_module_end(lua_State* L)
  274. {
  275. lua_pop(L,1); /* tidy stack (remove module) */
  276. }
  277. /* adding a linked variable to the module */
  278. SWIGINTERN void SWIG_Lua_module_add_variable(lua_State* L,const char* name,lua_CFunction getFn,lua_CFunction setFn)
  279. {
  280. assert(lua_istable(L,-1)); /* just in case */
  281. lua_getmetatable(L,-1); /* get the metatable */
  282. assert(lua_istable(L,-1)); /* just in case */
  283. SWIG_Lua_get_table(L,".get"); /* find the .get table */
  284. assert(lua_istable(L,-1)); /* should be a table: */
  285. SWIG_Lua_add_function(L,name,getFn);
  286. lua_pop(L,1); /* tidy stack (remove table) */
  287. if (setFn) /* if there is a set fn */
  288. {
  289. SWIG_Lua_get_table(L,".set"); /* find the .set table */
  290. assert(lua_istable(L,-1)); /* should be a table: */
  291. SWIG_Lua_add_function(L,name,setFn);
  292. lua_pop(L,1); /* tidy stack (remove table) */
  293. }
  294. lua_pop(L,1); /* tidy stack (remove meta) */
  295. }
  296. #endif
  297. /* adding a function module */
  298. SWIGINTERN void SWIG_Lua_module_add_function(lua_State* L,const char* name,lua_CFunction fn)
  299. {
  300. SWIG_Lua_add_function(L,name,fn);
  301. }
  302. /* -----------------------------------------------------------------------------
  303. * global variable support code: classes
  304. * ----------------------------------------------------------------------------- */
  305. /* the class.get method, performs the lookup of class attributes */
  306. SWIGINTERN int SWIG_Lua_class_get(lua_State* L)
  307. {
  308. /* there should be 2 params passed in
  309. (1) userdata (not the meta table)
  310. (2) string name of the attribute
  311. */
  312. assert(lua_isuserdata(L,-2)); /* just in case */
  313. lua_getmetatable(L,-2); /* get the meta table */
  314. assert(lua_istable(L,-1)); /* just in case */
  315. SWIG_Lua_get_table(L,".get"); /* find the .get table */
  316. assert(lua_istable(L,-1)); /* just in case */
  317. /* look for the key in the .get table */
  318. lua_pushvalue(L,2); /* key */
  319. lua_rawget(L,-2);
  320. lua_remove(L,-2); /* stack tidy, remove .get table */
  321. if (lua_iscfunction(L,-1))
  322. { /* found it so call the fn & return its value */
  323. lua_pushvalue(L,1); /* the userdata */
  324. lua_call(L,1,1); /* 1 value in (userdata),1 out (result) */
  325. lua_remove(L,-2); /* stack tidy, remove metatable */
  326. return 1;
  327. }
  328. lua_pop(L,1); /* remove whatever was there */
  329. /* ok, so try the .fn table */
  330. SWIG_Lua_get_table(L,".fn"); /* find the .get table */
  331. assert(lua_istable(L,-1)); /* just in case */
  332. lua_pushvalue(L,2); /* key */
  333. lua_rawget(L,-2); /* look for the fn */
  334. lua_remove(L,-2); /* stack tidy, remove .fn table */
  335. if (lua_isfunction(L,-1)) /* note: if its a C function or lua function */
  336. { /* found it so return the fn & let lua call it */
  337. lua_remove(L,-2); /* stack tidy, remove metatable */
  338. return 1;
  339. }
  340. lua_pop(L,1); /* remove whatever was there */
  341. /* NEW: looks for the __getitem() fn
  342. this is a user provided get fn */
  343. SWIG_Lua_get_table(L,"__getitem"); /* find the __getitem fn */
  344. if (lua_iscfunction(L,-1)) /* if its there */
  345. { /* found it so call the fn & return its value */
  346. lua_pushvalue(L,1); /* the userdata */
  347. lua_pushvalue(L,2); /* the parameter */
  348. lua_call(L,2,1); /* 2 value in (userdata),1 out (result) */
  349. lua_remove(L,-2); /* stack tidy, remove metatable */
  350. return 1;
  351. }
  352. return 0; /* sorry not known */
  353. }
  354. /* the class.set method, performs the lookup of class attributes */
  355. SWIGINTERN int SWIG_Lua_class_set(lua_State* L)
  356. {
  357. /* there should be 3 params passed in
  358. (1) table (not the meta table)
  359. (2) string name of the attribute
  360. (3) any for the new value
  361. printf("SWIG_Lua_class_set %p(%s) '%s' %p(%s)\n",
  362. lua_topointer(L,1),lua_typename(L,lua_type(L,1)),
  363. lua_tostring(L,2),
  364. lua_topointer(L,3),lua_typename(L,lua_type(L,3)));*/
  365. assert(lua_isuserdata(L,1)); /* just in case */
  366. lua_getmetatable(L,1); /* get the meta table */
  367. assert(lua_istable(L,-1)); /* just in case */
  368. SWIG_Lua_get_table(L,".set"); /* find the .set table */
  369. if (lua_istable(L,-1))
  370. {
  371. /* look for the key in the .set table */
  372. lua_pushvalue(L,2); /* key */
  373. lua_rawget(L,-2);
  374. if (lua_iscfunction(L,-1))
  375. { /* found it so call the fn & return its value */
  376. lua_pushvalue(L,1); /* userdata */
  377. lua_pushvalue(L,3); /* value */
  378. lua_call(L,2,0);
  379. return 0;
  380. }
  381. lua_pop(L,1); /* remove the value */
  382. }
  383. lua_pop(L,1); /* remove the value .set table */
  384. /* NEW: looks for the __setitem() fn
  385. this is a user provided set fn */
  386. SWIG_Lua_get_table(L,"__setitem"); /* find the fn */
  387. if (lua_iscfunction(L,-1)) /* if its there */
  388. { /* found it so call the fn & return its value */
  389. lua_pushvalue(L,1); /* the userdata */
  390. lua_pushvalue(L,2); /* the parameter */
  391. lua_pushvalue(L,3); /* the value */
  392. lua_call(L,3,0); /* 3 values in ,0 out */
  393. lua_remove(L,-2); /* stack tidy, remove metatable */
  394. return 1;
  395. }
  396. return 0;
  397. }
  398. /* the class.destruct method called by the interpreter */
  399. SWIGINTERN int SWIG_Lua_class_destruct(lua_State* L)
  400. {
  401. /* there should be 1 params passed in
  402. (1) userdata (not the meta table) */
  403. swig_lua_userdata* usr;
  404. swig_lua_class* clss;
  405. assert(lua_isuserdata(L,-1)); /* just in case */
  406. usr=(swig_lua_userdata*)lua_touserdata(L,-1); /* get it */
  407. /* if must be destroyed & has a destructor */
  408. if (usr->own) /* if must be destroyed */
  409. {
  410. clss=(swig_lua_class*)usr->type->clientdata; /* get the class */
  411. if (clss && clss->destructor) /* there is a destroy fn */
  412. {
  413. clss->destructor(usr->ptr); /* bye bye */
  414. }
  415. }
  416. return 0;
  417. }
  418. /* gets the swig class registry (or creates it) */
  419. SWIGINTERN void SWIG_Lua_get_class_registry(lua_State* L)
  420. {
  421. /* add this all into the swig registry: */
  422. lua_pushstring(L,"SWIG");
  423. lua_rawget(L,LUA_REGISTRYINDEX); /* get the registry */
  424. if (!lua_istable(L,-1)) /* not there */
  425. { /* must be first time, so add it */
  426. lua_pop(L,1); /* remove the result */
  427. lua_pushstring(L,"SWIG");
  428. lua_newtable(L);
  429. lua_rawset(L,LUA_REGISTRYINDEX);
  430. /* then get it */
  431. lua_pushstring(L,"SWIG");
  432. lua_rawget(L,LUA_REGISTRYINDEX);
  433. }
  434. }
  435. /* helper fn to get the classes metatable from the register */
  436. SWIGINTERN void SWIG_Lua_get_class_metatable(lua_State* L,const char* cname)
  437. {
  438. SWIG_Lua_get_class_registry(L); /* get the registry */
  439. lua_pushstring(L,cname); /* get the name */
  440. lua_rawget(L,-2); /* get it */
  441. lua_remove(L,-2); /* tidy up (remove registry) */
  442. }
  443. /* helper add a variable to a registered class */
  444. SWIGINTERN void SWIG_Lua_add_class_variable(lua_State* L,const char* name,lua_CFunction getFn,lua_CFunction setFn)
  445. {
  446. assert(lua_istable(L,-1)); /* just in case */
  447. SWIG_Lua_get_table(L,".get"); /* find the .get table */
  448. assert(lua_istable(L,-1)); /* just in case */
  449. SWIG_Lua_add_function(L,name,getFn);
  450. lua_pop(L,1); /* tidy stack (remove table) */
  451. if (setFn)
  452. {
  453. SWIG_Lua_get_table(L,".set"); /* find the .set table */
  454. assert(lua_istable(L,-1)); /* just in case */
  455. SWIG_Lua_add_function(L,name,setFn);
  456. lua_pop(L,1); /* tidy stack (remove table) */
  457. }
  458. }
  459. /* helper to recursively add class details (attributes & operations) */
  460. SWIGINTERN void SWIG_Lua_add_class_details(lua_State* L,swig_lua_class* clss)
  461. {
  462. int i;
  463. /* call all the base classes first: we can then override these later: */
  464. for(i=0;clss->bases[i];i++)
  465. {
  466. SWIG_Lua_add_class_details(L,clss->bases[i]);
  467. }
  468. /* add fns */
  469. for(i=0;clss->attributes[i].name;i++){
  470. SWIG_Lua_add_class_variable(L,clss->attributes[i].name,clss->attributes[i].getmethod,clss->attributes[i].setmethod);
  471. }
  472. /* add methods to the metatable */
  473. SWIG_Lua_get_table(L,".fn"); /* find the .fn table */
  474. assert(lua_istable(L,-1)); /* just in case */
  475. for(i=0;clss->methods[i].name;i++){
  476. SWIG_Lua_add_function(L,clss->methods[i].name,clss->methods[i].method);
  477. }
  478. lua_pop(L,1); /* tidy stack (remove table) */
  479. /* add operator overloads
  480. these look ANY method which start with "__" and assume they
  481. are operator overloads & add them to the metatable
  482. (this might mess up is someone defines a method __gc (the destructor)*/
  483. for(i=0;clss->methods[i].name;i++){
  484. if (clss->methods[i].name[0]=='_' && clss->methods[i].name[1]=='_'){
  485. SWIG_Lua_add_function(L,clss->methods[i].name,clss->methods[i].method);
  486. }
  487. }
  488. }
  489. /* set up the base classes pointers.
  490. Each class structure has a list of pointers to the base class structures.
  491. This function fills them.
  492. It cannot be done at compile time, as this will not work with hireachies
  493. spread over more than one swig file.
  494. Therefore it must be done at runtime, querying the SWIG type system.
  495. */
  496. SWIGINTERN void SWIG_Lua_init_base_class(lua_State* L,swig_lua_class* clss)
  497. {
  498. int i=0;
  499. swig_module_info* module=SWIG_GetModule(L);
  500. for(i=0;clss->base_names[i];i++)
  501. {
  502. if (clss->bases[i]==0) /* not found yet */
  503. {
  504. /* lookup and cache the base class */
  505. swig_type_info *info = SWIG_TypeQueryModule(module,module,clss->base_names[i]);
  506. if (info) clss->bases[i] = (swig_lua_class *) info->clientdata;
  507. }
  508. }
  509. }
  510. /* performs the entire class registration process */
  511. SWIGINTERN void SWIG_Lua_class_register(lua_State* L,swig_lua_class* clss)
  512. {
  513. /* add its constructor to module with the name of the class
  514. so you can do MyClass(...) as well as new_MyClass(...)
  515. BUT only if a constructor is defined
  516. (this overcomes the problem of pure virtual classes without constructors)*/
  517. if (clss->constructor)
  518. SWIG_Lua_add_function(L,clss->name,clss->constructor);
  519. SWIG_Lua_get_class_registry(L); /* get the registry */
  520. lua_pushstring(L,clss->name); /* get the name */
  521. lua_newtable(L); /* create the metatable */
  522. /* add string of class name called ".type" */
  523. lua_pushstring(L,".type");
  524. lua_pushstring(L,clss->name);
  525. lua_rawset(L,-3);
  526. /* add a table called ".get" */
  527. lua_pushstring(L,".get");
  528. lua_newtable(L);
  529. lua_rawset(L,-3);
  530. /* add a table called ".set" */
  531. lua_pushstring(L,".set");
  532. lua_newtable(L);
  533. lua_rawset(L,-3);
  534. /* add a table called ".fn" */
  535. lua_pushstring(L,".fn");
  536. lua_newtable(L);
  537. lua_rawset(L,-3);
  538. /* add accessor fns for using the .get,.set&.fn */
  539. SWIG_Lua_add_function(L,"__index",SWIG_Lua_class_get);
  540. SWIG_Lua_add_function(L,"__newindex",SWIG_Lua_class_set);
  541. SWIG_Lua_add_function(L,"__gc",SWIG_Lua_class_destruct);
  542. /* add it */
  543. lua_rawset(L,-3); /* metatable into registry */
  544. lua_pop(L,1); /* tidy stack (remove registry) */
  545. SWIG_Lua_get_class_metatable(L,clss->name);
  546. SWIG_Lua_add_class_details(L,clss); /* recursive adding of details (atts & ops) */
  547. lua_pop(L,1); /* tidy stack (remove class metatable) */
  548. }
  549. /* -----------------------------------------------------------------------------
  550. * Class/structure conversion fns
  551. * ----------------------------------------------------------------------------- */
  552. /* helper to add metatable to new lua object */
  553. SWIGINTERN void _SWIG_Lua_AddMetatable(lua_State* L,swig_type_info *type)
  554. {
  555. if (type->clientdata) /* there is clientdata: so add the metatable */
  556. {
  557. SWIG_Lua_get_class_metatable(L,((swig_lua_class*)(type->clientdata))->name);
  558. if (lua_istable(L,-1))
  559. {
  560. lua_setmetatable(L,-2);
  561. }
  562. else
  563. {
  564. lua_pop(L,1);
  565. }
  566. }
  567. }
  568. /* pushes a new object into the lua stack */
  569. SWIGRUNTIME void SWIG_Lua_NewPointerObj(lua_State* L,void* ptr,swig_type_info *type, int own)
  570. {
  571. swig_lua_userdata* usr;
  572. if (!ptr){
  573. lua_pushnil(L);
  574. return;
  575. }
  576. usr=(swig_lua_userdata*)lua_newuserdata(L,sizeof(swig_lua_userdata)); /* get data */
  577. usr->ptr=ptr; /* set the ptr */
  578. usr->type=type;
  579. usr->own=own;
  580. #if (SWIG_LUA_TARGET != SWIG_LUA_FLAVOR_ELUAC)
  581. _SWIG_Lua_AddMetatable(L,type); /* add metatable */
  582. #endif
  583. }
  584. /* takes a object from the lua stack & converts it into an object of the correct type
  585. (if possible) */
  586. SWIGRUNTIME int SWIG_Lua_ConvertPtr(lua_State* L,int index,void** ptr,swig_type_info *type,int flags)
  587. {
  588. swig_lua_userdata* usr;
  589. swig_cast_info *cast;
  590. if (lua_isnil(L,index)){*ptr=0; return SWIG_OK;} /* special case: lua nil => NULL pointer */
  591. usr=(swig_lua_userdata*)lua_touserdata(L,index); /* get data */
  592. if (usr)
  593. {
  594. if (flags & SWIG_POINTER_DISOWN) /* must disown the object */
  595. {
  596. usr->own=0;
  597. }
  598. if (!type) /* special cast void*, no casting fn */
  599. {
  600. *ptr=usr->ptr;
  601. return SWIG_OK; /* ok */
  602. }
  603. cast=SWIG_TypeCheckStruct(usr->type,type); /* performs normal type checking */
  604. if (cast)
  605. {
  606. int newmemory = 0;
  607. *ptr=SWIG_TypeCast(cast,usr->ptr,&newmemory);
  608. assert(!newmemory); /* newmemory handling not yet implemented */
  609. return SWIG_OK; /* ok */
  610. }
  611. }
  612. return SWIG_ERROR; /* error */
  613. }
  614. SWIGRUNTIME void* SWIG_Lua_MustGetPtr(lua_State* L,int index,swig_type_info *type,int flags,
  615. int argnum,const char* func_name){
  616. void* result;
  617. if (!SWIG_IsOK(SWIG_ConvertPtr(L,index,&result,type,flags))){
  618. lua_pushfstring(L,"Error in %s, expected a %s at argument number %d\n",
  619. func_name,(type && type->str)?type->str:"void*",argnum);
  620. lua_error(L);
  621. }
  622. return result;
  623. }
  624. /* pushes a packed userdata. user for member fn pointers only */
  625. SWIGRUNTIME void SWIG_Lua_NewPackedObj(lua_State* L,void* ptr,size_t size,swig_type_info *type)
  626. {
  627. swig_lua_rawdata* raw;
  628. assert(ptr); /* not acceptable to pass in a NULL value */
  629. raw=(swig_lua_rawdata*)lua_newuserdata(L,sizeof(swig_lua_rawdata)-1+size); /* alloc data */
  630. raw->type=type;
  631. raw->own=0;
  632. memcpy(raw->data,ptr,size); /* copy the data */
  633. _SWIG_Lua_AddMetatable(L,type); /* add metatable */
  634. }
  635. /* converts a packed userdata. user for member fn pointers only */
  636. SWIGRUNTIME int SWIG_Lua_ConvertPacked(lua_State* L,int index,void* ptr,size_t size,swig_type_info *type)
  637. {
  638. swig_lua_rawdata* raw;
  639. raw=(swig_lua_rawdata*)lua_touserdata(L,index); /* get data */
  640. if (!raw) return SWIG_ERROR; /* error */
  641. if (type==0 || type==raw->type) /* void* or identical type */
  642. {
  643. memcpy(ptr,raw->data,size); /* copy it */
  644. return SWIG_OK; /* ok */
  645. }
  646. return SWIG_ERROR; /* error */
  647. }
  648. /* a function to get the typestring of a piece of data */
  649. SWIGRUNTIME const char *SWIG_Lua_typename(lua_State *L, int tp)
  650. {
  651. swig_lua_userdata* usr;
  652. if (lua_isuserdata(L,tp))
  653. {
  654. usr=(swig_lua_userdata*)lua_touserdata(L,tp); /* get data */
  655. if (usr && usr->type && usr->type->str)
  656. return usr->type->str;
  657. return "userdata (unknown type)";
  658. }
  659. return lua_typename(L,lua_type(L,tp));
  660. }
  661. /* lua callable function to get the userdata's type */
  662. SWIGRUNTIME int SWIG_Lua_type(lua_State* L)
  663. {
  664. lua_pushstring(L,SWIG_Lua_typename(L,1));
  665. return 1;
  666. }
  667. /* lua callable function to compare userdata's value
  668. the issue is that two userdata may point to the same thing
  669. but to lua, they are different objects */
  670. SWIGRUNTIME int SWIG_Lua_equal(lua_State* L)
  671. {
  672. int result;
  673. swig_lua_userdata *usr1,*usr2;
  674. if (!lua_isuserdata(L,1) || !lua_isuserdata(L,2)) /* just in case */
  675. return 0; /* nil reply */
  676. usr1=(swig_lua_userdata*)lua_touserdata(L,1); /* get data */
  677. usr2=(swig_lua_userdata*)lua_touserdata(L,2); /* get data */
  678. /*result=(usr1->ptr==usr2->ptr && usr1->type==usr2->type); only works if type is the same*/
  679. result=(usr1->ptr==usr2->ptr);
  680. lua_pushboolean(L,result);
  681. return 1;
  682. }
  683. /* -----------------------------------------------------------------------------
  684. * global variable support code: class/struct typemap functions
  685. * ----------------------------------------------------------------------------- */
  686. #if ((SWIG_LUA_TARGET != SWIG_LUA_FLAVOR_ELUA) && (SWIG_LUA_TARGET != SWIG_LUA_FLAVOR_ELUAC))
  687. /* Install Constants */
  688. SWIGINTERN void
  689. SWIG_Lua_InstallConstants(lua_State* L, swig_lua_const_info constants[]) {
  690. int i;
  691. for (i = 0; constants[i].type; i++) {
  692. switch(constants[i].type) {
  693. case SWIG_LUA_INT:
  694. lua_pushstring(L,constants[i].name);
  695. lua_pushnumber(L,(lua_Number)constants[i].lvalue);
  696. lua_rawset(L,-3);
  697. break;
  698. case SWIG_LUA_FLOAT:
  699. lua_pushstring(L,constants[i].name);
  700. lua_pushnumber(L,(lua_Number)constants[i].dvalue);
  701. lua_rawset(L,-3);
  702. break;
  703. case SWIG_LUA_CHAR:
  704. lua_pushstring(L,constants[i].name);
  705. lua_pushfstring(L,"%c",(char)constants[i].lvalue);
  706. lua_rawset(L,-3);
  707. break;
  708. case SWIG_LUA_STRING:
  709. lua_pushstring(L,constants[i].name);
  710. lua_pushstring(L,(char *) constants[i].pvalue);
  711. lua_rawset(L,-3);
  712. break;
  713. case SWIG_LUA_POINTER:
  714. lua_pushstring(L,constants[i].name);
  715. SWIG_NewPointerObj(L,constants[i].pvalue, *(constants[i]).ptype,0);
  716. lua_rawset(L,-3);
  717. break;
  718. case SWIG_LUA_BINARY:
  719. lua_pushstring(L,constants[i].name);
  720. SWIG_NewMemberObj(L,constants[i].pvalue,constants[i].lvalue,*(constants[i]).ptype);
  721. lua_rawset(L,-3);
  722. break;
  723. default:
  724. break;
  725. }
  726. }
  727. }
  728. #endif
  729. /* -----------------------------------------------------------------------------
  730. * executing lua code from within the wrapper
  731. * ----------------------------------------------------------------------------- */
  732. #ifndef SWIG_DOSTRING_FAIL /* Allows redefining of error function */
  733. #define SWIG_DOSTRING_FAIL(S) fprintf(stderr,"%s\n",S)
  734. #endif
  735. /* Executes a C string in Lua a really simple way of calling lua from C
  736. Unfortunately lua keeps changing its API's, so we need a conditional compile
  737. In lua 5.0.X its lua_dostring()
  738. In lua 5.1.X its luaL_dostring()
  739. */
  740. SWIGINTERN int
  741. SWIG_Lua_dostring(lua_State *L, const char* str) {
  742. int ok,top;
  743. if (str==0 || str[0]==0) return 0; /* nothing to do */
  744. top=lua_gettop(L); /* save stack */
  745. #if (defined(LUA_VERSION_NUM) && (LUA_VERSION_NUM>=501))
  746. ok=luaL_dostring(L,str); /* looks like this is lua 5.1.X or later, good */
  747. #else
  748. ok=lua_dostring(L,str); /* might be lua 5.0.x, using lua_dostring */
  749. #endif
  750. if (ok!=0) {
  751. SWIG_DOSTRING_FAIL(lua_tostring(L,-1));
  752. }
  753. lua_settop(L,top); /* restore the stack */
  754. return ok;
  755. }
  756. #ifdef __cplusplus
  757. }
  758. #endif
  759. /* ------------------------------ end luarun.swg ------------------------------ */