Skip to content

Commit 6b47905

Browse files
committed
[metosin#203] make sci optional for smaller JS builds
1 parent a4b3ccd commit 6b47905

File tree

7 files changed

+78
-12
lines changed

7 files changed

+78
-12
lines changed

bin/kaocha

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env bash
2-
clojure -A:test -m kaocha.runner "$@"
2+
clojure -A:test:sci -m kaocha.runner "$@"

deps.edn

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
com.clojure-goes-fast/clj-async-profiler {:mvn/version "0.4.1"}}
2828
:jvm-opts ["-server"
2929
"-Xmx4096m"
30-
"-Dclojure.compiler.direct-linking=true"]}}
30+
"-Dclojure.compiler.direct-linking=true"]}
31+
:sci {:extra-deps {borkdude/sci {:git/url "https://github.com/borkdude/sci"
32+
:sha "eb97e01a5913fb81859da814ba55a25b807dd6cc"}}}}
3133
:deps {org.clojure/clojure {:mvn/version "1.10.1"}
32-
borkdude/sci {:git/url "https://github.com/borkdude/sci"
33-
:sha "eb97e01a5913fb81859da814ba55a25b807dd6cc"}
3434
borkdude/edamame {:mvn/version "0.0.11-alpha.12"}
3535
org.clojure/test.check {:mvn/version "1.0.0"}
3636
com.gfredericks/test.chuck {:mvn/version "0.2.10"}}}

src/malli/core.cljc

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
(ns malli.core
22
(:refer-clojure :exclude [eval type])
3-
(:require [sci.core :as sci]
4-
[malli.registry :as mr])
3+
(:require [malli.registry :as mr]
4+
#?(:clj [malli.impl.clj.dynaload :as dynaload]
5+
:cljs [malli.impl.cljs.dynaload :as dynaload]))
56
#?(:clj (:import (java.util.regex Pattern))))
67

78
;;
@@ -909,11 +910,12 @@
909910
(-map-entries schema)))))
910911

911912
(defn ^:no-doc eval [?code]
912-
(if (fn? ?code) ?code (sci/eval-string (str ?code) {:preset :termination-safe
913-
:bindings {'m/properties properties
914-
'm/type type
915-
'm/children children
916-
'm/map-entries map-entries}})))
913+
(if (fn? ?code) ?code (@dynaload/eval-string
914+
(str ?code) {:preset :termination-safe
915+
:bindings {'m/properties properties
916+
'm/type type
917+
'm/children children
918+
'm/map-entries map-entries}})))
917919
;;
918920
;; Visitors
919921
;;

src/malli/impl/clj/dynaload.clj

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
(ns malli.impl.clj.dynaload
2+
{:no-doc true})
3+
4+
(defonce ^:private dynalock (Object.))
5+
6+
(defmacro ^:private locking2
7+
"Executes exprs in an implicit do, while holding the monitor of x.
8+
Will release the monitor of x in all circumstances."
9+
{:added "1.0"}
10+
[x & body]
11+
`(let [lockee# ~x]
12+
(try
13+
(let [locklocal# lockee#]
14+
(monitor-enter locklocal#)
15+
(try
16+
~@body
17+
(finally
18+
(monitor-exit locklocal#)))))))
19+
20+
(defn dynaload
21+
[s]
22+
(delay
23+
(let [ns (namespace s)]
24+
(assert ns)
25+
(locking2 dynalock
26+
(require (symbol ns)))
27+
(let [v (resolve s)]
28+
(if v
29+
@v
30+
(throw (RuntimeException. (str "Var " s " is not on the classpath"))))))))
31+
32+
(def eval-string
33+
(dynaload 'sci.core/eval-string))

src/malli/impl/cljs/dynaload.cljc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(ns malli.impl.cljs.dynaload)
2+
3+
(defmacro dynaload [[_quote s]]
4+
`(LazyVar.
5+
(fn []
6+
(if (cljs.core/exists? ~s)
7+
~(vary-meta s assoc :cljs.analyzer/no-resolve true)
8+
(throw
9+
(js/Error.
10+
(str "Var " '~s " does not exist, "
11+
(namespace '~s) " never required")))))
12+
nil))

src/malli/impl/cljs/dynaload.cljs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
(ns malli.impl.cljs.dynaload
2+
{:no-doc true}
3+
(:require-macros
4+
[malli.impl.cljs.dynaload :refer [dynaload]]))
5+
6+
(deftype LazyVar [f ^:mutable cached]
7+
IDeref
8+
(-deref [this]
9+
(if-not (nil? cached)
10+
cached
11+
(let [x (f)]
12+
(when-not (nil? x)
13+
(set! cached x))
14+
x))))
15+
16+
(def eval-string
17+
(dynaload 'sci.core/eval-string))

test/malli/core_test.cljc

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
[malli.core :as m]
44
[malli.edn :as me]
55
[malli.transform :as mt]
6-
[malli.util :as mu]))
6+
[malli.util :as mu]
7+
;; TODO: separate tests for sci
8+
[sci.core]))
79

810
(defn with-schema-forms [result]
911
(some-> result

0 commit comments

Comments
 (0)