Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add defjsmacro to make generating JS cleaner #10

Merged
merged 2 commits into from

2 participants

@ibdknox

I've added in the notion of custom forms that can be created using defjsmacro. This allows for the use of clojure generated JS without having to wrap every single call in (clj). This makes a huge difference when you might be doing a bunch of nested calls to clj functions.

ibdknox added some commits
@ibdknox ibdknox added custom forms
Signed-off-by: Chris Granger <ibdknox@gmail.com>
634e038
@ibdknox ibdknox add in defjsmacro
Signed-off-by: Chris Granger <ibdknox@gmail.com>
2eae960
@arohner arohner merged commit 3df6a1e into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 9, 2011
  1. @ibdknox

    added custom forms

    ibdknox authored
    Signed-off-by: Chris Granger <ibdknox@gmail.com>
  2. @ibdknox

    add in defjsmacro

    ibdknox authored
    Signed-off-by: Chris Granger <ibdknox@gmail.com>
This page is out of date. Refresh to see the latest.
Showing with 45 additions and 2 deletions.
  1. +34 −0 src/com/reasonr/scriptjure.clj
  2. +11 −2 test/test_scriptjure.clj
View
34 src/com/reasonr/scriptjure.clj
@@ -91,9 +91,11 @@
(def chainable-infix-operators (set ['+ '- '* '/ '& '| '&& '||]))
+
(defn special-form? [expr]
(contains? special-forms expr))
+
(defn infix-operator? [expr]
(contains? infix-operators expr))
@@ -248,6 +250,8 @@
body (rest expr)]
(str (emit-function nil signature body))))))
+(declare emit-custom custom-form?)
+
(derive clojure.lang.Cons ::list)
(derive clojure.lang.IPersistentList ::list)
@@ -260,6 +264,7 @@
(> (count (str head)) 1)
(not (= (rstr/get (str head) 1) \.))) (emit-special 'dot-method expr)
+ (custom-form? head) (emit-custom head expr)
(special-form? head) (emit-special head expr)
(infix-operator? head) (emit-infix head expr)
(prefix-unary? head) (emit-prefix-unary head expr)
@@ -334,3 +339,32 @@
"takes one or more forms. Returns a string of the forms translated into javascript"
[& forms]
`(_js (quasiquote ~forms)))
+
+
+;;**********************************************************
+;; Custom forms
+;;**********************************************************
+
+(defonce custom-forms (atom {}))
+
+(defmacro defjsmacro [nme params & body]
+ `(do
+ (defn ~nme ~params
+ (js*
+ ~@body))
+ (add-custom-form '~nme ~nme)))
+
+(defn add-custom-form [form func]
+ (swap! custom-forms assoc form func))
+
+(defn get-custom [form]
+ (get @custom-forms form))
+
+(defn custom-form? [expr]
+ (get-custom expr))
+
+(defn emit-custom [head expr]
+ (when-let [func (get-custom head)]
+ (let [v (apply func (next expr))]
+ (emit v))))
+
View
13 test/test_scriptjure.clj
@@ -7,7 +7,7 @@
"strip extraneous whitespace so tests don't fail because of differences in whitespace"
[str]
(str/trim (str/replace (str/replace str #"\n" " ") #"[ ]+" " ")))
-
+
(deftest number-literal
(is (= (js 42) "42"))
(is (= (js 1/2) "0.5")))
@@ -166,4 +166,13 @@
(is (= (strip-whitespace(js (set! x 1 y 2)))
"x = 1; y = 2;")))
-(run-tests)
+(defjsmacro prn-hw [n]
+ (alert (str "hello world " (clj n))))
+
+(deftest custom-form-add
+ (is (get-custom 'prn-hw)))
+
+(deftest custom-form-use
+ (is (= (js (prn-hw "custom"))) "alert(\"hello world custom\")"))
+
+(run-tests)
Something went wrong with that request. Please try again.