~ubuntu-branches/ubuntu/edgy/swig1.3/edgy

« back to all changes in this revision

Viewing changes to Lib/lua/lua.swg

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-12-05 01:16:04 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051205011604-ygx904it6413k3go
Tags: 1.3.27-1ubuntu1
Resynchronise with Debian again, for the new subversion packages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***********************************************************************
 
2
 * lua.swg
 
3
 *
 
4
 *     SWIG Configuration File for Lua
 
5
 *     This file is parsed by SWIG before reading any other interface
 
6
 *     file.
 
7
 *
 
8
 * Author : Mark Gossage (mark@gossage.cjb.net)
 
9
 ************************************************************************/
 
10
 
 
11
/* -----------------------------------------------------------------------------
 
12
 *                          includes
 
13
 * ----------------------------------------------------------------------------- */
 
14
 
 
15
%runtime "swigrun.swg";         /* Common C API type-checking code */
 
16
%runtime "luarun.swg";          /* Lua runtime stuff */
 
17
 
 
18
/* -----------------------------------------------------------------------------
 
19
 *                          standard typemaps
 
20
 * ----------------------------------------------------------------------------- */
 
21
/* NEW LANGUAGE NOTE:
 
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)
 
29
*/
 
30
// numbers
 
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);%}
 
36
 
 
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++;%}
 
42
 
 
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);%}
 
47
 
 
48
%typemap(out) bool, const bool&
 
49
%{  lua_pushboolean(L,(int)$1); SWIG_arg++;%}
 
50
 
 
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;%}
 
54
 
 
55
%typemap(out) const bool&
 
56
%{  lua_pushboolean(L,(int)*$1); SWIG_arg++;%}
 
57
 
 
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);%}
 
61
 
 
62
%typemap(out) const char*, char*, const char[ANY], char[ANY]
 
63
%{  lua_pushstring(L,$1); SWIG_arg++;%}
 
64
 
 
65
// char's
 
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];%}
 
70
 
 
71
%typemap(out) char
 
72
%{  lua_pushfstring(L,"%c",$1); SWIG_arg++;%}
 
73
 
 
74
// by const ref
 
75
%typemap(in,checkfn="lua_isstring") const char& (char temp)
 
76
%{temp = ((char*)lua_tostring(L, $input))[0]; $1=&temp;%}
 
77
 
 
78
%typemap(out) const char&
 
79
%{  lua_pushfstring(L,"%c",*$1); SWIG_arg++;%}
 
80
 
 
81
 
 
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");%}
 
85
 
 
86
%typemap(out) SWIGTYPE*,SWIGTYPE&
 
87
%{SWIG_NewPointerObj(L,$1,$descriptor,$owner); SWIG_arg++; %}
 
88
 
 
89
// passing objects by value (yuk: do you really have to do this?)
 
90
// what we do is get it as a pointer (the $&ltype argp)
 
91
// then do an assignment
 
92
%typemap(in,checkfn="lua_isuserdata") SWIGTYPE
 
93
{
 
94
   $&ltype argp;
 
95
   if(SWIG_ConvertPtr(L,$input,(void**)(&argp),$&descriptor,SWIG_POINTER_EXCEPTION) == -1) SWIG_fail;
 
96
   $1 = *argp;
 
97
}
 
98
 
 
99
// Primitive types--return by value
 
100
// must make a new object, copy the data & return the new object
 
101
#ifdef __cplusplus
 
102
%typemap(out) SWIGTYPE
 
103
{
 
104
  $&1_ltype resultptr;
 
105
  resultptr = new $1_ltype(($1_ltype &) $1);
 
106
  SWIG_NewPointerObj(L,(void *) resultptr,$&1_descriptor,1); SWIG_arg++;
 
107
}
 
108
#else
 
109
%typemap(out) SWIGTYPE
 
110
{
 
111
  $&1_ltype resultptr;
 
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++;
 
115
}
 
116
#endif
 
117
 
 
118
// void (must be empty without the SWIG_arg++)
 
119
%typemap(out) void "";
 
