w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

minilua.c
Go to the documentation of this file.
1 /* This is a heavily customized and minimized copy of Lua 5.1.5. */
2 /* It's only used to build LuaJIT. It does NOT have all standard functions! */
3 /******************************************************************************
4 * Copyright (C) 1994-2012 Lua.org, PUC-Rio. All rights reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 ******************************************************************************/
25 #ifdef _MSC_VER
26 typedef unsigned __int64 U64;
27 #else
28 typedef unsigned long long U64;
29 #endif
30 int _CRT_glob = 0;
31 #include <stddef.h>
32 #include <stdarg.h>
33 #include <limits.h>
34 #include <math.h>
35 #include <ctype.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <setjmp.h>
40 #include <errno.h>
41 #include <time.h>
42 typedef enum{
60 TM_N
61 }TMS;
63 typedef enum{
101 OP_VARARG
107 OpArgK
108 };
109 typedef enum{
124 VVARARG
126 enum RESERVED{
133 };
134 typedef enum BinOpr{
143 #define LUA_QL(x)"'"x"'"
144 #define luai_apicheck(L,o){(void)L;}
145 #define lua_number2str(s,n)sprintf((s),"%.14g",(n))
146 #define lua_str2number(s,p)strtod((s),(p))
147 #define luai_numadd(a,b)((a)+(b))
148 #define luai_numsub(a,b)((a)-(b))
149 #define luai_nummul(a,b)((a)*(b))
150 #define luai_numdiv(a,b)((a)/(b))
151 #define luai_nummod(a,b)((a)-floor((a)/(b))*(b))
152 #define luai_numpow(a,b)(pow(a,b))
153 #define luai_numunm(a)(-(a))
154 #define luai_numeq(a,b)((a)==(b))
155 #define luai_numlt(a,b)((a)<(b))
156 #define luai_numle(a,b)((a)<=(b))
157 #define luai_numisnan(a)(!luai_numeq((a),(a)))
158 #define lua_number2int(i,d)((i)=(int)(d))
159 #define lua_number2integer(i,d)((i)=(lua_Integer)(d))
160 #define LUAI_THROW(L,c)longjmp((c)->b,1)
161 #define LUAI_TRY(L,c,a)if(setjmp((c)->b)==0){a}
162 #define lua_pclose(L,file)((void)((void)L,file),0)
163 #define lua_upvalueindex(i)((-10002)-(i))
164 typedef struct lua_State lua_State;
166 typedef const char*(*lua_Reader)(lua_State*L,void*ud,size_t*sz);
167 typedef void*(*lua_Alloc)(void*ud,void*ptr,size_t osize,size_t nsize);
168 typedef double lua_Number;
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)
179 #define lua_newtable(L)lua_createtable(L,0,0)
180 #define lua_pushcfunction(L,f)lua_pushcclosure(L,(f),0)
181 #define lua_strlen(L,i)lua_objlen(L,(i))
182 #define lua_isfunction(L,n)(lua_type(L,(n))==6)
183 #define lua_istable(L,n)(lua_type(L,(n))==5)
184 #define lua_isnil(L,n)(lua_type(L,(n))==0)
185 #define lua_isboolean(L,n)(lua_type(L,(n))==1)
186 #define lua_isnone(L,n)(lua_type(L,(n))==(-1))
187 #define lua_isnoneornil(L,n)(lua_type(L,(n))<=0)
188 #define lua_pushliteral(L,s)lua_pushlstring(L,""s,(sizeof(s)/sizeof(char))-1)
189 #define lua_setglobal(L,s)lua_setfield(L,(-10002),(s))
190 #define lua_tostring(L,i)lua_tolstring(L,(i),NULL)
191 typedef struct lua_Debug lua_Debug;
193 struct lua_Debug{
194 int event;
195 const char*name;
196 const char*namewhat;
197 const char*what;
198 const char*source;
199 int currentline;
200 int nups;
201 int linedefined;
202 int lastlinedefined;
203 char short_src[60];
204 int i_ci;
205 };
206 typedef unsigned int lu_int32;
207 typedef size_t lu_mem;
208 typedef ptrdiff_t l_mem;
209 typedef unsigned char lu_byte;
210 #define IntPoint(p)((unsigned int)(lu_mem)(p))
211 typedef union{double u;void*s;long l;}L_Umaxalign;
212 typedef double l_uacNumber;
213 #define check_exp(c,e)(e)
214 #define UNUSED(x)((void)(x))
215 #define cast(t,exp)((t)(exp))
216 #define cast_byte(i)cast(lu_byte,(i))
217 #define cast_num(i)cast(lua_Number,(i))
218 #define cast_int(i)cast(int,(i))
220 #define condhardstacktests(x)((void)0)
221 typedef union GCObject GCObject;
222 typedef struct GCheader{
225 typedef union{
226 GCObject*gc;
227 void*p;
228 lua_Number n;
229 int b;
230 }Value;
231 typedef struct lua_TValue{
234 #define ttisnil(o)(ttype(o)==0)
235 #define ttisnumber(o)(ttype(o)==3)
236 #define ttisstring(o)(ttype(o)==4)
237 #define ttistable(o)(ttype(o)==5)
238 #define ttisfunction(o)(ttype(o)==6)
239 #define ttisboolean(o)(ttype(o)==1)
240 #define ttisuserdata(o)(ttype(o)==7)
241 #define ttisthread(o)(ttype(o)==8)
242 #define ttislightuserdata(o)(ttype(o)==2)
243 #define ttype(o)((o)->tt)
244 #define gcvalue(o)check_exp(iscollectable(o),(o)->value.gc)
245 #define pvalue(o)check_exp(ttislightuserdata(o),(o)->value.p)
246 #define nvalue(o)check_exp(ttisnumber(o),(o)->value.n)
247 #define rawtsvalue(o)check_exp(ttisstring(o),&(o)->value.gc->ts)
248 #define tsvalue(o)(&rawtsvalue(o)->tsv)
249 #define rawuvalue(o)check_exp(ttisuserdata(o),&(o)->value.gc->u)
250 #define uvalue(o)(&rawuvalue(o)->uv)
251 #define clvalue(o)check_exp(ttisfunction(o),&(o)->value.gc->cl)
252 #define hvalue(o)check_exp(ttistable(o),&(o)->value.gc->h)
253 #define bvalue(o)check_exp(ttisboolean(o),(o)->value.b)
254 #define thvalue(o)check_exp(ttisthread(o),&(o)->value.gc->th)
255 #define l_isfalse(o)(ttisnil(o)||(ttisboolean(o)&&bvalue(o)==0))
256 #define checkconsistency(obj)
257 #define checkliveness(g,obj)
258 #define setnilvalue(obj)((obj)->tt=0)
259 #define setnvalue(obj,x){TValue*i_o=(obj);i_o->value.n=(x);i_o->tt=3;}
260 #define setbvalue(obj,x){TValue*i_o=(obj);i_o->value.b=(x);i_o->tt=1;}
261 #define setsvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=4;checkliveness(G(L),i_o);}
262 #define setuvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=7;checkliveness(G(L),i_o);}
263 #define setthvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=8;checkliveness(G(L),i_o);}
264 #define setclvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=6;checkliveness(G(L),i_o);}
265 #define sethvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=5;checkliveness(G(L),i_o);}
266 #define setptvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=(8+1);checkliveness(G(L),i_o);}
267 #define setobj(L,obj1,obj2){const TValue*o2=(obj2);TValue*o1=(obj1);o1->value=o2->value;o1->tt=o2->tt;checkliveness(G(L),o1);}
268 #define setttype(obj,tt)(ttype(obj)=(tt))
269 #define iscollectable(o)(ttype(o)>=4)
270 typedef TValue*StkId;
271 typedef union TString{
273 struct{
276 unsigned int hash;
277 size_t len;
280 #define getstr(ts)cast(const char*,(ts)+1)
281 #define svalue(o)getstr(rawtsvalue(o))
282 typedef union Udata{
284 struct{
286 struct Table*metatable;
287 struct Table*env;
288 size_t len;
289 }uv;
291 typedef struct Proto{
293 TValue*k;
295 struct Proto**p;
296 int*lineinfo;
300 int sizeupvalues;
301 int sizek;
302 int sizecode;
303 int sizelineinfo;
304 int sizep;
305 int sizelocvars;
306 int linedefined;
307 int lastlinedefined;
314 typedef struct LocVar{
316 int startpc;
317 int endpc;
319 typedef struct UpVal{
321 TValue*v;
322 union{
323 TValue value;
324 struct{
325 struct UpVal*prev;
326 struct UpVal*next;
327 }l;
328 }u;
330 typedef struct CClosure{
333 TValue upvalue[1];
335 typedef struct LClosure{
337 struct Proto*p;
338 UpVal*upvals[1];
340 typedef union Closure{
341 CClosure c;
342 LClosure l;
344 #define iscfunction(o)(ttype(o)==6&&clvalue(o)->c.isC)
345 typedef union TKey{
346 struct{
348 struct Node*next;
349 }nk;
350 TValue tvk;
352 typedef struct Node{
353 TValue i_val;
354 TKey i_key;
356 typedef struct Table{
358 lu_byte flags;
360 struct Table*metatable;
361 TValue*array;
362 Node*node;
363 Node*lastfree;
367 #define lmod(s,size)(check_exp((size&(size-1))==0,(cast(int,(s)&((size)-1)))))
368 #define twoto(x)((size_t)1<<(x))
369 #define sizenode(t)(twoto((t)->lsizenode))
370 static const TValue luaO_nilobject_;
371 #define ceillog2(x)(luaO_log2((x)-1)+1)
372 static int luaO_log2(unsigned int x);
373 #define gfasttm(g,et,e)((et)==NULL?NULL:((et)->flags&(1u<<(e)))?NULL:luaT_gettm(et,e,(g)->tmname[e]))
374 #define fasttm(l,et,e)gfasttm(G(l),et,e)
375 static const TValue*luaT_gettm(Table*events,TMS event,TString*ename);
376 #define luaM_reallocv(L,b,on,n,e)((cast(size_t,(n)+1)<=((size_t)(~(size_t)0)-2)/(e))?luaM_realloc_(L,(b),(on)*(e),(n)*(e)):luaM_toobig(L))
377 #define luaM_freemem(L,b,s)luaM_realloc_(L,(b),(s),0)
378 #define luaM_free(L,b)luaM_realloc_(L,(b),sizeof(*(b)),0)
379 #define luaM_freearray(L,b,n,t)luaM_reallocv(L,(b),n,0,sizeof(t))
380 #define luaM_malloc(L,t)luaM_realloc_(L,NULL,0,(t))
381 #define luaM_new(L,t)cast(t*,luaM_malloc(L,sizeof(t)))
382 #define luaM_newvector(L,n,t)cast(t*,luaM_reallocv(L,NULL,0,n,sizeof(t)))
383 #define luaM_growvector(L,v,nelems,size,t,limit,e)if((nelems)+1>(size))((v)=cast(t*,luaM_growaux_(L,v,&(size),sizeof(t),limit,e)))
384 #define luaM_reallocvector(L,v,oldn,n,t)((v)=cast(t*,luaM_reallocv(L,v,oldn,n,sizeof(t))))
385 static void*luaM_realloc_(lua_State*L,void*block,size_t oldsize,
386 size_t size);
387 static void*luaM_toobig(lua_State*L);
388 static void*luaM_growaux_(lua_State*L,void*block,int*size,
389 size_t size_elem,int limit,
390 const char*errormsg);
391 typedef struct Zio ZIO;
392 #define char2int(c)cast(int,cast(unsigned char,(c)))
393 #define zgetc(z)(((z)->n--)>0?char2int(*(z)->p++):luaZ_fill(z))
394 typedef struct Mbuffer{
395 char*buffer;
396 size_t n;
397 size_t buffsize;
399 #define luaZ_initbuffer(L,buff)((buff)->buffer=NULL,(buff)->buffsize=0)
400 #define luaZ_buffer(buff)((buff)->buffer)
401 #define luaZ_sizebuffer(buff)((buff)->buffsize)
402 #define luaZ_bufflen(buff)((buff)->n)
403 #define luaZ_resetbuffer(buff)((buff)->n=0)
404 #define luaZ_resizebuffer(L,buff,size)(luaM_reallocvector(L,(buff)->buffer,(buff)->buffsize,size,char),(buff)->buffsize=size)
405 #define luaZ_freebuffer(L,buff)luaZ_resizebuffer(L,buff,0)
406 struct Zio{
407 size_t n;
408 const char*p;
410 void*data;
411 lua_State*L;
412 };
413 static int luaZ_fill(ZIO*z);
414 struct lua_longjmp;
415 #define gt(L)(&L->l_gt)
416 #define registry(L)(&G(L)->l_registry)
417 typedef struct stringtable{
420 int size;
422 typedef struct CallInfo{
423 StkId base;
424 StkId func;
425 StkId top;
426 const Instruction*savedpc;
430 #define curr_func(L)(clvalue(L->ci->func))
431 #define ci_func(ci)(clvalue((ci)->func))
432 #define f_isLua(ci)(!ci_func(ci)->c.isC)
433 #define isLua(ci)(ttisfunction((ci)->func)&&f_isLua(ci))
434 typedef struct global_State{
437 void*ud;
443 GCObject*gray;
445 GCObject*weak;
452 int gcpause;
453 int gcstepmul;
456 struct lua_State*mainthread;
458 struct Table*mt[(8+1)];
461 struct lua_State{
464 StkId top;
467 CallInfo*ci;
470 StkId stack;
473 int stacksize;
475 unsigned short nCcalls;
476 unsigned short baseCcalls;
479 int basehookcount;
480 int hookcount;
486 struct lua_longjmp*errorJmp;
488 };
489 #define G(L)(L->l_G)
490 union GCObject{
492 union TString ts;
493 union Udata u;
494 union Closure cl;
495 struct Table h;
496 struct Proto p;
497 struct UpVal uv;
498 struct lua_State th;
499 };
500 #define rawgco2ts(o)check_exp((o)->gch.tt==4,&((o)->ts))
501 #define gco2ts(o)(&rawgco2ts(o)->tsv)
502 #define rawgco2u(o)check_exp((o)->gch.tt==7,&((o)->u))
503 #define gco2u(o)(&rawgco2u(o)->uv)
504 #define gco2cl(o)check_exp((o)->gch.tt==6,&((o)->cl))
505 #define gco2h(o)check_exp((o)->gch.tt==5,&((o)->h))
506 #define gco2p(o)check_exp((o)->gch.tt==(8+1),&((o)->p))
507 #define gco2uv(o)check_exp((o)->gch.tt==(8+2),&((o)->uv))
508 #define ngcotouv(o)check_exp((o)==NULL||(o)->gch.tt==(8+2),&((o)->uv))
509 #define gco2th(o)check_exp((o)->gch.tt==8,&((o)->th))
510 #define obj2gco(v)(cast(GCObject*,(v)))
511 static void luaE_freethread(lua_State*L,lua_State*L1);
512 #define pcRel(pc,p)(cast(int,(pc)-(p)->code)-1)
513 #define getline_(f,pc)(((f)->lineinfo)?(f)->lineinfo[pc]:0)
514 #define resethookcount(L)(L->hookcount=L->basehookcount)
515 static void luaG_typeerror(lua_State*L,const TValue*o,
516 const char*opname);
517 static void luaG_runerror(lua_State*L,const char*fmt,...);
518 #define luaD_checkstack(L,n)if((char*)L->stack_last-(char*)L->top<=(n)*(int)sizeof(TValue))luaD_growstack(L,n);else condhardstacktests(luaD_reallocstack(L,L->stacksize-5-1));
519 #define incr_top(L){luaD_checkstack(L,1);L->top++;}
520 #define savestack(L,p)((char*)(p)-(char*)L->stack)
521 #define restorestack(L,n)((TValue*)((char*)L->stack+(n)))
522 #define saveci(L,p)((char*)(p)-(char*)L->base_ci)
523 #define restoreci(L,n)((CallInfo*)((char*)L->base_ci+(n)))
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,
531 int limit,const char*errormsg){
532 void*newblock;
533 int newsize;
534 if(*size>=limit/2){
535 if(*size>=limit)
536 luaG_runerror(L,errormsg);
537 newsize=limit;
538 }
539 else{
540 newsize=(*size)*2;
541 if(newsize<4)
542 newsize=4;
543 }
544 newblock=luaM_reallocv(L,block,*size,newsize,size_elems);
545 *size=newsize;
546 return newblock;
547 }
548 static void*luaM_toobig(lua_State*L){
549 luaG_runerror(L,"memory allocation error: block too big");
550 return NULL;
551 }
552 static void*luaM_realloc_(lua_State*L,void*block,size_t osize,size_t nsize){
553 global_State*g=G(L);
554 block=(*g->frealloc)(g->ud,block,osize,nsize);
555 if(block==NULL&&nsize>0)
556 luaD_throw(L,4);
557 g->totalbytes=(g->totalbytes-osize)+nsize;
558 return block;
559 }
560 #define resetbits(x,m)((x)&=cast(lu_byte,~(m)))
561 #define setbits(x,m)((x)|=(m))
562 #define testbits(x,m)((x)&(m))
563 #define bitmask(b)(1<<(b))
564 #define bit2mask(b1,b2)(bitmask(b1)|bitmask(b2))
565 #define l_setbit(x,b)setbits(x,bitmask(b))
566 #define resetbit(x,b)resetbits(x,bitmask(b))
567 #define testbit(x,b)testbits(x,bitmask(b))
568 #define set2bits(x,b1,b2)setbits(x,(bit2mask(b1,b2)))
569 #define reset2bits(x,b1,b2)resetbits(x,(bit2mask(b1,b2)))
570 #define test2bits(x,b1,b2)testbits(x,(bit2mask(b1,b2)))
571 #define iswhite(x)test2bits((x)->gch.marked,0,1)
572 #define isblack(x)testbit((x)->gch.marked,2)
573 #define isgray(x)(!isblack(x)&&!iswhite(x))
574 #define otherwhite(g)(g->currentwhite^bit2mask(0,1))
575 #define isdead(g,v)((v)->gch.marked&otherwhite(g)&bit2mask(0,1))
576 #define changewhite(x)((x)->gch.marked^=bit2mask(0,1))
577 #define gray2black(x)l_setbit((x)->gch.marked,2)
578 #define valiswhite(x)(iscollectable(x)&&iswhite(gcvalue(x)))
579 #define luaC_white(g)cast(lu_byte,(g)->currentwhite&bit2mask(0,1))
580 #define luaC_checkGC(L){condhardstacktests(luaD_reallocstack(L,L->stacksize-5-1));if(G(L)->totalbytes>=G(L)->GCthreshold)luaC_step(L);}
581 #define luaC_barrier(L,p,v){if(valiswhite(v)&&isblack(obj2gco(p)))luaC_barrierf(L,obj2gco(p),gcvalue(v));}
582 #define luaC_barriert(L,t,v){if(valiswhite(v)&&isblack(obj2gco(t)))luaC_barrierback(L,t);}
583 #define luaC_objbarrier(L,p,o){if(iswhite(obj2gco(o))&&isblack(obj2gco(p)))luaC_barrierf(L,obj2gco(p),obj2gco(o));}
584 #define luaC_objbarriert(L,t,o){if(iswhite(obj2gco(o))&&isblack(obj2gco(t)))luaC_barrierback(L,t);}
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);
590 #define sizestring(s)(sizeof(union TString)+((s)->len+1)*sizeof(char))
591 #define sizeudata(u)(sizeof(union Udata)+(u)->len)
592 #define luaS_new(L,s)(luaS_newlstr(L,s,strlen(s)))
593 #define luaS_newliteral(L,s)(luaS_newlstr(L,""s,(sizeof(s)/sizeof(char))-1))
594 #define luaS_fix(s)l_setbit((s)->tsv.marked,5)
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)))
597 #define tonumber(o,n)(ttype(o)==3||(((o)=luaV_tonumber(o,n))!=NULL))
598 #define equalobj(L,o1,o2)(ttype(o1)==ttype(o2)&&luaV_equalval(L,o1,o2))
599 static int luaV_equalval(lua_State*L,const TValue*t1,const TValue*t2);
600 static const TValue*luaV_tonumber(const TValue*obj,TValue*n);
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);
604 static const TValue luaO_nilobject_={{NULL},0};
605 static int luaO_int2fb(unsigned int x){
606 int e=0;
607 while(x>=16){
608 x=(x+1)>>1;
609 e++;
610 }
611 if(x<8)return x;
612 else return((e+1)<<3)|(cast_int(x)-8);
613 }
614 static int luaO_fb2int(int x){
615 int e=(x>>3)&31;
616 if(e==0)return x;
617 else return((x&7)+8)<<(e-1);
618 }
619 static int luaO_log2(unsigned int x){
620 static const lu_byte log_2[256]={
621 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
622 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
623 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
624 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
625 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
626 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
627 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
628 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
629 };
630 int l=-1;
631 while(x>=256){l+=8;x>>=8;}
632 return l+log_2[x];
633 }
634 static int luaO_rawequalObj(const TValue*t1,const TValue*t2){
635 if(ttype(t1)!=ttype(t2))return 0;
636 else switch(ttype(t1)){
637 case 0:
638 return 1;
639 case 3:
640 return luai_numeq(nvalue(t1),nvalue(t2));
641 case 1:
642 return bvalue(t1)==bvalue(t2);
643 case 2:
644 return pvalue(t1)==pvalue(t2);
645 default:
646 return gcvalue(t1)==gcvalue(t2);
647 }
648 }
649 static int luaO_str2d(const char*s,lua_Number*result){
650 char*endptr;
651 *result=lua_str2number(s,&endptr);
652 if(endptr==s)return 0;
653 if(*endptr=='x'||*endptr=='X')
654 *result=cast_num(strtoul(s,&endptr,16));
655 if(*endptr=='\0')return 1;
656 while(isspace(cast(unsigned char,*endptr)))endptr++;
657 if(*endptr!='\0')return 0;
658 return 1;
659 }
660 static void pushstr(lua_State*L,const char*str){
661 setsvalue(L,L->top,luaS_new(L,str));
662 incr_top(L);
663 }
664 static const char*luaO_pushvfstring(lua_State*L,const char*fmt,va_list argp){
665 int n=1;
666 pushstr(L,"");
667 for(;;){
668 const char*e=strchr(fmt,'%');
669 if(e==NULL)break;
670 setsvalue(L,L->top,luaS_newlstr(L,fmt,e-fmt));
671 incr_top(L);
672 switch(*(e+1)){
673 case's':{
674 const char*s=va_arg(argp,char*);
675 if(s==NULL)s="(null)";
676 pushstr(L,s);
677 break;
678 }
679 case'c':{
680 char buff[2];
681 buff[0]=cast(char,va_arg(argp,int));
682 buff[1]='\0';
683 pushstr(L,buff);
684 break;
685 }
686 case'd':{
687 setnvalue(L->top,cast_num(va_arg(argp,int)));
688 incr_top(L);
689 break;
690 }
691 case'f':{
693 incr_top(L);
694 break;
695 }
696 case'p':{
697 char buff[4*sizeof(void*)+8];
698 sprintf(buff,"%p",va_arg(argp,void*));
699 pushstr(L,buff);
700 break;
701 }
702 case'%':{
703 pushstr(L,"%");
704 break;
705 }
706 default:{
707 char buff[3];
708 buff[0]='%';
709 buff[1]=*(e+1);
710 buff[2]='\0';
711 pushstr(L,buff);
712 break;
713 }
714 }
715 n+=2;
716 fmt=e+2;
717 }
718 pushstr(L,fmt);
719 luaV_concat(L,n+1,cast_int(L->top-L->base)-1);
720 L->top-=n;
721 return svalue(L->top-1);
722 }
723 static const char*luaO_pushfstring(lua_State*L,const char*fmt,...){
724 const char*msg;
725 va_list argp;
726 va_start(argp,fmt);
728 va_end(argp);
729 return msg;
730 }
731 static void luaO_chunkid(char*out,const char*source,size_t bufflen){
732 if(*source=='='){
734 out[bufflen-1]='\0';
735 }
736 else{
737 if(*source=='@'){
738 size_t l;
739 source++;
740 bufflen-=sizeof(" '...' ");
741 l=strlen(source);
742 strcpy(out,"");
743 if(l>bufflen){
744 source+=(l-bufflen);
745 strcat(out,"...");
746 }
747 strcat(out,source);
748 }
749 else{
750 size_t len=strcspn(source,"\n\r");
751 bufflen-=sizeof(" [string \"...\"] ");
753 strcpy(out,"[string \"");
754 if(source[len]!='\0'){
755 strncat(out,source,len);
756 strcat(out,"...");
757 }
758 else
759 strcat(out,source);
760 strcat(out,"\"]");
761 }
762 }
763 }
764 #define gnode(t,i)(&(t)->node[i])
765 #define gkey(n)(&(n)->i_key.nk)
766 #define gval(n)(&(n)->i_val)
767 #define gnext(n)((n)->i_key.nk.next)
768 #define key2tval(n)(&(n)->i_key.tvk)
769 static TValue*luaH_setnum(lua_State*L,Table*t,int key);
770 static const TValue*luaH_getstr(Table*t,TString*key);
771 static TValue*luaH_set(lua_State*L,Table*t,const TValue*key);
772 static const char*const luaT_typenames[]={
773 "nil","boolean","userdata","number",
774 "string","table","function","userdata","thread",
775 "proto","upval"
776 };
777 static void luaT_init(lua_State*L){
778 static const char*const luaT_eventname[]={
779 "__index","__newindex",
780 "__gc","__mode","__eq",
781 "__add","__sub","__mul","__div","__mod",
782 "__pow","__unm","__len","__lt","__le",
783 "__concat","__call"
784 };
785 int i;
786 for(i=0;i<TM_N;i++){
787 G(L)->tmname[i]=luaS_new(L,luaT_eventname[i]);
788 luaS_fix(G(L)->tmname[i]);
789 }
790 }
791 static const TValue*luaT_gettm(Table*events,TMS event,TString*ename){
792 const TValue*tm=luaH_getstr(events,ename);
793 if(ttisnil(tm)){
794 events->flags|=cast_byte(1u<<event);
795 return NULL;
796 }
797 else return tm;
798 }
799 static const TValue*luaT_gettmbyobj(lua_State*L,const TValue*o,TMS event){
800 Table*mt;
801 switch(ttype(o)){
802 case 5:
803 mt=hvalue(o)->metatable;
804 break;
805 case 7:
806 mt=uvalue(o)->metatable;
807 break;
808 default:
809 mt=G(L)->mt[ttype(o)];
810 }
811 return(mt?luaH_getstr(mt,G(L)->tmname[event]):(&luaO_nilobject_));
812 }
813 #define sizeCclosure(n)(cast(int,sizeof(CClosure))+cast(int,sizeof(TValue)*((n)-1)))
814 #define sizeLclosure(n)(cast(int,sizeof(LClosure))+cast(int,sizeof(TValue*)*((n)-1)))
817 luaC_link(L,obj2gco(c),6);
818 c->c.isC=1;
819 c->c.env=e;
820 c->c.nupvalues=cast_byte(nelems);
821 return c;
822 }
825 luaC_link(L,obj2gco(c),6);
826 c->l.isC=0;
827 c->l.env=e;
828 c->l.nupvalues=cast_byte(nelems);
829 while(nelems--)c->l.upvals[nelems]=NULL;
830 return c;
831 }
833 UpVal*uv=luaM_new(L,UpVal);
834 luaC_link(L,obj2gco(uv),(8+2));
835 uv->v=&uv->u.value;
836 setnilvalue(uv->v);
837 return uv;
838 }
840 global_State*g=G(L);
841 GCObject**pp=&L->openupval;
842 UpVal*p;
843 UpVal*uv;
844 while(*pp!=NULL&&(p=ngcotouv(*pp))->v>=level){
845 if(p->v==level){
846 if(isdead(g,obj2gco(p)))
848 return p;
849 }
850 pp=&p->next;
851 }
852 uv=luaM_new(L,UpVal);
853 uv->tt=(8+2);
854 uv->marked=luaC_white(g);
855 uv->v=level;
856 uv->next=*pp;
857 *pp=obj2gco(uv);
858 uv->u.l.prev=&g->uvhead;
859 uv->u.l.next=g->uvhead.u.l.next;
860 uv->u.l.next->u.l.prev=uv;
861 g->uvhead.u.l.next=uv;
862 return uv;
863 }
864 static void unlinkupval(UpVal*uv){
865 uv->u.l.next->u.l.prev=uv->u.l.prev;
866 uv->u.l.prev->u.l.next=uv->u.l.next;
867 }
868 static void luaF_freeupval(lua_State*L,UpVal*uv){
869 if(uv->v!=&uv->u.value)
870 unlinkupval(uv);
871 luaM_free(L,uv);
872 }
874 UpVal*uv;
875 global_State*g=G(L);
876 while(L->openupval!=NULL&&(uv=ngcotouv(L->openupval))->v>=level){
877 GCObject*o=obj2gco(uv);
878 L->openupval=uv->next;
879 if(isdead(g,o))
880 luaF_freeupval(L,uv);
881 else{
882 unlinkupval(uv);
883 setobj(L,&uv->u.value,uv->v);
884 uv->v=&uv->u.value;
885 luaC_linkupval(L,uv);
886 }
887 }
888 }
891 luaC_link(L,obj2gco(f),(8+1));
892 f->k=NULL;
893 f->sizek=0;
894 f->p=NULL;
895 f->sizep=0;
896 f->code=NULL;
897 f->sizecode=0;
898 f->sizelineinfo=0;
899 f->sizeupvalues=0;
900 f->nups=0;
901 f->upvalues=NULL;
902 f->numparams=0;
903 f->is_vararg=0;
904 f->maxstacksize=0;
905 f->lineinfo=NULL;
906 f->sizelocvars=0;
907 f->locvars=NULL;
908 f->linedefined=0;
909 f->lastlinedefined=0;
910 f->source=NULL;
911 return f;
912 }
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);
921 }
923 int size=(c->c.isC)?sizeCclosure(c->c.nupvalues):
924 sizeLclosure(c->l.nupvalues);
926 }
927 #define MASK1(n,p)((~((~(Instruction)0)<<n))<<p)
928 #define MASK0(n,p)(~MASK1(n,p))
929 #define GET_OPCODE(i)(cast(OpCode,((i)>>0)&MASK1(6,0)))
930 #define SET_OPCODE(i,o)((i)=(((i)&MASK0(6,0))|((cast(Instruction,o)<<0)&MASK1(6,0))))
931 #define GETARG_A(i)(cast(int,((i)>>(0+6))&MASK1(8,0)))
932 #define SETARG_A(i,u)((i)=(((i)&MASK0(8,(0+6)))|((cast(Instruction,u)<<(0+6))&MASK1(8,(0+6)))))
933 #define GETARG_B(i)(cast(int,((i)>>(((0+6)+8)+9))&MASK1(9,0)))
934 #define SETARG_B(i,b)((i)=(((i)&MASK0(9,(((0+6)+8)+9)))|((cast(Instruction,b)<<(((0+6)+8)+9))&MASK1(9,(((0+6)+8)+9)))))
935 #define GETARG_C(i)(cast(int,((i)>>((0+6)+8))&MASK1(9,0)))
936 #define SETARG_C(i,b)((i)=(((i)&MASK0(9,((0+6)+8)))|((cast(Instruction,b)<<((0+6)+8))&MASK1(9,((0+6)+8)))))
937 #define GETARG_Bx(i)(cast(int,((i)>>((0+6)+8))&MASK1((9+9),0)))
938 #define SETARG_Bx(i,b)((i)=(((i)&MASK0((9+9),((0+6)+8)))|((cast(Instruction,b)<<((0+6)+8))&MASK1((9+9),((0+6)+8)))))
939 #define GETARG_sBx(i)(GETARG_Bx(i)-(((1<<(9+9))-1)>>1))
940 #define SETARG_sBx(i,b)SETARG_Bx((i),cast(unsigned int,(b)+(((1<<(9+9))-1)>>1)))
941 #define CREATE_ABC(o,a,b,c)((cast(Instruction,o)<<0)|(cast(Instruction,a)<<(0+6))|(cast(Instruction,b)<<(((0+6)+8)+9))|(cast(Instruction,c)<<((0+6)+8)))
942 #define CREATE_ABx(o,a,bc)((cast(Instruction,o)<<0)|(cast(Instruction,a)<<(0+6))|(cast(Instruction,bc)<<((0+6)+8)))
943 #define ISK(x)((x)&(1<<(9-1)))
944 #define INDEXK(r)((int)(r)&~(1<<(9-1)))
945 #define RKASK(x)((x)|(1<<(9-1)))
946 static const lu_byte luaP_opmodes[(cast(int,OP_VARARG)+1)];
947 #define getBMode(m)(cast(enum OpArgMask,(luaP_opmodes[m]>>4)&3))
948 #define getCMode(m)(cast(enum OpArgMask,(luaP_opmodes[m]>>2)&3))
949 #define testTMode(m)(luaP_opmodes[m]&(1<<7))
950 typedef struct expdesc{
951 expkind k;
952 union{
953 struct{int info,aux;}s;
955 }u;
956 int t;
957 int f;
959 typedef struct upvaldesc{
963 struct BlockCnt;
964 typedef struct FuncState{
965 Proto*f;
967 struct FuncState*prev;
968 struct LexState*ls;
969 struct lua_State*L;
970 struct BlockCnt*bl;
971 int pc;
972 int lasttarget;
973 int jpc;
975 int nk;
976 int np;
977 short nlocvars;
980 unsigned short actvar[200];
983 const char*name);
984 struct lua_longjmp{
985 struct lua_longjmp*previous;
986 jmp_buf b;
987 volatile int status;
988 };
989 static void luaD_seterrorobj(lua_State*L,int errcode,StkId oldtop){
990 switch(errcode){
991 case 4:{
992 setsvalue(L,oldtop,luaS_newliteral(L,"not enough memory"));
993 break;
994 }
995 case 5:{
996 setsvalue(L,oldtop,luaS_newliteral(L,"error in error handling"));
997 break;
998 }
999 case 3:
1000 case 2:{
1001 setobj(L,oldtop,L->top-1);
1002 break;
1003 }
1004 }
1005 L->top=oldtop+1;
1006 }
1008 if(L->size_ci>20000){
1009 int inuse=cast_int(L->ci-L->base_ci);
1010 if(inuse+1<20000)
1011 luaD_reallocCI(L,20000);
1012 }
1013 }
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;
1022 L->errfunc=0;
1023 L->errorJmp=NULL;
1024 }
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);
1029 }
1030 else{
1031 L->status=cast_byte(errcode);
1032 if(G(L)->panic){
1033 resetstack(L,errcode);
1034 G(L)->panic(L);
1035 }
1037 }
1038 }
1040 struct lua_longjmp lj;
1041 lj.status=0;
1042 lj.previous=L->errorJmp;
1043 L->errorJmp=&lj;
1044 LUAI_TRY(L,&lj,
1045 (*f)(L,ud);
1046 );
1047 L->errorJmp=lj.previous;
1048 return lj.status;
1049 }
1050 static void correctstack(lua_State*L,TValue*oldstack){
1051 CallInfo*ci;
1052 GCObject*up;
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;
1060 }
1061 L->base=(L->base-oldstack)+L->stack;
1062 }
1063 static void luaD_reallocstack(lua_State*L,int newsize){
1064 TValue*oldstack=L->stack;
1065 int realsize=newsize+1+5;
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);
1070 }
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;
1077 }
1078 static void luaD_growstack(lua_State*L,int n){
1079 if(n<=L->stacksize)
1080 luaD_reallocstack(L,2*L->stacksize);
1081 else
1082 luaD_reallocstack(L,L->stacksize+n);
1083 }
1085 if(L->size_ci>20000)
1086 luaD_throw(L,5);
1087 else{
1088 luaD_reallocCI(L,2*L->size_ci);
1089 if(L->size_ci>20000)
1090 luaG_runerror(L,"stack overflow");
1091 }
1092 return++L->ci;
1093 }
1095 int i;
1096 int nfixargs=p->numparams;
1097 Table*htab=NULL;
1098 StkId base,fixed;
1099 for(;actual<nfixargs;++actual)
1100 setnilvalue(L->top++);
1101 fixed=L->top-actual;
1102 base=L->top;
1103 for(i=0;i<nfixargs;i++){
1104 setobj(L,L->top++,fixed+i);
1106 }
1107 if(htab){
1108 sethvalue(L,L->top++,htab);
1109 }
1110 return base;
1111 }
1114 StkId p;
1115 ptrdiff_t funcr=savestack(L,func);
1116 if(!ttisfunction(tm))
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);
1122 return func;
1123 }
1124 #define inc_ci(L)((L->ci==L->end_ci)?growCI(L):(condhardstacktests(luaD_reallocCI(L,L->size_ci)),++L->ci))
1125 static int luaD_precall(lua_State*L,StkId func,int nresults){
1126 LClosure*cl;
1127 ptrdiff_t funcr;
1128 if(!ttisfunction(func))
1129 func=tryfuncTM(L,func);
1130 funcr=savestack(L,func);
1131 cl=&clvalue(func)->l;
1132 L->ci->savedpc=L->savedpc;
1133 if(!cl->isC){
1134 CallInfo*ci;
1135 StkId st,base;
1136 Proto*p=cl->p;
1137 luaD_checkstack(L,p->maxstacksize);
1138 func=restorestack(L,funcr);
1139 if(!p->is_vararg){
1140 base=func+1;
1141 if(L->top>base+p->numparams)
1142 L->top=base+p->numparams;
1143 }
1144 else{
1145 int nargs=cast_int(L->top-func)-1;
1147 func=restorestack(L,funcr);
1148 }
1149 ci=inc_ci(L);
1150 ci->func=func;
1151 L->base=ci->base=base;
1152 ci->top=L->base+p->maxstacksize;
1153 L->savedpc=p->code;
1154 ci->tailcalls=0;
1155 ci->nresults=nresults;
1156 for(st=L->top;st<ci->top;st++)
1157 setnilvalue(st);
1158 L->top=ci->top;
1159 return 0;
1160 }
1161 else{
1162 CallInfo*ci;
1163 int n;
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;
1169 ci->nresults=nresults;
1170 n=(*curr_func(L)->c.f)(L);
1171 if(n<0)
1172 return 2;
1173 else{
1174 luaD_poscall(L,L->top-n);
1175 return 1;
1176 }
1177 }
1178 }
1179 static int luaD_poscall(lua_State*L,StkId firstResult){
1180 StkId res;
1181 int wanted,i;
1182 CallInfo*ci;
1183 ci=L->ci--;
1184 res=ci->func;
1185 wanted=ci->nresults;
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++);
1190 while(i-->0)
1191 setnilvalue(res++);
1192 L->top=res;
1193 return(wanted-(-1));
1194 }
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);
1201 }
1202 if(luaD_precall(L,func,nResults)==0)
1203 luaV_execute(L,1);
1204 L->nCcalls--;
1205 luaC_checkGC(L);
1206 }
1207 static int luaD_pcall(lua_State*L,Pfunc func,void*u,
1208 ptrdiff_t old_top,ptrdiff_t ef){
1209 int status;
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;
1216 if(status!=0){
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;
1226 }
1227 L->errfunc=old_errfunc;
1228 return status;
1229 }
1230 struct SParser{
1231 ZIO*z;
1232 Mbuffer buff;
1233 const char*name;
1234 };
1235 static void f_parser(lua_State*L,void*ud){
1236 int i;
1237 Proto*tf;
1238 Closure*cl;
1239 struct SParser*p=cast(struct SParser*,ud);
1240 luaC_checkGC(L);
1241 tf=luaY_parser(L,p->z,
1242 &p->buff,p->name);
1243 cl=luaF_newLclosure(L,tf->nups,hvalue(gt(L)));
1244 cl->l.p=tf;
1245 for(i=0;i<tf->nups;i++)
1246 cl->l.upvals[i]=luaF_newupval(L);
1247 setclvalue(L,L->top,cl);
1248 incr_top(L);
1249 }
1250 static int luaD_protectedparser(lua_State*L,ZIO*z,const char*name){
1251 struct SParser p;
1252 int status;
1253 p.z=z;p.name=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);
1257 return status;
1258 }
1259 static void luaS_resize(lua_State*L,int newsize){
1260 GCObject**newhash;
1261 stringtable*tb;
1262 int i;
1263 if(G(L)->gcstate==2)
1264 return;
1265 newhash=luaM_newvector(L,newsize,GCObject*);
1266 tb=&G(L)->strt;
1267 for(i=0;i<newsize;i++)newhash[i]=NULL;
1268 for(i=0;i<tb->size;i++){
1269 GCObject*p=tb->hash[i];
1270 while(p){
1271 GCObject*next=p->gch.next;
1272 unsigned int h=gco2ts(p)->hash;
1273 int h1=lmod(h,newsize);
1274 p->gch.next=newhash[h1];
1275 newhash[h1]=p;
1276 p=next;
1277 }
1278 }
1279 luaM_freearray(L,tb->hash,tb->size,TString*);
1280 tb->size=newsize;
1281 tb->hash=newhash;
1282 }
1283 static TString*newlstr(lua_State*L,const char*str,size_t l,
1284 unsigned int h){
1285 TString*ts;
1286 stringtable*tb;
1287 if(l+1>(((size_t)(~(size_t)0)-2)-sizeof(TString))/sizeof(char))
1288 luaM_toobig(L);
1289 ts=cast(TString*,luaM_malloc(L,(l+1)*sizeof(char)+sizeof(TString)));
1290 ts->tsv.len=l;
1291 ts->tsv.hash=h;
1292 ts->tsv.marked=luaC_white(G(L));
1293 ts->tsv.tt=4;
1294 ts->tsv.reserved=0;
1295 memcpy(ts+1,str,l*sizeof(char));
1296 ((char*)(ts+1))[l]='\0';
1297 tb=&G(L)->strt;
1298 h=lmod(h,tb->size);
1299 ts->tsv.next=tb->hash[h];
1300 tb->hash[h]=obj2gco(ts);
1301 tb->nuse++;
1302 if(tb->nuse>cast(lu_int32,tb->size)&&tb->size<=(INT_MAX-2)/2)
1303 luaS_resize(L,tb->size*2);
1304 return ts;
1305 }
1306 static TString*luaS_newlstr(lua_State*L,const char*str,size_t l){
1307 GCObject*o;
1308 unsigned int h=cast(unsigned int,l);
1309 size_t step=(l>>5)+1;
1310 size_t l1;
1311 for(l1=l;l1>=step;l1-=step)
1312 h=h^((h<<5)+(h>>2)+cast(unsigned char,str[l1-1]));
1313 for(o=G(L)->strt.hash[lmod(h,G(L)->strt.size)];
1314 o!=NULL;
1315 o=o->gch.next){
1316 TString*ts=rawgco2ts(o);
1317 if(ts->tsv.len==l&&(memcmp(str,getstr(ts),l)==0)){
1318 if(isdead(G(L),o))changewhite(o);
1319 return ts;
1320 }
1321 }
1322 return newlstr(L,str,l,h);
1323 }
1325 Udata*u;
1326 if(s>((size_t)(~(size_t)0)-2)-sizeof(Udata))
1327 luaM_toobig(L);
1328 u=cast(Udata*,luaM_malloc(L,s+sizeof(Udata)));
1329 u->uv.marked=luaC_white(G(L));
1330 u->uv.tt=7;
1331 u->uv.len=s;
1332 u->uv.metatable=NULL;
1333 u->uv.env=e;
1334 u->uv.next=G(L)->mainthread->next;
1335 G(L)->mainthread->next=obj2gco(u);
1336 return u;
1337 }
1338 #define hashpow2(t,n)(gnode(t,lmod((n),sizenode(t))))
1339 #define hashstr(t,str)hashpow2(t,(str)->tsv.hash)
1340 #define hashboolean(t,p)hashpow2(t,p)
1341 #define hashmod(t,n)(gnode(t,((n)%((sizenode(t)-1)|1))))
1342 #define hashpointer(t,p)hashmod(t,IntPoint(p))
1343 static const Node dummynode_={
1344 {{NULL},0},
1345 {{{NULL},0,NULL}}
1346 };
1347 static Node*hashnum(const Table*t,lua_Number n){
1348 unsigned int a[cast_int(sizeof(lua_Number)/sizeof(int))];
1349 int i;
1350 if(luai_numeq(n,0))
1351 return gnode(t,0);
1352 memcpy(a,&n,sizeof(a));
1353 for(i=1;i<cast_int(sizeof(lua_Number)/sizeof(int));i++)a[0]+=a[i];
1354 return hashmod(t,a[0]);
1355 }
1356 static Node*mainposition(const Table*t,const TValue*key){
1357 switch(ttype(key)){
1358 case 3:
1359 return hashnum(t,nvalue(key));
1360 case 4:
1361 return hashstr(t,rawtsvalue(key));
1362 case 1:
1363 return hashboolean(t,bvalue(key));
1364 case 2:
1365 return hashpointer(t,pvalue(key));
1366 default:
1367 return hashpointer(t,gcvalue(key));
1368 }
1369 }
1370 static int arrayindex(const TValue*key){
1371 if(ttisnumber(key)){
1373 int k;
1374 lua_number2int(k,n);
1375 if(luai_numeq(cast_num(k),n))
1376 return k;
1377 }
1378 return-1;
1379 }
1381 int i;
1382 if(ttisnil(key))return-1;
1383 i=arrayindex(key);
1384 if(0<i&&i<=t->sizearray)
1385 return i-1;
1386 else{
1388 do{
1390 (ttype(gkey(n))==(8+3)&&iscollectable(key)&&
1391 gcvalue(gkey(n))==gcvalue(key))){
1392 i=cast_int(n-gnode(t,0));
1393 return i+t->sizearray;
1394 }
1395 else n=gnext(n);
1396 }while(n);
1397 luaG_runerror(L,"invalid key to "LUA_QL("next"));
1398 return 0;
1399 }
1400 }
1402 int i=findindex(L,t,key);
1403 for(i++;i<t->sizearray;i++){
1404 if(!ttisnil(&t->array[i])){
1405 setnvalue(key,cast_num(i+1));
1406 setobj(L,key+1,&t->array[i]);
1407 return 1;
1408 }
1409 }
1410 for(i-=t->sizearray;i<(int)sizenode(t);i++){
1411 if(!ttisnil(gval(gnode(t,i)))){
1412 setobj(L,key,key2tval(gnode(t,i)));
1413 setobj(L,key+1,gval(gnode(t,i)));
1414 return 1;
1415 }
1416 }
1417 return 0;
1418 }
1419 static int computesizes(int nums[],int*narray){
1420 int i;
1421 int twotoi;
1422 int a=0;
1423 int na=0;
1424 int n=0;
1425 for(i=0,twotoi=1;twotoi/2<*narray;i++,twotoi*=2){
1426 if(nums[i]>0){
1427 a+=nums[i];
1428 if(a>twotoi/2){
1429 n=twotoi;
1430 na=a;
1431 }
1432 }
1433 if(a==*narray)break;
1434 }
1435 *narray=n;
1436 return na;
1437 }
1438 static int countint(const TValue*key,int*nums){
1439 int k=arrayindex(key);
1440 if(0<k&&k<=(1<<(32-2))){
1441 nums[ceillog2(k)]++;
1442 return 1;
1443 }
1444 else
1445 return 0;
1446 }
1447 static int numusearray(const Table*t,int*nums){
1448 int lg;
1449 int ttlg;
1450 int ause=0;
1451 int i=1;
1452 for(lg=0,ttlg=1;lg<=(32-2);lg++,ttlg*=2){
1453 int lc=0;
1454 int lim=ttlg;
1455 if(lim>t->sizearray){
1456 lim=t->sizearray;
1457 if(i>lim)
1458 break;
1459 }
1460 for(;i<=lim;i++){
1461 if(!ttisnil(&t->array[i-1]))
1462 lc++;
1463 }
1464 nums[lg]+=lc;
1465 ause+=lc;
1466 }
1467 return ause;
1468 }
1469 static int numusehash(const Table*t,int*nums,int*pnasize){
1470 int totaluse=0;
1471 int ause=0;
1472 int i=sizenode(t);
1473 while(i--){
1474 Node*n=&t->node[i];
1475 if(!ttisnil(gval(n))){
1476 ause+=countint(key2tval(n),nums);
1477 totaluse++;
1478 }
1479 }
1480 *pnasize+=ause;
1481 return totaluse;
1482 }
1484 int i;
1485 luaM_reallocvector(L,t->array,t->sizearray,size,TValue);
1486 for(i=t->sizearray;i<size;i++)
1487 setnilvalue(&t->array[i]);
1488 t->sizearray=size;
1489 }
1491 int lsize;
1492 if(size==0){
1493 t->node=cast(Node*,(&dummynode_));
1494 lsize=0;
1495 }
1496 else{
1497 int i;
1498 lsize=ceillog2(size);
1499 if(lsize>(32-2))
1500 luaG_runerror(L,"table overflow");
1501 size=twoto(lsize);
1502 t->node=luaM_newvector(L,size,Node);
1503 for(i=0;i<size;i++){
1504 Node*n=gnode(t,i);
1505 gnext(n)=NULL;
1506 setnilvalue(gkey(n));
1507 setnilvalue(gval(n));
1508 }
1509 }
1510 t->lsizenode=cast_byte(lsize);
1511 t->lastfree=gnode(t,size);
1512 }
1513 static void resize(lua_State*L,Table*t,int nasize,int nhsize){
1514 int i;
1515 int oldasize=t->sizearray;
1516 int oldhsize=t->lsizenode;
1517 Node*nold=t->node;
1518 if(nasize>oldasize)
1519 setarrayvector(L,t,nasize);
1520 setnodevector(L,t,nhsize);
1521 if(nasize<oldasize){
1522 t->sizearray=nasize;
1523 for(i=nasize;i<oldasize;i++){
1524 if(!ttisnil(&t->array[i]))
1525 setobj(L,luaH_setnum(L,t,i+1),&t->array[i]);
1526 }
1527 luaM_reallocvector(L,t->array,oldasize,nasize,TValue);
1528 }
1529 for(i=twoto(oldhsize)-1;i>=0;i--){
1530 Node*old=nold+i;
1531 if(!ttisnil(gval(old)))
1532 setobj(L,luaH_set(L,t,key2tval(old)),gval(old));
1533 }
1534 if(nold!=(&dummynode_))
1535 luaM_freearray(L,nold,twoto(oldhsize),Node);
1536 }
1537 static void luaH_resizearray(lua_State*L,Table*t,int nasize){
1538 int nsize=(t->node==(&dummynode_))?0:sizenode(t);
1539 resize(L,t,nasize,nsize);
1540 }
1541 static void rehash(lua_State*L,Table*t,const TValue*ek){
1542 int nasize,na;
1543 int nums[(32-2)+1];
1544 int i;
1545 int totaluse;
1546 for(i=0;i<=(32-2);i++)nums[i]=0;
1547 nasize=numusearray(t,nums);
1548 totaluse=nasize;
1549 totaluse+=numusehash(t,nums,&nasize);
1550 nasize+=countint(ek,nums);
1551 totaluse++;
1552 na=computesizes(nums,&nasize);
1553 resize(L,t,nasize,totaluse-na);
1554 }
1555 static Table*luaH_new(lua_State*L,int narray,int nhash){
1557 luaC_link(L,obj2gco(t),5);
1558 t->metatable=NULL;
1559 t->flags=cast_byte(~0);
1560 t->array=NULL;
1561 t->sizearray=0;
1562 t->lsizenode=0;
1563 t->node=cast(Node*,(&dummynode_));
1564 setarrayvector(L,t,narray);
1565 setnodevector(L,t,nhash);
1566 return t;
1567 }
1568 static void luaH_free(lua_State*L,Table*t){
1569 if(t->node!=(&dummynode_))
1570 luaM_freearray(L,t->node,sizenode(t),Node);
1571 luaM_freearray(L,t->array,t->sizearray,TValue);
1572 luaM_free(L,t);
1573 }
1575 while(t->lastfree-->t->node){
1576 if(ttisnil(gkey(t->lastfree)))
1577 return t->lastfree;
1578 }
1579 return NULL;
1580 }
1582 Node*mp=mainposition(t,key);
1583 if(!ttisnil(gval(mp))||mp==(&dummynode_)){
1584 Node*othern;
1585 Node*n=getfreepos(t);
1586 if(n==NULL){
1587 rehash(L,t,key);
1588 return luaH_set(L,t,key);
1589 }
1590 othern=mainposition(t,key2tval(mp));
1591 if(othern!=mp){
1592 while(gnext(othern)!=mp)othern=gnext(othern);
1593 gnext(othern)=n;
1594 *n=*mp;
1595 gnext(mp)=NULL;
1596 setnilvalue(gval(mp));
1597 }
1598 else{
1599 gnext(n)=gnext(mp);
1600 gnext(mp)=n;
1601 mp=n;
1602 }
1603 }
1604 gkey(mp)->value=key->value;gkey(mp)->tt=key->tt;
1605 luaC_barriert(L,t,key);
1606 return gval(mp);
1607 }
1608 static const TValue*luaH_getnum(Table*t,int key){
1609 if(cast(unsigned int,key)-1<cast(unsigned int,t->sizearray))
1610 return&t->array[key-1];
1611 else{
1613 Node*n=hashnum(t,nk);
1614 do{
1616 return gval(n);
1617 else n=gnext(n);
1618 }while(n);
1619 return(&luaO_nilobject_);
1620 }
1621 }
1623 Node*n=hashstr(t,key);
1624 do{
1625 if(ttisstring(gkey(n))&&rawtsvalue(gkey(n))==key)
1626 return gval(n);
1627 else n=gnext(n);
1628 }while(n);
1629 return(&luaO_nilobject_);
1630 }
1631 static const TValue*luaH_get(Table*t,const TValue*key){
1632 switch(ttype(key)){
1633 case 0:return(&luaO_nilobject_);
1634 case 4:return luaH_getstr(t,rawtsvalue(key));
1635 case 3:{
1636 int k;
1638 lua_number2int(k,n);
1640 return luaH_getnum(t,k);
1641 }
1642 default:{
1644 do{
1646 return gval(n);
1647 else n=gnext(n);
1648 }while(n);
1649 return(&luaO_nilobject_);
1650 }
1651 }
1652 }
1654 const TValue*p=luaH_get(t,key);
1655 t->flags=0;
1656 if(p!=(&luaO_nilobject_))
1657 return cast(TValue*,p);
1658 else{
1659 if(ttisnil(key))luaG_runerror(L,"table index is nil");
1660 else if(ttisnumber(key)&&luai_numisnan(nvalue(key)))
1661 luaG_runerror(L,"table index is NaN");
1662 return newkey(L,t,key);
1663 }
1664 }
1666 const TValue*p=luaH_getnum(t,key);
1667 if(p!=(&luaO_nilobject_))
1668 return cast(TValue*,p);
1669 else{
1670 TValue k;
1671 setnvalue(&k,cast_num(key));
1672 return newkey(L,t,&k);
1673 }
1674 }
1676 const TValue*p=luaH_getstr(t,key);
1677 if(p!=(&luaO_nilobject_))
1678 return cast(TValue*,p);
1679 else{
1680 TValue k;
1681 setsvalue(L,&k,key);
1682 return newkey(L,t,&k);
1683 }
1684 }
1685 static int unbound_search(Table*t,unsigned int j){
1686 unsigned int i=j;
1687 j++;
1688 while(!ttisnil(luaH_getnum(t,j))){
1689 i=j;
1690 j*=2;
1691 if(j>cast(unsigned int,(INT_MAX-2))){
1692 i=1;
1693 while(!ttisnil(luaH_getnum(t,i)))i++;
1694 return i-1;
1695 }
1696 }
1697 while(j-i>1){
1698 unsigned int m=(i+j)/2;
1699 if(ttisnil(luaH_getnum(t,m)))j=m;
1700 else i=m;
1701 }
1702 return i;
1703 }
1704 static int luaH_getn(Table*t){
1705 unsigned int j=t->sizearray;
1706 if(j>0&&ttisnil(&t->array[j-1])){
1707 unsigned int i=0;
1708 while(j-i>1){
1709 unsigned int m=(i+j)/2;
1710 if(ttisnil(&t->array[m-1]))j=m;
1711 else i=m;
1712 }
1713 return i;
1714 }
1715 else if(t->node==(&dummynode_))
1716 return j;
1717 else return unbound_search(t,j);
1718 }
1719 #define makewhite(g,x)((x)->gch.marked=cast_byte(((x)->gch.marked&cast_byte(~(bitmask(2)|bit2mask(0,1))))|luaC_white(g)))
1720 #define white2gray(x)reset2bits((x)->gch.marked,0,1)
1721 #define black2gray(x)resetbit((x)->gch.marked,2)
1722 #define stringmark(s)reset2bits((s)->tsv.marked,0,1)
1723 #define isfinalized(u)testbit((u)->marked,3)
1724 #define markfinalized(u)l_setbit((u)->marked,3)
1725 #define markvalue(g,o){checkconsistency(o);if(iscollectable(o)&&iswhite(gcvalue(o)))reallymarkobject(g,gcvalue(o));}
1726 #define markobject(g,t){if(iswhite(obj2gco(t)))reallymarkobject(g,obj2gco(t));}
1727 #define setthreshold(g)(g->GCthreshold=(g->estimate/100)*g->gcpause)
1728 static void removeentry(Node*n){
1729 if(iscollectable(gkey(n)))
1730 setttype(gkey(n),(8+3));
1731 }
1733 white2gray(o);
1734 switch(o->gch.tt){
1735 case 4:{
1736 return;
1737 }
1738 case 7:{
1739 Table*mt=gco2u(o)->metatable;
1740 gray2black(o);
1741 if(mt)markobject(g,mt);
1742 markobject(g,gco2u(o)->env);
1743 return;
1744 }
1745 case(8+2):{
1746 UpVal*uv=gco2uv(o);
1747 markvalue(g,uv->v);
1748 if(uv->v==&uv->u.value)
1749 gray2black(o);
1750 return;
1751 }
1752 case 6:{
1753 gco2cl(o)->c.gclist=g->gray;
1754 g->gray=o;
1755 break;
1756 }
1757 case 5:{
1758 gco2h(o)->gclist=g->gray;
1759 g->gray=o;
1760 break;
1761 }
1762 case 8:{
1763 gco2th(o)->gclist=g->gray;
1764 g->gray=o;
1765 break;
1766 }
1767 case(8+1):{
1768 gco2p(o)->gclist=g->gray;
1769 g->gray=o;
1770 break;
1771 }
1772 default:;
1773 }
1774 }
1775 static void marktmu(global_State*g){
1776 GCObject*u=g->tmudata;
1777 if(u){
1778 do{
1779 u=u->gch.next;
1780 makewhite(g,u);
1782 }while(u!=g->tmudata);
1783 }
1784 }
1785 static size_t luaC_separateudata(lua_State*L,int all){
1786 global_State*g=G(L);
1787 size_t deadmem=0;
1788 GCObject**p=&g->mainthread->next;
1789 GCObject*curr;
1790 while((curr=*p)!=NULL){
1791 if(!(iswhite(curr)||all)||isfinalized(gco2u(curr)))
1792 p=&curr->gch.next;
1793 else if(fasttm(L,gco2u(curr)->metatable,TM_GC)==NULL){
1795 p=&curr->gch.next;
1796 }
1797 else{
1798 deadmem+=sizeudata(gco2u(curr));
1800 *p=curr->gch.next;
1801 if(g->tmudata==NULL)
1802 g->tmudata=curr->gch.next=curr;
1803 else{
1804 curr->gch.next=g->tmudata->gch.next;
1805 g->tmudata->gch.next=curr;
1806 g->tmudata=curr;
1807 }
1808 }
1809 }
1810 return deadmem;
1811 }
1813 int i;
1814 int weakkey=0;
1815 int weakvalue=0;
1816 const TValue*mode;
1817 if(h->metatable)
1818 markobject(g,h->metatable);
1819 mode=gfasttm(g,h->metatable,TM_MODE);
1820 if(mode&&ttisstring(mode)){
1821 weakkey=(strchr(svalue(mode),'k')!=NULL);
1822 weakvalue=(strchr(svalue(mode),'v')!=NULL);
1823 if(weakkey||weakvalue){
1824 h->marked&=~(bitmask(3)|bitmask(4));
1825 h->marked|=cast_byte((weakkey<<3)|
1826 (weakvalue<<4));
1827 h->gclist=g->weak;
1828 g->weak=obj2gco(h);
1829 }
1830 }
1831 if(weakkey&&weakvalue)return 1;
1832 if(!weakvalue){
1833 i=h->sizearray;
1834 while(i--)
1835 markvalue(g,&h->array[i]);
1836 }
1837 i=sizenode(h);
1838 while(i--){
1839 Node*n=gnode(h,i);
1840 if(ttisnil(gval(n)))
1841 removeentry(n);
1842 else{
1843 if(!weakkey)markvalue(g,gkey(n));
1844 if(!weakvalue)markvalue(g,gval(n));
1845 }
1846 }
1847 return weakkey||weakvalue;
1848 }
1850 int i;
1851 if(f->source)stringmark(f->source);
1852 for(i=0;i<f->sizek;i++)
1853 markvalue(g,&f->k[i]);
1854 for(i=0;i<f->sizeupvalues;i++){
1855 if(f->upvalues[i])
1856 stringmark(f->upvalues[i]);
1857 }
1858 for(i=0;i<f->sizep;i++){
1859 if(f->p[i])
1860 markobject(g,f->p[i]);
1861 }
1862 for(i=0;i<f->sizelocvars;i++){
1863 if(f->locvars[i].varname)
1864 stringmark(f->locvars[i].varname);
1865 }
1866 }
1868 markobject(g,cl->c.env);
1869 if(cl->c.isC){
1870 int i;
1871 for(i=0;i<cl->c.nupvalues;i++)
1872 markvalue(g,&cl->c.upvalue[i]);
1873 }
1874 else{
1875 int i;
1876 markobject(g,cl->l.p);
1877 for(i=0;i<cl->l.nupvalues;i++)
1878 markobject(g,cl->l.upvals[i]);
1879 }
1880 }
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)
1885 return;
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);
1893 }
1895 StkId o,lim;
1896 CallInfo*ci;
1897 markvalue(g,gt(l));
1898 lim=l->top;
1899 for(ci=l->base_ci;ci<=l->ci;ci++){
1900 if(lim<ci->top)lim=ci->top;
1901 }
1902 for(o=l->stack;o<l->top;o++)
1903 markvalue(g,o);
1904 for(;o<=lim;o++)
1905 setnilvalue(o);
1906 checkstacksizes(l,lim);
1907 }
1909 GCObject*o=g->gray;
1910 gray2black(o);
1911 switch(o->gch.tt){
1912 case 5:{
1913 Table*h=gco2h(o);
1914 g->gray=h->gclist;
1915 if(traversetable(g,h))
1916 black2gray(o);
1917 return sizeof(Table)+sizeof(TValue)*h->sizearray+
1918 sizeof(Node)*sizenode(h);
1919 }
1920 case 6:{
1921 Closure*cl=gco2cl(o);
1922 g->gray=cl->c.gclist;
1923 traverseclosure(g,cl);
1924 return(cl->c.isC)?sizeCclosure(cl->c.nupvalues):
1925 sizeLclosure(cl->l.nupvalues);
1926 }
1927 case 8:{
1928 lua_State*th=gco2th(o);
1929 g->gray=th->gclist;
1930 th->gclist=g->grayagain;
1931 g->grayagain=o;
1932 black2gray(o);
1933 traversestack(g,th);
1934 return sizeof(lua_State)+sizeof(TValue)*th->stacksize+
1935 sizeof(CallInfo)*th->size_ci;
1936 }
1937 case(8+1):{
1938 Proto*p=gco2p(o);
1939 g->gray=p->gclist;
1940 traverseproto(g,p);
1941 return sizeof(Proto)+sizeof(Instruction)*p->sizecode+
1942 sizeof(Proto*)*p->sizep+
1943 sizeof(TValue)*p->sizek+
1944 sizeof(int)*p->sizelineinfo+
1945 sizeof(LocVar)*p->sizelocvars+
1946 sizeof(TString*)*p->sizeupvalues;
1947 }
1948 default:return 0;
1949 }
1950 }
1951 static size_t propagateall(global_State*g){
1952 size_t m=0;
1953 while(g->gray)m+=propagatemark(g);
1954 return m;
1955 }
1956 static int iscleared(const TValue*o,int iskey){
1957 if(!iscollectable(o))return 0;
1958 if(ttisstring(o)){
1960 return 0;
1961 }
1962 return iswhite(gcvalue(o))||
1963 (ttisuserdata(o)&&(!iskey&&isfinalized(uvalue(o))));
1964 }
1965 static void cleartable(GCObject*l){
1966 while(l){
1967 Table*h=gco2h(l);
1968 int i=h->sizearray;
1969 if(testbit(h->marked,4)){
1970 while(i--){
1971 TValue*o=&h->array[i];
1972 if(iscleared(o,0))
1973 setnilvalue(o);
1974 }
1975 }
1976 i=sizenode(h);
1977 while(i--){
1978 Node*n=gnode(h,i);
1979 if(!ttisnil(gval(n))&&
1980 (iscleared(key2tval(n),1)||iscleared(gval(n),0))){
1981 setnilvalue(gval(n));
1982 removeentry(n);
1983 }
1984 }
1985 l=h->gclist;
1986 }
1987 }
1988 static void freeobj(lua_State*L,GCObject*o){
1989 switch(o->gch.tt){
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;
1994 case 8:{
1996 break;
1997 }
1998 case 4:{
1999 G(L)->strt.nuse--;
2001 break;
2002 }
2003 case 7:{
2004 luaM_freemem(L,o,sizeudata(gco2u(o)));
2005 break;
2006 }
2007 default:;
2008 }
2009 }
2010 #define sweepwholelist(L,p)sweeplist(L,p,((lu_mem)(~(lu_mem)0)-2))
2012 GCObject*curr;
2013 global_State*g=G(L);
2014 int deadmask=otherwhite(g);
2015 while((curr=*p)!=NULL&&count-->0){
2016 if(curr->gch.tt==8)
2017 sweepwholelist(L,&gco2th(curr)->openupval);
2018 if((curr->gch.marked^bit2mask(0,1))&deadmask){
2019 makewhite(g,curr);
2020 p=&curr->gch.next;
2021 }
2022 else{
2023 *p=curr->gch.next;
2024 if(curr==g->rootgc)
2025 g->rootgc=curr->gch.next;
2026 freeobj(L,curr);
2027 }
2028 }
2029 return p;
2030 }
2031 static void checkSizes(lua_State*L){
2032 global_State*g=G(L);
2033 if(g->strt.nuse<cast(lu_int32,g->strt.size/4)&&
2034 g->strt.size>32*2)
2035 luaS_resize(L,g->strt.size/2);
2036 if(luaZ_sizebuffer(&g->buff)>32*2){
2037 size_t newsize=luaZ_sizebuffer(&g->buff)/2;
2038 luaZ_resizebuffer(L,&g->buff,newsize);
2039 }
2040 }
2041 static void GCTM(lua_State*L){
2042 global_State*g=G(L);
2043 GCObject*o=g->tmudata->gch.next;
2044 Udata*udata=rawgco2u(o);
2045 const TValue*tm;
2046 if(o==g->tmudata)
2047 g->tmudata=NULL;
2048 else
2049 g->tmudata->gch.next=udata->uv.next;
2050 udata->uv.next=g->mainthread->next;
2051 g->mainthread->next=o;
2052 makewhite(g,o);
2053 tm=fasttm(L,udata->uv.metatable,TM_GC);
2054 if(tm!=NULL){
2055 lu_byte oldah=L->allowhook;
2056 lu_mem oldt=g->GCthreshold;
2057 L->allowhook=0;
2058 g->GCthreshold=2*g->totalbytes;
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;
2064 g->GCthreshold=oldt;
2065 }
2066 }
2068 while(G(L)->tmudata)
2069 GCTM(L);
2070 }
2071 static void luaC_freeall(lua_State*L){
2072 global_State*g=G(L);
2073 int i;
2074 g->currentwhite=bit2mask(0,1)|bitmask(6);
2075 sweepwholelist(L,&g->rootgc);
2076 for(i=0;i<g->strt.size;i++)
2077 sweepwholelist(L,&g->strt.hash[i]);
2078 }
2079 static void markmt(global_State*g){
2080 int i;
2081 for(i=0;i<(8+1);i++)
2082 if(g->mt[i])markobject(g,g->mt[i]);
2083 }
2084 static void markroot(lua_State*L){
2085 global_State*g=G(L);
2086 g->gray=NULL;
2087 g->grayagain=NULL;
2088 g->weak=NULL;
2089 markobject(g,g->mainthread);
2090 markvalue(g,gt(g->mainthread));
2091 markvalue(g,registry(L));
2092 markmt(g);
2093 g->gcstate=1;
2094 }
2096 UpVal*uv;
2097 for(uv=g->uvhead.u.l.next;uv!=&g->uvhead;uv=uv->u.l.next){
2098 if(isgray(obj2gco(uv)))
2099 markvalue(g,uv->v);
2100 }
2101 }
2102 static void atomic(lua_State*L){
2103 global_State*g=G(L);
2104 size_t udsize;
2105 remarkupvals(g);
2106 propagateall(g);
2107 g->gray=g->weak;
2108 g->weak=NULL;
2109 markobject(g,L);
2110 markmt(g);
2111 propagateall(g);
2112 g->gray=g->grayagain;
2113 g->grayagain=NULL;
2114 propagateall(g);
2115 udsize=luaC_separateudata(L,0);
2116 marktmu(g);
2117 udsize+=propagateall(g);
2118 cleartable(g->weak);
2119 g->currentwhite=cast_byte(otherwhite(g));
2120 g->sweepstrgc=0;
2121 g->sweepgc=&g->rootgc;
2122 g->gcstate=2;
2123 g->estimate=g->totalbytes-udsize;
2124 }
2126 global_State*g=G(L);
2127 switch(g->gcstate){
2128 case 0:{
2129 markroot(L);
2130 return 0;
2131 }
2132 case 1:{
2133 if(g->gray)
2134 return propagatemark(g);
2135 else{
2136 atomic(L);
2137 return 0;
2138 }
2139 }
2140 case 2:{
2141 lu_mem old=g->totalbytes;
2142 sweepwholelist(L,&g->strt.hash[g->sweepstrgc++]);
2143 if(g->sweepstrgc>=g->strt.size)
2144 g->gcstate=3;
2145 g->estimate-=old-g->totalbytes;
2146 return 10;
2147 }
2148 case 3:{
2149 lu_mem old=g->totalbytes;
2150 g->sweepgc=sweeplist(L,g->sweepgc,40);
2151 if(*g->sweepgc==NULL){
2152 checkSizes(L);
2153 g->gcstate=4;
2154 }
2155 g->estimate-=old-g->totalbytes;
2156 return 40*10;
2157 }
2158 case 4:{
2159 if(g->tmudata){
2160 GCTM(L);
2161 if(g->estimate>100)
2162 g->estimate-=100;
2163 return 100;
2164 }
2165 else{
2166 g->gcstate=0;
2167 g->gcdept=0;
2168 return 0;
2169 }
2170 }
2171 default:return 0;
2172 }
2173 }
2174 static void luaC_step(lua_State*L){
2175 global_State*g=G(L);
2176 l_mem lim=(1024u/100)*g->gcstepmul;
2177 if(lim==0)
2178 lim=(((lu_mem)(~(lu_mem)0)-2)-1)/2;
2179 g->gcdept+=g->totalbytes-g->GCthreshold;
2180 do{
2181 lim-=singlestep(L);
2182 if(g->gcstate==0)
2183 break;
2184 }while(lim>0);
2185 if(g->gcstate!=0){
2186 if(g->gcdept<1024u)
2187 g->GCthreshold=g->totalbytes+1024u;
2188 else{
2189 g->gcdept-=1024u;
2190 g->GCthreshold=g->totalbytes;
2191 }
2192 }
2193 else{
2194 setthreshold(g);
2195 }
2196 }
2198 global_State*g=G(L);
2199 if(g->gcstate==1)
2201 else
2202 makewhite(g,o);
2203 }
2205 global_State*g=G(L);
2206 GCObject*o=obj2gco(t);
2207 black2gray(o);
2208 t->gclist=g->grayagain;
2209 g->grayagain=o;
2210 }
2212 global_State*g=G(L);
2213 o->gch.next=g->rootgc;
2214 g->rootgc=o;
2215 o->gch.marked=luaC_white(g);
2216 o->gch.tt=tt;
2217 }
2219 global_State*g=G(L);
2220 GCObject*o=obj2gco(uv);
2221 o->gch.next=g->rootgc;
2222 g->rootgc=o;
2223 if(isgray(o)){
2224 if(g->gcstate==1){
2225 gray2black(o);
2226 luaC_barrier(L,uv,uv->v);
2227 }
2228 else{
2229 makewhite(g,o);
2230 }
2231 }
2232 }
2233 typedef union{
2234 lua_Number r;
2235 TString*ts;
2236 }SemInfo;
2237 typedef struct Token{
2238 int token;
2241 typedef struct LexState{
2242 int current;
2243 int linenumber;
2244 int lastline;
2245 Token t;
2247 struct FuncState*fs;
2248 struct lua_State*L;
2249 ZIO*z;
2250 Mbuffer*buff;
2251 TString*source;
2254 static void luaX_init(lua_State*L);
2255 static void luaX_lexerror(LexState*ls,const char*msg,int token);
2256 #define state_size(x)(sizeof(x)+0)
2257 #define fromstate(l)(cast(lu_byte*,(l))-0)
2258 #define tostate(l)(cast(lua_State*,cast(lu_byte*,l)+0))
2259 typedef struct LG{
2261 global_State g;
2264 L1->base_ci=luaM_newvector(L,8,CallInfo);
2265 L1->ci=L1->base_ci;
2266 L1->size_ci=8;
2267 L1->end_ci=L1->base_ci+L1->size_ci-1;
2268 L1->stack=luaM_newvector(L,(2*20)+5,TValue);
2269 L1->stacksize=(2*20)+5;
2270 L1->top=L1->stack;
2271 L1->stack_last=L1->stack+(L1->stacksize-5)-1;
2272 L1->ci->func=L1->top;
2273 setnilvalue(L1->top++);
2274 L1->base=L1->ci->base=L1->top;
2275 L1->ci->top=L1->top+20;
2276 }
2278 luaM_freearray(L,L1->base_ci,L1->size_ci,CallInfo);
2279 luaM_freearray(L,L1->stack,L1->stacksize,TValue);
2280 }
2281 static void f_luaopen(lua_State*L,void*ud){
2282 global_State*g=G(L);
2283 UNUSED(ud);
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"));
2291 g->GCthreshold=4*g->totalbytes;
2292 }
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));
2311 }
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);
2320 }
2322 luaF_close(L1,L1->stack);
2323 freestack(L,L1);
2325 }
2327 int i;
2328 lua_State*L;
2329 global_State*g;
2330 void*l=(*f)(ud,NULL,0,state_size(LG));
2331 if(l==NULL)return NULL;
2332 L=tostate(l);
2333 g=&((LG*)L)->g;
2334 L->next=NULL;
2335 L->tt=8;
2336 g->currentwhite=bit2mask(0,5);
2337 L->marked=luaC_white(g);
2338 set2bits(L->marked,5,6);
2339 preinit_state(L,g);
2340 g->frealloc=f;
2341 g->ud=ud;
2342 g->mainthread=L;
2343 g->uvhead.u.l.prev=&g->uvhead;
2344 g->uvhead.u.l.next=&g->uvhead;
2345 g->GCthreshold=0;
2346 g->strt.size=0;
2347 g->strt.nuse=0;
2348 g->strt.hash=NULL;
2350 luaZ_initbuffer(L,&g->buff);
2351 g->panic=NULL;
2352 g->gcstate=0;
2353 g->rootgc=obj2gco(L);
2354 g->sweepstrgc=0;
2355 g->sweepgc=&g->rootgc;
2356 g->gray=NULL;
2357 g->grayagain=NULL;
2358 g->weak=NULL;
2359 g->tmudata=NULL;
2360 g->totalbytes=sizeof(LG);
2361 g->gcpause=200;
2362 g->gcstepmul=200;
2363 g->gcdept=0;
2364 for(i=0;i<(8+1);i++)g->mt[i]=NULL;
2366 close_state(L);
2367 L=NULL;
2368 }
2369 else
2370 {}
2371 return L;
2372 }
2373 static void callallgcTM(lua_State*L,void*ud){
2374 UNUSED(ud);
2375 luaC_callGCTM(L);
2376 }
2377 static void lua_close(lua_State*L){
2378 L=G(L)->mainthread;
2379 luaF_close(L,L->stack);
2381 L->errfunc=0;
2382 do{
2383 L->ci=L->base_ci;
2384 L->base=L->top=L->ci->base;
2385 L->nCcalls=L->baseCcalls=0;
2387 close_state(L);
2388 }
2389 #define getcode(fs,e)((fs)->f->code[(e)->u.s.info])
2390 #define luaK_codeAsBx(fs,o,A,sBx)luaK_codeABx(fs,o,A,(sBx)+(((1<<(9+9))-1)>>1))
2391 #define luaK_setmultret(fs,e)luaK_setreturns(fs,e,(-1))
2392 static int luaK_codeABx(FuncState*fs,OpCode o,int A,unsigned int Bx);
2393 static int luaK_codeABC(FuncState*fs,OpCode o,int A,int B,int C);
2394 static void luaK_setreturns(FuncState*fs,expdesc*e,int nresults);
2395 static void luaK_patchtohere(FuncState*fs,int list);
2396 static void luaK_concat(FuncState*fs,int*l1,int l2);
2398 if(!isLua(ci))return-1;
2399 if(ci==L->ci)
2400 ci->savedpc=L->savedpc;
2401 return pcRel(ci->savedpc,ci_func(ci)->l.p);
2402 }
2404 int pc=currentpc(L,ci);
2405 if(pc<0)
2406 return-1;
2407 else
2408 return getline_(ci_func(ci)->l.p,pc);
2409 }
2411 int status;
2412 CallInfo*ci;
2413 for(ci=L->ci;level>0&&ci>L->base_ci;ci--){
2414 level--;
2415 if(f_isLua(ci))
2416 level-=ci->tailcalls;
2417 }
2418 if(level==0&&ci>L->base_ci){
2419 status=1;
2420 ar->i_ci=cast_int(ci-L->base_ci);
2421 }
2422 else if(level<0){
2423 status=1;
2424 ar->i_ci=0;
2425 }
2426 else status=0;
2427 return status;
2428 }
2430 return(isLua(ci)?ci_func(ci)->l.p:NULL);
2431 }
2432 static void funcinfo(lua_Debug*ar,Closure*cl){
2433 if(cl->c.isC){
2434 ar->source="=[C]";
2435 ar->linedefined=-1;
2436 ar->lastlinedefined=-1;
2437 ar->what="C";
2438 }
2439 else{
2440 ar->source=getstr(cl->l.p->source);
2441 ar->linedefined=cl->l.p->linedefined;
2443 ar->what=(ar->linedefined==0)?"main":"Lua";
2444 }
2445 luaO_chunkid(ar->short_src,ar->source,60);
2446 }
2447 static void info_tailcall(lua_Debug*ar){
2448 ar->name=ar->namewhat="";
2449 ar->what="tail";
2450 ar->lastlinedefined=ar->linedefined=ar->currentline=-1;
2451 ar->source="=(tail call)";
2452 luaO_chunkid(ar->short_src,ar->source,60);
2453 ar->nups=0;
2454 }
2456 if(f==NULL||f->c.isC){
2457 setnilvalue(L->top);
2458 }
2459 else{
2460 Table*t=luaH_new(L,0,0);
2461 int*lineinfo=f->l.p->lineinfo;
2462 int i;
2463 for(i=0;i<f->l.p->sizelineinfo;i++)
2464 setbvalue(luaH_setnum(L,t,lineinfo[i]),1);
2465 sethvalue(L,L->top,t);
2466 }
2467 incr_top(L);
2468 }
2469 static int auxgetinfo(lua_State*L,const char*what,lua_Debug*ar,
2470 Closure*f,CallInfo*ci){
2471 int status=1;
2472 if(f==NULL){
2473 info_tailcall(ar);
2474 return status;
2475 }
2476 for(;*what;what++){
2477 switch(*what){
2478 case'S':{
2479 funcinfo(ar,f);
2480 break;
2481 }
2482 case'l':{
2483 ar->currentline=(ci)?currentline(L,ci):-1;
2484 break;
2485 }
2486 case'u':{
2487 ar->nups=f->c.nupvalues;
2488 break;
2489 }
2490 case'n':{
2491 ar->namewhat=(ci)?NULL:NULL;
2492 if(ar->namewhat==NULL){
2493 ar->namewhat="";
2494 ar->name=NULL;
2495 }
2496 break;
2497 }
2498 case'L':
2499 case'f':
2500 break;
2501 default:status=0;
2502 }
2503 }
2504 return status;
2505 }
2506 static int lua_getinfo(lua_State*L,const char*what,lua_Debug*ar){
2507 int status;
2508 Closure*f=NULL;
2509 CallInfo*ci=NULL;
2510 if(*what=='>'){
2511 StkId func=L->top-1;
2513 what++;
2514 f=clvalue(func);
2515 L->top--;
2516 }
2517 else if(ar->i_ci!=0){
2518 ci=L->base_ci+ar->i_ci;
2519 f=clvalue(ci->func);
2520 }
2521 status=auxgetinfo(L,what,ar,f,ci);
2522 if(strchr(what,'f')){
2523 if(f==NULL)setnilvalue(L->top);
2524 else setclvalue(L,L->top,f);
2525 incr_top(L);
2526 }
2527 if(strchr(what,'L'))
2529 return status;
2530 }
2531 static int isinstack(CallInfo*ci,const TValue*o){
2532 StkId p;
2533 for(p=ci->base;p<ci->top;p++)
2534 if(o==p)return 1;
2535 return 0;
2536 }
2537 static void luaG_typeerror(lua_State*L,const TValue*o,const char*op){
2538 const char*name=NULL;
2539 const char*t=luaT_typenames[ttype(o)];
2540 const char*kind=(isinstack(L->ci,o))?
2541 NULL:
2542 NULL;
2543 if(kind)
2544 luaG_runerror(L,"attempt to %s %s "LUA_QL("%s")" (a %s value)",
2545 op,kind,name,t);
2546 else
2547 luaG_runerror(L,"attempt to %s a %s value",op,t);
2548 }
2550 if(ttisstring(p1)||ttisnumber(p1))p1=p2;
2551 luaG_typeerror(L,p1,"concatenate");
2552 }
2553 static void luaG_aritherror(lua_State*L,const TValue*p1,const TValue*p2){
2554 TValue temp;
2555 if(luaV_tonumber(p1,&temp)==NULL)
2556 p2=p1;
2557 luaG_typeerror(L,p2,"perform arithmetic on");
2558 }
2559 static int luaG_ordererror(lua_State*L,const TValue*p1,const TValue*p2){
2560 const char*t1=luaT_typenames[ttype(p1)];
2561 const char*t2=luaT_typenames[ttype(p2)];
2562 if(t1[2]==t2[2])
2563 luaG_runerror(L,"attempt to compare two %s values",t1);
2564 else
2565 luaG_runerror(L,"attempt to compare %s with %s",t1,t2);
2566 return 0;
2567 }
2568 static void addinfo(lua_State*L,const char*msg){
2569 CallInfo*ci=L->ci;
2570 if(isLua(ci)){
2571 char buff[60];
2572 int line=currentline(L,ci);
2574 luaO_pushfstring(L,"%s:%d: %s",buff,line,msg);
2575 }
2576 }
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);
2585 }
2586 luaD_throw(L,2);
2587 }
2588 static void luaG_runerror(lua_State*L,const char*fmt,...){
2589 va_list argp;
2590 va_start(argp,fmt);
2592 va_end(argp);
2593 luaG_errormsg(L);
2594 }
2595 static int luaZ_fill(ZIO*z){
2596 size_t size;
2597 lua_State*L=z->L;
2598 const char*buff;
2599 buff=z->reader(L,z->data,&size);
2600 if(buff==NULL||size==0)return(-1);
2601 z->n=size-1;
2602 z->p=buff;
2603 return char2int(*(z->p++));
2604 }
2606 z->L=L;
2607 z->reader=reader;
2608 z->data=data;
2609 z->n=0;
2610 z->p=NULL;
2611 }
2612 static char*luaZ_openspace(lua_State*L,Mbuffer*buff,size_t n){
2613 if(n>buff->buffsize){
2614 if(n<32)n=32;
2616 }
2617 return buff->buffer;
2618 }
2619 #define opmode(t,a,b,c,m)(((t)<<7)|((a)<<6)|((b)<<4)|((c)<<2)|(m))
2620 static const lu_byte luaP_opmodes[(cast(int,OP_VARARG)+1)]={
2621 opmode(0,1,OpArgR,OpArgN,iABC)
2622 ,opmode(0,1,OpArgK,OpArgN,iABx)
2623 ,opmode(0,1,OpArgU,OpArgU,iABC)
2624 ,opmode(0,1,OpArgR,OpArgN,iABC)
2625 ,opmode(0,1,OpArgU,OpArgN,iABC)
2626 ,opmode(0,1,OpArgK,OpArgN,iABx)
2627 ,opmode(0,1,OpArgR,OpArgK,iABC)
2628 ,opmode(0,0,OpArgK,OpArgN,iABx)
2629 ,opmode(0,0,OpArgU,OpArgN,iABC)
2630 ,opmode(0,0,OpArgK,OpArgK,iABC)
2631 ,opmode(0,1,OpArgU,OpArgU,iABC)
2632 ,opmode(0,1,OpArgR,OpArgK,iABC)
2633 ,opmode(0,1,OpArgK,OpArgK,iABC)
2634 ,opmode(0,1,OpArgK,OpArgK,iABC)
2635 ,opmode(0,1,OpArgK,OpArgK,iABC)
2636 ,opmode(0,1,OpArgK,OpArgK,iABC)
2637 ,opmode(0,1,OpArgK,OpArgK,iABC)
2638 ,opmode(0,1,OpArgK,OpArgK,iABC)
2639 ,opmode(0,1,OpArgR,OpArgN,iABC)
2640 ,opmode(0,1,OpArgR,OpArgN,iABC)
2641 ,opmode(0,1,OpArgR,OpArgN,iABC)
2642 ,opmode(0,1,OpArgR,OpArgR,iABC)
2643 ,opmode(0,0,OpArgR,OpArgN,iAsBx)
2644 ,opmode(1,0,OpArgK,OpArgK,iABC)
2645 ,opmode(1,0,OpArgK,OpArgK,iABC)
2646 ,opmode(1,0,OpArgK,OpArgK,iABC)
2647 ,opmode(1,1,OpArgR,OpArgU,iABC)
2648 ,opmode(1,1,OpArgR,OpArgU,iABC)
2649 ,opmode(0,1,OpArgU,OpArgU,iABC)
2650 ,opmode(0,1,OpArgU,OpArgU,iABC)
2651 ,opmode(0,0,OpArgU,OpArgN,iABC)
2652 ,opmode(0,1,OpArgR,OpArgN,iAsBx)
2653 ,opmode(0,1,OpArgR,OpArgN,iAsBx)
2654 ,opmode(1,0,OpArgN,OpArgU,iABC)
2655 ,opmode(0,0,OpArgU,OpArgU,iABC)
2656 ,opmode(0,0,OpArgN,OpArgN,iABC)
2657 ,opmode(0,1,OpArgU,OpArgN,iABx)
2658 ,opmode(0,1,OpArgU,OpArgN,iABC)
2659 };
2660 #define next(ls)(ls->current=zgetc(ls->z))
2661 #define currIsNewline(ls)(ls->current=='\n'||ls->current=='\r')
2662 static const char*const luaX_tokens[]={
2663 "and","break","do","else","elseif",
2664 "end","false","for","function","if",
2665 "in","local","nil","not","or","repeat",
2666 "return","then","true","until","while",
2667 "..","...","==",">=","<=","~=",
2668 "<number>","<name>","<string>","<eof>",
2669 NULL
2670 };
2671 #define save_and_next(ls)(save(ls,ls->current),next(ls))
2672 static void save(LexState*ls,int c){
2673 Mbuffer*b=ls->buff;
2674 if(b->n+1>b->buffsize){
2675 size_t newsize;
2676 if(b->buffsize>=((size_t)(~(size_t)0)-2)/2)
2677 luaX_lexerror(ls,"lexical element too long",0);
2678 newsize=b->buffsize*2;
2679 luaZ_resizebuffer(ls->L,b,newsize);
2680 }
2681 b->buffer[b->n++]=cast(char,c);
2682 }
2683 static void luaX_init(lua_State*L){
2684 int i;
2685 for(i=0;i<(cast(int,TK_WHILE-257+1));i++){
2687 luaS_fix(ts);
2688 ts->tsv.reserved=cast_byte(i+1);
2689 }
2690 }
2691 static const char*luaX_token2str(LexState*ls,int token){
2692 if(token<257){
2693 return(iscntrl(token))?luaO_pushfstring(ls->L,"char(%d)",token):
2694 luaO_pushfstring(ls->L,"%c",token);
2695 }
2696 else
2697 return luaX_tokens[token-257];
2698 }
2699 static const char*txtToken(LexState*ls,int token){
2700 switch(token){
2701 case TK_NAME:
2702 case TK_STRING:
2703 case TK_NUMBER:
2704 save(ls,'\0');
2705 return luaZ_buffer(ls->buff);
2706 default:
2707 return luaX_token2str(ls,token);
2708 }
2709 }
2710 static void luaX_lexerror(LexState*ls,const char*msg,int token){
2711 char buff[80];
2712 luaO_chunkid(buff,getstr(ls->source),80);
2713 msg=luaO_pushfstring(ls->L,"%s:%d: %s",buff,ls->linenumber,msg);
2714 if(token)
2715 luaO_pushfstring(ls->L,"%s near "LUA_QL("%s"),msg,txtToken(ls,token));
2716 luaD_throw(ls->L,3);
2717 }
2718 static void luaX_syntaxerror(LexState*ls,const char*msg){
2719 luaX_lexerror(ls,msg,ls->t.token);
2720 }
2721 static TString*luaX_newstring(LexState*ls,const char*str,size_t l){
2722 lua_State*L=ls->L;
2723 TString*ts=luaS_newlstr(L,str,l);
2724 TValue*o=luaH_setstr(L,ls->fs->h,ts);
2725 if(ttisnil(o)){
2726 setbvalue(o,1);
2727 luaC_checkGC(L);
2728 }
2729 return ts;
2730 }
2731 static void inclinenumber(LexState*ls){
2732 int old=ls->current;
2733 next(ls);
2734 if(currIsNewline(ls)&&ls->current!=old)
2735 next(ls);
2736 if(++ls->linenumber>=(INT_MAX-2))
2737 luaX_syntaxerror(ls,"chunk has too many lines");
2738 }
2740 ls->decpoint='.';
2741 ls->L=L;
2742 ls->lookahead.token=TK_EOS;
2743 ls->z=z;
2744 ls->fs=NULL;
2745 ls->linenumber=1;
2746 ls->lastline=1;
2747 ls->source=source;
2748 luaZ_resizebuffer(ls->L,ls->buff,32);
2749 next(ls);
2750 }
2751 static int check_next(LexState*ls,const char*set){
2752 if(!strchr(set,ls->current))
2753 return 0;
2754 save_and_next(ls);
2755 return 1;
2756 }
2757 static void buffreplace(LexState*ls,char from,char to){
2758 size_t n=luaZ_bufflen(ls->buff);
2759 char*p=luaZ_buffer(ls->buff);
2760 while(n--)
2761 if(p[n]==from)p[n]=to;
2762 }
2763 static void read_numeral(LexState*ls,SemInfo*seminfo){
2764 do{
2765 save_and_next(ls);
2766 }while(isdigit(ls->current)||ls->current=='.');
2767 if(check_next(ls,"Ee"))
2768 check_next(ls,"+-");
2769 while(isalnum(ls->current)||ls->current=='_')
2770 save_and_next(ls);
2771 save(ls,'\0');
2772 buffreplace(ls,'.',ls->decpoint);
2773 if(!luaO_str2d(luaZ_buffer(ls->buff),&seminfo->r))
2774 luaX_lexerror(ls,"malformed number",TK_NUMBER);
2775 }
2776 static int skip_sep(LexState*ls){
2777 int count=0;
2778 int s=ls->current;
2779 save_and_next(ls);
2780 while(ls->current=='='){
2781 save_and_next(ls);
2782 count++;
2783 }
2784 return(ls->current==s)?count:(-count)-1;
2785 }
2786 static void read_long_string(LexState*ls,SemInfo*seminfo,int sep){
2787 int cont=0;
2788 (void)(cont);
2789 save_and_next(ls);
2790 if(currIsNewline(ls))
2791 inclinenumber(ls);
2792 for(;;){
2793 switch(ls->current){
2794 case(-1):
2795 luaX_lexerror(ls,(seminfo)?"unfinished long string":
2796 "unfinished long comment",TK_EOS);
2797 break;
2798 case']':{
2799 if(skip_sep(ls)==sep){
2800 save_and_next(ls);
2801 goto endloop;
2802 }
2803 break;
2804 }
2805 case'\n':
2806 case'\r':{
2807 save(ls,'\n');
2808 inclinenumber(ls);
2809 if(!seminfo)luaZ_resetbuffer(ls->buff);
2810 break;
2811 }
2812 default:{
2813 if(seminfo)save_and_next(ls);
2814 else next(ls);
2815 }
2816 }
2817 }endloop:
2818 if(seminfo)
2819 seminfo->ts=luaX_newstring(ls,luaZ_buffer(ls->buff)+(2+sep),
2820 luaZ_bufflen(ls->buff)-2*(2+sep));
2821 }
2822 static void read_string(LexState*ls,int del,SemInfo*seminfo){
2823 save_and_next(ls);
2824 while(ls->current!=del){
2825 switch(ls->current){
2826 case(-1):
2827 luaX_lexerror(ls,"unfinished string",TK_EOS);
2828 continue;
2829 case'\n':
2830 case'\r':
2831 luaX_lexerror(ls,"unfinished string",TK_STRING);
2832 continue;
2833 case'\\':{
2834 int c;
2835 next(ls);
2836 switch(ls->current){
2837 case'a':c='\a';break;
2838 case'b':c='\b';break;
2839 case'f':c='\f';break;
2840 case'n':c='\n';break;
2841 case'r':c='\r';break;
2842 case't':c='\t';break;
2843 case'v':c='\v';break;
2844 case'\n':
2845 case'\r':save(ls,'\n');inclinenumber(ls);continue;
2846 case(-1):continue;
2847 default:{
2848 if(!isdigit(ls->current))
2849 save_and_next(ls);
2850 else{
2851 int i=0;
2852 c=0;
2853 do{
2854 c=10*c+(ls->current-'0');
2855 next(ls);
2856 }while(++i<3&&isdigit(ls->current));
2857 if(c>UCHAR_MAX)
2858 luaX_lexerror(ls,"escape sequence too large",TK_STRING);
2859 save(ls,c);
2860 }
2861 continue;
2862 }
2863 }
2864 save(ls,c);
2865 next(ls);
2866 continue;
2867 }
2868 default:
2869 save_and_next(ls);
2870 }
2871 }
2872 save_and_next(ls);
2873 seminfo->ts=luaX_newstring(ls,luaZ_buffer(ls->buff)+1,
2874 luaZ_bufflen(ls->buff)-2);
2875 }
2876 static int llex(LexState*ls,SemInfo*seminfo){
2877 luaZ_resetbuffer(ls->buff);
2878 for(;;){
2879 switch(ls->current){
2880 case'\n':
2881 case'\r':{
2882 inclinenumber(ls);
2883 continue;
2884 }
2885 case'-':{
2886 next(ls);
2887 if(ls->current!='-')return'-';
2888 next(ls);
2889 if(ls->current=='['){
2890 int sep=skip_sep(ls);
2891 luaZ_resetbuffer(ls->buff);
2892 if(sep>=0){
2894 luaZ_resetbuffer(ls->buff);
2895 continue;
2896 }
2897 }
2898 while(!currIsNewline(ls)&&ls->current!=(-1))
2899 next(ls);
2900 continue;
2901 }
2902 case'[':{
2903 int sep=skip_sep(ls);
2904 if(sep>=0){
2905 read_long_string(ls,seminfo,sep);
2906 return TK_STRING;
2907 }
2908 else if(sep==-1)return'[';
2909 else luaX_lexerror(ls,"invalid long string delimiter",TK_STRING);
2910 }
2911 case'=':{
2912 next(ls);
2913 if(ls->current!='=')return'=';
2914 else{next(ls);return TK_EQ;}
2915 }
2916 case'<':{
2917 next(ls);
2918 if(ls->current!='=')return'<';
2919 else{next(ls);return TK_LE;}
2920 }
2921 case'>':{
2922 next(ls);
2923 if(ls->current!='=')return'>';
2924 else{next(ls);return TK_GE;}
2925 }
2926 case'~':{
2927 next(ls);
2928 if(ls->current!='=')return'~';
2929 else{next(ls);return TK_NE;}
2930 }
2931 case'"':
2932 case'\'':{
2933 read_string(ls,ls->current,seminfo);
2934 return TK_STRING;
2935 }
2936 case'.':{
2937 save_and_next(ls);
2938 if(check_next(ls,".")){
2939 if(check_next(ls,"."))
2940 return TK_DOTS;
2941 else return TK_CONCAT;
2942 }
2943 else if(!isdigit(ls->current))return'.';
2944 else{
2945 read_numeral(ls,seminfo);
2946 return TK_NUMBER;
2947 }
2948 }
2949 case(-1):{
2950 return TK_EOS;
2951 }
2952 default:{
2953 if(isspace(ls->current)){
2954 next(ls);
2955 continue;
2956 }
2957 else if(isdigit(ls->current)){
2958 read_numeral(ls,seminfo);
2959 return TK_NUMBER;
2960 }
2961 else if(isalpha(ls->current)||ls->current=='_'){
2962 TString*ts;
2963 do{
2964 save_and_next(ls);
2965 }while(isalnum(ls->current)||ls->current=='_');
2966 ts=luaX_newstring(ls,luaZ_buffer(ls->buff),
2967 luaZ_bufflen(ls->buff));
2968 if(ts->tsv.reserved>0)
2969 return ts->tsv.reserved-1+257;
2970 else{
2971 seminfo->ts=ts;
2972 return TK_NAME;
2973 }
2974 }
2975 else{
2976 int c=ls->current;
2977 next(ls);
2978 return c;
2979 }
2980 }
2981 }
2982 }
2983 }
2984 static void luaX_next(LexState*ls){
2985 ls->lastline=ls->linenumber;
2986 if(ls->lookahead.token!=TK_EOS){
2987 ls->t=ls->lookahead;
2988 ls->lookahead.token=TK_EOS;
2989 }
2990 else
2991 ls->t.token=llex(ls,&ls->t.seminfo);
2992 }
2993 static void luaX_lookahead(LexState*ls){
2994 ls->lookahead.token=llex(ls,&ls->lookahead.seminfo);
2995 }
2996 #define hasjumps(e)((e)->t!=(e)->f)
2997 static int isnumeral(expdesc*e){
2998 return(e->k==VKNUM&&e->t==(-1)&&e->f==(-1));
2999 }
3000 static void luaK_nil(FuncState*fs,int from,int n){
3001 Instruction*previous;
3002 if(fs->pc>fs->lasttarget){
3003 if(fs->pc==0){
3004 if(from>=fs->nactvar)
3005 return;
3006 }
3007 else{
3008 previous=&fs->f->code[fs->pc-1];
3009 if(GET_OPCODE(*previous)==OP_LOADNIL){
3010 int pfrom=GETARG_A(*previous);
3011 int pto=GETARG_B(*previous);
3012 if(pfrom<=from&&from<=pto+1){
3013 if(from+n-1>pto)
3014 SETARG_B(*previous,from+n-1);
3015 return;
3016 }
3017 }
3018 }
3019 }
3020 luaK_codeABC(fs,OP_LOADNIL,from,from+n-1,0);
3021 }
3022 static int luaK_jump(FuncState*fs){
3023 int jpc=fs->jpc;
3024 int j;
3025 fs->jpc=(-1);
3026 j=luaK_codeAsBx(fs,OP_JMP,0,(-1));
3027 luaK_concat(fs,&j,jpc);
3028 return j;
3029 }
3030 static void luaK_ret(FuncState*fs,int first,int nret){
3031 luaK_codeABC(fs,OP_RETURN,first,nret+1,0);
3032 }
3033 static int condjump(FuncState*fs,OpCode op,int A,int B,int C){
3034 luaK_codeABC(fs,op,A,B,C);
3035 return luaK_jump(fs);
3036 }
3037 static void fixjump(FuncState*fs,int pc,int dest){
3038 Instruction*jmp=&fs->f->code[pc];
3039 int offset=dest-(pc+1);
3040 if(abs(offset)>(((1<<(9+9))-1)>>1))
3041 luaX_syntaxerror(fs->ls,"control structure too long");
3042 SETARG_sBx(*jmp,offset);
3043 }
3044 static int luaK_getlabel(FuncState*fs){
3045 fs->lasttarget=fs->pc;
3046 return fs->pc;
3047 }
3048 static int getjump(FuncState*fs,int pc){
3049 int offset=GETARG_sBx(fs->f->code[pc]);
3050 if(offset==(-1))
3051 return(-1);
3052 else
3053 return(pc+1)+offset;
3054 }
3056 Instruction*pi=&fs->f->code[pc];
3057 if(pc>=1&&testTMode(GET_OPCODE(*(pi-1))))
3058 return pi-1;
3059 else
3060 return pi;
3061 }
3062 static int need_value(FuncState*fs,int list){
3063 for(;list!=(-1);list=getjump(fs,list)){
3065 if(GET_OPCODE(i)!=OP_TESTSET)return 1;
3066 }
3067 return 0;
3068 }
3069 static int patchtestreg(FuncState*fs,int node,int reg){
3071 if(GET_OPCODE(*i)!=OP_TESTSET)
3072 return 0;
3073 if(reg!=((1<<8)-1)&&reg!=GETARG_B(*i))
3074 SETARG_A(*i,reg);
3075 else
3077 return 1;
3078 }
3079 static void removevalues(FuncState*fs,int list){
3080 for(;list!=(-1);list=getjump(fs,list))
3081 patchtestreg(fs,list,((1<<8)-1));
3082 }
3083 static void patchlistaux(FuncState*fs,int list,int vtarget,int reg,
3084 int dtarget){
3085 while(list!=(-1)){
3086 int next=getjump(fs,list);
3087 if(patchtestreg(fs,list,reg))
3088 fixjump(fs,list,vtarget);
3089 else
3090 fixjump(fs,list,dtarget);
3091 list=next;
3092 }
3093 }
3094 static void dischargejpc(FuncState*fs){
3095 patchlistaux(fs,fs->jpc,fs->pc,((1<<8)-1),fs->pc);
3096 fs->jpc=(-1);
3097 }
3098 static void luaK_patchlist(FuncState*fs,int list,int target){
3099 if(target==fs->pc)
3101 else{
3102 patchlistaux(fs,list,target,((1<<8)-1),target);
3103 }
3104 }
3105 static void luaK_patchtohere(FuncState*fs,int list){
3106 luaK_getlabel(fs);
3107 luaK_concat(fs,&fs->jpc,list);
3108 }
3109 static void luaK_concat(FuncState*fs,int*l1,int l2){
3110 if(l2==(-1))return;
3111 else if(*l1==(-1))
3112 *l1=l2;
3113 else{
3114 int list=*l1;
3115 int next;
3116 while((next=getjump(fs,list))!=(-1))
3117 list=next;
3118 fixjump(fs,list,l2);
3119 }
3120 }
3121 static void luaK_checkstack(FuncState*fs,int n){
3122 int newstack=fs->freereg+n;
3123 if(newstack>fs->f->maxstacksize){
3124 if(newstack>=250)
3125 luaX_syntaxerror(fs->ls,"function or expression too complex");
3126 fs->f->maxstacksize=cast_byte(newstack);
3127 }
3128 }
3129 static void luaK_reserveregs(FuncState*fs,int n){
3130 luaK_checkstack(fs,n);
3131 fs->freereg+=n;
3132 }
3133 static void freereg(FuncState*fs,int reg){
3134 if(!ISK(reg)&&reg>=fs->nactvar){
3135 fs->freereg--;
3136 }
3137 }
3138 static void freeexp(FuncState*fs,expdesc*e){
3139 if(e->k==VNONRELOC)
3140 freereg(fs,e->u.s.info);
3141 }
3142 static int addk(FuncState*fs,TValue*k,TValue*v){
3143 lua_State*L=fs->L;
3144 TValue*idx=luaH_set(L,fs->h,k);
3145 Proto*f=fs->f;
3146 int oldsize=f->sizek;
3147 if(ttisnumber(idx)){
3148 return cast_int(nvalue(idx));
3149 }
3150 else{
3151 setnvalue(idx,cast_num(fs->nk));
3152 luaM_growvector(L,f->k,fs->nk,f->sizek,TValue,
3153 ((1<<(9+9))-1),"constant table overflow");
3154 while(oldsize<f->sizek)setnilvalue(&f->k[oldsize++]);
3155 setobj(L,&f->k[fs->nk],v);
3156 luaC_barrier(L,f,v);
3157 return fs->nk++;
3158 }
3159 }
3161 TValue o;
3162 setsvalue(fs->L,&o,s);
3163 return addk(fs,&o,&o);
3164 }
3166 TValue o;
3167 setnvalue(&o,