Lines Matching refs:L

144 #define luai_apicheck(L,o){(void)L;}  argument
160 #define LUAI_THROW(L,c)longjmp((c)->b,1) argument
161 #define LUAI_TRY(L,c,a)if(setjmp((c)->b)==0){a} argument
162 #define lua_pclose(L,file)((void)((void)L,file),0) argument
165 typedef int(*lua_CFunction)(lua_State*L);
166 typedef const char*(*lua_Reader)(lua_State*L,void*ud,size_t*sz);
170 static void lua_settop(lua_State*L,int idx);
171 static int lua_type(lua_State*L,int idx);
172 static const char* lua_tolstring(lua_State*L,int idx,size_t*len);
173 static size_t lua_objlen(lua_State*L,int idx);
174 static void lua_pushlstring(lua_State*L,const char*s,size_t l);
175 static void lua_pushcclosure(lua_State*L,lua_CFunction fn,int n);
176 static void lua_createtable(lua_State*L,int narr,int nrec);
177 static void lua_setfield(lua_State*L,int idx,const char*k);
178 #define lua_pop(L,n)lua_settop(L,-(n)-1) argument
179 #define lua_newtable(L)lua_createtable(L,0,0) argument
180 #define lua_pushcfunction(L,f)lua_pushcclosure(L,(f),0) argument
181 #define lua_strlen(L,i)lua_objlen(L,(i)) argument
182 #define lua_isfunction(L,n)(lua_type(L,(n))==6) argument
183 #define lua_istable(L,n)(lua_type(L,(n))==5) argument
184 #define lua_isnil(L,n)(lua_type(L,(n))==0) argument
185 #define lua_isboolean(L,n)(lua_type(L,(n))==1) argument
186 #define lua_isnone(L,n)(lua_type(L,(n))==(-1)) argument
187 #define lua_isnoneornil(L,n)(lua_type(L,(n))<=0) argument
188 #define lua_pushliteral(L,s)lua_pushlstring(L,""s,(sizeof(s)/sizeof(char))-1) argument
189 #define lua_setglobal(L,s)lua_setfield(L,(-10002),(s)) argument
190 #define lua_tostring(L,i)lua_tolstring(L,(i),NULL) argument
192 typedef void(*lua_Hook)(lua_State*L,lua_Debug*ar);
261 #define setsvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=4;checklivene… argument
262 #define setuvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=7;checklivene… argument
263 #define setthvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=8;checkliven… argument
264 #define setclvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=6;checkliven… argument
265 #define sethvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=5;checklivene… argument
266 #define setptvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=(8+1);checkl… argument
267 #define setobj(L,obj1,obj2){const TValue*o2=(obj2);TValue*o1=(obj1);o1->value=o2->value;o1->tt=o2->… argument
376 …fine luaM_reallocv(L,b,on,n,e)((cast(size_t,(n)+1)<=((size_t)(~(size_t)0)-2)/(e))?luaM_realloc_(L,… argument
377 #define luaM_freemem(L,b,s)luaM_realloc_(L,(b),(s),0) argument
378 #define luaM_free(L,b)luaM_realloc_(L,(b),sizeof(*(b)),0) argument
379 #define luaM_freearray(L,b,n,t)luaM_reallocv(L,(b),n,0,sizeof(t)) argument
380 #define luaM_malloc(L,t)luaM_realloc_(L,NULL,0,(t)) argument
381 #define luaM_new(L,t)cast(t*,luaM_malloc(L,sizeof(t))) argument
382 #define luaM_newvector(L,n,t)cast(t*,luaM_reallocv(L,NULL,0,n,sizeof(t))) argument
383 #define luaM_growvector(L,v,nelems,size,t,limit,e)if((nelems)+1>(size))((v)=cast(t*,luaM_growaux_(L argument
384 #define luaM_reallocvector(L,v,oldn,n,t)((v)=cast(t*,luaM_reallocv(L,v,oldn,n,sizeof(t)))) argument
385 static void*luaM_realloc_(lua_State*L,void*block,size_t oldsize,
387 static void*luaM_toobig(lua_State*L);
388 static void*luaM_growaux_(lua_State*L,void*block,int*size,
399 #define luaZ_initbuffer(L,buff)((buff)->buffer=NULL,(buff)->buffsize=0) argument
404 #define luaZ_resizebuffer(L,buff,size)(luaM_reallocvector(L,(buff)->buffer,(buff)->buffsize,size,ch… argument
405 #define luaZ_freebuffer(L,buff)luaZ_resizebuffer(L,buff,0) argument
411 lua_State*L; member
415 #define gt(L)(&L->l_gt) argument
416 #define registry(L)(&G(L)->l_registry) argument
430 #define curr_func(L)(clvalue(L->ci->func)) argument
489 #define G(L)(L->l_G) argument
511 static void luaE_freethread(lua_State*L,lua_State*L1);
514 #define resethookcount(L)(L->hookcount=L->basehookcount) argument
515 static void luaG_typeerror(lua_State*L,const TValue*o,
517 static void luaG_runerror(lua_State*L,const char*fmt,...);
518 …heckstack(L,n)if((char*)L->stack_last-(char*)L->top<=(n)*(int)sizeof(TValue))luaD_growstack(L,n);e… argument
519 #define incr_top(L){luaD_checkstack(L,1);L->top++;} argument
520 #define savestack(L,p)((char*)(p)-(char*)L->stack) argument
521 #define restorestack(L,n)((TValue*)((char*)L->stack+(n))) argument
522 #define saveci(L,p)((char*)(p)-(char*)L->base_ci) argument
523 #define restoreci(L,n)((CallInfo*)((char*)L->base_ci+(n))) argument
524 typedef void(*Pfunc)(lua_State*L,void*ud);
525 static int luaD_poscall(lua_State*L,StkId firstResult);
526 static void luaD_reallocCI(lua_State*L,int newsize);
527 static void luaD_reallocstack(lua_State*L,int newsize);
528 static void luaD_growstack(lua_State*L,int n);
529 static void luaD_throw(lua_State*L,int errcode);
530 static void*luaM_growaux_(lua_State*L,void*block,int*size,size_t size_elems, in luaM_growaux_() argument
536 luaG_runerror(L,errormsg); in luaM_growaux_()
544 newblock=luaM_reallocv(L,block,*size,newsize,size_elems); in luaM_growaux_()
548 static void*luaM_toobig(lua_State*L){ in luaM_toobig() argument
549 luaG_runerror(L,"memory allocation error: block too big"); in luaM_toobig()
552 static void*luaM_realloc_(lua_State*L,void*block,size_t osize,size_t nsize){ in luaM_realloc_() argument
553 global_State*g=G(L); in luaM_realloc_()
556 luaD_throw(L,4); in luaM_realloc_()
580 #define luaC_checkGC(L){condhardstacktests(luaD_reallocstack(L,L->stacksize-5-1));if(G(L)->totalbyt… argument
581 #define luaC_barrier(L,p,v){if(valiswhite(v)&&isblack(obj2gco(p)))luaC_barrierf(L,obj2gco(p),gcvalu… argument
582 #define luaC_barriert(L,t,v){if(valiswhite(v)&&isblack(obj2gco(t)))luaC_barrierback(L,t);} argument
583 #define luaC_objbarrier(L,p,o){if(iswhite(obj2gco(o))&&isblack(obj2gco(p)))luaC_barrierf(L,obj2gco(… argument
584 #define luaC_objbarriert(L,t,o){if(iswhite(obj2gco(o))&&isblack(obj2gco(t)))luaC_barrierback(L,t);} argument
585 static void luaC_step(lua_State*L);
586 static void luaC_link(lua_State*L,GCObject*o,lu_byte tt);
587 static void luaC_linkupval(lua_State*L,UpVal*uv);
588 static void luaC_barrierf(lua_State*L,GCObject*o,GCObject*v);
589 static void luaC_barrierback(lua_State*L,Table*t);
592 #define luaS_new(L,s)(luaS_newlstr(L,s,strlen(s))) argument
593 #define luaS_newliteral(L,s)(luaS_newlstr(L,""s,(sizeof(s)/sizeof(char))-1)) argument
595 static TString*luaS_newlstr(lua_State*L,const char*str,size_t l);
596 #define tostring(L,o)((ttype(o)==4)||(luaV_tostring(L,o))) argument
598 #define equalobj(L,o1,o2)(ttype(o1)==ttype(o2)&&luaV_equalval(L,o1,o2)) argument
599 static int luaV_equalval(lua_State*L,const TValue*t1,const TValue*t2);
601 static int luaV_tostring(lua_State*L,StkId obj);
602 static void luaV_execute(lua_State*L,int nexeccalls);
603 static void luaV_concat(lua_State*L,int total,int last);
660 static void pushstr(lua_State*L,const char*str){ in pushstr() argument
661 setsvalue(L,L->top,luaS_new(L,str)); in pushstr()
662 incr_top(L); in pushstr()
664 static const char*luaO_pushvfstring(lua_State*L,const char*fmt,va_list argp){ in luaO_pushvfstring() argument
666 pushstr(L,""); in luaO_pushvfstring()
670 setsvalue(L,L->top,luaS_newlstr(L,fmt,e-fmt)); in luaO_pushvfstring()
671 incr_top(L); in luaO_pushvfstring()
676 pushstr(L,s); in luaO_pushvfstring()
683 pushstr(L,buff); in luaO_pushvfstring()
687 setnvalue(L->top,cast_num(va_arg(argp,int))); in luaO_pushvfstring()
688 incr_top(L); in luaO_pushvfstring()
692 setnvalue(L->top,cast_num(va_arg(argp,l_uacNumber))); in luaO_pushvfstring()
693 incr_top(L); in luaO_pushvfstring()
699 pushstr(L,buff); in luaO_pushvfstring()
703 pushstr(L,"%"); in luaO_pushvfstring()
711 pushstr(L,buff); in luaO_pushvfstring()
718 pushstr(L,fmt); in luaO_pushvfstring()
719 luaV_concat(L,n+1,cast_int(L->top-L->base)-1); in luaO_pushvfstring()
720 L->top-=n; in luaO_pushvfstring()
721 return svalue(L->top-1); in luaO_pushvfstring()
723 static const char*luaO_pushfstring(lua_State*L,const char*fmt,...){
727 msg=luaO_pushvfstring(L,fmt,argp);
769 static TValue*luaH_setnum(lua_State*L,Table*t,int key);
771 static TValue*luaH_set(lua_State*L,Table*t,const TValue*key);
777 static void luaT_init(lua_State*L){
787 G(L)->tmname[i]=luaS_new(L,luaT_eventname[i]);
788 luaS_fix(G(L)->tmname[i]);
799 static const TValue*luaT_gettmbyobj(lua_State*L,const TValue*o,TMS event){
809 mt=G(L)->mt[ttype(o)];
811 return(mt?luaH_getstr(mt,G(L)->tmname[event]):(&luaO_nilobject_));
815 static Closure*luaF_newCclosure(lua_State*L,int nelems,Table*e){
816 Closure*c=cast(Closure*,luaM_malloc(L,sizeCclosure(nelems)));
817 luaC_link(L,obj2gco(c),6);
823 static Closure*luaF_newLclosure(lua_State*L,int nelems,Table*e){
824 Closure*c=cast(Closure*,luaM_malloc(L,sizeLclosure(nelems)));
825 luaC_link(L,obj2gco(c),6);
832 static UpVal*luaF_newupval(lua_State*L){
833 UpVal*uv=luaM_new(L,UpVal);
834 luaC_link(L,obj2gco(uv),(8+2));
839 static UpVal*luaF_findupval(lua_State*L,StkId level){
840 global_State*g=G(L);
841 GCObject**pp=&L->openupval;
852 uv=luaM_new(L,UpVal);
868 static void luaF_freeupval(lua_State*L,UpVal*uv){
871 luaM_free(L,uv);
873 static void luaF_close(lua_State*L,StkId level){
875 global_State*g=G(L);
876 while(L->openupval!=NULL&&(uv=ngcotouv(L->openupval))->v>=level){
878 L->openupval=uv->next;
880 luaF_freeupval(L,uv);
883 setobj(L,&uv->u.value,uv->v);
885 luaC_linkupval(L,uv);
889 static Proto*luaF_newproto(lua_State*L){
890 Proto*f=luaM_new(L,Proto);
891 luaC_link(L,obj2gco(f),(8+1));
913 static void luaF_freeproto(lua_State*L,Proto*f){
914 luaM_freearray(L,f->code,f->sizecode,Instruction);
915 luaM_freearray(L,f->p,f->sizep,Proto*);
916 luaM_freearray(L,f->k,f->sizek,TValue);
917 luaM_freearray(L,f->lineinfo,f->sizelineinfo,int);
918 luaM_freearray(L,f->locvars,f->sizelocvars,struct LocVar);
919 luaM_freearray(L,f->upvalues,f->sizeupvalues,TString*);
920 luaM_free(L,f);
922 static void luaF_freeclosure(lua_State*L,Closure*c){
925 luaM_freemem(L,c,size);
969 struct lua_State*L;
982 static Proto*luaY_parser(lua_State*L,ZIO*z,Mbuffer*buff,
989 static void luaD_seterrorobj(lua_State*L,int errcode,StkId oldtop){
992 setsvalue(L,oldtop,luaS_newliteral(L,"not enough memory"));
996 setsvalue(L,oldtop,luaS_newliteral(L,"error in error handling"));
1001 setobj(L,oldtop,L->top-1);
1005 L->top=oldtop+1;
1007 static void restore_stack_limit(lua_State*L){
1008 if(L->size_ci>20000){
1009 int inuse=cast_int(L->ci-L->base_ci);
1011 luaD_reallocCI(L,20000);
1014 static void resetstack(lua_State*L,int status){
1015 L->ci=L->base_ci;
1016 L->base=L->ci->base;
1017 luaF_close(L,L->base);
1018 luaD_seterrorobj(L,status,L->base);
1019 L->nCcalls=L->baseCcalls;
1020 L->allowhook=1;
1021 restore_stack_limit(L);
1022 L->errfunc=0;
1023 L->errorJmp=NULL;
1025 static void luaD_throw(lua_State*L,int errcode){
1026 if(L->errorJmp){
1027 L->errorJmp->status=errcode;
1028 LUAI_THROW(L,L->errorJmp);
1031 L->status=cast_byte(errcode);
1032 if(G(L)->panic){
1033 resetstack(L,errcode);
1034 G(L)->panic(L);
1039 static int luaD_rawrunprotected(lua_State*L,Pfunc f,void*ud){
1042 lj.previous=L->errorJmp;
1043 L->errorJmp=&lj;
1044 LUAI_TRY(L,&lj,
1045 (*f)(L,ud);
1047 L->errorJmp=lj.previous;
1050 static void correctstack(lua_State*L,TValue*oldstack){
1053 L->top=(L->top-oldstack)+L->stack;
1054 for(up=L->openupval;up!=NULL;up=up->gch.next)
1055 gco2uv(up)->v=(gco2uv(up)->v-oldstack)+L->stack;
1056 for(ci=L->base_ci;ci<=L->ci;ci++){
1057 ci->top=(ci->top-oldstack)+L->stack;
1058 ci->base=(ci->base-oldstack)+L->stack;
1059 ci->func=(ci->func-oldstack)+L->stack;
1061 L->base=(L->base-oldstack)+L->stack;
1063 static void luaD_reallocstack(lua_State*L,int newsize){
1064 TValue*oldstack=L->stack;
1066 luaM_reallocvector(L,L->stack,L->stacksize,realsize,TValue);
1067 L->stacksize=realsize;
1068 L->stack_last=L->stack+newsize;
1069 correctstack(L,oldstack);
1071 static void luaD_reallocCI(lua_State*L,int newsize){
1072 CallInfo*oldci=L->base_ci;
1073 luaM_reallocvector(L,L->base_ci,L->size_ci,newsize,CallInfo);
1074 L->size_ci=newsize;
1075 L->ci=(L->ci-oldci)+L->base_ci;
1076 L->end_ci=L->base_ci+L->size_ci-1;
1078 static void luaD_growstack(lua_State*L,int n){
1079 if(n<=L->stacksize)
1080 luaD_reallocstack(L,2*L->stacksize);
1082 luaD_reallocstack(L,L->stacksize+n);
1084 static CallInfo*growCI(lua_State*L){
1085 if(L->size_ci>20000)
1086 luaD_throw(L,5);
1088 luaD_reallocCI(L,2*L->size_ci);
1089 if(L->size_ci>20000)
1090 luaG_runerror(L,"stack overflow");
1092 return++L->ci;
1094 static StkId adjust_varargs(lua_State*L,Proto*p,int actual){
1100 setnilvalue(L->top++);
1101 fixed=L->top-actual;
1102 base=L->top;
1104 setobj(L,L->top++,fixed+i);
1108 sethvalue(L,L->top++,htab);
1112 static StkId tryfuncTM(lua_State*L,StkId func){
1113 const TValue*tm=luaT_gettmbyobj(L,func,TM_CALL);
1115 ptrdiff_t funcr=savestack(L,func);
1117 luaG_typeerror(L,func,"call");
1118 for(p=L->top;p>func;p--)setobj(L,p,p-1);
1119 incr_top(L);
1120 func=restorestack(L,funcr);
1121 setobj(L,func,tm);
1124 #define inc_ci(L)((L->ci==L->end_ci)?growCI(L):(condhardstacktests(luaD_reallocCI(L,L->size_ci)),++
1125 static int luaD_precall(lua_State*L,StkId func,int nresults){
1129 func=tryfuncTM(L,func);
1130 funcr=savestack(L,func);
1132 L->ci->savedpc=L->savedpc;
1137 luaD_checkstack(L,p->maxstacksize+p->numparams);
1138 func=restorestack(L,funcr);
1141 if(L->top>base+p->numparams)
1142 L->top=base+p->numparams;
1145 int nargs=cast_int(L->top-func)-1;
1146 base=adjust_varargs(L,p,nargs);
1147 func=restorestack(L,funcr);
1149 ci=inc_ci(L);
1151 L->base=ci->base=base;
1152 ci->top=L->base+p->maxstacksize;
1153 L->savedpc=p->code;
1156 for(st=L->top;st<ci->top;st++)
1158 L->top=ci->top;
1164 luaD_checkstack(L,20);
1165 ci=inc_ci(L);
1166 ci->func=restorestack(L,funcr);
1167 L->base=ci->base=ci->func+1;
1168 ci->top=L->top+20;
1170 n=(*curr_func(L)->c.f)(L);
1174 luaD_poscall(L,L->top-n);
1179 static int luaD_poscall(lua_State*L,StkId firstResult){
1183 ci=L->ci--;
1186 L->base=(ci-1)->base;
1187 L->savedpc=(ci-1)->savedpc;
1188 for(i=wanted;i!=0&&firstResult<L->top;i--)
1189 setobj(L,res++,firstResult++);
1192 L->top=res;
1195 static void luaD_call(lua_State*L,StkId func,int nResults){
1196 if(++L->nCcalls>=200){
1197 if(L->nCcalls==200)
1198 luaG_runerror(L,"C stack overflow");
1199 else if(L->nCcalls>=(200+(200>>3)))
1200 luaD_throw(L,5);
1202 if(luaD_precall(L,func,nResults)==0)
1203 luaV_execute(L,1);
1204 L->nCcalls--;
1205 luaC_checkGC(L);
1207 static int luaD_pcall(lua_State*L,Pfunc func,void*u,
1210 unsigned short oldnCcalls=L->nCcalls;
1211 ptrdiff_t old_ci=saveci(L,L->ci);
1212 lu_byte old_allowhooks=L->allowhook;
1213 ptrdiff_t old_errfunc=L->errfunc;
1214 L->errfunc=ef;
1215 status=luaD_rawrunprotected(L,func,u);
1217 StkId oldtop=restorestack(L,old_top);
1218 luaF_close(L,oldtop);
1219 luaD_seterrorobj(L,status,oldtop);
1220 L->nCcalls=oldnCcalls;
1221 L->ci=restoreci(L,old_ci);
1222 L->base=L->ci->base;
1223 L->savedpc=L->ci->savedpc;
1224 L->allowhook=old_allowhooks;
1225 restore_stack_limit(L);
1227 L->errfunc=old_errfunc;
1235 static void f_parser(lua_State*L,void*ud){
1240 luaC_checkGC(L);
1241 tf=luaY_parser(L,p->z,
1243 cl=luaF_newLclosure(L,tf->nups,hvalue(gt(L)));
1246 cl->l.upvals[i]=luaF_newupval(L);
1247 setclvalue(L,L->top,cl);
1248 incr_top(L);
1250 static int luaD_protectedparser(lua_State*L,ZIO*z,const char*name){
1254 luaZ_initbuffer(L,&p.buff);
1255 status=luaD_pcall(L,f_parser,&p,savestack(L,L->top),L->errfunc);
1256 luaZ_freebuffer(L,&p.buff);
1259 static void luaS_resize(lua_State*L,int newsize){
1263 if(G(L)->gcstate==2)
1265 newhash=luaM_newvector(L,newsize,GCObject*);
1266 tb=&G(L)->strt;
1279 luaM_freearray(L,tb->hash,tb->size,TString*);
1283 static TString*newlstr(lua_State*L,const char*str,size_t l,
1288 luaM_toobig(L);
1289 ts=cast(TString*,luaM_malloc(L,(l+1)*sizeof(char)+sizeof(TString)));
1292 ts->tsv.marked=luaC_white(G(L));
1297 tb=&G(L)->strt;
1303 luaS_resize(L,tb->size*2);
1306 static TString*luaS_newlstr(lua_State*L,const char*str,size_t l){
1313 for(o=G(L)->strt.hash[lmod(h,G(L)->strt.size)];
1318 if(isdead(G(L),o))changewhite(o);
1322 return newlstr(L,str,l,h);
1324 static Udata*luaS_newudata(lua_State*L,size_t s,Table*e){
1327 luaM_toobig(L);
1328 u=cast(Udata*,luaM_malloc(L,s+sizeof(Udata)));
1329 u->uv.marked=luaC_white(G(L));
1334 u->uv.next=G(L)->mainthread->next;
1335 G(L)->mainthread->next=obj2gco(u);
1380 static int findindex(lua_State*L,Table*t,StkId key){
1397 luaG_runerror(L,"invalid key to "LUA_QL("next"));
1401 static int luaH_next(lua_State*L,Table*t,StkId key){
1402 int i=findindex(L,t,key);
1406 setobj(L,key+1,&t->array[i]);
1412 setobj(L,key,key2tval(gnode(t,i)));
1413 setobj(L,key+1,gval(gnode(t,i)));
1483 static void setarrayvector(lua_State*L,Table*t,int size){
1485 luaM_reallocvector(L,t->array,t->sizearray,size,TValue);
1490 static void setnodevector(lua_State*L,Table*t,int size){
1500 luaG_runerror(L,"table overflow");
1502 t->node=luaM_newvector(L,size,Node);
1513 static void resize(lua_State*L,Table*t,int nasize,int nhsize){
1519 setarrayvector(L,t,nasize);
1520 setnodevector(L,t,nhsize);
1525 setobj(L,luaH_setnum(L,t,i+1),&t->array[i]);
1527 luaM_reallocvector(L,t->array,oldasize,nasize,TValue);
1532 setobj(L,luaH_set(L,t,key2tval(old)),gval(old));
1535 luaM_freearray(L,nold,twoto(oldhsize),Node);
1537 static void luaH_resizearray(lua_State*L,Table*t,int nasize){
1539 resize(L,t,nasize,nsize);
1541 static void rehash(lua_State*L,Table*t,const TValue*ek){
1553 resize(L,t,nasize,totaluse-na);
1555 static Table*luaH_new(lua_State*L,int narray,int nhash){
1556 Table*t=luaM_new(L,Table);
1557 luaC_link(L,obj2gco(t),5);
1564 setarrayvector(L,t,narray);
1565 setnodevector(L,t,nhash);
1568 static void luaH_free(lua_State*L,Table*t){
1570 luaM_freearray(L,t->node,sizenode(t),Node);
1571 luaM_freearray(L,t->array,t->sizearray,TValue);
1572 luaM_free(L,t);
1581 static TValue*newkey(lua_State*L,Table*t,const TValue*key){
1587 rehash(L,t,key);
1588 return luaH_set(L,t,key);
1605 luaC_barriert(L,t,key);
1654 static TValue*luaH_set(lua_State*L,Table*t,const TValue*key){
1660 if(ttisnil(key))luaG_runerror(L,"table index is nil");
1662 luaG_runerror(L,"table index is NaN");
1663 return newkey(L,t,key);
1666 static TValue*luaH_setnum(lua_State*L,Table*t,int key){
1673 return newkey(L,t,&k);
1676 static TValue*luaH_setstr(lua_State*L,Table*t,TString*key){
1682 setsvalue(L,&k,key);
1683 return newkey(L,t,&k);
1786 static size_t luaC_separateudata(lua_State*L,int all){
1787 global_State*g=G(L);
1794 else if(fasttm(L,gco2u(curr)->metatable,TM_GC)==NULL){
1882 static void checkstacksizes(lua_State*L,StkId max){
1883 int ci_used=cast_int(L->ci-L->base_ci);
1884 int s_used=cast_int(max-L->stack);
1885 if(L->size_ci>20000)
1887 if(4*ci_used<L->size_ci&&2*8<L->size_ci)
1888 luaD_reallocCI(L,L->size_ci/2);
1889 condhardstacktests(luaD_reallocCI(L,ci_used+1));
1890 if(4*s_used<L->stacksize&&
1891 2*((2*20)+5)<L->stacksize)
1892 luaD_reallocstack(L,L->stacksize/2);
1893 condhardstacktests(luaD_reallocstack(L,s_used));
1989 static void freeobj(lua_State*L,GCObject*o){
1991 case(8+1):luaF_freeproto(L,gco2p(o));break;
1992 case 6:luaF_freeclosure(L,gco2cl(o));break;
1993 case(8+2):luaF_freeupval(L,gco2uv(o));break;
1994 case 5:luaH_free(L,gco2h(o));break;
1996 luaE_freethread(L,gco2th(o));
2000 G(L)->strt.nuse--;
2001 luaM_freemem(L,o,sizestring(gco2ts(o)));
2005 luaM_freemem(L,o,sizeudata(gco2u(o)));
2011 #define sweepwholelist(L,p)sweeplist(L,p,((lu_mem)(~(lu_mem)0)-2))
2012 static GCObject**sweeplist(lua_State*L,GCObject**p,lu_mem count){
2014 global_State*g=G(L);
2018 sweepwholelist(L,&gco2th(curr)->openupval);
2027 freeobj(L,curr);
2032 static void checkSizes(lua_State*L){
2033 global_State*g=G(L);
2036 luaS_resize(L,g->strt.size/2);
2039 luaZ_resizebuffer(L,&g->buff,newsize);
2042 static void GCTM(lua_State*L){
2043 global_State*g=G(L);
2054 tm=fasttm(L,udata->uv.metatable,TM_GC);
2056 lu_byte oldah=L->allowhook;
2058 L->allowhook=0;
2060 setobj(L,L->top,tm);
2061 setuvalue(L,L->top+1,udata);
2062 L->top+=2;
2063 luaD_call(L,L->top-2,0);
2064 L->allowhook=oldah;
2068 static void luaC_callGCTM(lua_State*L){
2069 while(G(L)->tmudata)
2070 GCTM(L);
2072 static void luaC_freeall(lua_State*L){
2073 global_State*g=G(L);
2076 sweepwholelist(L,&g->rootgc);
2078 sweepwholelist(L,&g->strt.hash[i]);
2085 static void markroot(lua_State*L){
2086 global_State*g=G(L);
2092 markvalue(g,registry(L));
2103 static void atomic(lua_State*L){
2104 global_State*g=G(L);
2110 markobject(g,L);
2116 udsize=luaC_separateudata(L,0);
2126 static l_mem singlestep(lua_State*L){
2127 global_State*g=G(L);
2130 markroot(L);
2137 atomic(L);
2143 sweepwholelist(L,&g->strt.hash[g->sweepstrgc++]);
2151 g->sweepgc=sweeplist(L,g->sweepgc,40);
2153 checkSizes(L);
2161 GCTM(L);
2175 static void luaC_step(lua_State*L){
2176 global_State*g=G(L);
2182 lim-=singlestep(L);
2198 static void luaC_barrierf(lua_State*L,GCObject*o,GCObject*v){
2199 global_State*g=G(L);
2205 static void luaC_barrierback(lua_State*L,Table*t){
2206 global_State*g=G(L);
2212 static void luaC_link(lua_State*L,GCObject*o,lu_byte tt){
2213 global_State*g=G(L);
2219 static void luaC_linkupval(lua_State*L,UpVal*uv){
2220 global_State*g=G(L);
2227 luaC_barrier(L,uv,uv->v);
2249 struct lua_State*L;
2255 static void luaX_init(lua_State*L);
2264 static void stack_init(lua_State*L1,lua_State*L){
2265 L1->base_ci=luaM_newvector(L,8,CallInfo);
2269 L1->stack=luaM_newvector(L,(2*20)+5,TValue);
2278 static void freestack(lua_State*L,lua_State*L1){
2279 luaM_freearray(L,L1->base_ci,L1->size_ci,CallInfo);
2280 luaM_freearray(L,L1->stack,L1->stacksize,TValue);
2282 static void f_luaopen(lua_State*L,void*ud){
2283 global_State*g=G(L);
2285 stack_init(L,L);
2286 sethvalue(L,gt(L),luaH_new(L,0,2));
2287 sethvalue(L,registry(L),luaH_new(L,0,2));
2288 luaS_resize(L,32);
2289 luaT_init(L);
2290 luaX_init(L);
2291 luaS_fix(luaS_newliteral(L,"not enough memory"));
2294 static void preinit_state(lua_State*L,global_State*g){
2295 G(L)=g;
2296 L->stack=NULL;
2297 L->stacksize=0;
2298 L->errorJmp=NULL;
2299 L->hook=NULL;
2300 L->hookmask=0;
2301 L->basehookcount=0;
2302 L->allowhook=1;
2303 resethookcount(L);
2304 L->openupval=NULL;
2305 L->size_ci=0;
2306 L->nCcalls=L->baseCcalls=0;
2307 L->status=0;
2308 L->base_ci=L->ci=NULL;
2309 L->savedpc=NULL;
2310 L->errfunc=0;
2311 setnilvalue(gt(L));
2313 static void close_state(lua_State*L){
2314 global_State*g=G(L);
2315 luaF_close(L,L->stack);
2316 luaC_freeall(L);
2317 luaM_freearray(L,G(L)->strt.hash,G(L)->strt.size,TString*);
2318 luaZ_freebuffer(L,&g->buff);
2319 freestack(L,L);
2320 (*g->frealloc)(g->ud,fromstate(L),state_size(LG),0);
2322 static void luaE_freethread(lua_State*L,lua_State*L1){
2324 freestack(L,L1);
2325 luaM_freemem(L,fromstate(L1),state_size(lua_State));
2329 lua_State*L;
2333 L=tostate(l);
2334 g=&((LG*)L)->g;
2335 L->next=NULL;
2336 L->tt=8;
2338 L->marked=luaC_white(g);
2339 set2bits(L->marked,5,6);
2340 preinit_state(L,g);
2343 g->mainthread=L;
2350 setnilvalue(registry(L));
2351 luaZ_initbuffer(L,&g->buff);
2354 g->rootgc=obj2gco(L);
2366 if(luaD_rawrunprotected(L,f_luaopen,NULL)!=0){
2367 close_state(L);
2368 L=NULL;
2372 return L;
2374 static void callallgcTM(lua_State*L,void*ud){
2376 luaC_callGCTM(L);
2378 static void lua_close(lua_State*L){
2379 L=G(L)->mainthread;
2380 luaF_close(L,L->stack);
2381 luaC_separateudata(L,1);
2382 L->errfunc=0;
2384 L->ci=L->base_ci;
2385 L->base=L->top=L->ci->base;
2386 L->nCcalls=L->baseCcalls=0;
2387 }while(luaD_rawrunprotected(L,callallgcTM,NULL)!=0);
2388 close_state(L);
2398 static int currentpc(lua_State*L,CallInfo*ci){
2400 if(ci==L->ci)
2401 ci->savedpc=L->savedpc;
2404 static int currentline(lua_State*L,CallInfo*ci){
2405 int pc=currentpc(L,ci);
2411 static int lua_getstack(lua_State*L,int level,lua_Debug*ar){
2414 for(ci=L->ci;level>0&&ci>L->base_ci;ci--){
2419 if(level==0&&ci>L->base_ci){
2421 ar->i_ci=cast_int(ci-L->base_ci);
2456 static void collectvalidlines(lua_State*L,Closure*f){
2458 setnilvalue(L->top);
2461 Table*t=luaH_new(L,0,0);
2465 setbvalue(luaH_setnum(L,t,lineinfo[i]),1);
2466 sethvalue(L,L->top,t);
2468 incr_top(L);
2470 static int auxgetinfo(lua_State*L,const char*what,lua_Debug*ar,
2484 ar->currentline=(ci)?currentline(L,ci):-1;
2507 static int lua_getinfo(lua_State*L,const char*what,lua_Debug*ar){
2512 StkId func=L->top-1;
2513 luai_apicheck(L,ttisfunction(func));
2516 L->top--;
2519 ci=L->base_ci+ar->i_ci;
2522 status=auxgetinfo(L,what,ar,f,ci);
2524 if(f==NULL)setnilvalue(L->top);
2525 else setclvalue(L,L->top,f);
2526 incr_top(L);
2529 collectvalidlines(L,f);
2538 static void luaG_typeerror(lua_State*L,const TValue*o,const char*op){
2541 const char*kind=(isinstack(L->ci,o))?
2545 luaG_runerror(L,"attempt to %s %s "LUA_QL("%s")" (a %s value)",
2548 luaG_runerror(L,"attempt to %s a %s value",op,t);
2550 static void luaG_concaterror(lua_State*L,StkId p1,StkId p2){
2552 luaG_typeerror(L,p1,"concatenate");
2554 static void luaG_aritherror(lua_State*L,const TValue*p1,const TValue*p2){
2558 luaG_typeerror(L,p2,"perform arithmetic on");
2560 static int luaG_ordererror(lua_State*L,const TValue*p1,const TValue*p2){
2564 luaG_runerror(L,"attempt to compare two %s values",t1);
2566 luaG_runerror(L,"attempt to compare %s with %s",t1,t2);
2569 static void addinfo(lua_State*L,const char*msg){
2570 CallInfo*ci=L->ci;
2573 int line=currentline(L,ci);
2575 luaO_pushfstring(L,"%s:%d: %s",buff,line,msg);
2578 static void luaG_errormsg(lua_State*L){
2579 if(L->errfunc!=0){
2580 StkId errfunc=restorestack(L,L->errfunc);
2581 if(!ttisfunction(errfunc))luaD_throw(L,5);
2582 setobj(L,L->top,L->top-1);
2583 setobj(L,L->top-1,errfunc);
2584 incr_top(L);
2585 luaD_call(L,L->top-2,1);
2587 luaD_throw(L,2);
2589 static void luaG_runerror(lua_State*L,const char*fmt,...){
2592 addinfo(L,luaO_pushvfstring(L,fmt,argp));
2594 luaG_errormsg(L);
2598 lua_State*L=z->L;
2600 buff=z->reader(L,z->data,&size);
2606 static void luaZ_init(lua_State*L,ZIO*z,lua_Reader reader,void*data){
2607 z->L=L;
2613 static char*luaZ_openspace(lua_State*L,Mbuffer*buff,size_t n){
2616 luaZ_resizebuffer(L,buff,n);
2680 luaZ_resizebuffer(ls->L,b,newsize);
2684 static void luaX_init(lua_State*L){
2687 TString*ts=luaS_new(L,luaX_tokens[i]);
2694 return(iscntrl(token))?luaO_pushfstring(ls->L,"char(%d)",token):
2695 luaO_pushfstring(ls->L,"%c",token);
2714 msg=luaO_pushfstring(ls->L,"%s:%d: %s",buff,ls->linenumber,msg);
2716 luaO_pushfstring(ls->L,"%s near "LUA_QL("%s"),msg,txtToken(ls,token));
2717 luaD_throw(ls->L,3);
2723 lua_State*L=ls->L;
2724 TString*ts=luaS_newlstr(L,str,l);
2725 TValue*o=luaH_setstr(L,ls->fs->h,ts);
2728 luaC_checkGC(L);
2740 static void luaX_setinput(lua_State*L,LexState*ls,ZIO*z,TString*source){
2742 ls->L=L;
2749 luaZ_resizebuffer(ls->L,ls->buff,32);
3144 lua_State*L=fs->L;
3145 TValue*idx=luaH_set(L,fs->h,k);
3153 luaM_growvector(L,f->k,fs->nk,f->sizek,TValue,
3156 setobj(L,&f->k[fs->nk],v);
3157 luaC_barrier(L,f,v);
3163 setsvalue(fs->L,&o,s);
3179 sethvalue(fs->L,&k,fs->h);
3631 luaM_growvector(fs->L,f->code,fs->pc,f->sizecode,Instruction,
3634 luaM_growvector(fs->L,f->lineinfo,fs->pc,f->sizelineinfo,int,
3676 luaO_pushfstring(ls->L,LUA_QL("%s")" expected",luaX_token2str(ls,token)));
3680 luaO_pushfstring(fs->L,"main function has more than %d %s",limit,what):
3681 luaO_pushfstring(fs->L,"function at line %d has more than %d %s",
3706 luaX_syntaxerror(ls,luaO_pushfstring(ls->L,
3734 luaM_growvector(ls->L,f->locvars,fs->nlocvars,f->sizelocvars,
3738 luaC_objbarrier(ls->L,f,varname);
3769 luaM_growvector(fs->L,f->upvalues,f->nups,f->sizeupvalues,
3773 luaC_objbarrier(fs->L,f,name);
3838 if(++ls->L->nCcalls>200)
3841 #define leavelevel(ls)((ls)->L->nCcalls--)
3864 luaM_growvector(ls->L,f->p,fs->np,f->sizep,Proto*,
3868 luaC_objbarrier(ls->L,f,func->f);
3876 lua_State*L=ls->L;
3877 Proto*f=luaF_newproto(L);
3881 fs->L=L;
3894 fs->h=luaH_new(L,0,0);
3895 sethvalue(L,L->top,fs->h);
3896 incr_top(L);
3897 setptvalue(L,L->top,f);
3898 incr_top(L);
3901 lua_State*L=ls->L;
3906 luaM_reallocvector(L,f->code,f->sizecode,fs->pc,Instruction);
3908 luaM_reallocvector(L,f->lineinfo,f->sizelineinfo,fs->pc,int);
3910 luaM_reallocvector(L,f->k,f->sizek,fs->nk,TValue);
3912 luaM_reallocvector(L,f->p,f->sizep,fs->np,Proto*);
3914 luaM_reallocvector(L,f->locvars,f->sizelocvars,fs->nlocvars,LocVar);
3916 luaM_reallocvector(L,f->upvalues,f->sizeupvalues,f->nups,TString*);
3920 L->top-=2;
3922 static Proto*luaY_parser(lua_State*L,ZIO*z,Mbuffer*buff,const char*name){
3926 luaX_setinput(L,&lexstate,z,luaS_new(L,name));
4352 luaY_checklimit(ls->fs,nvars,200-ls->L->nCcalls,
4699 static int luaV_tostring(lua_State*L,StkId obj){
4706 setsvalue(L,obj,luaS_new(L,s));
4710 static void callTMres(lua_State*L,StkId res,const TValue*f,
4712 ptrdiff_t result=savestack(L,res);
4713 setobj(L,L->top,f);
4714 setobj(L,L->top+1,p1);
4715 setobj(L,L->top+2,p2);
4716 luaD_checkstack(L,3);
4717 L->top+=3;
4718 luaD_call(L,L->top-3,1);
4719 res=restorestack(L,result);
4720 L->top--;
4721 setobj(L,res,L->top);
4723 static void callTM(lua_State*L,const TValue*f,const TValue*p1,
4725 setobj(L,L->top,f);
4726 setobj(L,L->top+1,p1);
4727 setobj(L,L->top+2,p2);
4728 setobj(L,L->top+3,p3);
4729 luaD_checkstack(L,4);
4730 L->top+=4;
4731 luaD_call(L,L->top-4,0);
4733 static void luaV_gettable(lua_State*L,const TValue*t,TValue*key,StkId val){
4741 (tm=fasttm(L,h->metatable,TM_INDEX))==NULL){
4742 setobj(L,val,res);
4746 else if(ttisnil(tm=luaT_gettmbyobj(L,t,TM_INDEX)))
4747 luaG_typeerror(L,t,"index");
4749 callTMres(L,val,tm,t,key);
4754 luaG_runerror(L,"loop in gettable");
4756 static void luaV_settable(lua_State*L,const TValue*t,TValue*key,StkId val){
4763 TValue*oldval=luaH_set(L,h,key);
4765 (tm=fasttm(L,h->metatable,TM_NEWINDEX))==NULL){
4766 setobj(L,oldval,val);
4768 luaC_barriert(L,h,val);
4772 else if(ttisnil(tm=luaT_gettmbyobj(L,t,TM_NEWINDEX)))
4773 luaG_typeerror(L,t,"index");
4775 callTM(L,tm,t,key,val);
4778 setobj(L,&temp,tm);
4781 luaG_runerror(L,"loop in settable");
4783 static int call_binTM(lua_State*L,const TValue*p1,const TValue*p2,
4785 const TValue*tm=luaT_gettmbyobj(L,p1,event);
4787 tm=luaT_gettmbyobj(L,p2,event);
4789 callTMres(L,res,tm,p1,p2);
4792 static const TValue*get_compTM(lua_State*L,Table*mt1,Table*mt2,
4794 const TValue*tm1=fasttm(L,mt1,event);
4798 tm2=fasttm(L,mt2,event);
4804 static int call_orderTM(lua_State*L,const TValue*p1,const TValue*p2,
4806 const TValue*tm1=luaT_gettmbyobj(L,p1,event);
4809 tm2=luaT_gettmbyobj(L,p2,event);
4812 callTMres(L,L->top,tm1,p1,p2);
4813 return!l_isfalse(L->top);
4834 static int luaV_lessthan(lua_State*L,const TValue*l,const TValue*r){
4837 return luaG_ordererror(L,l,r);
4842 else if((res=call_orderTM(L,l,r,TM_LT))!=-1)
4844 return luaG_ordererror(L,l,r);
4846 static int lessequal(lua_State*L,const TValue*l,const TValue*r){
4849 return luaG_ordererror(L,l,r);
4854 else if((res=call_orderTM(L,l,r,TM_LE))!=-1)
4856 else if((res=call_orderTM(L,r,l,TM_LT))!=-1)
4858 return luaG_ordererror(L,l,r);
4860 static int luaV_equalval(lua_State*L,const TValue*t1,const TValue*t2){
4869 tm=get_compTM(L,uvalue(t1)->metatable,uvalue(t2)->metatable,
4875 tm=get_compTM(L,hvalue(t1)->metatable,hvalue(t2)->metatable,TM_EQ);
4881 callTMres(L,L->top,tm,t1,t2);
4882 return!l_isfalse(L->top);
4884 static void luaV_concat(lua_State*L,int total,int last){
4886 StkId top=L->base+last+1;
4888 if(!(ttisstring(top-2)||ttisnumber(top-2))||!tostring(L,top-1)){
4889 if(!call_binTM(L,top-2,top-1,top-2,TM_CONCAT))
4890 luaG_concaterror(L,top-2,top-1);
4892 (void)tostring(L,top-2);
4897 for(n=1;n<total&&tostring(L,top-n-1);n++){
4899 if(l>=((size_t)(~(size_t)0)-2)-tl)luaG_runerror(L,"string length overflow");
4902 buffer=luaZ_openspace(L,&G(L)->buff,tl);
4909 setsvalue(L,top-n,luaS_newlstr(L,buffer,tl));
4915 static void Arith(lua_State*L,StkId ra,const TValue*rb,
4933 else if(!call_binTM(L,rb,rc,ra,op))
4934 luaG_aritherror(L,rb,rc);
4936 #define runtime_check(L,c){if(!(c))break;}
4942 #define dojump(L,pc,i){(pc)+=(i);}
4943 #define Protect(x){L->savedpc=pc;{x;};base=L->base;}
4944 …ua_Number nb=nvalue(rb),nc=nvalue(rc);setnvalue(ra,op(nb,nc));}else Protect(Arith(L,ra,rb,rc,tm));}
4945 static void luaV_execute(lua_State*L,int nexeccalls){
4951 pc=L->savedpc;
4952 cl=&clvalue(L->ci->func)->l;
4953 base=L->base;
4961 setobj(L,ra,RB(i));
4965 setobj(L,ra,KBx(i));
4982 setobj(L,ra,cl->upvals[b]->v);
4988 sethvalue(L,&g,cl->env);
4989 Protect(luaV_gettable(L,&g,rb,ra));
4993 Protect(luaV_gettable(L,RB(i),RKC(i),ra));
4998 sethvalue(L,&g,cl->env);
4999 Protect(luaV_settable(L,&g,KBx(i),ra));
5004 setobj(L,uv->v,ra);
5005 luaC_barrier(L,uv,ra);
5009 Protect(luaV_settable(L,ra,RKB(i),RKC(i)));
5015 sethvalue(L,ra,luaH_new(L,luaO_fb2int(b),luaO_fb2int(c)));
5016 Protect(luaC_checkGC(L));
5021 setobj(L,ra+1,rb);
5022 Protect(luaV_gettable(L,rb,RKC(i),ra));
5056 Protect(Arith(L,ra,rb,rb,TM_UNM));
5078 if(!call_binTM(L,rb,(&luaO_nilobject_),ra,TM_LEN))
5079 luaG_typeerror(L,rb,"get length of");
5088 Protect(luaV_concat(L,c-b+1,c);luaC_checkGC(L));
5089 setobj(L,RA(i),base+b);
5093 dojump(L,pc,GETARG_sBx(i));
5100 if(equalobj(L,rb,rc)==GETARG_A(i))
5101 dojump(L,pc,GETARG_sBx(*pc));
5108 if(luaV_lessthan(L,RKB(i),RKC(i))==GETARG_A(i))
5109 dojump(L,pc,GETARG_sBx(*pc));
5116 if(lessequal(L,RKB(i),RKC(i))==GETARG_A(i))
5117 dojump(L,pc,GETARG_sBx(*pc));
5124 dojump(L,pc,GETARG_sBx(*pc));
5131 setobj(L,ra,rb);
5132 dojump(L,pc,GETARG_sBx(*pc));
5140 if(b!=0)L->top=ra+b;
5141 L->savedpc=pc;
5142 switch(luaD_precall(L,ra,nresults)){
5148 if(nresults>=0)L->top=L->ci->top;
5149 base=L->base;
5159 if(b!=0)L->top=ra+b;
5160 L->savedpc=pc;
5161 switch(luaD_precall(L,ra,(-1))){
5163 CallInfo*ci=L->ci-1;
5167 if(L->openupval)luaF_close(L,ci->base);
5168 L->base=ci->base=ci->func+((ci+1)->base-pfunc);
5169 for(aux=0;pfunc+aux<L->top;aux++)
5170 setobj(L,func+aux,pfunc+aux);
5171 ci->top=L->top=func+aux;
5172 ci->savedpc=L->savedpc;
5174 L->ci--;
5178 base=L->base;
5188 if(b!=0)L->top=ra+b-1;
5189 if(L->openupval)luaF_close(L,base);
5190 L->savedpc=pc;
5191 b=luaD_poscall(L,ra);
5195 if(b)L->top=L->ci->top;
5205 dojump(L,pc,GETARG_sBx(i));
5215 L->savedpc=pc;
5217 luaG_runerror(L,LUA_QL("for")" initial value must be a number");
5219 luaG_runerror(L,LUA_QL("for")" limit must be a number");
5221 luaG_runerror(L,LUA_QL("for")" step must be a number");
5223 dojump(L,pc,GETARG_sBx(i));
5228 setobj(L,cb+2,ra+2);
5229 setobj(L,cb+1,ra+1);
5230 setobj(L,cb,ra);
5231 L->top=cb+3;
5232 Protect(luaD_call(L,cb,GETARG_C(i)));
5233 L->top=L->ci->top;
5236 setobj(L,cb-1,cb);
5237 dojump(L,pc,GETARG_sBx(*pc));
5248 n=cast_int(L->top-ra)-1;
5249 L->top=L->ci->top;
5252 runtime_check(L,ttistable(ra));
5256 luaH_resizearray(L,h,last);
5259 setobj(L,luaH_setnum(L,h,last--),val);
5260 luaC_barriert(L,h,val);
5265 luaF_close(L,ra);
5274 ncl=luaF_newLclosure(L,nup,cl->env);
5280 ncl->l.upvals[j]=luaF_findupval(L,base+GETARG_B(*pc));
5283 setclvalue(L,ra,ncl);
5284 Protect(luaC_checkGC(L));
5290 CallInfo*ci=L->ci;
5293 Protect(luaD_checkstack(L,n));
5296 L->top=ra+n;
5300 setobj(L,ra+j,ci->base-n+j);
5311 #define api_checknelems(L,n)luai_apicheck(L,(n)<=(L->top-L->base))
5312 #define api_checkvalidindex(L,i)luai_apicheck(L,(i)!=(&luaO_nilobject_))
5313 #define api_incr_top(L){luai_apicheck(L,L->top<L->ci->top);L->top++;}
5314 static TValue*index2adr(lua_State*L,int idx){
5316 TValue*o=L->base+(idx-1);
5317 luai_apicheck(L,idx<=L->ci->top-L->base);
5318 if(o>=L->top)return cast(TValue*,(&luaO_nilobject_));
5322 luai_apicheck(L,idx!=0&&-idx<=L->top-L->base);
5323 return L->top+idx;
5326 case(-10000):return registry(L);
5328 Closure*func=curr_func(L);
5329 sethvalue(L,&L->env,func->c.env);
5330 return&L->env;
5332 case(-10002):return gt(L);
5334 Closure*func=curr_func(L);
5342 static Table*getcurrenv(lua_State*L){
5343 if(L->ci==L->base_ci)
5344 return hvalue(gt(L));
5346 Closure*func=curr_func(L);
5350 static int lua_checkstack(lua_State*L,int size){
5352 if(size>8000||(L->top-L->base+size)>8000)
5355 luaD_checkstack(L,size);
5356 if(L->ci->top<L->top+size)
5357 L->ci->top=L->top+size;
5361 static lua_CFunction lua_atpanic(lua_State*L,lua_CFunction panicf){
5363 old=G(L)->panic;
5364 G(L)->panic=panicf;
5367 static int lua_gettop(lua_State*L){
5368 return cast_int(L->top-L->base);
5370 static void lua_settop(lua_State*L,int idx){
5372 luai_apicheck(L,idx<=L->stack_last-L->base);
5373 while(L->top<L->base+idx)
5374 setnilvalue(L->top++);
5375 L->top=L->base+idx;
5378 luai_apicheck(L,-(idx+1)<=(L->top-L->base));
5379 L->top+=idx+1;
5382 static void lua_remove(lua_State*L,int idx){
5384 p=index2adr(L,idx);
5385 api_checkvalidindex(L,p);
5386 while(++p<L->top)setobj(L,p-1,p);
5387 L->top--;
5389 static void lua_insert(lua_State*L,int idx){
5392 p=index2adr(L,idx);
5393 api_checkvalidindex(L,p);
5394 for(q=L->top;q>p;q--)setobj(L,q,q-1);
5395 setobj(L,p,L->top);
5397 static void lua_replace(lua_State*L,int idx){
5399 if(idx==(-10001)&&L->ci==L->base_ci)
5400 luaG_runerror(L,"no calling environment");
5401 api_checknelems(L,1);
5402 o=index2adr(L,idx);
5403 api_checkvalidindex(L,o);
5405 Closure*func=curr_func(L);
5406 luai_apicheck(L,ttistable(L->top-1));
5407 func->c.env=hvalue(L->top-1);
5408 luaC_barrier(L,func,L->top-1);
5411 setobj(L,o,L->top-1);
5413 luaC_barrier(L,curr_func(L),L->top-1);
5415 L->top--;
5417 static void lua_pushvalue(lua_State*L,int idx){
5418 setobj(L,L->top,index2adr(L,idx));
5419 api_incr_top(L);
5421 static int lua_type(lua_State*L,int idx){
5422 StkId o=index2adr(L,idx);
5425 static const char*lua_typename(lua_State*L,int t){
5426 UNUSED(L);
5429 static int lua_iscfunction(lua_State*L,int idx){
5430 StkId o=index2adr(L,idx);
5433 static int lua_isnumber(lua_State*L,int idx){
5435 const TValue*o=index2adr(L,idx);
5438 static int lua_isstring(lua_State*L,int idx){
5439 int t=lua_type(L,idx);
5442 static int lua_rawequal(lua_State*L,int index1,int index2){
5443 StkId o1=index2adr(L,index1);
5444 StkId o2=index2adr(L,index2);
5448 static int lua_lessthan(lua_State*L,int index1,int index2){
5451 o1=index2adr(L,index1);
5452 o2=index2adr(L,index2);
5454 :luaV_lessthan(L,o1,o2);
5457 static lua_Number lua_tonumber(lua_State*L,int idx){
5459 const TValue*o=index2adr(L,idx);
5465 static lua_Integer lua_tointeger(lua_State*L,int idx){
5467 const TValue*o=index2adr(L,idx);
5477 static int lua_toboolean(lua_State*L,int idx){
5478 const TValue*o=index2adr(L,idx);
5481 static const char*lua_tolstring(lua_State*L,int idx,size_t*len){
5482 StkId o=index2adr(L,idx);
5484 if(!luaV_tostring(L,o)){
5488 luaC_checkGC(L);
5489 o=index2adr(L,idx);
5494 static size_t lua_objlen(lua_State*L,int idx){
5495 StkId o=index2adr(L,idx);
5502 l=(luaV_tostring(L,o)?tsvalue(o)->len:0);
5508 static lua_CFunction lua_tocfunction(lua_State*L,int idx){
5509 StkId o=index2adr(L,idx);
5512 static void*lua_touserdata(lua_State*L,int idx){
5513 StkId o=index2adr(L,idx);
5520 static void lua_pushnil(lua_State*L){
5521 setnilvalue(L->top);
5522 api_incr_top(L);
5524 static void lua_pushnumber(lua_State*L,lua_Number n){
5525 setnvalue(L->top,n);
5526 api_incr_top(L);
5528 static void lua_pushinteger(lua_State*L,lua_Integer n){
5529 setnvalue(L->top,cast_num(n));
5530 api_incr_top(L);
5532 static void lua_pushlstring(lua_State*L,const char*s,size_t len){
5533 luaC_checkGC(L);
5534 setsvalue(L,L->top,luaS_newlstr(L,s,len));
5535 api_incr_top(L);
5537 static void lua_pushstring(lua_State*L,const char*s){
5539 lua_pushnil(L);
5541 lua_pushlstring(L,s,strlen(s));
5543 static const char*lua_pushvfstring(lua_State*L,const char*fmt,
5546 luaC_checkGC(L);
5547 ret=luaO_pushvfstring(L,fmt,argp);
5550 static const char*lua_pushfstring(lua_State*L,const char*fmt,...){
5553 luaC_checkGC(L);
5555 ret=luaO_pushvfstring(L,fmt,argp);
5559 static void lua_pushcclosure(lua_State*L,lua_CFunction fn,int n){
5561 luaC_checkGC(L);
5562 api_checknelems(L,n);
5563 cl=luaF_newCclosure(L,n,getcurrenv(L));
5565 L->top-=n;
5567 setobj(L,&cl->c.upvalue[n],L->top+n);
5568 setclvalue(L,L->top,cl);
5569 api_incr_top(L);
5571 static void lua_pushboolean(lua_State*L,int b){
5572 setbvalue(L->top,(b!=0));
5573 api_incr_top(L);
5575 static int lua_pushthread(lua_State*L){
5576 setthvalue(L,L->top,L);
5577 api_incr_top(L);
5578 return(G(L)->mainthread==L);
5580 static void lua_gettable(lua_State*L,int idx){
5582 t=index2adr(L,idx);
5583 api_checkvalidindex(L,t);
5584 luaV_gettable(L,t,L->top-1,L->top-1);
5586 static void lua_getfield(lua_State*L,int idx,const char*k){
5589 t=index2adr(L,idx);
5590 api_checkvalidindex(L,t);
5591 setsvalue(L,&key,luaS_new(L,k));
5592 luaV_gettable(L,t,&key,L->top);
5593 api_incr_top(L);
5595 static void lua_rawget(lua_State*L,int idx){
5597 t=index2adr(L,idx);
5598 luai_apicheck(L,ttistable(t));
5599 setobj(L,L->top-1,luaH_get(hvalue(t),L->top-1));
5601 static void lua_rawgeti(lua_State*L,int idx,int n){
5603 o=index2adr(L,idx);
5604 luai_apicheck(L,ttistable(o));
5605 setobj(L,L->top,luaH_getnum(hvalue(o),n));
5606 api_incr_top(L);
5608 static void lua_createtable(lua_State*L,int narray,int nrec){
5609 luaC_checkGC(L);
5610 sethvalue(L,L->top,luaH_new(L,narray,nrec));
5611 api_incr_top(L);
5613 static int lua_getmetatable(lua_State*L,int objindex){
5617 obj=index2adr(L,objindex);
5626 mt=G(L)->mt[ttype(obj)];
5632 sethvalue(L,L->top,mt);
5633 api_incr_top(L);
5638 static void lua_getfenv(lua_State*L,int idx){
5640 o=index2adr(L,idx);
5641 api_checkvalidindex(L,o);
5644 sethvalue(L,L->top,clvalue(o)->c.env);
5647 sethvalue(L,L->top,uvalue(o)->env);
5650 setobj(L,L->top,gt(thvalue(o)));
5653 setnilvalue(L->top);
5656 api_incr_top(L);
5658 static void lua_settable(lua_State*L,int idx){
5660 api_checknelems(L,2);
5661 t=index2adr(L,idx);
5662 api_checkvalidindex(L,t);
5663 luaV_settable(L,t,L->top-2,L->top-1);
5664 L->top-=2;
5666 static void lua_setfield(lua_State*L,int idx,const char*k){
5669 api_checknelems(L,1);
5670 t=index2adr(L,idx);
5671 api_checkvalidindex(L,t);
5672 setsvalue(L,&key,luaS_new(L,k));
5673 luaV_settable(L,t,&key,L->top-1);
5674 L->top--;
5676 static void lua_rawset(lua_State*L,int idx){
5678 api_checknelems(L,2);
5679 t=index2adr(L,idx);
5680 luai_apicheck(L,ttistable(t));
5681 setobj(L,luaH_set(L,hvalue(t),L->top-2),L->top-1);
5682 luaC_barriert(L,hvalue(t),L->top-1);
5683 L->top-=2;
5685 static void lua_rawseti(lua_State*L,int idx,int n){
5687 api_checknelems(L,1);
5688 o=index2adr(L,idx);
5689 luai_apicheck(L,ttistable(o));
5690 setobj(L,luaH_setnum(L,hvalue(o),n),L->top-1);
5691 luaC_barriert(L,hvalue(o),L->top-1);
5692 L->top--;
5694 static int lua_setmetatable(lua_State*L,int objindex){
5697 api_checknelems(L,1);
5698 obj=index2adr(L,objindex);
5699 api_checkvalidindex(L,obj);
5700 if(ttisnil(L->top-1))
5703 luai_apicheck(L,ttistable(L->top-1));
5704 mt=hvalue(L->top-1);
5710 luaC_objbarriert(L,hvalue(obj),mt);
5716 luaC_objbarrier(L,rawuvalue(obj),mt);
5720 G(L)->mt[ttype(obj)]=mt;
5724 L->top--;
5727 static int lua_setfenv(lua_State*L,int idx){
5730 api_checknelems(L,1);
5731 o=index2adr(L,idx);
5732 api_checkvalidindex(L,o);
5733 luai_apicheck(L,ttistable(L->top-1));
5736 clvalue(o)->c.env=hvalue(L->top-1);
5739 uvalue(o)->env=hvalue(L->top-1);
5742 sethvalue(L,gt(thvalue(o)),hvalue(L->top-1));
5748 if(res)luaC_objbarrier(L,gcvalue(o),hvalue(L->top-1));
5749 L->top--;
5752 #define adjustresults(L,nres){if(nres==(-1)&&L->top>=L->ci->top)L->ci->top=L->top;}
5753 #define checkresults(L,na,nr)luai_apicheck(L,(nr)==(-1)||(L->ci->top-L->top>=(nr)-(na)))
5754 static void lua_call(lua_State*L,int nargs,int nresults){
5756 api_checknelems(L,nargs+1);
5757 checkresults(L,nargs,nresults);
5758 func=L->top-(nargs+1);
5759 luaD_call(L,func,nresults);
5760 adjustresults(L,nresults);
5766 static void f_call(lua_State*L,void*ud){
5768 luaD_call(L,c->func,c->nresults);
5770 static int lua_pcall(lua_State*L,int nargs,int nresults,int errfunc){
5774 api_checknelems(L,nargs+1);
5775 checkresults(L,nargs,nresults);
5779 StkId o=index2adr(L,errfunc);
5780 api_checkvalidindex(L,o);
5781 func=savestack(L,o);
5783 c.func=L->top-(nargs+1);
5785 status=luaD_pcall(L,f_call,&c,savestack(L,c.func),func);
5786 adjustresults(L,nresults);
5789 static int lua_load(lua_State*L,lua_Reader reader,void*data,
5794 luaZ_init(L,&z,reader,data);
5795 status=luaD_protectedparser(L,&z,chunkname);
5798 static int lua_error(lua_State*L){
5799 api_checknelems(L,1);
5800 luaG_errormsg(L);
5803 static int lua_next(lua_State*L,int idx){
5806 t=index2adr(L,idx);
5807 luai_apicheck(L,ttistable(t));
5808 more=luaH_next(L,hvalue(t),L->top-1);
5810 api_incr_top(L);
5813 L->top-=1;
5816 static void lua_concat(lua_State*L,int n){
5817 api_checknelems(L,n);
5819 luaC_checkGC(L);
5820 luaV_concat(L,n,cast_int(L->top-L->base)-1);
5821 L->top-=(n-1);
5824 setsvalue(L,L->top,luaS_newlstr(L,"",0));
5825 api_incr_top(L);
5828 static void*lua_newuserdata(lua_State*L,size_t size){
5830 luaC_checkGC(L);
5831 u=luaS_newudata(L,size,getcurrenv(L));
5832 setuvalue(L,L->top,u);
5833 api_incr_top(L);
5836 #define luaL_getn(L,i)((int)lua_objlen(L,i))
5837 #define luaL_setn(L,i,j)((void)0)
5842 static void luaI_openlib(lua_State*L,const char*libname,
5844 static int luaL_argerror(lua_State*L,int numarg,const char*extramsg);
5845 static const char* luaL_checklstring(lua_State*L,int numArg,
5847 static const char* luaL_optlstring(lua_State*L,int numArg,
5849 static lua_Integer luaL_checkinteger(lua_State*L,int numArg);
5850 static lua_Integer luaL_optinteger(lua_State*L,int nArg,
5852 static int luaL_error(lua_State*L,const char*fmt,...);
5853 static const char* luaL_findtable(lua_State*L,int idx,
5855 #define luaL_argcheck(L,cond,numarg,extramsg)((void)((cond)||luaL_argerror(L,(numarg),(extramsg))))
5856 #define luaL_checkstring(L,n)(luaL_checklstring(L,(n),NULL))
5857 #define luaL_optstring(L,n,d)(luaL_optlstring(L,(n),(d),NULL))
5858 #define luaL_checkint(L,n)((int)luaL_checkinteger(L,(n)))
5859 #define luaL_optint(L,n,d)((int)luaL_optinteger(L,(n),(d)))
5860 #define luaL_typename(L,i)lua_typename(L,lua_type(L,(i)))
5861 #define luaL_getmetatable(L,n)(lua_getfield(L,(-10000),(n)))
5862 #define luaL_opt(L,f,n,d)(lua_isnoneornil(L,(n))?(d):f(L,(n)))
5866 lua_State*L;
5872 static int luaL_argerror(lua_State*L,int narg,const char*extramsg){
5874 if(!lua_getstack(L,0,&ar))
5875 return luaL_error(L,"bad argument #%d (%s)",narg,extramsg);
5876 lua_getinfo(L,"n",&ar);
5880 return luaL_error(L,"calling "LUA_QL("%s")" on bad self (%s)",
5885 return luaL_error(L,"bad argument #%d to "LUA_QL("%s")" (%s)",
5888 static int luaL_typerror(lua_State*L,int narg,const char*tname){
5889 const char*msg=lua_pushfstring(L,"%s expected, got %s",
5890 tname,luaL_typename(L,narg));
5891 return luaL_argerror(L,narg,msg);
5893 static void tag_error(lua_State*L,int narg,int tag){
5894 luaL_typerror(L,narg,lua_typename(L,tag));
5896 static void luaL_where(lua_State*L,int level){
5898 if(lua_getstack(L,level,&ar)){
5899 lua_getinfo(L,"Sl",&ar);
5901 lua_pushfstring(L,"%s:%d: ",ar.short_src,ar.currentline);
5905 lua_pushliteral(L,"");
5907 static int luaL_error(lua_State*L,const char*fmt,...){
5910 luaL_where(L,1);
5911 lua_pushvfstring(L,fmt,argp);
5913 lua_concat(L,2);
5914 return lua_error(L);
5916 static int luaL_newmetatable(lua_State*L,const char*tname){
5917 lua_getfield(L,(-10000),tname);
5918 if(!lua_isnil(L,-1))
5920 lua_pop(L,1);
5921 lua_newtable(L);
5922 lua_pushvalue(L,-1);
5923 lua_setfield(L,(-10000),tname);
5926 static void*luaL_checkudata(lua_State*L,int ud,const char*tname){
5927 void*p=lua_touserdata(L,ud);
5929 if(lua_getmetatable(L,ud)){
5930 lua_getfield(L,(-10000),tname);
5931 if(lua_rawequal(L,-1,-2)){
5932 lua_pop(L,2);
5937 luaL_typerror(L,ud,tname);
5940 static void luaL_checkstack(lua_State*L,int space,const char*mes){
5941 if(!lua_checkstack(L,space))
5942 luaL_error(L,"stack overflow (%s)",mes);
5944 static void luaL_checktype(lua_State*L,int narg,int t){
5945 if(lua_type(L,narg)!=t)
5946 tag_error(L,narg,t);
5948 static void luaL_checkany(lua_State*L,int narg){
5949 if(lua_type(L,narg)==(-1))
5950 luaL_argerror(L,narg,"value expected");
5952 static const char*luaL_checklstring(lua_State*L,int narg,size_t*len){
5953 const char*s=lua_tolstring(L,narg,len);
5954 if(!s)tag_error(L,narg,4);
5957 static const char*luaL_optlstring(lua_State*L,int narg,
5959 if(lua_isnoneornil(L,narg)){
5964 else return luaL_checklstring(L,narg,len);
5966 static lua_Number luaL_checknumber(lua_State*L,int narg){
5967 lua_Number d=lua_tonumber(L,narg);
5968 if(d==0&&!lua_isnumber(L,narg))
5969 tag_error(L,narg,3);
5972 static lua_Integer luaL_checkinteger(lua_State*L,int narg){
5973 lua_Integer d=lua_tointeger(L,narg);
5974 if(d==0&&!lua_isnumber(L,narg))
5975 tag_error(L,narg,3);
5978 static lua_Integer luaL_optinteger(lua_State*L,int narg,
5980 return luaL_opt(L,luaL_checkinteger,narg,def);
5982 static int luaL_getmetafield(lua_State*L,int obj,const char*event){
5983 if(!lua_getmetatable(L,obj))
5985 lua_pushstring(L,event);
5986 lua_rawget(L,-2);
5987 if(lua_isnil(L,-1)){
5988 lua_pop(L,2);
5992 lua_remove(L,-2);
5996 static void luaL_register(lua_State*L,const char*libname,
5998 luaI_openlib(L,libname,l,0);
6005 static void luaI_openlib(lua_State*L,const char*libname,
6009 luaL_findtable(L,(-10000),"_LOADED",1);
6010 lua_getfield(L,-1,libname);
6011 if(!lua_istable(L,-1)){
6012 lua_pop(L,1);
6013 if(luaL_findtable(L,(-10002),libname,size)!=NULL)
6014 luaL_error(L,"name conflict for module "LUA_QL("%s"),libname);
6015 lua_pushvalue(L,-1);
6016 lua_setfield(L,-3,libname);
6018 lua_remove(L,-2);
6019 lua_insert(L,-(nup+1));
6024 lua_pushvalue(L,-nup);
6025 lua_pushcclosure(L,l->func,nup);
6026 lua_setfield(L,-(nup+2),l->name);
6028 lua_pop(L,nup);
6030 static const char*luaL_findtable(lua_State*L,int idx,
6033 lua_pushvalue(L,idx);
6037 lua_pushlstring(L,fname,e-fname);
6038 lua_rawget(L,-2);
6039 if(lua_isnil(L,-1)){
6040 lua_pop(L,1);
6041 lua_createtable(L,0,(*e=='.'?1:szhint));
6042 lua_pushlstring(L,fname,e-fname);
6043 lua_pushvalue(L,-2);
6044 lua_settable(L,-4);
6046 else if(!lua_istable(L,-1)){
6047 lua_pop(L,2);
6050 lua_remove(L,-2);
6061 lua_pushlstring(B->L,B->buffer,l);
6069 lua_State*L=B->L;
6071 size_t toplen=lua_strlen(L,-1);
6073 size_t l=lua_strlen(L,-(toget+1));
6080 lua_concat(L,toget);
6095 lua_concat(B->L,B->lvl);
6099 lua_State*L=B->L;
6101 const char*s=lua_tolstring(L,-1,&vl);
6105 lua_pop(L,1);
6109 lua_insert(L,-2);
6114 static void luaL_buffinit(lua_State*L,luaL_Buffer*B){
6115 B->L=L;
6124 static const char*getF(lua_State*L,void*ud,size_t*size){
6126 (void)L;
6136 static int errfile(lua_State*L,const char*what,int fnameindex){
6138 const char*filename=lua_tostring(L,fnameindex)+1;
6139 lua_pushfstring(L,"cannot %s %s: %s",what,filename,serr);
6140 lua_remove(L,fnameindex);
6143 static int luaL_loadfile(lua_State*L,const char*filename){
6147 int fnameindex=lua_gettop(L)+1;
6150 lua_pushliteral(L,"=stdin");
6154 lua_pushfstring(L,"@%s",filename);
6156 if(lf.f==NULL)return errfile(L,"open",fnameindex);
6166 if(lf.f==NULL)return errfile(L,"reopen",fnameindex);
6171 status=lua_load(L,getF,&lf,lua_tostring(L,-1));
6175 lua_settop(L,fnameindex);
6176 return errfile(L,"read",fnameindex);
6178 lua_remove(L,fnameindex);
6185 static const char*getS(lua_State*L,void*ud,size_t*size){
6187 (void)L;
6193 static int luaL_loadbuffer(lua_State*L,const char*buff,size_t size,
6198 return lua_load(L,getS,&ls,name);
6210 static int panic(lua_State*L){
6211 (void)L;
6213 lua_tostring(L,-1));
6217 lua_State*L=lua_newstate(l_alloc,NULL);
6218 if(L)lua_atpanic(L,&panic);
6219 return L;
6221 static int luaB_tonumber(lua_State*L){
6222 int base=luaL_optint(L,2,10);
6224 luaL_checkany(L,1);
6225 if(lua_isnumber(L,1)){
6226 lua_pushnumber(L,lua_tonumber(L,1));
6231 const char*s1=luaL_checkstring(L,1);
6234 luaL_argcheck(L,2<=base&&base<=36,2,"base out of range");
6239 lua_pushnumber(L,(lua_Number)n);
6244 lua_pushnil(L);
6247 static int luaB_error(lua_State*L){
6248 int level=luaL_optint(L,2,1);
6249 lua_settop(L,1);
6250 if(lua_isstring(L,1)&&level>0){
6251 luaL_where(L,level);
6252 lua_pushvalue(L,1);
6253 lua_concat(L,2);
6255 return lua_error(L);
6257 static int luaB_setmetatable(lua_State*L){
6258 int t=lua_type(L,2);
6259 luaL_checktype(L,1,5);
6260 luaL_argcheck(L,t==0||t==5,2,
6262 if(luaL_getmetafield(L,1,"__metatable"))
6263 luaL_error(L,"cannot change a protected metatable");
6264 lua_settop(L,2);
6265 lua_setmetatable(L,1);
6268 static void getfunc(lua_State*L,int opt){
6269 if(lua_isfunction(L,1))lua_pushvalue(L,1);
6272 int level=opt?luaL_optint(L,1,1):luaL_checkint(L,1);
6273 luaL_argcheck(L,level>=0,1,"level must be non-negative");
6274 if(lua_getstack(L,level,&ar)==0)
6275 luaL_argerror(L,1,"invalid level");
6276 lua_getinfo(L,"f",&ar);
6277 if(lua_isnil(L,-1))
6278 luaL_error(L,"no function environment for tail call at level %d",
6282 static int luaB_setfenv(lua_State*L){
6283 luaL_checktype(L,2,5);
6284 getfunc(L,0);
6285 lua_pushvalue(L,2);
6286 if(lua_isnumber(L,1)&&lua_tonumber(L,1)==0){
6287 lua_pushthread(L);
6288 lua_insert(L,-2);
6289 lua_setfenv(L,-2);
6292 else if(lua_iscfunction(L,-2)||lua_setfenv(L,-2)==0)
6293 luaL_error(L,
6297 static int luaB_rawget(lua_State*L){
6298 luaL_checktype(L,1,5);
6299 luaL_checkany(L,2);
6300 lua_settop(L,2);
6301 lua_rawget(L,1);
6304 static int luaB_type(lua_State*L){
6305 luaL_checkany(L,1);
6306 lua_pushstring(L,luaL_typename(L,1));
6309 static int luaB_next(lua_State*L){
6310 luaL_checktype(L,1,5);
6311 lua_settop(L,2);
6312 if(lua_next(L,1))
6315 lua_pushnil(L);
6319 static int luaB_pairs(lua_State*L){
6320 luaL_checktype(L,1,5);
6321 lua_pushvalue(L,lua_upvalueindex(1));
6322 lua_pushvalue(L,1);
6323 lua_pushnil(L);
6326 static int ipairsaux(lua_State*L){
6327 int i=luaL_checkint(L,2);
6328 luaL_checktype(L,1,5);
6330 lua_pushinteger(L,i);
6331 lua_rawgeti(L,1,i);
6332 return(lua_isnil(L,-1))?0:2;
6334 static int luaB_ipairs(lua_State*L){
6335 luaL_checktype(L,1,5);
6336 lua_pushvalue(L,lua_upvalueindex(1));
6337 lua_pushvalue(L,1);
6338 lua_pushinteger(L,0);
6341 static int load_aux(lua_State*L,int status){
6345 lua_pushnil(L);
6346 lua_insert(L,-2);
6350 static int luaB_loadstring(lua_State*L){
6352 const char*s=luaL_checklstring(L,1,&l);
6353 const char*chunkname=luaL_optstring(L,2,s);
6354 return load_aux(L,luaL_loadbuffer(L,s,l,chunkname));
6356 static int luaB_loadfile(lua_State*L){
6357 const char*fname=luaL_optstring(L,1,NULL);
6358 return load_aux(L,luaL_loadfile(L,fname));
6360 static int luaB_assert(lua_State*L){
6361 luaL_checkany(L,1);
6362 if(!lua_toboolean(L,1))
6363 return luaL_error(L,"%s",luaL_optstring(L,2,"assertion failed!"));
6364 return lua_gettop(L);
6366 static int luaB_unpack(lua_State*L){
6368 luaL_checktype(L,1,5);
6369 i=luaL_optint(L,2,1);
6370 e=luaL_opt(L,luaL_checkint,3,luaL_getn(L,1));
6373 if(n<=0||!lua_checkstack(L,n))
6374 return luaL_error(L,"too many results to unpack");
6375 lua_rawgeti(L,1,i);
6377 lua_rawgeti(L,1,i);
6380 static int luaB_pcall(lua_State*L){
6382 luaL_checkany(L,1);
6383 status=lua_pcall(L,lua_gettop(L)-1,(-1),0);
6384 lua_pushboolean(L,(status==0));
6385 lua_insert(L,1);
6386 return lua_gettop(L);
6388 static int luaB_newproxy(lua_State*L){
6389 lua_settop(L,1);
6390 lua_newuserdata(L,0);
6391 if(lua_toboolean(L,1)==0)
6393 else if(lua_isboolean(L,1)){
6394 lua_newtable(L);
6395 lua_pushvalue(L,-1);
6396 lua_pushboolean(L,1);
6397 lua_rawset(L,lua_upvalueindex(1));
6401 if(lua_getmetatable(L,1)){
6402 lua_rawget(L,lua_upvalueindex(1));
6403 validproxy=lua_toboolean(L,-1);
6404 lua_pop(L,1);
6406 luaL_argcheck(L,validproxy,1,"boolean or proxy expected");
6407 lua_getmetatable(L,1);
6409 lua_setmetatable(L,2);
6427 static void auxopen(lua_State*L,const char*name,
6429 lua_pushcfunction(L,u);
6430 lua_pushcclosure(L,f,1);
6431 lua_setfield(L,-2,name);
6433 static void base_open(lua_State*L){
6434 lua_pushvalue(L,(-10002));
6435 lua_setglobal(L,"_G");
6436 luaL_register(L,"_G",base_funcs);
6437 lua_pushliteral(L,"Lua 5.1");
6438 lua_setglobal(L,"_VERSION");
6439 auxopen(L,"ipairs",luaB_ipairs,ipairsaux);
6440 auxopen(L,"pairs",luaB_pairs,luaB_next);
6441 lua_createtable(L,0,1);
6442 lua_pushvalue(L,-1);
6443 lua_setmetatable(L,-2);
6444 lua_pushliteral(L,"kv");
6445 lua_setfield(L,-2,"__mode");
6446 lua_pushcclosure(L,luaB_newproxy,1);
6447 lua_setglobal(L,"newproxy");
6449 static int luaopen_base(lua_State*L){
6450 base_open(L);
6453 #define aux_getn(L,n)(luaL_checktype(L,n,5),luaL_getn(L,n))
6454 static int tinsert(lua_State*L){
6455 int e=aux_getn(L,1)+1;
6457 switch(lua_gettop(L)){
6464 pos=luaL_checkint(L,2);
6467 lua_rawgeti(L,1,i-1);
6468 lua_rawseti(L,1,i);
6473 return luaL_error(L,"wrong number of arguments to "LUA_QL("insert"));
6476 luaL_setn(L,1,e);
6477 lua_rawseti(L,1,pos);
6480 static int tremove(lua_State*L){
6481 int e=aux_getn(L,1);
6482 int pos=luaL_optint(L,2,e);
6485 luaL_setn(L,1,e-1);
6486 lua_rawgeti(L,1,pos);
6488 lua_rawgeti(L,1,pos+1);
6489 lua_rawseti(L,1,pos);
6491 lua_pushnil(L);
6492 lua_rawseti(L,1,e);
6495 static void addfield(lua_State*L,luaL_Buffer*b,int i){
6496 lua_rawgeti(L,1,i);
6497 if(!lua_isstring(L,-1))
6498 luaL_error(L,"invalid value (%s) at index %d in table for "
6499 LUA_QL("concat"),luaL_typename(L,-1),i);
6502 static int tconcat(lua_State*L){
6506 const char*sep=luaL_optlstring(L,2,"",&lsep);
6507 luaL_checktype(L,1,5);
6508 i=luaL_optint(L,3,1);
6509 last=luaL_opt(L,luaL_checkint,4,luaL_getn(L,1));
6510 luaL_buffinit(L,&b);
6512 addfield(L,&b,i);
6516 addfield(L,&b,i);
6520 static void set2(lua_State*L,int i,int j){
6521 lua_rawseti(L,1,i);
6522 lua_rawseti(L,1,j);
6524 static int sort_comp(lua_State*L,int a,int b){
6525 if(!lua_isnil(L,2)){
6527 lua_pushvalue(L,2);
6528 lua_pushvalue(L,a-1);
6529 lua_pushvalue(L,b-2);
6530 lua_call(L,2,1);
6531 res=lua_toboolean(L,-1);
6532 lua_pop(L,1);
6536 return lua_lessthan(L,a,b);
6538 static void auxsort(lua_State*L,int l,int u){
6541 lua_rawgeti(L,1,l);
6542 lua_rawgeti(L,1,u);
6543 if(sort_comp(L,-1,-2))
6544 set2(L,l,u);
6546 lua_pop(L,2);
6549 lua_rawgeti(L,1,i);
6550 lua_rawgeti(L,1,l);
6551 if(sort_comp(L,-2,-1))
6552 set2(L,i,l);
6554 lua_pop(L,1);
6555 lua_rawgeti(L,1,u);
6556 if(sort_comp(L,-1,-2))
6557 set2(L,i,u);
6559 lua_pop(L,2);
6562 lua_rawgeti(L,1,i);
6563 lua_pushvalue(L,-1);
6564 lua_rawgeti(L,1,u-1);
6565 set2(L,i,u-1);
6568 while(lua_rawgeti(L,1,++i),sort_comp(L,-1,-2)){
6569 if(i>u)luaL_error(L,"invalid order function for sorting");
6570 lua_pop(L,1);
6572 while(lua_rawgeti(L,1,--j),sort_comp(L,-3,-1)){
6573 if(j<l)luaL_error(L,"invalid order function for sorting");
6574 lua_pop(L,1);
6577 lua_pop(L,3);
6580 set2(L,i,j);
6582 lua_rawgeti(L,1,u-1);
6583 lua_rawgeti(L,1,i);
6584 set2(L,u-1,i);
6591 auxsort(L,j,i);
6594 static int sort(lua_State*L){
6595 int n=aux_getn(L,1);
6596 luaL_checkstack(L,40,"");
6597 if(!lua_isnoneornil(L,2))
6598 luaL_checktype(L,2,6);
6599 lua_settop(L,2);
6600 auxsort(L,1,n);
6610 static int luaopen_table(lua_State*L){
6611 luaL_register(L,"table",tab_funcs);
6615 static int pushresult(lua_State*L,int i,const char*filename){
6618 lua_pushboolean(L,1);
6622 lua_pushnil(L);
6624 lua_pushfstring(L,"%s: %s",filename,strerror(en));
6626 lua_pushfstring(L,"%s",strerror(en));
6627 lua_pushinteger(L,en);
6631 static void fileerror(lua_State*L,int arg,const char*filename){
6632 lua_pushfstring(L,"%s: %s",filename,strerror(errno));
6633 luaL_argerror(L,arg,lua_tostring(L,-1));
6635 #define tofilep(L)((FILE**)luaL_checkudata(L,1,"FILE*"))
6636 static int io_type(lua_State*L){
6638 luaL_checkany(L,1);
6639 ud=lua_touserdata(L,1);
6640 lua_getfield(L,(-10000),"FILE*");
6641 if(ud==NULL||!lua_getmetatable(L,1)||!lua_rawequal(L,-2,-1))
6642 lua_pushnil(L);
6644 lua_pushliteral(L,"closed file");
6646 lua_pushliteral(L,"file");
6649 static FILE*tofile(lua_State*L){
6650 FILE**f=tofilep(L);
6652 luaL_error(L,"attempt to use a closed file");
6655 static FILE**newfile(lua_State*L){
6656 FILE**pf=(FILE**)lua_newuserdata(L,sizeof(FILE*));
6658 luaL_getmetatable(L,"FILE*");
6659 lua_setmetatable(L,-2);
6662 static int io_noclose(lua_State*L){
6663 lua_pushnil(L);
6664 lua_pushliteral(L,"cannot close standard file");
6667 static int io_pclose(lua_State*L){
6668 FILE**p=tofilep(L);
6669 int ok=lua_pclose(L,*p);
6671 return pushresult(L,ok,NULL);
6673 static int io_fclose(lua_State*L){
6674 FILE**p=tofilep(L);
6677 return pushresult(L,ok,NULL);
6679 static int aux_close(lua_State*L){
6680 lua_getfenv(L,1);
6681 lua_getfield(L,-1,"__close");
6682 return(lua_tocfunction(L,-1))(L);
6684 static int io_close(lua_State*L){
6685 if(lua_isnone(L,1))
6686 lua_rawgeti(L,(-10001),2);
6687 tofile(L);
6688 return aux_close(L);
6690 static int io_gc(lua_State*L){
6691 FILE*f=*tofilep(L);
6693 aux_close(L);
6696 static int io_open(lua_State*L){
6697 const char*filename=luaL_checkstring(L,1);
6698 const char*mode=luaL_optstring(L,2,"r");
6699 FILE**pf=newfile(L);
6701 return(*pf==NULL)?pushresult(L,0,filename):1;
6703 static FILE*getiofile(lua_State*L,int findex){
6705 lua_rawgeti(L,(-10001),findex);
6706 f=*(FILE**)lua_touserdata(L,-1);
6708 luaL_error(L,"standard %s file is closed",fnames[findex-1]);
6711 static int g_iofile(lua_State*L,int f,const char*mode){
6712 if(!lua_isnoneornil(L,1)){
6713 const char*filename=lua_tostring(L,1);
6715 FILE**pf=newfile(L);
6718 fileerror(L,1,filename);
6721 tofile(L);
6722 lua_pushvalue(L,1);
6724 lua_rawseti(L,(-10001),f);
6726 lua_rawgeti(L,(-10001),f);
6729 static int io_input(lua_State*L){
6730 return g_iofile(L,1,"r");
6732 static int io_output(lua_State*L){
6733 return g_iofile(L,2,"w");
6735 static int io_readline(lua_State*L);
6736 static void aux_lines(lua_State*L,int idx,int toclose){
6737 lua_pushvalue(L,idx);
6738 lua_pushboolean(L,toclose);
6739 lua_pushcclosure(L,io_readline,2);
6741 static int f_lines(lua_State*L){
6742 tofile(L);
6743 aux_lines(L,1,0);
6746 static int io_lines(lua_State*L){
6747 if(lua_isnoneornil(L,1)){
6748 lua_rawgeti(L,(-10001),1);
6749 return f_lines(L);
6752 const char*filename=luaL_checkstring(L,1);
6753 FILE**pf=newfile(L);
6756 fileerror(L,1,filename);
6757 aux_lines(L,lua_gettop(L),1);
6761 static int read_number(lua_State*L,FILE*f){
6764 lua_pushnumber(L,d);
6768 lua_pushnil(L);
6772 static int test_eof(lua_State*L,FILE*f){
6775 lua_pushlstring(L,NULL,0);
6778 static int read_line(lua_State*L,FILE*f){
6780 luaL_buffinit(L,&b);
6786 return(lua_objlen(L,-1)>0);
6798 static int read_chars(lua_State*L,FILE*f,size_t n){
6802 luaL_buffinit(L,&b);
6812 return(n==0||lua_objlen(L,-1)>0);
6814 static int g_read(lua_State*L,FILE*f,int first){
6815 int nargs=lua_gettop(L)-1;
6820 success=read_line(L,f);
6824 luaL_checkstack(L,nargs+20,"too many arguments");
6827 if(lua_type(L,n)==3){
6828 size_t l=(size_t)lua_tointeger(L,n);
6829 success=(l==0)?test_eof(L,f):read_chars(L,f,l);
6832 const char*p=lua_tostring(L,n);
6833 luaL_argcheck(L,p&&p[0]=='*',n,"invalid option");
6836 success=read_number(L,f);
6839 success=read_line(L,f);
6842 read_chars(L,f,~((size_t)0));
6846 return luaL_argerror(L,n,"invalid format");
6852 return pushresult(L,0,NULL);
6854 lua_pop(L,1);
6855 lua_pushnil(L);
6859 static int io_read(lua_State*L){
6860 return g_read(L,getiofile(L,1),1);
6862 static int f_read(lua_State*L){
6863 return g_read(L,tofile(L),2);
6865 static int io_readline(lua_State*L){
6866 FILE*f=*(FILE**)lua_touserdata(L,lua_upvalueindex(1));
6869 luaL_error(L,"file is already closed");
6870 success=read_line(L,f);
6872 return luaL_error(L,"%s",strerror(errno));
6875 if(lua_toboolean(L,lua_upvalueindex(2))){
6876 lua_settop(L,0);
6877 lua_pushvalue(L,lua_upvalueindex(1));
6878 aux_close(L);
6883 static int g_write(lua_State*L,FILE*f,int arg){
6884 int nargs=lua_gettop(L)-1;
6887 if(lua_type(L,arg)==3){
6889 fprintf(f,"%.14g",lua_tonumber(L,arg))>0;
6893 const char*s=luaL_checklstring(L,arg,&l);
6897 return pushresult(L,status,NULL);
6899 static int io_write(lua_State*L){
6900 return g_write(L,getiofile(L,2),1);
6902 static int f_write(lua_State*L){
6903 return g_write(L,tofile(L),2);
6905 static int io_flush(lua_State*L){
6906 return pushresult(L,fflush(getiofile(L,2))==0,NULL);
6908 static int f_flush(lua_State*L){
6909 return pushresult(L,fflush(tofile(L))==0,NULL);
6932 static void createmeta(lua_State*L){
6933 luaL_newmetatable(L,"FILE*");
6934 lua_pushvalue(L,-1);
6935 lua_setfield(L,-2,"__index");
6936 luaL_register(L,NULL,flib);
6938 static void createstdfile(lua_State*L,FILE*f,int k,const char*fname){
6939 *newfile(L)=f;
6941 lua_pushvalue(L,-1);
6942 lua_rawseti(L,(-10001),k);
6944 lua_pushvalue(L,-2);
6945 lua_setfenv(L,-2);
6946 lua_setfield(L,-3,fname);
6948 static void newfenv(lua_State*L,lua_CFunction cls){
6949 lua_createtable(L,0,1);
6950 lua_pushcfunction(L,cls);
6951 lua_setfield(L,-2,"__close");
6953 static int luaopen_io(lua_State*L){
6954 createmeta(L);
6955 newfenv(L,io_fclose);
6956 lua_replace(L,(-10001));
6957 luaL_register(L,"io",iolib);
6958 newfenv(L,io_noclose);
6959 createstdfile(L,stdin,1,"stdin");
6960 createstdfile(L,stdout,2,"stdout");
6961 createstdfile(L,stderr,0,"stderr");
6962 lua_pop(L,1);
6963 lua_getfield(L,-1,"popen");
6964 newfenv(L,io_pclose);
6965 lua_setfenv(L,-2);
6966 lua_pop(L,1);
6969 static int os_pushresult(lua_State*L,int i,const char*filename){
6972 lua_pushboolean(L,1);
6976 lua_pushnil(L);
6977 lua_pushfstring(L,"%s: %s",filename,strerror(en));
6978 lua_pushinteger(L,en);
6982 static int os_remove(lua_State*L){
6983 const char*filename=luaL_checkstring(L,1);
6984 return os_pushresult(L,remove(filename)==0,filename);
6986 static int os_exit(lua_State*L){
6987 exit(luaL_optint(L,1,EXIT_SUCCESS));
6989 static int os_clock(lua_State*L){
6990 lua_pushnumber(L,((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
6999 static int luaopen_os(lua_State*L){
7000 luaL_register(L,"os",syslib);
7008 static int str_sub(lua_State*L){
7010 const char*s=luaL_checklstring(L,1,&l);
7011 ptrdiff_t start=posrelat(luaL_checkinteger(L,2),l);
7012 ptrdiff_t end=posrelat(luaL_optinteger(L,3,-1),l);
7016 lua_pushlstring(L,s+start-1,end-start+1);
7017 else lua_pushliteral(L,"");
7020 static int str_lower(lua_State*L){
7024 const char*s=luaL_checklstring(L,1,&l);
7025 luaL_buffinit(L,&b);
7031 static int str_upper(lua_State*L){
7035 const char*s=luaL_checklstring(L,1,&l);
7036 luaL_buffinit(L,&b);
7042 static int str_rep(lua_State*L){
7045 const char*s=luaL_checklstring(L,1,&l);
7046 int n=luaL_checkint(L,2);
7047 luaL_buffinit(L,&b);
7053 static int str_byte(lua_State*L){
7055 const char*s=luaL_checklstring(L,1,&l);
7056 ptrdiff_t posi=posrelat(luaL_optinteger(L,2,1),l);
7057 ptrdiff_t pose=posrelat(luaL_optinteger(L,3,posi),l);
7064 luaL_error(L,"string slice too long");
7065 luaL_checkstack(L,n,"string slice too long");
7067 lua_pushinteger(L,uchar(s[posi+i-1]));
7070 static int str_char(lua_State*L){
7071 int n=lua_gettop(L);
7074 luaL_buffinit(L,&b);
7076 int c=luaL_checkint(L,i);
7077 luaL_argcheck(L,uchar(c)==c,i,"invalid value");
7086 lua_State*L;
7096 return luaL_error(ms->L,"invalid capture index");
7103 return luaL_error(ms->L,"invalid pattern capture");
7109 luaL_error(ms->L,"malformed pattern (ends with "LUA_QL("%%")")");
7116 luaL_error(ms->L,"malformed pattern (missing "LUA_QL("]")")");
7177 luaL_error(ms->L,"unbalanced pattern");
7219 if(level>=32)luaL_error(ms->L,"too many captures");
7268 luaL_error(ms->L,"missing "LUA_QL("[")" after "
7345 lua_pushlstring(ms->L,s,e-s);
7347 luaL_error(ms->L,"invalid capture index");
7351 if(l==(-1))luaL_error(ms->L,"unfinished capture");
7353 lua_pushinteger(ms->L,ms->capture[i].init-ms->src_init+1);
7355 lua_pushlstring(ms->L,ms->capture[i].init,l);
7361 luaL_checkstack(ms->L,nlevels,"too many captures");
7366 static int str_find_aux(lua_State*L,int find){
7368 const char*s=luaL_checklstring(L,1,&l1);
7369 const char*p=luaL_checklstring(L,2,&l2);
7370 ptrdiff_t init=posrelat(luaL_optinteger(L,3,1),l1)-1;
7373 if(find&&(lua_toboolean(L,4)||
7377 lua_pushinteger(L,s2-s+1);
7378 lua_pushinteger(L,s2-s+l2);
7386 ms.L=L;
7394 lua_pushinteger(L,s1-s+1);
7395 lua_pushinteger(L,res-s);
7403 lua_pushnil(L);
7406 static int str_find(lua_State*L){
7407 return str_find_aux(L,1);
7409 static int str_match(lua_State*L){
7410 return str_find_aux(L,0);
7412 static int gmatch_aux(lua_State*L){
7415 const char*s=lua_tolstring(L,lua_upvalueindex(1),&ls);
7416 const char*p=lua_tostring(L,lua_upvalueindex(2));
7418 ms.L=L;
7421 for(src=s+(size_t)lua_tointeger(L,lua_upvalueindex(3));
7429 lua_pushinteger(L,newstart);
7430 lua_replace(L,lua_upvalueindex(3));
7436 static int gmatch(lua_State*L){
7437 luaL_checkstring(L,1);
7438 luaL_checkstring(L,2);
7439 lua_settop(L,2);
7440 lua_pushinteger(L,0);
7441 lua_pushcclosure(L,gmatch_aux,3);
7447 const char*news=lua_tolstring(ms->L,3,&l);
7466 lua_State*L=ms->L;
7467 switch(lua_type(L,3)){
7475 lua_pushvalue(L,3);
7477 lua_call(L,n,1);
7482 lua_gettable(L,3);
7486 if(!lua_toboolean(L,-1)){
7487 lua_pop(L,1);
7488 lua_pushlstring(L,s,e-s);
7490 else if(!lua_isstring(L,-1))
7491 luaL_error(L,"invalid replacement value (a %s)",luaL_typename(L,-1));
7494 static int str_gsub(lua_State*L){
7496 const char*src=luaL_checklstring(L,1,&srcl);
7497 const char*p=luaL_checkstring(L,2);
7498 int tr=lua_type(L,3);
7499 int max_s=luaL_optint(L,4,srcl+1);
7504 luaL_argcheck(L,tr==3||tr==4||
7507 luaL_buffinit(L,&b);
7508 ms.L=L;
7528 lua_pushinteger(L,n);
7531 static void addquoted(lua_State*L,luaL_Buffer*b,int arg){
7533 const char*s=luaL_checklstring(L,arg,&l);
7559 static const char*scanformat(lua_State*L,const char*strfrmt,char*form){
7563 luaL_error(L,"invalid format (repeated flags)");
7572 luaL_error(L,"invalid format (width or precision too long)");
7586 static int str_format(lua_State*L){
7587 int top=lua_gettop(L);
7590 const char*strfrmt=luaL_checklstring(L,arg,&sfl);
7593 luaL_buffinit(L,&b);
7603 luaL_argerror(L,arg,"no value");
7604 strfrmt=scanformat(L,strfrmt,form);
7607 sprintf(buff,form,(int)luaL_checknumber(L,arg));
7612 sprintf(buff,form,(long)luaL_checknumber(L,arg));
7617 sprintf(buff,form,(unsigned long)luaL_checknumber(L,arg));
7622 sprintf(buff,form,(double)luaL_checknumber(L,arg));
7626 addquoted(L,&b,arg);
7631 const char*s=luaL_checklstring(L,arg,&l);
7633 lua_pushvalue(L,arg);
7643 return luaL_error(L,"invalid option "LUA_QL("%%%c")" to "
7667 static void createmetatable(lua_State*L){
7668 lua_createtable(L,0,1);
7669 lua_pushliteral(L,"");
7670 lua_pushvalue(L,-2);
7671 lua_setmetatable(L,-2);
7672 lua_pop(L,1);
7673 lua_pushvalue(L,-2);
7674 lua_setfield(L,-2,"__index");
7675 lua_pop(L,1);
7677 static int luaopen_string(lua_State*L){
7678 luaL_register(L,"string",strlib);
7679 createmetatable(L);
7690 static void luaL_openlibs(lua_State*L){
7693 lua_pushcfunction(L,lib->func);
7694 lua_pushstring(L,lib->name);
7695 lua_call(L,1,0);
7699 static UB barg(lua_State*L,int idx){
7701 bn.n=lua_tonumber(L,idx)+6755399441055744.0;
7702 if(bn.n==0.0&&!lua_isnumber(L,idx))luaL_typerror(L,idx,"number");
7705 #define BRET(b)lua_pushnumber(L,(lua_Number)(int)(b));return 1;
7706 static int tobit(lua_State*L){
7707 BRET(barg(L,1))}
7708 static int bnot(lua_State*L){
7709 BRET(~barg(L,1))}
7710 static int band(lua_State*L){
7711 int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b&=barg(L,i);BRET(b)}
7712 static int bor(lua_State*L){
7713 int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b|=barg(L,i);BRET(b)}
7714 static int bxor(lua_State*L){
7715 int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b^=barg(L,i);BRET(b)}
7716 static int lshift(lua_State*L){
7717 UB b=barg(L,1),n=barg(L,2)&31;BRET(b<<n)}
7718 static int rshift(lua_State*L){
7719 UB b=barg(L,1),n=barg(L,2)&31;BRET(b>>n)}
7720 static int arshift(lua_State*L){
7721 UB b=barg(L,1),n=barg(L,2)&31;BRET((int)b>>n)}
7722 static int rol(lua_State*L){
7723 UB b=barg(L,1),n=barg(L,2)&31;BRET((b<<n)|(b>>(32-n)))}
7724 static int ror(lua_State*L){
7725 UB b=barg(L,1),n=barg(L,2)&31;BRET((b>>n)|(b<<(32-n)))}
7726 static int bswap(lua_State*L){
7727 UB b=barg(L,1);b=(b>>24)|((b>>8)&0xff00)|((b&0xff00)<<8)|(b<<24);BRET(b)}
7728 static int tohex(lua_State*L){
7729 UB b=barg(L,1);
7730 int n=lua_isnone(L,2)?8:(int)barg(L,2);
7737 lua_pushlstring(L,buf,(size_t)n);
7756 lua_State*L=luaL_newstate();
7758 luaL_openlibs(L);
7759 luaL_register(L,"bit",bitlib);
7761 lua_createtable(L,0,1);
7762 lua_pushstring(L,argv[1]);
7763 lua_rawseti(L,-2,0);
7764 lua_setglobal(L,"arg");
7765 if(luaL_loadfile(L,argv[1]))
7768 lua_pushstring(L,argv[i]);
7769 if(lua_pcall(L,argc-2,0,0)){
7771 fprintf(stderr,"Error: %s\n",lua_tostring(L,-1));
7774 lua_close(L);