Skip to content

Commit

Permalink
small optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Oct 11, 2024
1 parent e147737 commit 9aebc2b
Showing 1 changed file with 22 additions and 31 deletions.
53 changes: 22 additions & 31 deletions src/sci/impl/analyzer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1487,15 +1487,9 @@
ns (analyze-ns-form ctx expr)
lazy-seq (analyze-lazy-seq ctx expr)))

#?(:clj
(defn analyze-instance-method-interop [_ctx _expr ^Class clazz ^String meth]
(fn [obj & args]
(Reflector/invokeInstanceMethodOfClass
obj clazz meth
^objects (into-array Object args)))))

#?(:clj
(defn analyze-interop [ctx expr [^Class clazz meth]]
(defn analyze-interop [_ctx expr [^Class clazz meth]]
(let [meth (str meth)
stack (assoc (meta expr)
:ns @utils/current-ns
Expand All @@ -1508,7 +1502,10 @@
stack)
(if (str/starts-with? meth ".")
(let [meth (subs meth 1)
f (analyze-instance-method-interop ctx expr clazz meth)]
f (fn [obj & args]
(Reflector/invokeInstanceMethodOfClass
obj clazz meth
^objects (into-array Object args)))]
(sci.impl.types/->Node
f
stack))
Expand Down Expand Up @@ -1590,18 +1587,23 @@
(interop/invoke-static-method ctx bindings class method children))
nil)))))
(and f-meta (:sci.impl.analyzer/interop f-meta))
(let [f (analyze-instance-method-interop nil expr (first f)
(-> (second f)
str
(subs 1)))]
(return-call ctx
expr
f (analyze-children ctx (rest expr))
(assoc m
:ns @utils/current-ns
:file @utils/current-file
:sci.impl/f-meta f-meta)
nil))
(let [[obj & children] (analyze-children ctx (rest expr))
meth (-> (second f)
str
(subs 1))
clazz (first f)
children (into-array children)
child-count (count children)
stack (assoc m
:ns @utils/current-ns
:file @utils/current-file
:sci.impl/f-meta f-meta)]
(sci.impl.types/->Node
(let [obj (sci.impl.types/eval obj ctx bindings)]
(interop/invoke-instance-method ctx bindings obj clazz
meth
children child-count))
stack))
(and f-meta (:sci.impl.analyzer/invoke-constructor f-meta))
(invoke-constructor-node ctx (first f) (rest expr))
(and (not eval?) ;; the symbol is not a binding
Expand Down Expand Up @@ -1853,17 +1855,6 @@
arr)
nil))))))

#?(:clj
(comment
(def meths (.getMethods Integer))
(def with-name (filter (fn [^java.lang.reflect.Method m]
(= "parseInt" (.getName m)))
meths))
(def arities (map (fn [^java.lang.reflect.Method m]
(count (.getParameterTypes m)))
with-name))
))

;; This could be a protocol, but there's not a clear win in doing so:
;; https://github.com/babashka/sci/issues/848
(defn analyze
Expand Down

0 comments on commit 9aebc2b

Please sign in to comment.