cloroutine icon indicating copy to clipboard operation
cloroutine copied to clipboard

java interop failure

Open dustingetz opened this issue 2 years ago • 2 comments

Leo said: "it may be a cloroutine bug, move .readLine in a wrapper function"

#?(:clj (defn readerReadLine [r] (.readLine r)))

#?(:clj
   (defn file-stream [uri]
     (m/ap
      (with-open [r (clojure.java.io/reader uri)]
        (loop []
          (m/amb
           (m/? (m/via m/blk (.readLine r))) #_(readerReadLine r) ; ------------ here
           (recur)))))))

As written, crashes with below error. Wrapping .readLine in a clojure fn resolves the issue.

#error {
 :cause "No matching field found: readLine for class java.io.BufferedReader"
 :via
 [{:type java.lang.IllegalArgumentException
   :message "No matching field found: readLine for class java.io.BufferedReader"
   :at [clojure.lang.Reflector invokeNoArgInstanceMember "Reflector.java" 434]}]

dustingetz avatar Dec 01 '23 18:12 dustingetz

Also encountering this, doing (clojure.walk/macroexpand-all '(cr {} (.take 'anything))) shows that it expands a method call into field retrieving. Moving interop into wrapper function works.

lotuc avatar Mar 11 '24 22:03 lotuc

(do (def arr (java.util.ArrayList.)) (cloroutine.impl.analyze-clj/analyze nil '(.size arr)) results in host-interop. impl.cljc#L251 emits a field access for that (which would be wrong).

Notice that host-interop mean "Node for a no-arg host interop call or for a host interop field access" (field access (cloroutine.impl.analyze-clj/analyze nil '(.-in System)) also results in host-interop).

Also notice (. System in), (. System -in), (do (def arr (java.util.ArrayList.)) (. arr size)) all works.

For host-interop just replace built field access with (:m-or-f ast) would work.

Here is the try https://github.com/leonoel/cloroutine/compare/master...lotuc:cloroutine:fix/host-interop

lotuc avatar Feb 14 '25 07:02 lotuc