-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen.clj
505 lines (405 loc) · 12.2 KB
/
gen.clj
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
(ns com.phronemophobic.gen
(:require [clojure.data.json :as json])
(:import com.sun.jna.Structure$FFIType$size_t
com.sun.jna.CallbackProxy
com.sun.jna.Pointer)
)
(def void Void/TYPE)
(def main-class-loader @clojure.lang.Compiler/LOADER)
(def cljcef
(try
(com.sun.jna.NativeLibrary/getInstance "cljcef")
(catch java.lang.UnsatisfiedLinkError e
nil)))
(defmacro defc
([fn-name ret]
`(defc ~fn-name ~ret []))
([fn-name ret args]
(let [cfn-sym (with-meta (gensym "cfn") {:tag 'com.sun.jna.Function})]
`(if cljcef
(let [~cfn-sym (.getFunction ~(with-meta 'cljcef {:tag 'com.sun.jna.NativeLibrary})
~(name fn-name))]
(defn- ~fn-name [~@args]
(.invoke ~cfn-sym
~ret (to-array [~@args]))))
(defn- ~fn-name [~@args]
(throw (Exception. (str ~(name fn-name) " not loaded."))))))))
(defn load-structs []
(json/read
(clojure.java.io/reader
(clojure.java.io/resource "cef.json"))))
(defn api-structs []
(let [structs (load-structs)]
(into {}
(comp
(filter (fn [[k {:strs [path]}]]
(.endswith path "capi.h"))))
structs)))
(def simple-types
#{"int"
"float"
"double"
"short"
"size_t"
"uint32"
"uint64"
"wchar_t"
"const float"
"const char"
"char16"
"int32"
"int64"
["void"]
"char"})
(defn simple-type? [ptype]
(or (simple-types ptype)
(vector? ptype)))
(defn type->str [ptype]
(cond
(= ["void"] ptype)
"void*"
(= "void" ptype)
"void"
(= "const void" ptype)
"const void"
(vector? ptype)
(str (type->str (first ptype)) "*")
(simple-type? ptype)
ptype
(map? ptype)
(get ptype "decl")
(.startsWith ptype "struct ")
ptype
(.startsWith ptype "const struct ")
ptype
(and (.startsWith ptype "cef_")
(.endsWith ptype "_t"))
ptype
(and (.startsWith ptype "const cef_")
(.endsWith ptype "_t"))
ptype
:else
(throw (Exception. (str "can't convert type to string, " ptype)))))
(defn type->binding [ptype]
(cond
(simple-type? ptype)
"x"
(map? ptype)
(get ptype "name")
:else
(throw (Exception. (str "can't convert type to binding, " ptype)))
))
(defn type->arg [ptype]
(cond
(simple-type? ptype)
(str (type->str ptype) " " (type->binding ptype))
(map? ptype)
(get ptype "decl")
:else
(throw (Exception. (str "can't convert type to arg, " ptype))))
)
(defn struct-getter [sname [pname ptype]]
(cond
(map? ptype)
(str
(type->str (get ptype "ret"))
" "
sname "_call_" pname "(" sname "* s, " (clojure.string/join
", "
(map-indexed (fn [i arg-type]
(str (type->str arg-type) " x" i))
(get ptype "args"))) "){\n"
(when (not= "void" (get ptype "ret"))
"return ")
"s->" pname "(" (clojure.string/join "," (map-indexed (fn [i _] (str "x" i))
(get ptype "args")))
");"
"}"
)
:else
(str
(type->str ptype)
" "
sname "_get_" pname "(" sname "* s){ return s->"pname "; }")))
(defn struct-setter [sname [pname ptype]]
(when (or (simple-type? ptype)
(= "cef_string_t" ptype)
(map? ptype))
(case ptype
"cef_string_t"
(str
"void"
" "
sname "_set_" pname "(" sname "* self, const char *s)"
"{ CefString(&self->"pname ").FromASCII(s); }")
;;else
(str
"void"
" "
sname "_set_" pname "(" sname "* s, "
(type->arg ptype) " )"
"{ s->"pname " = " (type->binding ptype) "; }")))
)
(defn struct-create [struct]
(let [sname (get struct "name")]
(str
sname "* "
sname "_create(){\n"
sname "* s = new " sname "();\n"
(when (= (get-in struct ["props" "base"]) "cef_base_ref_counted_t")
(str "s->base.size = sizeof("sname ");\n"))
"return s;\n"
"}"
)))
(defn struct-destroy [struct]
(let [sname (get struct "name")]
(str
"void "
sname "_destroy( "sname "* s){\n"
"delete s;\n"
"}"
)))
(defn gen-struct [struct]
(let [sname (get struct "name")]
(clojure.string/join "\n\n"
(into
[(struct-create struct)
(struct-destroy struct)]
(sequence
cat
(for [[pname ptype] (get struct "props")
:when (not= "base" pname)]
[(when (or (simple-type? ptype)
(map? ptype))
(struct-getter sname [pname ptype]))
(struct-setter sname [pname ptype])]))))))
(defn gen-headers [structs]
(let [headers (->> structs
vals
(map #(get % "path"))
set)]
(clojure.string/join "\n"
(map #(str "#include \"" % "\"")
headers))))
(defn gen-c-wrappers
([]
(gen-c-wrappers (load-structs)))
([structs]
(clojure.string/join "\n\n"
[(gen-headers structs)
"extern \"C\" {\n"
(clojure.string/join "\n\n"
(map gen-struct (vals structs)))
"\n}"])))
(defn write-wrappers! []
(spit "csource/capi-wrappers.h"
(gen-c-wrappers)))
(defonce not-garbage
(atom []))
(defn preserve!
"Store this value so it's not garbage collected"
[x]
(swap! not-garbage conj x)
x)
(defn keywordize [s]
(-> s
(clojure.string/replace #"_" "-")
keyword))
(defn symbolize [s]
(-> s
(clojure.string/replace #"_" "-")
symbol))
(defn size-t [n]
(com.sun.jna.Structure$FFIType$size_t. n))
(defn type->class [t]
(case t
"int" `Integer/TYPE
"float" `Float/TYPE
"double" `Double/TYPE
"short" `Short/TYPE
"size_t" `com.sun.jna.Structure$FFIType$size_t
"uint32" `Integer/TYPE
"char" `Character/TYPE
"int64" `Long/TYPE
"void" `void
"cef_string_t" `String
"cef_process_id_t" Integer/TYPE
"cef_drag_operations_mask_t" Integer/TYPE
"cef_text_input_mode_t" Integer/TYPE
"cef_paint_element_type_t" Integer/TYPE
"cef_string_list_t" Pointer
;; else
(cond
(vector? t)
`Pointer
(map? t)
`com.sun.jna.CallbackProxy
:else
(assert false (str "Unknown Type: " t)))))
(declare coerce-type)
(defn coerce-callback [{:strs [args ret] :as type}]
(let [ret-sym (gensym "ret-")]
`(preserve!
(fn [x#]
(assert (fn? x#) (str "Expecting function. received " x#))
(reify
com.sun.jna.CallbackProxy
(getParameterTypes [_#]
(into-array Class [~@(map type->class args)]))
(getReturnType [_#]
~(type->class ret))
(callback ^void [_# args#]
(.setContextClassLoader (Thread/currentThread) main-class-loader)
(try
(let [~ret-sym (apply x# args#)]
~(if (= ret "void")
nil
(list (coerce-type ret)
ret-sym)))
(catch Exception e#
(println e#)))))
))))
(defn coerce-type [type]
(case type
"int" `int
"float" `float
"double" `double
"short" `short
"size_t" `size-t
"uint32" `int
"int64" `long
"char" `char
"cef_string_t" `str
["char"] `str
"cef_string_list_t" `(fn [x#]
(assert (instance? Pointer x#))
x#)
;; else
(cond
(and (map? type)
(= "fptr" (get type "type")))
(coerce-callback type)
(vector? type)
`(fn [x#]
(assert (instance? Pointer x#))
x#)
:else
(assert false (str "Unknown Type: " type)))))
(comment
(def tstruct (->(load-structs )
vals
(nth 2)
))
,)
(declare type->class)
(defn- def-struct-fn* [struct]
(let [sname (get struct "name")
create-name (symbol
(str sname "_create"))
destroy-name (symbol
(str sname "_destroy"))]
`(do
(defc ~create-name Pointer)
(defc ~destroy-name void [~'struct])
~@(for [[pname ptype] (get struct "props")
:when (map? ptype)
:let [call-name (symbol
(str sname "_call_" pname))]]
`(defc ~call-name void [~'self ~@(for [[i arg] (map-indexed vector (get ptype "args"))]
(symbol (str "x" i)))]))
~@(for [[pname ptype] (get struct "props")
:when (or (simple-type? ptype)
(= "cef_string_t" ptype)
(map? ptype))
:let [set-name (symbol
(str sname "_set_" pname))]]
`(defc ~set-name void [~'self ~'x])))))
(defn- def-struct-fns* [structs]
`(do
~@(map def-struct-fn* (vals structs))))
(defmacro def-struct-fns
([]
(def-struct-fns* (load-structs)))
([structs]
(def-struct-fns* structs)))
(defn can-gen-callback? [ptype]
(let [test (fn [ptype]
(or (#{"int"
"float"
"double"
"short"
"size_t"
"uint32"
"char"
"int64"
"void"
"cef_process_id_t"
"cef_drag_operations_mask_t"
"cef_text_input_mode_t"
"cef_paint_element_type_t"
"cef_string_list_t"
"cef_string_t"}
ptype)
(vector? ptype)
(map? ptype)))]
(and (test (get ptype "ret"))
(every? test (get ptype "args")))))
(defn can-gen-struct? [struct]
(every?
(fn ok? [ptype]
(and
(not= ptype [["char"]])
(or (simple-type? ptype)
(= "cef_string_t" ptype)
(and (map? ptype)
(can-gen-callback? ptype)))))
(-> struct
(get "props")
(dissoc "base")
vals
)))
(defn -gen-clj-struct [struct]
(let [fname (-> (get struct "name")
(clojure.string/replace #"^cef_" "")
(clojure.string/replace #"_t$" "")
(clojure.string/replace #"_" "-"))
fname (str "->" fname)
merge-name (symbol (str "merge" fname))
fname (symbol fname)
sname (get struct "name")
prop-keys (-> struct
(get "props")
keys
(->> (remove #{"base"}))
(->> (map symbolize)))
asserts (for [[k v] (get struct "props")]
`(assert ))
struct-sym (gensym "struct-")]
`(do
(defn ~merge-name [~'struct {:keys [~@prop-keys]}]
~@(let [struct-sym 'struct]
(for [[pname ptype] (get struct "props")
:when (not= pname "base")
:let [set-name (symbol
(str sname "_set_" pname))
binding (symbolize pname)]]
`(when ~binding
(~set-name ~struct-sym
(~(coerce-type ptype) ~binding)))))
~'struct)
(defn ~fname [{:keys [~@prop-keys] :as ~'m}]
(let [~struct-sym (preserve!
(~(symbol
(str sname "_create"))))]
(~merge-name ~struct-sym ~'m))))
))
(defmacro gen-clj-structs []
`(do
~@(->> (load-structs)
vals
(filter can-gen-struct?)
(map -gen-clj-struct))))
(comment
(gen-clj-structs))
;; (def-struct-fns)
;; (gen-clj-structs)