From 290fe066b68def1439d47d9c2b6e2f4dd8672ba2 Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Fri, 26 Aug 2011 06:24:34 -0600 Subject: [PATCH] add optional argument to `weak-box-value' and `ephemeron-value' --- collects/scribblings/reference/memory.scrbl | 8 ++++---- collects/tests/racket/basic.rktl | 6 +++--- collects/tests/racket/will.rktl | 15 +++++++++++++++ src/racket/src/list.c | 8 ++++---- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/collects/scribblings/reference/memory.scrbl b/collects/scribblings/reference/memory.scrbl index 7ecabee4b4..6fe042fb71 100644 --- a/collects/scribblings/reference/memory.scrbl +++ b/collects/scribblings/reference/memory.scrbl @@ -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?]{ diff --git a/collects/tests/racket/basic.rktl b/collects/tests/racket/basic.rktl index b9f75d6254..f9db3be9ed 100644 --- a/collects/tests/racket/basic.rktl +++ b/collects/tests/racket/basic.rktl @@ -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)))) diff --git a/collects/tests/racket/will.rktl b/collects/tests/racket/will.rktl index c648e59476..c6ccaee984 100644 --- a/collects/tests/racket/will.rktl +++ b/collects/tests/racket/will.rktl @@ -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) diff --git a/src/racket/src/list.c b/src/racket/src/list.c index 35d59055d6..be7e3cd2dd 100644 --- a/src/racket/src/list.c +++ b/src/racket/src/list.c @@ -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; }