From bb67d9260a1ae939f53af5c593314cf275480cd2 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 9 Jun 2021 20:15:13 -0700 Subject: ffi: bug: always using ffi_prep_cif_var. * share/txr/stdlib/ffi.c (deffi): Fix misnamed variable. The second value coming from sys:analyze-argtypes is the number of fixed arguments, not the number of variadic arguments. Furthermore, if this number is equal to nargs, we were supposed to have been passing nil instead to ffi-make-call-desc, which indicates the ordinary non-variadic function. We were always always passing a non-nil value, so always requesting variadic. That is fixed now thanks to the change to ffi_make_call_desc. * ffi.c (ffi_make_call_desc): Register the function as variadic if either nfixed is specified as nil, or if it is equal to ntotal. * txr.1: Document the convention change for ffi-make-call-desc. --- ffi.c | 4 ++-- share/txr/stdlib/ffi.tl | 4 ++-- txr.1 | 9 ++++++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/ffi.c b/ffi.c index ff06fe7b..5527fc97 100644 --- a/ffi.c +++ b/ffi.c @@ -4783,15 +4783,15 @@ val ffi_make_call_desc(val ntotal, val nfixed, val rettype, val argtypes, { val name = default_null_arg(name_in); val self = if3(name, name, lit("ffi-make-call-desc")); - cnum nf = c_num(default_arg(nfixed, zero), self); cnum nt = c_num(ntotal, self), i; + cnum nf = c_num(default_arg(nfixed, ntotal), self); struct txr_ffi_call_desc *tfcd = coerce(struct txr_ffi_call_desc *, chk_calloc(1, sizeof *tfcd)); ffi_type **args = coerce(ffi_type **, chk_xalloc(nt, sizeof *args, self)); val obj = cobj(coerce(mem_t *, tfcd), ffi_call_desc_s, &ffi_call_desc_ops); ffi_status ffis = FFI_OK; - tfcd->variadic = (nfixed != nil); + tfcd->variadic = (nt != nf); tfcd->nfixed = nf; tfcd->ntotal = nt; tfcd->argtypes = argtypes; diff --git a/share/txr/stdlib/ffi.tl b/share/txr/stdlib/ffi.tl index 73ee1936..dbf7888c 100644 --- a/share/txr/stdlib/ffi.tl +++ b/share/txr/stdlib/ffi.tl @@ -74,12 +74,12 @@ (arg-types-sym (gensym "arg-types-")) (call-desc-sym (gensym "call-desc-")) (fun-sym (gensym "ffi-fun-"))) - (tree-bind (nargs nvariadic . argtypes) (sys:analyze-argtypes f argtypes) + (tree-bind (nargs nfixed . argtypes) (sys:analyze-argtypes f argtypes) (let ((arg-syms (take nargs (gun (gensym))))) ^(progn (defvarl ,ret-type-sym (ffi-type-compile ',rettype)) (defvarl ,arg-types-sym [mapcar ffi-type-compile ',argtypes]) - (defvarl ,call-desc-sym (ffi-make-call-desc ,nargs ,nvariadic + (defvarl ,call-desc-sym (ffi-make-call-desc ,nargs ,nfixed ,ret-type-sym ,arg-types-sym ',name)) diff --git a/txr.1 b/txr.1 index f34c1b06..80d28239 100644 --- a/txr.1 +++ b/txr.1 @@ -75185,13 +75185,16 @@ of arguments in the call. If the call denotes a variadic function, the .meta nfixed -argument must be an integer between 1 and +argument must be an integer at least 1 and less than .metn ntotal , denoting the number of fixed arguments. If the call denotes an ordinary, non-variadic function, then .meta nfixed -must be specified as -.codn nil . +must either be specified specified as +.code nil +or else equal to the +.meta ntotal +argument. The .meta rettype -- cgit v1.2.3