[gambit-list] Compiling to Javascript

hashavtech at outlook.com hashavtech at outlook.com
Sat Jul 13 03:30:35 EDT 2019


Thank you, this helps a lot. Saving continuations in a js variable sound exactly like what I needed
________________________________
From: Marc Feeley <feeley at iro.umontreal.ca>
Sent: Monday, July 8, 2019 4:47 PM
To: hashavtech at outlook.com
Cc: gambit-list at iro.umontreal.ca
Subject: Re: [gambit-list] Compiling to Javascript

Why do you need GVM.init() ?  Currently, the Gambit VM can’t be initialized more than once so GVM.init() is a noop.

To export Scheme procedures as JavaScript global functions you can use the following js-set! procedure (which works because the g_scm2host runtime system function knows how to convert Scheme procedures to JavaScript functions):


(define (js-set! global procedure)
  (##inline-host-statement
   "(function () {return this;}())[g_scm2host(@1@)] = g_scm2host(@2@);"
   global
   procedure)
  (void))

;; sample use:

(define (scheme_add a b)
  (println "scheme_add")
  (fx+ a b))

(js-set! "scheme_add" scheme_add)


In the JS code you can then simply call scheme_add(10,20) to call the Scheme procedure with that name and get the value 30 back.  The JavaScript values 10 and 20 are converted to Scheme using g_host2scm and the resulting Scheme value is converted back to JavaScript using g_scm2host.

The js-set! procedure combined with continuations can be used to implement your pause-gvm procedure:


