Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added magic js namespace, replacing goog.global for accessing globals

  • Loading branch information...
commit 954e8529b1ec814f40af77d6035f90e93a9126ea 1 parent cd3eaa9
Rich Hickey richhickey authored
6 samples/twitterbuzz/src/twitterbuzz/anneal.cljs
@@ -9,13 +9,13 @@
9 9 (ns twitterbuzz.anneal)
10 10
11 11 (defn exp [x]
12   - (goog.global.Math/exp x))
  12 + (js/Math.exp x))
13 13
14 14 (defn abs [x]
15   - (goog.global.Math/abs x))
  15 + (js/Math.abs x))
16 16
17 17 (defn random []
18   - (goog.global.Math/random))
  18 + (js/Math.random))
19 19
20 20 (defn standard-prob [e e1 temp]
21 21 (if (< e1 e)
2  samples/twitterbuzz/src/twitterbuzz/layout.cljs
@@ -18,7 +18,7 @@
18 18 (* x x))
19 19
20 20 (defn sqrt [x]
21   - (goog.global.Math/sqrt x))
  21 + (js/Math.sqrt x))
22 22
23 23 (defn dist [{x1 :x y1 :y} {x2 :x y2 :y}]
24 24 (sqrt (+ (sqr (- x2 x1)) (sqr (- y2 y1)))))
2  samples/twitterbuzz/src/twitterbuzz/showgraph.cljs
@@ -36,7 +36,7 @@
36 36 (+ (* unit-arg (- canvas-size avatar-size)) (/ avatar-size 2)))
37 37
38 38 (defn log [& args]
39   - (goog.global.console/log (apply pr-str args)))
  39 + (js/console.log (apply pr-str args)))
