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

ε祈祈猫儿з 提交于 2019-12-07 04:37:16

问题


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.


回答1:


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.




回答2:


You could try using the classic command line utility expect.

expect replays a script of text-based interactions, watching for expected responses. Of course this requires that you author an expect script.

However, if you install expect from a package manager (like MacPorts), then it will also install the command autoexpect, which can watch your keystrokes and generate an expect script automatically.

For instance, you could do:

  autoexpect -f myscript swift

then interact on the repl, and quit when you're done. Then later you could do

  ./myscript

and it would replay that session.

The problem? autoexpect will generate a script that includes your final command of closing the REPL. I'm not sure if there's a way to run the script so that it omits that command, and passes control over to you.

This seems like exactly the sort of thing these utilities were originally designed for, so I would be surprised if the functionality is not already there.

Other similar commands are script and interact.



来源:https://stackoverflow.com/questions/27872764/swift-repl-how-to-save-load-the-repl-state-a-k-a-suspend-resume-snapshot-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!