/
intercept.clj
331 lines (270 loc) · 12.4 KB
/
intercept.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
; Copyright (c) Gunnar Völkel. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v1.0.txt at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
(ns debug.intercept
{:author "Gunnar Völkel"}
(:require
[debug.intercept.registry :as registry])
(:use
[clojure.core.incubator :only(-?>)]
[clojure.string :only (join)])
(:import java.io.File))
; store clojure.core/defn
(def core-defn (var-get #'clojure.core/defn))
(.setMacro (var core-defn))
; store clojure.core/deftype
(def core-deftype (var-get #'clojure.core/deftype))
(.setMacro (var core-deftype))
(def global-interception? false)
(def function-interception-registry (registry/create-registry))
(def type-interception-registry (registry/create-registry))
(defn create-function-key
"Create a key for the given namespace and symbol of the function that is used in the registry."
[func-ns, func-symb]
; if the function symbol has a namespace, ...
(if (namespace func-symb)
; ... then concatenate it with the namespace prefix via \. ...
(str func-ns "." func-symb)
; ... else concatenate it the namespace via \/.
(str func-ns "/" func-symb)))
(defn create-type-key
"Create a key for the given namespace and symbol of the type that is used in the registry."
[type-ns, type-symb]
(str type-ns "." type-symb))
(defn process-defn-decl
[func-decl]
(let [; extract doc string (if present) and create metadata map
meta-map (if (string? (first func-decl))
{:doc (first func-decl)}
{}),
; remove doc string if present
func-decl (if (string? (first func-decl))
(rest func-decl)
func-decl),
; add given metadata (if present) to map
meta-map (if (map? (first func-decl))
(merge meta-map (first func-decl))
meta-map),
; remove metadata if present
func-decl (if (map? (first func-decl))
(rest func-decl)
func-decl),
; if only a single function body then put it into a list
func-decl (if (vector? (first func-decl))
(list func-decl)
func-decl)
; add given metadata at the end (if present) to map
meta-map (if (map? (last func-decl))
(merge meta-map (last func-decl))
meta-map),
; remove metadata at the end if present
func-decl (if (map? (last func-decl))
(butlast func-decl)
func-decl)
]
{:meta-map meta-map, :func-body-list func-decl}))
(defn print-function-interception-info
[prefix, full-name, params]
(println
(format "%s: (%s %s)"
prefix
(str full-name)
(str params)))
(flush))
(defn print-type-interception-info
[prefix, full-name, fields]
(println
(format "%s: (deftype %s %s)"
prefix
(str full-name)
(str (vec fields)))))
(defn intercept-func-def
[defn-command, intercept-func, func-symb, func-decl]
(let [{:keys [meta-map, func-body-list]} (process-defn-decl func-decl),
modified-func-bodies (map (partial intercept-func *ns* func-symb) func-body-list)]
`(~defn-command ~func-symb ~meta-map ~@modified-func-bodies)))
; erst raussuchen der registrierten interception funktionen und dann intercept-funcdef aufrufen
(defn defn-intercept-body
[defn-command, func-symb, func-decl]
(let [func-key (create-function-key *ns*, func-symb),
{:keys [intercept-fn, info-prefix]} (registry/get-intercept-fn function-interception-registry, func-key)]
; if no interception function was found ...
(if (nil? intercept-fn)
; ... then define function normally ...
`(~defn-command ~func-symb ~@func-decl)
; ... else apply interception function on function body before definition.
`(do
(print-function-interception-info ~info-prefix, (str *ns* "/" '~func-symb), '~(list* (map first (:func-body-list (process-defn-decl func-decl)))) )
~(intercept-func-def defn-command intercept-fn func-symb func-decl)))))
(defmacro defn-intercept
[func-symb, & func-decl]
(defn-intercept-body 'debug.intercept/core-defn, func-symb, func-decl))
(defmacro defn-intercept-
[func-symb, & func-decl]
(defn-intercept-body 'clojure.core/defn-, func-symb, func-decl))
(defn interception-setup
[registry, create-key-fn, intercept-func, func-symbols, info-prefix]
(when (fn? intercept-func)
(let [
func-keys (map (partial create-key-fn *ns*) func-symbols)
]
(doseq [fkey func-keys]
(registry/update-registry registry, intercept-func, info-prefix, fkey))
; return nil to prevent ExceptionInInitializerError when returning the map
nil)))
(defn create-intercept-symbol
[symb]
(symbol "debug.intercept", (name symb)))
(defn create-local-symbol
[symb]
(symbol (-> *ns* ns-name name) (name symb)))
(defn create-setup-macro
[registry, create-key, macro-name, intercept-func, info-prefix]
`(defmacro ~macro-name
[& func-symbols#]
`(do
(interception-on)
(interception-setup
~'~(create-intercept-symbol registry),
~'~(create-intercept-symbol create-key),
~'~(create-local-symbol intercept-func),
'~func-symbols#
~~info-prefix))))
(defn ns-import-intercept-symbols
[symbol-coll]
(doseq [[fname frename] symbol-coll]
(ns-unmap *ns* frename)
(.refer ^clojure.lang.Namespace *ns* frename (resolve (symbol "debug.intercept" (str fname))))))
(defn resolvable-fn?
[fsymb]
(-?> fsymb resolve var-get fn?))
(defmacro create-function-interception-setup-macro
[macro-name, intercept-func, info-prefix]
(when (resolvable-fn? intercept-func)
(create-setup-macro 'function-interception-registry, 'create-function-key, macro-name, intercept-func, info-prefix)))
(defmacro create-type-interception-setup-macro
[macro-name, intercept-func, info-prefix]
(when (resolvable-fn? intercept-func)
(create-setup-macro 'type-interception-registry, 'create-type-key, macro-name, intercept-func, info-prefix)))
(defn intercept-config-add
[registry, intercept-func, info-prefix, func-symbols]
(when (fn? intercept-func)
(doseq [fkey (map str func-symbols)]
(registry/update-registry registry intercept-func, info-prefix, fkey))))
(defn combine-to-function-symbol
[func-symb-seq]
(let [ns (first func-symb-seq)]
(map #(symbol (str ns) (str %)) (rest func-symb-seq))))
(defn combine-to-type-symbol
[func-symb-seq]
(let [ns (first func-symb-seq)]
(map #(symbol (str ns "." %)) (rest func-symb-seq))))
(defn process-func-symbols
[registry, combine-to-symbol-fn, intercept-func, info-prefix, func-symbol-seq]
(let [{grouped-func-symbs true, single-func-symbs false} (group-by sequential? func-symbol-seq)]
(->> (mapcat combine-to-symbol-fn grouped-func-symbs)
(concat single-func-symbs)
(intercept-config-add registry, intercept-func, info-prefix))))
(defn create-config-macro
[registry, combine-to-symbol-fn, macro-name, intercept-func, info-prefix]
`(defmacro ~macro-name
[& func-symbols#]
`(process-func-symbols ~~registry, ~~combine-to-symbol-fn, ~~intercept-func, ~~info-prefix, '~func-symbols#)))
(defmacro create-function-interception-config-macro
[macro-name, intercept-func, info-prefix]
(when (resolvable-fn? intercept-func)
(create-config-macro ''debug.intercept/function-interception-registry, ''debug.intercept/combine-to-function-symbol, macro-name, intercept-func, info-prefix)))
(defmacro create-type-interception-config-macro
[macro-name, intercept-func, info-prefix]
(when (resolvable-fn? intercept-func)
(create-config-macro ''debug.intercept/type-interception-registry, ''debug.intercept/combine-to-type-symbol, macro-name, intercept-func, info-prefix)))
(defmacro create-funtion-interception-macros
"Creates both function interception macros, the local setup macro and the global config macro, for the given interception function."
[setup-macro-name, config-macro-name, intercept-func, info-prefix]
`(do
(create-function-interception-setup-macro ~setup-macro-name, ~intercept-func, ~info-prefix)
(create-function-interception-config-macro ~config-macro-name, ~intercept-func, ~info-prefix)))
(defmacro create-type-interception-macros
"Creates both type interception macros, the local setup macro and the global config macro, for the given interception function."
[setup-macro-name, config-macro-name, intercept-func, info-prefix]
`(do
(create-type-interception-setup-macro ~setup-macro-name, ~intercept-func, ~info-prefix)
(create-type-interception-config-macro ~config-macro-name, ~intercept-func, ~info-prefix)))
(defn create-datatype-fn-key
[ns, datatype-name, func-symb]
(str ns "." datatype-name "/" func-symb))
(defn intercept-spec-fn
[ns, datatype-name, [fn-name, params, & body :as spec-fn]]
(let [func-key (create-datatype-fn-key ns, datatype-name, fn-name),
{:keys [intercept-fn, info-prefix]} (registry/get-intercept-fn function-interception-registry, func-key)]
; if no interception function found, ...
(if (nil? intercept-fn)
; ... then return the function unchanged ...
spec-fn
; ... else apply interception function on the body of the given function.
(do
(print-function-interception-info info-prefix, func-key, params)
(list* fn-name (intercept-fn (str ns "." datatype-name) fn-name (list* params body)))))))
(defn intercept-specs
[ns, datatype-name, specs]
(map #(if (list? %) (intercept-spec-fn ns, datatype-name, %) %) specs))
; type-interceptio-function: [ns, name, fields, specs] -> [fields, specs]
(defn def*-intercept
[def-cmd, name, fields, specs]
(let [type-key (create-type-key *ns*, name),
{:keys [intercept-fn, info-prefix]} (registry/get-intercept-fn type-interception-registry, type-key),
intercept-type? (fn? intercept-fn),
[fields, specs] (if intercept-type? (intercept-fn *ns*, name, fields, specs) [fields, specs])]
`(do
~(when intercept-type? `(print-type-interception-info ~info-prefix, ~type-key, '~fields))
(~def-cmd ~name, ~fields, ~@(intercept-specs *ns*, name, specs)))))
(defmacro deftype-intercept
[name, fields, & specs]
(def*-intercept 'debug.intercept/core-deftype, name, fields, specs))
(defmacro defrecord-intercept
[name, fields, & specs]
(def*-intercept 'clojure.core/defrecord, name, fields, specs))
(defn file-exists?
[file-name]
(try
(when-let [f (File. file-name)]
(and (.exists f) (.isFile f)))
(catch Exception e false)))
(defn interception-on
"Replaces clojure.core macros for interception. Returns true, when interception was switched on - nil, if it was activated already."
[]
(when-not global-interception?
(alter-var-root #'global-interception? (constantly true))
(alter-var-root #'clojure.core/defn (constantly (var-get #'defn-intercept)))
(alter-var-root #'clojure.core/deftype (constantly (var-get #'deftype-intercept)))
true))
(defn interception-off
"Restores original clojure.core macros. Returns true, when interception was switched off - nil, if it was deactivated already."
[]
(when global-interception?
(alter-var-root #'global-interception? (constantly false))
(alter-var-root #'clojure.core/defn (constantly (var-get #'core-defn)))
(alter-var-root #'clojure.core/deftype (constantly (var-get #'core-deftype)))
true))
(def ^{:private true} interception-enabled? false) ; Determines whether interception of functions is enabled.
(defn enable-intercept
[enabled?]
(alter-var-root #'interception-enabled? (constantly enabled?)))
(defn setup-global-interception
[file-name, intercept-config-funcs]
(when (file-exists? file-name)
(doseq [func-symb intercept-config-funcs]
(use [(symbol (namespace func-symb)) :only [(symbol (name func-symb))]]))
(refer 'debug.intercept :only '[enable-intercept])
(load-file file-name)
(when interception-enabled? (interception-on))))
(defmacro intercept-global
[file-name & intercept-config-funcs]
(setup-global-interception (str file-name), intercept-config-funcs))
(defmacro intercept-global-runtime
[file-name & intercept-config-funcs]
`(setup-global-interception ~(str file-name), '~intercept-config-funcs))