40 40
41 41 (def avatar-hover
42 42 (doto
2  script/compile
@@ -10,7 +10,6 @@ java -jar closure/compiler/compiler.jar \
10 10 --compilation_level=SIMPLE_OPTIMIZATIONS \
11 11 --warning_level=VERBOSE \
12 12 --formatting=PRETTY_PRINT \
13   - --jscomp_off=undefinedVars \
14 13 --jscomp_off=missingProperties \
15 14 --js closure/library/closure/goog/base.js \
16 15 --js closure/library/closure/goog/string/string.js \
@@ -27,7 +26,6 @@ java -jar closure/compiler/compiler.jar \
27 26 --compilation_level=ADVANCED_OPTIMIZATIONS \
28 27 --warning_level=VERBOSE \
29 28 --formatting=PRETTY_PRINT \
30   - --jscomp_off=undefinedVars \
31 29 --jscomp_off=missingProperties \
32 30 --js closure/library/closure/goog/base.js \
33 31 --js closure/library/closure/goog/string/string.js \
128 src/clj/cljs/compiler.clj
@@ -29,7 +29,12 @@
29 29 (def ^:dynamic *cljs-warn-on-undeclared* false)
30 30
31 31 (defn munge [s]
32   - (let [ms (clojure.lang.Compiler/munge (str s))
  32 + (let [ss (str s)
  33 + ms (if (.contains ss "]")
  34 + (let [idx (inc (.lastIndexOf ss "]"))]
  35 + (str (subs ss 0 idx)
  36 + (clojure.lang.Compiler/munge (subs ss idx))))
  37 + (clojure.lang.Compiler/munge ss))
33 38 ms (if (js-reserved ms) (str ms "$") ms)]
34 39 (if (symbol? s)
35 40 (symbol ms)
@@ -70,67 +75,76 @@ goog.require = function(rule){Packages.clojure.lang.RT[\"var\"](\"cljs.compiler\
70 75 (and (get (:defs (@namespaces 'cljs.core)) sym)
71 76 (not (contains? (-> env :ns :excludes) sym))))
72 77
  78 +(defn js-var [sym]
  79 + (let [parts (string/split (name sym) #"\.")
  80 + step (fn [part] (str "['" part "']"))]
  81 + (apply str "goog.global" (map step parts))))
  82 +
73 83 (defn resolve-existing-var [env sym]
74   - (let [s (str sym)
75   - lb (-> env :locals sym)
76   - nm
77   - (cond
78   - lb (:name lb)
79   -
80   - (namespace sym)
81   - (let [ns (namespace sym)
82   - ns (if (= "clojure.core" ns) "cljs.core" ns)
83   - full-ns (resolve-ns-alias env ns)]
84   - (confirm-var-exists env full-ns (symbol (name sym)))
85   - (symbol (str full-ns "." (munge (name sym)))))
86   -
87   - (.contains s ".")
88   - (munge (let [idx (.indexOf s ".")
89   - prefix (symbol (subs s 0 idx))
90   - suffix (subs s idx)
91   - lb (-> env :locals prefix)]
92   - (if lb
93   - (symbol (str (:name lb) suffix))
94   - (do
95   - (confirm-var-exists env prefix (symbol suffix))
96   - sym))))
  84 + (if (= (namespace sym) "js")
  85 + {:name (js-var sym)}
  86 + (let [s (str sym)
  87 + lb (-> env :locals sym)
  88 + nm
  89 + (cond
  90 + lb (:name lb)
  91 +
  92 + (namespace sym)
  93 + (let [ns (namespace sym)
  94 + ns (if (= "clojure.core" ns) "cljs.core" ns)
  95 + full-ns (resolve-ns-alias env ns)]
  96 + (confirm-var-exists env full-ns (symbol (name sym)))
  97 + (symbol (str full-ns "." (munge (name sym)))))
  98 +
  99 + (.contains s ".")
  100 + (munge (let [idx (.indexOf s ".")
  101 + prefix (symbol (subs s 0 idx))
  102 + suffix (subs s idx)
  103 + lb (-> env :locals prefix)]
  104 + (if lb
  105 + (symbol (str (:name lb) suffix))
  106 + (do
  107 + (confirm-var-exists env prefix (symbol suffix))
  108 + sym))))
97 109
98   - :else
99   - (let [full-ns (if (core-name? env sym)
100   - 'cljs.core
101   - (-> env :ns :name))]
102   - (confirm-var-exists env full-ns sym)
103   - (munge (symbol (str full-ns "." (munge (name sym)))))))]
104   - {:name nm}))
  110 + :else
  111 + (let [full-ns (if (core-name? env sym)
  112 + 'cljs.core
  113 + (-> env :ns :name))]
  114 + (confirm-var-exists env full-ns sym)
  115 + (munge (symbol (str full-ns "." (munge (name sym)))))))]
  116 + {:name nm})))
105 117
106 118 (defn resolve-var [env sym]
107   - (let [s (str sym)
108   - lb (-> env :locals sym)
109   - nm
110   - (cond
111   - lb (:name lb)
  119 + (if (= (namespace sym) "js")
  120 + {:name (js-var sym)}
  121 + (let [s (str sym)
  122 + lb (-> env :locals sym)
  123 + nm
  124 + (cond
  125 + lb (:name lb)
112 126
113   - (namespace sym)
114   - (let [ns (namespace sym)
115   - ns (if (= "clojure.core" ns) "cljs.core" ns)]
116   - (symbol (str (resolve-ns-alias env ns) "." (munge (name sym)))))
117   -
118   - (.contains s ".")
119   - (munge (let [idx (.indexOf s ".")
120   - prefix (symbol (subs s 0 idx))
121   - suffix (subs s idx)
122   - lb (-> env :locals prefix)]
123   - (if lb
124   - (symbol (str (:name lb) suffix))
125   - sym)))
126   -
127   - :else
128   - (munge (symbol (str
129   - (if (core-name? env sym)
130   - 'cljs.core
131   - (-> env :ns :name))
132   - "." (munge (name sym))))))]
133   - {:name nm}))
  127 + (namespace sym)
  128 + (let [ns (namespace sym)
  129 + ns (if (= "clojure.core" ns) "cljs.core" ns)]
  130 + (symbol (str (resolve-ns-alias env ns) "." (munge (name sym)))))
  131 +
  132 + (.contains s ".")
  133 + (munge (let [idx (.indexOf s ".")
  134 + prefix (symbol (subs s 0 idx))
  135 + suffix (subs s idx)
  136 + lb (-> env :locals prefix)]
  137 + (if lb
  138 + (symbol (str (:name lb) suffix))
  139 + sym)))
  140 +
  141 + :else
  142 + (munge (symbol (str
  143 + (if (core-name? env sym)
  144 + 'cljs.core
  145 + (-> env :ns :name))
  146 + "." (munge (name sym))))))]
  147 + {:name nm})))
134 148
135 149 (defn- comma-sep [xs]
136 150 (apply str (interpose "," xs)))
4 src/clj/cljs/core.clj
@@ -61,9 +61,7 @@
61 61 (defmacro extend-type [tsym & impls]
62 62 (let [resolve #(let [ret (:name (cljs.compiler/resolve-var (dissoc &env :locals) %))]
63 63 (assert ret (str "Can't resolve: " %))
64   - (if (.startsWith (name ret) "goog.global.")
65   - (symbol (subs (name ret) 12))
66   - ret))
  64 + ret)
67 65 impl-map (loop [ret {} s impls]
68 66 (if (seq s)
69 67 (recur (assoc ret (resolve (first s)) (take-while seq? (next s)))
36 src/cljs/cljs/core.cljs
@@ -204,7 +204,7 @@
204 204 IHash
205 205 (-hash [o] 0))
206 206
207   -(extend-type goog.global.Date
  207 +(extend-type js/Date
208 208 IEquiv
209 209 (-equiv [o other] (identical? (.toString o) (.toString other))))
210 210
@@ -1132,11 +1132,10 @@ reduces them without incurring seq initialization"
1132 1132 (ci-reduce string f start))))
1133 1133
1134 1134 ;;hrm
1135   -(defn- string-call_
1136   - ([_ coll] (get coll (js* "this.toString()")))
1137   - ([_ coll not-found] (get coll (js* "this.toString()") not-found)))
1138   -
1139   -(js* "String.prototype.call = ~{}" string-call_)
  1135 +(set! js/String.prototype.call
  1136 + (fn
  1137 + ([_ coll] (get coll (js* "this.toString()")))
  1138 + ([_ coll not-found] (get coll (js* "this.toString()") not-found))))
1140 1139
1141 1140 ; could use reify
1142 1141 ;;; LazySeq ;;;
@@ -2368,14 +2367,10 @@ reduces them without incurring seq initialization"
2368 2367 post-match (subs s (+ match-idx (count match-str)))]
2369 2368 (when match-data (lazy-seq (cons match-data (re-seq re post-match))))))
2370 2369
2371   -;;; goog.global.RegExp needs special handling for reasons not yet
2372   -;;; known. This works in advanced mode, though. See
2373   -;;; http://code.google.com/p/closure-library/source/browse/trunk/closure/goog/debug/reflect.js?r=701#99
2374   -;;; for a potential lead.
2375 2370 (defn re-pattern
2376 2371 "Returns an instance of RegExp which has compiled the provided string."
2377 2372 [s]
2378   - (doto (js* "goog.global['RegExp'](~{s})") (.compile)))
  2373 + (doto (js/RegExp. s) (.compile ())))
2379 2374
2380 2375 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Printing ;;;;;;;;;;;;;;;;
2381 2376
@@ -2388,7 +2383,7 @@ reduces them without incurring seq initialization"
2388 2383 ; This should be different in different runtime environments. For example
2389 2384 ; when in the browser, could use console.debug instead of print.
2390 2385 (defn string-print [x]
2391   - (js* "print(~{x})")
  2386 + (js/print x)
2392 2387 nil)
2393 2388
2394 2389 (defn flush [] ;stub
@@ -2870,8 +2865,8 @@ reduces them without incurring seq initialization"
2870 2865 (true? false)
2871 2866 (false? false)
2872 2867 (false? true)
2873   - (true? goog.global.undefined)
2874   - (false? goog.global.undefined)]))
  2868 + (true? js/undefined)
  2869 + (false? js/undefined)]))
