[#603] Partial support for multiple classes in reify
This commit is contained in:
parent
5ba2c34794
commit
200d444745
5 changed files with 52 additions and 24 deletions
3
deps.edn
3
deps.edn
|
|
@ -25,7 +25,8 @@
|
||||||
org.hsqldb/hsqldb {:mvn/version "2.4.0"}
|
org.hsqldb/hsqldb {:mvn/version "2.4.0"}
|
||||||
datascript/datascript {:mvn/version "0.18.11"}
|
datascript/datascript {:mvn/version "0.18.11"}
|
||||||
http-kit/http-kit {:mvn/version "2.5.0"}
|
http-kit/http-kit {:mvn/version "2.5.0"}
|
||||||
babashka/clojure-lanterna {:mvn/version "0.9.8-SNAPSHOT"}}
|
babashka/clojure-lanterna {:mvn/version "0.9.8-SNAPSHOT"}
|
||||||
|
org.clojure/math.combinatorics {:mvn/version "0.1.6"}}
|
||||||
:aliases {:main
|
:aliases {:main
|
||||||
{:main-opts ["-m" "babashka.main"]}
|
{:main-opts ["-m" "babashka.main"]}
|
||||||
:profile
|
:profile
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,8 @@
|
||||||
[org.clojure/tools.cli "1.0.194"]
|
[org.clojure/tools.cli "1.0.194"]
|
||||||
[cheshire "5.10.0"]
|
[cheshire "5.10.0"]
|
||||||
[nrepl/bencode "1.1.0"]
|
[nrepl/bencode "1.1.0"]
|
||||||
[borkdude/sci.impl.reflector "0.0.1-java11"]]
|
[borkdude/sci.impl.reflector "0.0.1-java11"]
|
||||||
|
[org.clojure/math.combinatorics "0.1.6"]]
|
||||||
:profiles {:feature/xml {:source-paths ["feature-xml"]
|
:profiles {:feature/xml {:source-paths ["feature-xml"]
|
||||||
:dependencies [[org.clojure/data.xml "0.2.0-alpha6"]]}
|
:dependencies [[org.clojure/data.xml "0.2.0-alpha6"]]}
|
||||||
:feature/yaml {:source-paths ["feature-yaml"]
|
:feature/yaml {:source-paths ["feature-yaml"]
|
||||||
|
|
|
||||||
2
sci
2
sci
|
|
@ -1 +1 @@
|
||||||
Subproject commit ea5a87b34570b7baab358368b192e7ecb764a242
|
Subproject commit 323a2574ec4d59a0544a829c1fa529fcbc110140
|
||||||
|
|
@ -1,23 +1,40 @@
|
||||||
(ns babashka.impl.reify
|
(ns babashka.impl.reify
|
||||||
{:no-doc true})
|
{:no-doc true}
|
||||||
|
(:require [clojure.math.combinatorics :as combo]))
|
||||||
|
|
||||||
|
(set! *warn-on-reflection* false)
|
||||||
|
|
||||||
|
(defmacro gen-reify-combos
|
||||||
|
"Generates pre-compiled reify combinations"
|
||||||
|
[methods]
|
||||||
|
(let [subsets (rest (combo/subsets (seq methods)))]
|
||||||
|
(reduce (fn [opts classes]
|
||||||
|
(assoc opts
|
||||||
|
(set (map (fn [[class _]]
|
||||||
|
(list 'quote class))
|
||||||
|
classes))
|
||||||
|
(list 'fn ['methods]
|
||||||
|
(list* 'reify
|
||||||
|
(mapcat
|
||||||
|
(fn [[clazz methods]]
|
||||||
|
(cons clazz
|
||||||
|
(map
|
||||||
|
(fn [[meth args]]
|
||||||
|
(list meth args
|
||||||
|
(list*
|
||||||
|
(list 'get-in 'methods
|
||||||
|
[(list 'quote clazz) (list 'quote meth)])
|
||||||
|
args)))
|
||||||
|
methods)))
|
||||||
|
classes)))))
|
||||||
|
{}
|
||||||
|
subsets)))
|
||||||
|
|
||||||
|
#_:clj-kondo/ignore
|
||||||
(def reify-opts
|
(def reify-opts
|
||||||
{'java.nio.file.FileVisitor
|
(gen-reify-combos
|
||||||
(fn [{:keys [:methods]}]
|
{java.nio.file.FileVisitor {preVisitDirectory [this p attrs]
|
||||||
{:obj (reify java.nio.file.FileVisitor
|
postVisitDirectory [this p attrs]
|
||||||
(preVisitDirectory [this p attrs]
|
visitFile [this p attrs]}
|
||||||
((get methods 'preVisitDirectory) this p attrs))
|
java.io.FileFilter {accept [this f]}
|
||||||
(postVisitDirectory [this p attrs]
|
java.io.FilenameFilter {accept [this f s]}}))
|
||||||
((get methods 'postVisitDirectory) this p attrs))
|
|
||||||
(visitFile [this p attrs]
|
|
||||||
((get methods 'visitFile) this p attrs)))})
|
|
||||||
'java.io.FileFilter
|
|
||||||
(fn [{:keys [:methods]}]
|
|
||||||
{:obj (reify java.io.FileFilter
|
|
||||||
(accept [this f]
|
|
||||||
((get methods 'accept) this f)))})
|
|
||||||
'java.io.FilenameFilter
|
|
||||||
(fn [{:keys [:methods]}]
|
|
||||||
{:obj (reify java.io.FilenameFilter
|
|
||||||
(accept [this f s]
|
|
||||||
((get methods 'accept) this f s)))})})
|
|
||||||
|
|
|
||||||
|
|
@ -363,7 +363,16 @@
|
||||||
(is (.exists f2))
|
(is (.exists f2))
|
||||||
(let [v (bb nil "-f" (.getPath (io/file "test-resources" "babashka" "glob.clj")))]
|
(let [v (bb nil "-f" (.getPath (io/file "test-resources" "babashka" "glob.clj")))]
|
||||||
(is (vector? v))
|
(is (vector? v))
|
||||||
(is (.exists (io/file (first v)))))))
|
(is (.exists (io/file (first v))))))
|
||||||
|
(testing "reify can handle multiple classes at once"
|
||||||
|
(is (true? (bb nil "
|
||||||
|
(def filter-obj (reify java.io.FileFilter
|
||||||
|
(accept [this f] (prn (.getPath f)) true)
|
||||||
|
java.io.FilenameFilter
|
||||||
|
(accept [this f name] (prn name) true)))
|
||||||
|
(def s1 (with-out-str (.listFiles (clojure.java.io/file \".\") filter-obj)))
|
||||||
|
(def s2 (with-out-str (.list (clojure.java.io/file \".\") filter-obj)))
|
||||||
|
(and (pos? (count s1)) (pos? (count s2)))")))))
|
||||||
|
|
||||||
(deftest future-print-test
|
(deftest future-print-test
|
||||||
(testing "the root binding of sci/*out*"
|
(testing "the root binding of sci/*out*"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue