2006-05-24 18:42:30 -04:00
|
|
|
! Copyright (C) 2004, 2006 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2006-05-20 02:37:35 -04:00
|
|
|
IN: kernel-internals
|
2006-11-15 22:57:58 -05:00
|
|
|
USING: arrays generic namespaces sequences math ;
|
2005-02-24 20:52:17 -05:00
|
|
|
|
2005-11-12 00:37:24 -05:00
|
|
|
: >c ( continuation -- ) catchstack* push ;
|
|
|
|
: c> ( -- continuation ) catchstack* pop ;
|
2004-07-17 18:35:09 -04:00
|
|
|
|
2006-05-20 02:37:35 -04:00
|
|
|
IN: errors
|
|
|
|
USING: kernel ;
|
|
|
|
|
2006-08-01 18:27:07 -04:00
|
|
|
SYMBOL: error
|
|
|
|
SYMBOL: error-continuation
|
2006-11-15 22:57:58 -05:00
|
|
|
SYMBOL: error-stack-trace
|
|
|
|
SYMBOL: restarts
|
2006-08-01 18:27:07 -04:00
|
|
|
|
2006-08-15 21:23:05 -04:00
|
|
|
: catch ( try -- error/f )
|
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
|
|
|
|
2006-01-07 19:10:52 -05:00
|
|
|
: rethrow ( error -- )
|
2006-09-08 02:32:14 -04:00
|
|
|
catchstack* empty?
|
|
|
|
[ die ] [ dup error set-global c> continue-with ] if ;
|
2006-01-07 19:10:52 -05:00
|
|
|
|
2006-08-15 16:29:35 -04:00
|
|
|
: cleanup ( try cleanup -- )
|
2005-09-22 21:01:55 -04:00
|
|
|
[ >c >r call c> drop r> call ]
|
2006-09-08 02:32:14 -04:00
|
|
|
[ >r nip call r> rethrow ] ifcc ;
|
2006-01-07 16:03:31 -05:00
|
|
|
inline
|
2005-09-21 01:12:16 -04:00
|
|
|
|
2006-08-15 16:29:35 -04:00
|
|
|
: recover ( try recovery -- )
|
2005-09-22 21:01:55 -04:00
|
|
|
[ >c drop call c> drop ]
|
2006-09-08 02:32:14 -04:00
|
|
|
[ rot drop swap call ] ifcc ; inline
|
2005-03-25 21:43:06 -05:00
|
|
|
|
2006-11-30 02:15:42 -05:00
|
|
|
TUPLE: condition restarts continuation ;
|
2006-05-24 04:29:25 -04:00
|
|
|
|
|
|
|
C: condition ( error restarts cc -- condition )
|
2006-11-30 02:15:42 -05:00
|
|
|
[ set-condition-continuation ] keep
|
2006-05-24 04:29:25 -04:00
|
|
|
[ set-condition-restarts ] keep
|
|
|
|
[ set-delegate ] keep ;
|
|
|
|
|
2006-05-24 18:42:30 -04:00
|
|
|
: condition ( error restarts -- restart )
|
2006-05-24 04:29:25 -04:00
|
|
|
[ <condition> throw ] callcc1 2nip ;
|
|
|
|
|
2006-11-30 02:15:42 -05:00
|
|
|
TUPLE: restart name obj continuation ;
|
|
|
|
|
|
|
|
: restart ( restart -- )
|
|
|
|
dup restart-obj swap restart-continuation continue-with ;
|
|
|
|
|
2006-08-15 04:57:12 -04:00
|
|
|
GENERIC: compute-restarts ( error -- seq )
|
2006-05-24 04:29:25 -04:00
|
|
|
|
|
|
|
M: object compute-restarts drop { } ;
|
|
|
|
|
|
|
|
M: tuple compute-restarts delegate compute-restarts ;
|
|
|
|
|
|
|
|
M: condition compute-restarts
|
|
|
|
[ delegate compute-restarts ] keep
|
2006-11-30 02:15:42 -05:00
|
|
|
[ condition-continuation ] keep
|
|
|
|
condition-restarts [ first2 rot <restart> ] map-with
|
|
|
|
append ;
|
2006-08-02 15:17:13 -04:00
|
|
|
|
2006-11-15 22:57:58 -05:00
|
|
|
PREDICATE: array kernel-error ( obj -- ? )
|
2006-12-12 15:39:19 -05:00
|
|
|
dup first \ kernel-error eq? [
|
2006-12-14 02:35:22 -05:00
|
|
|
second 0 19 between?
|
2006-12-12 15:39:19 -05:00
|
|
|
] [
|
|
|
|
drop f
|
|
|
|
] if ;
|
2006-11-15 22:57:58 -05:00
|
|
|
|
2006-12-07 22:53:50 -05:00
|
|
|
TUPLE: assert got expect ;
|
|
|
|
|
|
|
|
: assert ( got expect -- * ) <assert> throw ;
|
|
|
|
|
|
|
|
: assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
|
|
|
|
|
|
|
|
: assert-depth ( quot -- ) depth slip depth swap assert= ;
|
|
|
|
|
2006-08-02 15:17:13 -04:00
|
|
|
DEFER: try
|