[cffi-devel] calls with variable number of arguments which are determined at run-time

James Bielman jamesjb at gmail.com
Fri Feb 9 08:29:24 UTC 2007


Lars Rune Nøstdal <larsnostdal at gmail.com> writes:

> Anyone have some suggestions as to how I can call foreign-functions
> with variable number (the count, types and values determined at
> run-time!) of arguments?
>
> I've nagged the CLL'ers about this and Pillsy came up with a hack
> that works:
>
> I was wondering if there where better ways to do this?

Hi Lars,

Off the top of my head, I think any reasonable approach is going to
need to involve calling the compiler.  Only the compiler knows how to
actually build the stack frames and such for the call.  For example,
there are arcane rules on some architectures about whether floating
point values are passed on the stack or in registers depending on the
number of arguments and their positions.

One possibility for making it a little less gruesome performance-wise
is to build a cache of precompiled functions for a set of function
types.  For each distinct set of argument types you can COMPILE a
lambda that builds the appropriate FOREIGN-FUNCALL, and store it in an
EQUAL hashtable with the list of arguments (:INT :FLOAT).

What's the use case for this?  Surely not sprintf; there's about a
million better ways to format text without resorting to such
things... ;-)

Down with varargs!

James



More information about the cffi-devel mailing list