Home | History | Annotate | Download | only in lua
      1 /* -----------------------------------------------------------------------------
      2  * lua.swg
      3  *
      4  * SWIG Configuration File for Lua.
      5  * This file is parsed by SWIG before reading any other interface file.
      6  * ----------------------------------------------------------------------------- */
      7 
      8 /* -----------------------------------------------------------------------------
      9  *                          includes
     10  * ----------------------------------------------------------------------------- */
     11 
     12 %include <luatypemaps.swg>          /* The typemaps */
     13 %include <luaruntime.swg>          /* The runtime stuff */
     14 
     15 //%include <typemaps/swigmacros.swg>
     16 /* -----------------------------------------------------------------------------
     17  *                          constants typemaps
     18  * ----------------------------------------------------------------------------- */
     19 // this basically adds to a table of constants
     20 %typemap(consttab) int, unsigned int, short, unsigned short, long, unsigned long, unsigned char, signed char, bool, enum SWIGTYPE
     21        {SWIG_LUA_CONSTTAB_INT("$symname", $value)}
     22 
     23 %typemap(consttab) float, double
     24        {SWIG_LUA_CONSTTAB_FLOAT("$symname", $value)}
     25 
     26 %typemap(consttab) long long, unsigned long long, signed long long
     27        {SWIG_LUA_CONSTTAB_FLOAT("$symname", $value)}
     28 
     29 %typemap(consttab) const long long&, const unsigned long long&, const signed long long&
     30        {SWIG_LUA_CONSTTAB_FLOAT("$symname", *$value)}
     31 
     32 %typemap(consttab) char *, const char *, char [], const char []
     33        {SWIG_LUA_CONSTTAB_STRING("$symname", $value)}
     34 
     35 // note: char is treated as a seperate special type
     36 // signed char & unsigned char are numbers
     37 %typemap(consttab) char
     38        {SWIG_LUA_CONSTTAB_CHAR("$symname", $value)}
     39 
     40 %typemap(consttab) long long, unsigned long long
     41        {SWIG_LUA_CONSTTAB_STRING("$symname", "$value")}
     42 
     43 %typemap(consttab) SWIGTYPE *, SWIGTYPE *const, SWIGTYPE &, SWIGTYPE []
     44        { SWIG_LUA_POINTER, (char *)"$symname", 0, 0, (void *)$value, &$1_descriptor}
     45 
     46 // member function pointers
     47 %typemap(consttab) SWIGTYPE (CLASS::*)
     48        { SWIG_LUA_BINARY,  (char *)"$symname", sizeof($type), 0, (void *)&$value, &$1_descriptor}
     49 
     50 
     51 /* -----------------------------------------------------------------------------
     52  *                          Overloaded operator support
     53  * ----------------------------------------------------------------------------- */
     54 // lua calls the + operator '__add'
     55 // python likes to call it '__add__'
     56 // Assuming most SWIGers will probably use the __add__ if they extend their classes
     57 // we have two sets of renames
     58 // one to rename the operator+() to __add()
     59 //	(this lets SWIG rename the operator overloads)
     60 // another is to rename __add__() to __add()
     61 //	(this means that people who wrote SWIG code to do that add will also work)
     62 
     63 #ifdef __cplusplus
     64 // this is extra renaming for lua
     65 // not all operators are supported, so only those that are, are listed
     66 %rename(__add)			*::operator+;
     67 %rename(__sub)			*::operator-;
     68 %rename(__mul)			*::operator*;
     69 %rename(__div)			*::operator/;
     70 %rename(__unm)      *::operator-();
     71 %rename(__unm)      *::operator-() const;
     72 
     73 %rename(__eq)			*::operator==;
     74 %ignore *::operator!=;      // note: Lua does not have a notequal operator
     75 						// it just uses 'not (a==b)'
     76 %rename(__lt)			*::operator<;
     77 %ignore *::operator>;   	// ditto less than vs greater than
     78 %rename(__le)			*::operator<=;
     79 %ignore *::operator>=;  // ditto less than vs greater than
     80 %ignore *::operator!;  // does not support not
     81 
     82 %rename(__call)			*::operator();	// the fn call operator
     83 
     84 // lua does not support overloading of:
     85 // 	logical/bitwise operators
     86 // 	assign operator
     87 // 	+=,-=,*=, etc
     88 // therefore ignoring them for now
     89 // it also doesn't support non class operators
     90 // eg friends or XX operator+(XX,XX)
     91 // also ignoring
     92 // note: some of these might be better to rename, but not doing that for now
     93 %ignore *::operator&&;	%ignore operator&&;
     94 %ignore *::operator||;	%ignore operator||;
     95 %ignore *::operator+=;
     96 %ignore *::operator-=;
     97 %ignore *::operator*=;
     98 %ignore *::operator/=;
     99 %ignore *::operator%=;
    100 %ignore *::operator++;	%ignore *::operator--;
    101 
    102 %ignore *::operator=;	// note: this might be better to rename to assign() or similar
    103 
    104 %ignore operator+;
    105 %ignore operator-;
    106 %ignore operator*;
    107 %ignore operator/;
    108 %ignore operator%;
    109 %ignore operator[];
    110 %ignore operator>;	%ignore operator>=;
    111 %ignore operator<;	%ignore operator<=;
    112 %ignore operator==;	%ignore operator!=;
    113 
    114 
    115 // renaming the python operators to be compatible with lua
    116 // this means that if a developer has written a fn __add__()
    117 // it will be used for the lua +
    118 %rename(__add)			*::__add__;
    119 %rename(__sub)			*::__sub__;
    120 %rename(__mul)			*::__mul__;
    121 %rename(__div)			*::__div__;
    122 %rename(__unm)			*::__neg__;		// lua calls unary minus,'unm' not 'neg'
    123 %rename(__tostring)		*::__str__;		// both map to __tostring
    124 %rename(__tostring)		*::__repr__;	// both map to __tostring
    125 
    126 
    127 %rename(__pow)			*::__pow__;		// lua power '^' operator
    128 %rename(__concat)		*::__concat__;  // lua concat '..' operator
    129 %rename(__eq)			*::__eq__;
    130 %rename(__lt)			*::__lt__;
    131 %rename(__le)			*::__le__;
    132 %rename(__call)			*::__call__;	// the fn call operator()
    133 
    134 // the [] operator has two parts, the get & the set
    135 %rename(__getitem)			*::__getitem__;	// the v=X[i] (get operator)
    136 %rename(__setitem)			*::__setitem__;	// the X[i]=v (set operator)
    137 
    138 
    139 #endif
    140 
    141 
    142 /* ------------------------------------------------------------
    143  *                              Exceptions
    144  * ------------------------------------------------------------ */
    145 /* Confession: I don't really like C++ exceptions
    146 The python/lua ones are great, but C++ ones I don't like
    147 (mainly because I cannot get the stack trace out of it)
    148 Therefore I have not bothered to try doing much in this
    149 
    150 Therefore currently its just enough to get a few test cases running ok
    151 
    152 note: if you wish to throw anything related to std::exception
    153 use %include <std_except.i> instead
    154 */
    155 
    156 // number as number+error
    157 %typemap(throws) int,unsigned int,signed int,
    158 				long,unsigned long,signed long,
    159 				short,unsigned short,signed short,
    160 				float,double,
    161 				long long,unsigned long long,
    162 				unsigned char, signed char,
    163                 int&,unsigned int&,signed int&,
    164 				long&,unsigned long&,signed long&,
    165 				short&,unsigned short&,signed short&,
    166 				float&,double&,
    167 				long long&,unsigned long long&,
    168 				unsigned char&, signed char&
    169 %{lua_pushnumber(L,(lua_Number)$1);SWIG_fail; %}
    170 
    171 %typemap(throws) bool,bool&
    172 %{lua_pushboolean(L,(int)($1==true));SWIG_fail; %}
    173 
    174 // enum as number+error
    175 %typemap(throws) enum SWIGTYPE
    176 %{lua_pushnumber(L,(lua_Number)(int)$1);SWIG_fail; %}
    177 
    178 // strings are just sent as errors
    179 %typemap(throws) char *, const char *
    180 %{lua_pushstring(L,$1);SWIG_fail;%}
    181 
    182 // char is changed to a string
    183 %typemap(throws) char
    184 %{lua_pushfstring(L,"%c",$1);SWIG_fail;%}
    185 
    186 /*
    187 Throwing object is a serious problem:
    188 Assuming some code throws a 'FooBar'
    189 There are a few options:
    190 - return a pointer to it: but its unclear how long this will last for.
    191 - return a copy of it: but not all objects are copyable
    192 	(see exception_partial_info in the test suite for a case where you cannot do this)
    193 - convert to a string & throw that
    194 	it's not so useful, but it works (this is more lua like).
    195 The third option (though not nice) is used
    196 For a more useful solution: see std_except for more details
    197 */
    198 
    199 // basic typemap for structs, classes, pointers & references
    200 // convert to string and error
    201 %typemap(throws) SWIGTYPE
    202 %{(void)$1; /* ignore it */
    203 lua_pushfstring(L,"object exception:%s",SWIG_TypePrettyName($1_descriptor));
    204 SWIG_fail;%}
    205 
    206 // code to make a copy of the object and return this
    207 // if you have a function which throws a FooBar & you want SWIG to return a copy of the object as its error
    208 // then use one of the below
    209 //	%apply SWIGTYPE EXCEPTION_BY_VAL {FooBar};
    210 //	%apply SWIGTYPE& EXCEPTION_BY_VAL {FooBar&}; // note: need & twice
    211 %typemap(throws) SWIGTYPE EXCEPTION_BY_VAL
    212 %{SWIG_NewPointerObj(L,(void *)new $1_ltype(($1_ltype &) $1),$&1_descriptor,1);
    213 SWIG_fail;%}
    214 
    215 // similar for object reference
    216 // note: swig typemaps seem a little confused around here, therefore we use $basetype
    217 %typemap(throws) SWIGTYPE& EXCEPTION_BY_VAL
    218 %{SWIG_NewPointerObj(L,(void *)new $basetype($1),$1_descriptor,1);
    219 SWIG_fail;%}
    220 
    221 
    222 // note: no support for object pointers
    223 // its not clear how long the pointer is valid for, therefore not supporting it
    224 
    225 /* -----------------------------------------------------------------------------
    226  *                          extras
    227  * ----------------------------------------------------------------------------- */
    228 // this %define is to allow insertion of lua source code into the wrapper file
    229 #define %luacode  %insert("luacode")
    230 
    231 
    232 /* ------------------------------ end lua.swg  ------------------------------ */
    233