Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added support for js/this in the compiler

  • Loading branch information...
commit b34f381baf8ba580ea562cb70581e11389a73d66 1 parent 63462a3
Fogus authored September 02, 2011
28  src/clj/cljs/compiler.clj
@@ -16,6 +16,8 @@
16 16
 (declare resolve-var)
17 17
 (require 'cljs.core)
18 18
 
  19
+(def js-this 'this)
  20
+
19 21
 (def js-reserved
20 22
   #{"abstract" "boolean" "break" "byte" "case"
21 23
     "catch" "char" "class" "const" "continue"
@@ -37,16 +39,18 @@
37 39
 (def ^:dynamic *cljs-warn-on-undeclared* false)
38 40
 
39 41
 (defn munge [s]
40  
-  (let [ss (str s)
41  
-        ms (if (.contains ss "]")
42  
-             (let [idx (inc (.lastIndexOf ss "]"))]
43  
-               (str (subs ss 0 idx)
44  
-                    (clojure.lang.Compiler/munge (subs ss idx))))
45  
-             (clojure.lang.Compiler/munge ss))
46  
-        ms (if (js-reserved ms) (str ms "$") ms)]
47  
-    (if (symbol? s)
48  
-      (symbol ms)
49  
-      ms)))
  42
+  (if (identical? s js-this)
  43
+    s
  44
+    (let [ss (str s)
  45
+          ms (if (.contains ss "]")
  46
+               (let [idx (inc (.lastIndexOf ss "]"))]
  47
+                 (str (subs ss 0 idx)
  48
+                      (clojure.lang.Compiler/munge (subs ss idx))))
  49
+               (clojure.lang.Compiler/munge ss))
  50
+          ms (if (js-reserved ms) (str ms "$") ms)]
  51
+      (if (symbol? s)
  52
+        (symbol ms)
  53
+        ms))))
50 54
 
51 55
 (defn confirm-var-exists [env prefix suffix]
52 56
   (when *cljs-warn-on-undeclared*
@@ -73,7 +77,9 @@
73 77
   (let [parts (string/split (name sym) #"\.")
74 78
         first (first parts)
75 79
         step (fn [part] (str "['" part "']"))]
76  
-    (apply str first (map step (rest parts)))))
  80
+    (if (= (symbol first) js-this)
  81
+      js-this
  82
+      (apply str first (map step (rest parts))))))
77 83
 
78 84
 (defn resolve-existing-var [env sym]
79 85
   (if (= (namespace sym) "js")
1  src/cljs/cljs/core.cljs
@@ -3224,3 +3224,4 @@ reduces them without incurring seq initialization"
3224 3224
     (let [str-name (name key-name)
3225 3225
           bind-fn (goog.bind f obj)]
3226 3226
       (js* "~{obj}[~{str-name}] = ~{bind-fn}"))))
  3227
+

0 notes on commit b34f381

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