Version franÁaise
Home ††† About ††† Download ††† Resources ††† Contact us †††
Browse thread
Wrapping var_args, or C ... in ocaml?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Guillaume Yziquel <guillaume.yziquel@c...>
Subject: Re: [Caml-list] Wrapping var_args, or C ... in ocaml?
Guillaume Yziquel a écrit :
> Florent Monnier a écrit :
>> Le dimanche 14 février 2010 23:46:10, Guillaume Yziquel a écrit :
>>> I mean, it seems that varargs means on the receiving end "the number 
>>> of arguments you'r giving me, as a function, is not limited", whereas 
>>> on the sending end, you hard-code the number of arguments in your C 
>>> code.
>>>
>>> Is there a way to map an OCaml list to an ellipsis? Or is it a C 
>>> limitation?
>>
>> Yes, as long as I know, for this you should use these kind of tools:
>> http://sourceware.org/libffi/
>> http://www.gnu.org/software/libffcall/avcall.html
>> http://www.nongnu.org/cinvoke/
> 
> Phew! These tools are quite tough to handle! (I just tried to use 
> avcall.h on this ellipsis.)

Got it.

The correct code is:

> CAMLprim value ocamlpython_py_tuple_pack (value ml_len, value ml_pyobjs) {
> 
>   av_alist argList;
>   PyObject * retVal;
>   av_start_ptr(argList, &PyTuple_Pack, PyObject*, &retVal);
> 
>   #if defined(__s390__) || defined(__hppa__) || defined(__cris__)
>   #define av_Py_ssize_t av_long
>   #else
>   #define av_Py_ssize_t av_int
>   #endif
> 
>   av_Py_ssize_t(argList, Pyoffset_val(ml_len));
>   while (ml_pyobjs != Val_emptylist) {
>     av_ptr(argList, PyObject*, Pyobj_val(Field(ml_pyobjs, 0)));
>     ml_pyobjs = Field(ml_pyobjs, 1);
>   }
> 
>   av_call(argList);
>   return(Val_owned_pyobj(retVal));
> }

Hope that will be useful to people wishing to wrap up varargs (though 
it's probably a bad idea, since the stack is limited...)

Python function calls are now possible by constructing the argument tuple.

-- 
      Guillaume Yziquel
http://yziquel.homelinux.org/