2005-01-29 14:18:28 -05:00
|
|
|
! Copyright (C) 2004, 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2005-03-21 14:39:46 -05:00
|
|
|
IN: kernel
|
2005-09-18 01:37:28 -04:00
|
|
|
DEFER: callcc1
|
2005-09-14 00:37:50 -04:00
|
|
|
DEFER: continue-with
|
|
|
|
|
2005-03-21 14:39:46 -05:00
|
|
|
IN: errors
|
2005-09-18 23:22:58 -04:00
|
|
|
USING: kernel-internals lists sequences ;
|
2005-02-24 20:52:17 -05:00
|
|
|
|
2005-08-25 15:27:38 -04:00
|
|
|
! This is a very lightweight exception handling system.
|
2004-07-17 18:35:09 -04:00
|
|
|
|
2005-08-25 15:27:38 -04:00
|
|
|
TUPLE: no-method object generic ;
|
2005-01-23 21:31:32 -05:00
|
|
|
|
2005-09-17 22:25:18 -04:00
|
|
|
: no-method ( object generic -- ) <no-method> throw ;
|
2004-11-25 21:51:47 -05:00
|
|
|
|
2004-11-25 23:14:17 -05:00
|
|
|
: catchstack ( -- cs ) 6 getenv ;
|
|
|
|
: set-catchstack ( cs -- ) 6 setenv ;
|
2004-11-25 21:51:47 -05:00
|
|
|
|
2004-11-25 23:14:17 -05:00
|
|
|
: >c ( catch -- ) catchstack cons set-catchstack ;
|
|
|
|
: c> ( catch -- ) catchstack uncons set-catchstack ;
|
2004-07-17 18:35:09 -04:00
|
|
|
|
2005-09-20 20:18:01 -04:00
|
|
|
: 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.
|
2005-09-18 23:22:58 -04:00
|
|
|
[ >c call f c> drop f ] callcc1 nip ; inline
|
2005-09-14 00:37:50 -04:00
|
|
|
|
2004-07-17 18:35:09 -04:00
|
|
|
: rethrow ( error -- )
|
|
|
|
#! Use rethrow when passing an error on from a catch block.
|
2005-09-21 01:12:16 -04:00
|
|
|
catchstack empty? [
|
|
|
|
die "Can't happen" throw
|
|
|
|
] [
|
|
|
|
c> continue-with
|
|
|
|
] ifte ;
|
2005-09-20 20:18:01 -04:00
|
|
|
|
|
|
|
: 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.
|
2005-09-21 01:12:16 -04:00
|
|
|
>r [ dup slip ] catch nip r>
|
|
|
|
swap slip [ rethrow ] when* ; inline
|
|
|
|
|
|
|
|
: recover ( try recovery -- | try: -- | recovery: -- )
|
|
|
|
#! Call the try quotation. If an exception is thrown in the
|
|
|
|
#! dynamic extent of the quotation, restore the datastack,
|
|
|
|
#! push the exception on the datastack, and call the
|
|
|
|
#! recovery quotation.
|
|
|
|
>r catch r> when* ; inline
|
2005-03-25 21:43:06 -05:00
|
|
|
|
|
|
|
GENERIC: error. ( error -- )
|