From afda89dc49d9e1cd87a20926d67cd8422dfb82a5 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Tue, 20 Sep 2005 19:35:23 +0000 Subject: [PATCH] Mark Gossage patch 1295168 git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7470 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Examples/lua/check.list | 3 +++ Examples/lua/class/runme.lua | 14 ++++++++------ Examples/lua/simple/runme.lua | 18 ++++++++---------- Examples/lua/variables/runme.lua | 16 ++++++++-------- Lib/exception.i | 15 ++++++++++++--- Lib/lua/lua.swg | 25 +++++++++++++++++++------ Source/Modules/lua.cxx | 4 +++- 7 files changed, 61 insertions(+), 34 deletions(-) diff --git a/Examples/lua/check.list b/Examples/lua/check.list index 4cdf56ab0..b98b94d4d 100644 --- a/Examples/lua/check.list +++ b/Examples/lua/check.list @@ -1,4 +1,7 @@ # see top-level Makefile.in class +constants +functest +pointer simple variables diff --git a/Examples/lua/class/runme.lua b/Examples/lua/class/runme.lua index 58d430538..3104abe7a 100644 --- a/Examples/lua/class/runme.lua +++ b/Examples/lua/class/runme.lua @@ -3,13 +3,15 @@ -- This file illustrates class C++ interface generated -- by SWIG. --- importing (lua does not have a nice way to do this) -loadlibrary = loadlib("example.so","Example_Init") -if loadlibrary == nil then - loadlibrary = loadlib("example.dll","Example_Init") +---- importing ---- +if string.sub(_VERSION,1,7)=='Lua 5.0' then + -- lua5.0 doesnt have a nice way to do this + lib=loadlib('example.dll','Example_Init') or loadlib('example.so','Example_Init') + assert(lib)() +else + -- lua 5.1 does + require('example') end -assert(loadlibrary, "could not find dynamic libray") -loadlibrary() ----- Object creation ----- diff --git a/Examples/lua/simple/runme.lua b/Examples/lua/simple/runme.lua index d072cd4c4..9111db0f3 100644 --- a/Examples/lua/simple/runme.lua +++ b/Examples/lua/simple/runme.lua @@ -1,16 +1,14 @@ --- file: runme.lua - --- importing (lua does not have a nice way to do this) -loadlibrary = loadlib("example.so","Example_Init") -if loadlibrary == nil then - loadlibrary = loadlib("example.dll","Example_Init") +---- importing ---- +if string.sub(_VERSION,1,7)=='Lua 5.0' then + -- lua5.0 doesnt have a nice way to do this + lib=loadlib('example.dll','Example_Init') or loadlib('example.so','Example_Init') + assert(lib)() +else + -- lua 5.1 does + require('example') end -assert(loadlibrary, "could not find dynamic libray") -loadlibrary() -- Call our gcd() function -print("hello world") - x = 42 y = 105 g = example.gcd(x,y) diff --git a/Examples/lua/variables/runme.lua b/Examples/lua/variables/runme.lua index 75235a33e..08c24c743 100644 --- a/Examples/lua/variables/runme.lua +++ b/Examples/lua/variables/runme.lua @@ -1,12 +1,12 @@ --- file: example.lua - --- importing (lua does not have a nice way to do this) -loadlibrary = loadlib("example.so","Example_Init") -if loadlibrary == nil then - loadlibrary = loadlib("example.dll","Example_Init") +---- importing ---- +if string.sub(_VERSION,1,7)=='Lua 5.0' then + -- lua5.0 doesnt have a nice way to do this + lib=loadlib('example.dll','Example_Init') or loadlib('example.so','Example_Init') + assert(lib)() +else + -- lua 5.1 does + require('example') end -assert(loadlibrary, "could not find dynamic libray") -loadlibrary() -- Try to set the values of some global variables diff --git a/Lib/exception.i b/Lib/exception.i index 15c93b599..5770e64d1 100644 --- a/Lib/exception.i +++ b/Lib/exception.i @@ -204,7 +204,7 @@ SWIGINTERN void SWIG_JavaException(JNIEnv *jenv, int code, const char *msg) { SWIGINTERN void SWIG_exception_(int code, const char *msg) { char msg_buf[OCAML_MSG_BUF_LEN]; sprintf( msg_buf, "Exception(%d): %s\n", code, msg ); - failwith( msg_buf ); + failwith( msg_buf ); } #define SWIG_exception(a,b) SWIG_exception_((a),(b)) %} @@ -318,6 +318,15 @@ SWIGINTERN void SWIG_CSharpException(int code, const char *msg) { %enddef #endif // SWIGCSHARP +#ifdef SWIGLUA + +%{ +#define SWIG_exception(a,b)\ +{ lua_pushfstring(L,"%s:%s",#a,b);SWIG_fail; } +%} + +#endif // SWIGLUA + #ifdef __cplusplus /* You can use the SWIG_CATCH_STDEXCEPT macro with the %exception @@ -330,11 +339,11 @@ SWIGINTERN void SWIG_CSharpException(int code, const char *msg) { catch (my_except& e) { ... } - SWIG_CATCH_STDEXCEPT // catch std::exception + SWIG_CATCH_STDEXCEPT // catch std::exception catch (...) { SWIG_exception(SWIG_UnknownError, "Unknown exception"); } - } + } */ %{ #include diff --git a/Lib/lua/lua.swg b/Lib/lua/lua.swg index 4d98e2609..744be2d2c 100644 --- a/Lib/lua/lua.swg +++ b/Lib/lua/lua.swg @@ -71,7 +71,7 @@ %typemap(out) char %{ lua_pushfstring(L,"%c",$1); SWIG_arg++;%} -// byref +// by const ref %typemap(in,checkfn="lua_isstring") const char& (char temp) %{temp = ((char*)lua_tostring(L, $input))[0]; $1=&temp;%} @@ -81,7 +81,6 @@ // pointers and references %typemap(in,checkfn="lua_isuserdata") SWIGTYPE*,SWIGTYPE&,SWIGTYPE[] -//%{if(SWIG_ConvertPtr(L,$input,(void**)(&$1),$descriptor,SWIG_POINTER_EXCEPTION) == -1) SWIG_fail; %} %{$1=($1_ltype)SWIG_MustGetPtr(L,$input,$descriptor,0,$argnum,"$symname");%} %typemap(out) SWIGTYPE*,SWIGTYPE& @@ -353,6 +352,9 @@ 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 Therefore currently its just enough to get a few test cases running ok + +note: if you wish to throw anything related to std::exception +use %include instead */ %typemap(throws) int,unsigned int,signed int, long,unsigned long,signed long, @@ -364,11 +366,11 @@ Therefore currently its just enough to get a few test cases running ok %{lua_pushfstring(L,"exception thrown of value %d",(long)$1); SWIG_fail; %} -#include "exception.h" -#define SWIG_exception(a,b)\ -{ lua_pushfstring(L,"%s thrown:%s",#a,b);SWIG_fail; } - +// strings are just sent as errors +%typemap(throws) char*, const char* +%{lua_pushstring(L,$1);SWIG_fail;%} +// anything else is sent as an object #ifdef __cplusplus %typemap(throws) SWIGTYPE { @@ -444,6 +446,17 @@ SWIGEXPORT int SWIG_init(lua_State* L) 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 +#ifdef __cplusplus +extern "C" +#endif +SWIGEXPORT int SWIG_import(lua_State* L) +{ + return SWIG_init(L); +} + %} /* Note: the initialization function is closed after all code is generated */ diff --git a/Source/Modules/lua.cxx b/Source/Modules/lua.cxx index fda267f5d..005a5d72d 100644 --- a/Source/Modules/lua.cxx +++ b/Source/Modules/lua.cxx @@ -213,7 +213,9 @@ NEW LANGUAGE NOTE:END ************************************************/ String *init_name = NewStringf("%(title)s_Init",module); Printf(f_header, "#define SWIG_init %s\n", init_name); - Printf(f_header, "#define SWIG_name \"%s\"\n\n", module); + Printf(f_header, "#define SWIG_name \"%s\"\n", module); + /* SWIG_import is a special function name for importing within Lua5.1 */ + Printf(f_header, "#define SWIG_import luaopen_%s\n\n", module); Printf(s_cmd_tab, "\nstatic swig_lua_command_info swig_commands[] = {\n"); Printf(s_var_tab, "\nstatic swig_lua_var_info swig_variables[] = {\n");