|
| 1 | +;; XXX: work on export-symbols from tech.parallel.utils? |
| 2 | +(ns hooks.libpython-clj.jna.base.def-pylib-fn |
| 3 | + "The def-pylib-fn macro from libpython-clj/jna/base.clj" |
| 4 | + (:require [clj-kondo.hooks-api :as api])) |
| 5 | + |
| 6 | +;; from: libpython-clj/jna/base.clj |
| 7 | + |
| 8 | +;; (defmacro def-pylib-fn |
| 9 | +;; [fn-name docstring rettype & argpairs] |
| 10 | +;; `(defn ~fn-name |
| 11 | +;; ~docstring |
| 12 | +;; ~(mapv first argpairs) |
| 13 | +;; (when-not (== (current-thread-id) (.get ^AtomicLong gil-thread-id)) |
| 14 | +;; (throw (Exception. "Failure to capture gil when calling into libpython"))) |
| 15 | +;; (let [~'tvm-fn (jna/find-function ~(str fn-name) *python-library*) |
| 16 | +;; ~'fn-args (object-array |
| 17 | +;; ~(mapv (fn [[arg-symbol arg-coersion]] |
| 18 | +;; (when (= arg-symbol arg-coersion) |
| 19 | +;; (throw (ex-info (format "Argument symbol (%s) cannot match coersion (%s)" |
| 20 | +;; arg-symbol arg-coersion) |
| 21 | +;; {}))) |
| 22 | +;; `(~arg-coersion ~arg-symbol)) |
| 23 | +;; argpairs))] |
| 24 | +;; ~(if rettype |
| 25 | +;; `(.invoke (jna-base/to-typed-fn ~'tvm-fn) ~rettype ~'fn-args) |
| 26 | +;; `(.invoke (jna-base/to-typed-fn ~'tvm-fn) ~'fn-args))))) |
| 27 | + |
| 28 | +;; called like: |
| 29 | + |
| 30 | +;; (def-pylib-fn PyImport_AddModule |
| 31 | +;; "Return value: Borrowed reference. |
| 32 | +;; |
| 33 | +;; Similar to PyImport_AddModuleObject(), but the name is a UTF-8 encoded string instead |
| 34 | +;; of a Unicode object." |
| 35 | +;; Pointer |
| 36 | +;; [name str]) |
| 37 | + |
| 38 | +(defn def-pylib-fn |
| 39 | + "Macro in libpython-clj/jna/base.clj. |
| 40 | +
|
| 41 | + Example call: |
| 42 | +
|
| 43 | + (def-pylib-fn PyImport_AddModule |
| 44 | + \"Return value: Borrowed reference. |
| 45 | +
|
| 46 | + Similar to PyImport_AddModuleObject(), but the name is a UTF-8 ... |
| 47 | + of a Unicode object.\" |
| 48 | + Pointer |
| 49 | + [name str]) |
| 50 | +
|
| 51 | + This has the form: |
| 52 | +
|
| 53 | + (def-pylib-fn fn-name docstring rettype & argpairs) |
| 54 | +
|
| 55 | + May be treating it as: |
| 56 | +
|
| 57 | + (defn fn-name |
| 58 | + [arg1 arg2 ,,, argn] |
| 59 | + [arg1 arg2 ,,, argn]) ; fake usage of args? |
| 60 | +
|
| 61 | + where arg1, ..., argn are extracted from argpairs is acceptable. |
| 62 | +
|
| 63 | + XXX: using the second elements of argpairs might be worth doing |
| 64 | + to avoid unused warnings. |
| 65 | +
|
| 66 | + " |
| 67 | + [{:keys [:node]}] |
| 68 | + (let [[_ fn-name _ _ & argpairs] (:children node) |
| 69 | + pairs (map (fn [vec-node] |
| 70 | + (let [[an-arg a-type] (:children vec-node)] |
| 71 | + [an-arg a-type])) |
| 72 | + argpairs) |
| 73 | + new-node (with-meta (api/list-node |
| 74 | + (apply concat |
| 75 | + [(api/token-node 'defn) |
| 76 | + fn-name |
| 77 | + (api/vector-node (map first pairs))] |
| 78 | + pairs)) |
| 79 | + (meta node))] |
| 80 | + ;; XXX: uncomment following and run clj-kondo on cl_format.clj to debug |
| 81 | + ;;(prn (api/sexpr node)) |
| 82 | + ;;(prn (api/sexpr new-node)) |
| 83 | + {:node new-node})) |
0 commit comments