From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 12EF7BBAF for ; Sat, 20 Feb 2010 03:40:43 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApYBAIvafktV2gB5hWdsb2JhbACDBZgbAQEBCgsKBRUirwqPWYExglNkBIld X-IronPort-AV: E=Sophos;i="4.49,506,1262559600"; d="scan'208";a="53137543" Received: from emailfrontal2.citycable.ch ([85.218.0.121]) by mail1-smtp-roc.national.inria.fr with SMTP; 20 Feb 2010 03:40:42 +0100 Received: from [192.168.0.12] (unknown [85.218.92.99]) (Authenticated sender: guillaume.yziquel@citycable.ch) by emailfrontal2.citycable.ch (Postfix) with ESMTPA id 52F1AB103E0; Sat, 20 Feb 2010 03:40:37 +0100 (CET) Message-ID: <4B7F4BCC.9000604@citycable.ch> Date: Sat, 20 Feb 2010 03:41:16 +0100 From: Guillaume Yziquel Reply-To: guillaume.yziquel@citycable.ch User-Agent: Mozilla-Thunderbird 2.0.0.22 (X11/20090707) MIME-Version: 1.0 To: OCaml List Cc: Florent Monnier Subject: Re: [Caml-list] Wrapping var_args, or C ... in ocaml? References: <4B781ACC.4040603@citycable.ch> <20100214180650.GA22433@annexia.org> <4B787D32.10505@citycable.ch> <201002151106.23642.fmonnier@linux-nantes.fr.eu.org> <4B7F3FED.4010100@citycable.ch> In-Reply-To: <4B7F3FED.4010100@citycable.ch> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Spam: no; 0.00; guillaume:01 guillaume:01 ocaml:01 varargs:01 ocaml:01 phew:01 camlprim:01 alist:01 hppa:01 val:01 val:01 emptylist:01 varargs:01 vrier:98 10,:98 Guillaume Yziquel a =C3=A9crit : > Florent Monnier a =C3=A9crit : >> Le dimanche 14 f=C3=A9vrier 2010 23:46:10, Guillaume Yziquel a =C3=A9c= rit : >>> I mean, it seems that varargs means on the receiving end "the number=20 >>> of arguments you'r giving me, as a function, is not limited", whereas= =20 >>> on the sending end, you hard-code the number of arguments in your C=20 >>> code. >>> >>> Is there a way to map an OCaml list to an ellipsis? Or is it a C=20 >>> 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/ >=20 > Phew! These tools are quite tough to handle! (I just tried to use=20 > avcall.h on this ellipsis.) Got it. The correct code is: > CAMLprim value ocamlpython_py_tuple_pack (value ml_len, value ml_pyobjs= ) { >=20 > av_alist argList; > PyObject * retVal; > av_start_ptr(argList, &PyTuple_Pack, PyObject*, &retVal); >=20 > #if defined(__s390__) || defined(__hppa__) || defined(__cris__) > #define av_Py_ssize_t av_long > #else > #define av_Py_ssize_t av_int > #endif >=20 > av_Py_ssize_t(argList, Pyoffset_val(ml_len)); > while (ml_pyobjs !=3D Val_emptylist) { > av_ptr(argList, PyObject*, Pyobj_val(Field(ml_pyobjs, 0))); > ml_pyobjs =3D Field(ml_pyobjs, 1); > } >=20 > av_call(argList); > return(Val_owned_pyobj(retVal)); > } Hope that will be useful to people wishing to wrap up varargs (though=20 it's probably a bad idea, since the stack is limited...) Python function calls are now possible by constructing the argument tuple= . --=20 Guillaume Yziquel http://yziquel.homelinux.org/