3

I create this entrypoint on the Java side:

    @CEntryPoint(name = "printStruct")
    public static void printStruct(IsolateThread thread, VoidPointer message, int size) {
        System.out.println(message.isNull());
        ByteBuffer buf = CTypeConversion.asByteBuffer(message, size);

        System.out.println(new String(buf.array()));
    }

It's compiled by the GraalVM native-image compiler and libexample.h is generated with:

    void printStruct(graal_isolatethread_t* thread, void* message, int size);

Then I build and run C code:

   int main() {
     graal_isolatethread_t *thread;


     int i = graal_create_isolate(NULL, NULL, &thread);
     if (i != 0) {
       printf("Error creating isolate %d", i);
       return -1;
     }
     printStruct(thread, "heh", 3);
     i = graal_tear_down_isolate(thread);
     if (i != 0) {
       printf("Error tearing down isolate %d", i);
     }

     return 0;
   }

It builds fine but being executed outputs:

  false
  java.lang.UnsupportedOperationException: null
     at java.nio.ByteBuffer.array(ByteBuffer.java:1471)
     at examples.App.printStruct(App.java:26)

I couldn't find any explanation of that. The documentation of asByteArray says:

Creates a ByteBuffer that refers to the native memory at the specified address.

So the message is not a null pointer but I cannot access byte array I passed.

1
  • 1
    Please check you tags, is this C or C++ ? Commented Oct 17, 2022 at 14:31

1 Answer 1

4

you need to transfer the bytes from the buffer to destination array:

    var buf = CTypeConversion.asByteBuffer(message, size);
    byte[] arr = new byte[buf.remaining()];
    buf.get(arr);
Sign up to request clarification or add additional context in comments.

3 Comments

So it copies the data to ByteBuffer first and then copies additionally to the byte array, I wonder if there is a way to copy it only once?
its not necessarily copied (if meaning is backed by an array) - this thread might be helpful stackoverflow.com/questions/31062973/…
Do you know how to do this the other way around? To pass an array from GraalVm to C?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.