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.
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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 ******************************************************************************/
56 enum OpMode
{iABC
,iABx
,iAsBx
};
122 TK_DO
,TK_ELSE
,TK_ELSEIF
,TK_END
,TK_FALSE
,TK_FOR
,TK_FUNCTION
,
123 TK_IF
,TK_IN
,TK_LOCAL
,TK_NIL
,TK_NOT
,TK_OR
,TK_REPEAT
,
124 TK_RETURN
,TK_THEN
,TK_TRUE
,TK_UNTIL
,TK_WHILE
,
125 TK_CONCAT
,TK_DOTS
,TK_EQ
,TK_GE
,TK_LE
,TK_NE
,TK_NUMBER
,
126 TK_NAME
,TK_STRING
,TK_EOS
129 OPR_ADD
,OPR_SUB
,OPR_MUL
,OPR_DIV
,OPR_MOD
,OPR_POW
,
132 OPR_LT
,OPR_LE
,OPR_GT
,OPR_GE
,
136 typedef enum UnOpr
{OPR_MINUS
,OPR_NOT
,OPR_LEN
,OPR_NOUNOPR
}UnOpr
;
137 #define LUA_QL(x)"'"x"'"
138 #define luai_apicheck(L,o){(void)L;}
139 #define lua_number2str(s,n)sprintf((s),"%.14g",(n))
140 #define lua_str2number(s,p)strtod((s),(p))
141 #define luai_numadd(a,b)((a)+(b))
142 #define luai_numsub(a,b)((a)-(b))
143 #define luai_nummul(a,b)((a)*(b))
144 #define luai_numdiv(a,b)((a)/(b))
145 #define luai_nummod(a,b)((a)-floor((a)/(b))*(b))
146 #define luai_numpow(a,b)(pow(a,b))
147 #define luai_numunm(a)(-(a))
148 #define luai_numeq(a,b)((a)==(b))
149 #define luai_numlt(a,b)((a)<(b))
150 #define luai_numle(a,b)((a)<=(b))
151 #define luai_numisnan(a)(!luai_numeq((a),(a)))
152 #define lua_number2int(i,d)((i)=(int)(d))
153 #define lua_number2integer(i,d)((i)=(lua_Integer)(d))
154 #define LUAI_THROW(L,c)longjmp((c)->b,1)
155 #define LUAI_TRY(L,c,a)if(setjmp((c)->b)==0){a}
156 #define lua_pclose(L,file)((void)((void)L,file),0)
157 #define lua_upvalueindex(i)((-10002)-(i))
158 typedef struct lua_State lua_State
;
159 typedef int(*lua_CFunction
)(lua_State
*L
);
160 typedef const char*(*lua_Reader
)(lua_State
*L
,void*ud
,size_t*sz
);
161 typedef void*(*lua_Alloc
)(void*ud
,void*ptr
,size_t osize
,size_t nsize
);
162 typedef double lua_Number
;
163 typedef ptrdiff_t lua_Integer
;
164 static void lua_settop(lua_State
*L
,int idx
);
165 static int lua_type(lua_State
*L
,int idx
);
166 static const char* lua_tolstring(lua_State
*L
,int idx
,size_t*len
);
167 static size_t lua_objlen(lua_State
*L
,int idx
);
168 static void lua_pushlstring(lua_State
*L
,const char*s
,size_t l
);
169 static void lua_pushcclosure(lua_State
*L
,lua_CFunction fn
,int n
);
170 static void lua_createtable(lua_State
*L
,int narr
,int nrec
);
171 static void lua_setfield(lua_State
*L
,int idx
,const char*k
);
172 #define lua_pop(L,n)lua_settop(L,-(n)-1)
173 #define lua_newtable(L)lua_createtable(L,0,0)
174 #define lua_pushcfunction(L,f)lua_pushcclosure(L,(f),0)
175 #define lua_strlen(L,i)lua_objlen(L,(i))
176 #define lua_isfunction(L,n)(lua_type(L,(n))==6)
177 #define lua_istable(L,n)(lua_type(L,(n))==5)
178 #define lua_isnil(L,n)(lua_type(L,(n))==0)
179 #define lua_isboolean(L,n)(lua_type(L,(n))==1)
180 #define lua_isnone(L,n)(lua_type(L,(n))==(-1))
181 #define lua_isnoneornil(L,n)(lua_type(L,(n))<=0)
182 #define lua_pushliteral(L,s)lua_pushlstring(L,""s,(sizeof(s)/sizeof(char))-1)
183 #define lua_setglobal(L,s)lua_setfield(L,(-10002),(s))
184 #define lua_tostring(L,i)lua_tolstring(L,(i),NULL)
185 typedef struct lua_Debug lua_Debug
;
186 typedef void(*lua_Hook
)(lua_State
*L
,lua_Debug
*ar
);
200 typedef unsigned int lu_int32
;
201 typedef size_t lu_mem
;
202 typedef ptrdiff_t l_mem
;
203 typedef unsigned char lu_byte
;
204 #define IntPoint(p)((unsigned int)(lu_mem)(p))
205 typedef union{double u
;void*s
;long l
;}L_Umaxalign
;
206 typedef double l_uacNumber
;
207 #define check_exp(c,e)(e)
208 #define UNUSED(x)((void)(x))
209 #define cast(t,exp)((t)(exp))
210 #define cast_byte(i)cast(lu_byte,(i))
211 #define cast_num(i)cast(lua_Number,(i))
212 #define cast_int(i)cast(int,(i))
213 typedef lu_int32 Instruction
;
214 #define condhardstacktests(x)((void)0)
215 typedef union GCObject GCObject
;
216 typedef struct GCheader
{
217 GCObject
*next
;lu_byte tt
;lu_byte marked
;
225 typedef struct lua_TValue
{
228 #define ttisnil(o)(ttype(o)==0)
229 #define ttisnumber(o)(ttype(o)==3)
230 #define ttisstring(o)(ttype(o)==4)
231 #define ttistable(o)(ttype(o)==5)
232 #define ttisfunction(o)(ttype(o)==6)
233 #define ttisboolean(o)(ttype(o)==1)
234 #define ttisuserdata(o)(ttype(o)==7)
235 #define ttisthread(o)(ttype(o)==8)
236 #define ttislightuserdata(o)(ttype(o)==2)
237 #define ttype(o)((o)->tt)
238 #define gcvalue(o)check_exp(iscollectable(o),(o)->value.gc)
239 #define pvalue(o)check_exp(ttislightuserdata(o),(o)->value.p)
240 #define nvalue(o)check_exp(ttisnumber(o),(o)->value.n)
241 #define rawtsvalue(o)check_exp(ttisstring(o),&(o)->value.gc->ts)
242 #define tsvalue(o)(&rawtsvalue(o)->tsv)
243 #define rawuvalue(o)check_exp(ttisuserdata(o),&(o)->value.gc->u)
244 #define uvalue(o)(&rawuvalue(o)->uv)
245 #define clvalue(o)check_exp(ttisfunction(o),&(o)->value.gc->cl)
246 #define hvalue(o)check_exp(ttistable(o),&(o)->value.gc->h)
247 #define bvalue(o)check_exp(ttisboolean(o),(o)->value.b)
248 #define thvalue(o)check_exp(ttisthread(o),&(o)->value.gc->th)
249 #define l_isfalse(o)(ttisnil(o)||(ttisboolean(o)&&bvalue(o)==0))
250 #define checkconsistency(obj)
251 #define checkliveness(g,obj)
252 #define setnilvalue(obj)((obj)->tt=0)
253 #define setnvalue(obj,x){TValue*i_o=(obj);i_o->value.n=(x);i_o->tt=3;}
254 #define setbvalue(obj,x){TValue*i_o=(obj);i_o->value.b=(x);i_o->tt=1;}
255 #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);}
256 #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);}
257 #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);}
258 #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);}
259 #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);}
260 #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);}
261 #define setobj(L,obj1,obj2){const TValue*o2=(obj2);TValue*o1=(obj1);o1->value=o2->value;o1->tt=o2->tt;checkliveness(G(L),o1);}
262 #define setttype(obj,tt)(ttype(obj)=(tt))
263 #define iscollectable(o)(ttype(o)>=4)
264 typedef TValue
*StkId
;
265 typedef union TString
{
268 GCObject
*next
;lu_byte tt
;lu_byte marked
;
274 #define getstr(ts)cast(const char*,(ts)+1)
275 #define svalue(o)getstr(rawtsvalue(o))
279 GCObject
*next
;lu_byte tt
;lu_byte marked
;
280 struct Table
*metatable
;
285 typedef struct Proto
{
286 GCObject
*next
;lu_byte tt
;lu_byte marked
;
291 struct LocVar
*locvars
;
306 lu_byte maxstacksize
;
308 typedef struct LocVar
{
313 typedef struct UpVal
{
314 GCObject
*next
;lu_byte tt
;lu_byte marked
;
324 typedef struct CClosure
{
325 GCObject
*next
;lu_byte tt
;lu_byte marked
;lu_byte isC
;lu_byte nupvalues
;GCObject
*gclist
;struct Table
*env
;
329 typedef struct LClosure
{
330 GCObject
*next
;lu_byte tt
;lu_byte marked
;lu_byte isC
;lu_byte nupvalues
;GCObject
*gclist
;struct Table
*env
;
334 typedef union Closure
{
338 #define iscfunction(o)(ttype(o)==6&&clvalue(o)->c.isC)
350 typedef struct Table
{
351 GCObject
*next
;lu_byte tt
;lu_byte marked
;
354 struct Table
*metatable
;
361 #define lmod(s,size)(check_exp((size&(size-1))==0,(cast(int,(s)&((size)-1)))))
362 #define twoto(x)((size_t)1<<(x))
363 #define sizenode(t)(twoto((t)->lsizenode))
364 static const TValue luaO_nilobject_
;
365 #define ceillog2(x)(luaO_log2((x)-1)+1)
366 static int luaO_log2(unsigned int x
);
367 #define gfasttm(g,et,e)((et)==NULL?NULL:((et)->flags&(1u<<(e)))?NULL:luaT_gettm(et,e,(g)->tmname[e]))
368 #define fasttm(l,et,e)gfasttm(G(l),et,e)
369 static const TValue
*luaT_gettm(Table
*events
,TMS event
,TString
*ename
);
370 #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))
371 #define luaM_freemem(L,b,s)luaM_realloc_(L,(b),(s),0)
372 #define luaM_free(L,b)luaM_realloc_(L,(b),sizeof(*(b)),0)
373 #define luaM_freearray(L,b,n,t)luaM_reallocv(L,(b),n,0,sizeof(t))
374 #define luaM_malloc(L,t)luaM_realloc_(L,NULL,0,(t))
375 #define luaM_new(L,t)cast(t*,luaM_malloc(L,sizeof(t)))
376 #define luaM_newvector(L,n,t)cast(t*,luaM_reallocv(L,NULL,0,n,sizeof(t)))
377 #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)))
378 #define luaM_reallocvector(L,v,oldn,n,t)((v)=cast(t*,luaM_reallocv(L,v,oldn,n,sizeof(t))))
379 static void*luaM_realloc_(lua_State
*L
,void*block
,size_t oldsize
,
381 static void*luaM_toobig(lua_State
*L
);
382 static void*luaM_growaux_(lua_State
*L
,void*block
,int*size
,
383 size_t size_elem
,int limit
,
384 const char*errormsg
);
385 typedef struct Zio ZIO
;
386 #define char2int(c)cast(int,cast(unsigned char,(c)))
387 #define zgetc(z)(((z)->n--)>0?char2int(*(z)->p++):luaZ_fill(z))
388 typedef struct Mbuffer
{
393 #define luaZ_initbuffer(L,buff)((buff)->buffer=NULL,(buff)->buffsize=0)
394 #define luaZ_buffer(buff)((buff)->buffer)
395 #define luaZ_sizebuffer(buff)((buff)->buffsize)
396 #define luaZ_bufflen(buff)((buff)->n)
397 #define luaZ_resetbuffer(buff)((buff)->n=0)
398 #define luaZ_resizebuffer(L,buff,size)(luaM_reallocvector(L,(buff)->buffer,(buff)->buffsize,size,char),(buff)->buffsize=size)
399 #define luaZ_freebuffer(L,buff)luaZ_resizebuffer(L,buff,0)
407 static int luaZ_fill(ZIO
*z
);
409 #define gt(L)(&L->l_gt)
410 #define registry(L)(&G(L)->l_registry)
411 typedef struct stringtable
{
416 typedef struct CallInfo
{
420 const Instruction
*savedpc
;
424 #define curr_func(L)(clvalue(L->ci->func))
425 #define ci_func(ci)(clvalue((ci)->func))
426 #define f_isLua(ci)(!ci_func(ci)->c.isC)
427 #define isLua(ci)(ttisfunction((ci)->func)&&f_isLua(ci))
428 typedef struct global_State
{
432 lu_byte currentwhite
;
450 struct lua_State
*mainthread
;
452 struct Table
*mt
[(8+1)];
453 TString
*tmname
[TM_N
];
456 GCObject
*next
;lu_byte tt
;lu_byte marked
;
462 const Instruction
*savedpc
;
469 unsigned short nCcalls
;
470 unsigned short baseCcalls
;
480 struct lua_longjmp
*errorJmp
;
494 #define rawgco2ts(o)check_exp((o)->gch.tt==4,&((o)->ts))
495 #define gco2ts(o)(&rawgco2ts(o)->tsv)
496 #define rawgco2u(o)check_exp((o)->gch.tt==7,&((o)->u))
497 #define gco2u(o)(&rawgco2u(o)->uv)
498 #define gco2cl(o)check_exp((o)->gch.tt==6,&((o)->cl))
499 #define gco2h(o)check_exp((o)->gch.tt==5,&((o)->h))
500 #define gco2p(o)check_exp((o)->gch.tt==(8+1),&((o)->p))
501 #define gco2uv(o)check_exp((o)->gch.tt==(8+2),&((o)->uv))
502 #define ngcotouv(o)check_exp((o)==NULL||(o)->gch.tt==(8+2),&((o)->uv))
503 #define gco2th(o)check_exp((o)->gch.tt==8,&((o)->th))
504 #define obj2gco(v)(cast(GCObject*,(v)))
505 static void luaE_freethread(lua_State
*L
,lua_State
*L1
);
506 #define pcRel(pc,p)(cast(int,(pc)-(p)->code)-1)
507 #define getline_(f,pc)(((f)->lineinfo)?(f)->lineinfo[pc]:0)
508 #define resethookcount(L)(L->hookcount=L->basehookcount)
509 static void luaG_typeerror(lua_State
*L
,const TValue
*o
,
511 static void luaG_runerror(lua_State
*L
,const char*fmt
,...);
512 #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));
513 #define incr_top(L){luaD_checkstack(L,1);L->top++;}
514 #define savestack(L,p)((char*)(p)-(char*)L->stack)
515 #define restorestack(L,n)((TValue*)((char*)L->stack+(n)))
516 #define saveci(L,p)((char*)(p)-(char*)L->base_ci)
517 #define restoreci(L,n)((CallInfo*)((char*)L->base_ci+(n)))
518 typedef void(*Pfunc
)(lua_State
*L
,void*ud
);
519 static int luaD_poscall(lua_State
*L
,StkId firstResult
);
520 static void luaD_reallocCI(lua_State
*L
,int newsize
);
521 static void luaD_reallocstack(lua_State
*L
,int newsize
);
522 static void luaD_growstack(lua_State
*L
,int n
);
523 static void luaD_throw(lua_State
*L
,int errcode
);
524 static void*luaM_growaux_(lua_State
*L
,void*block
,int*size
,size_t size_elems
,
525 int limit
,const char*errormsg
){
530 luaG_runerror(L
,errormsg
);
538 newblock
=luaM_reallocv(L
,block
,*size
,newsize
,size_elems
);
542 static void*luaM_toobig(lua_State
*L
){
543 luaG_runerror(L
,"memory allocation error: block too big");
546 static void*luaM_realloc_(lua_State
*L
,void*block
,size_t osize
,size_t nsize
){
548 block
=(*g
->frealloc
)(g
->ud
,block
,osize
,nsize
);
549 if(block
==NULL
&&nsize
>0)
551 g
->totalbytes
=(g
->totalbytes
-osize
)+nsize
;
554 #define resetbits(x,m)((x)&=cast(lu_byte,~(m)))
555 #define setbits(x,m)((x)|=(m))
556 #define testbits(x,m)((x)&(m))
557 #define bitmask(b)(1<<(b))
558 #define bit2mask(b1,b2)(bitmask(b1)|bitmask(b2))
559 #define l_setbit(x,b)setbits(x,bitmask(b))
560 #define resetbit(x,b)resetbits(x,bitmask(b))
561 #define testbit(x,b)testbits(x,bitmask(b))
562 #define set2bits(x,b1,b2)setbits(x,(bit2mask(b1,b2)))
563 #define reset2bits(x,b1,b2)resetbits(x,(bit2mask(b1,b2)))
564 #define test2bits(x,b1,b2)testbits(x,(bit2mask(b1,b2)))
565 #define iswhite(x)test2bits((x)->gch.marked,0,1)
566 #define isblack(x)testbit((x)->gch.marked,2)
567 #define isgray(x)(!isblack(x)&&!iswhite(x))
568 #define otherwhite(g)(g->currentwhite^bit2mask(0,1))
569 #define isdead(g,v)((v)->gch.marked&otherwhite(g)&bit2mask(0,1))
570 #define changewhite(x)((x)->gch.marked^=bit2mask(0,1))
571 #define gray2black(x)l_setbit((x)->gch.marked,2)
572 #define valiswhite(x)(iscollectable(x)&&iswhite(gcvalue(x)))
573 #define luaC_white(g)cast(lu_byte,(g)->currentwhite&bit2mask(0,1))
574 #define luaC_checkGC(L){condhardstacktests(luaD_reallocstack(L,L->stacksize-5-1));if(G(L)->totalbytes>=G(L)->GCthreshold)luaC_step(L);}
575 #define luaC_barrier(L,p,v){if(valiswhite(v)&&isblack(obj2gco(p)))luaC_barrierf(L,obj2gco(p),gcvalue(v));}
576 #define luaC_barriert(L,t,v){if(valiswhite(v)&&isblack(obj2gco(t)))luaC_barrierback(L,t);}
577 #define luaC_objbarrier(L,p,o){if(iswhite(obj2gco(o))&&isblack(obj2gco(p)))luaC_barrierf(L,obj2gco(p),obj2gco(o));}
578 #define luaC_objbarriert(L,t,o){if(iswhite(obj2gco(o))&&isblack(obj2gco(t)))luaC_barrierback(L,t);}
579 static void luaC_step(lua_State
*L
);
580 static void luaC_link(lua_State
*L
,GCObject
*o
,lu_byte tt
);
581 static void luaC_linkupval(lua_State
*L
,UpVal
*uv
);
582 static void luaC_barrierf(lua_State
*L
,GCObject
*o
,GCObject
*v
);
583 static void luaC_barrierback(lua_State
*L
,Table
*t
);
584 #define sizestring(s)(sizeof(union TString)+((s)->len+1)*sizeof(char))
585 #define sizeudata(u)(sizeof(union Udata)+(u)->len)
586 #define luaS_new(L,s)(luaS_newlstr(L,s,strlen(s)))
587 #define luaS_newliteral(L,s)(luaS_newlstr(L,""s,(sizeof(s)/sizeof(char))-1))
588 #define luaS_fix(s)l_setbit((s)->tsv.marked,5)
589 static TString
*luaS_newlstr(lua_State
*L
,const char*str
,size_t l
);
590 #define tostring(L,o)((ttype(o)==4)||(luaV_tostring(L,o)))
591 #define tonumber(o,n)(ttype(o)==3||(((o)=luaV_tonumber(o,n))!=NULL))
592 #define equalobj(L,o1,o2)(ttype(o1)==ttype(o2)&&luaV_equalval(L,o1,o2))
593 static int luaV_equalval(lua_State
*L
,const TValue
*t1
,const TValue
*t2
);
594 static const TValue
*luaV_tonumber(const TValue
*obj
,TValue
*n
);
595 static int luaV_tostring(lua_State
*L
,StkId obj
);
596 static void luaV_execute(lua_State
*L
,int nexeccalls
);
597 static void luaV_concat(lua_State
*L
,int total
,int last
);
598 static const TValue luaO_nilobject_
={{NULL
},0};
599 static int luaO_int2fb(unsigned int x
){
606 else return((e
+1)<<3)|(cast_int(x
)-8);
608 static int luaO_fb2int(int x
){
611 else return((x
&7)+8)<<(e
-1);
613 static int luaO_log2(unsigned int x
){
614 static const lu_byte log_2
[256]={
615 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,
616 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,
617 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,
618 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,
619 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,
620 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,
621 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,
622 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
625 while(x
>=256){l
+=8;x
>>=8;}
628 static int luaO_rawequalObj(const TValue
*t1
,const TValue
*t2
){
629 if(ttype(t1
)!=ttype(t2
))return 0;
630 else switch(ttype(t1
)){
634 return luai_numeq(nvalue(t1
),nvalue(t2
));
636 return bvalue(t1
)==bvalue(t2
);
638 return pvalue(t1
)==pvalue(t2
);
640 return gcvalue(t1
)==gcvalue(t2
);
643 static int luaO_str2d(const char*s
,lua_Number
*result
){
645 *result
=lua_str2number(s
,&endptr
);
646 if(endptr
==s
)return 0;
647 if(*endptr
=='x'||*endptr
=='X')
648 *result
=cast_num(strtoul(s
,&endptr
,16));
649 if(*endptr
=='\0')return 1;
650 while(isspace(cast(unsigned char,*endptr
)))endptr
++;
651 if(*endptr
!='\0')return 0;
654 static void pushstr(lua_State
*L
,const char*str
){
655 setsvalue(L
,L
->top
,luaS_new(L
,str
));
658 static const char*luaO_pushvfstring(lua_State
*L
,const char*fmt
,va_list argp
){
662 const char*e
=strchr(fmt
,'%');
664 setsvalue(L
,L
->top
,luaS_newlstr(L
,fmt
,e
-fmt
));
668 const char*s
=va_arg(argp
,char*);
669 if(s
==NULL
)s
="(null)";
675 buff
[0]=cast(char,va_arg(argp
,int));
681 setnvalue(L
->top
,cast_num(va_arg(argp
,int)));
686 setnvalue(L
->top
,cast_num(va_arg(argp
,l_uacNumber
)));
691 char buff
[4*sizeof(void*)+8];
692 sprintf(buff
,"%p",va_arg(argp
,void*));
713 luaV_concat(L
,n
+1,cast_int(L
->top
-L
->base
)-1);
715 return svalue(L
->top
-1);
717 static const char*luaO_pushfstring(lua_State
*L
,const char*fmt
,...){
721 msg
=luaO_pushvfstring(L
,fmt
,argp
);
725 static void luaO_chunkid(char*out
,const char*source
,size_t bufflen
){
727 strncpy(out
,source
+1,bufflen
);
734 bufflen
-=sizeof(" '...' ");
744 size_t len
=strcspn(source
,"\n\r");
745 bufflen
-=sizeof(" [string \"...\"] ");
746 if(len
>bufflen
)len
=bufflen
;
747 strcpy(out
,"[string \"");
748 if(source
[len
]!='\0'){
749 strncat(out
,source
,len
);
758 #define gnode(t,i)(&(t)->node[i])
759 #define gkey(n)(&(n)->i_key.nk)
760 #define gval(n)(&(n)->i_val)
761 #define gnext(n)((n)->i_key.nk.next)
762 #define key2tval(n)(&(n)->i_key.tvk)
763 static TValue
*luaH_setnum(lua_State
*L
,Table
*t
,int key
);
764 static const TValue
*luaH_getstr(Table
*t
,TString
*key
);
765 static TValue
*luaH_set(lua_State
*L
,Table
*t
,const TValue
*key
);
766 static const char*const luaT_typenames
[]={
767 "nil","boolean","userdata","number",
768 "string","table","function","userdata","thread",
771 static void luaT_init(lua_State
*L
){
772 static const char*const luaT_eventname
[]={
773 "__index","__newindex",
774 "__gc","__mode","__eq",
775 "__add","__sub","__mul","__div","__mod",
776 "__pow","__unm","__len","__lt","__le",
781 G(L
)->tmname
[i
]=luaS_new(L
,luaT_eventname
[i
]);
782 luaS_fix(G(L
)->tmname
[i
]);
785 static const TValue
*luaT_gettm(Table
*events
,TMS event
,TString
*ename
){
786 const TValue
*tm
=luaH_getstr(events
,ename
);
788 events
->flags
|=cast_byte(1u<<event
);
793 static const TValue
*luaT_gettmbyobj(lua_State
*L
,const TValue
*o
,TMS event
){
797 mt
=hvalue(o
)->metatable
;
800 mt
=uvalue(o
)->metatable
;
803 mt
=G(L
)->mt
[ttype(o
)];
805 return(mt
?luaH_getstr(mt
,G(L
)->tmname
[event
]):(&luaO_nilobject_
));
807 #define sizeCclosure(n)(cast(int,sizeof(CClosure))+cast(int,sizeof(TValue)*((n)-1)))
808 #define sizeLclosure(n)(cast(int,sizeof(LClosure))+cast(int,sizeof(TValue*)*((n)-1)))
809 static Closure
*luaF_newCclosure(lua_State
*L
,int nelems
,Table
*e
){
810 Closure
*c
=cast(Closure
*,luaM_malloc(L
,sizeCclosure(nelems
)));
811 luaC_link(L
,obj2gco(c
),6);
814 c
->c
.nupvalues
=cast_byte(nelems
);
817 static Closure
*luaF_newLclosure(lua_State
*L
,int nelems
,Table
*e
){
818 Closure
*c
=cast(Closure
*,luaM_malloc(L
,sizeLclosure(nelems
)));
819 luaC_link(L
,obj2gco(c
),6);
822 c
->l
.nupvalues
=cast_byte(nelems
);
823 while(nelems
--)c
->l
.upvals
[nelems
]=NULL
;
826 static UpVal
*luaF_newupval(lua_State
*L
){
827 UpVal
*uv
=luaM_new(L
,UpVal
);
828 luaC_link(L
,obj2gco(uv
),(8+2));
833 static UpVal
*luaF_findupval(lua_State
*L
,StkId level
){
835 GCObject
**pp
=&L
->openupval
;
838 while(*pp
!=NULL
&&(p
=ngcotouv(*pp
))->v
>=level
){
840 if(isdead(g
,obj2gco(p
)))
841 changewhite(obj2gco(p
));
846 uv
=luaM_new(L
,UpVal
);
848 uv
->marked
=luaC_white(g
);
852 uv
->u
.l
.prev
=&g
->uvhead
;
853 uv
->u
.l
.next
=g
->uvhead
.u
.l
.next
;
854 uv
->u
.l
.next
->u
.l
.prev
=uv
;
855 g
->uvhead
.u
.l
.next
=uv
;
858 static void unlinkupval(UpVal
*uv
){
859 uv
->u
.l
.next
->u
.l
.prev
=uv
->u
.l
.prev
;
860 uv
->u
.l
.prev
->u
.l
.next
=uv
->u
.l
.next
;
862 static void luaF_freeupval(lua_State
*L
,UpVal
*uv
){
863 if(uv
->v
!=&uv
->u
.value
)
867 static void luaF_close(lua_State
*L
,StkId level
){
870 while(L
->openupval
!=NULL
&&(uv
=ngcotouv(L
->openupval
))->v
>=level
){
871 GCObject
*o
=obj2gco(uv
);
872 L
->openupval
=uv
->next
;
874 luaF_freeupval(L
,uv
);
877 setobj(L
,&uv
->u
.value
,uv
->v
);
879 luaC_linkupval(L
,uv
);
883 static Proto
*luaF_newproto(lua_State
*L
){
884 Proto
*f
=luaM_new(L
,Proto
);
885 luaC_link(L
,obj2gco(f
),(8+1));
903 f
->lastlinedefined
=0;
907 static void luaF_freeproto(lua_State
*L
,Proto
*f
){
908 luaM_freearray(L
,f
->code
,f
->sizecode
,Instruction
);
909 luaM_freearray(L
,f
->p
,f
->sizep
,Proto
*);
910 luaM_freearray(L
,f
->k
,f
->sizek
,TValue
);
911 luaM_freearray(L
,f
->lineinfo
,f
->sizelineinfo
,int);
912 luaM_freearray(L
,f
->locvars
,f
->sizelocvars
,struct LocVar
);
913 luaM_freearray(L
,f
->upvalues
,f
->sizeupvalues
,TString
*);
916 static void luaF_freeclosure(lua_State
*L
,Closure
*c
){
917 int size
=(c
->c
.isC
)?sizeCclosure(c
->c
.nupvalues
):
918 sizeLclosure(c
->l
.nupvalues
);
919 luaM_freemem(L
,c
,size
);
921 #define MASK1(n,p)((~((~(Instruction)0)<<n))<<p)
922 #define MASK0(n,p)(~MASK1(n,p))
923 #define GET_OPCODE(i)(cast(OpCode,((i)>>0)&MASK1(6,0)))
924 #define SET_OPCODE(i,o)((i)=(((i)&MASK0(6,0))|((cast(Instruction,o)<<0)&MASK1(6,0))))
925 #define GETARG_A(i)(cast(int,((i)>>(0+6))&MASK1(8,0)))
926 #define SETARG_A(i,u)((i)=(((i)&MASK0(8,(0+6)))|((cast(Instruction,u)<<(0+6))&MASK1(8,(0+6)))))
927 #define GETARG_B(i)(cast(int,((i)>>(((0+6)+8)+9))&MASK1(9,0)))
928 #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)))))
929 #define GETARG_C(i)(cast(int,((i)>>((0+6)+8))&MASK1(9,0)))
930 #define SETARG_C(i,b)((i)=(((i)&MASK0(9,((0+6)+8)))|((cast(Instruction,b)<<((0+6)+8))&MASK1(9,((0+6)+8)))))
931 #define GETARG_Bx(i)(cast(int,((i)>>((0+6)+8))&MASK1((9+9),0)))
932 #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)))))
933 #define GETARG_sBx(i)(GETARG_Bx(i)-(((1<<(9+9))-1)>>1))
934 #define SETARG_sBx(i,b)SETARG_Bx((i),cast(unsigned int,(b)+(((1<<(9+9))-1)>>1)))
935 #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)))
936 #define CREATE_ABx(o,a,bc)((cast(Instruction,o)<<0)|(cast(Instruction,a)<<(0+6))|(cast(Instruction,bc)<<((0+6)+8)))
937 #define ISK(x)((x)&(1<<(9-1)))
938 #define INDEXK(r)((int)(r)&~(1<<(9-1)))
939 #define RKASK(x)((x)|(1<<(9-1)))
940 static const lu_byte luaP_opmodes
[(cast(int,OP_VARARG
)+1)];
941 #define getBMode(m)(cast(enum OpArgMask,(luaP_opmodes[m]>>4)&3))
942 #define getCMode(m)(cast(enum OpArgMask,(luaP_opmodes[m]>>2)&3))
943 #define testTMode(m)(luaP_opmodes[m]&(1<<7))
944 typedef struct expdesc
{
947 struct{int info
,aux
;}s
;
953 typedef struct upvaldesc
{
958 typedef struct FuncState
{
961 struct FuncState
*prev
;
973 upvaldesc upvalues
[60];
974 unsigned short actvar
[200];
976 static Proto
*luaY_parser(lua_State
*L
,ZIO
*z
,Mbuffer
*buff
,
979 struct lua_longjmp
*previous
;
983 static void luaD_seterrorobj(lua_State
*L
,int errcode
,StkId oldtop
){
986 setsvalue(L
,oldtop
,luaS_newliteral(L
,"not enough memory"));
990 setsvalue(L
,oldtop
,luaS_newliteral(L
,"error in error handling"));
995 setobj(L
,oldtop
,L
->top
-1);
1001 static void restore_stack_limit(lua_State
*L
){
1002 if(L
->size_ci
>20000){
1003 int inuse
=cast_int(L
->ci
-L
->base_ci
);
1005 luaD_reallocCI(L
,20000);
1008 static void resetstack(lua_State
*L
,int status
){
1010 L
->base
=L
->ci
->base
;
1011 luaF_close(L
,L
->base
);
1012 luaD_seterrorobj(L
,status
,L
->base
);
1013 L
->nCcalls
=L
->baseCcalls
;
1015 restore_stack_limit(L
);
1019 static void luaD_throw(lua_State
*L
,int errcode
){
1021 L
->errorJmp
->status
=errcode
;
1022 LUAI_THROW(L
,L
->errorJmp
);
1025 L
->status
=cast_byte(errcode
);
1027 resetstack(L
,errcode
);
1033 static int luaD_rawrunprotected(lua_State
*L
,Pfunc f
,void*ud
){
1034 struct lua_longjmp lj
;
1036 lj
.previous
=L
->errorJmp
;
1041 L
->errorJmp
=lj
.previous
;
1044 static void correctstack(lua_State
*L
,TValue
*oldstack
){
1047 L
->top
=(L
->top
-oldstack
)+L
->stack
;
1048 for(up
=L
->openupval
;up
!=NULL
;up
=up
->gch
.next
)
1049 gco2uv(up
)->v
=(gco2uv(up
)->v
-oldstack
)+L
->stack
;
1050 for(ci
=L
->base_ci
;ci
<=L
->ci
;ci
++){
1051 ci
->top
=(ci
->top
-oldstack
)+L
->stack
;
1052 ci
->base
=(ci
->base
-oldstack
)+L
->stack
;
1053 ci
->func
=(ci
->func
-oldstack
)+L
->stack
;
1055 L
->base
=(L
->base
-oldstack
)+L
->stack
;
1057 static void luaD_reallocstack(lua_State
*L
,int newsize
){
1058 TValue
*oldstack
=L
->stack
;
1059 int realsize
=newsize
+1+5;
1060 luaM_reallocvector(L
,L
->stack
,L
->stacksize
,realsize
,TValue
);
1061 L
->stacksize
=realsize
;
1062 L
->stack_last
=L
->stack
+newsize
;
1063 correctstack(L
,oldstack
);
1065 static void luaD_reallocCI(lua_State
*L
,int newsize
){
1066 CallInfo
*oldci
=L
->base_ci
;
1067 luaM_reallocvector(L
,L
->base_ci
,L
->size_ci
,newsize
,CallInfo
);
1069 L
->ci
=(L
->ci
-oldci
)+L
->base_ci
;
1070 L
->end_ci
=L
->base_ci
+L
->size_ci
-1;
1072 static void luaD_growstack(lua_State
*L
,int n
){
1074 luaD_reallocstack(L
,2*L
->stacksize
);
1076 luaD_reallocstack(L
,L
->stacksize
+n
);
1078 static CallInfo
*growCI(lua_State
*L
){
1079 if(L
->size_ci
>20000)
1082 luaD_reallocCI(L
,2*L
->size_ci
);
1083 if(L
->size_ci
>20000)
1084 luaG_runerror(L
,"stack overflow");
1088 static StkId
adjust_varargs(lua_State
*L
,Proto
*p
,int actual
){
1090 int nfixargs
=p
->numparams
;
1093 for(;actual
<nfixargs
;++actual
)
1094 setnilvalue(L
->top
++);
1095 fixed
=L
->top
-actual
;
1097 for(i
=0;i
<nfixargs
;i
++){
1098 setobj(L
,L
->top
++,fixed
+i
);
1099 setnilvalue(fixed
+i
);
1102 sethvalue(L
,L
->top
++,htab
);
1106 static StkId
tryfuncTM(lua_State
*L
,StkId func
){
1107 const TValue
*tm
=luaT_gettmbyobj(L
,func
,TM_CALL
);
1109 ptrdiff_t funcr
=savestack(L
,func
);
1110 if(!ttisfunction(tm
))
1111 luaG_typeerror(L
,func
,"call");
1112 for(p
=L
->top
;p
>func
;p
--)setobj(L
,p
,p
-1);
1114 func
=restorestack(L
,funcr
);
1118 #define inc_ci(L)((L->ci==L->end_ci)?growCI(L):(condhardstacktests(luaD_reallocCI(L,L->size_ci)),++L->ci))
1119 static int luaD_precall(lua_State
*L
,StkId func
,int nresults
){
1122 if(!ttisfunction(func
))
1123 func
=tryfuncTM(L
,func
);
1124 funcr
=savestack(L
,func
);
1125 cl
=&clvalue(func
)->l
;
1126 L
->ci
->savedpc
=L
->savedpc
;
1131 luaD_checkstack(L
,p
->maxstacksize
);
1132 func
=restorestack(L
,funcr
);
1135 if(L
->top
>base
+p
->numparams
)
1136 L
->top
=base
+p
->numparams
;
1139 int nargs
=cast_int(L
->top
-func
)-1;
1140 base
=adjust_varargs(L
,p
,nargs
);
1141 func
=restorestack(L
,funcr
);
1145 L
->base
=ci
->base
=base
;
1146 ci
->top
=L
->base
+p
->maxstacksize
;
1149 ci
->nresults
=nresults
;
1150 for(st
=L
->top
;st
<ci
->top
;st
++)
1158 luaD_checkstack(L
,20);
1160 ci
->func
=restorestack(L
,funcr
);
1161 L
->base
=ci
->base
=ci
->func
+1;
1163 ci
->nresults
=nresults
;
1164 n
=(*curr_func(L
)->c
.f
)(L
);
1168 luaD_poscall(L
,L
->top
-n
);
1173 static int luaD_poscall(lua_State
*L
,StkId firstResult
){
1179 wanted
=ci
->nresults
;
1180 L
->base
=(ci
-1)->base
;
1181 L
->savedpc
=(ci
-1)->savedpc
;
1182 for(i
=wanted
;i
!=0&&firstResult
<L
->top
;i
--)
1183 setobj(L
,res
++,firstResult
++);
1187 return(wanted
-(-1));
1189 static void luaD_call(lua_State
*L
,StkId func
,int nResults
){
1190 if(++L
->nCcalls
>=200){
1192 luaG_runerror(L
,"C stack overflow");
1193 else if(L
->nCcalls
>=(200+(200>>3)))
1196 if(luaD_precall(L
,func
,nResults
)==0)
1201 static int luaD_pcall(lua_State
*L
,Pfunc func
,void*u
,
1202 ptrdiff_t old_top
,ptrdiff_t ef
){
1204 unsigned short oldnCcalls
=L
->nCcalls
;
1205 ptrdiff_t old_ci
=saveci(L
,L
->ci
);
1206 lu_byte old_allowhooks
=L
->allowhook
;
1207 ptrdiff_t old_errfunc
=L
->errfunc
;
1209 status
=luaD_rawrunprotected(L
,func
,u
);
1211 StkId oldtop
=restorestack(L
,old_top
);
1212 luaF_close(L
,oldtop
);
1213 luaD_seterrorobj(L
,status
,oldtop
);
1214 L
->nCcalls
=oldnCcalls
;
1215 L
->ci
=restoreci(L
,old_ci
);
1216 L
->base
=L
->ci
->base
;
1217 L
->savedpc
=L
->ci
->savedpc
;
1218 L
->allowhook
=old_allowhooks
;
1219 restore_stack_limit(L
);
1221 L
->errfunc
=old_errfunc
;
1229 static void f_parser(lua_State
*L
,void*ud
){
1233 struct SParser
*p
=cast(struct SParser
*,ud
);
1235 tf
=luaY_parser(L
,p
->z
,
1237 cl
=luaF_newLclosure(L
,tf
->nups
,hvalue(gt(L
)));
1239 for(i
=0;i
<tf
->nups
;i
++)
1240 cl
->l
.upvals
[i
]=luaF_newupval(L
);
1241 setclvalue(L
,L
->top
,cl
);
1244 static int luaD_protectedparser(lua_State
*L
,ZIO
*z
,const char*name
){
1248 luaZ_initbuffer(L
,&p
.buff
);
1249 status
=luaD_pcall(L
,f_parser
,&p
,savestack(L
,L
->top
),L
->errfunc
);
1250 luaZ_freebuffer(L
,&p
.buff
);
1253 static void luaS_resize(lua_State
*L
,int newsize
){
1257 if(G(L
)->gcstate
==2)
1259 newhash
=luaM_newvector(L
,newsize
,GCObject
*);
1261 for(i
=0;i
<newsize
;i
++)newhash
[i
]=NULL
;
1262 for(i
=0;i
<tb
->size
;i
++){
1263 GCObject
*p
=tb
->hash
[i
];
1265 GCObject
*next
=p
->gch
.next
;
1266 unsigned int h
=gco2ts(p
)->hash
;
1267 int h1
=lmod(h
,newsize
);
1268 p
->gch
.next
=newhash
[h1
];
1273 luaM_freearray(L
,tb
->hash
,tb
->size
,TString
*);
1277 static TString
*newlstr(lua_State
*L
,const char*str
,size_t l
,
1281 if(l
+1>(((size_t)(~(size_t)0)-2)-sizeof(TString
))/sizeof(char))
1283 ts
=cast(TString
*,luaM_malloc(L
,(l
+1)*sizeof(char)+sizeof(TString
)));
1286 ts
->tsv
.marked
=luaC_white(G(L
));
1289 memcpy(ts
+1,str
,l
*sizeof(char));
1290 ((char*)(ts
+1))[l
]='\0';
1293 ts
->tsv
.next
=tb
->hash
[h
];
1294 tb
->hash
[h
]=obj2gco(ts
);
1296 if(tb
->nuse
>cast(lu_int32
,tb
->size
)&&tb
->size
<=(INT_MAX
-2)/2)
1297 luaS_resize(L
,tb
->size
*2);
1300 static TString
*luaS_newlstr(lua_State
*L
,const char*str
,size_t l
){
1302 unsigned int h
=cast(unsigned int,l
);
1303 size_t step
=(l
>>5)+1;
1305 for(l1
=l
;l1
>=step
;l1
-=step
)
1306 h
=h
^((h
<<5)+(h
>>2)+cast(unsigned char,str
[l1
-1]));
1307 for(o
=G(L
)->strt
.hash
[lmod(h
,G(L
)->strt
.size
)];
1310 TString
*ts
=rawgco2ts(o
);
1311 if(ts
->tsv
.len
==l
&&(memcmp(str
,getstr(ts
),l
)==0)){
1312 if(isdead(G(L
),o
))changewhite(o
);
1316 return newlstr(L
,str
,l
,h
);
1318 static Udata
*luaS_newudata(lua_State
*L
,size_t s
,Table
*e
){
1320 if(s
>((size_t)(~(size_t)0)-2)-sizeof(Udata
))
1322 u
=cast(Udata
*,luaM_malloc(L
,s
+sizeof(Udata
)));
1323 u
->uv
.marked
=luaC_white(G(L
));
1326 u
->uv
.metatable
=NULL
;
1328 u
->uv
.next
=G(L
)->mainthread
->next
;
1329 G(L
)->mainthread
->next
=obj2gco(u
);
1332 #define hashpow2(t,n)(gnode(t,lmod((n),sizenode(t))))
1333 #define hashstr(t,str)hashpow2(t,(str)->tsv.hash)
1334 #define hashboolean(t,p)hashpow2(t,p)
1335 #define hashmod(t,n)(gnode(t,((n)%((sizenode(t)-1)|1))))
1336 #define hashpointer(t,p)hashmod(t,IntPoint(p))
1337 static const Node dummynode_
={
1341 static Node
*hashnum(const Table
*t
,lua_Number n
){
1342 unsigned int a
[cast_int(sizeof(lua_Number
)/sizeof(int))];
1346 memcpy(a
,&n
,sizeof(a
));
1347 for(i
=1;i
<cast_int(sizeof(lua_Number
)/sizeof(int));i
++)a
[0]+=a
[i
];
1348 return hashmod(t
,a
[0]);
1350 static Node
*mainposition(const Table
*t
,const TValue
*key
){
1353 return hashnum(t
,nvalue(key
));
1355 return hashstr(t
,rawtsvalue(key
));
1357 return hashboolean(t
,bvalue(key
));
1359 return hashpointer(t
,pvalue(key
));
1361 return hashpointer(t
,gcvalue(key
));
1364 static int arrayindex(const TValue
*key
){
1365 if(ttisnumber(key
)){
1366 lua_Number n
=nvalue(key
);
1368 lua_number2int(k
,n
);
1369 if(luai_numeq(cast_num(k
),n
))
1374 static int findindex(lua_State
*L
,Table
*t
,StkId key
){
1376 if(ttisnil(key
))return-1;
1378 if(0<i
&&i
<=t
->sizearray
)
1381 Node
*n
=mainposition(t
,key
);
1383 if(luaO_rawequalObj(key2tval(n
),key
)||
1384 (ttype(gkey(n
))==(8+3)&&iscollectable(key
)&&
1385 gcvalue(gkey(n
))==gcvalue(key
))){
1386 i
=cast_int(n
-gnode(t
,0));
1387 return i
+t
->sizearray
;
1391 luaG_runerror(L
,"invalid key to "LUA_QL("next"));
1395 static int luaH_next(lua_State
*L
,Table
*t
,StkId key
){
1396 int i
=findindex(L
,t
,key
);
1397 for(i
++;i
<t
->sizearray
;i
++){
1398 if(!ttisnil(&t
->array
[i
])){
1399 setnvalue(key
,cast_num(i
+1));
1400 setobj(L
,key
+1,&t
->array
[i
]);
1404 for(i
-=t
->sizearray
;i
<(int)sizenode(t
);i
++){
1405 if(!ttisnil(gval(gnode(t
,i
)))){
1406 setobj(L
,key
,key2tval(gnode(t
,i
)));
1407 setobj(L
,key
+1,gval(gnode(t
,i
)));
1413 static int computesizes(int nums
[],int*narray
){
1419 for(i
=0,twotoi
=1;twotoi
/2<*narray
;i
++,twotoi
*=2){
1427 if(a
==*narray
)break;
1432 static int countint(const TValue
*key
,int*nums
){
1433 int k
=arrayindex(key
);
1434 if(0<k
&&k
<=(1<<(32-2))){
1435 nums
[ceillog2(k
)]++;
1441 static int numusearray(const Table
*t
,int*nums
){
1446 for(lg
=0,ttlg
=1;lg
<=(32-2);lg
++,ttlg
*=2){
1449 if(lim
>t
->sizearray
){
1455 if(!ttisnil(&t
->array
[i
-1]))
1463 static int numusehash(const Table
*t
,int*nums
,int*pnasize
){
1469 if(!ttisnil(gval(n
))){
1470 ause
+=countint(key2tval(n
),nums
);
1477 static void setarrayvector(lua_State
*L
,Table
*t
,int size
){
1479 luaM_reallocvector(L
,t
->array
,t
->sizearray
,size
,TValue
);
1480 for(i
=t
->sizearray
;i
<size
;i
++)
1481 setnilvalue(&t
->array
[i
]);
1484 static void setnodevector(lua_State
*L
,Table
*t
,int size
){
1487 t
->node
=cast(Node
*,(&dummynode_
));
1492 lsize
=ceillog2(size
);
1494 luaG_runerror(L
,"table overflow");
1496 t
->node
=luaM_newvector(L
,size
,Node
);
1497 for(i
=0;i
<size
;i
++){
1500 setnilvalue(gkey(n
));
1501 setnilvalue(gval(n
));
1504 t
->lsizenode
=cast_byte(lsize
);
1505 t
->lastfree
=gnode(t
,size
);
1507 static void resize(lua_State
*L
,Table
*t
,int nasize
,int nhsize
){
1509 int oldasize
=t
->sizearray
;
1510 int oldhsize
=t
->lsizenode
;
1513 setarrayvector(L
,t
,nasize
);
1514 setnodevector(L
,t
,nhsize
);
1515 if(nasize
<oldasize
){
1516 t
->sizearray
=nasize
;
1517 for(i
=nasize
;i
<oldasize
;i
++){
1518 if(!ttisnil(&t
->array
[i
]))
1519 setobj(L
,luaH_setnum(L
,t
,i
+1),&t
->array
[i
]);
1521 luaM_reallocvector(L
,t
->array
,oldasize
,nasize
,TValue
);
1523 for(i
=twoto(oldhsize
)-1;i
>=0;i
--){
1525 if(!ttisnil(gval(old
)))
1526 setobj(L
,luaH_set(L
,t
,key2tval(old
)),gval(old
));
1528 if(nold
!=(&dummynode_
))
1529 luaM_freearray(L
,nold
,twoto(oldhsize
),Node
);
1531 static void luaH_resizearray(lua_State
*L
,Table
*t
,int nasize
){
1532 int nsize
=(t
->node
==(&dummynode_
))?0:sizenode(t
);
1533 resize(L
,t
,nasize
,nsize
);
1535 static void rehash(lua_State
*L
,Table
*t
,const TValue
*ek
){
1540 for(i
=0;i
<=(32-2);i
++)nums
[i
]=0;
1541 nasize
=numusearray(t
,nums
);
1543 totaluse
+=numusehash(t
,nums
,&nasize
);
1544 nasize
+=countint(ek
,nums
);
1546 na
=computesizes(nums
,&nasize
);
1547 resize(L
,t
,nasize
,totaluse
-na
);
1549 static Table
*luaH_new(lua_State
*L
,int narray
,int nhash
){
1550 Table
*t
=luaM_new(L
,Table
);
1551 luaC_link(L
,obj2gco(t
),5);
1553 t
->flags
=cast_byte(~0);
1557 t
->node
=cast(Node
*,(&dummynode_
));
1558 setarrayvector(L
,t
,narray
);
1559 setnodevector(L
,t
,nhash
);
1562 static void luaH_free(lua_State
*L
,Table
*t
){
1563 if(t
->node
!=(&dummynode_
))
1564 luaM_freearray(L
,t
->node
,sizenode(t
),Node
);
1565 luaM_freearray(L
,t
->array
,t
->sizearray
,TValue
);
1568 static Node
*getfreepos(Table
*t
){
1569 while(t
->lastfree
-->t
->node
){
1570 if(ttisnil(gkey(t
->lastfree
)))
1575 static TValue
*newkey(lua_State
*L
,Table
*t
,const TValue
*key
){
1576 Node
*mp
=mainposition(t
,key
);
1577 if(!ttisnil(gval(mp
))||mp
==(&dummynode_
)){
1579 Node
*n
=getfreepos(t
);
1582 return luaH_set(L
,t
,key
);
1584 othern
=mainposition(t
,key2tval(mp
));
1586 while(gnext(othern
)!=mp
)othern
=gnext(othern
);
1590 setnilvalue(gval(mp
));
1598 gkey(mp
)->value
=key
->value
;gkey(mp
)->tt
=key
->tt
;
1599 luaC_barriert(L
,t
,key
);
1602 static const TValue
*luaH_getnum(Table
*t
,int key
){
1603 if(cast(unsigned int,key
-1)<cast(unsigned int,t
->sizearray
))
1604 return&t
->array
[key
-1];
1606 lua_Number nk
=cast_num(key
);
1607 Node
*n
=hashnum(t
,nk
);
1609 if(ttisnumber(gkey(n
))&&luai_numeq(nvalue(gkey(n
)),nk
))
1613 return(&luaO_nilobject_
);
1616 static const TValue
*luaH_getstr(Table
*t
,TString
*key
){
1617 Node
*n
=hashstr(t
,key
);
1619 if(ttisstring(gkey(n
))&&rawtsvalue(gkey(n
))==key
)
1623 return(&luaO_nilobject_
);
1625 static const TValue
*luaH_get(Table
*t
,const TValue
*key
){
1627 case 0:return(&luaO_nilobject_
);
1628 case 4:return luaH_getstr(t
,rawtsvalue(key
));
1631 lua_Number n
=nvalue(key
);
1632 lua_number2int(k
,n
);
1633 if(luai_numeq(cast_num(k
),nvalue(key
)))
1634 return luaH_getnum(t
,k
);
1637 Node
*n
=mainposition(t
,key
);
1639 if(luaO_rawequalObj(key2tval(n
),key
))
1643 return(&luaO_nilobject_
);
1647 static TValue
*luaH_set(lua_State
*L
,Table
*t
,const TValue
*key
){
1648 const TValue
*p
=luaH_get(t
,key
);
1650 if(p
!=(&luaO_nilobject_
))
1651 return cast(TValue
*,p
);
1653 if(ttisnil(key
))luaG_runerror(L
,"table index is nil");
1654 else if(ttisnumber(key
)&&luai_numisnan(nvalue(key
)))
1655 luaG_runerror(L
,"table index is NaN");
1656 return newkey(L
,t
,key
);
1659 static TValue
*luaH_setnum(lua_State
*L
,Table
*t
,int key
){
1660 const TValue
*p
=luaH_getnum(t
,key
);
1661 if(p
!=(&luaO_nilobject_
))
1662 return cast(TValue
*,p
);
1665 setnvalue(&k
,cast_num(key
));
1666 return newkey(L
,t
,&k
);
1669 static TValue
*luaH_setstr(lua_State
*L
,Table
*t
,TString
*key
){
1670 const TValue
*p
=luaH_getstr(t
,key
);
1671 if(p
!=(&luaO_nilobject_
))
1672 return cast(TValue
*,p
);
1675 setsvalue(L
,&k
,key
);
1676 return newkey(L
,t
,&k
);
1679 static int unbound_search(Table
*t
,unsigned int j
){
1682 while(!ttisnil(luaH_getnum(t
,j
))){
1685 if(j
>cast(unsigned int,(INT_MAX
-2))){
1687 while(!ttisnil(luaH_getnum(t
,i
)))i
++;
1692 unsigned int m
=(i
+j
)/2;
1693 if(ttisnil(luaH_getnum(t
,m
)))j
=m
;
1698 static int luaH_getn(Table
*t
){
1699 unsigned int j
=t
->sizearray
;
1700 if(j
>0&&ttisnil(&t
->array
[j
-1])){
1703 unsigned int m
=(i
+j
)/2;
1704 if(ttisnil(&t
->array
[m
-1]))j
=m
;
1709 else if(t
->node
==(&dummynode_
))
1711 else return unbound_search(t
,j
);
1713 #define makewhite(g,x)((x)->gch.marked=cast_byte(((x)->gch.marked&cast_byte(~(bitmask(2)|bit2mask(0,1))))|luaC_white(g)))
1714 #define white2gray(x)reset2bits((x)->gch.marked,0,1)
1715 #define black2gray(x)resetbit((x)->gch.marked,2)
1716 #define stringmark(s)reset2bits((s)->tsv.marked,0,1)
1717 #define isfinalized(u)testbit((u)->marked,3)
1718 #define markfinalized(u)l_setbit((u)->marked,3)
1719 #define markvalue(g,o){checkconsistency(o);if(iscollectable(o)&&iswhite(gcvalue(o)))reallymarkobject(g,gcvalue(o));}
1720 #define markobject(g,t){if(iswhite(obj2gco(t)))reallymarkobject(g,obj2gco(t));}
1721 #define setthreshold(g)(g->GCthreshold=(g->estimate/100)*g->gcpause)
1722 static void removeentry(Node
*n
){
1723 if(iscollectable(gkey(n
)))
1724 setttype(gkey(n
),(8+3));
1726 static void reallymarkobject(global_State
*g
,GCObject
*o
){
1733 Table
*mt
=gco2u(o
)->metatable
;
1735 if(mt
)markobject(g
,mt
);
1736 markobject(g
,gco2u(o
)->env
);
1742 if(uv
->v
==&uv
->u
.value
)
1747 gco2cl(o
)->c
.gclist
=g
->gray
;
1752 gco2h(o
)->gclist
=g
->gray
;
1757 gco2th(o
)->gclist
=g
->gray
;
1762 gco2p(o
)->gclist
=g
->gray
;
1769 static void marktmu(global_State
*g
){
1770 GCObject
*u
=g
->tmudata
;
1775 reallymarkobject(g
,u
);
1776 }while(u
!=g
->tmudata
);
1779 static size_t luaC_separateudata(lua_State
*L
,int all
){
1780 global_State
*g
=G(L
);
1782 GCObject
**p
=&g
->mainthread
->next
;
1784 while((curr
=*p
)!=NULL
){
1785 if(!(iswhite(curr
)||all
)||isfinalized(gco2u(curr
)))
1787 else if(fasttm(L
,gco2u(curr
)->metatable
,TM_GC
)==NULL
){
1788 markfinalized(gco2u(curr
));
1792 deadmem
+=sizeudata(gco2u(curr
));
1793 markfinalized(gco2u(curr
));
1795 if(g
->tmudata
==NULL
)
1796 g
->tmudata
=curr
->gch
.next
=curr
;
1798 curr
->gch
.next
=g
->tmudata
->gch
.next
;
1799 g
->tmudata
->gch
.next
=curr
;
1806 static int traversetable(global_State
*g
,Table
*h
){
1812 markobject(g
,h
->metatable
);
1813 mode
=gfasttm(g
,h
->metatable
,TM_MODE
);
1814 if(mode
&&ttisstring(mode
)){
1815 weakkey
=(strchr(svalue(mode
),'k')!=NULL
);
1816 weakvalue
=(strchr(svalue(mode
),'v')!=NULL
);
1817 if(weakkey
||weakvalue
){
1818 h
->marked
&=~(bitmask(3)|bitmask(4));
1819 h
->marked
|=cast_byte((weakkey
<<3)|
1825 if(weakkey
&&weakvalue
)return 1;
1829 markvalue(g
,&h
->array
[i
]);
1834 if(ttisnil(gval(n
)))
1837 if(!weakkey
)markvalue(g
,gkey(n
));
1838 if(!weakvalue
)markvalue(g
,gval(n
));
1841 return weakkey
||weakvalue
;
1843 static void traverseproto(global_State
*g
,Proto
*f
){
1845 if(f
->source
)stringmark(f
->source
);
1846 for(i
=0;i
<f
->sizek
;i
++)
1847 markvalue(g
,&f
->k
[i
]);
1848 for(i
=0;i
<f
->sizeupvalues
;i
++){
1850 stringmark(f
->upvalues
[i
]);
1852 for(i
=0;i
<f
->sizep
;i
++){
1854 markobject(g
,f
->p
[i
]);
1856 for(i
=0;i
<f
->sizelocvars
;i
++){
1857 if(f
->locvars
[i
].varname
)
1858 stringmark(f
->locvars
[i
].varname
);
1861 static void traverseclosure(global_State
*g
,Closure
*cl
){
1862 markobject(g
,cl
->c
.env
);
1865 for(i
=0;i
<cl
->c
.nupvalues
;i
++)
1866 markvalue(g
,&cl
->c
.upvalue
[i
]);
1870 markobject(g
,cl
->l
.p
);
1871 for(i
=0;i
<cl
->l
.nupvalues
;i
++)
1872 markobject(g
,cl
->l
.upvals
[i
]);
1875 static void checkstacksizes(lua_State
*L
,StkId max
){
1876 int ci_used
=cast_int(L
->ci
-L
->base_ci
);
1877 int s_used
=cast_int(max
-L
->stack
);
1878 if(L
->size_ci
>20000)
1880 if(4*ci_used
<L
->size_ci
&&2*8<L
->size_ci
)
1881 luaD_reallocCI(L
,L
->size_ci
/2);
1882 condhardstacktests(luaD_reallocCI(L
,ci_used
+1));
1883 if(4*s_used
<L
->stacksize
&&
1884 2*((2*20)+5)<L
->stacksize
)
1885 luaD_reallocstack(L
,L
->stacksize
/2);
1886 condhardstacktests(luaD_reallocstack(L
,s_used
));
1888 static void traversestack(global_State
*g
,lua_State
*l
){
1893 for(ci
=l
->base_ci
;ci
<=l
->ci
;ci
++){
1894 if(lim
<ci
->top
)lim
=ci
->top
;
1896 for(o
=l
->stack
;o
<l
->top
;o
++)
1900 checkstacksizes(l
,lim
);
1902 static l_mem
propagatemark(global_State
*g
){
1909 if(traversetable(g
,h
))
1911 return sizeof(Table
)+sizeof(TValue
)*h
->sizearray
+
1912 sizeof(Node
)*sizenode(h
);
1915 Closure
*cl
=gco2cl(o
);
1916 g
->gray
=cl
->c
.gclist
;
1917 traverseclosure(g
,cl
);
1918 return(cl
->c
.isC
)?sizeCclosure(cl
->c
.nupvalues
):
1919 sizeLclosure(cl
->l
.nupvalues
);
1922 lua_State
*th
=gco2th(o
);
1924 th
->gclist
=g
->grayagain
;
1927 traversestack(g
,th
);
1928 return sizeof(lua_State
)+sizeof(TValue
)*th
->stacksize
+
1929 sizeof(CallInfo
)*th
->size_ci
;
1935 return sizeof(Proto
)+sizeof(Instruction
)*p
->sizecode
+
1936 sizeof(Proto
*)*p
->sizep
+
1937 sizeof(TValue
)*p
->sizek
+
1938 sizeof(int)*p
->sizelineinfo
+
1939 sizeof(LocVar
)*p
->sizelocvars
+
1940 sizeof(TString
*)*p
->sizeupvalues
;
1945 static size_t propagateall(global_State
*g
){
1947 while(g
->gray
)m
+=propagatemark(g
);
1950 static int iscleared(const TValue
*o
,int iskey
){
1951 if(!iscollectable(o
))return 0;
1953 stringmark(rawtsvalue(o
));
1956 return iswhite(gcvalue(o
))||
1957 (ttisuserdata(o
)&&(!iskey
&&isfinalized(uvalue(o
))));
1959 static void cleartable(GCObject
*l
){
1963 if(testbit(h
->marked
,4)){
1965 TValue
*o
=&h
->array
[i
];
1973 if(!ttisnil(gval(n
))&&
1974 (iscleared(key2tval(n
),1)||iscleared(gval(n
),0))){
1975 setnilvalue(gval(n
));
1982 static void freeobj(lua_State
*L
,GCObject
*o
){
1984 case(8+1):luaF_freeproto(L
,gco2p(o
));break;
1985 case 6:luaF_freeclosure(L
,gco2cl(o
));break;
1986 case(8+2):luaF_freeupval(L
,gco2uv(o
));break;
1987 case 5:luaH_free(L
,gco2h(o
));break;
1989 luaE_freethread(L
,gco2th(o
));
1994 luaM_freemem(L
,o
,sizestring(gco2ts(o
)));
1998 luaM_freemem(L
,o
,sizeudata(gco2u(o
)));
2004 #define sweepwholelist(L,p)sweeplist(L,p,((lu_mem)(~(lu_mem)0)-2))
2005 static GCObject
**sweeplist(lua_State
*L
,GCObject
**p
,lu_mem count
){
2007 global_State
*g
=G(L
);
2008 int deadmask
=otherwhite(g
);
2009 while((curr
=*p
)!=NULL
&&count
-->0){
2011 sweepwholelist(L
,&gco2th(curr
)->openupval
);
2012 if((curr
->gch
.marked
^bit2mask(0,1))&deadmask
){
2019 g
->rootgc
=curr
->gch
.next
;
2025 static void checkSizes(lua_State
*L
){
2026 global_State
*g
=G(L
);
2027 if(g
->strt
.nuse
<cast(lu_int32
,g
->strt
.size
/4)&&
2029 luaS_resize(L
,g
->strt
.size
/2);
2030 if(luaZ_sizebuffer(&g
->buff
)>32*2){
2031 size_t newsize
=luaZ_sizebuffer(&g
->buff
)/2;
2032 luaZ_resizebuffer(L
,&g
->buff
,newsize
);
2035 static void GCTM(lua_State
*L
){
2036 global_State
*g
=G(L
);
2037 GCObject
*o
=g
->tmudata
->gch
.next
;
2038 Udata
*udata
=rawgco2u(o
);
2043 g
->tmudata
->gch
.next
=udata
->uv
.next
;
2044 udata
->uv
.next
=g
->mainthread
->next
;
2045 g
->mainthread
->next
=o
;
2047 tm
=fasttm(L
,udata
->uv
.metatable
,TM_GC
);
2049 lu_byte oldah
=L
->allowhook
;
2050 lu_mem oldt
=g
->GCthreshold
;
2052 g
->GCthreshold
=2*g
->totalbytes
;
2053 setobj(L
,L
->top
,tm
);
2054 setuvalue(L
,L
->top
+1,udata
);
2056 luaD_call(L
,L
->top
-2,0);
2058 g
->GCthreshold
=oldt
;
2061 static void luaC_callGCTM(lua_State
*L
){
2062 while(G(L
)->tmudata
)
2065 static void luaC_freeall(lua_State
*L
){
2066 global_State
*g
=G(L
);
2068 g
->currentwhite
=bit2mask(0,1)|bitmask(6);
2069 sweepwholelist(L
,&g
->rootgc
);
2070 for(i
=0;i
<g
->strt
.size
;i
++)
2071 sweepwholelist(L
,&g
->strt
.hash
[i
]);
2073 static void markmt(global_State
*g
){
2075 for(i
=0;i
<(8+1);i
++)
2076 if(g
->mt
[i
])markobject(g
,g
->mt
[i
]);
2078 static void markroot(lua_State
*L
){
2079 global_State
*g
=G(L
);
2083 markobject(g
,g
->mainthread
);
2084 markvalue(g
,gt(g
->mainthread
));
2085 markvalue(g
,registry(L
));
2089 static void remarkupvals(global_State
*g
){
2091 for(uv
=g
->uvhead
.u
.l
.next
;uv
!=&g
->uvhead
;uv
=uv
->u
.l
.next
){
2092 if(isgray(obj2gco(uv
)))
2096 static void atomic(lua_State
*L
){
2097 global_State
*g
=G(L
);
2106 g
->gray
=g
->grayagain
;
2109 udsize
=luaC_separateudata(L
,0);
2111 udsize
+=propagateall(g
);
2112 cleartable(g
->weak
);
2113 g
->currentwhite
=cast_byte(otherwhite(g
));
2115 g
->sweepgc
=&g
->rootgc
;
2117 g
->estimate
=g
->totalbytes
-udsize
;
2119 static l_mem
singlestep(lua_State
*L
){
2120 global_State
*g
=G(L
);
2128 return propagatemark(g
);
2135 lu_mem old
=g
->totalbytes
;
2136 sweepwholelist(L
,&g
->strt
.hash
[g
->sweepstrgc
++]);
2137 if(g
->sweepstrgc
>=g
->strt
.size
)
2139 g
->estimate
-=old
-g
->totalbytes
;
2143 lu_mem old
=g
->totalbytes
;
2144 g
->sweepgc
=sweeplist(L
,g
->sweepgc
,40);
2145 if(*g
->sweepgc
==NULL
){
2149 g
->estimate
-=old
-g
->totalbytes
;
2168 static void luaC_step(lua_State
*L
){
2169 global_State
*g
=G(L
);
2170 l_mem lim
=(1024u/100)*g
->gcstepmul
;
2172 lim
=(((lu_mem
)(~(lu_mem
)0)-2)-1)/2;
2173 g
->gcdept
+=g
->totalbytes
-g
->GCthreshold
;
2181 g
->GCthreshold
=g
->totalbytes
+1024u;
2184 g
->GCthreshold
=g
->totalbytes
;
2191 static void luaC_barrierf(lua_State
*L
,GCObject
*o
,GCObject
*v
){
2192 global_State
*g
=G(L
);
2194 reallymarkobject(g
,v
);
2198 static void luaC_barrierback(lua_State
*L
,Table
*t
){
2199 global_State
*g
=G(L
);
2200 GCObject
*o
=obj2gco(t
);
2202 t
->gclist
=g
->grayagain
;
2205 static void luaC_link(lua_State
*L
,GCObject
*o
,lu_byte tt
){
2206 global_State
*g
=G(L
);
2207 o
->gch
.next
=g
->rootgc
;
2209 o
->gch
.marked
=luaC_white(g
);
2212 static void luaC_linkupval(lua_State
*L
,UpVal
*uv
){
2213 global_State
*g
=G(L
);
2214 GCObject
*o
=obj2gco(uv
);
2215 o
->gch
.next
=g
->rootgc
;
2220 luaC_barrier(L
,uv
,uv
->v
);
2231 typedef struct Token
{
2235 typedef struct LexState
{
2241 struct FuncState
*fs
;
2248 static void luaX_init(lua_State
*L
);
2249 static void luaX_lexerror(LexState
*ls
,const char*msg
,int token
);
2250 #define state_size(x)(sizeof(x)+0)
2251 #define fromstate(l)(cast(lu_byte*,(l))-0)
2252 #define tostate(l)(cast(lua_State*,cast(lu_byte*,l)+0))
2257 static void stack_init(lua_State
*L1
,lua_State
*L
){
2258 L1
->base_ci
=luaM_newvector(L
,8,CallInfo
);
2261 L1
->end_ci
=L1
->base_ci
+L1
->size_ci
-1;
2262 L1
->stack
=luaM_newvector(L
,(2*20)+5,TValue
);
2263 L1
->stacksize
=(2*20)+5;
2265 L1
->stack_last
=L1
->stack
+(L1
->stacksize
-5)-1;
2266 L1
->ci
->func
=L1
->top
;
2267 setnilvalue(L1
->top
++);
2268 L1
->base
=L1
->ci
->base
=L1
->top
;
2269 L1
->ci
->top
=L1
->top
+20;
2271 static void freestack(lua_State
*L
,lua_State
*L1
){
2272 luaM_freearray(L
,L1
->base_ci
,L1
->size_ci
,CallInfo
);
2273 luaM_freearray(L
,L1
->stack
,L1
->stacksize
,TValue
);
2275 static void f_luaopen(lua_State
*L
,void*ud
){
2276 global_State
*g
=G(L
);
2279 sethvalue(L
,gt(L
),luaH_new(L
,0,2));
2280 sethvalue(L
,registry(L
),luaH_new(L
,0,2));
2284 luaS_fix(luaS_newliteral(L
,"not enough memory"));
2285 g
->GCthreshold
=4*g
->totalbytes
;
2287 static void preinit_state(lua_State
*L
,global_State
*g
){
2299 L
->nCcalls
=L
->baseCcalls
=0;
2301 L
->base_ci
=L
->ci
=NULL
;
2306 static void close_state(lua_State
*L
){
2307 global_State
*g
=G(L
);
2308 luaF_close(L
,L
->stack
);
2310 luaM_freearray(L
,G(L
)->strt
.hash
,G(L
)->strt
.size
,TString
*);
2311 luaZ_freebuffer(L
,&g
->buff
);
2313 (*g
->frealloc
)(g
->ud
,fromstate(L
),state_size(LG
),0);
2315 static void luaE_freethread(lua_State
*L
,lua_State
*L1
){
2316 luaF_close(L1
,L1
->stack
);
2318 luaM_freemem(L
,fromstate(L1
),state_size(lua_State
));
2320 static lua_State
*lua_newstate(lua_Alloc f
,void*ud
){
2324 void*l
=(*f
)(ud
,NULL
,0,state_size(LG
));
2325 if(l
==NULL
)return NULL
;
2330 g
->currentwhite
=bit2mask(0,5);
2331 L
->marked
=luaC_white(g
);
2332 set2bits(L
->marked
,5,6);
2337 g
->uvhead
.u
.l
.prev
=&g
->uvhead
;
2338 g
->uvhead
.u
.l
.next
=&g
->uvhead
;
2343 setnilvalue(registry(L
));
2344 luaZ_initbuffer(L
,&g
->buff
);
2347 g
->rootgc
=obj2gco(L
);
2349 g
->sweepgc
=&g
->rootgc
;
2354 g
->totalbytes
=sizeof(LG
);
2358 for(i
=0;i
<(8+1);i
++)g
->mt
[i
]=NULL
;
2359 if(luaD_rawrunprotected(L
,f_luaopen
,NULL
)!=0){
2367 static void callallgcTM(lua_State
*L
,void*ud
){
2371 static void lua_close(lua_State
*L
){
2373 luaF_close(L
,L
->stack
);
2374 luaC_separateudata(L
,1);
2378 L
->base
=L
->top
=L
->ci
->base
;
2379 L
->nCcalls
=L
->baseCcalls
=0;
2380 }while(luaD_rawrunprotected(L
,callallgcTM
,NULL
)!=0);
2383 #define getcode(fs,e)((fs)->f->code[(e)->u.s.info])
2384 #define luaK_codeAsBx(fs,o,A,sBx)luaK_codeABx(fs,o,A,(sBx)+(((1<<(9+9))-1)>>1))
2385 #define luaK_setmultret(fs,e)luaK_setreturns(fs,e,(-1))
2386 static int luaK_codeABx(FuncState
*fs
,OpCode o
,int A
,unsigned int Bx
);
2387 static int luaK_codeABC(FuncState
*fs
,OpCode o
,int A
,int B
,int C
);
2388 static void luaK_setreturns(FuncState
*fs
,expdesc
*e
,int nresults
);
2389 static void luaK_patchtohere(FuncState
*fs
,int list
);
2390 static void luaK_concat(FuncState
*fs
,int*l1
,int l2
);
2391 static int currentpc(lua_State
*L
,CallInfo
*ci
){
2392 if(!isLua(ci
))return-1;
2394 ci
->savedpc
=L
->savedpc
;
2395 return pcRel(ci
->savedpc
,ci_func(ci
)->l
.p
);
2397 static int currentline(lua_State
*L
,CallInfo
*ci
){
2398 int pc
=currentpc(L
,ci
);
2402 return getline_(ci_func(ci
)->l
.p
,pc
);
2404 static int lua_getstack(lua_State
*L
,int level
,lua_Debug
*ar
){
2407 for(ci
=L
->ci
;level
>0&&ci
>L
->base_ci
;ci
--){
2410 level
-=ci
->tailcalls
;
2412 if(level
==0&&ci
>L
->base_ci
){
2414 ar
->i_ci
=cast_int(ci
-L
->base_ci
);
2423 static Proto
*getluaproto(CallInfo
*ci
){
2424 return(isLua(ci
)?ci_func(ci
)->l
.p
:NULL
);
2426 static void funcinfo(lua_Debug
*ar
,Closure
*cl
){
2430 ar
->lastlinedefined
=-1;
2434 ar
->source
=getstr(cl
->l
.p
->source
);
2435 ar
->linedefined
=cl
->l
.p
->linedefined
;
2436 ar
->lastlinedefined
=cl
->l
.p
->lastlinedefined
;
2437 ar
->what
=(ar
->linedefined
==0)?"main":"Lua";
2439 luaO_chunkid(ar
->short_src
,ar
->source
,60);
2441 static void info_tailcall(lua_Debug
*ar
){
2442 ar
->name
=ar
->namewhat
="";
2444 ar
->lastlinedefined
=ar
->linedefined
=ar
->currentline
=-1;
2445 ar
->source
="=(tail call)";
2446 luaO_chunkid(ar
->short_src
,ar
->source
,60);
2449 static void collectvalidlines(lua_State
*L
,Closure
*f
){
2450 if(f
==NULL
||f
->c
.isC
){
2451 setnilvalue(L
->top
);
2454 Table
*t
=luaH_new(L
,0,0);
2455 int*lineinfo
=f
->l
.p
->lineinfo
;
2457 for(i
=0;i
<f
->l
.p
->sizelineinfo
;i
++)
2458 setbvalue(luaH_setnum(L
,t
,lineinfo
[i
]),1);
2459 sethvalue(L
,L
->top
,t
);
2463 static int auxgetinfo(lua_State
*L
,const char*what
,lua_Debug
*ar
,
2464 Closure
*f
,CallInfo
*ci
){
2477 ar
->currentline
=(ci
)?currentline(L
,ci
):-1;
2481 ar
->nups
=f
->c
.nupvalues
;
2485 ar
->namewhat
=(ci
)?NULL
:NULL
;
2486 if(ar
->namewhat
==NULL
){
2500 static int lua_getinfo(lua_State
*L
,const char*what
,lua_Debug
*ar
){
2505 StkId func
=L
->top
-1;
2506 luai_apicheck(L
,ttisfunction(func
));
2511 else if(ar
->i_ci
!=0){
2512 ci
=L
->base_ci
+ar
->i_ci
;
2513 f
=clvalue(ci
->func
);
2515 status
=auxgetinfo(L
,what
,ar
,f
,ci
);
2516 if(strchr(what
,'f')){
2517 if(f
==NULL
)setnilvalue(L
->top
);
2518 else setclvalue(L
,L
->top
,f
);
2521 if(strchr(what
,'L'))
2522 collectvalidlines(L
,f
);
2525 static int isinstack(CallInfo
*ci
,const TValue
*o
){
2527 for(p
=ci
->base
;p
<ci
->top
;p
++)
2531 static void luaG_typeerror(lua_State
*L
,const TValue
*o
,const char*op
){
2532 const char*name
=NULL
;
2533 const char*t
=luaT_typenames
[ttype(o
)];
2534 const char*kind
=(isinstack(L
->ci
,o
))?
2538 luaG_runerror(L
,"attempt to %s %s "LUA_QL("%s")" (a %s value)",
2541 luaG_runerror(L
,"attempt to %s a %s value",op
,t
);
2543 static void luaG_concaterror(lua_State
*L
,StkId p1
,StkId p2
){
2544 if(ttisstring(p1
)||ttisnumber(p1
))p1
=p2
;
2545 luaG_typeerror(L
,p1
,"concatenate");
2547 static void luaG_aritherror(lua_State
*L
,const TValue
*p1
,const TValue
*p2
){
2549 if(luaV_tonumber(p1
,&temp
)==NULL
)
2551 luaG_typeerror(L
,p2
,"perform arithmetic on");
2553 static int luaG_ordererror(lua_State
*L
,const TValue
*p1
,const TValue
*p2
){
2554 const char*t1
=luaT_typenames
[ttype(p1
)];
2555 const char*t2
=luaT_typenames
[ttype(p2
)];
2557 luaG_runerror(L
,"attempt to compare two %s values",t1
);
2559 luaG_runerror(L
,"attempt to compare %s with %s",t1
,t2
);
2562 static void addinfo(lua_State
*L
,const char*msg
){
2566 int line
=currentline(L
,ci
);
2567 luaO_chunkid(buff
,getstr(getluaproto(ci
)->source
),60);
2568 luaO_pushfstring(L
,"%s:%d: %s",buff
,line
,msg
);
2571 static void luaG_errormsg(lua_State
*L
){
2573 StkId errfunc
=restorestack(L
,L
->errfunc
);
2574 if(!ttisfunction(errfunc
))luaD_throw(L
,5);
2575 setobj(L
,L
->top
,L
->top
-1);
2576 setobj(L
,L
->top
-1,errfunc
);
2578 luaD_call(L
,L
->top
-2,1);
2582 static void luaG_runerror(lua_State
*L
,const char*fmt
,...){
2585 addinfo(L
,luaO_pushvfstring(L
,fmt
,argp
));
2589 static int luaZ_fill(ZIO
*z
){
2593 buff
=z
->reader(L
,z
->data
,&size
);
2594 if(buff
==NULL
||size
==0)return(-1);
2597 return char2int(*(z
->p
++));
2599 static void luaZ_init(lua_State
*L
,ZIO
*z
,lua_Reader reader
,void*data
){
2606 static char*luaZ_openspace(lua_State
*L
,Mbuffer
*buff
,size_t n
){
2607 if(n
>buff
->buffsize
){
2609 luaZ_resizebuffer(L
,buff
,n
);
2611 return buff
->buffer
;
2613 #define opmode(t,a,b,c,m)(((t)<<7)|((a)<<6)|((b)<<4)|((c)<<2)|(m))
2614 static const lu_byte luaP_opmodes
[(cast(int,OP_VARARG
)+1)]={
2615 opmode(0,1,OpArgR
,OpArgN
,iABC
)
2616 ,opmode(0,1,OpArgK
,OpArgN
,iABx
)
2617 ,opmode(0,1,OpArgU
,OpArgU
,iABC
)
2618 ,opmode(0,1,OpArgR
,OpArgN
,iABC
)
2619 ,opmode(0,1,OpArgU
,OpArgN
,iABC
)
2620 ,opmode(0,1,OpArgK
,OpArgN
,iABx
)
2621 ,opmode(0,1,OpArgR
,OpArgK
,iABC
)
2622 ,opmode(0,0,OpArgK
,OpArgN
,iABx
)
2623 ,opmode(0,0,OpArgU
,OpArgN
,iABC
)
2624 ,opmode(0,0,OpArgK
,OpArgK
,iABC
)
2625 ,opmode(0,1,OpArgU
,OpArgU
,iABC
)
2626 ,opmode(0,1,OpArgR
,OpArgK
,iABC
)
2627 ,opmode(0,1,OpArgK
,OpArgK
,iABC
)
2628 ,opmode(0,1,OpArgK
,OpArgK
,iABC
)
2629 ,opmode(0,1,OpArgK
,OpArgK
,iABC
)
2630 ,opmode(0,1,OpArgK
,OpArgK
,iABC
)
2631 ,opmode(0,1,OpArgK
,OpArgK
,iABC
)
2632 ,opmode(0,1,OpArgK
,OpArgK
,iABC
)
2633 ,opmode(0,1,OpArgR
,OpArgN
,iABC
)
2634 ,opmode(0,1,OpArgR
,OpArgN
,iABC
)
2635 ,opmode(0,1,OpArgR
,OpArgN
,iABC
)
2636 ,opmode(0,1,OpArgR
,OpArgR
,iABC
)
2637 ,opmode(0,0,OpArgR
,OpArgN
,iAsBx
)
2638 ,opmode(1,0,OpArgK
,OpArgK
,iABC
)
2639 ,opmode(1,0,OpArgK
,OpArgK
,iABC
)
2640 ,opmode(1,0,OpArgK
,OpArgK
,iABC
)
2641 ,opmode(1,1,OpArgR
,OpArgU
,iABC
)
2642 ,opmode(1,1,OpArgR
,OpArgU
,iABC
)
2643 ,opmode(0,1,OpArgU
,OpArgU
,iABC
)
2644 ,opmode(0,1,OpArgU
,OpArgU
,iABC
)
2645 ,opmode(0,0,OpArgU
,OpArgN
,iABC
)
2646 ,opmode(0,1,OpArgR
,OpArgN
,iAsBx
)
2647 ,opmode(0,1,OpArgR
,OpArgN
,iAsBx
)
2648 ,opmode(1,0,OpArgN
,OpArgU
,iABC
)
2649 ,opmode(0,0,OpArgU
,OpArgU
,iABC
)
2650 ,opmode(0,0,OpArgN
,OpArgN
,iABC
)
2651 ,opmode(0,1,OpArgU
,OpArgN
,iABx
)
2652 ,opmode(0,1,OpArgU
,OpArgN
,iABC
)
2654 #define next(ls)(ls->current=zgetc(ls->z))
2655 #define currIsNewline(ls)(ls->current=='\n'||ls->current=='\r')
2656 static const char*const luaX_tokens
[]={
2657 "and","break","do","else","elseif",
2658 "end","false","for","function","if",
2659 "in","local","nil","not","or","repeat",
2660 "return","then","true","until","while",
2661 "..","...","==",">=","<=","~=",
2662 "<number>","<name>","<string>","<eof>",
2665 #define save_and_next(ls)(save(ls,ls->current),next(ls))
2666 static void save(LexState
*ls
,int c
){
2668 if(b
->n
+1>b
->buffsize
){
2670 if(b
->buffsize
>=((size_t)(~(size_t)0)-2)/2)
2671 luaX_lexerror(ls
,"lexical element too long",0);
2672 newsize
=b
->buffsize
*2;
2673 luaZ_resizebuffer(ls
->L
,b
,newsize
);
2675 b
->buffer
[b
->n
++]=cast(char,c
);
2677 static void luaX_init(lua_State
*L
){
2679 for(i
=0;i
<(cast(int,TK_WHILE
-257+1));i
++){
2680 TString
*ts
=luaS_new(L
,luaX_tokens
[i
]);
2682 ts
->tsv
.reserved
=cast_byte(i
+1);
2685 static const char*luaX_token2str(LexState
*ls
,int token
){
2687 return(iscntrl(token
))?luaO_pushfstring(ls
->L
,"char(%d)",token
):
2688 luaO_pushfstring(ls
->L
,"%c",token
);
2691 return luaX_tokens
[token
-257];
2693 static const char*txtToken(LexState
*ls
,int token
){
2699 return luaZ_buffer(ls
->buff
);
2701 return luaX_token2str(ls
,token
);
2704 static void luaX_lexerror(LexState
*ls
,const char*msg
,int token
){
2706 luaO_chunkid(buff
,getstr(ls
->source
),80);
2707 msg
=luaO_pushfstring(ls
->L
,"%s:%d: %s",buff
,ls
->linenumber
,msg
);
2709 luaO_pushfstring(ls
->L
,"%s near "LUA_QL("%s"),msg
,txtToken(ls
,token
));
2710 luaD_throw(ls
->L
,3);
2712 static void luaX_syntaxerror(LexState
*ls
,const char*msg
){
2713 luaX_lexerror(ls
,msg
,ls
->t
.token
);
2715 static TString
*luaX_newstring(LexState
*ls
,const char*str
,size_t l
){
2717 TString
*ts
=luaS_newlstr(L
,str
,l
);
2718 TValue
*o
=luaH_setstr(L
,ls
->fs
->h
,ts
);
2725 static void inclinenumber(LexState
*ls
){
2726 int old
=ls
->current
;
2728 if(currIsNewline(ls
)&&ls
->current
!=old
)
2730 if(++ls
->linenumber
>=(INT_MAX
-2))
2731 luaX_syntaxerror(ls
,"chunk has too many lines");
2733 static void luaX_setinput(lua_State
*L
,LexState
*ls
,ZIO
*z
,TString
*source
){
2736 ls
->lookahead
.token
=TK_EOS
;
2742 luaZ_resizebuffer(ls
->L
,ls
->buff
,32);
2745 static int check_next(LexState
*ls
,const char*set
){
2746 if(!strchr(set
,ls
->current
))
2751 static void buffreplace(LexState
*ls
,char from
,char to
){
2752 size_t n
=luaZ_bufflen(ls
->buff
);
2753 char*p
=luaZ_buffer(ls
->buff
);
2755 if(p
[n
]==from
)p
[n
]=to
;
2757 static void read_numeral(LexState
*ls
,SemInfo
*seminfo
){
2760 }while(isdigit(ls
->current
)||ls
->current
=='.');
2761 if(check_next(ls
,"Ee"))
2762 check_next(ls
,"+-");
2763 while(isalnum(ls
->current
)||ls
->current
=='_')
2766 buffreplace(ls
,'.',ls
->decpoint
);
2767 if(!luaO_str2d(luaZ_buffer(ls
->buff
),&seminfo
->r
))
2768 luaX_lexerror(ls
,"malformed number",TK_NUMBER
);
2770 static int skip_sep(LexState
*ls
){
2774 while(ls
->current
=='='){
2778 return(ls
->current
==s
)?count
:(-count
)-1;
2780 static void read_long_string(LexState
*ls
,SemInfo
*seminfo
,int sep
){
2784 if(currIsNewline(ls
))
2787 switch(ls
->current
){
2789 luaX_lexerror(ls
,(seminfo
)?"unfinished long string":
2790 "unfinished long comment",TK_EOS
);
2793 if(skip_sep(ls
)==sep
){
2803 if(!seminfo
)luaZ_resetbuffer(ls
->buff
);
2807 if(seminfo
)save_and_next(ls
);
2813 seminfo
->ts
=luaX_newstring(ls
,luaZ_buffer(ls
->buff
)+(2+sep
),
2814 luaZ_bufflen(ls
->buff
)-2*(2+sep
));
2816 static void read_string(LexState
*ls
,int del
,SemInfo
*seminfo
){
2818 while(ls
->current
!=del
){
2819 switch(ls
->current
){
2821 luaX_lexerror(ls
,"unfinished string",TK_EOS
);
2825 luaX_lexerror(ls
,"unfinished string",TK_STRING
);
2830 switch(ls
->current
){
2831 case'a':c
='\a';break;
2832 case'b':c
='\b';break;
2833 case'f':c
='\f';break;
2834 case'n':c
='\n';break;
2835 case'r':c
='\r';break;
2836 case't':c
='\t';break;
2837 case'v':c
='\v';break;
2839 case'\r':save(ls
,'\n');inclinenumber(ls
);continue;
2842 if(!isdigit(ls
->current
))
2848 c
=10*c
+(ls
->current
-'0');
2850 }while(++i
<3&&isdigit(ls
->current
));
2852 luaX_lexerror(ls
,"escape sequence too large",TK_STRING
);
2867 seminfo
->ts
=luaX_newstring(ls
,luaZ_buffer(ls
->buff
)+1,
2868 luaZ_bufflen(ls
->buff
)-2);
2870 static int llex(LexState
*ls
,SemInfo
*seminfo
){
2871 luaZ_resetbuffer(ls
->buff
);
2873 switch(ls
->current
){
2881 if(ls
->current
!='-')return'-';
2883 if(ls
->current
=='['){
2884 int sep
=skip_sep(ls
);
2885 luaZ_resetbuffer(ls
->buff
);
2887 read_long_string(ls
,NULL
,sep
);
2888 luaZ_resetbuffer(ls
->buff
);
2892 while(!currIsNewline(ls
)&&ls
->current
!=(-1))
2897 int sep
=skip_sep(ls
);
2899 read_long_string(ls
,seminfo
,sep
);
2902 else if(sep
==-1)return'[';
2903 else luaX_lexerror(ls
,"invalid long string delimiter",TK_STRING
);
2907 if(ls
->current
!='=')return'=';
2908 else{next(ls
);return TK_EQ
;}
2912 if(ls
->current
!='=')return'<';
2913 else{next(ls
);return TK_LE
;}
2917 if(ls
->current
!='=')return'>';
2918 else{next(ls
);return TK_GE
;}
2922 if(ls
->current
!='=')return'~';
2923 else{next(ls
);return TK_NE
;}
2927 read_string(ls
,ls
->current
,seminfo
);
2932 if(check_next(ls
,".")){
2933 if(check_next(ls
,"."))
2935 else return TK_CONCAT
;
2937 else if(!isdigit(ls
->current
))return'.';
2939 read_numeral(ls
,seminfo
);
2947 if(isspace(ls
->current
)){
2951 else if(isdigit(ls
->current
)){
2952 read_numeral(ls
,seminfo
);
2955 else if(isalpha(ls
->current
)||ls
->current
=='_'){
2959 }while(isalnum(ls
->current
)||ls
->current
=='_');
2960 ts
=luaX_newstring(ls
,luaZ_buffer(ls
->buff
),
2961 luaZ_bufflen(ls
->buff
));
2962 if(ts
->tsv
.reserved
>0)
2963 return ts
->tsv
.reserved
-1+257;
2978 static void luaX_next(LexState
*ls
){
2979 ls
->lastline
=ls
->linenumber
;
2980 if(ls
->lookahead
.token
!=TK_EOS
){
2981 ls
->t
=ls
->lookahead
;
2982 ls
->lookahead
.token
=TK_EOS
;
2985 ls
->t
.token
=llex(ls
,&ls
->t
.seminfo
);
2987 static void luaX_lookahead(LexState
*ls
){
2988 ls
->lookahead
.token
=llex(ls
,&ls
->lookahead
.seminfo
);
2990 #define hasjumps(e)((e)->t!=(e)->f)
2991 static int isnumeral(expdesc
*e
){
2992 return(e
->k
==VKNUM
&&e
->t
==(-1)&&e
->f
==(-1));
2994 static void luaK_nil(FuncState
*fs
,int from
,int n
){
2995 Instruction
*previous
;
2996 if(fs
->pc
>fs
->lasttarget
){
2998 if(from
>=fs
->nactvar
)
3002 previous
=&fs
->f
->code
[fs
->pc
-1];
3003 if(GET_OPCODE(*previous
)==OP_LOADNIL
){
3004 int pfrom
=GETARG_A(*previous
);
3005 int pto
=GETARG_B(*previous
);
3006 if(pfrom
<=from
&&from
<=pto
+1){
3008 SETARG_B(*previous
,from
+n
-1);
3014 luaK_codeABC(fs
,OP_LOADNIL
,from
,from
+n
-1,0);
3016 static int luaK_jump(FuncState
*fs
){
3020 j
=luaK_codeAsBx(fs
,OP_JMP
,0,(-1));
3021 luaK_concat(fs
,&j
,jpc
);
3024 static void luaK_ret(FuncState
*fs
,int first
,int nret
){
3025 luaK_codeABC(fs
,OP_RETURN
,first
,nret
+1,0);
3027 static int condjump(FuncState
*fs
,OpCode op
,int A
,int B
,int C
){
3028 luaK_codeABC(fs
,op
,A
,B
,C
);
3029 return luaK_jump(fs
);
3031 static void fixjump(FuncState
*fs
,int pc
,int dest
){
3032 Instruction
*jmp
=&fs
->f
->code
[pc
];
3033 int offset
=dest
-(pc
+1);
3034 if(abs(offset
)>(((1<<(9+9))-1)>>1))
3035 luaX_syntaxerror(fs
->ls
,"control structure too long");
3036 SETARG_sBx(*jmp
,offset
);
3038 static int luaK_getlabel(FuncState
*fs
){
3039 fs
->lasttarget
=fs
->pc
;
3042 static int getjump(FuncState
*fs
,int pc
){
3043 int offset
=GETARG_sBx(fs
->f
->code
[pc
]);
3047 return(pc
+1)+offset
;
3049 static Instruction
*getjumpcontrol(FuncState
*fs
,int pc
){
3050 Instruction
*pi
=&fs
->f
->code
[pc
];
3051 if(pc
>=1&&testTMode(GET_OPCODE(*(pi
-1))))
3056 static int need_value(FuncState
*fs
,int list
){
3057 for(;list
!=(-1);list
=getjump(fs
,list
)){
3058 Instruction i
=*getjumpcontrol(fs
,list
);
3059 if(GET_OPCODE(i
)!=OP_TESTSET
)return 1;
3063 static int patchtestreg(FuncState
*fs
,int node
,int reg
){
3064 Instruction
*i
=getjumpcontrol(fs
,node
);
3065 if(GET_OPCODE(*i
)!=OP_TESTSET
)
3067 if(reg
!=((1<<8)-1)&®
!=GETARG_B(*i
))
3070 *i
=CREATE_ABC(OP_TEST
,GETARG_B(*i
),0,GETARG_C(*i
));
3073 static void removevalues(FuncState
*fs
,int list
){
3074 for(;list
!=(-1);list
=getjump(fs
,list
))
3075 patchtestreg(fs
,list
,((1<<8)-1));
3077 static void patchlistaux(FuncState
*fs
,int list
,int vtarget
,int reg
,
3080 int next
=getjump(fs
,list
);
3081 if(patchtestreg(fs
,list
,reg
))
3082 fixjump(fs
,list
,vtarget
);
3084 fixjump(fs
,list
,dtarget
);
3088 static void dischargejpc(FuncState
*fs
){
3089 patchlistaux(fs
,fs
->jpc
,fs
->pc
,((1<<8)-1),fs
->pc
);
3092 static void luaK_patchlist(FuncState
*fs
,int list
,int target
){
3094 luaK_patchtohere(fs
,list
);
3096 patchlistaux(fs
,list
,target
,((1<<8)-1),target
);
3099 static void luaK_patchtohere(FuncState
*fs
,int list
){
3101 luaK_concat(fs
,&fs
->jpc
,list
);
3103 static void luaK_concat(FuncState
*fs
,int*l1
,int l2
){
3110 while((next
=getjump(fs
,list
))!=(-1))
3112 fixjump(fs
,list
,l2
);
3115 static void luaK_checkstack(FuncState
*fs
,int n
){
3116 int newstack
=fs
->freereg
+n
;
3117 if(newstack
>fs
->f
->maxstacksize
){
3119 luaX_syntaxerror(fs
->ls
,"function or expression too complex");
3120 fs
->f
->maxstacksize
=cast_byte(newstack
);
3123 static void luaK_reserveregs(FuncState
*fs
,int n
){
3124 luaK_checkstack(fs
,n
);
3127 static void freereg(FuncState
*fs
,int reg
){
3128 if(!ISK(reg
)&®
>=fs
->nactvar
){
3132 static void freeexp(FuncState
*fs
,expdesc
*e
){
3134 freereg(fs
,e
->u
.s
.info
);
3136 static int addk(FuncState
*fs
,TValue
*k
,TValue
*v
){
3138 TValue
*idx
=luaH_set(L
,fs
->h
,k
);
3140 int oldsize
=f
->sizek
;
3141 if(ttisnumber(idx
)){
3142 return cast_int(nvalue(idx
));
3145 setnvalue(idx
,cast_num(fs
->nk
));
3146 luaM_growvector(L
,f
->k
,fs
->nk
,f
->sizek
,TValue
,
3147 ((1<<(9+9))-1),"constant table overflow");
3148 while(oldsize
<f
->sizek
)setnilvalue(&f
->k
[oldsize
++]);
3149 setobj(L
,&f
->k
[fs
->nk
],v
);
3150 luaC_barrier(L
,f
,v
);
3154 static int luaK_stringK(FuncState
*fs
,TString
*s
){
3156 setsvalue(fs
->L
,&o
,s
);
3157 return addk(fs
,&o
,&o
);
3159 static int luaK_numberK(FuncState
*fs
,lua_Number r
){
3162 return addk(fs
,&o
,&o
);
3164 static int boolK(FuncState
*fs
,int b
){
3167 return addk(fs
,&o
,&o
);
3169 static int nilK(FuncState
*fs
){
3172 sethvalue(fs
->L
,&k
,fs
->h
);
3173 return addk(fs
,&k
,&v
);
3175 static void luaK_setreturns(FuncState
*fs
,expdesc
*e
,int nresults
){
3177 SETARG_C(getcode(fs
,e
),nresults
+1);
3179 else if(e
->k
==VVARARG
){
3180 SETARG_B(getcode(fs
,e
),nresults
+1);
3181 SETARG_A(getcode(fs
,e
),fs
->freereg
);
3182 luaK_reserveregs(fs
,1);
3185 static void luaK_setoneret(FuncState
*fs
,expdesc
*e
){
3188 e
->u
.s
.info
=GETARG_A(getcode(fs
,e
));
3190 else if(e
->k
==VVARARG
){
3191 SETARG_B(getcode(fs
,e
),2);
3195 static void luaK_dischargevars(FuncState
*fs
,expdesc
*e
){
3202 e
->u
.s
.info
=luaK_codeABC(fs
,OP_GETUPVAL
,0,e
->u
.s
.info
,0);
3207 e
->u
.s
.info
=luaK_codeABx(fs
,OP_GETGLOBAL
,0,e
->u
.s
.info
);
3212 freereg(fs
,e
->u
.s
.aux
);
3213 freereg(fs
,e
->u
.s
.info
);
3214 e
->u
.s
.info
=luaK_codeABC(fs
,OP_GETTABLE
,0,e
->u
.s
.info
,e
->u
.s
.aux
);
3220 luaK_setoneret(fs
,e
);
3226 static int code_label(FuncState
*fs
,int A
,int b
,int jump
){
3228 return luaK_codeABC(fs
,OP_LOADBOOL
,A
,b
,jump
);
3230 static void discharge2reg(FuncState
*fs
,expdesc
*e
,int reg
){
3231 luaK_dischargevars(fs
,e
);
3237 case VFALSE
:case VTRUE
:{
3238 luaK_codeABC(fs
,OP_LOADBOOL
,reg
,e
->k
==VTRUE
,0);
3242 luaK_codeABx(fs
,OP_LOADK
,reg
,e
->u
.s
.info
);
3246 luaK_codeABx(fs
,OP_LOADK
,reg
,luaK_numberK(fs
,e
->u
.nval
));
3250 Instruction
*pc
=&getcode(fs
,e
);
3255 if(reg
!=e
->u
.s
.info
)
3256 luaK_codeABC(fs
,OP_MOVE
,reg
,e
->u
.s
.info
,0);
3266 static void discharge2anyreg(FuncState
*fs
,expdesc
*e
){
3267 if(e
->k
!=VNONRELOC
){
3268 luaK_reserveregs(fs
,1);
3269 discharge2reg(fs
,e
,fs
->freereg
-1);
3272 static void exp2reg(FuncState
*fs
,expdesc
*e
,int reg
){
3273 discharge2reg(fs
,e
,reg
);
3275 luaK_concat(fs
,&e
->t
,e
->u
.s
.info
);
3280 if(need_value(fs
,e
->t
)||need_value(fs
,e
->f
)){
3281 int fj
=(e
->k
==VJMP
)?(-1):luaK_jump(fs
);
3282 p_f
=code_label(fs
,reg
,0,1);
3283 p_t
=code_label(fs
,reg
,1,0);
3284 luaK_patchtohere(fs
,fj
);
3286 final
=luaK_getlabel(fs
);
3287 patchlistaux(fs
,e
->f
,final
,reg
,p_f
);
3288 patchlistaux(fs
,e
->t
,final
,reg
,p_t
);
3294 static void luaK_exp2nextreg(FuncState
*fs
,expdesc
*e
){
3295 luaK_dischargevars(fs
,e
);
3297 luaK_reserveregs(fs
,1);
3298 exp2reg(fs
,e
,fs
->freereg
-1);
3300 static int luaK_exp2anyreg(FuncState
*fs
,expdesc
*e
){
3301 luaK_dischargevars(fs
,e
);
3302 if(e
->k
==VNONRELOC
){
3303 if(!hasjumps(e
))return e
->u
.s
.info
;
3304 if(e
->u
.s
.info
>=fs
->nactvar
){
3305 exp2reg(fs
,e
,e
->u
.s
.info
);
3309 luaK_exp2nextreg(fs
,e
);
3312 static void luaK_exp2val(FuncState
*fs
,expdesc
*e
){
3314 luaK_exp2anyreg(fs
,e
);
3316 luaK_dischargevars(fs
,e
);
3318 static int luaK_exp2RK(FuncState
*fs
,expdesc
*e
){
3325 if(fs
->nk
<=((1<<(9-1))-1)){
3326 e
->u
.s
.info
=(e
->k
==VNIL
)?nilK(fs
):
3327 (e
->k
==VKNUM
)?luaK_numberK(fs
,e
->u
.nval
):
3328 boolK(fs
,(e
->k
==VTRUE
));
3330 return RKASK(e
->u
.s
.info
);
3335 if(e
->u
.s
.info
<=((1<<(9-1))-1))
3336 return RKASK(e
->u
.s
.info
);
3341 return luaK_exp2anyreg(fs
,e
);
3343 static void luaK_storevar(FuncState
*fs
,expdesc
*var
,expdesc
*ex
){
3347 exp2reg(fs
,ex
,var
->u
.s
.info
);
3351 int e
=luaK_exp2anyreg(fs
,ex
);
3352 luaK_codeABC(fs
,OP_SETUPVAL
,e
,var
->u
.s
.info
,0);
3356 int e
=luaK_exp2anyreg(fs
,ex
);
3357 luaK_codeABx(fs
,OP_SETGLOBAL
,e
,var
->u
.s
.info
);
3361 int e
=luaK_exp2RK(fs
,ex
);
3362 luaK_codeABC(fs
,OP_SETTABLE
,var
->u
.s
.info
,var
->u
.s
.aux
,e
);
3371 static void luaK_self(FuncState
*fs
,expdesc
*e
,expdesc
*key
){
3373 luaK_exp2anyreg(fs
,e
);
3376 luaK_reserveregs(fs
,2);
3377 luaK_codeABC(fs
,OP_SELF
,func
,e
->u
.s
.info
,luaK_exp2RK(fs
,key
));
3382 static void invertjump(FuncState
*fs
,expdesc
*e
){
3383 Instruction
*pc
=getjumpcontrol(fs
,e
->u
.s
.info
);
3384 SETARG_A(*pc
,!(GETARG_A(*pc
)));
3386 static int jumponcond(FuncState
*fs
,expdesc
*e
,int cond
){
3387 if(e
->k
==VRELOCABLE
){
3388 Instruction ie
=getcode(fs
,e
);
3389 if(GET_OPCODE(ie
)==OP_NOT
){
3391 return condjump(fs
,OP_TEST
,GETARG_B(ie
),0,!cond
);
3394 discharge2anyreg(fs
,e
);
3396 return condjump(fs
,OP_TESTSET
,((1<<8)-1),e
->u
.s
.info
,cond
);
3398 static void luaK_goiftrue(FuncState
*fs
,expdesc
*e
){
3400 luaK_dischargevars(fs
,e
);
3402 case VK
:case VKNUM
:case VTRUE
:{
3412 pc
=jumponcond(fs
,e
,0);
3416 luaK_concat(fs
,&e
->f
,pc
);
3417 luaK_patchtohere(fs
,e
->t
);
3420 static void luaK_goiffalse(FuncState
*fs
,expdesc
*e
){
3422 luaK_dischargevars(fs
,e
);
3424 case VNIL
:case VFALSE
:{
3433 pc
=jumponcond(fs
,e
,1);
3437 luaK_concat(fs
,&e
->t
,pc
);
3438 luaK_patchtohere(fs
,e
->f
);
3441 static void codenot(FuncState
*fs
,expdesc
*e
){
3442 luaK_dischargevars(fs
,e
);
3444 case VNIL
:case VFALSE
:{
3448 case VK
:case VKNUM
:case VTRUE
:{
3458 discharge2anyreg(fs
,e
);
3460 e
->u
.s
.info
=luaK_codeABC(fs
,OP_NOT
,0,e
->u
.s
.info
,0);
3468 {int temp
=e
->f
;e
->f
=e
->t
;e
->t
=temp
;}
3469 removevalues(fs
,e
->f
);
3470 removevalues(fs
,e
->t
);
3472 static void luaK_indexed(FuncState
*fs
,expdesc
*t
,expdesc
*k
){
3473 t
->u
.s
.aux
=luaK_exp2RK(fs
,k
);
3476 static int constfolding(OpCode op
,expdesc
*e1
,expdesc
*e2
){
3478 if(!isnumeral(e1
)||!isnumeral(e2
))return 0;
3482 case OP_ADD
:r
=luai_numadd(v1
,v2
);break;
3483 case OP_SUB
:r
=luai_numsub(v1
,v2
);break;
3484 case OP_MUL
:r
=luai_nummul(v1
,v2
);break;
3487 r
=luai_numdiv(v1
,v2
);break;
3490 r
=luai_nummod(v1
,v2
);break;
3491 case OP_POW
:r
=luai_numpow(v1
,v2
);break;
3492 case OP_UNM
:r
=luai_numunm(v1
);break;
3493 case OP_LEN
:return 0;
3496 if(luai_numisnan(r
))return 0;
3500 static void codearith(FuncState
*fs
,OpCode op
,expdesc
*e1
,expdesc
*e2
){
3501 if(constfolding(op
,e1
,e2
))
3504 int o2
=(op
!=OP_UNM
&&op
!=OP_LEN
)?luaK_exp2RK(fs
,e2
):0;
3505 int o1
=luaK_exp2RK(fs
,e1
);
3514 e1
->u
.s
.info
=luaK_codeABC(fs
,op
,0,o1
,o2
);
3518 static void codecomp(FuncState
*fs
,OpCode op
,int cond
,expdesc
*e1
,
3520 int o1
=luaK_exp2RK(fs
,e1
);
3521 int o2
=luaK_exp2RK(fs
,e2
);
3524 if(cond
==0&&op
!=OP_EQ
){
3526 temp
=o1
;o1
=o2
;o2
=temp
;
3529 e1
->u
.s
.info
=condjump(fs
,op
,cond
,o1
,o2
);
3532 static void luaK_prefix(FuncState
*fs
,UnOpr op
,expdesc
*e
){
3534 e2
.t
=e2
.f
=(-1);e2
.k
=VKNUM
;e2
.u
.nval
=0;
3538 luaK_exp2anyreg(fs
,e
);
3539 codearith(fs
,OP_UNM
,e
,&e2
);
3542 case OPR_NOT
:codenot(fs
,e
);break;
3544 luaK_exp2anyreg(fs
,e
);
3545 codearith(fs
,OP_LEN
,e
,&e2
);
3551 static void luaK_infix(FuncState
*fs
,BinOpr op
,expdesc
*v
){
3554 luaK_goiftrue(fs
,v
);
3558 luaK_goiffalse(fs
,v
);
3562 luaK_exp2nextreg(fs
,v
);
3565 case OPR_ADD
:case OPR_SUB
:case OPR_MUL
:case OPR_DIV
:
3566 case OPR_MOD
:case OPR_POW
:{
3567 if(!isnumeral(v
))luaK_exp2RK(fs
,v
);
3576 static void luaK_posfix(FuncState
*fs
,BinOpr op
,expdesc
*e1
,expdesc
*e2
){
3579 luaK_dischargevars(fs
,e2
);
3580 luaK_concat(fs
,&e2
->f
,e1
->f
);
3585 luaK_dischargevars(fs
,e2
);
3586 luaK_concat(fs
,&e2
->t
,e1
->t
);
3591 luaK_exp2val(fs
,e2
);
3592 if(e2
->k
==VRELOCABLE
&&GET_OPCODE(getcode(fs
,e2
))==OP_CONCAT
){
3594 SETARG_B(getcode(fs
,e2
),e1
->u
.s
.info
);
3595 e1
->k
=VRELOCABLE
;e1
->u
.s
.info
=e2
->u
.s
.info
;
3598 luaK_exp2nextreg(fs
,e2
);
3599 codearith(fs
,OP_CONCAT
,e1
,e2
);
3603 case OPR_ADD
:codearith(fs
,OP_ADD
,e1
,e2
);break;
3604 case OPR_SUB
:codearith(fs
,OP_SUB
,e1
,e2
);break;
3605 case OPR_MUL
:codearith(fs
,OP_MUL
,e1
,e2
);break;
3606 case OPR_DIV
:codearith(fs
,OP_DIV
,e1
,e2
);break;
3607 case OPR_MOD
:codearith(fs
,OP_MOD
,e1
,e2
);break;
3608 case OPR_POW
:codearith(fs
,OP_POW
,e1
,e2
);break;
3609 case OPR_EQ
:codecomp(fs
,OP_EQ
,1,e1
,e2
);break;
3610 case OPR_NE
:codecomp(fs
,OP_EQ
,0,e1
,e2
);break;
3611 case OPR_LT
:codecomp(fs
,OP_LT
,1,e1
,e2
);break;
3612 case OPR_LE
:codecomp(fs
,OP_LE
,1,e1
,e2
);break;
3613 case OPR_GT
:codecomp(fs
,OP_LT
,0,e1
,e2
);break;
3614 case OPR_GE
:codecomp(fs
,OP_LE
,0,e1
,e2
);break;
3618 static void luaK_fixline(FuncState
*fs
,int line
){
3619 fs
->f
->lineinfo
[fs
->pc
-1]=line
;
3621 static int luaK_code(FuncState
*fs
,Instruction i
,int line
){
3624 luaM_growvector(fs
->L
,f
->code
,fs
->pc
,f
->sizecode
,Instruction
,
3625 (INT_MAX
-2),"code size overflow");
3627 luaM_growvector(fs
->L
,f
->lineinfo
,fs
->pc
,f
->sizelineinfo
,int,
3628 (INT_MAX
-2),"code size overflow");
3629 f
->lineinfo
[fs
->pc
]=line
;
3632 static int luaK_codeABC(FuncState
*fs
,OpCode o
,int a
,int b
,int c
){
3633 return luaK_code(fs
,CREATE_ABC(o
,a
,b
,c
),fs
->ls
->lastline
);
3635 static int luaK_codeABx(FuncState
*fs
,OpCode o
,int a
,unsigned int bc
){
3636 return luaK_code(fs
,CREATE_ABx(o
,a
,bc
),fs
->ls
->lastline
);
3638 static void luaK_setlist(FuncState
*fs
,int base
,int nelems
,int tostore
){
3639 int c
=(nelems
-1)/50+1;
3640 int b
=(tostore
==(-1))?0:tostore
;
3642 luaK_codeABC(fs
,OP_SETLIST
,base
,b
,c
);
3644 luaK_codeABC(fs
,OP_SETLIST
,base
,b
,0);
3645 luaK_code(fs
,cast(Instruction
,c
),fs
->ls
->lastline
);
3649 #define hasmultret(k)((k)==VCALL||(k)==VVARARG)
3650 #define getlocvar(fs,i)((fs)->f->locvars[(fs)->actvar[i]])
3651 #define luaY_checklimit(fs,v,l,m)if((v)>(l))errorlimit(fs,l,m)
3652 typedef struct BlockCnt
{
3653 struct BlockCnt
*previous
;
3657 lu_byte isbreakable
;
3659 static void chunk(LexState
*ls
);
3660 static void expr(LexState
*ls
,expdesc
*v
);
3661 static void anchor_token(LexState
*ls
){
3662 if(ls
->t
.token
==TK_NAME
||ls
->t
.token
==TK_STRING
){
3663 TString
*ts
=ls
->t
.seminfo
.ts
;
3664 luaX_newstring(ls
,getstr(ts
),ts
->tsv
.len
);
3667 static void error_expected(LexState
*ls
,int token
){
3668 luaX_syntaxerror(ls
,
3669 luaO_pushfstring(ls
->L
,LUA_QL("%s")" expected",luaX_token2str(ls
,token
)));
3671 static void errorlimit(FuncState
*fs
,int limit
,const char*what
){
3672 const char*msg
=(fs
->f
->linedefined
==0)?
3673 luaO_pushfstring(fs
->L
,"main function has more than %d %s",limit
,what
):
3674 luaO_pushfstring(fs
->L
,"function at line %d has more than %d %s",
3675 fs
->f
->linedefined
,limit
,what
);
3676 luaX_lexerror(fs
->ls
,msg
,0);
3678 static int testnext(LexState
*ls
,int c
){
3685 static void check(LexState
*ls
,int c
){
3687 error_expected(ls
,c
);
3689 static void checknext(LexState
*ls
,int c
){
3693 #define check_condition(ls,c,msg){if(!(c))luaX_syntaxerror(ls,msg);}
3694 static void check_match(LexState
*ls
,int what
,int who
,int where
){
3695 if(!testnext(ls
,what
)){
3696 if(where
==ls
->linenumber
)
3697 error_expected(ls
,what
);
3699 luaX_syntaxerror(ls
,luaO_pushfstring(ls
->L
,
3700 LUA_QL("%s")" expected (to close "LUA_QL("%s")" at line %d)",
3701 luaX_token2str(ls
,what
),luaX_token2str(ls
,who
),where
));
3705 static TString
*str_checkname(LexState
*ls
){
3708 ts
=ls
->t
.seminfo
.ts
;
3712 static void init_exp(expdesc
*e
,expkind k
,int i
){
3717 static void codestring(LexState
*ls
,expdesc
*e
,TString
*s
){
3718 init_exp(e
,VK
,luaK_stringK(ls
->fs
,s
));
3720 static void checkname(LexState
*ls
,expdesc
*e
){
3721 codestring(ls
,e
,str_checkname(ls
));
3723 static int registerlocalvar(LexState
*ls
,TString
*varname
){
3724 FuncState
*fs
=ls
->fs
;
3726 int oldsize
=f
->sizelocvars
;
3727 luaM_growvector(ls
->L
,f
->locvars
,fs
->nlocvars
,f
->sizelocvars
,
3728 LocVar
,SHRT_MAX
,"too many local variables");
3729 while(oldsize
<f
->sizelocvars
)f
->locvars
[oldsize
++].varname
=NULL
;
3730 f
->locvars
[fs
->nlocvars
].varname
=varname
;
3731 luaC_objbarrier(ls
->L
,f
,varname
);
3732 return fs
->nlocvars
++;
3734 #define new_localvarliteral(ls,v,n)new_localvar(ls,luaX_newstring(ls,""v,(sizeof(v)/sizeof(char))-1),n)
3735 static void new_localvar(LexState
*ls
,TString
*name
,int n
){
3736 FuncState
*fs
=ls
->fs
;
3737 luaY_checklimit(fs
,fs
->nactvar
+n
+1,200,"local variables");
3738 fs
->actvar
[fs
->nactvar
+n
]=cast(unsigned short,registerlocalvar(ls
,name
));
3740 static void adjustlocalvars(LexState
*ls
,int nvars
){
3741 FuncState
*fs
=ls
->fs
;
3742 fs
->nactvar
=cast_byte(fs
->nactvar
+nvars
);
3743 for(;nvars
;nvars
--){
3744 getlocvar(fs
,fs
->nactvar
-nvars
).startpc
=fs
->pc
;
3747 static void removevars(LexState
*ls
,int tolevel
){
3748 FuncState
*fs
=ls
->fs
;
3749 while(fs
->nactvar
>tolevel
)
3750 getlocvar(fs
,--fs
->nactvar
).endpc
=fs
->pc
;
3752 static int indexupvalue(FuncState
*fs
,TString
*name
,expdesc
*v
){
3755 int oldsize
=f
->sizeupvalues
;
3756 for(i
=0;i
<f
->nups
;i
++){
3757 if(fs
->upvalues
[i
].k
==v
->k
&&fs
->upvalues
[i
].info
==v
->u
.s
.info
){
3761 luaY_checklimit(fs
,f
->nups
+1,60,"upvalues");
3762 luaM_growvector(fs
->L
,f
->upvalues
,f
->nups
,f
->sizeupvalues
,
3763 TString
*,(INT_MAX
-2),"");
3764 while(oldsize
<f
->sizeupvalues
)f
->upvalues
[oldsize
++]=NULL
;
3765 f
->upvalues
[f
->nups
]=name
;
3766 luaC_objbarrier(fs
->L
,f
,name
);
3767 fs
->upvalues
[f
->nups
].k
=cast_byte(v
->k
);
3768 fs
->upvalues
[f
->nups
].info
=cast_byte(v
->u
.s
.info
);
3771 static int searchvar(FuncState
*fs
,TString
*n
){
3773 for(i
=fs
->nactvar
-1;i
>=0;i
--){
3774 if(n
==getlocvar(fs
,i
).varname
)
3779 static void markupval(FuncState
*fs
,int level
){
3781 while(bl
&&bl
->nactvar
>level
)bl
=bl
->previous
;
3784 static int singlevaraux(FuncState
*fs
,TString
*n
,expdesc
*var
,int base
){
3786 init_exp(var
,VGLOBAL
,((1<<8)-1));
3790 int v
=searchvar(fs
,n
);
3792 init_exp(var
,VLOCAL
,v
);
3798 if(singlevaraux(fs
->prev
,n
,var
,0)==VGLOBAL
)
3800 var
->u
.s
.info
=indexupvalue(fs
,n
,var
);
3806 static void singlevar(LexState
*ls
,expdesc
*var
){
3807 TString
*varname
=str_checkname(ls
);
3808 FuncState
*fs
=ls
->fs
;
3809 if(singlevaraux(fs
,varname
,var
,1)==VGLOBAL
)
3810 var
->u
.s
.info
=luaK_stringK(fs
,varname
);
3812 static void adjust_assign(LexState
*ls
,int nvars
,int nexps
,expdesc
*e
){
3813 FuncState
*fs
=ls
->fs
;
3814 int extra
=nvars
-nexps
;
3815 if(hasmultret(e
->k
)){
3818 luaK_setreturns(fs
,e
,extra
);
3819 if(extra
>1)luaK_reserveregs(fs
,extra
-1);
3822 if(e
->k
!=VVOID
)luaK_exp2nextreg(fs
,e
);
3824 int reg
=fs
->freereg
;
3825 luaK_reserveregs(fs
,extra
);
3826 luaK_nil(fs
,reg
,extra
);
3830 static void enterlevel(LexState
*ls
){
3831 if(++ls
->L
->nCcalls
>200)
3832 luaX_lexerror(ls
,"chunk has too many syntax levels",0);
3834 #define leavelevel(ls)((ls)->L->nCcalls--)
3835 static void enterblock(FuncState
*fs
,BlockCnt
*bl
,lu_byte isbreakable
){
3837 bl
->isbreakable
=isbreakable
;
3838 bl
->nactvar
=fs
->nactvar
;
3840 bl
->previous
=fs
->bl
;
3843 static void leaveblock(FuncState
*fs
){
3845 fs
->bl
=bl
->previous
;
3846 removevars(fs
->ls
,bl
->nactvar
);
3848 luaK_codeABC(fs
,OP_CLOSE
,bl
->nactvar
,0,0);
3849 fs
->freereg
=fs
->nactvar
;
3850 luaK_patchtohere(fs
,bl
->breaklist
);
3852 static void pushclosure(LexState
*ls
,FuncState
*func
,expdesc
*v
){
3853 FuncState
*fs
=ls
->fs
;
3855 int oldsize
=f
->sizep
;
3857 luaM_growvector(ls
->L
,f
->p
,fs
->np
,f
->sizep
,Proto
*,
3858 ((1<<(9+9))-1),"constant table overflow");
3859 while(oldsize
<f
->sizep
)f
->p
[oldsize
++]=NULL
;
3860 f
->p
[fs
->np
++]=func
->f
;
3861 luaC_objbarrier(ls
->L
,f
,func
->f
);
3862 init_exp(v
,VRELOCABLE
,luaK_codeABx(fs
,OP_CLOSURE
,0,fs
->np
-1));
3863 for(i
=0;i
<func
->f
->nups
;i
++){
3864 OpCode o
=(func
->upvalues
[i
].k
==VLOCAL
)?OP_MOVE
:OP_GETUPVAL
;
3865 luaK_codeABC(fs
,o
,0,func
->upvalues
[i
].info
,0);
3868 static void open_func(LexState
*ls
,FuncState
*fs
){
3870 Proto
*f
=luaF_newproto(L
);
3885 f
->source
=ls
->source
;
3887 fs
->h
=luaH_new(L
,0,0);
3888 sethvalue(L
,L
->top
,fs
->h
);
3890 setptvalue(L
,L
->top
,f
);
3893 static void close_func(LexState
*ls
){
3895 FuncState
*fs
=ls
->fs
;
3899 luaM_reallocvector(L
,f
->code
,f
->sizecode
,fs
->pc
,Instruction
);
3901 luaM_reallocvector(L
,f
->lineinfo
,f
->sizelineinfo
,fs
->pc
,int);
3902 f
->sizelineinfo
=fs
->pc
;
3903 luaM_reallocvector(L
,f
->k
,f
->sizek
,fs
->nk
,TValue
);
3905 luaM_reallocvector(L
,f
->p
,f
->sizep
,fs
->np
,Proto
*);
3907 luaM_reallocvector(L
,f
->locvars
,f
->sizelocvars
,fs
->nlocvars
,LocVar
);
3908 f
->sizelocvars
=fs
->nlocvars
;
3909 luaM_reallocvector(L
,f
->upvalues
,f
->sizeupvalues
,f
->nups
,TString
*);
3910 f
->sizeupvalues
=f
->nups
;
3912 if(fs
)anchor_token(ls
);
3915 static Proto
*luaY_parser(lua_State
*L
,ZIO
*z
,Mbuffer
*buff
,const char*name
){
3916 struct LexState lexstate
;
3917 struct FuncState funcstate
;
3919 luaX_setinput(L
,&lexstate
,z
,luaS_new(L
,name
));
3920 open_func(&lexstate
,&funcstate
);
3921 funcstate
.f
->is_vararg
=2;
3922 luaX_next(&lexstate
);
3924 check(&lexstate
,TK_EOS
);
3925 close_func(&lexstate
);
3928 static void field(LexState
*ls
,expdesc
*v
){
3929 FuncState
*fs
=ls
->fs
;
3931 luaK_exp2anyreg(fs
,v
);
3934 luaK_indexed(fs
,v
,&key
);
3936 static void yindex(LexState
*ls
,expdesc
*v
){
3939 luaK_exp2val(ls
->fs
,v
);
3949 static void recfield(LexState
*ls
,struct ConsControl
*cc
){
3950 FuncState
*fs
=ls
->fs
;
3951 int reg
=ls
->fs
->freereg
;
3954 if(ls
->t
.token
==TK_NAME
){
3955 luaY_checklimit(fs
,cc
->nh
,(INT_MAX
-2),"items in a constructor");
3962 rkkey
=luaK_exp2RK(fs
,&key
);
3964 luaK_codeABC(fs
,OP_SETTABLE
,cc
->t
->u
.s
.info
,rkkey
,luaK_exp2RK(fs
,&val
));
3967 static void closelistfield(FuncState
*fs
,struct ConsControl
*cc
){
3968 if(cc
->v
.k
==VVOID
)return;
3969 luaK_exp2nextreg(fs
,&cc
->v
);
3971 if(cc
->tostore
==50){
3972 luaK_setlist(fs
,cc
->t
->u
.s
.info
,cc
->na
,cc
->tostore
);
3976 static void lastlistfield(FuncState
*fs
,struct ConsControl
*cc
){
3977 if(cc
->tostore
==0)return;
3978 if(hasmultret(cc
->v
.k
)){
3979 luaK_setmultret(fs
,&cc
->v
);
3980 luaK_setlist(fs
,cc
->t
->u
.s
.info
,cc
->na
,(-1));
3985 luaK_exp2nextreg(fs
,&cc
->v
);
3986 luaK_setlist(fs
,cc
->t
->u
.s
.info
,cc
->na
,cc
->tostore
);
3989 static void listfield(LexState
*ls
,struct ConsControl
*cc
){
3991 luaY_checklimit(ls
->fs
,cc
->na
,(INT_MAX
-2),"items in a constructor");
3995 static void constructor(LexState
*ls
,expdesc
*t
){
3996 FuncState
*fs
=ls
->fs
;
3997 int line
=ls
->linenumber
;
3998 int pc
=luaK_codeABC(fs
,OP_NEWTABLE
,0,0,0);
3999 struct ConsControl cc
;
4000 cc
.na
=cc
.nh
=cc
.tostore
=0;
4002 init_exp(t
,VRELOCABLE
,pc
);
4003 init_exp(&cc
.v
,VVOID
,0);
4004 luaK_exp2nextreg(ls
->fs
,t
);
4007 if(ls
->t
.token
=='}')break;
4008 closelistfield(fs
,&cc
);
4009 switch(ls
->t
.token
){
4012 if(ls
->lookahead
.token
!='=')
4027 }while(testnext(ls
,',')||testnext(ls
,';'));
4028 check_match(ls
,'}','{',line
);
4029 lastlistfield(fs
,&cc
);
4030 SETARG_B(fs
->f
->code
[pc
],luaO_int2fb(cc
.na
));
4031 SETARG_C(fs
->f
->code
[pc
],luaO_int2fb(cc
.nh
));
4033 static void parlist(LexState
*ls
){
4034 FuncState
*fs
=ls
->fs
;
4038 if(ls
->t
.token
!=')'){
4040 switch(ls
->t
.token
){
4042 new_localvar(ls
,str_checkname(ls
),nparams
++);
4050 default:luaX_syntaxerror(ls
,"<name> or "LUA_QL("...")" expected");
4052 }while(!f
->is_vararg
&&testnext(ls
,','));
4054 adjustlocalvars(ls
,nparams
);
4055 f
->numparams
=cast_byte(fs
->nactvar
-(f
->is_vararg
&1));
4056 luaK_reserveregs(fs
,fs
->nactvar
);
4058 static void body(LexState
*ls
,expdesc
*e
,int needself
,int line
){
4060 open_func(ls
,&new_fs
);
4061 new_fs
.f
->linedefined
=line
;
4064 new_localvarliteral(ls
,"self",0);
4065 adjustlocalvars(ls
,1);
4070 new_fs
.f
->lastlinedefined
=ls
->linenumber
;
4071 check_match(ls
,TK_END
,TK_FUNCTION
,line
);
4073 pushclosure(ls
,&new_fs
,e
);
4075 static int explist1(LexState
*ls
,expdesc
*v
){
4078 while(testnext(ls
,',')){
4079 luaK_exp2nextreg(ls
->fs
,v
);
4085 static void funcargs(LexState
*ls
,expdesc
*f
){
4086 FuncState
*fs
=ls
->fs
;
4089 int line
=ls
->linenumber
;
4090 switch(ls
->t
.token
){
4092 if(line
!=ls
->lastline
)
4093 luaX_syntaxerror(ls
,"ambiguous syntax (function call x new statement)");
4095 if(ls
->t
.token
==')')
4099 luaK_setmultret(fs
,&args
);
4101 check_match(ls
,')','(',line
);
4105 constructor(ls
,&args
);
4109 codestring(ls
,&args
,ls
->t
.seminfo
.ts
);
4114 luaX_syntaxerror(ls
,"function arguments expected");
4119 if(hasmultret(args
.k
))
4123 luaK_exp2nextreg(fs
,&args
);
4124 nparams
=fs
->freereg
-(base
+1);
4126 init_exp(f
,VCALL
,luaK_codeABC(fs
,OP_CALL
,base
,nparams
+1,2));
4127 luaK_fixline(fs
,line
);
4130 static void prefixexp(LexState
*ls
,expdesc
*v
){
4131 switch(ls
->t
.token
){
4133 int line
=ls
->linenumber
;
4136 check_match(ls
,')','(',line
);
4137 luaK_dischargevars(ls
->fs
,v
);
4145 luaX_syntaxerror(ls
,"unexpected symbol");
4150 static void primaryexp(LexState
*ls
,expdesc
*v
){
4151 FuncState
*fs
=ls
->fs
;
4154 switch(ls
->t
.token
){
4161 luaK_exp2anyreg(fs
,v
);
4163 luaK_indexed(fs
,v
,&key
);
4170 luaK_self(fs
,v
,&key
);
4174 case'(':case TK_STRING
:case'{':{
4175 luaK_exp2nextreg(fs
,v
);
4183 static void simpleexp(LexState
*ls
,expdesc
*v
){
4184 switch(ls
->t
.token
){
4186 init_exp(v
,VKNUM
,0);
4187 v
->u
.nval
=ls
->t
.seminfo
.r
;
4191 codestring(ls
,v
,ls
->t
.seminfo
.ts
);
4199 init_exp(v
,VTRUE
,0);
4203 init_exp(v
,VFALSE
,0);
4207 FuncState
*fs
=ls
->fs
;
4208 check_condition(ls
,fs
->f
->is_vararg
,
4209 "cannot use "LUA_QL("...")" outside a vararg function");
4210 fs
->f
->is_vararg
&=~4;
4211 init_exp(v
,VVARARG
,luaK_codeABC(fs
,OP_VARARG
,0,1,0));
4220 body(ls
,v
,0,ls
->linenumber
);
4230 static UnOpr
getunopr(int op
){
4232 case TK_NOT
:return OPR_NOT
;
4233 case'-':return OPR_MINUS
;
4234 case'#':return OPR_LEN
;
4235 default:return OPR_NOUNOPR
;
4238 static BinOpr
getbinopr(int op
){
4240 case'+':return OPR_ADD
;
4241 case'-':return OPR_SUB
;
4242 case'*':return OPR_MUL
;
4243 case'/':return OPR_DIV
;
4244 case'%':return OPR_MOD
;
4245 case'^':return OPR_POW
;
4246 case TK_CONCAT
:return OPR_CONCAT
;
4247 case TK_NE
:return OPR_NE
;
4248 case TK_EQ
:return OPR_EQ
;
4249 case'<':return OPR_LT
;
4250 case TK_LE
:return OPR_LE
;
4251 case'>':return OPR_GT
;
4252 case TK_GE
:return OPR_GE
;
4253 case TK_AND
:return OPR_AND
;
4254 case TK_OR
:return OPR_OR
;
4255 default:return OPR_NOBINOPR
;
4258 static const struct{
4262 {6,6},{6,6},{7,7},{7,7},{7,7},
4265 {3,3},{3,3},{3,3},{3,3},
4268 static BinOpr
subexpr(LexState
*ls
,expdesc
*v
,unsigned int limit
){
4272 uop
=getunopr(ls
->t
.token
);
4273 if(uop
!=OPR_NOUNOPR
){
4276 luaK_prefix(ls
->fs
,uop
,v
);
4278 else simpleexp(ls
,v
);
4279 op
=getbinopr(ls
->t
.token
);
4280 while(op
!=OPR_NOBINOPR
&&priority
[op
].left
>limit
){
4284 luaK_infix(ls
->fs
,op
,v
);
4285 nextop
=subexpr(ls
,&v2
,priority
[op
].right
);
4286 luaK_posfix(ls
->fs
,op
,v
,&v2
);
4292 static void expr(LexState
*ls
,expdesc
*v
){
4295 static int block_follow(int token
){
4297 case TK_ELSE
:case TK_ELSEIF
:case TK_END
:
4298 case TK_UNTIL
:case TK_EOS
:
4303 static void block(LexState
*ls
){
4304 FuncState
*fs
=ls
->fs
;
4306 enterblock(fs
,&bl
,0);
4311 struct LHS_assign
*prev
;
4314 static void check_conflict(LexState
*ls
,struct LHS_assign
*lh
,expdesc
*v
){
4315 FuncState
*fs
=ls
->fs
;
4316 int extra
=fs
->freereg
;
4318 for(;lh
;lh
=lh
->prev
){
4319 if(lh
->v
.k
==VINDEXED
){
4320 if(lh
->v
.u
.s
.info
==v
->u
.s
.info
){
4322 lh
->v
.u
.s
.info
=extra
;
4324 if(lh
->v
.u
.s
.aux
==v
->u
.s
.info
){
4326 lh
->v
.u
.s
.aux
=extra
;
4331 luaK_codeABC(fs
,OP_MOVE
,fs
->freereg
,v
->u
.s
.info
,0);
4332 luaK_reserveregs(fs
,1);
4335 static void assignment(LexState
*ls
,struct LHS_assign
*lh
,int nvars
){
4337 check_condition(ls
,VLOCAL
<=lh
->v
.k
&&lh
->v
.k
<=VINDEXED
,
4339 if(testnext(ls
,',')){
4340 struct LHS_assign nv
;
4342 primaryexp(ls
,&nv
.v
);
4344 check_conflict(ls
,lh
,&nv
.v
);
4345 luaY_checklimit(ls
->fs
,nvars
,200-ls
->L
->nCcalls
,
4346 "variables in assignment");
4347 assignment(ls
,&nv
,nvars
+1);
4352 nexps
=explist1(ls
,&e
);
4354 adjust_assign(ls
,nvars
,nexps
,&e
);
4356 ls
->fs
->freereg
-=nexps
-nvars
;
4359 luaK_setoneret(ls
->fs
,&e
);
4360 luaK_storevar(ls
->fs
,&lh
->v
,&e
);
4364 init_exp(&e
,VNONRELOC
,ls
->fs
->freereg
-1);
4365 luaK_storevar(ls
->fs
,&lh
->v
,&e
);
4367 static int cond(LexState
*ls
){
4370 if(v
.k
==VNIL
)v
.k
=VFALSE
;
4371 luaK_goiftrue(ls
->fs
,&v
);
4374 static void breakstat(LexState
*ls
){
4375 FuncState
*fs
=ls
->fs
;
4378 while(bl
&&!bl
->isbreakable
){
4383 luaX_syntaxerror(ls
,"no loop to break");
4385 luaK_codeABC(fs
,OP_CLOSE
,bl
->nactvar
,0,0);
4386 luaK_concat(fs
,&bl
->breaklist
,luaK_jump(fs
));
4388 static void whilestat(LexState
*ls
,int line
){
4389 FuncState
*fs
=ls
->fs
;
4394 whileinit
=luaK_getlabel(fs
);
4396 enterblock(fs
,&bl
,1);
4397 checknext(ls
,TK_DO
);
4399 luaK_patchlist(fs
,luaK_jump(fs
),whileinit
);
4400 check_match(ls
,TK_END
,TK_WHILE
,line
);
4402 luaK_patchtohere(fs
,condexit
);
4404 static void repeatstat(LexState
*ls
,int line
){
4406 FuncState
*fs
=ls
->fs
;
4407 int repeat_init
=luaK_getlabel(fs
);
4409 enterblock(fs
,&bl1
,1);
4410 enterblock(fs
,&bl2
,0);
4413 check_match(ls
,TK_UNTIL
,TK_REPEAT
,line
);
4417 luaK_patchlist(ls
->fs
,condexit
,repeat_init
);
4421 luaK_patchtohere(ls
->fs
,condexit
);
4423 luaK_patchlist(ls
->fs
,luaK_jump(fs
),repeat_init
);
4427 static int exp1(LexState
*ls
){
4432 luaK_exp2nextreg(ls
->fs
,&e
);
4435 static void forbody(LexState
*ls
,int base
,int line
,int nvars
,int isnum
){
4437 FuncState
*fs
=ls
->fs
;
4439 adjustlocalvars(ls
,3);
4440 checknext(ls
,TK_DO
);
4441 prep
=isnum
?luaK_codeAsBx(fs
,OP_FORPREP
,base
,(-1)):luaK_jump(fs
);
4442 enterblock(fs
,&bl
,0);
4443 adjustlocalvars(ls
,nvars
);
4444 luaK_reserveregs(fs
,nvars
);
4447 luaK_patchtohere(fs
,prep
);
4448 endfor
=(isnum
)?luaK_codeAsBx(fs
,OP_FORLOOP
,base
,(-1)):
4449 luaK_codeABC(fs
,OP_TFORLOOP
,base
,0,nvars
);
4450 luaK_fixline(fs
,line
);
4451 luaK_patchlist(fs
,(isnum
?endfor
:luaK_jump(fs
)),prep
+1);
4453 static void fornum(LexState
*ls
,TString
*varname
,int line
){
4454 FuncState
*fs
=ls
->fs
;
4455 int base
=fs
->freereg
;
4456 new_localvarliteral(ls
,"(for index)",0);
4457 new_localvarliteral(ls
,"(for limit)",1);
4458 new_localvarliteral(ls
,"(for step)",2);
4459 new_localvar(ls
,varname
,3);
4464 if(testnext(ls
,','))
4467 luaK_codeABx(fs
,OP_LOADK
,fs
->freereg
,luaK_numberK(fs
,1));
4468 luaK_reserveregs(fs
,1);
4470 forbody(ls
,base
,line
,1,1);
4472 static void forlist(LexState
*ls
,TString
*indexname
){
4473 FuncState
*fs
=ls
->fs
;
4477 int base
=fs
->freereg
;
4478 new_localvarliteral(ls
,"(for generator)",nvars
++);
4479 new_localvarliteral(ls
,"(for state)",nvars
++);
4480 new_localvarliteral(ls
,"(for control)",nvars
++);
4481 new_localvar(ls
,indexname
,nvars
++);
4482 while(testnext(ls
,','))
4483 new_localvar(ls
,str_checkname(ls
),nvars
++);
4484 checknext(ls
,TK_IN
);
4485 line
=ls
->linenumber
;
4486 adjust_assign(ls
,3,explist1(ls
,&e
),&e
);
4487 luaK_checkstack(fs
,3);
4488 forbody(ls
,base
,line
,nvars
-3,0);
4490 static void forstat(LexState
*ls
,int line
){
4491 FuncState
*fs
=ls
->fs
;
4494 enterblock(fs
,&bl
,1);
4496 varname
=str_checkname(ls
);
4497 switch(ls
->t
.token
){
4498 case'=':fornum(ls
,varname
,line
);break;
4499 case',':case TK_IN
:forlist(ls
,varname
);break;
4500 default:luaX_syntaxerror(ls
,LUA_QL("=")" or "LUA_QL("in")" expected");
4502 check_match(ls
,TK_END
,TK_FOR
,line
);
4505 static int test_then_block(LexState
*ls
){
4509 checknext(ls
,TK_THEN
);
4513 static void ifstat(LexState
*ls
,int line
){
4514 FuncState
*fs
=ls
->fs
;
4516 int escapelist
=(-1);
4517 flist
=test_then_block(ls
);
4518 while(ls
->t
.token
==TK_ELSEIF
){
4519 luaK_concat(fs
,&escapelist
,luaK_jump(fs
));
4520 luaK_patchtohere(fs
,flist
);
4521 flist
=test_then_block(ls
);
4523 if(ls
->t
.token
==TK_ELSE
){
4524 luaK_concat(fs
,&escapelist
,luaK_jump(fs
));
4525 luaK_patchtohere(fs
,flist
);
4530 luaK_concat(fs
,&escapelist
,flist
);
4531 luaK_patchtohere(fs
,escapelist
);
4532 check_match(ls
,TK_END
,TK_IF
,line
);
4534 static void localfunc(LexState
*ls
){
4536 FuncState
*fs
=ls
->fs
;
4537 new_localvar(ls
,str_checkname(ls
),0);
4538 init_exp(&v
,VLOCAL
,fs
->freereg
);
4539 luaK_reserveregs(fs
,1);
4540 adjustlocalvars(ls
,1);
4541 body(ls
,&b
,0,ls
->linenumber
);
4542 luaK_storevar(fs
,&v
,&b
);
4543 getlocvar(fs
,fs
->nactvar
-1).startpc
=fs
->pc
;
4545 static void localstat(LexState
*ls
){
4550 new_localvar(ls
,str_checkname(ls
),nvars
++);
4551 }while(testnext(ls
,','));
4552 if(testnext(ls
,'='))
4553 nexps
=explist1(ls
,&e
);
4558 adjust_assign(ls
,nvars
,nexps
,&e
);
4559 adjustlocalvars(ls
,nvars
);
4561 static int funcname(LexState
*ls
,expdesc
*v
){
4564 while(ls
->t
.token
=='.')
4566 if(ls
->t
.token
==':'){
4572 static void funcstat(LexState
*ls
,int line
){
4576 needself
=funcname(ls
,&v
);
4577 body(ls
,&b
,needself
,line
);
4578 luaK_storevar(ls
->fs
,&v
,&b
);
4579 luaK_fixline(ls
->fs
,line
);
4581 static void exprstat(LexState
*ls
){
4582 FuncState
*fs
=ls
->fs
;
4583 struct LHS_assign v
;
4584 primaryexp(ls
,&v
.v
);
4586 SETARG_C(getcode(fs
,&v
.v
),1);
4589 assignment(ls
,&v
,1);
4592 static void retstat(LexState
*ls
){
4593 FuncState
*fs
=ls
->fs
;
4597 if(block_follow(ls
->t
.token
)||ls
->t
.token
==';')
4600 nret
=explist1(ls
,&e
);
4601 if(hasmultret(e
.k
)){
4602 luaK_setmultret(fs
,&e
);
4603 if(e
.k
==VCALL
&&nret
==1){
4604 SET_OPCODE(getcode(fs
,&e
),OP_TAILCALL
);
4611 first
=luaK_exp2anyreg(fs
,&e
);
4613 luaK_exp2nextreg(fs
,&e
);
4618 luaK_ret(fs
,first
,nret
);
4620 static int statement(LexState
*ls
){
4621 int line
=ls
->linenumber
;
4622 switch(ls
->t
.token
){
4634 check_match(ls
,TK_END
,TK_DO
,line
);
4642 repeatstat(ls
,line
);
4651 if(testnext(ls
,TK_FUNCTION
))
4672 static void chunk(LexState
*ls
){
4675 while(!islast
&&!block_follow(ls
->t
.token
)){
4676 islast
=statement(ls
);
4678 ls
->fs
->freereg
=ls
->fs
->nactvar
;
4682 static const TValue
*luaV_tonumber(const TValue
*obj
,TValue
*n
){
4684 if(ttisnumber(obj
))return obj
;
4685 if(ttisstring(obj
)&&luaO_str2d(svalue(obj
),&num
)){
4692 static int luaV_tostring(lua_State
*L
,StkId obj
){
4693 if(!ttisnumber(obj
))
4697 lua_Number n
=nvalue(obj
);
4698 lua_number2str(s
,n
);
4699 setsvalue(L
,obj
,luaS_new(L
,s
));
4703 static void callTMres(lua_State
*L
,StkId res
,const TValue
*f
,
4704 const TValue
*p1
,const TValue
*p2
){
4705 ptrdiff_t result
=savestack(L
,res
);
4707 setobj(L
,L
->top
+1,p1
);
4708 setobj(L
,L
->top
+2,p2
);
4709 luaD_checkstack(L
,3);
4711 luaD_call(L
,L
->top
-3,1);
4712 res
=restorestack(L
,result
);
4714 setobj(L
,res
,L
->top
);
4716 static void callTM(lua_State
*L
,const TValue
*f
,const TValue
*p1
,
4717 const TValue
*p2
,const TValue
*p3
){
4719 setobj(L
,L
->top
+1,p1
);
4720 setobj(L
,L
->top
+2,p2
);
4721 setobj(L
,L
->top
+3,p3
);
4722 luaD_checkstack(L
,4);
4724 luaD_call(L
,L
->top
-4,0);
4726 static void luaV_gettable(lua_State
*L
,const TValue
*t
,TValue
*key
,StkId val
){
4728 for(loop
=0;loop
<100;loop
++){
4732 const TValue
*res
=luaH_get(h
,key
);
4734 (tm
=fasttm(L
,h
->metatable
,TM_INDEX
))==NULL
){
4739 else if(ttisnil(tm
=luaT_gettmbyobj(L
,t
,TM_INDEX
)))
4740 luaG_typeerror(L
,t
,"index");
4741 if(ttisfunction(tm
)){
4742 callTMres(L
,val
,tm
,t
,key
);
4747 luaG_runerror(L
,"loop in gettable");
4749 static void luaV_settable(lua_State
*L
,const TValue
*t
,TValue
*key
,StkId val
){
4752 for(loop
=0;loop
<100;loop
++){
4756 TValue
*oldval
=luaH_set(L
,h
,key
);
4757 if(!ttisnil(oldval
)||
4758 (tm
=fasttm(L
,h
->metatable
,TM_NEWINDEX
))==NULL
){
4759 setobj(L
,oldval
,val
);
4761 luaC_barriert(L
,h
,val
);
4765 else if(ttisnil(tm
=luaT_gettmbyobj(L
,t
,TM_NEWINDEX
)))
4766 luaG_typeerror(L
,t
,"index");
4767 if(ttisfunction(tm
)){
4768 callTM(L
,tm
,t
,key
,val
);
4774 luaG_runerror(L
,"loop in settable");
4776 static int call_binTM(lua_State
*L
,const TValue
*p1
,const TValue
*p2
,
4777 StkId res
,TMS event
){
4778 const TValue
*tm
=luaT_gettmbyobj(L
,p1
,event
);
4780 tm
=luaT_gettmbyobj(L
,p2
,event
);
4781 if(ttisnil(tm
))return 0;
4782 callTMres(L
,res
,tm
,p1
,p2
);
4785 static const TValue
*get_compTM(lua_State
*L
,Table
*mt1
,Table
*mt2
,
4787 const TValue
*tm1
=fasttm(L
,mt1
,event
);
4789 if(tm1
==NULL
)return NULL
;
4790 if(mt1
==mt2
)return tm1
;
4791 tm2
=fasttm(L
,mt2
,event
);
4792 if(tm2
==NULL
)return NULL
;
4793 if(luaO_rawequalObj(tm1
,tm2
))
4797 static int call_orderTM(lua_State
*L
,const TValue
*p1
,const TValue
*p2
,
4799 const TValue
*tm1
=luaT_gettmbyobj(L
,p1
,event
);
4801 if(ttisnil(tm1
))return-1;
4802 tm2
=luaT_gettmbyobj(L
,p2
,event
);
4803 if(!luaO_rawequalObj(tm1
,tm2
))
4805 callTMres(L
,L
->top
,tm1
,p1
,p2
);
4806 return!l_isfalse(L
->top
);
4808 static int l_strcmp(const TString
*ls
,const TString
*rs
){
4809 const char*l
=getstr(ls
);
4810 size_t ll
=ls
->tsv
.len
;
4811 const char*r
=getstr(rs
);
4812 size_t lr
=rs
->tsv
.len
;
4814 int temp
=strcoll(l
,r
);
4815 if(temp
!=0)return temp
;
4817 size_t len
=strlen(l
);
4819 return(len
==ll
)?0:1;
4823 l
+=len
;ll
-=len
;r
+=len
;lr
-=len
;
4827 static int luaV_lessthan(lua_State
*L
,const TValue
*l
,const TValue
*r
){
4829 if(ttype(l
)!=ttype(r
))
4830 return luaG_ordererror(L
,l
,r
);
4831 else if(ttisnumber(l
))
4832 return luai_numlt(nvalue(l
),nvalue(r
));
4833 else if(ttisstring(l
))
4834 return l_strcmp(rawtsvalue(l
),rawtsvalue(r
))<0;
4835 else if((res
=call_orderTM(L
,l
,r
,TM_LT
))!=-1)
4837 return luaG_ordererror(L
,l
,r
);
4839 static int lessequal(lua_State
*L
,const TValue
*l
,const TValue
*r
){
4841 if(ttype(l
)!=ttype(r
))
4842 return luaG_ordererror(L
,l
,r
);
4843 else if(ttisnumber(l
))
4844 return luai_numle(nvalue(l
),nvalue(r
));
4845 else if(ttisstring(l
))
4846 return l_strcmp(rawtsvalue(l
),rawtsvalue(r
))<=0;
4847 else if((res
=call_orderTM(L
,l
,r
,TM_LE
))!=-1)
4849 else if((res
=call_orderTM(L
,r
,l
,TM_LT
))!=-1)
4851 return luaG_ordererror(L
,l
,r
);
4853 static int luaV_equalval(lua_State
*L
,const TValue
*t1
,const TValue
*t2
){
4857 case 3:return luai_numeq(nvalue(t1
),nvalue(t2
));
4858 case 1:return bvalue(t1
)==bvalue(t2
);
4859 case 2:return pvalue(t1
)==pvalue(t2
);
4861 if(uvalue(t1
)==uvalue(t2
))return 1;
4862 tm
=get_compTM(L
,uvalue(t1
)->metatable
,uvalue(t2
)->metatable
,
4867 if(hvalue(t1
)==hvalue(t2
))return 1;
4868 tm
=get_compTM(L
,hvalue(t1
)->metatable
,hvalue(t2
)->metatable
,TM_EQ
);
4871 default:return gcvalue(t1
)==gcvalue(t2
);
4873 if(tm
==NULL
)return 0;
4874 callTMres(L
,L
->top
,tm
,t1
,t2
);
4875 return!l_isfalse(L
->top
);
4877 static void luaV_concat(lua_State
*L
,int total
,int last
){
4879 StkId top
=L
->base
+last
+1;
4881 if(!(ttisstring(top
-2)||ttisnumber(top
-2))||!tostring(L
,top
-1)){
4882 if(!call_binTM(L
,top
-2,top
-1,top
-2,TM_CONCAT
))
4883 luaG_concaterror(L
,top
-2,top
-1);
4884 }else if(tsvalue(top
-1)->len
==0)
4885 (void)tostring(L
,top
-2);
4887 size_t tl
=tsvalue(top
-1)->len
;
4890 for(n
=1;n
<total
&&tostring(L
,top
-n
-1);n
++){
4891 size_t l
=tsvalue(top
-n
-1)->len
;
4892 if(l
>=((size_t)(~(size_t)0)-2)-tl
)luaG_runerror(L
,"string length overflow");
4895 buffer
=luaZ_openspace(L
,&G(L
)->buff
,tl
);
4898 size_t l
=tsvalue(top
-i
)->len
;
4899 memcpy(buffer
+tl
,svalue(top
-i
),l
);
4902 setsvalue(L
,top
-n
,luaS_newlstr(L
,buffer
,tl
));
4908 static void Arith(lua_State
*L
,StkId ra
,const TValue
*rb
,
4909 const TValue
*rc
,TMS op
){
4912 if((b
=luaV_tonumber(rb
,&tempb
))!=NULL
&&
4913 (c
=luaV_tonumber(rc
,&tempc
))!=NULL
){
4914 lua_Number nb
=nvalue(b
),nc
=nvalue(c
);
4916 case TM_ADD
:setnvalue(ra
,luai_numadd(nb
,nc
));break;
4917 case TM_SUB
:setnvalue(ra
,luai_numsub(nb
,nc
));break;
4918 case TM_MUL
:setnvalue(ra
,luai_nummul(nb
,nc
));break;
4919 case TM_DIV
:setnvalue(ra
,luai_numdiv(nb
,nc
));break;
4920 case TM_MOD
:setnvalue(ra
,luai_nummod(nb
,nc
));break;
4921 case TM_POW
:setnvalue(ra
,luai_numpow(nb
,nc
));break;
4922 case TM_UNM
:setnvalue(ra
,luai_numunm(nb
));break;
4926 else if(!call_binTM(L
,rb
,rc
,ra
,op
))
4927 luaG_aritherror(L
,rb
,rc
);
4929 #define runtime_check(L,c){if(!(c))break;}
4930 #define RA(i)(base+GETARG_A(i))
4931 #define RB(i)check_exp(getBMode(GET_OPCODE(i))==OpArgR,base+GETARG_B(i))
4932 #define RKB(i)check_exp(getBMode(GET_OPCODE(i))==OpArgK,ISK(GETARG_B(i))?k+INDEXK(GETARG_B(i)):base+GETARG_B(i))
4933 #define RKC(i)check_exp(getCMode(GET_OPCODE(i))==OpArgK,ISK(GETARG_C(i))?k+INDEXK(GETARG_C(i)):base+GETARG_C(i))
4934 #define KBx(i)check_exp(getBMode(GET_OPCODE(i))==OpArgK,k+GETARG_Bx(i))
4935 #define dojump(L,pc,i){(pc)+=(i);}
4936 #define Protect(x){L->savedpc=pc;{x;};base=L->base;}
4937 #define arith_op(op,tm){TValue*rb=RKB(i);TValue*rc=RKC(i);if(ttisnumber(rb)&&ttisnumber(rc)){lua_Number nb=nvalue(rb),nc=nvalue(rc);setnvalue(ra,op(nb,nc));}else Protect(Arith(L,ra,rb,rc,tm));}
4938 static void luaV_execute(lua_State
*L
,int nexeccalls
){
4942 const Instruction
*pc
;
4945 cl
=&clvalue(L
->ci
->func
)->l
;
4949 const Instruction i
=*pc
++;
4952 switch(GET_OPCODE(i
)){
4958 setobj(L
,ra
,KBx(i
));
4962 setbvalue(ra
,GETARG_B(i
));
4963 if(GETARG_C(i
))pc
++;
4975 setobj(L
,ra
,cl
->upvals
[b
]->v
);
4981 sethvalue(L
,&g
,cl
->env
);
4982 Protect(luaV_gettable(L
,&g
,rb
,ra
));
4986 Protect(luaV_gettable(L
,RB(i
),RKC(i
),ra
));
4991 sethvalue(L
,&g
,cl
->env
);
4992 Protect(luaV_settable(L
,&g
,KBx(i
),ra
));
4996 UpVal
*uv
=cl
->upvals
[GETARG_B(i
)];
4998 luaC_barrier(L
,uv
,ra
);
5002 Protect(luaV_settable(L
,ra
,RKB(i
),RKC(i
)));
5008 sethvalue(L
,ra
,luaH_new(L
,luaO_fb2int(b
),luaO_fb2int(c
)));
5009 Protect(luaC_checkGC(L
));
5015 Protect(luaV_gettable(L
,rb
,RKC(i
),ra
));
5019 arith_op(luai_numadd
,TM_ADD
);
5023 arith_op(luai_numsub
,TM_SUB
);
5027 arith_op(luai_nummul
,TM_MUL
);
5031 arith_op(luai_numdiv
,TM_DIV
);
5035 arith_op(luai_nummod
,TM_MOD
);
5039 arith_op(luai_numpow
,TM_POW
);
5045 lua_Number nb
=nvalue(rb
);
5046 setnvalue(ra
,luai_numunm(nb
));
5049 Protect(Arith(L
,ra
,rb
,rb
,TM_UNM
));
5054 int res
=l_isfalse(RB(i
));
5059 const TValue
*rb
=RB(i
);
5062 setnvalue(ra
,cast_num(luaH_getn(hvalue(rb
))));
5066 setnvalue(ra
,cast_num(tsvalue(rb
)->len
));
5071 if(!call_binTM(L
,rb
,(&luaO_nilobject_
),ra
,TM_LEN
))
5072 luaG_typeerror(L
,rb
,"get length of");
5081 Protect(luaV_concat(L
,c
-b
+1,c
);luaC_checkGC(L
));
5082 setobj(L
,RA(i
),base
+b
);
5086 dojump(L
,pc
,GETARG_sBx(i
));
5093 if(equalobj(L
,rb
,rc
)==GETARG_A(i
))
5094 dojump(L
,pc
,GETARG_sBx(*pc
));
5101 if(luaV_lessthan(L
,RKB(i
),RKC(i
))==GETARG_A(i
))
5102 dojump(L
,pc
,GETARG_sBx(*pc
));
5109 if(lessequal(L
,RKB(i
),RKC(i
))==GETARG_A(i
))
5110 dojump(L
,pc
,GETARG_sBx(*pc
));
5116 if(l_isfalse(ra
)!=GETARG_C(i
))
5117 dojump(L
,pc
,GETARG_sBx(*pc
));
5123 if(l_isfalse(rb
)!=GETARG_C(i
)){
5125 dojump(L
,pc
,GETARG_sBx(*pc
));
5132 int nresults
=GETARG_C(i
)-1;
5133 if(b
!=0)L
->top
=ra
+b
;
5135 switch(luaD_precall(L
,ra
,nresults
)){
5141 if(nresults
>=0)L
->top
=L
->ci
->top
;
5152 if(b
!=0)L
->top
=ra
+b
;
5154 switch(luaD_precall(L
,ra
,(-1))){
5156 CallInfo
*ci
=L
->ci
-1;
5158 StkId func
=ci
->func
;
5159 StkId pfunc
=(ci
+1)->func
;
5160 if(L
->openupval
)luaF_close(L
,ci
->base
);
5161 L
->base
=ci
->base
=ci
->func
+((ci
+1)->base
-pfunc
);
5162 for(aux
=0;pfunc
+aux
<L
->top
;aux
++)
5163 setobj(L
,func
+aux
,pfunc
+aux
);
5164 ci
->top
=L
->top
=func
+aux
;
5165 ci
->savedpc
=L
->savedpc
;
5181 if(b
!=0)L
->top
=ra
+b
-1;
5182 if(L
->openupval
)luaF_close(L
,base
);
5184 b
=luaD_poscall(L
,ra
);
5188 if(b
)L
->top
=L
->ci
->top
;
5193 lua_Number step
=nvalue(ra
+2);
5194 lua_Number idx
=luai_numadd(nvalue(ra
),step
);
5195 lua_Number limit
=nvalue(ra
+1);
5196 if(luai_numlt(0,step
)?luai_numle(idx
,limit
)
5197 :luai_numle(limit
,idx
)){
5198 dojump(L
,pc
,GETARG_sBx(i
));
5200 setnvalue(ra
+3,idx
);
5205 const TValue
*init
=ra
;
5206 const TValue
*plimit
=ra
+1;
5207 const TValue
*pstep
=ra
+2;
5209 if(!tonumber(init
,ra
))
5210 luaG_runerror(L
,LUA_QL("for")" initial value must be a number");
5211 else if(!tonumber(plimit
,ra
+1))
5212 luaG_runerror(L
,LUA_QL("for")" limit must be a number");
5213 else if(!tonumber(pstep
,ra
+2))
5214 luaG_runerror(L
,LUA_QL("for")" step must be a number");
5215 setnvalue(ra
,luai_numsub(nvalue(ra
),nvalue(pstep
)));
5216 dojump(L
,pc
,GETARG_sBx(i
));
5221 setobj(L
,cb
+2,ra
+2);
5222 setobj(L
,cb
+1,ra
+1);
5225 Protect(luaD_call(L
,cb
,GETARG_C(i
)));
5230 dojump(L
,pc
,GETARG_sBx(*pc
));
5241 n
=cast_int(L
->top
-ra
)-1;
5244 if(c
==0)c
=cast_int(*pc
++);
5245 runtime_check(L
,ttistable(ra
));
5248 if(last
>h
->sizearray
)
5249 luaH_resizearray(L
,h
,last
);
5252 setobj(L
,luaH_setnum(L
,h
,last
--),val
);
5253 luaC_barriert(L
,h
,val
);
5265 p
=cl
->p
->p
[GETARG_Bx(i
)];
5267 ncl
=luaF_newLclosure(L
,nup
,cl
->env
);
5269 for(j
=0;j
<nup
;j
++,pc
++){
5270 if(GET_OPCODE(*pc
)==OP_GETUPVAL
)
5271 ncl
->l
.upvals
[j
]=cl
->upvals
[GETARG_B(*pc
)];
5273 ncl
->l
.upvals
[j
]=luaF_findupval(L
,base
+GETARG_B(*pc
));
5276 setclvalue(L
,ra
,ncl
);
5277 Protect(luaC_checkGC(L
));
5281 int b
=GETARG_B(i
)-1;
5284 int n
=cast_int(ci
->base
-ci
->func
)-cl
->p
->numparams
-1;
5286 Protect(luaD_checkstack(L
,n
));
5293 setobj(L
,ra
+j
,ci
->base
-n
+j
);
5304 #define api_checknelems(L,n)luai_apicheck(L,(n)<=(L->top-L->base))
5305 #define api_checkvalidindex(L,i)luai_apicheck(L,(i)!=(&luaO_nilobject_))
5306 #define api_incr_top(L){luai_apicheck(L,L->top<L->ci->top);L->top++;}
5307 static TValue
*index2adr(lua_State
*L
,int idx
){
5309 TValue
*o
=L
->base
+(idx
-1);
5310 luai_apicheck(L
,idx
<=L
->ci
->top
-L
->base
);
5311 if(o
>=L
->top
)return cast(TValue
*,(&luaO_nilobject_
));
5314 else if(idx
>(-10000)){
5315 luai_apicheck(L
,idx
!=0&&-idx
<=L
->top
-L
->base
);
5319 case(-10000):return registry(L
);
5321 Closure
*func
=curr_func(L
);
5322 sethvalue(L
,&L
->env
,func
->c
.env
);
5325 case(-10002):return gt(L
);
5327 Closure
*func
=curr_func(L
);
5329 return(idx
<=func
->c
.nupvalues
)
5330 ?&func
->c
.upvalue
[idx
-1]
5331 :cast(TValue
*,(&luaO_nilobject_
));
5335 static Table
*getcurrenv(lua_State
*L
){
5336 if(L
->ci
==L
->base_ci
)
5337 return hvalue(gt(L
));
5339 Closure
*func
=curr_func(L
);
5343 static int lua_checkstack(lua_State
*L
,int size
){
5345 if(size
>8000||(L
->top
-L
->base
+size
)>8000)
5348 luaD_checkstack(L
,size
);
5349 if(L
->ci
->top
<L
->top
+size
)
5350 L
->ci
->top
=L
->top
+size
;
5354 static lua_CFunction
lua_atpanic(lua_State
*L
,lua_CFunction panicf
){
5360 static int lua_gettop(lua_State
*L
){
5361 return cast_int(L
->top
-L
->base
);
5363 static void lua_settop(lua_State
*L
,int idx
){
5365 luai_apicheck(L
,idx
<=L
->stack_last
-L
->base
);
5366 while(L
->top
<L
->base
+idx
)
5367 setnilvalue(L
->top
++);
5371 luai_apicheck(L
,-(idx
+1)<=(L
->top
-L
->base
));
5375 static void lua_remove(lua_State
*L
,int idx
){
5378 api_checkvalidindex(L
,p
);
5379 while(++p
<L
->top
)setobj(L
,p
-1,p
);
5382 static void lua_insert(lua_State
*L
,int idx
){
5386 api_checkvalidindex(L
,p
);
5387 for(q
=L
->top
;q
>p
;q
--)setobj(L
,q
,q
-1);
5390 static void lua_replace(lua_State
*L
,int idx
){
5392 if(idx
==(-10001)&&L
->ci
==L
->base_ci
)
5393 luaG_runerror(L
,"no calling environment");
5394 api_checknelems(L
,1);
5396 api_checkvalidindex(L
,o
);
5398 Closure
*func
=curr_func(L
);
5399 luai_apicheck(L
,ttistable(L
->top
-1));
5400 func
->c
.env
=hvalue(L
->top
-1);
5401 luaC_barrier(L
,func
,L
->top
-1);
5404 setobj(L
,o
,L
->top
-1);
5406 luaC_barrier(L
,curr_func(L
),L
->top
-1);
5410 static void lua_pushvalue(lua_State
*L
,int idx
){
5411 setobj(L
,L
->top
,index2adr(L
,idx
));
5414 static int lua_type(lua_State
*L
,int idx
){
5415 StkId o
=index2adr(L
,idx
);
5416 return(o
==(&luaO_nilobject_
))?(-1):ttype(o
);
5418 static const char*lua_typename(lua_State
*L
,int t
){
5420 return(t
==(-1))?"no value":luaT_typenames
[t
];
5422 static int lua_iscfunction(lua_State
*L
,int idx
){
5423 StkId o
=index2adr(L
,idx
);
5424 return iscfunction(o
);
5426 static int lua_isnumber(lua_State
*L
,int idx
){
5428 const TValue
*o
=index2adr(L
,idx
);
5429 return tonumber(o
,&n
);
5431 static int lua_isstring(lua_State
*L
,int idx
){
5432 int t
=lua_type(L
,idx
);
5435 static int lua_rawequal(lua_State
*L
,int index1
,int index2
){
5436 StkId o1
=index2adr(L
,index1
);
5437 StkId o2
=index2adr(L
,index2
);
5438 return(o1
==(&luaO_nilobject_
)||o2
==(&luaO_nilobject_
))?0
5439 :luaO_rawequalObj(o1
,o2
);
5441 static int lua_lessthan(lua_State
*L
,int index1
,int index2
){
5444 o1
=index2adr(L
,index1
);
5445 o2
=index2adr(L
,index2
);
5446 i
=(o1
==(&luaO_nilobject_
)||o2
==(&luaO_nilobject_
))?0
5447 :luaV_lessthan(L
,o1
,o2
);
5450 static lua_Number
lua_tonumber(lua_State
*L
,int idx
){
5452 const TValue
*o
=index2adr(L
,idx
);
5458 static lua_Integer
lua_tointeger(lua_State
*L
,int idx
){
5460 const TValue
*o
=index2adr(L
,idx
);
5463 lua_Number num
=nvalue(o
);
5464 lua_number2integer(res
,num
);
5470 static int lua_toboolean(lua_State
*L
,int idx
){
5471 const TValue
*o
=index2adr(L
,idx
);
5472 return!l_isfalse(o
);
5474 static const char*lua_tolstring(lua_State
*L
,int idx
,size_t*len
){
5475 StkId o
=index2adr(L
,idx
);
5477 if(!luaV_tostring(L
,o
)){
5478 if(len
!=NULL
)*len
=0;
5484 if(len
!=NULL
)*len
=tsvalue(o
)->len
;
5487 static size_t lua_objlen(lua_State
*L
,int idx
){
5488 StkId o
=index2adr(L
,idx
);
5490 case 4:return tsvalue(o
)->len
;
5491 case 7:return uvalue(o
)->len
;
5492 case 5:return luaH_getn(hvalue(o
));
5495 l
=(luaV_tostring(L
,o
)?tsvalue(o
)->len
:0);
5501 static lua_CFunction
lua_tocfunction(lua_State
*L
,int idx
){
5502 StkId o
=index2adr(L
,idx
);
5503 return(!iscfunction(o
))?NULL
:clvalue(o
)->c
.f
;
5505 static void*lua_touserdata(lua_State
*L
,int idx
){
5506 StkId o
=index2adr(L
,idx
);
5508 case 7:return(rawuvalue(o
)+1);
5509 case 2:return pvalue(o
);
5510 default:return NULL
;
5513 static void lua_pushnil(lua_State
*L
){
5514 setnilvalue(L
->top
);
5517 static void lua_pushnumber(lua_State
*L
,lua_Number n
){
5518 setnvalue(L
->top
,n
);
5521 static void lua_pushinteger(lua_State
*L
,lua_Integer n
){
5522 setnvalue(L
->top
,cast_num(n
));
5525 static void lua_pushlstring(lua_State
*L
,const char*s
,size_t len
){
5527 setsvalue(L
,L
->top
,luaS_newlstr(L
,s
,len
));
5530 static void lua_pushstring(lua_State
*L
,const char*s
){
5534 lua_pushlstring(L
,s
,strlen(s
));
5536 static const char*lua_pushvfstring(lua_State
*L
,const char*fmt
,
5540 ret
=luaO_pushvfstring(L
,fmt
,argp
);
5543 static const char*lua_pushfstring(lua_State
*L
,const char*fmt
,...){
5548 ret
=luaO_pushvfstring(L
,fmt
,argp
);
5552 static void lua_pushcclosure(lua_State
*L
,lua_CFunction fn
,int n
){
5555 api_checknelems(L
,n
);
5556 cl
=luaF_newCclosure(L
,n
,getcurrenv(L
));
5560 setobj(L
,&cl
->c
.upvalue
[n
],L
->top
+n
);
5561 setclvalue(L
,L
->top
,cl
);
5564 static void lua_pushboolean(lua_State
*L
,int b
){
5565 setbvalue(L
->top
,(b
!=0));
5568 static int lua_pushthread(lua_State
*L
){
5569 setthvalue(L
,L
->top
,L
);
5571 return(G(L
)->mainthread
==L
);
5573 static void lua_gettable(lua_State
*L
,int idx
){
5576 api_checkvalidindex(L
,t
);
5577 luaV_gettable(L
,t
,L
->top
-1,L
->top
-1);
5579 static void lua_getfield(lua_State
*L
,int idx
,const char*k
){
5583 api_checkvalidindex(L
,t
);
5584 setsvalue(L
,&key
,luaS_new(L
,k
));
5585 luaV_gettable(L
,t
,&key
,L
->top
);
5588 static void lua_rawget(lua_State
*L
,int idx
){
5591 luai_apicheck(L
,ttistable(t
));
5592 setobj(L
,L
->top
-1,luaH_get(hvalue(t
),L
->top
-1));
5594 static void lua_rawgeti(lua_State
*L
,int idx
,int n
){
5597 luai_apicheck(L
,ttistable(o
));
5598 setobj(L
,L
->top
,luaH_getnum(hvalue(o
),n
));
5601 static void lua_createtable(lua_State
*L
,int narray
,int nrec
){
5603 sethvalue(L
,L
->top
,luaH_new(L
,narray
,nrec
));
5606 static int lua_getmetatable(lua_State
*L
,int objindex
){
5610 obj
=index2adr(L
,objindex
);
5613 mt
=hvalue(obj
)->metatable
;
5616 mt
=uvalue(obj
)->metatable
;
5619 mt
=G(L
)->mt
[ttype(obj
)];
5625 sethvalue(L
,L
->top
,mt
);
5631 static void lua_getfenv(lua_State
*L
,int idx
){
5634 api_checkvalidindex(L
,o
);
5637 sethvalue(L
,L
->top
,clvalue(o
)->c
.env
);
5640 sethvalue(L
,L
->top
,uvalue(o
)->env
);
5643 setobj(L
,L
->top
,gt(thvalue(o
)));
5646 setnilvalue(L
->top
);
5651 static void lua_settable(lua_State
*L
,int idx
){
5653 api_checknelems(L
,2);
5655 api_checkvalidindex(L
,t
);
5656 luaV_settable(L
,t
,L
->top
-2,L
->top
-1);
5659 static void lua_setfield(lua_State
*L
,int idx
,const char*k
){
5662 api_checknelems(L
,1);
5664 api_checkvalidindex(L
,t
);
5665 setsvalue(L
,&key
,luaS_new(L
,k
));
5666 luaV_settable(L
,t
,&key
,L
->top
-1);
5669 static void lua_rawset(lua_State
*L
,int idx
){
5671 api_checknelems(L
,2);
5673 luai_apicheck(L
,ttistable(t
));
5674 setobj(L
,luaH_set(L
,hvalue(t
),L
->top
-2),L
->top
-1);
5675 luaC_barriert(L
,hvalue(t
),L
->top
-1);
5678 static void lua_rawseti(lua_State
*L
,int idx
,int n
){
5680 api_checknelems(L
,1);
5682 luai_apicheck(L
,ttistable(o
));
5683 setobj(L
,luaH_setnum(L
,hvalue(o
),n
),L
->top
-1);
5684 luaC_barriert(L
,hvalue(o
),L
->top
-1);
5687 static int lua_setmetatable(lua_State
*L
,int objindex
){
5690 api_checknelems(L
,1);
5691 obj
=index2adr(L
,objindex
);
5692 api_checkvalidindex(L
,obj
);
5693 if(ttisnil(L
->top
-1))
5696 luai_apicheck(L
,ttistable(L
->top
-1));
5697 mt
=hvalue(L
->top
-1);
5701 hvalue(obj
)->metatable
=mt
;
5703 luaC_objbarriert(L
,hvalue(obj
),mt
);
5707 uvalue(obj
)->metatable
=mt
;
5709 luaC_objbarrier(L
,rawuvalue(obj
),mt
);
5713 G(L
)->mt
[ttype(obj
)]=mt
;
5720 static int lua_setfenv(lua_State
*L
,int idx
){
5723 api_checknelems(L
,1);
5725 api_checkvalidindex(L
,o
);
5726 luai_apicheck(L
,ttistable(L
->top
-1));
5729 clvalue(o
)->c
.env
=hvalue(L
->top
-1);
5732 uvalue(o
)->env
=hvalue(L
->top
-1);
5735 sethvalue(L
,gt(thvalue(o
)),hvalue(L
->top
-1));
5741 if(res
)luaC_objbarrier(L
,gcvalue(o
),hvalue(L
->top
-1));
5745 #define adjustresults(L,nres){if(nres==(-1)&&L->top>=L->ci->top)L->ci->top=L->top;}
5746 #define checkresults(L,na,nr)luai_apicheck(L,(nr)==(-1)||(L->ci->top-L->top>=(nr)-(na)))
5747 static void lua_call(lua_State
*L
,int nargs
,int nresults
){
5749 api_checknelems(L
,nargs
+1);
5750 checkresults(L
,nargs
,nresults
);
5751 func
=L
->top
-(nargs
+1);
5752 luaD_call(L
,func
,nresults
);
5753 adjustresults(L
,nresults
);
5759 static void f_call(lua_State
*L
,void*ud
){
5760 struct CallS
*c
=cast(struct CallS
*,ud
);
5761 luaD_call(L
,c
->func
,c
->nresults
);
5763 static int lua_pcall(lua_State
*L
,int nargs
,int nresults
,int errfunc
){
5767 api_checknelems(L
,nargs
+1);
5768 checkresults(L
,nargs
,nresults
);
5772 StkId o
=index2adr(L
,errfunc
);
5773 api_checkvalidindex(L
,o
);
5774 func
=savestack(L
,o
);
5776 c
.func
=L
->top
-(nargs
+1);
5777 c
.nresults
=nresults
;
5778 status
=luaD_pcall(L
,f_call
,&c
,savestack(L
,c
.func
),func
);
5779 adjustresults(L
,nresults
);
5782 static int lua_load(lua_State
*L
,lua_Reader reader
,void*data
,
5783 const char*chunkname
){
5786 if(!chunkname
)chunkname
="?";
5787 luaZ_init(L
,&z
,reader
,data
);
5788 status
=luaD_protectedparser(L
,&z
,chunkname
);
5791 static int lua_error(lua_State
*L
){
5792 api_checknelems(L
,1);
5796 static int lua_next(lua_State
*L
,int idx
){
5800 luai_apicheck(L
,ttistable(t
));
5801 more
=luaH_next(L
,hvalue(t
),L
->top
-1);
5809 static void lua_concat(lua_State
*L
,int n
){
5810 api_checknelems(L
,n
);
5813 luaV_concat(L
,n
,cast_int(L
->top
-L
->base
)-1);
5817 setsvalue(L
,L
->top
,luaS_newlstr(L
,"",0));
5821 static void*lua_newuserdata(lua_State
*L
,size_t size
){
5824 u
=luaS_newudata(L
,size
,getcurrenv(L
));
5825 setuvalue(L
,L
->top
,u
);
5829 #define luaL_getn(L,i)((int)lua_objlen(L,i))
5830 #define luaL_setn(L,i,j)((void)0)
5831 typedef struct luaL_Reg
{
5835 static void luaI_openlib(lua_State
*L
,const char*libname
,
5836 const luaL_Reg
*l
,int nup
);
5837 static int luaL_argerror(lua_State
*L
,int numarg
,const char*extramsg
);
5838 static const char* luaL_checklstring(lua_State
*L
,int numArg
,
5840 static const char* luaL_optlstring(lua_State
*L
,int numArg
,
5841 const char*def
,size_t*l
);
5842 static lua_Integer
luaL_checkinteger(lua_State
*L
,int numArg
);
5843 static lua_Integer
luaL_optinteger(lua_State
*L
,int nArg
,
5845 static int luaL_error(lua_State
*L
,const char*fmt
,...);
5846 static const char* luaL_findtable(lua_State
*L
,int idx
,
5847 const char*fname
,int szhint
);
5848 #define luaL_argcheck(L,cond,numarg,extramsg)((void)((cond)||luaL_argerror(L,(numarg),(extramsg))))
5849 #define luaL_checkstring(L,n)(luaL_checklstring(L,(n),NULL))
5850 #define luaL_optstring(L,n,d)(luaL_optlstring(L,(n),(d),NULL))
5851 #define luaL_checkint(L,n)((int)luaL_checkinteger(L,(n)))
5852 #define luaL_optint(L,n,d)((int)luaL_optinteger(L,(n),(d)))
5853 #define luaL_typename(L,i)lua_typename(L,lua_type(L,(i)))
5854 #define luaL_getmetatable(L,n)(lua_getfield(L,(-10000),(n)))
5855 #define luaL_opt(L,f,n,d)(lua_isnoneornil(L,(n))?(d):f(L,(n)))
5856 typedef struct luaL_Buffer
{
5860 char buffer
[BUFSIZ
];
5862 #define luaL_addchar(B,c)((void)((B)->p<((B)->buffer+BUFSIZ)||luaL_prepbuffer(B)),(*(B)->p++=(char)(c)))
5863 #define luaL_addsize(B,n)((B)->p+=(n))
5864 static char* luaL_prepbuffer(luaL_Buffer
*B
);
5865 static int luaL_argerror(lua_State
*L
,int narg
,const char*extramsg
){
5867 if(!lua_getstack(L
,0,&ar
))
5868 return luaL_error(L
,"bad argument #%d (%s)",narg
,extramsg
);
5869 lua_getinfo(L
,"n",&ar
);
5870 if(strcmp(ar
.namewhat
,"method")==0){
5873 return luaL_error(L
,"calling "LUA_QL("%s")" on bad self (%s)",
5878 return luaL_error(L
,"bad argument #%d to "LUA_QL("%s")" (%s)",
5879 narg
,ar
.name
,extramsg
);
5881 static int luaL_typerror(lua_State
*L
,int narg
,const char*tname
){
5882 const char*msg
=lua_pushfstring(L
,"%s expected, got %s",
5883 tname
,luaL_typename(L
,narg
));
5884 return luaL_argerror(L
,narg
,msg
);
5886 static void tag_error(lua_State
*L
,int narg
,int tag
){
5887 luaL_typerror(L
,narg
,lua_typename(L
,tag
));
5889 static void luaL_where(lua_State
*L
,int level
){
5891 if(lua_getstack(L
,level
,&ar
)){
5892 lua_getinfo(L
,"Sl",&ar
);
5893 if(ar
.currentline
>0){
5894 lua_pushfstring(L
,"%s:%d: ",ar
.short_src
,ar
.currentline
);
5898 lua_pushliteral(L
,"");
5900 static int luaL_error(lua_State
*L
,const char*fmt
,...){
5904 lua_pushvfstring(L
,fmt
,argp
);
5907 return lua_error(L
);
5909 static int luaL_newmetatable(lua_State
*L
,const char*tname
){
5910 lua_getfield(L
,(-10000),tname
);
5911 if(!lua_isnil(L
,-1))
5915 lua_pushvalue(L
,-1);
5916 lua_setfield(L
,(-10000),tname
);
5919 static void*luaL_checkudata(lua_State
*L
,int ud
,const char*tname
){
5920 void*p
=lua_touserdata(L
,ud
);
5922 if(lua_getmetatable(L
,ud
)){
5923 lua_getfield(L
,(-10000),tname
);
5924 if(lua_rawequal(L
,-1,-2)){
5930 luaL_typerror(L
,ud
,tname
);
5933 static void luaL_checkstack(lua_State
*L
,int space
,const char*mes
){
5934 if(!lua_checkstack(L
,space
))
5935 luaL_error(L
,"stack overflow (%s)",mes
);
5937 static void luaL_checktype(lua_State
*L
,int narg
,int t
){
5938 if(lua_type(L
,narg
)!=t
)
5939 tag_error(L
,narg
,t
);
5941 static void luaL_checkany(lua_State
*L
,int narg
){
5942 if(lua_type(L
,narg
)==(-1))
5943 luaL_argerror(L
,narg
,"value expected");
5945 static const char*luaL_checklstring(lua_State
*L
,int narg
,size_t*len
){
5946 const char*s
=lua_tolstring(L
,narg
,len
);
5947 if(!s
)tag_error(L
,narg
,4);
5950 static const char*luaL_optlstring(lua_State
*L
,int narg
,
5951 const char*def
,size_t*len
){
5952 if(lua_isnoneornil(L
,narg
)){
5954 *len
=(def
?strlen(def
):0);
5957 else return luaL_checklstring(L
,narg
,len
);
5959 static lua_Number
luaL_checknumber(lua_State
*L
,int narg
){
5960 lua_Number d
=lua_tonumber(L
,narg
);
5961 if(d
==0&&!lua_isnumber(L
,narg
))
5962 tag_error(L
,narg
,3);
5965 static lua_Integer
luaL_checkinteger(lua_State
*L
,int narg
){
5966 lua_Integer d
=lua_tointeger(L
,narg
);
5967 if(d
==0&&!lua_isnumber(L
,narg
))
5968 tag_error(L
,narg
,3);
5971 static lua_Integer
luaL_optinteger(lua_State
*L
,int narg
,
5973 return luaL_opt(L
,luaL_checkinteger
,narg
,def
);
5975 static int luaL_getmetafield(lua_State
*L
,int obj
,const char*event
){
5976 if(!lua_getmetatable(L
,obj
))
5978 lua_pushstring(L
,event
);
5980 if(lua_isnil(L
,-1)){
5989 static void luaL_register(lua_State
*L
,const char*libname
,
5991 luaI_openlib(L
,libname
,l
,0);
5993 static int libsize(const luaL_Reg
*l
){
5995 for(;l
->name
;l
++)size
++;
5998 static void luaI_openlib(lua_State
*L
,const char*libname
,
5999 const luaL_Reg
*l
,int nup
){
6001 int size
=libsize(l
);
6002 luaL_findtable(L
,(-10000),"_LOADED",1);
6003 lua_getfield(L
,-1,libname
);
6004 if(!lua_istable(L
,-1)){
6006 if(luaL_findtable(L
,(-10002),libname
,size
)!=NULL
)
6007 luaL_error(L
,"name conflict for module "LUA_QL("%s"),libname
);
6008 lua_pushvalue(L
,-1);
6009 lua_setfield(L
,-3,libname
);
6012 lua_insert(L
,-(nup
+1));
6017 lua_pushvalue(L
,-nup
);
6018 lua_pushcclosure(L
,l
->func
,nup
);
6019 lua_setfield(L
,-(nup
+2),l
->name
);
6023 static const char*luaL_findtable(lua_State
*L
,int idx
,
6024 const char*fname
,int szhint
){
6026 lua_pushvalue(L
,idx
);
6028 e
=strchr(fname
,'.');
6029 if(e
==NULL
)e
=fname
+strlen(fname
);
6030 lua_pushlstring(L
,fname
,e
-fname
);
6032 if(lua_isnil(L
,-1)){
6034 lua_createtable(L
,0,(*e
=='.'?1:szhint
));
6035 lua_pushlstring(L
,fname
,e
-fname
);
6036 lua_pushvalue(L
,-2);
6039 else if(!lua_istable(L
,-1)){
6048 #define bufflen(B)((B)->p-(B)->buffer)
6049 #define bufffree(B)((size_t)(BUFSIZ-bufflen(B)))
6050 static int emptybuffer(luaL_Buffer
*B
){
6051 size_t l
=bufflen(B
);
6054 lua_pushlstring(B
->L
,B
->buffer
,l
);
6060 static void adjuststack(luaL_Buffer
*B
){
6064 size_t toplen
=lua_strlen(L
,-1);
6066 size_t l
=lua_strlen(L
,-(toget
+1));
6067 if(B
->lvl
-toget
+1>=(20/2)||toplen
>l
){
6072 }while(toget
<B
->lvl
);
6073 lua_concat(L
,toget
);
6074 B
->lvl
=B
->lvl
-toget
+1;
6077 static char*luaL_prepbuffer(luaL_Buffer
*B
){
6082 static void luaL_addlstring(luaL_Buffer
*B
,const char*s
,size_t l
){
6084 luaL_addchar(B
,*s
++);
6086 static void luaL_pushresult(luaL_Buffer
*B
){
6088 lua_concat(B
->L
,B
->lvl
);
6091 static void luaL_addvalue(luaL_Buffer
*B
){
6094 const char*s
=lua_tolstring(L
,-1,&vl
);
6095 if(vl
<=bufffree(B
)){
6107 static void luaL_buffinit(lua_State
*L
,luaL_Buffer
*B
){
6112 typedef struct LoadF
{
6117 static const char*getF(lua_State
*L
,void*ud
,size_t*size
){
6118 LoadF
*lf
=(LoadF
*)ud
;
6125 if(feof(lf
->f
))return NULL
;
6126 *size
=fread(lf
->buff
,1,sizeof(lf
->buff
),lf
->f
);
6127 return(*size
>0)?lf
->buff
:NULL
;
6129 static int errfile(lua_State
*L
,const char*what
,int fnameindex
){
6130 const char*serr
=strerror(errno
);
6131 const char*filename
=lua_tostring(L
,fnameindex
)+1;
6132 lua_pushfstring(L
,"cannot %s %s: %s",what
,filename
,serr
);
6133 lua_remove(L
,fnameindex
);
6136 static int luaL_loadfile(lua_State
*L
,const char*filename
){
6138 int status
,readstatus
;
6140 int fnameindex
=lua_gettop(L
)+1;
6143 lua_pushliteral(L
,"=stdin");
6147 lua_pushfstring(L
,"@%s",filename
);
6148 lf
.f
=fopen(filename
,"r");
6149 if(lf
.f
==NULL
)return errfile(L
,"open",fnameindex
);
6154 while((c
=getc(lf
.f
))!=EOF
&&c
!='\n');
6155 if(c
=='\n')c
=getc(lf
.f
);
6157 if(c
=="\033Lua"[0]&&filename
){
6158 lf
.f
=freopen(filename
,"rb",lf
.f
);
6159 if(lf
.f
==NULL
)return errfile(L
,"reopen",fnameindex
);
6160 while((c
=getc(lf
.f
))!=EOF
&&c
!="\033Lua"[0]);
6164 status
=lua_load(L
,getF
,&lf
,lua_tostring(L
,-1));
6165 readstatus
=ferror(lf
.f
);
6166 if(filename
)fclose(lf
.f
);
6168 lua_settop(L
,fnameindex
);
6169 return errfile(L
,"read",fnameindex
);
6171 lua_remove(L
,fnameindex
);
6174 typedef struct LoadS
{
6178 static const char*getS(lua_State
*L
,void*ud
,size_t*size
){
6179 LoadS
*ls
=(LoadS
*)ud
;
6181 if(ls
->size
==0)return NULL
;
6186 static int luaL_loadbuffer(lua_State
*L
,const char*buff
,size_t size
,
6191 return lua_load(L
,getS
,&ls
,name
);
6193 static void*l_alloc(void*ud
,void*ptr
,size_t osize
,size_t nsize
){
6201 return realloc(ptr
,nsize
);
6203 static int panic(lua_State
*L
){
6205 fprintf(stderr
,"PANIC: unprotected error in call to Lua API (%s)\n",
6206 lua_tostring(L
,-1));
6209 static lua_State
*luaL_newstate(void){
6210 lua_State
*L
=lua_newstate(l_alloc
,NULL
);
6211 if(L
)lua_atpanic(L
,&panic
);
6214 static int luaB_tonumber(lua_State
*L
){
6215 int base
=luaL_optint(L
,2,10);
6218 if(lua_isnumber(L
,1)){
6219 lua_pushnumber(L
,lua_tonumber(L
,1));
6224 const char*s1
=luaL_checkstring(L
,1);
6227 luaL_argcheck(L
,2<=base
&&base
<=36,2,"base out of range");
6228 n
=strtoul(s1
,&s2
,base
);
6230 while(isspace((unsigned char)(*s2
)))s2
++;
6232 lua_pushnumber(L
,(lua_Number
)n
);
6240 static int luaB_error(lua_State
*L
){
6241 int level
=luaL_optint(L
,2,1);
6243 if(lua_isstring(L
,1)&&level
>0){
6244 luaL_where(L
,level
);
6248 return lua_error(L
);
6250 static int luaB_setmetatable(lua_State
*L
){
6251 int t
=lua_type(L
,2);
6252 luaL_checktype(L
,1,5);
6253 luaL_argcheck(L
,t
==0||t
==5,2,
6254 "nil or table expected");
6255 if(luaL_getmetafield(L
,1,"__metatable"))
6256 luaL_error(L
,"cannot change a protected metatable");
6258 lua_setmetatable(L
,1);
6261 static void getfunc(lua_State
*L
,int opt
){
6262 if(lua_isfunction(L
,1))lua_pushvalue(L
,1);
6265 int level
=opt
?luaL_optint(L
,1,1):luaL_checkint(L
,1);
6266 luaL_argcheck(L
,level
>=0,1,"level must be non-negative");
6267 if(lua_getstack(L
,level
,&ar
)==0)
6268 luaL_argerror(L
,1,"invalid level");
6269 lua_getinfo(L
,"f",&ar
);
6271 luaL_error(L
,"no function environment for tail call at level %d",
6275 static int luaB_setfenv(lua_State
*L
){
6276 luaL_checktype(L
,2,5);
6279 if(lua_isnumber(L
,1)&&lua_tonumber(L
,1)==0){
6285 else if(lua_iscfunction(L
,-2)||lua_setfenv(L
,-2)==0)
6287 LUA_QL("setfenv")" cannot change environment of given object");
6290 static int luaB_rawget(lua_State
*L
){
6291 luaL_checktype(L
,1,5);
6297 static int luaB_type(lua_State
*L
){
6299 lua_pushstring(L
,luaL_typename(L
,1));
6302 static int luaB_next(lua_State
*L
){
6303 luaL_checktype(L
,1,5);
6312 static int luaB_pairs(lua_State
*L
){
6313 luaL_checktype(L
,1,5);
6314 lua_pushvalue(L
,lua_upvalueindex(1));
6319 static int ipairsaux(lua_State
*L
){
6320 int i
=luaL_checkint(L
,2);
6321 luaL_checktype(L
,1,5);
6323 lua_pushinteger(L
,i
);
6325 return(lua_isnil(L
,-1))?0:2;
6327 static int luaB_ipairs(lua_State
*L
){
6328 luaL_checktype(L
,1,5);
6329 lua_pushvalue(L
,lua_upvalueindex(1));
6331 lua_pushinteger(L
,0);
6334 static int load_aux(lua_State
*L
,int status
){
6343 static int luaB_loadstring(lua_State
*L
){
6345 const char*s
=luaL_checklstring(L
,1,&l
);
6346 const char*chunkname
=luaL_optstring(L
,2,s
);
6347 return load_aux(L
,luaL_loadbuffer(L
,s
,l
,chunkname
));
6349 static int luaB_loadfile(lua_State
*L
){
6350 const char*fname
=luaL_optstring(L
,1,NULL
);
6351 return load_aux(L
,luaL_loadfile(L
,fname
));
6353 static int luaB_assert(lua_State
*L
){
6355 if(!lua_toboolean(L
,1))
6356 return luaL_error(L
,"%s",luaL_optstring(L
,2,"assertion failed!"));
6357 return lua_gettop(L
);
6359 static int luaB_unpack(lua_State
*L
){
6361 luaL_checktype(L
,1,5);
6362 i
=luaL_optint(L
,2,1);
6363 e
=luaL_opt(L
,luaL_checkint
,3,luaL_getn(L
,1));
6366 if(n
<=0||!lua_checkstack(L
,n
))
6367 return luaL_error(L
,"too many results to unpack");
6373 static int luaB_pcall(lua_State
*L
){
6376 status
=lua_pcall(L
,lua_gettop(L
)-1,(-1),0);
6377 lua_pushboolean(L
,(status
==0));
6379 return lua_gettop(L
);
6381 static int luaB_newproxy(lua_State
*L
){
6383 lua_newuserdata(L
,0);
6384 if(lua_toboolean(L
,1)==0)
6386 else if(lua_isboolean(L
,1)){
6388 lua_pushvalue(L
,-1);
6389 lua_pushboolean(L
,1);
6390 lua_rawset(L
,lua_upvalueindex(1));
6394 if(lua_getmetatable(L
,1)){
6395 lua_rawget(L
,lua_upvalueindex(1));
6396 validproxy
=lua_toboolean(L
,-1);
6399 luaL_argcheck(L
,validproxy
,1,"boolean or proxy expected");
6400 lua_getmetatable(L
,1);
6402 lua_setmetatable(L
,2);
6405 static const luaL_Reg base_funcs
[]={
6406 {"assert",luaB_assert
},
6407 {"error",luaB_error
},
6408 {"loadfile",luaB_loadfile
},
6409 {"loadstring",luaB_loadstring
},
6411 {"pcall",luaB_pcall
},
6412 {"rawget",luaB_rawget
},
6413 {"setfenv",luaB_setfenv
},
6414 {"setmetatable",luaB_setmetatable
},
6415 {"tonumber",luaB_tonumber
},
6417 {"unpack",luaB_unpack
},
6420 static void auxopen(lua_State
*L
,const char*name
,
6421 lua_CFunction f
,lua_CFunction u
){
6422 lua_pushcfunction(L
,u
);
6423 lua_pushcclosure(L
,f
,1);
6424 lua_setfield(L
,-2,name
);
6426 static void base_open(lua_State
*L
){
6427 lua_pushvalue(L
,(-10002));
6428 lua_setglobal(L
,"_G");
6429 luaL_register(L
,"_G",base_funcs
);
6430 lua_pushliteral(L
,"Lua 5.1");
6431 lua_setglobal(L
,"_VERSION");
6432 auxopen(L
,"ipairs",luaB_ipairs
,ipairsaux
);
6433 auxopen(L
,"pairs",luaB_pairs
,luaB_next
);
6434 lua_createtable(L
,0,1);
6435 lua_pushvalue(L
,-1);
6436 lua_setmetatable(L
,-2);
6437 lua_pushliteral(L
,"kv");
6438 lua_setfield(L
,-2,"__mode");
6439 lua_pushcclosure(L
,luaB_newproxy
,1);
6440 lua_setglobal(L
,"newproxy");
6442 static int luaopen_base(lua_State
*L
){
6446 #define aux_getn(L,n)(luaL_checktype(L,n,5),luaL_getn(L,n))
6447 static int tinsert(lua_State
*L
){
6448 int e
=aux_getn(L
,1)+1;
6450 switch(lua_gettop(L
)){
6457 pos
=luaL_checkint(L
,2);
6460 lua_rawgeti(L
,1,i
-1);
6466 return luaL_error(L
,"wrong number of arguments to "LUA_QL("insert"));
6470 lua_rawseti(L
,1,pos
);
6473 static int tremove(lua_State
*L
){
6474 int e
=aux_getn(L
,1);
6475 int pos
=luaL_optint(L
,2,e
);
6476 if(!(1<=pos
&&pos
<=e
))
6479 lua_rawgeti(L
,1,pos
);
6481 lua_rawgeti(L
,1,pos
+1);
6482 lua_rawseti(L
,1,pos
);
6488 static void addfield(lua_State
*L
,luaL_Buffer
*b
,int i
){
6490 if(!lua_isstring(L
,-1))
6491 luaL_error(L
,"invalid value (%s) at index %d in table for "
6492 LUA_QL("concat"),luaL_typename(L
,-1),i
);
6495 static int tconcat(lua_State
*L
){
6499 const char*sep
=luaL_optlstring(L
,2,"",&lsep
);
6500 luaL_checktype(L
,1,5);
6501 i
=luaL_optint(L
,3,1);
6502 last
=luaL_opt(L
,luaL_checkint
,4,luaL_getn(L
,1));
6503 luaL_buffinit(L
,&b
);
6506 luaL_addlstring(&b
,sep
,lsep
);
6510 luaL_pushresult(&b
);
6513 static void set2(lua_State
*L
,int i
,int j
){
6517 static int sort_comp(lua_State
*L
,int a
,int b
){
6518 if(!lua_isnil(L
,2)){
6521 lua_pushvalue(L
,a
-1);
6522 lua_pushvalue(L
,b
-2);
6524 res
=lua_toboolean(L
,-1);
6529 return lua_lessthan(L
,a
,b
);
6531 static void auxsort(lua_State
*L
,int l
,int u
){
6536 if(sort_comp(L
,-1,-2))
6544 if(sort_comp(L
,-2,-1))
6549 if(sort_comp(L
,-1,-2))
6556 lua_pushvalue(L
,-1);
6557 lua_rawgeti(L
,1,u
-1);
6561 while(lua_rawgeti(L
,1,++i
),sort_comp(L
,-1,-2)){
6562 if(i
>u
)luaL_error(L
,"invalid order function for sorting");
6565 while(lua_rawgeti(L
,1,--j
),sort_comp(L
,-3,-1)){
6566 if(j
<l
)luaL_error(L
,"invalid order function for sorting");
6575 lua_rawgeti(L
,1,u
-1);
6587 static int sort(lua_State
*L
){
6588 int n
=aux_getn(L
,1);
6589 luaL_checkstack(L
,40,"");
6590 if(!lua_isnoneornil(L
,2))
6591 luaL_checktype(L
,2,6);
6596 static const luaL_Reg tab_funcs
[]={
6603 static int luaopen_table(lua_State
*L
){
6604 luaL_register(L
,"table",tab_funcs
);
6607 static const char*const fnames
[]={"input","output"};
6608 static int pushresult(lua_State
*L
,int i
,const char*filename
){
6611 lua_pushboolean(L
,1);
6617 lua_pushfstring(L
,"%s: %s",filename
,strerror(en
));
6619 lua_pushfstring(L
,"%s",strerror(en
));
6620 lua_pushinteger(L
,en
);
6624 static void fileerror(lua_State
*L
,int arg
,const char*filename
){
6625 lua_pushfstring(L
,"%s: %s",filename
,strerror(errno
));
6626 luaL_argerror(L
,arg
,lua_tostring(L
,-1));
6628 #define tofilep(L)((FILE**)luaL_checkudata(L,1,"FILE*"))
6629 static int io_type(lua_State
*L
){
6632 ud
=lua_touserdata(L
,1);
6633 lua_getfield(L
,(-10000),"FILE*");
6634 if(ud
==NULL
||!lua_getmetatable(L
,1)||!lua_rawequal(L
,-2,-1))
6636 else if(*((FILE**)ud
)==NULL
)
6637 lua_pushliteral(L
,"closed file");
6639 lua_pushliteral(L
,"file");
6642 static FILE*tofile(lua_State
*L
){
6645 luaL_error(L
,"attempt to use a closed file");
6648 static FILE**newfile(lua_State
*L
){
6649 FILE**pf
=(FILE**)lua_newuserdata(L
,sizeof(FILE*));
6651 luaL_getmetatable(L
,"FILE*");
6652 lua_setmetatable(L
,-2);
6655 static int io_noclose(lua_State
*L
){
6657 lua_pushliteral(L
,"cannot close standard file");
6660 static int io_pclose(lua_State
*L
){
6662 int ok
=lua_pclose(L
,*p
);
6664 return pushresult(L
,ok
,NULL
);
6666 static int io_fclose(lua_State
*L
){
6668 int ok
=(fclose(*p
)==0);
6670 return pushresult(L
,ok
,NULL
);
6672 static int aux_close(lua_State
*L
){
6674 lua_getfield(L
,-1,"__close");
6675 return(lua_tocfunction(L
,-1))(L
);
6677 static int io_close(lua_State
*L
){
6679 lua_rawgeti(L
,(-10001),2);
6681 return aux_close(L
);
6683 static int io_gc(lua_State
*L
){
6689 static int io_open(lua_State
*L
){
6690 const char*filename
=luaL_checkstring(L
,1);
6691 const char*mode
=luaL_optstring(L
,2,"r");
6692 FILE**pf
=newfile(L
);
6693 *pf
=fopen(filename
,mode
);
6694 return(*pf
==NULL
)?pushresult(L
,0,filename
):1;
6696 static FILE*getiofile(lua_State
*L
,int findex
){
6698 lua_rawgeti(L
,(-10001),findex
);
6699 f
=*(FILE**)lua_touserdata(L
,-1);
6701 luaL_error(L
,"standard %s file is closed",fnames
[findex
-1]);
6704 static int g_iofile(lua_State
*L
,int f
,const char*mode
){
6705 if(!lua_isnoneornil(L
,1)){
6706 const char*filename
=lua_tostring(L
,1);
6708 FILE**pf
=newfile(L
);
6709 *pf
=fopen(filename
,mode
);
6711 fileerror(L
,1,filename
);
6717 lua_rawseti(L
,(-10001),f
);
6719 lua_rawgeti(L
,(-10001),f
);
6722 static int io_input(lua_State
*L
){
6723 return g_iofile(L
,1,"r");
6725 static int io_output(lua_State
*L
){
6726 return g_iofile(L
,2,"w");
6728 static int io_readline(lua_State
*L
);
6729 static void aux_lines(lua_State
*L
,int idx
,int toclose
){
6730 lua_pushvalue(L
,idx
);
6731 lua_pushboolean(L
,toclose
);
6732 lua_pushcclosure(L
,io_readline
,2);
6734 static int f_lines(lua_State
*L
){
6739 static int io_lines(lua_State
*L
){
6740 if(lua_isnoneornil(L
,1)){
6741 lua_rawgeti(L
,(-10001),1);
6745 const char*filename
=luaL_checkstring(L
,1);
6746 FILE**pf
=newfile(L
);
6747 *pf
=fopen(filename
,"r");
6749 fileerror(L
,1,filename
);
6750 aux_lines(L
,lua_gettop(L
),1);
6754 static int read_number(lua_State
*L
,FILE*f
){
6756 if(fscanf(f
,"%lf",&d
)==1){
6757 lua_pushnumber(L
,d
);
6765 static int test_eof(lua_State
*L
,FILE*f
){
6768 lua_pushlstring(L
,NULL
,0);
6771 static int read_line(lua_State
*L
,FILE*f
){
6773 luaL_buffinit(L
,&b
);
6776 char*p
=luaL_prepbuffer(&b
);
6777 if(fgets(p
,BUFSIZ
,f
)==NULL
){
6778 luaL_pushresult(&b
);
6779 return(lua_objlen(L
,-1)>0);
6782 if(l
==0||p
[l
-1]!='\n')
6785 luaL_addsize(&b
,l
-1);
6786 luaL_pushresult(&b
);
6791 static int read_chars(lua_State
*L
,FILE*f
,size_t n
){
6795 luaL_buffinit(L
,&b
);
6798 char*p
=luaL_prepbuffer(&b
);
6800 nr
=fread(p
,sizeof(char),rlen
,f
);
6801 luaL_addsize(&b
,nr
);
6803 }while(n
>0&&nr
==rlen
);
6804 luaL_pushresult(&b
);
6805 return(n
==0||lua_objlen(L
,-1)>0);
6807 static int g_read(lua_State
*L
,FILE*f
,int first
){
6808 int nargs
=lua_gettop(L
)-1;
6813 success
=read_line(L
,f
);
6817 luaL_checkstack(L
,nargs
+20,"too many arguments");
6819 for(n
=first
;nargs
--&&success
;n
++){
6820 if(lua_type(L
,n
)==3){
6821 size_t l
=(size_t)lua_tointeger(L
,n
);
6822 success
=(l
==0)?test_eof(L
,f
):read_chars(L
,f
,l
);
6825 const char*p
=lua_tostring(L
,n
);
6826 luaL_argcheck(L
,p
&&p
[0]=='*',n
,"invalid option");
6829 success
=read_number(L
,f
);
6832 success
=read_line(L
,f
);
6835 read_chars(L
,f
,~((size_t)0));
6839 return luaL_argerror(L
,n
,"invalid format");
6845 return pushresult(L
,0,NULL
);
6852 static int io_read(lua_State
*L
){
6853 return g_read(L
,getiofile(L
,1),1);
6855 static int f_read(lua_State
*L
){
6856 return g_read(L
,tofile(L
),2);
6858 static int io_readline(lua_State
*L
){
6859 FILE*f
=*(FILE**)lua_touserdata(L
,lua_upvalueindex(1));
6862 luaL_error(L
,"file is already closed");
6863 sucess
=read_line(L
,f
);
6865 return luaL_error(L
,"%s",strerror(errno
));
6868 if(lua_toboolean(L
,lua_upvalueindex(2))){
6870 lua_pushvalue(L
,lua_upvalueindex(1));
6876 static int g_write(lua_State
*L
,FILE*f
,int arg
){
6877 int nargs
=lua_gettop(L
)-1;
6879 for(;nargs
--;arg
++){
6880 if(lua_type(L
,arg
)==3){
6882 fprintf(f
,"%.14g",lua_tonumber(L
,arg
))>0;
6886 const char*s
=luaL_checklstring(L
,arg
,&l
);
6887 status
=status
&&(fwrite(s
,sizeof(char),l
,f
)==l
);
6890 return pushresult(L
,status
,NULL
);
6892 static int io_write(lua_State
*L
){
6893 return g_write(L
,getiofile(L
,2),1);
6895 static int f_write(lua_State
*L
){
6896 return g_write(L
,tofile(L
),2);
6898 static int io_flush(lua_State
*L
){
6899 return pushresult(L
,fflush(getiofile(L
,2))==0,NULL
);
6901 static int f_flush(lua_State
*L
){
6902 return pushresult(L
,fflush(tofile(L
))==0,NULL
);
6904 static const luaL_Reg iolib
[]={
6910 {"output",io_output
},
6916 static const luaL_Reg flib
[]={
6925 static void createmeta(lua_State
*L
){
6926 luaL_newmetatable(L
,"FILE*");
6927 lua_pushvalue(L
,-1);
6928 lua_setfield(L
,-2,"__index");
6929 luaL_register(L
,NULL
,flib
);
6931 static void createstdfile(lua_State
*L
,FILE*f
,int k
,const char*fname
){
6934 lua_pushvalue(L
,-1);
6935 lua_rawseti(L
,(-10001),k
);
6937 lua_pushvalue(L
,-2);
6939 lua_setfield(L
,-3,fname
);
6941 static void newfenv(lua_State
*L
,lua_CFunction cls
){
6942 lua_createtable(L
,0,1);
6943 lua_pushcfunction(L
,cls
);
6944 lua_setfield(L
,-2,"__close");
6946 static int luaopen_io(lua_State
*L
){
6948 newfenv(L
,io_fclose
);
6949 lua_replace(L
,(-10001));
6950 luaL_register(L
,"io",iolib
);
6951 newfenv(L
,io_noclose
);
6952 createstdfile(L
,stdin
,1,"stdin");
6953 createstdfile(L
,stdout
,2,"stdout");
6954 createstdfile(L
,stderr
,0,"stderr");
6956 lua_getfield(L
,-1,"popen");
6957 newfenv(L
,io_pclose
);
6962 static int os_pushresult(lua_State
*L
,int i
,const char*filename
){
6965 lua_pushboolean(L
,1);
6970 lua_pushfstring(L
,"%s: %s",filename
,strerror(en
));
6971 lua_pushinteger(L
,en
);
6975 static int os_remove(lua_State
*L
){
6976 const char*filename
=luaL_checkstring(L
,1);
6977 return os_pushresult(L
,remove(filename
)==0,filename
);
6979 static int os_exit(lua_State
*L
){
6980 exit(luaL_optint(L
,1,EXIT_SUCCESS
));
6982 static const luaL_Reg syslib
[]={
6984 {"remove",os_remove
},
6987 static int luaopen_os(lua_State
*L
){
6988 luaL_register(L
,"os",syslib
);
6991 #define uchar(c)((unsigned char)(c))
6992 static ptrdiff_t posrelat(ptrdiff_t pos
,size_t len
){
6993 if(pos
<0)pos
+=(ptrdiff_t)len
+1;
6994 return(pos
>=0)?pos
:0;
6996 static int str_sub(lua_State
*L
){
6998 const char*s
=luaL_checklstring(L
,1,&l
);
6999 ptrdiff_t start
=posrelat(luaL_checkinteger(L
,2),l
);
7000 ptrdiff_t end
=posrelat(luaL_optinteger(L
,3,-1),l
);
7002 if(end
>(ptrdiff_t)l
)end
=(ptrdiff_t)l
;
7004 lua_pushlstring(L
,s
+start
-1,end
-start
+1);
7005 else lua_pushliteral(L
,"");
7008 static int str_lower(lua_State
*L
){
7012 const char*s
=luaL_checklstring(L
,1,&l
);
7013 luaL_buffinit(L
,&b
);
7015 luaL_addchar(&b
,tolower(uchar(s
[i
])));
7016 luaL_pushresult(&b
);
7019 static int str_upper(lua_State
*L
){
7023 const char*s
=luaL_checklstring(L
,1,&l
);
7024 luaL_buffinit(L
,&b
);
7026 luaL_addchar(&b
,toupper(uchar(s
[i
])));
7027 luaL_pushresult(&b
);
7030 static int str_rep(lua_State
*L
){
7033 const char*s
=luaL_checklstring(L
,1,&l
);
7034 int n
=luaL_checkint(L
,2);
7035 luaL_buffinit(L
,&b
);
7037 luaL_addlstring(&b
,s
,l
);
7038 luaL_pushresult(&b
);
7041 static int str_byte(lua_State
*L
){
7043 const char*s
=luaL_checklstring(L
,1,&l
);
7044 ptrdiff_t posi
=posrelat(luaL_optinteger(L
,2,1),l
);
7045 ptrdiff_t pose
=posrelat(luaL_optinteger(L
,3,posi
),l
);
7048 if((size_t)pose
>l
)pose
=l
;
7049 if(posi
>pose
)return 0;
7050 n
=(int)(pose
-posi
+1);
7052 luaL_error(L
,"string slice too long");
7053 luaL_checkstack(L
,n
,"string slice too long");
7055 lua_pushinteger(L
,uchar(s
[posi
+i
-1]));
7058 static int str_char(lua_State
*L
){
7059 int n
=lua_gettop(L
);
7062 luaL_buffinit(L
,&b
);
7064 int c
=luaL_checkint(L
,i
);
7065 luaL_argcheck(L
,uchar(c
)==c
,i
,"invalid value");
7066 luaL_addchar(&b
,uchar(c
));
7068 luaL_pushresult(&b
);
7071 typedef struct MatchState
{
7072 const char*src_init
;
7081 static int check_capture(MatchState
*ms
,int l
){
7083 if(l
<0||l
>=ms
->level
||ms
->capture
[l
].len
==(-1))
7084 return luaL_error(ms
->L
,"invalid capture index");
7087 static int capture_to_close(MatchState
*ms
){
7088 int level
=ms
->level
;
7089 for(level
--;level
>=0;level
--)
7090 if(ms
->capture
[level
].len
==(-1))return level
;
7091 return luaL_error(ms
->L
,"invalid pattern capture");
7093 static const char*classend(MatchState
*ms
,const char*p
){
7097 luaL_error(ms
->L
,"malformed pattern (ends with "LUA_QL("%%")")");
7104 luaL_error(ms
->L
,"malformed pattern (missing "LUA_QL("]")")");
7105 if(*(p
++)=='%'&&*p
!='\0')
7115 static int match_class(int c
,int cl
){
7117 switch(tolower(cl
)){
7118 case'a':res
=isalpha(c
);break;
7119 case'c':res
=iscntrl(c
);break;
7120 case'd':res
=isdigit(c
);break;
7121 case'l':res
=islower(c
);break;
7122 case'p':res
=ispunct(c
);break;
7123 case's':res
=isspace(c
);break;
7124 case'u':res
=isupper(c
);break;
7125 case'w':res
=isalnum(c
);break;
7126 case'x':res
=isxdigit(c
);break;
7127 case'z':res
=(c
==0);break;
7128 default:return(cl
==c
);
7130 return(islower(cl
)?res
:!res
);
7132 static int matchbracketclass(int c
,const char*p
,const char*ec
){
7141 if(match_class(c
,uchar(*p
)))
7144 else if((*(p
+1)=='-')&&(p
+2<ec
)){
7146 if(uchar(*(p
-2))<=c
&&c
<=uchar(*p
))
7149 else if(uchar(*p
)==c
)return sig
;
7153 static int singlematch(int c
,const char*p
,const char*ep
){
7156 case'%':return match_class(c
,uchar(*(p
+1)));
7157 case'[':return matchbracketclass(c
,p
,ep
-1);
7158 default:return(uchar(*p
)==c
);
7161 static const char*match(MatchState
*ms
,const char*s
,const char*p
);
7162 static const char*matchbalance(MatchState
*ms
,const char*s
,
7164 if(*p
==0||*(p
+1)==0)
7165 luaL_error(ms
->L
,"unbalanced pattern");
7166 if(*s
!=*p
)return NULL
;
7171 while(++s
<ms
->src_end
){
7173 if(--cont
==0)return s
+1;
7175 else if(*s
==b
)cont
++;
7180 static const char*max_expand(MatchState
*ms
,const char*s
,
7181 const char*p
,const char*ep
){
7183 while((s
+i
)<ms
->src_end
&&singlematch(uchar(*(s
+i
)),p
,ep
))
7186 const char*res
=match(ms
,(s
+i
),ep
+1);
7192 static const char*min_expand(MatchState
*ms
,const char*s
,
7193 const char*p
,const char*ep
){
7195 const char*res
=match(ms
,s
,ep
+1);
7198 else if(s
<ms
->src_end
&&singlematch(uchar(*s
),p
,ep
))
7203 static const char*start_capture(MatchState
*ms
,const char*s
,
7204 const char*p
,int what
){
7206 int level
=ms
->level
;
7207 if(level
>=32)luaL_error(ms
->L
,"too many captures");
7208 ms
->capture
[level
].init
=s
;
7209 ms
->capture
[level
].len
=what
;
7211 if((res
=match(ms
,s
,p
))==NULL
)
7215 static const char*end_capture(MatchState
*ms
,const char*s
,
7217 int l
=capture_to_close(ms
);
7219 ms
->capture
[l
].len
=s
-ms
->capture
[l
].init
;
7220 if((res
=match(ms
,s
,p
))==NULL
)
7221 ms
->capture
[l
].len
=(-1);
7224 static const char*match_capture(MatchState
*ms
,const char*s
,int l
){
7226 l
=check_capture(ms
,l
);
7227 len
=ms
->capture
[l
].len
;
7228 if((size_t)(ms
->src_end
-s
)>=len
&&
7229 memcmp(ms
->capture
[l
].init
,s
,len
)==0)
7233 static const char*match(MatchState
*ms
,const char*s
,const char*p
){
7238 return start_capture(ms
,s
,p
+2,(-2));
7240 return start_capture(ms
,s
,p
+1,(-1));
7243 return end_capture(ms
,s
,p
+1);
7248 s
=matchbalance(ms
,s
,p
+2);
7249 if(s
==NULL
)return NULL
;
7253 const char*ep
;char previous
;
7256 luaL_error(ms
->L
,"missing "LUA_QL("[")" after "
7257 LUA_QL("%%f")" in pattern");
7259 previous
=(s
==ms
->src_init
)?'\0':*(s
-1);
7260 if(matchbracketclass(uchar(previous
),p
,ep
-1)||
7261 !matchbracketclass(uchar(*s
),p
,ep
-1))return NULL
;
7265 if(isdigit(uchar(*(p
+1)))){
7266 s
=match_capture(ms
,s
,uchar(*(p
+1)));
7267 if(s
==NULL
)return NULL
;
7279 return(s
==ms
->src_end
)?s
:NULL
;
7283 const char*ep
=classend(ms
,p
);
7284 int m
=s
<ms
->src_end
&&singlematch(uchar(*s
),p
,ep
);
7288 if(m
&&((res
=match(ms
,s
+1,ep
+1))!=NULL
))
7293 return max_expand(ms
,s
,p
,ep
);
7296 return(m
?max_expand(ms
,s
+1,p
,ep
):NULL
);
7299 return min_expand(ms
,s
,p
,ep
);
7309 static const char*lmemfind(const char*s1
,size_t l1
,
7310 const char*s2
,size_t l2
){
7312 else if(l2
>l1
)return NULL
;
7317 while(l1
>0&&(init
=(const char*)memchr(s1
,*s2
,l1
))!=NULL
){
7319 if(memcmp(init
,s2
+1,l2
)==0)
7329 static void push_onecapture(MatchState
*ms
,int i
,const char*s
,
7333 lua_pushlstring(ms
->L
,s
,e
-s
);
7335 luaL_error(ms
->L
,"invalid capture index");
7338 ptrdiff_t l
=ms
->capture
[i
].len
;
7339 if(l
==(-1))luaL_error(ms
->L
,"unfinished capture");
7341 lua_pushinteger(ms
->L
,ms
->capture
[i
].init
-ms
->src_init
+1);
7343 lua_pushlstring(ms
->L
,ms
->capture
[i
].init
,l
);
7346 static int push_captures(MatchState
*ms
,const char*s
,const char*e
){
7348 int nlevels
=(ms
->level
==0&&s
)?1:ms
->level
;
7349 luaL_checkstack(ms
->L
,nlevels
,"too many captures");
7350 for(i
=0;i
<nlevels
;i
++)
7351 push_onecapture(ms
,i
,s
,e
);
7354 static int str_find_aux(lua_State
*L
,int find
){
7356 const char*s
=luaL_checklstring(L
,1,&l1
);
7357 const char*p
=luaL_checklstring(L
,2,&l2
);
7358 ptrdiff_t init
=posrelat(luaL_optinteger(L
,3,1),l1
)-1;
7360 else if((size_t)(init
)>l1
)init
=(ptrdiff_t)l1
;
7361 if(find
&&(lua_toboolean(L
,4)||
7362 strpbrk(p
,"^$*+?.([%-")==NULL
)){
7363 const char*s2
=lmemfind(s
+init
,l1
-init
,p
,l2
);
7365 lua_pushinteger(L
,s2
-s
+1);
7366 lua_pushinteger(L
,s2
-s
+l2
);
7372 int anchor
=(*p
=='^')?(p
++,1):0;
7373 const char*s1
=s
+init
;
7380 if((res
=match(&ms
,s1
,p
))!=NULL
){
7382 lua_pushinteger(L
,s1
-s
+1);
7383 lua_pushinteger(L
,res
-s
);
7384 return push_captures(&ms
,NULL
,0)+2;
7387 return push_captures(&ms
,s1
,res
);
7389 }while(s1
++<ms
.src_end
&&!anchor
);
7394 static int str_find(lua_State
*L
){
7395 return str_find_aux(L
,1);
7397 static int str_match(lua_State
*L
){
7398 return str_find_aux(L
,0);
7400 static int gmatch_aux(lua_State
*L
){
7403 const char*s
=lua_tolstring(L
,lua_upvalueindex(1),&ls
);
7404 const char*p
=lua_tostring(L
,lua_upvalueindex(2));
7409 for(src
=s
+(size_t)lua_tointeger(L
,lua_upvalueindex(3));
7414 if((e
=match(&ms
,src
,p
))!=NULL
){
7415 lua_Integer newstart
=e
-s
;
7416 if(e
==src
)newstart
++;
7417 lua_pushinteger(L
,newstart
);
7418 lua_replace(L
,lua_upvalueindex(3));
7419 return push_captures(&ms
,src
,e
);
7424 static int gmatch(lua_State
*L
){
7425 luaL_checkstring(L
,1);
7426 luaL_checkstring(L
,2);
7428 lua_pushinteger(L
,0);
7429 lua_pushcclosure(L
,gmatch_aux
,3);
7432 static void add_s(MatchState
*ms
,luaL_Buffer
*b
,const char*s
,
7435 const char*news
=lua_tolstring(ms
->L
,3,&l
);
7438 luaL_addchar(b
,news
[i
]);
7441 if(!isdigit(uchar(news
[i
])))
7442 luaL_addchar(b
,news
[i
]);
7443 else if(news
[i
]=='0')
7444 luaL_addlstring(b
,s
,e
-s
);
7446 push_onecapture(ms
,news
[i
]-'1',s
,e
);
7452 static void add_value(MatchState
*ms
,luaL_Buffer
*b
,const char*s
,
7455 switch(lua_type(L
,3)){
7464 n
=push_captures(ms
,s
,e
);
7469 push_onecapture(ms
,0,s
,e
);
7474 if(!lua_toboolean(L
,-1)){
7476 lua_pushlstring(L
,s
,e
-s
);
7478 else if(!lua_isstring(L
,-1))
7479 luaL_error(L
,"invalid replacement value (a %s)",luaL_typename(L
,-1));
7482 static int str_gsub(lua_State
*L
){
7484 const char*src
=luaL_checklstring(L
,1,&srcl
);
7485 const char*p
=luaL_checkstring(L
,2);
7486 int tr
=lua_type(L
,3);
7487 int max_s
=luaL_optint(L
,4,srcl
+1);
7488 int anchor
=(*p
=='^')?(p
++,1):0;
7492 luaL_argcheck(L
,tr
==3||tr
==4||
7494 "string/function/table expected");
7495 luaL_buffinit(L
,&b
);
7498 ms
.src_end
=src
+srcl
;
7505 add_value(&ms
,&b
,src
,e
);
7509 else if(src
<ms
.src_end
)
7510 luaL_addchar(&b
,*src
++);
7514 luaL_addlstring(&b
,src
,ms
.src_end
-src
);
7515 luaL_pushresult(&b
);
7516 lua_pushinteger(L
,n
);
7519 static void addquoted(lua_State
*L
,luaL_Buffer
*b
,int arg
){
7521 const char*s
=luaL_checklstring(L
,arg
,&l
);
7522 luaL_addchar(b
,'"');
7525 case'"':case'\\':case'\n':{
7526 luaL_addchar(b
,'\\');
7531 luaL_addlstring(b
,"\\r",2);
7535 luaL_addlstring(b
,"\\000",4);
7545 luaL_addchar(b
,'"');
7547 static const char*scanformat(lua_State
*L
,const char*strfrmt
,char*form
){
7548 const char*p
=strfrmt
;
7549 while(*p
!='\0'&&strchr("-+ #0",*p
)!=NULL
)p
++;
7550 if((size_t)(p
-strfrmt
)>=sizeof("-+ #0"))
7551 luaL_error(L
,"invalid format (repeated flags)");
7552 if(isdigit(uchar(*p
)))p
++;
7553 if(isdigit(uchar(*p
)))p
++;
7556 if(isdigit(uchar(*p
)))p
++;
7557 if(isdigit(uchar(*p
)))p
++;
7559 if(isdigit(uchar(*p
)))
7560 luaL_error(L
,"invalid format (width or precision too long)");
7562 strncpy(form
,strfrmt
,p
-strfrmt
+1);
7567 static void addintlen(char*form
){
7568 size_t l
=strlen(form
);
7569 char spec
=form
[l
-1];
7570 strcpy(form
+l
-1,"l");
7571 form
[l
+sizeof("l")-2]=spec
;
7572 form
[l
+sizeof("l")-1]='\0';
7574 static int str_format(lua_State
*L
){
7575 int top
=lua_gettop(L
);
7578 const char*strfrmt
=luaL_checklstring(L
,arg
,&sfl
);
7579 const char*strfrmt_end
=strfrmt
+sfl
;
7581 luaL_buffinit(L
,&b
);
7582 while(strfrmt
<strfrmt_end
){
7584 luaL_addchar(&b
,*strfrmt
++);
7585 else if(*++strfrmt
=='%')
7586 luaL_addchar(&b
,*strfrmt
++);
7588 char form
[(sizeof("-+ #0")+sizeof("l")+10)];
7591 luaL_argerror(L
,arg
,"no value");
7592 strfrmt
=scanformat(L
,strfrmt
,form
);
7595 sprintf(buff
,form
,(int)luaL_checknumber(L
,arg
));
7600 sprintf(buff
,form
,(long)luaL_checknumber(L
,arg
));
7603 case'o':case'u':case'x':case'X':{
7605 sprintf(buff
,form
,(unsigned long)luaL_checknumber(L
,arg
));
7608 case'e':case'E':case'f':
7610 sprintf(buff
,form
,(double)luaL_checknumber(L
,arg
));
7614 addquoted(L
,&b
,arg
);
7619 const char*s
=luaL_checklstring(L
,arg
,&l
);
7620 if(!strchr(form
,'.')&&l
>=100){
7621 lua_pushvalue(L
,arg
);
7626 sprintf(buff
,form
,s
);
7631 return luaL_error(L
,"invalid option "LUA_QL("%%%c")" to "
7632 LUA_QL("format"),*(strfrmt
-1));
7635 luaL_addlstring(&b
,buff
,strlen(buff
));
7638 luaL_pushresult(&b
);
7641 static const luaL_Reg strlib
[]={
7645 {"format",str_format
},
7648 {"lower",str_lower
},
7649 {"match",str_match
},
7652 {"upper",str_upper
},
7655 static void createmetatable(lua_State
*L
){
7656 lua_createtable(L
,0,1);
7657 lua_pushliteral(L
,"");
7658 lua_pushvalue(L
,-2);
7659 lua_setmetatable(L
,-2);
7661 lua_pushvalue(L
,-2);
7662 lua_setfield(L
,-2,"__index");
7665 static int luaopen_string(lua_State
*L
){
7666 luaL_register(L
,"string",strlib
);
7670 static const luaL_Reg lualibs
[]={
7672 {"table",luaopen_table
},
7675 {"string",luaopen_string
},
7678 static void luaL_openlibs(lua_State
*L
){
7679 const luaL_Reg
*lib
=lualibs
;
7680 for(;lib
->func
;lib
++){
7681 lua_pushcfunction(L
,lib
->func
);
7682 lua_pushstring(L
,lib
->name
);
7686 int main(int argc
,char**argv
){
7687 lua_State
*L
=luaL_newstate();
7690 if(argc
<2)return sizeof(void*);
7691 lua_createtable(L
,0,1);
7692 lua_pushstring(L
,argv
[1]);
7693 lua_rawseti(L
,-2,0);
7694 lua_setglobal(L
,"arg");
7695 if(luaL_loadfile(L
,argv
[1]))
7698 lua_pushstring(L
,argv
[i
]);
7699 if(lua_pcall(L
,argc
-2,0,0)){
7701 fprintf(stderr
,"Error: %s\n",lua_tostring(L
,-1));