2008-11-05 09:19:59 -05:00
|
|
|
! Copyright (C) 2008 James Cash
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-11-05 23:50:33 -05:00
|
|
|
USING: kernel sequences symbols fry words assocs tools.annotations coroutines ;
|
2008-11-05 09:19:59 -05:00
|
|
|
IN: advice
|
|
|
|
|
2008-11-06 00:20:15 -05:00
|
|
|
SYMBOLS: before after around advised ;
|
2008-11-05 09:19:59 -05:00
|
|
|
|
2008-11-06 00:44:11 -05:00
|
|
|
<PRIVATE
|
2008-11-05 23:50:33 -05:00
|
|
|
: advise ( quot name word loc -- )
|
|
|
|
word-prop set-at ;
|
2008-11-06 00:44:11 -05:00
|
|
|
PRIVATE>
|
2008-11-05 23:50:33 -05:00
|
|
|
|
|
|
|
: advise-before ( quot name word -- )
|
|
|
|
before advise ;
|
|
|
|
|
|
|
|
: advise-after ( quot name word -- )
|
|
|
|
after advise ;
|
|
|
|
|
|
|
|
: advise-around ( quot name word -- )
|
2008-11-06 00:44:11 -05:00
|
|
|
[ \ coterminate suffix ] 2dip
|
2008-11-05 23:50:33 -05:00
|
|
|
around advise ;
|
|
|
|
|
2008-11-05 09:19:59 -05:00
|
|
|
: get-advice ( word type -- seq )
|
|
|
|
word-prop values ;
|
|
|
|
|
|
|
|
: call-before ( word -- )
|
|
|
|
before get-advice [ call ] each ;
|
|
|
|
|
|
|
|
: call-after ( word -- )
|
|
|
|
after get-advice [ call ] each ;
|
2008-11-05 23:50:33 -05:00
|
|
|
|
|
|
|
: call-around ( main word -- )
|
2008-11-06 00:44:11 -05:00
|
|
|
around get-advice [ cocreate ] map tuck
|
2008-11-06 00:20:15 -05:00
|
|
|
[ [ coresume ] each ] [ call ] [ reverse [ coresume ] each ] tri* ;
|
2008-11-05 09:19:59 -05:00
|
|
|
|
|
|
|
: remove-advice ( name word loc -- )
|
|
|
|
word-prop delete-at ;
|
2008-11-05 23:50:33 -05:00
|
|
|
|
|
|
|
: ad-do-it ( input -- result )
|
|
|
|
coyield ;
|
2008-11-06 00:20:15 -05:00
|
|
|
|
|
|
|
: advised? ( word -- ? )
|
|
|
|
advised word-prop ;
|
2008-11-05 09:19:59 -05:00
|
|
|
|
|
|
|
: make-advised ( word -- )
|
2008-11-05 23:50:33 -05:00
|
|
|
[ dup [ over dup '[ _ call-before _ _ call-around _ call-after ] ] annotate ]
|
2008-11-06 00:20:15 -05:00
|
|
|
[ { before after around } [ H{ } clone swap set-word-prop ] with each ]
|
|
|
|
[ t advised set-word-prop ] tri ;
|
2008-11-05 09:19:59 -05:00
|
|
|
|