Skip to content

Commit beca0d1

Browse files
authored
Simplify $guard & $reset-protect using call-in-continuation (#904)
1 parent 0955250 commit beca0d1

File tree

2 files changed

+49
-62
lines changed

2 files changed

+49
-62
lines changed

s/7.ss

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
;;; 7.ss
22
;;; Copyright 1984-2017 Cisco Systems, Inc.
3-
;;;
3+
;;;
44
;;; Licensed under the Apache License, Version 2.0 (the "License");
55
;;; you may not use this file except in compliance with the License.
66
;;; You may obtain a copy of the License at
7-
;;;
7+
;;;
88
;;; http://www.apache.org/licenses/LICENSE-2.0
9-
;;;
9+
;;;
1010
;;; Unless required by applicable law or agreed to in writing, software
1111
;;; distributed under the License is distributed on an "AS IS" BASIS,
1212
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -466,7 +466,7 @@
466466
(define-record-type (sstats make-sstats sstats?)
467467
(nongenerative #{sstats pfwch3jd8ts96giujpitoverj-0})
468468
(sealed #t)
469-
(fields
469+
(fields
470470
(mutable cpu sstats-cpu set-sstats-cpu!)
471471
(mutable real sstats-real set-sstats-real!)
472472
(mutable bytes sstats-bytes set-sstats-bytes!)
@@ -479,7 +479,7 @@
479479
(lambda (cpu real bytes gc-count gc-cpu gc-real gc-bytes)
480480
(new cpu real bytes gc-count gc-cpu gc-real gc-bytes))))))
481481
(define exact-integer? (lambda (x) (and (integer? x) (exact? x))))
482-
(set-who! make-sstats
482+
(set-who! make-sstats
483483
(lambda (cpu real bytes gc-count gc-cpu gc-real gc-bytes)
484484
(define verify-time
485485
(lambda (name x)
@@ -616,29 +616,26 @@
616616
(unless (and (real? v) (not (negative? v)))
617617
($oops 'collect-maximum-generation-threshold-factor "~s is not a nonnegative real" v))
618618
v)))
619-
619+
620620
(define $reset-protect
621621
(lambda (body out)
622-
((call/cc
623-
(lambda (k)
624-
(parameterize ([reset-handler
622+
(call/cc
623+
(lambda (k)
624+
(parameterize ([reset-handler
625625
(lambda ()
626-
(k (lambda ()
627-
(out)
628-
((reset-handler)))))])
629-
(with-exception-handler
630-
(lambda (c)
631-
; would prefer not to burn bridges even for serious condition
632-
; if the exception is continuable, but we have no way to know
633-
; short of grubbing through the continuation
634-
(if (serious-condition? c)
635-
(k (lambda () (out) (raise c)))
636-
(raise-continuable c)))
637-
(lambda ()
638-
(call-with-values body
639-
(case-lambda
640-
[(v) (lambda () v)]
641-
[v* (lambda () (apply values v*))]))))))))))
626+
(call-in-continuation k
627+
(lambda ()
628+
(out)
629+
((reset-handler)))))])
630+
(with-exception-handler
631+
(lambda (c)
632+
; would prefer not to burn bridges even for serious condition
633+
; if the exception is continuable, but we have no way to know
634+
; short of grubbing through the continuation
635+
(if (serious-condition? c)
636+
(call-in-continuation k (lambda () (out) (raise c)))
637+
(raise-continuable c)))
638+
body))))))
642639

643640
(define exit-handler)
644641
(define reset-handler)
@@ -890,7 +887,7 @@
890887
(docollect
891888
(lambda (gct prev-allocated-after-max)
892889
(let ([max-gen? (fx= g (collect-maximum-generation))])
893-
(values
890+
(values
894891
; make gc-trip to look like we've just collected generation g
895892
; w/o also having collected generation g+1
896893
(if max-gen?
@@ -1288,7 +1285,7 @@
12881285
(condition-wait $collect-cond $tc-mutex)
12891286
(f)]))))
12901287
(critical-section
1291-
(dynamic-wind
1288+
(dynamic-wind
12921289
once
12931290
(collect-request-handler)
12941291
(lambda () (set! $collect-request-pending #f))))))))
@@ -1467,7 +1464,7 @@
14671464
(define-record-type pass-stats
14681465
(nongenerative)
14691466
(sealed #t)
1470-
(fields
1467+
(fields
14711468
(mutable calls)
14721469
(mutable cpu)
14731470
(mutable gc-cpu)
@@ -1489,7 +1486,7 @@
14891486
(set! stats-ht (make-eq-hashtable))))
14901487

14911488
(set! $enable-pass-timing (make-parameter #f))
1492-
1489+
14931490
(set-who! $pass-time
14941491
(lambda (name thunk)
14951492
(unless (symbol? name) ($oops who "~s is not a symbol" name))
@@ -1539,8 +1536,8 @@
15391536
(define (build-result namev psv)
15401537
(vector->list
15411538
(vector-map
1542-
(lambda (name ps)
1543-
(list name
1539+
(lambda (name ps)
1540+
(list name
15441541
(pass-stats-calls ps)
15451542
(pass-stats-cpu ps)
15461543
(pass-stats-gc-cpu ps)

s/exceptions.ss

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
;;; exceptions.ss
22
;;; Copyright 1984-2017 Cisco Systems, Inc.
3-
;;;
3+
;;;
44
;;; Licensed under the Apache License, Version 2.0 (the "License");
55
;;; you may not use this file except in compliance with the License.
66
;;; You may obtain a copy of the License at
7-
;;;
7+
;;;
88
;;; http://www.apache.org/licenses/LICENSE-2.0
9-
;;;
9+
;;;
1010
;;; Unless required by applicable law or agreed to in writing, software
1111
;;; distributed under the License is distributed on an "AS IS" BASIS,
1212
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -94,7 +94,7 @@ TODO:
9494
[(message-condition? c)
9595
(let ([irritants (if (irritants-condition? c) (condition-irritants c) '())])
9696
(case (and (list? irritants) (length irritants))
97-
[(0)
97+
[(0)
9898
($report-string op
9999
(and prefix? (if (warning-only? c) "warning" "exception"))
100100
(and (who-condition? c) (condition-who c))
@@ -263,37 +263,27 @@ TODO:
263263
(set-who! $guard
264264
(lambda (supply-else? guards body)
265265
(if supply-else?
266-
((call/cc
267-
(lambda (kouter)
268-
(let ([original-handler-stack ($current-handler-stack)])
269-
(with-exception-handler
270-
(lambda (arg)
271-
((call/cc
266+
(call/cc
267+
(lambda (kouter)
268+
(let ([original-handler-stack ($current-handler-stack)])
269+
(with-exception-handler
270+
(lambda (arg)
271+
(call/cc
272272
(lambda (kinner)
273-
(kouter
273+
(call-in-continuation kouter
274274
(lambda ()
275275
(guards arg
276276
(lambda ()
277-
(kinner
277+
(call-in-continuation kinner
278278
(lambda ()
279279
(parameterize ([$current-handler-stack original-handler-stack])
280-
(raise-continuable arg))))))))))))
281-
(lambda ()
282-
(call-with-values
283-
body
284-
(case-lambda
285-
[(x) (lambda () x)]
286-
[vals (lambda () (apply values vals))]))))))))
287-
((call/cc
288-
(lambda (k)
289-
(with-exception-handler
290-
(lambda (arg) (k (lambda () (guards arg))))
291-
(lambda ()
292-
(call-with-values
293-
body
294-
(case-lambda
295-
[(x) (lambda () x)]
296-
[vals (lambda () (apply values vals))]))))))))))
280+
(raise-continuable arg)))))))))))
281+
body))))
282+
(call/cc
283+
(lambda (k)
284+
(with-exception-handler
285+
(lambda (arg) (call-in-continuation k (lambda () (guards arg))))
286+
body))))))
297287
)
298288

299289
(define-syntax guard
@@ -471,7 +461,7 @@ TODO:
471461
;;; defining its child types, even though the system is compiled with
472462
;;; (eval-syntax-expanders-when) not including compile.
473463
(begin
474-
(let-syntax ([a (syntax-rules ()
464+
(let-syntax ([a (syntax-rules ()
475465
[(_ &condition) ; leave only &condition visible
476466
(define-record-type (&condition make-simple-condition simple-condition?)
477467
(nongenerative #{&condition oyb459ue1fphfx4-a}))])])
@@ -706,7 +696,7 @@ TODO:
706696
(for-each
707697
(lambda (m) (unless (string? m) ($oops who "~s is not a string" m)))
708698
messages)
709-
(error-help #f who #f
699+
(error-help #f who #f
710700
(if (null? messages) "invalid syntax" (apply string-append messages))
711701
#f (make-syntax-violation form #f))))
712702

0 commit comments

Comments
 (0)