Permalink
Browse files

Added support for js/this in the compiler

  • Loading branch information...
1 parent 63462a3 commit b34f381baf8ba580ea562cb70581e11389a73d66 @fogus fogus committed Sep 2, 2011
Showing with 18 additions and 11 deletions.
  1. +17 −11 src/clj/cljs/compiler.clj
  2. +1 −0 src/cljs/cljs/core.cljs
View
28 src/clj/cljs/compiler.clj
@@ -16,6 +16,8 @@
(declare resolve-var)
(require 'cljs.core)
+(def js-this 'this)
+
(def js-reserved
#{"abstract" "boolean" "break" "byte" "case"
"catch" "char" "class" "const" "continue"
@@ -37,16 +39,18 @@
(def ^:dynamic *cljs-warn-on-undeclared* false)
(defn munge [s]
- (let [ss (str s)
- ms (if (.contains ss "]")
- (let [idx (inc (.lastIndexOf ss "]"))]
- (str (subs ss 0 idx)
- (clojure.lang.Compiler/munge (subs ss idx))))
- (clojure.lang.Compiler/munge ss))
- ms (if (js-reserved ms) (str ms "$") ms)]
- (if (symbol? s)
- (symbol ms)
- ms)))
+ (if (identical? s js-this)
+ s
+ (let [ss (str s)
+ ms (if (.contains ss "]")
+ (let [idx (inc (.lastIndexOf ss "]"))]
+ (str (subs ss 0 idx)
+ (clojure.lang.Compiler/munge (subs ss idx))))
+ (clojure.lang.Compiler/munge ss))
+ ms (if (js-reserved ms) (str ms "$") ms)]
+ (if (symbol? s)
+ (symbol ms)
+ ms))))
(defn confirm-var-exists [env prefix suffix]
(when *cljs-warn-on-undeclared*
@@ -73,7 +77,9 @@
(let [parts (string/split (name sym) #"\.")
first (first parts)
step (fn [part] (str "['" part "']"))]
- (apply str first (map step (rest parts)))))
+ (if (= (symbol first) js-this)
+ js-this
+ (apply str first (map step (rest parts))))))
(defn resolve-existing-var [env sym]
(if (= (namespace sym) "js")
View
1 src/cljs/cljs/core.cljs
@@ -3224,3 +3224,4 @@ reduces them without incurring seq initialization"
(let [str-name (name key-name)
bind-fn (goog.bind f obj)]
(js* "~{obj}[~{str-name}] = ~{bind-fn}"))))
+

0 comments on commit b34f381

Please sign in to comment.