fixed several test cases

added long long support
changed typemaps to use SWIG_ConvertPtr rather than SWIG_MustGetPointer
started spliting lua.swg into smaller parts to make it neater


git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9450 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Mark Gossage 2006-10-14 07:15:52 +00:00
commit f073625488
8 changed files with 482 additions and 376 deletions

View file

@ -1,6 +1,12 @@
Version 1.3.30 (in progress)
============================
10/14/2006: mgossage
[Lua] added OUTPUT& for all number types, added a long long type
fixed several test cases.
update: changed typemaps to use SWIG_ConvertPtr rather than SWIG_MustGetPointer
started spliting lua.swg into smaller parts to make it neater
10/13/2006: wsfulton
[C#, Java] Marginally better support for multiple inheritance only in that you can
control what the base class is. This is done using the new 'replace' attribute in the

View file

@ -12,147 +12,8 @@
* includes
* ----------------------------------------------------------------------------- */
%runtime "swigrun.swg"; /* Common C API type-checking code */
%runtime "luarun.swg"; /* Lua runtime stuff */
/* -----------------------------------------------------------------------------
* standard typemaps
* ----------------------------------------------------------------------------- */
/* NEW LANGUAGE NOTE:
the 'checkfn' param is something that I added for typemap(in)
it is an optional fn call to check the type of the lua object
the fn call must be of the form
int checkfn(lua_State *L, int index);
and return 1/0 depending upon if this is the correct type
For the typemap(out), an additional SWIG_arg parmeter must be incremented
to reflect the number of values returned (normally SWIG_arg++; will do)
*/
// numbers
%typemap(in,checkfn="lua_isnumber") int,short,long,
unsigned int,unsigned short,unsigned long,
signed char,unsigned char,
float,double
%{$1 = ($type)lua_tonumber(L, $input);%}
%typemap(out) int,short,long,
unsigned int,unsigned short,unsigned long,
signed char,unsigned char,
float,double
%{ lua_pushnumber(L, (lua_Number) $1); SWIG_arg++;%}
/* enums have to be handled slightly differently
VC++ .net will not allow a cast from double to enum directly
therefore cast to an int first.
Thanks to Jason Rego <JasonR@rainbowstudios.com> for finding this.
*/
%typemap(in,checkfn="lua_isnumber") enum SWIGTYPE
%{$1 = ($type)(int)lua_tonumber(L, $input);%}
%typemap(out) enum SWIGTYPE
%{ lua_pushnumber(L, (lua_Number)(int)($1)); SWIG_arg++;%}
// boolean (which is a special type in lua)
// note: 1 & 0 are not booleans in lua, only true & false
%typemap(in,checkfn="lua_isboolean") bool
%{$1 = (bool)lua_toboolean(L, $input);%}
%typemap(out) bool, const bool&
%{ lua_pushboolean(L,(int)$1); SWIG_arg++;%}
// for const bool&, SWIG treats this as a const bool* so we must dereference it
%typemap(in,checkfn="lua_isboolean") const bool& (bool temp)
%{temp=(bool)lua_toboolean(L, $input); $1=&temp;%}
%typemap(out) const bool&
%{ lua_pushboolean(L,(int)*$1); SWIG_arg++;%}
// strings (char* and char[])
%typemap(in,checkfn="lua_isstring") const char*, char*
%{$1 = ($1_ltype)lua_tostring(L, $input);%}
%typemap(in,checkfn="lua_isstring") const char[ANY], char[ANY]
%{$1 = (char*)lua_tostring(L, $input);%}
%typemap(out) const char*, char*
%{ lua_pushstring(L,(const char*)$1); SWIG_arg++;%}
%typemap(out) const char[ANY], char[ANY]
%{ lua_pushstring(L,$1); SWIG_arg++;%}
// char's
// currently treating chars as small strings, not as numbers
// (however signed & unsigned char's are numbers...)
%typemap(in,checkfn="lua_isstring") char
%{$1 = ((char*)lua_tostring(L, $input))[0];%}
%typemap(out) char
%{ lua_pushfstring(L,"%c",$1); SWIG_arg++;%}
// by const ref
%typemap(in,checkfn="lua_isstring") const char& (char temp)
%{temp = ((char*)lua_tostring(L, $input))[0]; $1=&temp;%}
%typemap(out) const char&
%{ lua_pushfstring(L,"%c",*$1); SWIG_arg++;%}
// pointers and references
// under SWIG rules, it is ok, to have a pass in a lua nil,
// it should be converted to a SWIG NULL.
// This will only be allowed for pointers & arrays, not refs or by value
// the checkfn lua_isuserdata will only work for userdata
// the checkfn SWIG_isptrtype will work for both userdata and nil's
%typemap(in,checkfn="SWIG_isptrtype") SWIGTYPE*,SWIGTYPE[]
%{$1=($1_ltype)SWIG_MustGetPtr(L,$input,$descriptor,0,$argnum,"$symname");%}
%typemap(in,checkfn="lua_isuserdata") SWIGTYPE&
%{$1=($1_ltype)SWIG_MustGetPtr(L,$input,$descriptor,0,$argnum,"$symname");%}
%typemap(out) SWIGTYPE*,SWIGTYPE&
%{SWIG_NewPointerObj(L,$1,$descriptor,$owner); SWIG_arg++; %}
// passing objects by value (yuk: do you really have to do this?)
// what we do is get it as a pointer (the $&ltype argp)
// then do an assignment
%typemap(in,checkfn="lua_isuserdata") SWIGTYPE
{
$&ltype argp;
if(SWIG_ConvertPtr(L,$input,(void**)(&argp),$&descriptor,0)) SWIG_fail;
$1 = *argp;
}
// Primitive types--return by value
// must make a new object, copy the data & return the new object
#ifdef __cplusplus
%typemap(out) SWIGTYPE
{
$&1_ltype resultptr;
resultptr = new $1_ltype(($1_ltype &) $1);
SWIG_NewPointerObj(L,(void *) resultptr,$&1_descriptor,1); SWIG_arg++;
}
#else
%typemap(out) SWIGTYPE
{
$&1_ltype resultptr;
resultptr = ($&1_ltype) malloc(sizeof($1_type));
memmove(resultptr, &$1, sizeof($1_type));
SWIG_NewPointerObj(L,(void *) resultptr,$&1_descriptor,1); SWIG_arg++;
}
#endif
// void (must be empty without the SWIG_arg++)
%typemap(out) void "";
/* void* is a special case
A function void fn(void*) should take any kind of pointer as a parameter (just like C/C++ does)
but if its an output, then it should be wrappered like any other SWIG object (using default typemap)
*/
//%typemap(in,checkfn="lua_isuserdata") void*
//%typemap(in,checkfn="SWIG_isptrtype") void*
//%{$1=((swig_lua_userdata*)(lua_touserdata(L,$input)))->ptr;%}
%typemap(in,checkfn="SWIG_isptrtype") void*
%{$1=($1_ltype)SWIG_MustGetPtr(L,$input,0,0,$argnum,"$symname");%}
//%{SWIG_ConvertPtr(L,$input, (void **) &ptr, 0, 0)); %}
%include <luatypemaps.swg> /* The typemaps */
%include <luaruntime.swg> /* The runtime stuff */
/* -----------------------------------------------------------------------------
* constants typemaps
@ -164,6 +25,12 @@ but if its an output, then it should be wrappered like any other SWIG object (us
%typemap(consttab) float, double
{ SWIG_LUA_FLOAT, (char *)"$symname", 0, (double) $value, 0, 0}
%typemap(consttab) long long, unsigned long long, signed long long
{ SWIG_LUA_FLOAT, (char *)"$symname", 0, (double) $value, 0, 0}
%typemap(consttab) const long long&, const unsigned long long&, const signed long long&
{ SWIG_LUA_FLOAT, (char *)"$symname", 0, (double) *$value, 0, 0}
%typemap(consttab) char *, const char *, char [], const char []
{ SWIG_LUA_STRING, (char *)"$symname", 0, 0, (void *)$value, 0}
@ -178,139 +45,12 @@ but if its an output, then it should be wrappered like any other SWIG object (us
%typemap(consttab) SWIGTYPE *, SWIGTYPE &, SWIGTYPE []
{ SWIG_LUA_POINTER, (char *)"$symname", 0, 0, (void *)$value, &$1_descriptor}
// TODO: not complete
//%typemap(consttab) SWIGTYPE (CLASS::*)
// { SWIG_LUA_BINARY, (char *)"$symname", sizeof($type), 0, (void *)&$value, &$1_descriptor}
/* -----------------------------------------------------------------------------
* typecheck rules
* ----------------------------------------------------------------------------- */
/* These are needed for the overloaded functions
These define the detection routines which will spot what
parmeters match which function
*/
// unfortunately lua only considers one type of number
// so all numbers (int,float,double) match
// you could add an advanced fn to get type & check if its integral
%typecheck(SWIG_TYPECHECK_INTEGER)
int, short, long,
unsigned int, unsigned short, unsigned long,
signed char, unsigned char,
long long, unsigned long long,
const int &, const short &, const long &,
const unsigned int &, const unsigned short &, const unsigned long &,
const long long &, const unsigned long long &,
enum SWIGTYPE, float, double,
const float &, const double &
{
$1 = lua_isnumber(L,$input);
}
%typecheck(SWIG_TYPECHECK_BOOL)
bool, const bool &
{
$1 = lua_isboolean(L,$input);
}
// special check for a char (string of length 1)
%typecheck(SWIG_TYPECHECK_CHAR) char {
$1 = lua_isstring(L,$input) && (lua_strlen(L,$input)==1);
}
%typecheck(SWIG_TYPECHECK_STRING) char * {
$1 = lua_isstring(L,$input);
}
%typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE *, SWIGTYPE [] {
void *ptr;
if (SWIG_isptrtype(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, $1_descriptor, 0)) {
$1 = 0;
} else {
$1 = 1;
}
}
%typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE, SWIGTYPE & {
void *ptr;
if (lua_isuserdata(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, $1_descriptor, 0)) {
$1 = 0;
} else {
$1 = 1;
}
}
%typecheck(SWIG_TYPECHECK_VOIDPTR) void * {
void *ptr;
if (SWIG_isptrtype(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, 0, 0)) {
$1 = 0;
} else {
$1 = 1;
}
}
/* -----------------------------------------------------------------------------
* Const reference issues
* ----------------------------------------------------------------------------- */
// additional typemaps for privitives by const reference:
// given a function:
// int intbyref(const int& i);
// SWIG assumes that this code will need a pointer to int to be passed in
// (this might be ok for passing objects by const ref, but not a primitive)
// therefore we add a set of blanket typemaps to fix this
// also a set for fns which return const X&
// %typemap(in,checkfn="lua_isnumber") const int &(int temp)
// %{ temp = (int)lua_tonumber(L,$input); $1=&temp;%}
// %typemap(out) const int&
// %{ lua_pushnumber(L, (double) $*1); SWIG_arg++;%}
// %typecheck(in,checkfn="lua_isnumber") const int &
// now the code
%define SWIG_NUMBER_BY_CONST_REF(TYPE)
%typemap(in,checkfn="lua_isnumber") const TYPE &($basetype temp)
%{ temp=($basetype)lua_tonumber(L,$input); $1=&temp;%}
%typemap(out) const TYPE&
%{ lua_pushnumber(L, (lua_Number) *$1); SWIG_arg++;%}
%enddef
SWIG_NUMBER_BY_CONST_REF(int);
SWIG_NUMBER_BY_CONST_REF(unsigned int);
SWIG_NUMBER_BY_CONST_REF(signed int);
SWIG_NUMBER_BY_CONST_REF(short);
SWIG_NUMBER_BY_CONST_REF(unsigned short);
SWIG_NUMBER_BY_CONST_REF(signed short);
SWIG_NUMBER_BY_CONST_REF(long);
SWIG_NUMBER_BY_CONST_REF(unsigned long);
SWIG_NUMBER_BY_CONST_REF(signed long);
//SWIG_NUMBER_BY_CONST_REF(char); // char's are now small strings
SWIG_NUMBER_BY_CONST_REF(unsigned char);
SWIG_NUMBER_BY_CONST_REF(signed char);
SWIG_NUMBER_BY_CONST_REF(float);
SWIG_NUMBER_BY_CONST_REF(double);
SWIG_NUMBER_BY_CONST_REF(enum SWIGTYPE);
// Also needed for object ptrs by const ref
// eg const A* ref_pointer(A* const& a);
// found in mixed_types.i
%typemap(in,checkfn="lua_isuserdata") SWIGTYPE* const &($*ltype temp)
%{temp=($*ltype)SWIG_MustGetPtr(L,$input,$*descriptor,0,$argnum,"$symname");
$1=&temp;%}
// and the typecheck code
%typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE* const &
%{
void *ptr;
if (lua_isuserdata(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, $*descriptor, 0)) {
$1 = 0;
} else {
$1 = 1;
}
%}
/* -----------------------------------------------------------------------------
* Overloaded operator support
* ----------------------------------------------------------------------------- */
@ -380,7 +120,7 @@ The python ones are great, but C++ ones I dont like
(mainly because I cannot get the stack trace out of it)
Therefore I have not bothered to try doing much in this
On top of this I an not clear on how best to do this is Lua
On top of this I am not clear on how best to do this is Lua
Therefore currently its just enough to get a few test cases running ok
@ -394,7 +134,7 @@ use %include <std_except.i> instead
long long,unsigned long long,
char, unsigned char, signed char,
enum SWIGTYPE
%{lua_pushfstring(L,"exception thrown of value %f",(double)$1);
%{lua_pushfstring(L,"numeric exception thrown of value %f",(double)$1);
SWIG_fail; %}
// strings are just sent as errors
@ -410,6 +150,11 @@ SWIG_fail; %}
SWIG_NewPointerObj(L,(void *) resultptr,$&1_descriptor,1);
SWIG_fail;
}
/* %typemap(throws) SWIGTYPE&
{
SWIG_NewPointerObj(L,(void *) &$1,$&1_descriptor,1);
SWIG_fail;
}*/
#else
%typemap(throws) SWIGTYPE
{
@ -422,90 +167,9 @@ SWIG_fail; %}
#endif
/* ------------------------------------------------------------
* SWIG_init
* ------------------------------------------------------------ */
%insert(initbeforefunc) "swiginit.swg"
%insert(initbeforefunc) %{
/* Forward declaration of where the user's %init{} gets inserted */
#ifdef __cplusplus
extern "C" {
#endif
void SWIG_init_user(lua_State* L );
/* this is the initialization function
added at the very end of the code
the function is always called SWIG_init, but an eariler #define will rename it
*/
SWIGEXPORT int SWIG_init(lua_State* L)
{
int i;
/* start with global table */
lua_pushvalue(L,LUA_GLOBALSINDEX);
SWIG_InitializeModule((void*)L);
SWIG_PropagateClientData();
/* invoke user-specific initialization */
SWIG_init_user(L);
/* add a global fn */
SWIG_Lua_add_function(L,"swig_type",SWIG_Lua_type);
SWIG_Lua_add_function(L,"swig_equals",SWIG_Lua_equal);
/* begin the module (its a table with the same name as the module) */
SWIG_Lua_module_begin(L,SWIG_name);
/* add commands/functions */
for (i = 0; swig_commands[i].name; i++){
SWIG_Lua_module_add_function(L,swig_commands[i].name,swig_commands[i].func);
}
/*luaL_openlib(L,NULL,swig_commands,0);*/
/* all in one */
/*luaL_openlib(L,SWIG_name,swig_commands,0);*/
/* add variables */
for (i = 0; swig_variables[i].name; i++){
SWIG_Lua_module_add_variable(L,swig_variables[i].name,swig_variables[i].get,swig_variables[i].set);
}
/* additional registration structs & classes in lua: */
for (i = 0; swig_types[i]; i++){
if (swig_types[i]->clientdata){
SWIG_Lua_class_register(L,(swig_lua_class*)(swig_types[i]->clientdata));
}
}
/* constants */
SWIG_Lua_InstallConstants(L,swig_constants);
/* end module */
/*SWIG_Lua_module_end(L);*/
lua_pop(L,1); /* tidy stack (remove module table)*/
lua_pop(L,1); /* tidy stack (remove global table)*/
return 1;
}
/* Lua 5.1 has a different name for importing libraries
luaopen_XXX, where XXX is the name of the module (not capitalised)
this function will allow Lua 5.1 to import correctly.
There is a #define in the wrapper to rename 'SWIG_import' to the correct name
*/
SWIGEXPORT int SWIG_import(lua_State* L)
{
return SWIG_init(L);
}
#ifdef __cplusplus
}
#endif
%}
/* Note: the initialization function is closed after all code is generated */
/* -----------------------------------------------------------------------------
* extras
* ----------------------------------------------------------------------------- */
/* ------------------------------ end lua.swg ------------------------------ */

