Skip to content

Commit

Permalink
Use CLJS version with :js-op
Browse files Browse the repository at this point in the history
  • Loading branch information
frenchy64 committed Aug 10, 2013
1 parent 92472e8 commit 716dff1
Show file tree
Hide file tree
Showing 24 changed files with 733 additions and 143 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.clojure</groupId>
<artifactId>jvm.tools.analyzer</artifactId>
<version>0.4.3-SNAPSHOT</version>
<version>0.4.4-SNAPSHOT</version>
</dependency>
<!-- for algo.monads -->
<dependency>
Expand Down
19 changes: 15 additions & 4 deletions project.clj
Expand Up @@ -3,13 +3,14 @@
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}

:dependencies [[org.clojure/jvm.tools.analyzer "0.4.3-SNAPSHOT"
:exclusions [org.clojure/clojure]]
:dependencies [[org.clojure/jvm.tools.analyzer "0.4.4-SNAPSHOT"
:exclusions [org.clojure/clojure
org.clojure/clojurescript]]
[org.clojure/core.contracts "0.0.4"
:exclusions [org.clojure/clojure]]
[org.clojure/math.combinatorics "0.0.2"
:exclusions [org.clojure/clojure]]
[org.clojure/clojurescript "0.0-1806"]
;[org.clojure/clojurescript "0.0-1806"]
[org.clojure/tools.trace "0.7.5"
:exclusions [org.clojure/clojure]]
[org.clojure/clojure "1.5.1"]
Expand All @@ -20,14 +21,24 @@
[com.taoensso/timbre "2.1.2"]
[org.clojure/core.match "0.2.0-alpha12"]
[org.clojure/core.async "0.1.0-20130802.160123-63"]

; temporary deps for CLJS
[com.google.javascript/closure-compiler "v20130603"]
[org.clojure/google-closure-library "0.0-20130212-95c19e7f0f5f"]
[org.clojure/data.json "0.2.2"]
[org.mozilla/rhino "1.7R4"]
[org.clojure/tools.reader "0.7.5"]

]

:global-vars {*warn-on-reflection* true}

:repositories {"sonatype-oss-public" "https://oss.sonatype.org/content/groups/public/"}

:source-paths ["src/main/clojure"
"src/main/cljs"]
"src/main/cljs"
"../clojurescript/src/clj"
"../clojurescript/src/cljs"]
:test-paths ["src/test/clojure"
"src/test/cljs"]

Expand Down
6 changes: 6 additions & 0 deletions src/main/cljs/cljs/core/typed.cljs
Expand Up @@ -6,3 +6,9 @@
"Internal use only. Use ann-form."
[form ty]
form)

(defn ^:skip-wiki
ann-protocol*
"Internal use only. Use ann-protocol."
[vbnd varsym mth]
nil)
208 changes: 208 additions & 0 deletions src/main/cljs/cljs/core/typed/async.cljs
@@ -0,0 +1,208 @@
(ns
^{:doc
"This namespace contains annotations and helper macros for type
checking core.async code. Ensure clojure.core.async is require'd
before performing type checking.
go
use go>
chan
use chan>
buffer
use buffer> (similar for other buffer constructors)
"}
cljs.core.typed.async
(:require-macros [cljs.core.typed :refer [ann ann-pdatatype def-alias ann-protocol inst
AnyInteger tc-ignore Seqable]
:as t]))

;TODO how do we encode that nil is illegal to provide to Ports/Channels?
; Is it essential?

;;;;;;;;;;;;;;;;;;;;
;; Protocols

(ann-protocol [[w :variance :contravariant]
[r :variance :covariant]]
clojure.core.async.impl.protocols/Channel)

(ann-protocol [[r :variance :covariant]]
clojure.core.async.impl.protocols/ReadPort)

(ann-protocol [[w :variance :contravariant]]
clojure.core.async.impl.protocols/WritePort)

(ann-protocol [[x :variance :invariant]]
clojure.core.async.impl.protocols/Buffer)

;(ann-pdatatype clojure.core.async.impl.channels.ManyToManyChannel
; [[w :covariant]
; [r :contravariant]]
; []
; :unchecked-ancestors #{(clojure.core.async.impl.protocols/Channel w r)
; (clojure.core.async.impl.protocols/ReadPort r)
; (clojure.core.async.impl.protocols/WritePort w)})
;
;;;;;;;;;;;;;;;;;;;;;
;;; Aliases
;
;(def-alias ReadOnlyChan
; "A core.async channel that statically disallows writes."
; (TFn [[r :variance :covariant]]
; (Extends [(clojure.core.async.impl.protocols/WritePort Nothing)
; (clojure.core.async.impl.protocols/ReadPort r)
; (clojure.core.async.impl.protocols/Channel Nothing r)])))
;
;(def-alias Chan
; "A core.async channel"
; (TFn [[x :variance :invariant]]
; (Extends [(clojure.core.async.impl.protocols/WritePort x)
; (clojure.core.async.impl.protocols/ReadPort x)
; (clojure.core.async.impl.protocols/Channel x x)])))
;
;(def-alias TimeoutChan
; "A timeout channel"
; (Chan Any))
;
;(def-alias Buffer
; "A buffer of type x."
; (TFn [[x :variance :invariant]]
; (clojure.core.async.impl.protocols/Buffer x)))
;
;(def-alias ReadOnlyPort
; "A read-only port that can read type x"
; (TFn [[r :variance :covariant]]
; (Extends [(clojure.core.async.impl.protocols/ReadPort r)
; (clojure.core.async.impl.protocols/WritePort Nothing)])))
;
;(def-alias WriteOnlyPort
; "A write-only port that can write type x"
; (TFn [[x :variance :invariant]]
; (Extends [(clojure.core.async.impl.protocols/ReadPort x)
; (clojure.core.async.impl.protocols/WritePort x)])))
;
;(def-alias Port
; "A port that can read and write type x"
; (TFn [[x :variance :invariant]]
; (Extends [(clojure.core.async.impl.protocols/ReadPort x)
; (clojure.core.async.impl.protocols/WritePort x)])))
;
;;;;;;;;;;;;;;;;;;;;;
;;; Var annotations
;
;(ann ^:no-check clojure.core.async/buffer (All [x] [AnyInteger -> (Buffer x)]))
;(ann ^:no-check clojure.core.async/dropping-buffer (All [x] [AnyInteger -> (Buffer x)]))
;(ann ^:no-check clojure.core.async/sliding-buffer (All [x] [AnyInteger -> (Buffer x)]))
;
;(ann ^:no-check clojure.core.async/thread-call (All [x] [[-> x] -> (Chan x)]))
;
;(ann ^:no-check clojure.core.async/timeout [AnyInteger -> TimeoutChan])
;
;(ann ^:no-check clojure.core.async/chan (All [x]
; (Fn [-> (Chan x)]
; [(U (Buffer x) AnyInteger) -> (Chan x)])))
;;(ann clojure.core.async/>! (All [x] [(Chan x) -> (Chan x)]))
;
;(ann ^:no-check clojure.core.async.impl.ioc-macros/aget-object [AtomicReferenceArray AnyInteger -> Any])
;(ann ^:no-check clojure.core.async.impl.ioc-macros/aset-object [AtomicReferenceArray Any -> nil])
;(ann ^:no-check clojure.core.async.impl.ioc-macros/run-state-machine [AtomicReferenceArray -> Any])
;
;;FIXME what is 2nd arg?
;(ann ^:no-check clojure.core.async.impl.ioc-macros/put! (All [x] [AnyInteger Any (Chan x) x -> Any]))
;(ann ^:no-check clojure.core.async.impl.ioc-macros/return-chan (All [x] [AtomicReferenceArray x -> (Chan x)]))
;
;(ann ^:no-check clojure.core.async/<!! (All [x] [(ReadOnlyPort x) -> (U nil x)]))
;(ann ^:no-check clojure.core.async/>!! (All [x] [(WriteOnlyPort x) x -> nil]))
;(ann ^:no-check clojure.core.async/alts!!
; (All [x d]
; (Fn [(Seqable (U (Port x) '[(Port x) x])) (Seqable (Port x)) & :mandatory {:default d} :optional {:priority (U nil true)} ->
; (U '[d ':default] '[x (Port x)])]
; [(Seqable (U (Port x) '[(Port x) x])) & :optional {:priority (U nil true)} -> '[x (Port x)]])))
;
;(ann ^:no-check clojure.core.async/close! [(ReadOnlyChan Any) -> nil])
;
;(ann ^:no-check clojure.core.async.impl.dispatch/run [[-> (ReadOnlyChan Any)] -> Executor])
;;(ann clojure.core.async.impl.ioc-macros/async-chan-wrapper kV
;
;;;;;;;;;;;;;;;;;;;;;
;;; Typed wrappers
;
;(t/tc-ignore
;(defn ^:private v [vsym]
; {:pre [(symbol? vsym)
; (namespace vsym)]}
; (let [ns (find-ns (symbol (namespace vsym)))
; _ (assert ns (str "Cannot find namespace: " (namespace vsym)))
; var (ns-resolve ns (symbol (name vsym)))]
; (assert (var? var) (str "Cannot find var: " vsym))
; @var))
; )
;
;(defmacro go>
; "Asynchronously executes the body, returning immediately to the
; calling thread. Additionally, any visible calls to <!, >! and alt!/alts!
; channel operations within the body will block (if necessary) by
; 'parking' the calling thread rather than tying up an OS thread (or
; the only JS thread when in ClojureScript). Upon completion of the
; operation, the body will be resumed.
;
; Returns a channel which will receive the result of the body when
; completed"
; [& body]
; `(let [c# (chan> ~'Any 1)
; captured-bindings# (clojure.lang.Var/getThreadBindingFrame)]
; (tc-ignore
; (clojure.core.async.impl.dispatch/run
; (fn []
; (let [f# ~((v 'clojure.core.async.impl.ioc-macros/state-machine)
; body 1 &env (v 'clojure.core.async.impl.ioc-macros/async-custom-terminators))
; state# (-> (f#)
; (clojure.core.async.impl.ioc-macros/aset-all!
; clojure.core.async.impl.ioc-macros/USER-START-IDX c#
; clojure.core.async.impl.ioc-macros/BINDINGS-IDX captured-bindings#))]
; (clojure.core.async.impl.ioc-macros/run-state-machine state#)))))
; c#))
;
;
;(defmacro chan>
; "A statically typed core.async channel.
;
; (chan> t ...) creates a buffer that can read and write type t.
; Subsequent arguments are passed directly to clojure.core.async/chan.
;
; Note:
; (chan> t ...) is the same as ((inst chan t) ...)"
; [t & args]
; `((inst clojure.core.async/chan ~t) ~@args))
;
;(defmacro buffer>
; "A statically typed core.async buffer.
;
; (buffer> t ...) creates a buffer that can read and write type t.
; Subsequent arguments are passed directly to clojure.core.async/buffer.
;
; Note: (buffer> t ...) is the same as ((inst buffer t) ...)"
; [t & args]
; `((inst clojure.core.async/buffer ~t) ~@args))
;
;(defmacro sliding-buffer>
; "A statically typed core.async sliding buffer.
;
; (sliding-buffer> t ...) creates a sliding buffer that can read and write type t.
; Subsequent arguments are passed directly to clojure.core.async/sliding-buffer.
;
; Note: (sliding-buffer> t ...) is the same as ((inst sliding-buffer t) ...)"
; [t & args]
; `((inst clojure.core.async/sliding-buffer ~t) ~@args))
;
;(defmacro dropping-buffer>
; "A statically typed core.async dropping buffer.
;
; (dropping-buffer> t ...) creates a dropping buffer that can read and write type t.
; Subsequent arguments are passed directly to clojure.core.async/dropping-buffer.
;
; Note: (dropping-buffer> t ...) is the same as ((inst dropping-buffer t) ...)"
; [t & args]
; `((inst clojure.core.async/dropping-buffer ~t) ~@args))
35 changes: 33 additions & 2 deletions src/main/clojure/cljs/core/typed.clj
Expand Up @@ -28,6 +28,37 @@
[varsym typesyn]
`(ann* '~varsym '~typesyn))

(defmacro
^{:forms '[(ann-protocol vbnd varsym & methods)
(ann-protocol varsym & methods)]}
ann-protocol
"Annotate a possibly polymorphic protocol var with method types.
eg. (ann-protocol IFoo
bar
[IFoo -> Any]
baz
[IFoo -> Number])
; polymorphic
(ann-protocol [[x :variance :covariant]]
IFoo
bar
[IFoo -> Any]
baz
[IFoo -> Number])"
[& args]
(let [bnd-provided? (vector? (first args))
vbnd (when bnd-provided?
(first args))
varsym (if bnd-provided?
(second args)
(first args))
{:as mth} (if bnd-provided?
(next (next args))
(next args))]
`(ann-protocol* '~vbnd '~varsym '~mth)))

(def ^:dynamic *currently-checking-cljs* nil)
(def ^{:doc "Internal use only"} ^:skip-wiki ^:dynamic *delayed-errors*)
(def ^:dynamic *already-collected*)
Expand Down Expand Up @@ -56,8 +87,8 @@
;check
c-ast ((v 'clojure.core.typed.check-cljs/check) ast
(when expected-provided?
(v 'clojure.core.typed.type-rep/ret
(v 'clojure.core.typed.parse-type/parse-type expected))))]
((v 'clojure.core.typed.type-rep/ret)
((v 'clojure.core.typed.parse-unparse/parse-type) expected))))]
;handle errors
(if-let [errors (seq @*delayed-errors*)]
(t/print-errors! errors)
Expand Down
31 changes: 26 additions & 5 deletions src/main/clojure/clojure/core/typed.clj
Expand Up @@ -910,19 +910,39 @@
(defn ^:skip-wiki
ann-protocol*
"Internal use only. Use ann-protocol."
[varsym mth]
[vbnd varsym mth]
nil)

(defmacro ann-protocol
"Annotate protocol var with method types.
(defmacro
^{:forms '[(ann-protocol vbnd varsym & methods)
(ann-protocol varsym & methods)]}
ann-protocol
"Annotate a possibly polymorphic protocol var with method types.
eg. (ann-protocol IFoo
bar
[IFoo -> Any]
baz
[IFoo -> Number])
; polymorphic
(ann-protocol [[x :variance :covariant]]
IFoo
bar
[IFoo -> Any]
baz
[IFoo -> Number])"
[varsym & {:as mth}]
`(ann-protocol* '~varsym '~mth))
[& args]
(let [bnd-provided? (vector? (first args))
vbnd (when bnd-provided?
(first args))
varsym (if bnd-provided?
(second args)
(first args))
{:as mth} (if bnd-provided?
(next (next args))
(next args))]
`(ann-protocol* '~vbnd '~varsym '~mth)))

(defn ^:skip-wiki
ann-pprotocol*
Expand All @@ -933,6 +953,7 @@
(defmacro ann-pprotocol
"Annotate polymorphic protocol with a polymorphic binder and method types."
[varsym vbnd & {:as mth}]
(prn "UNSUPPPORTED OPERATION: ann-pprotocol, use ann-protocol with binder as first argument, ie. before protocol name")
`(ann-pprotocol* '~varsym '~vbnd '~mth))

(defn ^:skip-wiki
Expand Down

0 comments on commit 716dff1

Please sign in to comment.