The Lua documentation does not seem to specify whether the pointer passed to lua_CFunction is guaranteed to point to and call the function. A pointer to the exact same lua_State object (directly pointing to the function directly through lua_cpcall() or calling C Lua code).
A small test shows that the pointer refers to the same object, but I don’t know if this Guaranteed.
#include
#include
#include
static lua_State *state;
static int test_fn(lua_State *L)
{
printf("global:%p local :%p ", state, L);
return 0;
}
int main(int argc, char const **argv)
{< br /> state = luaL_newstate();
luaL_openlibs(state);
lua_register(state, "test_fn", test_fn);
luaL_dostring(state, " test_fn()");
lua_close(state);
}
Sample output:
global: 0x87ef008 local:0x87ef008
I know that Lua provides a function with a stack that only contains the parameters of the function, which makes me a little uneasy, maybe this can be achieved by passing the function to the different state pointers that track the independent stack To achieve. It can also be implemented in any other way, no different lua_State is needed-in fact I hope it does work in this way- But the documentation doesn’t seem to clearly say any way.
Are these pointers guaranteed to be equal? If not, under what circumstances might it be different?
If your Lua code does not use coroutines, there is only one thread, so you will always get the global state.
Context: I’m working on a set of bindings, and many of my functions refer to the “global” Lua state. (It’s not actually global in the code I’m developing, but it’s local to a specific runtime instance, so global, because all All callback functions can access the shared state.)
The Lua documentation does not seem to specify whether the pointer passed to lua_CFunction is guaranteed to be a pointer to the lua_State object that is exactly the same as the calling function (directly through lua_cpcall() Or indirectly point to the function by calling the Lua code that calls C).
A small test shows that the pointer refers to the same object, but I don’t know if this is guaranteed.
#include
#include
#include
static lua_State *state;
static int test_fn(lua_State *L)
{
printf("global:%p local:%p ", state, L);< br /> return 0;
}
int main(int argc, char const **argv)
{
state = luaL_newstate();
luaL_openlibs(state);
lua_register(state, "test_fn", test_fn);
luaL_dostring(state, "test_fn()");
lua_close(state);
}
Sample output:
global:0x87ef 008 local:0x87ef008
I know that Lua provides a function with a stack containing only the parameters of the function, which makes me a little uneasy. Maybe this can be done by passing the function to a different state pointer that tracks the independent stack. It can also be implemented in any other way, without the need for a different lua_State-in fact I hope it does work this way-but the documentation doesn’t seem to clearly say either way.
These pointers Is it guaranteed to be equal? If not, under what circumstances might it be different?
If you call a C function from Lua code, then the lua_State parameter will be the Lua thread that called the Lua function.
If you The Lua code does not use coroutines, there is only one thread, so you will always get the global state.