[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: ffi helper
Taylan Ulrich Bayırlı/Kammer
Re: ffi helper
Mon, 20 Mar 2017 15:49:08 +0100
Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux)
Matt Wette <address@hidden> writes:
>> On Mar 13, 2017, at 5:53 PM, Matt Wette <address@hidden> wrote:
>>> On Mar 8, 2017, at 6:06 PM, Matt Wette <address@hidden> wrote:
>>> I’m now working on a FFI helper based on the nyacc C99 parser.
> I am making some progress with some ragged prototype code.
> Here is a top-level program, just pulling out three declarations to start:
> (let* ((file "/opt/local/include/cairo/cairo-svg.h")
> (defs '())
> (incs '("/opt/local/include" "/opt/local/include/cairo"
> (tree (my-parse defs incs file))
> (file-decls (reverse (c99-trans-unit->udict tree #:filter
> (udecl-dict (c99-trans-unit->udict/deep tree)))
> (lambda (pair type-list)
> (udecl->ffi-decl (cdr pair) type-list))
> (lambda (e)
> (member (car e)
> '("cairo_matrix_t" "cairo_surface_t" "cairo_svg_surface_create")))
> And here is the autogenerated output for the declarations:
> (define-std-pointer-wrapper cairo_surface_t*)
> (define cairo_matrix_t
> `(,(string->symbol "xx") ,double)
> `(,(string->symbol "yx") ,double)
> `(,(string->symbol "xy") ,double)
> `(,(string->symbol "yy") ,double)
> `(,(string->symbol "x0") ,double)
> `(,(string->symbol "y0") ,double)))
> (define cairo_svg_surface_create
> (let ((f (pointer->procedure
> (libcairo-func "cairo_svg_surface_create")
> (list '* double double))))
> (lambda (filename width_in_points height_in_points)
> (let ((~filename (string->pointer filename))
> (~width_in_points width_in_points)
> (~height_in_points height_in_points)
> (f ~filename ~width_in_points ~height_in_points)))
> (wrap-cairo_surface_t* ~result)))))
> I was not able to come up with a procedure that would take arguments
> “xx” and “double” and generate the bs:struct pattern `(xx ,double).
> Help is welcome.
You mean you get "xx" and "double" as strings? For xx, string->symbol
as you already do is OK. As for double, that's a bit tricky. I see a
- Turn the string into a symbol and eval it:
(eval (string->symbol str)
(resolve-module '(bytestructures guile numeric)))
- Use the reflective module API:
(module-ref (resolve-module '(bytestructures guile numeric))
- Create an explicit mapping from name (string or symbol) to object:
(import (bytestructures guile numeric))
`(("double" . ,double)
("float" . ,float)
(The so-far undocumented module (bytestructures guile numeric-metadata)
contains a mapping similar to this, but it only contains name with
explicit bit width so e.g. 'double' isn't in it, so I guess it's not the
right thing here.)
While 'eval' is normally discouraged, I think it's fine in this case,
since we use string->symbol. I don't see anything bad resulting from
evaluating an arbitrary symbol.
However, the second option could be said to be a tad bit more "correct"
than using eval, so I'd go with that and only use the eval method if I
had to write standards-compliant code where eval is available but not
something like module-ref.
The third option gives you full control but it shouldn't be necessary.
Hope that helps,