[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Exit one Lua state from a (C) API function
- From: Sean Conner <sean@...>
- Date: Tue, 2 Mar 2021 03:10:33 -0500
It was thus said that the Great bel once stated:
> I need to create a Lua API function to close/exit a Lua state from a Lua
> script.
> While “os.exit” will exit the entire host application, I just want to
> “exit” one (of multiple) Lua states and return to the “lua_pcall” in C,
> while the application (and the other states) keep running.
>
> Something like this:
>
> my_C_function()
> lua_newstate
> lua_pcall -->
> Some Lua code
> Lua pcall -->
> more Lua code
> call "state exit" function -------------|
> Lua code that should not be executed |
> More Lua code that should not be executed |
> [continue here] <------------------------------|
> lua_close
It's hard to follow your code above. So let's add some state to
it---namely some Lua states:
my_C_function(L1)
L2 = lua_newstate()
lua_pcall(L? .. what lua state goes here?)
-- skip rest of code above because of lack of
-- information about which state we're in where
There also may be some confusion. There are a few functions that return a
new Lua state:
lua_State *lua_newstate(lua_Alloc,void *);
lua_State *luaL_newstate(void);
lua_State *lua_tothread(lua_State *,int);
lua_State *lua_newthread(lua_State *);
The first two create a new Lua state, without reference to any other
state. The next one, lua_tothread() is like lua_tointeger() or
lua_tostring(), returning the value from a Lua stack index. The last one at
confused me when I was first starting out. It returns a new Lua state L2
that is related to the given Lua state L---they both share the same global
space. Is this perhaps what the issue is?
Even if you are creating a separate Lua state (separate from the calling
state), then your question, or rather, example, becomes:
lua_pcall[1]
-- code code code
lua_pcall[2]
-- code code code
state_exit() -- returns to [1], skips [2]
end
-- resumed here
So aside from the Lua state weirdness, you are trying to, for lack of a
better term, skip back to the outermost pcall?
-spc