diff --git a/js-assembler/package.rkt b/js-assembler/package.rkt index 7a0d233..0a67e81 100644 --- a/js-assembler/package.rkt +++ b/js-assembler/package.rkt @@ -11,6 +11,7 @@ "../parser/parse-bytecode.rkt" racket/match racket/list + racket/promise (prefix-in query: "../lang/js/query.rkt") (planet dyoo/closure-compile:1:1) (prefix-in runtime: "get-runtime.rkt") @@ -301,22 +302,25 @@ MACHINE.modules[~s] = (define (compress x) - (if (current-compress-javascript?) - (closure-compile x) - x)) + (cond [(current-compress-javascript?) + (log-debug "compressing javascript...") + (closure-compile x)] + [else + (log-debug "not compressing javascript...") + x])) (define *the-runtime* - (let ([buffer (open-output-string)]) - (write-runtime buffer) - (compress - (get-output-string buffer)))) + (delay (let ([buffer (open-output-string)]) + (write-runtime buffer) + (compress + (get-output-string buffer))))) ;; get-runtime: -> string (define (get-runtime) - *the-runtime*) + (force *the-runtime*)) diff --git a/scribblings/manual.scrbl b/scribblings/manual.scrbl index a4f8ddf..94762f4 100644 --- a/scribblings/manual.scrbl +++ b/scribblings/manual.scrbl @@ -153,7 +153,7 @@ and if this does appear, then Whalesong should be installed successfully. -@subsection{Running Whalesong} +@subsection{Making Standalone @tt{.xhtml} files with Whalesong} Let's try making a simple, standalone executable. At the moment, the program must be written in the base language of @racket[(planet @@ -234,19 +234,102 @@ web browser, we should see a pale, green page with some output. - - - @;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -@section{Extended example} +@subsection{Using Whalesong functions from JavaScript} @;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -(This example needs to use modules. It should also show how we can use the -other command-line options to compress the javascript, and how to -use @tt{get-javascript} and @tt{get-runtime}, to allow the user to -build a customized html file.) +Whalesong also allows functions defined from Racket to be used from +JavaScript. As an example, we can take the boring @emph{factorial} +function and define it in a module called @filepath{fact.rkt}: + +@margin-note{ +The files can also be downloaded here: +@itemlist[@item{@link["http://hashcollision.org/whalesong/fact-example/fact.rkt"]{fact.rkt}} +@item{@link["http://hashcollision.org/whalesong/fact-example/index.html"]{index.html}}] +with generated JavaScript binaries here: +@itemlist[ +@item{@link["http://hashcollision.org/whalesong/fact-example/fact.js"]{fact.js}} +@item{@link["http://hashcollision.org/whalesong/fact-example/runtime.js"]{runtime.js}} +] +} +@filebox["fact.rkt"]{ +@verbatim|{ +#lang planet dyoo/whalesong +(provide fact) +(define (fact x) + (cond + [(= x 0) + 1] + [else + (* x (fact (sub1 x)))])) +}|} + +Instead of creating a standalone @tt{.xhtml}, we can use @tt{whalesong} to +get us the module's code. From the command-line: +@verbatim|{ + $ whalesong get-javascript fact.rkt > fact.js + $ ls -l fact.js + -rw-r--r-- 1 dyoo dyoo 27421 2011-07-11 22:02 fact.js +}| + +This file does require some runtime support not included in +@filepath{fact.js}; let's generate the @tt{runtime.js} and save +it as well. At the command-line: +@verbatim|{ + $ whalesong get-runtime > runtime.js + $ ls -l runtime.js + -rw-r--r-- 1 dyoo dyoo 544322 2011-07-11 22:12 runtime.js +}| +Now that we have these, let's write an @filepath{index.html} that uses +the @racket[fact] function that we @racket[provide]ed from +@filepath{fact.rkt}. +@filebox["index.html"]{ +@verbatim|{ + + +
+ + + + + + + +The factorial of 10000 is being computed. + + +}| +} + +Replacing the @racket[10000] with @racket["one-billion-dollars"] should +reliably produce a proper error message. @;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; diff --git a/tests/coersing/Makefile b/tests/coersing/Makefile index 0c1cfb4..a616d5b 100644 --- a/tests/coersing/Makefile +++ b/tests/coersing/Makefile @@ -1,5 +1,5 @@ #!/bin/sh all: - ../../whalesong get-javascript fact.rkt > fact.js - ../../whalesong get-runtime > runtime.js + ../../whalesong get-javascript --verbose --compress-javascript fact.rkt > fact.js + ../../whalesong get-runtime --verbose --compress-javascript > runtime.js diff --git a/tests/coersing/fact.rkt b/tests/coersing/fact.rkt index 3aa02db..daa6c20 100644 --- a/tests/coersing/fact.rkt +++ b/tests/coersing/fact.rkt @@ -8,4 +8,4 @@ (* x (fact (sub1 x)))])) -(printf "test: ~s\n" (fact 4)) \ No newline at end of file +;;(printf "test: ~s\n" (fact 4)) \ No newline at end of file diff --git a/tests/coersing/index.html b/tests/coersing/index.html index 3a70530..b873231 100644 --- a/tests/coersing/index.html +++ b/tests/coersing/index.html @@ -3,29 +3,29 @@ - +The factorial of 10000 is being computed.