Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift REPL: how to save/load the REPL state? (a.k.a. suspend/resume, snapshot, clone)

In the Swift REPL, what is a way to preserve the REPL state?

  • For example, I want to do a bunch of work in the REPL, then save it, so I can load it later.

  • This concept might be named save/load, suspend/resume, snapshot/clone, serialize/deserialize, etc.

Any solution that gets me toward this will help, even if it's a hack like these:

  • Record all the history lines, then replay them in another REPL.

  • Serialize all the objects, then deserialize them in another REPL.

  • Snapshot the RAM or VM, then clone it to another machine.

  • Save a core image of the global state, then execute it later.

My goal is to save REPL work on one machine, then load it on another machine.

I only need the final state; I don't need stacks, or history, or handles, etc.

The XCode Playgrounds have a similar feature, using "Save", which externalizes content.

like image 233
joelparkerhenderson Avatar asked Sep 29 '22 01:09

joelparkerhenderson


1 Answers

Maybe this could help you a bit.

Just found out actually Swift REPL save current session in a file.

Type __FILE__ in REPL, you will see the session file.

  1> __FILE__
$R0: String = "/var/folders/6j/xs_0g88d55dgprjrwdws898w0000gn/T/lldb/3869/repl1.swift"

You can view the file content, it keeps tracking current REPL session. I am sure you can build an one line Swift code to copy that file into your save folder, which you have to run at end of your session.

BTW, in that temp folder, the repl.swift is actually more compact than repl1.swift. You probably want to copy repl.swift.

like image 76
huocp Avatar answered Nov 15 '22 09:11

huocp