~ chicken-core (chicken-5) 6ea929ddb2c21ad340e1403248529017352928c5


commit 6ea929ddb2c21ad340e1403248529017352928c5
Author:     Peter Bex <peter@more-magic.net>
AuthorDate: Tue Dec 6 22:12:59 2016 +0100
Commit:     Evan Hanson <evhan@foldling.org>
CommitDate: Mon Dec 12 11:22:36 2016 +1300

    Save "current" values when leaving parameterizes.
    
    These remembered parameter values are reinstated when we enter the
    dynamic extent again, instead of resetting to the original values the
    parameters had when first entering the extent, like we did in the fix
    for #1227.
    
    Signed-off-by: Evan Hanson <evhan@foldling.org>

diff --git a/NEWS b/NEWS
index 3d785825..9a68b2f6 100644
--- a/NEWS
+++ b/NEWS
@@ -69,6 +69,10 @@
 
 - Core libraries:
   - Keywords are more consistently read/written, like symbols (#1332).
+  - SRFI-39: When jumping out of a parameterized dynamic extent,
+    "parameterize" now remember the actual values, so when jumping back
+    in, they are restored (fixes #1336, thanks to Joo ChurlSoo).
+    This was a regression caused by the fix for #1227.
 
 4.11.1
 
diff --git a/chicken-syntax.scm b/chicken-syntax.scm
index b4a19a19..8b164145 100644
--- a/chicken-syntax.scm
+++ b/chicken-syntax.scm
@@ -321,9 +321,15 @@
 	     (##core#set! ,convert? #f) ) )
 	   (##core#lambda () ,@body)
 	   (##core#lambda ()
-	    ;; Restore parameters to their original, saved values
-	    ,@(map (lambda (p s) `(,p ,s #f #t))
-		   param-aliases saveds) )) ) ) ) ) )))
+	     (##core#let
+	      ;; Remember the current value of each parameter.
+	      ,(map (lambda (p s temp) `(,temp (,p)))
+		    param-aliases saveds temps)
+	      ;; Restore each parameter to its old value.
+	      ,@(map (lambda (p s) `(,p ,s #f #t)) param-aliases saveds)
+	      ;; Save current value for later re-invocations.
+	      ,@(map (lambda (s temp) `(##core#set! ,s ,temp))
+		     saveds temps)))))))))))
 
 (##sys#extend-macro-environment
  'when '()
diff --git a/tests/library-tests.scm b/tests/library-tests.scm
index b928653a..3b7fb02b 100644
--- a/tests/library-tests.scm
+++ b/tests/library-tests.scm
@@ -600,6 +600,41 @@ A
   (assert (equal? original-error (current-error-port)))
   (assert (equal? original-exception-handler (current-exception-handler))))
 
+;; Re-entering dynamic extent of a parameterize should not reset to
+;; original outer values but remember values when jumping out (another
+;; regression due to #1227, pointed out by Joo ChurlSoo in #1336).
+
+(let ((f (make-parameter 'a))
+      (path '())
+      (g (make-parameter 'g))
+      (c #f))
+  (let ((add (lambda () (set! path (cons (f) path)))))
+    (add)
+    (parameterize ((f 'b)
+		   (g (call-with-current-continuation
+		       (lambda (c0) (set! c c0) 'c))))
+      (add) (f (g)) (add))
+    (f 'd)
+    (add)
+    (if (< (length path) 8)
+	(c 'e)
+	(assert (equal? '(a b c d b e d b e d) (reverse path))))))
+
+(let ((f (make-parameter 'a))
+      (path '())
+      (g (make-parameter 'g))
+      (c #f))
+  (let ((add (lambda () (set! path (cons (f) path)))))
+    (add)
+    (parameterize ((f 'b))
+      (g (call-with-current-continuation (lambda (c0) (set! c c0) 'c)))
+      (add) (f (g)) (add))
+    (f 'd)
+    (add)
+    (if (< (length path) 8)
+	(c 'e)
+	(assert (equal? '(a b c d c e d e e d) (reverse path))))))
+
 ;;; vector and blob limits
 
 (assert-fail (make-blob -1))
Trap