chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] Trying to wrap foreign function type pointers


From: Matt Gushee
Subject: [Chicken-users] Trying to wrap foreign function type pointers
Date: Fri, 8 May 2015 17:54:50 -0600

Good day--

I am attempting to write a Chicken wrapper for libsass (C library implementation of the Sass CSS preprocessor). The library is actually written in C++, but it has a mostly pretty straightforward C-style API. As the website [libsass.org] says, "The point is to be simple, fast, and easy to integrate."

Anyway, initially I am doing a naive implementation where I basically take each of the 3 main header files and translate it into a Scheme module. I've got one module done - it compiles cleanly and, to the limited extent I've tested, seems to work quickly. I think I'm getting close with the second module, but am running into trouble with a couple of function pointer types. To keep things simple, I'll just show one, since both types are structurally similar, are used in similar functions, and raise the same kind of error

sass_functions.h has:

    typedef Sass_Import_List (*Sass_Importer_Fn)
        (const char* url, Sass_Importer_Entry cb, struct Sass_Compiler* compiler);

All the Sass ... types are abstract types - is that the right term? They are declared in one or another header, but their implementations are hidden. The relevant declarations are:

    typedef struct Sass_Import* (*Sass_Import_List);
    typedef struct Sass_Importer (*Sass_Importer_Entry);
    struct Sass_Compiler;

And the Sass_Importer_Fn type is used as an argument to just one function:

    ADDAPI Sass_Importer_Entry ADDCALL sass_make_importer (Sass_Importer_Fn importer, double priority, void* cookie);

The corresponding Scheme code in sass-functions.scm is:

    (foreign-declare "#include <sass_functions.h>")

    (define-foreign-type sass-compiler (struct "Sass_Compiler"))
    (define-foreign-type import-list (c-pointer (c-pointer (struct "Sass_Import"))))
    (define-foreign-type importer-entry (c-pointer (struct "Sass_Importer")))

    (define-foreign-type importer-fn
        (function import-list
                     (c-string importer-entry (c-pointer sass-compiler))))

    (define make-importer
        (foreign-lambda importer-entry
                               sass_make_importer
                               importer-fn double c-pointer))

However, the make-importer definition raises a compiler error:

$ csc -s -lsass sass-functions.scm

sass-functions.c: In function ‘stub54’:
sass-functions.c:308:56: warning: passing argument 1 of ‘sass_make_importer’ from incompatible pointer type
 C_r=C_mpointer_or_false(&C_a,(void*)sass_make_importer(t0,t1,t2));
                                                        ^
In file included from /usr/include/sass.h:38:0,
                 from /usr/include/sass_values.h:6,
                 from sass-functions.c:13:
/usr/include/sass_functions.h:46:36: note: expected ‘Sass_Importer_Fn’ but argument is of type ‘struct Sass_Import ** (*)(char *, struct Sass_Importer *, struct Sass_Compiler *)’
 ADDAPI Sass_Importer_Entry ADDCALL sass_make_importer (Sass_Importer_Fn importer, double priority, void* cookie);
                                    ^

I'm not sure where to go with this. All I can say with confidence is that the importer-fn argument is of the wrong type - but not exactly how it is wrong. I have Googled, and also browsed the Chicken docs in the hope of finding a similar piece of code with detailed documentation, but haven't found anything that makes sense to me. Can anyone help clarify what is going on here?

--
Matt Gushee


reply via email to

[Prev in Thread] Current Thread [Next in Thread]