[gambit-list] Questions & documentation on the universal backend

Marc Feeley feeley at iro.umontreal.ca
Sun Aug 14 08:26:33 EDT 2016

Yes that is mostly correct.  However I would recommend the following commands:

for JavaScript:

$ cd lib
$ make _gambit.js
$ cd ..
$ gsc/gsc -:=. -warnings -target js code.scm
$ gsc/gsc -:=. -warnings -target js -link -l lib/_gambit.js code.js
$ cat code_.js code.js lib/_gambit.js > p.js
$ node p.js

for Python:

$ cd lib
$ make _gambit.py
$ cd ..
$ gsc/gsc -:=. -warnings -target python code.scm
$ gsc/gsc -:=. -warnings -target python -link -l lib/_gambit.py code.py
$ cat code_.py code.py lib/_gambit.py > p.py
$ python p.py

Note that there was a small ordering problem in the generated code (now fixed) that prevented the Python code to run properly.

The universal backend is a WIP but close to a “final product”.  Remaining on the TODO:

1) changes to the Gambit library (lib/*.scm) and the runtime system to support more of the Gambit system features, in particular non-blocking I/O which is needed to support Gambit’s green threads
2) integration to the main installation process (to build and install _gambit.js, _gambit.py, etc in the Gambit installation directory)
3) documentation (!)

The universal backend is definitely usable without #1 (some users have built commercial products with it) as long as the application uses ##inline-host-* to interface with hand written code to do the user-interface or I/O.

Here is a simple example for using ##inline-host-* in JavaScript:

(declare (extended-bindings))

(##inline-host-declaration "

function rand(n) {
  return Math.floor(n * Math.random());


(define (rand n)
  (+ 1 (##inline-host-expression "g_host2scm(rand(g_scm2host(@1@)))" n)))

(println (rand 6))
(println (rand 6))
(println (rand 6))
(println (rand 6))

(##inline-host-statement "console.log(@1@);" 123)
(##inline-host-statement "console.log(@1@);" "hello")
(##inline-host-statement "console.log(@1@);" 'world)
(##inline-host-statement "console.log(@1@);" (cons 11 22))
(##inline-host-statement "console.log(@1@);" (vector 11 22 33))
(##inline-host-statement "console.log(@1@);" rand)

Hope that helps!


> On Aug 14, 2016, at 12:23 AM, Jon Woodring <woodring.jon at gmail.com> wrote:
> Is there any documentation on compiling to javascript, or is it mostly in the mailing list archives? After some scrounging, I was finally able to figure out how to compile and get a simple (println (fib 5)) working:
> $ cd gambit/lib
> $ gsc -:~~bin=../bin,~~lib=../lib,~~include=../include -f -target js -prelude "(##include \"header.scm\")" -o _gambit.js ../lib/_univlib.scm
> $ gsc -warnings -target js code.scm
> $ gsc -warnings -link -l _gambit.js code.js
> $ cat code_.js _gambit.js code.js > p.js
> $ node p.js
> 1) Is that mostly correct? I take it it is still a WIP because I couldn't find documentation.
> 2) What if I want to use the Python backend? I tried swapping in all of the targets with -target python but then get this error:
> $ python2 p.py
> Traceback (most recent call last):
>   File "p.py", line 26310, in <module>
>     g_bb1_serial_2d_number_2d__3e_object.name = g_cst1727___gambit
> NameError: global name 'g_cst1727___gambit' is not defined
> 3) Is there a concise example of #inline-host-expression (-statement & -declaration) to be able to have scheme call javascript and vice versa? Same with python? I'm not sure what is correct from my reading of the mailing archive.
> Thanks,
> -Jon
> _______________________________________________
> Gambit-list mailing list
> Gambit-list at iro.umontreal.ca
> https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list

More information about the Gambit-list mailing list