Expose with-open to CLJS.

This commit is contained in:
Nick Alexander 2016-07-11 21:57:20 -07:00
parent d5cfbeaa45
commit d42e2f02a6

View file

@ -3,6 +3,7 @@
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
(ns datomish.test-macros
(:refer-clojure :exclude [with-open])
(:require
[datomish.pair-chan]))
@ -35,3 +36,35 @@
~(with-meta name {:async true})
(let [[v# e#] (clojure.core.async/<!! (datomish.pair-chan/go-pair ~@body))]
(clojure.test/is (= e# nil))))))
;; CLJS doesn't expose `with-open`, for reasons unknown. Duplicate the definition of
;; `with-open` (and `assert-args`) here.
;; See https://github.com/clojure/clojure/blob/clojure-1.7.0/src/clj/clojure/core.clj#L3679-L3698.
(defmacro ^{:private true} assert-args
[& pairs]
`(do (when-not ~(first pairs)
(throw (IllegalArgumentException.
(str (first ~'&form) " requires " ~(second pairs) " in " ~'*ns* ":" (:line (meta ~'&form))))))
~(let [more (nnext pairs)]
(when more
(list* `assert-args more)))))
(defmacro with-open
"bindings => [name init ...]
Evaluates body in a try expression with names bound to the values
of the inits, and a finally clause that calls (.close name) on each
name in reverse order."
{:added "1.0"}
[bindings & body]
(assert-args
(vector? bindings) "a vector for its binding"
(even? (count bindings)) "an even number of forms in binding vector")
(cond
(= (count bindings) 0) `(do ~@body)
(symbol? (bindings 0)) `(let ~(subvec bindings 0 2)
(try
(with-open ~(subvec bindings 2) ~@body)
(finally
(. ~(bindings 0) close))))
:else (throw (java.lang.IllegalArgumentException.
"with-open only allows Symbols in bindings"))))