lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Aaron B. once stated:
> On Wed, 19 Jul 2023 13:26:01 -0400
> Sean Conner <sean@conman.org> wrote:
> 
> > The hard
> > part will be obtaining the current stack for the coroutine as currently
> > there is no API for that, nor for creating the new call stack.  Also
> > difficult would be getting the current execution location (IP or PC if you
> > know assembly) of the coroutine being duplicated.
> 
> Implementing all this would also allow the ability to
> serialize/deserialize a coroutine, allowing it to be saved across Lua
> instances or persist across reboots, maybe even save in a key/value
> store. I have direct use cases for that ability.
> 
> 
> >   Another complication are references to tables, userdata and coroutines
> > created by the suspended coroutine.  as they are just that---references and
> > not copies.  Do they need to be copied as well?  Or are references okay? 
> > Tables can be copied but that can get quite expensive, but you are out of
> > luck for userdata values as they represent state outside of Lua.
> 
> Perhaps, keep userdata - and also tables - as references, but then add
> metamethods to define something more sensible. This especially makes
> sense in the serialization use case. IE:

  Back when I was working on this (not seriously though), if I had the
following table:

	{
	  foo = true,
	  bar = function(x)
	    -- ...
	  end,
	  output = io.stdout,
	  setmetatable = _G.setmetatable,
	}

the serialized format [1] would serialize this as [2]:

	{
	  "foo" : true,
	  "bar" : (function) [ 
		[ -- upvalues ],
		[ -- uservalues ],
		{ -- environment }, -- or as (Lua_object) "_G"
		" ... code for function "
	  ],
	  "output" : (Lua_object) "io.stdout",
	  "setmetatable" : (Lua_object) "setmetatable"
	}

  So for known userdata, they could be tagged with the name (like
io.stdout), and for others, additional tagging that both sides know about to
help serialize it.  For the function, the "... code for function ..." would
work for code written in Lua, but for C functions, again, I ended up tagging
them with a known name (for standard Lua functions like 'select' or
'error').  I didn't get far, but you can see what I did here:

	https://github.com/spc476/LuaFunctionSerialize

  -spc

[1]	I was using CBOR (http://cbor.io/) for the formating [3]

[2]	Using the textual notation from RFC-8949

[3]	https://github.com/spc476/CBOR