Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#917 Instance method fn #940

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading