Permalink
Browse files

* src/clj/cljs/compiler.clj: add no-op to the compiler. allow (set! *…

…unchecked-math* true/false) just like Clojure.
  • Loading branch information...
1 parent 4257e06 commit 9da24024e5ba284f5492f67009b770c6ad1e7d8c @swannodette swannodette committed Jan 29, 2012
Showing with 29 additions and 21 deletions.
  1. +27 −14 src/clj/cljs/compiler.clj
  2. +2 −7 src/clj/cljs/core.clj
View
@@ -9,11 +9,12 @@
(set! *warn-on-reflection* true)
(ns cljs.compiler
- (:refer-clojure :exclude [munge macroexpand-1])
+ (:refer-clojure :exclude [munge macroexpand-1 *unchecked-math*])
(:require [clojure.java.io :as io]
[clojure.string :as string]))
(declare resolve-var)
+(def ^:dynamic *unchecked-math* (atom false))
(require 'cljs.core)
(def js-reserved
@@ -237,6 +238,9 @@
~@body
(when-not (= :expr (:context env#)) (print ";\n"))))
+(defmethod emit :no-op [m]
+ (println "null;"))
+
(defmethod emit :var
[{:keys [info env] :as arg}]
(emit-wrap env (print (munge (:name info)))))
@@ -788,21 +792,30 @@
[_ env [_ target val] _]
(disallowing-recur
(let [enve (assoc env :context :expr)
- targetexpr (if (symbol? target)
- (do
- (let [local (-> env :locals target)]
- (assert (or (nil? local)
- (and (:field local)
- (:mutable local)))
- "Can't set! local var or non-mutable field"))
- (analyze-symbol enve target))
- (when (seq? target)
- (let [targetexpr (analyze-seq enve target nil)]
- (when (:field targetexpr)
- targetexpr))))
+ targetexpr (cond
+ (= target '*unchecked-math*)
+ (do
+ (reset! *unchecked-math* val)
+ ::set-unchecked-math)
+
+ (symbol? target)
+ (do
+ (let [local (-> env :locals target)]
+ (assert (or (nil? local)
+ (and (:field local)
+ (:mutable local)))
+ "Can't set! local var or non-mutable field"))
+ (analyze-symbol enve target))
+
+ :else (when (seq? target)
+ (let [targetexpr (analyze-seq enve target nil)]
+ (when (:field targetexpr)
+ targetexpr))))
valexpr (analyze enve val)]
(assert targetexpr "set! target must be a field or a symbol naming a var")
- {:env env :op :set! :target targetexpr :val valexpr :children [targetexpr valexpr]})))
+ (if (= targetexpr ::set-unchecked-math)
+ {:env env :op :no-op}
+ {:env env :op :set! :target targetexpr :val valexpr :children [targetexpr valexpr]}))))
(defmethod parse 'ns
[_ env [_ name & args] _]
View
@@ -61,11 +61,6 @@
(defmacro aset [a i v]
(list 'js* "(~{}[~{}] = ~{})" a i v))
-(def ^:dynamic *js-unchecked-arithmetic* (atom false))
-
-(defmacro set-unchecked-arithmetic! [v]
- (reset! *js-unchecked-arithmetic* v))
-
;; arith-ops and def-arith-op-errors are helpers to help
;; prevent code bloat from checked math ops
@@ -85,7 +80,7 @@
(let [return-expr (if (seq args)
`(~'js* ~expr ~@args)
`(~'js* ~expr))]
- (if @*js-unchecked-arithmetic*
+ (if @cljs.compiler/*unchecked-math*
return-expr
`(if (~'js* ~(s/join " && " (repeat (count args) "typeof ~{} == 'number'")) ~@args)
~return-expr
@@ -111,7 +106,7 @@
(defmacro /
([x] `(/ 1 ~x))
([x y] (let [expr (check-numbers "(~{} / ~{})" '/ x y)]
- (if @*js-unchecked-arithmetic*
+ (if @cljs.compiler/*unchecked-math*
expr
`(if (zero? ~y)
(throw (js/Error. "Arithmetic exception: divide by zero"))

0 comments on commit 9da2402

Please sign in to comment.