[gambit-list] -target js

Marc Feeley feeley at iro.umontreal.ca
Fri Nov 22 09:04:27 EST 2013

On Nov 21, 2013, at 11:01 AM, Francois Magnan <magnan at categoricaldesign.com> wrote:

> Hi,
> Sorry to disturb you again on this. I continued my experiments with “-target js”. I reimplemented quasiquote, unquote… and I stumbled on a strange bug that mystifies me: now “cons” function is not known. I have sucessfully compiled programs using cons with -target js many times but now it just won’t compile it correctly. Here is a simplified version of the code just to illustrate what I see:

If you want to pass the cons procedure to fold, you need the cons global variable to be bound to the cons procedure.  This binding of the cons global variable to the cons procedure is the responsibility of the Gambit runtime system (which as you know is not complete at this point).  So you have to explicitly do the binding with:

(define (cons x y) (##cons x y))
(define (car x) (##car x))
(define (cdr x) (##cdr x))
(define (set-car! x y) (##set-car! x y))
(define (set-cdr! x y) (##set-cdr! x y))

This works because the call to ##cons is inlined by the compiler.

Alternatively, you could do:

(fold (lambda (x y) (cons x y)) ...)

This works because the call to cons is transformed to a call to ##cons by the compiler.

Note that it is up to the runtime system to do error checking, so you may want to do:

(define (car x) (if (##pair? x) (##car x) (error "oops")))


More information about the Gambit-list mailing list