2875 2870 ;; apply
2876 2871 (assert (= 0 (apply + nil)))
2877 2872 (assert (= 0 (apply + (list))))
@@ -2943,11 +2938,10 @@ reduces them without incurring seq initialization"
2943 2938 (assert (= 1 (try* 1 (finally (reset! a 42)))))
2944 2939 (assert (= 42 (deref a))))
2945 2940
2946   - ;;this fails in v8 advanced mode - we need to trim off goog.global in catch
2947   - #_(let [a (atom nil)]
  2941 + (let [a (atom nil)]
2948 2942 (assert (= 1 (try 1)))
2949   - (assert (= 2 (try 1 (throw (goog.global.Error.)) (catch goog.global.Error e 2))))
2950   - (assert (= 2 (try 1 (throw (goog.global.Error.)) (catch goog.global.Error e 1 2))))
  2943 + (assert (= 2 (try 1 (throw (js/Error.)) (catch js/Error e 2))))
  2944 + (assert (= 2 (try 1 (throw (js/Error.)) (catch js/Error e 1 2))))
2951 2945 (assert (= 1 (try 1 (finally (reset! a 42)))))
2952 2946 (assert (= 42 (deref a))))
2953 2947
@@ -3170,9 +3164,9 @@ reduces them without incurring seq initialization"
3170 3164 (assert (= @s (reverse v))))
3171 3165
3172 3166 ;; delay
3173   - ;; (let [d (delay (. (goog.global.Date.) (getTime)))]
  3167 + ;; (let [d (delay (. (js/Date.) (getTime)))]
3174 3168 ;; (assert (false? (realized? d)))
3175   - ;; (let [d2 (. (goog.global.Date.) (getTime))]
  3169 + ;; (let [d2 (. (js/Date.) (getTime))]
3176 3170 ;; (assert (> d2 (deref d))))
3177 3171 ;; (assert (true? (realized? d)))
3178 3172 ;; (let [d3 (deref d)]
@@ -3258,7 +3252,7 @@ reduces them without incurring seq initialization"
3258 3252 :ok
3259 3253 )
3260 3254
3261   -#_(goog.global/print (assoc {} :a 1))
  3255 +#_(js/print (assoc {} :a 1))
3262 3256
3263 3257
3264 3258
8 src/cljs/cljs/reader.cljs
@@ -106,11 +106,11 @@ nil if the end of stream has been reached")
106 106 (nth groups 3) [(nth groups 3) 10]
107 107 (nth groups 4) [(nth groups 4) 16]
108 108 (nth groups 5) [(nth groups 5) 8]
109   - (nth groups 7) [(nth groups 7) (goog.global/parseInt (nth groups 7))]
  109 + (nth groups 7) [(nth groups 7) (js/parseInt (nth groups 7))]
110 110 :default [nil nil])]
111 111 (if (nil? n)
112 112 nil
113   - (* negate (goog.global/parseInt n radix)))))))
  113 + (* negate (js/parseInt n radix)))))))
114 114
115 115
116 116 (defn- match-ratio
@@ -118,11 +118,11 @@ nil if the end of stream has been reached")
118 118 (let [groups (re-find ratio-pattern s)
119 119 numinator (nth groups 1)
120 120 denominator (nth groups 2)]
121   - (/ (goog.global/parseInt numinator) (goog.global/parseInt denominator))))
  121 + (/ (js/parseInt numinator) (js/parseInt denominator))))
122 122
123 123 (defn- match-float
124 124 [s]
125   - (goog.global/parseFloat s))
  125 + (js/parseFloat s))
126 126
127 127 (defn- match-number
128 128 [s]

0 comments on commit 954e852

Please sign in to comment.
Something went wrong with that request. Please try again.