lua-users home
lua-l archive

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

On Fri, Apr 23, 2021 at 12:10 PM 孙世龙 sunshilong <> wrote:

> How to exchange the values of a specific variable (e.g: maybe a table)
> between Lua_States? Is there a reliable library could achieve this?

Generally, you cannot. With constraints on values, perhaps.

> What aspects should I consider?

Userdata cannot be serialized (generally), and cannot be safely used
except in their original Lua state. This should be pretty obvious when
considering Lua states within different processes, but this is also
true of Lua states within one process. Exceptionally, if only
particular kinds of userdata are allowed, which are designed for this
kind of exchange.

Most probably the same is true of Lua's coroutines/threads.

C and Lua functions probably could be exchanged between Lua states in
the same process, if their upvalues are given due treatment and are
otherwise within the imposed constraints. C functions cannot
(generally) be exchanged between multiple processes, Lua functions
probably can, if they run on exactly the same architecture and use the
same Lua version (and preferably exactly the same Lua

Tables, when their values are within the imposed constraints, could
still be tricky because of metatables, and the metatables, and
metamethods must also be within the constraints. Obviously this must
be recursively true, and due attention must be given to cycles in
tables and metatables, including, for example,
table/metatabe/metamethod/upvalue cycles. It is not impossible to
serialize cyclic structures loselessly, but is certainly not
completely trivial.

I may have forgotten something.

Essentially, it is only nil, Boolean, number and string values, and
tables of these without metatables, that can be exchanged easily,
everything else is either impossible or arcane or some weirdness
waiting to happen.