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.
7 * basic typemaps for Lua.
8 * ----------------------------------------------------------------------------- */
10 /* -----------------------------------------------------------------------------
12 * ----------------------------------------------------------------------------- */
14 the 'checkfn' param is something that I added for typemap(in)
15 it is an optional fn call to check the type of the lua object
16 the fn call must be of the form
17 int checkfn(lua_State *L, int index);
18 and return 1/0 depending upon if this is the correct type
19 For the typemap(out), an additional SWIG_arg parmeter must be incremented
20 to reflect the number of values returned (normally SWIG_arg++; will do)
23 %typemap(in,checkfn="lua_isnumber") int, short, long,
24 signed char, float, double
25 %{$1 = ($type)lua_tonumber(L, $input);%}
27 // additional check for unsigned numbers, to not permit negative input
28 %typemap(in,checkfn="lua_isnumber") unsigned int,
29 unsigned short, unsigned long, unsigned char
30 %{SWIG_contract_assert((lua_tonumber(L,$input)>=0),"number must not be negative")
31 $1 = ($type)lua_tonumber(L, $input);%}
33 %typemap(out) int,short,long,
34 unsigned int,unsigned short,unsigned long,
35 signed char,unsigned char,
37 %{ lua_pushnumber(L, (lua_Number) $1); SWIG_arg++;%}
39 // we must also provide typemaps for privitives by const reference:
41 // int intbyref(const int& i);
42 // SWIG assumes that this code will need a pointer to int to be passed in
43 // (this might be ok for objects by const ref, but not for numeric primitives)
44 // therefore we add a set of typemaps to fix this (for both in & out)
45 %typemap(in,checkfn="lua_isnumber") const int&($basetype temp)
46 %{ temp=($basetype)lua_tonumber(L,$input); $1=&temp;%}
48 %typemap(in,checkfn="lua_isnumber") const unsigned int&($basetype temp)
49 %{SWIG_contract_assert((lua_tonumber(L,$input)>=0),"number must not be negative")
50 temp=($basetype)lua_tonumber(L,$input); $1=&temp;%}
52 %typemap(out) const int&, const unsigned int&
53 %{ lua_pushnumber(L, (lua_Number) *$1); SWIG_arg++;%}
55 // for the other numbers we can just use an apply statement to cover them
56 %apply const int & {const short&,const long&,const signed char&,
57 const float&,const double&};
59 %apply const unsigned int & {const unsigned short&,const unsigned long&,
60 const unsigned char&};
62 /* enums have to be handled slightly differently
63 VC++ .net will not allow a cast from lua_Number(double) to enum directly.
65 %typemap(in,checkfn="lua_isnumber") enum SWIGTYPE
66 %{$1 = ($type)(int)lua_tonumber(L, $input);%}
68 %typemap(out) enum SWIGTYPE
69 %{ lua_pushnumber(L, (lua_Number)(int)($1)); SWIG_arg++;%}
72 %typemap(in,checkfn="lua_isnumber") const enum SWIGTYPE &($basetype temp)
73 %{ temp=($basetype)(int)lua_tonumber(L,$input); $1=&temp;%}
74 %typemap(out) const enum SWIGTYPE &
75 %{ lua_pushnumber(L, (lua_Number) *$1); SWIG_arg++;%}
78 // boolean (which is a special type in lua)
79 // note: lua_toboolean() returns 1 or 0
80 // note: 1 & 0 are not booleans in lua, only true & false
81 %typemap(in,checkfn="lua_isboolean") bool
82 %{$1 = (lua_toboolean(L, $input)!=0);%}
85 %{ lua_pushboolean(L,(int)($1!=0)); SWIG_arg++;%}
87 // for const bool&, SWIG treats this as a const bool* so we must dereference it
88 %typemap(in,checkfn="lua_isboolean") const bool& (bool temp)
89 %{temp=(lua_toboolean(L, $input)!=0);
92 %typemap(out) const bool&
93 %{ lua_pushboolean(L,(int)((*$1)!=0)); SWIG_arg++;%}
95 // strings (char* and char[])
96 %typemap(in,checkfn="lua_isstring") const char*, char*
97 %{$1 = ($ltype)lua_tostring(L, $input);%}
99 %typemap(in,checkfn="lua_isstring") const char[ANY], char[ANY]
100 %{$1 = ($ltype)lua_tostring(L, $input);%}
102 %typemap(out) const char*, char*
103 %{ lua_pushstring(L,(const char*)$1); SWIG_arg++;%}
105 %typemap(out) const char[ANY], char[ANY]
106 %{ lua_pushstring(L,(const char*)$1); SWIG_arg++;%}
109 // currently treating chars as small strings, not as numbers
110 // (however signed & unsigned char's are numbers...)
111 %typemap(in,checkfn="lua_isstring") char
112 %{$1 = (lua_tostring(L, $input))[0];%}
115 %{ lua_pushfstring(L,"%c",$1); SWIG_arg++;%}
118 %typemap(in,checkfn="lua_isstring") const char& (char temp)
119 %{temp = (lua_tostring(L, $input))[0]; $1=&temp;%}
121 %typemap(out) const char&
122 %{ lua_pushfstring(L,"%c",*$1); SWIG_arg++;%}
124 // pointers and references
125 // under SWIG rules, it is ok, to have a pass in a lua nil,
126 // it should be converted to a SWIG NULL.
127 // This will only be allowed for pointers & arrays, not refs or by value
128 // the checkfn lua_isuserdata will only work for userdata
129 // the checkfn SWIG_isptrtype will work for both userdata and nil's
130 %typemap(in,checkfn="SWIG_isptrtype") SWIGTYPE*,SWIGTYPE[]
132 if (!SWIG_IsOK(SWIG_ConvertPtr(L,$input,(void**)&$1,$descriptor,$disown))){
133 SWIG_fail_ptr("$symname",$argnum,$descriptor);
137 %typemap(in,checkfn="lua_isuserdata") SWIGTYPE&
139 if (!SWIG_IsOK(SWIG_ConvertPtr(L,$input,(void**)&$1,$descriptor,$disown))){
140 SWIG_fail_ptr("$symname",$argnum,$descriptor);
145 %typemap(out) SWIGTYPE*,SWIGTYPE&
146 %{SWIG_NewPointerObj(L,$1,$descriptor,$owner); SWIG_arg++; %}
149 // this uses the SWIG_TypeDynamicCast() which relies on RTTI to find out what the pointer really is
150 // the we return it as the correct type
151 %typemap(out) SWIGTYPE *DYNAMIC,
154 swig_type_info *ty = SWIG_TypeDynamicCast($1_descriptor, (void **) &$1);
155 SWIG_NewPointerObj(L,(void*)$1,ty,$owner); SWIG_arg++;
159 // passing objects by value
160 // SWIG_ConvertPtr wants an object pointer (the $<ype argp)
161 // then dereferences it to get the object
162 %typemap(in,checkfn="lua_isuserdata") SWIGTYPE ($<ype argp)
164 if (!SWIG_IsOK(SWIG_ConvertPtr(L,$input,(void**)&argp,$&descriptor,0))){
165 SWIG_fail_ptr("$symname",$argnum,$&descriptor);
170 // Also needed for object ptrs by const ref
171 // eg A* const& ref_pointer(A* const& a);
172 // found in mixed_types.i
173 %typemap(in,checkfn="lua_isuserdata") SWIGTYPE *&($*ltype temp)
174 %{temp=($*ltype)SWIG_MustGetPtr(L,$input,$*descriptor,0,$argnum,"$symname");
177 %typemap(out) SWIGTYPE *&
178 %{SWIG_NewPointerObj(L,*$1,$*descriptor,$owner); SWIG_arg++; %}
181 // DISOWN-ing typemaps
182 // if you have an object pointer which must be disowned, use this typemap
183 // eg. for void destroy_foo(Foo* toDie);
184 // use %apply SWIGTYPE* DISOWN {Foo* toDie};
185 // you could just use %delobject, but this is more flexible
186 %typemap(in,checkfn="SWIG_isptrtype") SWIGTYPE* DISOWN,SWIGTYPE DISOWN[]
187 %{ if (!SWIG_IsOK(SWIG_ConvertPtr(L,$input,(void**)&$1,$descriptor,SWIG_POINTER_DISOWN))){
188 SWIG_fail_ptr("$symname",$argnum,$descriptor);
193 // Primitive types--return by value
194 // must make a new object, copy the data & return the new object
195 // Note: the brackets are {...} and not %{..%}, because we want them to be included in the wrapper
196 // this is because typemap(out) does not support local variables, like in typemap(in) does
197 // and we need the $&1_ltype resultptr; to be declared
199 %typemap(out) SWIGTYPE
201 $&1_ltype resultptr = new $1_ltype((const $1_ltype &) $1);
202 SWIG_NewPointerObj(L,(void *) resultptr,$&1_descriptor,1); SWIG_arg++;
205 %typemap(out) SWIGTYPE
208 resultptr = ($&1_ltype) malloc(sizeof($1_type));
209 memmove(resultptr, &$1, sizeof($1_type));
210 SWIG_NewPointerObj(L,(void *) resultptr,$&1_descriptor,1); SWIG_arg++;
214 // member function pointer
215 // a member fn ptr is not 4 bytes like a normal pointer, but 8 bytes (at least on mingw)
216 // so the standard wrappering cannot be done
217 // nor can you cast a member function pointer to a void* (obviously)
218 // therefore a special wrappering functions SWIG_ConvertMember() & SWIG_NewMemberObj() were written
220 %typemap(in,checkfn="lua_isuserdata") SWIGTYPE (CLASS::*)
222 if (!SWIG_IsOK(SWIG_ConvertMember(L,$input,(void*)(&$1),sizeof($type),$descriptor)))
223 SWIG_fail_ptr("$symname",$argnum,$descriptor);
226 %typemap(out) SWIGTYPE (CLASS::*)
228 SWIG_NewMemberObj(L,(void*)(&$1),sizeof($type),$descriptor); SWIG_arg++;
233 // void (must be empty without the SWIG_arg++)
234 %typemap(out) void "";
236 /* void* is a special case
237 A function void fn(void*) should take any kind of pointer as a parameter (just like C/C++ does)
238 but if its an output, then it should be wrappered like any other SWIG object (using default typemap)
240 %typemap(in,checkfn="SWIG_isptrtype") void*
241 %{$1=($1_ltype)SWIG_MustGetPtr(L,$input,0,0,$argnum,"$symname");%}
243 /* long long is another special case:
244 as lua only supports one numeric type (lua_Number), we will just
245 cast it to that & accept the loss of precision.
246 An alternative solution would be a long long struct or class
247 with the relevant operators.
249 %apply long {long long, signed long long, unsigned long long};
250 %apply const long& {const long long&, const signed long long&, const unsigned long long&};
252 /* It is possible to also pass a lua_State* into a function, so
253 void fn(int a, float b, lua_State* s) is wrappable as
254 > fn(1,4.3) -- note: the state is implicitly passed in
256 %typemap(in, numinputs=0) lua_State*
261 /* -----------------------------------------------------------------------------
263 * ----------------------------------------------------------------------------- */
264 /* These are needed for the overloaded functions
265 These define the detection routines which will spot what
266 parmeters match which function
269 // unfortunately lua only considers one type of number
270 // so all numbers (int,float,double) match
271 // you could add an advanced fn to get type & check if its integral
272 %typecheck(SWIG_TYPECHECK_INTEGER)
274 unsigned int, unsigned short, unsigned long,
275 signed char, unsigned char,
276 long long, unsigned long long, signed long long,
277 const int &, const short &, const long &,
278 const unsigned int &, const unsigned short &, const unsigned long &,
279 const signed char&, const unsigned char&,
280 const long long &, const unsigned long long &,
281 enum SWIGTYPE, const enum SWIGTYPE&,
282 float, double, const float &, const double&
284 $1 = lua_isnumber(L,$input);
287 %typecheck(SWIG_TYPECHECK_BOOL)
290 $1 = lua_isboolean(L,$input);
293 // special check for a char (string of length 1)
294 %typecheck(SWIG_TYPECHECK_CHAR) char, const char& {
295 $1 = lua_isstring(L,$input) && (lua_strlen(L,$input)==1);
298 %typecheck(SWIG_TYPECHECK_STRING) char *, char[] {
299 $1 = lua_isstring(L,$input);
302 %typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE *, SWIGTYPE [] {
304 if (SWIG_isptrtype(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, $1_descriptor, 0)) {
311 %typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE & {
313 if (lua_isuserdata(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, $1_descriptor, 0)) {
320 %typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE {
322 if (lua_isuserdata(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, $&1_descriptor, 0)) {
329 %typecheck(SWIG_TYPECHECK_VOIDPTR) void * {
331 if (SWIG_isptrtype(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, 0, 0)) {
338 // Also needed for object ptrs by const ref
339 // eg const A* ref_pointer(A* const& a);
340 // found in mixed_types.i
341 %typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE* const &
344 if (lua_isuserdata(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, $*descriptor, 0)) {
351 /* -----------------------------------------------------------------------------
353 * ----------------------------------------------------------------------------- */
355 // Array reference typemaps
356 %apply SWIGTYPE & { SWIGTYPE ((&)[ANY]) }
358 // size_t (which is just a unsigned long)
359 %apply unsigned long { size_t };
360 %apply const unsigned long & { const size_t & };
363 /* -----------------------------------------------------------------------------
365 * ----------------------------------------------------------------------------- */
366 // swig::LANGUAGE_OBJ was added to allow containers of native objects
367 // however its rather difficult to do this in lua, as you cannot hold pointers
368 // to native objects (they are held in the interpreter)
369 // therefore for now: just ignoring this feature
371 %ignore swig::LANGUAGE_OBJ;
376 typedef struct{} LANGUAGE_OBJ;
380 #endif // __cplusplus