Skip to content

Commit

Permalink
#917 Instance method fn
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude authored Oct 10, 2024
1 parent 67b2626 commit e1b133c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
29 changes: 19 additions & 10 deletions src/sci/impl/analyzer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1803,21 +1803,30 @@

#?(:clj
(defn analyze-interop-ifn [_ctx expr [^Class clazz meth]]
(let [stack (assoc (meta expr)
(let [meth (str meth)
stack (assoc (meta expr)
:ns @utils/current-ns
:file @utils/current-file)]
(if-let [_fld (try (Reflector/getStaticField ^Class clazz ^String (str meth))
(if-let [_fld (try (Reflector/getStaticField ^Class clazz ^String meth)
(catch IllegalArgumentException _
nil))]
(sci.impl.types/->Node
(interop/get-static-field clazz (str meth))
stack)
(sci.impl.types/->Node
(fn [& args]
(Reflector/invokeStaticMethod
(.getName clazz) (str meth)
^objects (into-array Object args)))
stack)))))
(interop/get-static-field clazz meth)
stack)
(if (str/starts-with? meth ".")
(let [meth (subs meth 1)]
(sci.impl.types/->Node
(fn [obj & args]
(Reflector/invokeInstanceMethod
obj meth
^objects (into-array Object args)))
stack))
(sci.impl.types/->Node
(fn [& args]
(Reflector/invokeStaticMethod
clazz meth
^objects (into-array Object args)))
stack))))))

#?(:clj
(comment
Expand Down
7 changes: 1 addition & 6 deletions src/sci/impl/resolve.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,8 @@
:cljs (.split (str sym-name) "."))]
{:sci.impl.analyzer/static-access true})
#?(:clj
;; TODO:
;; - [ ] if sym-name doesn't start with dot, it might be a static method which we need to turn into an IFn
;; - [ ] but if it's a field, we still need to do the field call
;; - [ ] if sym-name starts with dot, it's def. a method call which we need to create an IFn for
(with-meta
[clazz #?(:clj sym-name
:cljs (.split (str sym-name) "."))]
[clazz sym-name]
{:sci.impl.analyzer/interop-ifn true})
:cljs
(let [stack (assoc (meta sym)
Expand Down
3 changes: 2 additions & 1 deletion test/sci/interop_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@

#?(:clj
(deftest clojure-1_12-interop-test
(is (= [1 2 3] (eval* "(map Integer/parseInt [\"1\" \"2\" \"3\"])")))))
(is (= [1 2 3] (eval* "(map Integer/parseInt [\"1\" \"2\" \"3\"])")))
(is (= [1 2 3] (eval* "(map String/.length [\"1\" \"22\" \"333\"])")))))

(when-not tu/native?
(deftest exception-data
Expand Down

0 comments on commit e1b133c

Please sign in to comment.