Closed joinr closed 3 years ago
Hi! Thanks for the question. Unlike Java, method descriptors (via :desc
) specify the return value as the last element. Your method descriptor vectors are missing the return value:
(require '[insn.core :as core])
(def a (into-array Character/TYPE "foo"))
(core/new-instance
{:name "test.Klass"
:flags #{:public}
:methods [{:flags #{:public :static}, :name "asetLiteral", :desc [chars :int :char :char] ;; <-- here
:emit [[:aload 0]
[:iload 1]
[:iload 2]
[:castore]
[:iload 2]
[:ireturn]]}]})
[(test.Klass/asetLiteral a 0 \b)
(seq a)]
;; => [\b (\b \o \o)]
Awesome! thanks
Thanks for this library, it's really opened up my eyes toward custom jvm bytecode magic.
I'm just playing around with some simple class definitions. One I'd like is to work with a primitive array, and int index, and a primitive value, and update the array at index with the value (basically a clone of clojure.core/aset without any int cast).
If I write a minimal java class, I get this:
Then I get a complaint from the classloader regarding incompatibility of types:
If I try to dumb it down to the simplest possible return, a method that takes an int array, an int, and an int, and returns the last arg (int):
Am I missing something in translating the bytecode to insn? It seems straightforward enough...
Platform:
Windows 10, openjdk version "1.8.0_222" OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_222-b10) OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.222-b10, mixed mode)
Dependencies: (the insn and asm stuff was cloned from tech.datatype, so I may be out of date!) [[org.clojure/clojure "1.10.1"] [com.clojure-goes-fast/clj-java-decompiler "0.3.0"] [insn "0.4.0" :exclusions [org.ow2.asm/asm]] [org.ow2.asm/asm "7.1"]]