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 ******************************************************************************/
26 typedef unsigned __int64 U64
;
28 typedef unsigned long long U64
;
62 enum OpMode
{iABC
,iABx
,iAsBx
};
128 TK_DO
,TK_ELSE
,TK_ELSEIF
,TK_END
,TK_FALSE
,TK_FOR
,TK_FUNCTION
,
129 TK_IF
,TK_IN
,TK_LOCAL
,TK_NIL
,TK_NOT
,TK_OR
,TK_REPEAT
,
130 TK_RETURN
,TK_THEN
,TK_TRUE
,TK_UNTIL
,TK_WHILE
,
131 TK_CONCAT
,TK_DOTS
,TK_EQ
,TK_GE
,TK_LE
,TK_NE
,TK_NUMBER
,
132 TK_NAME
,TK_STRING
,TK_EOS
135 OPR_ADD
,OPR_SUB
,OPR_MUL
,OPR_DIV
,OPR_MOD
,OPR_POW
,
138 OPR_LT
,OPR_LE
,OPR_GT
,OPR_GE
,
142 typedef enum UnOpr
{OPR_MINUS
,OPR_NOT
,OPR_LEN
,OPR_NOUNOPR
}UnOpr
;
143 #define LUA_QL(x)"'"x"'"
144 #define luai_apicheck(L,o){(void)L;}
145 #define lua_number2str(s,n)sprintf((s),"%.14g",(n))
146 #define lua_str2number(s,p)strtod((s),(p))
147 #define luai_numadd(a,b)((a)+(b))
148 #define luai_numsub(a,b)((a)-(b))
149 #define luai_nummul(a,b)((a)*(b))
150 #define luai_numdiv(a,b)((a)/(b))
151 #define luai_nummod(a,b)((a)-floor((a)/(b))*(b))
152 #define luai_numpow(a,b)(pow(a,b))
153 #define luai_numunm(a)(-(a))
154 #define luai_numeq(a,b)((a)==(b))
155 #define luai_numlt(a,b)((a)<(b))
156 #define luai_numle(a,b)((a)<=(b))
157 #define luai_numisnan(a)(!luai_numeq((a),(a)))
158 #define lua_number2int(i,d)((i)=(int)(d))
159 #define lua_number2integer(i,d)((i)=(lua_Integer)(d))
160 #define LUAI_THROW(L,c)longjmp((c)->b,1)
161 #define LUAI_TRY(L,c,a)if(setjmp((c)->b)==0){a}
162 #define lua_pclose(L,file)((void)((void)L,file),0)
163 #define lua_upvalueindex(i)((-10002)-(i))
164 typedef struct lua_State lua_State
;
165 typedef int(*lua_CFunction
)(lua_State
*L
);
166 typedef const char*(*lua_Reader
)(lua_State
*L
,void*ud
,size_t*sz
);
167 typedef void*(*lua_Alloc
)(void*ud
,void*ptr
,size_t osize
,size_t nsize
);
168 typedef double lua_Number
;
169 typedef ptrdiff_t lua_Integer
;
170 static void lua_settop(lua_State
*L
,int idx
);
171 static int lua_type(lua_State
*L
,int idx
);
172 static const char* lua_tolstring(lua_State
*L
,int idx
,size_t*len
);
173 static size_t lua_objlen(lua_State
*L
,int idx
);
174 static void lua_pushlstring(lua_State
*L
,const char*s
,size_t l
);
175 static void lua_pushcclosure(lua_State
*L
,lua_CFunction fn
,int n
);
176 static void lua_createtable(lua_State
*L
,int narr
,int nrec
);
177 static void lua_setfield(lua_State
*L
,int idx
,const char*k
);
178 #define lua_pop(L,n)lua_settop(L,-(n)-1)
179 #define lua_newtable(L)lua_createtable(L,0,0)
180 #define lua_pushcfunction(L,f)lua_pushcclosure(L,(f),0)
181 #define lua_strlen(L,i)lua_objlen(L,(i))
182 #define lua_isfunction(L,n)(lua_type(L,(n))==6)
183 #define lua_istable(L,n)(lua_type(L,(n))==5)
184 #define lua_isnil(L,n)(lua_type(L,(n))==0)
185 #define lua_isboolean(L,n)(lua_type(L,(n))==1)
186 #define lua_isnone(L,n)(lua_type(L,(n))==(-1))
187 #define lua_isnoneornil(L,n)(lua_type(L,(n))<=0)
188 #define lua_pushliteral(L,s)lua_pushlstring(L,""s,(sizeof(s)/sizeof(char))-1)
189 #define lua_setglobal(L,s)lua_setfield(L,(-10002),(s))
190 #define lua_tostring(L,i)lua_tolstring(L,(i),NULL)
191 typedef struct lua_Debug lua_Debug
;
192 typedef void(*lua_Hook
)(lua_State
*L
,lua_Debug
*ar
);
206 typedef unsigned int lu_int32
;
207 typedef size_t lu_mem
;
208 typedef ptrdiff_t l_mem
;
209 typedef unsigned char lu_byte
;
210 #define IntPoint(p)((unsigned int)(lu_mem)(p))
211 typedef union{double u
;void*s
;long l
;}L_Umaxalign
;
212 typedef double l_uacNumber
;
213 #define check_exp(c,e)(e)
214 #define UNUSED(x)((void)(x))
215 #define cast(t,exp)((t)(exp))
216 #define cast_byte(i)cast(lu_byte,(i))
217 #define cast_num(i)cast(lua_Number,(i))
218 #define cast_int(i)cast(int,(i))
219 typedef lu_int32 Instruction
;
220 #define condhardstacktests(x)((void)0)
221 typedef union GCObject GCObject
;
222 typedef struct GCheader
{
223 GCObject
*next
;lu_byte tt
;lu_byte marked
;
231 typedef struct lua_TValue
{
234 #define ttisnil(o)(ttype(o)==0)
235 #define ttisnumber(o)(ttype(o)==3)
236 #define ttisstring(o)(ttype(o)==4)
237 #define ttistable(o)(ttype(o)==5)
238 #define ttisfunction(o)(ttype(o)==6)
239 #define ttisboolean(o)(ttype(o)==1)
240 #define ttisuserdata(o)(ttype(o)==7)
241 #define ttisthread(o)(ttype(o)==8)
242 #define ttislightuserdata(o)(ttype(o)==2)
243 #define ttype(o)((o)->tt)
244 #define gcvalue(o)check_exp(iscollectable(o),(o)->value.gc)
245 #define pvalue(o)check_exp(ttislightuserdata(o),(o)->value.p)
246 #define nvalue(o)check_exp(ttisnumber(o),(o)->value.n)
247 #define rawtsvalue(o)check_exp(ttisstring(o),&(o)->value.gc->ts)
248 #define tsvalue(o)(&rawtsvalue(o)->tsv)
249 #define rawuvalue(o)check_exp(ttisuserdata(o),&(o)->value.gc->u)
250 #define uvalue(o)(&rawuvalue(o)->uv)
251 #define clvalue(o)check_exp(ttisfunction(o),&(o)->value.gc->cl)
252 #define hvalue(o)check_exp(ttistable(o),&(o)->value.gc->h)
253 #define bvalue(o)check_exp(ttisboolean(o),(o)->value.b)
254 #define thvalue(o)check_exp(ttisthread(o),&(o)->value.gc->th)
255 #define l_isfalse(o)(ttisnil(o)||(ttisboolean(o)&&bvalue(o)==0))
256 #define checkconsistency(obj)
257 #define checkliveness(g,obj)
258 #define setnilvalue(obj)((obj)->tt=0)
259 #define setnvalue(obj,x){TValue*i_o=(obj);i_o->value.n=(x);i_o->tt=3;}
260 #define setbvalue(obj,x){TValue*i_o=(obj);i_o->value.b=(x);i_o->tt=1;}
261 #define setsvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=4;checkliveness(G(L),i_o);}
262 #define setuvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=7;checkliveness(G(L),i_o);}
263 #define setthvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=8;checkliveness(G(L),i_o);}
264 #define setclvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=6;checkliveness(G(L),i_o);}
265 #define sethvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=5;checkliveness(G(L),i_o);}
266 #define setptvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=(8+1);checkliveness(G(L),i_o);}
267 #define setobj(L,obj1,obj2){const TValue*o2=(obj2);TValue*o1=(obj1);o1->value=o2->value;o1->tt=o2->tt;checkliveness(G(L),o1);}
268 #define setttype(obj,tt)(ttype(obj)=(tt))
269 #define iscollectable(o)(ttype(o)>=4)
270 typedef TValue
*StkId
;
271 typedef union TString
{
274 GCObject
*next
;lu_byte tt
;lu_byte marked
;
280 #define getstr(ts)cast(const char*,(ts)+1)
281 #define svalue(o)getstr(rawtsvalue(o))
285 GCObject
*next
;lu_byte tt
;lu_byte marked
;
286 struct Table
*metatable
;
291 typedef struct Proto
{
292 GCObject
*next
;lu_byte tt
;lu_byte marked
;
297 struct LocVar
*locvars
;
312 lu_byte maxstacksize
;
314 typedef struct LocVar
{
319 typedef struct UpVal
{
320 GCObject
*next
;lu_byte tt
;lu_byte marked
;
330 typedef struct CClosure
{
331 GCObject
*next
;lu_byte tt
;lu_byte marked
;lu_byte isC
;lu_byte nupvalues
;GCObject
*gclist
;struct Table
*env
;
335 typedef struct LClosure
{
336 GCObject
*next
;lu_byte tt
;lu_byte marked
;lu_byte isC
;lu_byte nupvalues
;GCObject
*gclist
;struct Table
*env
;
340 typedef union Closure
{
344 #define iscfunction(o)(ttype(o)==6&&clvalue(o)->c.isC)
356 typedef struct Table
{
357 GCObject
*next
;lu_byte tt
;lu_byte marked
;
360 struct Table
*metatable
;
367 #define lmod(s,size)(check_exp((size&(size-1))==0,(cast(int,(s)&((size)-1)))))
368 #define twoto(x)((size_t)1<<(x))
369 #define sizenode(t)(twoto((t)->lsizenode))
370 static const TValue luaO_nilobject_
;
371 #define ceillog2(x)(luaO_log2((x)-1)+1)
372 static int luaO_log2(unsigned int x
);
373 #define gfasttm(g,et,e)((et)==NULL?NULL:((et)->flags&(1u<<(e)))?NULL:luaT_gettm(et,e,(g)->tmname[e]))
374 #define fasttm(l,et,e)gfasttm(G(l),et,e)
375 static const TValue
*luaT_gettm(Table
*events
,TMS event
,TString
*ename
);
376 #define luaM_reallocv(L,b,on,n,e)((cast(size_t,(n)+1)<=((size_t)(~(size_t)0)-2)/(e))?luaM_realloc_(L,(b),(on)*(e),(n)*(e)):luaM_toobig(L))
377 #define luaM_freemem(L,b,s)luaM_realloc_(L,(b),(s),0)
378 #define luaM_free(L,b)luaM_realloc_(L,(b),sizeof(*(b)),0)
379 #define luaM_freearray(L,b,n,t)luaM_reallocv(L,(b),n,0,sizeof(t))
380 #define luaM_malloc(L,t)luaM_realloc_(L,NULL,0,(t))
381 #define luaM_new(L,t)cast(t*,luaM_malloc(L,sizeof(t)))
382 #define luaM_newvector(L,n,t)cast(t*,luaM_reallocv(L,NULL,0,n,sizeof(t)))
383 #define luaM_growvector(L,v,nelems,size,t,limit,e)if((nelems)+1>(size))((v)=cast(t*,luaM_growaux_(L,v,&(size),sizeof(t),limit,e)))
384 #define luaM_reallocvector(L,v,oldn,n,t)((v)=cast(t*,luaM_reallocv(L,v,oldn,n,sizeof(t))))
385 static void*luaM_realloc_(lua_State
*L
,void*block
,size_t oldsize
,
387 static void*luaM_toobig(lua_State
*L
);
388 static void*luaM_growaux_(lua_State
*L
,void*block
,int*size
,
389 size_t size_elem
,int limit
,
390 const char*errormsg
);
391 typedef struct Zio ZIO
;
392 #define char2int(c)cast(int,cast(unsigned char,(c)))
393 #define zgetc(z)(((z)->n--)>0?char2int(*(z)->p++):luaZ_fill(z))
394 typedef struct Mbuffer
{
399 #define luaZ_initbuffer(L,buff)((buff)->buffer=NULL,(buff)->buffsize=0)
400 #define luaZ_buffer(buff)((buff)->buffer)
401 #define luaZ_sizebuffer(buff)((buff)->buffsize)
402 #define luaZ_bufflen(buff)((buff)->n)
403 #define luaZ_resetbuffer(buff)((buff)->n=0)
404 #define luaZ_resizebuffer(L,buff,size)(luaM_reallocvector(L,(buff)->buffer,(buff)->buffsize,size,char),(buff)->buffsize=size)
405 #define luaZ_freebuffer(L,buff)luaZ_resizebuffer(L,buff,0)
413 static int luaZ_fill(ZIO
*z
);
415 #define gt(L)(&L->l_gt)
416 #define registry(L)(&G(L)->l_registry)
417 typedef struct stringtable
{
422 typedef struct CallInfo
{
426 const Instruction
*savedpc
;
430 #define curr_func(L)(clvalue(L->ci->func))
431 #define ci_func(ci)(clvalue((ci)->func))
432 #define f_isLua(ci)(!ci_func(ci)->c.isC)
433 #define isLua(ci)(ttisfunction((ci)->func)&&f_isLua(ci))
434 typedef struct global_State
{
438 lu_byte currentwhite
;
456 struct lua_State
*mainthread
;
458 struct Table
*mt
[(8+1)];
459 TString
*tmname
[TM_N
];
462 GCObject
*next
;lu_byte tt
;lu_byte marked
;
468 const Instruction
*savedpc
;
475 unsigned short nCcalls
;
476 unsigned short baseCcalls
;
486 struct lua_longjmp
*errorJmp
;
500 #define rawgco2ts(o)check_exp((o)->gch.tt==4,&((o)->ts))
501 #define gco2ts(o)(&rawgco2ts(o)->tsv)
502 #define rawgco2u(o)check_exp((o)->gch.tt==7,&((o)->u))
503 #define gco2u(o)(&rawgco2u(o)->uv)
504 #define gco2cl(o)check_exp((o)->gch.tt==6,&((o)->cl))
505 #define gco2h(o)check_exp((o)->gch.tt==5,&((o)->h))
506 #define gco2p(o)check_exp((o)->gch.tt==(8+1),&((o)->p))
507 #define gco2uv(o)check_exp((o)->gch.tt==(8+2),&((o)->uv))
508 #define ngcotouv(o)check_exp((o)==NULL||(o)->gch.tt==(8+2),&((o)->uv))
509 #define gco2th(o)check_exp((o)->gch.tt==8,&((o)->th))
510 #define obj2gco(v)(cast(GCObject*,(v)))
511 static void luaE_freethread(lua_State
*L
,lua_State
*L1
);
512 #define pcRel(pc,p)(cast(int,(pc)-(p)->code)-1)
513 #define getline_(f,pc)(((f)->lineinfo)?(f)->lineinfo[pc]:0)
514 #define resethookcount(L)(L->hookcount=L->basehookcount)
515 static void luaG_typeerror(lua_State
*L
,const TValue
*o
,
517 static void luaG_runerror(lua_State
*L
,const char*fmt
,...);
518 #define luaD_checkstack(L,n)if((char*)L->stack_last-(char*)L->top<=(n)*(int)sizeof(TValue))luaD_growstack(L,n);else condhardstacktests(luaD_reallocstack(L,L->stacksize-5-1));
519 #define incr_top(L){luaD_checkstack(L,1);L->top++;}
520 #define savestack(L,p)((char*)(p)-(char*)L->stack)
521 #define restorestack(L,n)((TValue*)((char*)L->stack+(n)))
522 #define saveci(L,p)((char*)(p)-(char*)L->base_ci)
523 #define restoreci(L,n)((CallInfo*)((char*)L->base_ci+(n)))
524 typedef void(*Pfunc
)(lua_State
*L
,void*ud
);
525 static int luaD_poscall(lua_State
*L
,StkId firstResult
);
526 static void luaD_reallocCI(lua_State
*L
,int newsize
);
527 static void luaD_reallocstack(lua_State
*L
,int newsize
);
528 static void luaD_growstack(lua_State
*L
,int n
);
529 static void luaD_throw(lua_State
*L
,int errcode
);
530 static void*luaM_growaux_(lua_State
*L
,void*block
,int*size
,size_t size_elems
,
531 int limit
,const char*errormsg
){
536 luaG_runerror(L
,errormsg
);
544 newblock
=luaM_reallocv(L
,block
,*size
,newsize
,size_elems
);
548 static void*luaM_toobig(lua_State
*L
){
549 luaG_runerror(L
,"memory allocation error: block too big");
552 static void*luaM_realloc_(lua_State
*L
,void*block
,size_t osize
,size_t nsize
){
554 block
=(*g
->frealloc
)(g
->ud
,block
,osize
,nsize
);
555 if(block
==NULL
&&nsize
>0)
557 g
->totalbytes
=(g
->totalbytes
-osize
)+nsize
;
560 #define resetbits(x,m)((x)&=cast(lu_byte,~(m)))
561 #define setbits(x,m)((x)|=(m))
562 #define testbits(x,m)((x)&(m))
563 #define bitmask(b)(1<<(b))
564 #define bit2mask(b1,b2)(bitmask(b1)|bitmask(b2))
565 #define l_setbit(x,b)setbits(x,bitmask(b))
566 #define resetbit(x,b)resetbits(x,bitmask(b))
567 #define testbit(x,b)testbits(x,bitmask(b))
568 #define set2bits(x,b1,b2)setbits(x,(bit2mask(b1,b2)))
569 #define reset2bits(x,b1,b2)resetbits(x,(bit2mask(b1,b2)))
570 #define test2bits(x,b1,b2)testbits(x,(bit2mask(b1,b2)))
571 #define iswhite(x)test2bits((x)->gch.marked,0,1)
572 #define isblack(x)testbit((x)->gch.marked,2)
573 #define isgray(x)(!isblack(x)&&!iswhite(x))
574 #define otherwhite(g)(g->currentwhite^bit2mask(0,1))
575 #define isdead(g,v)((v)->gch.marked&otherwhite(g)&bit2mask(0,1))
576 #define changewhite(x)((x)->gch.marked^=bit2mask(0,1))
577 #define gray2black(x)l_setbit((x)->gch.marked,2)
578 #define valiswhite(x)(iscollectable(x)&&iswhite(gcvalue(x)))
579 #define luaC_white(g)cast(lu_byte,(g)->currentwhite&bit2mask(0,1))
580 #define luaC_checkGC(L){condhardstacktests(luaD_reallocstack(L,L->stacksize-5-1));if(G(L)->totalbytes>=G(L)->GCthreshold)luaC_step(L);}
581 #define luaC_barrier(L,p,v){if(valiswhite(v)&&isblack(obj2gco(p)))luaC_barrierf(L,obj2gco(p),gcvalue(v));}
582 #define luaC_barriert(L,t,v){if(valiswhite(v)&&isblack(obj2gco(t)))luaC_barrierback(L,t);}
583 #define luaC_objbarrier(L,p,o){if(iswhite(obj2gco(o))&&isblack(obj2gco(p)))luaC_barrierf(L,obj2gco(p),obj2gco(o));}
584 #define luaC_objbarriert(L,t,o){if(iswhite(obj2gco(o))&&isblack(obj2gco(t)))luaC_barrierback(L,t);}
585 static void luaC_step(lua_State
*L
);
586 static void luaC_link(lua_State
*L
,GCObject
*o
,lu_byte tt
);
587 static void luaC_linkupval(lua_State
*L
,UpVal
*uv
);
588 static void luaC_barrierf(lua_State
*L
,GCObject
*o
,GCObject
*v
);
589 static void luaC_barrierback(lua_State
*L
,Table
*t
);
590 #define sizestring(s)(sizeof(union TString)+((s)->len+1)*sizeof(char))
591 #define sizeudata(u)(sizeof(union Udata)+(u)->len)
592 #define luaS_new(L,s)(luaS_newlstr(L,s,strlen(s)))
593 #define luaS_newliteral(L,s)(luaS_newlstr(L,""s,(sizeof(s)/sizeof(char))-1))
594 #define luaS_fix(s)l_setbit((s)->tsv.marked,5)
595 static TString
*luaS_newlstr(lua_State
*L
,const char*str
,size_t l
);
596 #define tostring(L,o)((ttype(o)==4)||(luaV_tostring(L,o)))
597 #define tonumber(o,n)(ttype(o)==3||(((o)=luaV_tonumber(o,n))!=NULL))
598 #define equalobj(L,o1,o2)(ttype(o1)==ttype(o2)&&luaV_equalval(L,o1,o2))
599 static int luaV_equalval(lua_State
*L
,const TValue
*t1
,const TValue
*t2
);
600 static const TValue
*luaV_tonumber(const TValue
*obj
,TValue
*n
);
601 static int luaV_tostring(lua_State
*L
,StkId obj
);
602 static void luaV_execute(lua_State
*L
,int nexeccalls
);
603 static void luaV_concat(lua_State
*L
,int total
,int last
);
604 static const TValue luaO_nilobject_
={{NULL
},0};
605 static int luaO_int2fb(unsigned int x
){
612 else return((e
+1)<<3)|(cast_int(x
)-8);
614 static int luaO_fb2int(int x
){
617 else return((x
&7)+8)<<(e
-1);
619 static int luaO_log2(unsigned int x
){
620 static const lu_byte log_2
[256]={
621 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
622 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
623 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
624 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
625 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
626 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
627 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
628 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
631 while(x
>=256){l
+=8;x
>>=8;}
634 static int luaO_rawequalObj(const TValue
*t1
,const TValue
*t2
){
635 if(ttype(t1
)!=ttype(t2
))return 0;
636 else switch(ttype(t1
)){
640 return luai_numeq(nvalue(t1
),nvalue(t2
));
642 return bvalue(t1
)==bvalue(t2
);
644 return pvalue(t1
)==pvalue(t2
);
646 return gcvalue(t1
)==gcvalue(t2
);
649 static int luaO_str2d(const char*s
,lua_Number
*result
){
651 *result
=lua_str2number(s
,&endptr
);
652 if(endptr
==s
)return 0;
653 if(*endptr
=='x'||*endptr
=='X')
654 *result
=cast_num(strtoul(s
,&endptr
,16));
655 if(*endptr
=='\0')return 1;
656 while(isspace(cast(unsigned char,*endptr
)))endptr
++;
657 if(*endptr
!='\0')return 0;
660 static void pushstr(lua_State
*L
,const char*str
){
661 setsvalue(L
,L
->top
,luaS_new(L
,str
));
664 static const char*luaO_pushvfstring(lua_State
*L
,const char*fmt
,va_list argp
){
668 const char*e
=strchr(fmt
,'%');
670 setsvalue(L
,L
->top
,luaS_newlstr(L
,fmt
,e
-fmt
));
674 const char*s
=va_arg(argp
,char*);
675 if(s
==NULL
)s
="(null)";
681 buff
[0]=cast(char,va_arg(argp
,int));
687 setnvalue(L
->top
,cast_num(va_arg(argp
,int)));
692 setnvalue(L
->top
,cast_num(va_arg(argp
,l_uacNumber
)));
697 char buff
[4*sizeof(void*)+8];
698 sprintf(buff
,"%p",va_arg(argp
,void*));
719 luaV_concat(L
,n
+1,cast_int(L
->top
-L
->base
)-1);
721 return svalue(L
->top
-1);
723 static const char*luaO_pushfstring(lua_State
*L
,const char*fmt
,...){
727 msg
=luaO_pushvfstring(L
,fmt
,argp
);
731 static void luaO_chunkid(char*out
,const char*source
,size_t bufflen
){
733 strncpy(out
,source
+1,bufflen
);
740 bufflen
-=sizeof(" '...' ");
750 size_t len
=strcspn(source
,"\n\r");
751 bufflen
-=sizeof(" [string \"...\"] ");
752 if(len
>bufflen
)len
=bufflen
;
753 strcpy(out
,"[string \"");
754 if(source
[len
]!='\0'){
755 strncat(out
,source
,len
);
764 #define gnode(t,i)(&(t)->node[i])
765 #define gkey(n)(&(n)->i_key.nk)
766 #define gval(n)(&(n)->i_val)
767 #define gnext(n)((n)->i_key.nk.next)
768 #define key2tval(n)(&(n)->i_key.tvk)
769 static TValue
*luaH_setnum(lua_State
*L
,Table
*t
,int key
);
770 static const TValue
*luaH_getstr(Table
*t
,TString
*key
);
771 static TValue
*luaH_set(lua_State
*L
,Table
*t
,const TValue
*key
);
772 static const char*const luaT_typenames
[]={
773 "nil","boolean","userdata","number",
774 "string","table","function","userdata","thread",
777 static void luaT_init(lua_State
*L
){
778 static const char*const luaT_eventname
[]={
779 "__index","__newindex",
780 "__gc","__mode","__eq",
781 "__add","__sub","__mul","__div","__mod",
782 "__pow","__unm","__len","__lt","__le",
787 G(L
)->tmname
[i
]=luaS_new(L
,luaT_eventname
[i
]);
788 luaS_fix(G(L
)->tmname
[i
]);
791 static const TValue
*luaT_gettm(Table
*events
,TMS event
,TString
*ename
){
792 const TValue
*tm
=luaH_getstr(events
,ename
);
794 events
->flags
|=cast_byte(1u<<event
);
799 static const TValue
*luaT_gettmbyobj(lua_State
*L
,const TValue
*o
,TMS event
){
803 mt
=hvalue(o
)->metatable
;
806 mt
=uvalue(o
)->metatable
;
809 mt
=G(L
)->mt
[ttype(o
)];
811 return(mt
?luaH_getstr(mt
,G(L
)->tmname
[event
]):(&luaO_nilobject_
));
813 #define sizeCclosure(n)(cast(int,sizeof(CClosure))+cast(int,sizeof(TValue)*((n)-1)))
814 #define sizeLclosure(n)(cast(int,sizeof(LClosure))+cast(int,sizeof(TValue*)*((n)-1)))
815 static Closure
*luaF_newCclosure(lua_State
*L
,int nelems
,Table
*e
){
816 Closure
*c
=cast(Closure
*,luaM_malloc(L
,sizeCclosure(nelems
)));
817 luaC_link(L
,obj2gco(c
),6);
820 c
->c
.nupvalues
=cast_byte(nelems
);
823 static Closure
*luaF_newLclosure(lua_State
*L
,int nelems
,Table
*e
){
824 Closure
*c
=cast(Closure
*,luaM_malloc(L
,sizeLclosure(nelems
)));
825 luaC_link(L
,obj2gco(c
),6);
828 c
->l
.nupvalues
=cast_byte(nelems
);
829 while(nelems
--)c
->l
.upvals
[nelems
]=NULL
;
832 static UpVal
*luaF_newupval(lua_State
*L
){
833 UpVal
*uv
=luaM_new(L
,UpVal
);
834 luaC_link(L
,obj2gco(uv
),(8+2));
839 static UpVal
*luaF_findupval(lua_State
*L
,StkId level
){
841 GCObject
**pp
=&L
->openupval
;
844 while(*pp
!=NULL
&&(p
=ngcotouv(*pp
))->v
>=level
){
846 if(isdead(g
,obj2gco(p
)))
847 changewhite(obj2gco(p
));
852 uv
=luaM_new(L
,UpVal
);
854 uv
->marked
=luaC_white(g
);
858 uv
->u
.l
.prev
=&g
->uvhead
;
859 uv
->u
.l
.next
=g
->uvhead
.u
.l
.next
;
860 uv
->u
.l
.next
->u
.l
.prev
=uv
;
861 g
->uvhead
.u
.l
.next
=uv
;
864 static void unlinkupval(UpVal
*uv
){
865 uv
->u
.l
.next
->u
.l
.prev
=uv
->u
.l
.prev
;
866 uv
->u
.l
.prev
->u
.l
.next
=uv
->u
.l
.next
;
868 static void luaF_freeupval(lua_State
*L
,UpVal
*uv
){
869 if(uv
->v
!=&uv
->u
.value
)
873 static void luaF_close(lua_State
*L
,StkId level
){
876 while(L
->openupval
!=NULL
&&(uv
=ngcotouv(L
->openupval
))->v
>=level
){
877 GCObject
*o
=obj2gco(uv
);
878 L
->openupval
=uv
->next
;
880 luaF_freeupval(L
,uv
);
883 setobj(L
,&uv
->u
.value
,uv
->v
);
885 luaC_linkupval(L
,uv
);
889 static Proto
*luaF_newproto(lua_State
*L
){
890 Proto
*f
=luaM_new(L
,Proto
);
891 luaC_link(L
,obj2gco(f
),(8+1));
909 f
->lastlinedefined
=0;
913 static void luaF_freeproto(lua_State
*L
,Proto
*f
){
914 luaM_freearray(L
,f
->code
,f
->sizecode
,Instruction
);
915 luaM_freearray(L
,f
->p
,f
->sizep
,Proto
*);
916 luaM_freearray(L
,f
->k
,f
->sizek
,TValue
);
917 luaM_freearray(L
,f
->lineinfo
,f
->sizelineinfo
,int);
918 luaM_freearray(L
,f
->locvars
,f
->sizelocvars
,struct LocVar
);
919 luaM_freearray(L
,f
->upvalues
,f
->sizeupvalues
,TString
*);
922 static void luaF_freeclosure(lua_State
*L
,Closure
*c
){
923 int size
=(c
->c
.isC
)?sizeCclosure(c
->c
.nupvalues
):
924 sizeLclosure(c
->l
.nupvalues
);
925 luaM_freemem(L
,c
,size
);
927 #define MASK1(n,p)((~((~(Instruction)0)<<n))<<p)
928 #define MASK0(n,p)(~MASK1(n,p))
929 #define GET_OPCODE(i)(cast(OpCode,((i)>>0)&MASK1(6,0)))
930 #define SET_OPCODE(i,o)((i)=(((i)&MASK0(6,0))|((cast(Instruction,o)<<0)&MASK1(6,0))))
931 #define GETARG_A(i)(cast(int,((i)>>(0+6))&MASK1(8,0)))
932 #define SETARG_A(i,u)((i)=(((i)&MASK0(8,(0+6)))|((cast(Instruction,u)<<(0+6))&MASK1(8,(0+6)))))
933 #define GETARG_B(i)(cast(int,((i)>>(((0+6)+8)+9))&MASK1(9,0)))
934 #define SETARG_B(i,b)((i)=(((i)&MASK0(9,(((0+6)+8)+9)))|((cast(Instruction,b)<<(((0+6)+8)+9))&MASK1(9,(((0+6)+8)+9)))))
935 #define GETARG_C(i)(cast(int,((i)>>((0+6)+8))&MASK1(9,0)))
936 #define SETARG_C(i,b)((i)=(((i)&MASK0(9,((0+6)+8)))|((cast(Instruction,b)<<((0+6)+8))&MASK1(9,((0+6)+8)))))
937 #define GETARG_Bx(i)(cast(int,((i)>>((0+6)+8))&MASK1((9+9),0)))
938 #define SETARG_Bx(i,b)((i)=(((i)&MASK0((9+9),((0+6)+8)))|((cast(Instruction,b)<<((0+6)+8))&MASK1((9+9),((0+6)+8)))))
939 #define GETARG_sBx(i)(GETARG_Bx(i)-(((1<<(9+9))-1)>>1))
940 #define SETARG_sBx(i,b)SETARG_Bx((i),cast(unsigned int,(b)+(((1<<(9+9))-1)>>1)))
941 #define CREATE_ABC(o,a,b,c)((cast(Instruction,o)<<0)|(cast(Instruction,a)<<(0+6))|(cast(Instruction,b)<<(((0+6)+8)+9))|(cast(Instruction,c)<<((0+6)+8)))
942 #define CREATE_ABx(o,a,bc)((cast(Instruction,o)<<0)|(cast(Instruction,a)<<(0+6))|(cast(Instruction,bc)<<((0+6)+8)))
943 #define ISK(x)((x)&(1<<(9-1)))
944 #define INDEXK(r)((int)(r)&~(1<<(9-1)))
945 #define RKASK(x)((x)|(1<<(9-1)))
946 static const lu_byte luaP_opmodes
[(cast(int,OP_VARARG
)+1)];
947 #define getBMode(m)(cast(enum OpArgMask,(luaP_opmodes[m]>>4)&3))
948 #define getCMode(m)(cast(enum OpArgMask,(luaP_opmodes[m]>>2)&3))
949 #define testTMode(m)(luaP_opmodes[m]&(1<<7))
950 typedef struct expdesc
{
953 struct{int info
,aux
;}s
;
959 typedef struct upvaldesc
{
964 typedef struct FuncState
{
967 struct FuncState
*prev
;
979 upvaldesc upvalues
[60];
980 unsigned short actvar
[200];
982 static Proto
*luaY_parser(lua_State
*L
,ZIO
*z
,Mbuffer
*buff
,
985 struct lua_longjmp
*previous
;
989 static void luaD_seterrorobj(lua_State
*L
,int errcode
,StkId oldtop
){
992 setsvalue(L
,oldtop
,luaS_newliteral(L
,"not enough memory"));
996 setsvalue(L
,oldtop
,luaS_newliteral(L
,"error in error handling"));
1001 setobj(L
,oldtop
,L
->top
-1);
1007 static void restore_stack_limit(lua_State
*L
){
1008 if(L
->size_ci
>20000){
1009 int inuse
=cast_int(L
->ci
-L
->base_ci
);
1011 luaD_reallocCI(L
,20000);
1014 static void resetstack(lua_State
*L
,int status
){
1016 L
->base
=L
->ci
->base
;
1017 luaF_close(L
,L
->base
);
1018 luaD_seterrorobj(L
,status
,L
->base
);
1019 L
->nCcalls
=L
->baseCcalls
;
1021 restore_stack_limit(L
);
1025 static void luaD_throw(lua_State
*L
,int errcode
){
1027 L
->errorJmp
->status
=errcode
;
1028 LUAI_THROW(L
,L
->errorJmp
);
1031 L
->status
=cast_byte(errcode
);
1033 resetstack(L
,errcode
);
1039 static int luaD_rawrunprotected(lua_State
*L
,Pfunc f
,void*ud
){
1040 struct lua_longjmp lj
;
1042 lj
.previous
=L
->errorJmp
;
1047 L
->errorJmp
=lj
.previous
;
1050 static void correctstack(lua_State
*L
,TValue
*oldstack
){
1053 L
->top
=(L
->top
-oldstack
)+L
->stack
;
1054 for(up
=L
->openupval
;up
!=NULL
;up
=up
->gch
.next
)
1055 gco2uv(up
)->v
=(gco2uv(up
)->v
-oldstack
)+L
->stack
;
1056 for(ci
=L
->base_ci
;ci
<=L
->ci
;ci
++){
1057 ci
->top
=(ci
->top
-oldstack
)+L
->stack
;
1058 ci
->base
=(ci
->base
-oldstack
)+L
->stack
;
1059 ci
->func
=(ci
->func
-oldstack
)+L
->stack
;
1061 L
->base
=(L
->base
-oldstack
)+L
->stack
;
1063 static void luaD_reallocstack(lua_State
*L
,int newsize
){
1064 TValue
*oldstack
=L
->stack
;
1065 int realsize
=newsize
+1+5;
1066 luaM_reallocvector(L
,L
->stack
,L
->stacksize
,realsize
,TValue
);
1067 L
->stacksize
=realsize
;
1068 L
->stack_last
=L
->stack
+newsize
;
1069 correctstack(L
,oldstack
);
1071 static void luaD_reallocCI(lua_State
*L
,int newsize
){
1072 CallInfo
*oldci
=L
->base_ci
;
1073 luaM_reallocvector(L
,L
->base_ci
,L
->size_ci
,newsize
,CallInfo
);
1075 L
->ci
=(L
->ci
-oldci
)+L
->base_ci
;
1076 L
->end_ci
=L
->base_ci
+L
->size_ci
-1;
1078 static void luaD_growstack(lua_State
*L
,int n
){
1080 luaD_reallocstack(L
,2*L
->stacksize
);
1082 luaD_reallocstack(L
,L
->stacksize
+n
);
1084 static CallInfo
*growCI(lua_State
*L
){
1085 if(L
->size_ci
>20000)
1088 luaD_reallocCI(L
,2*L
->size_ci
);
1089 if(L
->size_ci
>20000)
1090 luaG_runerror(L
,"stack overflow");
1094 static StkId
adjust_varargs(lua_State
*L
,Proto
*p
,int actual
){
1096 int nfixargs
=p
->numparams
;
1099 for(;actual
<nfixargs
;++actual
)
1100 setnilvalue(L
->top
++);
1101 fixed
=L
->top
-actual
;
1103 for(i
=0;i
<nfixargs
;i
++){
1104 setobj(L
,L
->top
++,fixed
+i
);
1105 setnilvalue(fixed
+i
);
1108 sethvalue(L
,L
->top
++,htab
);
1112 static StkId
tryfuncTM(lua_State
*L
,StkId func
){
1113 const TValue
*tm
=luaT_gettmbyobj(L
,func
,TM_CALL
);
1115 ptrdiff_t funcr
=savestack(L
,func
);
1116 if(!ttisfunction(tm
))
1117 luaG_typeerror(L
,func
,"call");
1118 for(p
=L
->top
;p
>func
;p
--)setobj(L
,p
,p
-1);
1120 func
=restorestack(L
,funcr
);
1124 #define inc_ci(L)((L->ci==L->end_ci)?growCI(L):(condhardstacktests(luaD_reallocCI(L,L->size_ci)),++L->ci))
1125 static int luaD_precall(lua_State
*L
,StkId func
,int nresults
){
1128 if(!ttisfunction(func
))
1129 func
=tryfuncTM(L
,func
);
1130 funcr
=savestack(L
,func
);
1131 cl
=&clvalue(func
)->l
;
1132 L
->ci
->savedpc
=L
->savedpc
;
1137 luaD_checkstack(L
,p
->maxstacksize
+p
->numparams
);
1138 func
=restorestack(L
,funcr
);
1141 if(L
->top
>base
+p
->numparams
)
1142 L
->top
=base
+p
->numparams
;
1145 int nargs
=cast_int(L
->top
-func
)-1;
1146 base
=adjust_varargs(L
,p
,nargs
);
1147 func
=restorestack(L
,funcr
);
1151 L
->base
=ci
->base
=base
;
1152 ci
->top
=L
->base
+p
->maxstacksize
;
1155 ci
->nresults
=nresults
;
1156 for(st
=L
->top
;st
<ci
->top
;st
++)
1164 luaD_checkstack(L
,20);
1166 ci
->func
=restorestack(L
,funcr
);
1167 L
->base
=ci
->base
=ci
->func
+1;
1169 ci
->nresults
=nresults
;
1170 n
=(*curr_func(L
)->c
.f
)(L
);
1174 luaD_poscall(L
,L
->top
-n
);
1179 static int luaD_poscall(lua_State
*L
,StkId firstResult
){
1185 wanted
=ci
->nresults
;
1186 L
->base
=(ci
-1)->base
;
1187 L
->savedpc
=(ci
-1)->savedpc
;
1188 for(i
=wanted
;i
!=0&&firstResult
<L
->top
;i
--)
1189 setobj(L
,res
++,firstResult
++);
1193 return(wanted
-(-1));
1195 static void luaD_call(lua_State
*L
,StkId func
,int nResults
){
1196 if(++L
->nCcalls
>=200){
1198 luaG_runerror(L
,"C stack overflow");
1199 else if(L
->nCcalls
>=(200+(200>>3)))
1202 if(luaD_precall(L
,func
,nResults
)==0)
1207 static int luaD_pcall(lua_State
*L
,Pfunc func
,void*u
,
1208 ptrdiff_t old_top
,ptrdiff_t ef
){
1210 unsigned short oldnCcalls
=L
->nCcalls
;
1211 ptrdiff_t old_ci
=saveci(L
,L
->ci
);
1212 lu_byte old_allowhooks
=L
->allowhook
;
1213 ptrdiff_t old_errfunc
=L
->errfunc
;
1215 status
=luaD_rawrunprotected(L
,func
,u
);
1217 StkId oldtop
=restorestack(L
,old_top
);
1218 luaF_close(L
,oldtop
);
1219 luaD_seterrorobj(L
,status
,oldtop
);
1220 L
->nCcalls
=oldnCcalls
;
1221 L
->ci
=restoreci(L
,old_ci
);
1222 L
->base
=L
->ci
->base
;
1223 L
->savedpc
=L
->ci
->savedpc
;
1224 L
->allowhook
=old_allowhooks
;
1225 restore_stack_limit(L
);
1227 L
->errfunc
=old_errfunc
;
1235 static void f_parser(lua_State
*L
,void*ud
){
1239 struct SParser
*p
=cast(struct SParser
*,ud
);
1241 tf
=luaY_parser(L
,p
->z
,
1243 cl
=luaF_newLclosure(L
,tf
->nups
,hvalue(gt(L
)));
1245 for(i
=0;i
<tf
->nups
;i
++)
1246 cl
->l
.upvals
[i
]=luaF_newupval(L
);
1247 setclvalue(L
,L
->top
,cl
);
1250 static int luaD_protectedparser(lua_State
*L
,ZIO
*z
,const char*name
){
1254 luaZ_initbuffer(L
,&p
.buff
);
1255 status
=luaD_pcall(L
,f_parser
,&p
,savestack(L
,L
->top
),L
->errfunc
);
1256 luaZ_freebuffer(L
,&p
.buff
);
1259 static void luaS_resize(lua_State
*L
,int newsize
){
1263 if(G(L
)->gcstate
==2)
1265 newhash
=luaM_newvector(L
,newsize
,GCObject
*);
1267 for(i
=0;i
<newsize
;i
++)newhash
[i
]=NULL
;
1268 for(i
=0;i
<tb
->size
;i
++){
1269 GCObject
*p
=tb
->hash
[i
];
1271 GCObject
*next
=p
->gch
.next
;
1272 unsigned int h
=gco2ts(p
)->hash
;
1273 int h1
=lmod(h
,newsize
);
1274 p
->gch
.next
=newhash
[h1
];
1279 luaM_freearray(L
,tb
->hash
,tb
->size
,TString
*);
1283 static TString
*newlstr(lua_State
*L
,const char*str
,size_t l
,
1287 if(l
+1>(((size_t)(~(size_t)0)-2)-sizeof(TString
))/sizeof(char))
1289 ts
=cast(TString
*,luaM_malloc(L
,(l
+1)*sizeof(char)+sizeof(TString
)));
1292 ts
->tsv
.marked
=luaC_white(G(L
));
1295 memcpy(ts
+1,str
,l
*sizeof(char));
1296 ((char*)(ts
+1))[l
]='\0';
1299 ts
->tsv
.next
=tb
->hash
[h
];
1300 tb
->hash
[h
]=obj2gco(ts
);
1302 if(tb
->nuse
>cast(lu_int32
,tb
->size
)&&tb
->size
<=(INT_MAX
-2)/2)
1303 luaS_resize(L
,tb
->size
*2);
1306 static TString
*luaS_newlstr(lua_State
*L
,const char*str
,size_t l
){
1308 unsigned int h
=cast(unsigned int,l
);
1309 size_t step
=(l
>>5)+1;
1311 for(l1
=l
;l1
>=step
;l1
-=step
)
1312 h
=h
^((h
<<5)+(h
>>2)+cast(unsigned char,str
[l1
-1]));
1313 for(o
=G(L
)->strt
.hash
[lmod(h
,G(L
)->strt
.size
)];
1316 TString
*ts
=rawgco2ts(o
);
1317 if(ts
->tsv
.len
==l
&&(memcmp(str
,getstr(ts
),l
)==0)){
1318 if(isdead(G(L
),o
))changewhite(o
);
1322 return newlstr(L
,str
,l
,h
);
1324 static Udata
*luaS_newudata(lua_State
*L
,size_t s
,Table
*e
){
1326 if(s
>((size_t)(~(size_t)0)-2)-sizeof(Udata
))
1328 u
=cast(Udata
*,luaM_malloc(L
,s
+sizeof(Udata
)));
1329 u
->uv
.marked
=luaC_white(G(L
));
1332 u
->uv
.metatable
=NULL
;
1334 u
->uv
.next
=G(L
)->mainthread
->next
;
1335 G(L
)->mainthread
->next
=obj2gco(u
);
1338 #define hashpow2(t,n)(gnode(t,lmod((n),sizenode(t))))
1339 #define hashstr(t,str)hashpow2(t,(str)->tsv.hash)
1340 #define hashboolean(t,p)hashpow2(t,p)
1341 #define hashmod(t,n)(gnode(t,((n)%((sizenode(t)-1)|1))))
1342 #define hashpointer(t,p)hashmod(t,IntPoint(p))
1343 static const Node dummynode_
={
1347 static Node
*hashnum(const Table
*t
,lua_Number n
){
1348 unsigned int a
[cast_int(sizeof(lua_Number
)/sizeof(int))];
1352 memcpy(a
,&n
,sizeof(a
));
1353 for(i
=1;i
<cast_int(sizeof(lua_Number
)/sizeof(int));i
++)a
[0]+=a
[i
];
1354 return hashmod(t
,a
[0]);
1356 static Node
*mainposition(const Table
*t
,const TValue
*key
){
1359 return hashnum(t
,nvalue(key
));
1361 return hashstr(t
,rawtsvalue(key
));
1363 return hashboolean(t
,bvalue(key
));
1365 return hashpointer(t
,pvalue(key
));
1367 return hashpointer(t
,gcvalue(key
));
1370 static int arrayindex(const TValue
*key
){
1371 if(ttisnumber(key
)){
1372 lua_Number n
=nvalue(key
);
1374 lua_number2int(k
,n
);
1375 if(luai_numeq(cast_num(k
),n
))
1380 static int findindex(lua_State
*L
,Table
*t
,StkId key
){
1382 if(ttisnil(key
))return-1;
1384 if(0<i
&&i
<=t
->sizearray
)
1387 Node
*n
=mainposition(t
,key
);
1389 if(luaO_rawequalObj(key2tval(n
),key
)||
1390 (ttype(gkey(n
))==(8+3)&&iscollectable(key
)&&
1391 gcvalue(gkey(n
))==gcvalue(key
))){
1392 i
=cast_int(n
-gnode(t
,0));
1393 return i
+t
->sizearray
;
1397 luaG_runerror(L
,"invalid key to "LUA_QL("next"));
1401 static int luaH_next(lua_State
*L
,Table
*t
,StkId key
){
1402 int i
=findindex(L
,t
,key
);
1403 for(i
++;i
<t
->sizearray
;i
++){
1404 if(!ttisnil(&t
->array
[i
])){
1405 setnvalue(key
,cast_num(i
+1));
1406 setobj(L
,key
+1,&t
->array
[i
]);
1410 for(i
-=t
->sizearray
;i
<(int)sizenode(t
);i
++){
1411 if(!ttisnil(gval(gnode(t
,i
)))){
1412 setobj(L
,key
,key2tval(gnode(t
,i
)));
1413 setobj(L
,key
+1,gval(gnode(t
,i
)));
1419 static int computesizes(int nums
[],int*narray
){
1425 for(i
=0,twotoi
=1;twotoi
/2<*narray
;i
++,twotoi
*=2){
1433 if(a
==*narray
)break;
1438 static int countint(const TValue
*key
,int*nums
){
1439 int k
=arrayindex(key
);
1440 if(0<k
&&k
<=(1<<(32-2))){
1441 nums
[ceillog2(k
)]++;
1447 static int numusearray(const Table
*t
,int*nums
){
1452 for(lg
=0,ttlg
=1;lg
<=(32-2);lg
++,ttlg
*=2){
1455 if(lim
>t
->sizearray
){
1461 if(!ttisnil(&t
->array
[i
-1]))
1469 static int numusehash(const Table
*t
,int*nums
,int*pnasize
){
1475 if(!ttisnil(gval(n
))){
1476 ause
+=countint(key2tval(n
),nums
);
1483 static void setarrayvector(lua_State
*L
,Table
*t
,int size
){
1485 luaM_reallocvector(L
,t
->array
,t
->sizearray
,size
,TValue
);
1486 for(i
=t
->sizearray
;i
<size
;i
++)
1487 setnilvalue(&t
->array
[i
]);
1490 static void setnodevector(lua_State
*L
,Table
*t
,int size
){
1493 t
->node
=cast(Node
*,(&dummynode_
));
1498 lsize
=ceillog2(size
);
1500 luaG_runerror(L
,"table overflow");
1502 t
->node
=luaM_newvector(L
,size
,Node
);
1503 for(i
=0;i
<size
;i
++){
1506 setnilvalue(gkey(n
));
1507 setnilvalue(gval(n
));
1510 t
->lsizenode
=cast_byte(lsize
);
1511 t
->lastfree
=gnode(t
,size
);
1513 static void resize(lua_State
*L
,Table
*t
,int nasize
,int nhsize
){
1515 int oldasize
=t
->sizearray
;
1516 int oldhsize
=t
->lsizenode
;
1519 setarrayvector(L
,t
,nasize
);
1520 setnodevector(L
,t
,nhsize
);
1521 if(nasize
<oldasize
){
1522 t
->sizearray
=nasize
;
1523 for(i
=nasize
;i
<oldasize
;i
++){
1524 if(!ttisnil(&t
->array
[i
]))
1525 setobj(L
,luaH_setnum(L
,t
,i
+1),&t
->array
[i
]);
1527 luaM_reallocvector(L
,t
->array
,oldasize
,nasize
,TValue
);
1529 for(i
=twoto(oldhsize
)-1;i
>=0;i
--){
1531 if(!ttisnil(gval(old
)))
1532 setobj(L
,luaH_set(L
,t
,key2tval(old
)),gval(old
));
1534 if(nold
!=(&dummynode_
))
1535 luaM_freearray(L
,nold
,twoto(oldhsize
),Node
);
1537 static void luaH_resizearray(lua_State
*L
,Table
*t
,int nasize
){
1538 int nsize
=(t
->node
==(&dummynode_
))?0:sizenode(t
);
1539 resize(L
,t
,nasize
,nsize
);
1541 static void rehash(lua_State
*L
,Table
*t
,const TValue
*ek
){
1546 for(i
=0;i
<=(32-2);i
++)nums
[i
]=0;
1547 nasize
=numusearray(t
,nums
);
1549 totaluse
+=numusehash(t
,nums
,&nasize
);
1550 nasize
+=countint(ek
,nums
);
1552 na
=computesizes(nums
,&nasize
);
1553 resize(L
,t
,nasize
,totaluse
-na
);
1555 static Table
*luaH_new(lua_State
*L
,int narray
,int nhash
){
1556 Table
*t
=luaM_new(L
,Table
);
1557 luaC_link(L
,obj2gco(t
),5);
1559 t
->flags
=cast_byte(~0);
1563 t
->node
=cast(Node
*,(&dummynode_
));
1564 setarrayvector(L
,t
,narray
);
1565 setnodevector(L
,t
,nhash
);
1568 static void luaH_free(lua_State
*L
,Table
*t
){
1569 if(t
->node
!=(&dummynode_
))
1570 luaM_freearray(L
,t
->node
,sizenode(t
),Node
);
1571 luaM_freearray(L
,t
->array
,t
->sizearray
,TValue
);
1574 static Node
*getfreepos(Table
*t
){
1575 while(t
->lastfree
-->t
->node
){
1576 if(ttisnil(gkey(t
->lastfree
)))
1581 static TValue
*newkey(lua_State
*L
,Table
*t
,const TValue
*key
){
1582 Node
*mp
=mainposition(t
,key
);
1583 if(!ttisnil(gval(mp
))||mp
==(&dummynode_
)){
1585 Node
*n
=getfreepos(t
);
1588 return luaH_set(L
,t
,key
);
1590 othern
=mainposition(t
,key2tval(mp
));
1592 while(gnext(othern
)!=mp
)othern
=gnext(othern
);
1596 setnilvalue(gval(mp
));
1604 gkey(mp
)->value
=key
->value
;gkey(mp
)->tt
=key
->tt
;
1605 luaC_barriert(L
,t
,key
);
1608 static const TValue
*luaH_getnum(Table
*t
,int key
){
1609 if(cast(unsigned int,key
)-1<cast(unsigned int,t
->sizearray
))
1610 return&t
->array
[key
-1];
1612 lua_Number nk
=cast_num(key
);
1613 Node
*n
=hashnum(t
,nk
);
1615 if(ttisnumber(gkey(n
))&&luai_numeq(nvalue(gkey(n
)),nk
))
1619 return(&luaO_nilobject_
);
1622 static const TValue
*luaH_getstr(Table
*t
,TString
*key
){
1623 Node
*n
=hashstr(t
,key
);
1625 if(ttisstring(gkey(n
))&&rawtsvalue(gkey(n
))==key
)
1629 return(&luaO_nilobject_
);
1631 static const TValue
*luaH_get(Table
*t
,const TValue
*key
){
1633 case 0:return(&luaO_nilobject_
);
1634 case 4:return luaH_getstr(t
,rawtsvalue(key
));
1637 lua_Number n
=nvalue(key
);
1638 lua_number2int(k
,n
);
1639 if(luai_numeq(cast_num(k
),nvalue(key
)))
1640 return luaH_getnum(t
,k
);
1644 Node
*n
=mainposition(t
,key
);
1646 if(luaO_rawequalObj(key2tval(n
),key
))
1650 return(&luaO_nilobject_
);
1654 static TValue
*luaH_set(lua_State
*L
,Table
*t
,const TValue
*key
){
1655 const TValue
*p
=luaH_get(t
,key
);
1657 if(p
!=(&luaO_nilobject_
))
1658 return cast(TValue
*,p
);
1660 if(ttisnil(key
))luaG_runerror(L
,"table index is nil");
1661 else if(ttisnumber(key
)&&luai_numisnan(nvalue(key
)))
1662 luaG_runerror(L
,"table index is NaN");
1663 return newkey(L
,t
,key
);
1666 static TValue
*luaH_setnum(lua_State
*L
,Table
*t
,int key
){
1667 const TValue
*p
=luaH_getnum(t
,key
);
1668 if(p
!=(&luaO_nilobject_
))
1669 return cast(TValue
*,p
);
1672 setnvalue(&k
,cast_num(key
));
1673 return newkey(L
,t
,&k
);
1676 static TValue
*luaH_setstr(lua_State
*L
,Table
*t
,TString
*key
){
1677 const TValue
*p
=luaH_getstr(t
,key
);
1678 if(p
!=(&luaO_nilobject_
))
1679 return cast(TValue
*,p
);
1682 setsvalue(L
,&k
,key
);
1683 return newkey(L
,t
,&k
);
1686 static int unbound_search(Table
*t
,unsigned int j
){
1689 while(!ttisnil(luaH_getnum(t
,j
))){
1692 if(j
>cast(unsigned int,(INT_MAX
-2))){
1694 while(!ttisnil(luaH_getnum(t
,i
)))i
++;
1699 unsigned int m
=(i
+j
)/2;
1700 if(ttisnil(luaH_getnum(t
,m
)))j
=m
;
1705 static int luaH_getn(Table
*t
){
1706 unsigned int j
=t
->sizearray
;
1707 if(j
>0&&ttisnil(&t
->array
[j
-1])){
1710 unsigned int m
=(i
+j
)/2;
1711 if(ttisnil(&t
->array
[m
-1]))j
=m
;
1716 else if(t
->node
==(&dummynode_
))
1718 else return unbound_search(t
,j
);
1720 #define makewhite(g,x)((x)->gch.marked=cast_byte(((x)->gch.marked&cast_byte(~(bitmask(2)|bit2mask(0,1))))|luaC_white(g)))
1721 #define white2gray(x)reset2bits((x)->gch.marked,0,1)
1722 #define black2gray(x)resetbit((x)->gch.marked,2)
1723 #define stringmark(s)reset2bits((s)->tsv.marked,0,1)
1724 #define isfinalized(u)testbit((u)->marked,3)
1725 #define markfinalized(u)l_setbit((u)->marked,3)
1726 #define markvalue(g,o){checkconsistency(o);if(iscollectable(o)&&iswhite(gcvalue(o)))reallymarkobject(g,gcvalue(o));}
1727 #define markobject(g,t){if(iswhite(obj2gco(t)))reallymarkobject(g,obj2gco(t));}
1728 #define setthreshold(g)(g->GCthreshold=(g->estimate/100)*g->gcpause)
1729 static void removeentry(Node
*n
){
1730 if(iscollectable(gkey(n
)))
1731 setttype(gkey(n
),(8+3));
1733 static void reallymarkobject(global_State
*g
,GCObject
*o
){
1740 Table
*mt
=gco2u(o
)->metatable
;
1742 if(mt
)markobject(g
,mt
);
1743 markobject(g
,gco2u(o
)->env
);
1749 if(uv
->v
==&uv
->u
.value
)
1754 gco2cl(o
)->c
.gclist
=g
->gray
;
1759 gco2h(o
)->gclist
=g
->gray
;
1764 gco2th(o
)->gclist
=g
->gray
;
1769 gco2p(o
)->gclist
=g
->gray
;
1776 static void marktmu(global_State
*g
){
1777 GCObject
*u
=g
->tmudata
;
1782 reallymarkobject(g
,u
);
1783 }while(u
!=g
->tmudata
);
1786 static size_t luaC_separateudata(lua_State
*L
,int all
){
1787 global_State
*g
=G(L
);
1789 GCObject
**p
=&g
->mainthread
->next
;
1791 while((curr
=*p
)!=NULL
){
1792 if(!(iswhite(curr
)||all
)||isfinalized(gco2u(curr
)))
1794 else if(fasttm(L
,gco2u(curr
)->metatable
,TM_GC
)==NULL
){
1795 markfinalized(gco2u(curr
));
1799 deadmem
+=sizeudata(gco2u(curr
));
1800 markfinalized(gco2u(curr
));
1802 if(g
->tmudata
==NULL
)
1803 g
->tmudata
=curr
->gch
.next
=curr
;
1805 curr
->gch
.next
=g
->tmudata
->gch
.next
;
1806 g
->tmudata
->gch
.next
=curr
;
1813 static int traversetable(global_State
*g
,Table
*h
){
1819 markobject(g
,h
->metatable
);
1820 mode
=gfasttm(g
,h
->metatable
,TM_MODE
);
1821 if(mode
&&ttisstring(mode
)){
1822 weakkey
=(strchr(svalue(mode
),'k')!=NULL
);
1823 weakvalue
=(strchr(svalue(mode
),'v')!=NULL
);
1824 if(weakkey
||weakvalue
){
1825 h
->marked
&=~(bitmask(3)|bitmask(4));
1826 h
->marked
|=cast_byte((weakkey
<<3)|
1832 if(weakkey
&&weakvalue
)return 1;
1836 markvalue(g
,&h
->array
[i
]);
1841 if(ttisnil(gval(n
)))
1844 if(!weakkey
)markvalue(g
,gkey(n
));
1845 if(!weakvalue
)markvalue(g
,gval(n
));
1848 return weakkey
||weakvalue
;
1850 static void traverseproto(global_State
*g
,Proto
*f
){
1852 if(f
->source
)stringmark(f
->source
);
1853 for(i
=0;i
<f
->sizek
;i
++)
1854 markvalue(g
,&f
->k
[i
]);
1855 for(i
=0;i
<f
->sizeupvalues
;i
++){
1857 stringmark(f
->upvalues
[i
]);
1859 for(i
=0;i
<f
->sizep
;i
++){
1861 markobject(g
,f
->p
[i
]);
1863 for(i
=0;i
<f
->sizelocvars
;i
++){
1864 if(f
->locvars
[i
].varname
)
1865 stringmark(f
->locvars
[i
].varname
);
1868 static void traverseclosure(global_State
*g
,Closure
*cl
){
1869 markobject(g
,cl
->c
.env
);
1872 for(i
=0;i
<cl
->c
.nupvalues
;i
++)
1873 markvalue(g
,&cl
->c
.upvalue
[i
]);
1877 markobject(g
,cl
->l
.p
);
1878 for(i
=0;i
<cl
->l
.nupvalues
;i
++)
1879 markobject(g
,cl
->l
.upvals
[i
]);
1882 static void checkstacksizes(lua_State
*L
,StkId max
){
1883 int ci_used
=cast_int(L
->ci
-L
->base_ci
);
1884 int s_used
=cast_int(max
-L
->stack
);
1885 if(L
->size_ci
>20000)
1887 if(4*ci_used
<L
->size_ci
&&2*8<L
->size_ci
)
1888 luaD_reallocCI(L
,L
->size_ci
/2);
1889 condhardstacktests(luaD_reallocCI(L
,ci_used
+1));
1890 if(4*s_used
<L
->stacksize
&&
1891 2*((2*20)+5)<L
->stacksize
)
1892 luaD_reallocstack(L
,L
->stacksize
/2);
1893 condhardstacktests(luaD_reallocstack(L
,s_used
));
1895 static void traversestack(global_State
*g
,lua_State
*l
){
1900 for(ci
=l
->base_ci
;ci
<=l
->ci
;ci
++){
1901 if(lim
<ci
->top
)lim
=ci
->top
;
1903 for(o
=l
->stack
;o
<l
->top
;o
++)
1907 checkstacksizes(l
,lim
);
1909 static l_mem
propagatemark(global_State
*g
){
1916 if(traversetable(g
,h
))
1918 return sizeof(Table
)+sizeof(TValue
)*h
->sizearray
+
1919 sizeof(Node
)*sizenode(h
);
1922 Closure
*cl
=gco2cl(o
);
1923 g
->gray
=cl
->c
.gclist
;
1924 traverseclosure(g
,cl
);
1925 return(cl
->c
.isC
)?sizeCclosure(cl
->c
.nupvalues
):
1926 sizeLclosure(cl
->l
.nupvalues
);
1929 lua_State
*th
=gco2th(o
);
1931 th
->gclist
=g
->grayagain
;
1934 traversestack(g
,th
);
1935 return sizeof(lua_State
)+sizeof(TValue
)*th
->stacksize
+
1936 sizeof(CallInfo
)*th
->size_ci
;
1942 return sizeof(Proto
)+sizeof(Instruction
)*p
->sizecode
+
1943 sizeof(Proto
*)*p
->sizep
+
1944 sizeof(TValue
)*p
->sizek
+
1945 sizeof(int)*p
->sizelineinfo
+
1946 sizeof(LocVar
)*p
->sizelocvars
+
1947 sizeof(TString
*)*p
->sizeupvalues
;
1952 static size_t propagateall(global_State
*g
){
1954 while(g
->gray
)m
+=propagatemark(g
);
1957 static int iscleared(const TValue
*o
,int iskey
){
1958 if(!iscollectable(o
))return 0;
1960 stringmark(rawtsvalue(o
));
1963 return iswhite(gcvalue(o
))||
1964 (ttisuserdata(o
)&&(!iskey
&&isfinalized(uvalue(o
))));
1966 static void cleartable(GCObject
*l
){
1970 if(testbit(h
->marked
,4)){
1972 TValue
*o
=&h
->array
[i
];
1980 if(!ttisnil(gval(n
))&&
1981 (iscleared(key2tval(n
),1)||iscleared(gval(n
),0))){
1982 setnilvalue(gval(n
));
1989 static void freeobj(lua_State
*L
,GCObject
*o
){
1991 case(8+1):luaF_freeproto(L
,gco2p(o
));break;
1992 case 6:luaF_freeclosure(L
,gco2cl(o
));break;
1993 case(8+2):luaF_freeupval(L
,gco2uv(o
));break;
1994 case 5:luaH_free(L
,gco2h(o
));break;
1996 luaE_freethread(L
,gco2th(o
));
2001 luaM_freemem(L
,o
,sizestring(gco2ts(o
)));
2005 luaM_freemem(L
,o
,sizeudata(gco2u(o
)));
2011 #define sweepwholelist(L,p)sweeplist(L,p,((lu_mem)(~(lu_mem)0)-2))
2012 static GCObject
**sweeplist(lua_State
*L
,GCObject
**p
,lu_mem count
){
2014 global_State
*g
=G(L
);
2015 int deadmask
=otherwhite(g
);
2016 while((curr
=*p
)!=NULL
&&count
-->0){
2018 sweepwholelist(L
,&gco2th(curr
)->openupval
);
2019 if((curr
->gch
.marked
^bit2mask(0,1))&deadmask
){
2026 g
->rootgc
=curr
->gch
.next
;
2032 static void checkSizes(lua_State
*L
){
2033 global_State
*g
=G(L
);
2034 if(g
->strt
.nuse
<cast(lu_int32
,g
->strt
.size
/4)&&
2036 luaS_resize(L
,g
->strt
.size
/2);
2037 if(luaZ_sizebuffer(&g
->buff
)>32*2){
2038 size_t newsize
=luaZ_sizebuffer(&g
->buff
)/2;
2039 luaZ_resizebuffer(L
,&g
->buff
,newsize
);
2042 static void GCTM(lua_State
*L
){
2043 global_State
*g
=G(L
);
2044 GCObject
*o
=g
->tmudata
->gch
.next
;
2045 Udata
*udata
=rawgco2u(o
);
2050 g
->tmudata
->gch
.next
=udata
->uv
.next
;
2051 udata
->uv
.next
=g
->mainthread
->next
;
2052 g
->mainthread
->next
=o
;
2054 tm
=fasttm(L
,udata
->uv
.metatable
,TM_GC
);
2056 lu_byte oldah
=L
->allowhook
;
2057 lu_mem oldt
=g
->GCthreshold
;
2059 g
->GCthreshold
=2*g
->totalbytes
;
2060 setobj(L
,L
->top
,tm
);
2061 setuvalue(L
,L
->top
+1,udata
);
2063 luaD_call(L
,L
->top
-2,0);
2065 g
->GCthreshold
=oldt
;
2068 static void luaC_callGCTM(lua_State
*L
){
2069 while(G(L
)->tmudata
)
2072 static void luaC_freeall(lua_State
*L
){
2073 global_State
*g
=G(L
);
2075 g
->currentwhite
=bit2mask(0,1)|bitmask(6);
2076 sweepwholelist(L
,&g
->rootgc
);
2077 for(i
=0;i
<g
->strt
.size
;i
++)
2078 sweepwholelist(L
,&g
->strt
.hash
[i
]);
2080 static void markmt(global_State
*g
){
2082 for(i
=0;i
<(8+1);i
++)
2083 if(g
->mt
[i
])markobject(g
,g
->mt
[i
]);
2085 static void markroot(lua_State
*L
){
2086 global_State
*g
=G(L
);
2090 markobject(g
,g
->mainthread
);
2091 markvalue(g
,gt(g
->mainthread
));
2092 markvalue(g
,registry(L
));
2096 static void remarkupvals(global_State
*g
){
2098 for(uv
=g
->uvhead
.u
.l
.next
;uv
!=&g
->uvhead
;uv
=uv
->u
.l
.next
){
2099 if(isgray(obj2gco(uv
)))
2103 static void atomic(lua_State
*L
){
2104 global_State
*g
=G(L
);
2113 g
->gray
=g
->grayagain
;
2116 udsize
=luaC_separateudata(L
,0);
2118 udsize
+=propagateall(g
);
2119 cleartable(g
->weak
);
2120 g
->currentwhite
=cast_byte(otherwhite(g
));
2122 g
->sweepgc
=&g
->rootgc
;
2124 g
->estimate
=g
->totalbytes
-udsize
;
2126 static l_mem
singlestep(lua_State
*L
){
2127 global_State
*g
=G(L
);
2135 return propagatemark(g
);
2142 lu_mem old
=g
->totalbytes
;
2143 sweepwholelist(L
,&g
->strt
.hash
[g
->sweepstrgc
++]);
2144 if(g
->sweepstrgc
>=g
->strt
.size
)
2146 g
->estimate
-=old
-g
->totalbytes
;
2150 lu_mem old
=g
->totalbytes
;
2151 g
->sweepgc
=sweeplist(L
,g
->sweepgc
,40);
2152 if(*g
->sweepgc
==NULL
){
2156 g
->estimate
-=old
-g
->totalbytes
;
2175 static void luaC_step(lua_State
*L
){
2176 global_State
*g
=G(L
);
2177 l_mem lim
=(1024u/100)*g
->gcstepmul
;
2179 lim
=(((lu_mem
)(~(lu_mem
)0)-2)-1)/2;
2180 g
->gcdept
+=g
->totalbytes
-g
->GCthreshold
;
2188 g
->GCthreshold
=g
->totalbytes
+1024u;
2191 g
->GCthreshold
=g
->totalbytes
;
2198 static void luaC_barrierf(lua_State
*L
,GCObject
*o
,GCObject
*v
){
2199 global_State
*g
=G(L
);
2201 reallymarkobject(g
,v
);
2205 static void luaC_barrierback(lua_State
*L
,Table
*t
){
2206 global_State
*g
=G(L
);
2207 GCObject
*o
=obj2gco(t
);
2209 t
->gclist
=g
->grayagain
;
2212 static void luaC_link(lua_State
*L
,GCObject
*o
,lu_byte tt
){
2213 global_State
*g
=G(L
);
2214 o
->gch
.next
=g
->rootgc
;
2216 o
->gch
.marked
=luaC_white(g
);
2219 static void luaC_linkupval(lua_State
*L
,UpVal
*uv
){
2220 global_State
*g
=G(L
);
2221 GCObject
*o
=obj2gco(uv
);
2222 o
->gch
.next
=g
->rootgc
;
2227 luaC_barrier(L
,uv
,uv
->v
);
2238 typedef struct Token
{
2242 typedef struct LexState
{
2248 struct FuncState
*fs
;
2255 static void luaX_init(lua_State
*L
);
2256 static void luaX_lexerror(LexState
*ls
,const char*msg
,int token
);
2257 #define state_size(x)(sizeof(x)+0)
2258 #define fromstate(l)(cast(lu_byte*,(l))-0)
2259 #define tostate(l)(cast(lua_State*,cast(lu_byte*,l)+0))
2264 static void stack_init(lua_State
*L1
,lua_State
*L
){
2265 L1
->base_ci
=luaM_newvector(L
,8,CallInfo
);
2268 L1
->end_ci
=L1
->base_ci
+L1
->size_ci
-1;
2269 L1
->stack
=luaM_newvector(L
,(2*20)+5,TValue
);
2270 L1
->stacksize
=(2*20)+5;
2272 L1
->stack_last
=L1
->stack
+(L1
->stacksize
-5)-1;
2273 L1
->ci
->func
=L1
->top
;
2274 setnilvalue(L1
->top
++);
2275 L1
->base
=L1
->ci
->base
=L1
->top
;
2276 L1
->ci
->top
=L1
->top
+20;
2278 static void freestack(lua_State
*L
,lua_State
*L1
){
2279 luaM_freearray(L
,L1
->base_ci
,L1
->size_ci
,CallInfo
);
2280 luaM_freearray(L
,L1
->stack
,L1
->stacksize
,TValue
);
2282 static void f_luaopen(lua_State
*L
,void*ud
){
2283 global_State
*g
=G(L
);
2286 sethvalue(L
,gt(L
),luaH_new(L
,0,2));
2287 sethvalue(L
,registry(L
),luaH_new(L
,0,2));
2291 luaS_fix(luaS_newliteral(L
,"not enough memory"));
2292 g
->GCthreshold
=4*g
->totalbytes
;
2294 static void preinit_state(lua_State
*L
,global_State
*g
){
2306 L
->nCcalls
=L
->baseCcalls
=0;
2308 L
->base_ci
=L
->ci
=NULL
;
2313 static void close_state(lua_State
*L
){
2314 global_State
*g
=G(L
);
2315 luaF_close(L
,L
->stack
);
2317 luaM_freearray(L
,G(L
)->strt
.hash
,G(L
)->strt
.size
,TString
*);
2318 luaZ_freebuffer(L
,&g
->buff
);
2320 (*g
->frealloc
)(g
->ud
,fromstate(L
),state_size(LG
),0);
2322 static void luaE_freethread(lua_State
*L
,lua_State
*L1
){
2323 luaF_close(L1
,L1
->stack
);
2325 luaM_freemem(L
,fromstate(L1
),state_size(lua_State
));
2327 static lua_State
*lua_newstate(lua_Alloc f
,void*ud
){
2331 void*l
=(*f
)(ud
,NULL
,0,state_size(LG
));
2332 if(l
==NULL
)return NULL
;
2337 g
->currentwhite
=bit2mask(0,5);
2338 L
->marked
=luaC_white(g
);
2339 set2bits(L
->marked
,5,6);
2344 g
->uvhead
.u
.l
.prev
=&g
->uvhead
;
2345 g
->uvhead
.u
.l
.next
=&g
->uvhead
;
2350 setnilvalue(registry(L
));
2351 luaZ_initbuffer(L
,&g
->buff
);
2354 g
->rootgc
=obj2gco(L
);
2356 g
->sweepgc
=&g
->rootgc
;
2361 g
->totalbytes
=sizeof(LG
);
2365 for(i
=0;i
<(8+1);i
++)g
->mt
[i
]=NULL
;
2366 if(luaD_rawrunprotected(L
,f_luaopen
,NULL
)!=0){
2374 static void callallgcTM(lua_State
*L
,void*ud
){
2378 static void lua_close(lua_State
*L
){
2380 luaF_close(L
,L
->stack
);
2381 luaC_separateudata(L
,1);
2385 L
->base
=L
->top
=L
->ci
->base
;
2386 L
->nCcalls
=L
->baseCcalls
=0;
2387 }while(luaD_rawrunprotected(L
,callallgcTM
,NULL
)!=0);
2390 #define getcode(fs,e)((fs)->f->code[(e)->u.s.info])
2391 #define luaK_codeAsBx(fs,o,A,sBx)luaK_codeABx(fs,o,A,(sBx)+(((1<<(9+9))-1)>>1))
2392 #define luaK_setmultret(fs,e)luaK_setreturns(fs,e,(-1))
2393 static int luaK_codeABx(FuncState
*fs
,OpCode o
,int A
,unsigned int Bx
);
2394 static int luaK_codeABC(FuncState
*fs
,OpCode o
,int A
,int B
,int C
);
2395 static void luaK_setreturns(FuncState
*fs
,expdesc
*e
,int nresults
);
2396 static void luaK_patchtohere(FuncState
*fs
,int list
);
2397 static void luaK_concat(FuncState
*fs
,int*l1
,int l2
);
2398 static int currentpc(lua_State
*L
,CallInfo
*ci
){
2399 if(!isLua(ci
))return-1;
2401 ci
->savedpc
=L
->savedpc
;
2402 return pcRel(ci
->savedpc
,ci_func(ci
)->l
.p
);
2404 static int currentline(lua_State
*L
,CallInfo
*ci
){
2405 int pc
=currentpc(L
,ci
);
2409 return getline_(ci_func(ci
)->l
.p
,pc
);
2411 static int lua_getstack(lua_State
*L
,int level
,lua_Debug
*ar
){
2414 for(ci
=L
->ci
;level
>0&&ci
>L
->base_ci
;ci
--){
2417 level
-=ci
->tailcalls
;
2419 if(level
==0&&ci
>L
->base_ci
){
2421 ar
->i_ci
=cast_int(ci
-L
->base_ci
);
2430 static Proto
*getluaproto(CallInfo
*ci
){
2431 return(isLua(ci
)?ci_func(ci
)->l
.p
:NULL
);
2433 static void funcinfo(lua_Debug
*ar
,Closure
*cl
){
2437 ar
->lastlinedefined
=-1;
2441 ar
->source
=getstr(cl
->l
.p
->source
);
2442 ar
->linedefined
=cl
->l
.p
->linedefined
;
2443 ar
->lastlinedefined
=cl
->l
.p
->lastlinedefined
;
2444 ar
->what
=(ar
->linedefined
==0)?"main":"Lua";
2446 luaO_chunkid(ar
->short_src
,ar
->source
,60);
2448 static void info_tailcall(lua_Debug
*ar
){
2449 ar
->name
=ar
->namewhat
="";
2451 ar
->lastlinedefined
=ar
->linedefined
=ar
->currentline
=-1;
2452 ar
->source
="=(tail call)";
2453 luaO_chunkid(ar
->short_src
,ar
->source
,60);
2456 static void collectvalidlines(lua_State
*L
,Closure
*f
){
2457 if(f
==NULL
||f
->c
.isC
){
2458 setnilvalue(L
->top
);
2461 Table
*t
=luaH_new(L
,0,0);
2462 int*lineinfo
=f
->l
.p
->lineinfo
;
2464 for(i
=0;i
<f
->l
.p
->sizelineinfo
;i
++)
2465 setbvalue(luaH_setnum(L
,t
,lineinfo
[i
]),1);
2466 sethvalue(L
,L
->top
,t
);
2470 static int auxgetinfo(lua_State
*L
,const char*what
,lua_Debug
*ar
,
2471 Closure
*f
,CallInfo
*ci
){
2484 ar
->currentline
=(ci
)?currentline(L
,ci
):-1;
2488 ar
->nups
=f
->c
.nupvalues
;
2492 ar
->namewhat
=(ci
)?NULL
:NULL
;
2493 if(ar
->namewhat
==NULL
){
2507 static int lua_getinfo(lua_State
*L
,const char*what
,lua_Debug
*ar
){
2512 StkId func
=L
->top
-1;
2513 luai_apicheck(L
,ttisfunction(func
));
2518 else if(ar
->i_ci
!=0){
2519 ci
=L
->base_ci
+ar
->i_ci
;
2520 f
=clvalue(ci
->func
);
2522 status
=auxgetinfo(L
,what
,ar
,f
,ci
);
2523 if(strchr(what
,'f')){
2524 if(f
==NULL
)setnilvalue(L
->top
);
2525 else setclvalue(L
,L
->top
,f
);
2528 if(strchr(what
,'L'))
2529 collectvalidlines(L
,f
);
2532 static int isinstack(CallInfo
*ci
,const TValue
*o
){
2534 for(p
=ci
->base
;p
<ci
->top
;p
++)
2538 static void luaG_typeerror(lua_State
*L
,const TValue
*o
,const char*op
){
2539 const char*name
=NULL
;
2540 const char*t
=luaT_typenames
[ttype(o
)];
2541 const char*kind
=(isinstack(L
->ci
,o
))?
2545 luaG_runerror(L
,"attempt to %s %s "LUA_QL("%s")" (a %s value)",
2548 luaG_runerror(L
,"attempt to %s a %s value",op
,t
);
2550 static void luaG_concaterror(lua_State
*L
,StkId p1
,StkId p2
){
2551 if(ttisstring(p1
)||ttisnumber(p1
))p1
=p2
;
2552 luaG_typeerror(L
,p1
,"concatenate");
2554 static void luaG_aritherror(lua_State
*L
,const TValue
*p1
,const TValue
*p2
){
2556 if(luaV_tonumber(p1
,&temp
)==NULL
)
2558 luaG_typeerror(L
,p2
,"perform arithmetic on");
2560 static int luaG_ordererror(lua_State
*L
,const TValue
*p1
,const TValue
*p2
){
2561 const char*t1
=luaT_typenames
[ttype(p1
)];
2562 const char*t2
=luaT_typenames
[ttype(p2
)];
2564 luaG_runerror(L
,"attempt to compare two %s values",t1
);
2566 luaG_runerror(L
,"attempt to compare %s with %s",t1
,t2
);
2569 static void addinfo(lua_State
*L
,const char*msg
){
2573 int line
=currentline(L
,ci
);
2574 luaO_chunkid(buff
,getstr(getluaproto(ci
)->source
),60);
2575 luaO_pushfstring(L
,"%s:%d: %s",buff
,line
,msg
);
2578 static void luaG_errormsg(lua_State
*L
){
2580 StkId errfunc
=restorestack(L
,L
->errfunc
);
2581 if(!ttisfunction(errfunc
))luaD_throw(L
,5);
2582 setobj(L
,L
->top
,L
->top
-1);
2583 setobj(L
,L
->top
-1,errfunc
);
2585 luaD_call(L
,L
->top
-2,1);
2589 static void luaG_runerror(lua_State
*L
,const char*fmt
,...){
2592 addinfo(L
,luaO_pushvfstring(L
,fmt
,argp
));
2596 static int luaZ_fill(ZIO
*z
){
2600 buff
=z
->reader(L
,z
->data
,&size
);
2601 if(buff
==NULL
||size
==0)return(-1);
2604 return char2int(*(z
->p
++));
2606 static void luaZ_init(lua_State
*L
,ZIO
*z
,lua_Reader reader
,void*data
){
2613 static char*luaZ_openspace(lua_State
*L
,Mbuffer
*buff
,size_t n
){
2614 if(n
>buff
->buffsize
){
2616 luaZ_resizebuffer(L
,buff
,n
);
2618 return buff
->buffer
;
2620 #define opmode(t,a,b,c,m)(((t)<<7)|((a)<<6)|((b)<<4)|((c)<<2)|(m))
2621 static const lu_byte luaP_opmodes
[(cast(int,OP_VARARG
)+1)]={
2622 opmode(0,1,OpArgR
,OpArgN
,iABC
)
2623 ,opmode(0,1,OpArgK
,OpArgN
,iABx
)
2624 ,opmode(0,1,OpArgU
,OpArgU
,iABC
)
2625 ,opmode(0,1,OpArgR
,OpArgN
,iABC
)
2626 ,opmode(0,1,OpArgU
,OpArgN
,iABC
)
2627 ,opmode(0,1,OpArgK
,OpArgN
,iABx
)
2628 ,opmode(0,1,OpArgR
,OpArgK
,iABC
)
2629 ,opmode(0,0,OpArgK
,OpArgN
,iABx
)
2630 ,opmode(0,0,OpArgU
,OpArgN
,iABC
)
2631 ,opmode(0,0,OpArgK
,OpArgK
,iABC
)
2632 ,opmode(0,1,OpArgU
,OpArgU
,iABC
)
2633 ,opmode(0,1,OpArgR
,OpArgK
,iABC
)
2634 ,opmode(0,1,OpArgK
,OpArgK
,iABC
)
2635 ,opmode(0,1,OpArgK
,OpArgK
,iABC
)
2636 ,opmode(0,1,OpArgK
,OpArgK
,iABC
)
2637 ,opmode(0,1,OpArgK
,OpArgK
,iABC
)
2638 ,opmode(0,1,OpArgK
,OpArgK
,iABC
)
2639 ,opmode(0,1,OpArgK
,OpArgK
,iABC
)
2640 ,opmode(0,1,OpArgR
,OpArgN
,iABC
)
2641 ,opmode(0,1,OpArgR
,OpArgN
,iABC
)
2642 ,opmode(0,1,OpArgR
,OpArgN
,iABC
)
2643 ,opmode(0,1,OpArgR
,OpArgR
,iABC
)
2644 ,opmode(0,0,OpArgR
,OpArgN
,iAsBx
)
2645 ,opmode(1,0,OpArgK
,OpArgK
,iABC
)
2646 ,opmode(1,0,OpArgK
,OpArgK
,iABC
)
2647 ,opmode(1,0,OpArgK
,OpArgK
,iABC
)
2648 ,opmode(1,1,OpArgR
,OpArgU
,iABC
)
2649 ,opmode(1,1,OpArgR
,OpArgU
,iABC
)
2650 ,opmode(0,1,OpArgU
,OpArgU
,iABC
)
2651 ,opmode(0,1,OpArgU
,OpArgU
,iABC
)
2652 ,opmode(0,0,OpArgU
,OpArgN
,iABC
)
2653 ,opmode(0,1,OpArgR
,OpArgN
,iAsBx
)
2654 ,opmode(0,1,OpArgR
,OpArgN
,iAsBx
)
2655 ,opmode(1,0,OpArgN
,OpArgU
,iABC
)
2656 ,opmode(0,0,OpArgU
,OpArgU
,iABC
)
2657 ,opmode(0,0,OpArgN
,OpArgN
,iABC
)
2658 ,opmode(0,1,OpArgU
,OpArgN
,iABx
)
2659 ,opmode(0,1,OpArgU
,OpArgN
,iABC
)
2661 #define next(ls)(ls->current=zgetc(ls->z))
2662 #define currIsNewline(ls)(ls->current=='\n'||ls->current=='\r')
2663 static const char*const luaX_tokens
[]={
2664 "and","break","do","else","elseif",
2665 "end","false","for","function","if",
2666 "in","local","nil","not","or","repeat",
2667 "return","then","true","until","while",
2668 "..","...","==",">=","<=","~=",
2669 "<number>","<name>","<string>","<eof>",
2672 #define save_and_next(ls)(save(ls,ls->current),next(ls))
2673 static void save(LexState
*ls
,int c
){
2675 if(b
->n
+1>b
->buffsize
){
2677 if(b
->buffsize
>=((size_t)(~(size_t)0)-2)/2)
2678 luaX_lexerror(ls
,"lexical element too long",0);
2679 newsize
=b
->buffsize
*2;
2680 luaZ_resizebuffer(ls
->L
,b
,newsize
);
2682 b
->buffer
[b
->n
++]=cast(char,c
);
2684 static void luaX_init(lua_State
*L
){
2686 for(i
=0;i
<(cast(int,TK_WHILE
-257+1));i
++){
2687 TString
*ts
=luaS_new(L
,luaX_tokens
[i
]);
2689 ts
->tsv
.reserved
=cast_byte(i
+1);
2692 static const char*luaX_token2str(LexState
*ls
,int token
){
2694 return(iscntrl(token
))?luaO_pushfstring(ls
->L
,"char(%d)",token
):
2695 luaO_pushfstring(ls
->L
,"%c",token
);
2698 return luaX_tokens
[token
-257];
2700 static const char*txtToken(LexState
*ls
,int token
){
2706 return luaZ_buffer(ls
->buff
);
2708 return luaX_token2str(ls
,token
);
2711 static void luaX_lexerror(LexState
*ls
,const char*msg
,int token
){
2713 luaO_chunkid(buff
,getstr(ls
->source
),80);
2714 msg
=luaO_pushfstring(ls
->L
,"%s:%d: %s",buff
,ls
->linenumber
,msg
);
2716 luaO_pushfstring(ls
->L
,"%s near "LUA_QL("%s"),msg
,txtToken(ls
,token
));
2717 luaD_throw(ls
->L
,3);
2719 static void luaX_syntaxerror(LexState
*ls
,const char*msg
){
2720 luaX_lexerror(ls
,msg
,ls
->t
.token
);
2722 static TString
*luaX_newstring(LexState
*ls
,const char*str
,size_t l
){
2724 TString
*ts
=luaS_newlstr(L
,str
,l
);
2725 TValue
*o
=luaH_setstr(L
,ls
->fs
->h
,ts
);
2732 static void inclinenumber(LexState
*ls
){
2733 int old
=ls
->current
;
2735 if(currIsNewline(ls
)&&ls
->current
!=old
)
2737 if(++ls
->linenumber
>=(INT_MAX
-2))
2738 luaX_syntaxerror(ls
,"chunk has too many lines");
2740 static void luaX_setinput(lua_State
*L
,LexState
*ls
,ZIO
*z
,TString
*source
){
2743 ls
->lookahead
.token
=TK_EOS
;
2749 luaZ_resizebuffer(ls
->L
,ls
->buff
,32);
2752 static int check_next(LexState
*ls
,const char*set
){
2753 if(!strchr(set
,ls
->current
))
2758 static void buffreplace(LexState
*ls
,char from
,char to
){
2759 size_t n
=luaZ_bufflen(ls
->buff
);
2760 char*p
=luaZ_buffer(ls
->buff
);
2762 if(p
[n
]==from
)p
[n
]=to
;
2764 static void read_numeral(LexState
*ls
,SemInfo
*seminfo
){
2767 }while(isdigit(ls
->current
)||ls
->current
=='.');
2768 if(check_next(ls
,"Ee"))
2769 check_next(ls
,"+-");
2770 while(isalnum(ls
->current
)||ls
->current
=='_')
2773 buffreplace(ls
,'.',ls
->decpoint
);
2774 if(!luaO_str2d(luaZ_buffer(ls
->buff
),&seminfo
->r
))
2775 luaX_lexerror(ls
,"malformed number",TK_NUMBER
);
2777 static int skip_sep(LexState
*ls
){
2781 while(ls
->current
=='='){
2785 return(ls
->current
==s
)?count
:(-count
)-1;
2787 static void read_long_string(LexState
*ls
,SemInfo
*seminfo
,int sep
){
2791 if(currIsNewline(ls
))
2794 switch(ls
->current
){
2796 luaX_lexerror(ls
,(seminfo
)?"unfinished long string":
2797 "unfinished long comment",TK_EOS
);
2800 if(skip_sep(ls
)==sep
){
2810 if(!seminfo
)luaZ_resetbuffer(ls
->buff
);
2814 if(seminfo
)save_and_next(ls
);
2820 seminfo
->ts
=luaX_newstring(ls
,luaZ_buffer(ls
->buff
)+(2+sep
),
2821 luaZ_bufflen(ls
->buff
)-2*(2+sep
));
2823 static void read_string(LexState
*ls
,int del
,SemInfo
*seminfo
){
2825 while(ls
->current
!=del
){
2826 switch(ls
->current
){
2828 luaX_lexerror(ls
,"unfinished string",TK_EOS
);
2832 luaX_lexerror(ls
,"unfinished string",TK_STRING
);
2837 switch(ls
->current
){
2838 case'a':c
='\a';break;
2839 case'b':c
='\b';break;
2840 case'f':c
='\f';break;
2841 case'n':c
='\n';break;
2842 case'r':c
='\r';break;
2843 case't':c
='\t';break;
2844 case'v':c
='\v';break;
2846 case'\r':save(ls
,'\n');inclinenumber(ls
);continue;
2849 if(!isdigit(ls
->current
))
2855 c
=10*c
+(ls
->current
-'0');
2857 }while(++i
<3&&isdigit(ls
->current
));
2859 luaX_lexerror(ls
,"escape sequence too large",TK_STRING
);
2874 seminfo
->ts
=luaX_newstring(ls
,luaZ_buffer(ls
->buff
)+1,
2875 luaZ_bufflen(ls
->buff
)-2);
2877 static int llex(LexState
*ls
,SemInfo
*seminfo
){
2878 luaZ_resetbuffer(ls
->buff
);
2880 switch(ls
->current
){
2888 if(ls
->current
!='-')return'-';
2890 if(ls
->current
=='['){
2891 int sep
=skip_sep(ls
);
2892 luaZ_resetbuffer(ls
->buff
);
2894 read_long_string(ls
,NULL
,sep
);
2895 luaZ_resetbuffer(ls
->buff
);
2899 while(!currIsNewline(ls
)&&ls
->current
!=(-1))
2904 int sep
=skip_sep(ls
);
2906 read_long_string(ls
,seminfo
,sep
);
2909 else if (sep
!=-1)luaX_lexerror(ls
,"invalid long string delimiter",TK_STRING
);
2914 if(ls
->current
!='=')return'=';
2915 else{next(ls
);return TK_EQ
;}
2919 if(ls
->current
!='=')return'<';
2920 else{next(ls
);return TK_LE
;}
2924 if(ls
->current
!='=')return'>';
2925 else{next(ls
);return TK_GE
;}
2929 if(ls
->current
!='=')return'~';
2930 else{next(ls
);return TK_NE
;}
2934 read_string(ls
,ls
->current
,seminfo
);
2939 if(check_next(ls
,".")){
2940 if(check_next(ls
,"."))
2942 else return TK_CONCAT
;
2944 else if(!isdigit(ls
->current
))return'.';
2946 read_numeral(ls
,seminfo
);
2954 if(isspace(ls
->current
)){
2958 else if(isdigit(ls
->current
)){
2959 read_numeral(ls
,seminfo
);
2962 else if(isalpha(ls
->current
)||ls
->current
=='_'){
2966 }while(isalnum(ls
->current
)||ls
->current
=='_');
2967 ts
=luaX_newstring(ls
,luaZ_buffer(ls
->buff
),
2968 luaZ_bufflen(ls
->buff
));
2969 if(ts
->tsv
.reserved
>0)
2970 return ts
->tsv
.reserved
-1+257;
2985 static void luaX_next(LexState
*ls
){
2986 ls
->lastline
=ls
->linenumber
;
2987 if(ls
->lookahead
.token
!=TK_EOS
){
2988 ls
->t
=ls
->lookahead
;
2989 ls
->lookahead
.token
=TK_EOS
;
2992 ls
->t
.token
=llex(ls
,&ls
->t
.seminfo
);
2994 static void luaX_lookahead(LexState
*ls
){
2995 ls
->lookahead
.token
=llex(ls
,&ls
->lookahead
.seminfo
);
2997 #define hasjumps(e)((e)->t!=(e)->f)
2998 static int isnumeral(expdesc
*e
){
2999 return(e
->k
==VKNUM
&&e
->t
==(-1)&&e
->f
==(-1));
3001 static void luaK_nil(FuncState
*fs
,int from
,int n
){
3002 Instruction
*previous
;
3003 if(fs
->pc
>fs
->lasttarget
){
3005 if(from
>=fs
->nactvar
)
3009 previous
=&fs
->f
->code
[fs
->pc
-1];
3010 if(GET_OPCODE(*previous
)==OP_LOADNIL
){
3011 int pfrom
=GETARG_A(*previous
);
3012 int pto
=GETARG_B(*previous
);
3013 if(pfrom
<=from
&&from
<=pto
+1){
3015 SETARG_B(*previous
,from
+n
-1);
3021 luaK_codeABC(fs
,OP_LOADNIL
,from
,from
+n
-1,0);
3023 static int luaK_jump(FuncState
*fs
){
3027 j
=luaK_codeAsBx(fs
,OP_JMP
,0,(-1));
3028 luaK_concat(fs
,&j
,jpc
);
3031 static void luaK_ret(FuncState
*fs
,int first
,int nret
){
3032 luaK_codeABC(fs
,OP_RETURN
,first
,nret
+1,0);
3034 static int condjump(FuncState
*fs
,OpCode op
,int A
,int B
,int C
){
3035 luaK_codeABC(fs
,op
,A
,B
,C
);
3036 return luaK_jump(fs
);
3038 static void fixjump(FuncState
*fs
,int pc
,int dest
){
3039 Instruction
*jmp
=&fs
->f
->code
[pc
];
3040 int offset
=dest
-(pc
+1);
3041 if(abs(offset
)>(((1<<(9+9))-1)>>1))
3042 luaX_syntaxerror(fs
->ls
,"control structure too long");
3043 SETARG_sBx(*jmp
,offset
);
3045 static int luaK_getlabel(FuncState
*fs
){
3046 fs
->lasttarget
=fs
->pc
;
3049 static int getjump(FuncState
*fs
,int pc
){
3050 int offset
=GETARG_sBx(fs
->f
->code
[pc
]);
3054 return(pc
+1)+offset
;
3056 static Instruction
*getjumpcontrol(FuncState
*fs
,int pc
){
3057 Instruction
*pi
=&fs
->f
->code
[pc
];
3058 if(pc
>=1&&testTMode(GET_OPCODE(*(pi
-1))))
3063 static int need_value(FuncState
*fs
,int list
){
3064 for(;list
!=(-1);list
=getjump(fs
,list
)){
3065 Instruction i
=*getjumpcontrol(fs
,list
);
3066 if(GET_OPCODE(i
)!=OP_TESTSET
)return 1;
3070 static int patchtestreg(FuncState
*fs
,int node
,int reg
){
3071 Instruction
*i
=getjumpcontrol(fs
,node
);
3072 if(GET_OPCODE(*i
)!=OP_TESTSET
)
3074 if(reg
!=((1<<8)-1)&®
!=GETARG_B(*i
))
3077 *i
=CREATE_ABC(OP_TEST
,GETARG_B(*i
),0,GETARG_C(*i
));
3080 static void removevalues(FuncState
*fs
,int list
){
3081 for(;list
!=(-1);list
=getjump(fs
,list
))
3082 patchtestreg(fs
,list
,((1<<8)-1));
3084 static void patchlistaux(FuncState
*fs
,int list
,int vtarget
,int reg
,
3087 int next
=getjump(fs
,list
);
3088 if(patchtestreg(fs
,list
,reg
))
3089 fixjump(fs
,list
,vtarget
);
3091 fixjump(fs
,list
,dtarget
);
3095 static void dischargejpc(FuncState
*fs
){
3096 patchlistaux(fs
,fs
->jpc
,fs
->pc
,((1<<8)-1),fs
->pc
);
3099 static void luaK_patchlist(FuncState
*fs
,int list
,int target
){
3101 luaK_patchtohere(fs
,list
);
3103 patchlistaux(fs
,list
,target
,((1<<8)-1),target
);
3106 static void luaK_patchtohere(FuncState
*fs
,int list
){
3108 luaK_concat(fs
,&fs
->jpc
,list
);
3110 static void luaK_concat(FuncState
*fs
,int*l1
,int l2
){
3117 while((next
=getjump(fs
,list
))!=(-1))
3119 fixjump(fs
,list
,l2
);
3122 static void luaK_checkstack(FuncState
*fs
,int n
){
3123 int newstack
=fs
->freereg
+n
;
3124 if(newstack
>fs
->f
->maxstacksize
){
3126 luaX_syntaxerror(fs
->ls
,"function or expression too complex");
3127 fs
->f
->maxstacksize
=cast_byte(newstack
);
3130 static void luaK_reserveregs(FuncState
*fs
,int n
){
3131 luaK_checkstack(fs
,n
);
3134 static void freereg(FuncState
*fs
,int reg
){
3135 if(!ISK(reg
)&®
>=fs
->nactvar
){
3139 static void freeexp(FuncState
*fs
,expdesc
*e
){
3141 freereg(fs
,e
->u
.s
.info
);
3143 static int addk(FuncState
*fs
,TValue
*k
,TValue
*v
){
3145 TValue
*idx
=luaH_set(L
,fs
->h
,k
);
3147 int oldsize
=f
->sizek
;
3148 if(ttisnumber(idx
)){
3149 return cast_int(nvalue(idx
));
3152 setnvalue(idx
,cast_num(fs
->nk
));
3153 luaM_growvector(L
,f
->k
,fs
->nk
,f
->sizek
,TValue
,
3154 ((1<<(9+9))-1),"constant table overflow");
3155 while(oldsize
<f
->sizek
)setnilvalue(&f
->k
[oldsize
++]);
3156 setobj(L
,&f
->k
[fs
->nk
],v
);
3157 luaC_barrier(L
,f
,v
);
3161 static int luaK_stringK(FuncState
*fs
,TString
*s
){
3163 setsvalue(fs
->L
,&o
,s
);
3164 return addk(fs
,&o
,&o
);
3166 static int luaK_numberK(FuncState
*fs
,lua_Number r
){
3169 return addk(fs
,&o
,&o
);
3171 static int boolK(FuncState
*fs
,int b
){
3174 return addk(fs
,&o
,&o
);
3176 static int nilK(FuncState
*fs
){
3179 sethvalue(fs
->L
,&k
,fs
->h
);
3180 return addk(fs
,&k
,&v
);
3182 static void luaK_setreturns(FuncState
*fs
,expdesc
*e
,int nresults
){
3184 SETARG_C(getcode(fs
,e
),nresults
+1);
3186 else if(e
->k
==VVARARG
){
3187 SETARG_B(getcode(fs
,e
),nresults
+1);
3188 SETARG_A(getcode(fs
,e
),fs
->freereg
);
3189 luaK_reserveregs(fs
,1);
3192 static void luaK_setoneret(FuncState
*fs
,expdesc
*e
){
3195 e
->u
.s
.info
=GETARG_A(getcode(fs
,e
));
3197 else if(e
->k
==VVARARG
){
3198 SETARG_B(getcode(fs
,e
),2);
3202 static void luaK_dischargevars(FuncState
*fs
,expdesc
*e
){
3209 e
->u
.s
.info
=luaK_codeABC(fs
,OP_GETUPVAL
,0,e
->u
.s
.info
,0);
3214 e
->u
.s
.info
=luaK_codeABx(fs
,OP_GETGLOBAL
,0,e
->u
.s
.info
);
3219 freereg(fs
,e
->u
.s
.aux
);
3220 freereg(fs
,e
->u
.s
.info
);
3221 e
->u
.s
.info
=luaK_codeABC(fs
,OP_GETTABLE
,0,e
->u
.s
.info
,e
->u
.s
.aux
);
3227 luaK_setoneret(fs
,e
);
3233 static int code_label(FuncState
*fs
,int A
,int b
,int jump
){
3235 return luaK_codeABC(fs
,OP_LOADBOOL
,A
,b
,jump
);
3237 static void discharge2reg(FuncState
*fs
,expdesc
*e
,int reg
){
3238 luaK_dischargevars(fs
,e
);
3244 case VFALSE
:case VTRUE
:{
3245 luaK_codeABC(fs
,OP_LOADBOOL
,reg
,e
->k
==VTRUE
,0);
3249 luaK_codeABx(fs
,OP_LOADK
,reg
,e
->u
.s
.info
);
3253 luaK_codeABx(fs
,OP_LOADK
,reg
,luaK_numberK(fs
,e
->u
.nval
));
3257 Instruction
*pc
=&getcode(fs
,e
);
3262 if(reg
!=e
->u
.s
.info
)
3263 luaK_codeABC(fs
,OP_MOVE
,reg
,e
->u
.s
.info
,0);
3273 static void discharge2anyreg(FuncState
*fs
,expdesc
*e
){
3274 if(e
->k
!=VNONRELOC
){
3275 luaK_reserveregs(fs
,1);
3276 discharge2reg(fs
,e
,fs
->freereg
-1);
3279 static void exp2reg(FuncState
*fs
,expdesc
*e
,int reg
){
3280 discharge2reg(fs
,e
,reg
);
3282 luaK_concat(fs
,&e
->t
,e
->u
.s
.info
);
3287 if(need_value(fs
,e
->t
)||need_value(fs
,e
->f
)){
3288 int fj
=(e
->k
==VJMP
)?(-1):luaK_jump(fs
);
3289 p_f
=code_label(fs
,reg
,0,1);
3290 p_t
=code_label(fs
,reg
,1,0);
3291 luaK_patchtohere(fs
,fj
);
3293 final
=luaK_getlabel(fs
);
3294 patchlistaux(fs
,e
->f
,final
,reg
,p_f
);
3295 patchlistaux(fs
,e
->t
,final
,reg
,p_t
);
3301 static void luaK_exp2nextreg(FuncState
*fs
,expdesc
*e
){
3302 luaK_dischargevars(fs
,e
);
3304 luaK_reserveregs(fs
,1);
3305 exp2reg(fs
,e
,fs
->freereg
-1);
3307 static int luaK_exp2anyreg(FuncState
*fs
,expdesc
*e
){
3308 luaK_dischargevars(fs
,e
);
3309 if(e
->k
==VNONRELOC
){
3310 if(!hasjumps(e
))return e
->u
.s
.info
;
3311 if(e
->u
.s
.info
>=fs
->nactvar
){
3312 exp2reg(fs
,e
,e
->u
.s
.info
);
3316 luaK_exp2nextreg(fs
,e
);
3319 static void luaK_exp2val(FuncState
*fs
,expdesc
*e
){
3321 luaK_exp2anyreg(fs
,e
);
3323 luaK_dischargevars(fs
,e
);
3325 static int luaK_exp2RK(FuncState
*fs
,expdesc
*e
){
3332 if(fs
->nk
<=((1<<(9-1))-1)){
3333 e
->u
.s
.info
=(e
->k
==VNIL
)?nilK(fs
):
3334 (e
->k
==VKNUM
)?luaK_numberK(fs
,e
->u
.nval
):
3335 boolK(fs
,(e
->k
==VTRUE
));
3337 return RKASK(e
->u
.s
.info
);
3342 if(e
->u
.s
.info
<=((1<<(9-1))-1))
3343 return RKASK(e
->u
.s
.info
);
3348 return luaK_exp2anyreg(fs
,e
);
3350 static void luaK_storevar(FuncState
*fs
,expdesc
*var
,expdesc
*ex
){
3354 exp2reg(fs
,ex
,var
->u
.s
.info
);
3358 int e
=luaK_exp2anyreg(fs
,ex
);
3359 luaK_codeABC(fs
,OP_SETUPVAL
,e
,var
->u
.s
.info
,0);
3363 int e
=luaK_exp2anyreg(fs
,ex
);
3364 luaK_codeABx(fs
,OP_SETGLOBAL
,e
,var
->u
.s
.info
);
3368 int e
=luaK_exp2RK(fs
,ex
);
3369 luaK_codeABC(fs
,OP_SETTABLE
,var
->u
.s
.info
,var
->u
.s
.aux
,e
);
3378 static void luaK_self(FuncState
*fs
,expdesc
*e
,expdesc
*key
){
3380 luaK_exp2anyreg(fs
,e
);
3383 luaK_reserveregs(fs
,2);
3384 luaK_codeABC(fs
,OP_SELF
,func
,e
->u
.s
.info
,luaK_exp2RK(fs
,key
));
3389 static void invertjump(FuncState
*fs
,expdesc
*e
){
3390 Instruction
*pc
=getjumpcontrol(fs
,e
->u
.s
.info
);
3391 SETARG_A(*pc
,!(GETARG_A(*pc
)));
3393 static int jumponcond(FuncState
*fs
,expdesc
*e
,int cond
){
3394 if(e
->k
==VRELOCABLE
){
3395 Instruction ie
=getcode(fs
,e
);
3396 if(GET_OPCODE(ie
)==OP_NOT
){
3398 return condjump(fs
,OP_TEST
,GETARG_B(ie
),0,!cond
);
3401 discharge2anyreg(fs
,e
);
3403 return condjump(fs
,OP_TESTSET
,((1<<8)-1),e
->u
.s
.info
,cond
);
3405 static void luaK_goiftrue(FuncState
*fs
,expdesc
*e
){
3407 luaK_dischargevars(fs
,e
);
3409 case VK
:case VKNUM
:case VTRUE
:{
3419 pc
=jumponcond(fs
,e
,0);
3423 luaK_concat(fs
,&e
->f
,pc
);
3424 luaK_patchtohere(fs
,e
->t
);
3427 static void luaK_goiffalse(FuncState
*fs
,expdesc
*e
){
3429 luaK_dischargevars(fs
,e
);
3431 case VNIL
:case VFALSE
:{
3440 pc
=jumponcond(fs
,e
,1);
3444 luaK_concat(fs
,&e
->t
,pc
);
3445 luaK_patchtohere(fs
,e
->f
);
3448 static void codenot(FuncState
*fs
,expdesc
*e
){
3449 luaK_dischargevars(fs
,e
);
3451 case VNIL
:case VFALSE
:{
3455 case VK
:case VKNUM
:case VTRUE
:{
3465 discharge2anyreg(fs
,e
);
3467 e
->u
.s
.info
=luaK_codeABC(fs
,OP_NOT
,0,e
->u
.s
.info
,0);
3475 {int temp
=e
->f
;e
->f
=e
->t
;e
->t
=temp
;}
3476 removevalues(fs
,e
->f
);
3477 removevalues(fs
,e
->t
);
3479 static void luaK_indexed(FuncState
*fs
,expdesc
*t
,expdesc
*k
){
3480 t
->u
.s
.aux
=luaK_exp2RK(fs
,k
);
3483 static int constfolding(OpCode op
,expdesc
*e1
,expdesc
*e2
){
3485 if(!isnumeral(e1
)||!isnumeral(e2
))return 0;
3489 case OP_ADD
:r
=luai_numadd(v1
,v2
);break;
3490 case OP_SUB
:r
=luai_numsub(v1
,v2
);break;
3491 case OP_MUL
:r
=luai_nummul(v1
,v2
);break;
3494 r
=luai_numdiv(v1
,v2
);break;
3497 r
=luai_nummod(v1
,v2
);break;
3498 case OP_POW
:r
=luai_numpow(v1
,v2
);break;
3499 case OP_UNM
:r
=luai_numunm(v1
);break;
3500 case OP_LEN
:return 0;
3503 if(luai_numisnan(r
))return 0;
3507 static void codearith(FuncState
*fs
,OpCode op
,expdesc
*e1
,expdesc
*e2
){
3508 if(constfolding(op
,e1
,e2
))
3511 int o2
=(op
!=OP_UNM
&&op
!=OP_LEN
)?luaK_exp2RK(fs
,e2
):0;
3512 int o1
=luaK_exp2RK(fs
,e1
);
3521 e1
->u
.s
.info
=luaK_codeABC(fs
,op
,0,o1
,o2
);
3525 static void codecomp(FuncState
*fs
,OpCode op
,int cond
,expdesc
*e1
,
3527 int o1
=luaK_exp2RK(fs
,e1
);
3528 int o2
=luaK_exp2RK(fs
,e2
);
3531 if(cond
==0&&op
!=OP_EQ
){
3533 temp
=o1
;o1
=o2
;o2
=temp
;
3536 e1
->u
.s
.info
=condjump(fs
,op
,cond
,o1
,o2
);
3539 static void luaK_prefix(FuncState
*fs
,UnOpr op
,expdesc
*e
){
3541 e2
.t
=e2
.f
=(-1);e2
.k
=VKNUM
;e2
.u
.nval
=0;
3545 luaK_exp2anyreg(fs
,e
);
3546 codearith(fs
,OP_UNM
,e
,&e2
);
3549 case OPR_NOT
:codenot(fs
,e
);break;
3551 luaK_exp2anyreg(fs
,e
);
3552 codearith(fs
,OP_LEN
,e
,&e2
);
3558 static void luaK_infix(FuncState
*fs
,BinOpr op
,expdesc
*v
){
3561 luaK_goiftrue(fs
,v
);
3565 luaK_goiffalse(fs
,v
);
3569 luaK_exp2nextreg(fs
,v
);
3572 case OPR_ADD
:case OPR_SUB
:case OPR_MUL
:case OPR_DIV
:
3573 case OPR_MOD
:case OPR_POW
:{
3574 if(!isnumeral(v
))luaK_exp2RK(fs
,v
);
3583 static void luaK_posfix(FuncState
*fs
,BinOpr op
,expdesc
*e1
,expdesc
*e2
){
3586 luaK_dischargevars(fs
,e2
);
3587 luaK_concat(fs
,&e2
->f
,e1
->f
);
3592 luaK_dischargevars(fs
,e2
);
3593 luaK_concat(fs
,&e2
->t
,e1
->t
);
3598 luaK_exp2val(fs
,e2
);
3599 if(e2
->k
==VRELOCABLE
&&GET_OPCODE(getcode(fs
,e2
))==OP_CONCAT
){
3601 SETARG_B(getcode(fs
,e2
),e1
->u
.s
.info
);
3602 e1
->k
=VRELOCABLE
;e1
->u
.s
.info
=e2
->u
.s
.info
;
3605 luaK_exp2nextreg(fs
,e2
);
3606 codearith(fs
,OP_CONCAT
,e1
,e2
);
3610 case OPR_ADD
:codearith(fs
,OP_ADD
,e1
,e2
);break;
3611 case OPR_SUB
:codearith(fs
,OP_SUB
,e1
,e2
);break;
3612 case OPR_MUL
:codearith(fs
,OP_MUL
,e1
,e2
);break;
3613 case OPR_DIV
:codearith(fs
,OP_DIV
,e1
,e2
);break;
3614 case OPR_MOD
:codearith(fs
,OP_MOD
,e1
,e2
);break;
3615 case OPR_POW
:codearith(fs
,OP_POW
,e1
,e2
);break;
3616 case OPR_EQ
:codecomp(fs
,OP_EQ
,1,e1
,e2
);break;
3617 case OPR_NE
:codecomp(fs
,OP_EQ
,0,e1
,e2
);break;
3618 case OPR_LT
:codecomp(fs
,OP_LT
,1,e1
,e2
);break;
3619 case OPR_LE
:codecomp(fs
,OP_LE
,1,e1
,e2
);break;
3620 case OPR_GT
:codecomp(fs
,OP_LT
,0,e1
,e2
);break;
3621 case OPR_GE
:codecomp(fs
,OP_LE
,0,e1
,e2
);break;
3625 static void luaK_fixline(FuncState
*fs
,int line
){
3626 fs
->f
->lineinfo
[fs
->pc
-1]=line
;
3628 static int luaK_code(FuncState
*fs
,Instruction i
,int line
){
3631 luaM_growvector(fs
->L
,f
->code
,fs
->pc
,f
->sizecode
,Instruction
,
3632 (INT_MAX
-2),"code size overflow");
3634 luaM_growvector(fs
->L
,f
->lineinfo
,fs
->pc
,f
->sizelineinfo
,int,
3635 (INT_MAX
-2),"code size overflow");
3636 f
->lineinfo
[fs
->pc
]=line
;
3639 static int luaK_codeABC(FuncState
*fs
,OpCode o
,int a
,int b
,int c
){
3640 return luaK_code(fs
,CREATE_ABC(o
,a
,b
,c
),fs
->ls
->lastline
);
3642 static int luaK_codeABx(FuncState
*fs
,OpCode o
,int a
,unsigned int bc
){
3643 return luaK_code(fs
,CREATE_ABx(o
,a
,bc
),fs
->ls
->lastline
);
3645 static void luaK_setlist(FuncState
*fs
,int base
,int nelems
,int tostore
){
3646 int c
=(nelems
-1)/50+1;
3647 int b
=(tostore
==(-1))?0:tostore
;
3649 luaK_codeABC(fs
,OP_SETLIST
,base
,b
,c
);
3651 luaK_codeABC(fs
,OP_SETLIST
,base
,b
,0);
3652 luaK_code(fs
,cast(Instruction
,c
),fs
->ls
->lastline
);
3656 #define hasmultret(k)((k)==VCALL||(k)==VVARARG)
3657 #define getlocvar(fs,i)((fs)->f->locvars[(fs)->actvar[i]])
3658 #define luaY_checklimit(fs,v,l,m)if((v)>(l))errorlimit(fs,l,m)
3659 typedef struct BlockCnt
{
3660 struct BlockCnt
*previous
;
3664 lu_byte isbreakable
;
3666 static void chunk(LexState
*ls
);
3667 static void expr(LexState
*ls
,expdesc
*v
);
3668 static void anchor_token(LexState
*ls
){
3669 if(ls
->t
.token
==TK_NAME
||ls
->t
.token
==TK_STRING
){
3670 TString
*ts
=ls
->t
.seminfo
.ts
;
3671 luaX_newstring(ls
,getstr(ts
),ts
->tsv
.len
);
3674 static void error_expected(LexState
*ls
,int token
){
3675 luaX_syntaxerror(ls
,
3676 luaO_pushfstring(ls
->L
,LUA_QL("%s")" expected",luaX_token2str(ls
,token
)));
3678 static void errorlimit(FuncState
*fs
,int limit
,const char*what
){
3679 const char*msg
=(fs
->f
->linedefined
==0)?
3680 luaO_pushfstring(fs
->L
,"main function has more than %d %s",limit
,what
):
3681 luaO_pushfstring(fs
->L
,"function at line %d has more than %d %s",
3682 fs
->f
->linedefined
,limit
,what
);
3683 luaX_lexerror(fs
->ls
,msg
,0);
3685 static int testnext(LexState
*ls
,int c
){
3692 static void check(LexState
*ls
,int c
){
3694 error_expected(ls
,c
);
3696 static void checknext(LexState
*ls
,int c
){
3700 #define check_condition(ls,c,msg){if(!(c))luaX_syntaxerror(ls,msg);}
3701 static void check_match(LexState
*ls
,int what
,int who
,int where
){
3702 if(!testnext(ls
,what
)){
3703 if(where
==ls
->linenumber
)
3704 error_expected(ls
,what
);
3706 luaX_syntaxerror(ls
,luaO_pushfstring(ls
->L
,
3707 LUA_QL("%s")" expected (to close "LUA_QL("%s")" at line %d)",
3708 luaX_token2str(ls
,what
),luaX_token2str(ls
,who
),where
));
3712 static TString
*str_checkname(LexState
*ls
){
3715 ts
=ls
->t
.seminfo
.ts
;
3719 static void init_exp(expdesc
*e
,expkind k
,int i
){
3724 static void codestring(LexState
*ls
,expdesc
*e
,TString
*s
){
3725 init_exp(e
,VK
,luaK_stringK(ls
->fs
,s
));
3727 static void checkname(LexState
*ls
,expdesc
*e
){
3728 codestring(ls
,e
,str_checkname(ls
));
3730 static int registerlocalvar(LexState
*ls
,TString
*varname
){
3731 FuncState
*fs
=ls
->fs
;
3733 int oldsize
=f
->sizelocvars
;
3734 luaM_growvector(ls
->L
,f
->locvars
,fs
->nlocvars
,f
->sizelocvars
,
3735 LocVar
,SHRT_MAX
,"too many local variables");
3736 while(oldsize
<f
->sizelocvars
)f
->locvars
[oldsize
++].varname
=NULL
;
3737 f
->locvars
[fs
->nlocvars
].varname
=varname
;
3738 luaC_objbarrier(ls
->L
,f
,varname
);
3739 return fs
->nlocvars
++;
3741 #define new_localvarliteral(ls,v,n)new_localvar(ls,luaX_newstring(ls,""v,(sizeof(v)/sizeof(char))-1),n)
3742 static void new_localvar(LexState
*ls
,TString
*name
,int n
){
3743 FuncState
*fs
=ls
->fs
;
3744 luaY_checklimit(fs
,fs
->nactvar
+n
+1,200,"local variables");
3745 fs
->actvar
[fs
->nactvar
+n
]=cast(unsigned short,registerlocalvar(ls
,name
));
3747 static void adjustlocalvars(LexState
*ls
,int nvars
){
3748 FuncState
*fs
=ls
->fs
;
3749 fs
->nactvar
=cast_byte(fs
->nactvar
+nvars
);
3750 for(;nvars
;nvars
--){
3751 getlocvar(fs
,fs
->nactvar
-nvars
).startpc
=fs
->pc
;
3754 static void removevars(LexState
*ls
,int tolevel
){
3755 FuncState
*fs
=ls
->fs
;
3756 while(fs
->nactvar
>tolevel
)
3757 getlocvar(fs
,--fs
->nactvar
).endpc
=fs
->pc
;
3759 static int indexupvalue(FuncState
*fs
,TString
*name
,expdesc
*v
){
3762 int oldsize
=f
->sizeupvalues
;
3763 for(i
=0;i
<f
->nups
;i
++){
3764 if(fs
->upvalues
[i
].k
==v
->k
&&fs
->upvalues
[i
].info
==v
->u
.s
.info
){
3768 luaY_checklimit(fs
,f
->nups
+1,60,"upvalues");
3769 luaM_growvector(fs
->L
,f
->upvalues
,f
->nups
,f
->sizeupvalues
,
3770 TString
*,(INT_MAX
-2),"");
3771 while(oldsize
<f
->sizeupvalues
)f
->upvalues
[oldsize
++]=NULL
;
3772 f
->upvalues
[f
->nups
]=name
;
3773 luaC_objbarrier(fs
->L
,f
,name
);
3774 fs
->upvalues
[f
->nups
].k
=cast_byte(v
->k
);
3775 fs
->upvalues
[f
->nups
].info
=cast_byte(v
->u
.s
.info
);
3778 static int searchvar(FuncState
*fs
,TString
*n
){
3780 for(i
=fs
->nactvar
-1;i
>=0;i
--){
3781 if(n
==getlocvar(fs
,i
).varname
)
3786 static void markupval(FuncState
*fs
,int level
){
3788 while(bl
&&bl
->nactvar
>level
)bl
=bl
->previous
;
3791 static int singlevaraux(FuncState
*fs
,TString
*n
,expdesc
*var
,int base
){
3793 init_exp(var
,VGLOBAL
,((1<<8)-1));
3797 int v
=searchvar(fs
,n
);
3799 init_exp(var
,VLOCAL
,v
);
3805 if(singlevaraux(fs
->prev
,n
,var
,0)==VGLOBAL
)
3807 var
->u
.s
.info
=indexupvalue(fs
,n
,var
);
3813 static void singlevar(LexState
*ls
,expdesc
*var
){
3814 TString
*varname
=str_checkname(ls
);
3815 FuncState
*fs
=ls
->fs
;
3816 if(singlevaraux(fs
,varname
,var
,1)==VGLOBAL
)
3817 var
->u
.s
.info
=luaK_stringK(fs
,varname
);
3819 static void adjust_assign(LexState
*ls
,int nvars
,int nexps
,expdesc
*e
){
3820 FuncState
*fs
=ls
->fs
;
3821 int extra
=nvars
-nexps
;
3822 if(hasmultret(e
->k
)){
3825 luaK_setreturns(fs
,e
,extra
);
3826 if(extra
>1)luaK_reserveregs(fs
,extra
-1);
3829 if(e
->k
!=VVOID
)luaK_exp2nextreg(fs
,e
);
3831 int reg
=fs
->freereg
;
3832 luaK_reserveregs(fs
,extra
);
3833 luaK_nil(fs
,reg
,extra
);
3837 static void enterlevel(LexState
*ls
){
3838 if(++ls
->L
->nCcalls
>200)
3839 luaX_lexerror(ls
,"chunk has too many syntax levels",0);
3841 #define leavelevel(ls)((ls)->L->nCcalls--)
3842 static void enterblock(FuncState
*fs
,BlockCnt
*bl
,lu_byte isbreakable
){
3844 bl
->isbreakable
=isbreakable
;
3845 bl
->nactvar
=fs
->nactvar
;
3847 bl
->previous
=fs
->bl
;
3850 static void leaveblock(FuncState
*fs
){
3852 fs
->bl
=bl
->previous
;
3853 removevars(fs
->ls
,bl
->nactvar
);
3855 luaK_codeABC(fs
,OP_CLOSE
,bl
->nactvar
,0,0);
3856 fs
->freereg
=fs
->nactvar
;
3857 luaK_patchtohere(fs
,bl
->breaklist
);
3859 static void pushclosure(LexState
*ls
,FuncState
*func
,expdesc
*v
){
3860 FuncState
*fs
=ls
->fs
;
3862 int oldsize
=f
->sizep
;
3864 luaM_growvector(ls
->L
,f
->p
,fs
->np
,f
->sizep
,Proto
*,
3865 ((1<<(9+9))-1),"constant table overflow");
3866 while(oldsize
<f
->sizep
)f
->p
[oldsize
++]=NULL
;
3867 f
->p
[fs
->np
++]=func
->f
;
3868 luaC_objbarrier(ls
->L
,f
,func
->f
);
3869 init_exp(v
,VRELOCABLE
,luaK_codeABx(fs
,OP_CLOSURE
,0,fs
->np
-1));
3870 for(i
=0;i
<func
->f
->nups
;i
++){
3871 OpCode o
=(func
->upvalues
[i
].k
==VLOCAL
)?OP_MOVE
:OP_GETUPVAL
;
3872 luaK_codeABC(fs
,o
,0,func
->upvalues
[i
].info
,0);
3875 static void open_func(LexState
*ls
,FuncState
*fs
){
3877 Proto
*f
=luaF_newproto(L
);
3892 f
->source
=ls
->source
;
3894 fs
->h
=luaH_new(L
,0,0);
3895 sethvalue(L
,L
->top
,fs
->h
);
3897 setptvalue(L
,L
->top
,f
);
3900 static void close_func(LexState
*ls
){
3902 FuncState
*fs
=ls
->fs
;
3906 luaM_reallocvector(L
,f
->code
,f
->sizecode
,fs
->pc
,Instruction
);
3908 luaM_reallocvector(L
,f
->lineinfo
,f
->sizelineinfo
,fs
->pc
,int);
3909 f
->sizelineinfo
=fs
->pc
;
3910 luaM_reallocvector(L
,f
->k
,f
->sizek
,fs
->nk
,TValue
);
3912 luaM_reallocvector(L
,f
->p
,f
->sizep
,fs
->np
,Proto
*);
3914 luaM_reallocvector(L
,f
->locvars
,f
->sizelocvars
,fs
->nlocvars
,LocVar
);
3915 f
->sizelocvars
=fs
->nlocvars
;
3916 luaM_reallocvector(L
,f
->upvalues
,f
->sizeupvalues
,f
->nups
,TString
*);
3917 f
->sizeupvalues
=f
->nups
;
3919 if(fs
)anchor_token(ls
);
3922 static Proto
*luaY_parser(lua_State
*L
,ZIO
*z
,Mbuffer
*buff
,const char*name
){
3923 struct LexState lexstate
;
3924 struct FuncState funcstate
;
3926 luaX_setinput(L
,&lexstate
,z
,luaS_new(L
,name
));
3927 open_func(&lexstate
,&funcstate
);
3928 funcstate
.f
->is_vararg
=2;
3929 luaX_next(&lexstate
);
3931 check(&lexstate
,TK_EOS
);
3932 close_func(&lexstate
);
3935 static void field(LexState
*ls
,expdesc
*v
){
3936 FuncState
*fs
=ls
->fs
;
3938 luaK_exp2anyreg(fs
,v
);
3941 luaK_indexed(fs
,v
,&key
);
3943 static void yindex(LexState
*ls
,expdesc
*v
){
3946 luaK_exp2val(ls
->fs
,v
);
3956 static void recfield(LexState
*ls
,struct ConsControl
*cc
){
3957 FuncState
*fs
=ls
->fs
;
3958 int reg
=ls
->fs
->freereg
;
3961 if(ls
->t
.token
==TK_NAME
){
3962 luaY_checklimit(fs
,cc
->nh
,(INT_MAX
-2),"items in a constructor");
3969 rkkey
=luaK_exp2RK(fs
,&key
);
3971 luaK_codeABC(fs
,OP_SETTABLE
,cc
->t
->u
.s
.info
,rkkey
,luaK_exp2RK(fs
,&val
));
3974 static void closelistfield(FuncState
*fs
,struct ConsControl
*cc
){
3975 if(cc
->v
.k
==VVOID
)return;
3976 luaK_exp2nextreg(fs
,&cc
->v
);
3978 if(cc
->tostore
==50){
3979 luaK_setlist(fs
,cc
->t
->u
.s
.info
,cc
->na
,cc
->tostore
);
3983 static void lastlistfield(FuncState
*fs
,struct ConsControl
*cc
){
3984 if(cc
->tostore
==0)return;
3985 if(hasmultret(cc
->v
.k
)){
3986 luaK_setmultret(fs
,&cc
->v
);
3987 luaK_setlist(fs
,cc
->t
->u
.s
.info
,cc
->na
,(-1));
3992 luaK_exp2nextreg(fs
,&cc
->v
);
3993 luaK_setlist(fs
,cc
->t
->u
.s
.info
,cc
->na
,cc
->tostore
);
3996 static void listfield(LexState
*ls
,struct ConsControl
*cc
){
3998 luaY_checklimit(ls
->fs
,cc
->na
,(INT_MAX
-2),"items in a constructor");
4002 static void constructor(LexState
*ls
,expdesc
*t
){
4003 FuncState
*fs
=ls
->fs
;
4004 int line
=ls
->linenumber
;
4005 int pc
=luaK_codeABC(fs
,OP_NEWTABLE
,0,0,0);
4006 struct ConsControl cc
;
4007 cc
.na
=cc
.nh
=cc
.tostore
=0;
4009 init_exp(t
,VRELOCABLE
,pc
);
4010 init_exp(&cc
.v
,VVOID
,0);
4011 luaK_exp2nextreg(ls
->fs
,t
);
4014 if(ls
->t
.token
=='}')break;
4015 closelistfield(fs
,&cc
);
4016 switch(ls
->t
.token
){
4019 if(ls
->lookahead
.token
!='=')
4034 }while(testnext(ls
,',')||testnext(ls
,';'));
4035 check_match(ls
,'}','{',line
);
4036 lastlistfield(fs
,&cc
);
4037 SETARG_B(fs
->f
->code
[pc
],luaO_int2fb(cc
.na
));
4038 SETARG_C(fs
->f
->code
[pc
],luaO_int2fb(cc
.nh
));
4040 static void parlist(LexState
*ls
){
4041 FuncState
*fs
=ls
->fs
;
4045 if(ls
->t
.token
!=')'){
4047 switch(ls
->t
.token
){
4049 new_localvar(ls
,str_checkname(ls
),nparams
++);
4057 default:luaX_syntaxerror(ls
,"<name> or "LUA_QL("...")" expected");
4059 }while(!f
->is_vararg
&&testnext(ls
,','));
4061 adjustlocalvars(ls
,nparams
);
4062 f
->numparams
=cast_byte(fs
->nactvar
-(f
->is_vararg
&1));
4063 luaK_reserveregs(fs
,fs
->nactvar
);
4065 static void body(LexState
*ls
,expdesc
*e
,int needself
,int line
){
4067 open_func(ls
,&new_fs
);
4068 new_fs
.f
->linedefined
=line
;
4071 new_localvarliteral(ls
,"self",0);
4072 adjustlocalvars(ls
,1);
4077 new_fs
.f
->lastlinedefined
=ls
->linenumber
;
4078 check_match(ls
,TK_END
,TK_FUNCTION
,line
);
4080 pushclosure(ls
,&new_fs
,e
);
4082 static int explist1(LexState
*ls
,expdesc
*v
){
4085 while(testnext(ls
,',')){
4086 luaK_exp2nextreg(ls
->fs
,v
);
4092 static void funcargs(LexState
*ls
,expdesc
*f
){
4093 FuncState
*fs
=ls
->fs
;
4096 int line
=ls
->linenumber
;
4097 switch(ls
->t
.token
){
4099 if(line
!=ls
->lastline
)
4100 luaX_syntaxerror(ls
,"ambiguous syntax (function call x new statement)");
4102 if(ls
->t
.token
==')')
4106 luaK_setmultret(fs
,&args
);
4108 check_match(ls
,')','(',line
);
4112 constructor(ls
,&args
);
4116 codestring(ls
,&args
,ls
->t
.seminfo
.ts
);
4121 luaX_syntaxerror(ls
,"function arguments expected");
4126 if(hasmultret(args
.k
))
4130 luaK_exp2nextreg(fs
,&args
);
4131 nparams
=fs
->freereg
-(base
+1);
4133 init_exp(f
,VCALL
,luaK_codeABC(fs
,OP_CALL
,base
,nparams
+1,2));
4134 luaK_fixline(fs
,line
);
4137 static void prefixexp(LexState
*ls
,expdesc
*v
){
4138 switch(ls
->t
.token
){
4140 int line
=ls
->linenumber
;
4143 check_match(ls
,')','(',line
);
4144 luaK_dischargevars(ls
->fs
,v
);
4152 luaX_syntaxerror(ls
,"unexpected symbol");
4157 static void primaryexp(LexState
*ls
,expdesc
*v
){
4158 FuncState
*fs
=ls
->fs
;
4161 switch(ls
->t
.token
){
4168 luaK_exp2anyreg(fs
,v
);
4170 luaK_indexed(fs
,v
,&key
);
4177 luaK_self(fs
,v
,&key
);
4181 case'(':case TK_STRING
:case'{':{
4182 luaK_exp2nextreg(fs
,v
);
4190 static void simpleexp(LexState
*ls
,expdesc
*v
){
4191 switch(ls
->t
.token
){
4193 init_exp(v
,VKNUM
,0);
4194 v
->u
.nval
=ls
->t
.seminfo
.r
;
4198 codestring(ls
,v
,ls
->t
.seminfo
.ts
);
4206 init_exp(v
,VTRUE
,0);
4210 init_exp(v
,VFALSE
,0);
4214 FuncState
*fs
=ls
->fs
;
4215 check_condition(ls
,fs
->f
->is_vararg
,
4216 "cannot use "LUA_QL("...")" outside a vararg function");
4217 fs
->f
->is_vararg
&=~4;
4218 init_exp(v
,VVARARG
,luaK_codeABC(fs
,OP_VARARG
,0,1,0));
4227 body(ls
,v
,0,ls
->linenumber
);
4237 static UnOpr
getunopr(int op
){
4239 case TK_NOT
:return OPR_NOT
;
4240 case'-':return OPR_MINUS
;
4241 case'#':return OPR_LEN
;
4242 default:return OPR_NOUNOPR
;
4245 static BinOpr
getbinopr(int op
){
4247 case'+':return OPR_ADD
;
4248 case'-':return OPR_SUB
;
4249 case'*':return OPR_MUL
;
4250 case'/':return OPR_DIV
;
4251 case'%':return OPR_MOD
;
4252 case'^':return OPR_POW
;
4253 case TK_CONCAT
:return OPR_CONCAT
;
4254 case TK_NE
:return OPR_NE
;
4255 case TK_EQ
:return OPR_EQ
;
4256 case'<':return OPR_LT
;
4257 case TK_LE
:return OPR_LE
;
4258 case'>':return OPR_GT
;
4259 case TK_GE
:return OPR_GE
;
4260 case TK_AND
:return OPR_AND
;
4261 case TK_OR
:return OPR_OR
;
4262 default:return OPR_NOBINOPR
;
4265 static const struct{
4269 {6,6},{6,6},{7,7},{7,7},{7,7},
4272 {3,3},{3,3},{3,3},{3,3},
4275 static BinOpr
subexpr(LexState
*ls
,expdesc
*v
,unsigned int limit
){
4279 uop
=getunopr(ls
->t
.token
);
4280 if(uop
!=OPR_NOUNOPR
){
4283 luaK_prefix(ls
->fs
,uop
,v
);
4285 else simpleexp(ls
,v
);
4286 op
=getbinopr(ls
->t
.token
);
4287 while(op
!=OPR_NOBINOPR
&&priority
[op
].left
>limit
){
4291 luaK_infix(ls
->fs
,op
,v
);
4292 nextop
=subexpr(ls
,&v2
,priority
[op
].right
);
4293 luaK_posfix(ls
->fs
,op
,v
,&v2
);
4299 static void expr(LexState
*ls
,expdesc
*v
){
4302 static int block_follow(int token
){
4304 case TK_ELSE
:case TK_ELSEIF
:case TK_END
:
4305 case TK_UNTIL
:case TK_EOS
:
4310 static void block(LexState
*ls
){
4311 FuncState
*fs
=ls
->fs
;
4313 enterblock(fs
,&bl
,0);
4318 struct LHS_assign
*prev
;
4321 static void check_conflict(LexState
*ls
,struct LHS_assign
*lh
,expdesc
*v
){
4322 FuncState
*fs
=ls
->fs
;
4323 int extra
=fs
->freereg
;
4325 for(;lh
;lh
=lh
->prev
){
4326 if(lh
->v
.k
==VINDEXED
){
4327 if(lh
->v
.u
.s
.info
==v
->u
.s
.info
){
4329 lh
->v
.u
.s
.info
=extra
;
4331 if(lh
->v
.u
.s
.aux
==v
->u
.s
.info
){
4333 lh
->v
.u
.s
.aux
=extra
;
4338 luaK_codeABC(fs
,OP_MOVE
,fs
->freereg
,v
->u
.s
.info
,0);
4339 luaK_reserveregs(fs
,1);
4342 static void assignment(LexState
*ls
,struct LHS_assign
*lh
,int nvars
){
4344 check_condition(ls
,VLOCAL
<=lh
->v
.k
&&lh
->v
.k
<=VINDEXED
,
4346 if(testnext(ls
,',')){
4347 struct LHS_assign nv
;
4349 primaryexp(ls
,&nv
.v
);
4351 check_conflict(ls
,lh
,&nv
.v
);
4352 luaY_checklimit(ls
->fs
,nvars
,200-ls
->L
->nCcalls
,
4353 "variables in assignment");
4354 assignment(ls
,&nv
,nvars
+1);
4359 nexps
=explist1(ls
,&e
);
4361 adjust_assign(ls
,nvars
,nexps
,&e
);
4363 ls
->fs
->freereg
-=nexps
-nvars
;
4366 luaK_setoneret(ls
->fs
,&e
);
4367 luaK_storevar(ls
->fs
,&lh
->v
,&e
);
4371 init_exp(&e
,VNONRELOC
,ls
->fs
->freereg
-1);
4372 luaK_storevar(ls
->fs
,&lh
->v
,&e
);
4374 static int cond(LexState
*ls
){
4377 if(v
.k
==VNIL
)v
.k
=VFALSE
;
4378 luaK_goiftrue(ls
->fs
,&v
);
4381 static void breakstat(LexState
*ls
){
4382 FuncState
*fs
=ls
->fs
;
4385 while(bl
&&!bl
->isbreakable
){
4390 luaX_syntaxerror(ls
,"no loop to break");
4392 luaK_codeABC(fs
,OP_CLOSE
,bl
->nactvar
,0,0);
4393 luaK_concat(fs
,&bl
->breaklist
,luaK_jump(fs
));
4395 static void whilestat(LexState
*ls
,int line
){
4396 FuncState
*fs
=ls
->fs
;
4401 whileinit
=luaK_getlabel(fs
);
4403 enterblock(fs
,&bl
,1);
4404 checknext(ls
,TK_DO
);
4406 luaK_patchlist(fs
,luaK_jump(fs
),whileinit
);
4407 check_match(ls
,TK_END
,TK_WHILE
,line
);
4409 luaK_patchtohere(fs
,condexit
);
4411 static void repeatstat(LexState
*ls
,int line
){
4413 FuncState
*fs
=ls
->fs
;
4414 int repeat_init
=luaK_getlabel(fs
);
4416 enterblock(fs
,&bl1
,1);
4417 enterblock(fs
,&bl2
,0);
4420 check_match(ls
,TK_UNTIL
,TK_REPEAT
,line
);
4424 luaK_patchlist(ls
->fs
,condexit
,repeat_init
);
4428 luaK_patchtohere(ls
->fs
,condexit
);
4430 luaK_patchlist(ls
->fs
,luaK_jump(fs
),repeat_init
);
4434 static int exp1(LexState
*ls
){
4439 luaK_exp2nextreg(ls
->fs
,&e
);
4442 static void forbody(LexState
*ls
,int base
,int line
,int nvars
,int isnum
){
4444 FuncState
*fs
=ls
->fs
;
4446 adjustlocalvars(ls
,3);
4447 checknext(ls
,TK_DO
);
4448 prep
=isnum
?luaK_codeAsBx(fs
,OP_FORPREP
,base
,(-1)):luaK_jump(fs
);
4449 enterblock(fs
,&bl
,0);
4450 adjustlocalvars(ls
,nvars
);
4451 luaK_reserveregs(fs
,nvars
);
4454 luaK_patchtohere(fs
,prep
);
4455 endfor
=(isnum
)?luaK_codeAsBx(fs
,OP_FORLOOP
,base
,(-1)):
4456 luaK_codeABC(fs
,OP_TFORLOOP
,base
,0,nvars
);
4457 luaK_fixline(fs
,line
);
4458 luaK_patchlist(fs
,(isnum
?endfor
:luaK_jump(fs
)),prep
+1);
4460 static void fornum(LexState
*ls
,TString
*varname
,int line
){
4461 FuncState
*fs
=ls
->fs
;
4462 int base
=fs
->freereg
;
4463 new_localvarliteral(ls
,"(for index)",0);
4464 new_localvarliteral(ls
,"(for limit)",1);
4465 new_localvarliteral(ls
,"(for step)",2);
4466 new_localvar(ls
,varname
,3);
4471 if(testnext(ls
,','))
4474 luaK_codeABx(fs
,OP_LOADK
,fs
->freereg
,luaK_numberK(fs
,1));
4475 luaK_reserveregs(fs
,1);
4477 forbody(ls
,base
,line
,1,1);
4479 static void forlist(LexState
*ls
,TString
*indexname
){
4480 FuncState
*fs
=ls
->fs
;
4484 int base
=fs
->freereg
;
4485 new_localvarliteral(ls
,"(for generator)",nvars
++);
4486 new_localvarliteral(ls
,"(for state)",nvars
++);
4487 new_localvarliteral(ls
,"(for control)",nvars
++);
4488 new_localvar(ls
,indexname
,nvars
++);
4489 while(testnext(ls
,','))
4490 new_localvar(ls
,str_checkname(ls
),nvars
++);
4491 checknext(ls
,TK_IN
);
4492 line
=ls
->linenumber
;
4493 adjust_assign(ls
,3,explist1(ls
,&e
),&e
);
4494 luaK_checkstack(fs
,3);
4495 forbody(ls
,base
,line
,nvars
-3,0);
4497 static void forstat(LexState
*ls
,int line
){
4498 FuncState
*fs
=ls
->fs
;
4501 enterblock(fs
,&bl
,1);
4503 varname
=str_checkname(ls
);
4504 switch(ls
->t
.token
){
4505 case'=':fornum(ls
,varname
,line
);break;
4506 case',':case TK_IN
:forlist(ls
,varname
);break;
4507 default:luaX_syntaxerror(ls
,LUA_QL("=")" or "LUA_QL("in")" expected");
4509 check_match(ls
,TK_END
,TK_FOR
,line
);
4512 static int test_then_block(LexState
*ls
){
4516 checknext(ls
,TK_THEN
);
4520 static void ifstat(LexState
*ls
,int line
){
4521 FuncState
*fs
=ls
->fs
;
4523 int escapelist
=(-1);
4524 flist
=test_then_block(ls
);
4525 while(ls
->t
.token
==TK_ELSEIF
){
4526 luaK_concat(fs
,&escapelist
,luaK_jump(fs
));
4527 luaK_patchtohere(fs
,flist
);
4528 flist
=test_then_block(ls
);
4530 if(ls
->t
.token
==TK_ELSE
){
4531 luaK_concat(fs
,&escapelist
,luaK_jump(fs
));
4532 luaK_patchtohere(fs
,flist
);
4537 luaK_concat(fs
,&escapelist
,flist
);
4538 luaK_patchtohere(fs
,escapelist
);
4539 check_match(ls
,TK_END
,TK_IF
,line
);
4541 static void localfunc(LexState
*ls
){
4543 FuncState
*fs
=ls
->fs
;
4544 new_localvar(ls
,str_checkname(ls
),0);
4545 init_exp(&v
,VLOCAL
,fs
->freereg
);
4546 luaK_reserveregs(fs
,1);
4547 adjustlocalvars(ls
,1);
4548 body(ls
,&b
,0,ls
->linenumber
);
4549 luaK_storevar(fs
,&v
,&b
);
4550 getlocvar(fs
,fs
->nactvar
-1).startpc
=fs
->pc
;
4552 static void localstat(LexState
*ls
){
4557 new_localvar(ls
,str_checkname(ls
),nvars
++);
4558 }while(testnext(ls
,','));
4559 if(testnext(ls
,'='))
4560 nexps
=explist1(ls
,&e
);
4565 adjust_assign(ls
,nvars
,nexps
,&e
);
4566 adjustlocalvars(ls
,nvars
);
4568 static int funcname(LexState
*ls
,expdesc
*v
){
4571 while(ls
->t
.token
=='.')
4573 if(ls
->t
.token
==':'){
4579 static void funcstat(LexState
*ls
,int line
){
4583 needself
=funcname(ls
,&v
);
4584 body(ls
,&b
,needself
,line
);
4585 luaK_storevar(ls
->fs
,&v
,&b
);
4586 luaK_fixline(ls
->fs
,line
);
4588 static void exprstat(LexState
*ls
){
4589 FuncState
*fs
=ls
->fs
;
4590 struct LHS_assign v
;
4591 primaryexp(ls
,&v
.v
);
4593 SETARG_C(getcode(fs
,&v
.v
),1);
4596 assignment(ls
,&v
,1);
4599 static void retstat(LexState
*ls
){
4600 FuncState
*fs
=ls
->fs
;
4604 if(block_follow(ls
->t
.token
)||ls
->t
.token
==';')
4607 nret
=explist1(ls
,&e
);
4608 if(hasmultret(e
.k
)){
4609 luaK_setmultret(fs
,&e
);
4610 if(e
.k
==VCALL
&&nret
==1){
4611 SET_OPCODE(getcode(fs
,&e
),OP_TAILCALL
);
4618 first
=luaK_exp2anyreg(fs
,&e
);
4620 luaK_exp2nextreg(fs
,&e
);
4625 luaK_ret(fs
,first
,nret
);
4627 static int statement(LexState
*ls
){
4628 int line
=ls
->linenumber
;
4629 switch(ls
->t
.token
){
4641 check_match(ls
,TK_END
,TK_DO
,line
);
4649 repeatstat(ls
,line
);
4658 if(testnext(ls
,TK_FUNCTION
))
4679 static void chunk(LexState
*ls
){
4682 while(!islast
&&!block_follow(ls
->t
.token
)){
4683 islast
=statement(ls
);
4685 ls
->fs
->freereg
=ls
->fs
->nactvar
;
4689 static const TValue
*luaV_tonumber(const TValue
*obj
,TValue
*n
){
4691 if(ttisnumber(obj
))return obj
;
4692 if(ttisstring(obj
)&&luaO_str2d(svalue(obj
),&num
)){
4699 static int luaV_tostring(lua_State
*L
,StkId obj
){
4700 if(!ttisnumber(obj
))
4704 lua_Number n
=nvalue(obj
);
4705 lua_number2str(s
,n
);
4706 setsvalue(L
,obj
,luaS_new(L
,s
));
4710 static void callTMres(lua_State
*L
,StkId res
,const TValue
*f
,
4711 const TValue
*p1
,const TValue
*p2
){
4712 ptrdiff_t result
=savestack(L
,res
);
4714 setobj(L
,L
->top
+1,p1
);
4715 setobj(L
,L
->top
+2,p2
);
4716 luaD_checkstack(L
,3);
4718 luaD_call(L
,L
->top
-3,1);
4719 res
=restorestack(L
,result
);
4721 setobj(L
,res
,L
->top
);
4723 static void callTM(lua_State
*L
,const TValue
*f
,const TValue
*p1
,
4724 const TValue
*p2
,const TValue
*p3
){
4726 setobj(L
,L
->top
+1,p1
);
4727 setobj(L
,L
->top
+2,p2
);
4728 setobj(L
,L
->top
+3,p3
);
4729 luaD_checkstack(L
,4);
4731 luaD_call(L
,L
->top
-4,0);
4733 static void luaV_gettable(lua_State
*L
,const TValue
*t
,TValue
*key
,StkId val
){
4735 for(loop
=0;loop
<100;loop
++){
4739 const TValue
*res
=luaH_get(h
,key
);
4741 (tm
=fasttm(L
,h
->metatable
,TM_INDEX
))==NULL
){
4746 else if(ttisnil(tm
=luaT_gettmbyobj(L
,t
,TM_INDEX
)))
4747 luaG_typeerror(L
,t
,"index");
4748 if(ttisfunction(tm
)){
4749 callTMres(L
,val
,tm
,t
,key
);
4754 luaG_runerror(L
,"loop in gettable");
4756 static void luaV_settable(lua_State
*L
,const TValue
*t
,TValue
*key
,StkId val
){
4759 for(loop
=0;loop
<100;loop
++){
4763 TValue
*oldval
=luaH_set(L
,h
,key
);
4764 if(!ttisnil(oldval
)||
4765 (tm
=fasttm(L
,h
->metatable
,TM_NEWINDEX
))==NULL
){
4766 setobj(L
,oldval
,val
);
4768 luaC_barriert(L
,h
,val
);
4772 else if(ttisnil(tm
=luaT_gettmbyobj(L
,t
,TM_NEWINDEX
)))
4773 luaG_typeerror(L
,t
,"index");
4774 if(ttisfunction(tm
)){
4775 callTM(L
,tm
,t
,key
,val
);
4781 luaG_runerror(L
,"loop in settable");
4783 static int call_binTM(lua_State
*L
,const TValue
*p1
,const TValue
*p2
,
4784 StkId res
,TMS event
){
4785 const TValue
*tm
=luaT_gettmbyobj(L
,p1
,event
);
4787 tm
=luaT_gettmbyobj(L
,p2
,event
);
4788 if(ttisnil(tm
))return 0;
4789 callTMres(L
,res
,tm
,p1
,p2
);
4792 static const TValue
*get_compTM(lua_State
*L
,Table
*mt1
,Table
*mt2
,
4794 const TValue
*tm1
=fasttm(L
,mt1
,event
);
4796 if(tm1
==NULL
)return NULL
;
4797 if(mt1
==mt2
)return tm1
;
4798 tm2
=fasttm(L
,mt2
,event
);
4799 if(tm2
==NULL
)return NULL
;
4800 if(luaO_rawequalObj(tm1
,tm2
))
4804 static int call_orderTM(lua_State
*L
,const TValue
*p1
,const TValue
*p2
,
4806 const TValue
*tm1
=luaT_gettmbyobj(L
,p1
,event
);
4808 if(ttisnil(tm1
))return-1;
4809 tm2
=luaT_gettmbyobj(L
,p2
,event
);
4810 if(!luaO_rawequalObj(tm1
,tm2
))
4812 callTMres(L
,L
->top
,tm1
,p1
,p2
);
4813 return!l_isfalse(L
->top
);
4815 static int l_strcmp(const TString
*ls
,const TString
*rs
){
4816 const char*l
=getstr(ls
);
4817 size_t ll
=ls
->tsv
.len
;
4818 const char*r
=getstr(rs
);
4819 size_t lr
=rs
->tsv
.len
;
4821 int temp
=strcoll(l
,r
);
4822 if(temp
!=0)return temp
;
4824 size_t len
=strlen(l
);
4826 return(len
==ll
)?0:1;
4830 l
+=len
;ll
-=len
;r
+=len
;lr
-=len
;
4834 static int luaV_lessthan(lua_State
*L
,const TValue
*l
,const TValue
*r
){
4836 if(ttype(l
)!=ttype(r
))
4837 return luaG_ordererror(L
,l
,r
);
4838 else if(ttisnumber(l
))
4839 return luai_numlt(nvalue(l
),nvalue(r
));
4840 else if(ttisstring(l
))
4841 return l_strcmp(rawtsvalue(l
),rawtsvalue(r
))<0;
4842 else if((res
=call_orderTM(L
,l
,r
,TM_LT
))!=-1)
4844 return luaG_ordererror(L
,l
,r
);
4846 static int lessequal(lua_State
*L
,const TValue
*l
,const TValue
*r
){
4848 if(ttype(l
)!=ttype(r
))
4849 return luaG_ordererror(L
,l
,r
);
4850 else if(ttisnumber(l
))
4851 return luai_numle(nvalue(l
),nvalue(r
));
4852 else if(ttisstring(l
))
4853 return l_strcmp(rawtsvalue(l
),rawtsvalue(r
))<=0;
4854 else if((res
=call_orderTM(L
,l
,r
,TM_LE
))!=-1)
4856 else if((res
=call_orderTM(L
,r
,l
,TM_LT
))!=-1)
4858 return luaG_ordererror(L
,l
,r
);
4860 static int luaV_equalval(lua_State
*L
,const TValue
*t1
,const TValue
*t2
){
4864 case 3:return luai_numeq(nvalue(t1
),nvalue(t2
));
4865 case 1:return bvalue(t1
)==bvalue(t2
);
4866 case 2:return pvalue(t1
)==pvalue(t2
);
4868 if(uvalue(t1
)==uvalue(t2
))return 1;
4869 tm
=get_compTM(L
,uvalue(t1
)->metatable
,uvalue(t2
)->metatable
,
4874 if(hvalue(t1
)==hvalue(t2
))return 1;
4875 tm
=get_compTM(L
,hvalue(t1
)->metatable
,hvalue(t2
)->metatable
,TM_EQ
);
4878 default:return gcvalue(t1
)==gcvalue(t2
);
4880 if(tm
==NULL
)return 0;
4881 callTMres(L
,L
->top
,tm
,t1
,t2
);
4882 return!l_isfalse(L
->top
);
4884 static void luaV_concat(lua_State
*L
,int total
,int last
){
4886 StkId top
=L
->base
+last
+1;
4888 if(!(ttisstring(top
-2)||ttisnumber(top
-2))||!tostring(L
,top
-1)){
4889 if(!call_binTM(L
,top
-2,top
-1,top
-2,TM_CONCAT
))
4890 luaG_concaterror(L
,top
-2,top
-1);
4891 }else if(tsvalue(top
-1)->len
==0)
4892 (void)tostring(L
,top
-2);
4894 size_t tl
=tsvalue(top
-1)->len
;
4897 for(n
=1;n
<total
&&tostring(L
,top
-n
-1);n
++){
4898 size_t l
=tsvalue(top
-n
-1)->len
;
4899 if(l
>=((size_t)(~(size_t)0)-2)-tl
)luaG_runerror(L
,"string length overflow");
4902 buffer
=luaZ_openspace(L
,&G(L
)->buff
,tl
);
4905 size_t l
=tsvalue(top
-i
)->len
;
4906 memcpy(buffer
+tl
,svalue(top
-i
),l
);
4909 setsvalue(L
,top
-n
,luaS_newlstr(L
,buffer
,tl
));
4915 static void Arith(lua_State
*L
,StkId ra
,const TValue
*rb
,
4916 const TValue
*rc
,TMS op
){
4919 if((b
=luaV_tonumber(rb
,&tempb
))!=NULL
&&
4920 (c
=luaV_tonumber(rc
,&tempc
))!=NULL
){
4921 lua_Number nb
=nvalue(b
),nc
=nvalue(c
);
4923 case TM_ADD
:setnvalue(ra
,luai_numadd(nb
,nc
));break;
4924 case TM_SUB
:setnvalue(ra
,luai_numsub(nb
,nc
));break;
4925 case TM_MUL
:setnvalue(ra
,luai_nummul(nb
,nc
));break;
4926 case TM_DIV
:setnvalue(ra
,luai_numdiv(nb
,nc
));break;
4927 case TM_MOD
:setnvalue(ra
,luai_nummod(nb
,nc
));break;
4928 case TM_POW
:setnvalue(ra
,luai_numpow(nb
,nc
));break;
4929 case TM_UNM
:setnvalue(ra
,luai_numunm(nb
));break;
4933 else if(!call_binTM(L
,rb
,rc
,ra
,op
))
4934 luaG_aritherror(L
,rb
,rc
);
4936 #define runtime_check(L,c){if(!(c))break;}
4937 #define RA(i)(base+GETARG_A(i))
4938 #define RB(i)check_exp(getBMode(GET_OPCODE(i))==OpArgR,base+GETARG_B(i))
4939 #define RKB(i)check_exp(getBMode(GET_OPCODE(i))==OpArgK,ISK(GETARG_B(i))?k+INDEXK(GETARG_B(i)):base+GETARG_B(i))
4940 #define RKC(i)check_exp(getCMode(GET_OPCODE(i))==OpArgK,ISK(GETARG_C(i))?k+INDEXK(GETARG_C(i)):base+GETARG_C(i))
4941 #define KBx(i)check_exp(getBMode(GET_OPCODE(i))==OpArgK,k+GETARG_Bx(i))
4942 #define dojump(L,pc,i){(pc)+=(i);}
4943 #define Protect(x){L->savedpc=pc;{x;};base=L->base;}
4944 #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));}
4945 static void luaV_execute(lua_State
*L
,int nexeccalls
){
4949 const Instruction
*pc
;
4952 cl
=&clvalue(L
->ci
->func
)->l
;
4956 const Instruction i
=*pc
++;
4959 switch(GET_OPCODE(i
)){
4965 setobj(L
,ra
,KBx(i
));
4969 setbvalue(ra
,GETARG_B(i
));
4970 if(GETARG_C(i
))pc
++;
4982 setobj(L
,ra
,cl
->upvals
[b
]->v
);
4988 sethvalue(L
,&g
,cl
->env
);
4989 Protect(luaV_gettable(L
,&g
,rb
,ra
));
4993 Protect(luaV_gettable(L
,RB(i
),RKC(i
),ra
));
4998 sethvalue(L
,&g
,cl
->env
);
4999 Protect(luaV_settable(L
,&g
,KBx(i
),ra
));
5003 UpVal
*uv
=cl
->upvals
[GETARG_B(i
)];
5005 luaC_barrier(L
,uv
,ra
);
5009 Protect(luaV_settable(L
,ra
,RKB(i
),RKC(i
)));
5015 sethvalue(L
,ra
,luaH_new(L
,luaO_fb2int(b
),luaO_fb2int(c
)));
5016 Protect(luaC_checkGC(L
));
5022 Protect(luaV_gettable(L
,rb
,RKC(i
),ra
));
5026 arith_op(luai_numadd
,TM_ADD
);
5030 arith_op(luai_numsub
,TM_SUB
);
5034 arith_op(luai_nummul
,TM_MUL
);
5038 arith_op(luai_numdiv
,TM_DIV
);
5042 arith_op(luai_nummod
,TM_MOD
);
5046 arith_op(luai_numpow
,TM_POW
);
5052 lua_Number nb
=nvalue(rb
);
5053 setnvalue(ra
,luai_numunm(nb
));
5056 Protect(Arith(L
,ra
,rb
,rb
,TM_UNM
));
5061 int res
=l_isfalse(RB(i
));
5066 const TValue
*rb
=RB(i
);
5069 setnvalue(ra
,cast_num(luaH_getn(hvalue(rb
))));
5073 setnvalue(ra
,cast_num(tsvalue(rb
)->len
));
5078 if(!call_binTM(L
,rb
,(&luaO_nilobject_
),ra
,TM_LEN
))
5079 luaG_typeerror(L
,rb
,"get length of");
5088 Protect(luaV_concat(L
,c
-b
+1,c
);luaC_checkGC(L
));
5089 setobj(L
,RA(i
),base
+b
);
5093 dojump(L
,pc
,GETARG_sBx(i
));
5100 if(equalobj(L
,rb
,rc
)==GETARG_A(i
))
5101 dojump(L
,pc
,GETARG_sBx(*pc
));
5108 if(luaV_lessthan(L
,RKB(i
),RKC(i
))==GETARG_A(i
))
5109 dojump(L
,pc
,GETARG_sBx(*pc
));
5116 if(lessequal(L
,RKB(i
),RKC(i
))==GETARG_A(i
))
5117 dojump(L
,pc
,GETARG_sBx(*pc
));
5123 if(l_isfalse(ra
)!=GETARG_C(i
))
5124 dojump(L
,pc
,GETARG_sBx(*pc
));
5130 if(l_isfalse(rb
)!=GETARG_C(i
)){
5132 dojump(L
,pc
,GETARG_sBx(*pc
));
5139 int nresults
=GETARG_C(i
)-1;
5140 if(b
!=0)L
->top
=ra
+b
;
5142 switch(luaD_precall(L
,ra
,nresults
)){
5148 if(nresults
>=0)L
->top
=L
->ci
->top
;
5159 if(b
!=0)L
->top
=ra
+b
;
5161 switch(luaD_precall(L
,ra
,(-1))){
5163 CallInfo
*ci
=L
->ci
-1;
5165 StkId func
=ci
->func
;
5166 StkId pfunc
=(ci
+1)->func
;
5167 if(L
->openupval
)luaF_close(L
,ci
->base
);
5168 L
->base
=ci
->base
=ci
->func
+((ci
+1)->base
-pfunc
);
5169 for(aux
=0;pfunc
+aux
<L
->top
;aux
++)
5170 setobj(L
,func
+aux
,pfunc
+aux
);
5171 ci
->top
=L
->top
=func
+aux
;
5172 ci
->savedpc
=L
->savedpc
;
5188 if(b
!=0)L
->top
=ra
+b
-1;
5189 if(L
->openupval
)luaF_close(L
,base
);
5191 b
=luaD_poscall(L
,ra
);
5195 if(b
)L
->top
=L
->ci
->top
;
5200 lua_Number step
=nvalue(ra
+2);
5201 lua_Number idx
=luai_numadd(nvalue(ra
),step
);
5202 lua_Number limit
=nvalue(ra
+1);
5203 if(luai_numlt(0,step
)?luai_numle(idx
,limit
)
5204 :luai_numle(limit
,idx
)){
5205 dojump(L
,pc
,GETARG_sBx(i
));
5207 setnvalue(ra
+3,idx
);
5212 const TValue
*init
=ra
;
5213 const TValue
*plimit
=ra
+1;
5214 const TValue
*pstep
=ra
+2;
5216 if(!tonumber(init
,ra
))
5217 luaG_runerror(L
,LUA_QL("for")" initial value must be a number");
5218 else if(!tonumber(plimit
,ra
+1))
5219 luaG_runerror(L
,LUA_QL("for")" limit must be a number");
5220 else if(!tonumber(pstep
,ra
+2))
5221 luaG_runerror(L
,LUA_QL("for")" step must be a number");
5222 setnvalue(ra
,luai_numsub(nvalue(ra
),nvalue(pstep
)));
5223 dojump(L
,pc
,GETARG_sBx(i
));
5228 setobj(L
,cb
+2,ra
+2);
5229 setobj(L
,cb
+1,ra
+1);
5232 Protect(luaD_call(L
,cb
,GETARG_C(i
)));
5237 dojump(L
,pc
,GETARG_sBx(*pc
));
5248 n
=cast_int(L
->top
-ra
)-1;
5251 if(c
==0)c
=cast_int(*pc
++);
5252 runtime_check(L
,ttistable(ra
));
5255 if(last
>h
->sizearray
)
5256 luaH_resizearray(L
,h
,last
);
5259 setobj(L
,luaH_setnum(L
,h
,last
--),val
);
5260 luaC_barriert(L
,h
,val
);
5272 p
=cl
->p
->p
[GETARG_Bx(i
)];
5274 ncl
=luaF_newLclosure(L
,nup
,cl
->env
);
5276 for(j
=0;j
<nup
;j
++,pc
++){
5277 if(GET_OPCODE(*pc
)==OP_GETUPVAL
)
5278 ncl
->l
.upvals
[j
]=cl
->upvals
[GETARG_B(*pc
)];
5280 ncl
->l
.upvals
[j
]=luaF_findupval(L
,base
+GETARG_B(*pc
));
5283 setclvalue(L
,ra
,ncl
);
5284 Protect(luaC_checkGC(L
));
5288 int b
=GETARG_B(i
)-1;
5291 int n
=cast_int(ci
->base
-ci
->func
)-cl
->p
->numparams
-1;
5293 Protect(luaD_checkstack(L
,n
));
5300 setobj(L
,ra
+j
,ci
->base
-n
+j
);
5311 #define api_checknelems(L,n)luai_apicheck(L,(n)<=(L->top-L->base))
5312 #define api_checkvalidindex(L,i)luai_apicheck(L,(i)!=(&luaO_nilobject_))
5313 #define api_incr_top(L){luai_apicheck(L,L->top<L->ci->top);L->top++;}
5314 static TValue
*index2adr(lua_State
*L
,int idx
){
5316 TValue
*o
=L
->base
+(idx
-1);
5317 luai_apicheck(L
,idx
<=L
->ci
->top
-L
->base
);
5318 if(o
>=L
->top
)return cast(TValue
*,(&luaO_nilobject_
));
5321 else if(idx
>(-10000)){
5322 luai_apicheck(L
,idx
!=0&&-idx
<=L
->top
-L
->base
);
5326 case(-10000):return registry(L
);
5328 Closure
*func
=curr_func(L
);
5329 sethvalue(L
,&L
->env
,func
->c
.env
);
5332 case(-10002):return gt(L
);
5334 Closure
*func
=curr_func(L
);
5336 return(idx
<=func
->c
.nupvalues
)
5337 ?&func
->c
.upvalue
[idx
-1]
5338 :cast(TValue
*,(&luaO_nilobject_
));
5342 static Table
*getcurrenv(lua_State
*L
){
5343 if(L
->ci
==L
->base_ci
)
5344 return hvalue(gt(L
));
5346 Closure
*func
=curr_func(L
);
5350 static int lua_checkstack(lua_State
*L
,int size
){
5352 if(size
>8000||(L
->top
-L
->base
+size
)>8000)
5355 luaD_checkstack(L
,size
);
5356 if(L
->ci
->top
<L
->top
+size
)
5357 L
->ci
->top
=L
->top
+size
;
5361 static lua_CFunction
lua_atpanic(lua_State
*L
,lua_CFunction panicf
){
5367 static int lua_gettop(lua_State
*L
){
5368 return cast_int(L
->top
-L
->base
);
5370 static void lua_settop(lua_State
*L
,int idx
){
5372 luai_apicheck(L
,idx
<=L
->stack_last
-L
->base
);
5373 while(L
->top
<L
->base
+idx
)
5374 setnilvalue(L
->top
++);
5378 luai_apicheck(L
,-(idx
+1)<=(L
->top
-L
->base
));
5382 static void lua_remove(lua_State
*L
,int idx
){
5385 api_checkvalidindex(L
,p
);
5386 while(++p
<L
->top
)setobj(L
,p
-1,p
);
5389 static void lua_insert(lua_State
*L
,int idx
){
5393 api_checkvalidindex(L
,p
);
5394 for(q
=L
->top
;q
>p
;q
--)setobj(L
,q
,q
-1);
5397 static void lua_replace(lua_State
*L
,int idx
){
5399 if(idx
==(-10001)&&L
->ci
==L
->base_ci
)
5400 luaG_runerror(L
,"no calling environment");
5401 api_checknelems(L
,1);
5403 api_checkvalidindex(L
,o
);
5405 Closure
*func
=curr_func(L
);
5406 luai_apicheck(L
,ttistable(L
->top
-1));
5407 func
->c
.env
=hvalue(L
->top
-1);
5408 luaC_barrier(L
,func
,L
->top
-1);
5411 setobj(L
,o
,L
->top
-1);
5413 luaC_barrier(L
,curr_func(L
),L
->top
-1);
5417 static void lua_pushvalue(lua_State
*L
,int idx
){
5418 setobj(L
,L
->top
,index2adr(L
,idx
));
5421 static int lua_type(lua_State
*L
,int idx
){
5422 StkId o
=index2adr(L
,idx
);
5423 return(o
==(&luaO_nilobject_
))?(-1):ttype(o
);
5425 static const char*lua_typename(lua_State
*L
,int t
){
5427 return(t
==(-1))?"no value":luaT_typenames
[t
];
5429 static int lua_iscfunction(lua_State
*L
,int idx
){
5430 StkId o
=index2adr(L
,idx
);
5431 return iscfunction(o
);
5433 static int lua_isnumber(lua_State
*L
,int idx
){
5435 const TValue
*o
=index2adr(L
,idx
);
5436 return tonumber(o
,&n
);
5438 static int lua_isstring(lua_State
*L
,int idx
){
5439 int t
=lua_type(L
,idx
);
5442 static int lua_rawequal(lua_State
*L
,int index1
,int index2
){
5443 StkId o1
=index2adr(L
,index1
);
5444 StkId o2
=index2adr(L
,index2
);
5445 return(o1
==(&luaO_nilobject_
)||o2
==(&luaO_nilobject_
))?0
5446 :luaO_rawequalObj(o1
,o2
);
5448 static int lua_lessthan(lua_State
*L
,int index1
,int index2
){
5451 o1
=index2adr(L
,index1
);
5452 o2
=index2adr(L
,index2
);
5453 i
=(o1
==(&luaO_nilobject_
)||o2
==(&luaO_nilobject_
))?0
5454 :luaV_lessthan(L
,o1
,o2
);
5457 static lua_Number
lua_tonumber(lua_State
*L
,int idx
){
5459 const TValue
*o
=index2adr(L
,idx
);
5465 static lua_Integer
lua_tointeger(lua_State
*L
,int idx
){
5467 const TValue
*o
=index2adr(L
,idx
);
5470 lua_Number num
=nvalue(o
);
5471 lua_number2integer(res
,num
);
5477 static int lua_toboolean(lua_State
*L
,int idx
){
5478 const TValue
*o
=index2adr(L
,idx
);
5479 return!l_isfalse(o
);
5481 static const char*lua_tolstring(lua_State
*L
,int idx
,size_t*len
){
5482 StkId o
=index2adr(L
,idx
);
5484 if(!luaV_tostring(L
,o
)){
5485 if(len
!=NULL
)*len
=0;
5491 if(len
!=NULL
)*len
=tsvalue(o
)->len
;
5494 static size_t lua_objlen(lua_State
*L
,int idx
){
5495 StkId o
=index2adr(L
,idx
);
5497 case 4:return tsvalue(o
)->len
;
5498 case 7:return uvalue(o
)->len
;
5499 case 5:return luaH_getn(hvalue(o
));
5502 l
=(luaV_tostring(L
,o
)?tsvalue(o
)->len
:0);
5508 static lua_CFunction
lua_tocfunction(lua_State
*L
,int idx
){
5509 StkId o
=index2adr(L
,idx
);
5510 return(!iscfunction(o
))?NULL
:clvalue(o
)->c
.f
;
5512 static void*lua_touserdata(lua_State
*L
,int idx
){
5513 StkId o
=index2adr(L
,idx
);
5515 case 7:return(rawuvalue(o
)+1);
5516 case 2:return pvalue(o
);
5517 default:return NULL
;
5520 static void lua_pushnil(lua_State
*L
){
5521 setnilvalue(L
->top
);
5524 static void lua_pushnumber(lua_State
*L
,lua_Number n
){
5525 setnvalue(L
->top
,n
);
5528 static void lua_pushinteger(lua_State
*L
,lua_Integer n
){
5529 setnvalue(L
->top
,cast_num(n
));
5532 static void lua_pushlstring(lua_State
*L
,const char*s
,size_t len
){
5534 setsvalue(L
,L
->top
,luaS_newlstr(L
,s
,len
));
5537 static void lua_pushstring(lua_State
*L
,const char*s
){
5541 lua_pushlstring(L
,s
,strlen(s
));
5543 static const char*lua_pushvfstring(lua_State
*L
,const char*fmt
,
5547 ret
=luaO_pushvfstring(L
,fmt
,argp
);
5550 static const char*lua_pushfstring(lua_State
*L
,const char*fmt
,...){
5555 ret
=luaO_pushvfstring(L
,fmt
,argp
);
5559 static void lua_pushcclosure(lua_State
*L
,lua_CFunction fn
,int n
){
5562 api_checknelems(L
,n
);
5563 cl
=luaF_newCclosure(L
,n
,getcurrenv(L
));
5567 setobj(L
,&cl
->c
.upvalue
[n
],L
->top
+n
);
5568 setclvalue(L
,L
->top
,cl
);
5571 static void lua_pushboolean(lua_State
*L
,int b
){
5572 setbvalue(L
->top
,(b
!=0));
5575 static int lua_pushthread(lua_State
*L
){
5576 setthvalue(L
,L
->top
,L
);
5578 return(G(L
)->mainthread
==L
);
5580 static void lua_gettable(lua_State
*L
,int idx
){
5583 api_checkvalidindex(L
,t
);
5584 luaV_gettable(L
,t
,L
->top
-1,L
->top
-1);
5586 static void lua_getfield(lua_State
*L
,int idx
,const char*k
){
5590 api_checkvalidindex(L
,t
);
5591 setsvalue(L
,&key
,luaS_new(L
,k
));
5592 luaV_gettable(L
,t
,&key
,L
->top
);
5595 static void lua_rawget(lua_State
*L
,int idx
){
5598 luai_apicheck(L
,ttistable(t
));
5599 setobj(L
,L
->top
-1,luaH_get(hvalue(t
),L
->top
-1));
5601 static void lua_rawgeti(lua_State
*L
,int idx
,int n
){
5604 luai_apicheck(L
,ttistable(o
));
5605 setobj(L
,L
->top
,luaH_getnum(hvalue(o
),n
));
5608 static void lua_createtable(lua_State
*L
,int narray
,int nrec
){
5610 sethvalue(L
,L
->top
,luaH_new(L
,narray
,nrec
));
5613 static int lua_getmetatable(lua_State
*L
,int objindex
){
5617 obj
=index2adr(L
,objindex
);
5620 mt
=hvalue(obj
)->metatable
;
5623 mt
=uvalue(obj
)->metatable
;
5626 mt
=G(L
)->mt
[ttype(obj
)];
5632 sethvalue(L
,L
->top
,mt
);
5638 static void lua_getfenv(lua_State
*L
,int idx
){
5641 api_checkvalidindex(L
,o
);
5644 sethvalue(L
,L
->top
,clvalue(o
)->c
.env
);
5647 sethvalue(L
,L
->top
,uvalue(o
)->env
);
5650 setobj(L
,L
->top
,gt(thvalue(o
)));
5653 setnilvalue(L
->top
);
5658 static void lua_settable(lua_State
*L
,int idx
){
5660 api_checknelems(L
,2);
5662 api_checkvalidindex(L
,t
);
5663 luaV_settable(L
,t
,L
->top
-2,L
->top
-1);
5666 static void lua_setfield(lua_State
*L
,int idx
,const char*k
){
5669 api_checknelems(L
,1);
5671 api_checkvalidindex(L
,t
);
5672 setsvalue(L
,&key
,luaS_new(L
,k
));
5673 luaV_settable(L
,t
,&key
,L
->top
-1);
5676 static void lua_rawset(lua_State
*L
,int idx
){
5678 api_checknelems(L
,2);
5680 luai_apicheck(L
,ttistable(t
));
5681 setobj(L
,luaH_set(L
,hvalue(t
),L
->top
-2),L
->top
-1);
5682 luaC_barriert(L
,hvalue(t
),L
->top
-1);
5685 static void lua_rawseti(lua_State
*L
,int idx
,int n
){
5687 api_checknelems(L
,1);
5689 luai_apicheck(L
,ttistable(o
));
5690 setobj(L
,luaH_setnum(L
,hvalue(o
),n
),L
->top
-1);
5691 luaC_barriert(L
,hvalue(o
),L
->top
-1);
5694 static int lua_setmetatable(lua_State
*L
,int objindex
){
5697 api_checknelems(L
,1);
5698 obj
=index2adr(L
,objindex
);
5699 api_checkvalidindex(L
,obj
);
5700 if(ttisnil(L
->top
-1))
5703 luai_apicheck(L
,ttistable(L
->top
-1));
5704 mt
=hvalue(L
->top
-1);
5708 hvalue(obj
)->metatable
=mt
;
5710 luaC_objbarriert(L
,hvalue(obj
),mt
);
5714 uvalue(obj
)->metatable
=mt
;
5716 luaC_objbarrier(L
,rawuvalue(obj
),mt
);
5720 G(L
)->mt
[ttype(obj
)]=mt
;
5727 static int lua_setfenv(lua_State
*L
,int idx
){
5730 api_checknelems(L
,1);
5732 api_checkvalidindex(L
,o
);
5733 luai_apicheck(L
,ttistable(L
->top
-1));
5736 clvalue(o
)->c
.env
=hvalue(L
->top
-1);
5739 uvalue(o
)->env
=hvalue(L
->top
-1);
5742 sethvalue(L
,gt(thvalue(o
)),hvalue(L
->top
-1));
5748 if(res
)luaC_objbarrier(L
,gcvalue(o
),hvalue(L
->top
-1));
5752 #define adjustresults(L,nres){if(nres==(-1)&&L->top>=L->ci->top)L->ci->top=L->top;}
5753 #define checkresults(L,na,nr)luai_apicheck(L,(nr)==(-1)||(L->ci->top-L->top>=(nr)-(na)))
5754 static void lua_call(lua_State
*L
,int nargs
,int nresults
){
5756 api_checknelems(L
,nargs
+1);
5757 checkresults(L
,nargs
,nresults
);
5758 func
=L
->top
-(nargs
+1);
5759 luaD_call(L
,func
,nresults
);
5760 adjustresults(L
,nresults
);
5766 static void f_call(lua_State
*L
,void*ud
){
5767 struct CallS
*c
=cast(struct CallS
*,ud
);
5768 luaD_call(L
,c
->func
,c
->nresults
);
5770 static int lua_pcall(lua_State
*L
,int nargs
,int nresults
,int errfunc
){
5774 api_checknelems(L
,nargs
+1);
5775 checkresults(L
,nargs
,nresults
);
5779 StkId o
=index2adr(L
,errfunc
);
5780 api_checkvalidindex(L
,o
);
5781 func
=savestack(L
,o
);
5783 c
.func
=L
->top
-(nargs
+1);
5784 c
.nresults
=nresults
;
5785 status
=luaD_pcall(L
,f_call
,&c
,savestack(L
,c
.func
),func
);
5786 adjustresults(L
,nresults
);
5789 static int lua_load(lua_State
*L
,lua_Reader reader
,void*data
,
5790 const char*chunkname
){
5793 if(!chunkname
)chunkname
="?";
5794 luaZ_init(L
,&z
,reader
,data
);
5795 status
=luaD_protectedparser(L
,&z
,chunkname
);
5798 static int lua_error(lua_State
*L
){
5799 api_checknelems(L
,1);
5803 static int lua_next(lua_State
*L
,int idx
){
5807 luai_apicheck(L
,ttistable(t
));
5808 more
=luaH_next(L
,hvalue(t
),L
->top
-1);
5816 static void lua_concat(lua_State
*L
,int n
){
5817 api_checknelems(L
,n
);
5820 luaV_concat(L
,n
,cast_int(L
->top
-L
->base
)-1);
5824 setsvalue(L
,L
->top
,luaS_newlstr(L
,"",0));
5828 static void*lua_newuserdata(lua_State
*L
,size_t size
){
5831 u
=luaS_newudata(L
,size
,getcurrenv(L
));
5832 setuvalue(L
,L
->top
,u
);
5836 #define luaL_getn(L,i)((int)lua_objlen(L,i))
5837 #define luaL_setn(L,i,j)((void)0)
5838 typedef struct luaL_Reg
{
5842 static void luaI_openlib(lua_State
*L
,const char*libname
,
5843 const luaL_Reg
*l
,int nup
);
5844 static int luaL_argerror(lua_State
*L
,int numarg
,const char*extramsg
);
5845 static const char* luaL_checklstring(lua_State
*L
,int numArg
,
5847 static const char* luaL_optlstring(lua_State
*L
,int numArg
,
5848 const char*def
,size_t*l
);
5849 static lua_Integer
luaL_checkinteger(lua_State
*L
,int numArg
);
5850 static lua_Integer
luaL_optinteger(lua_State
*L
,int nArg
,
5852 static int luaL_error(lua_State
*L
,const char*fmt
,...);
5853 static const char* luaL_findtable(lua_State
*L
,int idx
,
5854 const char*fname
,int szhint
);
5855 #define luaL_argcheck(L,cond,numarg,extramsg)((void)((cond)||luaL_argerror(L,(numarg),(extramsg))))
5856 #define luaL_checkstring(L,n)(luaL_checklstring(L,(n),NULL))
5857 #define luaL_optstring(L,n,d)(luaL_optlstring(L,(n),(d),NULL))
5858 #define luaL_checkint(L,n)((int)luaL_checkinteger(L,(n)))
5859 #define luaL_optint(L,n,d)((int)luaL_optinteger(L,(n),(d)))
5860 #define luaL_typename(L,i)lua_typename(L,lua_type(L,(i)))
5861 #define luaL_getmetatable(L,n)(lua_getfield(L,(-10000),(n)))
5862 #define luaL_opt(L,f,n,d)(lua_isnoneornil(L,(n))?(d):f(L,(n)))
5863 typedef struct luaL_Buffer
{
5867 char buffer
[BUFSIZ
];
5869 #define luaL_addchar(B,c)((void)((B)->p<((B)->buffer+BUFSIZ)||luaL_prepbuffer(B)),(*(B)->p++=(char)(c)))
5870 #define luaL_addsize(B,n)((B)->p+=(n))
5871 static char* luaL_prepbuffer(luaL_Buffer
*B
);
5872 static int luaL_argerror(lua_State
*L
,int narg
,const char*extramsg
){
5874 if(!lua_getstack(L
,0,&ar
))
5875 return luaL_error(L
,"bad argument #%d (%s)",narg
,extramsg
);
5876 lua_getinfo(L
,"n",&ar
);
5877 if(strcmp(ar
.namewhat
,"method")==0){
5880 return luaL_error(L
,"calling "LUA_QL("%s")" on bad self (%s)",
5885 return luaL_error(L
,"bad argument #%d to "LUA_QL("%s")" (%s)",
5886 narg
,ar
.name
,extramsg
);
5888 static int luaL_typerror(lua_State
*L
,int narg
,const char*tname
){
5889 const char*msg
=lua_pushfstring(L
,"%s expected, got %s",
5890 tname
,luaL_typename(L
,narg
));
5891 return luaL_argerror(L
,narg
,msg
);
5893 static void tag_error(lua_State
*L
,int narg
,int tag
){
5894 luaL_typerror(L
,narg
,lua_typename(L
,tag
));
5896 static void luaL_where(lua_State
*L
,int level
){
5898 if(lua_getstack(L
,level
,&ar
)){
5899 lua_getinfo(L
,"Sl",&ar
);
5900 if(ar
.currentline
>0){
5901 lua_pushfstring(L
,"%s:%d: ",ar
.short_src
,ar
.currentline
);
5905 lua_pushliteral(L
,"");
5907 static int luaL_error(lua_State
*L
,const char*fmt
,...){
5911 lua_pushvfstring(L
,fmt
,argp
);
5914 return lua_error(L
);
5916 static int luaL_newmetatable(lua_State
*L
,const char*tname
){
5917 lua_getfield(L
,(-10000),tname
);
5918 if(!lua_isnil(L
,-1))
5922 lua_pushvalue(L
,-1);
5923 lua_setfield(L
,(-10000),tname
);
5926 static void*luaL_checkudata(lua_State
*L
,int ud
,const char*tname
){
5927 void*p
=lua_touserdata(L
,ud
);
5929 if(lua_getmetatable(L
,ud
)){
5930 lua_getfield(L
,(-10000),tname
);
5931 if(lua_rawequal(L
,-1,-2)){
5937 luaL_typerror(L
,ud
,tname
);
5940 static void luaL_checkstack(lua_State
*L
,int space
,const char*mes
){
5941 if(!lua_checkstack(L
,space
))
5942 luaL_error(L
,"stack overflow (%s)",mes
);
5944 static void luaL_checktype(lua_State
*L
,int narg
,int t
){
5945 if(lua_type(L
,narg
)!=t
)
5946 tag_error(L
,narg
,t
);
5948 static void luaL_checkany(lua_State
*L
,int narg
){
5949 if(lua_type(L
,narg
)==(-1))
5950 luaL_argerror(L
,narg
,"value expected");
5952 static const char*luaL_checklstring(lua_State
*L
,int narg
,size_t*len
){
5953 const char*s
=lua_tolstring(L
,narg
,len
);
5954 if(!s
)tag_error(L
,narg
,4);
5957 static const char*luaL_optlstring(lua_State
*L
,int narg
,
5958 const char*def
,size_t*len
){
5959 if(lua_isnoneornil(L
,narg
)){
5961 *len
=(def
?strlen(def
):0);
5964 else return luaL_checklstring(L
,narg
,len
);
5966 static lua_Number
luaL_checknumber(lua_State
*L
,int narg
){
5967 lua_Number d
=lua_tonumber(L
,narg
);
5968 if(d
==0&&!lua_isnumber(L
,narg
))
5969 tag_error(L
,narg
,3);
5972 static lua_Integer
luaL_checkinteger(lua_State
*L
,int narg
){
5973 lua_Integer d
=lua_tointeger(L
,narg
);
5974 if(d
==0&&!lua_isnumber(L
,narg
))
5975 tag_error(L
,narg
,3);
5978 static lua_Integer
luaL_optinteger(lua_State
*L
,int narg
,
5980 return luaL_opt(L
,luaL_checkinteger
,narg
,def
);
5982 static int luaL_getmetafield(lua_State
*L
,int obj
,const char*event
){
5983 if(!lua_getmetatable(L
,obj
))
5985 lua_pushstring(L
,event
);
5987 if(lua_isnil(L
,-1)){
5996 static void luaL_register(lua_State
*L
,const char*libname
,
5998 luaI_openlib(L
,libname
,l
,0);
6000 static int libsize(const luaL_Reg
*l
){
6002 for(;l
->name
;l
++)size
++;
6005 static void luaI_openlib(lua_State
*L
,const char*libname
,
6006 const luaL_Reg
*l
,int nup
){
6008 int size
=libsize(l
);
6009 luaL_findtable(L
,(-10000),"_LOADED",1);
6010 lua_getfield(L
,-1,libname
);
6011 if(!lua_istable(L
,-1)){
6013 if(luaL_findtable(L
,(-10002),libname
,size
)!=NULL
)
6014 luaL_error(L
,"name conflict for module "LUA_QL("%s"),libname
);
6015 lua_pushvalue(L
,-1);
6016 lua_setfield(L
,-3,libname
);
6019 lua_insert(L
,-(nup
+1));
6024 lua_pushvalue(L
,-nup
);
6025 lua_pushcclosure(L
,l
->func
,nup
);
6026 lua_setfield(L
,-(nup
+2),l
->name
);
6030 static const char*luaL_findtable(lua_State
*L
,int idx
,
6031 const char*fname
,int szhint
){
6033 lua_pushvalue(L
,idx
);
6035 e
=strchr(fname
,'.');
6036 if(e
==NULL
)e
=fname
+strlen(fname
);
6037 lua_pushlstring(L
,fname
,e
-fname
);
6039 if(lua_isnil(L
,-1)){
6041 lua_createtable(L
,0,(*e
=='.'?1:szhint
));
6042 lua_pushlstring(L
,fname
,e
-fname
);
6043 lua_pushvalue(L
,-2);
6046 else if(!lua_istable(L
,-1)){
6055 #define bufflen(B)((B)->p-(B)->buffer)
6056 #define bufffree(B)((size_t)(BUFSIZ-bufflen(B)))
6057 static int emptybuffer(luaL_Buffer
*B
){
6058 size_t l
=bufflen(B
);
6061 lua_pushlstring(B
->L
,B
->buffer
,l
);
6067 static void adjuststack(luaL_Buffer
*B
){
6071 size_t toplen
=lua_strlen(L
,-1);
6073 size_t l
=lua_strlen(L
,-(toget
+1));
6074 if(B
->lvl
-toget
+1>=(20/2)||toplen
>l
){
6079 }while(toget
<B
->lvl
);
6080 lua_concat(L
,toget
);
6081 B
->lvl
=B
->lvl
-toget
+1;
6084 static char*luaL_prepbuffer(luaL_Buffer
*B
){
6089 static void luaL_addlstring(luaL_Buffer
*B
,const char*s
,size_t l
){
6091 luaL_addchar(B
,*s
++);
6093 static void luaL_pushresult(luaL_Buffer
*B
){
6095 lua_concat(B
->L
,B
->lvl
);
6098 static void luaL_addvalue(luaL_Buffer
*B
){
6101 const char*s
=lua_tolstring(L
,-1,&vl
);
6102 if(vl
<=bufffree(B
)){
6114 static void luaL_buffinit(lua_State
*L
,luaL_Buffer
*B
){
6119 typedef struct LoadF
{
6124 static const char*getF(lua_State
*L
,void*ud
,size_t*size
){
6125 LoadF
*lf
=(LoadF
*)ud
;
6132 if(feof(lf
->f
))return NULL
;
6133 *size
=fread(lf
->buff
,1,sizeof(lf
->buff
),lf
->f
);
6134 return(*size
>0)?lf
->buff
:NULL
;
6136 static int errfile(lua_State
*L
,const char*what
,int fnameindex
){
6137 const char*serr
=strerror(errno
);
6138 const char*filename
=lua_tostring(L
,fnameindex
)+1;
6139 lua_pushfstring(L
,"cannot %s %s: %s",what
,filename
,serr
);
6140 lua_remove(L
,fnameindex
);
6143 static int luaL_loadfile(lua_State
*L
,const char*filename
){
6145 int status
,readstatus
;
6147 int fnameindex
=lua_gettop(L
)+1;
6150 lua_pushliteral(L
,"=stdin");
6154 lua_pushfstring(L
,"@%s",filename
);
6155 lf
.f
=fopen(filename
,"r");
6156 if(lf
.f
==NULL
)return errfile(L
,"open",fnameindex
);
6161 while((c
=getc(lf
.f
))!=EOF
&&c
!='\n');
6162 if(c
=='\n')c
=getc(lf
.f
);
6164 if(c
=="\033Lua"[0]&&filename
){
6165 lf
.f
=freopen(filename
,"rb",lf
.f
);
6166 if(lf
.f
==NULL
)return errfile(L
,"reopen",fnameindex
);
6167 while((c
=getc(lf
.f
))!=EOF
&&c
!="\033Lua"[0]);
6171 status
=lua_load(L
,getF
,&lf
,lua_tostring(L
,-1));
6172 readstatus
=ferror(lf
.f
);
6173 if(filename
)fclose(lf
.f
);
6175 lua_settop(L
,fnameindex
);
6176 return errfile(L
,"read",fnameindex
);
6178 lua_remove(L
,fnameindex
);
6181 typedef struct LoadS
{
6185 static const char*getS(lua_State
*L
,void*ud
,size_t*size
){
6186 LoadS
*ls
=(LoadS
*)ud
;
6188 if(ls
->size
==0)return NULL
;
6193 static int luaL_loadbuffer(lua_State
*L
,const char*buff
,size_t size
,
6198 return lua_load(L
,getS
,&ls
,name
);
6200 static void*l_alloc(void*ud
,void*ptr
,size_t osize
,size_t nsize
){
6208 return realloc(ptr
,nsize
);
6210 static int panic(lua_State
*L
){
6212 fprintf(stderr
,"PANIC: unprotected error in call to Lua API (%s)\n",
6213 lua_tostring(L
,-1));
6216 static lua_State
*luaL_newstate(void){
6217 lua_State
*L
=lua_newstate(l_alloc
,NULL
);
6218 if(L
)lua_atpanic(L
,&panic
);
6221 static int luaB_tonumber(lua_State
*L
){
6222 int base
=luaL_optint(L
,2,10);
6225 if(lua_isnumber(L
,1)){
6226 lua_pushnumber(L
,lua_tonumber(L
,1));
6231 const char*s1
=luaL_checkstring(L
,1);
6234 luaL_argcheck(L
,2<=base
&&base
<=36,2,"base out of range");
6235 n
=strtoul(s1
,&s2
,base
);
6237 while(isspace((unsigned char)(*s2
)))s2
++;
6239 lua_pushnumber(L
,(lua_Number
)n
);
6247 static int luaB_error(lua_State
*L
){
6248 int level
=luaL_optint(L
,2,1);
6250 if(lua_isstring(L
,1)&&level
>0){
6251 luaL_where(L
,level
);
6255 return lua_error(L
);
6257 static int luaB_setmetatable(lua_State
*L
){
6258 int t
=lua_type(L
,2);
6259 luaL_checktype(L
,1,5);
6260 luaL_argcheck(L
,t
==0||t
==5,2,
6261 "nil or table expected");
6262 if(luaL_getmetafield(L
,1,"__metatable"))
6263 luaL_error(L
,"cannot change a protected metatable");
6265 lua_setmetatable(L
,1);
6268 static void getfunc(lua_State
*L
,int opt
){
6269 if(lua_isfunction(L
,1))lua_pushvalue(L
,1);
6272 int level
=opt
?luaL_optint(L
,1,1):luaL_checkint(L
,1);
6273 luaL_argcheck(L
,level
>=0,1,"level must be non-negative");
6274 if(lua_getstack(L
,level
,&ar
)==0)
6275 luaL_argerror(L
,1,"invalid level");
6276 lua_getinfo(L
,"f",&ar
);
6278 luaL_error(L
,"no function environment for tail call at level %d",
6282 static int luaB_setfenv(lua_State
*L
){
6283 luaL_checktype(L
,2,5);
6286 if(lua_isnumber(L
,1)&&lua_tonumber(L
,1)==0){
6292 else if(lua_iscfunction(L
,-2)||lua_setfenv(L
,-2)==0)
6294 LUA_QL("setfenv")" cannot change environment of given object");
6297 static int luaB_rawget(lua_State
*L
){
6298 luaL_checktype(L
,1,5);
6304 static int luaB_type(lua_State
*L
){
6306 lua_pushstring(L
,luaL_typename(L
,1));
6309 static int luaB_next(lua_State
*L
){
6310 luaL_checktype(L
,1,5);
6319 static int luaB_pairs(lua_State
*L
){
6320 luaL_checktype(L
,1,5);
6321 lua_pushvalue(L
,lua_upvalueindex(1));
6326 static int ipairsaux(lua_State
*L
){
6327 int i
=luaL_checkint(L
,2);
6328 luaL_checktype(L
,1,5);
6330 lua_pushinteger(L
,i
);
6332 return(lua_isnil(L
,-1))?0:2;
6334 static int luaB_ipairs(lua_State
*L
){
6335 luaL_checktype(L
,1,5);
6336 lua_pushvalue(L
,lua_upvalueindex(1));
6338 lua_pushinteger(L
,0);
6341 static int load_aux(lua_State
*L
,int status
){
6350 static int luaB_loadstring(lua_State
*L
){
6352 const char*s
=luaL_checklstring(L
,1,&l
);
6353 const char*chunkname
=luaL_optstring(L
,2,s
);
6354 return load_aux(L
,luaL_loadbuffer(L
,s
,l
,chunkname
));
6356 static int luaB_loadfile(lua_State
*L
){
6357 const char*fname
=luaL_optstring(L
,1,NULL
);
6358 return load_aux(L
,luaL_loadfile(L
,fname
));
6360 static int luaB_assert(lua_State
*L
){
6362 if(!lua_toboolean(L
,1))
6363 return luaL_error(L
,"%s",luaL_optstring(L
,2,"assertion failed!"));
6364 return lua_gettop(L
);
6366 static int luaB_unpack(lua_State
*L
){
6368 luaL_checktype(L
,1,5);
6369 i
=luaL_optint(L
,2,1);
6370 e
=luaL_opt(L
,luaL_checkint
,3,luaL_getn(L
,1));
6373 if(n
<=0||!lua_checkstack(L
,n
))
6374 return luaL_error(L
,"too many results to unpack");
6380 static int luaB_pcall(lua_State
*L
){
6383 status
=lua_pcall(L
,lua_gettop(L
)-1,(-1),0);
6384 lua_pushboolean(L
,(status
==0));
6386 return lua_gettop(L
);
6388 static int luaB_newproxy(lua_State
*L
){
6390 lua_newuserdata(L
,0);
6391 if(lua_toboolean(L
,1)==0)
6393 else if(lua_isboolean(L
,1)){
6395 lua_pushvalue(L
,-1);
6396 lua_pushboolean(L
,1);
6397 lua_rawset(L
,lua_upvalueindex(1));
6401 if(lua_getmetatable(L
,1)){
6402 lua_rawget(L
,lua_upvalueindex(1));
6403 validproxy
=lua_toboolean(L
,-1);
6406 luaL_argcheck(L
,validproxy
,1,"boolean or proxy expected");
6407 lua_getmetatable(L
,1);
6409 lua_setmetatable(L
,2);
6412 static const luaL_Reg base_funcs
[]={
6413 {"assert",luaB_assert
},
6414 {"error",luaB_error
},
6415 {"loadfile",luaB_loadfile
},
6416 {"loadstring",luaB_loadstring
},
6418 {"pcall",luaB_pcall
},
6419 {"rawget",luaB_rawget
},
6420 {"setfenv",luaB_setfenv
},
6421 {"setmetatable",luaB_setmetatable
},
6422 {"tonumber",luaB_tonumber
},
6424 {"unpack",luaB_unpack
},
6427 static void auxopen(lua_State
*L
,const char*name
,
6428 lua_CFunction f
,lua_CFunction u
){
6429 lua_pushcfunction(L
,u
);
6430 lua_pushcclosure(L
,f
,1);
6431 lua_setfield(L
,-2,name
);
6433 static void base_open(lua_State
*L
){
6434 lua_pushvalue(L
,(-10002));
6435 lua_setglobal(L
,"_G");
6436 luaL_register(L
,"_G",base_funcs
);
6437 lua_pushliteral(L
,"Lua 5.1");
6438 lua_setglobal(L
,"_VERSION");
6439 auxopen(L
,"ipairs",luaB_ipairs
,ipairsaux
);
6440 auxopen(L
,"pairs",luaB_pairs
,luaB_next
);
6441 lua_createtable(L
,0,1);
6442 lua_pushvalue(L
,-1);
6443 lua_setmetatable(L
,-2);
6444 lua_pushliteral(L
,"kv");
6445 lua_setfield(L
,-2,"__mode");
6446 lua_pushcclosure(L
,luaB_newproxy
,1);
6447 lua_setglobal(L
,"newproxy");
6449 static int luaopen_base(lua_State
*L
){
6453 #define aux_getn(L,n)(luaL_checktype(L,n,5),luaL_getn(L,n))
6454 static int tinsert(lua_State
*L
){
6455 int e
=aux_getn(L
,1)+1;
6457 switch(lua_gettop(L
)){
6464 pos
=luaL_checkint(L
,2);
6467 lua_rawgeti(L
,1,i
-1);
6473 return luaL_error(L
,"wrong number of arguments to "LUA_QL("insert"));
6477 lua_rawseti(L
,1,pos
);
6480 static int tremove(lua_State
*L
){
6481 int e
=aux_getn(L
,1);
6482 int pos
=luaL_optint(L
,2,e
);
6483 if(!(1<=pos
&&pos
<=e
))
6486 lua_rawgeti(L
,1,pos
);
6488 lua_rawgeti(L
,1,pos
+1);
6489 lua_rawseti(L
,1,pos
);
6495 static void addfield(lua_State
*L
,luaL_Buffer
*b
,int i
){
6497 if(!lua_isstring(L
,-1))
6498 luaL_error(L
,"invalid value (%s) at index %d in table for "
6499 LUA_QL("concat"),luaL_typename(L
,-1),i
);
6502 static int tconcat(lua_State
*L
){
6506 const char*sep
=luaL_optlstring(L
,2,"",&lsep
);
6507 luaL_checktype(L
,1,5);
6508 i
=luaL_optint(L
,3,1);
6509 last
=luaL_opt(L
,luaL_checkint
,4,luaL_getn(L
,1));
6510 luaL_buffinit(L
,&b
);
6513 luaL_addlstring(&b
,sep
,lsep
);
6517 luaL_pushresult(&b
);
6520 static void set2(lua_State
*L
,int i
,int j
){
6524 static int sort_comp(lua_State
*L
,int a
,int b
){
6525 if(!lua_isnil(L
,2)){
6528 lua_pushvalue(L
,a
-1);
6529 lua_pushvalue(L
,b
-2);
6531 res
=lua_toboolean(L
,-1);
6536 return lua_lessthan(L
,a
,b
);
6538 static void auxsort(lua_State
*L
,int l
,int u
){
6543 if(sort_comp(L
,-1,-2))
6551 if(sort_comp(L
,-2,-1))
6556 if(sort_comp(L
,-1,-2))
6563 lua_pushvalue(L
,-1);
6564 lua_rawgeti(L
,1,u
-1);
6568 while(lua_rawgeti(L
,1,++i
),sort_comp(L
,-1,-2)){
6569 if(i
>u
)luaL_error(L
,"invalid order function for sorting");
6572 while(lua_rawgeti(L
,1,--j
),sort_comp(L
,-3,-1)){
6573 if(j
<l
)luaL_error(L
,"invalid order function for sorting");
6582 lua_rawgeti(L
,1,u
-1);
6594 static int sort(lua_State
*L
){
6595 int n
=aux_getn(L
,1);
6596 luaL_checkstack(L
,40,"");
6597 if(!lua_isnoneornil(L
,2))
6598 luaL_checktype(L
,2,6);
6603 static const luaL_Reg tab_funcs
[]={
6610 static int luaopen_table(lua_State
*L
){
6611 luaL_register(L
,"table",tab_funcs
);
6614 static const char*const fnames
[]={"input","output"};
6615 static int pushresult(lua_State
*L
,int i
,const char*filename
){
6618 lua_pushboolean(L
,1);
6624 lua_pushfstring(L
,"%s: %s",filename
,strerror(en
));
6626 lua_pushfstring(L
,"%s",strerror(en
));
6627 lua_pushinteger(L
,en
);
6631 static void fileerror(lua_State
*L
,int arg
,const char*filename
){
6632 lua_pushfstring(L
,"%s: %s",filename
,strerror(errno
));
6633 luaL_argerror(L
,arg
,lua_tostring(L
,-1));
6635 #define tofilep(L)((FILE**)luaL_checkudata(L,1,"FILE*"))
6636 static int io_type(lua_State
*L
){
6639 ud
=lua_touserdata(L
,1);
6640 lua_getfield(L
,(-10000),"FILE*");
6641 if(ud
==NULL
||!lua_getmetatable(L
,1)||!lua_rawequal(L
,-2,-1))
6643 else if(*((FILE**)ud
)==NULL
)
6644 lua_pushliteral(L
,"closed file");
6646 lua_pushliteral(L
,"file");
6649 static FILE*tofile(lua_State
*L
){
6652 luaL_error(L
,"attempt to use a closed file");
6655 static FILE**newfile(lua_State
*L
){
6656 FILE**pf
=(FILE**)lua_newuserdata(L
,sizeof(FILE*));
6658 luaL_getmetatable(L
,"FILE*");
6659 lua_setmetatable(L
,-2);
6662 static int io_noclose(lua_State
*L
){
6664 lua_pushliteral(L
,"cannot close standard file");
6667 static int io_pclose(lua_State
*L
){
6669 int ok
=lua_pclose(L
,*p
);
6671 return pushresult(L
,ok
,NULL
);
6673 static int io_fclose(lua_State
*L
){
6675 int ok
=(fclose(*p
)==0);
6677 return pushresult(L
,ok
,NULL
);
6679 static int aux_close(lua_State
*L
){
6681 lua_getfield(L
,-1,"__close");
6682 return(lua_tocfunction(L
,-1))(L
);
6684 static int io_close(lua_State
*L
){
6686 lua_rawgeti(L
,(-10001),2);
6688 return aux_close(L
);
6690 static int io_gc(lua_State
*L
){
6696 static int io_open(lua_State
*L
){
6697 const char*filename
=luaL_checkstring(L
,1);
6698 const char*mode
=luaL_optstring(L
,2,"r");
6699 FILE**pf
=newfile(L
);
6700 *pf
=fopen(filename
,mode
);
6701 return(*pf
==NULL
)?pushresult(L
,0,filename
):1;
6703 static FILE*getiofile(lua_State
*L
,int findex
){
6705 lua_rawgeti(L
,(-10001),findex
);
6706 f
=*(FILE**)lua_touserdata(L
,-1);
6708 luaL_error(L
,"standard %s file is closed",fnames
[findex
-1]);
6711 static int g_iofile(lua_State
*L
,int f
,const char*mode
){
6712 if(!lua_isnoneornil(L
,1)){
6713 const char*filename
=lua_tostring(L
,1);
6715 FILE**pf
=newfile(L
);
6716 *pf
=fopen(filename
,mode
);
6718 fileerror(L
,1,filename
);
6724 lua_rawseti(L
,(-10001),f
);
6726 lua_rawgeti(L
,(-10001),f
);
6729 static int io_input(lua_State
*L
){
6730 return g_iofile(L
,1,"r");
6732 static int io_output(lua_State
*L
){
6733 return g_iofile(L
,2,"w");
6735 static int io_readline(lua_State
*L
);
6736 static void aux_lines(lua_State
*L
,int idx
,int toclose
){
6737 lua_pushvalue(L
,idx
);
6738 lua_pushboolean(L
,toclose
);
6739 lua_pushcclosure(L
,io_readline
,2);
6741 static int f_lines(lua_State
*L
){
6746 static int io_lines(lua_State
*L
){
6747 if(lua_isnoneornil(L
,1)){
6748 lua_rawgeti(L
,(-10001),1);
6752 const char*filename
=luaL_checkstring(L
,1);
6753 FILE**pf
=newfile(L
);
6754 *pf
=fopen(filename
,"r");
6756 fileerror(L
,1,filename
);
6757 aux_lines(L
,lua_gettop(L
),1);
6761 static int read_number(lua_State
*L
,FILE*f
){
6763 if(fscanf(f
,"%lf",&d
)==1){
6764 lua_pushnumber(L
,d
);
6772 static int test_eof(lua_State
*L
,FILE*f
){
6775 lua_pushlstring(L
,NULL
,0);
6778 static int read_line(lua_State
*L
,FILE*f
){
6780 luaL_buffinit(L
,&b
);
6783 char*p
=luaL_prepbuffer(&b
);
6784 if(fgets(p
,BUFSIZ
,f
)==NULL
){
6785 luaL_pushresult(&b
);
6786 return(lua_objlen(L
,-1)>0);
6789 if(l
==0||p
[l
-1]!='\n')
6792 luaL_addsize(&b
,l
-1);
6793 luaL_pushresult(&b
);
6798 static int read_chars(lua_State
*L
,FILE*f
,size_t n
){
6802 luaL_buffinit(L
,&b
);
6805 char*p
=luaL_prepbuffer(&b
);
6807 nr
=fread(p
,sizeof(char),rlen
,f
);
6808 luaL_addsize(&b
,nr
);
6810 }while(n
>0&&nr
==rlen
);
6811 luaL_pushresult(&b
);
6812 return(n
==0||lua_objlen(L
,-1)>0);
6814 static int g_read(lua_State
*L
,FILE*f
,int first
){
6815 int nargs
=lua_gettop(L
)-1;
6820 success
=read_line(L
,f
);
6824 luaL_checkstack(L
,nargs
+20,"too many arguments");
6826 for(n
=first
;nargs
--&&success
;n
++){
6827 if(lua_type(L
,n
)==3){
6828 size_t l
=(size_t)lua_tointeger(L
,n
);
6829 success
=(l
==0)?test_eof(L
,f
):read_chars(L
,f
,l
);
6832 const char*p
=lua_tostring(L
,n
);
6833 luaL_argcheck(L
,p
&&p
[0]=='*',n
,"invalid option");
6836 success
=read_number(L
,f
);
6839 success
=read_line(L
,f
);
6842 read_chars(L
,f
,~((size_t)0));
6846 return luaL_argerror(L
,n
,"invalid format");
6852 return pushresult(L
,0,NULL
);
6859 static int io_read(lua_State
*L
){
6860 return g_read(L
,getiofile(L
,1),1);
6862 static int f_read(lua_State
*L
){
6863 return g_read(L
,tofile(L
),2);
6865 static int io_readline(lua_State
*L
){
6866 FILE*f
=*(FILE**)lua_touserdata(L
,lua_upvalueindex(1));
6869 luaL_error(L
,"file is already closed");
6870 sucess
=read_line(L
,f
);
6872 return luaL_error(L
,"%s",strerror(errno
));
6875 if(lua_toboolean(L
,lua_upvalueindex(2))){
6877 lua_pushvalue(L
,lua_upvalueindex(1));
6883 static int g_write(lua_State
*L
,FILE*f
,int arg
){
6884 int nargs
=lua_gettop(L
)-1;
6886 for(;nargs
--;arg
++){
6887 if(lua_type(L
,arg
)==3){
6889 fprintf(f
,"%.14g",lua_tonumber(L
,arg
))>0;
6893 const char*s
=luaL_checklstring(L
,arg
,&l
);
6894 status
=status
&&(fwrite(s
,sizeof(char),l
,f
)==l
);
6897 return pushresult(L
,status
,NULL
);
6899 static int io_write(lua_State
*L
){
6900 return g_write(L
,getiofile(L
,2),1);
6902 static int f_write(lua_State
*L
){
6903 return g_write(L
,tofile(L
),2);
6905 static int io_flush(lua_State
*L
){
6906 return pushresult(L
,fflush(getiofile(L
,2))==0,NULL
);
6908 static int f_flush(lua_State
*L
){
6909 return pushresult(L
,fflush(tofile(L
))==0,NULL
);
6911 static const luaL_Reg iolib
[]={
6917 {"output",io_output
},
6923 static const luaL_Reg flib
[]={
6932 static void createmeta(lua_State
*L
){
6933 luaL_newmetatable(L
,"FILE*");
6934 lua_pushvalue(L
,-1);
6935 lua_setfield(L
,-2,"__index");
6936 luaL_register(L
,NULL
,flib
);
6938 static void createstdfile(lua_State
*L
,FILE*f
,int k
,const char*fname
){
6941 lua_pushvalue(L
,-1);
6942 lua_rawseti(L
,(-10001),k
);
6944 lua_pushvalue(L
,-2);
6946 lua_setfield(L
,-3,fname
);
6948 static void newfenv(lua_State
*L
,lua_CFunction cls
){
6949 lua_createtable(L
,0,1);
6950 lua_pushcfunction(L
,cls
);
6951 lua_setfield(L
,-2,"__close");
6953 static int luaopen_io(lua_State
*L
){
6955 newfenv(L
,io_fclose
);
6956 lua_replace(L
,(-10001));
6957 luaL_register(L
,"io",iolib
);
6958 newfenv(L
,io_noclose
);
6959 createstdfile(L
,stdin
,1,"stdin");
6960 createstdfile(L
,stdout
,2,"stdout");
6961 createstdfile(L
,stderr
,0,"stderr");
6963 lua_getfield(L
,-1,"popen");
6964 newfenv(L
,io_pclose
);
6969 static int os_pushresult(lua_State
*L
,int i
,const char*filename
){
6972 lua_pushboolean(L
,1);
6977 lua_pushfstring(L
,"%s: %s",filename
,strerror(en
));
6978 lua_pushinteger(L
,en
);
6982 static int os_remove(lua_State
*L
){
6983 const char*filename
=luaL_checkstring(L
,1);
6984 return os_pushresult(L
,remove(filename
)==0,filename
);
6986 static int os_exit(lua_State
*L
){
6987 exit(luaL_optint(L
,1,EXIT_SUCCESS
));
6989 static const luaL_Reg syslib
[]={
6991 {"remove",os_remove
},
6994 static int luaopen_os(lua_State
*L
){
6995 luaL_register(L
,"os",syslib
);
6998 #define uchar(c)((unsigned char)(c))
6999 static ptrdiff_t posrelat(ptrdiff_t pos
,size_t len
){
7000 if(pos
<0)pos
+=(ptrdiff_t)len
+1;
7001 return(pos
>=0)?pos
:0;
7003 static int str_sub(lua_State
*L
){
7005 const char*s
=luaL_checklstring(L
,1,&l
);
7006 ptrdiff_t start
=posrelat(luaL_checkinteger(L
,2),l
);
7007 ptrdiff_t end
=posrelat(luaL_optinteger(L
,3,-1),l
);
7009 if(end
>(ptrdiff_t)l
)end
=(ptrdiff_t)l
;
7011 lua_pushlstring(L
,s
+start
-1,end
-start
+1);
7012 else lua_pushliteral(L
,"");
7015 static int str_lower(lua_State
*L
){
7019 const char*s
=luaL_checklstring(L
,1,&l
);
7020 luaL_buffinit(L
,&b
);
7022 luaL_addchar(&b
,tolower(uchar(s
[i
])));
7023 luaL_pushresult(&b
);
7026 static int str_upper(lua_State
*L
){
7030 const char*s
=luaL_checklstring(L
,1,&l
);
7031 luaL_buffinit(L
,&b
);
7033 luaL_addchar(&b
,toupper(uchar(s
[i
])));
7034 luaL_pushresult(&b
);
7037 static int str_rep(lua_State
*L
){
7040 const char*s
=luaL_checklstring(L
,1,&l
);
7041 int n
=luaL_checkint(L
,2);
7042 luaL_buffinit(L
,&b
);
7044 luaL_addlstring(&b
,s
,l
);
7045 luaL_pushresult(&b
);
7048 static int str_byte(lua_State
*L
){
7050 const char*s
=luaL_checklstring(L
,1,&l
);
7051 ptrdiff_t posi
=posrelat(luaL_optinteger(L
,2,1),l
);
7052 ptrdiff_t pose
=posrelat(luaL_optinteger(L
,3,posi
),l
);
7055 if((size_t)pose
>l
)pose
=l
;
7056 if(posi
>pose
)return 0;
7057 n
=(int)(pose
-posi
+1);
7059 luaL_error(L
,"string slice too long");
7060 luaL_checkstack(L
,n
,"string slice too long");
7062 lua_pushinteger(L
,uchar(s
[posi
+i
-1]));
7065 static int str_char(lua_State
*L
){
7066 int n
=lua_gettop(L
);
7069 luaL_buffinit(L
,&b
);
7071 int c
=luaL_checkint(L
,i
);
7072 luaL_argcheck(L
,uchar(c
)==c
,i
,"invalid value");
7073 luaL_addchar(&b
,uchar(c
));
7075 luaL_pushresult(&b
);
7078 typedef struct MatchState
{
7079 const char*src_init
;
7088 static int check_capture(MatchState
*ms
,int l
){
7090 if(l
<0||l
>=ms
->level
||ms
->capture
[l
].len
==(-1))
7091 return luaL_error(ms
->L
,"invalid capture index");
7094 static int capture_to_close(MatchState
*ms
){
7095 int level
=ms
->level
;
7096 for(level
--;level
>=0;level
--)
7097 if(ms
->capture
[level
].len
==(-1))return level
;
7098 return luaL_error(ms
->L
,"invalid pattern capture");
7100 static const char*classend(MatchState
*ms
,const char*p
){
7104 luaL_error(ms
->L
,"malformed pattern (ends with "LUA_QL("%%")")");
7111 luaL_error(ms
->L
,"malformed pattern (missing "LUA_QL("]")")");
7112 if(*(p
++)=='%'&&*p
!='\0')
7122 static int match_class(int c
,int cl
){
7124 switch(tolower(cl
)){
7125 case'a':res
=isalpha(c
);break;
7126 case'c':res
=iscntrl(c
);break;
7127 case'd':res
=isdigit(c
);break;
7128 case'l':res
=islower(c
);break;
7129 case'p':res
=ispunct(c
);break;
7130 case's':res
=isspace(c
);break;
7131 case'u':res
=isupper(c
);break;
7132 case'w':res
=isalnum(c
);break;
7133 case'x':res
=isxdigit(c
);break;
7134 case'z':res
=(c
==0);break;
7135 default:return(cl
==c
);
7137 return(islower(cl
)?res
:!res
);
7139 static int matchbracketclass(int c
,const char*p
,const char*ec
){
7148 if(match_class(c
,uchar(*p
)))
7151 else if((*(p
+1)=='-')&&(p
+2<ec
)){
7153 if(uchar(*(p
-2))<=c
&&c
<=uchar(*p
))
7156 else if(uchar(*p
)==c
)return sig
;
7160 static int singlematch(int c
,const char*p
,const char*ep
){
7163 case'%':return match_class(c
,uchar(*(p
+1)));
7164 case'[':return matchbracketclass(c
,p
,ep
-1);
7165 default:return(uchar(*p
)==c
);
7168 static const char*match(MatchState
*ms
,const char*s
,const char*p
);
7169 static const char*matchbalance(MatchState
*ms
,const char*s
,
7171 if(*p
==0||*(p
+1)==0)
7172 luaL_error(ms
->L
,"unbalanced pattern");
7173 if(*s
!=*p
)return NULL
;
7178 while(++s
<ms
->src_end
){
7180 if(--cont
==0)return s
+1;
7182 else if(*s
==b
)cont
++;
7187 static const char*max_expand(MatchState
*ms
,const char*s
,
7188 const char*p
,const char*ep
){
7190 while((s
+i
)<ms
->src_end
&&singlematch(uchar(*(s
+i
)),p
,ep
))
7193 const char*res
=match(ms
,(s
+i
),ep
+1);
7199 static const char*min_expand(MatchState
*ms
,const char*s
,
7200 const char*p
,const char*ep
){
7202 const char*res
=match(ms
,s
,ep
+1);
7205 else if(s
<ms
->src_end
&&singlematch(uchar(*s
),p
,ep
))
7210 static const char*start_capture(MatchState
*ms
,const char*s
,
7211 const char*p
,int what
){
7213 int level
=ms
->level
;
7214 if(level
>=32)luaL_error(ms
->L
,"too many captures");
7215 ms
->capture
[level
].init
=s
;
7216 ms
->capture
[level
].len
=what
;
7218 if((res
=match(ms
,s
,p
))==NULL
)
7222 static const char*end_capture(MatchState
*ms
,const char*s
,
7224 int l
=capture_to_close(ms
);
7226 ms
->capture
[l
].len
=s
-ms
->capture
[l
].init
;
7227 if((res
=match(ms
,s
,p
))==NULL
)
7228 ms
->capture
[l
].len
=(-1);
7231 static const char*match_capture(MatchState
*ms
,const char*s
,int l
){
7233 l
=check_capture(ms
,l
);
7234 len
=ms
->capture
[l
].len
;
7235 if((size_t)(ms
->src_end
-s
)>=len
&&
7236 memcmp(ms
->capture
[l
].init
,s
,len
)==0)
7240 static const char*match(MatchState
*ms
,const char*s
,const char*p
){
7245 return start_capture(ms
,s
,p
+2,(-2));
7247 return start_capture(ms
,s
,p
+1,(-1));
7250 return end_capture(ms
,s
,p
+1);
7255 s
=matchbalance(ms
,s
,p
+2);
7256 if(s
==NULL
)return NULL
;
7260 const char*ep
;char previous
;
7263 luaL_error(ms
->L
,"missing "LUA_QL("[")" after "
7264 LUA_QL("%%f")" in pattern");
7266 previous
=(s
==ms
->src_init
)?'\0':*(s
-1);
7267 if(matchbracketclass(uchar(previous
),p
,ep
-1)||
7268 !matchbracketclass(uchar(*s
),p
,ep
-1))return NULL
;
7272 if(isdigit(uchar(*(p
+1)))){
7273 s
=match_capture(ms
,s
,uchar(*(p
+1)));
7274 if(s
==NULL
)return NULL
;
7286 return(s
==ms
->src_end
)?s
:NULL
;
7290 const char*ep
=classend(ms
,p
);
7291 int m
=s
<ms
->src_end
&&singlematch(uchar(*s
),p
,ep
);
7295 if(m
&&((res
=match(ms
,s
+1,ep
+1))!=NULL
))
7300 return max_expand(ms
,s
,p
,ep
);
7303 return(m
?max_expand(ms
,s
+1,p
,ep
):NULL
);
7306 return min_expand(ms
,s
,p
,ep
);
7316 static const char*lmemfind(const char*s1
,size_t l1
,
7317 const char*s2
,size_t l2
){
7319 else if(l2
>l1
)return NULL
;
7324 while(l1
>0&&(init
=(const char*)memchr(s1
,*s2
,l1
))!=NULL
){
7326 if(memcmp(init
,s2
+1,l2
)==0)
7336 static void push_onecapture(MatchState
*ms
,int i
,const char*s
,
7340 lua_pushlstring(ms
->L
,s
,e
-s
);
7342 luaL_error(ms
->L
,"invalid capture index");
7345 ptrdiff_t l
=ms
->capture
[i
].len
;
7346 if(l
==(-1))luaL_error(ms
->L
,"unfinished capture");
7348 lua_pushinteger(ms
->L
,ms
->capture
[i
].init
-ms
->src_init
+1);
7350 lua_pushlstring(ms
->L
,ms
->capture
[i
].init
,l
);
7353 static int push_captures(MatchState
*ms
,const char*s
,const char*e
){
7355 int nlevels
=(ms
->level
==0&&s
)?1:ms
->level
;
7356 luaL_checkstack(ms
->L
,nlevels
,"too many captures");
7357 for(i
=0;i
<nlevels
;i
++)
7358 push_onecapture(ms
,i
,s
,e
);
7361 static int str_find_aux(lua_State
*L
,int find
){
7363 const char*s
=luaL_checklstring(L
,1,&l1
);
7364 const char*p
=luaL_checklstring(L
,2,&l2
);
7365 ptrdiff_t init
=posrelat(luaL_optinteger(L
,3,1),l1
)-1;
7367 else if((size_t)(init
)>l1
)init
=(ptrdiff_t)l1
;
7368 if(find
&&(lua_toboolean(L
,4)||
7369 strpbrk(p
,"^$*+?.([%-")==NULL
)){
7370 const char*s2
=lmemfind(s
+init
,l1
-init
,p
,l2
);
7372 lua_pushinteger(L
,s2
-s
+1);
7373 lua_pushinteger(L
,s2
-s
+l2
);
7379 int anchor
=(*p
=='^')?(p
++,1):0;
7380 const char*s1
=s
+init
;
7387 if((res
=match(&ms
,s1
,p
))!=NULL
){
7389 lua_pushinteger(L
,s1
-s
+1);
7390 lua_pushinteger(L
,res
-s
);
7391 return push_captures(&ms
,NULL
,0)+2;
7394 return push_captures(&ms
,s1
,res
);
7396 }while(s1
++<ms
.src_end
&&!anchor
);
7401 static int str_find(lua_State
*L
){
7402 return str_find_aux(L
,1);
7404 static int str_match(lua_State
*L
){
7405 return str_find_aux(L
,0);
7407 static int gmatch_aux(lua_State
*L
){
7410 const char*s
=lua_tolstring(L
,lua_upvalueindex(1),&ls
);
7411 const char*p
=lua_tostring(L
,lua_upvalueindex(2));
7416 for(src
=s
+(size_t)lua_tointeger(L
,lua_upvalueindex(3));
7421 if((e
=match(&ms
,src
,p
))!=NULL
){
7422 lua_Integer newstart
=e
-s
;
7423 if(e
==src
)newstart
++;
7424 lua_pushinteger(L
,newstart
);
7425 lua_replace(L
,lua_upvalueindex(3));
7426 return push_captures(&ms
,src
,e
);
7431 static int gmatch(lua_State
*L
){
7432 luaL_checkstring(L
,1);
7433 luaL_checkstring(L
,2);
7435 lua_pushinteger(L
,0);
7436 lua_pushcclosure(L
,gmatch_aux
,3);
7439 static void add_s(MatchState
*ms
,luaL_Buffer
*b
,const char*s
,
7442 const char*news
=lua_tolstring(ms
->L
,3,&l
);
7445 luaL_addchar(b
,news
[i
]);
7448 if(!isdigit(uchar(news
[i
])))
7449 luaL_addchar(b
,news
[i
]);
7450 else if(news
[i
]=='0')
7451 luaL_addlstring(b
,s
,e
-s
);
7453 push_onecapture(ms
,news
[i
]-'1',s
,e
);
7459 static void add_value(MatchState
*ms
,luaL_Buffer
*b
,const char*s
,
7462 switch(lua_type(L
,3)){
7471 n
=push_captures(ms
,s
,e
);
7476 push_onecapture(ms
,0,s
,e
);
7481 if(!lua_toboolean(L
,-1)){
7483 lua_pushlstring(L
,s
,e
-s
);
7485 else if(!lua_isstring(L
,-1))
7486 luaL_error(L
,"invalid replacement value (a %s)",luaL_typename(L
,-1));
7489 static int str_gsub(lua_State
*L
){
7491 const char*src
=luaL_checklstring(L
,1,&srcl
);
7492 const char*p
=luaL_checkstring(L
,2);
7493 int tr
=lua_type(L
,3);
7494 int max_s
=luaL_optint(L
,4,srcl
+1);
7495 int anchor
=(*p
=='^')?(p
++,1):0;
7499 luaL_argcheck(L
,tr
==3||tr
==4||
7501 "string/function/table expected");
7502 luaL_buffinit(L
,&b
);
7505 ms
.src_end
=src
+srcl
;
7512 add_value(&ms
,&b
,src
,e
);
7516 else if(src
<ms
.src_end
)
7517 luaL_addchar(&b
,*src
++);
7521 luaL_addlstring(&b
,src
,ms
.src_end
-src
);
7522 luaL_pushresult(&b
);
7523 lua_pushinteger(L
,n
);
7526 static void addquoted(lua_State
*L
,luaL_Buffer
*b
,int arg
){
7528 const char*s
=luaL_checklstring(L
,arg
,&l
);
7529 luaL_addchar(b
,'"');
7532 case'"':case'\\':case'\n':{
7533 luaL_addchar(b
,'\\');
7538 luaL_addlstring(b
,"\\r",2);
7542 luaL_addlstring(b
,"\\000",4);
7552 luaL_addchar(b
,'"');
7554 static const char*scanformat(lua_State
*L
,const char*strfrmt
,char*form
){
7555 const char*p
=strfrmt
;
7556 while(*p
!='\0'&&strchr("-+ #0",*p
)!=NULL
)p
++;
7557 if((size_t)(p
-strfrmt
)>=sizeof("-+ #0"))
7558 luaL_error(L
,"invalid format (repeated flags)");
7559 if(isdigit(uchar(*p
)))p
++;
7560 if(isdigit(uchar(*p
)))p
++;
7563 if(isdigit(uchar(*p
)))p
++;
7564 if(isdigit(uchar(*p
)))p
++;
7566 if(isdigit(uchar(*p
)))
7567 luaL_error(L
,"invalid format (width or precision too long)");
7569 strncpy(form
,strfrmt
,p
-strfrmt
+1);
7574 static void addintlen(char*form
){
7575 size_t l
=strlen(form
);
7576 char spec
=form
[l
-1];
7577 strcpy(form
+l
-1,"l");
7578 form
[l
+sizeof("l")-2]=spec
;
7579 form
[l
+sizeof("l")-1]='\0';
7581 static int str_format(lua_State
*L
){
7582 int top
=lua_gettop(L
);
7585 const char*strfrmt
=luaL_checklstring(L
,arg
,&sfl
);
7586 const char*strfrmt_end
=strfrmt
+sfl
;
7588 luaL_buffinit(L
,&b
);
7589 while(strfrmt
<strfrmt_end
){
7591 luaL_addchar(&b
,*strfrmt
++);
7592 else if(*++strfrmt
=='%')
7593 luaL_addchar(&b
,*strfrmt
++);
7595 char form
[(sizeof("-+ #0")+sizeof("l")+10)];
7598 luaL_argerror(L
,arg
,"no value");
7599 strfrmt
=scanformat(L
,strfrmt
,form
);
7602 sprintf(buff
,form
,(int)luaL_checknumber(L
,arg
));
7607 sprintf(buff
,form
,(long)luaL_checknumber(L
,arg
));
7610 case'o':case'u':case'x':case'X':{
7612 sprintf(buff
,form
,(unsigned long)luaL_checknumber(L
,arg
));
7615 case'e':case'E':case'f':
7617 sprintf(buff
,form
,(double)luaL_checknumber(L
,arg
));
7621 addquoted(L
,&b
,arg
);
7626 const char*s
=luaL_checklstring(L
,arg
,&l
);
7627 if(!strchr(form
,'.')&&l
>=100){
7628 lua_pushvalue(L
,arg
);
7633 sprintf(buff
,form
,s
);
7638 return luaL_error(L
,"invalid option "LUA_QL("%%%c")" to "
7639 LUA_QL("format"),*(strfrmt
-1));
7642 luaL_addlstring(&b
,buff
,strlen(buff
));
7645 luaL_pushresult(&b
);
7648 static const luaL_Reg strlib
[]={
7652 {"format",str_format
},
7655 {"lower",str_lower
},
7656 {"match",str_match
},
7659 {"upper",str_upper
},
7662 static void createmetatable(lua_State
*L
){
7663 lua_createtable(L
,0,1);
7664 lua_pushliteral(L
,"");
7665 lua_pushvalue(L
,-2);
7666 lua_setmetatable(L
,-2);
7668 lua_pushvalue(L
,-2);
7669 lua_setfield(L
,-2,"__index");
7672 static int luaopen_string(lua_State
*L
){
7673 luaL_register(L
,"string",strlib
);
7677 static const luaL_Reg lualibs
[]={
7679 {"table",luaopen_table
},
7682 {"string",luaopen_string
},
7685 static void luaL_openlibs(lua_State
*L
){
7686 const luaL_Reg
*lib
=lualibs
;
7687 for(;lib
->func
;lib
++){
7688 lua_pushcfunction(L
,lib
->func
);
7689 lua_pushstring(L
,lib
->name
);
7693 typedef unsigned int UB
;
7694 static UB
barg(lua_State
*L
,int idx
){
7695 union{lua_Number n
;U64 b
;}bn
;
7696 bn
.n
=lua_tonumber(L
,idx
)+6755399441055744.0;
7697 if(bn
.n
==0.0&&!lua_isnumber(L
,idx
))luaL_typerror(L
,idx
,"number");
7700 #define BRET(b)lua_pushnumber(L,(lua_Number)(int)(b));return 1;
7701 static int tobit(lua_State
*L
){
7703 static int bnot(lua_State
*L
){
7705 static int band(lua_State
*L
){
7706 int i
;UB b
=barg(L
,1);for(i
=lua_gettop(L
);i
>1;i
--)b
&=barg(L
,i
);BRET(b
)}
7707 static int bor(lua_State
*L
){
7708 int i
;UB b
=barg(L
,1);for(i
=lua_gettop(L
);i
>1;i
--)b
|=barg(L
,i
);BRET(b
)}
7709 static int bxor(lua_State
*L
){
7710 int i
;UB b
=barg(L
,1);for(i
=lua_gettop(L
);i
>1;i
--)b
^=barg(L
,i
);BRET(b
)}
7711 static int lshift(lua_State
*L
){
7712 UB b
=barg(L
,1),n
=barg(L
,2)&31;BRET(b
<<n
)}
7713 static int rshift(lua_State
*L
){
7714 UB b
=barg(L
,1),n
=barg(L
,2)&31;BRET(b
>>n
)}
7715 static int arshift(lua_State
*L
){
7716 UB b
=barg(L
,1),n
=barg(L
,2)&31;BRET((int)b
>>n
)}
7717 static int rol(lua_State
*L
){
7718 UB b
=barg(L
,1),n
=barg(L
,2)&31;BRET((b
<<n
)|(b
>>(32-n
)))}
7719 static int ror(lua_State
*L
){
7720 UB b
=barg(L
,1),n
=barg(L
,2)&31;BRET((b
>>n
)|(b
<<(32-n
)))}
7721 static int bswap(lua_State
*L
){
7722 UB b
=barg(L
,1);b
=(b
>>24)|((b
>>8)&0xff00)|((b
&0xff00)<<8)|(b
<<24);BRET(b
)}
7723 static int tohex(lua_State
*L
){
7725 int n
=lua_isnone(L
,2)?8:(int)barg(L
,2);
7726 const char*hexdigits
="0123456789abcdef";
7729 if(n
<0){n
=-n
;hexdigits
="0123456789ABCDEF";}
7731 for(i
=(int)n
;--i
>=0;){buf
[i
]=hexdigits
[b
&15];b
>>=4;}
7732 lua_pushlstring(L
,buf
,(size_t)n
);
7735 static const struct luaL_Reg bitlib
[]={
7743 {"arshift",arshift
},
7750 int main(int argc
,char**argv
){
7751 lua_State
*L
=luaL_newstate();
7754 luaL_register(L
,"bit",bitlib
);
7755 if(argc
<2)return sizeof(void*);
7756 lua_createtable(L
,0,1);
7757 lua_pushstring(L
,argv
[1]);
7758 lua_rawseti(L
,-2,0);
7759 lua_setglobal(L
,"arg");
7760 if(luaL_loadfile(L
,argv
[1]))
7763 lua_pushstring(L
,argv
[i
]);
7764 if(lua_pcall(L
,argc
-2,0,0)){
7766 fprintf(stderr
,"Error: %s\n",lua_tostring(L
,-1));