Is there any circumstance that needs to invoke lua_newstate () to create two or more Lua states in a single C\C++ process?

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

Is there any circumstance that needs to invoke lua_newstate () to create two or more Lua states in a single C\C++ process?

孙世龙 sunshilong
HI, list

Is there any circumstance that needs to invoke lua_newstate () to
create two or more Lua states in a single C\C++ process?
It would be better if you can give me a few simple examples.
What aspects should I consider(or be aware of) when creating two or
more Lua states in a single C\C++ process?

Best regards
Sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: Is there any circumstance that needs to invoke lua_newstate () to create two or more Lua states in a single C\C++ process?

JeanHeyd Meneide
Dear Sunshilong,

     Each Lua state is entirely self-contained, and therefore you can
make 10s or even 100s of them in one C/C++ process. The common way to
do multithreading with Lua is to create multiple lua_State's, and then
let them run independently while setting up ways to communicate
information across threads in specific ways to need it.

    Even for non-multithreaded programs, a common way to guarantee
sandboxing without hijacking tables is to create a Lua State for each
participant.

     Note if you have 2 or more Lua States, you cannot use a reference
(Table, Userdata, etc.) that came from Lua State #1 with Lua State #2
without invoking undefined behavior (and, in severe cases, a crash).

Hope that helps,
Derp

On Tue, Oct 20, 2020 at 9:50 PM 孙世龙 sunshilong <[hidden email]> wrote:

>
> HI, list
>
> Is there any circumstance that needs to invoke lua_newstate () to
> create two or more Lua states in a single C\C++ process?
> It would be better if you can give me a few simple examples.
> What aspects should I consider(or be aware of) when creating two or
> more Lua states in a single C\C++ process?
>
> Best regards
> Sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: Is there any circumstance that needs to invoke lua_newstate () to create two or more Lua states in a single C\C++ process?

孙世龙 sunshilong
Dear Derp:

Thank you for your detailed explanation.

>Even for non-multithreaded programs, a common way to guarantee
>sandboxing without hijacking tables is to create a Lua State for each
>participant.

I can't fully understand it.
What is "sandboxing without hijacking tables'?
Could you please explain that more in detail for me or suggest some
documents for me to go through?

Thank you for your attention to my question.
Best regards
Sunshilong

On Wed, Oct 21, 2020 at 9:55 AM JeanHeyd Meneide
<[hidden email]> wrote:

>
> Dear Sunshilong,
>
>      Each Lua state is entirely self-contained, and therefore you can
> make 10s or even 100s of them in one C/C++ process. The common way to
> do multithreading with Lua is to create multiple lua_State's, and then
> let them run independently while setting up ways to communicate
> information across threads in specific ways to need it.
>
>     Even for non-multithreaded programs, a common way to guarantee
> sandboxing without hijacking tables is to create a Lua State for each
> participant.
>
>      Note if you have 2 or more Lua States, you cannot use a reference
> (Table, Userdata, etc.) that came from Lua State #1 with Lua State #2
> without invoking undefined behavior (and, in severe cases, a crash).
>
> Hope that helps,
> Derp
>
> On Tue, Oct 20, 2020 at 9:50 PM 孙世龙 sunshilong <[hidden email]> wrote:
> >
> > HI, list
> >
> > Is there any circumstance that needs to invoke lua_newstate () to
> > create two or more Lua states in a single C\C++ process?
> > It would be better if you can give me a few simple examples.
> > What aspects should I consider(or be aware of) when creating two or
> > more Lua states in a single C\C++ process?
> >
> > Best regards
> > Sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: Is there any circumstance that needs to invoke lua_newstate () to create two or more Lua states in a single C\C++ process?

JeanHeyd Meneide
Dear Sunshilong,

     Sandboxing in a single Lua State is achieved by overriding the
"environment" in which a Lua function call is run. Since all scripts
are just function calls, this applies to script loading/running as
well. Some light reading on the subject (in English; sorry, I have
nothing in other languages: http://lua-users.org/wiki/SandBoxes)

     This is what I mean by "without hijacking tables"; using multiple
states means that there is no need to try to substitute a
function/script's environment table. You just transfer information
explicitly between lua_States.

     This is one of the primary reasons I see multiple Lua States in a
single threaded program. There are other use cases but those are
outside of my understanding.

Sincerely,
Derp

On Tue, Oct 20, 2020 at 11:16 PM 孙世龙 sunshilong <[hidden email]> wrote:

>
> Dear Derp:
>
> Thank you for your detailed explanation.
>
> >Even for non-multithreaded programs, a common way to guarantee
> >sandboxing without hijacking tables is to create a Lua State for each
> >participant.
>
> I can't fully understand it.
> What is "sandboxing without hijacking tables'?
> Could you please explain that more in detail for me or suggest some
> documents for me to go through?
>
> Thank you for your attention to my question.
> Best regards
> Sunshilong
>
> On Wed, Oct 21, 2020 at 9:55 AM JeanHeyd Meneide
> <[hidden email]> wrote:
> >
> > Dear Sunshilong,
> >
> >      Each Lua state is entirely self-contained, and therefore you can
> > make 10s or even 100s of them in one C/C++ process. The common way to
> > do multithreading with Lua is to create multiple lua_State's, and then
> > let them run independently while setting up ways to communicate
> > information across threads in specific ways to need it.
> >
> >     Even for non-multithreaded programs, a common way to guarantee
> > sandboxing without hijacking tables is to create a Lua State for each
> > participant.
> >
> >      Note if you have 2 or more Lua States, you cannot use a reference
> > (Table, Userdata, etc.) that came from Lua State #1 with Lua State #2
> > without invoking undefined behavior (and, in severe cases, a crash).
> >
> > Hope that helps,
> > Derp
> >
> > On Tue, Oct 20, 2020 at 9:50 PM 孙世龙 sunshilong <[hidden email]> wrote:
> > >
> > > HI, list
> > >
> > > Is there any circumstance that needs to invoke lua_newstate () to
> > > create two or more Lua states in a single C\C++ process?
> > > It would be better if you can give me a few simple examples.
> > > What aspects should I consider(or be aware of) when creating two or
> > > more Lua states in a single C\C++ process?
> > >
> > > Best regards
> > > Sunshilong