Re: Recursion in foreign-safe-lambda

Massimo Nocentini via Chicken-users <[email protected]>
Newsgroups gmane.lisp.scheme.chicken
Message-ID <CACOChSD3Jsxxeedh=LC2BKNVN4+ncwJv4k6s88cvqrCs_J2yDQ@mail.gmail.com>
Hi Felix,

Thank you, I see your point. I eventually have the following code that
allows me to recur in C calling callback functions provided by Scheme code.

For the sake of clarity, I have the following Scheme definitions:

>
> (define (simdjson-load filename)
> (let1 (P (foreign-safe-lambda scheme-object "chicken_simdjson_load"
> (const c-string)
> scheme-object
> scheme-object
> scheme-object
> scheme-object
> scheme-object
> scheme-object))
> (P filename list identity make-vector cons (λ (v i x) (vector-set! v i x)
> v) reverse)))
>
> (define (simdjson-load/ondemand filename)
> (let1 (P (foreign-safe-lambda scheme-object
> "chicken_simdjson_load_ondemand"
> (const c-string)
> scheme-object
> scheme-object
> scheme-object
> scheme-object
> scheme-object
> scheme-object))
> (P filename list identity make-vector cons (λ (v i x) (vector-set! v i x)
> v) reverse)))
>

 and in C do the required allocations, passing them as arguments to the
given functions, as in the following C code chunk:

...
> switch (element.type())
> {
> case dom::element_type::ARRAY:
> {
> dom::array array = dom::array(element);
> size_t length = array.size();
>
> C_save(C_fix(length));
> res = C_callback(callback_vector, 1);
>
> size_t i = 0;
> for (dom::element child : array)
> {
>
> tmp = chicken_simdjson_visit(
> child,
> callback_object,
> callback_identity,
> callback_vector,
> callback_list,
> callback_vector_set,
> callback_list_finalize);
>
> C_save(tmp);
> C_save(C_fix(i));
> C_save(res);
>
> res = C_callback(callback_vector_set, 3);
>
> i++;
> }
>
> break;
> }
>


By the way, my aim is to implement a binding over the simdjson
<https://github.com/simdjson/simdjson> library. Can this approach work?

Massimo

>
On Wed, Sep 24, 2025 at 4:14 PM Felix Winkelmann <
[email protected]> wrote:

> On Mon Sep 22, 2025 at 9:46 AM CEST, Massimo Nocentini via Chicken-users
> wrote:
> >
> > I have a progress using foreign-primitive for allocating objects.
> >
> > On the contrary, given the following definitions:
> >
> > (defineparse(foreign-primitivescheme-object(((constc-string) filename)
> > (scheme-objectl))
> > "P(C_k, filename, l);"))
> >
> > where function P is defined in a companion cpp file and reads as follows:
> > C_wordc(C_wordC_k, C_wordl)
> > {
> > if(l==C_SCHEME_END_OF_LIST)
> > {
> > C_return(C_SCHEME_END_OF_LIST);
> > }
> > C_wordcdr=c(C_k, C_i_cdr(l));
> > C_word*ptr=C_alloc(C_SIZEOF_PAIR);
> > C_wordres=C_a_pair(&ptr, C_i_car(l), cdr);
> > C_return(res);
> > }
> > externvoidP(C_wordC_k, constchar*filename, C_wordl)
> > {
> > C_kontinue(C_k, c(C_k, l));
> > }
> > My goal here is to just walk through the given list l and allocate fresh
> > pairs for it. When I try:
> > (parse "something.json" '(1 2 3))
> >
> > I still get:
> >
> > [...]
> >
> > What I'm doing wrong?
>
> This can't work, as C_alloc allocates on the stack and C_return
> in the function "c" will pop any created data again. C_return is
> only valid _inside_ the foreign-primitive body.
>
>
> cheers,
> felix
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.