Permalink
Browse files

analyzeable js*

js* now consistently maps onto inlining macros. js* ast nodes are now
annotated with the name of the original macro in the :js-op field
  • Loading branch information...
1 parent 99a7c93 commit 6d8eade531cec22c0b501c4fc3e8553182a7382d @swannodette swannodette committed Aug 10, 2013
Showing with 19 additions and 11 deletions.
  1. +12 −3 src/clj/cljs/analyzer.clj
  2. +7 −8 src/clj/cljs/core.clj
View
@@ -832,7 +832,8 @@
enve (assoc env :context :expr)
argexprs (vec (map #(analyze enve %) args))]
{:env env :op :js :segs (seg jsform) :args argexprs
- :tag (-> form meta :tag) :form form :children argexprs}))
+ :tag (-> form meta :tag) :form form :children argexprs
+ :js-op (-> form meta :js-op)}))
(let [interp (fn interp [^String s]
(let [idx (.indexOf s "~{")]
(if (= -1 idx)
@@ -841,7 +842,7 @@
inner (:name (resolve-existing-var env (symbol (subs s (+ 2 idx) end))))]
(cons (subs s 0 idx) (cons inner (interp (subs s (inc end)))))))))]
{:env env :op :js :form form :code (apply str (interp jsform))
- :tag (-> form meta :tag)})))
+ :tag (-> form meta :tag) :js-op (-> form meta :js-op)})))
(defn parse-invoke
[env [f & args :as form]]
@@ -903,7 +904,15 @@
form
(if-let [mac (and (symbol? op) (get-expander op env))]
(binding [*ns* (create-ns *cljs-ns*)]
- (apply mac form env (rest form)))
+ (let [form' (apply mac form env (rest form))]
+ (if (seq? form')
+ (let [sym' (first form')
+ sym (first form)]
+ (if (= sym' 'js*)
+ (vary-meta form' assoc
+ :js-op (if (namespace sym) sym (symbol "cljs.core" (str sym))))
+ form'))
+ form')))
(if (symbol? op)
(let [opname (str op)]
(cond
View
@@ -188,7 +188,7 @@
(let [strs (->> (repeat (count xs) "cljs.core.str(~{})")
(interpose ",")
(apply core/str))]
- (concat (list 'js* (core/str "[" strs "].join('')")) xs)))
+ (list* 'js* (core/str "[" strs "].join('')") xs)))
(defn bool-expr [e]
(vary-meta e assoc :tag 'boolean))
@@ -538,10 +538,13 @@
~@impls))
(new ~t ~@locals nil))))
+(defmacro ^:private js-this []
+ (list 'js* "this"))
+
(defmacro this-as
"Defines a scope where JavaScript's implicit \"this\" is bound to the name provided."
[name & body]
- `(let [~name (~'js* "this")]
+ `(let [~name (js-this)]
~@body))
(defn to-property [sym]
@@ -1177,9 +1180,7 @@
(take (count rest))
(interpose ",")
(apply core/str))]
- (concat
- (list 'js* (core/str "[" xs-str "]"))
- rest)))
+ (list* 'js* (core/str "[" xs-str "]") rest)))
(defmacro make-array
[size]
@@ -1190,9 +1191,7 @@
(take (quot (count rest) 2))
(interpose ",")
(apply core/str))]
- (concat
- (list 'js* (core/str "{" kvs-str "}"))
- rest)))
+ (list* 'js* (core/str "{" kvs-str "}") rest)))
(defmacro alength [a]
(list 'js* "~{}.length" a))

1 comment on commit 6d8eade

Contributor

brandonbloom commented on 6d8eade Aug 11, 2013

Clever!

Please sign in to comment.