/* ** open parts of the state that may cause memory-allocation errors */ static void f_luaopen (lua_State *L, void *ud) { global_State *g = G(L); UNUSED(ud); stack_init(L, L); /* init stack */ init_registry(L, g); #if LUA_FASTREF_SUPPORT { TValue n; sethvalue(L, &G(L)->l_refs, luaH_new(L, 1, 0)); /* refs */ setnvalue(&n, 0); setobj2t(L, luaH_setnum(L, hvalue(&G(L)->l_refs), LUA_RIDX_FASTREF_FREELIST), &n); setnilvalue(&g->fastrefNilValue); } #endif /* LUA_FASTREF_SUPPORT */ luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ luaT_init(L); luaX_init(L); /* pre-create memory-error message */ g->memerrmsg = luaS_newliteral(L, MEMERRMSG); luaS_fix(g->memerrmsg); /* it should never be collected */ g->gcrunning = 1; /* allow gc */ g->version = lua_version(NULL); luai_userstateopen(L); }
LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { int i; lua_State *L; global_State *g; void *l = (*f)(ud, NULL, 0, state_size(LG)); if (l == NULL) return NULL; L = tostate(l); g = &((LG *)L)->g; L->next = NULL; L->tt = LUA_TTHREAD; g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); L->marked = luaC_white(g); set2bits(L->marked, FIXEDBIT, SFIXEDBIT); preinit_state(L, g); g->frealloc = f; g->ud = ud; g->mainthread = L; g->uvhead.u.l.prev = &g->uvhead; g->uvhead.u.l.next = &g->uvhead; g->GCthreshold = 0; /* mark it as unfinished state */ g->strt.size = 0; g->strt.nuse = 0; g->strt.hash = NULL; setnilvalue(registry(L)); luaZ_initbuffer(L, &g->buff); g->panic = NULL; g->gcstate = GCSpause; g->rootgc = obj2gco(L); g->sweepstrgc = 0; g->sweepgc = &g->rootgc; g->gray = NULL; g->grayagain = NULL; g->weak = NULL; g->tmudata = NULL; g->totalbytes = sizeof(LG); g->gcpause = LUAI_GCPAUSE; g->gcstepmul = LUAI_GCMUL; g->gcdept = 0; for (i=0; i<NUM_TAGS; i++) g->mt[i] = NULL; if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) { /* memory allocation error: free partial state */ close_state(L); L = NULL; } else luai_userstateopen(L); /* SPRING additions, default to disabled functions */ g->fopen_func = NULL; g->popen_func = NULL; g->pclose_func = NULL; g->system_func = NULL; g->remove_func = NULL; g->rename_func = NULL; return L; }
/* ** open parts of the state that may cause memory-allocation errors. ** ('g->version' != NULL flags that the state was completely build) */ static void f_luaopen (lua_State *L, void *ud) { global_State *g = G(L); UNUSED(ud); stack_init(L, L); /* init stack */ init_registry(L, g); luaS_init(L); luaT_init(L); luaX_init(L); g->gcrunning = 1; /* allow gc */ g->version = lua_version(NULL); luai_userstateopen(L); }
LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { int i; lua_State *L; global_State *g; /* cast -> llimits.h line 93 是一个宏*/ /* f -> lauxlib.c l_alloc */ LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG))); /* -> LG *l = (LG *)((*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));*/ if (l == NULL) return NULL; L = &l->l.l; g = &l->g; L->next = NULL; L->tt = LUA_TTHREAD; g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); L->marked = luaC_white(g); g->gckind = KGC_NORMAL; preinit_state(L, g); g->frealloc = f; g->ud = ud; g->mainthread = L; g->seed = makeseed(L); g->uvhead.u.l.prev = &g->uvhead; g->uvhead.u.l.next = &g->uvhead; g->gcrunning = 0; /* no GC while building state */ g->GCestimate = 0; g->strt.size = 0; g->strt.nuse = 0; g->strt.hash = NULL; setnilvalue(&g->l_registry); luaZ_initbuffer(L, &g->buff); g->panic = NULL; g->version = lua_version(NULL); g->gcstate = GCSpause; g->allgc = NULL; g->finobj = NULL; g->tobefnz = NULL; g->sweepgc = g->sweepfin = NULL; g->gray = g->grayagain = NULL; g->weak = g->ephemeron = g->allweak = NULL; g->totalbytes = sizeof(LG); g->GCdebt = 0; g->gcpause = LUAI_GCPAUSE; g->gcmajorinc = LUAI_GCMAJOR; g->gcstepmul = LUAI_GCMUL; for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { /* memory allocation error: free partial state */ close_state(L); L = NULL; } else luai_userstateopen(L); return L; }
/* ** open parts of the state that may cause memory-allocation errors */ static void f_luaopen (lua_State *L, void *ud) { global_State *g = G(L); UNUSED(ud); stack_init(L, L); /* init stack */ init_registry(L, g); luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ luaT_init(L); luaX_init(L); /* pre-create memory-error message */ g->memerrmsg = luaS_newliteral(L, MEMERRMSG); luaS_fix(g->memerrmsg); /* it should never be collected */ g->gcrunning = 1; /* allow gc */ g->version = lua_version(NULL); luai_userstateopen(L); }
/* ** open parts of the state that may cause memory-allocation errors. ** ('g->version' != NULL flags that the state was completely build) */ static void f_luaopen (lua_State *L, void *ud) { global_State *g = G(L); UNUSED(ud); stack_init(L, L); /* init stack */ init_registry(L, g); #if LUA_FASTREF_SUPPORT { TValue n; sethvalue(L, &G(L)->l_refs, luaH_new(L)); /* refs */ setivalue(&n, 0); luaH_setint(L, hvalue(&G(L)->l_refs), LUA_RIDX_FASTREF_FREELIST, &n); setnilvalue(&g->fastrefNilValue); } #endif /* LUA_FASTREF_SUPPORT */ luaS_init(L); luaT_init(L); luaX_init(L); g->gcrunning = 1; /* allow gc */ g->version = lua_version(NULL); luai_userstateopen(L); }
LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { int i; lua_State *L; global_State *g; #if LUAPLUS_EXTENSIONS void *l = (*f)(ud, NULL, 0, state_size(LG), "lua_State", 0); #else void *l = (*f)(ud, NULL, 0, state_size(LG)); #endif /* LUAPLUS_EXTENSIONS */ if (l == NULL) return NULL; L = tostate(l); g = &((LG *)L)->g; L->next = NULL; #if LUA_REFCOUNT L->prev = NULL; #endif /* LUA_REFCOUNT */ L->tt = LUA_TTHREAD; g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); L->marked = luaC_white(g); set2bits(L->marked, FIXEDBIT, SFIXEDBIT); preinit_state(L, g); g->frealloc = f; g->ud = ud; g->mainthread = L; g->uvhead.u.l.prev = &g->uvhead; g->uvhead.u.l.next = &g->uvhead; g->GCthreshold = 0; /* mark it as unfinished state */ g->strt.size = 0; g->strt.nuse = 0; g->strt.hash = NULL; #if LUA_REFCOUNT setnilvalue2n(L, registry(L)); #else setnilvalue(registry(L)); #endif /* LUA_REFCOUNT */ luaZ_initbuffer(L, &g->buff); g->panic = NULL; g->gcstate = GCSpause; g->rootgc = obj2gco(L); g->sweepstrgc = 0; g->sweepgc = &g->rootgc; g->gray = NULL; g->grayagain = NULL; g->weak = NULL; g->tmudata = NULL; g->totalbytes = sizeof(LG); g->gcpause = LUAI_GCPAUSE; g->gcstepmul = LUAI_GCMUL; g->gcdept = 0; #if LUAPLUS_EXTENSIONS g->loadNotifyFunction = NULL; g->userGCFunction = NULL; g->gchead_next = &g->gctail_next; g->gchead_prev = NULL; g->gctail_next = NULL; g->gctail_prev = &g->gchead_next; #endif /* LUAPLUS_EXTENSIONS */ for (i=0; i<NUM_TAGS; i++) g->mt[i] = NULL; if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) { /* memory allocation error: free partial state */ close_state(L); L = NULL; } else luai_userstateopen(L); return L; }