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