Re: Passing function pointer to crunch via chicken

2025-12-20 Thread Felix Winkelmann via Chicken-users
On Sun Dec 14, 2025 at 11:50 AM CET, Diogo wrote:
> On Sat, Dec 13, 2025 at 03:41:57PM +0100, Felix Winkelmann wrote:
>
> Thanks for that. I will give a try right away. It is possible to "derefence"
> the impl wrapper so that I could achieve this selection of behavior from the
> chicken side?

There is no reliable way to extract a raw callable function pointer
from a closure, since there is context information the code needs
access to.

>
> To exemplify the whole issue, I am testing a crunch procedure
>
>   scan-matrix(m, read-col)
>
> Where m is "matrix" object in crunch that represents a keyboard and "real-col"
> is a procedure which reads the state of one keyboard column and returns it
> encoded in some byte. When running this on a microcontroller, I want to use
> real-col that uses quite platform specific functionality which I cannot 
> compile
> when testing this on the host. On the host, I want to create a "mock" read-col
> in crunch, and then use chicken to call scan-matrix  with that mock read-col 
> and
> then evaluate the results among other things.
>
> Do you have another/better suggestion how I could replace the functionality
> inside a crunch module that is called from chicken?

You can use the normal foreign function interface facilities for callbacks,
i.e. "define-external" to define a callback entry-point and "location"
to obtain the C function pointer of the entry-point. But I see here that
there is no way to parameterize the generated wrapper for crunched procedures
(the functionality is there, just not accessible). I will add this.


cheers,
felix



Re: Passing function pointer to crunch via chicken

2025-12-14 Thread Diogo via Chicken-users
On Sat, Dec 13, 2025 at 03:41:57PM +0100, Felix Winkelmann wrote:
> On Fri Dec 12, 2025 at 8:34 AM CET, Diogo via Chicken-users wrote:
> It turns out function-type arguments are not handled yet by the wrapper
> generation code, I've added this (see git repo). But note that your
> examples won't work, as you pass Scheme/CHICKEN procedures ("impl")
> to CRUNCHed code, since the "impl" represents the wrapper, not
> the raw function pointer.

Thanks for that. I will give a try right away. It is possible to "derefence"
the impl wrapper so that I could achieve this selection of behavior from the
chicken side?

To exemplify the whole issue, I am testing a crunch procedure

scan-matrix(m, read-col)

Where m is "matrix" object in crunch that represents a keyboard and "real-col"
is a procedure which reads the state of one keyboard column and returns it
encoded in some byte. When running this on a microcontroller, I want to use
real-col that uses quite platform specific functionality which I cannot compile
when testing this on the host. On the host, I want to create a "mock" read-col
in crunch, and then use chicken to call scan-matrix  with that mock read-col and
then evaluate the results among other things.

Do you have another/better suggestion how I could replace the functionality
inside a crunch module that is called from chicken?

Best,
-Diogo



Re: Passing function pointer to crunch via chicken

2025-12-13 Thread Felix Winkelmann via Chicken-users
On Fri Dec 12, 2025 at 8:34 AM CET, Diogo via Chicken-users wrote:
>
> The problem seems to be related to the compute type because I get this error:
>
> % csc fpointer.scm
> Error: during expansion of (crunch ...) - argument type for foreign 
> wrapper
> is invalid
> dispatch
> (procedure (integer) integer)
>
>   Call history: ...
>

It turns out function-type arguments are not handled yet by the wrapper
generation code, I've added this (see git repo). But note that your
examples won't work, as you pass Scheme/CHICKEN procedures ("impl")
to CRUNCHed code, since the "impl" represents the wrapper, not
the raw function pointer.


cheers,
felix