Skip to content

Commit 8b3ce24

Browse files
swannodettedfrese
andauthored
Fix apply of IFn for more than 20 arguments. (#167)
Co-authored-by: David Frese <[email protected]>
1 parent ba26724 commit 8b3ce24

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/main/clojure/cljs/core.cljc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,15 +1499,20 @@
14991499
(core/defn- add-ifn-methods [type type-sym [f & meths :as form]]
15001500
(core/let [meths (map #(adapt-ifn-params type %) meths)
15011501
this-sym (with-meta 'self__ {:tag type})
1502-
argsym (gensym "args")]
1502+
argsym (gensym "args")
1503+
max-ifn-arity 20]
15031504
(concat
15041505
[`(set! ~(extend-prefix type-sym 'call) ~(with-meta `(fn ~@meths) (meta form)))
15051506
`(set! ~(extend-prefix type-sym 'apply)
15061507
~(with-meta
15071508
`(fn ~[this-sym argsym]
15081509
(this-as ~this-sym
15091510
(.apply (.-call ~this-sym) ~this-sym
1510-
(.concat (array ~this-sym) (cljs.core/aclone ~argsym)))))
1511+
(.concat (array ~this-sym)
1512+
(if (> (.-length ~argsym) ~max-ifn-arity)
1513+
(doto (.slice ~argsym 0 ~max-ifn-arity)
1514+
(.push (.slice ~argsym ~max-ifn-arity)))
1515+
~argsym)))))
15111516
(meta form)))]
15121517
(ifn-invoke-methods type type-sym form))))
15131518

src/test/cljs/cljs/apply_test.cljs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,11 @@
4242
(is (= '(1 2 3 4)) (apply meta-f 1 2 3 4 []))
4343
(is (= '(1 2 3 4 5)) (apply meta-f 1 2 3 4 5 []))
4444
(is (= (range 1 8)) (apply meta-f 1 2 3 4 5 [6 7]))
45-
;; Currently: 20 is not seqable :(
46-
#_(is (= (range 21) (apply meta-f (range 21)))
45+
(is (= (range 21) (apply meta-f (range 21)))
4746
"Should properly call the last IFn arity with 20 args with last being a seq")
48-
;; Currently: Tries to call arity 21. Fault at .apply of the deftype proto
49-
;; Though, it could probably also be caught right by apply
50-
#_(is (= (range 22) (apply meta-f (range 22)))
47+
(is (= (range 22) (apply meta-f (range 22)))
5148
"Should properly call the last IFn arity with 20 args with last being a seq")
52-
#_(is (= (range 22) (.apply meta-f nil (to-array (range 22))))
49+
(is (= (range 22) (.apply meta-f nil (to-array (range 22))))
5350
".apply should also handle >20 arguments"))
5451

5552
(deftest multi-arity-test

0 commit comments

Comments
 (0)