summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam S Fulton <wsf@fultondesigns.co.uk>2011-08-22 19:27:56 +0000
committerWilliam S Fulton <wsf@fultondesigns.co.uk>2011-08-22 19:27:56 +0000
commit932f47a84540ddb103239f3e10c662988b396232 (patch)
tree21b3834293460076c6f2874b263df246bdbd192c
parent61124e61a75f08dad76c2283b0aa2fe68e790302 (diff)
downloadswig-932f47a84540ddb103239f3e10c662988b396232.tar.gz
SF patch #3394339 from Torsten Landschoff - new option -nomoduleglobal to disable installing the module table into the global namespace. Require call also returns the module table instead of a string
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12780 626c5289-ae23-0410-ae9c-e8d60b6d4f22
-rw-r--r--CHANGES.current5
-rw-r--r--Doc/Manual/Lua.html23
-rw-r--r--Examples/test-suite/lua/Makefile.in12
-rw-r--r--Examples/test-suite/lua/lua_no_module_global_runme.lua24
-rw-r--r--Examples/test-suite/lua_no_module_global.i5
-rw-r--r--Lib/lua/luarun.swg10
-rw-r--r--Lib/lua/luaruntime.swg5
-rw-r--r--Source/Modules/lua.cxx14
8 files changed, 87 insertions, 11 deletions
diff --git a/CHANGES.current b/CHANGES.current
index e9d47b54c..33bb5b909 100644
--- a/CHANGES.current
+++ b/CHANGES.current
@@ -5,6 +5,11 @@ See the RELEASENOTES file for a summary of changes in each release.
Version 2.0.5 (in progress)
===========================
+2011-08-22: wsfulton
+ [Lua] SF patch #3394339 from Torsten Landschoff - new option -nomoduleglobal to disable installing
+ the module table into the global namespace. Require call also returns the module table instead
+ of a string.
+
2011-08-12: wsfulton
SF bug # 3333549 - %shared_ptr fixes when the type is a template using template parameters
that are typedef'd to another type.
diff --git a/Doc/Manual/Lua.html b/Doc/Manual/Lua.html
index 04f96f2f3..218a9a498 100644
--- a/Doc/Manual/Lua.html
+++ b/Doc/Manual/Lua.html
@@ -105,6 +105,29 @@ This creates a C/C++ source file <tt>example_wrap.c</tt> or <tt>example_wrap.cxx
<p>
The name of the wrapper file is derived from the name of the input file. For example, if the input file is <tt>example.i</tt>, the name of the wrapper file is <tt>example_wrap.c</tt>. To change this, you can use the -o option. The wrappered module will export one function <tt>"int luaopen_example(lua_State* L)"</tt> which must be called to register the module with the Lua interpreter. The name "luaopen_example" depends upon the name of the module.
</p>
+
+<H3><a name="Lua_commandline"></a>25.2.1 Additional command line options</H3>
+
+<p>
+The following table list the additional commandline options available for the Lua module. They can also be seen by using:
+</p>
+
+<div class="code"><pre>
+swig -lua -help
+</pre></div>
+
+<table summary="Lua specific options">
+<tr>
+<th>Lua specific options</th>
+</tr>
+
+<tr>
+<td>-nomoduleglobal</td>
+<td>Do not register the module name as a global variable but return the module table from calls to require.</td>
+</tr>
+
+</table>
+
<H3><a name="Lua_nn4"></a>25.2.1 Compiling and Linking and Interpreter</H3>
diff --git a/Examples/test-suite/lua/Makefile.in b/Examples/test-suite/lua/Makefile.in
index 0ddc86a7d..d6a6554f4 100644
--- a/Examples/test-suite/lua/Makefile.in
+++ b/Examples/test-suite/lua/Makefile.in
@@ -11,12 +11,12 @@ top_builddir = @top_builddir@
# sorry, currently very few test cases work/have been written
-#CPP_TEST_CASES += \
-# cnum
+CPP_TEST_CASES += \
+ lua_no_module_global \
-#C_TEST_CASES += \
-# file_test \
-# nondynamic
+
+C_TEST_CASES += \
+ lua_no_module_global \
include $(srcdir)/../common.mk
@@ -25,7 +25,7 @@ include $(srcdir)/../common.mk
LIBS = -L.
# Custom tests - tests with additional commandline options
-# none!
+lua_no_module_global.%: SWIGOPT += -nomoduleglobal
# Rules for the different types of tests
%.cpptest:
diff --git a/Examples/test-suite/lua/lua_no_module_global_runme.lua b/Examples/test-suite/lua/lua_no_module_global_runme.lua
new file mode 100644
index 000000000..9eb436c3f
--- /dev/null
+++ b/Examples/test-suite/lua/lua_no_module_global_runme.lua
@@ -0,0 +1,24 @@
+-- require is only available in Lua 5.1
+
+if string.sub(_VERSION,1,7)=='Lua 5.1' then
+
+ -- Initially the package should not be loaded
+ assert(package.loaded["lua_no_module_global"] == nil)
+
+ -- Load the module
+ the_module = require "lua_no_module_global"
+
+ -- require should return the module table
+ assert(the_module.hi_mom ~= nil)
+ assert(the_module.hi_mom() == "hi mom!")
+
+ -- But it should not end up in the global table _G, subject to
+ -- the -nomoduleglobal swig option.
+ assert(_G["lua_no_module_global"] == nil)
+
+ -- According to the Lua 5.1 reference manual, require should also
+ -- store the module table into package.loaded["name"]
+ assert(package.loaded["lua_no_module_global"] == the_module)
+ assert(package.loaded["lua_no_module_global"].hi_mom() == "hi mom!")
+
+end
diff --git a/Examples/test-suite/lua_no_module_global.i b/Examples/test-suite/lua_no_module_global.i
new file mode 100644
index 000000000..7f71788f5
--- /dev/null
+++ b/Examples/test-suite/lua_no_module_global.i
@@ -0,0 +1,5 @@
+%module lua_no_module_global
+%{
+ const char *hi_mom() { return "hi mom!"; }
+%}
+const char *hi_mom();
diff --git a/Lib/lua/luarun.swg b/Lib/lua/luarun.swg
index 89b762637..1810e9895 100644
--- a/Lib/lua/luarun.swg
+++ b/Lib/lua/luarun.swg
@@ -237,7 +237,7 @@ SWIGINTERN int SWIG_Lua_module_set(lua_State* L)
return 0;
}
-/* registering a module in lua */
+/* registering a module in lua. Pushes the module table on the stack. */
SWIGINTERN void SWIG_Lua_module_begin(lua_State* L,const char* name)
{
assert(lua_istable(L,-1)); /* just in case */
@@ -254,8 +254,16 @@ SWIGINTERN void SWIG_Lua_module_begin(lua_State* L,const char* name)
lua_newtable(L); /* the .set table */
lua_rawset(L,-3); /* add .set into metatable */
lua_setmetatable(L,-2); /* sets meta table in module */
+#ifdef SWIG_LUA_MODULE_GLOBAL
+ /* If requested, install the module directly into the global namespace. */
lua_rawset(L,-3); /* add module into parent */
SWIG_Lua_get_table(L,name); /* get the table back out */
+#else
+ /* Do not install the module table as global name. The stack top has
+ the module table with the name below. We pop the top and replace
+ the name with it. */
+ lua_replace(L,-2);
+#endif
}
/* ending the register */
diff --git a/Lib/lua/luaruntime.swg b/Lib/lua/luaruntime.swg
index 5823d4fcf..e28644510 100644
--- a/Lib/lua/luaruntime.swg
+++ b/Lib/lua/luaruntime.swg
@@ -59,8 +59,9 @@ SWIGEXPORT int SWIG_init(lua_State* L)
/* invoke user-specific initialization */
SWIG_init_user(L);
/* end module */
- lua_pop(L,1); /* tidy stack (remove module table)*/
- lua_pop(L,1); /* tidy stack (remove global table)*/
+ /* Note: We do not clean up the stack here (Lua will do this for us). At this
+ point, we have the globals table and out module table on the stack. Returning
+ one value makes the module table the result of the require command. */
return 1;
}
diff --git a/Source/Modules/lua.cxx b/Source/Modules/lua.cxx
index c38ffdae9..9b4bb6ee3 100644
--- a/Source/Modules/lua.cxx
+++ b/Source/Modules/lua.cxx
@@ -83,10 +83,11 @@ void display_mapping(DOH *d) {
NEW LANGUAGE NOTE:END ************************************************/
static const char *usage = (char *) "\
Lua Options (available with -lua)\n\
- [no additional options]\n\
+ -nomoduleglobal - Do not register the module name as a global variable \n\
+ but return the module table from calls to require.\n\
\n";
-
+static int nomoduleglobal = 0;
/* NEW LANGUAGE NOTE:***********************************************
To add a new language, you need to derive your class from
@@ -172,6 +173,9 @@ public:
if (argv[i]) {
if (strcmp(argv[i], "-help") == 0) { // usage flags
fputs(usage, stdout);
+ } else if (strcmp(argv[i], "-nomoduleglobal") == 0) {
+ nomoduleglobal = 1;
+ Swig_mark_arg(i);
}
}
}
@@ -263,6 +267,12 @@ public:
Printf(f_runtime, "\n");
Printf(f_runtime, "#define SWIGLUA\n");
+ if (nomoduleglobal) {
+ Printf(f_runtime, "#define SWIG_LUA_NO_MODULE_GLOBAL\n");
+ } else {
+ Printf(f_runtime, "#define SWIG_LUA_MODULE_GLOBAL\n");
+ }
+
// if (NoInclude) {
// Printf(f_runtime, "#define SWIG_NOINCLUDE\n");
// }