1
/***********************************************************************
4
* SWIG Configuration File for Lua
5
* This file is parsed by SWIG before reading any other interface
8
* Author : Mark Gossage (mark@gossage.cjb.net)
9
************************************************************************/
11
/* -----------------------------------------------------------------------------
13
* ----------------------------------------------------------------------------- */
15
%runtime "swigrun.swg"; /* Common C API type-checking code */
16
%runtime "luarun.swg"; /* Lua runtime stuff */
18
/* -----------------------------------------------------------------------------
20
* ----------------------------------------------------------------------------- */
22
the 'checkfn' param is something that I added for typemap(in)
23
it is an optional fn call to check the type of the lua object
24
the fn call must be of the form
25
int checkfn(lua_State *L, int index);
26
and return 1/0 depending upon if this is the correct type
27
For the typemap(out), an additional SWIG_arg parmeter must be incremented
28
to reflect the number of values returned (normally SWIG_arg++; will do)
31
%typemap(in,checkfn="lua_isnumber") int,short,long,
32
unsigned int,unsigned short,unsigned long,
33
signed char,unsigned char,
34
float,double,enum SWIGTYPE
35
%{$1 = ($type)lua_tonumber(L, $input);%}
37
%typemap(out) int,short,long,
38
unsigned int,unsigned short,unsigned long,
39
signed char,unsigned char,
40
float,double,enum SWIGTYPE
41
%{ lua_pushnumber(L, (double) $1); SWIG_arg++;%}
43
// boolean (which is a special type in lua)
44
// note: 1 & 0 are not booleans in lua, only true & false
45
%typemap(in,checkfn="lua_isboolean") bool
46
%{$1 = (bool)lua_toboolean(L, $input);%}
48
%typemap(out) bool, const bool&
49
%{ lua_pushboolean(L,(int)$1); SWIG_arg++;%}
51
// for const bool&, SWIG treats this as a const bool* so we must dereference it
52
%typemap(in,checkfn="lua_isboolean") const bool& (bool temp)
53
%{temp=(bool)lua_toboolean(L, $input); $1=&temp;%}
55
%typemap(out) const bool&
56
%{ lua_pushboolean(L,(int)*$1); SWIG_arg++;%}
58
// strings (char* and char[])
59
%typemap(in,checkfn="lua_isstring") const char*, char*, const char[ANY], char[ANY]
60
%{$1 = (char*)lua_tostring(L, $input);%}
62
%typemap(out) const char*, char*, const char[ANY], char[ANY]
63
%{ lua_pushstring(L,$1); SWIG_arg++;%}
66
// currently treating chars as small strings, not as numbers
67
// (however signed & unsigned char's are numbers...)
68
%typemap(in,checkfn="lua_isstring") char
69
%{$1 = ((char*)lua_tostring(L, $input))[0];%}
72
%{ lua_pushfstring(L,"%c",$1); SWIG_arg++;%}
75
%typemap(in,checkfn="lua_isstring") const char& (char temp)
76
%{temp = ((char*)lua_tostring(L, $input))[0]; $1=&temp;%}
78
%typemap(out) const char&
79
%{ lua_pushfstring(L,"%c",*$1); SWIG_arg++;%}
82
// pointers and references
83
%typemap(in,checkfn="lua_isuserdata") SWIGTYPE*,SWIGTYPE&,SWIGTYPE[]
84
%{$1=($1_ltype)SWIG_MustGetPtr(L,$input,$descriptor,0,$argnum,"$symname");%}
86
%typemap(out) SWIGTYPE*,SWIGTYPE&
87
%{SWIG_NewPointerObj(L,$1,$descriptor,$owner); SWIG_arg++; %}
89
// passing objects by value (yuk: do you really have to do this?)
90
// what we do is get it as a pointer (the $<ype argp)
91
// then do an assignment
92
%typemap(in,checkfn="lua_isuserdata") SWIGTYPE
95
if(SWIG_ConvertPtr(L,$input,(void**)(&argp),$&descriptor,SWIG_POINTER_EXCEPTION) == -1) SWIG_fail;
99
// Primitive types--return by value
100
// must make a new object, copy the data & return the new object
102
%typemap(out) SWIGTYPE
105
resultptr = new $1_ltype(($1_ltype &) $1);
106
SWIG_NewPointerObj(L,(void *) resultptr,$&1_descriptor,1); SWIG_arg++;
109
%typemap(out) SWIGTYPE
112
resultptr = ($&1_ltype) malloc(sizeof($1_type));
113
memmove(resultptr, &$1, sizeof($1_type));
114
SWIG_NewPointerObj(L,(void *) resultptr,$&1_descriptor,1); SWIG_arg++;
118
// void (must be empty without the SWIG_arg++)
119
%typemap(out) void "";
122
/* -----------------------------------------------------------------------------
124
* ----------------------------------------------------------------------------- */
125
// this basically adds to a table of constants
126
%typemap(consttab) int, unsigned int, short, unsigned short, long, unsigned long, unsigned char, signed char, bool, enum SWIGTYPE
127
{ SWIG_LUA_INT, (char *)"$symname", (long) $value, 0, 0, 0}
129
%typemap(consttab) float, double
130
{ SWIG_LUA_FLOAT, (char *)"$symname", 0, (double) $value, 0, 0}
132
%typemap(consttab) char, char *
133
{ SWIG_LUA_STRING, (char *)"$symname", 0, 0, (void *)"$value", 0}
135
%typemap(consttab) long long, unsigned long long
136
{ SWIG_LUA_STRING, (char *) "$symname", 0, 0, (void *)"$value", 0}
138
%typemap(consttab) SWIGTYPE *, SWIGTYPE &, SWIGTYPE []
139
{ SWIG_LUA_POINTER, (char *)"$symname", 0, 0, (void *)$value, &$1_descriptor}
141
// TODO: not complete
142
//%typemap(consttab) SWIGTYPE (CLASS::*)
143
// { SWIG_LUA_BINARY, (char *)"$symname", sizeof($type), 0, (void *)&$value, &$1_descriptor}
146
/* -----------------------------------------------------------------------------
148
* ----------------------------------------------------------------------------- */
149
/* These are needed for the overloaded functions
150
These define the detection routines which will spot what
151
parmeters match which function
154
// unfortunately lua only considers one type of number
155
// so all numbers (int,float,double) match
156
// you could add an advanced fn to get type & check if its integral
157
%typecheck(SWIG_TYPECHECK_INTEGER)
159
unsigned int, unsigned short, unsigned long,
160
signed char, unsigned char,
161
long long, unsigned long long,
162
const int &, const short &, const long &,
163
const unsigned int &, const unsigned short &, const unsigned long &,
164
const long long &, const unsigned long long &,
165
enum SWIGTYPE, float, double,
166
const float &, const double &
168
$1 = lua_isnumber(L,$input);
171
%typecheck(SWIG_TYPECHECK_BOOL)
174
$1 = lua_isboolean(L,$input);
177
// special check for a char (string of length 1)
178
%typecheck(SWIG_TYPECHECK_CHAR) char {
179
$1 = lua_isstring(L,$input) && (lua_strlen(L,$input)==1);
182
%typecheck(SWIG_TYPECHECK_STRING) char * {
183
$1 = lua_isstring(L,$input);
186
%typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE, SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] {
188
if (lua_isuserdata(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, $1_descriptor, 0)) {
195
%typecheck(SWIG_TYPECHECK_VOIDPTR) void * {
197
if (lua_isuserdata(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, 0, 0)) {
204
/* -----------------------------------------------------------------------------
205
* Const reference issues
206
* ----------------------------------------------------------------------------- */
207
// additional typemaps for privitives by const reference:
209
// int intbyref(const int& i);
210
// SWIG assumes that this code will need a pointer to int to be passed in
211
// (this might be ok for passing objects by const ref, but not a primitive)
212
// therefore we add a set of blanket typemaps to fix this
213
// also a set for fns which return const X&
215
// %typemap(in,checkfn="lua_isnumber") const int &(int temp)
216
// %{ temp = (int)lua_tonumber(L,$input); $1=&temp;%}
217
// %typemap(out) const int&
218
// %{ lua_pushnumber(L, (double) $*1); SWIG_arg++;%}
219
// %typecheck(in,checkfn="lua_isnumber") const int &
222
%define SWIG_NUMBER_BY_CONST_REF(TYPE)
223
%typemap(in,checkfn="lua_isnumber") const TYPE &($basetype temp)
224
%{ temp=($basetype)lua_tonumber(L,$input); $1=&temp;%}
225
%typemap(out) const TYPE&
226
%{ lua_pushnumber(L, (double) *$1); SWIG_arg++;%}
229
SWIG_NUMBER_BY_CONST_REF(int);
230
SWIG_NUMBER_BY_CONST_REF(unsigned int);
231
SWIG_NUMBER_BY_CONST_REF(signed int);
232
SWIG_NUMBER_BY_CONST_REF(short);
233
SWIG_NUMBER_BY_CONST_REF(unsigned short);
234
SWIG_NUMBER_BY_CONST_REF(signed short);
235
SWIG_NUMBER_BY_CONST_REF(long);
236
SWIG_NUMBER_BY_CONST_REF(unsigned long);
237
SWIG_NUMBER_BY_CONST_REF(signed long);
238
//SWIG_NUMBER_BY_CONST_REF(char); // char's are now small strings
239
SWIG_NUMBER_BY_CONST_REF(unsigned char);
240
SWIG_NUMBER_BY_CONST_REF(signed char);
241
SWIG_NUMBER_BY_CONST_REF(float);
242
SWIG_NUMBER_BY_CONST_REF(double);
243
SWIG_NUMBER_BY_CONST_REF(enum SWIGTYPE);
245
// Also needed for object ptrs by const ref
246
// eg const A* ref_pointer(A* const& a);
247
// found in mixed_types.i
249
%typemap(in,checkfn="lua_isuserdata") SWIGTYPE* const &($*ltype temp)
250
%{temp=($*ltype)SWIG_MustGetPtr(L,$input,$*descriptor,0,$argnum,"$symname");
252
// and the pytcheck code
253
%typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE* const &
256
if (lua_isuserdata(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, $*descriptor, 0)) {
264
/* -----------------------------------------------------------------------------
265
* Overloaded operator support
266
* ----------------------------------------------------------------------------- */
267
// lua likes to call the + operator '__add'
268
// python likes to call it '__add__'
269
// Assuming most SWIGers will probably use the __add__ if they extend their classes
270
// we have two sets of renames
271
// one to rename the operator+() to __add()
272
// (this lets SWIG rename the operator overloads)
273
// another is to rename __add__() to __add()
274
// (this means that people who wrote SWIG code to do that add will also work)
277
// this is extra renaming for lua
278
// not all operators are supported, so only those that are, are listed
281
/* Ignored operators */
282
%ignorewarn("362:operator= ignored") operator=;
283
%ignorewarn("362:operator% ignored") operator%;
284
%ignorewarn("383:operator++ ignored") operator++;
285
%ignorewarn("384:operator-- ignored") operator--;
286
%ignorewarn("361:operator! ignored") operator!;
287
%ignorewarn("381:operator&& ignored") operator&&;
288
%ignorewarn("382:operator|| ignored") operator||;
289
%ignorewarn("386:operator->* ignored") operator->*;
290
%ignorewarn("362:operator+= ignored") operator+=;
291
%ignorewarn("362:operator-= ignored") operator-=;
292
%ignorewarn("362:operator*= ignored") operator*=;
293
%ignorewarn("362:operator/= ignored") operator/=;
294
%ignorewarn("362:operator%= ignored") operator%=;
295
%ignorewarn("389:operator[] ignored (consider using %extend)") operator[];
297
// renaming the main C++ operators
298
%rename(__add) *::operator+;
299
%rename(__sub) *::operator-;
300
%rename(__mul) *::operator*;
301
%rename(__div) *::operator/;
302
%rename(__unm) *::operator-();
303
%rename(__unm) *::operator-() const;
305
%rename(__eq) *::operator==; // note: Lua does not have a not equal
306
// it just uses 'not (a==b)'
307
%ignore *::operator!=;
308
%rename(__lt) *::operator<; // ditto less than vs greater than
309
%ignore *::operator>;
310
%rename(__le) *::operator<=; // ditto less than vs greater than
311
%ignore *::operator>=;
313
%rename(__call) *::operator(); // the fn call operator
317
// renaming the python operators to be compatible with lua
318
// this means that if a developer has written a fn __add__()
319
// it will be used for the lua +
320
%rename(__add) *::__add__;
321
%rename(__sub) *::__sub__;
322
%rename(__mul) *::__mul__;
323
%rename(__div) *::__div__;
324
%rename(__unm) *::__neg__; // lua calls unary minus,'unm' not 'neg'
325
%rename(__tostring) *::__str__; // both map to __tostring
326
%rename(__tostring) *::__repr__; // both map to __tostring
329
%rename(__pow) *::__pow__; // lua power '^' operator
330
%rename(__concat) *::__concat__; // lua concat '..' operator
331
%rename(__eq) *::__eq__;
332
%rename(__lt) *::__lt__;
333
%rename(__le) *::__le__;
334
%rename(__call) *::__call__; // the fn call operator()
336
// the [] operator has two parts, the get & the set
337
%rename(__getitem) *::__getitem__; // the v=X[i] (get operator)
338
%rename(__setitem) *::__setitem__; // the X[i]=v (set operator)
344
/* ------------------------------------------------------------
346
* ------------------------------------------------------------ */
347
/* Confession: I dont really like C++ exceptions
348
The python ones are great, but C++ ones I dont like
349
(mainly because I cannot get the stack trace out of it)
350
Therefore I have not bothered to try doing much in this
352
On top of this I an not clear on how best to do this is Lua
354
Therefore currently its just enough to get a few test cases running ok
356
note: if you wish to throw anything related to std::exception
357
use %include <std_except.i> instead
359
%typemap(throws) int,unsigned int,signed int,
360
long,unsigned long,signed long,
361
short,unsigned short,signed short,
363
long long,unsigned long long,
364
char, unsigned char, signed char,
366
%{lua_pushfstring(L,"exception thrown of value %d",(long)$1);
369
// strings are just sent as errors
370
%typemap(throws) char*, const char*
371
%{lua_pushstring(L,$1);SWIG_fail;%}
373
// anything else is sent as an object
375
%typemap(throws) SWIGTYPE
378
resultptr = new $1_ltype(($1_ltype &) $1);
379
SWIG_NewPointerObj(L,(void *) resultptr,$&1_descriptor,1);
383
%typemap(throws) SWIGTYPE
386
resultptr = ($&1_ltype) malloc(sizeof($1_type));
387
memmove(resultptr, &$1, sizeof($1_type));
388
SWIG_NewPointerObj(L,(void *) resultptr,$&1_descriptor,1);
394
/* ------------------------------------------------------------
396
* ------------------------------------------------------------ */
397
%insert(init) "swiginit.swg"
398
// this is the initialization function
399
// added at the very end of the code
400
// the function is always called SWIG_init, but an eariler #define will rename it
406
SWIGEXPORT int SWIG_init(lua_State* L)
410
// start with global table
411
lua_pushvalue(L,LUA_GLOBALSINDEX);
413
SWIG_InitializeModule((void*)L);
414
SWIG_PropagateClientData();
417
SWIG_Lua_add_function(L,"swig_type",SWIG_Lua_type);
418
SWIG_Lua_add_function(L,"swig_equals",SWIG_Lua_equal);
420
// begin the module (its a table with the same name as the module)
421
SWIG_Lua_module_begin(L,SWIG_name);
422
// add commands/functions
423
for (i = 0; swig_commands[i].name; i++){
424
SWIG_Lua_module_add_function(L,swig_commands[i].name,swig_commands[i].wrapper);
427
for (i = 0; swig_variables[i].name; i++){
428
SWIG_Lua_module_add_variable(L,swig_variables[i].name,swig_variables[i].get,swig_variables[i].set);
431
// additional registration structs & classes in lua:
432
for (i = 0; swig_types[i]; i++){
433
if (swig_types[i]->clientdata){
434
SWIG_Lua_class_register(L,(swig_lua_class*)(swig_types[i]->clientdata));
439
SWIG_Lua_InstallConstants(L,swig_constants);
442
SWIG_Lua_module_end(L);
444
lua_pop(L,1); // tidy stack (remove global table)
449
// Lua 5.1 has a different name for importing libraries
450
// luaopen_XXX, where XXX is the name of the module (not capitalised)
451
// this function will allow Lua 5.1 to import correctly
455
SWIGEXPORT int SWIG_import(lua_State* L)
462
/* Note: the initialization function is closed after all code is generated */
465
/*************************** end lua.swg ******************************/
b'\\ No newline at end of file'