Guest User

Untitled

a guest
Aug 28th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. ;; Simple and rough substitute for defadvice :around on SBCL. Should
  2. ;; be easy to port by substituting something appropriate for
  3. ;; sb-introspect:function-lambda-list
  4.  
  5. ;; Its easy to get confused with wrapped functions - if you redefine a
  6. ;; function while it is wrapped, it will still seem to be wrapped
  7. ;; (entry in the hash table for the name) but won't be.
  8.  
  9. (defvar *wrapped-functions* (make-hash-table))
  10.  
  11. (defun wrapped-function-p (function)
  12. (if (gethash function *wrapped-functions*) t nil))
  13.  
  14. (defmacro undefwrapper (function)
  15. (check-type function symbol)
  16. `(let ((orig-function (gethash ',function *wrapped-functions*)))
  17. (when orig-function
  18. (setf (fdefinition ',function) orig-function)
  19. (remhash ',function *wrapped-functions*))))
  20.  
  21. (defmacro defwrapper (function &body body)
  22. (labels ((copy (node)
  23. (etypecase node
  24. (symbol (intern (symbol-name node)))
  25. (atom node)
  26. (cons (cons (copy (car node))
  27. (copy (cdr node)))))))
  28. (check-type function symbol)
  29. `(progn
  30. (assert (fboundp ',function))
  31. (assert (not (gethash ',function *wrapped-functions*)))
  32. (setf (gethash ',function *wrapped-functions*) #',function)
  33. (macrolet ((get-orig-function ()
  34. `(gethash ',',function *wrapped-functions*))
  35. (call-orig-function (&rest args)
  36. `(apply (gethash ',',function *wrapped-functions*) (list ,@args))))
  37. (setf (fdefinition ',function)
  38. (lambda ,(copy (sb-introspect:function-lambda-list function))
  39. ,@body))))))
  40.  
  41. ;; (defun sum (values) (reduce #'+ values))
  42.  
  43. ;; (sum '(1 3 5)) => 9
  44.  
  45. ;; (defwrapper sum
  46. ;; (let ((result (call-orig-function values)))
  47. ;; (values result
  48. ;; (/ result (length values)))))
  49.  
  50. ;; (sum '(1 3 5)) => 9 3
Add Comment
Please, Sign In to add comment