Permalink
Browse files

IFn implemented for keywords, symbols, Arrays, Pairs

  • Loading branch information...
1 parent c31a2d7 commit 239347e4fa8fd7ef97d5a76cf96ff34b6a14eb1a @takeoutweight committed Mar 24, 2012
Showing with 42 additions and 4 deletions.
  1. +2 −2 src/clj/cljs/core.clj
  2. +39 −1 src/cljs/cljs/core.cljs
  3. +1 −1 src/cljs/cljs/polymorphic-apply.scm
View
@@ -576,7 +576,7 @@
fn-name-sym (gensym fname)
fn-vtable-name (symbol (str fname "---vtable"))
prim-types [:Number :Pair :Boolean :Nil :Null
- :Char :Array :Table :Symbol :Keyword :Procedure :String]
+ :Char :Array :Symbol :Keyword :Procedure :String]
prim-fnames (map #(symbol (str fname "---cljs_core$" (name %))) prim-types)
call-form (fn [p-fn]
(if variadic?
@@ -620,7 +620,7 @@
(map (fn [s] `(scm* {} ~s)) (remove #{'&} rst))))])
prim-types specialized-fns)))]
`(do
- ~@(map (fn [sf] `(def ~sf)) prim-fnames)
+ ~@(map (fn [sf] `(defn ~sf [& args] (throw (cljs.core/Error. (str "Protocol/Type pair: " (quote ~sf) " not defined."))))) prim-fnames)
(def ~fn-vtable-name {})
~(list 'scm*
{fn-name-sym fname
View
@@ -66,9 +66,15 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;; core protocols ;;;;;;;;;;;;;
(scm* {} (define cljs.core/protocol-impls (make-table)))
+;all proto defns need to be able to throw errors with str msgs- but errors themselves can implement protocols
+(scm* {} (define make-cljs.core/Error))
+(scm* {} (define cljs.core/str))
+
(defprotocol IFn
(-invoke [this args]))
+(scm* {} (include "polymorphic-apply.scm")) ;to trap non-proc exceptions and calls -invoke
+
(defprotocol ICounted
(-count [coll] "constant time count"))
@@ -364,6 +370,12 @@
(scm* [coll n] (list-ref coll n))
not-found))
+ Ifn
+ (-invoke [coll [k not-found]]
+ (if not-found
+ (-nth coll k not-found)
+ (-nth coll k)))
+
ISeqable
(-seq [coll] coll)
@@ -395,6 +407,28 @@
IHash
(-hash [o] (scm-equal?-hash o)))
+(extend-type Keyword
+ IEquiv
+ (-equiv [x o] (identical? x o))
+
+ IHash
+ (-hash [o] (scm-equal?-hash o))
+
+ IFn
+ (-invoke [k [coll not-found]]
+ (-lookup coll k not-found)))
+
+(extend-type Symbol
+ IEquiv
+ (-equiv [x o] (identical? x o))
+
+ IHash
+ (-hash [o] (scm-equal?-hash o))
+
+ IFn
+ (-invoke [k [coll not-found]]
+ (-lookup coll k not-found)))
+
(extend-type Procedure
IHash
(-hash [o] (scm-equal?-hash o)))
@@ -524,6 +558,10 @@ reduces them without incurring seq initialization"
([array k not-found]
(-nth array k not-found)))
+ IFn
+ (-invoke [coll [k not-found]]
+ (-lookup coll k not-found))
+
IReduce
(-reduce
([array f]
@@ -2487,7 +2525,7 @@ reduces them without incurring seq initialization"
ILookup
(-lookup
- ([table k] (scm* [table k] (table-ref table k)))
+ ([table k] (-lookup table k nil))
([table k not-found]
(scm* [table k not-found]
(with-exception-catcher
@@ -20,7 +20,7 @@
(define (polymorphic-raise-nonprocedure-operator-exception oper args code rte)
(##declare (not interrupts-enabled))
- (apply cljs.core/-invoke oper args))
+ (cljs.core/-invoke oper args))
;This seems to be necessary for interpreted code. Compiled code uses above procedures.
(let ((old-handler (current-exception-handler)))

0 comments on commit 239347e

Please sign in to comment.