(define (pause-gvm)
  (##continuation-capture
   (lambda (c)
     (js-set! "GVM_resume"
              (lambda () (##continuation-return-no-winding c #f)))
     (##inline-host-statement "return null;")))) ;; cause trampoline to exit

;; sample use:

(define (scheme_add2 a b)
  (println "scheme_add2")
  (pause-gvm)
  (fx+ a b))

(js-set! "scheme_add2" scheme_add2)


Then in your JavaScript code you can do:

scheme_add2(10,20);   // will return (with an unspecified result) when pause-gvm is called
res = GVM_resume();   // resume execution after the pause-gvm, and returns 30


This could form the basis for a Scheme-level thread system whose execution is event-driven.  Implementation left as an exercice…

Marc



> On Jul 8, 2019, at 7:34 AM, hashavtech at outlook.com wrote:
>
> Thank you for the detailed example, it helped a lot. I've played with running Scheme in a browser and it's a lot of fun! (Although writing custom runtime library is not easy for a newcomer. For example is there a list of what is supported by the compiler out of the box - such as fx+ and friends? )
>
> In any case this was using compiled Scheme as a main script, and I was thinking how to use Scheme with some JS framework that does not like to relinquish control. Ideally I think there'd be possible to do the following:
>
>        • Initialize Gambit VM, something like   GVM.init();
>        • Later call into some procedure passing parameters:  GM.call('do-scheme-stuff', [param1, param2]);
>        • from scheme code to be able to pause the execution and to return control to the external javascript. Something like this:
> (define (dostuff)
> ... (define (add-to-menu option) (##inline-host-expression "addToGlobalFrameworkMenu(...)"))
>   (add-to-menu "option1")
>   (add-to-menu "option2")
>   (##pause-vm)
>
> Where ##pause-vm will stop executing scheme code. Later it'd be possible in JS to do GVM.resume("option2") and have (##pause-vm) return with the supplied value. Alternatively, we'd be able call GVM.dropStack() to trash the scheme stack and start anew with GVM.call('other-entry-point', ['more values']);
>
> There does not seem to be anything in the manual regarding the various backends such as JS, so I'm not sure, but most of this probably is possible?
>
>
> From: Marc Feeley <feeley at iro.umontreal.ca>
> Sent: Friday, July 5, 2019 1:46 PM
> To: hashavtech at outlook.com
> Cc: gambit-list at iro.umontreal.ca
> Subject: Re: [gambit-list] Compiling to Javascript
>
> I’m currently working on implementing an intelligent linker that can remove the parts of the runtime library that are not needed by the application.  For the moment, to get a compact executable you need to write your own runtime library for the functions you need.  If you only need basic stuff, this may be a reasonable undertaking.
>
> Below is a sample application split in 2 files: “lib.scm” is the runtime library and “app.scm” is the main program.  The library contains definitions of for-each, append, etc.  Note that some operations like fx+, fx-, fx<, cons, car, cdr, println don’t need to be defined because the Gambit compiler treats them as inlined primitives (they compile to the corresponding JavaScript code instead of being actual Scheme procedure calls).
>
> Here is how this application is linked, and the result of executing the app:
>
> % make
> gsc -target js -c lib.scm
> gsc -target js -c app.scm
> gsc -target js -link -l lib app.js
> gsc -target js -c lib.scm
> cat app_.js lib.js app.js > linked_app.js
> wc *.js
>      339     870    7244 app.js
>      651    1931   14992 app_.js
>      335     853    7937 lib.js
>     1325    3654   30173 linked_app.js
>     2650    7308   60346 total
> node linked_app.js
> >>> lib.scm
> >>> app.scm
> 1
> 1
> 2
> 3
> 5
> 8
> 410
> 9227465
>
> The JavaScript code is 30K bytes, and half of that is in app_.js that is the runtime system (which won’t grow much if the application is bigger).
>
> Aside from the intelligent linker, a more compact code could be obtained by minimizing the JavaScript code either with an external minimizer or by reducing the size of the identifiers that are emitted by the compiler and not pretty-printing the JavaScipt code which adds lots of spaces for indentation.
>
> Marc
>
>
>
> ------------------------------------------------------------
> # makefile
>
> #GSC=../gsc/gsc -:=.. -target js
> GSC=gsc -target js
>
> all: run
>
> lib.js: lib.scm
>         $(GSC) -c lib.scm
>
> app.js: app.scm
>         $(GSC) -c app.scm
>
> app_.js: lib.js app.js
>         $(GSC) -link -l lib app.js
>
> linked_app.js: lib.js app.js app_.js
>         $(GSC) -c lib.scm
>         cat app_.js lib.js app.js > linked_app.js
>         wc *.js
>
> run: linked_app.js
>         node linked_app.js
>
> clean:
>         rm -f lib.js app.js app_.js linked_app.js
> ------------------------------------------------------------
> ;; file: lib.scm
>
> (declare (standard-bindings) (extended-bindings) (not safe) (not run-time-bindings))
>
> (println ">>> lib.scm")
>
> (define (for-each f lst)
>   (if (pair? lst)
>       (begin
>         (f (car lst))
>         (for-each f (cdr lst)))))
>
> (define (append lst1 lst2)
>   (if (pair? lst1)
>       (cons (car lst1) (append (cdr lst1) lst2))
>       lst2))
>
> (define (current-milliseconds)
>   (##inline-host-expression "Date.now()"))
>
> (define (js-alert obj)
>   (##inline-host-statement "alert(g_scm2js(@1@));" obj))
>
> (app#) ;; execute app.scm module
> ------------------------------------------------------------
> ;; file: app.scm
>
> (declare (standard-bindings) (extended-bindings) (not safe) (not run-time-bindings))
>
> (println ">>> app.scm")
>
> (define (fib n) (if (fx< n 2) n (fx+ (fib (fx- n 1)) (fib (fx- n 2)))))
>
> (for-each
>  (lambda (x)
>    (println (fib x)))
>  (append '(1 2 3)
>          '(4 5 6)))
>
> (let* ((start (current-milliseconds))
>        (result (fib 35))
>        (end (current-milliseconds)))
>   (js-alert (fx- end start))
>   (js-alert result))
> ------------------------------------------------------------
>
>
> > On Jun 29, 2019, at 10:59 AM, hashavtech at outlook.com wrote:
> >
> > There are some messages in 2013 and 2016 that gave me a hint of what needs to be done in order to compile to js. But it seems that some things have changed (for example the gsc with -target js does not seem to produce a .js file, it gave me an .o1 file that contained Javascript). I was able to compile a working example once but the .js file contains the whole _gambit.js library and is 22 MB in size. That isn't really what I was hoping for. I've seen it mentioned that there was some work done on only including the necessary parts of the library, but I was unable to figure out how to do it (I'm new to Gambit although not to Lisp).
> >
> > I'd be glad for some help getting from a Scheme file to a working and reasonably-sized file. My goal is to include scheme-sourced parts in a web app, so there probably will be some interoperability issues, but at the moment I'm stuck at the compilation stage.
> >
> > _______________________________________________
> > Gambit-list mailing list
> > Gambit-list at iro.umontreal.ca
> > https://mailman.iro.umontreal.ca/cgi-bin/mailman/listinfo/gambit-list


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20190713/6a58755a/attachment.htm>


More information about the Gambit-list mailing list