Marc,
Thank you for your reply. Â This was exactly what I was looking for. Â Is the "Gambit_js2scm" function available in v4.7.2, or will I need to upgrade to the latest code?
I'm glad to hear you are continuing to improve the universal backend and am excited that first-class continuations are now implemented. Â With first-class continuations available are there plans to implement green threads as well? Â I've thought that green threads may be a interesting method to implement sprites for a HTML5 game.
Bob
Post by Bob ColemanGreetings,
I have been playing around with the javascript backend and have been impressed with it's capabilities.
Iâm glad you like it. Over the past week I have made several enhancements with the short term goal of implementing a remote debugger (so that Scheme code running in the browser can be debugged live using a REPL running on the server). Just a few minutes ago I committed the support for first-class continuations which are needed for the exception handling of the REPL.
Post by Bob Coleman Interacting with other javascript libraries is not a problem, but I haven't found a way for other javascript code to call scheme defined functions. Is there a way to define functions to be called by javascript similar to c-lambda or c-define?
It is quite easy to interface to JavaScript functions from Scheme. In fact, using JavaScriptâs eval gives a much simpler approach than using Gambitâs C interface for the C back-end. For example, you can define
(define js
 (##inline-host-expression "Gambit_js2scm(eval)"))
This creates a Scheme callable function which performs a JavaScript eval. Then, in your code you can say:
(define alert (js "alert"))
(alert "hello!")
This will âconvertâ the JavaScript âalertâ function to a Scheme function.
For the other way around (exporting a Scheme function callable from JavaScript), the js function can be used like this:
(define setglo (js "function (name,val) { window[name] = val; }"))
then
(setglo "foo" (lambda (n) (* n n)))
And then in JavaScript land your can call (foo 10).
Post by Bob ColemanAlso, is it possible to access the value of the -target flag in scheme? I would like to define some functions differently based on whether the program is being built for the C or javascript backend.
Define this macro:
(##define-macro (case-target . clauses)
 (let ((target (if (and (pair? ##compilation-options)
             (pair? (car ##compilation-options)))
          (let ((t (assq 'target ##compilation-options)))
           (if t (cadr t) 'c))
          'c)))
  (let loop ((clauses clauses))
   (if (pair? clauses)
     (let* ((clause (car clauses))
         (cases (car clause)))
      (if (or (eq? cases 'else)
          (memq target cases))
        `(begin ,@(cdr clause))
        (loop (cdr clauses))))
     `(begin)))))
and use it like this:
(case-target
 ((c)
  (define foo (c-lambda () int "foo")))
 ((js)
  (define foo (lambda () (##inline-host-expression "42")))))
Marc