There is a long standing request in Debian to make SWIG wrapped Lua modules return the module table when importing the module:
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=468414
As it turns out, this is not too hard to fix. While looking at it, I also noticed that loading a SWIG-generated Lua module will also clobber a global variable of the same name (often not a problem, but who knows).
The attached patch adds a new option -nomoduleglobal to disable installing the module table into the global namespace _G.
There is a slight change in behaviour: In any case, the require call to load a module will now return the module table. Originally, the name of the module was returned (as a string), which does not seem very useful for me.
I included a unit test with the patch.
Please consider applying the patch,
Torsten
Make require always return the module's table and optionally skip the global registration
Some small tweaks to the original patch adding in html documentation and Makefile changes and warning fixes
I've put in a slightly modified patch for cosmetic type changes. However, my main concern is that if the test is run as a C++ test, there is an assertion...
~/swig/trunk-2/Examples/test-suite/lua$ make -s lua_no_module_global.cpptest
checking testcase lua_no_module_global (with run test) under lua
/usr/bin/lua: ./lua_no_module_global_runme.lua:17: assertion failed!
stack traceback:
[C]: in function 'assert'
./lua_no_module_global_runme.lua:17: in main chunk
[C]: ?
make: *** [lua_no_module_global.cpptest] Error 1
Can you fix it for C++?
The C++ test is trivial to fix - SWIG is not invoked with the correct options. I missed that the C tests can also run as C++ tests. This is fixed by
--- a/Examples/test-suite/lua/Makefile.in
+++ b/Examples/test-suite/lua/Makefile.in
@@ -24,7 +24,7 @@ include $(srcdir)/../common.mk
LIBS = -L.
# Custom tests - tests with additional commandline options
-lua_no_module_global.ctest: SWIGOPT += -nomoduleglobal
+lua_no_module_global.%: SWIGOPT += -nomoduleglobal
# Rules for the different types of tests
%.cpptest:
I will combine this patch with your changes and attach the updated patch.
Fix the C++ test
Applied to svn for swig-2.0.5. Thanks.