Improve reify error messages (#1352)

Co-authored-by: Crispin Wellington <retrogradeorbit@gmail.com>
This commit is contained in:
Michiel Borkent 2022-09-06 10:24:15 +02:00 committed by GitHub
parent 3e2738201c
commit cd54d2a911
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 11 deletions

View file

@ -9,6 +9,13 @@ A preview of the next release can be installed from
Check out our new project: [bbin](https://github.com/babashka/bbin): install any Babashka script or project with one command. Check out our new project: [bbin](https://github.com/babashka/bbin): install any Babashka script or project with one command.
- Throw exception on attempt to reify multiple interfaces
- Allow java.lang.Object reify with empty methods
## 0.9.162 (2022-09-04)
Check out our new project: [bbin](https://github.com/babashka/bbin): install any Babashka script or project with one command.
- [#1343](https://github.com/babashka/babashka/issues/1343): Fix postgres feature - [#1343](https://github.com/babashka/babashka/issues/1343): Fix postgres feature
- [#1345](https://github.com/babashka/babashka/issues/1345): add `javax.net.ssl.SSLException` and `java.net.SocketTimeoutException` classes ([@lread](https://github.com/lread)) - [#1345](https://github.com/babashka/babashka/issues/1345): add `javax.net.ssl.SSLException` and `java.net.SocketTimeoutException` classes ([@lread](https://github.com/lread))
- Fix `satisfies?` with marker protocol (no methods) - Fix `satisfies?` with marker protocol (no methods)

View file

@ -16,7 +16,8 @@
"babashka.nrepl/src" "babashka.nrepl/src"
"depstar/src" "process/src" "depstar/src" "process/src"
"deps.clj/src" "deps.clj/resources" "deps.clj/src" "deps.clj/resources"
"resources" "sci/resources"], "resources" "sci/resources"
"reify/src"],
:deps {org.clojure/clojure {:mvn/version "1.11.1"}, :deps {org.clojure/clojure {:mvn/version "1.11.1"},
org.babashka/sci {:local/root "sci"} org.babashka/sci {:local/root "sci"}
org.babashka/babashka.impl.reify {:mvn/version "0.1.2"} org.babashka/babashka.impl.reify {:mvn/version "0.1.2"}

View file

@ -45,8 +45,26 @@
(invoke [this a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20] (invoke-fn this a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20)) (invoke [this a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20] (invoke-fn this a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20))
(applyTo [this arglist] (apply-fn this arglist))))) (applyTo [this arglist] (apply-fn this arglist)))))
(defn reify-object [m]
(let [methods (:methods m)
toString-fn (or (get methods 'toString)
(fn [this]
(str
(.getName (.getClass this))
"@"
(Integer/toHexString (.hashCode this)))))]
(reify
sci.impl.types.IReified
(getMethods [_] (:methods m))
(getInterfaces [_] (:interfaces m))
(getProtocols [_] (:protocols m))
java.lang.Object
(toString [this] (toString-fn this)))))
(defmacro gen-reify-fn [] (defmacro gen-reify-fn []
`(fn [~'m] `(fn [~'m]
(when (> (count (:interfaces ~'m)) 1)
(throw (UnsupportedOperationException. "babashka reify only supports implementing a single interface")))
(if (empty? (:interfaces ~'m)) (if (empty? (:interfaces ~'m))
(reify (reify
sci.impl.types.IReified sci.impl.types.IReified
@ -55,19 +73,12 @@
(getProtocols [_] (:protocols ~'m))) (getProtocols [_] (:protocols ~'m)))
(case (.getName ~(with-meta `(first (:interfaces ~'m)) (case (.getName ~(with-meta `(first (:interfaces ~'m))
{:tag 'Class})) {:tag 'Class}))
"java.lang.Object"
(reify
java.lang.Object
(toString [~'this]
((method-or-bust (:methods ~'m) (quote ~'toString)) ~'this))
sci.impl.types.IReified
(getMethods [_] (:methods ~'m))
(getInterfaces [_] (:interfaces ~'m))
(getProtocols [_] (:protocols ~'m)))
~@(mapcat identity ~@(mapcat identity
(cons (cons
["clojure.lang.IFn" ["clojure.lang.IFn"
`(reify-ifn ~'m)] `(reify-ifn ~'m)
"java.lang.Object"
`(reify-object ~'m)]
(for [i interfaces] (for [i interfaces]
(let [in (.getName ^Class i)] (let [in (.getName ^Class i)]
[in [in

View file

@ -51,6 +51,11 @@
]"))))) ]")))))
(deftest reify-object (deftest reify-object
(testing "empty methods"
(is (clojure.string/starts-with?
(bb nil "
(str (reify Object))")
"babashka.impl.reify")))
(testing "toString" (testing "toString"
(is (= ":foo" (is (= ":foo"
(bb nil " (bb nil "
@ -114,3 +119,12 @@
iter (reify java.util.function.Consumer (accept [_ x] (vswap! res conj x)))) iter (reify java.util.function.Consumer (accept [_ x] (vswap! res conj x))))
(= [true :a :b :c] @res))] (= [true :a :b :c] @res))]
(is (true? (bb nil prog))))) (is (true? (bb nil prog)))))
(deftest reify-multiple-interfaces-test
(testing "throws exception"
(is (thrown?
clojure.lang.ExceptionInfo
(bb nil "
(reify
java.lang.Object (toString [_] \"foo\")
clojure.lang.Seqable (seq [_] '(1 2 3)))")))))