boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

125-gc-construction-mv.scm (1138B)


      1 ; Collection during multi-step object construction and multiple-values
      2 ; handling must preserve every in-flight reference.
      3 
      4 (define-record-type box
      5   (mk-box value next)
      6   box?
      7   (value box-value box-value-set!)
      8   (next box-next box-next-set!))
      9 
     10 (define (build n tail)
     11   (if (= n 0)
     12       tail
     13       (let ((b (mk-box n #f)))
     14         (collect-garbage)
     15         (box-next-set! b (build (- n 1) tail))
     16         b)))
     17 
     18 (define sentinel (cons 'end '()))
     19 (define chain (build 40 sentinel))
     20 (collect-garbage)
     21 
     22 (define (sum-chain b acc)
     23   (if (box? b)
     24       (sum-chain (box-next b) (+ acc (box-value b)))
     25       acc))
     26 
     27 (if (= (sum-chain chain 0) 820) 0 (sys-exit 1))
     28 (if (eq? sentinel
     29          (let loop ((b chain))
     30            (if (box? b) (loop (box-next b)) b)))
     31     0
     32     (sys-exit 2))
     33 
     34 (define producer
     35   (lambda ()
     36     (let ((a (cons 10 20))
     37           (b (bytevector 30 40)))
     38       (collect-garbage)
     39       (values a b 2))))
     40 
     41 (define answer
     42   (call-with-values producer
     43     (lambda (a b n)
     44       (+ (car a) (cdr a) (bytevector-u8-ref b 0)
     45          (bytevector-u8-ref b 1) n))))
     46 (collect-garbage)
     47 (if (= answer 102) 0 (sys-exit 3))
     48 
     49 (sys-exit 42)