120
 
 
121
 
 
122
/* -----------------------------------------------------------------------------
 
123
 *                          constants typemaps
 
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}
 
128
 
 
129
%typemap(consttab) float, double
 
130
       { SWIG_LUA_FLOAT,   (char *)"$symname", 0, (double) $value, 0, 0}
 
131
 
 
132
%typemap(consttab) char, char *
 
133
       { SWIG_LUA_STRING,  (char *)"$symname", 0, 0, (void *)"$value", 0}
 
134
 
 
135
%typemap(consttab) long long, unsigned long long
 
136
       { SWIG_LUA_STRING, (char *) "$symname", 0, 0, (void *)"$value", 0}
 
137
 
 
138
%typemap(consttab) SWIGTYPE *, SWIGTYPE &, SWIGTYPE []
 
139
       { SWIG_LUA_POINTER, (char *)"$symname", 0, 0, (void *)$value, &$1_descriptor}
 
140
 
 
141
// TODO: not complete
 
142
//%typemap(consttab) SWIGTYPE (CLASS::*)
 
143
//       { SWIG_LUA_BINARY,  (char *)"$symname", sizeof($type), 0, (void *)&$value, &$1_descriptor}
 
144
 
 
145
 
 
146
/* -----------------------------------------------------------------------------
 
147
 *                          typecheck rules
 
148
 * ----------------------------------------------------------------------------- */
 
149
/* These are needed for the overloaded functions
 
150
These define the detection routines which will spot what
 
151
parmeters match which function
 
152
*/
 
153
 
 
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)
 
158
         int, short, long,
 
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 &
 
167
{
 
168
  $1 = lua_isnumber(L,$input);
 
169
}
 
170
 
 
171
%typecheck(SWIG_TYPECHECK_BOOL)
 
172
    bool, const bool &
 
173
{
 
174
  $1 = lua_isboolean(L,$input);
 
175
}
 
176
 
 
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);
 
180
}
 
181
 
 
182
%typecheck(SWIG_TYPECHECK_STRING) char * {
 
183
  $1 = lua_isstring(L,$input);
 
184
}
 
185
 
 
186
%typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE, SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] {
 
187
  void *ptr;
 
188
  if (lua_isuserdata(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, $1_descriptor, 0)) {
 
189
    $1 = 0;
 
190
  } else {
 
191
    $1 = 1;
 
192
  }
 
193
}
 
194
 
 
195
%typecheck(SWIG_TYPECHECK_VOIDPTR) void * {
 
196
  void *ptr;
 
197
  if (lua_isuserdata(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, 0, 0)) {
 
198
    $1 = 0;
 
199
  } else {
 
200
    $1 = 1;
 
201
  }
 
202
}
 
203
 
 
204
/* -----------------------------------------------------------------------------
 
205
 *                          Const reference issues
 
206
 * ----------------------------------------------------------------------------- */
 
207
// additional typemaps for privitives by const reference:
 
208
// given a function:
 
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&
 
214
 
 
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 &
 
220
 
 
221
// now the code
 
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++;%}
 
227
%enddef
 
228
 
 
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);
 
244
 
 
245
// Also needed for object ptrs by const ref
 
246
// eg const A* ref_pointer(A* const& a);
 
247
// found in mixed_types.i
 
248
 
 
249
%typemap(in,checkfn="lua_isuserdata") SWIGTYPE* const &($*ltype temp)
 
250
%{temp=($*ltype)SWIG_MustGetPtr(L,$input,$*descriptor,0,$argnum,"$symname");
 
251
$1=&temp;%}
 
252
// and the pytcheck code
 
253
%typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE* const &
 
254
%{
 
255
  void *ptr;
 
256
  if (lua_isuserdata(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, $*descriptor, 0)) {
 
257
    $1 = 0;
 
258
  } else {
 
259
    $1 = 1;
 
260
  }
 
261
%}
 
262
 
 
263
 
 
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)
 
275
 
 
276
#ifdef __cplusplus
 
277
// this is extra renaming for lua
 
278
// not all operators are supported, so only those that are, are listed
 
279
 
 
280
 
 
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[];
 
296
 
 
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;
 
