From e4c91b3917a4be58a694e51fc49cf1b8399955d3 Mon Sep 17 00:00:00 2001 From: Danny Yoo Date: Mon, 11 Jul 2011 22:02:54 -0400 Subject: [PATCH 1/4] making the example look nicer. --- tests/coersing/fact.rkt | 2 +- tests/coersing/index.html | 14 ++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) 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..a2d3ef6 100644 --- a/tests/coersing/index.html +++ b/tests/coersing/index.html @@ -3,29 +3,27 @@ - +The factorial of 10000 is being computed. From a01da31310df049f8e20a42f725899ad4eaa9e5e Mon Sep 17 00:00:00 2001 From: Danny Yoo Date: Mon, 11 Jul 2011 22:22:09 -0400 Subject: [PATCH 2/4] updating documentation with the example --- scribblings/manual.scrbl | 89 +++++++++++++++++++++++++++++++++++---- tests/coersing/index.html | 6 ++- 2 files changed, 84 insertions(+), 11 deletions(-) diff --git a/scribblings/manual.scrbl b/scribblings/manual.scrbl index a4f8ddf..d95e906 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,90 @@ 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 @tt{factorial} +function and define it in a module called @filepath{factorial.rkt}: +@filebox["factorial.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 factorial.rkt > factorial.js + $ ls -l factorial.js + -rw-r--r-- 1 dyoo dyoo 27421 2011-07-11 22:02 factorial.js +}| + +This file does require some runtime support not included in +@filepath{factorial.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{factorial.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/index.html b/tests/coersing/index.html index a2d3ef6..b873231 100644 --- a/tests/coersing/index.html +++ b/tests/coersing/index.html @@ -16,9 +16,11 @@ plt.runtime.ready(function() { $('#answer').text(v.toString()); }, function(err) { - $('#answer').text(err.message); + $('#answer').text(err.message).css("color", "red"); }, - 10000); + 10000 + // "one-billion-dollars" + ); }); From c030b53333f2738fe7cf8a6bafb409f6d6bef6c2 Mon Sep 17 00:00:00 2001 From: Danny Yoo Date: Mon, 11 Jul 2011 22:40:16 -0400 Subject: [PATCH 3/4] adjusting the builder so that compression will work as an option --- js-assembler/package.rkt | 20 ++++++++++++-------- tests/coersing/Makefile | 4 ++-- whalesong.rkt | 26 +++++++++++++++++++++----- 3 files changed, 35 insertions(+), 15 deletions(-) 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/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/whalesong.rkt b/whalesong.rkt index 3ae44f5..ef19026 100755 --- a/whalesong.rkt +++ b/whalesong.rkt @@ -8,6 +8,7 @@ "js-assembler/package.rkt" "private/command.rkt" "logger.rkt" + "parameters.rkt" raco/command-name) @@ -43,6 +44,10 @@ [("-v" "--verbose") ("Display verbose messages.") (current-verbose? #t)] + [("--compress-javascript") + ("Compress JavaScript with Google Closure (requires Java)") + (current-compress-javascript? #t)] + #:args (path) (do-the-build path)] ["get-runtime" "print the runtime library to standard output" @@ -51,6 +56,10 @@ [("-v" "--verbose") ("Display verbose messages.") (current-verbose? #t)] + [("--compress-javascript") + ("Compress JavaScript with Google Closure (requires Java)") + (current-compress-javascript? #t)] + #:args () (print-the-runtime)] ["get-javascript" "Gets just the JavaScript code and prints it to standard output" @@ -59,6 +68,11 @@ [("-v" "--verbose") ("Display verbose messages.") (current-verbose? #t)] + + [("--compress-javascript") + ("Compress JavaScript with Google Closure (requires Java)") + (current-compress-javascript? #t)] + #:args (file) (get-javascript-code file)])) @@ -77,7 +91,8 @@ (let ([msg (sync receiver)]) (match msg [(vector level msg data) - (printf "~a: ~a\n" level msg)])) + (fprintf (current-error-port)"~a: ~a\n" level msg) + (flush-output (current-error-port))])) (loop))))))) @@ -109,15 +124,16 @@ (define (print-the-runtime) (turn-on-logger!) - (write-runtime (current-output-port))) + (display (get-runtime) (current-output-port))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (get-javascript-code filename) (turn-on-logger!) - (write-standalone-code - (make-ModuleSource (build-path filename)) - (current-output-port))) + (display (get-standalone-code + (make-ModuleSource (build-path filename))) + (current-output-port))) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; From 10699d6dc9b8cd721e3d7e5674a8873ac28c73fd Mon Sep 17 00:00:00 2001 From: Danny Yoo Date: Mon, 11 Jul 2011 22:56:36 -0400 Subject: [PATCH 4/4] adding rough documentation with example on using whalesong functions from javascript --- scribblings/manual.scrbl | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/scribblings/manual.scrbl b/scribblings/manual.scrbl index d95e906..94762f4 100644 --- a/scribblings/manual.scrbl +++ b/scribblings/manual.scrbl @@ -239,10 +239,22 @@ web browser, we should see a pale, green page with some output. @;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Whalesong also allows functions defined from Racket to be used from -JavaScript. As an example, we can take the boring @tt{factorial} -function and define it in a module called @filepath{factorial.rkt}: +JavaScript. As an example, we can take the boring @emph{factorial} +function and define it in a module called @filepath{fact.rkt}: -@filebox["factorial.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) @@ -257,13 +269,13 @@ function and define it in a module called @filepath{factorial.rkt}: 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 factorial.rkt > factorial.js - $ ls -l factorial.js - -rw-r--r-- 1 dyoo dyoo 27421 2011-07-11 22:02 factorial.js + $ 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{factorial.js}; let's generate the @tt{runtime.js} and save +@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 @@ -272,7 +284,7 @@ it as well. At the command-line: }| 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{factorial.rkt}. +@filepath{fact.rkt}. @filebox["index.html"]{ @verbatim|{