views:

225

answers:

1

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:

http://github.com/280north/narwhal/blob/34ac15261fa4acdef3867256e97d7aabb94766e0/engines/rhino/lib/fs-base.js#L32-42

Here's how chdir is implemented:

http://github.com/280north/narwhal/blob/34ac15261fa4acdef3867256e97d7aabb94766e0/engines/rhino/lib/fs-base.js#L438-444

Here's where we need the solution for getcwd

http://github.com/280north/narwhal/blob/34ac15261fa4acdef3867256e97d7aabb94766e0/engines/rhino/lib/fs-base.js#L416-419

Solutions in Ruby or any other embedded language for JNA would help.

+1  A: 

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...

jspcal
Solution: http://github.com/kriskowal/narwhal/blob/3d6a7cd99c086340f695255057e93e877ed482d1/engines/rhino/lib/fs-base.js#L477-486
Kris Kowal