0

I'm using the new Foreign function & memory API which is part of project Panama and available in preview in JAVA 19.

This is the complete callback function

public static byte addFive(byte t) {
    Byte test = (Byte) t;
    return (byte) (Long.valueOf(test) + 5);
}

The native library function which is called with as paramater, the function addFive and (byte) -41.

uint8_t callback_test(uint8_t (*fn)(uint8_t), uint8_t arg) {
    uint8_t res = fn(arg + 1) * 2;
    return res;
}

I expect t to have value -40, and this does show in the Intellij debugger. But when you print it, it prints 216 instead. In addition, casting to Byte gives an ArrayIndexOutOfBoundsException.

I do realize that the native function is an unsigned int, so the returned value of 216 makes sense. However, a java byte should not be allowed that value, so, I expect java to automatically convert it to -41.

Is this behavior expected?

What is the correct way to get the unsigned int from the native code passed to the java function as a byte in the range [-128, 127]?

Tristan
  • 2,000
  • 17
  • 32
  • 1
    Could you give it a try with the JDK20 early access build? (https://jdk.java.net/20/) The `ArrayIndexOutOfBoundsException` could be caused by a bug that exists in 19 (https://github.com/openjdk/panama-foreign/pull/720) – Jorn Vernee Jan 02 '23 at 13:03
  • @JornVernee It does work in the JDK20 early access build, so that explains it. Thanks! – Tristan Jan 02 '23 at 13:31

0 Answers0