[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: What is the second argument of lua_newstate(lua_Alloc f, void *ud) used for?
- From: 孙世龙 sunshilong <sunshilong369@...>
- Date: Tue, 20 Oct 2020 15:58:50 +0800
Hi, list
What is the second argument of lua_newstate(lua_Alloc f, void *ud) used for?
As per the manual of
Lua(https://www.lua.org/manual/5.3/manual.html#lua_Alloc), which says
that:
The second argument, ud, is an opaque pointer that Lua passes to the
allocator in every call.
The second argument is NULL when lua_newsate() is called by
luaL_newstate() which is invoked by main() function.
For your reference, here is the related code snippet:
LUALIB_API lua_State *luaL_newstate (void) {
lua_State *L = lua_newstate(l_alloc, NULL);
if (L) {
int *warnstate; /* space for warning state */
lua_atpanic(L, &panic);
warnstate = (int *)lua_newuserdatauv(L, sizeof(int), 0);
luaL_ref(L, LUA_REGISTRYINDEX); /* make sure it won't be collected */
*warnstate = 0; /* default is warnings off */
lua_setwarnf(L, warnf, warnstate);
}
return L;
}
I am a little confused that when should I pass a valid pointer to
lua_newstate() as a second argument?
What is it used for?
I would be grateful to have some help with this question.
It would better if you can give me some simple examples.
Best regards
Sunshilong