Skip to main content

new_state

Function new_state 

Source
pub fn new_state() -> Option<LuaState>
Expand description

Create a new independent Lua state. Returns None only on OOM.

C: LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud) (pub)

PORT NOTE: The C API takes a custom allocator (f, ud). The Rust-native API uses the global Rust allocator; those parameters are dropped. Equivalent to LuaState::new() at the call site.

// C: LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud) {
//   int i;
//   lua_State *L;
//   global_State *g;
//   LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
//   if (l == NULL) return NULL;
//   L = &l->l.l; g = &l->g;
//   L->tt = LUA_VTHREAD;
//   g->currentwhite = bitmask(WHITE0BIT);
//   L->marked = luaC_white(g);
//   preinit_thread(L, g);
//   g->allgc = obj2gco(L);
//   L->next = NULL;
//   incnny(L);
//   g->frealloc = f; g->ud = ud; g->warnf = NULL; g->ud_warn = NULL;
//   g->mainthread = L; g->seed = luai_makeseed(L);
//   g->gcstp = GCSTPGC;
//   ... (zero-init all GC list pointers and tunables) ...
//   setivalue(&g->nilvalue, 0);  /* signal: state not yet built */
//   ... (setgcparam tunables) ...
//   for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
//   if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
//     close_state(L); L = NULL;
//   }
//   return L;
// }