factor/library/errors.factor

42 lines
1.3 KiB
Factor

! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: kernel
DEFER: callcc1
DEFER: continue-with
IN: errors
USING: kernel-internals lists sequences ;
! This is a very lightweight exception handling system.
TUPLE: no-method object generic ;
: no-method ( object generic -- ) <no-method> throw ;
: catchstack ( -- cs ) 6 getenv ;
: set-catchstack ( cs -- ) 6 setenv ;
: >c ( catch -- ) catchstack cons set-catchstack ;
: c> ( catch -- ) catchstack uncons set-catchstack ;
: catch ( try -- exception/f | try: -- )
#! Call the try quotation. If an exception is thrown in the
#! dynamic extent of the quotation, restore the datastack
#! and push the exception. Otherwise, the data stack is
#! not restored, and f is pushed.
[ >c call f c> drop f ] callcc1 nip ; inline
: rethrow ( error -- )
#! Use rethrow when passing an error on from a catch block.
catchstack empty? [ die ] [ c> continue-with ] ifte ;
: cleanup ( try cleanup -- | try: -- | cleanup: -- )
#! Call the try quotation. If an exception is thrown in the
#! dynamic extent of the quotation, restore the datastack
#! and run the cleanup quotation. Then throw the error to
#! the next outermost catch handler.
>r [ call ] catch r> swap slip
[ nip rethrow ] when* ; inline
GENERIC: error. ( error -- )