Merge branch 'more_argcargv'

* more_argcargv:
  Document argc argv library
  argcargv.i cosmetic updates
  Typemaps for (int ARGC, char **ARGV) fixup
  Fix argcargv.i in Perl5, Tcl, PHP Add missing type map for type check. Add testing of argcargv.i for Perl5, Tcl, PHP and Ruby.
  Add Lua test for argcargv.i
  Add argcargv.i to more languages: Perl 5, Tcl, PHP
  Add argcargv.i to Lua
This commit is contained in:
William S Fulton 2022-05-15 19:42:25 +01:00
commit d60724b636
15 changed files with 360 additions and 23 deletions

57
Lib/lua/argcargv.i Normal file
View file

@ -0,0 +1,57 @@
/* ------------------------------------------------------------
* SWIG library containing argc and argv multi-argument typemaps
Use it as follows:
%apply (int ARGC, char **ARGV) { (size_t argc, const char **argv) }
extern int mainApp(size_t argc, const char **argv);
then from 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);
}

32
Lib/perl5/argcargv.i Normal file
View file

@ -0,0 +1,32 @@
/* ------------------------------------------------------------
* SWIG library containing argc and argv multi-argument typemaps
* ------------------------------------------------------------ */
%typemap(in) (int ARGC, char **ARGV) {
int i;
I32 len;
AV *av = (AV *)SvRV($input);
if (SvTYPE(av) != SVt_PVAV) {
SWIG_croak("in method '$symname', Expecting reference to argv array");
goto fail;
}
len = av_len(av) + 1;
$1 = ($1_ltype) len;
$2 = (char **) malloc((len+1)*sizeof(char *));
for (i = 0; i < len; i++) {
SV **tv = av_fetch(av, i, 0);
$2[i] = SvPV_nolen(*tv);
}
$2[i] = NULL;
}
%typemap(typecheck, precedence=SWIG_TYPECHECK_STRING_ARRAY) (int ARGC, char **ARGV) {
AV *av = (AV *)SvRV($input);
$1 = SvTYPE(av) == SVt_PVAV;
}
%typemap(freearg) (int ARGC, char **ARGV) {
if ($2 != NULL) {
free((void *)$2);
}
}

40
Lib/php/argcargv.i Normal file
View file

@ -0,0 +1,40 @@
/* ------------------------------------------------------------
* SWIG library containing argc and argv multi-argument typemaps
* ------------------------------------------------------------ */
%typemap(in) (int ARGC, char **ARGV) {
int len, i;
zval *val;
zend_array *ar;
if (Z_TYPE($input) != IS_ARRAY) {
SWIG_PHP_Error(E_ERROR, "Type error in '$symname'. Expected array");
goto fail;
}
ar = Z_ARR($input);
len = zend_array_count(ar);
$1 = ($1_ltype) len;
$2 = (char **) malloc((len+1)*sizeof(char *));
i = 0;
ZEND_HASH_FOREACH_VAL(ar, val) {
if (Z_TYPE(*val) != IS_STRING) {
SWIG_PHP_Error(E_ERROR, "Array must use strings only, in '$symname'.");
goto fail;
}
if (i == len) {
SWIG_PHP_Error(E_ERROR, "Array is bigger than zend report in '$symname'.");
goto fail;
}
$2[i++] = Z_STRVAL(*val);
} ZEND_HASH_FOREACH_END();
$2[i] = NULL;
}
%typemap(typecheck, precedence=SWIG_TYPECHECK_STRING_ARRAY) (int ARGC, char **ARGV) {
$1 = Z_TYPE($input) == IS_ARRAY;
}
%typemap(freearg) (int ARGC, char **ARGV) {
if ($2 != NULL) {
free((void *)$2);
}
}

View file

