Version 13 of save the whole runtime environment to a file and restore it later

Updated 2006-02-23 16:50:45

Shin The Gin - Wouldn't it be nice for an interative environment to save its complete state into a file and restore it later?

How would one do it with Tcl?

I know Tcl has a lot of introspection capabilities, but do they enable a complete saving of states?

RS: See serializing and Dumping interpreter state

NEM: See also continuation, depending on what is meant by interpreter state.

escargo: It sounds like saving and restoring a Smalltalk workspace (or also going back a ways, an APL workspace). Squeak Smalltalk [L1 ] has workspaces.

Shin The Gin escargo: Yes, that's what I had in mind. I think of it as a multistep routine.

  1. Save all variables
  2. Save all arrays
  3. Save all procedures
  4. Traverse and save the widget tree

Whereas save here means to write out tcl code, that can be sourced later.

George Peter Staplin You should be able to do essentially what Emacs does, and dump the dynamic portions of the executable image. Basically with an ELF executable you can dump the .data segment to an image file, and then restore it on the next startup. There's a little more involved, but it's definitely possible.

Lars H: This "Emacs approach" is rather crude though, GPS. Languages with poor introspection capabilities have to do it by dumping raw memory (MS Word also comes to mind), but surely Tcl can do better! (OK, sometimes speed is of the essence, but usually it's not.)

RJ I do this.. only saves variable contents. To save window state, capture widget contents to variables first, then in the restore, insert them. Same with focus, tags, etc. (Global only here. For namespaces, just add another nested level of foreach [lappend [namespace children ::] ::])

 proc save_state        {}        {
   uplevel        {
                set fh [open $statefile w]
                foreach v [info var]        {
                        if [array exists $v]        {
                                puts $fh "a $v = [array get $v]" 
                        } else {
                                puts $fh "v $v = [set $v]"
                        }
                }
                close $fh
   }
 }

 package require fileutil  
 # cuz I love this package (obv. you could open/close a file yourself)
 proc restore_state {}        {
   uplevel {
                fileutil::foreachLine l $::statefile        {
                        if {[lindex $l 1] == "type"}        {
                                continue
                        }
                        if {[lindex $l 0] == "a"}        {
                                array set [lindex $l 1] [lrange $l 3 end]
                        #        puts "setting a [lindex $l 1]"
                        } else {
                                set [lindex $l 1] [lrange $l 3 end]        
                        #        puts "setting v [lindex $l 1]"
                        }
                }
                puts "Done restoring session"
   }
 }

See also:


Category Concept