Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Keep locals in dce.

  • Loading branch information...
commit 56838c4d3c29a1336e66d4a77ce67ada1e41efb1 1 parent 370c03b
Phil Hagelberg authored
21  src/dce/Exception.clj
@@ -70,11 +70,20 @@
70 70
 (defn -count [self]
71 71
   (count (.state self)))
72 72
 
73  
-(defn throw+
  73
+(defn- locals [env]
  74
+  (into {} (for [[n _] env]
  75
+             [(list 'quote n) n])))
  76
+
  77
+(defmacro throw+
74 78
   ([x]
75  
-     (throw
76  
-      (if (instance? Throwable x)
77  
-        x
78  
-        (dce.Exception. x))))
  79
+     `(let [x# ~x]
  80
+        (throw
  81
+         (if (instance? Throwable x#)
  82
+           x#
  83
+           (dce.Exception.
  84
+            (if (:locals x#)
  85
+              x#
  86
+              (assoc x# :locals ~(locals &env))))))))
79 87
   ([k v & kvs]
80  
-     (throw+ (apply hash-map k v kvs))))
  88
+     (let [m (apply hash-map k v kvs)]
  89
+       `(throw+ ~m))))
9  test/dce/test/exception.clj
@@ -60,6 +60,8 @@
60 60
       :bummer-dude)
61 61
     (catch funky? x#
62 62
       :external-pred)
  63
+    (catch :locals e#
  64
+      e#)
63 65
     (catch Exception _#
64 66
       :exception)))
65 67
 
@@ -70,4 +72,9 @@
70 72
                      (throw+ (Exception. "whoops")))))
71 73
   (is (= :external-pred (mega-try
72 74
                          (throw+ :funkiness :most-excellent))))
73  
-  (is (thrown? Throwable (mega-try (throw+ (Throwable. "hi'"))))))
  75
+  (is (thrown? Throwable (mega-try (throw+ (Throwable. "hi"))))))
  76
+
  77
+(deftest test-locals
  78
+  (is (= :not-variable ('a-local (:locals (mega-try
  79
+                                           (let [a-local :not-variable]
  80
+                                             (throw+ {}))))))))

0 notes on commit 56838c4

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