View file

@ -31,10 +31,10 @@ just push the parameters, call the function and return the result.
lua_call(fn.L,2,1); // 2 in, 1 out
return luaL_checknumber(fn.L,-1);
}
SWIG will automatically performs the wrappering of the arguments in and out.
However: if you wish to store the function between calls, look to the SWIGLUA_REF below.
However: if you wish to store the function between calls, look to the SWIGLUA_REF below.
*/
// this is for the C code only, we don't want SWIG to wrapper it for us.
@ -69,10 +69,10 @@ note: it should be passed by value, not byref or as a pointer.
The SWIGLUA_REF holds a pointer to the lua_State, and an integer reference to the object.
Because it holds a permenet ref to an object, the SWIGLUA_REF must be handled with a bit more care.
It should be initalised to {0,0}. The function swiglua_ref_set() should be used to set it.
swiglua_ref_clear() should be used to clear it when not in use, and swiglua_ref_get() to get the
swiglua_ref_clear() should be used to clear it when not in use, and swiglua_ref_get() to get the
data back.
Note: the typemap does not check that the object is in fact a function,
Note: the typemap does not check that the object is in fact a function,
if you need that you must add it yourself.
@ -84,10 +84,10 @@ if you need that you must add it yourself.
lua_call(fn.L,2,1); // 2 in, 1 out
return luaL_checknumber(fn.L,-1);
}
SWIG will automatically performs the wrappering of the arguments in and out.
However: if you wish to store the function between calls, look to the SWIGLUA_REF below.
However: if you wish to store the function between calls, look to the SWIGLUA_REF below.
*/

