diff options
author | Nils Gladitz <nilsgladitz@gmail.com> | 2015-07-01 12:24:12 +0200 |
---|---|---|
committer | Nils Gladitz <nilsgladitz@gmail.com> | 2015-07-01 12:24:12 +0200 |
commit | ca208cfe35033b31fb7d05c160278c2eb583f499 (patch) | |
tree | 28d48245f0b11153d2ceeb2c032aa6cf194ccee1 | |
parent | 8208d12aa55fff495685f6b58c0db4f4c9721278 (diff) | |
download | swig-ca208cfe35033b31fb7d05c160278c2eb583f499.tar.gz |
lua: push characters as unformatted 1-character strings
Since Lua 5.3 the "%c" format character in lua_pushfstring will produce
the string "<\XXX>" (XXX being a decimal code sequence) when
given unprintable characters.
Use lua_pushlstring instead to reproduce the old behavior.
-rw-r--r-- | Examples/test-suite/chartest.i | 15 | ||||
-rw-r--r-- | Examples/test-suite/common.mk | 1 | ||||
-rw-r--r-- | Examples/test-suite/lua/chartest_runme.lua | 14 | ||||
-rw-r--r-- | Lib/lua/lua.swg | 2 | ||||
-rw-r--r-- | Lib/lua/luarun.swg | 5 | ||||
-rw-r--r-- | Lib/lua/luatypemaps.swg | 4 |
6 files changed, 37 insertions, 4 deletions
diff --git a/Examples/test-suite/chartest.i b/Examples/test-suite/chartest.i new file mode 100644 index 000000000..e81cf54a4 --- /dev/null +++ b/Examples/test-suite/chartest.i @@ -0,0 +1,15 @@ +%module chartest + +%inline %{ +char printable_global_char = 'a'; +char unprintable_global_char = 0x7F; + +char GetPrintableChar() { + return 'a'; +} + +char GetUnprintableChar() { + return 0x7F; +} + +%} diff --git a/Examples/test-suite/common.mk b/Examples/test-suite/common.mk index 12b70719b..7457b815a 100644 --- a/Examples/test-suite/common.mk +++ b/Examples/test-suite/common.mk @@ -138,6 +138,7 @@ CPP_TEST_CASES += \ casts \ char_binary \ char_strings \ + chartest \ class_forward \ class_ignore \ class_scope_weird \ diff --git a/Examples/test-suite/lua/chartest_runme.lua b/Examples/test-suite/lua/chartest_runme.lua new file mode 100644 index 000000000..e15f3dc6b --- /dev/null +++ b/Examples/test-suite/lua/chartest_runme.lua @@ -0,0 +1,14 @@ +require("import") -- the import fn +import("chartest") -- import code + +function char_assert(char, code) + assert(type(char) == 'string') + assert(char:len() == 1) + assert(char:byte() == code) +end + +char_assert(chartest.GetPrintableChar(), 0x61) +char_assert(chartest.GetUnprintableChar(), 0x7F) + +char_assert(chartest.printable_global_char, 0x61) +char_assert(chartest.unprintable_global_char, 0x7F) diff --git a/Lib/lua/lua.swg b/Lib/lua/lua.swg index 892d15798..9c21d0812 100644 --- a/Lib/lua/lua.swg +++ b/Lib/lua/lua.swg @@ -185,7 +185,7 @@ use %include <std_except.i> instead // char is changed to a string %typemap(throws) char -%{lua_pushfstring(L,"%c",$1);SWIG_fail;%} +%{lua_pushlstring(L,&$1,1);SWIG_fail;%} /* Throwing object is a serious problem: diff --git a/Lib/lua/luarun.swg b/Lib/lua/luarun.swg index 0ab045287..721fac22b 100644 --- a/Lib/lua/luarun.swg +++ b/Lib/lua/luarun.swg @@ -1841,7 +1841,10 @@ SWIG_Lua_InstallConstants(lua_State *L, swig_lua_const_info constants[]) { break; case SWIG_LUA_CHAR: lua_pushstring(L,constants[i].name); - lua_pushfstring(L,"%c",(char)constants[i].lvalue); + { + char c = constants[i].lvalue; + lua_pushlstring(L,&c,1); + } lua_rawset(L,-3); break; case SWIG_LUA_STRING: diff --git a/Lib/lua/luatypemaps.swg b/Lib/lua/luatypemaps.swg index c86b40d74..5d98e6107 100644 --- a/Lib/lua/luatypemaps.swg +++ b/Lib/lua/luatypemaps.swg @@ -122,14 +122,14 @@ SWIGINTERN int SWIG_lua_isnilstring(lua_State *L, int idx) { %{$1 = (lua_tostring(L, $input))[0];%} %typemap(out) char -%{ lua_pushfstring(L,"%c",$1); SWIG_arg++;%} +%{ lua_pushlstring(L, &$1, 1); SWIG_arg++;%} // by const ref %typemap(in,checkfn="SWIG_lua_isnilstring",fragment="SWIG_lua_isnilstring") const char& (char temp) %{temp = (lua_tostring(L, $input))[0]; $1=&temp;%} %typemap(out) const char& -%{ lua_pushfstring(L,"%c",*$1); SWIG_arg++;%} +%{ lua_pushlstring(L, $1, 1); SWIG_arg++;%} // pointers and references // under SWIG rules, it is ok, to have a pass in a lua nil, |