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