athos / JiSE

JiSE: Java in S-Expression
Eclipse Public License 2.0
104 stars 2 forks source link

Getting Example to Work #6

Closed zcaudate closed 4 years ago

zcaudate commented 4 years ago

@athos: I'm looking to get jnr-ffi working with jise and am porting the Gettimeofday example here:

https://github.com/jnr/jnr-ffi-examples/blob/master/gettimeofday/src/main/java/gettimeofday/Gettimeofday.java

I'm having problems compiling the defclass form. Would you please take a look and let me know what I'm doing wrong.

https://gist.github.com/zcaudate/f05001f6cc38a8a02f9b0892bab02a82

I'm getting this error here, which I'm not sure is defclass or jnr related.

 Error: constructor jnr.ffi.Struct$time_t in class
   jnr.ffi.Struct$time_t cannot be applied to given types
   (/Users/chris/Development/caudata/statstrade/statstrade-server/src/statstrade/lib/curl.clj:55:26)
   {:column 26,
    :line 55,
    :alternatives
    ({:param-types
      [#object[clojure.asm.Type 0x29c9e0cb "Ljnr/ffi/Struct;"]],
      :access #{:public}}
     {:param-types
      [#object[clojure.asm.Type 0x23d3c400 "Ljnr/ffi/Struct;"]
       #object[clojure.asm.Type 0x7093025d "Ljnr/ffi/Struct$Offset;"]],
      :access #{:public}})}
athos commented 4 years ago

I looked into it and figured out how that error happened.

Struct.time_t is an inner class of the Struct class, and an inner class in general has an associated enclosing instance. When javac compiles an inner class C, it implicitly passes the enclosing instance to C's constructors as their first argument and maintains internally the relationships between the enclosing instance and each instance of C. JiSE does not support inner classes (or any other nested classes) at all right now, nor has such an internal mechanism.

To work around the issue, you'll need to feed the enclosing instance (i.e. this) on your own to the constructors of inner classes, as follows:

^:public
(defm Timeval [^jnr.ffi.Runtime rt]
  (super rt)
  (set! (.tv_sec this) (Struct$time_t. this))
  (set! (.tv_usec this) (Struct$SignedLong. this))))
zcaudate commented 4 years ago

Ah that makes sense. The example is working now.

Thanks!