Merge branch 'erezgeva-master' into more_argcargv
* erezgeva-master: Add Lua test for argcargv.i Add argcargv.i to Lua
This commit is contained in:
commit
b15fed7d2d
2 changed files with 84 additions and 0 deletions
26
Examples/test-suite/lua/argcargvtest_runme.lua
Normal file
26
Examples/test-suite/lua/argcargvtest_runme.lua
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
require("import") -- the import fn
|
||||
import("argcargvtest") -- import lib
|
||||
v = argcargvtest
|
||||
|
||||
-- catch "undefined" global variables
|
||||
local env = _ENV -- Lua 5.2
|
||||
if not env then env = getfenv () end -- Lua 5.1
|
||||
setmetatable(env, {__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
|
||||
|
||||
largs = {"hi", "hola", "hello"}
|
||||
assert(v.mainc(largs) == 3, "bad main typemap")
|
||||
|
||||
targs = {"hi", "hola"}
|
||||
assert(v.mainv(targs, 1) == "hola", "bad main typemap")
|
||||
|
||||
targs = {"hi", "hola"}
|
||||
assert(v.mainv(targs, 1) == "hola", "bad main typemap")
|
||||
|
||||
errorVal = 0
|
||||
function try()
|
||||
mainv("hello", 1)
|
||||
errorVal = 1
|
||||
end
|
||||
assert(not pcall(try) and errorVal == 0, "bad main typemap")
|
||||
|
||||
v.initializeApp(largs)
|
||||
58
Lib/lua/argcargv.i
Normal file
58
Lib/lua/argcargv.i
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
/* ------------------------------------------------------------
|
||||
* --- Argc & Argv ---
|
||||
* ------------------------------------------------------------
|
||||
*
|
||||
* Use it as follow:
|
||||
*
|
||||
* %apply (int ARGC, char **ARGV) { (size_t argc, const char **argv) }
|
||||
* extern int mainApp(size_t argc, const char **argv);
|
||||
*
|
||||
* then in the lua:
|
||||
*
|
||||
* args = { "arg0", "arg1" }
|
||||
* mainApp(args);
|
||||
*
|
||||
* ------------------------------------------------------------ */
|
||||
|
||||
%{
|
||||
SWIGINTERN int SWIG_argv_size(lua_State* L, int index) {
|
||||
int n=0;
|
||||
while(1){
|
||||
lua_rawgeti(L,index,n+1);
|
||||
if (lua_isnil(L,-1))
|
||||
break;
|
||||
++n;
|
||||
lua_pop(L,1);
|
||||
}
|
||||
lua_pop(L,1);
|
||||
return n;
|
||||
}
|
||||
%}
|
||||
|
||||
%typemap(in) (int ARGC, char **ARGV) {
|
||||
if (lua_istable(L,$input)) {
|
||||
int i, size = SWIG_argv_size(L,$input);
|
||||
$1 = ($1_ltype) size;
|
||||
$2 = (char **) malloc((size+1)*sizeof(char *));
|
||||
for (i = 0; i < size; i++) {
|
||||
lua_rawgeti(L,$input,i+1);
|
||||
if (lua_isnil(L,-1))
|
||||
break;
|
||||
$2[i] = (char *)lua_tostring(L, -1);
|
||||
lua_pop(L,1);
|
||||
}
|
||||
$2[i]=NULL;
|
||||
} else {
|
||||
$1 = 0; $2 = 0;
|
||||
lua_pushstring(L,"Expecting argv array");
|
||||
lua_error(L);
|
||||
}
|
||||
}
|
||||
|
||||
%typemap(typecheck, precedence=SWIG_TYPECHECK_STRING_ARRAY) (int ARGC, char **ARGV) {
|
||||
$1 = lua_istable(L,$input);
|
||||
}
|
||||
|
||||
%typemap(freearg) (int ARGC, char **ARGV) {
|
||||
free((char *) $2);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue