2 Javascript
The Javascript language in Mirrors allows the programatic assembly of syntactically valid Javascript. The language uses the AST structures and pretty printing libraries from Dave Herman’s javascript.plt package as its underlying representation.
Javascript syntax describes the syntax for creating blocks of Javascript and Rendering Javascript and sending Javascript responses describes how to send Javascript responses in the PLT web server.
2.1 Javascript syntax
(require (planet untyped/mirrors/javascript/syntax)) |
Mirrors provides a set of macros for rendering blocks of Javascript, intended to replace Jay McCarthy’s javascript.plt (which is no longer in development). Very little checking is performed to make sure the Javascript code makes sense.
Builds a Javascript declaration, statement or expression. Unquote can be used to insert Scheme expressions resulting in Boolean, numeric, string, symbol, bytes or URL values.
custom-syntax-id indicates an identifier bound with define-javascript-syntax.
(define-javascript-syntax (id arg ) js-expr) |
(define-javascript-syntax id js-transformer) |
(define-javascript-syntax id js-transformer expr-transformer) |
Defines a custom syntactic form for use in js blocks.
The first form above behaves like define-syntax-rule. js-expr should be a regular js block.
Examples: | ||
| ||
> (javascript->string (js (!max 1 2))) | ||
"1 > 2 ? 1 : 2;" | ||
> (!max 1 2) | ||
eval:4:0: !max: must be used as a javascript expression in: | ||
(!max 1 2) |
The second and third forms behave like define-match-expander. js-transformer is a syntax transformer procedure used during Javascript expansion, which accepts a Javascript form as an input and returns a complete js block representing the expansion. expr-transformer is a transformer procedure that is used in regular Scheme expansion. When js-transformer is omitted, use of the syntax form outside of a Javascript block results in a syntax error.
Examples: | |||||||
| |||||||
> (javascript->string (js (!min 1 2))) | |||||||
"1 < 2 ? 1 : 2;" | |||||||
> (!min 1 2) | |||||||
1 |
(opt-js boolean-expr js-stmt ) |
Syntactic shorthand for:
(if boolean-expr |
(js js-stmt ) |
(js)) |
Only works at the statement level.
Examples: | ||
| ||
"alert(\"This alert will be shown....\")" | ||
| ||
"" |
(javascript? val) → boolean? |
val : any |
Returns #t if val is a Javascript declaration, statement or expression.
(javascript-declaration? val) → boolean? |
val : any |
Returns #t if val is a Javascript declaration.
(javascript-statement? val) → boolean? |
val : any |
Returns #t if val is a Javascript statement.
(javascript-expression? val) → boolean? |
val : any |
Returns #t if val is a Javascript expression.
2.2 Rendering Javascript and sending Javascript responses
2.2.1 Rendering Javascript in string form
(javascript->string val) → string? |
val : javascript-statement? |
Renders a Javascript statement as a compact string with no line breaks or indentation.
(javascript->pretty-string val) → string? |
val : javascript-statement? |
Renders a Javascript statement as a formatted string with line breaks and indentation.
render-pretty-javascript? : (parameter boolean?) |
Affects the output of javascript->string and javascript->pretty-string:
when render-pretty-javascript? is set to #t (the default value), the rendering procedures behave as documented above;
when render-pretty-javascript? is set to #f, the rendering procedures switch to an experimental renderer that writes all Javascript extremely quickly onto a single line.
Essentially, setting render-pretty-javascript? to #t gives better rendering performance at the expense of legibility. The performance gains are only noticeable when rendering large (1000+ line) blocks of Javascript.
At the time of writing the fast renderer has not been properly tested: use it with caution!
2.2.2 Sending HTTP responses with Javascript content
| ||||||||||||||||||||||||||||||||||||||||||
code : integer? = 200 | ||||||||||||||||||||||||||||||||||||||||||
message : (U string? bytes?) = #"OK" | ||||||||||||||||||||||||||||||||||||||||||
seconds : integer? = (current-seconds) | ||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||
headers : (alistof symbol? string?) = no-cache-http-headers | ||||||||||||||||||||||||||||||||||||||||||
content : javascript-statement? |
Takes a js statement and wraps it in an HTTP response object that can be used with the PLT web server (including procedures such as send/suspend and send/suspend/dispatch). The keyword arguments correspond to the first five arguments of make-response/full.