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
This commit is contained in:
William S Fulton 2011-08-22 19:27:56 +00:00
parent 61124e61a7
commit 932f47a845
8 changed files with 87 additions and 11 deletions

View File

@ -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.

View File

@ -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>

View File

@ -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:

View File

@ -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

View File

@ -0,0 +1,5 @@
%module lua_no_module_global
%{
const char *hi_mom() { return "hi mom!"; }
%}
const char *hi_mom();

View File

@ -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 */

View File

@ -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;
}

View File

@ -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");
// }