|
From: Toby A. <tob...@pe...> - 2004-03-03 19:48:57
|
Attached is a patch that allows one to serialize a closure (or most
other objects) and read it back in a different process.
Given the following definitions:
(import "java.io.*")
(define (serialize filename x)
(let* ((fos (FileOutputStream. filename))
(oos (ObjectOutputStream. fos)))
(.writeObject oos x)
(.close oos)))
(define (deserialize filename)
(let* ((fis (FileInputStream. filename))
(ois (ObjectInputStream. fis))
(x (.readObject ois)))
(.close ois)
x))
one can do the following:
JScheme 6.2 1/22/2004 http://jscheme.sourceforge.net
> (define qux 42)
42
> (define (baz) qux)
(lambda baz ()...)
> (define (foo) baz)
(lambda foo ()...)
> (foo)
(lambda baz ()...)
> ((foo))
42
> (serialize "foo.ser" foo)
#null
>
Process scheme finished
JScheme 6.2 1/22/2004 http://jscheme.sourceforge.net
> (define bar (deserialize "foo.ser"))
(lambda foo ()...)
> (bar)
(lambda baz ()...)
> ((bar))
42
> baz
(lambda baz ()...)
> qux
42
> (set! qux 20)
20
> ((bar))
20
>
Process scheme finished
JScheme 6.2 1/22/2004 http://jscheme.sourceforge.net
> (define qux 10)
10
> (define bar (deserialize "foo.ser"))
(lambda foo ()...)
> ((bar))
10
What happens is that any free references in the closure have their
values snapshotted on serialization and these snapshot values are used
to reestablish the top level bindings if none exist on deserialization.
If the appropriate binding do exists then the free references are hooked
up as I would expect (but you may not).
Anyway, I thought this was pretty cool. Is there any interest in
including this patch in the official sources?
Toby.
|