Skip to content
Browse files

Added support for js/this in the compiler

  • Loading branch information...
1 parent dc5c94c commit ab9807a5b143f3d1f681e9edb3d0897fdbb41148 @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"
@@ -38,16 +40,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))))
;;todo - move to core.cljs, using js
(def ^String bootjs "
@@ -88,7 +92,9 @@ goog.require = function(rule){Packages.clojure.lang.RT[\"var\"](\"cljs.compiler\
(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
@@ -3221,3 +3221,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 ab9807a

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