@ -1,13 +1,10 @@
/* ------------------------------------------------------------
* --- Argc & Argv ---
* SWIG library containing argc and argv multi-argument typemaps
* ------------------------------------------------------------ */
%fragment("SWIG_AsArgcArgv","header",fragment="SWIG_AsCharPtrAndSize") {
SWIGINTERN int
SWIG_AsArgcArgv(PyObject *input,
swig_type_info *ppchar_info,
size_t *argc, char ***argv, int *owner)
{
SWIG_AsArgcArgv(PyObject *input, swig_type_info *ppchar_info, size_t *argc, char ***argv, int *owner) {
void *vptr;
int res = SWIG_ConvertPtr(input, &vptr, ppchar_info, 0);
if (!SWIG_IsOK(res)) {
@ -51,7 +48,7 @@ SWIG_AsArgcArgv(PyObject *input,
} else {
/* seems dangerous, but the user asked for it... */
size_t i = 0;
if (argv) { while (*argv[i] != 0) ++i;}
if (argv) { while (*argv[i] != 0) ++i;}
if (argc) *argc = i;
if (owner) *owner = 0;
return SWIG_OK;
@ -66,10 +63,10 @@ SWIG_AsArgcArgv(PyObject *input,
%typemap(in,noblock=0,fragment="SWIG_AsArgcArgv") (int ARGC, char **ARGV) (int res,char **argv = 0, size_t argc = 0, int owner= 0) {
res = SWIG_AsArgcArgv($input, $descriptor(char**), &argc, &argv, &owner);
if (!SWIG_IsOK(res)) {
if (!SWIG_IsOK(res)) {
$1 = 0; $2 = 0;
%argument_fail(SWIG_TypeError, "int ARGC, char **ARGV", $symname, $argnum);
} else {
} else {
$1 = %static_cast(argc,$1_ltype);
$2 = %static_cast(argv, $2_ltype);
}

View file

@ -1,24 +1,20 @@
/* ------------------------------------------------------------
* --- Argc & Argv ---
* ------------------------------------------------------------ */
/* ------------------------------------------------------------
* SWIG library containing argc and argv multi-argument typemaps
Use it as follow:
Use it as follows:
%apply (int ARGC, char **ARGV) { (size_t argc, const char **argv) }
%inline %{
int mainApp(size_t argc, const char **argv)
{
int mainApp(size_t argc, const char **argv) {
return argc;
}
then in the ruby side:
then from ruby:
args = ["asdf", "asdf2"]
mainApp(args);
$args = ["asdf", "asdf2"]
mainApp(args)
* ------------------------------------------------------------ */
@ -31,7 +27,7 @@
VALUE *ptr = RARRAY_PTR($input);
for (i=0; i < size; i++, ptr++) {
$2[i]= StringValuePtr(*ptr);
}
}
$2[i]=NULL;
} else {
$1 = 0; $2 = 0;

29
Lib/tcl/argcargv.i Normal file
View file

@ -0,0 +1,29 @@
/* ------------------------------------------------------------
* SWIG library containing argc and argv multi-argument typemaps
* ------------------------------------------------------------ */
%typemap(in) (int ARGC, char **ARGV) {
int i, nitems;
Tcl_Obj **listobjv;
if (Tcl_ListObjGetElements(interp, $input, &nitems, &listobjv) == TCL_ERROR) {
SWIG_exception_fail(SWIG_ValueError, "in method '$symname', Expecting list of argv");
goto fail;
}
$1 = ($1_ltype) nitems;
$2 = (char **) malloc((nitems+1)*sizeof(char *));
for (i = 0; i < nitems; i++) {
$2[i] = Tcl_GetStringFromObj(listobjv[i], NULL);
}
$2[i] = NULL;
}
%typemap(typecheck, precedence=SWIG_TYPECHECK_STRING_ARRAY) (int ARGC, char **ARGV) {
int len;
$1 = Tcl_ListObjLength(interp, $input, &len) == TCL_OK;
}
%typemap(freearg) (int ARGC, char **ARGV) {
if ($2 != NULL) {
free((void *)$2);
}
}