/* simplified from lstate.c */ lua_State *lua_open (int stacksize) { lua_State *L = luaM_new(NULL, lua_State); if (L == NULL) return NULL; /* memory allocation error */ L->stack = NULL; L->strt.size = L->udt.size = 0; L->strt.nuse = L->udt.nuse = 0; L->strt.hash = NULL; L->udt.hash = NULL; L->Mbuffer = NULL; L->Mbuffsize = 0; L->rootproto = NULL; L->rootcl = NULL; L->roottable = NULL; L->TMtable = NULL; L->last_tag = -1; L->refArray = NULL; L->refSize = 0; L->refFree = NONEXT; L->nblocks = sizeof(lua_State); L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */ L->callhook = NULL; L->linehook = NULL; L->allowhooks = 1; L->errorJmp = NULL; if (stacksize == 0) stacksize = DEFAULT_STACK_SIZE; else stacksize += LUA_MINSTACK; L->gt = luaH_new(L, 10); /* table of globals */ luaS_init(L); luaX_init(L); L->GCthreshold = 2*L->nblocks; return L; }
/* ** open parts that may cause memory-allocation errors */ static void f_luaopen (lua_State *L, void *ud) { /* create a new global state */ global_State *g = luaM_new(NULL, global_State); UNUSED(ud); if (g == NULL) luaD_throw(L, LUA_ERRMEM); L->l_G = g; g->mainthread = L; g->GCthreshold = 0; /* mark it as unfinished state */ g->strt.size = 0; g->strt.nuse = 0; g->strt.hash = NULL; setnilvalue(defaultmeta(L)); setnilvalue(registry(L)); luaZ_initbuffer(L, &g->buff); g->panic = default_panic; g->rootgc = NULL; g->rootudata = NULL; g->tmudata = NULL; setnilvalue(gkey(g->dummynode)); setnilvalue(gval(g->dummynode)); g->dummynode->next = NULL; g->nblocks = sizeof(lua_State) + sizeof(global_State); stack_init(L, L); /* init stack */ /* create default meta table with a dummy table, and then close the loop */ defaultmeta(L)->tt = LUA_TTABLE; sethvalue(defaultmeta(L), luaH_new(L, 0, 0)); hvalue(defaultmeta(L))->metatable = hvalue(defaultmeta(L)); sethvalue(gt(L), luaH_new(L, 0, 4)); /* table of globals */ sethvalue(registry(L), luaH_new(L, 4, 4)); /* registry */ luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ luaT_init(L); luaX_init(L); luaS_fix(luaS_newliteral(L, MEMERRMSG)); g->GCthreshold = 4*G(L)->nblocks; }
static int f_luaopen (lua_State *L, void *ud) { #else static void f_luaopen (lua_State *L, void *ud) { #endif /* LUA_EXT_RESUMABLEVM */ global_State *g = G(L); UNUSED(ud); stack_init(L, L); /* init stack */ #if LUA_MEMORY_STATS luaM_setname(L, "lua.globals"); #endif /* LUA_MEMORY_STATS */ sethvalue(L, gt(L), luaH_new(L, 0, 2)); /* table of globals */ #if LUA_MEMORY_STATS luaM_setname(L, "lua.registry"); #endif /* LUA_MEMORY_STATS */ sethvalue(L, registry(L), luaH_new(L, 0, 2)); /* registry */ #if LUA_FASTREF_SUPPORT { TValue n; sethvalue(L, &G(L)->l_refs, luaH_new(L, 0, 2)); /* 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); luaS_fix(luaS_newliteral(L, MEMERRMSG)); g->GCthreshold = 4*g->totalbytes; #if LUA_EXT_RESUMABLEVM return 0; #endif /* LUA_EXT_RESUMABLEVM */ }
/* ** 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); }
int terra_init(lua_State * L) { terra_State * T = (terra_State*) malloc(sizeof(terra_State)); assert(T); memset(T,0,sizeof(terra_State)); //some of lua stuff expects pointers to be null on entry T->L = L; assert (T->L); lua_newtable(T->L); lua_pushlightuserdata(L, T); lua_setfield(L, -2, "__terrastate"); //reference to our T object, so that we can load it from the lua state on other API calls lua_setfield(T->L,LUA_GLOBALSINDEX,"terra"); //create global terra object terra_kindsinit(T); //initialize lua mapping from T_Kind to/from string int err = terra_loadandrunbytecodes(T->L,luaJIT_BC_strict,luaJIT_BC_strict_SIZE, "strict.lua") || terra_loadandrunbytecodes(T->L,luaJIT_BC_terralib,luaJIT_BC_terralib_SIZE, "terralib.lua"); if(err) { free(T); return err; } terra_cwrapperinit(T); lua_getfield(T->L,LUA_GLOBALSINDEX,"terra"); lua_pushcfunction(T->L,terra_luaload); lua_setfield(T->L,-2,"load"); lua_pushcfunction(T->L,terra_lualoadstring); lua_setfield(T->L,-2,"loadstring"); lua_pushcfunction(T->L,terra_lualoadfile); lua_setfield(T->L,-2,"loadfile"); lua_newtable(T->L); lua_setfield(T->L,-2,"_trees"); //to hold parser generated trees lua_pop(T->L,1); luaX_init(T); err = terra_compilerinit(T); if(err) { free(T); return err; } err = terra_cudainit(T); /* if cuda is not enabled, this does nothing */ if(err) { free(T); return err; } return 0; }
/* ** open parts 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 */ sethvalue(L, gt(L), luaH_new(L, 0, 2)); /* table of globals */ sethvalue(L, registry(L), luaH_new(L, 0, 2)); /* registry */ luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ luaT_init(L); luaX_init(L); luaS_fix(luaS_newliteral(L, MEMERRMSG)); g->GCthreshold = 4*g->totalbytes; }
/* ** 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); }
/* ** 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 */ }
/** * 完成与内存有关的初始化工作, 包括分配栈空间, 初始化注册表, 预分配 * 某些字符串资源(保留关键字等) */ static void f_luaopen (lua_State *L, void *ud) { Dlog("luaopen begin."); global_State *g = G(L); UNUSED(ud); stack_init(L, L); /* init stack */ init_registry(L, g); Dlog("ini size of string table with size %d", MINSTRTABSIZE); 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); luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ g->gcrunning = 1; /* allow gc */ g->version = lua_version(NULL); luai_userstateopen(L); Dlog("luaopen end."); }
/* ** open parts that may cause memory-allocation errors */ static void f_luaopen (lua_State *L, void *ud) { int stacksize = *(int *)ud; if (stacksize == 0) stacksize = DEFAULT_STACK_SIZE; else stacksize += LUA_MINSTACK; L->gt = luaH_new(L, 10); /* table of globals */ luaD_init(L, stacksize); luaS_init(L); luaX_init(L); luaT_init(L); lua_newtable(L); lua_ref(L, 1); /* create registry */ lua_register(L, LUA_ERRORMESSAGE, errormessage); #ifdef LUA_DEBUG luaB_opentests(L); if (lua_state == NULL) lua_state = L; /* keep first state to be opened */ #endif LUA_ASSERT(lua_gettop(L) == 0, "wrong API stack"); }
void lua_resetglobals() { lua_openthr(); rootproto.next = nullptr; rootproto.marked = 0; rootcl.next = nullptr; rootcl.marked = 0; rootglobal.next = nullptr; rootglobal.marked = 0; roottable.next = nullptr; roottable.marked = 0; refArray = nullptr; refSize = 0; GCthreshold = GARBAGE_BLOCK; nblocks = 0; luaD_init(); luaS_init(); luaX_init(); }
/* ** 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); }
void lua_open (void) { if (lua_state) return; lua_state = luaM_new(lua_State); L->Cstack.base = 0; L->Cstack.lua2C = 0; L->Cstack.num = 0; L->errorJmp = NULL; L->Mbuffer = NULL; L->Mbuffbase = 0; L->Mbuffsize = 0; L->Mbuffnext = 0; L->Cblocks = NULL; L->numCblocks = 0; L->debug = 0; L->callhook = NULL; L->linehook = NULL; L->rootproto.next = NULL; L->rootproto.marked = 0; L->rootcl.next = NULL; L->rootcl.marked = 0; L->rootglobal.next = NULL; L->rootglobal.marked = 0; L->roottable.next = NULL; L->roottable.marked = 0; L->IMtable = NULL; L->refArray = NULL; L->refSize = 0; L->GCthreshold = GARBAGE_BLOCK; L->nblocks = 0; luaD_init(); luaS_init(); luaX_init(); luaT_init(); luaB_predefine(); }
int terra_initwithoptions(lua_State * L, terra_Options * options) { terra_State * T = (terra_State*) lua_newuserdata(L, sizeof(terra_State)); terra_ongc(L, -1, terra_free); assert(T); memset(T,0,sizeof(terra_State)); //some of lua stuff expects pointers to be null on entry T->options = *options; #ifdef __arm__ T->options.usemcjit = true; //force MCJIT since old JIT is partially broken on ARM #endif T->numlivefunctions = 1; T->L = L; assert (T->L); lua_newtable(T->L); lua_insert(L, -2); lua_setfield(L, -2, "__terrastate"); //reference to our T object, so that we can load it from the lua state on other API calls lua_setfield(T->L,LUA_GLOBALSINDEX,"terra"); //create global terra object terra_kindsinit(T); //initialize lua mapping from T_Kind to/from string setterrahome(T->L); //find the location of support files such as the clang resource directory int err = terra_compilerinit(T); if(err) { return err; } err = terra_loadandrunbytecodes(T->L,luaJIT_BC_strict,luaJIT_BC_strict_SIZE, "strict.lua") || terra_loadandrunbytecodes(T->L,luaJIT_BC_terralib,luaJIT_BC_terralib_SIZE, "terralib.lua"); if(err) { return err; } terra_cwrapperinit(T); lua_getfield(T->L,LUA_GLOBALSINDEX,"terra"); lua_pushcfunction(T->L,terra_luaload); lua_setfield(T->L,-2,"load"); lua_pushcfunction(T->L,terra_lualoadstring); lua_setfield(T->L,-2,"loadstring"); lua_pushcfunction(T->L,terra_lualoadfile); lua_setfield(T->L,-2,"loadfile"); lua_newtable(T->L); lua_setfield(T->L,-2,"_trees"); //to hold parser generated trees lua_pushinteger(L, T->options.verbose); lua_setfield(L, -2, "isverbose"); lua_pushinteger(L, T->options.debug); lua_setfield(L, -2, "isdebug"); terra_registerinternalizedfiles(L,-1); lua_pop(T->L,1); //'terra' global luaX_init(T); terra_debuginit(T); err = terra_cudainit(T); /* if cuda is not enabled, this does nothing */ if(err) { return err; } return 0; }
/* ** open parts that may cause memory-allocation errors */ static void f_luaopen (lua_State *L, void *ud) { int i; global_State globalState; lua_State luaState; global_State *g; #ifdef _DEBUG luaState.allocName = "Lua_global_State"; #endif _DEBUG luaState.l_G = &globalState; globalState.reallocFunc = luaHelper_Realloc; globalState.freeFunc = luaHelper_Free; globalState.memData = luaHelper_memData; globalState.nblocks = sizeof(lua_State) + sizeof(global_State); // Bogus. /* create a new global state */ g = luaM_new(&luaState, global_State); UNUSED(ud); if (g == NULL) luaD_throw(L, LUA_ERRMEM); L->l_G = g; g->mainthread = L; g->GCthreshold = 0; /* mark it as unfinished state */ g->strt.size = 0; g->strt.nuse = 0; g->strt.hash = NULL; setnilvalue2n(defaultmeta(L)); setnilvalue2n(registry(L)); luaZ_initbuffer(L, &g->buff); g->panic = default_panic; #if !LUA_REFCOUNT g->rootgc = NULL; g->rootudata = NULL; g->tmudata = NULL; #else LUA_REFCOUNT g->rootgc_head.next = (GCObject*)&g->rootgc_tail; g->rootgc_head.prev = NULL; g->rootgc_tail.next = NULL; g->rootgc_tail.prev = (GCObject*)&g->rootgc_head; g->rootgc_head.tt = LUA_TNIL; g->rootgc_head.marked = 0; g->rootgc_head.ref = 0; g->rootgc_tail.tt = LUA_TNIL; g->rootgc_tail.marked = 0; g->rootgc_tail.ref = 0; g->rootudata_head.next = (GCObject*)&g->rootudata_tail; g->rootudata_head.prev = NULL; g->rootudata_tail.next = NULL; g->rootudata_tail.prev = (GCObject*)&g->rootudata_head; g->rootudata_head.tt = LUA_TNIL; g->rootudata_head.marked = 0; g->rootudata_head.ref = 0; g->rootudata_tail.tt = LUA_TNIL; g->rootudata_tail.marked = 0; g->rootudata_tail.ref = 0; g->tmudata_head.next = (GCObject*)&g->tmudata_tail; g->tmudata_head.prev = NULL; g->tmudata_tail.next = NULL; g->tmudata_tail.prev = (GCObject*)&g->tmudata_head; g->tmudata_head.tt = LUA_TNIL; g->tmudata_head.marked = 0; g->tmudata_head.ref = 0; g->tmudata_tail.tt = LUA_TNIL; g->tmudata_tail.marked = 0; g->tmudata_tail.ref = 0; #endif LUA_REFCOUNT setnilvalue2n(gkey(g->dummynode)); setnilvalue2n(gval(g->dummynode)); g->dummynode->next = NULL; g->nblocks = sizeof(lua_State) + sizeof(global_State); g->reallocFunc = luaHelper_Realloc; g->freeFunc = luaHelper_Free; g->memData = luaHelper_memData; g->fatalErrorFunc = defaultFatalErrorFunc; #ifdef LUA_MTSUPPORT g->lockData = NULL; g->lockFunc = NULL; g->unlockFunc = NULL; #endif LUA_MTSUPPORT g->userGCFunction = NULL; g->globalUserData = NULL; stack_init(L, L); /* init stack */ for (i = 0; i < LUA_NTYPES; i++) { defaultmetatypes(L, i)->value.gc = NULL; } /* create default meta table with a dummy table, and then close the loop */ defaultmeta(L)->tt = LUA_TNUMBER; defaultmeta(L)->value.gc = NULL; sethvalue2n(defaultmeta(L), luaH_new(L, 0, 0)); __AddRefDirect(hvalue(defaultmeta(L))); hvalue(defaultmeta(L))->metatable = hvalue(defaultmeta(L)); __AddRefDirect(hvalue(defaultmeta(L))->metatable); /* build meta tables */ for (i = 0; i < LUA_NTYPES; i++) { luaM_setname(L, "Lua_defaultMetaTypes"); sethvalue2n(defaultmetatypes(L, i), luaH_new(L, 0, 0)); hvalue(defaultmetatypes(L, i))->metatable = hvalue(defaultmeta(L)); } luaM_setname(L, "Lua_Globals"); sethvalue(gt(L), luaH_new(L, 0, 4)); /* table of globals */ __AddRefDirect(hvalue(gt(L))); luaM_setname(L, "Lua_Registry"); sethvalue(registry(L), luaH_new(L, 4, 4)); /* registry */ __AddRef(registry(L)); g->minimumstrings = lua_minimumnumstrings; luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ luaT_init(L); luaX_init(L); luaS_fix(luaS_newliteral(L, MEMERRMSG)); g->GCthreshold = 4*G(L)->nblocks; luaZ_openspace(L, &g->buff, lua_minimumauxspace); }