-
Notifications
You must be signed in to change notification settings - Fork 5
/
assess-call.el
115 lines (91 loc) · 3.68 KB
/
assess-call.el
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
;;; assess-call.el --- Call and Return -*- lexical-binding: t -*-
;;; Header:
;; This file is not part of Emacs
;; Author: Phillip Lord <[email protected]>
;; Maintainer: Phillip Lord <[email protected]>
;; The contents of this file are subject to the GPL License, Version 3.0.
;; Copyright (C) 2016-2022 Free Software Foundation, Inc.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Capture calls to functions, checking parameters and return values.
;;; Code:
;; ** Call Capture
;; Here we provide a function for tracing calls to a particular function. This
;; can be a direct or indirect call; parameters and return values are available
;; for inspection afterwards. For example:
;; #+begin_src elisp
;; (assess-call-capture
;; '+
;; (lambda()
;; (+ 1 1)))
;; ;; => (((1 1) . 2))
;; #+end_src
;; The return value is a list of cons cells, one for each invocation, of the
;; parameters and return values.
;; #+begin_src emacs-lisp
(defun assess-call--capture-lambda ()
"Return function which captures args and returns of another.
The returned function takes FN the function to call, and any
number of ARGS to call the function with. In the special case,
that FN is equal to `:return`, then all previous args and return
values of FN are returned instead."
(let ((capture-store nil))
(lambda (fn &rest args)
(if (eq fn :return)
capture-store
(let ((rtn (apply fn args)))
(setq capture-store
(cons (cons args rtn)
capture-store))
rtn)))))
(defun assess-call-capture (sym-fn fn)
"Trace all calls to SYM-FN when FN is called with no args.
The return value is a list of cons cells, with car being the
parameters of the calls, and the cdr being the return value."
(let ((capture-lambda
(assess-call--capture-lambda)))
(unwind-protect
(progn (advice-add sym-fn :around capture-lambda)
(funcall fn)
(funcall capture-lambda :return))
(advice-remove sym-fn capture-lambda))))
(defun assess-call--hook-capture-lambda ()
"Returns a function which captures all of its args.
The returned function takes any number of ARGS. In the special
case that the first arg is `:return` then it returns all previous
args."
(let ((capture-store nil))
(lambda (&rest args)
(if (eq (car-safe args) :return)
capture-store
(setq capture-store
(cons
args
capture-store))))))
(defun assess-call-capture-hook (hook-var fn &optional append local)
"Trace all calls to HOOK-VAR when FN is called with no args.
APPEND and LOCAL are passed to `add-hook` and documented there."
(let ((capture-lambda
(assess-call--hook-capture-lambda)))
(unwind-protect
(progn
(add-hook hook-var
capture-lambda
append local)
(funcall fn)
(funcall capture-lambda :return))
(remove-hook hook-var
capture-lambda
local))))
(provide 'assess-call)
;;; assess-call.el ends here
;; #+end_src