PageRenderTime 30ms CodeModel.GetById 21ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

/trunk/Lib/lua/luarun.swg

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