304
 
 
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>=;
 
312
 
 
313
%rename(__call)                 *::operator();  // the fn call operator
 
314
 
 
315
 
 
316
 
 
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
 
327
 
 
328
 
 
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()
 
335
 
 
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)
 
339
 
 
340
 
 
341
#endif
 
342
 
 
343
 
 
344
/* ------------------------------------------------------------
 
345
 *                              Exceptions
 
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
 
351
 
 
352
On top of this I an not clear on how best to do this is Lua
 
353
 
 
354
Therefore currently its just enough to get a few test cases running ok
 
355
 
 
356
note: if you wish to throw anything related to std::exception
 
357
use %include <std_except.i> instead
 
358
*/
 
359
%typemap(throws) int,unsigned int,signed int,
 
360
                                long,unsigned long,signed long,
 
361
                                short,unsigned short,signed short,
 
362
                                bool,float,double,
 
363
                                long long,unsigned long long,
 
364
                                char, unsigned char, signed char,
 
365
                                enum SWIGTYPE
 
366
%{lua_pushfstring(L,"exception thrown of value %d",(long)$1);
 
367
SWIG_fail; %}
 
368
 
 
369
// strings are just sent as errors
 
370
%typemap(throws) char*, const char*
 
371
%{lua_pushstring(L,$1);SWIG_fail;%}
 
372
 
 
373
// anything else is sent as an object
 
374
#ifdef __cplusplus
 
375
        %typemap(throws) SWIGTYPE
 
376
        {
 
377
          $&1_ltype resultptr;
 
378
          resultptr = new $1_ltype(($1_ltype &) $1);
 
379
          SWIG_NewPointerObj(L,(void *) resultptr,$&1_descriptor,1);
 
380
          SWIG_fail;
 
381
        }
 
382
#else
 
383
        %typemap(throws) SWIGTYPE
 
384
        {
 
385
          $&1_ltype resultptr;
 
386
          resultptr = ($&1_ltype) malloc(sizeof($1_type));
 
387
          memmove(resultptr, &$1, sizeof($1_type));
 
388
          SWIG_NewPointerObj(L,(void *) resultptr,$&1_descriptor,1);
 
389
          SWIG_fail;
 
390
        }
 
391
#endif
 
392
 
 
393
 
 
394
/* ------------------------------------------------------------
 
395
 *                              SWIG_init
 
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
 
401
%init %{
 
402
 
 
403
#ifdef __cplusplus
 
404
extern "C"
 
405
#endif
 
406
SWIGEXPORT int SWIG_init(lua_State* L)
 
407
{
 
408
        int i;
 
409
 
 
410
        // start with global table
 
411
        lua_pushvalue(L,LUA_GLOBALSINDEX);
 
412
 
 
413
    SWIG_InitializeModule((void*)L);
 
414
    SWIG_PropagateClientData();
 
415
 
 
416
        // add a global fn
 
417
        SWIG_Lua_add_function(L,"swig_type",SWIG_Lua_type);
 
418
        SWIG_Lua_add_function(L,"swig_equals",SWIG_Lua_equal);
 
419
 
 
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);
 
425
        }
 
426
        // add variables
 
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);
 
429
        }
 
430
 
 
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));
 
435
                }
 
436
        }
 
437
 
 
438
        // constants
 
439
        SWIG_Lua_InstallConstants(L,swig_constants);
 
440
 
 
441
        // end module
 
442
        SWIG_Lua_module_end(L);
 
443
 
 
444
        lua_pop(L,1);                      // tidy stack (remove global table)
 
445
 
 
446
        return 1;
 
447
}
 
448
 
 
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
 
452
#ifdef __cplusplus
 
453
extern "C"
 
454
#endif
 
455
SWIGEXPORT int SWIG_import(lua_State* L)
 
456
{
 
457
        return SWIG_init(L);
 
458
}
 
459
 
 
460
%}
 
461
 
 
462
/* Note: the initialization function is closed after all code is generated */
 
463
 
 
464
 
 
465
/*************************** end lua.swg ******************************/
 
 
b'\\ No newline at end of file'