~ chicken-core (master) d63e474b80ec73d1adfc9a07e7d96b8f277b842e
commit d63e474b80ec73d1adfc9a07e7d96b8f277b842e
Author: felix <felix@call-with-current-continuation.org>
AuthorDate: Sat Oct 3 16:35:07 2026 +0200
Commit: felix <felix@call-with-current-continuation.org>
CommitDate: Sat Oct 3 16:35:07 2026 +0200
unify native and r7rs exception handling
- ##sys#current-exception-handler is now a list
- R7RS exn operations use the very same instead of their own exn-stack
- fix semantics, avoid looping in nested "guard" forms (reported by
Daniel Colascione)
Many thanks to Kristian Lein-Mathisen for investigating the looping
problem and providing a first draft solution.
diff --git a/chicken-syntax.scm b/chicken-syntax.scm
index e47c4ca6..8bf30fba 100644
--- a/chicken-syntax.scm
+++ b/chicken-syntax.scm
@@ -1250,7 +1250,7 @@
(guard-aux reraise (car more) (cdr more)))))))
`((scheme#call-with-current-continuation
(,%lambda (,%guard-k)
- (scheme#with-exception-handler
+ (chicken.condition#with-exception-handler
(,%lambda (,%condition)
((scheme#call-with-current-continuation
(,%lambda (,%handler-k)
diff --git a/library.scm b/library.scm
index c4455b14..3d8af3eb 100644
--- a/library.scm
+++ b/library.scm
@@ -6413,17 +6413,21 @@ EOF
(string-append msg " - " strerror) args))))
(define (abort x)
- (##sys#current-exception-handler x)
- (abort
- (##sys#make-structure
- 'condition
- '(exn)
- (list '(exn . message) "exception handler returned"
- '(exn . arguments) '()
- '(exn . location) #f) ) ) )
+ (let ((h (car ##sys#current-exception-handler)))
+ (set! ##sys#current-exception-handler (cdr ##sys#current-exception-handler))
+ (h x)
+ (abort
+ (##sys#make-structure
+ 'condition
+ '(exn)
+ (list '(exn . message) "exception handler returned"
+ '(exn . arguments) '()
+ '(exn . location) #f) ) ) ) )
(define (signal x)
- (##sys#current-exception-handler x) )
+ (let ((h (car ##sys#current-exception-handler)))
+ (set! ##sys#current-exception-handler (cdr ##sys#current-exception-handler))
+ (h x) ) )
(define ##sys#error-handler
(make-parameter
@@ -6463,7 +6467,7 @@ EOF
(define ##sys#current-exception-handler
;; Exception-handler for the primordial thread:
(let ((string-append string-append))
- (lambda (c)
+ (define (h c)
(when (##sys#structure? c 'condition)
(set! ##sys#last-exception c)
(let ((kinds (##sys#slot c 1)))
@@ -6502,23 +6506,27 @@ EOF
(##sys#make-structure
'condition
'(uncaught-exception)
- (list '(uncaught-exception . reason) c)) ) ) ) )
+ (list '(uncaught-exception . reason) c)) ))
+ (let ((eh (list h)))
+ (##sys#setslot eh 1 eh)
+ eh)))
(define (with-exception-handler handler thunk)
- (let ([oldh ##sys#current-exception-handler])
+ (let ((old ##sys#current-exception-handler))
(##sys#dynamic-wind
- (lambda () (set! ##sys#current-exception-handler handler))
+ (lambda ()
+ (set! ##sys#current-exception-handler (cons handler old)))
thunk
- (lambda () (set! ##sys#current-exception-handler oldh)) ) ) )
+ (lambda ()
+ (set! ##sys#current-exception-handler old)) ) ) )
-;; TODO: Make this a proper parameter
(define (current-exception-handler . args)
(if (null? args)
- ##sys#current-exception-handler
+ (car ##sys#current-exception-handler)
(let ((proc (car args)))
(##sys#check-closure proc 'current-exception-handler)
(let-optionals (cdr args) ((convert? #t) (set? #t))
- (when set? (set! ##sys#current-exception-handler proc)))
+ (when set? (set-car! ##sys#current-exception-handler proc)))
proc)))
;;; Condition object manipulation
@@ -6773,49 +6781,14 @@ EOF
;;; R7RS exceptions
-(define ##sys#r7rs-exn-handlers
- (make-parameter
- (let ((lst (list ##sys#current-exception-handler)))
- (set-cdr! lst lst)
- lst)))
-
-(define scheme#with-exception-handler
- (let ((eh ##sys#r7rs-exn-handlers))
- (lambda (handler thunk)
- (dynamic-wind
- (lambda ()
- ;; We might be interoperating with srfi-12 handlers set by intermediate
- ;; non-R7RS code, so check if a new handler was set in the meanwhile.
- (unless (eq? (car (eh)) ##sys#current-exception-handler)
- (eh (cons ##sys#current-exception-handler (eh))))
- (eh (cons handler (eh)))
- (set! ##sys#current-exception-handler handler))
- thunk
- (lambda ()
- (eh (cdr (eh)))
- (set! ##sys#current-exception-handler (car (eh))))))))
-
-(define scheme#raise
- (let ((eh ##sys#r7rs-exn-handlers))
- (lambda (obj)
- (scheme#with-exception-handler
- (cadr (eh))
- (lambda ()
- ((cadr (eh)) obj)
- ((car (eh))
- (make-property-condition
- 'exn
- 'message "exception handler returned"
- 'arguments '()
- 'location #f)))))))
-
-(define scheme#raise-continuable
- (let ((eh ##sys#r7rs-exn-handlers))
- (lambda (obj)
- (scheme#with-exception-handler
- (cadr (eh))
- (lambda ()
- ((cadr (eh)) obj))))))
+(define (scheme#raise-continuable obj)
+ (let ((h (car ##sys#current-exception-handler)))
+ (dynamic-wind
+ (lambda ()
+ (set! ##sys#current-exception-handler (cdr ##sys#current-exception-handler)))
+ (lambda () (h obj))
+ (lambda ()
+ (set! ##sys#current-exception-handler (cons h ##sys#current-exception-handler))))))
(define scheme#error-object? condition?)
(define scheme#error-object-message (condition-property-accessor 'exn 'message))
@@ -7011,7 +6984,7 @@ EOF
##sys#standard-input
##sys#standard-output
##sys#standard-error
- ##sys#default-exception-handler
+ (list ##sys#default-exception-handler)
(##sys#vector-resize ##sys#current-parameter-vector
(##sys#size ##sys#current-parameter-vector) #f) )
name ; #6 name
diff --git a/modules.scm b/modules.scm
index e22f36f5..f229281c 100644
--- a/modules.scm
+++ b/modules.scm
@@ -1273,8 +1273,9 @@
(open-input-bytevector . scheme#open-input-bytevector)
(get-output-string . scheme#get-output-string)
(get-output-bytevector . scheme#get-output-bytevector)
- (with-exception-handler . scheme#with-exception-handler)
- (raise . scheme#raise) (raise-continuable . scheme#raise-continuable)
+ (with-exception-handler . chicken.condition#with-exception-handler)
+ (raise . chicken.condition#abort)
+ (raise-continuable . scheme#raise-continuable)
(error . chicken.base#error)
(file-error? . scheme#file-error?)
(read-error? . scheme#read-error?)
diff --git a/tests/r7rs-tests-2.scm b/tests/r7rs-tests-2.scm
index 7648ec1a..157aad92 100644
--- a/tests/r7rs-tests-2.scm
+++ b/tests/r7rs-tests-2.scm
@@ -660,12 +660,25 @@
(lambda () (+ 1 (raise 'an-error)))))))
(test-error "with-exception-handler (return)"
(with-exception-handler
- (lambda (e) 'ignore)
+ (lambda (e) 'ignore) ;; <-- should raise "handler returned"
(lambda () (+ 1 (raise 'an-error)))))
(test-error "with-exception-handler (raise)"
(with-exception-handler
(lambda (e) (raise 'another-error))
(lambda () (+ 1 (raise 'an-error)))))
+ (test "with-exception-handler (raise nested)"
+ '(caught outer inner boom)
+ (call/cc
+ (lambda (return)
+ (with-exception-handler
+ (lambda (e) (return (cons 'caught e)))
+ (lambda ()
+ (with-exception-handler
+ (lambda (e) (raise (cons 'outer e)))
+ (lambda ()
+ (with-exception-handler
+ (lambda (e) (raise (cons 'inner e)))
+ (lambda () (raise '(boom)))))))))))
(test "with-exception-handler (raise-continuable)"
'("should be a number" 65)
(let* ((exception-object #f)
@@ -674,6 +687,27 @@
(lambda (e) (set! exception-object e) 42)
(lambda () (+ (raise-continuable "should be a number") 23)))))
(list exception-object return-value)))
+ (test "with-exception-handler (raise-continuable nested)"
+ '(top outer inner boom)
+ (with-exception-handler
+ (lambda (e) (cons 'top e))
+ (lambda ()
+ (with-exception-handler
+ (lambda (e) (raise-continuable (cons 'outer e)))
+ (lambda ()
+ (with-exception-handler
+ (lambda (e) (raise-continuable (cons 'inner e)))
+ (lambda () (raise-continuable '(boom)))))))))
+ (test "with-exception-handler (raise-continuable returning)"
+ '(caught 4 caught 3 caught 2 caught 1)
+ (with-exception-handler
+ (lambda (e) (cons 'caught e))
+ (lambda ()
+ (let* ((a (raise-continuable (cons 1 '())))
+ (a (raise-continuable (cons 2 a)))
+ (a (raise-continuable (cons 3 a)))
+ (a (raise-continuable (cons 4 a))))
+ a))))
(test "error-object? (#f)" #f (error-object? 'no))
(test "error-object? (#t)" #t (error-object? (catch (car '()))))
(test "error-object-message" "fubar" (error-object-message (catch (error "fubar"))))
@@ -695,7 +729,13 @@
(guard (condition
((assq 'a condition) => cdr)
((assq 'b condition)))
- (raise '((b . 23))))))
+ (raise '((b . 23)))))
+ (test "guard (nested)" ;; by Daniel Colascione
+ 'caught
+ (guard (catcher (else 'caught))
+ (guard (outer (#f #f))
+ (guard (inner (#f #f))
+ (raise 'boom))))))
;; call-with-port is not supposed to close its port when leaving the
;; dynamic extent, only on normal return.
diff --git a/types.db b/types.db
index f7a69c9c..d6a4b097 100644
--- a/types.db
+++ b/types.db
@@ -848,8 +848,6 @@
(scheme#interaction-environment
(#(procedure #:clean) scheme#interaction-environment () (struct environment)))
-(scheme#with-exception-handler (#(procedure #:clean #:enforce) scheme#with-exception-handler ((procedure (*) . *) (procedure () . *)) . *))
-(scheme#raise (#(procedure #:clean #:enforce) scheme#raise (*) noreturn))
(scheme#raise-continuable (#(procedure #:clean #:enforce) scheme#raise-continuable (*) . *))
(scheme#error-object-irritants
(#(procedure #:clean #:enforce)
@@ -1196,11 +1194,7 @@
(chicken.condition#condition-property-accessor (#(procedure #:clean #:enforce) chicken.condition#condition-property-accessor (* * #!optional *) (procedure ((struct condition)) *)))
(chicken.condition#current-exception-handler
- (#(procedure #:clean #:enforce) chicken.condition#current-exception-handler (#!optional (procedure (*) noreturn) boolean boolean) procedure)
- ((procedure) (let ((#(tmp1) #(1)))
- (let ((#(tmp2) (set! ##sys#current-exception-handler #(tmp1))))
- #(tmp1))))
- (() ##sys#current-exception-handler))
+ (#(procedure #:clean #:enforce) chicken.condition#current-exception-handler (#!optional (procedure (*) noreturn) boolean boolean) procedure))
(chicken.condition#get-condition-property (#(procedure #:clean #:enforce) chicken.condition#get-condition-property ((struct condition) * * #!optional *) *))
(chicken.condition#make-composite-condition (#(procedure #:clean #:enforce) chicken.condition#make-composite-condition (#!rest (struct condition)) (struct condition)))
(chicken.condition#make-property-condition (#(procedure #:clean #:enforce) chicken.condition#make-property-condition (* #!rest *) (struct condition)))
Trap