Narrow scope of reify

This commit is contained in:
Michiel Borkent 2021-03-13 12:44:12 +01:00
parent f58748ebbe
commit 8551817724
2 changed files with 133 additions and 116 deletions

View file

@ -4,28 +4,42 @@
(set! *warn-on-reflection* false) (set! *warn-on-reflection* false)
;; Notes
;; We abandoned the 'one reify object that implements all interfaces' approach
;; due to false positives. E.g. when you would print a reified object, you would
;; get: 'Not implemented: seq', because print-method thought this object was
;; seqable, while in fact, it wasn't.
(defn method-or-bust [methods k]
(or (get methods k)
(throw (UnsupportedOperationException. "Method not implemented: " k))))
(defmacro gen-reify-combos (defmacro gen-reify-combos
"Generates pre-compiled reify combinations" "Generates pre-compiled reify combinations"
[methods] [methods]
(let [prelude ['reify (let [prelude '(reify
'sci.impl.types.IReified sci.impl.types.IReified
'(getInterfaces [this] (getInterfaces [this]
interfaces) interfaces)
'(getMethods [this] (getMethods [this]
methods) methods)
'(getProtocols [this] (getProtocols [this]
protocols) protocols))]
'java.lang.Object
'(toString [this]
(if-let [m (get methods 'toString)]
(m this)
(str (.. this getClass getName)
"@" (Integer/toHexString (.hashCode this)))))]]
(list 'fn [{:keys '[interfaces methods protocols]}] (list 'fn [{:keys '[interfaces methods protocols]}]
`(cond ~'(empty? interfaces) ~prelude
~'(> (count interfaces)
1)
(throw (new Exception "Babashka currently does not support reifying more than one interface."))
:else
(case (.getName ~(with-meta '(first interfaces)
{:tag 'Class}))
~@(mapcat
(fn [[clazz methods]]
(list
(str clazz)
(concat prelude (concat prelude
(mapcat (fn [[clazz methods]] (cons clazz
(cons
clazz
(mapcat (mapcat
(fn [[meth arities]] (fn [[meth arities]]
(map (map
@ -37,13 +51,22 @@
~(str meth))))) ~(str meth)))))
arity))) arity)))
arities)) arities))
methods))) methods)))))
methods))))) methods)))))
#_:clj-kondo/ignore ;; (require 'clojure.pprint)
;; (clojure.pprint/pprint
;; (macroexpand '(gen-reify-combos {java.nio.file.FileVisitor
;; {preVisitDirectory [[this p attrs]]
;; postVisitDirectory [[this p attrs]]
;; visitFile [[this p attrs]]}})))
#_:clj-kondo/ignore)
(def reify-fn (def reify-fn
(gen-reify-combos (gen-reify-combos
{java.nio.file.FileVisitor {java.lang.Object
{toString [[this]]}
java.nio.file.FileVisitor
{preVisitDirectory [[this p attrs]] {preVisitDirectory [[this p attrs]]
postVisitDirectory [[this p attrs]] postVisitDirectory [[this p attrs]]
visitFile [[this p attrs]]} visitFile [[this p attrs]]}

View file

@ -2,8 +2,7 @@
(:require (:require
[babashka.test-utils :as test-utils] [babashka.test-utils :as test-utils]
[clojure.edn :as edn] [clojure.edn :as edn]
[clojure.test :as test :refer [deftest is testing]] [clojure.test :as test :refer [deftest is testing]]))
[clojure.string :as str]))
(defn bb [input & args] (defn bb [input & args]
(edn/read-string (edn/read-string
@ -12,15 +11,15 @@
(apply test-utils/bb (when (some? input) (str input)) (map str args)))) (apply test-utils/bb (when (some? input) (str input)) (map str args))))
(deftest file-filter-test (deftest file-filter-test
(testing "reify can handle multiple classes at once"
(is (true? (bb nil " (is (true? (bb nil "
(def filter-obj (reify java.io.FileFilter (def filter-obj (reify java.io.FileFilter
(accept [this f] (prn (.getPath f)) true) (accept [this f] (prn (.getPath f)) true)))
java.io.FilenameFilter (def filename-filter-obj
(reify java.io.FilenameFilter
(accept [this f name] (prn name) true))) (accept [this f name] (prn name) true)))
(def s1 (with-out-str (.listFiles (clojure.java.io/file \".\") filter-obj))) (def s1 (with-out-str (.listFiles (clojure.java.io/file \".\") filter-obj)))
(def s2 (with-out-str (.list (clojure.java.io/file \".\") filter-obj))) (def s2 (with-out-str (.listFiles (clojure.java.io/file \".\") filename-filter-obj)))
(and (pos? (count s1)) (pos? (count s2)))"))))) (and (pos? (count s1)) (pos? (count s2)))"))))
(deftest reify-multiple-arities-test (deftest reify-multiple-arities-test
(testing "ILookup" (testing "ILookup"
@ -65,9 +64,4 @@
(def m (reify Object (def m (reify Object
(toString [_] (str :foo)))) (toString [_] (str :foo))))
(hash m) (hash m)
")))) ")))))
(testing "toString still works when not overriding it"
(is (bb nil "
(def m (reify Object))
(str m)
"))))