Skip to content

Commit

Permalink
make dynaload more ClojureScript friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
swannodette committed Jun 1, 2016
1 parent c0b1142 commit f75687f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
27 changes: 20 additions & 7 deletions src/main/cljs/cljs/spec/impl/gen.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,26 @@

(ns cljs.spec.impl.gen
(:refer-clojure :exclude [delay])
(:require [cljs.core :as c]))
(:require [cljs.core :as c]
[clojure.string :as string]))

(defmacro dynaload [[quote s]]
`(if (c/exists? ~s)
~(vary-meta s assoc :cljs.analyzer/no-resolve true)
(fn [& ~'args]
(throw (js/Error. (str "Var " '~s " is not on the classpath"))))))
(let [xs (string/split (namespace s) #"\.")
cnt (count xs)
checks (map
(fn [n xs]
`(c/exists? ~(symbol (string/join "." (take n xs)))))
(range 2 cnt)
(repeat xs))]
`(cljs.spec.impl.gen/LazyVar.
(fn []
(if (and ~@checks (c/exists? ~s))
~(vary-meta s assoc :cljs.analyzer/no-resolve true)
(throw
(js/Error.
(str "Var " '~s " does not exist, "
(namespace '~s) " never required")))))
nil)))

(defmacro delay
"given body that returns a generator, returns a
Expand All @@ -28,7 +41,7 @@
[s]
(let [fqn (symbol "clojure.test.check.generators" (name s))
doc (str "Lazy loaded version of " fqn)]
`(let [g# (c/delay (dynaload '~fqn))]
`(let [g# (dynaload '~fqn)]
(defn ~s
~doc
[& ~'args]
Expand All @@ -47,7 +60,7 @@
[s]
(let [fqn (symbol "clojure.test.check.generators" (name s))
doc (str "Fn returning " fqn)]
`(let [g# (c/delay (dynaload '~fqn))]
`(let [g# (dynaload '~fqn)]
(defn ~s
~doc
[& ~'args]
Expand Down
20 changes: 15 additions & 5 deletions src/main/cljs/cljs/spec/impl/gen.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,34 @@
[cljs.spec.impl.gen :as gen :refer [dynaload lazy-combinators lazy-prims]])
(:require [cljs.core :as c]))

(deftype LazyVar [f ^:mutable cached]
IDeref
(-deref [this]
(if-not (nil? cached)
cached
(let [x (f)]
(when-not (nil? x)
(set! cached x))
x))))

(def ^:private quick-check-ref
(c/delay (dynaload 'clojure.test.check/quick-check)))
(dynaload 'clojure.test.check/quick-check))

(defn quick-check
[& args]
(apply @quick-check-ref args))

(def ^:private for-all*-ref
(c/delay (dynaload 'clojure.test.check.properties/for-all*)))
(dynaload 'clojure.test.check.properties/for-all*))

(defn for-all*
"Dynamically loaded clojure.test.check.properties/for-all*."
[& args]
(apply @for-all*-ref args))

(let [g? (c/delay (dynaload 'clojure.test.check.generators/generator?))
g (c/delay (dynaload 'clojure.test.check.generators/generate))
mkg (c/delay (dynaload 'clojure.test.check.generators/->Generator))]
(let [g? (dynaload 'clojure.test.check.generators/generator?)
g (dynaload 'clojure.test.check.generators/generate)
mkg (dynaload 'clojure.test.check.generators/->Generator)]
(defn- generator?
[x]
(@g? x))
Expand Down

0 comments on commit f75687f

Please sign in to comment.