Serializable VM

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Serializable VM

Egor Skriptunoff-2
Hi!

There is an interesting SO question:
https://stackoverflow.com/questions/63365401/resuming-lua-script-using-its-c-api

OP uses Lua in his game and wants to be able to get (using Lua C API)
current Lua VM snapshot in some serializable form
for saving it inside a savegame file.
When the saved game is loaded, Lua VM resumes from the saved snapshot.
It looks like a useful feature for embeddable language.
It would also allow running a long-lasting script on one computer,
then pausing it and resuming the calculations on another computer.

The idea might be too hard to implement, but it surely worth discussing.
Reply | Threaded
Open this post in threaded view
|

Re: Serializable VM

Luiz Henrique de Figueiredo
> OP uses Lua in his game and wants to be able to get (using Lua C API)
> current Lua VM snapshot in some serializable form
> for saving it inside a savegame file.

See http://lua-users.org/lists/lua-l/2014-02/msg00552.html
Reply | Threaded
Open this post in threaded view
|

Re: Serializable VM

Egor Skriptunoff-2
On Sun, Aug 16, 2020 at 10:37 PM Luiz Henrique de Figueiredo wrote:
Reply | Threaded
Open this post in threaded view
|

Re: Serializable VM

Soni "They/Them" L.


On 2020-08-16 5:01 p.m., Egor Skriptunoff wrote:
> On Sun, Aug 16, 2020 at 10:37 PM Luiz Henrique de Figueiredo wrote:
>
>     See http://lua-users.org/lists/lua-l/2014-02/msg00552.html
>
>
>
> Can't find the lper project.

Try Eris? https://github.com/fnuecke/eris
Reply | Threaded
Open this post in threaded view
|

Re: Serializable VM

Jonathan Goble
In reply to this post by Egor Skriptunoff-2
On Sun, Aug 16, 2020 at 4:02 PM Egor Skriptunoff <[hidden email]> wrote:
On Sun, Aug 16, 2020 at 10:37 PM Luiz Henrique de Figueiredo wrote:

Looks like it was removed when the page was overhauled. The Wayback Machine seems to have it here: https://web.archive.org/web/20140718214439/http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#lper
Reply | Threaded
Open this post in threaded view
|

Re: Serializable VM

Luiz Henrique de Figueiredo
In reply to this post by Egor Skriptunoff-2
>> See http://lua-users.org/lists/lua-l/2014-02/msg00552.html
>
> Can't find the lper project.

As I said in that message, lper did not quite work and has been removed.
Reply | Threaded
Open this post in threaded view
|

Re: Serializable VM

Egor Skriptunoff-2
In reply to this post by Soni "They/Them" L.
On Sun, Aug 16, 2020 at 11:20 PM Soni "They/Them" L. wrote:
Try Eris? https://github.com/fnuecke/eris

Eris can only persist a Lua value (you must have this value in the Lua C API stack).
How to persist the whole VM with Eris?
VM is not a Lua value.


Reply | Threaded
Open this post in threaded view
|

Re: Serializable VM

Soni "They/Them" L.


On 2020-08-16 10:03 p.m., Egor Skriptunoff wrote:
> On Sun, Aug 16, 2020 at 11:20 PM Soni "They/Them" L. wrote:
>
>     Try Eris? https://github.com/fnuecke/eris
>
>
> Eris can only persist a Lua value (you must have this value in the Lua
> C API stack).
> How to persist the whole VM with Eris?
> VM is not a Lua value.

1. wrap the main coroutine.
2. that's literally it, there are no extra steps. as long as you can get
it to yield you can persist the whole thing other than the wrapper itself.
Reply | Threaded
Open this post in threaded view
|

Re: Serializable VM

Sean Conner
In reply to this post by Egor Skriptunoff-2
It was thus said that the Great Egor Skriptunoff once stated:

> Hi!
>
> There is an interesting SO question:
> https://stackoverflow.com/questions/63365401/resuming-lua-script-using-its-c-api
>
> OP uses Lua in his game and wants to be able to get (using Lua C API)
> current Lua VM snapshot in some serializable form
> for saving it inside a savegame file.
> When the saved game is loaded, Lua VM resumes from the saved snapshot.
> It looks like a useful feature for embeddable language.
> It would also allow running a long-lasting script on one computer,
> then pausing it and resuming the calculations on another computer.
>
> The idea might be too hard to implement, but it surely worth discussing.

  One way to save the Lua state is to provide a custom allocator to Lua that
always allocates from the same address range for Lua.  It would then be a
simple matter to save said address range to a file and restore it.  The
trick then would be to somehow resume the Lua VM from this saved state.

  -spc
Reply | Threaded
Open this post in threaded view
|

Re: Serializable VM

Egor Skriptunoff-2
In reply to this post by Soni "They/Them" L.
On Mon, Aug 17, 2020 at 4:06 AM Soni "They/Them" L. wrote:
as long as you can get it to yield

Can I yield from a hook?

Reply | Threaded
Open this post in threaded view
|

Re: Serializable VM

云风 Cloud Wu
Egor Skriptunoff <[hidden email]> 于2020年8月17日周一 上午10:18写道:
>
> On Mon, Aug 17, 2020 at 4:06 AM Soni "They/Them" L. wrote:
>>
>> as long as you can get it to yield
>
>
> Can I yield from a hook?
>

You can yield from a C hook function , but you can't yield from a lua
hook function.


--
http://blog.codingnow.com
Reply | Threaded
Open this post in threaded view
|

Re: Serializable VM

Soni "They/Them" L.
In reply to this post by Egor Skriptunoff-2


On 2020-08-16 11:17 p.m., Egor Skriptunoff wrote:
> On Mon, Aug 17, 2020 at 4:06 AM Soni "They/Them" L. wrote:
>
>     as long as you can get it to yield
>
>
> Can I yield from a hook?
>
You cannot. Well, you can, but you can't serialize those. You also
cannot use arbitrary libraries. You need to design your libraries around
it, and at that point you might as well add "external" yield points into
them.

I'd be extremely surprised if you could serialize arbitrary libraries.
Reply | Threaded
Open this post in threaded view
|

Re: Serializable VM

Egor Skriptunoff-2
On Mon, Aug 17, 2020 at 1:59 PM Soni "They/Them" L. wrote:
You also cannot use arbitrary libraries.
You need to design your libraries around it.

Of course, external libraries written in C should provide their own
serialize()/deserialize() methods.
Reply | Threaded
Open this post in threaded view
|

Re: Serializable VM

Stephen E. Baker
In reply to this post by Egor Skriptunoff-2
We do something like this for CorsixTH. Essentially walking the object graph from _G persisting everything. References to things already saved and calling persist / depersist metamethods to handle userdata. An overload of dofile was added so that we could restore function references correctly. A limitation is that shared upvalues become disconnected.



I have a work in progress pull request to 5.4 compatibility that cleans it up a little bit. Feel free to use it as a starting point. 

On Sun., Aug. 16, 2020, 3:23 p.m. Egor Skriptunoff, <[hidden email]> wrote:
Hi!

There is an interesting SO question:
https://stackoverflow.com/questions/63365401/resuming-lua-script-using-its-c-api

OP uses Lua in his game and wants to be able to get (using Lua C API)
current Lua VM snapshot in some serializable form
for saving it inside a savegame file.
When the saved game is loaded, Lua VM resumes from the saved snapshot.
It looks like a useful feature for embeddable language.
It would also allow running a long-lasting script on one computer,
then pausing it and resuming the calculations on another computer.

The idea might be too hard to implement, but it surely worth discussing.