This example demonstrates how to interface Scheme with Javascript. DHTML effects are implemented in "native" Javascript, while the bubblesort algorithm is implemented in Scheme (and compiled to Javascript).
5 | 3 | 10 | 7 |
The bubblesort algorithm is implemented in bs.scm (which compiles to scm-bs.js). The native DHTML code is in bs-demo.js. This web page references in its <head>
section both the native and the compiled Scheme code, as well as the backend files cpscm-drv.js and prelude.js.
To call a Scheme function from Javascript, find the mangled name (by calling (symbol->js 'fun)
from the Scheme compiler, where fun
is your Scheme function). Then (assuming the mangled name is cpscmfun
) insert something like the following in Javascript:
var result = cpscm__drive (cpscm__call_scm (cpscmfun, cpscm__id, arg1, arg2 /* ... */));
To create a Javascript function callable from Scheme, again find out the mangled name (say cpscmjsfun
), and define the function as in:
var cpscmjsfun = cpscm__cpswrap (function jsfun (x, y) { return x + y; });
You may also want to override the default error handler, as in bs-demo.js.