branch: externals/assess commit f1edef32206a3a80e51453f7630ba04d94def25f Author: Phillip Lord <phillip.l...@russet.org.uk> Commit: Phillip Lord <phillip.l...@russet.org.uk>
Move call implementation to closure Previously, calls were captured in a global var which was only going to work with one call at a time. Now we use a closure to avoid this problem. --- assess-call.el | 21 ++++++++++++++++----- test/assess-call-test.el | 8 ++++---- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/assess-call.el b/assess-call.el index 4a9dfdabad..cd5c35f54e 100644 --- a/assess-call.el +++ b/assess-call.el @@ -62,16 +62,27 @@ assess-call--capture-store)) rtn)) +(defun assess-call--capture-lambda () + (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))))))) + (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." - (setq assess-call--capture-store nil) - (advice-add sym-fn :around #'assess-call--capture-advice) - (funcall fn) - (advice-remove sym-fn #'siyphus-call--capture-advice) - assess-call--capture-store) + (let ((capture-lambda + (assess-call--capture-lambda))) + (advice-add sym-fn :around capture-lambda) + (funcall fn) + (advice-remove sym-fn capture-lambda) + (funcall capture-lambda :return))) (provide 'assess-call) ;;; assess-call.el ends here diff --git a/test/assess-call-test.el b/test/assess-call-test.el index 25ae531d58..9d0876b3fb 100644 --- a/test/assess-call-test.el +++ b/test/assess-call-test.el @@ -52,7 +52,7 @@ (lambda () (assess-call-call-return-car 20 21 22)))))) -(defun call-capture-multiply (a b) +(defun assess-call-capture-multiply (a b) (* a b)) (ert-deftest call-capture-twice () @@ -60,9 +60,9 @@ (equal '(((3 4) . 12) ((1 2) . 2)) (assess-call-capture - 'call-capture-multiply + 'assess-call-capture-multiply (lambda () - (call-capture-multiply 1 2) - (call-capture-multiply 3 4)))))) + (assess-call-capture-multiply 1 2) + (assess-call-capture-multiply 3 4)))))) (provide 'assess-call-test)