On this page:
2.1 Javascript syntax
js
define-javascript-syntax
opt-js
javascript?
javascript-declaration?
javascript-statement?
javascript-expression?
2.2 Rendering Javascript and sending Javascript responses
2.2.1 Rendering Javascript in string form
javascript->string
javascript->pretty-string
render-pretty-javascript?
2.2.2 Sending HTTP responses with Javascript content
make-js-response
Version: 4.1.4.3

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.

(js js-stmt ...)
 
js-stmt = js-decl
  | (custom-syntax-id js-stmt ...)
  | (!begin js-stmt ...)
  | (!block js-stmt ...)
  | (!regexp str regexp-key ...)
  | (if js-expr js-stmt js-stmt)
  | (do js-stmt ... js-expr)
  | (while js-expr js-stmt ...)
  | (for (opt-decl opt-expr opt-expr) js-stmt ...)
  | (for-in (decl js-expr) js-stmt ...)
  | (break)
  | (break id)
  | (continue)
  | (continue id)
  | (return)
  | (return js-expr)
  | (with js-expr js-stmt ...)
  | (switch switch-clause ...)
  | (!label id js-stmt)
  | (throw js-expr)
  | (try try-clause ...)
  | ,expr
  | ,@expr
  | js-expr
     
js-decl = (custom-syntax-id js-decl ...)
  | (function id (id ...) js-stmt ...)
  | (var js-init ...)
     
js-init = id
  | [id js-expr]
  | ,expr
  | [id ,expr]
     
js-expr = (custom-syntax-id js-expr ...)
  | (!array js-expr ...)
  | (!object [property js-expr] ...)
  | (!index js-expr js-expr)
  | (!dot js-expr dot-expr ...)
  | (!all js-expr ...)
  | (? js-expr js-expr js-expr)
  | (new js-expr js-expr ...)
  | (function (id ...) js-stmt ...)
  | ,expr
  | null
  | this
  | id
  | boolean-literal
  | number-literal
  | string-literal
  | 'symbol-literal
     
custom-syntax-id = id
     
regexp-key = #:global? boolean-literal
  | #:global? ,boolean-expr
  | #:ci? boolean-literal
  | #:ci? ,boolean-expr
     
dot-expr = id    ; property-style accessor : a.b
  | (!index id expr) ; array-index-style accessor: a.b[1]
  | (id js-expr ...) ; method-style accessor: a.b(1, 2)
     
opt-decl = _
  | var-decl
     
opt-expr = _
  | expr

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:

  > (define-javascript-syntax (!max a b)
      (js (? (> a b) a b)))
  > (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:

  > (define-javascript-syntax !min
      (lambda (stx)
        (syntax-case stx ()
          [(_ a b) #'(js (? (< a b) a b))]))
      (lambda (stx)
        (syntax-case stx ()
          [(_ a b) #'(if (< a b) a b)])))
  > (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:

  > (javascript->string
     (opt-js #t (alert "This alert will be shown....")))

  "alert(\"This alert will be shown....\")"

  > (javascript->string
     (opt-js #f (alert "...but this alert won't.")))

  ""

(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:

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

(make-js-response [#:code code    
  #:message message    
  #:seconds seconds    
  #:mime-type mime-type    
  #:headers headers]    
  content)  response/full?
  code : integer? = 200
  message : (U string? bytes?) = #"OK"
  seconds : integer? = (current-seconds)
  mime-type : (U string? bytes?)
   = #"text/javascript; charset=utf-8"
  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.