coerce a number to a short

somewhat-functional-programmer somewhat-functional-programmer at protonmail.com
Wed Nov 27 04:53:27 UTC 2019


Here's a workaround in the meantime (though it's more verbose)... using java.lang.Short.reverseBytes(short s) as the test case[1]:

;; no such method failure
(jstatic "reverseBytes" "java.lang.Short" #x7f)

;; this works
(jstatic
 (jmethod (jclass "java.lang.Short") "reverseBytes" (jclass "short"))
 (jclass "java.lang.Short")
 #x7f)


This is also true with instance methods...

(require :abcl-contrib)
(require :jss)

(defvar *bb* (#"allocate" 'java.nio.ByteBuffer 1024))

;; no such method failure
(jcall "putShort" *bb* 20)

;; works
(jcall
 (jmethod (jclass "java.nio.ByteBuffer") "putShort" (jclass "short"))
 *bb*
 20)

;; works (not a short though)
(jcall "putInt" *bb* 20)


Hope this helps!  I hope to look into this in more detail (potential problem / enhancement opportunity lies in org.armedbear.lisp.Java's method resolution)... a change here would also let the jss syntax also work:
(#"putShort" *bb* 20)


[1] https://docs.oracle.com/javase/7/docs/api/java/lang/Short.html#reverseBytes(short)


‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Wednesday, November 27, 2019 2:28 AM, Brad Knotwell <bknotwell at yahoo.com> wrote:

> I'm pretty sure you're right.  I have a MWE below that shows an unintuitive difference.
>
> Thx.
>
> --Brad
>
> (defun void-function ()
>
>   (let* ((result (jstatic "square" "Main" 32)))
>
>     (format t "in void-function, result of calling square(32): ~a~%" result)))
>
> (defun void-function-short ()
>
>   (let* ((result (jstatic "squareshort" "Main" 16)))
>
>     (format t "in void-function, result of calling squareshort(16): ~a~%" result)))
>
> (void-function)
>
> (void-function-short)
>
> import org.armedbear.lisp.*;
>
> public class Main
>
> {
>
>     public static int square(int a) {
>
>         return a * a;
>
>     }
>
>     public static int squareshort(short a)
>
>     {
>
>         return a * a;
>
>     }
>
> }
>
> $ java -cp ~/abcl_excel_gen/abcl-bin-1.6.0/abcl.jar:. org.armedbear.lisp.Main
>
> Armed Bear Common Lisp 1.6.0
>
> Java 11.0.1 Oracle Corporation
>
> Java HotSpot(TM) 64-Bit Server VM
>
> Low-level initialization completed in 0.295 seconds.
>
> Startup completed in 1.663 seconds.
>
> Type ":help" for a list of available commands.
>
> CL-USER(1): (load "lispfunctions.lisp")
>
> in void-function, result of calling square(32): 1024
>
> Error loading /Users/bknotwel/abcl_excel_gen/bug/lispfunctions.lisp at line 11 (offset 358)
>
> #<THREAD "interpreter" {560A10B3}>: Debugger invoked on condition of type ERROR
>
>   no such method
>
> Restarts:
>
>   0: TOP-LEVEL Return to top level.
>
> [1] CL-USER(2):
>
> On Tuesday, November 26, 2019, 3:53:11 PM PST, Mark Evenson <evenson at panix.com> wrote:
>
> > On Nov 26, 2019, at 18:38, Brad Knotwell <bknotwell at yahoo.com> wrote:
> >
> > I've been using ABCL to get access to a Java library and it's worked generally well.  I have run into one problem I haven't been able to workaround.
> >
> > I need to make a call to a method that takes a short as an argument.  I'm passing in a value--10--that fits in a short but ABCL can't find the method (presumedly due to a type issue).  I've tried using jcoerce but it's not doing what I expect.  What's the right way to do this?
>
> This is probably a bug in how the Bear’s FFI converts the “10” when it attempts
> to locate your specific Java call site, but without a test the contains the
> Java code that you are calling into diagnosing exactly what is going in is
> difficult.
>
> When I get the time (or unless someone beats me to it), I will try to make such
> a test to figure out what is going on.
>
> We could certainly do with a test suite that combinatorally probes our call
> site location logic.
>
> --
> "A screaming comes across the sky.  It has happened before but there is nothing
> to compare to it now."



More information about the armedbear-devel mailing list