Proto *luaF_newproto (lua_State *L) { Proto *f; luaM_setname(L, "Lua_proto"); f = luaM_new(L, Proto); #if LUA_REFCOUNT f->ref = 0; #endif LUA_REFCOUNT luaC_link(L, valtogco(f), LUA_TPROTO); f->k = NULL; f->sizek = 0; f->p = NULL; f->sizep = 0; f->code = NULL; f->sizecode = 0; f->sizelineinfo = 0; f->sizeupvalues = 0; f->nups = 0; f->upvalues = NULL; f->numparams = 0; f->is_vararg = 0; f->maxstacksize = 0; f->lineinfo = NULL; f->sizelocvars = 0; f->locvars = NULL; f->lineDefined = 0; f->source = NULL; return f; }
/* 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; }
LUA_API 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 (luaD_runprotected(L, f_luaopen, &stacksize) != 0) { /* memory allocation error: free partial state */ lua_close(L); return NULL; } L->GCthreshold = 2*L->nblocks; return L; }
Proto *luaF_newproto (lua_State *L, SharedProto *sp) { GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto)); Proto *f = gco2p(o); f->sp = NULL; f->k = NULL; f->p = NULL; f->cache = NULL; if (sp == NULL) { sp = luaM_new(L, SharedProto); sp->l_G = G(L); sp->sizek = 0; sp->sizep = 0; sp->code = NULL; sp->sizecode = 0; sp->lineinfo = NULL; sp->sizelineinfo = 0; sp->upvalues = NULL; sp->sizeupvalues = 0; sp->numparams = 0; sp->is_vararg = 0; sp->maxstacksize = 0; sp->locvars = NULL; sp->sizelocvars = 0; sp->linedefined = 0; sp->lastlinedefined = 0; sp->source = NULL; } f->sp = sp; return f; }
/* ** 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 UpVal *makeupval(lua_State *L, int stackpos) { UpVal *uv = luaM_new(L, UpVal); uv->tt = LUA_TUPVAL; uv->v = &uv->value; setobj(uv->v, getobject(L, stackpos)); luaC_link(L, valtogco(uv), LUA_TUPVAL); return uv; }
CallInfo *luaE_extendCI (lua_State *L) { CallInfo *ci = luaM_new(L, CallInfo); lua_assert(L->ci->next == NULL); L->ci->next = ci; ci->previous = L->ci; ci->next = NULL; return ci; }
/* ** fill a closure with new closed upvalues */ void luaF_initupvals (lua_State *L, LClosure *cl) { int i; for (i = 0; i < cl->nupvalues; i++) { UpVal *uv = luaM_new(L, UpVal); uv->refcount = 1; uv->v = &uv->u.value; /* make it closed */ setnilvalue(uv->v); cl->upvals[i] = uv; } }
Hash *luaH_new(int32 nhash) { Hash *t = luaM_new(Hash); nhash = luaO_redimension((int32)((float)nhash / REHASH_LIMIT)); nodevector(t) = hashnodecreate(nhash); nhash(t) = nhash; nuse(t) = 0; t->htag = TagDefault; luaO_insertlist(&roottable, (GCnode *)t); nblocks += gcsize(nhash); return t; }
CallInfo *luaE_extendCI (lua_State *L) { CallInfo *ci = luaM_new(L, CallInfo); ci->jitstatus = 0; ci->stacklevel = L->ci->stacklevel+1; lua_assert(L->ci->next == NULL); L->ci->next = ci; ci->previous = L->ci; ci->next = NULL; L->nci++; return ci; }
Hash *luaH_new (lua_State *L, int size) { Hash *t = luaM_new(L, Hash); t->htag = TagDefault; t->next = L->roottable; L->roottable = t; t->mark = t; t->size = 0; L->nblocks += gcsize(L, 0); t->node = NULL; setnodevector(L, t, luaO_power2(size)); return t; }
static TaggedString *newone_u (char *buff, int tag, unsigned long h) { TaggedString *ts = luaM_new(TaggedString); ts->u.d.v = buff; ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag; ts->constindex = -1; /* tag -> this is a userdata */ L->nblocks++; ts->head.marked = 0; ts->head.next = (GCnode *)ts; /* signal it is in no list */ ts->hash = h; return ts; }
void lua_open() { if (lua_state) return; lua_rootState = lua_state = luaM_new(LState); lua_stateinit(lua_state); lua_resetglobals(); luaT_init(); luaB_predefine(); luaL_addlibtolist(stdErrorRimFunc, (sizeof(stdErrorRimFunc) / sizeof(stdErrorRimFunc[0]))); if (Debug::isChannelEnabled(Debug::Lua)) lua_callhook = callHook; }
TProtoFunc *luaF_newproto() { TProtoFunc *f = luaM_new(TProtoFunc); f->code = nullptr; f->lineDefined = 0; f->fileName = nullptr; f->consts = nullptr; f->nconsts = 0; f->locvars = nullptr; luaO_insertlist(&rootproto, (GCnode *)f); nblocks += gcsizeproto(f); return f; }
void start_script() { lua_Object paramObj = lua_getparam(1); lua_Type type = ttype(Address(paramObj)); if (paramObj == LUA_NOOBJECT || (type != LUA_T_CPROTO && type != LUA_T_PROTO)) { warning("lua: Bad argument to start_script. - lua/ltask.cpp:32"); // NOTE: Decomment the lua_error if you want to see the stacktrace. // It is commented out because si.lua, in the function si.set_up_actors (line 848), // calls "start_script(si.naranja_drinking)", which doesn't exist. The problem with // that is that lua_error ends the function that was going on, breaking // si.set really badly. // lua_error("Bad argument to start_script"); return; } LState *state = luaM_new(LState); lua_stateinit(state); state->next = lua_state->next; state->prev = lua_state; if (state->next) state->next->prev = state; lua_state->next = state; state->taskFunc.ttype = type; state->taskFunc.value = Address(paramObj)->value; int l = 2; for (lua_Object object = lua_getparam(l++); object != LUA_NOOBJECT; object = lua_getparam(l++)) { TObject ptr; ptr.ttype = Address(object)->ttype; ptr.value = Address(object)->value; LState *tmpState = lua_state; lua_state = state; luaA_pushobject(&ptr); lua_state = tmpState; } ttype(lua_state->stack.top) = LUA_T_TASK; nvalue(lua_state->stack.top) = (float)state->id; incr_top; }
UpVal *luaF_findupval (lua_State *L, StkId level) { GCObject **pp = &L->openupval; UpVal *p; UpVal *v; while ((p = ngcotouv(*pp)) != NULL && p->v >= level) { if (p->v == level) return p; pp = &p->next; } v = luaM_new(L, UpVal); /* not found: create a new one */ v->tt = LUA_TUPVAL; v->marked = 1; /* open upvalues should not be collected */ v->v = level; /* current value lives in the stack */ v->next = *pp; /* chain it in the proper position */ #if LUA_REFCOUNT v->prev = NULL; v->ref = 0; #endif LUA_REFCOUNT *pp = valtogco(v); return v; }
UpVal *luaF_findupval (lua_State *L, StkId level) { UpVal **pp = &L->openupval; UpVal *p; UpVal *uv; lua_assert(isintwups(L) || L->openupval == NULL); while (*pp != NULL && (p = *pp)->v >= level) { lua_assert(upisopen(p)); if (p->v == level) /* found a corresponding upvalue? */ return p; /* return it */ pp = &p->u.open.next; } /* not found: create a new upvalue */ uv = luaM_new(L, UpVal); uv->refcount = 0; uv->u.open.next = *pp; /* link it to list of open upvalues */ uv->u.open.touched = 1; *pp = uv; uv->v = level; /* current value lives in the stack */ if (!isintwups(L)) { /* thread not in list of threads with upvalues? */ L->twups = G(L)->twups; /* link it to the list */ G(L)->twups = L; } return uv; }
Proto *luaF_newproto (lua_State *L) { Proto *f = luaM_new(L, Proto); f->knum = NULL; f->nknum = 0; f->kstr = NULL; f->nkstr = 0; f->kproto = NULL; f->nkproto = 0; f->code = NULL; f->ncode = 0; f->numparams = 0; f->is_vararg = 0; f->maxstacksize = 0; f->marked = 0; f->lineinfo = NULL; f->nlineinfo = 0; f->nlocvars = 0; f->locvars = NULL; f->lineDefined = 0; f->source = NULL; f->next = L->rootproto; /* chain in list of protos */ L->rootproto = f; return f; }
void start_script() { lua_Object paramObj = lua_getparam(1); lua_Type type = paramObj == LUA_NOOBJECT ? LUA_T_NIL : ttype(Address(paramObj)); if (paramObj == LUA_NOOBJECT || (type != LUA_T_CPROTO && type != LUA_T_PROTO)) { lua_error("Bad argument to start_script"); return; } LState *state = luaM_new(LState); lua_stateinit(state); state->next = lua_state->next; state->prev = lua_state; if (state->next) state->next->prev = state; lua_state->next = state; state->taskFunc.ttype = type; state->taskFunc.value = Address(paramObj)->value; int l = 2; for (lua_Object object = lua_getparam(l++); object != LUA_NOOBJECT; object = lua_getparam(l++)) { TObject ptr; ptr.ttype = Address(object)->ttype; ptr.value = Address(object)->value; LState *tmpState = lua_state; lua_state = state; luaA_pushobject(&ptr); lua_state = tmpState; } ttype(lua_state->stack.top) = LUA_T_TASK; nvalue(lua_state->stack.top) = (float)state->id; incr_top; }
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(); }
int32 luaD_call(StkId base, int32 nResults) { lua_Task *tmpTask = lua_state->task; if (!lua_state->task || lua_state->state_counter2) { lua_Task *t = luaM_new(lua_Task); lua_taskinit(t, lua_state->task, base, nResults); lua_state->task = t; } else { tmpTask = lua_state->some_task; } while (1) { lua_CFunction function = NULL; StkId firstResult = 0; TObject *funcObj = lua_state->stack.stack + base - 1; if (ttype(funcObj) == LUA_T_CLOSURE) { Closure *c = clvalue(funcObj); TObject *proto = &(c->consts[0]); ttype(funcObj) = LUA_T_CLMARK; if (ttype(proto) == LUA_T_CPROTO) { function = fvalue(funcObj); firstResult = callCclosure(c, fvalue(proto), base); } else { lua_taskresume(lua_state->task, c, tfvalue(proto), base); firstResult = luaV_execute(lua_state->task); } } else if (ttype(funcObj) == LUA_T_PMARK) { if (!lua_state->task->some_flag) { TObject *im = luaT_getimbyObj(funcObj, IM_FUNCTION); if (ttype(im) == LUA_T_NIL) lua_error("call expression not a function"); luaD_callTM(im, (lua_state->stack.top - lua_state->stack.stack) - (base - 1), nResults); continue; } firstResult = luaV_execute(lua_state->task); } else if (ttype(funcObj) == LUA_T_CMARK) { if (!lua_state->task->some_flag) { TObject *im = luaT_getimbyObj(funcObj, IM_FUNCTION); if (ttype(im) == LUA_T_NIL) lua_error("call expression not a function"); luaD_callTM(im, (lua_state->stack.top - lua_state->stack.stack) - (base - 1), nResults); continue; } } else if (ttype(funcObj) == LUA_T_CLMARK) { Closure *c = clvalue(funcObj); TObject *proto = &(c->consts[0]); if (!lua_state->task->some_flag) { TObject *im = luaT_getimbyObj(funcObj, IM_FUNCTION); if (ttype(im) == LUA_T_NIL) lua_error("call expression not a function"); luaD_callTM(im, (lua_state->stack.top - lua_state->stack.stack) - (base - 1), nResults); continue; } if (ttype(proto) != LUA_T_CPROTO) firstResult = luaV_execute(lua_state->task); } else if (ttype(funcObj) == LUA_T_PROTO) { ttype(funcObj) = LUA_T_PMARK; lua_taskresume(lua_state->task, NULL, tfvalue(funcObj), base); firstResult = luaV_execute(lua_state->task); } else if (ttype(funcObj) == LUA_T_CPROTO) { ttype(funcObj) = LUA_T_CMARK; function = fvalue(funcObj); firstResult = callC(fvalue(funcObj), base); } else { TObject *im = luaT_getimbyObj(funcObj, IM_FUNCTION); if (ttype(im) == LUA_T_NIL) { // NOTE: Originally this throwed the lua_error. Anyway it is commented here because // when in year 4 bi.exit() calls bi.book.act:free(). But bi.book.act is nil, // hence it enters this branch and the error blocks the game. // Now we try instead to survive and go on with the function. lua_Task *t = lua_state->task; lua_state->task = t->next; lua_state->some_task = tmpTask; luaM_free(t); warning("Lua: call expression not a function"); return 1; // lua_error("call expression not a function"); } luaD_callTM(im, (lua_state->stack.top - lua_state->stack.stack) - (base - 1), nResults); continue; } if (firstResult <= 0) { nResults = lua_state->task->aux; base = -firstResult; lua_Task *t = luaM_new(lua_Task); lua_taskinit(t, lua_state->task, base, nResults); lua_state->task = t; } else { nResults = lua_state->task->some_results; base = lua_state->task->some_base; if (nResults != 255) luaD_adjusttop(firstResult + nResults); base--; nResults = lua_state->stack.top - (lua_state->stack.stack + firstResult); for (int32 i = 0; i < nResults; i++) *(lua_state->stack.stack + base + i) = *(lua_state->stack.stack + firstResult + i); lua_state->stack.top -= firstResult - base; lua_Task *tmp = lua_state->task; lua_state->task = lua_state->task->next; luaM_free(tmp); if (lua_state->task) { nResults = lua_state->task->some_results; base = lua_state->task->some_base; } if (function == break_here) { if (!lua_state->state_counter1) { lua_state->some_task = tmpTask; return 1; } } } if (lua_state->task == tmpTask) break; } 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); }