add optional argument to weak-box-value' and ephemeron-value'

This commit is contained in:
Matthew Flatt 2011-08-26 06:24:34 -06:00
parent b0245395b7
commit 290fe066b6
4 changed files with 26 additions and 11 deletions

View File

@ -23,12 +23,12 @@ where the value can be replaced by @racket[#f] (see
Returns a new weak box that initially contains @racket[v].}
@defproc[(weak-box-value [weak-box weak-box?]) any]{
@defproc[(weak-box-value [weak-box weak-box?] [gced-v any/c #f]) any/c]{
Returns the value contained in @racket[weak-box]. If the garbage
collector has proven that the previous content value of
@racket[weak-box] was reachable only through a weak reference, then
@racket[#f] is returned.}
@racket[gced-v] (which defaults to @racket[#f]) is returned.}
@defproc[(weak-box? [v any/c]) boolean?]{
@ -82,11 +82,11 @@ Returns a new @tech{ephemeron} whose key is @racket[key] and whose
value is initially @racket[v].}
@defproc[(ephemeron-value [ephemeron ephemeron?]) any]{
@defproc[(ephemeron-value [ephemeron ephemeron?] [gced-v any/c #f]) any/c]{
Returns the value contained in @racket[ephemeron]. If the garbage
collector has proven that the key for @racket[ephemeron] is only
weakly reachable, then the result is @racket[#f].}
weakly reachable, then the result is @racket[gced-v] (which defaults to @racket[#f]).}
@defproc[(ephemeron? [v any/c]) boolean?]{

View File

@ -184,15 +184,15 @@
(define b (box 5))
(test 5 unbox b)
(when set-box!
(set-box! b 6)
(test 6 unbox b))
(set-box! b 6)
(test 6 unbox b))
(test #t box? b)
(test #f box? 5)
(arity-test box 1 1)
(arity-test unbox 1 1)
(arity-test box? 1 1)
(when set-box!
(arity-test set-box! 2 2))
(arity-test set-box! 2 2))
(err/rt-test (unbox 8))
(when set-box!
(err/rt-test (set-box! 8 8))))

View File

@ -165,6 +165,21 @@
;; ensure that `something' is retained:
(test #t symbol? (list-ref (list something) (random 1))))
;; ----------------------------------------
;; `weak-box-value' and `ephemeron-value' optional result:
(let ()
(define stuff
(for/list ([n 100])
(cons (make-weak-box (gensym))
(make-ephemeron (gensym) 10))))
(collect-garbage)
(define n (for/fold ([n 0]) ([p stuff])
(+ n
(or (weak-box-value (car p) 0) 1)
(or (ephemeron-value (cdr p) 0) 1))))
(test #t < n 50))
;; ----------------------------------------
(report-errs)

View File

@ -641,7 +641,7 @@ scheme_init_list (Scheme_Env *env)
scheme_add_global_constant("weak-box-value",
scheme_make_immed_prim(weak_box_value,
"weak-box-value",
1, 1),
1, 2),
env);
scheme_add_global_constant("weak-box?",
scheme_make_folding_prim(weak_boxp,
@ -657,7 +657,7 @@ scheme_init_list (Scheme_Env *env)
scheme_add_global_constant("ephemeron-value",
scheme_make_immed_prim(ephemeron_value,
"ephemeron-value",
1, 1),
1, 2),
env);
scheme_add_global_constant("ephemeron?",
scheme_make_folding_prim(ephemeronp,
@ -3160,7 +3160,7 @@ static Scheme_Object *weak_box_value(int argc, Scheme_Object *argv[])
o = SCHEME_BOX_VAL(argv[0]);
if (!o)
return scheme_false;
return ((argc > 1) ? argv[1] : scheme_false);
else
return o;
}
@ -3414,7 +3414,7 @@ static Scheme_Object *ephemeron_value(int argc, Scheme_Object **argv)
v = scheme_ephemeron_value(argv[0]);
if (!v)
return scheme_false;
return ((argc > 1) ? argv[1] : scheme_false);
else
return v;
}