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);
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);
1653 static TValue*luaH_set(lua_State*L,Table*t,const TValue*key){
1659 if(ttisnil(key))luaG_runerror(L,"table index is nil");
1661 luaG_runerror(L,"table index is NaN");
1662 return newkey(L,t,key);
1665 static TValue*luaH_setnum(lua_State*L,Table*t,int key){
1672 return newkey(L,t,&k);
1675 static TValue*luaH_setstr(lua_State*L,Table*t,TString*key){
1681 setsvalue(L,&k,key);
1682 return newkey(L,t,&k);
1785 static size_t luaC_separateudata(lua_State*L,int all){
1786 global_State*g=G(L);
1793 else if(fasttm(L,gco2u(curr)->metatable,TM_GC)==NULL){
1881 static void checkstacksizes(lua_State*L,StkId max){
1882 int ci_used=cast_int(L->ci-L->base_ci);
1883 int s_used=cast_int(max-L->stack);
1884 if(L->size_ci>20000)
1886 if(4*ci_used<L->size_ci&&2*8<L->size_ci)
1887 luaD_reallocCI(L,L->size_ci/2);
1888 condhardstacktests(luaD_reallocCI(L,ci_used+1));
1889 if(4*s_used<L->stacksize&&
1890 2*((2*20)+5)<L->stacksize)
1891 luaD_reallocstack(L,L->stacksize/2);
1892 condhardstacktests(luaD_reallocstack(L,s_used));
1988 static void freeobj(lua_State*L,GCObject*o){
1990 case(8+1):luaF_freeproto(L,gco2p(o));break;
1991 case 6:luaF_freeclosure(L,gco2cl(o));break;
1992 case(8+2):luaF_freeupval(L,gco2uv(o));break;
1993 case 5:luaH_free(L,gco2h(o));break;
1995 luaE_freethread(L,gco2th(o));
1999 G(L)->strt.nuse--;
2000 luaM_freemem(L,o,sizestring(gco2ts(o)));
2004 luaM_freemem(L,o,sizeudata(gco2u(o)));
2010 #define sweepwholelist(L,p)sweeplist(L,p,((lu_mem)(~(lu_mem)0)-2))
2011 static GCObject**sweeplist(lua_State*L,GCObject**p,lu_mem count){
2013 global_State*g=G(L);
2017 sweepwholelist(L,&gco2th(curr)->openupval);
2026 freeobj(L,curr);
2031 static void checkSizes(lua_State*L){
2032 global_State*g=G(L);
2035 luaS_resize(L,g->strt.size/2);
2038 luaZ_resizebuffer(L,&g->buff,newsize);
2041 static void GCTM(lua_State*L){
2042 global_State*g=G(L);
2053 tm=fasttm(L,udata->uv.metatable,TM_GC);
2055 lu_byte oldah=L->allowhook;
2057 L->allowhook=0;
2059 setobj(L,L->top,tm);
2060 setuvalue(L,L->top+1,udata);
2061 L->top+=2;
2062 luaD_call(L,L->top-2,0);
2063 L->allowhook=oldah;
2067 static void luaC_callGCTM(lua_State*L){
2068 while(G(L)->tmudata)
2069 GCTM(L);
2071 static void luaC_freeall(lua_State*L){
2072 global_State*g=G(L);
2075 sweepwholelist(L,&g->rootgc);
2077 sweepwholelist(L,&g->strt.hash[i]);
2084 static void markroot(lua_State*L){
2085 global_State*g=G(L);
2091 markvalue(g,registry(L));
2102 static void atomic(lua_State*L){
2103 global_State*g=G(L);
2109 markobject(g,L);
2115 udsize=luaC_separateudata(L,0);
2125 static l_mem singlestep(lua_State*L){
2126 global_State*g=G(L);
2129 markroot(L);
2136 atomic(L);
2142 sweepwholelist(L,&g->strt.hash[g->sweepstrgc++]);
2150 g->sweepgc=sweeplist(L,g->sweepgc,40);
2152 checkSizes(L);
2160 GCTM(L);
2174 static void luaC_step(lua_State*L){
2175 global_State*g=G(L);
2181 lim-=singlestep(L);
2197 static void luaC_barrierf(lua_State*L,GCObject*o,GCObject*v){
2198 global_State*g=G(L);
2204 static void luaC_barrierback(lua_State*L,Table*t){
2205 global_State*g=G(L);
2211 static void luaC_link(lua_State*L,GCObject*o,lu_byte tt){
2212 global_State*g=G(L);
2218 static void luaC_linkupval(lua_State*L,UpVal*uv){
2219 global_State*g=G(L);
2226 luaC_barrier(L,uv,uv->v);
2248 struct lua_State*L;
2254 static void luaX_init(lua_State*L);
2263 static void stack_init(lua_State*L1,lua_State*L){
2264 L1->base_ci=luaM_newvector(L,8,CallInfo);
2268 L1->stack=luaM_newvector(L,(2*20)+5,TValue);
2277 static void freestack(lua_State*L,lua_State*L1){
2278 luaM_freearray(L,L1->base_ci,L1->size_ci,CallInfo);
2279 luaM_freearray(L,L1->stack,L1->stacksize,TValue);
2281 static void f_luaopen(lua_State*L,void*ud){
2282 global_State*g=G(L);
2284 stack_init(L,L);
2285 sethvalue(L,gt(L),luaH_new(L,0,2));
2286 sethvalue(L,registry(L),luaH_new(L,0,2));
2287 luaS_resize(L,32);
2288 luaT_init(L);
2289 luaX_init(L);
2290 luaS_fix(luaS_newliteral(L,"not enough memory"));
2293 static void preinit_state(lua_State*L,global_State*g){
2294 G(L)=g;
2295 L->stack=NULL;
2296 L->stacksize=0;
2297 L->errorJmp=NULL;
2298 L->hook=NULL;
2299 L->hookmask=0;
2300 L->basehookcount=0;
2301 L->allowhook=1;
2302 resethookcount(L);
2303 L->openupval=NULL;
2304 L->size_ci=0;
2305 L->nCcalls=L->baseCcalls=0;
2306 L->status=0;
2307 L->base_ci=L->ci=NULL;
2308 L->savedpc=NULL;
2309 L->errfunc=0;
2310 setnilvalue(gt(L));
2312 static void close_state(lua_State*L){
2313 global_State*g=G(L);
2314 luaF_close(L,L->stack);
2315 luaC_freeall(L);
2316 luaM_freearray(L,G(L)->strt.hash,G(L)->strt.size,TString*);
2317 luaZ_freebuffer(L,&g->buff);
2318 freestack(L,L);
2319 (*g->frealloc)(g->ud,fromstate(L),state_size(LG),0);
2321 static void luaE_freethread(lua_State*L,lua_State*L1){
2323 freestack(L,L1);
2324 luaM_freemem(L,fromstate(L1),state_size(lua_State));
2328 lua_State*L;
2332 L=tostate(l);
2333 g=&((LG*)L)->g;
2334 L->next=NULL;
2335 L->tt=8;
2337 L->marked=luaC_white(g);
2338 set2bits(L->marked,5,6);
2339 preinit_state(L,g);
2342 g->mainthread=L;
2349 setnilvalue(registry(L));
2350 luaZ_initbuffer(L,&g->buff);
2353 g->rootgc=obj2gco(L);
2365 if(luaD_rawrunprotected(L,f_luaopen,NULL)!=0){
2366 close_state(L);
2367 L=NULL;
2371 return L;
2373 static void callallgcTM(lua_State*L,void*ud){
2375 luaC_callGCTM(L);
2377 static void lua_close(lua_State*L){
2378 L=G(L)->mainthread;
2379 luaF_close(L,L->stack);
2380 luaC_separateudata(L,1);
2381 L->errfunc=0;
2383 L->ci=L->base_ci;
2384 L->base=L->top=L->ci->base;
2385 L->nCcalls=L->baseCcalls=0;
2386 }while(luaD_rawrunprotected(L,callallgcTM,NULL)!=0);
2387 close_state(L);
2397 static int currentpc(lua_State*L,CallInfo*ci){
2399 if(ci==L->ci)
2400 ci->savedpc=L->savedpc;
2403 static int currentline(lua_State*L,CallInfo*ci){
2404 int pc=currentpc(L,ci);
2410 static int lua_getstack(lua_State*L,int level,lua_Debug*ar){
2413 for(ci=L->ci;level>0&&ci>L->base_ci;ci--){
2418 if(level==0&&ci>L->base_ci){
2420 ar->i_ci=cast_int(ci-L->base_ci);
2455 static void collectvalidlines(lua_State*L,Closure*f){
2457 setnilvalue(L->top);
2460 Table*t=luaH_new(L,0,0);
2464 setbvalue(luaH_setnum(L,t,lineinfo[i]),1);
2465 sethvalue(L,L->top,t);
2467 incr_top(L);
2469 static int auxgetinfo(lua_State*L,const char*what,lua_Debug*ar,
2483 ar->currentline=(ci)?currentline(L,ci):-1;
2506 static int lua_getinfo(lua_State*L,const char*what,lua_Debug*ar){
2511 StkId func=L->top-1;
2512 luai_apicheck(L,ttisfunction(func));
2515 L->top--;
2518 ci=L->base_ci+ar->i_ci;
2521 status=auxgetinfo(L,what,ar,f,ci);
2523 if(f==NULL)setnilvalue(L->top);
2524 else setclvalue(L,L->top,f);
2525 incr_top(L);
2528 collectvalidlines(L,f);
2537 static void luaG_typeerror(lua_State*L,const TValue*o,const char*op){
2540 const char*kind=(isinstack(L->ci,o))?
2544 luaG_runerror(L,"attempt to %s %s "LUA_QL("%s")" (a %s value)",
2547 luaG_runerror(L,"attempt to %s a %s value",op,t);
2549 static void luaG_concaterror(lua_State*L,StkId p1,StkId p2){
2551 luaG_typeerror(L,p1,"concatenate");
2553 static void luaG_aritherror(lua_State*L,const TValue*p1,const TValue*p2){
2557 luaG_typeerror(L,p2,"perform arithmetic on");
2559 static int luaG_ordererror(lua_State*L,const TValue*p1,const TValue*p2){
2563 luaG_runerror(L,"attempt to compare two %s values",t1);
2565 luaG_runerror(L,"attempt to compare %s with %s",t1,t2);
2568 static void addinfo(lua_State*L,const char*msg){
2569 CallInfo*ci=L->ci;
2572 int line=currentline(L,ci);
2574 luaO_pushfstring(L,"%s:%d: %s",buff,line,msg);
2577 static void luaG_errormsg(lua_State*L){
2578 if(L->errfunc!=0){
2579 StkId errfunc=restorestack(L,L->errfunc);
2580 if(!ttisfunction(errfunc))luaD_throw(L,5);
2581 setobj(L,L->top,L->top-1);
2582 setobj(L,L->top-1,errfunc);
2583 incr_top(L);
2584 luaD_call(L,L->top-2,1);
2586 luaD_throw(L,2);
2588 static void luaG_runerror(lua_State*L,const char*fmt,...){
2591 addinfo(L,luaO_pushvfstring(L,fmt,argp));
2593 luaG_errormsg(L);
2597 lua_State*L=z->L;
2599 buff=z->reader(L,z->data,&size);
2605 static void luaZ_init(lua_State*L,ZIO*z,lua_Reader reader,void*data){
2606 z->L=L;
2612 static char*luaZ_openspace(lua_State*L,Mbuffer*buff,size_t n){
2615 luaZ_resizebuffer(L,buff,n);
2679 luaZ_resizebuffer(ls->L,b,newsize);
2683 static void luaX_init(lua_State*L){
2686 TString*ts=luaS_new(L,luaX_tokens[i]);
2693 return(iscntrl(token))?luaO_pushfstring(ls->L,"char(%d)",token):
2694 luaO_pushfstring(ls->L,"%c",token);
2713 msg=luaO_pushfstring(ls->L,"%s:%d: %s",buff,ls->linenumber,msg);
2715 luaO_pushfstring(ls->L,"%s near "LUA_QL("%s"),msg,txtToken(ls,token));
2716 luaD_throw(ls->L,3);
2722 lua_State*L=ls->L;
2723 TString*ts=luaS_newlstr(L,str,l);
2724 TValue*o=luaH_setstr(L,ls->fs->h,ts);
2727 luaC_checkGC(L);
2739 static void luaX_setinput(lua_State*L,LexState*ls,ZIO*z,TString*source){
2741 ls->L=L;
2748 luaZ_resizebuffer(ls->L,ls->buff,32);
3143 lua_State*L=fs->L;
3144 TValue*idx=luaH_set(L,fs->h,k);
3152 luaM_growvector(L,f->k,fs->nk,f->sizek,TValue,
3155 setobj(L,&f->k[fs->nk],v);
3156 luaC_barrier(L,f,v);
3162 setsvalue(fs->L,&o,s);
3178 sethvalue(fs->L,&k,fs->h);
3630 luaM_growvector(fs->L,f->code,fs->pc,f->sizecode,Instruction,
3633 luaM_growvector(fs->L,f->lineinfo,fs->pc,f->sizelineinfo,int,
3675 luaO_pushfstring(ls->L,LUA_QL("%s")" expected",luaX_token2str(ls,token)));
3679 luaO_pushfstring(fs->L,"main function has more than %d %s",limit,what):
3680 luaO_pushfstring(fs->L,"function at line %d has more than %d %s",
3705 luaX_syntaxerror(ls,luaO_pushfstring(ls->L,
3733 luaM_growvector(ls->L,f->locvars,fs->nlocvars,f->sizelocvars,
3737 luaC_objbarrier(ls->L,f,varname);
3768 luaM_growvector(fs->L,f->upvalues,f->nups,f->sizeupvalues,
3772 luaC_objbarrier(fs->L,f,name);
3837 if(++ls->L->nCcalls>200)
3840 #define leavelevel(ls)((ls)->L->nCcalls--)
3863 luaM_growvector(ls->L,f->p,fs->np,f->sizep,Proto*,
3867 luaC_objbarrier(ls->L,f,func->f);
3875 lua_State*L=ls->L;
3876 Proto*f=luaF_newproto(L);
3880 fs->L=L;
3893 fs->h=luaH_new(L,0,0);
3894 sethvalue(L,L->top,fs->h);
3895 incr_top(L);
3896 setptvalue(L,L->top,f);
3897 incr_top(L);
3900 lua_State*L=ls->L;
3905 luaM_reallocvector(L,f->code,f->sizecode,fs->pc,Instruction);
3907 luaM_reallocvector(L,f->lineinfo,f->sizelineinfo,fs->pc,int);
3909 luaM_reallocvector(L,f->k,f->sizek,fs->nk,TValue);
3911 luaM_reallocvector(L,f->p,f->sizep,fs->np,Proto*);
3913 luaM_reallocvector(L,f->locvars,f->sizelocvars,fs->nlocvars,LocVar);
3915 luaM_reallocvector(L,f->upvalues,f->sizeupvalues,f->nups,TString*);
3919 L->top-=2;
3921 static Proto*luaY_parser(lua_State*L,ZIO*z,Mbuffer*buff,const char*name){
3925 luaX_setinput(L,&lexstate,z,luaS_new(L,name));
4351 luaY_checklimit(ls->fs,nvars,200-ls->L->nCcalls,
4698 static int luaV_tostring(lua_State*L,StkId obj){
4705 setsvalue(L,obj,luaS_new(L,s));
4709 static void callTMres(lua_State*L,StkId res,const TValue*f,
4711 ptrdiff_t result=savestack(L,res);
4712 setobj(L,L->top,f);
4713 setobj(L,L->top+1,p1);
4714 setobj(L,L->top+2,p2);
4715 luaD_checkstack(L,3);
4716 L->top+=3;
4717 luaD_call(L,L->top-3,1);
4718 res=restorestack(L,result);
4719 L->top--;
4720 setobj(L,res,L->top);
4722 static void callTM(lua_State*L,const TValue*f,const TValue*p1,
4724 setobj(L,L->top,f);
4725 setobj(L,L->top+1,p1);
4726 setobj(L,L->top+2,p2);
4727 setobj(L,L->top+3,p3);
4728 luaD_checkstack(L,4);
4729 L->top+=4;
4730 luaD_call(L,L->top-4,0);
4732 static void luaV_gettable(lua_State*L,const TValue*t,TValue*key,StkId val){
4740 (tm=fasttm(L,h->metatable,TM_INDEX))==NULL){
4741 setobj(L,val,res);
4745 else if(ttisnil(tm=luaT_gettmbyobj(L,t,TM_INDEX)))
4746 luaG_typeerror(L,t,"index");
4748 callTMres(L,val,tm,t,key);
4753 luaG_runerror(L,"loop in gettable");
4755 static void luaV_settable(lua_State*L,const TValue*t,TValue*key,StkId val){
4762 TValue*oldval=luaH_set(L,h,key);
4764 (tm=fasttm(L,h->metatable,TM_NEWINDEX))==NULL){
4765 setobj(L,oldval,val);
4767 luaC_barriert(L,h,val);
4771 else if(ttisnil(tm=luaT_gettmbyobj(L,t,TM_NEWINDEX)))
4772 luaG_typeerror(L,t,"index");
4774 callTM(L,tm,t,key,val);
4777 setobj(L,&temp,tm);
4780 luaG_runerror(L,"loop in settable");
4782 static int call_binTM(lua_State*L,const TValue*p1,const TValue*p2,
4784 const TValue*tm=luaT_gettmbyobj(L,p1,event);
4786 tm=luaT_gettmbyobj(L,p2,event);
4788 callTMres(L,res,tm,p1,p2);
4791 static const TValue*get_compTM(lua_State*L,Table*mt1,Table*mt2,
4793 const TValue*tm1=fasttm(L,mt1,event);
4797 tm2=fasttm(L,mt2,event);
4803 static int call_orderTM(lua_State*L,const TValue*p1,const TValue*p2,
4805 const TValue*tm1=luaT_gettmbyobj(L,p1,event);
4808 tm2=luaT_gettmbyobj(L,p2,event);
4811 callTMres(L,L->top,tm1,p1,p2);
4812 return!l_isfalse(L->top);
4833 static int luaV_lessthan(lua_State*L,const TValue*l,const TValue*r){
4836 return luaG_ordererror(L,l,r);
4841 else if((res=call_orderTM(L,l,r,TM_LT))!=-1)
4843 return luaG_ordererror(L,l,r);
4845 static int lessequal(lua_State*L,const TValue*l,const TValue*r){
4848 return luaG_ordererror(L,l,r);
4853 else if((res=call_orderTM(L,l,r,TM_LE))!=-1)
4855 else if((res=call_orderTM(L,r,l,TM_LT))!=-1)
4857 return luaG_ordererror(L,l,r);
4859 static int luaV_equalval(lua_State*L,const TValue*t1,const TValue*t2){
4868 tm=get_compTM(L,uvalue(t1)->metatable,uvalue(t2)->metatable,
4874 tm=get_compTM(L,hvalue(t1)->metatable,hvalue(t2)->metatable,TM_EQ);
4880 callTMres(L,L->top,tm,t1,t2);
4881 return!l_isfalse(L->top);
4883 static void luaV_concat(lua_State*L,int total,int last){
4885 StkId top=L->base+last+1;
4887 if(!(ttisstring(top-2)||ttisnumber(top-2))||!tostring(L,top-1)){
4888 if(!call_binTM(L,top-2,top-1,top-2,TM_CONCAT))
4889 luaG_concaterror(L,top-2,top-1);
4891 (void)tostring(L,top-2);
4896 for(n=1;n<total&&tostring(L,top-n-1);n++){
4898 if(l>=((size_t)(~(size_t)0)-2)-tl)luaG_runerror(L,"string length overflow");
4901 buffer=luaZ_openspace(L,&G(L)->buff,tl);
4908 setsvalue(L,top-n,luaS_newlstr(L,buffer,tl));
4914 static void Arith(lua_State*L,StkId ra,const TValue*rb,
4932 else if(!call_binTM(L,rb,rc,ra,op))
4933 luaG_aritherror(L,rb,rc);
4935 #define runtime_check(L,c){if(!(c))break;}
4941 #define dojump(L,pc,i){(pc)+=(i);}
4942 #define Protect(x){L->savedpc=pc;{x;};base=L->base;}
4943 …ua_Number nb=nvalue(rb),nc=nvalue(rc);setnvalue(ra,op(nb,nc));}else Protect(Arith(L,ra,rb,rc,tm));}
4944 static void luaV_execute(lua_State*L,int nexeccalls){
4950 pc=L->savedpc;
4951 cl=&clvalue(L->ci->func)->l;
4952 base=L->base;
4960 setobj(L,ra,RB(i));
4964 setobj(L,ra,KBx(i));
4981 setobj(L,ra,cl->upvals[b]->v);
4987 sethvalue(L,&g,cl->env);
4988 Protect(luaV_gettable(L,&g,rb,ra));
4992 Protect(luaV_gettable(L,RB(i),RKC(i),ra));
4997 sethvalue(L,&g,cl->env);
4998 Protect(luaV_settable(L,&g,KBx(i),ra));
5003 setobj(L,uv->v,ra);
5004 luaC_barrier(L,uv,ra);
5008 Protect(luaV_settable(L,ra,RKB(i),RKC(i)));
5014 sethvalue(L,ra,luaH_new(L,luaO_fb2int(b),luaO_fb2int(c)));
5015 Protect(luaC_checkGC(L));
5020 setobj(L,ra+1,rb);
5021 Protect(luaV_gettable(L,rb,RKC(i),ra));
5055 Protect(Arith(L,ra,rb,rb,TM_UNM));
5077 if(!call_binTM(L,rb,(&luaO_nilobject_),ra,TM_LEN))
5078 luaG_typeerror(L,rb,"get length of");
5087 Protect(luaV_concat(L,c-b+1,c);luaC_checkGC(L));
5088 setobj(L,RA(i),base+b);
5092 dojump(L,pc,GETARG_sBx(i));
5099 if(equalobj(L,rb,rc)==GETARG_A(i))
5100 dojump(L,pc,GETARG_sBx(*pc));
5107 if(luaV_lessthan(L,RKB(i),RKC(i))==GETARG_A(i))
5108 dojump(L,pc,GETARG_sBx(*pc));
5115 if(lessequal(L,RKB(i),RKC(i))==GETARG_A(i))
5116 dojump(L,pc,GETARG_sBx(*pc));
5123 dojump(L,pc,GETARG_sBx(*pc));
5130 setobj(L,ra,rb);
5131 dojump(L,pc,GETARG_sBx(*pc));
5139 if(b!=0)L->top=ra+b;
5140 L->savedpc=pc;
5141 switch(luaD_precall(L,ra,nresults)){
5147 if(nresults>=0)L->top=L->ci->top;
5148 base=L->base;
5158 if(b!=0)L->top=ra+b;
5159 L->savedpc=pc;
5160 switch(luaD_precall(L,ra,(-1))){
5162 CallInfo*ci=L->ci-1;
5166 if(L->openupval)luaF_close(L,ci->base);
5167 L->base=ci->base=ci->func+((ci+1)->base-pfunc);
5168 for(aux=0;pfunc+aux<L->top;aux++)
5169 setobj(L,func+aux,pfunc+aux);
5170 ci->top=L->top=func+aux;
5171 ci->savedpc=L->savedpc;
5173 L->ci--;
5177 base=L->base;
5187 if(b!=0)L->top=ra+b-1;
5188 if(L->openupval)luaF_close(L,base);
5189 L->savedpc=pc;
5190 b=luaD_poscall(L,ra);
5194 if(b)L->top=L->ci->top;
5204 dojump(L,pc,GETARG_sBx(i));
5214 L->savedpc=pc;
5216 luaG_runerror(L,LUA_QL("for")" initial value must be a number");
5218 luaG_runerror(L,LUA_QL("for")" limit must be a number");
5220 luaG_runerror(L,LUA_QL("for")" step must be a number");
5222 dojump(L,pc,GETARG_sBx(i));
5227 setobj(L,cb+2,ra+2);
5228 setobj(L,cb+1,ra+1);
5229 setobj(L,cb,ra);
5230 L->top=cb+3;
5231 Protect(luaD_call(L,cb,GETARG_C(i)));
5232 L->top=L->ci->top;
5235 setobj(L,cb-1,cb);
5236 dojump(L,pc,GETARG_sBx(*pc));
5247 n=cast_int(L->top-ra)-1;
5248 L->top=L->ci->top;
5251 runtime_check(L,ttistable(ra));
5255 luaH_resizearray(L,h,last);
5258 setobj(L,luaH_setnum(L,h,last--),val);
5259 luaC_barriert(L,h,val);
5264 luaF_close(L,ra);
5273 ncl=luaF_newLclosure(L,nup,cl->env);
5279 ncl->l.upvals[j]=luaF_findupval(L,base+GETARG_B(*pc));
5282 setclvalue(L,ra,ncl);
5283 Protect(luaC_checkGC(L));
5289 CallInfo*ci=L->ci;
5292 Protect(luaD_checkstack(L,n));
5295 L->top=ra+n;
5299 setobj(L,ra+j,ci->base-n+j);
5310 #define api_checknelems(L,n)luai_apicheck(L,(n)<=(L->top-L->base))
5311 #define api_checkvalidindex(L,i)luai_apicheck(L,(i)!=(&luaO_nilobject_))
5312 #define api_incr_top(L){luai_apicheck(L,L->top<L->ci->top);L->top++;}
5313 static TValue*index2adr(lua_State*L,int idx){
5315 TValue*o=L->base+(idx-1);
5316 luai_apicheck(L,idx<=L->ci->top-L->base);
5317 if(o>=L->top)return cast(TValue*,(&luaO_nilobject_));
5321 luai_apicheck(L,idx!=0&&-idx<=L->top-L->base);
5322 return L->top+idx;
5325 case(-10000):return registry(L);
5327 Closure*func=curr_func(L);
5328 sethvalue(L,&L->env,func->c.env);
5329 return&L->env;
5331 case(-10002):return gt(L);
5333 Closure*func=curr_func(L);
5341 static Table*getcurrenv(lua_State*L){
5342 if(L->ci==L->base_ci)
5343 return hvalue(gt(L));
5345 Closure*func=curr_func(L);
5349 static int lua_checkstack(lua_State*L,int size){
5351 if(size>8000||(L->top-L->base+size)>8000)
5354 luaD_checkstack(L,size);
5355 if(L->ci->top<L->top+size)
5356 L->ci->top=L->top+size;
5360 static lua_CFunction lua_atpanic(lua_State*L,lua_CFunction panicf){
5362 old=G(L)->panic;
5363 G(L)->panic=panicf;
5366 static int lua_gettop(lua_State*L){
5367 return cast_int(L->top-L->base);
5369 static void lua_settop(lua_State*L,int idx){
5371 luai_apicheck(L,idx<=L->stack_last-L->base);
5372 while(L->top<L->base+idx)
5373 setnilvalue(L->top++);
5374 L->top=L->base+idx;
5377 luai_apicheck(L,-(idx+1)<=(L->top-L->base));
5378 L->top+=idx+1;
5381 static void lua_remove(lua_State*L,int idx){
5383 p=index2adr(L,idx);
5384 api_checkvalidindex(L,p);
5385 while(++p<L->top)setobj(L,p-1,p);
5386 L->top--;
5388 static void lua_insert(lua_State*L,int idx){
5391 p=index2adr(L,idx);
5392 api_checkvalidindex(L,p);
5393 for(q=L->top;q>p;q--)setobj(L,q,q-1);
5394 setobj(L,p,L->top);
5396 static void lua_replace(lua_State*L,int idx){
5398 if(idx==(-10001)&&L->ci==L->base_ci)
5399 luaG_runerror(L,"no calling environment");
5400 api_checknelems(L,1);
5401 o=index2adr(L,idx);
5402 api_checkvalidindex(L,o);
5404 Closure*func=curr_func(L);
5405 luai_apicheck(L,ttistable(L->top-1));
5406 func->c.env=hvalue(L->top-1);
5407 luaC_barrier(L,func,L->top-1);
5410 setobj(L,o,L->top-1);
5412 luaC_barrier(L,curr_func(L),L->top-1);
5414 L->top--;
5416 static void lua_pushvalue(lua_State*L,int idx){
5417 setobj(L,L->top,index2adr(L,idx));
5418 api_incr_top(L);
5420 static int lua_type(lua_State*L,int idx){
5421 StkId o=index2adr(L,idx);
5424 static const char*lua_typename(lua_State*L,int t){
5425 UNUSED(L);
5428 static int lua_iscfunction(lua_State*L,int idx){
5429 StkId o=index2adr(L,idx);
5432 static int lua_isnumber(lua_State*L,int idx){
5434 const TValue*o=index2adr(L,idx);
5437 static int lua_isstring(lua_State*L,int idx){
5438 int t=lua_type(L,idx);
5441 static int lua_rawequal(lua_State*L,int index1,int index2){
5442 StkId o1=index2adr(L,index1);
5443 StkId o2=index2adr(L,index2);
5447 static int lua_lessthan(lua_State*L,int index1,int index2){
5450 o1=index2adr(L,index1);
5451 o2=index2adr(L,index2);
5453 :luaV_lessthan(L,o1,o2);
5456 static lua_Number lua_tonumber(lua_State*L,int idx){
5458 const TValue*o=index2adr(L,idx);
5464 static lua_Integer lua_tointeger(lua_State*L,int idx){
5466 const TValue*o=index2adr(L,idx);
5476 static int lua_toboolean(lua_State*L,int idx){
5477 const TValue*o=index2adr(L,idx);
5480 static const char*lua_tolstring(lua_State*L,int idx,size_t*len){
5481 StkId o=index2adr(L,idx);
5483 if(!luaV_tostring(L,o)){
5487 luaC_checkGC(L);
5488 o=index2adr(L,idx);
5493 static size_t lua_objlen(lua_State*L,int idx){
5494 StkId o=index2adr(L,idx);
5501 l=(luaV_tostring(L,o)?tsvalue(o)->len:0);
5507 static lua_CFunction lua_tocfunction(lua_State*L,int idx){
5508 StkId o=index2adr(L,idx);
5511 static void*lua_touserdata(lua_State*L,int idx){
5512 StkId o=index2adr(L,idx);
5519 static void lua_pushnil(lua_State*L){
5520 setnilvalue(L->top);
5521 api_incr_top(L);
5523 static void lua_pushnumber(lua_State*L,lua_Number n){
5524 setnvalue(L->top,n);
5525 api_incr_top(L);
5527 static void lua_pushinteger(lua_State*L,lua_Integer n){
5528 setnvalue(L->top,cast_num(n));
5529 api_incr_top(L);
5531 static void lua_pushlstring(lua_State*L,const char*s,size_t len){
5532 luaC_checkGC(L);
5533 setsvalue(L,L->top,luaS_newlstr(L,s,len));
5534 api_incr_top(L);
5536 static void lua_pushstring(lua_State*L,const char*s){
5538 lua_pushnil(L);
5540 lua_pushlstring(L,s,strlen(s));
5542 static const char*lua_pushvfstring(lua_State*L,const char*fmt,
5545 luaC_checkGC(L);
5546 ret=luaO_pushvfstring(L,fmt,argp);
5549 static const char*lua_pushfstring(lua_State*L,const char*fmt,...){
5552 luaC_checkGC(L);
5554 ret=luaO_pushvfstring(L,fmt,argp);
5558 static void lua_pushcclosure(lua_State*L,lua_CFunction fn,int n){
5560 luaC_checkGC(L);
5561 api_checknelems(L,n);
5562 cl=luaF_newCclosure(L,n,getcurrenv(L));
5564 L->top-=n;
5566 setobj(L,&cl->c.upvalue[n],L->top+n);
5567 setclvalue(L,L->top,cl);
5568 api_incr_top(L);
5570 static void lua_pushboolean(lua_State*L,int b){
5571 setbvalue(L->top,(b!=0));
5572 api_incr_top(L);
5574 static int lua_pushthread(lua_State*L){
5575 setthvalue(L,L->top,L);
5576 api_incr_top(L);
5577 return(G(L)->mainthread==L);
5579 static void lua_gettable(lua_State*L,int idx){
5581 t=index2adr(L,idx);
5582 api_checkvalidindex(L,t);
5583 luaV_gettable(L,t,L->top-1,L->top-1);
5585 static void lua_getfield(lua_State*L,int idx,const char*k){
5588 t=index2adr(L,idx);
5589 api_checkvalidindex(L,t);
5590 setsvalue(L,&key,luaS_new(L,k));
5591 luaV_gettable(L,t,&key,L->top);
5592 api_incr_top(L);
5594 static void lua_rawget(lua_State*L,int idx){
5596 t=index2adr(L,idx);
5597 luai_apicheck(L,ttistable(t));
5598 setobj(L,L->top-1,luaH_get(hvalue(t),L->top-1));
5600 static void lua_rawgeti(lua_State*L,int idx,int n){
5602 o=index2adr(L,idx);
5603 luai_apicheck(L,ttistable(o));
5604 setobj(L,L->top,luaH_getnum(hvalue(o),n));
5605 api_incr_top(L);
5607 static void lua_createtable(lua_State*L,int narray,int nrec){
5608 luaC_checkGC(L);
5609 sethvalue(L,L->top,luaH_new(L,narray,nrec));
5610 api_incr_top(L);
5612 static int lua_getmetatable(lua_State*L,int objindex){
5616 obj=index2adr(L,objindex);
5625 mt=G(L)->mt[ttype(obj)];
5631 sethvalue(L,L->top,mt);
5632 api_incr_top(L);
5637 static void lua_getfenv(lua_State*L,int idx){
5639 o=index2adr(L,idx);
5640 api_checkvalidindex(L,o);
5643 sethvalue(L,L->top,clvalue(o)->c.env);
5646 sethvalue(L,L->top,uvalue(o)->env);
5649 setobj(L,L->top,gt(thvalue(o)));
5652 setnilvalue(L->top);
5655 api_incr_top(L);
5657 static void lua_settable(lua_State*L,int idx){
5659 api_checknelems(L,2);
5660 t=index2adr(L,idx);
5661 api_checkvalidindex(L,t);
5662 luaV_settable(L,t,L->top-2,L->top-1);
5663 L->top-=2;
5665 static void lua_setfield(lua_State*L,int idx,const char*k){
5668 api_checknelems(L,1);
5669 t=index2adr(L,idx);
5670 api_checkvalidindex(L,t);
5671 setsvalue(L,&key,luaS_new(L,k));
5672 luaV_settable(L,t,&key,L->top-1);
5673 L->top--;
5675 static void lua_rawset(lua_State*L,int idx){
5677 api_checknelems(L,2);
5678 t=index2adr(L,idx);
5679 luai_apicheck(L,ttistable(t));
5680 setobj(L,luaH_set(L,hvalue(t),L->top-2),L->top-1);
5681 luaC_barriert(L,hvalue(t),L->top-1);
5682 L->top-=2;
5684 static void lua_rawseti(lua_State*L,int idx,int n){
5686 api_checknelems(L,1);
5687 o=index2adr(L,idx);
5688 luai_apicheck(L,ttistable(o));
5689 setobj(L,luaH_setnum(L,hvalue(o),n),L->top-1);
5690 luaC_barriert(L,hvalue(o),L->top-1);
5691 L->top--;
5693 static int lua_setmetatable(lua_State*L,int objindex){
5696 api_checknelems(L,1);
5697 obj=index2adr(L,objindex);
5698 api_checkvalidindex(L,obj);
5699 if(ttisnil(L->top-1))
5702 luai_apicheck(L,ttistable(L->top-1));
5703 mt=hvalue(L->top-1);
5709 luaC_objbarriert(L,hvalue(obj),mt);
5715 luaC_objbarrier(L,rawuvalue(obj),mt);
5719 G(L)->mt[ttype(obj)]=mt;
5723 L->top--;
5726 static int lua_setfenv(lua_State*L,int idx){
5729 api_checknelems(L,1);
5730 o=index2adr(L,idx);
5731 api_checkvalidindex(L,o);
5732 luai_apicheck(L,ttistable(L->top-1));
5735 clvalue(o)->c.env=hvalue(L->top-1);
5738 uvalue(o)->env=hvalue(L->top-1);
5741 sethvalue(L,gt(thvalue(o)),hvalue(L->top-1));
5747 if(res)luaC_objbarrier(L,gcvalue(o),hvalue(L->top-1));
5748 L->top--;
5751 #define adjustresults(L,nres){if(nres==(-1)&&L->top>=L->ci->top)L->ci->top=L->top;}
5752 #define checkresults(L,na,nr)luai_apicheck(L,(nr)==(-1)||(L->ci->top-L->top>=(nr)-(na)))
5753 static void lua_call(lua_State*L,int nargs,int nresults){
5755 api_checknelems(L,nargs+1);
5756 checkresults(L,nargs,nresults);
5757 func=L->top-(nargs+1);
5758 luaD_call(L,func,nresults);
5759 adjustresults(L,nresults);
5765 static void f_call(lua_State*L,void*ud){
5767 luaD_call(L,c->func,c->nresults);
5769 static int lua_pcall(lua_State*L,int nargs,int nresults,int errfunc){
5773 api_checknelems(L,nargs+1);
5774 checkresults(L,nargs,nresults);
5778 StkId o=index2adr(L,errfunc);
5779 api_checkvalidindex(L,o);
5780 func=savestack(L,o);
5782 c.func=L->top-(nargs+1);
5784 status=luaD_pcall(L,f_call,&c,savestack(L,c.func),func);
5785 adjustresults(L,nresults);
5788 static int lua_load(lua_State*L,lua_Reader reader,void*data,
5793 luaZ_init(L,&z,reader,data);
5794 status=luaD_protectedparser(L,&z,chunkname);
5797 static int lua_error(lua_State*L){
5798 api_checknelems(L,1);
5799 luaG_errormsg(L);
5802 static int lua_next(lua_State*L,int idx){
5805 t=index2adr(L,idx);
5806 luai_apicheck(L,ttistable(t));
5807 more=luaH_next(L,hvalue(t),L->top-1);
5809 api_incr_top(L);
5812 L->top-=1;
5815 static void lua_concat(lua_State*L,int n){
5816 api_checknelems(L,n);
5818 luaC_checkGC(L);
5819 luaV_concat(L,n,cast_int(L->top-L->base)-1);
5820 L->top-=(n-1);
5823 setsvalue(L,L->top,luaS_newlstr(L,"",0));
5824 api_incr_top(L);
5827 static void*lua_newuserdata(lua_State*L,size_t size){
5829 luaC_checkGC(L);
5830 u=luaS_newudata(L,size,getcurrenv(L));
5831 setuvalue(L,L->top,u);
5832 api_incr_top(L);
5835 #define luaL_getn(L,i)((int)lua_objlen(L,i))
5836 #define luaL_setn(L,i,j)((void)0)
5841 static void luaI_openlib(lua_State*L,const char*libname,
5843 static int luaL_argerror(lua_State*L,int numarg,const char*extramsg);
5844 static const char* luaL_checklstring(lua_State*L,int numArg,
5846 static const char* luaL_optlstring(lua_State*L,int numArg,
5848 static lua_Integer luaL_checkinteger(lua_State*L,int numArg);
5849 static lua_Integer luaL_optinteger(lua_State*L,int nArg,
5851 static int luaL_error(lua_State*L,const char*fmt,...);
5852 static const char* luaL_findtable(lua_State*L,int idx,
5854 #define luaL_argcheck(L,cond,numarg,extramsg)((void)((cond)||luaL_argerror(L,(numarg),(extramsg))))
5855 #define luaL_checkstring(L,n)(luaL_checklstring(L,(n),NULL))
5856 #define luaL_optstring(L,n,d)(luaL_optlstring(L,(n),(d),NULL))
5857 #define luaL_checkint(L,n)((int)luaL_checkinteger(L,(n)))
5858 #define luaL_optint(L,n,d)((int)luaL_optinteger(L,(n),(d)))
5859 #define luaL_typename(L,i)lua_typename(L,lua_type(L,(i)))
5860 #define luaL_getmetatable(L,n)(lua_getfield(L,(-10000),(n)))
5861 #define luaL_opt(L,f,n,d)(lua_isnoneornil(L,(n))?(d):f(L,(n)))
5865 lua_State*L;
5871 static int luaL_argerror(lua_State*L,int narg,const char*extramsg){
5873 if(!lua_getstack(L,0,&ar))
5874 return luaL_error(L,"bad argument #%d (%s)",narg,extramsg);
5875 lua_getinfo(L,"n",&ar);
5879 return luaL_error(L,"calling "LUA_QL("%s")" on bad self (%s)",
5884 return luaL_error(L,"bad argument #%d to "LUA_QL("%s")" (%s)",
5887 static int luaL_typerror(lua_State*L,int narg,const char*tname){
5888 const char*msg=lua_pushfstring(L,"%s expected, got %s",
5889 tname,luaL_typename(L,narg));
5890 return luaL_argerror(L,narg,msg);
5892 static void tag_error(lua_State*L,int narg,int tag){
5893 luaL_typerror(L,narg,lua_typename(L,tag));
5895 static void luaL_where(lua_State*L,int level){
5897 if(lua_getstack(L,level,&ar)){
5898 lua_getinfo(L,"Sl",&ar);
5900 lua_pushfstring(L,"%s:%d: ",ar.short_src,ar.currentline);
5904 lua_pushliteral(L,"");
5906 static int luaL_error(lua_State*L,const char*fmt,...){
5909 luaL_where(L,1);
5910 lua_pushvfstring(L,fmt,argp);
5912 lua_concat(L,2);
5913 return lua_error(L);
5915 static int luaL_newmetatable(lua_State*L,const char*tname){
5916 lua_getfield(L,(-10000),tname);
5917 if(!lua_isnil(L,-1))
5919 lua_pop(L,1);
5920 lua_newtable(L);
5921 lua_pushvalue(L,-1);
5922 lua_setfield(L,(-10000),tname);
5925 static void*luaL_checkudata(lua_State*L,int ud,const char*tname){
5926 void*p=lua_touserdata(L,ud);
5928 if(lua_getmetatable(L,ud)){
5929 lua_getfield(L,(-10000),tname);
5930 if(lua_rawequal(L,-1,-2)){
5931 lua_pop(L,2);
5936 luaL_typerror(L,ud,tname);
5939 static void luaL_checkstack(lua_State*L,int space,const char*mes){
5940 if(!lua_checkstack(L,space))
5941 luaL_error(L,"stack overflow (%s)",mes);
5943 static void luaL_checktype(lua_State*L,int narg,int t){
5944 if(lua_type(L,narg)!=t)
5945 tag_error(L,narg,t);
5947 static void luaL_checkany(lua_State*L,int narg){
5948 if(lua_type(L,narg)==(-1))
5949 luaL_argerror(L,narg,"value expected");
5951 static const char*luaL_checklstring(lua_State*L,int narg,size_t*len){
5952 const char*s=lua_tolstring(L,narg,len);
5953 if(!s)tag_error(L,narg,4);
5956 static const char*luaL_optlstring(lua_State*L,int narg,
5958 if(lua_isnoneornil(L,narg)){
5963 else return luaL_checklstring(L,narg,len);
5965 static lua_Number luaL_checknumber(lua_State*L,int narg){
5966 lua_Number d=lua_tonumber(L,narg);
5967 if(d==0&&!lua_isnumber(L,narg))
5968 tag_error(L,narg,3);
5971 static lua_Integer luaL_checkinteger(lua_State*L,int narg){
5972 lua_Integer d=lua_tointeger(L,narg);
5973 if(d==0&&!lua_isnumber(L,narg))
5974 tag_error(L,narg,3);
5977 static lua_Integer luaL_optinteger(lua_State*L,int narg,
5979 return luaL_opt(L,luaL_checkinteger,narg,def);
5981 static int luaL_getmetafield(lua_State*L,int obj,const char*event){
5982 if(!lua_getmetatable(L,obj))
5984 lua_pushstring(L,event);
5985 lua_rawget(L,-2);
5986 if(lua_isnil(L,-1)){
5987 lua_pop(L,2);
5991 lua_remove(L,-2);
5995 static void luaL_register(lua_State*L,const char*libname,
5997 luaI_openlib(L,libname,l,0);
6004 static void luaI_openlib(lua_State*L,const char*libname,
6008 luaL_findtable(L,(-10000),"_LOADED",1);
6009 lua_getfield(L,-1,libname);
6010 if(!lua_istable(L,-1)){
6011 lua_pop(L,1);
6012 if(luaL_findtable(L,(-10002),libname,size)!=NULL)
6013 luaL_error(L,"name conflict for module "LUA_QL("%s"),libname);
6014 lua_pushvalue(L,-1);
6015 lua_setfield(L,-3,libname);
6017 lua_remove(L,-2);
6018 lua_insert(L,-(nup+1));
6023 lua_pushvalue(L,-nup);
6024 lua_pushcclosure(L,l->func,nup);
6025 lua_setfield(L,-(nup+2),l->name);
6027 lua_pop(L,nup);
6029 static const char*luaL_findtable(lua_State*L,int idx,
6032 lua_pushvalue(L,idx);
6036 lua_pushlstring(L,fname,e-fname);
6037 lua_rawget(L,-2);
6038 if(lua_isnil(L,-1)){
6039 lua_pop(L,1);
6040 lua_createtable(L,0,(*e=='.'?1:szhint));
6041 lua_pushlstring(L,fname,e-fname);
6042 lua_pushvalue(L,-2);
6043 lua_settable(L,-4);
6045 else if(!lua_istable(L,-1)){
6046 lua_pop(L,2);
6049 lua_remove(L,-2);
6060 lua_pushlstring(B->L,B->buffer,l);
6068 lua_State*L=B->L;
6070 size_t toplen=lua_strlen(L,-1);
6072 size_t l=lua_strlen(L,-(toget+1));
6079 lua_concat(L,toget);
6094 lua_concat(B->L,B->lvl);
6098 lua_State*L=B->L;
6100 const char*s=lua_tolstring(L,-1,&vl);
6104 lua_pop(L,1);
6108 lua_insert(L,-2);
6113 static void luaL_buffinit(lua_State*L,luaL_Buffer*B){
6114 B->L=L;
6123 static const char*getF(lua_State*L,void*ud,size_t*size){
6125 (void)L;
6135 static int errfile(lua_State*L,const char*what,int fnameindex){
6137 const char*filename=lua_tostring(L,fnameindex)+1;
6138 lua_pushfstring(L,"cannot %s %s: %s",what,filename,serr);
6139 lua_remove(L,fnameindex);
6142 static int luaL_loadfile(lua_State*L,const char*filename){
6146 int fnameindex=lua_gettop(L)+1;
6149 lua_pushliteral(L,"=stdin");
6153 lua_pushfstring(L,"@%s",filename);
6155 if(lf.f==NULL)return errfile(L,"open",fnameindex);
6165 if(lf.f==NULL)return errfile(L,"reopen",fnameindex);
6170 status=lua_load(L,getF,&lf,lua_tostring(L,-1));
6174 lua_settop(L,fnameindex);
6175 return errfile(L,"read",fnameindex);
6177 lua_remove(L,fnameindex);
6184 static const char*getS(lua_State*L,void*ud,size_t*size){
6186 (void)L;
6192 static int luaL_loadbuffer(lua_State*L,const char*buff,size_t size,
6197 return lua_load(L,getS,&ls,name);
6209 static int panic(lua_State*L){
6210 (void)L;
6212 lua_tostring(L,-1));
6216 lua_State*L=lua_newstate(l_alloc,NULL);
6217 if(L)lua_atpanic(L,&panic);
6218 return L;
6220 static int luaB_tonumber(lua_State*L){
6221 int base=luaL_optint(L,2,10);
6223 luaL_checkany(L,1);
6224 if(lua_isnumber(L,1)){
6225 lua_pushnumber(L,lua_tonumber(L,1));
6230 const char*s1=luaL_checkstring(L,1);
6233 luaL_argcheck(L,2<=base&&base<=36,2,"base out of range");
6238 lua_pushnumber(L,(lua_Number)n);
6243 lua_pushnil(L);
6246 static int luaB_error(lua_State*L){
6247 int level=luaL_optint(L,2,1);
6248 lua_settop(L,1);
6249 if(lua_isstring(L,1)&&level>0){
6250 luaL_where(L,level);
6251 lua_pushvalue(L,1);
6252 lua_concat(L,2);
6254 return lua_error(L);
6256 static int luaB_setmetatable(lua_State*L){
6257 int t=lua_type(L,2);
6258 luaL_checktype(L,1,5);
6259 luaL_argcheck(L,t==0||t==5,2,
6261 if(luaL_getmetafield(L,1,"__metatable"))
6262 luaL_error(L,"cannot change a protected metatable");
6263 lua_settop(L,2);
6264 lua_setmetatable(L,1);
6267 static void getfunc(lua_State*L,int opt){
6268 if(lua_isfunction(L,1))lua_pushvalue(L,1);
6271 int level=opt?luaL_optint(L,1,1):luaL_checkint(L,1);
6272 luaL_argcheck(L,level>=0,1,"level must be non-negative");
6273 if(lua_getstack(L,level,&ar)==0)
6274 luaL_argerror(L,1,"invalid level");
6275 lua_getinfo(L,"f",&ar);
6276 if(lua_isnil(L,-1))
6277 luaL_error(L,"no function environment for tail call at level %d",
6281 static int luaB_setfenv(lua_State*L){
6282 luaL_checktype(L,2,5);
6283 getfunc(L,0);
6284 lua_pushvalue(L,2);
6285 if(lua_isnumber(L,1)&&lua_tonumber(L,1)==0){
6286 lua_pushthread(L);
6287 lua_insert(L,-2);
6288 lua_setfenv(L,-2);
6291 else if(lua_iscfunction(L,-2)||lua_setfenv(L,-2)==0)
6292 luaL_error(L,
6296 static int luaB_rawget(lua_State*L){
6297 luaL_checktype(L,1,5);
6298 luaL_checkany(L,2);
6299 lua_settop(L,2);
6300 lua_rawget(L,1);
6303 static int luaB_type(lua_State*L){
6304 luaL_checkany(L,1);
6305 lua_pushstring(L,luaL_typename(L,1));
6308 static int luaB_next(lua_State*L){
6309 luaL_checktype(L,1,5);
6310 lua_settop(L,2);
6311 if(lua_next(L,1))
6314 lua_pushnil(L);
6318 static int luaB_pairs(lua_State*L){
6319 luaL_checktype(L,1,5);
6320 lua_pushvalue(L,lua_upvalueindex(1));
6321 lua_pushvalue(L,1);
6322 lua_pushnil(L);
6325 static int ipairsaux(lua_State*L){
6326 int i=luaL_checkint(L,2);
6327 luaL_checktype(L,1,5);
6329 lua_pushinteger(L,i);
6330 lua_rawgeti(L,1,i);
6331 return(lua_isnil(L,-1))?0:2;
6333 static int luaB_ipairs(lua_State*L){
6334 luaL_checktype(L,1,5);
6335 lua_pushvalue(L,lua_upvalueindex(1));
6336 lua_pushvalue(L,1);
6337 lua_pushinteger(L,0);
6340 static int load_aux(lua_State*L,int status){
6344 lua_pushnil(L);
6345 lua_insert(L,-2);
6349 static int luaB_loadstring(lua_State*L){
6351 const char*s=luaL_checklstring(L,1,&l);
6352 const char*chunkname=luaL_optstring(L,2,s);
6353 return load_aux(L,luaL_loadbuffer(L,s,l,chunkname));
6355 static int luaB_loadfile(lua_State*L){
6356 const char*fname=luaL_optstring(L,1,NULL);
6357 return load_aux(L,luaL_loadfile(L,fname));
6359 static int luaB_assert(lua_State*L){
6360 luaL_checkany(L,1);
6361 if(!lua_toboolean(L,1))
6362 return luaL_error(L,"%s",luaL_optstring(L,2,"assertion failed!"));
6363 return lua_gettop(L);
6365 static int luaB_unpack(lua_State*L){
6367 luaL_checktype(L,1,5);
6368 i=luaL_optint(L,2,1);
6369 e=luaL_opt(L,luaL_checkint,3,luaL_getn(L,1));
6372 if(n<=0||!lua_checkstack(L,n))
6373 return luaL_error(L,"too many results to unpack");
6374 lua_rawgeti(L,1,i);
6376 lua_rawgeti(L,1,i);
6379 static int luaB_pcall(lua_State*L){
6381 luaL_checkany(L,1);
6382 status=lua_pcall(L,lua_gettop(L)-1,(-1),0);
6383 lua_pushboolean(L,(status==0));
6384 lua_insert(L,1);
6385 return lua_gettop(L);
6387 static int luaB_newproxy(lua_State*L){
6388 lua_settop(L,1);
6389 lua_newuserdata(L,0);
6390 if(lua_toboolean(L,1)==0)
6392 else if(lua_isboolean(L,1)){
6393 lua_newtable(L);
6394 lua_pushvalue(L,-1);
6395 lua_pushboolean(L,1);
6396 lua_rawset(L,lua_upvalueindex(1));
6400 if(lua_getmetatable(L,1)){
6401 lua_rawget(L,lua_upvalueindex(1));
6402 validproxy=lua_toboolean(L,-1);
6403 lua_pop(L,1);
6405 luaL_argcheck(L,validproxy,1,"boolean or proxy expected");
6406 lua_getmetatable(L,1);
6408 lua_setmetatable(L,2);
6426 static void auxopen(lua_State*L,const char*name,
6428 lua_pushcfunction(L,u);
6429 lua_pushcclosure(L,f,1);
6430 lua_setfield(L,-2,name);
6432 static void base_open(lua_State*L){
6433 lua_pushvalue(L,(-10002));
6434 lua_setglobal(L,"_G");
6435 luaL_register(L,"_G",base_funcs);
6436 lua_pushliteral(L,"Lua 5.1");
6437 lua_setglobal(L,"_VERSION");
6438 auxopen(L,"ipairs",luaB_ipairs,ipairsaux);
6439 auxopen(L,"pairs",luaB_pairs,luaB_next);
6440 lua_createtable(L,0,1);
6441 lua_pushvalue(L,-1);
6442 lua_setmetatable(L,-2);
6443 lua_pushliteral(L,"kv");
6444 lua_setfield(L,-2,"__mode");
6445 lua_pushcclosure(L,luaB_newproxy,1);
6446 lua_setglobal(L,"newproxy");
6448 static int luaopen_base(lua_State*L){
6449 base_open(L);
6452 #define aux_getn(L,n)(luaL_checktype(L,n,5),luaL_getn(L,n))
6453 static int tinsert(lua_State*L){
6454 int e=aux_getn(L,1)+1;
6456 switch(lua_gettop(L)){
6463 pos=luaL_checkint(L,2);
6466 lua_rawgeti(L,1,i-1);
6467 lua_rawseti(L,1,i);
6472 return luaL_error(L,"wrong number of arguments to "LUA_QL("insert"));
6475 luaL_setn(L,1,e);
6476 lua_rawseti(L,1,pos);
6479 static int tremove(lua_State*L){
6480 int e=aux_getn(L,1);
6481 int pos=luaL_optint(L,2,e);
6484 luaL_setn(L,1,e-1);
6485 lua_rawgeti(L,1,pos);
6487 lua_rawgeti(L,1,pos+1);
6488 lua_rawseti(L,1,pos);
6490 lua_pushnil(L);
6491 lua_rawseti(L,1,e);
6494 static void addfield(lua_State*L,luaL_Buffer*b,int i){
6495 lua_rawgeti(L,1,i);
6496 if(!lua_isstring(L,-1))
6497 luaL_error(L,"invalid value (%s) at index %d in table for "
6498 LUA_QL("concat"),luaL_typename(L,-1),i);
6501 static int tconcat(lua_State*L){
6505 const char*sep=luaL_optlstring(L,2,"",&lsep);
6506 luaL_checktype(L,1,5);
6507 i=luaL_optint(L,3,1);
6508 last=luaL_opt(L,luaL_checkint,4,luaL_getn(L,1));
6509 luaL_buffinit(L,&b);
6511 addfield(L,&b,i);
6515 addfield(L,&b,i);
6519 static void set2(lua_State*L,int i,int j){
6520 lua_rawseti(L,1,i);
6521 lua_rawseti(L,1,j);
6523 static int sort_comp(lua_State*L,int a,int b){
6524 if(!lua_isnil(L,2)){
6526 lua_pushvalue(L,2);
6527 lua_pushvalue(L,a-1);
6528 lua_pushvalue(L,b-2);
6529 lua_call(L,2,1);
6530 res=lua_toboolean(L,-1);
6531 lua_pop(L,1);
6535 return lua_lessthan(L,a,b);
6537 static void auxsort(lua_State*L,int l,int u){
6540 lua_rawgeti(L,1,l);
6541 lua_rawgeti(L,1,u);
6542 if(sort_comp(L,-1,-2))
6543 set2(L,l,u);
6545 lua_pop(L,2);
6548 lua_rawgeti(L,1,i);
6549 lua_rawgeti(L,1,l);
6550 if(sort_comp(L,-2,-1))
6551 set2(L,i,l);
6553 lua_pop(L,1);
6554 lua_rawgeti(L,1,u);
6555 if(sort_comp(L,-1,-2))
6556 set2(L,i,u);
6558 lua_pop(L,2);
6561 lua_rawgeti(L,1,i);
6562 lua_pushvalue(L,-1);
6563 lua_rawgeti(L,1,u-1);
6564 set2(L,i,u-1);
6567 while(lua_rawgeti(L,1,++i),sort_comp(L,-1,-2)){
6568 if(i>u)luaL_error(L,"invalid order function for sorting");
6569 lua_pop(L,1);
6571 while(lua_rawgeti(L,1,--j),sort_comp(L,-3,-1)){
6572 if(j<l)luaL_error(L,"invalid order function for sorting");
6573 lua_pop(L,1);
6576 lua_pop(L,3);
6579 set2(L,i,j);
6581 lua_rawgeti(L,1,u-1);
6582 lua_rawgeti(L,1,i);
6583 set2(L,u-1,i);
6590 auxsort(L,j,i);
6593 static int sort(lua_State*L){
6594 int n=aux_getn(L,1);
6595 luaL_checkstack(L,40,"");
6596 if(!lua_isnoneornil(L,2))
6597 luaL_checktype(L,2,6);
6598 lua_settop(L,2);
6599 auxsort(L,1,n);
6609 static int luaopen_table(lua_State*L){
6610 luaL_register(L,"table",tab_funcs);
6614 static int pushresult(lua_State*L,int i,const char*filename){
6617 lua_pushboolean(L,1);
6621 lua_pushnil(L);
6623 lua_pushfstring(L,"%s: %s",filename,strerror(en));
6625 lua_pushfstring(L,"%s",strerror(en));
6626 lua_pushinteger(L,en);
6630 static void fileerror(lua_State*L,int arg,const char*filename){
6631 lua_pushfstring(L,"%s: %s",filename,strerror(errno));
6632 luaL_argerror(L,arg,lua_tostring(L,-1));
6634 #define tofilep(L)((FILE**)luaL_checkudata(L,1,"FILE*"))
6635 static int io_type(lua_State*L){
6637 luaL_checkany(L,1);
6638 ud=lua_touserdata(L,1);
6639 lua_getfield(L,(-10000),"FILE*");
6640 if(ud==NULL||!lua_getmetatable(L,1)||!lua_rawequal(L,-2,-1))
6641 lua_pushnil(L);
6643 lua_pushliteral(L,"closed file");
6645 lua_pushliteral(L,"file");
6648 static FILE*tofile(lua_State*L){
6649 FILE**f=tofilep(L);
6651 luaL_error(L,"attempt to use a closed file");
6654 static FILE**newfile(lua_State*L){
6655 FILE**pf=(FILE**)lua_newuserdata(L,sizeof(FILE*));
6657 luaL_getmetatable(L,"FILE*");
6658 lua_setmetatable(L,-2);
6661 static int io_noclose(lua_State*L){
6662 lua_pushnil(L);
6663 lua_pushliteral(L,"cannot close standard file");
6666 static int io_pclose(lua_State*L){
6667 FILE**p=tofilep(L);
6668 int ok=lua_pclose(L,*p);
6670 return pushresult(L,ok,NULL);
6672 static int io_fclose(lua_State*L){
6673 FILE**p=tofilep(L);
6676 return pushresult(L,ok,NULL);
6678 static int aux_close(lua_State*L){
6679 lua_getfenv(L,1);
6680 lua_getfield(L,-1,"__close");
6681 return(lua_tocfunction(L,-1))(L);
6683 static int io_close(lua_State*L){
6684 if(lua_isnone(L,1))
6685 lua_rawgeti(L,(-10001),2);
6686 tofile(L);
6687 return aux_close(L);
6689 static int io_gc(lua_State*L){
6690 FILE*f=*tofilep(L);
6692 aux_close(L);
6695 static int io_open(lua_State*L){
6696 const char*filename=luaL_checkstring(L,1);
6697 const char*mode=luaL_optstring(L,2,"r");
6698 FILE**pf=newfile(L);
6700 return(*pf==NULL)?pushresult(L,0,filename):1;
6702 static FILE*getiofile(lua_State*L,int findex){
6704 lua_rawgeti(L,(-10001),findex);
6705 f=*(FILE**)lua_touserdata(L,-1);
6707 luaL_error(L,"standard %s file is closed",fnames[findex-1]);
6710 static int g_iofile(lua_State*L,int f,const char*mode){
6711 if(!lua_isnoneornil(L,1)){
6712 const char*filename=lua_tostring(L,1);
6714 FILE**pf=newfile(L);
6717 fileerror(L,1,filename);
6720 tofile(L);
6721 lua_pushvalue(L,1);
6723 lua_rawseti(L,(-10001),f);
6725 lua_rawgeti(L,(-10001),f);
6728 static int io_input(lua_State*L){
6729 return g_iofile(L,1,"r");
6731 static int io_output(lua_State*L){
6732 return g_iofile(L,2,"w");
6734 static int io_readline(lua_State*L);
6735 static void aux_lines(lua_State*L,int idx,int toclose){
6736 lua_pushvalue(L,idx);
6737 lua_pushboolean(L,toclose);
6738 lua_pushcclosure(L,io_readline,2);
6740 static int f_lines(lua_State*L){
6741 tofile(L);
6742 aux_lines(L,1,0);
6745 static int io_lines(lua_State*L){
6746 if(lua_isnoneornil(L,1)){
6747 lua_rawgeti(L,(-10001),1);
6748 return f_lines(L);
6751 const char*filename=luaL_checkstring(L,1);
6752 FILE**pf=newfile(L);
6755 fileerror(L,1,filename);
6756 aux_lines(L,lua_gettop(L),1);
6760 static int read_number(lua_State*L,FILE*f){
6763 lua_pushnumber(L,d);
6767 lua_pushnil(L);
6771 static int test_eof(lua_State*L,FILE*f){
6774 lua_pushlstring(L,NULL,0);
6777 static int read_line(lua_State*L,FILE*f){
6779 luaL_buffinit(L,&b);
6785 return(lua_objlen(L,-1)>0);
6797 static int read_chars(lua_State*L,FILE*f,size_t n){
6801 luaL_buffinit(L,&b);
6811 return(n==0||lua_objlen(L,-1)>0);
6813 static int g_read(lua_State*L,FILE*f,int first){
6814 int nargs=lua_gettop(L)-1;
6819 success=read_line(L,f);
6823 luaL_checkstack(L,nargs+20,"too many arguments");
6826 if(lua_type(L,n)==3){
6827 size_t l=(size_t)lua_tointeger(L,n);
6828 success=(l==0)?test_eof(L,f):read_chars(L,f,l);
6831 const char*p=lua_tostring(L,n);
6832 luaL_argcheck(L,p&&p[0]=='*',n,"invalid option");
6835 success=read_number(L,f);
6838 success=read_line(L,f);
6841 read_chars(L,f,~((size_t)0));
6845 return luaL_argerror(L,n,"invalid format");
6851 return pushresult(L,0,NULL);
6853 lua_pop(L,1);
6854 lua_pushnil(L);
6858 static int io_read(lua_State*L){
6859 return g_read(L,getiofile(L,1),1);
6861 static int f_read(lua_State*L){
6862 return g_read(L,tofile(L),2);
6864 static int io_readline(lua_State*L){
6865 FILE*f=*(FILE**)lua_touserdata(L,lua_upvalueindex(1));
6868 luaL_error(L,"file is already closed");
6869 success=read_line(L,f);
6871 return luaL_error(L,"%s",strerror(errno));
6874 if(lua_toboolean(L,lua_upvalueindex(2))){
6875 lua_settop(L,0);
6876 lua_pushvalue(L,lua_upvalueindex(1));
6877 aux_close(L);
6882 static int g_write(lua_State*L,FILE*f,int arg){
6883 int nargs=lua_gettop(L)-1;
6886 if(lua_type(L,arg)==3){
6888 fprintf(f,"%.14g",lua_tonumber(L,arg))>0;
6892 const char*s=luaL_checklstring(L,arg,&l);
6896 return pushresult(L,status,NULL);
6898 static int io_write(lua_State*L){
6899 return g_write(L,getiofile(L,2),1);
6901 static int f_write(lua_State*L){
6902 return g_write(L,tofile(L),2);
6904 static int io_flush(lua_State*L){
6905 return pushresult(L,fflush(getiofile(L,2))==0,NULL);
6907 static int f_flush(lua_State*L){
6908 return pushresult(L,fflush(tofile(L))==0,NULL);
6931 static void createmeta(lua_State*L){
6932 luaL_newmetatable(L,"FILE*");
6933 lua_pushvalue(L,-1);
6934 lua_setfield(L,-2,"__index");
6935 luaL_register(L,NULL,flib);
6937 static void createstdfile(lua_State*L,FILE*f,int k,const char*fname){
6938 *newfile(L)=f;
6940 lua_pushvalue(L,-1);
6941 lua_rawseti(L,(-10001),k);
6943 lua_pushvalue(L,-2);
6944 lua_setfenv(L,-2);
6945 lua_setfield(L,-3,fname);
6947 static void newfenv(lua_State*L,lua_CFunction cls){
6948 lua_createtable(L,0,1);
6949 lua_pushcfunction(L,cls);
6950 lua_setfield(L,-2,"__close");
6952 static int luaopen_io(lua_State*L){
6953 createmeta(L);
6954 newfenv(L,io_fclose);
6955 lua_replace(L,(-10001));
6956 luaL_register(L,"io",iolib);
6957 newfenv(L,io_noclose);
6958 createstdfile(L,stdin,1,"stdin");
6959 createstdfile(L,stdout,2,"stdout");
6960 createstdfile(L,stderr,0,"stderr");
6961 lua_pop(L,1);
6962 lua_getfield(L,-1,"popen");
6963 newfenv(L,io_pclose);
6964 lua_setfenv(L,-2);
6965 lua_pop(L,1);
6968 static int os_pushresult(lua_State*L,int i,const char*filename){
6971 lua_pushboolean(L,1);
6975 lua_pushnil(L);
6976 lua_pushfstring(L,"%s: %s",filename,strerror(en));
6977 lua_pushinteger(L,en);
6981 static int os_remove(lua_State*L){
6982 const char*filename=luaL_checkstring(L,1);
6983 return os_pushresult(L,remove(filename)==0,filename);
6985 static int os_exit(lua_State*L){
6986 exit(luaL_optint(L,1,EXIT_SUCCESS));
6993 static int luaopen_os(lua_State*L){
6994 luaL_register(L,"os",syslib);
7002 static int str_sub(lua_State*L){
7004 const char*s=luaL_checklstring(L,1,&l);
7005 ptrdiff_t start=posrelat(luaL_checkinteger(L,2),l);
7006 ptrdiff_t end=posrelat(luaL_optinteger(L,3,-1),l);
7010 lua_pushlstring(L,s+start-1,end-start+1);
7011 else lua_pushliteral(L,"");
7014 static int str_lower(lua_State*L){
7018 const char*s=luaL_checklstring(L,1,&l);
7019 luaL_buffinit(L,&b);
7025 static int str_upper(lua_State*L){
7029 const char*s=luaL_checklstring(L,1,&l);
7030 luaL_buffinit(L,&b);
7036 static int str_rep(lua_State*L){
7039 const char*s=luaL_checklstring(L,1,&l);
7040 int n=luaL_checkint(L,2);
7041 luaL_buffinit(L,&b);
7047 static int str_byte(lua_State*L){
7049 const char*s=luaL_checklstring(L,1,&l);
7050 ptrdiff_t posi=posrelat(luaL_optinteger(L,2,1),l);
7051 ptrdiff_t pose=posrelat(luaL_optinteger(L,3,posi),l);
7058 luaL_error(L,"string slice too long");
7059 luaL_checkstack(L,n,"string slice too long");
7061 lua_pushinteger(L,uchar(s[posi+i-1]));
7064 static int str_char(lua_State*L){
7065 int n=lua_gettop(L);
7068 luaL_buffinit(L,&b);
7070 int c=luaL_checkint(L,i);
7071 luaL_argcheck(L,uchar(c)==c,i,"invalid value");
7080 lua_State*L;
7090 return luaL_error(ms->L,"invalid capture index");
7097 return luaL_error(ms->L,"invalid pattern capture");
7103 luaL_error(ms->L,"malformed pattern (ends with "LUA_QL("%%")")");
7110 luaL_error(ms->L,"malformed pattern (missing "LUA_QL("]")")");
7171 luaL_error(ms->L,"unbalanced pattern");
7213 if(level>=32)luaL_error(ms->L,"too many captures");
7262 luaL_error(ms->L,"missing "LUA_QL("[")" after "
7339 lua_pushlstring(ms->L,s,e-s);
7341 luaL_error(ms->L,"invalid capture index");
7345 if(l==(-1))luaL_error(ms->L,"unfinished capture");
7347 lua_pushinteger(ms->L,ms->capture[i].init-ms->src_init+1);
7349 lua_pushlstring(ms->L,ms->capture[i].init,l);
7355 luaL_checkstack(ms->L,nlevels,"too many captures");
7360 static int str_find_aux(lua_State*L,int find){
7362 const char*s=luaL_checklstring(L,1,&l1);
7363 const char*p=luaL_checklstring(L,2,&l2);
7364 ptrdiff_t init=posrelat(luaL_optinteger(L,3,1),l1)-1;
7367 if(find&&(lua_toboolean(L,4)||
7371 lua_pushinteger(L,s2-s+1);
7372 lua_pushinteger(L,s2-s+l2);
7380 ms.L=L;
7388 lua_pushinteger(L,s1-s+1);
7389 lua_pushinteger(L,res-s);
7397 lua_pushnil(L);
7400 static int str_find(lua_State*L){
7401 return str_find_aux(L,1);
7403 static int str_match(lua_State*L){
7404 return str_find_aux(L,0);
7406 static int gmatch_aux(lua_State*L){
7409 const char*s=lua_tolstring(L,lua_upvalueindex(1),&ls);
7410 const char*p=lua_tostring(L,lua_upvalueindex(2));
7412 ms.L=L;
7415 for(src=s+(size_t)lua_tointeger(L,lua_upvalueindex(3));
7423 lua_pushinteger(L,newstart);
7424 lua_replace(L,lua_upvalueindex(3));
7430 static int gmatch(lua_State*L){
7431 luaL_checkstring(L,1);
7432 luaL_checkstring(L,2);
7433 lua_settop(L,2);
7434 lua_pushinteger(L,0);
7435 lua_pushcclosure(L,gmatch_aux,3);
7441 const char*news=lua_tolstring(ms->L,3,&l);
7460 lua_State*L=ms->L;
7461 switch(lua_type(L,3)){
7469 lua_pushvalue(L,3);
7471 lua_call(L,n,1);
7476 lua_gettable(L,3);
7480 if(!lua_toboolean(L,-1)){
7481 lua_pop(L,1);
7482 lua_pushlstring(L,s,e-s);
7484 else if(!lua_isstring(L,-1))
7485 luaL_error(L,"invalid replacement value (a %s)",luaL_typename(L,-1));
7488 static int str_gsub(lua_State*L){
7490 const char*src=luaL_checklstring(L,1,&srcl);
7491 const char*p=luaL_checkstring(L,2);
7492 int tr=lua_type(L,3);
7493 int max_s=luaL_optint(L,4,srcl+1);
7498 luaL_argcheck(L,tr==3||tr==4||
7501 luaL_buffinit(L,&b);
7502 ms.L=L;
7522 lua_pushinteger(L,n);
7525 static void addquoted(lua_State*L,luaL_Buffer*b,int arg){
7527 const char*s=luaL_checklstring(L,arg,&l);
7553 static const char*scanformat(lua_State*L,const char*strfrmt,char*form){
7557 luaL_error(L,"invalid format (repeated flags)");
7566 luaL_error(L,"invalid format (width or precision too long)");
7580 static int str_format(lua_State*L){
7581 int top=lua_gettop(L);
7584 const char*strfrmt=luaL_checklstring(L,arg,&sfl);
7587 luaL_buffinit(L,&b);
7597 luaL_argerror(L,arg,"no value");
7598 strfrmt=scanformat(L,strfrmt,form);
7601 sprintf(buff,form,(int)luaL_checknumber(L,arg));
7606 sprintf(buff,form,(long)luaL_checknumber(L,arg));
7611 sprintf(buff,form,(unsigned long)luaL_checknumber(L,arg));
7616 sprintf(buff,form,(double)luaL_checknumber(L,arg));
7620 addquoted(L,&b,arg);
7625 const char*s=luaL_checklstring(L,arg,&l);
7627 lua_pushvalue(L,arg);
7637 return luaL_error(L,"invalid option "LUA_QL("%%%c")" to "
7661 static void createmetatable(lua_State*L){
7662 lua_createtable(L,0,1);
7663 lua_pushliteral(L,"");
7664 lua_pushvalue(L,-2);
7665 lua_setmetatable(L,-2);
7666 lua_pop(L,1);
7667 lua_pushvalue(L,-2);
7668 lua_setfield(L,-2,"__index");
7669 lua_pop(L,1);
7671 static int luaopen_string(lua_State*L){
7672 luaL_register(L,"string",strlib);
7673 createmetatable(L);
7684 static void luaL_openlibs(lua_State*L){
7687 lua_pushcfunction(L,lib->func);
7688 lua_pushstring(L,lib->name);
7689 lua_call(L,1,0);
7693 static UB barg(lua_State*L,int idx){
7695 bn.n=lua_tonumber(L,idx)+6755399441055744.0;
7696 if(bn.n==0.0&&!lua_isnumber(L,idx))luaL_typerror(L,idx,"number");
7699 #define BRET(b)lua_pushnumber(L,(lua_Number)(int)(b));return 1;
7700 static int tobit(lua_State*L){
7701 BRET(barg(L,1))}
7702 static int bnot(lua_State*L){
7703 BRET(~barg(L,1))}
7704 static int band(lua_State*L){
7705 int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b&=barg(L,i);BRET(b)}
7706 static int bor(lua_State*L){
7707 int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b|=barg(L,i);BRET(b)}
7708 static int bxor(lua_State*L){
7709 int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b^=barg(L,i);BRET(b)}
7710 static int lshift(lua_State*L){
7711 UB b=barg(L,1),n=barg(L,2)&31;BRET(b<<n)}
7712 static int rshift(lua_State*L){
7713 UB b=barg(L,1),n=barg(L,2)&31;BRET(b>>n)}
7714 static int arshift(lua_State*L){
7715 UB b=barg(L,1),n=barg(L,2)&31;BRET((int)b>>n)}
7716 static int rol(lua_State*L){
7717 UB b=barg(L,1),n=barg(L,2)&31;BRET((b<<n)|(b>>(32-n)))}
7718 static int ror(lua_State*L){
7719 UB b=barg(L,1),n=barg(L,2)&31;BRET((b>>n)|(b<<(32-n)))}
7720 static int bswap(lua_State*L){
7721 UB b=barg(L,1);b=(b>>24)|((b>>8)&0xff00)|((b&0xff00)<<8)|(b<<24);BRET(b)}
7722 static int tohex(lua_State*L){
7723 UB b=barg(L,1);
7724 int n=lua_isnone(L,2)?8:(int)barg(L,2);
7731 lua_pushlstring(L,buf,(size_t)n);
7750 lua_State*L=luaL_newstate();
7752 luaL_openlibs(L);
7753 luaL_register(L,"bit",bitlib);
7755 lua_createtable(L,0,1);
7756 lua_pushstring(L,argv[1]);
7757 lua_rawseti(L,-2,0);
7758 lua_setglobal(L,"arg");
7759 if(luaL_loadfile(L,argv[1]))
7762 lua_pushstring(L,argv[i]);
7763 if(lua_pcall(L,argc-2,0,0)){
7765 fprintf(stderr,"Error: %s\n",lua_tostring(L,-1));
7768 lua_close(L);