This is merely an example and not a full interop library.
All three examples attempt to call the same Nim proc (defined in implementation.nim
) from different Clojure implementations.
To build it (assuming you already have Nim and Clojure), you'll need jnim
, which can be installed with nimble install jnim
. Then do:
javac Wrapper.java
nim --app:lib -d:noSignalHandler c wrapper_jni.nim
A .class
file and .so
file will be generated.
To run it, do the following:
export CLASSPATH=.
export LD_LIBRARY_PATH=.
clojure use_it.clj
It should print [0 0 1 0 1 2 0 1 2 3 0 1 2 3 4 0 1 2 3 4 5]
.
Just like the JVM version requires jnim
, this version requires jffi
. Only tested with Lumo. Other ClojureScript environments should be possible as well, but it would require some additional work.
Build it like this:
nim js -d:nodejs --opt:size wrapper_node.nim
Then run it:
lumo use_it.cljs
It should print [0 0 1 0 1 2 0 1 2 3 0 1 2 3 4 0 1 2 3 4 5]
.
To implement a babashka pod in Nim, an ad-hoc bencode implementation (readbabashkainput.nim
) was necessary (existing Nim implementations of bencode all seem to require strings, while we need to parse stdin on the fly). This pod assumes well-formed input and intentionally omits most error handling.
Build it:
nim c pod.nim
Run it:
bb use_it.bb
It should print [0 0 1 0 1 2 0 1 2 3 0 1 2 3 4 0 1 2 3 4 5]
.