View file

@ -15,6 +15,7 @@ extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include <malloc.h>
#include <assert.h> /* for a few sanity tests */
/* -----------------------------------------------------------------------------
@ -90,11 +91,13 @@ typedef struct {
#define SWIG_contract_assert(expr, msg) \
if (!(expr)) { lua_pushstring(L, (char *) msg); goto fail; } else
/* helper #defines */
#define SWIG_fail {goto fail;}
#define SWIG_fail_arg(I) {lua_pushfstring(L,"argument %d incorrect/missing",I);goto fail;}
#define SWIG_fail_ptr(func_name,argnum,type) \
{lua_pushfstring(L,"Error in %s, expected a %s at argument number %d\n",\
func_name,(type && type->str)?type->str:"void*",argnum);\
goto fail;}
#define SWIG_Lua_get_table(L,n) \
(lua_pushstring(L, n), lua_rawget(L,-2))
@ -107,6 +110,12 @@ typedef struct {
/* special helper for allowing 'nil' for usertypes */
#define SWIG_isptrtype(L,I) (lua_isuserdata(L,I) || lua_isnil(L,I))
#ifdef __cplusplus
/* Special helper for member function pointers */
#define SWIG_as_voidptr(a) const_cast< void * >(static_cast< const void * >(a))
#define SWIG_as_voidptrptr(a) ((void)SWIG_as_voidptr(*a),reinterpret_cast< void** >(a))
#endif
/* -----------------------------------------------------------------------------
* global variable support code: modules
* ----------------------------------------------------------------------------- */
@ -505,29 +514,29 @@ SWIGRUNTIME int SWIG_Lua_ConvertPtr(lua_State* L,int index,void** ptr,swig_type
{
swig_lua_userdata* usr;
swig_cast_info *cast;
if (lua_isnil(L,index)){*ptr=0; return 0;} /* special case: lua nil => NULL pointer */
if (lua_isnil(L,index)){*ptr=0; return SWIG_OK;} /* special case: lua nil => NULL pointer */
usr=(swig_lua_userdata*)lua_touserdata(L,index); /* get data */
if (usr)
{
if (!type) /* special cast void*, no casting fn */
{
*ptr=usr->ptr;
return 0; /* ok */
return SWIG_OK; /* ok */
}
cast=SWIG_TypeCheckStruct(usr->type,type); /* performs normal type checking */
if (cast)
{
*ptr=SWIG_TypeCast(cast,usr->ptr);
return 0; /* ok */
return SWIG_OK; /* ok */
}
}
return 1; /* error */
return SWIG_ERROR; /* error */
}
SWIGRUNTIME void* SWIG_Lua_MustGetPtr(lua_State* L,int index,swig_type_info *type,int flags,
int argnum,const char* func_name){
void* result;
if (SWIG_ConvertPtr(L,index,&result,type,flags)){
if (!SWIG_IsOK(SWIG_ConvertPtr(L,index,&result,type,flags))){
lua_pushfstring(L,"Error in %s, expected a %s at argument number %d\n",
func_name,(type && type->str)?type->str:"void*",argnum);
lua_error(L);

View file

@ -0,0 +1,94 @@
/* -----------------------------------------------------------------------------
* See the LICENSE file for information on copyright, usage and redistribution
* of SWIG, and the README file for authors - http://www.swig.org/release.html.
*
* luaruntime.swg
*
* all the runtime code for .
* ----------------------------------------------------------------------------- */
%runtime "swigrun.swg"; /* Common C API type-checking code */
%runtime "luarun.swg"; /* Lua runtime stuff */
%insert(initbeforefunc) "swiginit.swg"
%insert(initbeforefunc) %{
/* Forward declaration of where the user's %init{} gets inserted */
#ifdef __cplusplus
extern "C" {
#endif
void SWIG_init_user(lua_State* L );
/* this is the initialization function
added at the very end of the code
the function is always called SWIG_init, but an eariler #define will rename it
*/
SWIGEXPORT int SWIG_init(lua_State* L)
{
int i;
/* start with global table */
lua_pushvalue(L,LUA_GLOBALSINDEX);
SWIG_InitializeModule((void*)L);
SWIG_PropagateClientData();
/* invoke user-specific initialization */
SWIG_init_user(L);
/* add a global fn */
SWIG_Lua_add_function(L,"swig_type",SWIG_Lua_type);
SWIG_Lua_add_function(L,"swig_equals",SWIG_Lua_equal);
/* begin the module (its a table with the same name as the module) */
SWIG_Lua_module_begin(L,SWIG_name);
/* add commands/functions */
for (i = 0; swig_commands[i].name; i++){
SWIG_Lua_module_add_function(L,swig_commands[i].name,swig_commands[i].func);
}
/*luaL_openlib(L,NULL,swig_commands,0);*/
/* all in one */
/*luaL_openlib(L,SWIG_name,swig_commands,0);*/
/* add variables */
for (i = 0; swig_variables[i].name; i++){
SWIG_Lua_module_add_variable(L,swig_variables[i].name,swig_variables[i].get,swig_variables[i].set);
}
/* additional registration structs & classes in lua: */
for (i = 0; swig_types[i]; i++){
if (swig_types[i]->clientdata){
SWIG_Lua_class_register(L,(swig_lua_class*)(swig_types[i]->clientdata));
}
}
/* constants */
SWIG_Lua_InstallConstants(L,swig_constants);
/* end module */
/*SWIG_Lua_module_end(L);*/
lua_pop(L,1); /* tidy stack (remove module table)*/
lua_pop(L,1); /* tidy stack (remove global table)*/
return 1;
}
/* Lua 5.1 has a different name for importing libraries
luaopen_XXX, where XXX is the name of the module (not capitalised)
this function will allow Lua 5.1 to import correctly.
There is a #define in the wrapper to rename 'SWIG_import' to the correct name
*/
SWIGEXPORT int SWIG_import(lua_State* L)
{
return SWIG_init(L);
}
#ifdef __cplusplus
}
#endif
%}
/* Note: the initialization function is closed after all code is generated */

View file

@ -0,0 +1,329 @@
/* -----------------------------------------------------------------------------
* See the LICENSE file for information on copyright, usage and redistribution
* of SWIG, and the README file for authors - http://www.swig.org/release.html.
*
* luatypemaps.swg
*
* basic typemaps for Lua.
* ----------------------------------------------------------------------------- */
/* -----------------------------------------------------------------------------
* standard typemaps
* ----------------------------------------------------------------------------- */
/* NEW LANGUAGE NOTE:
the 'checkfn' param is something that I added for typemap(in)
it is an optional fn call to check the type of the lua object
the fn call must be of the form
int checkfn(lua_State *L, int index);
and return 1/0 depending upon if this is the correct type
For the typemap(out), an additional SWIG_arg parmeter must be incremented
to reflect the number of values returned (normally SWIG_arg++; will do)
*/
// numbers
%typemap(in,checkfn="lua_isnumber") int,short,long,
unsigned int,unsigned short,unsigned long,
signed char,unsigned char,
float,double
%{$1 = ($type)lua_tonumber(L, $input);%}
%typemap(out) int,short,long,
unsigned int,unsigned short,unsigned long,
signed char,unsigned char,
float,double
%{ lua_pushnumber(L, (lua_Number) $1); SWIG_arg++;%}
/* enums have to be handled slightly differently
VC++ .net will not allow a cast from double to enum directly
therefore cast to an int first.
Thanks to Jason Rego <JasonR@rainbowstudios.com> for finding this.
*/
%typemap(in,checkfn="lua_isnumber") enum SWIGTYPE
%{$1 = ($type)(int)lua_tonumber(L, $input);%}
%typemap(out) enum SWIGTYPE
%{ lua_pushnumber(L, (lua_Number)(int)($1)); SWIG_arg++;%}
// boolean (which is a special type in lua)
// note: 1 & 0 are not booleans in lua, only true & false
%typemap(in,checkfn="lua_isboolean") bool
%{$1 = (bool)lua_toboolean(L, $input);%}
%typemap(out) bool, const bool&
%{ lua_pushboolean(L,(int)$1); SWIG_arg++;%}
// for const bool&, SWIG treats this as a const bool* so we must dereference it
%typemap(in,checkfn="lua_isboolean") const bool& (bool temp)
%{temp=(bool)lua_toboolean(L, $input); $1=&temp;%}
%typemap(out) const bool&
%{ lua_pushboolean(L,(int)*$1); SWIG_arg++;%}
// strings (char* and char[])
%typemap(in,checkfn="lua_isstring") const char*, char*
%{$1 = ($1_ltype)lua_tostring(L, $input);%}
%typemap(in,checkfn="lua_isstring") const char[ANY], char[ANY]
%{$1 = (char*)lua_tostring(L, $input);%}
%typemap(out) const char*, char*
%{ lua_pushstring(L,(const char*)$1); SWIG_arg++;%}
%typemap(out) const char[ANY], char[ANY]
%{ lua_pushstring(L,$1); SWIG_arg++;%}
// char's
// currently treating chars as small strings, not as numbers
// (however signed & unsigned char's are numbers...)
%typemap(in,checkfn="lua_isstring") char
%{$1 = ((char*)lua_tostring(L, $input))[0];%}
%typemap(out) char
%{ lua_pushfstring(L,"%c",$1); SWIG_arg++;%}
// by const ref
%typemap(in,checkfn="lua_isstring") const char& (char temp)
%{temp = ((char*)lua_tostring(L, $input))[0]; $1=&temp;%}
%typemap(out) const char&
%{ lua_pushfstring(L,"%c",*$1); SWIG_arg++;%}
// pointers and references
// under SWIG rules, it is ok, to have a pass in a lua nil,
// it should be converted to a SWIG NULL.
// This will only be allowed for pointers & arrays, not refs or by value
// the checkfn lua_isuserdata will only work for userdata
// the checkfn SWIG_isptrtype will work for both userdata and nil's
%typemap(in,checkfn="SWIG_isptrtype") SWIGTYPE*,SWIGTYPE[]
%{
if (!SWIG_IsOK(SWIG_ConvertPtr(L,$input,(void**)&$1,$descriptor,0))){
SWIG_fail_ptr("$symname",$argnum,$descriptor);
}
%}
%typemap(in,checkfn="lua_isuserdata") SWIGTYPE&
%{
if (!SWIG_IsOK(SWIG_ConvertPtr(L,$input,(void**)&$1,$descriptor,0))){
SWIG_fail_ptr("$symname",$argnum,$descriptor);
}
%}
/*%typemap(in,checkfn="SWIG_isptrtype") SWIGTYPE*,SWIGTYPE[]
%{$1=($1_ltype)SWIG_MustGetPtr(L,$input,$descriptor,0,$argnum,"$symname");%}
%typemap(in,checkfn="lua_isuserdata") SWIGTYPE&
%{$1=($1_ltype)SWIG_MustGetPtr(L,$input,$descriptor,0,$argnum,"$symname");%}
*/
%typemap(out) SWIGTYPE*,SWIGTYPE&
%{SWIG_NewPointerObj(L,$1,$descriptor,$owner); SWIG_arg++; %}
// passing objects by value
// SWIG expects the object pointer (the $&ltype argp)
// then dereferences it
%typemap(in,checkfn="lua_isuserdata") SWIGTYPE ($&ltype argp)
%{
if (!SWIG_IsOK(SWIG_ConvertPtr(L,$input,(void**)(&argp),$&descriptor,0))){
SWIG_fail_ptr("$symname",$argnum,$descriptor);
}
$1 = *argp;
%}
// member function pointer
// currently does not work as C++ doesn't like casting member fn pointer to void*
/*%typemap(in,checkfn="lua_isuserdata") SWIGTYPE (CLASS::*)
%{
if (SWIG_ConvertPtr(L,$input,(void**)(&$1),$descriptor,0) == -1)
SWIG_fail_ptr("$symname",$argnum,$descriptor);
%}
%typemap(out) SWIGTYPE (CLASS::*)
%{SWIG_NewPointerObj(L,SWIG_as_voidptr($1),$descriptor,$owner); SWIG_arg++; %}*/
// Primitive types--return by value
// must make a new object, copy the data & return the new object
#ifdef __cplusplus
%typemap(out) SWIGTYPE
%{
$&1_ltype resultptr;
resultptr = new $1_ltype(($1_ltype &) $1);
SWIG_NewPointerObj(L,(void *) resultptr,$&1_descriptor,1); SWIG_arg++;
%}
#else
%typemap(out) SWIGTYPE
%{
$&1_ltype resultptr;
resultptr = ($&1_ltype) malloc(sizeof($1_type));
memmove(resultptr, &$1, sizeof($1_type));
SWIG_NewPointerObj(L,(void *) resultptr,$&1_descriptor,1); SWIG_arg++;
%}
#endif
/*#ifdef __cplusplus
%typemap(out) SWIGTYPE ($&1_ltype resultptr)
%{
resultptr = new $1_ltype(($1_ltype &) $1);
SWIG_NewPointerObj(L,(void *) resultptr,$&1_descriptor,1); SWIG_arg++;
%}
#else
%typemap(out) SWIGTYPE ($&1_ltype resultptr)
%{
resultptr = ($&1_ltype) malloc(sizeof($1_type));
memmove(resultptr, &$1, sizeof($1_type));
SWIG_NewPointerObj(L,(void *) resultptr,$&1_descriptor,1); SWIG_arg++;
%}
#endif*/
// void (must be empty without the SWIG_arg++)
%typemap(out) void "";
/* void* is a special case
A function void fn(void*) should take any kind of pointer as a parameter (just like C/C++ does)
but if its an output, then it should be wrappered like any other SWIG object (using default typemap)
*/
%typemap(in,checkfn="SWIG_isptrtype") void*
%{$1=($1_ltype)SWIG_MustGetPtr(L,$input,0,0,$argnum,"$symname");%}
/* long long is another special case:
as lua only supports one numeric type (lua_Number), we will just
cast it to that & accept the loss of precision.
An alternative solution would be a long long struct or class
with the relevant operators.
*/
%typemap(in,checkfn="lua_isnumber") long long, unsigned long long, signed long long
%{$1 = ($type)lua_tonumber(L, $input);%}
%typemap(out) long long, unsigned long long, signed long long
%{ lua_pushnumber(L, (lua_Number) $1); SWIG_arg++;%}
/* -----------------------------------------------------------------------------
* typecheck rules
* ----------------------------------------------------------------------------- */
/* These are needed for the overloaded functions
These define the detection routines which will spot what
parmeters match which function
*/
// unfortunately lua only considers one type of number
// so all numbers (int,float,double) match
// you could add an advanced fn to get type & check if its integral
%typecheck(SWIG_TYPECHECK_INTEGER)
int, short, long,
unsigned int, unsigned short, unsigned long,
signed char, unsigned char,
long long, unsigned long long, signed long long,
const int &, const short &, const long &,
const unsigned int &, const unsigned short &, const unsigned long &,
const long long &, const unsigned long long &,
enum SWIGTYPE, float, double,
const float &, const double &
{
$1 = lua_isnumber(L,$input);
}
%typecheck(SWIG_TYPECHECK_BOOL)
bool, const bool &
{
$1 = lua_isboolean(L,$input);
}
// special check for a char (string of length 1)
%typecheck(SWIG_TYPECHECK_CHAR) char {
$1 = lua_isstring(L,$input) && (lua_strlen(L,$input)==1);
}
%typecheck(SWIG_TYPECHECK_STRING) char * {
$1 = lua_isstring(L,$input);
}
%typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE *, SWIGTYPE [] {
void *ptr;
if (SWIG_isptrtype(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, $1_descriptor, 0)) {
$1 = 0;
} else {
$1 = 1;
}
}
%typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE, SWIGTYPE & {
void *ptr;
if (lua_isuserdata(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, $1_descriptor, 0)) {
$1 = 0;
} else {
$1 = 1;
}
}
%typecheck(SWIG_TYPECHECK_VOIDPTR) void * {
void *ptr;
if (SWIG_isptrtype(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, 0, 0)) {
$1 = 0;
} else {
$1 = 1;
}
}
/* -----------------------------------------------------------------------------
* Const reference issues
* ----------------------------------------------------------------------------- */
// additional typemaps for privitives by const reference:
// given a function:
// int intbyref(const int& i);
// SWIG assumes that this code will need a pointer to int to be passed in
// (this might be ok for passing objects by const ref, but not a primitive)
// therefore we add a set of blanket typemaps to fix this
// also a set for fns which return const X&
// %typemap(in,checkfn="lua_isnumber") const int &(int temp)
// %{ temp = (int)lua_tonumber(L,$input); $1=&temp;%}
// %typemap(out) const int&
// %{ lua_pushnumber(L, (lua_Number) *$1); SWIG_arg++;%}
// %typecheck(in,checkfn="lua_isnumber") const int &
// now the code
%define SWIG_NUMBER_BY_CONST_REF(TYPE)
%typemap(in,checkfn="lua_isnumber") const TYPE &($basetype temp)
%{ temp=($basetype)lua_tonumber(L,$input); $1=&temp;%}
%typemap(out) const TYPE&
%{ lua_pushnumber(L, (lua_Number) *$1); SWIG_arg++;%}
%enddef
SWIG_NUMBER_BY_CONST_REF(int);
SWIG_NUMBER_BY_CONST_REF(unsigned int);
SWIG_NUMBER_BY_CONST_REF(signed int);
SWIG_NUMBER_BY_CONST_REF(short);
SWIG_NUMBER_BY_CONST_REF(unsigned short);
SWIG_NUMBER_BY_CONST_REF(signed short);
SWIG_NUMBER_BY_CONST_REF(long);
SWIG_NUMBER_BY_CONST_REF(unsigned long);
SWIG_NUMBER_BY_CONST_REF(signed long);
//SWIG_NUMBER_BY_CONST_REF(char); // char's are small strings not numbers
SWIG_NUMBER_BY_CONST_REF(unsigned char);
SWIG_NUMBER_BY_CONST_REF(signed char);
SWIG_NUMBER_BY_CONST_REF(float);
SWIG_NUMBER_BY_CONST_REF(double);
SWIG_NUMBER_BY_CONST_REF(enum SWIGTYPE);
SWIG_NUMBER_BY_CONST_REF(long long);
SWIG_NUMBER_BY_CONST_REF(signed long long);
SWIG_NUMBER_BY_CONST_REF(unsigned long long);
// Also needed for object ptrs by const ref
// eg const A* ref_pointer(A* const& a);
// found in mixed_types.i
%typemap(in,checkfn="lua_isuserdata") SWIGTYPE* const &($*ltype temp)
%{temp=($*ltype)SWIG_MustGetPtr(L,$input,$*descriptor,0,$argnum,"$symname");
$1=&temp;%}
// and the typecheck code
%typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE* const &
{
void *ptr;
if (lua_isuserdata(L,$input)==0 || SWIG_ConvertPtr(L,$input, (void **) &ptr, $*descriptor, 0)) {
$1 = 0;
} else {
$1 = 1;
}
}

View file

@ -58,6 +58,8 @@ or provide a %apply statement
%{ lua_pushnumber(L, (lua_Number) *$1); SWIG_arg++;%}
%typemap(in) TYPE *INOUT = TYPE *INPUT;
%typemap(argout) TYPE *INOUT = TYPE *OUTPUT;
%typemap(in) TYPE &OUTPUT = TYPE *OUTPUT;
%typemap(argout) TYPE &OUTPUT = TYPE *OUTPUT;
%typemap(in) TYPE &INOUT = TYPE *INPUT;
%typemap(argout) TYPE &INOUT = TYPE *OUTPUT;
// const version (the $*ltype is the basic number without ptr or const's)
@ -67,12 +69,16 @@ or provide a %apply statement
%enddef
// now the code
SWIG_NUMBER_TYPEMAP(int); SWIG_NUMBER_TYPEMAP(unsigned int);
SWIG_NUMBER_TYPEMAP(short); SWIG_NUMBER_TYPEMAP(unsigned short);
SWIG_NUMBER_TYPEMAP(long); SWIG_NUMBER_TYPEMAP(unsigned long);
SWIG_NUMBER_TYPEMAP(int); SWIG_NUMBER_TYPEMAP(unsigned int); SWIG_NUMBER_TYPEMAP(signed int);
SWIG_NUMBER_TYPEMAP(short); SWIG_NUMBER_TYPEMAP(unsigned short); SWIG_NUMBER_TYPEMAP(signed short);
SWIG_NUMBER_TYPEMAP(long); SWIG_NUMBER_TYPEMAP(unsigned long); SWIG_NUMBER_TYPEMAP(signed long);
SWIG_NUMBER_TYPEMAP(float);
SWIG_NUMBER_TYPEMAP(double);
SWIG_NUMBER_TYPEMAP(enum SWIGTYPE);
SWIG_NUMBER_TYPEMAP(enum SWIGTYPE);
// also for long longs's
SWIG_NUMBER_TYPEMAP(long long); SWIG_NUMBER_TYPEMAP(unsigned long long); SWIG_NUMBER_TYPEMAP(signed long long);
// note we dont do char, as a char* is probably a string not a ptr to a single char
/* -----------------------------------------------------------------------------

View file

@ -52,6 +52,7 @@ char cvsroot_lua_cxx[] = "$Header$";
#define REPORT(T,D) // no info:
//#define REPORT(T,D) {Printf(stdout,T"\n");} // only title
//#define REPORT(T,D) {Printf(stdout,T"\n");display_mapping(D);} // the works
//#define REPORT(T,D) {Printf(stdout,T"\n");Swig_print_node(D);} // the works
void display_mapping(DOH* d)
{
@ -79,10 +80,7 @@ NEW LANGUAGE NOTE:END ************************************************/
static const char *usage = (char*)"\
Lua Options (available with -lua)\n\
(coming soon.)\n\n";
// -ldflags - Print runtime libraries to link with\n_
// -prefix name - Set a prefix to be appended to all names\n_
// -namespace - Build module into a Tcl 8 namespace. \n_
// -pkgversion - Set package version.\n\n";
/* NEW LANGUAGE NOTE:***********************************************
@ -585,7 +583,7 @@ NEW LANGUAGE NOTE:END ************************************************/
/* Substitute the function name */
Replaceall(f->code,"$symname",iname);
Replaceall(f->code,"$result","resultobj");
Replaceall(f->code,"$result","result");
/* Dump the function out */
Wrapper_print(f,f_wrappers);