From e64dbc7691281abcdb8bbba074ab146be7d1ec73 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 28 Feb 2021 18:23:45 +0100 Subject: [PATCH] Support reifying clojure.lang.IFn and clojure.lang.ILookup (#740) --- sci | 2 +- src/babashka/impl/reify.clj | 56 ++++++++++++++++++++++++++++--------- test/babashka/main_test.clj | 30 ++++++++++++++++++++ 3 files changed, 74 insertions(+), 14 deletions(-) diff --git a/sci b/sci index 96a0c77d..d174e28f 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 96a0c77d75066746b91ab242dc144d04b10b399b +Subproject commit d174e28fcb05a53868404c7cd0efc43093b66442 diff --git a/src/babashka/impl/reify.clj b/src/babashka/impl/reify.clj index 11b9cb1f..57644f77 100644 --- a/src/babashka/impl/reify.clj +++ b/src/babashka/impl/reify.clj @@ -18,23 +18,53 @@ (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))) + (mapcat + (fn [[meth arities]] + (map + (fn [arity] + (list meth arity + (list* + (list 'get-in 'methods + [(list 'quote clazz) (list 'quote meth)]) + arity))) + arities)) + methods))) classes))))) {} subsets))) +#_(prn (macroexpand '(gen-reify-combos + {java.io.FileFilter {accept [[this f]]}}))) + #_:clj-kondo/ignore (def reify-opts (gen-reify-combos - {java.nio.file.FileVisitor {preVisitDirectory [this p attrs] - postVisitDirectory [this p attrs] - visitFile [this p attrs]} - java.io.FileFilter {accept [this f]} - java.io.FilenameFilter {accept [this f s]}})) + {java.nio.file.FileVisitor {preVisitDirectory [[this p attrs]] + postVisitDirectory [[this p attrs]] + visitFile [[this p attrs]]} + java.io.FileFilter {accept [[this f]]} + java.io.FilenameFilter {accept [[this f s]]} + clojure.lang.ILookup {valAt [[this k] [this k default]]} + clojure.lang.IFn {applyTo [[this arglist]] + invoke [[this] + [this a1] + [this a1 a2] + [this a1 a2 a3] + [this a1 a2 a3 a4] + [this a1 a2 a3 a4 a5] + [this a1 a2 a3 a4 a5 a6] + [this a1 a2 a3 a4 a5 a6 a7] + [this a1 a2 a3 a4 a5 a6 a7 a8] + [this a1 a2 a3 a4 a5 a6 a7 a8 a9] + [this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10] + [this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11] + [this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12] + [this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13] + [this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14] + [this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15] + [this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16] + [this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17] + [this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18] + [this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19] + [this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20] + [this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 varargs]]}})) diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index 7ec984c9..6b7b4e1a 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -576,6 +576,36 @@ (when test-utils/native? (is (bb nil "(defmethod print-method sci.lang.IVar [o w] (.write w (str :foo (symbol o)))) (def x 1) (= \":foouser/x\" (pr-str #'x))")))) +(deftest reify-multiple-arities-test + (testing "ILookup" + (is (= ["->:foo" 10] + (bb nil " +(def m (reify clojure.lang.ILookup + (valAt [this x] (str \"->\" x)) + (valAt [this x y] y))) +[(:foo m) (:foo m 10)]")))) + (testing "IFn" + (is (= [:yo :three :six :twelve :eighteen :nineteen 19] + (bb nil " +(def m (reify clojure.lang.IFn + (invoke [this] :yo) + (invoke [this _ _ _] :three) + (invoke [this _ _ _ _ _ _] :six) + (invoke [this _ _ _ _ _ _ _ _ _ _ _ _] :twelve) + (invoke [this _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _] :eighteen) + (invoke [this _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _] :nineteen) + (applyTo [this args] (last args)))) +[ +(m) +(m 1 2 3) +(m 1 2 3 4 5 6) +(m 1 2 3 4 5 6 1 2 3 4 5 6) +(m 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6) +(m 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1) +(apply m (range 20)) +]"))))) + + ;;;; Scratch (comment