forked from Chouser/clojure-jna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
106 lines (81 loc) · 3.18 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
Dynamically load and use native C libs from Clojure using JNA
Requires JNA: https://jna.dev.java.net/
To fetch this lib:
git clone git://github.com/Chouser/clojure-jna.git
cd clojure-jna
In order to use JNA on Ubuntu, I did:
aptitude install libjna-java
Then I have to start Clojure with a parameter to tell Java where to find the
JNA native libs, the JNA .jar, and of course Clojure and this lib. Again on
Ubuntu that looks something like this:
java -Djava.library.path=/usr/lib/jni -Djava.ext.dirs=/usr/share/java -cp clojure-1.0.0.jar:src clojure.main
Then use it:
Clojure 1.0.0-
user=> (use 'net.n01se.clojure-jna)
nil
user=> (jna-invoke Integer c/printf "My number: %d\n" 5)
My number: 5
13
The first argument to jna-invoke is the native function's return value. The
second is a symbol, in this case c/printf. The "c" part is the name of the
library, in this case "libc". The "printf" part is of course the name of the
function to call. The rest are arguments to the native function.
That 13 is printf's return value -- I guess it's the number of bytes printed or
something? Anyway, feel free to ignore it just like all C programs do.
If you're going to be calling the same function a few times, you might find it
convenient to be able to call it like a regular Clojure function. Use jna-fn
for that:
user=> (doc jna-fn)
-------------------------
net.n01se.clojure-jna/jna-fn
([return-type function-symbol])
Macro
Return a Clojure function that wraps a native library function:
(def c-printf (jna-fn Integer c/printf))
(c-printf "My number: %d\n" 5)
nil
user=> (def c-printf (jna-fn Integer c/printf))
#'user/c-printf
user=> (c-printf "My number: %d\n" 5)
My number: 5
13
user=> (c-printf "My number: %d\n" 10)
My number: 10
14
If you're going to be calling a bunch of functions from the same native lib, you
might like to use jna-ns to create a Clojure namespace full of Clojure functions
that wrap the native functions:
user=> (doc jna-ns)
-------------------------
net.n01se.clojure-jna/jna-ns
([new-ns libname fnspecs])
Macro
Create a namespace full of Clojure functions that wrap functions from
a native library:
(jna-ns native-c c [Integer printf, Integer open, Integer close])
(native-c/printf "one %s two\n" "hello")
nil
user=> (jna-ns native-c c [Integer printf, Integer open, Integer close])
#<Namespace native-c>
user=> (native-c/printf "one %s two\n" "hello")
one hello two
14
user=> (native-c/open "README")
-1
Enjoy!
--Chouser
-- Begin Berger's Section --
Adding a lot of function in preparation to dynamically compile functions for CUDA-clojure mapping.
user> (def fdef (create-jna-fn-def "add_floatNumber" Float Float Float))
#'user/fdef
user> (get-cpp-mangled-name fdef)
"_ZN15add_floatNumberEff"
user> fdef
{:return-type java.lang.Float, :name "add_floatNumber", :arguments (java.lang.Float java.lang.Float)}
(def fndef (create-namespaced-jna-fn-def ["test1" "test2"] "add_floatNumber" Float [:pointer Float] Integer))
#'user/fndef
(get-cpp-mangled-name fndef)
"_ZN5test15test215add_floatNumberEPfi"
user> fndef
{:namespaces ["test1" "test2"], :return-type java.lang.Float, :name "add_floatNumber", :arguments ([:pointer java.lang.Float] java.lang.Integer)}
-- End Berger's Section --