PageRenderTime 13ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/README.markdown

http://github.com/arohner/scriptjure
Markdown | 238 lines | 169 code | 69 blank | 0 comment | 0 complexity | c9940ed7ecc9d970a2571d9dfe976b60 MD5 | raw file
  1. Scriptjure is a Clojure library for generating javascript from Clojure forms. Its primary goal is to make it simple to embed "glue" javascript in Clojure webapps. Generated Scriptjure javascript is intended to be readable.
  2. At the moment, Scriptjure is very simple, but is still under active development.
  3. Sample Code
  4. ===========
  5. (use [com.reasonr.scriptjure :only (js)])
  6. (js (fn foo [e]
  7. (var x 42)
  8. (return (+ x e))))
  9. results in the string "function foo (e) { x = 42; return (x + e); }"
  10. Rules
  11. =====
  12. `(js)` is a macro that takes one or more sexprs and returns a string that is valid javascript.
  13. Numbers
  14. -------
  15. Clojure numbers are converted as you would expect:
  16. (js 42)
  17. => "42"
  18. Strings
  19. ------
  20. (js "foo")
  21. => "\"foo\""
  22. Symbols
  23. -------
  24. Clojure symbols and keywords are converted to javascript symbols:
  25. (js foo)
  26. => "foo"
  27. (js :bar)
  28. => "bar"
  29. Since JS is a macro, symbols will not be evaluated, so there is no need to quote them. Actually, (js 'foo) will be interpreted as (js (quote foo)), which is probably not what you want. Scriptjure makes no attempt to verify that a generated symbol is defined in the JS environment.
  30. Arrays, Maps
  31. ----------
  32. Clojure arrays and maps are converted to array literals, and JSON:
  33. (js [1 2 3])
  34. => "[1, 2, 3]"
  35. (js {:packages "columnchart"})
  36. => "{packages: \"columnchart\"}"
  37. Note that JSON map keys aren't necessarily converted to strings. If you want the key to be a string rather than a symbol, use a Clojure string. Yes, this doesn't follow the JSON spec, but some JS libraries require this.
  38. Lists
  39. ----
  40. Lists where the first element is a symbol are converted to function calls, and "special forms." If the head of the list is not one of the special forms, a list returns a normal function call.
  41. Normal Function Calls
  42. -----------------
  43. The head of the list is the name of the function. All remaining items in the list are treated as arguments to the call:
  44. (js (alert "hello world"))
  45. => "alert(\"hello world\")"
  46. (js (foo x y))
  47. => "foo(x, y)"
  48. Special Forms
  49. -----------
  50. If the head of the list is a symbol in the special forms list, rather than resulting in a normal function call, something else will happen:
  51. **var**
  52. (var symbol value)
  53. Var takes two arguments, and defines a new variable
  54. (js (var x 3))
  55. => "var x = 3;"
  56. **set!**
  57. (set! symbol value)
  58. Takes two arguments, assignment.
  59. (js (set! x 5))
  60. => "x = 5;"
  61. **if**
  62. (if test true-form & false-form)
  63. Returns a javascript if statement. Like Clojure, true-form and false-form take one form each. If you want multiple statements in the body, combine with a do statement.
  64. (js (if (== foo 3) (foo x) (bar y)))
  65. => "if ( (foo == 3) ) {
  66. foo(x);
  67. }
  68. else {
  69. bar(y);
  70. }"
  71. **try / catch / finally**
  72. (try expr* catch-clause? finally-clause?)
  73. catch-clause -> (catch e expr*)
  74. finally-clause -> (finally expr*)
  75. Returns a JavaScript `try` / `catch` / `finally` block. All non-`catch` and non-`finally` forms within a `try` form are executed in an implicit `do` statement. The `catch` clause (if present) generates an unconditional `catch` block (multiple conditional `catch` blocks are not supported at this time), with `e` bound to the exception object. The `finally` clause (if present) is used to generate a `finally` block. All expressions in the `catch` and `finally` clauses are executed in implicit `do` statements.
  76. (js (try
  77. (set! x 5)
  78. (catch e
  79. (print (+ "BOOM: " e)))
  80. (finally
  81. (print "saved!"))))
  82. => "try{
  83. x = 5;
  84. }
  85. catch(e){
  86. print((\"BOOM: \" + e));
  87. }
  88. finally{
  89. print(\"saved!\");
  90. }"
  91. An Exception will be thrown if there are no `catch` or `finally` clauses, or if there are more than one of either.
  92. **return**
  93. (return value)
  94. Takes one argument, results in a return statement
  95. (js (return x))
  96. => "return x;"
  97. **delete**
  98. (delete value)
  99. Takes one argument, results in a delete statement
  100. (js (delete x))
  101. => "delete x;"
  102. **new**
  103. (new Obj & args)
  104. Results in a new statement. The first argument is the object. All remaining items in the list are treated as arguments to the contructor.
  105. (js (new google.visualization.Query url))
  106. => "new google.visualization.Query(url)"
  107. **aget**
  108. (aget obj & indexes)
  109. (js (aget foo 42))
  110. => "foo[42]"
  111. Array access can also be chained. This is helpful not only for multidimensional arrays, but for reaching deep into objects using a series of keys (similar to `clojure.core/get-in`)
  112. (js (aget foo bar "baz"))
  113. => "foo[bar][\"baz\"]"
  114. To set an array, combine with set!
  115. (js (set! (aget foo 42) 13))
  116. **do**
  117. (do & exprs)
  118. Returns the series of expressions, separated by semicolons
  119. (js (do
  120. (var x 3)
  121. (var y 4)))
  122. => "var x = 3;
  123. var y = 4;"
  124. **dot Method calls**
  125. (. method Obj & args)
  126. Works like the dot form in Clojure. If the first item in the list is a dot, calls method on Obj. All remaining items are arguments to the method call
  127. (js (. google.chart bar :a :b))
  128. => "google.chart.bar(a,b)"
  129. .method also works:
  130. (js (.bar google.chart :a :b))
  131. => "google.chart.bar(a,b)"
  132. **fn**
  133. (fn [args] & body)
  134. (fn name [args] & body)
  135. Results in a function expression or statement. Forms in body are separated by semicolons
  136. (js (fn [e]
  137. (var x 42)
  138. (return (+ x e))))
  139. => "function (e) { var x = 42; return (x + e); }"
  140. **infix operators**
  141. (infix x y)
  142. If the head of the list is a symbol in the infix operator list, the list results in infix math. The current list is [+ - / * == === < > <= >= !=]. All infix operatations currently only support two operands. All infix expressions are parenthesized to avoid precedence issues.
  143. (js (> x y))
  144. => "(x > y)"
  145. ** Getting data into JS **
  146. To get the value of a clojure expression into javascript, use (clj)
  147. (let [foo 42]
  148. (js (+ 3 (clj foo))))
  149. => (js (+ 3 42)) => "(3 + 42)"
  150. `clj` is a "marker" in the js macro. The `clj` can contain arbitrary normal Clojure, and the result is passed into `(js)`. The `clj` form is allowed to return anything that scriptjure knows how to handle. Since `clj` is not a var, it never needs to be qualified. The clj form is only valid inside a `(js)` form.
  151. `clj` can be use anywhere in a `js` form:
  152. (js (fn (clj foo) [x] (return x)))
  153. This will return a javascript function, with the name being whatever Clojure value foo resolves to.
  154. ** Composing JS in Clojure **
  155. If you want to pass a js form from one clojure function to another, use js*
  156. (let [extra-js (js* (do (baz x) (var y 4)))]
  157. (defn gen-js [extra-js]
  158. (js (fn foo [x]
  159. (bar x)
  160. (clj extra-js)))))
  161. => "function foo(x) {
  162. bar(x);
  163. baz(x);
  164. var y = 4;
  165. }"
  166. `cljs` and `cljs*` are shortcuts for `(js (clj ...))` and `(js* (clj ..))` respectively. Note that both only take one form.
  167. License
  168. =======
  169. Scriptjure is licensed under the EPL, the same as Clojure core. See epl-v10.html in the root directory for more information.