aboutsummaryrefslogtreecommitdiff
path: root/clojurescript
diff options
context:
space:
mode:
authorChouser <chouser@n01se.net>2008-09-17 00:03:44 +0000
committerChouser <chouser@n01se.net>2008-09-17 00:03:44 +0000
commitc68f9aefb7d4b4f2240e196556f83f76e9fd4ef5 (patch)
tree31721cb18ea8ba1525e181629316db80bbac6c08 /clojurescript
parent80c4d45654e505dbfb148441da75184b02e0f62d (diff)
ClojureScript: even more local var emits are now avoided.
Diffstat (limited to 'clojurescript')
-rw-r--r--clojurescript/t02.cljs22
-rw-r--r--clojurescript/t02.html12
-rw-r--r--clojurescript/tojs.clj10
3 files changed, 39 insertions, 5 deletions
diff --git a/clojurescript/t02.cljs b/clojurescript/t02.cljs
new file mode 100644
index 00000000..e25d0543
--- /dev/null
+++ b/clojurescript/t02.cljs
@@ -0,0 +1,22 @@
+; This may look like Clojure, but it's actually ClojureScript. Macros
+; may be used here, but should be defined elsewhere, in regular
+; Clojure code.
+(ns n01se)
+
+(defn my-take
+ "Returns a lazy seq of the first n items in coll, or all items if
+ there are fewer than n."
+ [n coll]
+ (when (and (pos? n) (seq coll))
+ (lazy-cons (first coll) (my-take (dec n) (rest coll)))))
+
+(defn script-src []
+ (for [elem (.getElementsByTagName document "script")]
+ (do
+ (prn :next)
+ (if-let src (.src elem)
+ src
+ "--none--"))))
+
+(doseq src (my-take 2 (script-src))
+ (prn src))
diff --git a/clojurescript/t02.html b/clojurescript/t02.html
new file mode 100644
index 00000000..d0f0ebe6
--- /dev/null
+++ b/clojurescript/t02.html
@@ -0,0 +1,12 @@
+<html>
+ <body>
+ <textarea rows="24" cols="80" id="ta"></textarea>
+ <script type="text/javascript">
+ var ta = document.getElementById( 'ta' );
+ function print( x ) { ta.value += x + "\n"; }
+ </script>
+ <script type="text/javascript" src="clj.js"></script>
+ <script type="text/javascript" src="PersistentVector-proto.js"></script>
+ <script type="text/javascript" src="t02.js"></script>
+ </body>
+</html>
diff --git a/clojurescript/tojs.clj b/clojurescript/tojs.clj
index 982ddcc2..b4715b2f 100644
--- a/clojurescript/tojs.clj
+++ b/clojurescript/tojs.clj
@@ -26,7 +26,7 @@
(defn fnmethod [fm maxm ctx]
(let [lm (into {} (for [[lb lb] (.locals fm)]
[lb (str (.name lb) "_" (.idx lb))]))
- thisfn (some #(when (= (.name %) (:fnname ctx)) %) (keys lm))
+ thisfn (first (filter #(= 0 (.idx %)) (keys lm)))
[body has-recur] (binding [*has-recur* false]
[(tojs (.body fm)
(merge-with merge ctx {:localmap lm}))
@@ -35,7 +35,7 @@
(when has-recur ["_cnt" "_rtn"])
(vals (reduce dissoc lm (cons thisfn (when (= fm maxm)
(.reqParms fm)))))
- (when thisfn [(str (lm thisfn) "=arguments.callee")])
+ (when (:fnname ctx) [(str (lm thisfn) "=arguments.callee")])
(when (not= fm maxm)
(for [lb (.reqParms fm)]
[(lm lb) "=arguments[" (dec (.idx lb)) "]"]))
@@ -44,12 +44,12 @@
(count (.reqParms fm)) ")"]))]
(.reqParms maxm)
(vstr [(when (seq inits)
- (apply vector "var " (interpose "," inits)))
+ [(apply vector "var " (interpose "," inits)) ";\n"])
(if has-recur
- [";do{_cnt=0;\n_rtn="
+ ["do{_cnt=0;_rtn="
body
"\n}while(_cnt);return _rtn;"]
- [";return (" body ")"])])))
+ ["return (" body ")"])])))
(defmethod tojs clojure.lang.Compiler$FnExpr [e ctx]
(let [maxm (or (.variadicMethod e)