Allocating a char buffer with JNA, Rhino, JavaScript
-
22-09-2019 - |
Question
In Narwhal, we are using JNA to make libc
calls like getcwd
and chdir
. I've only been able to use this with my limited knowledge of the JNA interface as it pertains to JavaScript in Rhino, dealing exclusively with primitives. I need to know how to allocate a char buffer so I can pass it to getcwd
, retrieve a JavaScript String from that buffer, and deallocate the buffer, presumably in a finally
clause.
Here's how we grab the libc
interface:
Here's how chdir
is implemented:
Here's where we need the solution for getcwd
Solutions in Ruby or any other embedded language for JNA would help.
Solution
you could use an nio.Buffer or jna.Pointer to pass an output buffer, something like:
invokeString(new jna.Memory(4097), 4097)
getcwd will return a char* to the input buffer, so jna will marshall the return to a string
to support longer paths, you could take an optional length
in java, new File(".").getAbsolutePath() can get the current path, so you might not need getcwd...