Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#203] make sci optional for smaller JS builds #210

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/kaocha
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env bash
clojure -A:test -m kaocha.runner "$@"
clojure -A:test:sci -m kaocha.runner "$@"
6 changes: 3 additions & 3 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
com.clojure-goes-fast/clj-async-profiler {:mvn/version "0.4.1"}}
:jvm-opts ["-server"
"-Xmx4096m"
"-Dclojure.compiler.direct-linking=true"]}}
"-Dclojure.compiler.direct-linking=true"]}
:sci {:extra-deps {borkdude/sci {:git/url "https://github.com/borkdude/sci"
:sha "eb97e01a5913fb81859da814ba55a25b807dd6cc"}}}}
:deps {org.clojure/clojure {:mvn/version "1.10.1"}
borkdude/sci {:git/url "https://github.com/borkdude/sci"
:sha "eb97e01a5913fb81859da814ba55a25b807dd6cc"}
borkdude/edamame {:mvn/version "0.0.11-alpha.12"}
org.clojure/test.check {:mvn/version "1.0.0"}
com.gfredericks/test.chuck {:mvn/version "0.2.10"}}}
16 changes: 9 additions & 7 deletions src/malli/core.cljc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns malli.core
(:refer-clojure :exclude [eval type])
(:require [sci.core :as sci]
[malli.registry :as mr])
(:require [malli.registry :as mr]
#?(:clj [malli.impl.clj.dynaload :as dynaload]
:cljs [malli.impl.cljs.dynaload :as dynaload]))
#?(:clj (:import (java.util.regex Pattern))))

;;
Expand Down Expand Up @@ -909,11 +910,12 @@
(-map-entries schema)))))

(defn ^:no-doc eval [?code]
(if (fn? ?code) ?code (sci/eval-string (str ?code) {:preset :termination-safe
:bindings {'m/properties properties
'm/type type
'm/children children
'm/map-entries map-entries}})))
(if (fn? ?code) ?code (@dynaload/eval-string
(str ?code) {:preset :termination-safe
:bindings {'m/properties properties
'm/type type
'm/children children
'm/map-entries map-entries}})))
;;
;; Visitors
;;
Expand Down
33 changes: 33 additions & 0 deletions src/malli/impl/clj/dynaload.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(ns malli.impl.clj.dynaload
{:no-doc true})

(defonce ^:private dynalock (Object.))

(defmacro ^:private locking2
"Executes exprs in an implicit do, while holding the monitor of x.
Will release the monitor of x in all circumstances."
{:added "1.0"}
[x & body]
`(let [lockee# ~x]
(try
(let [locklocal# lockee#]
(monitor-enter locklocal#)
(try
~@body
(finally
(monitor-exit locklocal#)))))))

(defn dynaload
[s]
(delay
(let [ns (namespace s)]
(assert ns)
(locking2 dynalock
(require (symbol ns)))
(let [v (resolve s)]
(if v
@v
(throw (RuntimeException. (str "Var " s " is not on the classpath"))))))))

(def eval-string
(dynaload 'sci.core/eval-string))
12 changes: 12 additions & 0 deletions src/malli/impl/cljs/dynaload.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(ns malli.impl.cljs.dynaload)

(defmacro dynaload [[_quote s]]
`(LazyVar.
(fn []
(if (cljs.core/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))
17 changes: 17 additions & 0 deletions src/malli/impl/cljs/dynaload.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ns malli.impl.cljs.dynaload
{:no-doc true}
(:require-macros
[malli.impl.cljs.dynaload :refer [dynaload]]))

(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 eval-string
(dynaload 'sci.core/eval-string))
4 changes: 3 additions & 1 deletion test/malli/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
[malli.core :as m]
[malli.edn :as me]
[malli.transform :as mt]
[malli.util :as mu]))
[malli.util :as mu]
;; TODO: separate tests for sci
[sci.core]))

(defn with-schema-forms [result]
(some-> result
Expand Down