| 1 |
/* This is a heavily customized and minimized copy of Lua 5.1.5. */ |
| 2 |
/* It's only used to build LuaJIT. It does NOT have all standard functions! */ |
| 3 |
/****************************************************************************** |
| 4 |
* Copyright (C) 1994-2012 Lua.org, PUC-Rio. All rights reserved. |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining |
| 7 |
* a copy of this software and associated documentation files (the |
| 8 |
* "Software"), to deal in the Software without restriction, including |
| 9 |
* without limitation the rights to use, copy, modify, merge, publish, |
| 10 |
* distribute, sublicense, and/or sell copies of the Software, and to |
| 11 |
* permit persons to whom the Software is furnished to do so, subject to |
| 12 |
* the following conditions: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be |
| 15 |
* included in all copies or substantial portions of the Software. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 21 |
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 22 |
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 23 |
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
******************************************************************************/ |
| 25 |
#ifdef _MSC_VER |
| 26 |
typedef unsigned __int64 U64; |
| 27 |
#else |
| 28 |
typedef unsigned long long U64; |
| 29 |
#endif |
| 30 |
int _CRT_glob = 0; |
| 31 |
#include <stddef.h> |
| 32 |
#include <stdarg.h> |
| 33 |
#include <limits.h> |
| 34 |
#include <math.h> |
| 35 |
#include <ctype.h> |
| 36 |
#include <stdio.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <string.h> |
| 39 |
#include <setjmp.h> |
| 40 |
#include <errno.h> |
| 41 |
#include <time.h> |
| 42 |
typedef enum{ |
| 43 |
TM_INDEX, |
| 44 |
TM_NEWINDEX, |
| 45 |
TM_GC, |
| 46 |
TM_MODE, |
| 47 |
TM_EQ, |
| 48 |
TM_ADD, |
| 49 |
TM_SUB, |
| 50 |
TM_MUL, |
| 51 |
TM_DIV, |
| 52 |
TM_MOD, |
| 53 |
TM_POW, |
| 54 |
TM_UNM, |
| 55 |
TM_LEN, |
| 56 |
TM_LT, |
| 57 |
TM_LE, |
| 58 |
TM_CONCAT, |
| 59 |
TM_CALL, |
| 60 |
TM_N |
| 61 |
}TMS; |
| 62 |
enum OpMode{iABC,iABx,iAsBx}; |
| 63 |
typedef enum{ |
| 64 |
OP_MOVE, |
| 65 |
OP_LOADK, |
| 66 |
OP_LOADBOOL, |
| 67 |
OP_LOADNIL, |
| 68 |
OP_GETUPVAL, |
| 69 |
OP_GETGLOBAL, |
| 70 |
OP_GETTABLE, |
| 71 |
OP_SETGLOBAL, |
| 72 |
OP_SETUPVAL, |
| 73 |
OP_SETTABLE, |
| 74 |
OP_NEWTABLE, |
| 75 |
OP_SELF, |
| 76 |
OP_ADD, |
| 77 |
OP_SUB, |
| 78 |
OP_MUL, |
| 79 |
OP_DIV, |
| 80 |
OP_MOD, |
| 81 |
OP_POW, |
| 82 |
OP_UNM, |
| 83 |
OP_NOT, |
| 84 |
OP_LEN, |
| 85 |
OP_CONCAT, |
| 86 |
OP_JMP, |
| 87 |
OP_EQ, |
| 88 |
OP_LT, |
| 89 |
OP_LE, |
| 90 |
OP_TEST, |
| 91 |
OP_TESTSET, |
| 92 |
OP_CALL, |
| 93 |
OP_TAILCALL, |
| 94 |
OP_RETURN, |
| 95 |
OP_FORLOOP, |
| 96 |
OP_FORPREP, |
| 97 |
OP_TFORLOOP, |
| 98 |
OP_SETLIST, |
| 99 |
OP_CLOSE, |
| 100 |
OP_CLOSURE, |
| 101 |
OP_VARARG |
| 102 |
}OpCode; |
| 103 |
enum OpArgMask{ |
| 104 |
OpArgN, |
| 105 |
OpArgU, |
| 106 |
OpArgR, |
| 107 |
OpArgK |
| 108 |
}; |
| 109 |
typedef enum{ |
| 110 |
VVOID, |
| 111 |
VNIL, |
| 112 |
VTRUE, |
| 113 |
VFALSE, |
| 114 |
VK, |
| 115 |
VKNUM, |
| 116 |
VLOCAL, |
| 117 |
VUPVAL, |
| 118 |
VGLOBAL, |
| 119 |
VINDEXED, |
| 120 |
VJMP, |
| 121 |
VRELOCABLE, |
| 122 |
VNONRELOC, |
| 123 |
VCALL, |
| 124 |
VVARARG |
| 125 |
}expkind; |
| 126 |
enum RESERVED{ |
| 127 |
TK_AND=257,TK_BREAK, |
| 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 |
| 133 |
}; |
| 134 |
typedef enum BinOpr{ |
| 135 |
OPR_ADD,OPR_SUB,OPR_MUL,OPR_DIV,OPR_MOD,OPR_POW, |
| 136 |
OPR_CONCAT, |
| 137 |
OPR_NE,OPR_EQ, |
| 138 |
OPR_LT,OPR_LE,OPR_GT,OPR_GE, |
| 139 |
OPR_AND,OPR_OR, |
| 140 |
OPR_NOBINOPR |
| 141 |
}BinOpr; |
| 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); |
| 193 |
struct lua_Debug{ |
| 194 |
int event; |
| 195 |
const char*name; |
| 196 |
const char*namewhat; |
| 197 |
const char*what; |
| 198 |
const char*source; |
| 199 |
int currentline; |
| 200 |
int nups; |
| 201 |
int linedefined; |
| 202 |
int lastlinedefined; |
| 203 |
char short_src[60]; |
| 204 |
int i_ci; |
| 205 |
}; |
| 206 |
typedef unsigned int lu_int32; |
| 207 |
typedef size_t lu_mem; |
| 208 |
typedef ptrdiff_t l_mem; |
| 209 |
typedef unsigned char lu_byte; |
| 210 |
#define IntPoint(p)((unsigned int)(lu_mem)(p)) |
| 211 |
typedef union{double u;void*s;long l;}L_Umaxalign; |
| 212 |
typedef double l_uacNumber; |
| 213 |
#define check_exp(c,e)(e) |
| 214 |
#define UNUSED(x)((void)(x)) |
| 215 |
#define cast(t,exp)((t)(exp)) |
| 216 |
#define cast_byte(i)cast(lu_byte,(i)) |
| 217 |
#define cast_num(i)cast(lua_Number,(i)) |
| 218 |
#define cast_int(i)cast(int,(i)) |
| 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; |
| 224 |
}GCheader; |
| 225 |
typedef union{ |
| 226 |
GCObject*gc; |
| 227 |
void*p; |
| 228 |
lua_Number n; |
| 229 |
int b; |
| 230 |
}Value; |
| 231 |
typedef struct lua_TValue{ |
| 232 |
Value value;int tt; |
| 233 |
}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{ |
| 272 |
L_Umaxalign dummy; |
| 273 |
struct{ |
| 274 |
GCObject*next;lu_byte tt;lu_byte marked; |
| 275 |
lu_byte reserved; |
| 276 |
unsigned int hash; |
| 277 |
size_t len; |
| 278 |
}tsv; |
| 279 |
}TString; |
| 280 |
#define getstr(ts)cast(const char*,(ts)+1) |
| 281 |
#define svalue(o)getstr(rawtsvalue(o)) |
| 282 |
typedef union Udata{ |
| 283 |
L_Umaxalign dummy; |
| 284 |
struct{ |
| 285 |
GCObject*next;lu_byte tt;lu_byte marked; |
| 286 |
struct Table*metatable; |
| 287 |
struct Table*env; |
| 288 |
size_t len; |
| 289 |
}uv; |
| 290 |
}Udata; |
| 291 |
typedef struct Proto{ |
| 292 |
GCObject*next;lu_byte tt;lu_byte marked; |
| 293 |
TValue*k; |
| 294 |
Instruction*code; |
| 295 |
struct Proto**p; |
| 296 |
int*lineinfo; |
| 297 |
struct LocVar*locvars; |
| 298 |
TString**upvalues; |
| 299 |
TString*source; |
| 300 |
int sizeupvalues; |
| 301 |
int sizek; |
| 302 |
int sizecode; |
| 303 |
int sizelineinfo; |
| 304 |
int sizep; |
| 305 |
int sizelocvars; |
| 306 |
int linedefined; |
| 307 |
int lastlinedefined; |
| 308 |
GCObject*gclist; |
| 309 |
lu_byte nups; |
| 310 |
lu_byte numparams; |
| 311 |
lu_byte is_vararg; |
| 312 |
lu_byte maxstacksize; |
| 313 |
}Proto; |
| 314 |
typedef struct LocVar{ |
| 315 |
TString*varname; |
| 316 |
int startpc; |
| 317 |
int endpc; |
| 318 |
}LocVar; |
| 319 |
typedef struct UpVal{ |
| 320 |
GCObject*next;lu_byte tt;lu_byte marked; |
| 321 |
TValue*v; |
| 322 |
union{ |
| 323 |
TValue value; |
| 324 |
struct{ |
| 325 |
struct UpVal*prev; |
| 326 |
struct UpVal*next; |
| 327 |
}l; |
| 328 |
}u; |
| 329 |
}UpVal; |
| 330 |
typedef struct CClosure{ |
| 331 |
GCObject*next;lu_byte tt;lu_byte marked;lu_byte isC;lu_byte nupvalues;GCObject*gclist;struct Table*env; |
| 332 |
lua_CFunction f; |
| 333 |
TValue upvalue[1]; |
| 334 |
}CClosure; |
| 335 |
typedef struct LClosure{ |
| 336 |
GCObject*next;lu_byte tt;lu_byte marked;lu_byte isC;lu_byte nupvalues;GCObject*gclist;struct Table*env; |
| 337 |
struct Proto*p; |
| 338 |
UpVal*upvals[1]; |
| 339 |
}LClosure; |
| 340 |
typedef union Closure{ |
| 341 |
CClosure c; |
| 342 |
LClosure l; |
| 343 |
}Closure; |
| 344 |
#define iscfunction(o)(ttype(o)==6&&clvalue(o)->c.isC) |
| 345 |
typedef union TKey{ |
| 346 |
struct{ |
| 347 |
Value value;int tt; |
| 348 |
struct Node*next; |
| 349 |
}nk; |
| 350 |
TValue tvk; |
| 351 |
}TKey; |
| 352 |
typedef struct Node{ |
| 353 |
TValue i_val; |
| 354 |
TKey i_key; |
| 355 |
}Node; |
| 356 |
typedef struct Table{ |
| 357 |
GCObject*next;lu_byte tt;lu_byte marked; |
| 358 |
lu_byte flags; |
| 359 |
lu_byte lsizenode; |
| 360 |
struct Table*metatable; |
| 361 |
TValue*array; |
| 362 |
Node*node; |
| 363 |
Node*lastfree; |
| 364 |
GCObject*gclist; |
| 365 |
int sizearray; |
| 366 |
}Table; |
| 367 |
#define lmod(s,size)(check_exp((size&(size-1))==0,(cast(int,(s)&((size)-1))))) |
| 368 |
#define twoto(x)((size_t)1<<(x)) |
| 369 |
#define sizenode(t)(twoto((t)->lsizenode)) |
| 370 |
static const TValue luaO_nilobject_; |
| 371 |
#define ceillog2(x)(luaO_log2((x)-1)+1) |
| 372 |
static int luaO_log2(unsigned int x); |
| 373 |
#define gfasttm(g,et,e)((et)==NULL?NULL:((et)->flags&(1u<<(e)))?NULL:luaT_gettm(et,e,(g)->tmname[e])) |
| 374 |
#define fasttm(l,et,e)gfasttm(G(l),et,e) |
| 375 |
static const TValue*luaT_gettm(Table*events,TMS event,TString*ename); |
| 376 |
#define luaM_reallocv(L,b,on,n,e)((cast(size_t,(n)+1)<=((size_t)(~(size_t)0)-2)/(e))?luaM_realloc_(L,(b),(on)*(e),(n)*(e)):luaM_toobig(L)) |
| 377 |
#define luaM_freemem(L,b,s)luaM_realloc_(L,(b),(s),0) |
| 378 |
#define luaM_free(L,b)luaM_realloc_(L,(b),sizeof(*(b)),0) |
| 379 |
#define luaM_freearray(L,b,n,t)luaM_reallocv(L,(b),n,0,sizeof(t)) |
| 380 |
#define luaM_malloc(L,t)luaM_realloc_(L,NULL,0,(t)) |
| 381 |
#define luaM_new(L,t)cast(t*,luaM_malloc(L,sizeof(t))) |
| 382 |
#define luaM_newvector(L,n,t)cast(t*,luaM_reallocv(L,NULL,0,n,sizeof(t))) |
| 383 |
#define luaM_growvector(L,v,nelems,size,t,limit,e)if((nelems)+1>(size))((v)=cast(t*,luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) |
| 384 |
#define luaM_reallocvector(L,v,oldn,n,t)((v)=cast(t*,luaM_reallocv(L,v,oldn,n,sizeof(t)))) |
| 385 |
static void*luaM_realloc_(lua_State*L,void*block,size_t oldsize, |
| 386 |
size_t size); |
| 387 |
static void*luaM_toobig(lua_State*L); |
| 388 |
static void*luaM_growaux_(lua_State*L,void*block,int*size, |
| 389 |
size_t size_elem,int limit, |
| 390 |
const char*errormsg); |
| 391 |
typedef struct Zio ZIO; |
| 392 |
#define char2int(c)cast(int,cast(unsigned char,(c))) |
| 393 |
#define zgetc(z)(((z)->n--)>0?char2int(*(z)->p++):luaZ_fill(z)) |
| 394 |
typedef struct Mbuffer{ |
| 395 |
char*buffer; |
| 396 |
size_t n; |
| 397 |
size_t buffsize; |
| 398 |
}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) |
| 406 |
struct Zio{ |
| 407 |
size_t n; |
| 408 |
const char*p; |
| 409 |
lua_Reader reader; |
| 410 |
void*data; |
| 411 |
lua_State*L; |
| 412 |
}; |
| 413 |
static int luaZ_fill(ZIO*z); |
| 414 |
struct lua_longjmp; |
| 415 |
#define gt(L)(&L->l_gt) |
| 416 |
#define registry(L)(&G(L)->l_registry) |
| 417 |
typedef struct stringtable{ |
| 418 |
GCObject**hash; |
| 419 |
lu_int32 nuse; |
| 420 |
int size; |
| 421 |
}stringtable; |
| 422 |
typedef struct CallInfo{ |
| 423 |
StkId base; |
| 424 |
StkId func; |
| 425 |
StkId top; |
| 426 |
const Instruction*savedpc; |
| 427 |
int nresults; |
| 428 |
int tailcalls; |
| 429 |
}CallInfo; |
| 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{ |
| 435 |
stringtable strt; |
| 436 |
lua_Alloc frealloc; |
| 437 |
void*ud; |
| 438 |
lu_byte currentwhite; |
| 439 |
lu_byte gcstate; |
| 440 |
int sweepstrgc; |
| 441 |
GCObject*rootgc; |
| 442 |
GCObject**sweepgc; |
| 443 |
GCObject*gray; |
| 444 |
GCObject*grayagain; |
| 445 |
GCObject*weak; |
| 446 |
GCObject*tmudata; |
| 447 |
Mbuffer buff; |
| 448 |
lu_mem GCthreshold; |
| 449 |
lu_mem totalbytes; |
| 450 |
lu_mem estimate; |
| 451 |
lu_mem gcdept; |
| 452 |
int gcpause; |
| 453 |
int gcstepmul; |
| 454 |
lua_CFunction panic; |
| 455 |
TValue l_registry; |
| 456 |
struct lua_State*mainthread; |
| 457 |
UpVal uvhead; |
| 458 |
struct Table*mt[(8+1)]; |
| 459 |
TString*tmname[TM_N]; |
| 460 |
}global_State; |
| 461 |
struct lua_State{ |
| 462 |
GCObject*next;lu_byte tt;lu_byte marked; |
| 463 |
lu_byte status; |
| 464 |
StkId top; |
| 465 |
StkId base; |
| 466 |
global_State*l_G; |
| 467 |
CallInfo*ci; |
| 468 |
const Instruction*savedpc; |
| 469 |
StkId stack_last; |
| 470 |
StkId stack; |
| 471 |
CallInfo*end_ci; |
| 472 |
CallInfo*base_ci; |
| 473 |
int stacksize; |
| 474 |
int size_ci; |
| 475 |
unsigned short nCcalls; |
| 476 |
unsigned short baseCcalls; |
| 477 |
lu_byte hookmask; |
| 478 |
lu_byte allowhook; |
| 479 |
int basehookcount; |
| 480 |
int hookcount; |
| 481 |
lua_Hook hook; |
| 482 |
TValue l_gt; |
| 483 |
TValue env; |
| 484 |
GCObject*openupval; |
| 485 |
GCObject*gclist; |
| 486 |
struct lua_longjmp*errorJmp; |
| 487 |
ptrdiff_t errfunc; |
| 488 |
}; |
| 489 |
#define G(L)(L->l_G) |
| 490 |
union GCObject{ |
| 491 |
GCheader gch; |
| 492 |
union TString ts; |
| 493 |
union Udata u; |
| 494 |
union Closure cl; |
| 495 |
struct Table h; |
| 496 |
struct Proto p; |
| 497 |
struct UpVal uv; |
| 498 |
struct lua_State th; |
| 499 |
}; |
| 500 |
#define rawgco2ts(o)check_exp((o)->gch.tt==4,&((o)->ts)) |
| 501 |
#define gco2ts(o)(&rawgco2ts(o)->tsv) |
| 502 |
#define rawgco2u(o)check_exp((o)->gch.tt==7,&((o)->u)) |
| 503 |
#define gco2u(o)(&rawgco2u(o)->uv) |
| 504 |
#define gco2cl(o)check_exp((o)->gch.tt==6,&((o)->cl)) |
| 505 |
#define gco2h(o)check_exp((o)->gch.tt==5,&((o)->h)) |
| 506 |
#define gco2p(o)check_exp((o)->gch.tt==(8+1),&((o)->p)) |
| 507 |
#define gco2uv(o)check_exp((o)->gch.tt==(8+2),&((o)->uv)) |
| 508 |
#define ngcotouv(o)check_exp((o)==NULL||(o)->gch.tt==(8+2),&((o)->uv)) |
| 509 |
#define gco2th(o)check_exp((o)->gch.tt==8,&((o)->th)) |
| 510 |
#define obj2gco(v)(cast(GCObject*,(v))) |
| 511 |
static void luaE_freethread(lua_State*L,lua_State*L1); |
| 512 |
#define pcRel(pc,p)(cast(int,(pc)-(p)->code)-1) |
| 513 |
#define getline_(f,pc)(((f)->lineinfo)?(f)->lineinfo[pc]:0) |
| 514 |
#define resethookcount(L)(L->hookcount=L->basehookcount) |
| 515 |
static void luaG_typeerror(lua_State*L,const TValue*o, |
| 516 |
const char*opname); |
| 517 |
static void luaG_runerror(lua_State*L,const char*fmt,...); |
| 518 |
#define luaD_checkstack(L,n)if((char*)L->stack_last-(char*)L->top<=(n)*(int)sizeof(TValue))luaD_growstack(L,n);else condhardstacktests(luaD_reallocstack(L,L->stacksize-5-1)); |
| 519 |
#define incr_top(L){luaD_checkstack(L,1);L->top++;} |
| 520 |
#define savestack(L,p)((char*)(p)-(char*)L->stack) |
| 521 |
#define restorestack(L,n)((TValue*)((char*)L->stack+(n))) |
| 522 |
#define saveci(L,p)((char*)(p)-(char*)L->base_ci) |
| 523 |
#define restoreci(L,n)((CallInfo*)((char*)L->base_ci+(n))) |
| 524 |
typedef void(*Pfunc)(lua_State*L,void*ud); |
| 525 |
static int luaD_poscall(lua_State*L,StkId firstResult); |
| 526 |
static void luaD_reallocCI(lua_State*L,int newsize); |
| 527 |
static void luaD_reallocstack(lua_State*L,int newsize); |
| 528 |
static void luaD_growstack(lua_State*L,int n); |
| 529 |
static void luaD_throw(lua_State*L,int errcode); |
| 530 |
static void*luaM_growaux_(lua_State*L,void*block,int*size,size_t size_elems, |
| 531 |
int limit,const char*errormsg){ |
| 532 |
void*newblock; |
| 533 |
int newsize; |
| 534 |
if(*size>=limit/2){ |
| 535 |
if(*size>=limit) |
| 536 |
luaG_runerror(L,errormsg); |
| 537 |
newsize=limit; |
| 538 |
} |
| 539 |
else{ |
| 540 |
newsize=(*size)*2; |
| 541 |
if(newsize<4) |
| 542 |
newsize=4; |
| 543 |
} |
| 544 |
newblock=luaM_reallocv(L,block,*size,newsize,size_elems); |
| 545 |
*size=newsize; |
| 546 |
return newblock; |
| 547 |
} |
| 548 |
static void*luaM_toobig(lua_State*L){ |
| 549 |
luaG_runerror(L,"memory allocation error: block too big"); |
| 550 |
return NULL; |
| 551 |
} |
| 552 |
static void*luaM_realloc_(lua_State*L,void*block,size_t osize,size_t nsize){ |
| 553 |
global_State*g=G(L); |
| 554 |
block=(*g->frealloc)(g->ud,block,osize,nsize); |
| 555 |
if(block==NULL&&nsize>0) |
| 556 |
luaD_throw(L,4); |
| 557 |
g->totalbytes=(g->totalbytes-osize)+nsize; |
| 558 |
return block; |
| 559 |
} |
| 560 |
#define resetbits(x,m)((x)&=cast(lu_byte,~(m))) |
| 561 |
#define setbits(x,m)((x)|=(m)) |
| 562 |
#define testbits(x,m)((x)&(m)) |
| 563 |
#define bitmask(b)(1<<(b)) |
| 564 |
#define bit2mask(b1,b2)(bitmask(b1)|bitmask(b2)) |
| 565 |
#define l_setbit(x,b)setbits(x,bitmask(b)) |
| 566 |
#define resetbit(x,b)resetbits(x,bitmask(b)) |
| 567 |
#define testbit(x,b)testbits(x,bitmask(b)) |
| 568 |
#define set2bits(x,b1,b2)setbits(x,(bit2mask(b1,b2))) |
| 569 |
#define reset2bits(x,b1,b2)resetbits(x,(bit2mask(b1,b2))) |
| 570 |
#define test2bits(x,b1,b2)testbits(x,(bit2mask(b1,b2))) |
| 571 |
#define iswhite(x)test2bits((x)->gch.marked,0,1) |
| 572 |
#define isblack(x)testbit((x)->gch.marked,2) |
| 573 |
#define isgray(x)(!isblack(x)&&!iswhite(x)) |
| 574 |
#define otherwhite(g)(g->currentwhite^bit2mask(0,1)) |
| 575 |
#define isdead(g,v)((v)->gch.marked&otherwhite(g)&bit2mask(0,1)) |
| 576 |
#define changewhite(x)((x)->gch.marked^=bit2mask(0,1)) |
| 577 |
#define gray2black(x)l_setbit((x)->gch.marked,2) |
| 578 |
#define valiswhite(x)(iscollectable(x)&&iswhite(gcvalue(x))) |
| 579 |
#define luaC_white(g)cast(lu_byte,(g)->currentwhite&bit2mask(0,1)) |
| 580 |
#define luaC_checkGC(L){condhardstacktests(luaD_reallocstack(L,L->stacksize-5-1));if(G(L)->totalbytes>=G(L)->GCthreshold)luaC_step(L);} |
| 581 |
#define luaC_barrier(L,p,v){if(valiswhite(v)&&isblack(obj2gco(p)))luaC_barrierf(L,obj2gco(p),gcvalue(v));} |
| 582 |
#define luaC_barriert(L,t,v){if(valiswhite(v)&&isblack(obj2gco(t)))luaC_barrierback(L,t);} |
| 583 |
#define luaC_objbarrier(L,p,o){if(iswhite(obj2gco(o))&&isblack(obj2gco(p)))luaC_barrierf(L,obj2gco(p),obj2gco(o));} |
| 584 |
#define luaC_objbarriert(L,t,o){if(iswhite(obj2gco(o))&&isblack(obj2gco(t)))luaC_barrierback(L,t);} |
| 585 |
static void luaC_step(lua_State*L); |
| 586 |
static void luaC_link(lua_State*L,GCObject*o,lu_byte tt); |
| 587 |
static void luaC_linkupval(lua_State*L,UpVal*uv); |
| 588 |
static void luaC_barrierf(lua_State*L,GCObject*o,GCObject*v); |
| 589 |
static void luaC_barrierback(lua_State*L,Table*t); |
| 590 |
#define sizestring(s)(sizeof(union TString)+((s)->len+1)*sizeof(char)) |
| 591 |
#define sizeudata(u)(sizeof(union Udata)+(u)->len) |
| 592 |
#define luaS_new(L,s)(luaS_newlstr(L,s,strlen(s))) |
| 593 |
#define luaS_newliteral(L,s)(luaS_newlstr(L,""s,(sizeof(s)/sizeof(char))-1)) |
| 594 |
#define luaS_fix(s)l_setbit((s)->tsv.marked,5) |
| 595 |
static TString*luaS_newlstr(lua_State*L,const char*str,size_t l); |
| 596 |
#define tostring(L,o)((ttype(o)==4)||(luaV_tostring(L,o))) |
| 597 |
#define tonumber(o,n)(ttype(o)==3||(((o)=luaV_tonumber(o,n))!=NULL)) |
| 598 |
#define equalobj(L,o1,o2)(ttype(o1)==ttype(o2)&&luaV_equalval(L,o1,o2)) |
| 599 |
static int luaV_equalval(lua_State*L,const TValue*t1,const TValue*t2); |
| 600 |
static const TValue*luaV_tonumber(const TValue*obj,TValue*n); |
| 601 |
static int luaV_tostring(lua_State*L,StkId obj); |
| 602 |
static void luaV_execute(lua_State*L,int nexeccalls); |
| 603 |
static void luaV_concat(lua_State*L,int total,int last); |
| 604 |
static const TValue luaO_nilobject_={{NULL},0}; |
| 605 |
static int luaO_int2fb(unsigned int x){ |
| 606 |
int e=0; |
| 607 |
while(x>=16){ |
| 608 |
x=(x+1)>>1; |
| 609 |
e++; |
| 610 |
} |
| 611 |
if(x<8)return x; |
| 612 |
else return((e+1)<<3)|(cast_int(x)-8); |
| 613 |
} |
| 614 |
static int luaO_fb2int(int x){ |
| 615 |
int e=(x>>3)&31; |
| 616 |
if(e==0)return x; |
| 617 |
else return((x&7)+8)<<(e-1); |
| 618 |
} |
| 619 |
static int luaO_log2(unsigned int x){ |
| 620 |
static const lu_byte log_2[256]={ |
| 621 |
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, |
| 622 |
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, |
| 623 |
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, |
| 624 |
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, |
| 625 |
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, |
| 626 |
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, |
| 627 |
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, |
| 628 |
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 |
| 629 |
}; |
| 630 |
int l=-1; |
| 631 |
while(x>=256){l+=8;x>>=8;} |
| 632 |
return l+log_2[x]; |
| 633 |
} |
| 634 |
static int luaO_rawequalObj(const TValue*t1,const TValue*t2){ |
| 635 |
if(ttype(t1)!=ttype(t2))return 0; |
| 636 |
else switch(ttype(t1)){ |
| 637 |
case 0: |
| 638 |
return 1; |
| 639 |
case 3: |
| 640 |
return luai_numeq(nvalue(t1),nvalue(t2)); |
| 641 |
case 1: |
| 642 |
return bvalue(t1)==bvalue(t2); |
| 643 |
case 2: |
| 644 |
return pvalue(t1)==pvalue(t2); |
| 645 |
default: |
| 646 |
return gcvalue(t1)==gcvalue(t2); |
| 647 |
} |
| 648 |
} |
| 649 |
static int luaO_str2d(const char*s,lua_Number*result){ |
| 650 |
char*endptr; |
| 651 |
*result=lua_str2number(s,&endptr); |
| 652 |
if(endptr==s)return 0; |
| 653 |
if(*endptr=='x'||*endptr=='X') |
| 654 |
*result=cast_num(strtoul(s,&endptr,16)); |
| 655 |
if(*endptr=='\0')return 1; |
| 656 |
while(isspace(cast(unsigned char,*endptr)))endptr++; |
| 657 |
if(*endptr!='\0')return 0; |
| 658 |
return 1; |
| 659 |
} |
| 660 |
static void pushstr(lua_State*L,const char*str){ |
| 661 |
setsvalue(L,L->top,luaS_new(L,str)); |
| 662 |
incr_top(L); |
| 663 |
} |
| 664 |
static const char*luaO_pushvfstring(lua_State*L,const char*fmt,va_list argp){ |
| 665 |
int n=1; |
| 666 |
pushstr(L,""); |
| 667 |
for(;;){ |
| 668 |
const char*e=strchr(fmt,'%'); |
| 669 |
if(e==NULL)break; |
| 670 |
setsvalue(L,L->top,luaS_newlstr(L,fmt,e-fmt)); |
| 671 |
incr_top(L); |
| 672 |
switch(*(e+1)){ |
| 673 |
case's':{ |
| 674 |
const char*s=va_arg(argp,char*); |
| 675 |
if(s==NULL)s="(null)"; |
| 676 |
pushstr(L,s); |
| 677 |
break; |
| 678 |
} |
| 679 |
case'c':{ |
| 680 |
char buff[2]; |
| 681 |
buff[0]=cast(char,va_arg(argp,int)); |
| 682 |
buff[1]='\0'; |
| 683 |
pushstr(L,buff); |
| 684 |
break; |
| 685 |
} |
| 686 |
case'd':{ |
| 687 |
setnvalue(L->top,cast_num(va_arg(argp,int))); |
| 688 |
incr_top(L); |
| 689 |
break; |
| 690 |
} |
| 691 |
case'f':{ |
| 692 |
setnvalue(L->top,cast_num(va_arg(argp,l_uacNumber))); |
| 693 |
incr_top(L); |
| 694 |
break; |
| 695 |
} |
| 696 |
case'p':{ |
| 697 |
char buff[4*sizeof(void*)+8]; |
| 698 |
sprintf(buff,"%p",va_arg(argp,void*)); |
| 699 |
pushstr(L,buff); |
| 700 |
break; |
| 701 |
} |
| 702 |
case'%':{ |
| 703 |
pushstr(L,"%"); |
| 704 |
break; |
| 705 |
} |
| 706 |
default:{ |
| 707 |
char buff[3]; |
| 708 |
buff[0]='%'; |
| 709 |
buff[1]=*(e+1); |
| 710 |
buff[2]='\0'; |
| 711 |
pushstr(L,buff); |
| 712 |
break; |
| 713 |
} |
| 714 |
} |
| 715 |
n+=2; |
| 716 |
fmt=e+2; |
| 717 |
} |
| 718 |
pushstr(L,fmt); |
| 719 |
luaV_concat(L,n+1,cast_int(L->top-L->base)-1); |
| 720 |
L->top-=n; |
| 721 |
return svalue(L->top-1); |
| 722 |
} |
| 723 |
static const char*luaO_pushfstring(lua_State*L,const char*fmt,...){ |
| 724 |
const char*msg; |
| 725 |
va_list argp; |
| 726 |
va_start(argp,fmt); |
| 727 |
msg=luaO_pushvfstring(L,fmt,argp); |
| 728 |
va_end(argp); |
| 729 |
return msg; |
| 730 |
} |
| 731 |
static void luaO_chunkid(char*out,const char*source,size_t bufflen){ |
| 732 |
if(*source=='='){ |
| 733 |
strncpy(out,source+1,bufflen); |
| 734 |
out[bufflen-1]='\0'; |
| 735 |
} |
| 736 |
else{ |
| 737 |
if(*source=='@'){ |
| 738 |
size_t l; |
| 739 |
source++; |
| 740 |
bufflen-=sizeof(" '...' "); |
| 741 |
l=strlen(source); |
| 742 |
strcpy(out,""); |
| 743 |
if(l>bufflen){ |
| 744 |
source+=(l-bufflen); |
| 745 |
strcat(out,"..."); |
| 746 |
} |
| 747 |
strcat(out,source); |
| 748 |
} |
| 749 |
else{ |
| 750 |
size_t len=strcspn(source,"\n\r"); |
| 751 |
bufflen-=sizeof(" [string \"...\"] "); |
| 752 |
if(len>bufflen)len=bufflen; |
| 753 |
strcpy(out,"[string \""); |
| 754 |
if(source[len]!='\0'){ |
| 755 |
strncat(out,source,len); |
| 756 |
strcat(out,"..."); |
| 757 |
} |
| 758 |
else |
| 759 |
strcat(out,source); |
| 760 |
strcat(out,"\"]"); |
| 761 |
} |
| 762 |
} |
| 763 |
} |
| 764 |
#define gnode(t,i)(&(t)->node[i]) |
| 765 |
#define gkey(n)(&(n)->i_key.nk) |
| 766 |
#define gval(n)(&(n)->i_val) |
| 767 |
#define gnext(n)((n)->i_key.nk.next) |
| 768 |
#define key2tval(n)(&(n)->i_key.tvk) |
| 769 |
static TValue*luaH_setnum(lua_State*L,Table*t,int key); |
| 770 |
static const TValue*luaH_getstr(Table*t,TString*key); |
| 771 |
static TValue*luaH_set(lua_State*L,Table*t,const TValue*key); |
| 772 |
static const char*const luaT_typenames[]={ |
| 773 |
"nil","boolean","userdata","number", |
| 774 |
"string","table","function","userdata","thread", |
| 775 |
"proto","upval" |
| 776 |
}; |
| 777 |
static void luaT_init(lua_State*L){ |
| 778 |
static const char*const luaT_eventname[]={ |
| 779 |
"__index","__newindex", |
| 780 |
"__gc","__mode","__eq", |
| 781 |
"__add","__sub","__mul","__div","__mod", |
| 782 |
"__pow","__unm","__len","__lt","__le", |
| 783 |
"__concat","__call" |
| 784 |
}; |
| 785 |
int i; |
| 786 |
for(i=0;i<TM_N;i++){ |
| 787 |
G(L)->tmname[i]=luaS_new(L,luaT_eventname[i]); |
| 788 |
luaS_fix(G(L)->tmname[i]); |
| 789 |
} |
| 790 |
} |
| 791 |
static const TValue*luaT_gettm(Table*events,TMS event,TString*ename){ |
| 792 |
const TValue*tm=luaH_getstr(events,ename); |
| 793 |
if(ttisnil(tm)){ |
| 794 |
events->flags|=cast_byte(1u<<event); |
| 795 |
return NULL; |
| 796 |
} |
| 797 |
else return tm; |
| 798 |
} |
| 799 |
static const TValue*luaT_gettmbyobj(lua_State*L,const TValue*o,TMS event){ |
| 800 |
Table*mt; |
| 801 |
switch(ttype(o)){ |
| 802 |
case 5: |
| 803 |
mt=hvalue(o)->metatable; |
| 804 |
break; |
| 805 |
case 7: |
| 806 |
mt=uvalue(o)->metatable; |
| 807 |
break; |
| 808 |
default: |
| 809 |
mt=G(L)->mt[ttype(o)]; |
| 810 |
} |
| 811 |
return(mt?luaH_getstr(mt,G(L)->tmname[event]):(&luaO_nilobject_)); |
| 812 |
} |
| 813 |
#define sizeCclosure(n)(cast(int,sizeof(CClosure))+cast(int,sizeof(TValue)*((n)-1))) |
| 814 |
#define sizeLclosure(n)(cast(int,sizeof(LClosure))+cast(int,sizeof(TValue*)*((n)-1))) |
| 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); |
| 818 |
c->c.isC=1; |
| 819 |
c->c.env=e; |
| 820 |
c->c.nupvalues=cast_byte(nelems); |
| 821 |
return c; |
| 822 |
} |
| 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); |
| 826 |
c->l.isC=0; |
| 827 |
c->l.env=e; |
| 828 |
c->l.nupvalues=cast_byte(nelems); |
| 829 |
while(nelems--)c->l.upvals[nelems]=NULL; |
| 830 |
return c; |
| 831 |
} |
| 832 |
static UpVal*luaF_newupval(lua_State*L){ |
| 833 |
UpVal*uv=luaM_new(L,UpVal); |
| 834 |
luaC_link(L,obj2gco(uv),(8+2)); |
| 835 |
uv->v=&uv->u.value; |
| 836 |
setnilvalue(uv->v); |
| 837 |
return uv; |
| 838 |
} |
| 839 |
static UpVal*luaF_findupval(lua_State*L,StkId level){ |
| 840 |
global_State*g=G(L); |
| 841 |
GCObject**pp=&L->openupval; |
| 842 |
UpVal*p; |
| 843 |
UpVal*uv; |
| 844 |
while(*pp!=NULL&&(p=ngcotouv(*pp))->v>=level){ |
| 845 |
if(p->v==level){ |
| 846 |
if(isdead(g,obj2gco(p))) |
| 847 |
changewhite(obj2gco(p)); |
| 848 |
return p; |
| 849 |
} |
| 850 |
pp=&p->next; |
| 851 |
} |
| 852 |
uv=luaM_new(L,UpVal); |
| 853 |
uv->tt=(8+2); |
| 854 |
uv->marked=luaC_white(g); |
| 855 |
uv->v=level; |
| 856 |
uv->next=*pp; |
| 857 |
*pp=obj2gco(uv); |
| 858 |
uv->u.l.prev=&g->uvhead; |
| 859 |
uv->u.l.next=g->uvhead.u.l.next; |
| 860 |
uv->u.l.next->u.l.prev=uv; |
| 861 |
g->uvhead.u.l.next=uv; |
| 862 |
return uv; |
| 863 |
} |
| 864 |
static void unlinkupval(UpVal*uv){ |
| 865 |
uv->u.l.next->u.l.prev=uv->u.l.prev; |
| 866 |
uv->u.l.prev->u.l.next=uv->u.l.next; |
| 867 |
} |
| 868 |
static void luaF_freeupval(lua_State*L,UpVal*uv){ |
| 869 |
if(uv->v!=&uv->u.value) |
| 870 |
unlinkupval(uv); |
| 871 |
luaM_free(L,uv); |
| 872 |
} |
| 873 |
static void luaF_close(lua_State*L,StkId level){ |
| 874 |
UpVal*uv; |
| 875 |
global_State*g=G(L); |
| 876 |
while(L->openupval!=NULL&&(uv=ngcotouv(L->openupval))->v>=level){ |
| 877 |
GCObject*o=obj2gco(uv); |
| 878 |
L->openupval=uv->next; |
| 879 |
if(isdead(g,o)) |
| 880 |
luaF_freeupval(L,uv); |
| 881 |
else{ |
| 882 |
unlinkupval(uv); |
| 883 |
setobj(L,&uv->u.value,uv->v); |
| 884 |
uv->v=&uv->u.value; |
| 885 |
luaC_linkupval(L,uv); |
| 886 |
} |
| 887 |
} |
| 888 |
} |
| 889 |
static Proto*luaF_newproto(lua_State*L){ |
| 890 |
Proto*f=luaM_new(L,Proto); |
| 891 |
luaC_link(L,obj2gco(f),(8+1)); |
| 892 |
f->k=NULL; |
| 893 |
f->sizek=0; |
| 894 |
f->p=NULL; |
| 895 |
f->sizep=0; |
| 896 |
f->code=NULL; |
| 897 |
f->sizecode=0; |
| 898 |
f->sizelineinfo=0; |
| 899 |
f->sizeupvalues=0; |
| 900 |
f->nups=0; |
| 901 |
f->upvalues=NULL; |
| 902 |
f->numparams=0; |
| 903 |
f->is_vararg=0; |
| 904 |
f->maxstacksize=0; |
| 905 |
f->lineinfo=NULL; |
| 906 |
f->sizelocvars=0; |
| 907 |
f->locvars=NULL; |
| 908 |
f->linedefined=0; |
| 909 |
f->lastlinedefined=0; |
| 910 |
f->source=NULL; |
| 911 |
return f; |
| 912 |
} |
| 913 |
static void luaF_freeproto(lua_State*L,Proto*f){ |
| 914 |
luaM_freearray(L,f->code,f->sizecode,Instruction); |
| 915 |
luaM_freearray(L,f->p,f->sizep,Proto*); |
| 916 |
luaM_freearray(L,f->k,f->sizek,TValue); |
| 917 |
luaM_freearray(L,f->lineinfo,f->sizelineinfo,int); |
| 918 |
luaM_freearray(L,f->locvars,f->sizelocvars,struct LocVar); |
| 919 |
luaM_freearray(L,f->upvalues,f->sizeupvalues,TString*); |
| 920 |
luaM_free(L,f); |
| 921 |
} |
| 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); |
| 926 |
} |
| 927 |
#define MASK1(n,p)((~((~(Instruction)0)<<n))<<p) |
| 928 |
#define MASK0(n,p)(~MASK1(n,p)) |
| 929 |
#define GET_OPCODE(i)(cast(OpCode,((i)>>0)&MASK1(6,0))) |
| 930 |
#define SET_OPCODE(i,o)((i)=(((i)&MASK0(6,0))|((cast(Instruction,o)<<0)&MASK1(6,0)))) |
| 931 |
#define GETARG_A(i)(cast(int,((i)>>(0+6))&MASK1(8,0))) |
| 932 |
#define SETARG_A(i,u)((i)=(((i)&MASK0(8,(0+6)))|((cast(Instruction,u)<<(0+6))&MASK1(8,(0+6))))) |
| 933 |
#define GETARG_B(i)(cast(int,((i)>>(((0+6)+8)+9))&MASK1(9,0))) |
| 934 |
#define SETARG_B(i,b)((i)=(((i)&MASK0(9,(((0+6)+8)+9)))|((cast(Instruction,b)<<(((0+6)+8)+9))&MASK1(9,(((0+6)+8)+9))))) |
| 935 |
#define GETARG_C(i)(cast(int,((i)>>((0+6)+8))&MASK1(9,0))) |
| 936 |
#define SETARG_C(i,b)((i)=(((i)&MASK0(9,((0+6)+8)))|((cast(Instruction,b)<<((0+6)+8))&MASK1(9,((0+6)+8))))) |
| 937 |
#define GETARG_Bx(i)(cast(int,((i)>>((0+6)+8))&MASK1((9+9),0))) |
| 938 |
#define SETARG_Bx(i,b)((i)=(((i)&MASK0((9+9),((0+6)+8)))|((cast(Instruction,b)<<((0+6)+8))&MASK1((9+9),((0+6)+8))))) |
| 939 |
#define GETARG_sBx(i)(GETARG_Bx(i)-(((1<<(9+9))-1)>>1)) |
| 940 |
#define SETARG_sBx(i,b)SETARG_Bx((i),cast(unsigned int,(b)+(((1<<(9+9))-1)>>1))) |
| 941 |
#define CREATE_ABC(o,a,b,c)((cast(Instruction,o)<<0)|(cast(Instruction,a)<<(0+6))|(cast(Instruction,b)<<(((0+6)+8)+9))|(cast(Instruction,c)<<((0+6)+8))) |
| 942 |
#define CREATE_ABx(o,a,bc)((cast(Instruction,o)<<0)|(cast(Instruction,a)<<(0+6))|(cast(Instruction,bc)<<((0+6)+8))) |
| 943 |
#define ISK(x)((x)&(1<<(9-1))) |
| 944 |
#define INDEXK(r)((int)(r)&~(1<<(9-1))) |
| 945 |
#define RKASK(x)((x)|(1<<(9-1))) |
| 946 |
static const lu_byte luaP_opmodes[(cast(int,OP_VARARG)+1)]; |
| 947 |
#define getBMode(m)(cast(enum OpArgMask,(luaP_opmodes[m]>>4)&3)) |
| 948 |
#define getCMode(m)(cast(enum OpArgMask,(luaP_opmodes[m]>>2)&3)) |
| 949 |
#define testTMode(m)(luaP_opmodes[m]&(1<<7)) |
| 950 |
typedef struct expdesc{ |
| 951 |
expkind k; |
| 952 |
union{ |
| 953 |
struct{int info,aux;}s; |
| 954 |
lua_Number nval; |
| 955 |
}u; |
| 956 |
int t; |
| 957 |
int f; |
| 958 |
}expdesc; |
| 959 |
typedef struct upvaldesc{ |
| 960 |
lu_byte k; |
| 961 |
lu_byte info; |
| 962 |
}upvaldesc; |
| 963 |
struct BlockCnt; |
| 964 |
typedef struct FuncState{ |
| 965 |
Proto*f; |
| 966 |
Table*h; |
| 967 |
struct FuncState*prev; |
| 968 |
struct LexState*ls; |
| 969 |
struct lua_State*L; |
| 970 |
struct BlockCnt*bl; |
| 971 |
int pc; |
| 972 |
int lasttarget; |
| 973 |
int jpc; |
| 974 |
int freereg; |
| 975 |
int nk; |
| 976 |
int np; |
| 977 |
short nlocvars; |
| 978 |
lu_byte nactvar; |
| 979 |
upvaldesc upvalues[60]; |
| 980 |
unsigned short actvar[200]; |
| 981 |
}FuncState; |
| 982 |
static Proto*luaY_parser(lua_State*L,ZIO*z,Mbuffer*buff, |
| 983 |
const char*name); |
| 984 |
struct lua_longjmp{ |
| 985 |
struct lua_longjmp*previous; |
| 986 |
jmp_buf b; |
| 987 |
volatile int status; |
| 988 |
}; |
| 989 |
static void luaD_seterrorobj(lua_State*L,int errcode,StkId oldtop){ |
| 990 |
switch(errcode){ |
| 991 |
case 4:{ |
| 992 |
setsvalue(L,oldtop,luaS_newliteral(L,"not enough memory")); |
| 993 |
break; |
| 994 |
} |
| 995 |
case 5:{ |
| 996 |
setsvalue(L,oldtop,luaS_newliteral(L,"error in error handling")); |
| 997 |
break; |
| 998 |
} |
| 999 |
case 3: |
| 1000 |
case 2:{ |
| 1001 |
setobj(L,oldtop,L->top-1); |
| 1002 |
break; |
| 1003 |
} |
| 1004 |
} |
| 1005 |
L->top=oldtop+1; |
| 1006 |
} |
| 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); |
| 1010 |
if(inuse+1<20000) |
| 1011 |
luaD_reallocCI(L,20000); |
| 1012 |
} |
| 1013 |
} |
| 1014 |
static void resetstack(lua_State*L,int status){ |
| 1015 |
L->ci=L->base_ci; |
| 1016 |
L->base=L->ci->base; |
| 1017 |
luaF_close(L,L->base); |
| 1018 |
luaD_seterrorobj(L,status,L->base); |
| 1019 |
L->nCcalls=L->baseCcalls; |
| 1020 |
L->allowhook=1; |
| 1021 |
restore_stack_limit(L); |
| 1022 |
L->errfunc=0; |
| 1023 |
L->errorJmp=NULL; |
| 1024 |
} |
| 1025 |
static void luaD_throw(lua_State*L,int errcode){ |
| 1026 |
if(L->errorJmp){ |
| 1027 |
L->errorJmp->status=errcode; |
| 1028 |
LUAI_THROW(L,L->errorJmp); |
| 1029 |
} |
| 1030 |
else{ |
| 1031 |
L->status=cast_byte(errcode); |
| 1032 |
if(G(L)->panic){ |
| 1033 |
resetstack(L,errcode); |
| 1034 |
G(L)->panic(L); |
| 1035 |
} |
| 1036 |
exit(EXIT_FAILURE); |
| 1037 |
} |
| 1038 |
} |
| 1039 |
static int luaD_rawrunprotected(lua_State*L,Pfunc f,void*ud){ |
| 1040 |
struct lua_longjmp lj; |
| 1041 |
lj.status=0; |
| 1042 |
lj.previous=L->errorJmp; |
| 1043 |
L->errorJmp=&lj; |
| 1044 |
LUAI_TRY(L,&lj, |
| 1045 |
(*f)(L,ud); |
| 1046 |
); |
| 1047 |
L->errorJmp=lj.previous; |
| 1048 |
return lj.status; |
| 1049 |
} |
| 1050 |
static void correctstack(lua_State*L,TValue*oldstack){ |
| 1051 |
CallInfo*ci; |
| 1052 |
GCObject*up; |
| 1053 |
L->top=(L->top-oldstack)+L->stack; |
| 1054 |
for(up=L->openupval;up!=NULL;up=up->gch.next) |
| 1055 |
gco2uv(up)->v=(gco2uv(up)->v-oldstack)+L->stack; |
| 1056 |
for(ci=L->base_ci;ci<=L->ci;ci++){ |
| 1057 |
ci->top=(ci->top-oldstack)+L->stack; |
| 1058 |
ci->base=(ci->base-oldstack)+L->stack; |
| 1059 |
ci->func=(ci->func-oldstack)+L->stack; |
| 1060 |
} |
| 1061 |
L->base=(L->base-oldstack)+L->stack; |
| 1062 |
} |
| 1063 |
static void luaD_reallocstack(lua_State*L,int newsize){ |
| 1064 |
TValue*oldstack=L->stack; |
| 1065 |
int realsize=newsize+1+5; |
| 1066 |
luaM_reallocvector(L,L->stack,L->stacksize,realsize,TValue); |
| 1067 |
L->stacksize=realsize; |
| 1068 |
L->stack_last=L->stack+newsize; |
| 1069 |
correctstack(L,oldstack); |
| 1070 |
} |
| 1071 |
static void luaD_reallocCI(lua_State*L,int newsize){ |
| 1072 |
CallInfo*oldci=L->base_ci; |
| 1073 |
luaM_reallocvector(L,L->base_ci,L->size_ci,newsize,CallInfo); |
| 1074 |
L->size_ci=newsize; |
| 1075 |
L->ci=(L->ci-oldci)+L->base_ci; |
| 1076 |
L->end_ci=L->base_ci+L->size_ci-1; |
| 1077 |
} |
| 1078 |
static void luaD_growstack(lua_State*L,int n){ |
| 1079 |
if(n<=L->stacksize) |
| 1080 |
luaD_reallocstack(L,2*L->stacksize); |
| 1081 |
else |
| 1082 |
luaD_reallocstack(L,L->stacksize+n); |
| 1083 |
} |
| 1084 |
static CallInfo*growCI(lua_State*L){ |
| 1085 |
if(L->size_ci>20000) |
| 1086 |
luaD_throw(L,5); |
| 1087 |
else{ |
| 1088 |
luaD_reallocCI(L,2*L->size_ci); |
| 1089 |
if(L->size_ci>20000) |
| 1090 |
luaG_runerror(L,"stack overflow"); |
| 1091 |
} |
| 1092 |
return++L->ci; |
| 1093 |
} |
| 1094 |
static StkId adjust_varargs(lua_State*L,Proto*p,int actual){ |
| 1095 |
int i; |
| 1096 |
int nfixargs=p->numparams; |
| 1097 |
Table*htab=NULL; |
| 1098 |
StkId base,fixed; |
| 1099 |
for(;actual<nfixargs;++actual) |
| 1100 |
setnilvalue(L->top++); |
| 1101 |
fixed=L->top-actual; |
| 1102 |
base=L->top; |
| 1103 |
for(i=0;i<nfixargs;i++){ |
| 1104 |
setobj(L,L->top++,fixed+i); |
| 1105 |
setnilvalue(fixed+i); |
| 1106 |
} |
| 1107 |
if(htab){ |
| 1108 |
sethvalue(L,L->top++,htab); |
| 1109 |
} |
| 1110 |
return base; |
| 1111 |
} |
| 1112 |
static StkId tryfuncTM(lua_State*L,StkId func){ |
| 1113 |
const TValue*tm=luaT_gettmbyobj(L,func,TM_CALL); |
| 1114 |
StkId p; |
| 1115 |
ptrdiff_t funcr=savestack(L,func); |
| 1116 |
if(!ttisfunction(tm)) |
| 1117 |
luaG_typeerror(L,func,"call"); |
| 1118 |
for(p=L->top;p>func;p--)setobj(L,p,p-1); |
| 1119 |
incr_top(L); |
| 1120 |
func=restorestack(L,funcr); |
| 1121 |
setobj(L,func,tm); |
| 1122 |
return func; |
| 1123 |
} |
| 1124 |
#define inc_ci(L)((L->ci==L->end_ci)?growCI(L):(condhardstacktests(luaD_reallocCI(L,L->size_ci)),++L->ci)) |
| 1125 |
static int luaD_precall(lua_State*L,StkId func,int nresults){ |
| 1126 |
LClosure*cl; |
| 1127 |
ptrdiff_t funcr; |
| 1128 |
if(!ttisfunction(func)) |
| 1129 |
func=tryfuncTM(L,func); |
| 1130 |
funcr=savestack(L,func); |
| 1131 |
cl=&clvalue(func)->l; |
| 1132 |
L->ci->savedpc=L->savedpc; |
| 1133 |
if(!cl->isC){ |
| 1134 |
CallInfo*ci; |
| 1135 |
StkId st,base; |
| 1136 |
Proto*p=cl->p; |
| 1137 |
luaD_checkstack(L,p->maxstacksize+p->numparams); |
| 1138 |
func=restorestack(L,funcr); |
| 1139 |
if(!p->is_vararg){ |
| 1140 |
base=func+1; |
| 1141 |
if(L->top>base+p->numparams) |
| 1142 |
L->top=base+p->numparams; |
| 1143 |
} |
| 1144 |
else{ |
| 1145 |
int nargs=cast_int(L->top-func)-1; |
| 1146 |
base=adjust_varargs(L,p,nargs); |
| 1147 |
func=restorestack(L,funcr); |
| 1148 |
} |
| 1149 |
ci=inc_ci(L); |
| 1150 |
ci->func=func; |
| 1151 |
L->base=ci->base=base; |
| 1152 |
ci->top=L->base+p->maxstacksize; |
| 1153 |
L->savedpc=p->code; |
| 1154 |
ci->tailcalls=0; |
| 1155 |
ci->nresults=nresults; |
| 1156 |
for(st=L->top;st<ci->top;st++) |
| 1157 |
setnilvalue(st); |
| 1158 |
L->top=ci->top; |
| 1159 |
return 0; |
| 1160 |
} |
| 1161 |
else{ |
| 1162 |
CallInfo*ci; |
| 1163 |
int n; |
| 1164 |
luaD_checkstack(L,20); |
| 1165 |
ci=inc_ci(L); |
| 1166 |
ci->func=restorestack(L,funcr); |
| 1167 |
L->base=ci->base=ci->func+1; |
| 1168 |
ci->top=L->top+20; |
| 1169 |
ci->nresults=nresults; |
| 1170 |
n=(*curr_func(L)->c.f)(L); |
| 1171 |
if(n<0) |
| 1172 |
return 2; |
| 1173 |
else{ |
| 1174 |
luaD_poscall(L,L->top-n); |
| 1175 |
return 1; |
| 1176 |
} |
| 1177 |
} |
| 1178 |
} |
| 1179 |
static int luaD_poscall(lua_State*L,StkId firstResult){ |
| 1180 |
StkId res; |
| 1181 |
int wanted,i; |
| 1182 |
CallInfo*ci; |
| 1183 |
ci=L->ci--; |
| 1184 |
res=ci->func; |
| 1185 |
wanted=ci->nresults; |
| 1186 |
L->base=(ci-1)->base; |
| 1187 |
L->savedpc=(ci-1)->savedpc; |
| 1188 |
for(i=wanted;i!=0&&firstResult<L->top;i--) |
| 1189 |
setobj(L,res++,firstResult++); |
| 1190 |
while(i-->0) |
| 1191 |
setnilvalue(res++); |
| 1192 |
L->top=res; |
| 1193 |
return(wanted-(-1)); |
| 1194 |
} |
| 1195 |
static void luaD_call(lua_State*L,StkId func,int nResults){ |
| 1196 |
if(++L->nCcalls>=200){ |
| 1197 |
if(L->nCcalls==200) |
| 1198 |
luaG_runerror(L,"C stack overflow"); |
| 1199 |
else if(L->nCcalls>=(200+(200>>3))) |
| 1200 |
luaD_throw(L,5); |
| 1201 |
} |
| 1202 |
if(luaD_precall(L,func,nResults)==0) |
| 1203 |
luaV_execute(L,1); |
| 1204 |
L->nCcalls--; |
| 1205 |
luaC_checkGC(L); |
| 1206 |
} |
| 1207 |
static int luaD_pcall(lua_State*L,Pfunc func,void*u, |
| 1208 |
ptrdiff_t old_top,ptrdiff_t ef){ |
| 1209 |
int status; |
| 1210 |
unsigned short oldnCcalls=L->nCcalls; |
| 1211 |
ptrdiff_t old_ci=saveci(L,L->ci); |
| 1212 |
lu_byte old_allowhooks=L->allowhook; |
| 1213 |
ptrdiff_t old_errfunc=L->errfunc; |
| 1214 |
L->errfunc=ef; |
| 1215 |
status=luaD_rawrunprotected(L,func,u); |
| 1216 |
if(status!=0){ |
| 1217 |
StkId oldtop=restorestack(L,old_top); |
| 1218 |
luaF_close(L,oldtop); |
| 1219 |
luaD_seterrorobj(L,status,oldtop); |
| 1220 |
L->nCcalls=oldnCcalls; |
| 1221 |
L->ci=restoreci(L,old_ci); |
| 1222 |
L->base=L->ci->base; |
| 1223 |
L->savedpc=L->ci->savedpc; |
| 1224 |
L->allowhook=old_allowhooks; |
| 1225 |
restore_stack_limit(L); |
| 1226 |
} |
| 1227 |
L->errfunc=old_errfunc; |
| 1228 |
return status; |
| 1229 |
} |
| 1230 |
struct SParser{ |
| 1231 |
ZIO*z; |
| 1232 |
Mbuffer buff; |
| 1233 |
const char*name; |
| 1234 |
}; |
| 1235 |
static void f_parser(lua_State*L,void*ud){ |
| 1236 |
int i; |
| 1237 |
Proto*tf; |
| 1238 |
Closure*cl; |
| 1239 |
struct SParser*p=cast(struct SParser*,ud); |
| 1240 |
luaC_checkGC(L); |
| 1241 |
tf=luaY_parser(L,p->z, |
| 1242 |
&p->buff,p->name); |
| 1243 |
cl=luaF_newLclosure(L,tf->nups,hvalue(gt(L))); |
| 1244 |
cl->l.p=tf; |
| 1245 |
for(i=0;i<tf->nups;i++) |
| 1246 |
cl->l.upvals[i]=luaF_newupval(L); |
| 1247 |
setclvalue(L,L->top,cl); |
| 1248 |
incr_top(L); |
| 1249 |
} |
| 1250 |
static int luaD_protectedparser(lua_State*L,ZIO*z,const char*name){ |
| 1251 |
struct SParser p; |
| 1252 |
int status; |
| 1253 |
p.z=z;p.name=name; |
| 1254 |
luaZ_initbuffer(L,&p.buff); |
| 1255 |
status=luaD_pcall(L,f_parser,&p,savestack(L,L->top),L->errfunc); |
| 1256 |
luaZ_freebuffer(L,&p.buff); |
| 1257 |
return status; |
| 1258 |
} |
| 1259 |
static void luaS_resize(lua_State*L,int newsize){ |
| 1260 |
GCObject**newhash; |
| 1261 |
stringtable*tb; |
| 1262 |
int i; |
| 1263 |
if(G(L)->gcstate==2) |
| 1264 |
return; |
| 1265 |
newhash=luaM_newvector(L,newsize,GCObject*); |
| 1266 |
tb=&G(L)->strt; |
| 1267 |
for(i=0;i<newsize;i++)newhash[i]=NULL; |
| 1268 |
for(i=0;i<tb->size;i++){ |
| 1269 |
GCObject*p=tb->hash[i]; |
| 1270 |
while(p){ |
| 1271 |
GCObject*next=p->gch.next; |
| 1272 |
unsigned int h=gco2ts(p)->hash; |
| 1273 |
int h1=lmod(h,newsize); |
| 1274 |
p->gch.next=newhash[h1]; |
| 1275 |
newhash[h1]=p; |
| 1276 |
p=next; |
| 1277 |
} |
| 1278 |
} |
| 1279 |
luaM_freearray(L,tb->hash,tb->size,TString*); |
| 1280 |
tb->size=newsize; |
| 1281 |
tb->hash=newhash; |
| 1282 |
} |
| 1283 |
static TString*newlstr(lua_State*L,const char*str,size_t l, |
| 1284 |
unsigned int h){ |
| 1285 |
TString*ts; |
| 1286 |
stringtable*tb; |
| 1287 |
if(l+1>(((size_t)(~(size_t)0)-2)-sizeof(TString))/sizeof(char)) |
| 1288 |
luaM_toobig(L); |
| 1289 |
ts=cast(TString*,luaM_malloc(L,(l+1)*sizeof(char)+sizeof(TString))); |
| 1290 |
ts->tsv.len=l; |
| 1291 |
ts->tsv.hash=h; |
| 1292 |
ts->tsv.marked=luaC_white(G(L)); |
| 1293 |
ts->tsv.tt=4; |
| 1294 |
ts->tsv.reserved=0; |
| 1295 |
memcpy(ts+1,str,l*sizeof(char)); |
| 1296 |
((char*)(ts+1))[l]='\0'; |
| 1297 |
tb=&G(L)->strt; |
| 1298 |
h=lmod(h,tb->size); |
| 1299 |
ts->tsv.next=tb->hash[h]; |
| 1300 |
tb->hash[h]=obj2gco(ts); |
| 1301 |
tb->nuse++; |
| 1302 |
if(tb->nuse>cast(lu_int32,tb->size)&&tb->size<=(INT_MAX-2)/2) |
| 1303 |
luaS_resize(L,tb->size*2); |
| 1304 |
return ts; |
| 1305 |
} |
| 1306 |
static TString*luaS_newlstr(lua_State*L,const char*str,size_t l){ |
| 1307 |
GCObject*o; |
| 1308 |
unsigned int h=cast(unsigned int,l); |
| 1309 |
size_t step=(l>>5)+1; |
| 1310 |
size_t l1; |
| 1311 |
for(l1=l;l1>=step;l1-=step) |
| 1312 |
h=h^((h<<5)+(h>>2)+cast(unsigned char,str[l1-1])); |
| 1313 |
for(o=G(L)->strt.hash[lmod(h,G(L)->strt.size)]; |
| 1314 |
o!=NULL; |
| 1315 |
o=o->gch.next){ |
| 1316 |
TString*ts=rawgco2ts(o); |
| 1317 |
if(ts->tsv.len==l&&(memcmp(str,getstr(ts),l)==0)){ |
| 1318 |
if(isdead(G(L),o))changewhite(o); |
| 1319 |
return ts; |
| 1320 |
} |
| 1321 |
} |
| 1322 |
return newlstr(L,str,l,h); |
| 1323 |
} |
| 1324 |
static Udata*luaS_newudata(lua_State*L,size_t s,Table*e){ |
| 1325 |
Udata*u; |
| 1326 |
if(s>((size_t)(~(size_t)0)-2)-sizeof(Udata)) |
| 1327 |
luaM_toobig(L); |
| 1328 |
u=cast(Udata*,luaM_malloc(L,s+sizeof(Udata))); |
| 1329 |
u->uv.marked=luaC_white(G(L)); |
| 1330 |
u->uv.tt=7; |
| 1331 |
u->uv.len=s; |
| 1332 |
u->uv.metatable=NULL; |
| 1333 |
u->uv.env=e; |
| 1334 |
u->uv.next=G(L)->mainthread->next; |
| 1335 |
G(L)->mainthread->next=obj2gco(u); |
| 1336 |
return u; |
| 1337 |
} |
| 1338 |
#define hashpow2(t,n)(gnode(t,lmod((n),sizenode(t)))) |
| 1339 |
#define hashstr(t,str)hashpow2(t,(str)->tsv.hash) |
| 1340 |
#define hashboolean(t,p)hashpow2(t,p) |
| 1341 |
#define hashmod(t,n)(gnode(t,((n)%((sizenode(t)-1)|1)))) |
| 1342 |
#define hashpointer(t,p)hashmod(t,IntPoint(p)) |
| 1343 |
static const Node dummynode_={ |
| 1344 |
{{NULL},0}, |
| 1345 |
{{{NULL},0,NULL}} |
| 1346 |
}; |
| 1347 |
static Node*hashnum(const Table*t,lua_Number n){ |
| 1348 |
unsigned int a[cast_int(sizeof(lua_Number)/sizeof(int))]; |
| 1349 |
int i; |
| 1350 |
if(luai_numeq(n,0)) |
| 1351 |
return gnode(t,0); |
| 1352 |
memcpy(a,&n,sizeof(a)); |
| 1353 |
for(i=1;i<cast_int(sizeof(lua_Number)/sizeof(int));i++)a[0]+=a[i]; |
| 1354 |
return hashmod(t,a[0]); |
| 1355 |
} |
| 1356 |
static Node*mainposition(const Table*t,const TValue*key){ |
| 1357 |
switch(ttype(key)){ |
| 1358 |
case 3: |
| 1359 |
return hashnum(t,nvalue(key)); |
| 1360 |
case 4: |
| 1361 |
return hashstr(t,rawtsvalue(key)); |
| 1362 |
case 1: |
| 1363 |
return hashboolean(t,bvalue(key)); |
| 1364 |
case 2: |
| 1365 |
return hashpointer(t,pvalue(key)); |
| 1366 |
default: |
| 1367 |
return hashpointer(t,gcvalue(key)); |
| 1368 |
} |
| 1369 |
} |
| 1370 |
static int arrayindex(const TValue*key){ |
| 1371 |
if(ttisnumber(key)){ |
| 1372 |
lua_Number n=nvalue(key); |
| 1373 |
int k; |
| 1374 |
lua_number2int(k,n); |
| 1375 |
if(luai_numeq(cast_num(k),n)) |
| 1376 |
return k; |
| 1377 |
} |
| 1378 |
return-1; |
| 1379 |
} |
| 1380 |
static int findindex(lua_State*L,Table*t,StkId key){ |
| 1381 |
int i; |
| 1382 |
if(ttisnil(key))return-1; |
| 1383 |
i=arrayindex(key); |
| 1384 |
if(0<i&&i<=t->sizearray) |
| 1385 |
return i-1; |
| 1386 |
else{ |
| 1387 |
Node*n=mainposition(t,key); |
| 1388 |
do{ |
| 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; |
| 1394 |
} |
| 1395 |
else n=gnext(n); |
| 1396 |
}while(n); |
| 1397 |
luaG_runerror(L,"invalid key to "LUA_QL("next")); |
| 1398 |
return 0; |
| 1399 |
} |
| 1400 |
} |
| 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]); |
| 1407 |
return 1; |
| 1408 |
} |
| 1409 |
} |
| 1410 |
for(i-=t->sizearray;i<(int)sizenode(t);i++){ |
| 1411 |
if(!ttisnil(gval(gnode(t,i)))){ |
| 1412 |
setobj(L,key,key2tval(gnode(t,i))); |
| 1413 |
setobj(L,key+1,gval(gnode(t,i))); |
| 1414 |
return 1; |
| 1415 |
} |
| 1416 |
} |
| 1417 |
return 0; |
| 1418 |
} |
| 1419 |
static int computesizes(int nums[],int*narray){ |
| 1420 |
int i; |
| 1421 |
int twotoi; |
| 1422 |
int a=0; |
| 1423 |
int na=0; |
| 1424 |
int n=0; |
| 1425 |
for(i=0,twotoi=1;twotoi/2<*narray;i++,twotoi*=2){ |
| 1426 |
if(nums[i]>0){ |
| 1427 |
a+=nums[i]; |
| 1428 |
if(a>twotoi/2){ |
| 1429 |
n=twotoi; |
| 1430 |
na=a; |
| 1431 |
} |
| 1432 |
} |
| 1433 |
if(a==*narray)break; |
| 1434 |
} |
| 1435 |
*narray=n; |
| 1436 |
return na; |
| 1437 |
} |
| 1438 |
static int countint(const TValue*key,int*nums){ |
| 1439 |
int k=arrayindex(key); |
| 1440 |
if(0<k&&k<=(1<<(32-2))){ |
| 1441 |
nums[ceillog2(k)]++; |
| 1442 |
return 1; |
| 1443 |
} |
| 1444 |
else |
| 1445 |
return 0; |
| 1446 |
} |
| 1447 |
static int numusearray(const Table*t,int*nums){ |
| 1448 |
int lg; |
| 1449 |
int ttlg; |
| 1450 |
int ause=0; |
| 1451 |
int i=1; |
| 1452 |
for(lg=0,ttlg=1;lg<=(32-2);lg++,ttlg*=2){ |
| 1453 |
int lc=0; |
| 1454 |
int lim=ttlg; |
| 1455 |
if(lim>t->sizearray){ |
| 1456 |
lim=t->sizearray; |
| 1457 |
if(i>lim) |
| 1458 |
break; |
| 1459 |
} |
| 1460 |
for(;i<=lim;i++){ |
| 1461 |
if(!ttisnil(&t->array[i-1])) |
| 1462 |
lc++; |
| 1463 |
} |
| 1464 |
nums[lg]+=lc; |
| 1465 |
ause+=lc; |
| 1466 |
} |
| 1467 |
return ause; |
| 1468 |
} |
| 1469 |
static int numusehash(const Table*t,int*nums,int*pnasize){ |
| 1470 |
int totaluse=0; |
| 1471 |
int ause=0; |
| 1472 |
int i=sizenode(t); |
| 1473 |
while(i--){ |
| 1474 |
Node*n=&t->node[i]; |
| 1475 |
if(!ttisnil(gval(n))){ |
| 1476 |
ause+=countint(key2tval(n),nums); |
| 1477 |
totaluse++; |
| 1478 |
} |
| 1479 |
} |
| 1480 |
*pnasize+=ause; |
| 1481 |
return totaluse; |
| 1482 |
} |
| 1483 |
static void setarrayvector(lua_State*L,Table*t,int size){ |
| 1484 |
int i; |
| 1485 |
luaM_reallocvector(L,t->array,t->sizearray,size,TValue); |
| 1486 |
for(i=t->sizearray;i<size;i++) |
| 1487 |
setnilvalue(&t->array[i]); |
| 1488 |
t->sizearray=size; |
| 1489 |
} |
| 1490 |
static void setnodevector(lua_State*L,Table*t,int size){ |
| 1491 |
int lsize; |
| 1492 |
if(size==0){ |
| 1493 |
t->node=cast(Node*,(&dummynode_)); |
| 1494 |
lsize=0; |
| 1495 |
} |
| 1496 |
else{ |
| 1497 |
int i; |
| 1498 |
lsize=ceillog2(size); |
| 1499 |
if(lsize>(32-2)) |
| 1500 |
luaG_runerror(L,"table overflow"); |
| 1501 |
size=twoto(lsize); |
| 1502 |
t->node=luaM_newvector(L,size,Node); |
| 1503 |
for(i=0;i<size;i++){ |
| 1504 |
Node*n=gnode(t,i); |
| 1505 |
gnext(n)=NULL; |
| 1506 |
setnilvalue(gkey(n)); |
| 1507 |
setnilvalue(gval(n)); |
| 1508 |
} |
| 1509 |
} |
| 1510 |
t->lsizenode=cast_byte(lsize); |
| 1511 |
t->lastfree=gnode(t,size); |
| 1512 |
} |
| 1513 |
static void resize(lua_State*L,Table*t,int nasize,int nhsize){ |
| 1514 |
int i; |
| 1515 |
int oldasize=t->sizearray; |
| 1516 |
int oldhsize=t->lsizenode; |
| 1517 |
Node*nold=t->node; |
| 1518 |
if(nasize>oldasize) |
| 1519 |
setarrayvector(L,t,nasize); |
| 1520 |
setnodevector(L,t,nhsize); |
| 1521 |
if(nasize<oldasize){ |
| 1522 |
t->sizearray=nasize; |
| 1523 |
for(i=nasize;i<oldasize;i++){ |
| 1524 |
if(!ttisnil(&t->array[i])) |
| 1525 |
setobj(L,luaH_setnum(L,t,i+1),&t->array[i]); |
| 1526 |
} |
| 1527 |
luaM_reallocvector(L,t->array,oldasize,nasize,TValue); |
| 1528 |
} |
| 1529 |
for(i=twoto(oldhsize)-1;i>=0;i--){ |
| 1530 |
Node*old=nold+i; |
| 1531 |
if(!ttisnil(gval(old))) |
| 1532 |
setobj(L,luaH_set(L,t,key2tval(old)),gval(old)); |
| 1533 |
} |
| 1534 |
if(nold!=(&dummynode_)) |
| 1535 |
luaM_freearray(L,nold,twoto(oldhsize),Node); |
| 1536 |
} |
| 1537 |
static void luaH_resizearray(lua_State*L,Table*t,int nasize){ |
| 1538 |
int nsize=(t->node==(&dummynode_))?0:sizenode(t); |
| 1539 |
resize(L,t,nasize,nsize); |
| 1540 |
} |
| 1541 |
static void rehash(lua_State*L,Table*t,const TValue*ek){ |
| 1542 |
int nasize,na; |
| 1543 |
int nums[(32-2)+1]; |
| 1544 |
int i; |
| 1545 |
int totaluse; |
| 1546 |
for(i=0;i<=(32-2);i++)nums[i]=0; |
| 1547 |
nasize=numusearray(t,nums); |
| 1548 |
totaluse=nasize; |
| 1549 |
totaluse+=numusehash(t,nums,&nasize); |
| 1550 |
nasize+=countint(ek,nums); |
| 1551 |
totaluse++; |
| 1552 |
na=computesizes(nums,&nasize); |
| 1553 |
resize(L,t,nasize,totaluse-na); |
| 1554 |
} |
| 1555 |
static Table*luaH_new(lua_State*L,int narray,int nhash){ |
| 1556 |
Table*t=luaM_new(L,Table); |
| 1557 |
luaC_link(L,obj2gco(t),5); |
| 1558 |
t->metatable=NULL; |
| 1559 |
t->flags=cast_byte(~0); |
| 1560 |
t->array=NULL; |
| 1561 |
t->sizearray=0; |
| 1562 |
t->lsizenode=0; |
| 1563 |
t->node=cast(Node*,(&dummynode_)); |
| 1564 |
setarrayvector(L,t,narray); |
| 1565 |
setnodevector(L,t,nhash); |
| 1566 |
return t; |
| 1567 |
} |
| 1568 |
static void luaH_free(lua_State*L,Table*t){ |
| 1569 |
if(t->node!=(&dummynode_)) |
| 1570 |
luaM_freearray(L,t->node,sizenode(t),Node); |
| 1571 |
luaM_freearray(L,t->array,t->sizearray,TValue); |
| 1572 |
luaM_free(L,t); |
| 1573 |
} |
| 1574 |
static Node*getfreepos(Table*t){ |
| 1575 |
while(t->lastfree-->t->node){ |
| 1576 |
if(ttisnil(gkey(t->lastfree))) |
| 1577 |
return t->lastfree; |
| 1578 |
} |
| 1579 |
return NULL; |
| 1580 |
} |
| 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_)){ |
| 1584 |
Node*othern; |
| 1585 |
Node*n=getfreepos(t); |
| 1586 |
if(n==NULL){ |
| 1587 |
rehash(L,t,key); |
| 1588 |
return luaH_set(L,t,key); |
| 1589 |
} |
| 1590 |
othern=mainposition(t,key2tval(mp)); |
| 1591 |
if(othern!=mp){ |
| 1592 |
while(gnext(othern)!=mp)othern=gnext(othern); |
| 1593 |
gnext(othern)=n; |
| 1594 |
*n=*mp; |
| 1595 |
gnext(mp)=NULL; |
| 1596 |
setnilvalue(gval(mp)); |
| 1597 |
} |
| 1598 |
else{ |
| 1599 |
gnext(n)=gnext(mp); |
| 1600 |
gnext(mp)=n; |
| 1601 |
mp=n; |
| 1602 |
} |
| 1603 |
} |
| 1604 |
gkey(mp)->value=key->value;gkey(mp)->tt=key->tt; |
| 1605 |
luaC_barriert(L,t,key); |
| 1606 |
return gval(mp); |
| 1607 |
} |
| 1608 |
static const TValue*luaH_getnum(Table*t,int key){ |
| 1609 |
if(cast(unsigned int,key)-1<cast(unsigned int,t->sizearray)) |
| 1610 |
return&t->array[key-1]; |
| 1611 |
else{ |
| 1612 |
lua_Number nk=cast_num(key); |
| 1613 |
Node*n=hashnum(t,nk); |
| 1614 |
do{ |
| 1615 |
if(ttisnumber(gkey(n))&&luai_numeq(nvalue(gkey(n)),nk)) |
| 1616 |
return gval(n); |
| 1617 |
else n=gnext(n); |
| 1618 |
}while(n); |
| 1619 |
return(&luaO_nilobject_); |
| 1620 |
} |
| 1621 |
} |
| 1622 |
static const TValue*luaH_getstr(Table*t,TString*key){ |
| 1623 |
Node*n=hashstr(t,key); |
| 1624 |
do{ |
| 1625 |
if(ttisstring(gkey(n))&&rawtsvalue(gkey(n))==key) |
| 1626 |
return gval(n); |
| 1627 |
else n=gnext(n); |
| 1628 |
}while(n); |
| 1629 |
return(&luaO_nilobject_); |
| 1630 |
} |
| 1631 |
static const TValue*luaH_get(Table*t,const TValue*key){ |
| 1632 |
switch(ttype(key)){ |
| 1633 |
case 0:return(&luaO_nilobject_); |
| 1634 |
case 4:return luaH_getstr(t,rawtsvalue(key)); |
| 1635 |
case 3:{ |
| 1636 |
int k; |
| 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); |
| 1641 |
} |
| 1642 |
/*fallthrough*/ |
| 1643 |
default:{ |
| 1644 |
Node*n=mainposition(t,key); |
| 1645 |
do{ |
| 1646 |
if(luaO_rawequalObj(key2tval(n),key)) |
| 1647 |
return gval(n); |
| 1648 |
else n=gnext(n); |
| 1649 |
}while(n); |
| 1650 |
return(&luaO_nilobject_); |
| 1651 |
} |
| 1652 |
} |
| 1653 |
} |
| 1654 |
static TValue*luaH_set(lua_State*L,Table*t,const TValue*key){ |
| 1655 |
const TValue*p=luaH_get(t,key); |
| 1656 |
t->flags=0; |
| 1657 |
if(p!=(&luaO_nilobject_)) |
| 1658 |
return cast(TValue*,p); |
| 1659 |
else{ |
| 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); |
| 1664 |
} |
| 1665 |
} |
| 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); |
| 1670 |
else{ |
| 1671 |
TValue k; |
| 1672 |
setnvalue(&k,cast_num(key)); |
| 1673 |
return newkey(L,t,&k); |
| 1674 |
} |
| 1675 |
} |
| 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); |
| 1680 |
else{ |
| 1681 |
TValue k; |
| 1682 |
setsvalue(L,&k,key); |
| 1683 |
return newkey(L,t,&k); |
| 1684 |
} |
| 1685 |
} |
| 1686 |
static int unbound_search(Table*t,unsigned int j){ |
| 1687 |
unsigned int i=j; |
| 1688 |
j++; |
| 1689 |
while(!ttisnil(luaH_getnum(t,j))){ |
| 1690 |
i=j; |
| 1691 |
j*=2; |
| 1692 |
if(j>cast(unsigned int,(INT_MAX-2))){ |
| 1693 |
i=1; |
| 1694 |
while(!ttisnil(luaH_getnum(t,i)))i++; |
| 1695 |
return i-1; |
| 1696 |
} |
| 1697 |
} |
| 1698 |
while(j-i>1){ |
| 1699 |
unsigned int m=(i+j)/2; |
| 1700 |
if(ttisnil(luaH_getnum(t,m)))j=m; |
| 1701 |
else i=m; |
| 1702 |
} |
| 1703 |
return i; |
| 1704 |
} |
| 1705 |
static int luaH_getn(Table*t){ |
| 1706 |
unsigned int j=t->sizearray; |
| 1707 |
if(j>0&&ttisnil(&t->array[j-1])){ |
| 1708 |
unsigned int i=0; |
| 1709 |
while(j-i>1){ |
| 1710 |
unsigned int m=(i+j)/2; |
| 1711 |
if(ttisnil(&t->array[m-1]))j=m; |
| 1712 |
else i=m; |
| 1713 |
} |
| 1714 |
return i; |
| 1715 |
} |
| 1716 |
else if(t->node==(&dummynode_)) |
| 1717 |
return j; |
| 1718 |
else return unbound_search(t,j); |
| 1719 |
} |
| 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)); |
| 1732 |
} |
| 1733 |
static void reallymarkobject(global_State*g,GCObject*o){ |
| 1734 |
white2gray(o); |
| 1735 |
switch(o->gch.tt){ |
| 1736 |
case 4:{ |
| 1737 |
return; |
| 1738 |
} |
| 1739 |
case 7:{ |
| 1740 |
Table*mt=gco2u(o)->metatable; |
| 1741 |
gray2black(o); |
| 1742 |
if(mt)markobject(g,mt); |
| 1743 |
markobject(g,gco2u(o)->env); |
| 1744 |
return; |
| 1745 |
} |
| 1746 |
case(8+2):{ |
| 1747 |
UpVal*uv=gco2uv(o); |
| 1748 |
markvalue(g,uv->v); |
| 1749 |
if(uv->v==&uv->u.value) |
| 1750 |
gray2black(o); |
| 1751 |
return; |
| 1752 |
} |
| 1753 |
case 6:{ |
| 1754 |
gco2cl(o)->c.gclist=g->gray; |
| 1755 |
g->gray=o; |
| 1756 |
break; |
| 1757 |
} |
| 1758 |
case 5:{ |
| 1759 |
gco2h(o)->gclist=g->gray; |
| 1760 |
g->gray=o; |
| 1761 |
break; |
| 1762 |
} |
| 1763 |
case 8:{ |
| 1764 |
gco2th(o)->gclist=g->gray; |
| 1765 |
g->gray=o; |
| 1766 |
break; |
| 1767 |
} |
| 1768 |
case(8+1):{ |
| 1769 |
gco2p(o)->gclist=g->gray; |
| 1770 |
g->gray=o; |
| 1771 |
break; |
| 1772 |
} |
| 1773 |
default:; |
| 1774 |
} |
| 1775 |
} |
| 1776 |
static void marktmu(global_State*g){ |
| 1777 |
GCObject*u=g->tmudata; |
| 1778 |
if(u){ |
| 1779 |
do{ |
| 1780 |
u=u->gch.next; |
| 1781 |
makewhite(g,u); |
| 1782 |
reallymarkobject(g,u); |
| 1783 |
}while(u!=g->tmudata); |
| 1784 |
} |
| 1785 |
} |
| 1786 |
static size_t luaC_separateudata(lua_State*L,int all){ |
| 1787 |
global_State*g=G(L); |
| 1788 |
size_t deadmem=0; |
| 1789 |
GCObject**p=&g->mainthread->next; |
| 1790 |
GCObject*curr; |
| 1791 |
while((curr=*p)!=NULL){ |
| 1792 |
if(!(iswhite(curr)||all)||isfinalized(gco2u(curr))) |
| 1793 |
p=&curr->gch.next; |
| 1794 |
else if(fasttm(L,gco2u(curr)->metatable,TM_GC)==NULL){ |
| 1795 |
markfinalized(gco2u(curr)); |
| 1796 |
p=&curr->gch.next; |
| 1797 |
} |
| 1798 |
else{ |
| 1799 |
deadmem+=sizeudata(gco2u(curr)); |
| 1800 |
markfinalized(gco2u(curr)); |
| 1801 |
*p=curr->gch.next; |
| 1802 |
if(g->tmudata==NULL) |
| 1803 |
g->tmudata=curr->gch.next=curr; |
| 1804 |
else{ |
| 1805 |
curr->gch.next=g->tmudata->gch.next; |
| 1806 |
g->tmudata->gch.next=curr; |
| 1807 |
g->tmudata=curr; |
| 1808 |
} |
| 1809 |
} |
| 1810 |
} |
| 1811 |
return deadmem; |
| 1812 |
} |
| 1813 |
static int traversetable(global_State*g,Table*h){ |
| 1814 |
int i; |
| 1815 |
int weakkey=0; |
| 1816 |
int weakvalue=0; |
| 1817 |
const TValue*mode; |
| 1818 |
if(h->metatable) |
| 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)| |
| 1827 |
(weakvalue<<4)); |
| 1828 |
h->gclist=g->weak; |
| 1829 |
g->weak=obj2gco(h); |
| 1830 |
} |
| 1831 |
} |
| 1832 |
if(weakkey&&weakvalue)return 1; |
| 1833 |
if(!weakvalue){ |
| 1834 |
i=h->sizearray; |
| 1835 |
while(i--) |
| 1836 |
markvalue(g,&h->array[i]); |
| 1837 |
} |
| 1838 |
i=sizenode(h); |
| 1839 |
while(i--){ |
| 1840 |
Node*n=gnode(h,i); |
| 1841 |
if(ttisnil(gval(n))) |
| 1842 |
removeentry(n); |
| 1843 |
else{ |
| 1844 |
if(!weakkey)markvalue(g,gkey(n)); |
| 1845 |
if(!weakvalue)markvalue(g,gval(n)); |
| 1846 |
} |
| 1847 |
} |
| 1848 |
return weakkey||weakvalue; |
| 1849 |
} |
| 1850 |
static void traverseproto(global_State*g,Proto*f){ |
| 1851 |
int i; |
| 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++){ |
| 1856 |
if(f->upvalues[i]) |
| 1857 |
stringmark(f->upvalues[i]); |
| 1858 |
} |
| 1859 |
for(i=0;i<f->sizep;i++){ |
| 1860 |
if(f->p[i]) |
| 1861 |
markobject(g,f->p[i]); |
| 1862 |
} |
| 1863 |
for(i=0;i<f->sizelocvars;i++){ |
| 1864 |
if(f->locvars[i].varname) |
| 1865 |
stringmark(f->locvars[i].varname); |
| 1866 |
} |
| 1867 |
} |
| 1868 |
static void traverseclosure(global_State*g,Closure*cl){ |
| 1869 |
markobject(g,cl->c.env); |
| 1870 |
if(cl->c.isC){ |
| 1871 |
int i; |
| 1872 |
for(i=0;i<cl->c.nupvalues;i++) |
| 1873 |
markvalue(g,&cl->c.upvalue[i]); |
| 1874 |
} |
| 1875 |
else{ |
| 1876 |
int i; |
| 1877 |
markobject(g,cl->l.p); |
| 1878 |
for(i=0;i<cl->l.nupvalues;i++) |
| 1879 |
markobject(g,cl->l.upvals[i]); |
| 1880 |
} |
| 1881 |
} |
| 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) |
| 1886 |
return; |
| 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)); |
| 1894 |
} |
| 1895 |
static void traversestack(global_State*g,lua_State*l){ |
| 1896 |
StkId o,lim; |
| 1897 |
CallInfo*ci; |
| 1898 |
markvalue(g,gt(l)); |
| 1899 |
lim=l->top; |
| 1900 |
for(ci=l->base_ci;ci<=l->ci;ci++){ |
| 1901 |
if(lim<ci->top)lim=ci->top; |
| 1902 |
} |
| 1903 |
for(o=l->stack;o<l->top;o++) |
| 1904 |
markvalue(g,o); |
| 1905 |
for(;o<=lim;o++) |
| 1906 |
setnilvalue(o); |
| 1907 |
checkstacksizes(l,lim); |
| 1908 |
} |
| 1909 |
static l_mem propagatemark(global_State*g){ |
| 1910 |
GCObject*o=g->gray; |
| 1911 |
gray2black(o); |
| 1912 |
switch(o->gch.tt){ |
| 1913 |
case 5:{ |
| 1914 |
Table*h=gco2h(o); |
| 1915 |
g->gray=h->gclist; |
| 1916 |
if(traversetable(g,h)) |
| 1917 |
black2gray(o); |
| 1918 |
return sizeof(Table)+sizeof(TValue)*h->sizearray+ |
| 1919 |
sizeof(Node)*sizenode(h); |
| 1920 |
} |
| 1921 |
case 6:{ |
| 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); |
| 1927 |
} |
| 1928 |
case 8:{ |
| 1929 |
lua_State*th=gco2th(o); |
| 1930 |
g->gray=th->gclist; |
| 1931 |
th->gclist=g->grayagain; |
| 1932 |
g->grayagain=o; |
| 1933 |
black2gray(o); |
| 1934 |
traversestack(g,th); |
| 1935 |
return sizeof(lua_State)+sizeof(TValue)*th->stacksize+ |
| 1936 |
sizeof(CallInfo)*th->size_ci; |
| 1937 |
} |
| 1938 |
case(8+1):{ |
| 1939 |
Proto*p=gco2p(o); |
| 1940 |
g->gray=p->gclist; |
| 1941 |
traverseproto(g,p); |
| 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; |
| 1948 |
} |
| 1949 |
default:return 0; |
| 1950 |
} |
| 1951 |
} |
| 1952 |
static size_t propagateall(global_State*g){ |
| 1953 |
size_t m=0; |
| 1954 |
while(g->gray)m+=propagatemark(g); |
| 1955 |
return m; |
| 1956 |
} |
| 1957 |
static int iscleared(const TValue*o,int iskey){ |
| 1958 |
if(!iscollectable(o))return 0; |
| 1959 |
if(ttisstring(o)){ |
| 1960 |
stringmark(rawtsvalue(o)); |
| 1961 |
return 0; |
| 1962 |
} |
| 1963 |
return iswhite(gcvalue(o))|| |
| 1964 |
(ttisuserdata(o)&&(!iskey&&isfinalized(uvalue(o)))); |
| 1965 |
} |
| 1966 |
static void cleartable(GCObject*l){ |
| 1967 |
while(l){ |
| 1968 |
Table*h=gco2h(l); |
| 1969 |
int i=h->sizearray; |
| 1970 |
if(testbit(h->marked,4)){ |
| 1971 |
while(i--){ |
| 1972 |
TValue*o=&h->array[i]; |
| 1973 |
if(iscleared(o,0)) |
| 1974 |
setnilvalue(o); |
| 1975 |
} |
| 1976 |
} |
| 1977 |
i=sizenode(h); |
| 1978 |
while(i--){ |
| 1979 |
Node*n=gnode(h,i); |
| 1980 |
if(!ttisnil(gval(n))&& |
| 1981 |
(iscleared(key2tval(n),1)||iscleared(gval(n),0))){ |
| 1982 |
setnilvalue(gval(n)); |
| 1983 |
removeentry(n); |
| 1984 |
} |
| 1985 |
} |
| 1986 |
l=h->gclist; |
| 1987 |
} |
| 1988 |
} |
| 1989 |
static void freeobj(lua_State*L,GCObject*o){ |
| 1990 |
switch(o->gch.tt){ |
| 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; |
| 1995 |
case 8:{ |
| 1996 |
luaE_freethread(L,gco2th(o)); |
| 1997 |
break; |
| 1998 |
} |
| 1999 |
case 4:{ |
| 2000 |
G(L)->strt.nuse--; |
| 2001 |
luaM_freemem(L,o,sizestring(gco2ts(o))); |
| 2002 |
break; |
| 2003 |
} |
| 2004 |
case 7:{ |
| 2005 |
luaM_freemem(L,o,sizeudata(gco2u(o))); |
| 2006 |
break; |
| 2007 |
} |
| 2008 |
default:; |
| 2009 |
} |
| 2010 |
} |
| 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){ |
| 2013 |
GCObject*curr; |
| 2014 |
global_State*g=G(L); |
| 2015 |
int deadmask=otherwhite(g); |
| 2016 |
while((curr=*p)!=NULL&&count-->0){ |
| 2017 |
if(curr->gch.tt==8) |
| 2018 |
sweepwholelist(L,&gco2th(curr)->openupval); |
| 2019 |
if((curr->gch.marked^bit2mask(0,1))&deadmask){ |
| 2020 |
makewhite(g,curr); |
| 2021 |
p=&curr->gch.next; |
| 2022 |
} |
| 2023 |
else{ |
| 2024 |
*p=curr->gch.next; |
| 2025 |
if(curr==g->rootgc) |
| 2026 |
g->rootgc=curr->gch.next; |
| 2027 |
freeobj(L,curr); |
| 2028 |
} |
| 2029 |
} |
| 2030 |
return p; |
| 2031 |
} |
| 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)&& |
| 2035 |
g->strt.size>32*2) |
| 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); |
| 2040 |
} |
| 2041 |
} |
| 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); |
| 2046 |
const TValue*tm; |
| 2047 |
if(o==g->tmudata) |
| 2048 |
g->tmudata=NULL; |
| 2049 |
else |
| 2050 |
g->tmudata->gch.next=udata->uv.next; |
| 2051 |
udata->uv.next=g->mainthread->next; |
| 2052 |
g->mainthread->next=o; |
| 2053 |
makewhite(g,o); |
| 2054 |
tm=fasttm(L,udata->uv.metatable,TM_GC); |
| 2055 |
if(tm!=NULL){ |
| 2056 |
lu_byte oldah=L->allowhook; |
| 2057 |
lu_mem oldt=g->GCthreshold; |
| 2058 |
L->allowhook=0; |
| 2059 |
g->GCthreshold=2*g->totalbytes; |
| 2060 |
setobj(L,L->top,tm); |
| 2061 |
setuvalue(L,L->top+1,udata); |
| 2062 |
L->top+=2; |
| 2063 |
luaD_call(L,L->top-2,0); |
| 2064 |
L->allowhook=oldah; |
| 2065 |
g->GCthreshold=oldt; |
| 2066 |
} |
| 2067 |
} |
| 2068 |
static void luaC_callGCTM(lua_State*L){ |
| 2069 |
while(G(L)->tmudata) |
| 2070 |
GCTM(L); |
| 2071 |
} |
| 2072 |
static void luaC_freeall(lua_State*L){ |
| 2073 |
global_State*g=G(L); |
| 2074 |
int i; |
| 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]); |
| 2079 |
} |
| 2080 |
static void markmt(global_State*g){ |
| 2081 |
int i; |
| 2082 |
for(i=0;i<(8+1);i++) |
| 2083 |
if(g->mt[i])markobject(g,g->mt[i]); |
| 2084 |
} |
| 2085 |
static void markroot(lua_State*L){ |
| 2086 |
global_State*g=G(L); |
| 2087 |
g->gray=NULL; |
| 2088 |
g->grayagain=NULL; |
| 2089 |
g->weak=NULL; |
| 2090 |
markobject(g,g->mainthread); |
| 2091 |
markvalue(g,gt(g->mainthread)); |
| 2092 |
markvalue(g,registry(L)); |
| 2093 |
markmt(g); |
| 2094 |
g->gcstate=1; |
| 2095 |
} |
| 2096 |
static void remarkupvals(global_State*g){ |
| 2097 |
UpVal*uv; |
| 2098 |
for(uv=g->uvhead.u.l.next;uv!=&g->uvhead;uv=uv->u.l.next){ |
| 2099 |
if(isgray(obj2gco(uv))) |
| 2100 |
markvalue(g,uv->v); |
| 2101 |
} |
| 2102 |
} |
| 2103 |
static void atomic(lua_State*L){ |
| 2104 |
global_State*g=G(L); |
| 2105 |
size_t udsize; |
| 2106 |
remarkupvals(g); |
| 2107 |
propagateall(g); |
| 2108 |
g->gray=g->weak; |
| 2109 |
g->weak=NULL; |
| 2110 |
markobject(g,L); |
| 2111 |
markmt(g); |
| 2112 |
propagateall(g); |
| 2113 |
g->gray=g->grayagain; |
| 2114 |
g->grayagain=NULL; |
| 2115 |
propagateall(g); |
| 2116 |
udsize=luaC_separateudata(L,0); |
| 2117 |
marktmu(g); |
| 2118 |
udsize+=propagateall(g); |
| 2119 |
cleartable(g->weak); |
| 2120 |
g->currentwhite=cast_byte(otherwhite(g)); |
| 2121 |
g->sweepstrgc=0; |
| 2122 |
g->sweepgc=&g->rootgc; |
| 2123 |
g->gcstate=2; |
| 2124 |
g->estimate=g->totalbytes-udsize; |
| 2125 |
} |
| 2126 |
static l_mem singlestep(lua_State*L){ |
| 2127 |
global_State*g=G(L); |
| 2128 |
switch(g->gcstate){ |
| 2129 |
case 0:{ |
| 2130 |
markroot(L); |
| 2131 |
return 0; |
| 2132 |
} |
| 2133 |
case 1:{ |
| 2134 |
if(g->gray) |
| 2135 |
return propagatemark(g); |
| 2136 |
else{ |
| 2137 |
atomic(L); |
| 2138 |
return 0; |
| 2139 |
} |
| 2140 |
} |
| 2141 |
case 2:{ |
| 2142 |
lu_mem old=g->totalbytes; |
| 2143 |
sweepwholelist(L,&g->strt.hash[g->sweepstrgc++]); |
| 2144 |
if(g->sweepstrgc>=g->strt.size) |
| 2145 |
g->gcstate=3; |
| 2146 |
g->estimate-=old-g->totalbytes; |
| 2147 |
return 10; |
| 2148 |
} |
| 2149 |
case 3:{ |
| 2150 |
lu_mem old=g->totalbytes; |
| 2151 |
g->sweepgc=sweeplist(L,g->sweepgc,40); |
| 2152 |
if(*g->sweepgc==NULL){ |
| 2153 |
checkSizes(L); |
| 2154 |
g->gcstate=4; |
| 2155 |
} |
| 2156 |
g->estimate-=old-g->totalbytes; |
| 2157 |
return 40*10; |
| 2158 |
} |
| 2159 |
case 4:{ |
| 2160 |
if(g->tmudata){ |
| 2161 |
GCTM(L); |
| 2162 |
if(g->estimate>100) |
| 2163 |
g->estimate-=100; |
| 2164 |
return 100; |
| 2165 |
} |
| 2166 |
else{ |
| 2167 |
g->gcstate=0; |
| 2168 |
g->gcdept=0; |
| 2169 |
return 0; |
| 2170 |
} |
| 2171 |
} |
| 2172 |
default:return 0; |
| 2173 |
} |
| 2174 |
} |
| 2175 |
static void luaC_step(lua_State*L){ |
| 2176 |
global_State*g=G(L); |
| 2177 |
l_mem lim=(1024u/100)*g->gcstepmul; |
| 2178 |
if(lim==0) |
| 2179 |
lim=(((lu_mem)(~(lu_mem)0)-2)-1)/2; |
| 2180 |
g->gcdept+=g->totalbytes-g->GCthreshold; |
| 2181 |
do{ |
| 2182 |
lim-=singlestep(L); |
| 2183 |
if(g->gcstate==0) |
| 2184 |
break; |
| 2185 |
}while(lim>0); |
| 2186 |
if(g->gcstate!=0){ |
| 2187 |
if(g->gcdept<1024u) |
| 2188 |
g->GCthreshold=g->totalbytes+1024u; |
| 2189 |
else{ |
| 2190 |
g->gcdept-=1024u; |
| 2191 |
g->GCthreshold=g->totalbytes; |
| 2192 |
} |
| 2193 |
} |
| 2194 |
else{ |
| 2195 |
setthreshold(g); |
| 2196 |
} |
| 2197 |
} |
| 2198 |
static void luaC_barrierf(lua_State*L,GCObject*o,GCObject*v){ |
| 2199 |
global_State*g=G(L); |
| 2200 |
if(g->gcstate==1) |
| 2201 |
reallymarkobject(g,v); |
| 2202 |
else |
| 2203 |
makewhite(g,o); |
| 2204 |
} |
| 2205 |
static void luaC_barrierback(lua_State*L,Table*t){ |
| 2206 |
global_State*g=G(L); |
| 2207 |
GCObject*o=obj2gco(t); |
| 2208 |
black2gray(o); |
| 2209 |
t->gclist=g->grayagain; |
| 2210 |
g->grayagain=o; |
| 2211 |
} |
| 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; |
| 2215 |
g->rootgc=o; |
| 2216 |
o->gch.marked=luaC_white(g); |
| 2217 |
o->gch.tt=tt; |
| 2218 |
} |
| 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; |
| 2223 |
g->rootgc=o; |
| 2224 |
if(isgray(o)){ |
| 2225 |
if(g->gcstate==1){ |
| 2226 |
gray2black(o); |
| 2227 |
luaC_barrier(L,uv,uv->v); |
| 2228 |
} |
| 2229 |
else{ |
| 2230 |
makewhite(g,o); |
| 2231 |
} |
| 2232 |
} |
| 2233 |
} |
| 2234 |
typedef union{ |
| 2235 |
lua_Number r; |
| 2236 |
TString*ts; |
| 2237 |
}SemInfo; |
| 2238 |
typedef struct Token{ |
| 2239 |
int token; |
| 2240 |
SemInfo seminfo; |
| 2241 |
}Token; |
| 2242 |
typedef struct LexState{ |
| 2243 |
int current; |
| 2244 |
int linenumber; |
| 2245 |
int lastline; |
| 2246 |
Token t; |
| 2247 |
Token lookahead; |
| 2248 |
struct FuncState*fs; |
| 2249 |
struct lua_State*L; |
| 2250 |
ZIO*z; |
| 2251 |
Mbuffer*buff; |
| 2252 |
TString*source; |
| 2253 |
char decpoint; |
| 2254 |
}LexState; |
| 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)) |
| 2260 |
typedef struct LG{ |
| 2261 |
lua_State l; |
| 2262 |
global_State g; |
| 2263 |
}LG; |
| 2264 |
static void stack_init(lua_State*L1,lua_State*L){ |
| 2265 |
L1->base_ci=luaM_newvector(L,8,CallInfo); |
| 2266 |
L1->ci=L1->base_ci; |
| 2267 |
L1->size_ci=8; |
| 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; |
| 2271 |
L1->top=L1->stack; |
| 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; |
| 2277 |
} |
| 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); |
| 2281 |
} |
| 2282 |
static void f_luaopen(lua_State*L,void*ud){ |
| 2283 |
global_State*g=G(L); |
| 2284 |
UNUSED(ud); |
| 2285 |
stack_init(L,L); |
| 2286 |
sethvalue(L,gt(L),luaH_new(L,0,2)); |
| 2287 |
sethvalue(L,registry(L),luaH_new(L,0,2)); |
| 2288 |
luaS_resize(L,32); |
| 2289 |
luaT_init(L); |
| 2290 |
luaX_init(L); |
| 2291 |
luaS_fix(luaS_newliteral(L,"not enough memory")); |
| 2292 |
g->GCthreshold=4*g->totalbytes; |
| 2293 |
} |
| 2294 |
static void preinit_state(lua_State*L,global_State*g){ |
| 2295 |
G(L)=g; |
| 2296 |
L->stack=NULL; |
| 2297 |
L->stacksize=0; |
| 2298 |
L->errorJmp=NULL; |
| 2299 |
L->hook=NULL; |
| 2300 |
L->hookmask=0; |
| 2301 |
L->basehookcount=0; |
| 2302 |
L->allowhook=1; |
| 2303 |
resethookcount(L); |
| 2304 |
L->openupval=NULL; |
| 2305 |
L->size_ci=0; |
| 2306 |
L->nCcalls=L->baseCcalls=0; |
| 2307 |
L->status=0; |
| 2308 |
L->base_ci=L->ci=NULL; |
| 2309 |
L->savedpc=NULL; |
| 2310 |
L->errfunc=0; |
| 2311 |
setnilvalue(gt(L)); |
| 2312 |
} |
| 2313 |
static void close_state(lua_State*L){ |
| 2314 |
global_State*g=G(L); |
| 2315 |
luaF_close(L,L->stack); |
| 2316 |
luaC_freeall(L); |
| 2317 |
luaM_freearray(L,G(L)->strt.hash,G(L)->strt.size,TString*); |
| 2318 |
luaZ_freebuffer(L,&g->buff); |
| 2319 |
freestack(L,L); |
| 2320 |
(*g->frealloc)(g->ud,fromstate(L),state_size(LG),0); |
| 2321 |
} |
| 2322 |
static void luaE_freethread(lua_State*L,lua_State*L1){ |
| 2323 |
luaF_close(L1,L1->stack); |
| 2324 |
freestack(L,L1); |
| 2325 |
luaM_freemem(L,fromstate(L1),state_size(lua_State)); |
| 2326 |
} |
| 2327 |
static lua_State*lua_newstate(lua_Alloc f,void*ud){ |
| 2328 |
int i; |
| 2329 |
lua_State*L; |
| 2330 |
global_State*g; |
| 2331 |
void*l=(*f)(ud,NULL,0,state_size(LG)); |
| 2332 |
if(l==NULL)return NULL; |
| 2333 |
L=tostate(l); |
| 2334 |
g=&((LG*)L)->g; |
| 2335 |
L->next=NULL; |
| 2336 |
L->tt=8; |
| 2337 |
g->currentwhite=bit2mask(0,5); |
| 2338 |
L->marked=luaC_white(g); |
| 2339 |
set2bits(L->marked,5,6); |
| 2340 |
preinit_state(L,g); |
| 2341 |
g->frealloc=f; |
| 2342 |
g->ud=ud; |
| 2343 |
g->mainthread=L; |
| 2344 |
g->uvhead.u.l.prev=&g->uvhead; |
| 2345 |
g->uvhead.u.l.next=&g->uvhead; |
| 2346 |
g->GCthreshold=0; |
| 2347 |
g->strt.size=0; |
| 2348 |
g->strt.nuse=0; |
| 2349 |
g->strt.hash=NULL; |
| 2350 |
setnilvalue(registry(L)); |
| 2351 |
luaZ_initbuffer(L,&g->buff); |
| 2352 |
g->panic=NULL; |
| 2353 |
g->gcstate=0; |
| 2354 |
g->rootgc=obj2gco(L); |
| 2355 |
g->sweepstrgc=0; |
| 2356 |
g->sweepgc=&g->rootgc; |
| 2357 |
g->gray=NULL; |
| 2358 |
g->grayagain=NULL; |
| 2359 |
g->weak=NULL; |
| 2360 |
g->tmudata=NULL; |
| 2361 |
g->totalbytes=sizeof(LG); |
| 2362 |
g->gcpause=200; |
| 2363 |
g->gcstepmul=200; |
| 2364 |
g->gcdept=0; |
| 2365 |
for(i=0;i<(8+1);i++)g->mt[i]=NULL; |
| 2366 |
if(luaD_rawrunprotected(L,f_luaopen,NULL)!=0){ |
| 2367 |
close_state(L); |
| 2368 |
L=NULL; |
| 2369 |
} |
| 2370 |
else |
| 2371 |
{} |
| 2372 |
return L; |
| 2373 |
} |
| 2374 |
static void callallgcTM(lua_State*L,void*ud){ |
| 2375 |
UNUSED(ud); |
| 2376 |
luaC_callGCTM(L); |
| 2377 |
} |
| 2378 |
static void lua_close(lua_State*L){ |
| 2379 |
L=G(L)->mainthread; |
| 2380 |
luaF_close(L,L->stack); |
| 2381 |
luaC_separateudata(L,1); |
| 2382 |
L->errfunc=0; |
| 2383 |
do{ |
| 2384 |
L->ci=L->base_ci; |
| 2385 |
L->base=L->top=L->ci->base; |
| 2386 |
L->nCcalls=L->baseCcalls=0; |
| 2387 |
}while(luaD_rawrunprotected(L,callallgcTM,NULL)!=0); |
| 2388 |
close_state(L); |
| 2389 |
} |
| 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; |
| 2400 |
if(ci==L->ci) |
| 2401 |
ci->savedpc=L->savedpc; |
| 2402 |
return pcRel(ci->savedpc,ci_func(ci)->l.p); |
| 2403 |
} |
| 2404 |
static int currentline(lua_State*L,CallInfo*ci){ |
| 2405 |
int pc=currentpc(L,ci); |
| 2406 |
if(pc<0) |
| 2407 |
return-1; |
| 2408 |
else |
| 2409 |
return getline_(ci_func(ci)->l.p,pc); |
| 2410 |
} |
| 2411 |
static int lua_getstack(lua_State*L,int level,lua_Debug*ar){ |
| 2412 |
int status; |
| 2413 |
CallInfo*ci; |
| 2414 |
for(ci=L->ci;level>0&&ci>L->base_ci;ci--){ |
| 2415 |
level--; |
| 2416 |
if(f_isLua(ci)) |
| 2417 |
level-=ci->tailcalls; |
| 2418 |
} |
| 2419 |
if(level==0&&ci>L->base_ci){ |
| 2420 |
status=1; |
| 2421 |
ar->i_ci=cast_int(ci-L->base_ci); |
| 2422 |
} |
| 2423 |
else if(level<0){ |
| 2424 |
status=1; |
| 2425 |
ar->i_ci=0; |
| 2426 |
} |
| 2427 |
else status=0; |
| 2428 |
return status; |
| 2429 |
} |
| 2430 |
static Proto*getluaproto(CallInfo*ci){ |
| 2431 |
return(isLua(ci)?ci_func(ci)->l.p:NULL); |
| 2432 |
} |
| 2433 |
static void funcinfo(lua_Debug*ar,Closure*cl){ |
| 2434 |
if(cl->c.isC){ |
| 2435 |
ar->source="=[C]"; |
| 2436 |
ar->linedefined=-1; |
| 2437 |
ar->lastlinedefined=-1; |
| 2438 |
ar->what="C"; |
| 2439 |
} |
| 2440 |
else{ |
| 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"; |
| 2445 |
} |
| 2446 |
luaO_chunkid(ar->short_src,ar->source,60); |
| 2447 |
} |
| 2448 |
static void info_tailcall(lua_Debug*ar){ |
| 2449 |
ar->name=ar->namewhat=""; |
| 2450 |
ar->what="tail"; |
| 2451 |
ar->lastlinedefined=ar->linedefined=ar->currentline=-1; |
| 2452 |
ar->source="=(tail call)"; |
| 2453 |
luaO_chunkid(ar->short_src,ar->source,60); |
| 2454 |
ar->nups=0; |
| 2455 |
} |
| 2456 |
static void collectvalidlines(lua_State*L,Closure*f){ |
| 2457 |
if(f==NULL||f->c.isC){ |
| 2458 |
setnilvalue(L->top); |
| 2459 |
} |
| 2460 |
else{ |
| 2461 |
Table*t=luaH_new(L,0,0); |
| 2462 |
int*lineinfo=f->l.p->lineinfo; |
| 2463 |
int i; |
| 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); |
| 2467 |
} |
| 2468 |
incr_top(L); |
| 2469 |
} |
| 2470 |
static int auxgetinfo(lua_State*L,const char*what,lua_Debug*ar, |
| 2471 |
Closure*f,CallInfo*ci){ |
| 2472 |
int status=1; |
| 2473 |
if(f==NULL){ |
| 2474 |
info_tailcall(ar); |
| 2475 |
return status; |
| 2476 |
} |
| 2477 |
for(;*what;what++){ |
| 2478 |
switch(*what){ |
| 2479 |
case'S':{ |
| 2480 |
funcinfo(ar,f); |
| 2481 |
break; |
| 2482 |
} |
| 2483 |
case'l':{ |
| 2484 |
ar->currentline=(ci)?currentline(L,ci):-1; |
| 2485 |
break; |
| 2486 |
} |
| 2487 |
case'u':{ |
| 2488 |
ar->nups=f->c.nupvalues; |
| 2489 |
break; |
| 2490 |
} |
| 2491 |
case'n':{ |
| 2492 |
ar->namewhat=(ci)?NULL:NULL; |
| 2493 |
if(ar->namewhat==NULL){ |
| 2494 |
ar->namewhat=""; |
| 2495 |
ar->name=NULL; |
| 2496 |
} |
| 2497 |
break; |
| 2498 |
} |
| 2499 |
case'L': |
| 2500 |
case'f': |
| 2501 |
break; |
| 2502 |
default:status=0; |
| 2503 |
} |
| 2504 |
} |
| 2505 |
return status; |
| 2506 |
} |
| 2507 |
static int lua_getinfo(lua_State*L,const char*what,lua_Debug*ar){ |
| 2508 |
int status; |
| 2509 |
Closure*f=NULL; |
| 2510 |
CallInfo*ci=NULL; |
| 2511 |
if(*what=='>'){ |
| 2512 |
StkId func=L->top-1; |
| 2513 |
luai_apicheck(L,ttisfunction(func)); |
| 2514 |
what++; |
| 2515 |
f=clvalue(func); |
| 2516 |
L->top--; |
| 2517 |
} |
| 2518 |
else if(ar->i_ci!=0){ |
| 2519 |
ci=L->base_ci+ar->i_ci; |
| 2520 |
f=clvalue(ci->func); |
| 2521 |
} |
| 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); |
| 2526 |
incr_top(L); |
| 2527 |
} |
| 2528 |
if(strchr(what,'L')) |
| 2529 |
collectvalidlines(L,f); |
| 2530 |
return status; |
| 2531 |
} |
| 2532 |
static int isinstack(CallInfo*ci,const TValue*o){ |
| 2533 |
StkId p; |
| 2534 |
for(p=ci->base;p<ci->top;p++) |
| 2535 |
if(o==p)return 1; |
| 2536 |
return 0; |
| 2537 |
} |
| 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))? |
| 2542 |
NULL: |
| 2543 |
NULL; |
| 2544 |
if(kind) |
| 2545 |
luaG_runerror(L,"attempt to %s %s "LUA_QL("%s")" (a %s value)", |
| 2546 |
op,kind,name,t); |
| 2547 |
else |
| 2548 |
luaG_runerror(L,"attempt to %s a %s value",op,t); |
| 2549 |
} |
| 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"); |
| 2553 |
} |
| 2554 |
static void luaG_aritherror(lua_State*L,const TValue*p1,const TValue*p2){ |
| 2555 |
TValue temp; |
| 2556 |
if(luaV_tonumber(p1,&temp)==NULL) |
| 2557 |
p2=p1; |
| 2558 |
luaG_typeerror(L,p2,"perform arithmetic on"); |
| 2559 |
} |
| 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)]; |
| 2563 |
if(t1[2]==t2[2]) |
| 2564 |
luaG_runerror(L,"attempt to compare two %s values",t1); |
| 2565 |
else |
| 2566 |
luaG_runerror(L,"attempt to compare %s with %s",t1,t2); |
| 2567 |
return 0; |
| 2568 |
} |
| 2569 |
static void addinfo(lua_State*L,const char*msg){ |
| 2570 |
CallInfo*ci=L->ci; |
| 2571 |
if(isLua(ci)){ |
| 2572 |
char buff[60]; |
| 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); |
| 2576 |
} |
| 2577 |
} |
| 2578 |
static void luaG_errormsg(lua_State*L){ |
| 2579 |
if(L->errfunc!=0){ |
| 2580 |
StkId errfunc=restorestack(L,L->errfunc); |
| 2581 |
if(!ttisfunction(errfunc))luaD_throw(L,5); |
| 2582 |
setobj(L,L->top,L->top-1); |
| 2583 |
setobj(L,L->top-1,errfunc); |
| 2584 |
incr_top(L); |
| 2585 |
luaD_call(L,L->top-2,1); |
| 2586 |
} |
| 2587 |
luaD_throw(L,2); |
| 2588 |
} |
| 2589 |
static void luaG_runerror(lua_State*L,const char*fmt,...){ |
| 2590 |
va_list argp; |
| 2591 |
va_start(argp,fmt); |
| 2592 |
addinfo(L,luaO_pushvfstring(L,fmt,argp)); |
| 2593 |
va_end(argp); |
| 2594 |
luaG_errormsg(L); |
| 2595 |
} |
| 2596 |
static int luaZ_fill(ZIO*z){ |
| 2597 |
size_t size; |
| 2598 |
lua_State*L=z->L; |
| 2599 |
const char*buff; |
| 2600 |
buff=z->reader(L,z->data,&size); |
| 2601 |
if(buff==NULL||size==0)return(-1); |
| 2602 |
z->n=size-1; |
| 2603 |
z->p=buff; |
| 2604 |
return char2int(*(z->p++)); |
| 2605 |
} |
| 2606 |
static void luaZ_init(lua_State*L,ZIO*z,lua_Reader reader,void*data){ |
| 2607 |
z->L=L; |
| 2608 |
z->reader=reader; |
| 2609 |
z->data=data; |
| 2610 |
z->n=0; |
| 2611 |
z->p=NULL; |
| 2612 |
} |
| 2613 |
static char*luaZ_openspace(lua_State*L,Mbuffer*buff,size_t n){ |
| 2614 |
if(n>buff->buffsize){ |
| 2615 |
if(n<32)n=32; |
| 2616 |
luaZ_resizebuffer(L,buff,n); |
| 2617 |
} |
| 2618 |
return buff->buffer; |
| 2619 |
} |
| 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) |
| 2660 |
}; |
| 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>", |
| 2670 |
NULL |
| 2671 |
}; |
| 2672 |
#define save_and_next(ls)(save(ls,ls->current),next(ls)) |
| 2673 |
static void save(LexState*ls,int c){ |
| 2674 |
Mbuffer*b=ls->buff; |
| 2675 |
if(b->n+1>b->buffsize){ |
| 2676 |
size_t newsize; |
| 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); |
| 2681 |
} |
| 2682 |
b->buffer[b->n++]=cast(char,c); |
| 2683 |
} |
| 2684 |
static void luaX_init(lua_State*L){ |
| 2685 |
int i; |
| 2686 |
for(i=0;i<(cast(int,TK_WHILE-257+1));i++){ |
| 2687 |
TString*ts=luaS_new(L,luaX_tokens[i]); |
| 2688 |
luaS_fix(ts); |
| 2689 |
ts->tsv.reserved=cast_byte(i+1); |
| 2690 |
} |
| 2691 |
} |
| 2692 |
static const char*luaX_token2str(LexState*ls,int token){ |
| 2693 |
if(token<257){ |
| 2694 |
return(iscntrl(token))?luaO_pushfstring(ls->L,"char(%d)",token): |
| 2695 |
luaO_pushfstring(ls->L,"%c",token); |
| 2696 |
} |
| 2697 |
else |
| 2698 |
return luaX_tokens[token-257]; |
| 2699 |
} |
| 2700 |
static const char*txtToken(LexState*ls,int token){ |
| 2701 |
switch(token){ |
| 2702 |
case TK_NAME: |
| 2703 |
case TK_STRING: |
| 2704 |
case TK_NUMBER: |
| 2705 |
save(ls,'\0'); |
| 2706 |
return luaZ_buffer(ls->buff); |
| 2707 |
default: |
| 2708 |
return luaX_token2str(ls,token); |
| 2709 |
} |
| 2710 |
} |
| 2711 |
static void luaX_lexerror(LexState*ls,const char*msg,int token){ |
| 2712 |
char buff[80]; |
| 2713 |
luaO_chunkid(buff,getstr(ls->source),80); |
| 2714 |
msg=luaO_pushfstring(ls->L,"%s:%d: %s",buff,ls->linenumber,msg); |
| 2715 |
if(token) |
| 2716 |
luaO_pushfstring(ls->L,"%s near "LUA_QL("%s"),msg,txtToken(ls,token)); |
| 2717 |
luaD_throw(ls->L,3); |
| 2718 |
} |
| 2719 |
static void luaX_syntaxerror(LexState*ls,const char*msg){ |
| 2720 |
luaX_lexerror(ls,msg,ls->t.token); |
| 2721 |
} |
| 2722 |
static TString*luaX_newstring(LexState*ls,const char*str,size_t l){ |
| 2723 |
lua_State*L=ls->L; |
| 2724 |
TString*ts=luaS_newlstr(L,str,l); |
| 2725 |
TValue*o=luaH_setstr(L,ls->fs->h,ts); |
| 2726 |
if(ttisnil(o)){ |
| 2727 |
setbvalue(o,1); |
| 2728 |
luaC_checkGC(L); |
| 2729 |
} |
| 2730 |
return ts; |
| 2731 |
} |
| 2732 |
static void inclinenumber(LexState*ls){ |
| 2733 |
int old=ls->current; |
| 2734 |
next(ls); |
| 2735 |
if(currIsNewline(ls)&&ls->current!=old) |
| 2736 |
next(ls); |
| 2737 |
if(++ls->linenumber>=(INT_MAX-2)) |
| 2738 |
luaX_syntaxerror(ls,"chunk has too many lines"); |
| 2739 |
} |
| 2740 |
static void luaX_setinput(lua_State*L,LexState*ls,ZIO*z,TString*source){ |
| 2741 |
ls->decpoint='.'; |
| 2742 |
ls->L=L; |
| 2743 |
ls->lookahead.token=TK_EOS; |
| 2744 |
ls->z=z; |
| 2745 |
ls->fs=NULL; |
| 2746 |
ls->linenumber=1; |
| 2747 |
ls->lastline=1; |
| 2748 |
ls->source=source; |
| 2749 |
luaZ_resizebuffer(ls->L,ls->buff,32); |
| 2750 |
next(ls); |
| 2751 |
} |
| 2752 |
static int check_next(LexState*ls,const char*set){ |
| 2753 |
if(!strchr(set,ls->current)) |
| 2754 |
return 0; |
| 2755 |
save_and_next(ls); |
| 2756 |
return 1; |
| 2757 |
} |
| 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); |
| 2761 |
while(n--) |
| 2762 |
if(p[n]==from)p[n]=to; |
| 2763 |
} |
| 2764 |
static void read_numeral(LexState*ls,SemInfo*seminfo){ |
| 2765 |
do{ |
| 2766 |
save_and_next(ls); |
| 2767 |
}while(isdigit(ls->current)||ls->current=='.'); |
| 2768 |
if(check_next(ls,"Ee")) |
| 2769 |
check_next(ls,"+-"); |
| 2770 |
while(isalnum(ls->current)||ls->current=='_') |
| 2771 |
save_and_next(ls); |
| 2772 |
save(ls,'\0'); |
| 2773 |
buffreplace(ls,'.',ls->decpoint); |
| 2774 |
if(!luaO_str2d(luaZ_buffer(ls->buff),&seminfo->r)) |
| 2775 |
luaX_lexerror(ls,"malformed number",TK_NUMBER); |
| 2776 |
} |
| 2777 |
static int skip_sep(LexState*ls){ |
| 2778 |
int count=0; |
| 2779 |
int s=ls->current; |
| 2780 |
save_and_next(ls); |
| 2781 |
while(ls->current=='='){ |
| 2782 |
save_and_next(ls); |
| 2783 |
count++; |
| 2784 |
} |
| 2785 |
return(ls->current==s)?count:(-count)-1; |
| 2786 |
} |
| 2787 |
static void read_long_string(LexState*ls,SemInfo*seminfo,int sep){ |
| 2788 |
int cont=0; |
| 2789 |
(void)(cont); |
| 2790 |
save_and_next(ls); |
| 2791 |
if(currIsNewline(ls)) |
| 2792 |
inclinenumber(ls); |
| 2793 |
for(;;){ |
| 2794 |
switch(ls->current){ |
| 2795 |
case(-1): |
| 2796 |
luaX_lexerror(ls,(seminfo)?"unfinished long string": |
| 2797 |
"unfinished long comment",TK_EOS); |
| 2798 |
break; |
| 2799 |
case']':{ |
| 2800 |
if(skip_sep(ls)==sep){ |
| 2801 |
save_and_next(ls); |
| 2802 |
goto endloop; |
| 2803 |
} |
| 2804 |
break; |
| 2805 |
} |
| 2806 |
case'\n': |
| 2807 |
case'\r':{ |
| 2808 |
save(ls,'\n'); |
| 2809 |
inclinenumber(ls); |
| 2810 |
if(!seminfo)luaZ_resetbuffer(ls->buff); |
| 2811 |
break; |
| 2812 |
} |
| 2813 |
default:{ |
| 2814 |
if(seminfo)save_and_next(ls); |
| 2815 |
else next(ls); |
| 2816 |
} |
| 2817 |
} |
| 2818 |
}endloop: |
| 2819 |
if(seminfo) |
| 2820 |
seminfo->ts=luaX_newstring(ls,luaZ_buffer(ls->buff)+(2+sep), |
| 2821 |
luaZ_bufflen(ls->buff)-2*(2+sep)); |
| 2822 |
} |
| 2823 |
static void read_string(LexState*ls,int del,SemInfo*seminfo){ |
| 2824 |
save_and_next(ls); |
| 2825 |
while(ls->current!=del){ |
| 2826 |
switch(ls->current){ |
| 2827 |
case(-1): |
| 2828 |
luaX_lexerror(ls,"unfinished string",TK_EOS); |
| 2829 |
continue; |
| 2830 |
case'\n': |
| 2831 |
case'\r': |
| 2832 |
luaX_lexerror(ls,"unfinished string",TK_STRING); |
| 2833 |
continue; |
| 2834 |
case'\\':{ |
| 2835 |
int c; |
| 2836 |
next(ls); |
| 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; |
| 2845 |
case'\n': |
| 2846 |
case'\r':save(ls,'\n');inclinenumber(ls);continue; |
| 2847 |
case(-1):continue; |
| 2848 |
default:{ |
| 2849 |
if(!isdigit(ls->current)) |
| 2850 |
save_and_next(ls); |
| 2851 |
else{ |
| 2852 |
int i=0; |
| 2853 |
c=0; |
| 2854 |
do{ |
| 2855 |
c=10*c+(ls->current-'0'); |
| 2856 |
next(ls); |
| 2857 |
}while(++i<3&&isdigit(ls->current)); |
| 2858 |
if(c>UCHAR_MAX) |
| 2859 |
luaX_lexerror(ls,"escape sequence too large",TK_STRING); |
| 2860 |
save(ls,c); |
| 2861 |
} |
| 2862 |
continue; |
| 2863 |
} |
| 2864 |
} |
| 2865 |
save(ls,c); |
| 2866 |
next(ls); |
| 2867 |
continue; |
| 2868 |
} |
| 2869 |
default: |
| 2870 |
save_and_next(ls); |
| 2871 |
} |
| 2872 |
} |
| 2873 |
save_and_next(ls); |
| 2874 |
seminfo->ts=luaX_newstring(ls,luaZ_buffer(ls->buff)+1, |
| 2875 |
luaZ_bufflen(ls->buff)-2); |
| 2876 |
} |
| 2877 |
static int llex(LexState*ls,SemInfo*seminfo){ |
| 2878 |
luaZ_resetbuffer(ls->buff); |
| 2879 |
for(;;){ |
| 2880 |
switch(ls->current){ |
| 2881 |
case'\n': |
| 2882 |
case'\r':{ |
| 2883 |
inclinenumber(ls); |
| 2884 |
continue; |
| 2885 |
} |
| 2886 |
case'-':{ |
| 2887 |
next(ls); |
| 2888 |
if(ls->current!='-')return'-'; |
| 2889 |
next(ls); |
| 2890 |
if(ls->current=='['){ |
| 2891 |
int sep=skip_sep(ls); |
| 2892 |
luaZ_resetbuffer(ls->buff); |
| 2893 |
if(sep>=0){ |
| 2894 |
read_long_string(ls,NULL,sep); |
| 2895 |
luaZ_resetbuffer(ls->buff); |
| 2896 |
continue; |
| 2897 |
} |
| 2898 |
} |
| 2899 |
while(!currIsNewline(ls)&&ls->current!=(-1)) |
| 2900 |
next(ls); |
| 2901 |
continue; |
| 2902 |
} |
| 2903 |
case'[':{ |
| 2904 |
int sep=skip_sep(ls); |
| 2905 |
if(sep>=0){ |
| 2906 |
read_long_string(ls,seminfo,sep); |
| 2907 |
return TK_STRING; |
| 2908 |
} |
| 2909 |
else if (sep!=-1)luaX_lexerror(ls,"invalid long string delimiter",TK_STRING); |
| 2910 |
return'['; |
| 2911 |
} |
| 2912 |
case'=':{ |
| 2913 |
next(ls); |
| 2914 |
if(ls->current!='=')return'='; |
| 2915 |
else{next(ls);return TK_EQ;} |
| 2916 |
} |
| 2917 |
case'<':{ |
| 2918 |
next(ls); |
| 2919 |
if(ls->current!='=')return'<'; |
| 2920 |
else{next(ls);return TK_LE;} |
| 2921 |
} |
| 2922 |
case'>':{ |
| 2923 |
next(ls); |
| 2924 |
if(ls->current!='=')return'>'; |
| 2925 |
else{next(ls);return TK_GE;} |
| 2926 |
} |
| 2927 |
case'~':{ |
| 2928 |
next(ls); |
| 2929 |
if(ls->current!='=')return'~'; |
| 2930 |
else{next(ls);return TK_NE;} |
| 2931 |
} |
| 2932 |
case'"': |
| 2933 |
case'\'':{ |
| 2934 |
read_string(ls,ls->current,seminfo); |
| 2935 |
return TK_STRING; |
| 2936 |
} |
| 2937 |
case'.':{ |
| 2938 |
save_and_next(ls); |
| 2939 |
if(check_next(ls,".")){ |
| 2940 |
if(check_next(ls,".")) |
| 2941 |
return TK_DOTS; |
| 2942 |
else return TK_CONCAT; |
| 2943 |
} |
| 2944 |
else if(!isdigit(ls->current))return'.'; |
| 2945 |
else{ |
| 2946 |
read_numeral(ls,seminfo); |
| 2947 |
return TK_NUMBER; |
| 2948 |
} |
| 2949 |
} |
| 2950 |
case(-1):{ |
| 2951 |
return TK_EOS; |
| 2952 |
} |
| 2953 |
default:{ |
| 2954 |
if(isspace(ls->current)){ |
| 2955 |
next(ls); |
| 2956 |
continue; |
| 2957 |
} |
| 2958 |
else if(isdigit(ls->current)){ |
| 2959 |
read_numeral(ls,seminfo); |
| 2960 |
return TK_NUMBER; |
| 2961 |
} |
| 2962 |
else if(isalpha(ls->current)||ls->current=='_'){ |
| 2963 |
TString*ts; |
| 2964 |
do{ |
| 2965 |
save_and_next(ls); |
| 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; |
| 2971 |
else{ |
| 2972 |
seminfo->ts=ts; |
| 2973 |
return TK_NAME; |
| 2974 |
} |
| 2975 |
} |
| 2976 |
else{ |
| 2977 |
int c=ls->current; |
| 2978 |
next(ls); |
| 2979 |
return c; |
| 2980 |
} |
| 2981 |
} |
| 2982 |
} |
| 2983 |
} |
| 2984 |
} |
| 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; |
| 2990 |
} |
| 2991 |
else |
| 2992 |
ls->t.token=llex(ls,&ls->t.seminfo); |
| 2993 |
} |
| 2994 |
static void luaX_lookahead(LexState*ls){ |
| 2995 |
ls->lookahead.token=llex(ls,&ls->lookahead.seminfo); |
| 2996 |
} |
| 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)); |
| 3000 |
} |
| 3001 |
static void luaK_nil(FuncState*fs,int from,int n){ |
| 3002 |
Instruction*previous; |
| 3003 |
if(fs->pc>fs->lasttarget){ |
| 3004 |
if(fs->pc==0){ |
| 3005 |
if(from>=fs->nactvar) |
| 3006 |
return; |
| 3007 |
} |
| 3008 |
else{ |
| 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){ |
| 3014 |
if(from+n-1>pto) |
| 3015 |
SETARG_B(*previous,from+n-1); |
| 3016 |
return; |
| 3017 |
} |
| 3018 |
} |
| 3019 |
} |
| 3020 |
} |
| 3021 |
luaK_codeABC(fs,OP_LOADNIL,from,from+n-1,0); |
| 3022 |
} |
| 3023 |
static int luaK_jump(FuncState*fs){ |
| 3024 |
int jpc=fs->jpc; |
| 3025 |
int j; |
| 3026 |
fs->jpc=(-1); |
| 3027 |
j=luaK_codeAsBx(fs,OP_JMP,0,(-1)); |
| 3028 |
luaK_concat(fs,&j,jpc); |
| 3029 |
return j; |
| 3030 |
} |
| 3031 |
static void luaK_ret(FuncState*fs,int first,int nret){ |
| 3032 |
luaK_codeABC(fs,OP_RETURN,first,nret+1,0); |
| 3033 |
} |
| 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); |
| 3037 |
} |
| 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); |
| 3044 |
} |
| 3045 |
static int luaK_getlabel(FuncState*fs){ |
| 3046 |
fs->lasttarget=fs->pc; |
| 3047 |
return fs->pc; |
| 3048 |
} |
| 3049 |
static int getjump(FuncState*fs,int pc){ |
| 3050 |
int offset=GETARG_sBx(fs->f->code[pc]); |
| 3051 |
if(offset==(-1)) |
| 3052 |
return(-1); |
| 3053 |
else |
| 3054 |
return(pc+1)+offset; |
| 3055 |
} |
| 3056 |
static Instruction*getjumpcontrol(FuncState*fs,int pc){ |
| 3057 |
Instruction*pi=&fs->f->code[pc]; |
| 3058 |
if(pc>=1&&testTMode(GET_OPCODE(*(pi-1)))) |
| 3059 |
return pi-1; |
| 3060 |
else |
| 3061 |
return pi; |
| 3062 |
} |
| 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; |
| 3067 |
} |
| 3068 |
return 0; |
| 3069 |
} |
| 3070 |
static int patchtestreg(FuncState*fs,int node,int reg){ |
| 3071 |
Instruction*i=getjumpcontrol(fs,node); |
| 3072 |
if(GET_OPCODE(*i)!=OP_TESTSET) |
| 3073 |
return 0; |
| 3074 |
if(reg!=((1<<8)-1)&®!=GETARG_B(*i)) |
| 3075 |
SETARG_A(*i,reg); |
| 3076 |
else |
| 3077 |
*i=CREATE_ABC(OP_TEST,GETARG_B(*i),0,GETARG_C(*i)); |
| 3078 |
return 1; |
| 3079 |
} |
| 3080 |
static void removevalues(FuncState*fs,int list){ |
| 3081 |
for(;list!=(-1);list=getjump(fs,list)) |
| 3082 |
patchtestreg(fs,list,((1<<8)-1)); |
| 3083 |
} |
| 3084 |
static void patchlistaux(FuncState*fs,int list,int vtarget,int reg, |
| 3085 |
int dtarget){ |
| 3086 |
while(list!=(-1)){ |
| 3087 |
int next=getjump(fs,list); |
| 3088 |
if(patchtestreg(fs,list,reg)) |
| 3089 |
fixjump(fs,list,vtarget); |
| 3090 |
else |
| 3091 |
fixjump(fs,list,dtarget); |
| 3092 |
list=next; |
| 3093 |
} |
| 3094 |
} |
| 3095 |
static void dischargejpc(FuncState*fs){ |
| 3096 |
patchlistaux(fs,fs->jpc,fs->pc,((1<<8)-1),fs->pc); |
| 3097 |
fs->jpc=(-1); |
| 3098 |
} |
| 3099 |
static void luaK_patchlist(FuncState*fs,int list,int target){ |
| 3100 |
if(target==fs->pc) |
| 3101 |
luaK_patchtohere(fs,list); |
| 3102 |
else{ |
| 3103 |
patchlistaux(fs,list,target,((1<<8)-1),target); |
| 3104 |
} |
| 3105 |
} |
| 3106 |
static void luaK_patchtohere(FuncState*fs,int list){ |
| 3107 |
luaK_getlabel(fs); |
| 3108 |
luaK_concat(fs,&fs->jpc,list); |
| 3109 |
} |
| 3110 |
static void luaK_concat(FuncState*fs,int*l1,int l2){ |
| 3111 |
if(l2==(-1))return; |
| 3112 |
else if(*l1==(-1)) |
| 3113 |
*l1=l2; |
| 3114 |
else{ |
| 3115 |
int list=*l1; |
| 3116 |
int next; |
| 3117 |
while((next=getjump(fs,list))!=(-1)) |
| 3118 |
list=next; |
| 3119 |
fixjump(fs,list,l2); |
| 3120 |
} |
| 3121 |
} |
| 3122 |
static void luaK_checkstack(FuncState*fs,int n){ |
| 3123 |
int newstack=fs->freereg+n; |
| 3124 |
if(newstack>fs->f->maxstacksize){ |
| 3125 |
if(newstack>=250) |
| 3126 |
luaX_syntaxerror(fs->ls,"function or expression too complex"); |
| 3127 |
fs->f->maxstacksize=cast_byte(newstack); |
| 3128 |
} |
| 3129 |
} |
| 3130 |
static void luaK_reserveregs(FuncState*fs,int n){ |
| 3131 |
luaK_checkstack(fs,n); |
| 3132 |
fs->freereg+=n; |
| 3133 |
} |
| 3134 |
static void freereg(FuncState*fs,int reg){ |
| 3135 |
if(!ISK(reg)&®>=fs->nactvar){ |
| 3136 |
fs->freereg--; |
| 3137 |
} |
| 3138 |
} |
| 3139 |
static void freeexp(FuncState*fs,expdesc*e){ |
| 3140 |
if(e->k==VNONRELOC) |
| 3141 |
freereg(fs,e->u.s.info); |
| 3142 |
} |
| 3143 |
static int addk(FuncState*fs,TValue*k,TValue*v){ |
| 3144 |
lua_State*L=fs->L; |
| 3145 |
TValue*idx=luaH_set(L,fs->h,k); |
| 3146 |
Proto*f=fs->f; |
| 3147 |
int oldsize=f->sizek; |
| 3148 |
if(ttisnumber(idx)){ |
| 3149 |
return cast_int(nvalue(idx)); |
| 3150 |
} |
| 3151 |
else{ |
| 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); |
| 3158 |
return fs->nk++; |
| 3159 |
} |
| 3160 |
} |
| 3161 |
static int luaK_stringK(FuncState*fs,TString*s){ |
| 3162 |
TValue o; |
| 3163 |
setsvalue(fs->L,&o,s); |
| 3164 |
return addk(fs,&o,&o); |
| 3165 |
} |
| 3166 |
static int luaK_numberK(FuncState*fs,lua_Number r){ |
| 3167 |
TValue o; |
| 3168 |
setnvalue(&o,r); |
| 3169 |
return addk(fs,&o,&o); |
| 3170 |
} |
| 3171 |
static int boolK(FuncState*fs,int b){ |
| 3172 |
TValue o; |
| 3173 |
setbvalue(&o,b); |
| 3174 |
return addk(fs,&o,&o); |
| 3175 |
} |
| 3176 |
static int nilK(FuncState*fs){ |
| 3177 |
TValue k,v; |
| 3178 |
setnilvalue(&v); |
| 3179 |
sethvalue(fs->L,&k,fs->h); |
| 3180 |
return addk(fs,&k,&v); |
| 3181 |
} |
| 3182 |
static void luaK_setreturns(FuncState*fs,expdesc*e,int nresults){ |
| 3183 |
if(e->k==VCALL){ |
| 3184 |
SETARG_C(getcode(fs,e),nresults+1); |
| 3185 |
} |
| 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); |
| 3190 |
} |
| 3191 |
} |
| 3192 |
static void luaK_setoneret(FuncState*fs,expdesc*e){ |
| 3193 |
if(e->k==VCALL){ |
| 3194 |
e->k=VNONRELOC; |
| 3195 |
e->u.s.info=GETARG_A(getcode(fs,e)); |
| 3196 |
} |
| 3197 |
else if(e->k==VVARARG){ |
| 3198 |
SETARG_B(getcode(fs,e),2); |
| 3199 |
e->k=VRELOCABLE; |
| 3200 |
} |
| 3201 |
} |
| 3202 |
static void luaK_dischargevars(FuncState*fs,expdesc*e){ |
| 3203 |
switch(e->k){ |
| 3204 |
case VLOCAL:{ |
| 3205 |
e->k=VNONRELOC; |
| 3206 |
break; |
| 3207 |
} |
| 3208 |
case VUPVAL:{ |
| 3209 |
e->u.s.info=luaK_codeABC(fs,OP_GETUPVAL,0,e->u.s.info,0); |
| 3210 |
e->k=VRELOCABLE; |
| 3211 |
break; |
| 3212 |
} |
| 3213 |
case VGLOBAL:{ |
| 3214 |
e->u.s.info=luaK_codeABx(fs,OP_GETGLOBAL,0,e->u.s.info); |
| 3215 |
e->k=VRELOCABLE; |
| 3216 |
break; |
| 3217 |
} |
| 3218 |
case VINDEXED:{ |
| 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); |
| 3222 |
e->k=VRELOCABLE; |
| 3223 |
break; |
| 3224 |
} |
| 3225 |
case VVARARG: |
| 3226 |
case VCALL:{ |
| 3227 |
luaK_setoneret(fs,e); |
| 3228 |
break; |
| 3229 |
} |
| 3230 |
default:break; |
| 3231 |
} |
| 3232 |
} |
| 3233 |
static int code_label(FuncState*fs,int A,int b,int jump){ |
| 3234 |
luaK_getlabel(fs); |
| 3235 |
return luaK_codeABC(fs,OP_LOADBOOL,A,b,jump); |
| 3236 |
} |
| 3237 |
static void discharge2reg(FuncState*fs,expdesc*e,int reg){ |
| 3238 |
luaK_dischargevars(fs,e); |
| 3239 |
switch(e->k){ |
| 3240 |
case VNIL:{ |
| 3241 |
luaK_nil(fs,reg,1); |
| 3242 |
break; |
| 3243 |
} |
| 3244 |
case VFALSE:case VTRUE:{ |
| 3245 |
luaK_codeABC(fs,OP_LOADBOOL,reg,e->k==VTRUE,0); |
| 3246 |
break; |
| 3247 |
} |
| 3248 |
case VK:{ |
| 3249 |
luaK_codeABx(fs,OP_LOADK,reg,e->u.s.info); |
| 3250 |
break; |
| 3251 |
} |
| 3252 |
case VKNUM:{ |
| 3253 |
luaK_codeABx(fs,OP_LOADK,reg,luaK_numberK(fs,e->u.nval)); |
| 3254 |
break; |
| 3255 |
} |
| 3256 |
case VRELOCABLE:{ |
| 3257 |
Instruction*pc=&getcode(fs,e); |
| 3258 |
SETARG_A(*pc,reg); |
| 3259 |
break; |
| 3260 |
} |
| 3261 |
case VNONRELOC:{ |
| 3262 |
if(reg!=e->u.s.info) |
| 3263 |
luaK_codeABC(fs,OP_MOVE,reg,e->u.s.info,0); |
| 3264 |
break; |
| 3265 |
} |
| 3266 |
default:{ |
| 3267 |
return; |
| 3268 |
} |
| 3269 |
} |
| 3270 |
e->u.s.info=reg; |
| 3271 |
e->k=VNONRELOC; |
| 3272 |
} |
| 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); |
| 3277 |
} |
| 3278 |
} |
| 3279 |
static void exp2reg(FuncState*fs,expdesc*e,int reg){ |
| 3280 |
discharge2reg(fs,e,reg); |
| 3281 |
if(e->k==VJMP) |
| 3282 |
luaK_concat(fs,&e->t,e->u.s.info); |
| 3283 |
if(hasjumps(e)){ |
| 3284 |
int final; |
| 3285 |
int p_f=(-1); |
| 3286 |
int p_t=(-1); |
| 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); |
| 3292 |
} |
| 3293 |
final=luaK_getlabel(fs); |
| 3294 |
patchlistaux(fs,e->f,final,reg,p_f); |
| 3295 |
patchlistaux(fs,e->t,final,reg,p_t); |
| 3296 |
} |
| 3297 |
e->f=e->t=(-1); |
| 3298 |
e->u.s.info=reg; |
| 3299 |
e->k=VNONRELOC; |
| 3300 |
} |
| 3301 |
static void luaK_exp2nextreg(FuncState*fs,expdesc*e){ |
| 3302 |
luaK_dischargevars(fs,e); |
| 3303 |
freeexp(fs,e); |
| 3304 |
luaK_reserveregs(fs,1); |
| 3305 |
exp2reg(fs,e,fs->freereg-1); |
| 3306 |
} |
| 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); |
| 3313 |
return e->u.s.info; |
| 3314 |
} |
| 3315 |
} |
| 3316 |
luaK_exp2nextreg(fs,e); |
| 3317 |
return e->u.s.info; |
| 3318 |
} |
| 3319 |
static void luaK_exp2val(FuncState*fs,expdesc*e){ |
| 3320 |
if(hasjumps(e)) |
| 3321 |
luaK_exp2anyreg(fs,e); |
| 3322 |
else |
| 3323 |
luaK_dischargevars(fs,e); |
| 3324 |
} |
| 3325 |
static int luaK_exp2RK(FuncState*fs,expdesc*e){ |
| 3326 |
luaK_exp2val(fs,e); |
| 3327 |
switch(e->k){ |
| 3328 |
case VKNUM: |
| 3329 |
case VTRUE: |
| 3330 |
case VFALSE: |
| 3331 |
case VNIL:{ |
| 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)); |
| 3336 |
e->k=VK; |
| 3337 |
return RKASK(e->u.s.info); |
| 3338 |
} |
| 3339 |
else break; |
| 3340 |
} |
| 3341 |
case VK:{ |
| 3342 |
if(e->u.s.info<=((1<<(9-1))-1)) |
| 3343 |
return RKASK(e->u.s.info); |
| 3344 |
else break; |
| 3345 |
} |
| 3346 |
default:break; |
| 3347 |
} |
| 3348 |
return luaK_exp2anyreg(fs,e); |
| 3349 |
} |
| 3350 |
static void luaK_storevar(FuncState*fs,expdesc*var,expdesc*ex){ |
| 3351 |
switch(var->k){ |
| 3352 |
case VLOCAL:{ |
| 3353 |
freeexp(fs,ex); |
| 3354 |
exp2reg(fs,ex,var->u.s.info); |
| 3355 |
return; |
| 3356 |
} |
| 3357 |
case VUPVAL:{ |
| 3358 |
int e=luaK_exp2anyreg(fs,ex); |
| 3359 |
luaK_codeABC(fs,OP_SETUPVAL,e,var->u.s.info,0); |
| 3360 |
break; |
| 3361 |
} |
| 3362 |
case VGLOBAL:{ |
| 3363 |
int e=luaK_exp2anyreg(fs,ex); |
| 3364 |
luaK_codeABx(fs,OP_SETGLOBAL,e,var->u.s.info); |
| 3365 |
break; |
| 3366 |
} |
| 3367 |
case VINDEXED:{ |
| 3368 |
int e=luaK_exp2RK(fs,ex); |
| 3369 |
luaK_codeABC(fs,OP_SETTABLE,var->u.s.info,var->u.s.aux,e); |
| 3370 |
break; |
| 3371 |
} |
| 3372 |
default:{ |
| 3373 |
break; |
| 3374 |
} |
| 3375 |
} |
| 3376 |
freeexp(fs,ex); |
| 3377 |
} |
| 3378 |
static void luaK_self(FuncState*fs,expdesc*e,expdesc*key){ |
| 3379 |
int func; |
| 3380 |
luaK_exp2anyreg(fs,e); |
| 3381 |
freeexp(fs,e); |
| 3382 |
func=fs->freereg; |
| 3383 |
luaK_reserveregs(fs,2); |
| 3384 |
luaK_codeABC(fs,OP_SELF,func,e->u.s.info,luaK_exp2RK(fs,key)); |
| 3385 |
freeexp(fs,key); |
| 3386 |
e->u.s.info=func; |
| 3387 |
e->k=VNONRELOC; |
| 3388 |
} |
| 3389 |
static void invertjump(FuncState*fs,expdesc*e){ |
| 3390 |
Instruction*pc=getjumpcontrol(fs,e->u.s.info); |
| 3391 |
SETARG_A(*pc,!(GETARG_A(*pc))); |
| 3392 |
} |
| 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){ |
| 3397 |
fs->pc--; |
| 3398 |
return condjump(fs,OP_TEST,GETARG_B(ie),0,!cond); |
| 3399 |
} |
| 3400 |
} |
| 3401 |
discharge2anyreg(fs,e); |
| 3402 |
freeexp(fs,e); |
| 3403 |
return condjump(fs,OP_TESTSET,((1<<8)-1),e->u.s.info,cond); |
| 3404 |
} |
| 3405 |
static void luaK_goiftrue(FuncState*fs,expdesc*e){ |
| 3406 |
int pc; |
| 3407 |
luaK_dischargevars(fs,e); |
| 3408 |
switch(e->k){ |
| 3409 |
case VK:case VKNUM:case VTRUE:{ |
| 3410 |
pc=(-1); |
| 3411 |
break; |
| 3412 |
} |
| 3413 |
case VJMP:{ |
| 3414 |
invertjump(fs,e); |
| 3415 |
pc=e->u.s.info; |
| 3416 |
break; |
| 3417 |
} |
| 3418 |
default:{ |
| 3419 |
pc=jumponcond(fs,e,0); |
| 3420 |
break; |
| 3421 |
} |
| 3422 |
} |
| 3423 |
luaK_concat(fs,&e->f,pc); |
| 3424 |
luaK_patchtohere(fs,e->t); |
| 3425 |
e->t=(-1); |
| 3426 |
} |
| 3427 |
static void luaK_goiffalse(FuncState*fs,expdesc*e){ |
| 3428 |
int pc; |
| 3429 |
luaK_dischargevars(fs,e); |
| 3430 |
switch(e->k){ |
| 3431 |
case VNIL:case VFALSE:{ |
| 3432 |
pc=(-1); |
| 3433 |
break; |
| 3434 |
} |
| 3435 |
case VJMP:{ |
| 3436 |
pc=e->u.s.info; |
| 3437 |
break; |
| 3438 |
} |
| 3439 |
default:{ |
| 3440 |
pc=jumponcond(fs,e,1); |
| 3441 |
break; |
| 3442 |
} |
| 3443 |
} |
| 3444 |
luaK_concat(fs,&e->t,pc); |
| 3445 |
luaK_patchtohere(fs,e->f); |
| 3446 |
e->f=(-1); |
| 3447 |
} |
| 3448 |
static void codenot(FuncState*fs,expdesc*e){ |
| 3449 |
luaK_dischargevars(fs,e); |
| 3450 |
switch(e->k){ |
| 3451 |
case VNIL:case VFALSE:{ |
| 3452 |
e->k=VTRUE; |
| 3453 |
break; |
| 3454 |
} |
| 3455 |
case VK:case VKNUM:case VTRUE:{ |
| 3456 |
e->k=VFALSE; |
| 3457 |
break; |
| 3458 |
} |
| 3459 |
case VJMP:{ |
| 3460 |
invertjump(fs,e); |
| 3461 |
break; |
| 3462 |
} |
| 3463 |
case VRELOCABLE: |
| 3464 |
case VNONRELOC:{ |
| 3465 |
discharge2anyreg(fs,e); |
| 3466 |
freeexp(fs,e); |
| 3467 |
e->u.s.info=luaK_codeABC(fs,OP_NOT,0,e->u.s.info,0); |
| 3468 |
e->k=VRELOCABLE; |
| 3469 |
break; |
| 3470 |
} |
| 3471 |
default:{ |
| 3472 |
break; |
| 3473 |
} |
| 3474 |
} |
| 3475 |
{int temp=e->f;e->f=e->t;e->t=temp;} |
| 3476 |
removevalues(fs,e->f); |
| 3477 |
removevalues(fs,e->t); |
| 3478 |
} |
| 3479 |
static void luaK_indexed(FuncState*fs,expdesc*t,expdesc*k){ |
| 3480 |
t->u.s.aux=luaK_exp2RK(fs,k); |
| 3481 |
t->k=VINDEXED; |
| 3482 |
} |
| 3483 |
static int constfolding(OpCode op,expdesc*e1,expdesc*e2){ |
| 3484 |
lua_Number v1,v2,r; |
| 3485 |
if(!isnumeral(e1)||!isnumeral(e2))return 0; |
| 3486 |
v1=e1->u.nval; |
| 3487 |
v2=e2->u.nval; |
| 3488 |
switch(op){ |
| 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; |
| 3492 |
case OP_DIV: |
| 3493 |
if(v2==0)return 0; |
| 3494 |
r=luai_numdiv(v1,v2);break; |
| 3495 |
case OP_MOD: |
| 3496 |
if(v2==0)return 0; |
| 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; |
| 3501 |
default:r=0;break; |
| 3502 |
} |
| 3503 |
if(luai_numisnan(r))return 0; |
| 3504 |
e1->u.nval=r; |
| 3505 |
return 1; |
| 3506 |
} |
| 3507 |
static void codearith(FuncState*fs,OpCode op,expdesc*e1,expdesc*e2){ |
| 3508 |
if(constfolding(op,e1,e2)) |
| 3509 |
return; |
| 3510 |
else{ |
| 3511 |
int o2=(op!=OP_UNM&&op!=OP_LEN)?luaK_exp2RK(fs,e2):0; |
| 3512 |
int o1=luaK_exp2RK(fs,e1); |
| 3513 |
if(o1>o2){ |
| 3514 |
freeexp(fs,e1); |
| 3515 |
freeexp(fs,e2); |
| 3516 |
} |
| 3517 |
else{ |
| 3518 |
freeexp(fs,e2); |
| 3519 |
freeexp(fs,e1); |
| 3520 |
} |
| 3521 |
e1->u.s.info=luaK_codeABC(fs,op,0,o1,o2); |
| 3522 |
e1->k=VRELOCABLE; |
| 3523 |
} |
| 3524 |
} |
| 3525 |
static void codecomp(FuncState*fs,OpCode op,int cond,expdesc*e1, |
| 3526 |
expdesc*e2){ |
| 3527 |
int o1=luaK_exp2RK(fs,e1); |
| 3528 |
int o2=luaK_exp2RK(fs,e2); |
| 3529 |
freeexp(fs,e2); |
| 3530 |
freeexp(fs,e1); |
| 3531 |
if(cond==0&&op!=OP_EQ){ |
| 3532 |
int temp; |
| 3533 |
temp=o1;o1=o2;o2=temp; |
| 3534 |
cond=1; |
| 3535 |
} |
| 3536 |
e1->u.s.info=condjump(fs,op,cond,o1,o2); |
| 3537 |
e1->k=VJMP; |
| 3538 |
} |
| 3539 |
static void luaK_prefix(FuncState*fs,UnOpr op,expdesc*e){ |
| 3540 |
expdesc e2; |
| 3541 |
e2.t=e2.f=(-1);e2.k=VKNUM;e2.u.nval=0; |
| 3542 |
switch(op){ |
| 3543 |
case OPR_MINUS:{ |
| 3544 |
if(!isnumeral(e)) |
| 3545 |
luaK_exp2anyreg(fs,e); |
| 3546 |
codearith(fs,OP_UNM,e,&e2); |
| 3547 |
break; |
| 3548 |
} |
| 3549 |
case OPR_NOT:codenot(fs,e);break; |
| 3550 |
case OPR_LEN:{ |
| 3551 |
luaK_exp2anyreg(fs,e); |
| 3552 |
codearith(fs,OP_LEN,e,&e2); |
| 3553 |
break; |
| 3554 |
} |
| 3555 |
default:; |
| 3556 |
} |
| 3557 |
} |
| 3558 |
static void luaK_infix(FuncState*fs,BinOpr op,expdesc*v){ |
| 3559 |
switch(op){ |
| 3560 |
case OPR_AND:{ |
| 3561 |
luaK_goiftrue(fs,v); |
| 3562 |
break; |
| 3563 |
} |
| 3564 |
case OPR_OR:{ |
| 3565 |
luaK_goiffalse(fs,v); |
| 3566 |
break; |
| 3567 |
} |
| 3568 |
case OPR_CONCAT:{ |
| 3569 |
luaK_exp2nextreg(fs,v); |
| 3570 |
break; |
| 3571 |
} |
| 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); |
| 3575 |
break; |
| 3576 |
} |
| 3577 |
default:{ |
| 3578 |
luaK_exp2RK(fs,v); |
| 3579 |
break; |
| 3580 |
} |
| 3581 |
} |
| 3582 |
} |
| 3583 |
static void luaK_posfix(FuncState*fs,BinOpr op,expdesc*e1,expdesc*e2){ |
| 3584 |
switch(op){ |
| 3585 |
case OPR_AND:{ |
| 3586 |
luaK_dischargevars(fs,e2); |
| 3587 |
luaK_concat(fs,&e2->f,e1->f); |
| 3588 |
*e1=*e2; |
| 3589 |
break; |
| 3590 |
} |
| 3591 |
case OPR_OR:{ |
| 3592 |
luaK_dischargevars(fs,e2); |
| 3593 |
luaK_concat(fs,&e2->t,e1->t); |
| 3594 |
*e1=*e2; |
| 3595 |
break; |
| 3596 |
} |
| 3597 |
case OPR_CONCAT:{ |
| 3598 |
luaK_exp2val(fs,e2); |
| 3599 |
if(e2->k==VRELOCABLE&&GET_OPCODE(getcode(fs,e2))==OP_CONCAT){ |
| 3600 |
freeexp(fs,e1); |
| 3601 |
SETARG_B(getcode(fs,e2),e1->u.s.info); |
| 3602 |
e1->k=VRELOCABLE;e1->u.s.info=e2->u.s.info; |
| 3603 |
} |
| 3604 |
else{ |
| 3605 |
luaK_exp2nextreg(fs,e2); |
| 3606 |
codearith(fs,OP_CONCAT,e1,e2); |
| 3607 |
} |
| 3608 |
break; |
| 3609 |
} |
| 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; |
| 3622 |
default:; |
| 3623 |
} |
| 3624 |
} |
| 3625 |
static void luaK_fixline(FuncState*fs,int line){ |
| 3626 |
fs->f->lineinfo[fs->pc-1]=line; |
| 3627 |
} |
| 3628 |
static int luaK_code(FuncState*fs,Instruction i,int line){ |
| 3629 |
Proto*f=fs->f; |
| 3630 |
dischargejpc(fs); |
| 3631 |
luaM_growvector(fs->L,f->code,fs->pc,f->sizecode,Instruction, |
| 3632 |
(INT_MAX-2),"code size overflow"); |
| 3633 |
f->code[fs->pc]=i; |
| 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; |
| 3637 |
return fs->pc++; |
| 3638 |
} |
| 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); |
| 3641 |
} |
| 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); |
| 3644 |
} |
| 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; |
| 3648 |
if(c<=((1<<9)-1)) |
| 3649 |
luaK_codeABC(fs,OP_SETLIST,base,b,c); |
| 3650 |
else{ |
| 3651 |
luaK_codeABC(fs,OP_SETLIST,base,b,0); |
| 3652 |
luaK_code(fs,cast(Instruction,c),fs->ls->lastline); |
| 3653 |
} |
| 3654 |
fs->freereg=base+1; |
| 3655 |
} |
| 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; |
| 3661 |
int breaklist; |
| 3662 |
lu_byte nactvar; |
| 3663 |
lu_byte upval; |
| 3664 |
lu_byte isbreakable; |
| 3665 |
}BlockCnt; |
| 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); |
| 3672 |
} |
| 3673 |
} |
| 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))); |
| 3677 |
} |
| 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); |
| 3684 |
} |
| 3685 |
static int testnext(LexState*ls,int c){ |
| 3686 |
if(ls->t.token==c){ |
| 3687 |
luaX_next(ls); |
| 3688 |
return 1; |
| 3689 |
} |
| 3690 |
else return 0; |
| 3691 |
} |
| 3692 |
static void check(LexState*ls,int c){ |
| 3693 |
if(ls->t.token!=c) |
| 3694 |
error_expected(ls,c); |
| 3695 |
} |
| 3696 |
static void checknext(LexState*ls,int c){ |
| 3697 |
check(ls,c); |
| 3698 |
luaX_next(ls); |
| 3699 |
} |
| 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); |
| 3705 |
else{ |
| 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)); |
| 3709 |
} |
| 3710 |
} |
| 3711 |
} |
| 3712 |
static TString*str_checkname(LexState*ls){ |
| 3713 |
TString*ts; |
| 3714 |
check(ls,TK_NAME); |
| 3715 |
ts=ls->t.seminfo.ts; |
| 3716 |
luaX_next(ls); |
| 3717 |
return ts; |
| 3718 |
} |
| 3719 |
static void init_exp(expdesc*e,expkind k,int i){ |
| 3720 |
e->f=e->t=(-1); |
| 3721 |
e->k=k; |
| 3722 |
e->u.s.info=i; |
| 3723 |
} |
| 3724 |
static void codestring(LexState*ls,expdesc*e,TString*s){ |
| 3725 |
init_exp(e,VK,luaK_stringK(ls->fs,s)); |
| 3726 |
} |
| 3727 |
static void checkname(LexState*ls,expdesc*e){ |
| 3728 |
codestring(ls,e,str_checkname(ls)); |
| 3729 |
} |
| 3730 |
static int registerlocalvar(LexState*ls,TString*varname){ |
| 3731 |
FuncState*fs=ls->fs; |
| 3732 |
Proto*f=fs->f; |
| 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++; |
| 3740 |
} |
| 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)); |
| 3746 |
} |
| 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; |
| 3752 |
} |
| 3753 |
} |
| 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; |
| 3758 |
} |
| 3759 |
static int indexupvalue(FuncState*fs,TString*name,expdesc*v){ |
| 3760 |
int i; |
| 3761 |
Proto*f=fs->f; |
| 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){ |
| 3765 |
return i; |
| 3766 |
} |
| 3767 |
} |
| 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); |
| 3776 |
return f->nups++; |
| 3777 |
} |
| 3778 |
static int searchvar(FuncState*fs,TString*n){ |
| 3779 |
int i; |
| 3780 |
for(i=fs->nactvar-1;i>=0;i--){ |
| 3781 |
if(n==getlocvar(fs,i).varname) |
| 3782 |
return i; |
| 3783 |
} |
| 3784 |
return-1; |
| 3785 |
} |
| 3786 |
static void markupval(FuncState*fs,int level){ |
| 3787 |
BlockCnt*bl=fs->bl; |
| 3788 |
while(bl&&bl->nactvar>level)bl=bl->previous; |
| 3789 |
if(bl)bl->upval=1; |
| 3790 |
} |
| 3791 |
static int singlevaraux(FuncState*fs,TString*n,expdesc*var,int base){ |
| 3792 |
if(fs==NULL){ |
| 3793 |
init_exp(var,VGLOBAL,((1<<8)-1)); |
| 3794 |
return VGLOBAL; |
| 3795 |
} |
| 3796 |
else{ |
| 3797 |
int v=searchvar(fs,n); |
| 3798 |
if(v>=0){ |
| 3799 |
init_exp(var,VLOCAL,v); |
| 3800 |
if(!base) |
| 3801 |
markupval(fs,v); |
| 3802 |
return VLOCAL; |
| 3803 |
} |
| 3804 |
else{ |
| 3805 |
if(singlevaraux(fs->prev,n,var,0)==VGLOBAL) |
| 3806 |
return VGLOBAL; |
| 3807 |
var->u.s.info=indexupvalue(fs,n,var); |
| 3808 |
var->k=VUPVAL; |
| 3809 |
return VUPVAL; |
| 3810 |
} |
| 3811 |
} |
| 3812 |
} |
| 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); |
| 3818 |
} |
| 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)){ |
| 3823 |
extra++; |
| 3824 |
if(extra<0)extra=0; |
| 3825 |
luaK_setreturns(fs,e,extra); |
| 3826 |
if(extra>1)luaK_reserveregs(fs,extra-1); |
| 3827 |
} |
| 3828 |
else{ |
| 3829 |
if(e->k!=VVOID)luaK_exp2nextreg(fs,e); |
| 3830 |
if(extra>0){ |
| 3831 |
int reg=fs->freereg; |
| 3832 |
luaK_reserveregs(fs,extra); |
| 3833 |
luaK_nil(fs,reg,extra); |
| 3834 |
} |
| 3835 |
} |
| 3836 |
} |
| 3837 |
static void enterlevel(LexState*ls){ |
| 3838 |
if(++ls->L->nCcalls>200) |
| 3839 |
luaX_lexerror(ls,"chunk has too many syntax levels",0); |
| 3840 |
} |
| 3841 |
#define leavelevel(ls)((ls)->L->nCcalls--) |
| 3842 |
static void enterblock(FuncState*fs,BlockCnt*bl,lu_byte isbreakable){ |
| 3843 |
bl->breaklist=(-1); |
| 3844 |
bl->isbreakable=isbreakable; |
| 3845 |
bl->nactvar=fs->nactvar; |
| 3846 |
bl->upval=0; |
| 3847 |
bl->previous=fs->bl; |
| 3848 |
fs->bl=bl; |
| 3849 |
} |
| 3850 |
static void leaveblock(FuncState*fs){ |
| 3851 |
BlockCnt*bl=fs->bl; |
| 3852 |
fs->bl=bl->previous; |
| 3853 |
removevars(fs->ls,bl->nactvar); |
| 3854 |
if(bl->upval) |
| 3855 |
luaK_codeABC(fs,OP_CLOSE,bl->nactvar,0,0); |
| 3856 |
fs->freereg=fs->nactvar; |
| 3857 |
luaK_patchtohere(fs,bl->breaklist); |
| 3858 |
} |
| 3859 |
static void pushclosure(LexState*ls,FuncState*func,expdesc*v){ |
| 3860 |
FuncState*fs=ls->fs; |
| 3861 |
Proto*f=fs->f; |
| 3862 |
int oldsize=f->sizep; |
| 3863 |
int i; |
| 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); |
| 3873 |
} |
| 3874 |
} |
| 3875 |
static void open_func(LexState*ls,FuncState*fs){ |
| 3876 |
lua_State*L=ls->L; |
| 3877 |
Proto*f=luaF_newproto(L); |
| 3878 |
fs->f=f; |
| 3879 |
fs->prev=ls->fs; |
| 3880 |
fs->ls=ls; |
| 3881 |
fs->L=L; |
| 3882 |
ls->fs=fs; |
| 3883 |
fs->pc=0; |
| 3884 |
fs->lasttarget=-1; |
| 3885 |
fs->jpc=(-1); |
| 3886 |
fs->freereg=0; |
| 3887 |
fs->nk=0; |
| 3888 |
fs->np=0; |
| 3889 |
fs->nlocvars=0; |
| 3890 |
fs->nactvar=0; |
| 3891 |
fs->bl=NULL; |
| 3892 |
f->source=ls->source; |
| 3893 |
f->maxstacksize=2; |
| 3894 |
fs->h=luaH_new(L,0,0); |
| 3895 |
sethvalue(L,L->top,fs->h); |
| 3896 |
incr_top(L); |
| 3897 |
setptvalue(L,L->top,f); |
| 3898 |
incr_top(L); |
| 3899 |
} |
| 3900 |
static void close_func(LexState*ls){ |
| 3901 |
lua_State*L=ls->L; |
| 3902 |
FuncState*fs=ls->fs; |
| 3903 |
Proto*f=fs->f; |
| 3904 |
removevars(ls,0); |
| 3905 |
luaK_ret(fs,0,0); |
| 3906 |
luaM_reallocvector(L,f->code,f->sizecode,fs->pc,Instruction); |
| 3907 |
f->sizecode=fs->pc; |
| 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); |
| 3911 |
f->sizek=fs->nk; |
| 3912 |
luaM_reallocvector(L,f->p,f->sizep,fs->np,Proto*); |
| 3913 |
f->sizep=fs->np; |
| 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; |
| 3918 |
ls->fs=fs->prev; |
| 3919 |
if(fs)anchor_token(ls); |
| 3920 |
L->top-=2; |
| 3921 |
} |
| 3922 |
static Proto*luaY_parser(lua_State*L,ZIO*z,Mbuffer*buff,const char*name){ |
| 3923 |
struct LexState lexstate; |
| 3924 |
struct FuncState funcstate; |
| 3925 |
lexstate.buff=buff; |
| 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); |
| 3930 |
chunk(&lexstate); |
| 3931 |
check(&lexstate,TK_EOS); |
| 3932 |
close_func(&lexstate); |
| 3933 |
return funcstate.f; |
| 3934 |
} |
| 3935 |
static void field(LexState*ls,expdesc*v){ |
| 3936 |
FuncState*fs=ls->fs; |
| 3937 |
expdesc key; |
| 3938 |
luaK_exp2anyreg(fs,v); |
| 3939 |
luaX_next(ls); |
| 3940 |
checkname(ls,&key); |
| 3941 |
luaK_indexed(fs,v,&key); |
| 3942 |
} |
| 3943 |
static void yindex(LexState*ls,expdesc*v){ |
| 3944 |
luaX_next(ls); |
| 3945 |
expr(ls,v); |
| 3946 |
luaK_exp2val(ls->fs,v); |
| 3947 |
checknext(ls,']'); |
| 3948 |
} |
| 3949 |
struct ConsControl{ |
| 3950 |
expdesc v; |
| 3951 |
expdesc*t; |
| 3952 |
int nh; |
| 3953 |
int na; |
| 3954 |
int tostore; |
| 3955 |
}; |
| 3956 |
static void recfield(LexState*ls,struct ConsControl*cc){ |
| 3957 |
FuncState*fs=ls->fs; |
| 3958 |
int reg=ls->fs->freereg; |
| 3959 |
expdesc key,val; |
| 3960 |
int rkkey; |
| 3961 |
if(ls->t.token==TK_NAME){ |
| 3962 |
luaY_checklimit(fs,cc->nh,(INT_MAX-2),"items in a constructor"); |
| 3963 |
checkname(ls,&key); |
| 3964 |
} |
| 3965 |
else |
| 3966 |
yindex(ls,&key); |
| 3967 |
cc->nh++; |
| 3968 |
checknext(ls,'='); |
| 3969 |
rkkey=luaK_exp2RK(fs,&key); |
| 3970 |
expr(ls,&val); |
| 3971 |
luaK_codeABC(fs,OP_SETTABLE,cc->t->u.s.info,rkkey,luaK_exp2RK(fs,&val)); |
| 3972 |
fs->freereg=reg; |
| 3973 |
} |
| 3974 |
static void closelistfield(FuncState*fs,struct ConsControl*cc){ |
| 3975 |
if(cc->v.k==VVOID)return; |
| 3976 |
luaK_exp2nextreg(fs,&cc->v); |
| 3977 |
cc->v.k=VVOID; |
| 3978 |
if(cc->tostore==50){ |
| 3979 |
luaK_setlist(fs,cc->t->u.s.info,cc->na,cc->tostore); |
| 3980 |
cc->tostore=0; |
| 3981 |
} |
| 3982 |
} |
| 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)); |
| 3988 |
cc->na--; |
| 3989 |
} |
| 3990 |
else{ |
| 3991 |
if(cc->v.k!=VVOID) |
| 3992 |
luaK_exp2nextreg(fs,&cc->v); |
| 3993 |
luaK_setlist(fs,cc->t->u.s.info,cc->na,cc->tostore); |
| 3994 |
} |
| 3995 |
} |
| 3996 |
static void listfield(LexState*ls,struct ConsControl*cc){ |
| 3997 |
expr(ls,&cc->v); |
| 3998 |
luaY_checklimit(ls->fs,cc->na,(INT_MAX-2),"items in a constructor"); |
| 3999 |
cc->na++; |
| 4000 |
cc->tostore++; |
| 4001 |
} |
| 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; |
| 4008 |
cc.t=t; |
| 4009 |
init_exp(t,VRELOCABLE,pc); |
| 4010 |
init_exp(&cc.v,VVOID,0); |
| 4011 |
luaK_exp2nextreg(ls->fs,t); |
| 4012 |
checknext(ls,'{'); |
| 4013 |
do{ |
| 4014 |
if(ls->t.token=='}')break; |
| 4015 |
closelistfield(fs,&cc); |
| 4016 |
switch(ls->t.token){ |
| 4017 |
case TK_NAME:{ |
| 4018 |
luaX_lookahead(ls); |
| 4019 |
if(ls->lookahead.token!='=') |
| 4020 |
listfield(ls,&cc); |
| 4021 |
else |
| 4022 |
recfield(ls,&cc); |
| 4023 |
break; |
| 4024 |
} |
| 4025 |
case'[':{ |
| 4026 |
recfield(ls,&cc); |
| 4027 |
break; |
| 4028 |
} |
| 4029 |
default:{ |
| 4030 |
listfield(ls,&cc); |
| 4031 |
break; |
| 4032 |
} |
| 4033 |
} |
| 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)); |
| 4039 |
} |
| 4040 |
static void parlist(LexState*ls){ |
| 4041 |
FuncState*fs=ls->fs; |
| 4042 |
Proto*f=fs->f; |
| 4043 |
int nparams=0; |
| 4044 |
f->is_vararg=0; |
| 4045 |
if(ls->t.token!=')'){ |
| 4046 |
do{ |
| 4047 |
switch(ls->t.token){ |
| 4048 |
case TK_NAME:{ |
| 4049 |
new_localvar(ls,str_checkname(ls),nparams++); |
| 4050 |
break; |
| 4051 |
} |
| 4052 |
case TK_DOTS:{ |
| 4053 |
luaX_next(ls); |
| 4054 |
f->is_vararg|=2; |
| 4055 |
break; |
| 4056 |
} |
| 4057 |
default:luaX_syntaxerror(ls,"<name> or "LUA_QL("...")" expected"); |
| 4058 |
} |
| 4059 |
}while(!f->is_vararg&&testnext(ls,',')); |
| 4060 |
} |
| 4061 |
adjustlocalvars(ls,nparams); |
| 4062 |
f->numparams=cast_byte(fs->nactvar-(f->is_vararg&1)); |
| 4063 |
luaK_reserveregs(fs,fs->nactvar); |
| 4064 |
} |
| 4065 |
static void body(LexState*ls,expdesc*e,int needself,int line){ |
| 4066 |
FuncState new_fs; |
| 4067 |
open_func(ls,&new_fs); |
| 4068 |
new_fs.f->linedefined=line; |
| 4069 |
checknext(ls,'('); |
| 4070 |
if(needself){ |
| 4071 |
new_localvarliteral(ls,"self",0); |
| 4072 |
adjustlocalvars(ls,1); |
| 4073 |
} |
| 4074 |
parlist(ls); |
| 4075 |
checknext(ls,')'); |
| 4076 |
chunk(ls); |
| 4077 |
new_fs.f->lastlinedefined=ls->linenumber; |
| 4078 |
check_match(ls,TK_END,TK_FUNCTION,line); |
| 4079 |
close_func(ls); |
| 4080 |
pushclosure(ls,&new_fs,e); |
| 4081 |
} |
| 4082 |
static int explist1(LexState*ls,expdesc*v){ |
| 4083 |
int n=1; |
| 4084 |
expr(ls,v); |
| 4085 |
while(testnext(ls,',')){ |
| 4086 |
luaK_exp2nextreg(ls->fs,v); |
| 4087 |
expr(ls,v); |
| 4088 |
n++; |
| 4089 |
} |
| 4090 |
return n; |
| 4091 |
} |
| 4092 |
static void funcargs(LexState*ls,expdesc*f){ |
| 4093 |
FuncState*fs=ls->fs; |
| 4094 |
expdesc args; |
| 4095 |
int base,nparams; |
| 4096 |
int line=ls->linenumber; |
| 4097 |
switch(ls->t.token){ |
| 4098 |
case'(':{ |
| 4099 |
if(line!=ls->lastline) |
| 4100 |
luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)"); |
| 4101 |
luaX_next(ls); |
| 4102 |
if(ls->t.token==')') |
| 4103 |
args.k=VVOID; |
| 4104 |
else{ |
| 4105 |
explist1(ls,&args); |
| 4106 |
luaK_setmultret(fs,&args); |
| 4107 |
} |
| 4108 |
check_match(ls,')','(',line); |
| 4109 |
break; |
| 4110 |
} |
| 4111 |
case'{':{ |
| 4112 |
constructor(ls,&args); |
| 4113 |
break; |
| 4114 |
} |
| 4115 |
case TK_STRING:{ |
| 4116 |
codestring(ls,&args,ls->t.seminfo.ts); |
| 4117 |
luaX_next(ls); |
| 4118 |
break; |
| 4119 |
} |
| 4120 |
default:{ |
| 4121 |
luaX_syntaxerror(ls,"function arguments expected"); |
| 4122 |
return; |
| 4123 |
} |
| 4124 |
} |
| 4125 |
base=f->u.s.info; |
| 4126 |
if(hasmultret(args.k)) |
| 4127 |
nparams=(-1); |
| 4128 |
else{ |
| 4129 |
if(args.k!=VVOID) |
| 4130 |
luaK_exp2nextreg(fs,&args); |
| 4131 |
nparams=fs->freereg-(base+1); |
| 4132 |
} |
| 4133 |
init_exp(f,VCALL,luaK_codeABC(fs,OP_CALL,base,nparams+1,2)); |
| 4134 |
luaK_fixline(fs,line); |
| 4135 |
fs->freereg=base+1; |
| 4136 |
} |
| 4137 |
static void prefixexp(LexState*ls,expdesc*v){ |
| 4138 |
switch(ls->t.token){ |
| 4139 |
case'(':{ |
| 4140 |
int line=ls->linenumber; |
| 4141 |
luaX_next(ls); |
| 4142 |
expr(ls,v); |
| 4143 |
check_match(ls,')','(',line); |
| 4144 |
luaK_dischargevars(ls->fs,v); |
| 4145 |
return; |
| 4146 |
} |
| 4147 |
case TK_NAME:{ |
| 4148 |
singlevar(ls,v); |
| 4149 |
return; |
| 4150 |
} |
| 4151 |
default:{ |
| 4152 |
luaX_syntaxerror(ls,"unexpected symbol"); |
| 4153 |
return; |
| 4154 |
} |
| 4155 |
} |
| 4156 |
} |
| 4157 |
static void primaryexp(LexState*ls,expdesc*v){ |
| 4158 |
FuncState*fs=ls->fs; |
| 4159 |
prefixexp(ls,v); |
| 4160 |
for(;;){ |
| 4161 |
switch(ls->t.token){ |
| 4162 |
case'.':{ |
| 4163 |
field(ls,v); |
| 4164 |
break; |
| 4165 |
} |
| 4166 |
case'[':{ |
| 4167 |
expdesc key; |
| 4168 |
luaK_exp2anyreg(fs,v); |
| 4169 |
yindex(ls,&key); |
| 4170 |
luaK_indexed(fs,v,&key); |
| 4171 |
break; |
| 4172 |
} |
| 4173 |
case':':{ |
| 4174 |
expdesc key; |
| 4175 |
luaX_next(ls); |
| 4176 |
checkname(ls,&key); |
| 4177 |
luaK_self(fs,v,&key); |
| 4178 |
funcargs(ls,v); |
| 4179 |
break; |
| 4180 |
} |
| 4181 |
case'(':case TK_STRING:case'{':{ |
| 4182 |
luaK_exp2nextreg(fs,v); |
| 4183 |
funcargs(ls,v); |
| 4184 |
break; |
| 4185 |
} |
| 4186 |
default:return; |
| 4187 |
} |
| 4188 |
} |
| 4189 |
} |
| 4190 |
static void simpleexp(LexState*ls,expdesc*v){ |
| 4191 |
switch(ls->t.token){ |
| 4192 |
case TK_NUMBER:{ |
| 4193 |
init_exp(v,VKNUM,0); |
| 4194 |
v->u.nval=ls->t.seminfo.r; |
| 4195 |
break; |
| 4196 |
} |
| 4197 |
case TK_STRING:{ |
| 4198 |
codestring(ls,v,ls->t.seminfo.ts); |
| 4199 |
break; |
| 4200 |
} |
| 4201 |
case TK_NIL:{ |
| 4202 |
init_exp(v,VNIL,0); |
| 4203 |
break; |
| 4204 |
} |
| 4205 |
case TK_TRUE:{ |
| 4206 |
init_exp(v,VTRUE,0); |
| 4207 |
break; |
| 4208 |
} |
| 4209 |
case TK_FALSE:{ |
| 4210 |
init_exp(v,VFALSE,0); |
| 4211 |
break; |
| 4212 |
} |
| 4213 |
case TK_DOTS:{ |
| 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)); |
| 4219 |
break; |
| 4220 |
} |
| 4221 |
case'{':{ |
| 4222 |
constructor(ls,v); |
| 4223 |
return; |
| 4224 |
} |
| 4225 |
case TK_FUNCTION:{ |
| 4226 |
luaX_next(ls); |
| 4227 |
body(ls,v,0,ls->linenumber); |
| 4228 |
return; |
| 4229 |
} |
| 4230 |
default:{ |
| 4231 |
primaryexp(ls,v); |
| 4232 |
return; |
| 4233 |
} |
| 4234 |
} |
| 4235 |
luaX_next(ls); |
| 4236 |
} |
| 4237 |
static UnOpr getunopr(int op){ |
| 4238 |
switch(op){ |
| 4239 |
case TK_NOT:return OPR_NOT; |
| 4240 |
case'-':return OPR_MINUS; |
| 4241 |
case'#':return OPR_LEN; |
| 4242 |
default:return OPR_NOUNOPR; |
| 4243 |
} |
| 4244 |
} |
| 4245 |
static BinOpr getbinopr(int op){ |
| 4246 |
switch(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; |
| 4263 |
} |
| 4264 |
} |
| 4265 |
static const struct{ |
| 4266 |
lu_byte left; |
| 4267 |
lu_byte right; |
| 4268 |
}priority[]={ |
| 4269 |
{6,6},{6,6},{7,7},{7,7},{7,7}, |
| 4270 |
{10,9},{5,4}, |
| 4271 |
{3,3},{3,3}, |
| 4272 |
{3,3},{3,3},{3,3},{3,3}, |
| 4273 |
{2,2},{1,1} |
| 4274 |
}; |
| 4275 |
static BinOpr subexpr(LexState*ls,expdesc*v,unsigned int limit){ |
| 4276 |
BinOpr op; |
| 4277 |
UnOpr uop; |
| 4278 |
enterlevel(ls); |
| 4279 |
uop=getunopr(ls->t.token); |
| 4280 |
if(uop!=OPR_NOUNOPR){ |
| 4281 |
luaX_next(ls); |
| 4282 |
subexpr(ls,v,8); |
| 4283 |
luaK_prefix(ls->fs,uop,v); |
| 4284 |
} |
| 4285 |
else simpleexp(ls,v); |
| 4286 |
op=getbinopr(ls->t.token); |
| 4287 |
while(op!=OPR_NOBINOPR&&priority[op].left>limit){ |
| 4288 |
expdesc v2; |
| 4289 |
BinOpr nextop; |
| 4290 |
luaX_next(ls); |
| 4291 |
luaK_infix(ls->fs,op,v); |
| 4292 |
nextop=subexpr(ls,&v2,priority[op].right); |
| 4293 |
luaK_posfix(ls->fs,op,v,&v2); |
| 4294 |
op=nextop; |
| 4295 |
} |
| 4296 |
leavelevel(ls); |
| 4297 |
return op; |
| 4298 |
} |
| 4299 |
static void expr(LexState*ls,expdesc*v){ |
| 4300 |
subexpr(ls,v,0); |
| 4301 |
} |
| 4302 |
static int block_follow(int token){ |
| 4303 |
switch(token){ |
| 4304 |
case TK_ELSE:case TK_ELSEIF:case TK_END: |
| 4305 |
case TK_UNTIL:case TK_EOS: |
| 4306 |
return 1; |
| 4307 |
default:return 0; |
| 4308 |
} |
| 4309 |
} |
| 4310 |
static void block(LexState*ls){ |
| 4311 |
FuncState*fs=ls->fs; |
| 4312 |
BlockCnt bl; |
| 4313 |
enterblock(fs,&bl,0); |
| 4314 |
chunk(ls); |
| 4315 |
leaveblock(fs); |
| 4316 |
} |
| 4317 |
struct LHS_assign{ |
| 4318 |
struct LHS_assign*prev; |
| 4319 |
expdesc v; |
| 4320 |
}; |
| 4321 |
static void check_conflict(LexState*ls,struct LHS_assign*lh,expdesc*v){ |
| 4322 |
FuncState*fs=ls->fs; |
| 4323 |
int extra=fs->freereg; |
| 4324 |
int conflict=0; |
| 4325 |
for(;lh;lh=lh->prev){ |
| 4326 |
if(lh->v.k==VINDEXED){ |
| 4327 |
if(lh->v.u.s.info==v->u.s.info){ |
| 4328 |
conflict=1; |
| 4329 |
lh->v.u.s.info=extra; |
| 4330 |
} |
| 4331 |
if(lh->v.u.s.aux==v->u.s.info){ |
| 4332 |
conflict=1; |
| 4333 |
lh->v.u.s.aux=extra; |
| 4334 |
} |
| 4335 |
} |
| 4336 |
} |
| 4337 |
if(conflict){ |
| 4338 |
luaK_codeABC(fs,OP_MOVE,fs->freereg,v->u.s.info,0); |
| 4339 |
luaK_reserveregs(fs,1); |
| 4340 |
} |
| 4341 |
} |
| 4342 |
static void assignment(LexState*ls,struct LHS_assign*lh,int nvars){ |
| 4343 |
expdesc e; |
| 4344 |
check_condition(ls,VLOCAL<=lh->v.k&&lh->v.k<=VINDEXED, |
| 4345 |
"syntax error"); |
| 4346 |
if(testnext(ls,',')){ |
| 4347 |
struct LHS_assign nv; |
| 4348 |
nv.prev=lh; |
| 4349 |
primaryexp(ls,&nv.v); |
| 4350 |
if(nv.v.k==VLOCAL) |
| 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); |
| 4355 |
} |
| 4356 |
else{ |
| 4357 |
int nexps; |
| 4358 |
checknext(ls,'='); |
| 4359 |
nexps=explist1(ls,&e); |
| 4360 |
if(nexps!=nvars){ |
| 4361 |
adjust_assign(ls,nvars,nexps,&e); |
| 4362 |
if(nexps>nvars) |
| 4363 |
ls->fs->freereg-=nexps-nvars; |
| 4364 |
} |
| 4365 |
else{ |
| 4366 |
luaK_setoneret(ls->fs,&e); |
| 4367 |
luaK_storevar(ls->fs,&lh->v,&e); |
| 4368 |
return; |
| 4369 |
} |
| 4370 |
} |
| 4371 |
init_exp(&e,VNONRELOC,ls->fs->freereg-1); |
| 4372 |
luaK_storevar(ls->fs,&lh->v,&e); |
| 4373 |
} |
| 4374 |
static int cond(LexState*ls){ |
| 4375 |
expdesc v; |
| 4376 |
expr(ls,&v); |
| 4377 |
if(v.k==VNIL)v.k=VFALSE; |
| 4378 |
luaK_goiftrue(ls->fs,&v); |
| 4379 |
return v.f; |
| 4380 |
} |
| 4381 |
static void breakstat(LexState*ls){ |
| 4382 |
FuncState*fs=ls->fs; |
| 4383 |
BlockCnt*bl=fs->bl; |
| 4384 |
int upval=0; |
| 4385 |
while(bl&&!bl->isbreakable){ |
| 4386 |
upval|=bl->upval; |
| 4387 |
bl=bl->previous; |
| 4388 |
} |
| 4389 |
if(!bl) |
| 4390 |
luaX_syntaxerror(ls,"no loop to break"); |
| 4391 |
if(upval) |
| 4392 |
luaK_codeABC(fs,OP_CLOSE,bl->nactvar,0,0); |
| 4393 |
luaK_concat(fs,&bl->breaklist,luaK_jump(fs)); |
| 4394 |
} |
| 4395 |
static void whilestat(LexState*ls,int line){ |
| 4396 |
FuncState*fs=ls->fs; |
| 4397 |
int whileinit; |
| 4398 |
int condexit; |
| 4399 |
BlockCnt bl; |
| 4400 |
luaX_next(ls); |
| 4401 |
whileinit=luaK_getlabel(fs); |
| 4402 |
condexit=cond(ls); |
| 4403 |
enterblock(fs,&bl,1); |
| 4404 |
checknext(ls,TK_DO); |
| 4405 |
block(ls); |
| 4406 |
luaK_patchlist(fs,luaK_jump(fs),whileinit); |
| 4407 |
check_match(ls,TK_END,TK_WHILE,line); |
| 4408 |
leaveblock(fs); |
| 4409 |
luaK_patchtohere(fs,condexit); |
| 4410 |
} |
| 4411 |
static void repeatstat(LexState*ls,int line){ |
| 4412 |
int condexit; |
| 4413 |
FuncState*fs=ls->fs; |
| 4414 |
int repeat_init=luaK_getlabel(fs); |
| 4415 |
BlockCnt bl1,bl2; |
| 4416 |
enterblock(fs,&bl1,1); |
| 4417 |
enterblock(fs,&bl2,0); |
| 4418 |
luaX_next(ls); |
| 4419 |
chunk(ls); |
| 4420 |
check_match(ls,TK_UNTIL,TK_REPEAT,line); |
| 4421 |
condexit=cond(ls); |
| 4422 |
if(!bl2.upval){ |
| 4423 |
leaveblock(fs); |
| 4424 |
luaK_patchlist(ls->fs,condexit,repeat_init); |
| 4425 |
} |
| 4426 |
else{ |
| 4427 |
breakstat(ls); |
| 4428 |
luaK_patchtohere(ls->fs,condexit); |
| 4429 |
leaveblock(fs); |
| 4430 |
luaK_patchlist(ls->fs,luaK_jump(fs),repeat_init); |
| 4431 |
} |
| 4432 |
leaveblock(fs); |
| 4433 |
} |
| 4434 |
static int exp1(LexState*ls){ |
| 4435 |
expdesc e; |
| 4436 |
int k; |
| 4437 |
expr(ls,&e); |
| 4438 |
k=e.k; |
| 4439 |
luaK_exp2nextreg(ls->fs,&e); |
| 4440 |
return k; |
| 4441 |
} |
| 4442 |
static void forbody(LexState*ls,int base,int line,int nvars,int isnum){ |
| 4443 |
BlockCnt bl; |
| 4444 |
FuncState*fs=ls->fs; |
| 4445 |
int prep,endfor; |
| 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); |
| 4452 |
block(ls); |
| 4453 |
leaveblock(fs); |
| 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); |
| 4459 |
} |
| 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); |
| 4467 |
checknext(ls,'='); |
| 4468 |
exp1(ls); |
| 4469 |
checknext(ls,','); |
| 4470 |
exp1(ls); |
| 4471 |
if(testnext(ls,',')) |
| 4472 |
exp1(ls); |
| 4473 |
else{ |
| 4474 |
luaK_codeABx(fs,OP_LOADK,fs->freereg,luaK_numberK(fs,1)); |
| 4475 |
luaK_reserveregs(fs,1); |
| 4476 |
} |
| 4477 |
forbody(ls,base,line,1,1); |
| 4478 |
} |
| 4479 |
static void forlist(LexState*ls,TString*indexname){ |
| 4480 |
FuncState*fs=ls->fs; |
| 4481 |
expdesc e; |
| 4482 |
int nvars=0; |
| 4483 |
int line; |
| 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); |
| 4496 |
} |
| 4497 |
static void forstat(LexState*ls,int line){ |
| 4498 |
FuncState*fs=ls->fs; |
| 4499 |
TString*varname; |
| 4500 |
BlockCnt bl; |
| 4501 |
enterblock(fs,&bl,1); |
| 4502 |
luaX_next(ls); |
| 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"); |
| 4508 |
} |
| 4509 |
check_match(ls,TK_END,TK_FOR,line); |
| 4510 |
leaveblock(fs); |
| 4511 |
} |
| 4512 |
static int test_then_block(LexState*ls){ |
| 4513 |
int condexit; |
| 4514 |
luaX_next(ls); |
| 4515 |
condexit=cond(ls); |
| 4516 |
checknext(ls,TK_THEN); |
| 4517 |
block(ls); |
| 4518 |
return condexit; |
| 4519 |
} |
| 4520 |
static void ifstat(LexState*ls,int line){ |
| 4521 |
FuncState*fs=ls->fs; |
| 4522 |
int flist; |
| 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); |
| 4529 |
} |
| 4530 |
if(ls->t.token==TK_ELSE){ |
| 4531 |
luaK_concat(fs,&escapelist,luaK_jump(fs)); |
| 4532 |
luaK_patchtohere(fs,flist); |
| 4533 |
luaX_next(ls); |
| 4534 |
block(ls); |
| 4535 |
} |
| 4536 |
else |
| 4537 |
luaK_concat(fs,&escapelist,flist); |
| 4538 |
luaK_patchtohere(fs,escapelist); |
| 4539 |
check_match(ls,TK_END,TK_IF,line); |
| 4540 |
} |
| 4541 |
static void localfunc(LexState*ls){ |
| 4542 |
expdesc v,b; |
| 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; |
| 4551 |
} |
| 4552 |
static void localstat(LexState*ls){ |
| 4553 |
int nvars=0; |
| 4554 |
int nexps; |
| 4555 |
expdesc e; |
| 4556 |
do{ |
| 4557 |
new_localvar(ls,str_checkname(ls),nvars++); |
| 4558 |
}while(testnext(ls,',')); |
| 4559 |
if(testnext(ls,'=')) |
| 4560 |
nexps=explist1(ls,&e); |
| 4561 |
else{ |
| 4562 |
e.k=VVOID; |
| 4563 |
nexps=0; |
| 4564 |
} |
| 4565 |
adjust_assign(ls,nvars,nexps,&e); |
| 4566 |
adjustlocalvars(ls,nvars); |
| 4567 |
} |
| 4568 |
static int funcname(LexState*ls,expdesc*v){ |
| 4569 |
int needself=0; |
| 4570 |
singlevar(ls,v); |
| 4571 |
while(ls->t.token=='.') |
| 4572 |
field(ls,v); |
| 4573 |
if(ls->t.token==':'){ |
| 4574 |
needself=1; |
| 4575 |
field(ls,v); |
| 4576 |
} |
| 4577 |
return needself; |
| 4578 |
} |
| 4579 |
static void funcstat(LexState*ls,int line){ |
| 4580 |
int needself; |
| 4581 |
expdesc v,b; |
| 4582 |
luaX_next(ls); |
| 4583 |
needself=funcname(ls,&v); |
| 4584 |
body(ls,&b,needself,line); |
| 4585 |
luaK_storevar(ls->fs,&v,&b); |
| 4586 |
luaK_fixline(ls->fs,line); |
| 4587 |
} |
| 4588 |
static void exprstat(LexState*ls){ |
| 4589 |
FuncState*fs=ls->fs; |
| 4590 |
struct LHS_assign v; |
| 4591 |
primaryexp(ls,&v.v); |
| 4592 |
if(v.v.k==VCALL) |
| 4593 |
SETARG_C(getcode(fs,&v.v),1); |
| 4594 |
else{ |
| 4595 |
v.prev=NULL; |
| 4596 |
assignment(ls,&v,1); |
| 4597 |
} |
| 4598 |
} |
| 4599 |
static void retstat(LexState*ls){ |
| 4600 |
FuncState*fs=ls->fs; |
| 4601 |
expdesc e; |
| 4602 |
int first,nret; |
| 4603 |
luaX_next(ls); |
| 4604 |
if(block_follow(ls->t.token)||ls->t.token==';') |
| 4605 |
first=nret=0; |
| 4606 |
else{ |
| 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); |
| 4612 |
} |
| 4613 |
first=fs->nactvar; |
| 4614 |
nret=(-1); |
| 4615 |
} |
| 4616 |
else{ |
| 4617 |
if(nret==1) |
| 4618 |
first=luaK_exp2anyreg(fs,&e); |
| 4619 |
else{ |
| 4620 |
luaK_exp2nextreg(fs,&e); |
| 4621 |
first=fs->nactvar; |
| 4622 |
} |
| 4623 |
} |
| 4624 |
} |
| 4625 |
luaK_ret(fs,first,nret); |
| 4626 |
} |
| 4627 |
static int statement(LexState*ls){ |
| 4628 |
int line=ls->linenumber; |
| 4629 |
switch(ls->t.token){ |
| 4630 |
case TK_IF:{ |
| 4631 |
ifstat(ls,line); |
| 4632 |
return 0; |
| 4633 |
} |
| 4634 |
case TK_WHILE:{ |
| 4635 |
whilestat(ls,line); |
| 4636 |
return 0; |
| 4637 |
} |
| 4638 |
case TK_DO:{ |
| 4639 |
luaX_next(ls); |
| 4640 |
block(ls); |
| 4641 |
check_match(ls,TK_END,TK_DO,line); |
| 4642 |
return 0; |
| 4643 |
} |
| 4644 |
case TK_FOR:{ |
| 4645 |
forstat(ls,line); |
| 4646 |
return 0; |
| 4647 |
} |
| 4648 |
case TK_REPEAT:{ |
| 4649 |
repeatstat(ls,line); |
| 4650 |
return 0; |
| 4651 |
} |
| 4652 |
case TK_FUNCTION:{ |
| 4653 |
funcstat(ls,line); |
| 4654 |
return 0; |
| 4655 |
} |
| 4656 |
case TK_LOCAL:{ |
| 4657 |
luaX_next(ls); |
| 4658 |
if(testnext(ls,TK_FUNCTION)) |
| 4659 |
localfunc(ls); |
| 4660 |
else |
| 4661 |
localstat(ls); |
| 4662 |
return 0; |
| 4663 |
} |
| 4664 |
case TK_RETURN:{ |
| 4665 |
retstat(ls); |
| 4666 |
return 1; |
| 4667 |
} |
| 4668 |
case TK_BREAK:{ |
| 4669 |
luaX_next(ls); |
| 4670 |
breakstat(ls); |
| 4671 |
return 1; |
| 4672 |
} |
| 4673 |
default:{ |
| 4674 |
exprstat(ls); |
| 4675 |
return 0; |
| 4676 |
} |
| 4677 |
} |
| 4678 |
} |
| 4679 |
static void chunk(LexState*ls){ |
| 4680 |
int islast=0; |
| 4681 |
enterlevel(ls); |
| 4682 |
while(!islast&&!block_follow(ls->t.token)){ |
| 4683 |
islast=statement(ls); |
| 4684 |
testnext(ls,';'); |
| 4685 |
ls->fs->freereg=ls->fs->nactvar; |
| 4686 |
} |
| 4687 |
leavelevel(ls); |
| 4688 |
} |
| 4689 |
static const TValue*luaV_tonumber(const TValue*obj,TValue*n){ |
| 4690 |
lua_Number num; |
| 4691 |
if(ttisnumber(obj))return obj; |
| 4692 |
if(ttisstring(obj)&&luaO_str2d(svalue(obj),&num)){ |
| 4693 |
setnvalue(n,num); |
| 4694 |
return n; |
| 4695 |
} |
| 4696 |
else |
| 4697 |
return NULL; |
| 4698 |
} |
| 4699 |
static int luaV_tostring(lua_State*L,StkId obj){ |
| 4700 |
if(!ttisnumber(obj)) |
| 4701 |
return 0; |
| 4702 |
else{ |
| 4703 |
char s[32]; |
| 4704 |
lua_Number n=nvalue(obj); |
| 4705 |
lua_number2str(s,n); |
| 4706 |
setsvalue(L,obj,luaS_new(L,s)); |
| 4707 |
return 1; |
| 4708 |
} |
| 4709 |
} |
| 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); |
| 4713 |
setobj(L,L->top,f); |
| 4714 |
setobj(L,L->top+1,p1); |
| 4715 |
setobj(L,L->top+2,p2); |
| 4716 |
luaD_checkstack(L,3); |
| 4717 |
L->top+=3; |
| 4718 |
luaD_call(L,L->top-3,1); |
| 4719 |
res=restorestack(L,result); |
| 4720 |
L->top--; |
| 4721 |
setobj(L,res,L->top); |
| 4722 |
} |
| 4723 |
static void callTM(lua_State*L,const TValue*f,const TValue*p1, |
| 4724 |
const TValue*p2,const TValue*p3){ |
| 4725 |
setobj(L,L->top,f); |
| 4726 |
setobj(L,L->top+1,p1); |
| 4727 |
setobj(L,L->top+2,p2); |
| 4728 |
setobj(L,L->top+3,p3); |
| 4729 |
luaD_checkstack(L,4); |
| 4730 |
L->top+=4; |
| 4731 |
luaD_call(L,L->top-4,0); |
| 4732 |
} |
| 4733 |
static void luaV_gettable(lua_State*L,const TValue*t,TValue*key,StkId val){ |
| 4734 |
int loop; |
| 4735 |
for(loop=0;loop<100;loop++){ |
| 4736 |
const TValue*tm; |
| 4737 |
if(ttistable(t)){ |
| 4738 |
Table*h=hvalue(t); |
| 4739 |
const TValue*res=luaH_get(h,key); |
| 4740 |
if(!ttisnil(res)|| |
| 4741 |
(tm=fasttm(L,h->metatable,TM_INDEX))==NULL){ |
| 4742 |
setobj(L,val,res); |
| 4743 |
return; |
| 4744 |
} |
| 4745 |
} |
| 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); |
| 4750 |
return; |
| 4751 |
} |
| 4752 |
t=tm; |
| 4753 |
} |
| 4754 |
luaG_runerror(L,"loop in gettable"); |
| 4755 |
} |
| 4756 |
static void luaV_settable(lua_State*L,const TValue*t,TValue*key,StkId val){ |
| 4757 |
int loop; |
| 4758 |
TValue temp; |
| 4759 |
for(loop=0;loop<100;loop++){ |
| 4760 |
const TValue*tm; |
| 4761 |
if(ttistable(t)){ |
| 4762 |
Table*h=hvalue(t); |
| 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); |
| 4767 |
h->flags=0; |
| 4768 |
luaC_barriert(L,h,val); |
| 4769 |
return; |
| 4770 |
} |
| 4771 |
} |
| 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); |
| 4776 |
return; |
| 4777 |
} |
| 4778 |
setobj(L,&temp,tm); |
| 4779 |
t=&temp; |
| 4780 |
} |
| 4781 |
luaG_runerror(L,"loop in settable"); |
| 4782 |
} |
| 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); |
| 4786 |
if(ttisnil(tm)) |
| 4787 |
tm=luaT_gettmbyobj(L,p2,event); |
| 4788 |
if(ttisnil(tm))return 0; |
| 4789 |
callTMres(L,res,tm,p1,p2); |
| 4790 |
return 1; |
| 4791 |
} |
| 4792 |
static const TValue*get_compTM(lua_State*L,Table*mt1,Table*mt2, |
| 4793 |
TMS event){ |
| 4794 |
const TValue*tm1=fasttm(L,mt1,event); |
| 4795 |
const TValue*tm2; |
| 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)) |
| 4801 |
return tm1; |
| 4802 |
return NULL; |
| 4803 |
} |
| 4804 |
static int call_orderTM(lua_State*L,const TValue*p1,const TValue*p2, |
| 4805 |
TMS event){ |
| 4806 |
const TValue*tm1=luaT_gettmbyobj(L,p1,event); |
| 4807 |
const TValue*tm2; |
| 4808 |
if(ttisnil(tm1))return-1; |
| 4809 |
tm2=luaT_gettmbyobj(L,p2,event); |
| 4810 |
if(!luaO_rawequalObj(tm1,tm2)) |
| 4811 |
return-1; |
| 4812 |
callTMres(L,L->top,tm1,p1,p2); |
| 4813 |
return!l_isfalse(L->top); |
| 4814 |
} |
| 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; |
| 4820 |
for(;;){ |
| 4821 |
int temp=strcoll(l,r); |
| 4822 |
if(temp!=0)return temp; |
| 4823 |
else{ |
| 4824 |
size_t len=strlen(l); |
| 4825 |
if(len==lr) |
| 4826 |
return(len==ll)?0:1; |
| 4827 |
else if(len==ll) |
| 4828 |
return-1; |
| 4829 |
len++; |
| 4830 |
l+=len;ll-=len;r+=len;lr-=len; |
| 4831 |
} |
| 4832 |
} |
| 4833 |
} |
| 4834 |
static int luaV_lessthan(lua_State*L,const TValue*l,const TValue*r){ |
| 4835 |
int res; |
| 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) |
| 4843 |
return res; |
| 4844 |
return luaG_ordererror(L,l,r); |
| 4845 |
} |
| 4846 |
static int lessequal(lua_State*L,const TValue*l,const TValue*r){ |
| 4847 |
int res; |
| 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) |
| 4855 |
return res; |
| 4856 |
else if((res=call_orderTM(L,r,l,TM_LT))!=-1) |
| 4857 |
return!res; |
| 4858 |
return luaG_ordererror(L,l,r); |
| 4859 |
} |
| 4860 |
static int luaV_equalval(lua_State*L,const TValue*t1,const TValue*t2){ |
| 4861 |
const TValue*tm; |
| 4862 |
switch(ttype(t1)){ |
| 4863 |
case 0:return 1; |
| 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); |
| 4867 |
case 7:{ |
| 4868 |
if(uvalue(t1)==uvalue(t2))return 1; |
| 4869 |
tm=get_compTM(L,uvalue(t1)->metatable,uvalue(t2)->metatable, |
| 4870 |
TM_EQ); |
| 4871 |
break; |
| 4872 |
} |
| 4873 |
case 5:{ |
| 4874 |
if(hvalue(t1)==hvalue(t2))return 1; |
| 4875 |
tm=get_compTM(L,hvalue(t1)->metatable,hvalue(t2)->metatable,TM_EQ); |
| 4876 |
break; |
| 4877 |
} |
| 4878 |
default:return gcvalue(t1)==gcvalue(t2); |
| 4879 |
} |
| 4880 |
if(tm==NULL)return 0; |
| 4881 |
callTMres(L,L->top,tm,t1,t2); |
| 4882 |
return!l_isfalse(L->top); |
| 4883 |
} |
| 4884 |
static void luaV_concat(lua_State*L,int total,int last){ |
| 4885 |
do{ |
| 4886 |
StkId top=L->base+last+1; |
| 4887 |
int n=2; |
| 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); |
| 4893 |
else{ |
| 4894 |
size_t tl=tsvalue(top-1)->len; |
| 4895 |
char*buffer; |
| 4896 |
int i; |
| 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"); |
| 4900 |
tl+=l; |
| 4901 |
} |
| 4902 |
buffer=luaZ_openspace(L,&G(L)->buff,tl); |
| 4903 |
tl=0; |
| 4904 |
for(i=n;i>0;i--){ |
| 4905 |
size_t l=tsvalue(top-i)->len; |
| 4906 |
memcpy(buffer+tl,svalue(top-i),l); |
| 4907 |
tl+=l; |
| 4908 |
} |
| 4909 |
setsvalue(L,top-n,luaS_newlstr(L,buffer,tl)); |
| 4910 |
} |
| 4911 |
total-=n-1; |
| 4912 |
last-=n-1; |
| 4913 |
}while(total>1); |
| 4914 |
} |
| 4915 |
static void Arith(lua_State*L,StkId ra,const TValue*rb, |
| 4916 |
const TValue*rc,TMS op){ |
| 4917 |
TValue tempb,tempc; |
| 4918 |
const TValue*b,*c; |
| 4919 |
if((b=luaV_tonumber(rb,&tempb))!=NULL&& |
| 4920 |
(c=luaV_tonumber(rc,&tempc))!=NULL){ |
| 4921 |
lua_Number nb=nvalue(b),nc=nvalue(c); |
| 4922 |
switch(op){ |
| 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; |
| 4930 |
default:break; |
| 4931 |
} |
| 4932 |
} |
| 4933 |
else if(!call_binTM(L,rb,rc,ra,op)) |
| 4934 |
luaG_aritherror(L,rb,rc); |
| 4935 |
} |
| 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){ |
| 4946 |
LClosure*cl; |
| 4947 |
StkId base; |
| 4948 |
TValue*k; |
| 4949 |
const Instruction*pc; |
| 4950 |
reentry: |
| 4951 |
pc=L->savedpc; |
| 4952 |
cl=&clvalue(L->ci->func)->l; |
| 4953 |
base=L->base; |
| 4954 |
k=cl->p->k; |
| 4955 |
for(;;){ |
| 4956 |
const Instruction i=*pc++; |
| 4957 |
StkId ra; |
| 4958 |
ra=RA(i); |
| 4959 |
switch(GET_OPCODE(i)){ |
| 4960 |
case OP_MOVE:{ |
| 4961 |
setobj(L,ra,RB(i)); |
| 4962 |
continue; |
| 4963 |
} |
| 4964 |
case OP_LOADK:{ |
| 4965 |
setobj(L,ra,KBx(i)); |
| 4966 |
continue; |
| 4967 |
} |
| 4968 |
case OP_LOADBOOL:{ |
| 4969 |
setbvalue(ra,GETARG_B(i)); |
| 4970 |
if(GETARG_C(i))pc++; |
| 4971 |
continue; |
| 4972 |
} |
| 4973 |
case OP_LOADNIL:{ |
| 4974 |
TValue*rb=RB(i); |
| 4975 |
do{ |
| 4976 |
setnilvalue(rb--); |
| 4977 |
}while(rb>=ra); |
| 4978 |
continue; |
| 4979 |
} |
| 4980 |
case OP_GETUPVAL:{ |
| 4981 |
int b=GETARG_B(i); |
| 4982 |
setobj(L,ra,cl->upvals[b]->v); |
| 4983 |
continue; |
| 4984 |
} |
| 4985 |
case OP_GETGLOBAL:{ |
| 4986 |
TValue g; |
| 4987 |
TValue*rb=KBx(i); |
| 4988 |
sethvalue(L,&g,cl->env); |
| 4989 |
Protect(luaV_gettable(L,&g,rb,ra)); |
| 4990 |
continue; |
| 4991 |
} |
| 4992 |
case OP_GETTABLE:{ |
| 4993 |
Protect(luaV_gettable(L,RB(i),RKC(i),ra)); |
| 4994 |
continue; |
| 4995 |
} |
| 4996 |
case OP_SETGLOBAL:{ |
| 4997 |
TValue g; |
| 4998 |
sethvalue(L,&g,cl->env); |
| 4999 |
Protect(luaV_settable(L,&g,KBx(i),ra)); |
| 5000 |
continue; |
| 5001 |
} |
| 5002 |
case OP_SETUPVAL:{ |
| 5003 |
UpVal*uv=cl->upvals[GETARG_B(i)]; |
| 5004 |
setobj(L,uv->v,ra); |
| 5005 |
luaC_barrier(L,uv,ra); |
| 5006 |
continue; |
| 5007 |
} |
| 5008 |
case OP_SETTABLE:{ |
| 5009 |
Protect(luaV_settable(L,ra,RKB(i),RKC(i))); |
| 5010 |
continue; |
| 5011 |
} |
| 5012 |
case OP_NEWTABLE:{ |
| 5013 |
int b=GETARG_B(i); |
| 5014 |
int c=GETARG_C(i); |
| 5015 |
sethvalue(L,ra,luaH_new(L,luaO_fb2int(b),luaO_fb2int(c))); |
| 5016 |
Protect(luaC_checkGC(L)); |
| 5017 |
continue; |
| 5018 |
} |
| 5019 |
case OP_SELF:{ |
| 5020 |
StkId rb=RB(i); |
| 5021 |
setobj(L,ra+1,rb); |
| 5022 |
Protect(luaV_gettable(L,rb,RKC(i),ra)); |
| 5023 |
continue; |
| 5024 |
} |
| 5025 |
case OP_ADD:{ |
| 5026 |
arith_op(luai_numadd,TM_ADD); |
| 5027 |
continue; |
| 5028 |
} |
| 5029 |
case OP_SUB:{ |
| 5030 |
arith_op(luai_numsub,TM_SUB); |
| 5031 |
continue; |
| 5032 |
} |
| 5033 |
case OP_MUL:{ |
| 5034 |
arith_op(luai_nummul,TM_MUL); |
| 5035 |
continue; |
| 5036 |
} |
| 5037 |
case OP_DIV:{ |
| 5038 |
arith_op(luai_numdiv,TM_DIV); |
| 5039 |
continue; |
| 5040 |
} |
| 5041 |
case OP_MOD:{ |
| 5042 |
arith_op(luai_nummod,TM_MOD); |
| 5043 |
continue; |
| 5044 |
} |
| 5045 |
case OP_POW:{ |
| 5046 |
arith_op(luai_numpow,TM_POW); |
| 5047 |
continue; |
| 5048 |
} |
| 5049 |
case OP_UNM:{ |
| 5050 |
TValue*rb=RB(i); |
| 5051 |
if(ttisnumber(rb)){ |
| 5052 |
lua_Number nb=nvalue(rb); |
| 5053 |
setnvalue(ra,luai_numunm(nb)); |
| 5054 |
} |
| 5055 |
else{ |
| 5056 |
Protect(Arith(L,ra,rb,rb,TM_UNM)); |
| 5057 |
} |
| 5058 |
continue; |
| 5059 |
} |
| 5060 |
case OP_NOT:{ |
| 5061 |
int res=l_isfalse(RB(i)); |
| 5062 |
setbvalue(ra,res); |
| 5063 |
continue; |
| 5064 |
} |
| 5065 |
case OP_LEN:{ |
| 5066 |
const TValue*rb=RB(i); |
| 5067 |
switch(ttype(rb)){ |
| 5068 |
case 5:{ |
| 5069 |
setnvalue(ra,cast_num(luaH_getn(hvalue(rb)))); |
| 5070 |
break; |
| 5071 |
} |
| 5072 |
case 4:{ |
| 5073 |
setnvalue(ra,cast_num(tsvalue(rb)->len)); |
| 5074 |
break; |
| 5075 |
} |
| 5076 |
default:{ |
| 5077 |
Protect( |
| 5078 |
if(!call_binTM(L,rb,(&luaO_nilobject_),ra,TM_LEN)) |
| 5079 |
luaG_typeerror(L,rb,"get length of"); |
| 5080 |
) |
| 5081 |
} |
| 5082 |
} |
| 5083 |
continue; |
| 5084 |
} |
| 5085 |
case OP_CONCAT:{ |
| 5086 |
int b=GETARG_B(i); |
| 5087 |
int c=GETARG_C(i); |
| 5088 |
Protect(luaV_concat(L,c-b+1,c);luaC_checkGC(L)); |
| 5089 |
setobj(L,RA(i),base+b); |
| 5090 |
continue; |
| 5091 |
} |
| 5092 |
case OP_JMP:{ |
| 5093 |
dojump(L,pc,GETARG_sBx(i)); |
| 5094 |
continue; |
| 5095 |
} |
| 5096 |
case OP_EQ:{ |
| 5097 |
TValue*rb=RKB(i); |
| 5098 |
TValue*rc=RKC(i); |
| 5099 |
Protect( |
| 5100 |
if(equalobj(L,rb,rc)==GETARG_A(i)) |
| 5101 |
dojump(L,pc,GETARG_sBx(*pc)); |
| 5102 |
) |
| 5103 |
pc++; |
| 5104 |
continue; |
| 5105 |
} |
| 5106 |
case OP_LT:{ |
| 5107 |
Protect( |
| 5108 |
if(luaV_lessthan(L,RKB(i),RKC(i))==GETARG_A(i)) |
| 5109 |
dojump(L,pc,GETARG_sBx(*pc)); |
| 5110 |
) |
| 5111 |
pc++; |
| 5112 |
continue; |
| 5113 |
} |
| 5114 |
case OP_LE:{ |
| 5115 |
Protect( |
| 5116 |
if(lessequal(L,RKB(i),RKC(i))==GETARG_A(i)) |
| 5117 |
dojump(L,pc,GETARG_sBx(*pc)); |
| 5118 |
) |
| 5119 |
pc++; |
| 5120 |
continue; |
| 5121 |
} |
| 5122 |
case OP_TEST:{ |
| 5123 |
if(l_isfalse(ra)!=GETARG_C(i)) |
| 5124 |
dojump(L,pc,GETARG_sBx(*pc)); |
| 5125 |
pc++; |
| 5126 |
continue; |
| 5127 |
} |
| 5128 |
case OP_TESTSET:{ |
| 5129 |
TValue*rb=RB(i); |
| 5130 |
if(l_isfalse(rb)!=GETARG_C(i)){ |
| 5131 |
setobj(L,ra,rb); |
| 5132 |
dojump(L,pc,GETARG_sBx(*pc)); |
| 5133 |
} |
| 5134 |
pc++; |
| 5135 |
continue; |
| 5136 |
} |
| 5137 |
case OP_CALL:{ |
| 5138 |
int b=GETARG_B(i); |
| 5139 |
int nresults=GETARG_C(i)-1; |
| 5140 |
if(b!=0)L->top=ra+b; |
| 5141 |
L->savedpc=pc; |
| 5142 |
switch(luaD_precall(L,ra,nresults)){ |
| 5143 |
case 0:{ |
| 5144 |
nexeccalls++; |
| 5145 |
goto reentry; |
| 5146 |
} |
| 5147 |
case 1:{ |
| 5148 |
if(nresults>=0)L->top=L->ci->top; |
| 5149 |
base=L->base; |
| 5150 |
continue; |
| 5151 |
} |
| 5152 |
default:{ |
| 5153 |
return; |
| 5154 |
} |
| 5155 |
} |
| 5156 |
} |
| 5157 |
case OP_TAILCALL:{ |
| 5158 |
int b=GETARG_B(i); |
| 5159 |
if(b!=0)L->top=ra+b; |
| 5160 |
L->savedpc=pc; |
| 5161 |
switch(luaD_precall(L,ra,(-1))){ |
| 5162 |
case 0:{ |
| 5163 |
CallInfo*ci=L->ci-1; |
| 5164 |
int aux; |
| 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; |
| 5173 |
ci->tailcalls++; |
| 5174 |
L->ci--; |
| 5175 |
goto reentry; |
| 5176 |
} |
| 5177 |
case 1:{ |
| 5178 |
base=L->base; |
| 5179 |
continue; |
| 5180 |
} |
| 5181 |
default:{ |
| 5182 |
return; |
| 5183 |
} |
| 5184 |
} |
| 5185 |
} |
| 5186 |
case OP_RETURN:{ |
| 5187 |
int b=GETARG_B(i); |
| 5188 |
if(b!=0)L->top=ra+b-1; |
| 5189 |
if(L->openupval)luaF_close(L,base); |
| 5190 |
L->savedpc=pc; |
| 5191 |
b=luaD_poscall(L,ra); |
| 5192 |
if(--nexeccalls==0) |
| 5193 |
return; |
| 5194 |
else{ |
| 5195 |
if(b)L->top=L->ci->top; |
| 5196 |
goto reentry; |
| 5197 |
} |
| 5198 |
} |
| 5199 |
case OP_FORLOOP:{ |
| 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)); |
| 5206 |
setnvalue(ra,idx); |
| 5207 |
setnvalue(ra+3,idx); |
| 5208 |
} |
| 5209 |
continue; |
| 5210 |
} |
| 5211 |
case OP_FORPREP:{ |
| 5212 |
const TValue*init=ra; |
| 5213 |
const TValue*plimit=ra+1; |
| 5214 |
const TValue*pstep=ra+2; |
| 5215 |
L->savedpc=pc; |
| 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)); |
| 5224 |
continue; |
| 5225 |
} |
| 5226 |
case OP_TFORLOOP:{ |
| 5227 |
StkId cb=ra+3; |
| 5228 |
setobj(L,cb+2,ra+2); |
| 5229 |
setobj(L,cb+1,ra+1); |
| 5230 |
setobj(L,cb,ra); |
| 5231 |
L->top=cb+3; |
| 5232 |
Protect(luaD_call(L,cb,GETARG_C(i))); |
| 5233 |
L->top=L->ci->top; |
| 5234 |
cb=RA(i)+3; |
| 5235 |
if(!ttisnil(cb)){ |
| 5236 |
setobj(L,cb-1,cb); |
| 5237 |
dojump(L,pc,GETARG_sBx(*pc)); |
| 5238 |
} |
| 5239 |
pc++; |
| 5240 |
continue; |
| 5241 |
} |
| 5242 |
case OP_SETLIST:{ |
| 5243 |
int n=GETARG_B(i); |
| 5244 |
int c=GETARG_C(i); |
| 5245 |
int last; |
| 5246 |
Table*h; |
| 5247 |
if(n==0){ |
| 5248 |
n=cast_int(L->top-ra)-1; |
| 5249 |
L->top=L->ci->top; |
| 5250 |
} |
| 5251 |
if(c==0)c=cast_int(*pc++); |
| 5252 |
runtime_check(L,ttistable(ra)); |
| 5253 |
h=hvalue(ra); |
| 5254 |
last=((c-1)*50)+n; |
| 5255 |
if(last>h->sizearray) |
| 5256 |
luaH_resizearray(L,h,last); |
| 5257 |
for(;n>0;n--){ |
| 5258 |
TValue*val=ra+n; |
| 5259 |
setobj(L,luaH_setnum(L,h,last--),val); |
| 5260 |
luaC_barriert(L,h,val); |
| 5261 |
} |
| 5262 |
continue; |
| 5263 |
} |
| 5264 |
case OP_CLOSE:{ |
| 5265 |
luaF_close(L,ra); |
| 5266 |
continue; |
| 5267 |
} |
| 5268 |
case OP_CLOSURE:{ |
| 5269 |
Proto*p; |
| 5270 |
Closure*ncl; |
| 5271 |
int nup,j; |
| 5272 |
p=cl->p->p[GETARG_Bx(i)]; |
| 5273 |
nup=p->nups; |
| 5274 |
ncl=luaF_newLclosure(L,nup,cl->env); |
| 5275 |
ncl->l.p=p; |
| 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)]; |
| 5279 |
else{ |
| 5280 |
ncl->l.upvals[j]=luaF_findupval(L,base+GETARG_B(*pc)); |
| 5281 |
} |
| 5282 |
} |
| 5283 |
setclvalue(L,ra,ncl); |
| 5284 |
Protect(luaC_checkGC(L)); |
| 5285 |
continue; |
| 5286 |
} |
| 5287 |
case OP_VARARG:{ |
| 5288 |
int b=GETARG_B(i)-1; |
| 5289 |
int j; |
| 5290 |
CallInfo*ci=L->ci; |
| 5291 |
int n=cast_int(ci->base-ci->func)-cl->p->numparams-1; |
| 5292 |
if(b==(-1)){ |
| 5293 |
Protect(luaD_checkstack(L,n)); |
| 5294 |
ra=RA(i); |
| 5295 |
b=n; |
| 5296 |
L->top=ra+n; |
| 5297 |
} |
| 5298 |
for(j=0;j<b;j++){ |
| 5299 |
if(j<n){ |
| 5300 |
setobj(L,ra+j,ci->base-n+j); |
| 5301 |
} |
| 5302 |
else{ |
| 5303 |
setnilvalue(ra+j); |
| 5304 |
} |
| 5305 |
} |
| 5306 |
continue; |
| 5307 |
} |
| 5308 |
} |
| 5309 |
} |
| 5310 |
} |
| 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){ |
| 5315 |
if(idx>0){ |
| 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_)); |
| 5319 |
else return o; |
| 5320 |
} |
| 5321 |
else if(idx>(-10000)){ |
| 5322 |
luai_apicheck(L,idx!=0&&-idx<=L->top-L->base); |
| 5323 |
return L->top+idx; |
| 5324 |
} |
| 5325 |
else switch(idx){ |
| 5326 |
case(-10000):return registry(L); |
| 5327 |
case(-10001):{ |
| 5328 |
Closure*func=curr_func(L); |
| 5329 |
sethvalue(L,&L->env,func->c.env); |
| 5330 |
return&L->env; |
| 5331 |
} |
| 5332 |
case(-10002):return gt(L); |
| 5333 |
default:{ |
| 5334 |
Closure*func=curr_func(L); |
| 5335 |
idx=(-10002)-idx; |
| 5336 |
return(idx<=func->c.nupvalues) |
| 5337 |
?&func->c.upvalue[idx-1] |
| 5338 |
:cast(TValue*,(&luaO_nilobject_)); |
| 5339 |
} |
| 5340 |
} |
| 5341 |
} |
| 5342 |
static Table*getcurrenv(lua_State*L){ |
| 5343 |
if(L->ci==L->base_ci) |
| 5344 |
return hvalue(gt(L)); |
| 5345 |
else{ |
| 5346 |
Closure*func=curr_func(L); |
| 5347 |
return func->c.env; |
| 5348 |
} |
| 5349 |
} |
| 5350 |
static int lua_checkstack(lua_State*L,int size){ |
| 5351 |
int res=1; |
| 5352 |
if(size>8000||(L->top-L->base+size)>8000) |
| 5353 |
res=0; |
| 5354 |
else if(size>0){ |
| 5355 |
luaD_checkstack(L,size); |
| 5356 |
if(L->ci->top<L->top+size) |
| 5357 |
L->ci->top=L->top+size; |
| 5358 |
} |
| 5359 |
return res; |
| 5360 |
} |
| 5361 |
static lua_CFunction lua_atpanic(lua_State*L,lua_CFunction panicf){ |
| 5362 |
lua_CFunction old; |
| 5363 |
old=G(L)->panic; |
| 5364 |
G(L)->panic=panicf; |
| 5365 |
return old; |
| 5366 |
} |
| 5367 |
static int lua_gettop(lua_State*L){ |
| 5368 |
return cast_int(L->top-L->base); |
| 5369 |
} |
| 5370 |
static void lua_settop(lua_State*L,int idx){ |
| 5371 |
if(idx>=0){ |
| 5372 |
luai_apicheck(L,idx<=L->stack_last-L->base); |
| 5373 |
while(L->top<L->base+idx) |
| 5374 |
setnilvalue(L->top++); |
| 5375 |
L->top=L->base+idx; |
| 5376 |
} |
| 5377 |
else{ |
| 5378 |
luai_apicheck(L,-(idx+1)<=(L->top-L->base)); |
| 5379 |
L->top+=idx+1; |
| 5380 |
} |
| 5381 |
} |
| 5382 |
static void lua_remove(lua_State*L,int idx){ |
| 5383 |
StkId p; |
| 5384 |
p=index2adr(L,idx); |
| 5385 |
api_checkvalidindex(L,p); |
| 5386 |
while(++p<L->top)setobj(L,p-1,p); |
| 5387 |
L->top--; |
| 5388 |
} |
| 5389 |
static void lua_insert(lua_State*L,int idx){ |
| 5390 |
StkId p; |
| 5391 |
StkId q; |
| 5392 |
p=index2adr(L,idx); |
| 5393 |
api_checkvalidindex(L,p); |
| 5394 |
for(q=L->top;q>p;q--)setobj(L,q,q-1); |
| 5395 |
setobj(L,p,L->top); |
| 5396 |
} |
| 5397 |
static void lua_replace(lua_State*L,int idx){ |
| 5398 |
StkId o; |
| 5399 |
if(idx==(-10001)&&L->ci==L->base_ci) |
| 5400 |
luaG_runerror(L,"no calling environment"); |
| 5401 |
api_checknelems(L,1); |
| 5402 |
o=index2adr(L,idx); |
| 5403 |
api_checkvalidindex(L,o); |
| 5404 |
if(idx==(-10001)){ |
| 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); |
| 5409 |
} |
| 5410 |
else{ |
| 5411 |
setobj(L,o,L->top-1); |
| 5412 |
if(idx<(-10002)) |
| 5413 |
luaC_barrier(L,curr_func(L),L->top-1); |
| 5414 |
} |
| 5415 |
L->top--; |
| 5416 |
} |
| 5417 |
static void lua_pushvalue(lua_State*L,int idx){ |
| 5418 |
setobj(L,L->top,index2adr(L,idx)); |
| 5419 |
api_incr_top(L); |
| 5420 |
} |
| 5421 |
static int lua_type(lua_State*L,int idx){ |
| 5422 |
StkId o=index2adr(L,idx); |
| 5423 |
return(o==(&luaO_nilobject_))?(-1):ttype(o); |
| 5424 |
} |
| 5425 |
static const char*lua_typename(lua_State*L,int t){ |
| 5426 |
UNUSED(L); |
| 5427 |
return(t==(-1))?"no value":luaT_typenames[t]; |
| 5428 |
} |
| 5429 |
static int lua_iscfunction(lua_State*L,int idx){ |
| 5430 |
StkId o=index2adr(L,idx); |
| 5431 |
return iscfunction(o); |
| 5432 |
} |
| 5433 |
static int lua_isnumber(lua_State*L,int idx){ |
| 5434 |
TValue n; |
| 5435 |
const TValue*o=index2adr(L,idx); |
| 5436 |
return tonumber(o,&n); |
| 5437 |
} |
| 5438 |
static int lua_isstring(lua_State*L,int idx){ |
| 5439 |
int t=lua_type(L,idx); |
| 5440 |
return(t==4||t==3); |
| 5441 |
} |
| 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); |
| 5447 |
} |
| 5448 |
static int lua_lessthan(lua_State*L,int index1,int index2){ |
| 5449 |
StkId o1,o2; |
| 5450 |
int i; |
| 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); |
| 5455 |
return i; |
| 5456 |
} |
| 5457 |
static lua_Number lua_tonumber(lua_State*L,int idx){ |
| 5458 |
TValue n; |
| 5459 |
const TValue*o=index2adr(L,idx); |
| 5460 |
if(tonumber(o,&n)) |
| 5461 |
return nvalue(o); |
| 5462 |
else |
| 5463 |
return 0; |
| 5464 |
} |
| 5465 |
static lua_Integer lua_tointeger(lua_State*L,int idx){ |
| 5466 |
TValue n; |
| 5467 |
const TValue*o=index2adr(L,idx); |
| 5468 |
if(tonumber(o,&n)){ |
| 5469 |
lua_Integer res; |
| 5470 |
lua_Number num=nvalue(o); |
| 5471 |
lua_number2integer(res,num); |
| 5472 |
return res; |
| 5473 |
} |
| 5474 |
else |
| 5475 |
return 0; |
| 5476 |
} |
| 5477 |
static int lua_toboolean(lua_State*L,int idx){ |
| 5478 |
const TValue*o=index2adr(L,idx); |
| 5479 |
return!l_isfalse(o); |
| 5480 |
} |
| 5481 |
static const char*lua_tolstring(lua_State*L,int idx,size_t*len){ |
| 5482 |
StkId o=index2adr(L,idx); |
| 5483 |
if(!ttisstring(o)){ |
| 5484 |
if(!luaV_tostring(L,o)){ |
| 5485 |
if(len!=NULL)*len=0; |
| 5486 |
return NULL; |
| 5487 |
} |
| 5488 |
luaC_checkGC(L); |
| 5489 |
o=index2adr(L,idx); |
| 5490 |
} |
| 5491 |
if(len!=NULL)*len=tsvalue(o)->len; |
| 5492 |
return svalue(o); |
| 5493 |
} |
| 5494 |
static size_t lua_objlen(lua_State*L,int idx){ |
| 5495 |
StkId o=index2adr(L,idx); |
| 5496 |
switch(ttype(o)){ |
| 5497 |
case 4:return tsvalue(o)->len; |
| 5498 |
case 7:return uvalue(o)->len; |
| 5499 |
case 5:return luaH_getn(hvalue(o)); |
| 5500 |
case 3:{ |
| 5501 |
size_t l; |
| 5502 |
l=(luaV_tostring(L,o)?tsvalue(o)->len:0); |
| 5503 |
return l; |
| 5504 |
} |
| 5505 |
default:return 0; |
| 5506 |
} |
| 5507 |
} |
| 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; |
| 5511 |
} |
| 5512 |
static void*lua_touserdata(lua_State*L,int idx){ |
| 5513 |
StkId o=index2adr(L,idx); |
| 5514 |
switch(ttype(o)){ |
| 5515 |
case 7:return(rawuvalue(o)+1); |
| 5516 |
case 2:return pvalue(o); |
| 5517 |
default:return NULL; |
| 5518 |
} |
| 5519 |
} |
| 5520 |
static void lua_pushnil(lua_State*L){ |
| 5521 |
setnilvalue(L->top); |
| 5522 |
api_incr_top(L); |
| 5523 |
} |
| 5524 |
static void lua_pushnumber(lua_State*L,lua_Number n){ |
| 5525 |
setnvalue(L->top,n); |
| 5526 |
api_incr_top(L); |
| 5527 |
} |
| 5528 |
static void lua_pushinteger(lua_State*L,lua_Integer n){ |
| 5529 |
setnvalue(L->top,cast_num(n)); |
| 5530 |
api_incr_top(L); |
| 5531 |
} |
| 5532 |
static void lua_pushlstring(lua_State*L,const char*s,size_t len){ |
| 5533 |
luaC_checkGC(L); |
| 5534 |
setsvalue(L,L->top,luaS_newlstr(L,s,len)); |
| 5535 |
api_incr_top(L); |
| 5536 |
} |
| 5537 |
static void lua_pushstring(lua_State*L,const char*s){ |
| 5538 |
if(s==NULL) |
| 5539 |
lua_pushnil(L); |
| 5540 |
else |
| 5541 |
lua_pushlstring(L,s,strlen(s)); |
| 5542 |
} |
| 5543 |
static const char*lua_pushvfstring(lua_State*L,const char*fmt, |
| 5544 |
va_list argp){ |
| 5545 |
const char*ret; |
| 5546 |
luaC_checkGC(L); |
| 5547 |
ret=luaO_pushvfstring(L,fmt,argp); |
| 5548 |
return ret; |
| 5549 |
} |
| 5550 |
static const char*lua_pushfstring(lua_State*L,const char*fmt,...){ |
| 5551 |
const char*ret; |
| 5552 |
va_list argp; |
| 5553 |
luaC_checkGC(L); |
| 5554 |
va_start(argp,fmt); |
| 5555 |
ret=luaO_pushvfstring(L,fmt,argp); |
| 5556 |
va_end(argp); |
| 5557 |
return ret; |
| 5558 |
} |
| 5559 |
static void lua_pushcclosure(lua_State*L,lua_CFunction fn,int n){ |
| 5560 |
Closure*cl; |
| 5561 |
luaC_checkGC(L); |
| 5562 |
api_checknelems(L,n); |
| 5563 |
cl=luaF_newCclosure(L,n,getcurrenv(L)); |
| 5564 |
cl->c.f=fn; |
| 5565 |
L->top-=n; |
| 5566 |
while(n--) |
| 5567 |
setobj(L,&cl->c.upvalue[n],L->top+n); |
| 5568 |
setclvalue(L,L->top,cl); |
| 5569 |
api_incr_top(L); |
| 5570 |
} |
| 5571 |
static void lua_pushboolean(lua_State*L,int b){ |
| 5572 |
setbvalue(L->top,(b!=0)); |
| 5573 |
api_incr_top(L); |
| 5574 |
} |
| 5575 |
static int lua_pushthread(lua_State*L){ |
| 5576 |
setthvalue(L,L->top,L); |
| 5577 |
api_incr_top(L); |
| 5578 |
return(G(L)->mainthread==L); |
| 5579 |
} |
| 5580 |
static void lua_gettable(lua_State*L,int idx){ |
| 5581 |
StkId t; |
| 5582 |
t=index2adr(L,idx); |
| 5583 |
api_checkvalidindex(L,t); |
| 5584 |
luaV_gettable(L,t,L->top-1,L->top-1); |
| 5585 |
} |
| 5586 |
static void lua_getfield(lua_State*L,int idx,const char*k){ |
| 5587 |
StkId t; |
| 5588 |
TValue key; |
| 5589 |
t=index2adr(L,idx); |
| 5590 |
api_checkvalidindex(L,t); |
| 5591 |
setsvalue(L,&key,luaS_new(L,k)); |
| 5592 |
luaV_gettable(L,t,&key,L->top); |
| 5593 |
api_incr_top(L); |
| 5594 |
} |
| 5595 |
static void lua_rawget(lua_State*L,int idx){ |
| 5596 |
StkId t; |
| 5597 |
t=index2adr(L,idx); |
| 5598 |
luai_apicheck(L,ttistable(t)); |
| 5599 |
setobj(L,L->top-1,luaH_get(hvalue(t),L->top-1)); |
| 5600 |
} |
| 5601 |
static void lua_rawgeti(lua_State*L,int idx,int n){ |
| 5602 |
StkId o; |
| 5603 |
o=index2adr(L,idx); |
| 5604 |
luai_apicheck(L,ttistable(o)); |
| 5605 |
setobj(L,L->top,luaH_getnum(hvalue(o),n)); |
| 5606 |
api_incr_top(L); |
| 5607 |
} |
| 5608 |
static void lua_createtable(lua_State*L,int narray,int nrec){ |
| 5609 |
luaC_checkGC(L); |
| 5610 |
sethvalue(L,L->top,luaH_new(L,narray,nrec)); |
| 5611 |
api_incr_top(L); |
| 5612 |
} |
| 5613 |
static int lua_getmetatable(lua_State*L,int objindex){ |
| 5614 |
const TValue*obj; |
| 5615 |
Table*mt=NULL; |
| 5616 |
int res; |
| 5617 |
obj=index2adr(L,objindex); |
| 5618 |
switch(ttype(obj)){ |
| 5619 |
case 5: |
| 5620 |
mt=hvalue(obj)->metatable; |
| 5621 |
break; |
| 5622 |
case 7: |
| 5623 |
mt=uvalue(obj)->metatable; |
| 5624 |
break; |
| 5625 |
default: |
| 5626 |
mt=G(L)->mt[ttype(obj)]; |
| 5627 |
break; |
| 5628 |
} |
| 5629 |
if(mt==NULL) |
| 5630 |
res=0; |
| 5631 |
else{ |
| 5632 |
sethvalue(L,L->top,mt); |
| 5633 |
api_incr_top(L); |
| 5634 |
res=1; |
| 5635 |
} |
| 5636 |
return res; |
| 5637 |
} |
| 5638 |
static void lua_getfenv(lua_State*L,int idx){ |
| 5639 |
StkId o; |
| 5640 |
o=index2adr(L,idx); |
| 5641 |
api_checkvalidindex(L,o); |
| 5642 |
switch(ttype(o)){ |
| 5643 |
case 6: |
| 5644 |
sethvalue(L,L->top,clvalue(o)->c.env); |
| 5645 |
break; |
| 5646 |
case 7: |
| 5647 |
sethvalue(L,L->top,uvalue(o)->env); |
| 5648 |
break; |
| 5649 |
case 8: |
| 5650 |
setobj(L,L->top,gt(thvalue(o))); |
| 5651 |
break; |
| 5652 |
default: |
| 5653 |
setnilvalue(L->top); |
| 5654 |
break; |
| 5655 |
} |
| 5656 |
api_incr_top(L); |
| 5657 |
} |
| 5658 |
static void lua_settable(lua_State*L,int idx){ |
| 5659 |
StkId t; |
| 5660 |
api_checknelems(L,2); |
| 5661 |
t=index2adr(L,idx); |
| 5662 |
api_checkvalidindex(L,t); |
| 5663 |
luaV_settable(L,t,L->top-2,L->top-1); |
| 5664 |
L->top-=2; |
| 5665 |
} |
| 5666 |
static void lua_setfield(lua_State*L,int idx,const char*k){ |
| 5667 |
StkId t; |
| 5668 |
TValue key; |
| 5669 |
api_checknelems(L,1); |
| 5670 |
t=index2adr(L,idx); |
| 5671 |
api_checkvalidindex(L,t); |
| 5672 |
setsvalue(L,&key,luaS_new(L,k)); |
| 5673 |
luaV_settable(L,t,&key,L->top-1); |
| 5674 |
L->top--; |
| 5675 |
} |
| 5676 |
static void lua_rawset(lua_State*L,int idx){ |
| 5677 |
StkId t; |
| 5678 |
api_checknelems(L,2); |
| 5679 |
t=index2adr(L,idx); |
| 5680 |
luai_apicheck(L,ttistable(t)); |
| 5681 |
setobj(L,luaH_set(L,hvalue(t),L->top-2),L->top-1); |
| 5682 |
luaC_barriert(L,hvalue(t),L->top-1); |
| 5683 |
L->top-=2; |
| 5684 |
} |
| 5685 |
static void lua_rawseti(lua_State*L,int idx,int n){ |
| 5686 |
StkId o; |
| 5687 |
api_checknelems(L,1); |
| 5688 |
o=index2adr(L,idx); |
| 5689 |
luai_apicheck(L,ttistable(o)); |
| 5690 |
setobj(L,luaH_setnum(L,hvalue(o),n),L->top-1); |
| 5691 |
luaC_barriert(L,hvalue(o),L->top-1); |
| 5692 |
L->top--; |
| 5693 |
} |
| 5694 |
static int lua_setmetatable(lua_State*L,int objindex){ |
| 5695 |
TValue*obj; |
| 5696 |
Table*mt; |
| 5697 |
api_checknelems(L,1); |
| 5698 |
obj=index2adr(L,objindex); |
| 5699 |
api_checkvalidindex(L,obj); |
| 5700 |
if(ttisnil(L->top-1)) |
| 5701 |
mt=NULL; |
| 5702 |
else{ |
| 5703 |
luai_apicheck(L,ttistable(L->top-1)); |
| 5704 |
mt=hvalue(L->top-1); |
| 5705 |
} |
| 5706 |
switch(ttype(obj)){ |
| 5707 |
case 5:{ |
| 5708 |
hvalue(obj)->metatable=mt; |
| 5709 |
if(mt) |
| 5710 |
luaC_objbarriert(L,hvalue(obj),mt); |
| 5711 |
break; |
| 5712 |
} |
| 5713 |
case 7:{ |
| 5714 |
uvalue(obj)->metatable=mt; |
| 5715 |
if(mt) |
| 5716 |
luaC_objbarrier(L,rawuvalue(obj),mt); |
| 5717 |
break; |
| 5718 |
} |
| 5719 |
default:{ |
| 5720 |
G(L)->mt[ttype(obj)]=mt; |
| 5721 |
break; |
| 5722 |
} |
| 5723 |
} |
| 5724 |
L->top--; |
| 5725 |
return 1; |
| 5726 |
} |
| 5727 |
static int lua_setfenv(lua_State*L,int idx){ |
| 5728 |
StkId o; |
| 5729 |
int res=1; |
| 5730 |
api_checknelems(L,1); |
| 5731 |
o=index2adr(L,idx); |
| 5732 |
api_checkvalidindex(L,o); |
| 5733 |
luai_apicheck(L,ttistable(L->top-1)); |
| 5734 |
switch(ttype(o)){ |
| 5735 |
case 6: |
| 5736 |
clvalue(o)->c.env=hvalue(L->top-1); |
| 5737 |
break; |
| 5738 |
case 7: |
| 5739 |
uvalue(o)->env=hvalue(L->top-1); |
| 5740 |
break; |
| 5741 |
case 8: |
| 5742 |
sethvalue(L,gt(thvalue(o)),hvalue(L->top-1)); |
| 5743 |
break; |
| 5744 |
default: |
| 5745 |
res=0; |
| 5746 |
break; |
| 5747 |
} |
| 5748 |
if(res)luaC_objbarrier(L,gcvalue(o),hvalue(L->top-1)); |
| 5749 |
L->top--; |
| 5750 |
return res; |
| 5751 |
} |
| 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){ |
| 5755 |
StkId func; |
| 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); |
| 5761 |
} |
| 5762 |
struct CallS{ |
| 5763 |
StkId func; |
| 5764 |
int nresults; |
| 5765 |
}; |
| 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); |
| 5769 |
} |
| 5770 |
static int lua_pcall(lua_State*L,int nargs,int nresults,int errfunc){ |
| 5771 |
struct CallS c; |
| 5772 |
int status; |
| 5773 |
ptrdiff_t func; |
| 5774 |
api_checknelems(L,nargs+1); |
| 5775 |
checkresults(L,nargs,nresults); |
| 5776 |
if(errfunc==0) |
| 5777 |
func=0; |
| 5778 |
else{ |
| 5779 |
StkId o=index2adr(L,errfunc); |
| 5780 |
api_checkvalidindex(L,o); |
| 5781 |
func=savestack(L,o); |
| 5782 |
} |
| 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); |
| 5787 |
return status; |
| 5788 |
} |
| 5789 |
static int lua_load(lua_State*L,lua_Reader reader,void*data, |
| 5790 |
const char*chunkname){ |
| 5791 |
ZIO z; |
| 5792 |
int status; |
| 5793 |
if(!chunkname)chunkname="?"; |
| 5794 |
luaZ_init(L,&z,reader,data); |
| 5795 |
status=luaD_protectedparser(L,&z,chunkname); |
| 5796 |
return status; |
| 5797 |
} |
| 5798 |
static int lua_error(lua_State*L){ |
| 5799 |
api_checknelems(L,1); |
| 5800 |
luaG_errormsg(L); |
| 5801 |
return 0; |
| 5802 |
} |
| 5803 |
static int lua_next(lua_State*L,int idx){ |
| 5804 |
StkId t; |
| 5805 |
int more; |
| 5806 |
t=index2adr(L,idx); |
| 5807 |
luai_apicheck(L,ttistable(t)); |
| 5808 |
more=luaH_next(L,hvalue(t),L->top-1); |
| 5809 |
if(more){ |
| 5810 |
api_incr_top(L); |
| 5811 |
} |
| 5812 |
else |
| 5813 |
L->top-=1; |
| 5814 |
return more; |
| 5815 |
} |
| 5816 |
static void lua_concat(lua_State*L,int n){ |
| 5817 |
api_checknelems(L,n); |
| 5818 |
if(n>=2){ |
| 5819 |
luaC_checkGC(L); |
| 5820 |
luaV_concat(L,n,cast_int(L->top-L->base)-1); |
| 5821 |
L->top-=(n-1); |
| 5822 |
} |
| 5823 |
else if(n==0){ |
| 5824 |
setsvalue(L,L->top,luaS_newlstr(L,"",0)); |
| 5825 |
api_incr_top(L); |
| 5826 |
} |
| 5827 |
} |
| 5828 |
static void*lua_newuserdata(lua_State*L,size_t size){ |
| 5829 |
Udata*u; |
| 5830 |
luaC_checkGC(L); |
| 5831 |
u=luaS_newudata(L,size,getcurrenv(L)); |
| 5832 |
setuvalue(L,L->top,u); |
| 5833 |
api_incr_top(L); |
| 5834 |
return u+1; |
| 5835 |
} |
| 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{ |
| 5839 |
const char*name; |
| 5840 |
lua_CFunction func; |
| 5841 |
}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, |
| 5846 |
size_t*l); |
| 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, |
| 5851 |
lua_Integer def); |
| 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{ |
| 5864 |
char*p; |
| 5865 |
int lvl; |
| 5866 |
lua_State*L; |
| 5867 |
char buffer[BUFSIZ]; |
| 5868 |
}luaL_Buffer; |
| 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){ |
| 5873 |
lua_Debug ar; |
| 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){ |
| 5878 |
narg--; |
| 5879 |
if(narg==0) |
| 5880 |
return luaL_error(L,"calling "LUA_QL("%s")" on bad self (%s)", |
| 5881 |
ar.name,extramsg); |
| 5882 |
} |
| 5883 |
if(ar.name==NULL) |
| 5884 |
ar.name="?"; |
| 5885 |
return luaL_error(L,"bad argument #%d to "LUA_QL("%s")" (%s)", |
| 5886 |
narg,ar.name,extramsg); |
| 5887 |
} |
| 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); |
| 5892 |
} |
| 5893 |
static void tag_error(lua_State*L,int narg,int tag){ |
| 5894 |
luaL_typerror(L,narg,lua_typename(L,tag)); |
| 5895 |
} |
| 5896 |
static void luaL_where(lua_State*L,int level){ |
| 5897 |
lua_Debug ar; |
| 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); |
| 5902 |
return; |
| 5903 |
} |
| 5904 |
} |
| 5905 |
lua_pushliteral(L,""); |
| 5906 |
} |
| 5907 |
static int luaL_error(lua_State*L,const char*fmt,...){ |
| 5908 |
va_list argp; |
| 5909 |
va_start(argp,fmt); |
| 5910 |
luaL_where(L,1); |
| 5911 |
lua_pushvfstring(L,fmt,argp); |
| 5912 |
va_end(argp); |
| 5913 |
lua_concat(L,2); |
| 5914 |
return lua_error(L); |
| 5915 |
} |
| 5916 |
static int luaL_newmetatable(lua_State*L,const char*tname){ |
| 5917 |
lua_getfield(L,(-10000),tname); |
| 5918 |
if(!lua_isnil(L,-1)) |
| 5919 |
return 0; |
| 5920 |
lua_pop(L,1); |
| 5921 |
lua_newtable(L); |
| 5922 |
lua_pushvalue(L,-1); |
| 5923 |
lua_setfield(L,(-10000),tname); |
| 5924 |
return 1; |
| 5925 |
} |
| 5926 |
static void*luaL_checkudata(lua_State*L,int ud,const char*tname){ |
| 5927 |
void*p=lua_touserdata(L,ud); |
| 5928 |
if(p!=NULL){ |
| 5929 |
if(lua_getmetatable(L,ud)){ |
| 5930 |
lua_getfield(L,(-10000),tname); |
| 5931 |
if(lua_rawequal(L,-1,-2)){ |
| 5932 |
lua_pop(L,2); |
| 5933 |
return p; |
| 5934 |
} |
| 5935 |
} |
| 5936 |
} |
| 5937 |
luaL_typerror(L,ud,tname); |
| 5938 |
return NULL; |
| 5939 |
} |
| 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); |
| 5943 |
} |
| 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); |
| 5947 |
} |
| 5948 |
static void luaL_checkany(lua_State*L,int narg){ |
| 5949 |
if(lua_type(L,narg)==(-1)) |
| 5950 |
luaL_argerror(L,narg,"value expected"); |
| 5951 |
} |
| 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); |
| 5955 |
return s; |
| 5956 |
} |
| 5957 |
static const char*luaL_optlstring(lua_State*L,int narg, |
| 5958 |
const char*def,size_t*len){ |
| 5959 |
if(lua_isnoneornil(L,narg)){ |
| 5960 |
if(len) |
| 5961 |
*len=(def?strlen(def):0); |
| 5962 |
return def; |
| 5963 |
} |
| 5964 |
else return luaL_checklstring(L,narg,len); |
| 5965 |
} |
| 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); |
| 5970 |
return d; |
| 5971 |
} |
| 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); |
| 5976 |
return d; |
| 5977 |
} |
| 5978 |
static lua_Integer luaL_optinteger(lua_State*L,int narg, |
| 5979 |
lua_Integer def){ |
| 5980 |
return luaL_opt(L,luaL_checkinteger,narg,def); |
| 5981 |
} |
| 5982 |
static int luaL_getmetafield(lua_State*L,int obj,const char*event){ |
| 5983 |
if(!lua_getmetatable(L,obj)) |
| 5984 |
return 0; |
| 5985 |
lua_pushstring(L,event); |
| 5986 |
lua_rawget(L,-2); |
| 5987 |
if(lua_isnil(L,-1)){ |
| 5988 |
lua_pop(L,2); |
| 5989 |
return 0; |
| 5990 |
} |
| 5991 |
else{ |
| 5992 |
lua_remove(L,-2); |
| 5993 |
return 1; |
| 5994 |
} |
| 5995 |
} |
| 5996 |
static void luaL_register(lua_State*L,const char*libname, |
| 5997 |
const luaL_Reg*l){ |
| 5998 |
luaI_openlib(L,libname,l,0); |
| 5999 |
} |
| 6000 |
static int libsize(const luaL_Reg*l){ |
| 6001 |
int size=0; |
| 6002 |
for(;l->name;l++)size++; |
| 6003 |
return size; |
| 6004 |
} |
| 6005 |
static void luaI_openlib(lua_State*L,const char*libname, |
| 6006 |
const luaL_Reg*l,int nup){ |
| 6007 |
if(libname){ |
| 6008 |
int size=libsize(l); |
| 6009 |
luaL_findtable(L,(-10000),"_LOADED",1); |
| 6010 |
lua_getfield(L,-1,libname); |
| 6011 |
if(!lua_istable(L,-1)){ |
| 6012 |
lua_pop(L,1); |
| 6013 |
if(luaL_findtable(L,(-10002),libname,size)!=NULL) |
| 6014 |
luaL_error(L,"name conflict for module "LUA_QL("%s"),libname); |
| 6015 |
lua_pushvalue(L,-1); |
| 6016 |
lua_setfield(L,-3,libname); |
| 6017 |
} |
| 6018 |
lua_remove(L,-2); |
| 6019 |
lua_insert(L,-(nup+1)); |
| 6020 |
} |
| 6021 |
for(;l->name;l++){ |
| 6022 |
int i; |
| 6023 |
for(i=0;i<nup;i++) |
| 6024 |
lua_pushvalue(L,-nup); |
| 6025 |
lua_pushcclosure(L,l->func,nup); |
| 6026 |
lua_setfield(L,-(nup+2),l->name); |
| 6027 |
} |
| 6028 |
lua_pop(L,nup); |
| 6029 |
} |
| 6030 |
static const char*luaL_findtable(lua_State*L,int idx, |
| 6031 |
const char*fname,int szhint){ |
| 6032 |
const char*e; |
| 6033 |
lua_pushvalue(L,idx); |
| 6034 |
do{ |
| 6035 |
e=strchr(fname,'.'); |
| 6036 |
if(e==NULL)e=fname+strlen(fname); |
| 6037 |
lua_pushlstring(L,fname,e-fname); |
| 6038 |
lua_rawget(L,-2); |
| 6039 |
if(lua_isnil(L,-1)){ |
| 6040 |
lua_pop(L,1); |
| 6041 |
lua_createtable(L,0,(*e=='.'?1:szhint)); |
| 6042 |
lua_pushlstring(L,fname,e-fname); |
| 6043 |
lua_pushvalue(L,-2); |
| 6044 |
lua_settable(L,-4); |
| 6045 |
} |
| 6046 |
else if(!lua_istable(L,-1)){ |
| 6047 |
lua_pop(L,2); |
| 6048 |
return fname; |
| 6049 |
} |
| 6050 |
lua_remove(L,-2); |
| 6051 |
fname=e+1; |
| 6052 |
}while(*e=='.'); |
| 6053 |
return NULL; |
| 6054 |
} |
| 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); |
| 6059 |
if(l==0)return 0; |
| 6060 |
else{ |
| 6061 |
lua_pushlstring(B->L,B->buffer,l); |
| 6062 |
B->p=B->buffer; |
| 6063 |
B->lvl++; |
| 6064 |
return 1; |
| 6065 |
} |
| 6066 |
} |
| 6067 |
static void adjuststack(luaL_Buffer*B){ |
| 6068 |
if(B->lvl>1){ |
| 6069 |
lua_State*L=B->L; |
| 6070 |
int toget=1; |
| 6071 |
size_t toplen=lua_strlen(L,-1); |
| 6072 |
do{ |
| 6073 |
size_t l=lua_strlen(L,-(toget+1)); |
| 6074 |
if(B->lvl-toget+1>=(20/2)||toplen>l){ |
| 6075 |
toplen+=l; |
| 6076 |
toget++; |
| 6077 |
} |
| 6078 |
else break; |
| 6079 |
}while(toget<B->lvl); |
| 6080 |
lua_concat(L,toget); |
| 6081 |
B->lvl=B->lvl-toget+1; |
| 6082 |
} |
| 6083 |
} |
| 6084 |
static char*luaL_prepbuffer(luaL_Buffer*B){ |
| 6085 |
if(emptybuffer(B)) |
| 6086 |
adjuststack(B); |
| 6087 |
return B->buffer; |
| 6088 |
} |
| 6089 |
static void luaL_addlstring(luaL_Buffer*B,const char*s,size_t l){ |
| 6090 |
while(l--) |
| 6091 |
luaL_addchar(B,*s++); |
| 6092 |
} |
| 6093 |
static void luaL_pushresult(luaL_Buffer*B){ |
| 6094 |
emptybuffer(B); |
| 6095 |
lua_concat(B->L,B->lvl); |
| 6096 |
B->lvl=1; |
| 6097 |
} |
| 6098 |
static void luaL_addvalue(luaL_Buffer*B){ |
| 6099 |
lua_State*L=B->L; |
| 6100 |
size_t vl; |
| 6101 |
const char*s=lua_tolstring(L,-1,&vl); |
| 6102 |
if(vl<=bufffree(B)){ |
| 6103 |
memcpy(B->p,s,vl); |
| 6104 |
B->p+=vl; |
| 6105 |
lua_pop(L,1); |
| 6106 |
} |
| 6107 |
else{ |
| 6108 |
if(emptybuffer(B)) |
| 6109 |
lua_insert(L,-2); |
| 6110 |
B->lvl++; |
| 6111 |
adjuststack(B); |
| 6112 |
} |
| 6113 |
} |
| 6114 |
static void luaL_buffinit(lua_State*L,luaL_Buffer*B){ |
| 6115 |
B->L=L; |
| 6116 |
B->p=B->buffer; |
| 6117 |
B->lvl=0; |
| 6118 |
} |
| 6119 |
typedef struct LoadF{ |
| 6120 |
int extraline; |
| 6121 |
FILE*f; |
| 6122 |
char buff[BUFSIZ]; |
| 6123 |
}LoadF; |
| 6124 |
static const char*getF(lua_State*L,void*ud,size_t*size){ |
| 6125 |
LoadF*lf=(LoadF*)ud; |
| 6126 |
(void)L; |
| 6127 |
if(lf->extraline){ |
| 6128 |
lf->extraline=0; |
| 6129 |
*size=1; |
| 6130 |
return"\n"; |
| 6131 |
} |
| 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; |
| 6135 |
} |
| 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); |
| 6141 |
return(5+1); |
| 6142 |
} |
| 6143 |
static int luaL_loadfile(lua_State*L,const char*filename){ |
| 6144 |
LoadF lf; |
| 6145 |
int status,readstatus; |
| 6146 |
int c; |
| 6147 |
int fnameindex=lua_gettop(L)+1; |
| 6148 |
lf.extraline=0; |
| 6149 |
if(filename==NULL){ |
| 6150 |
lua_pushliteral(L,"=stdin"); |
| 6151 |
lf.f=stdin; |
| 6152 |
} |
| 6153 |
else{ |
| 6154 |
lua_pushfstring(L,"@%s",filename); |
| 6155 |
lf.f=fopen(filename,"r"); |
| 6156 |
if(lf.f==NULL)return errfile(L,"open",fnameindex); |
| 6157 |
} |
| 6158 |
c=getc(lf.f); |
| 6159 |
if(c=='#'){ |
| 6160 |
lf.extraline=1; |
| 6161 |
while((c=getc(lf.f))!=EOF&&c!='\n'); |
| 6162 |
if(c=='\n')c=getc(lf.f); |
| 6163 |
} |
| 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]); |
| 6168 |
lf.extraline=0; |
| 6169 |
} |
| 6170 |
ungetc(c,lf.f); |
| 6171 |
status=lua_load(L,getF,&lf,lua_tostring(L,-1)); |
| 6172 |
readstatus=ferror(lf.f); |
| 6173 |
if(filename)fclose(lf.f); |
| 6174 |
if(readstatus){ |
| 6175 |
lua_settop(L,fnameindex); |
| 6176 |
return errfile(L,"read",fnameindex); |
| 6177 |
} |
| 6178 |
lua_remove(L,fnameindex); |
| 6179 |
return status; |
| 6180 |
} |
| 6181 |
typedef struct LoadS{ |
| 6182 |
const char*s; |
| 6183 |
size_t size; |
| 6184 |
}LoadS; |
| 6185 |
static const char*getS(lua_State*L,void*ud,size_t*size){ |
| 6186 |
LoadS*ls=(LoadS*)ud; |
| 6187 |
(void)L; |
| 6188 |
if(ls->size==0)return NULL; |
| 6189 |
*size=ls->size; |
| 6190 |
ls->size=0; |
| 6191 |
return ls->s; |
| 6192 |
} |
| 6193 |
static int luaL_loadbuffer(lua_State*L,const char*buff,size_t size, |
| 6194 |
const char*name){ |
| 6195 |
LoadS ls; |
| 6196 |
ls.s=buff; |
| 6197 |
ls.size=size; |
| 6198 |
return lua_load(L,getS,&ls,name); |
| 6199 |
} |
| 6200 |
static void*l_alloc(void*ud,void*ptr,size_t osize,size_t nsize){ |
| 6201 |
(void)ud; |
| 6202 |
(void)osize; |
| 6203 |
if(nsize==0){ |
| 6204 |
free(ptr); |
| 6205 |
return NULL; |
| 6206 |
} |
| 6207 |
else |
| 6208 |
return realloc(ptr,nsize); |
| 6209 |
} |
| 6210 |
static int panic(lua_State*L){ |
| 6211 |
(void)L; |
| 6212 |
fprintf(stderr,"PANIC: unprotected error in call to Lua API (%s)\n", |
| 6213 |
lua_tostring(L,-1)); |
| 6214 |
return 0; |
| 6215 |
} |
| 6216 |
static lua_State*luaL_newstate(void){ |
| 6217 |
lua_State*L=lua_newstate(l_alloc,NULL); |
| 6218 |
if(L)lua_atpanic(L,&panic); |
| 6219 |
return L; |
| 6220 |
} |
| 6221 |
static int luaB_tonumber(lua_State*L){ |
| 6222 |
int base=luaL_optint(L,2,10); |
| 6223 |
if(base==10){ |
| 6224 |
luaL_checkany(L,1); |
| 6225 |
if(lua_isnumber(L,1)){ |
| 6226 |
lua_pushnumber(L,lua_tonumber(L,1)); |
| 6227 |
return 1; |
| 6228 |
} |
| 6229 |
} |
| 6230 |
else{ |
| 6231 |
const char*s1=luaL_checkstring(L,1); |
| 6232 |
char*s2; |
| 6233 |
unsigned long n; |
| 6234 |
luaL_argcheck(L,2<=base&&base<=36,2,"base out of range"); |
| 6235 |
n=strtoul(s1,&s2,base); |
| 6236 |
if(s1!=s2){ |
| 6237 |
while(isspace((unsigned char)(*s2)))s2++; |
| 6238 |
if(*s2=='\0'){ |
| 6239 |
lua_pushnumber(L,(lua_Number)n); |
| 6240 |
return 1; |
| 6241 |
} |
| 6242 |
} |
| 6243 |
} |
| 6244 |
lua_pushnil(L); |
| 6245 |
return 1; |
| 6246 |
} |
| 6247 |
static int luaB_error(lua_State*L){ |
| 6248 |
int level=luaL_optint(L,2,1); |
| 6249 |
lua_settop(L,1); |
| 6250 |
if(lua_isstring(L,1)&&level>0){ |
| 6251 |
luaL_where(L,level); |
| 6252 |
lua_pushvalue(L,1); |
| 6253 |
lua_concat(L,2); |
| 6254 |
} |
| 6255 |
return lua_error(L); |
| 6256 |
} |
| 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"); |
| 6264 |
lua_settop(L,2); |
| 6265 |
lua_setmetatable(L,1); |
| 6266 |
return 1; |
| 6267 |
} |
| 6268 |
static void getfunc(lua_State*L,int opt){ |
| 6269 |
if(lua_isfunction(L,1))lua_pushvalue(L,1); |
| 6270 |
else{ |
| 6271 |
lua_Debug ar; |
| 6272 |
int level=opt?luaL_optint(L,1,1):luaL_checkint(L,1); |
| 6273 |
luaL_argcheck(L,level>=0,1,"level must be non-negative"); |
| 6274 |
if(lua_getstack(L,level,&ar)==0) |
| 6275 |
luaL_argerror(L,1,"invalid level"); |
| 6276 |
lua_getinfo(L,"f",&ar); |
| 6277 |
if(lua_isnil(L,-1)) |
| 6278 |
luaL_error(L,"no function environment for tail call at level %d", |
| 6279 |
level); |
| 6280 |
} |
| 6281 |
} |
| 6282 |
static int luaB_setfenv(lua_State*L){ |
| 6283 |
luaL_checktype(L,2,5); |
| 6284 |
getfunc(L,0); |
| 6285 |
lua_pushvalue(L,2); |
| 6286 |
if(lua_isnumber(L,1)&&lua_tonumber(L,1)==0){ |
| 6287 |
lua_pushthread(L); |
| 6288 |
lua_insert(L,-2); |
| 6289 |
lua_setfenv(L,-2); |
| 6290 |
return 0; |
| 6291 |
} |
| 6292 |
else if(lua_iscfunction(L,-2)||lua_setfenv(L,-2)==0) |
| 6293 |
luaL_error(L, |
| 6294 |
LUA_QL("setfenv")" cannot change environment of given object"); |
| 6295 |
return 1; |
| 6296 |
} |
| 6297 |
static int luaB_rawget(lua_State*L){ |
| 6298 |
luaL_checktype(L,1,5); |
| 6299 |
luaL_checkany(L,2); |
| 6300 |
lua_settop(L,2); |
| 6301 |
lua_rawget(L,1); |
| 6302 |
return 1; |
| 6303 |
} |
| 6304 |
static int luaB_type(lua_State*L){ |
| 6305 |
luaL_checkany(L,1); |
| 6306 |
lua_pushstring(L,luaL_typename(L,1)); |
| 6307 |
return 1; |
| 6308 |
} |
| 6309 |
static int luaB_next(lua_State*L){ |
| 6310 |
luaL_checktype(L,1,5); |
| 6311 |
lua_settop(L,2); |
| 6312 |
if(lua_next(L,1)) |
| 6313 |
return 2; |
| 6314 |
else{ |
| 6315 |
lua_pushnil(L); |
| 6316 |
return 1; |
| 6317 |
} |
| 6318 |
} |
| 6319 |
static int luaB_pairs(lua_State*L){ |
| 6320 |
luaL_checktype(L,1,5); |
| 6321 |
lua_pushvalue(L,lua_upvalueindex(1)); |
| 6322 |
lua_pushvalue(L,1); |
| 6323 |
lua_pushnil(L); |
| 6324 |
return 3; |
| 6325 |
} |
| 6326 |
static int ipairsaux(lua_State*L){ |
| 6327 |
int i=luaL_checkint(L,2); |
| 6328 |
luaL_checktype(L,1,5); |
| 6329 |
i++; |
| 6330 |
lua_pushinteger(L,i); |
| 6331 |
lua_rawgeti(L,1,i); |
| 6332 |
return(lua_isnil(L,-1))?0:2; |
| 6333 |
} |
| 6334 |
static int luaB_ipairs(lua_State*L){ |
| 6335 |
luaL_checktype(L,1,5); |
| 6336 |
lua_pushvalue(L,lua_upvalueindex(1)); |
| 6337 |
lua_pushvalue(L,1); |
| 6338 |
lua_pushinteger(L,0); |
| 6339 |
return 3; |
| 6340 |
} |
| 6341 |
static int load_aux(lua_State*L,int status){ |
| 6342 |
if(status==0) |
| 6343 |
return 1; |
| 6344 |
else{ |
| 6345 |
lua_pushnil(L); |
| 6346 |
lua_insert(L,-2); |
| 6347 |
return 2; |
| 6348 |
} |
| 6349 |
} |
| 6350 |
static int luaB_loadstring(lua_State*L){ |
| 6351 |
size_t 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)); |
| 6355 |
} |
| 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)); |
| 6359 |
} |
| 6360 |
static int luaB_assert(lua_State*L){ |
| 6361 |
luaL_checkany(L,1); |
| 6362 |
if(!lua_toboolean(L,1)) |
| 6363 |
return luaL_error(L,"%s",luaL_optstring(L,2,"assertion failed!")); |
| 6364 |
return lua_gettop(L); |
| 6365 |
} |
| 6366 |
static int luaB_unpack(lua_State*L){ |
| 6367 |
int i,e,n; |
| 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)); |
| 6371 |
if(i>e)return 0; |
| 6372 |
n=e-i+1; |
| 6373 |
if(n<=0||!lua_checkstack(L,n)) |
| 6374 |
return luaL_error(L,"too many results to unpack"); |
| 6375 |
lua_rawgeti(L,1,i); |
| 6376 |
while(i++<e) |
| 6377 |
lua_rawgeti(L,1,i); |
| 6378 |
return n; |
| 6379 |
} |
| 6380 |
static int luaB_pcall(lua_State*L){ |
| 6381 |
int status; |
| 6382 |
luaL_checkany(L,1); |
| 6383 |
status=lua_pcall(L,lua_gettop(L)-1,(-1),0); |
| 6384 |
lua_pushboolean(L,(status==0)); |
| 6385 |
lua_insert(L,1); |
| 6386 |
return lua_gettop(L); |
| 6387 |
} |
| 6388 |
static int luaB_newproxy(lua_State*L){ |
| 6389 |
lua_settop(L,1); |
| 6390 |
lua_newuserdata(L,0); |
| 6391 |
if(lua_toboolean(L,1)==0) |
| 6392 |
return 1; |
| 6393 |
else if(lua_isboolean(L,1)){ |
| 6394 |
lua_newtable(L); |
| 6395 |
lua_pushvalue(L,-1); |
| 6396 |
lua_pushboolean(L,1); |
| 6397 |
lua_rawset(L,lua_upvalueindex(1)); |
| 6398 |
} |
| 6399 |
else{ |
| 6400 |
int validproxy=0; |
| 6401 |
if(lua_getmetatable(L,1)){ |
| 6402 |
lua_rawget(L,lua_upvalueindex(1)); |
| 6403 |
validproxy=lua_toboolean(L,-1); |
| 6404 |
lua_pop(L,1); |
| 6405 |
} |
| 6406 |
luaL_argcheck(L,validproxy,1,"boolean or proxy expected"); |
| 6407 |
lua_getmetatable(L,1); |
| 6408 |
} |
| 6409 |
lua_setmetatable(L,2); |
| 6410 |
return 1; |
| 6411 |
} |
| 6412 |
static const luaL_Reg base_funcs[]={ |
| 6413 |
{"assert",luaB_assert}, |
| 6414 |
{"error",luaB_error}, |
| 6415 |
{"loadfile",luaB_loadfile}, |
| 6416 |
{"loadstring",luaB_loadstring}, |
| 6417 |
{"next",luaB_next}, |
| 6418 |
{"pcall",luaB_pcall}, |
| 6419 |
{"rawget",luaB_rawget}, |
| 6420 |
{"setfenv",luaB_setfenv}, |
| 6421 |
{"setmetatable",luaB_setmetatable}, |
| 6422 |
{"tonumber",luaB_tonumber}, |
| 6423 |
{"type",luaB_type}, |
| 6424 |
{"unpack",luaB_unpack}, |
| 6425 |
{NULL,NULL} |
| 6426 |
}; |
| 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); |
| 6432 |
} |
| 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"); |
| 6448 |
} |
| 6449 |
static int luaopen_base(lua_State*L){ |
| 6450 |
base_open(L); |
| 6451 |
return 1; |
| 6452 |
} |
| 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; |
| 6456 |
int pos; |
| 6457 |
switch(lua_gettop(L)){ |
| 6458 |
case 2:{ |
| 6459 |
pos=e; |
| 6460 |
break; |
| 6461 |
} |
| 6462 |
case 3:{ |
| 6463 |
int i; |
| 6464 |
pos=luaL_checkint(L,2); |
| 6465 |
if(pos>e)e=pos; |
| 6466 |
for(i=e;i>pos;i--){ |
| 6467 |
lua_rawgeti(L,1,i-1); |
| 6468 |
lua_rawseti(L,1,i); |
| 6469 |
} |
| 6470 |
break; |
| 6471 |
} |
| 6472 |
default:{ |
| 6473 |
return luaL_error(L,"wrong number of arguments to "LUA_QL("insert")); |
| 6474 |
} |
| 6475 |
} |
| 6476 |
luaL_setn(L,1,e); |
| 6477 |
lua_rawseti(L,1,pos); |
| 6478 |
return 0; |
| 6479 |
} |
| 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)) |
| 6484 |
return 0; |
| 6485 |
luaL_setn(L,1,e-1); |
| 6486 |
lua_rawgeti(L,1,pos); |
| 6487 |
for(;pos<e;pos++){ |
| 6488 |
lua_rawgeti(L,1,pos+1); |
| 6489 |
lua_rawseti(L,1,pos); |
| 6490 |
} |
| 6491 |
lua_pushnil(L); |
| 6492 |
lua_rawseti(L,1,e); |
| 6493 |
return 1; |
| 6494 |
} |
| 6495 |
static void addfield(lua_State*L,luaL_Buffer*b,int i){ |
| 6496 |
lua_rawgeti(L,1,i); |
| 6497 |
if(!lua_isstring(L,-1)) |
| 6498 |
luaL_error(L,"invalid value (%s) at index %d in table for " |
| 6499 |
LUA_QL("concat"),luaL_typename(L,-1),i); |
| 6500 |
luaL_addvalue(b); |
| 6501 |
} |
| 6502 |
static int tconcat(lua_State*L){ |
| 6503 |
luaL_Buffer b; |
| 6504 |
size_t lsep; |
| 6505 |
int i,last; |
| 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); |
| 6511 |
for(;i<last;i++){ |
| 6512 |
addfield(L,&b,i); |
| 6513 |
luaL_addlstring(&b,sep,lsep); |
| 6514 |
} |
| 6515 |
if(i==last) |
| 6516 |
addfield(L,&b,i); |
| 6517 |
luaL_pushresult(&b); |
| 6518 |
return 1; |
| 6519 |
} |
| 6520 |
static void set2(lua_State*L,int i,int j){ |
| 6521 |
lua_rawseti(L,1,i); |
| 6522 |
lua_rawseti(L,1,j); |
| 6523 |
} |
| 6524 |
static int sort_comp(lua_State*L,int a,int b){ |
| 6525 |
if(!lua_isnil(L,2)){ |
| 6526 |
int res; |
| 6527 |
lua_pushvalue(L,2); |
| 6528 |
lua_pushvalue(L,a-1); |
| 6529 |
lua_pushvalue(L,b-2); |
| 6530 |
lua_call(L,2,1); |
| 6531 |
res=lua_toboolean(L,-1); |
| 6532 |
lua_pop(L,1); |
| 6533 |
return res; |
| 6534 |
} |
| 6535 |
else |
| 6536 |
return lua_lessthan(L,a,b); |
| 6537 |
} |
| 6538 |
static void auxsort(lua_State*L,int l,int u){ |
| 6539 |
while(l<u){ |
| 6540 |
int i,j; |
| 6541 |
lua_rawgeti(L,1,l); |
| 6542 |
lua_rawgeti(L,1,u); |
| 6543 |
if(sort_comp(L,-1,-2)) |
| 6544 |
set2(L,l,u); |
| 6545 |
else |
| 6546 |
lua_pop(L,2); |
| 6547 |
if(u-l==1)break; |
| 6548 |
i=(l+u)/2; |
| 6549 |
lua_rawgeti(L,1,i); |
| 6550 |
lua_rawgeti(L,1,l); |
| 6551 |
if(sort_comp(L,-2,-1)) |
| 6552 |
set2(L,i,l); |
| 6553 |
else{ |
| 6554 |
lua_pop(L,1); |
| 6555 |
lua_rawgeti(L,1,u); |
| 6556 |
if(sort_comp(L,-1,-2)) |
| 6557 |
set2(L,i,u); |
| 6558 |
else |
| 6559 |
lua_pop(L,2); |
| 6560 |
} |
| 6561 |
if(u-l==2)break; |
| 6562 |
lua_rawgeti(L,1,i); |
| 6563 |
lua_pushvalue(L,-1); |
| 6564 |
lua_rawgeti(L,1,u-1); |
| 6565 |
set2(L,i,u-1); |
| 6566 |
i=l;j=u-1; |
| 6567 |
for(;;){ |
| 6568 |
while(lua_rawgeti(L,1,++i),sort_comp(L,-1,-2)){ |
| 6569 |
if(i>u)luaL_error(L,"invalid order function for sorting"); |
| 6570 |
lua_pop(L,1); |
| 6571 |
} |
| 6572 |
while(lua_rawgeti(L,1,--j),sort_comp(L,-3,-1)){ |
| 6573 |
if(j<l)luaL_error(L,"invalid order function for sorting"); |
| 6574 |
lua_pop(L,1); |
| 6575 |
} |
| 6576 |
if(j<i){ |
| 6577 |
lua_pop(L,3); |
| 6578 |
break; |
| 6579 |
} |
| 6580 |
set2(L,i,j); |
| 6581 |
} |
| 6582 |
lua_rawgeti(L,1,u-1); |
| 6583 |
lua_rawgeti(L,1,i); |
| 6584 |
set2(L,u-1,i); |
| 6585 |
if(i-l<u-i){ |
| 6586 |
j=l;i=i-1;l=i+2; |
| 6587 |
} |
| 6588 |
else{ |
| 6589 |
j=i+1;i=u;u=j-2; |
| 6590 |
} |
| 6591 |
auxsort(L,j,i); |
| 6592 |
} |
| 6593 |
} |
| 6594 |
static int sort(lua_State*L){ |
| 6595 |
int n=aux_getn(L,1); |
| 6596 |
luaL_checkstack(L,40,""); |
| 6597 |
if(!lua_isnoneornil(L,2)) |
| 6598 |
luaL_checktype(L,2,6); |
| 6599 |
lua_settop(L,2); |
| 6600 |
auxsort(L,1,n); |
| 6601 |
return 0; |
| 6602 |
} |
| 6603 |
static const luaL_Reg tab_funcs[]={ |
| 6604 |
{"concat",tconcat}, |
| 6605 |
{"insert",tinsert}, |
| 6606 |
{"remove",tremove}, |
| 6607 |
{"sort",sort}, |
| 6608 |
{NULL,NULL} |
| 6609 |
}; |
| 6610 |
static int luaopen_table(lua_State*L){ |
| 6611 |
luaL_register(L,"table",tab_funcs); |
| 6612 |
return 1; |
| 6613 |
} |
| 6614 |
static const char*const fnames[]={"input","output"}; |
| 6615 |
static int pushresult(lua_State*L,int i,const char*filename){ |
| 6616 |
int en=errno; |
| 6617 |
if(i){ |
| 6618 |
lua_pushboolean(L,1); |
| 6619 |
return 1; |
| 6620 |
} |
| 6621 |
else{ |
| 6622 |
lua_pushnil(L); |
| 6623 |
if(filename) |
| 6624 |
lua_pushfstring(L,"%s: %s",filename,strerror(en)); |
| 6625 |
else |
| 6626 |
lua_pushfstring(L,"%s",strerror(en)); |
| 6627 |
lua_pushinteger(L,en); |
| 6628 |
return 3; |
| 6629 |
} |
| 6630 |
} |
| 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)); |
| 6634 |
} |
| 6635 |
#define tofilep(L)((FILE**)luaL_checkudata(L,1,"FILE*")) |
| 6636 |
static int io_type(lua_State*L){ |
| 6637 |
void*ud; |
| 6638 |
luaL_checkany(L,1); |
| 6639 |
ud=lua_touserdata(L,1); |
| 6640 |
lua_getfield(L,(-10000),"FILE*"); |
| 6641 |
if(ud==NULL||!lua_getmetatable(L,1)||!lua_rawequal(L,-2,-1)) |
| 6642 |
lua_pushnil(L); |
| 6643 |
else if(*((FILE**)ud)==NULL) |
| 6644 |
lua_pushliteral(L,"closed file"); |
| 6645 |
else |
| 6646 |
lua_pushliteral(L,"file"); |
| 6647 |
return 1; |
| 6648 |
} |
| 6649 |
static FILE*tofile(lua_State*L){ |
| 6650 |
FILE**f=tofilep(L); |
| 6651 |
if(*f==NULL) |
| 6652 |
luaL_error(L,"attempt to use a closed file"); |
| 6653 |
return*f; |
| 6654 |
} |
| 6655 |
static FILE**newfile(lua_State*L){ |
| 6656 |
FILE**pf=(FILE**)lua_newuserdata(L,sizeof(FILE*)); |
| 6657 |
*pf=NULL; |
| 6658 |
luaL_getmetatable(L,"FILE*"); |
| 6659 |
lua_setmetatable(L,-2); |
| 6660 |
return pf; |
| 6661 |
} |
| 6662 |
static int io_noclose(lua_State*L){ |
| 6663 |
lua_pushnil(L); |
| 6664 |
lua_pushliteral(L,"cannot close standard file"); |
| 6665 |
return 2; |
| 6666 |
} |
| 6667 |
static int io_pclose(lua_State*L){ |
| 6668 |
FILE**p=tofilep(L); |
| 6669 |
int ok=lua_pclose(L,*p); |
| 6670 |
*p=NULL; |
| 6671 |
return pushresult(L,ok,NULL); |
| 6672 |
} |
| 6673 |
static int io_fclose(lua_State*L){ |
| 6674 |
FILE**p=tofilep(L); |
| 6675 |
int ok=(fclose(*p)==0); |
| 6676 |
*p=NULL; |
| 6677 |
return pushresult(L,ok,NULL); |
| 6678 |
} |
| 6679 |
static int aux_close(lua_State*L){ |
| 6680 |
lua_getfenv(L,1); |
| 6681 |
lua_getfield(L,-1,"__close"); |
| 6682 |
return(lua_tocfunction(L,-1))(L); |
| 6683 |
} |
| 6684 |
static int io_close(lua_State*L){ |
| 6685 |
if(lua_isnone(L,1)) |
| 6686 |
lua_rawgeti(L,(-10001),2); |
| 6687 |
tofile(L); |
| 6688 |
return aux_close(L); |
| 6689 |
} |
| 6690 |
static int io_gc(lua_State*L){ |
| 6691 |
FILE*f=*tofilep(L); |
| 6692 |
if(f!=NULL) |
| 6693 |
aux_close(L); |
| 6694 |
return 0; |
| 6695 |
} |
| 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; |
| 6702 |
} |
| 6703 |
static FILE*getiofile(lua_State*L,int findex){ |
| 6704 |
FILE*f; |
| 6705 |
lua_rawgeti(L,(-10001),findex); |
| 6706 |
f=*(FILE**)lua_touserdata(L,-1); |
| 6707 |
if(f==NULL) |
| 6708 |
luaL_error(L,"standard %s file is closed",fnames[findex-1]); |
| 6709 |
return f; |
| 6710 |
} |
| 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); |
| 6714 |
if(filename){ |
| 6715 |
FILE**pf=newfile(L); |
| 6716 |
*pf=fopen(filename,mode); |
| 6717 |
if(*pf==NULL) |
| 6718 |
fileerror(L,1,filename); |
| 6719 |
} |
| 6720 |
else{ |
| 6721 |
tofile(L); |
| 6722 |
lua_pushvalue(L,1); |
| 6723 |
} |
| 6724 |
lua_rawseti(L,(-10001),f); |
| 6725 |
} |
| 6726 |
lua_rawgeti(L,(-10001),f); |
| 6727 |
return 1; |
| 6728 |
} |
| 6729 |
static int io_input(lua_State*L){ |
| 6730 |
return g_iofile(L,1,"r"); |
| 6731 |
} |
| 6732 |
static int io_output(lua_State*L){ |
| 6733 |
return g_iofile(L,2,"w"); |
| 6734 |
} |
| 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); |
| 6740 |
} |
| 6741 |
static int f_lines(lua_State*L){ |
| 6742 |
tofile(L); |
| 6743 |
aux_lines(L,1,0); |
| 6744 |
return 1; |
| 6745 |
} |
| 6746 |
static int io_lines(lua_State*L){ |
| 6747 |
if(lua_isnoneornil(L,1)){ |
| 6748 |
lua_rawgeti(L,(-10001),1); |
| 6749 |
return f_lines(L); |
| 6750 |
} |
| 6751 |
else{ |
| 6752 |
const char*filename=luaL_checkstring(L,1); |
| 6753 |
FILE**pf=newfile(L); |
| 6754 |
*pf=fopen(filename,"r"); |
| 6755 |
if(*pf==NULL) |
| 6756 |
fileerror(L,1,filename); |
| 6757 |
aux_lines(L,lua_gettop(L),1); |
| 6758 |
return 1; |
| 6759 |
} |
| 6760 |
} |
| 6761 |
static int read_number(lua_State*L,FILE*f){ |
| 6762 |
lua_Number d; |
| 6763 |
if(fscanf(f,"%lf",&d)==1){ |
| 6764 |
lua_pushnumber(L,d); |
| 6765 |
return 1; |
| 6766 |
} |
| 6767 |
else{ |
| 6768 |
lua_pushnil(L); |
| 6769 |
return 0; |
| 6770 |
} |
| 6771 |
} |
| 6772 |
static int test_eof(lua_State*L,FILE*f){ |
| 6773 |
int c=getc(f); |
| 6774 |
ungetc(c,f); |
| 6775 |
lua_pushlstring(L,NULL,0); |
| 6776 |
return(c!=EOF); |
| 6777 |
} |
| 6778 |
static int read_line(lua_State*L,FILE*f){ |
| 6779 |
luaL_Buffer b; |
| 6780 |
luaL_buffinit(L,&b); |
| 6781 |
for(;;){ |
| 6782 |
size_t l; |
| 6783 |
char*p=luaL_prepbuffer(&b); |
| 6784 |
if(fgets(p,BUFSIZ,f)==NULL){ |
| 6785 |
luaL_pushresult(&b); |
| 6786 |
return(lua_objlen(L,-1)>0); |
| 6787 |
} |
| 6788 |
l=strlen(p); |
| 6789 |
if(l==0||p[l-1]!='\n') |
| 6790 |
luaL_addsize(&b,l); |
| 6791 |
else{ |
| 6792 |
luaL_addsize(&b,l-1); |
| 6793 |
luaL_pushresult(&b); |
| 6794 |
return 1; |
| 6795 |
} |
| 6796 |
} |
| 6797 |
} |
| 6798 |
static int read_chars(lua_State*L,FILE*f,size_t n){ |
| 6799 |
size_t rlen; |
| 6800 |
size_t nr; |
| 6801 |
luaL_Buffer b; |
| 6802 |
luaL_buffinit(L,&b); |
| 6803 |
rlen=BUFSIZ; |
| 6804 |
do{ |
| 6805 |
char*p=luaL_prepbuffer(&b); |
| 6806 |
if(rlen>n)rlen=n; |
| 6807 |
nr=fread(p,sizeof(char),rlen,f); |
| 6808 |
luaL_addsize(&b,nr); |
| 6809 |
n-=nr; |
| 6810 |
}while(n>0&&nr==rlen); |
| 6811 |
luaL_pushresult(&b); |
| 6812 |
return(n==0||lua_objlen(L,-1)>0); |
| 6813 |
} |
| 6814 |
static int g_read(lua_State*L,FILE*f,int first){ |
| 6815 |
int nargs=lua_gettop(L)-1; |
| 6816 |
int success; |
| 6817 |
int n; |
| 6818 |
clearerr(f); |
| 6819 |
if(nargs==0){ |
| 6820 |
success=read_line(L,f); |
| 6821 |
n=first+1; |
| 6822 |
} |
| 6823 |
else{ |
| 6824 |
luaL_checkstack(L,nargs+20,"too many arguments"); |
| 6825 |
success=1; |
| 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); |
| 6830 |
} |
| 6831 |
else{ |
| 6832 |
const char*p=lua_tostring(L,n); |
| 6833 |
luaL_argcheck(L,p&&p[0]=='*',n,"invalid option"); |
| 6834 |
switch(p[1]){ |
| 6835 |
case'n': |
| 6836 |
success=read_number(L,f); |
| 6837 |
break; |
| 6838 |
case'l': |
| 6839 |
success=read_line(L,f); |
| 6840 |
break; |
| 6841 |
case'a': |
| 6842 |
read_chars(L,f,~((size_t)0)); |
| 6843 |
success=1; |
| 6844 |
break; |
| 6845 |
default: |
| 6846 |
return luaL_argerror(L,n,"invalid format"); |
| 6847 |
} |
| 6848 |
} |
| 6849 |
} |
| 6850 |
} |
| 6851 |
if(ferror(f)) |
| 6852 |
return pushresult(L,0,NULL); |
| 6853 |
if(!success){ |
| 6854 |
lua_pop(L,1); |
| 6855 |
lua_pushnil(L); |
| 6856 |
} |
| 6857 |
return n-first; |
| 6858 |
} |
| 6859 |
static int io_read(lua_State*L){ |
| 6860 |
return g_read(L,getiofile(L,1),1); |
| 6861 |
} |
| 6862 |
static int f_read(lua_State*L){ |
| 6863 |
return g_read(L,tofile(L),2); |
| 6864 |
} |
| 6865 |
static int io_readline(lua_State*L){ |
| 6866 |
FILE*f=*(FILE**)lua_touserdata(L,lua_upvalueindex(1)); |
| 6867 |
int sucess; |
| 6868 |
if(f==NULL) |
| 6869 |
luaL_error(L,"file is already closed"); |
| 6870 |
sucess=read_line(L,f); |
| 6871 |
if(ferror(f)) |
| 6872 |
return luaL_error(L,"%s",strerror(errno)); |
| 6873 |
if(sucess)return 1; |
| 6874 |
else{ |
| 6875 |
if(lua_toboolean(L,lua_upvalueindex(2))){ |
| 6876 |
lua_settop(L,0); |
| 6877 |
lua_pushvalue(L,lua_upvalueindex(1)); |
| 6878 |
aux_close(L); |
| 6879 |
} |
| 6880 |
return 0; |
| 6881 |
} |
| 6882 |
} |
| 6883 |
static int g_write(lua_State*L,FILE*f,int arg){ |
| 6884 |
int nargs=lua_gettop(L)-1; |
| 6885 |
int status=1; |
| 6886 |
for(;nargs--;arg++){ |
| 6887 |
if(lua_type(L,arg)==3){ |
| 6888 |
status=status&& |
| 6889 |
fprintf(f,"%.14g",lua_tonumber(L,arg))>0; |
| 6890 |
} |
| 6891 |
else{ |
| 6892 |
size_t l; |
| 6893 |
const char*s=luaL_checklstring(L,arg,&l); |
| 6894 |
status=status&&(fwrite(s,sizeof(char),l,f)==l); |
| 6895 |
} |
| 6896 |
} |
| 6897 |
return pushresult(L,status,NULL); |
| 6898 |
} |
| 6899 |
static int io_write(lua_State*L){ |
| 6900 |
return g_write(L,getiofile(L,2),1); |
| 6901 |
} |
| 6902 |
static int f_write(lua_State*L){ |
| 6903 |
return g_write(L,tofile(L),2); |
| 6904 |
} |
| 6905 |
static int io_flush(lua_State*L){ |
| 6906 |
return pushresult(L,fflush(getiofile(L,2))==0,NULL); |
| 6907 |
} |
| 6908 |
static int f_flush(lua_State*L){ |
| 6909 |
return pushresult(L,fflush(tofile(L))==0,NULL); |
| 6910 |
} |
| 6911 |
static const luaL_Reg iolib[]={ |
| 6912 |
{"close",io_close}, |
| 6913 |
{"flush",io_flush}, |
| 6914 |
{"input",io_input}, |
| 6915 |
{"lines",io_lines}, |
| 6916 |
{"open",io_open}, |
| 6917 |
{"output",io_output}, |
| 6918 |
{"read",io_read}, |
| 6919 |
{"type",io_type}, |
| 6920 |
{"write",io_write}, |
| 6921 |
{NULL,NULL} |
| 6922 |
}; |
| 6923 |
static const luaL_Reg flib[]={ |
| 6924 |
{"close",io_close}, |
| 6925 |
{"flush",f_flush}, |
| 6926 |
{"lines",f_lines}, |
| 6927 |
{"read",f_read}, |
| 6928 |
{"write",f_write}, |
| 6929 |
{"__gc",io_gc}, |
| 6930 |
{NULL,NULL} |
| 6931 |
}; |
| 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); |
| 6937 |
} |
| 6938 |
static void createstdfile(lua_State*L,FILE*f,int k,const char*fname){ |
| 6939 |
*newfile(L)=f; |
| 6940 |
if(k>0){ |
| 6941 |
lua_pushvalue(L,-1); |
| 6942 |
lua_rawseti(L,(-10001),k); |
| 6943 |
} |
| 6944 |
lua_pushvalue(L,-2); |
| 6945 |
lua_setfenv(L,-2); |
| 6946 |
lua_setfield(L,-3,fname); |
| 6947 |
} |
| 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"); |
| 6952 |
} |
| 6953 |
static int luaopen_io(lua_State*L){ |
| 6954 |
createmeta(L); |
| 6955 |
newfenv(L,io_fclose); |
| 6956 |
lua_replace(L,(-10001)); |
| 6957 |
luaL_register(L,"io",iolib); |
| 6958 |
newfenv(L,io_noclose); |
| 6959 |
createstdfile(L,stdin,1,"stdin"); |
| 6960 |
createstdfile(L,stdout,2,"stdout"); |
| 6961 |
createstdfile(L,stderr,0,"stderr"); |
| 6962 |
lua_pop(L,1); |
| 6963 |
lua_getfield(L,-1,"popen"); |
| 6964 |
newfenv(L,io_pclose); |
| 6965 |
lua_setfenv(L,-2); |
| 6966 |
lua_pop(L,1); |
| 6967 |
return 1; |
| 6968 |
} |
| 6969 |
static int os_pushresult(lua_State*L,int i,const char*filename){ |
| 6970 |
int en=errno; |
| 6971 |
if(i){ |
| 6972 |
lua_pushboolean(L,1); |
| 6973 |
return 1; |
| 6974 |
} |
| 6975 |
else{ |
| 6976 |
lua_pushnil(L); |
| 6977 |
lua_pushfstring(L,"%s: %s",filename,strerror(en)); |
| 6978 |
lua_pushinteger(L,en); |
| 6979 |
return 3; |
| 6980 |
} |
| 6981 |
} |
| 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); |
| 6985 |
} |
| 6986 |
static int os_exit(lua_State*L){ |
| 6987 |
exit(luaL_optint(L,1,EXIT_SUCCESS)); |
| 6988 |
} |
| 6989 |
static const luaL_Reg syslib[]={ |
| 6990 |
{"exit",os_exit}, |
| 6991 |
{"remove",os_remove}, |
| 6992 |
{NULL,NULL} |
| 6993 |
}; |
| 6994 |
static int luaopen_os(lua_State*L){ |
| 6995 |
luaL_register(L,"os",syslib); |
| 6996 |
return 1; |
| 6997 |
} |
| 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; |
| 7002 |
} |
| 7003 |
static int str_sub(lua_State*L){ |
| 7004 |
size_t 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); |
| 7008 |
if(start<1)start=1; |
| 7009 |
if(end>(ptrdiff_t)l)end=(ptrdiff_t)l; |
| 7010 |
if(start<=end) |
| 7011 |
lua_pushlstring(L,s+start-1,end-start+1); |
| 7012 |
else lua_pushliteral(L,""); |
| 7013 |
return 1; |
| 7014 |
} |
| 7015 |
static int str_lower(lua_State*L){ |
| 7016 |
size_t l; |
| 7017 |
size_t i; |
| 7018 |
luaL_Buffer b; |
| 7019 |
const char*s=luaL_checklstring(L,1,&l); |
| 7020 |
luaL_buffinit(L,&b); |
| 7021 |
for(i=0;i<l;i++) |
| 7022 |
luaL_addchar(&b,tolower(uchar(s[i]))); |
| 7023 |
luaL_pushresult(&b); |
| 7024 |
return 1; |
| 7025 |
} |
| 7026 |
static int str_upper(lua_State*L){ |
| 7027 |
size_t l; |
| 7028 |
size_t i; |
| 7029 |
luaL_Buffer b; |
| 7030 |
const char*s=luaL_checklstring(L,1,&l); |
| 7031 |
luaL_buffinit(L,&b); |
| 7032 |
for(i=0;i<l;i++) |
| 7033 |
luaL_addchar(&b,toupper(uchar(s[i]))); |
| 7034 |
luaL_pushresult(&b); |
| 7035 |
return 1; |
| 7036 |
} |
| 7037 |
static int str_rep(lua_State*L){ |
| 7038 |
size_t l; |
| 7039 |
luaL_Buffer b; |
| 7040 |
const char*s=luaL_checklstring(L,1,&l); |
| 7041 |
int n=luaL_checkint(L,2); |
| 7042 |
luaL_buffinit(L,&b); |
| 7043 |
while(n-->0) |
| 7044 |
luaL_addlstring(&b,s,l); |
| 7045 |
luaL_pushresult(&b); |
| 7046 |
return 1; |
| 7047 |
} |
| 7048 |
static int str_byte(lua_State*L){ |
| 7049 |
size_t 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); |
| 7053 |
int n,i; |
| 7054 |
if(posi<=0)posi=1; |
| 7055 |
if((size_t)pose>l)pose=l; |
| 7056 |
if(posi>pose)return 0; |
| 7057 |
n=(int)(pose-posi+1); |
| 7058 |
if(posi+n<=pose) |
| 7059 |
luaL_error(L,"string slice too long"); |
| 7060 |
luaL_checkstack(L,n,"string slice too long"); |
| 7061 |
for(i=0;i<n;i++) |
| 7062 |
lua_pushinteger(L,uchar(s[posi+i-1])); |
| 7063 |
return n; |
| 7064 |
} |
| 7065 |
static int str_char(lua_State*L){ |
| 7066 |
int n=lua_gettop(L); |
| 7067 |
int i; |
| 7068 |
luaL_Buffer b; |
| 7069 |
luaL_buffinit(L,&b); |
| 7070 |
for(i=1;i<=n;i++){ |
| 7071 |
int c=luaL_checkint(L,i); |
| 7072 |
luaL_argcheck(L,uchar(c)==c,i,"invalid value"); |
| 7073 |
luaL_addchar(&b,uchar(c)); |
| 7074 |
} |
| 7075 |
luaL_pushresult(&b); |
| 7076 |
return 1; |
| 7077 |
} |
| 7078 |
typedef struct MatchState{ |
| 7079 |
const char*src_init; |
| 7080 |
const char*src_end; |
| 7081 |
lua_State*L; |
| 7082 |
int level; |
| 7083 |
struct{ |
| 7084 |
const char*init; |
| 7085 |
ptrdiff_t len; |
| 7086 |
}capture[32]; |
| 7087 |
}MatchState; |
| 7088 |
static int check_capture(MatchState*ms,int l){ |
| 7089 |
l-='1'; |
| 7090 |
if(l<0||l>=ms->level||ms->capture[l].len==(-1)) |
| 7091 |
return luaL_error(ms->L,"invalid capture index"); |
| 7092 |
return l; |
| 7093 |
} |
| 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"); |
| 7099 |
} |
| 7100 |
static const char*classend(MatchState*ms,const char*p){ |
| 7101 |
switch(*p++){ |
| 7102 |
case'%':{ |
| 7103 |
if(*p=='\0') |
| 7104 |
luaL_error(ms->L,"malformed pattern (ends with "LUA_QL("%%")")"); |
| 7105 |
return p+1; |
| 7106 |
} |
| 7107 |
case'[':{ |
| 7108 |
if(*p=='^')p++; |
| 7109 |
do{ |
| 7110 |
if(*p=='\0') |
| 7111 |
luaL_error(ms->L,"malformed pattern (missing "LUA_QL("]")")"); |
| 7112 |
if(*(p++)=='%'&&*p!='\0') |
| 7113 |
p++; |
| 7114 |
}while(*p!=']'); |
| 7115 |
return p+1; |
| 7116 |
} |
| 7117 |
default:{ |
| 7118 |
return p; |
| 7119 |
} |
| 7120 |
} |
| 7121 |
} |
| 7122 |
static int match_class(int c,int cl){ |
| 7123 |
int res; |
| 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); |
| 7136 |
} |
| 7137 |
return(islower(cl)?res:!res); |
| 7138 |
} |
| 7139 |
static int matchbracketclass(int c,const char*p,const char*ec){ |
| 7140 |
int sig=1; |
| 7141 |
if(*(p+1)=='^'){ |
| 7142 |
sig=0; |
| 7143 |
p++; |
| 7144 |
} |
| 7145 |
while(++p<ec){ |
| 7146 |
if(*p=='%'){ |
| 7147 |
p++; |
| 7148 |
if(match_class(c,uchar(*p))) |
| 7149 |
return sig; |
| 7150 |
} |
| 7151 |
else if((*(p+1)=='-')&&(p+2<ec)){ |
| 7152 |
p+=2; |
| 7153 |
if(uchar(*(p-2))<=c&&c<=uchar(*p)) |
| 7154 |
return sig; |
| 7155 |
} |
| 7156 |
else if(uchar(*p)==c)return sig; |
| 7157 |
} |
| 7158 |
return!sig; |
| 7159 |
} |
| 7160 |
static int singlematch(int c,const char*p,const char*ep){ |
| 7161 |
switch(*p){ |
| 7162 |
case'.':return 1; |
| 7163 |
case'%':return match_class(c,uchar(*(p+1))); |
| 7164 |
case'[':return matchbracketclass(c,p,ep-1); |
| 7165 |
default:return(uchar(*p)==c); |
| 7166 |
} |
| 7167 |
} |
| 7168 |
static const char*match(MatchState*ms,const char*s,const char*p); |
| 7169 |
static const char*matchbalance(MatchState*ms,const char*s, |
| 7170 |
const char*p){ |
| 7171 |
if(*p==0||*(p+1)==0) |
| 7172 |
luaL_error(ms->L,"unbalanced pattern"); |
| 7173 |
if(*s!=*p)return NULL; |
| 7174 |
else{ |
| 7175 |
int b=*p; |
| 7176 |
int e=*(p+1); |
| 7177 |
int cont=1; |
| 7178 |
while(++s<ms->src_end){ |
| 7179 |
if(*s==e){ |
| 7180 |
if(--cont==0)return s+1; |
| 7181 |
} |
| 7182 |
else if(*s==b)cont++; |
| 7183 |
} |
| 7184 |
} |
| 7185 |
return NULL; |
| 7186 |
} |
| 7187 |
static const char*max_expand(MatchState*ms,const char*s, |
| 7188 |
const char*p,const char*ep){ |
| 7189 |
ptrdiff_t i=0; |
| 7190 |
while((s+i)<ms->src_end&&singlematch(uchar(*(s+i)),p,ep)) |
| 7191 |
i++; |
| 7192 |
while(i>=0){ |
| 7193 |
const char*res=match(ms,(s+i),ep+1); |
| 7194 |
if(res)return res; |
| 7195 |
i--; |
| 7196 |
} |
| 7197 |
return NULL; |
| 7198 |
} |
| 7199 |
static const char*min_expand(MatchState*ms,const char*s, |
| 7200 |
const char*p,const char*ep){ |
| 7201 |
for(;;){ |
| 7202 |
const char*res=match(ms,s,ep+1); |
| 7203 |
if(res!=NULL) |
| 7204 |
return res; |
| 7205 |
else if(s<ms->src_end&&singlematch(uchar(*s),p,ep)) |
| 7206 |
s++; |
| 7207 |
else return NULL; |
| 7208 |
} |
| 7209 |
} |
| 7210 |
static const char*start_capture(MatchState*ms,const char*s, |
| 7211 |
const char*p,int what){ |
| 7212 |
const char*res; |
| 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; |
| 7217 |
ms->level=level+1; |
| 7218 |
if((res=match(ms,s,p))==NULL) |
| 7219 |
ms->level--; |
| 7220 |
return res; |
| 7221 |
} |
| 7222 |
static const char*end_capture(MatchState*ms,const char*s, |
| 7223 |
const char*p){ |
| 7224 |
int l=capture_to_close(ms); |
| 7225 |
const char*res; |
| 7226 |
ms->capture[l].len=s-ms->capture[l].init; |
| 7227 |
if((res=match(ms,s,p))==NULL) |
| 7228 |
ms->capture[l].len=(-1); |
| 7229 |
return res; |
| 7230 |
} |
| 7231 |
static const char*match_capture(MatchState*ms,const char*s,int l){ |
| 7232 |
size_t len; |
| 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) |
| 7237 |
return s+len; |
| 7238 |
else return NULL; |
| 7239 |
} |
| 7240 |
static const char*match(MatchState*ms,const char*s,const char*p){ |
| 7241 |
init: |
| 7242 |
switch(*p){ |
| 7243 |
case'(':{ |
| 7244 |
if(*(p+1)==')') |
| 7245 |
return start_capture(ms,s,p+2,(-2)); |
| 7246 |
else |
| 7247 |
return start_capture(ms,s,p+1,(-1)); |
| 7248 |
} |
| 7249 |
case')':{ |
| 7250 |
return end_capture(ms,s,p+1); |
| 7251 |
} |
| 7252 |
case'%':{ |
| 7253 |
switch(*(p+1)){ |
| 7254 |
case'b':{ |
| 7255 |
s=matchbalance(ms,s,p+2); |
| 7256 |
if(s==NULL)return NULL; |
| 7257 |
p+=4;goto init; |
| 7258 |
} |
| 7259 |
case'f':{ |
| 7260 |
const char*ep;char previous; |
| 7261 |
p+=2; |
| 7262 |
if(*p!='[') |
| 7263 |
luaL_error(ms->L,"missing "LUA_QL("[")" after " |
| 7264 |
LUA_QL("%%f")" in pattern"); |
| 7265 |
ep=classend(ms,p); |
| 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; |
| 7269 |
p=ep;goto init; |
| 7270 |
} |
| 7271 |
default:{ |
| 7272 |
if(isdigit(uchar(*(p+1)))){ |
| 7273 |
s=match_capture(ms,s,uchar(*(p+1))); |
| 7274 |
if(s==NULL)return NULL; |
| 7275 |
p+=2;goto init; |
| 7276 |
} |
| 7277 |
goto dflt; |
| 7278 |
} |
| 7279 |
} |
| 7280 |
} |
| 7281 |
case'\0':{ |
| 7282 |
return s; |
| 7283 |
} |
| 7284 |
case'$':{ |
| 7285 |
if(*(p+1)=='\0') |
| 7286 |
return(s==ms->src_end)?s:NULL; |
| 7287 |
else goto dflt; |
| 7288 |
} |
| 7289 |
default:dflt:{ |
| 7290 |
const char*ep=classend(ms,p); |
| 7291 |
int m=s<ms->src_end&&singlematch(uchar(*s),p,ep); |
| 7292 |
switch(*ep){ |
| 7293 |
case'?':{ |
| 7294 |
const char*res; |
| 7295 |
if(m&&((res=match(ms,s+1,ep+1))!=NULL)) |
| 7296 |
return res; |
| 7297 |
p=ep+1;goto init; |
| 7298 |
} |
| 7299 |
case'*':{ |
| 7300 |
return max_expand(ms,s,p,ep); |
| 7301 |
} |
| 7302 |
case'+':{ |
| 7303 |
return(m?max_expand(ms,s+1,p,ep):NULL); |
| 7304 |
} |
| 7305 |
case'-':{ |
| 7306 |
return min_expand(ms,s,p,ep); |
| 7307 |
} |
| 7308 |
default:{ |
| 7309 |
if(!m)return NULL; |
| 7310 |
s++;p=ep;goto init; |
| 7311 |
} |
| 7312 |
} |
| 7313 |
} |
| 7314 |
} |
| 7315 |
} |
| 7316 |
static const char*lmemfind(const char*s1,size_t l1, |
| 7317 |
const char*s2,size_t l2){ |
| 7318 |
if(l2==0)return s1; |
| 7319 |
else if(l2>l1)return NULL; |
| 7320 |
else{ |
| 7321 |
const char*init; |
| 7322 |
l2--; |
| 7323 |
l1=l1-l2; |
| 7324 |
while(l1>0&&(init=(const char*)memchr(s1,*s2,l1))!=NULL){ |
| 7325 |
init++; |
| 7326 |
if(memcmp(init,s2+1,l2)==0) |
| 7327 |
return init-1; |
| 7328 |
else{ |
| 7329 |
l1-=init-s1; |
| 7330 |
s1=init; |
| 7331 |
} |
| 7332 |
} |
| 7333 |
return NULL; |
| 7334 |
} |
| 7335 |
} |
| 7336 |
static void push_onecapture(MatchState*ms,int i,const char*s, |
| 7337 |
const char*e){ |
| 7338 |
if(i>=ms->level){ |
| 7339 |
if(i==0) |
| 7340 |
lua_pushlstring(ms->L,s,e-s); |
| 7341 |
else |
| 7342 |
luaL_error(ms->L,"invalid capture index"); |
| 7343 |
} |
| 7344 |
else{ |
| 7345 |
ptrdiff_t l=ms->capture[i].len; |
| 7346 |
if(l==(-1))luaL_error(ms->L,"unfinished capture"); |
| 7347 |
if(l==(-2)) |
| 7348 |
lua_pushinteger(ms->L,ms->capture[i].init-ms->src_init+1); |
| 7349 |
else |
| 7350 |
lua_pushlstring(ms->L,ms->capture[i].init,l); |
| 7351 |
} |
| 7352 |
} |
| 7353 |
static int push_captures(MatchState*ms,const char*s,const char*e){ |
| 7354 |
int i; |
| 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); |
| 7359 |
return nlevels; |
| 7360 |
} |
| 7361 |
static int str_find_aux(lua_State*L,int find){ |
| 7362 |
size_t l1,l2; |
| 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; |
| 7366 |
if(init<0)init=0; |
| 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); |
| 7371 |
if(s2){ |
| 7372 |
lua_pushinteger(L,s2-s+1); |
| 7373 |
lua_pushinteger(L,s2-s+l2); |
| 7374 |
return 2; |
| 7375 |
} |
| 7376 |
} |
| 7377 |
else{ |
| 7378 |
MatchState ms; |
| 7379 |
int anchor=(*p=='^')?(p++,1):0; |
| 7380 |
const char*s1=s+init; |
| 7381 |
ms.L=L; |
| 7382 |
ms.src_init=s; |
| 7383 |
ms.src_end=s+l1; |
| 7384 |
do{ |
| 7385 |
const char*res; |
| 7386 |
ms.level=0; |
| 7387 |
if((res=match(&ms,s1,p))!=NULL){ |
| 7388 |
if(find){ |
| 7389 |
lua_pushinteger(L,s1-s+1); |
| 7390 |
lua_pushinteger(L,res-s); |
| 7391 |
return push_captures(&ms,NULL,0)+2; |
| 7392 |
} |
| 7393 |
else |
| 7394 |
return push_captures(&ms,s1,res); |
| 7395 |
} |
| 7396 |
}while(s1++<ms.src_end&&!anchor); |
| 7397 |
} |
| 7398 |
lua_pushnil(L); |
| 7399 |
return 1; |
| 7400 |
} |
| 7401 |
static int str_find(lua_State*L){ |
| 7402 |
return str_find_aux(L,1); |
| 7403 |
} |
| 7404 |
static int str_match(lua_State*L){ |
| 7405 |
return str_find_aux(L,0); |
| 7406 |
} |
| 7407 |
static int gmatch_aux(lua_State*L){ |
| 7408 |
MatchState ms; |
| 7409 |
size_t ls; |
| 7410 |
const char*s=lua_tolstring(L,lua_upvalueindex(1),&ls); |
| 7411 |
const char*p=lua_tostring(L,lua_upvalueindex(2)); |
| 7412 |
const char*src; |
| 7413 |
ms.L=L; |
| 7414 |
ms.src_init=s; |
| 7415 |
ms.src_end=s+ls; |
| 7416 |
for(src=s+(size_t)lua_tointeger(L,lua_upvalueindex(3)); |
| 7417 |
src<=ms.src_end; |
| 7418 |
src++){ |
| 7419 |
const char*e; |
| 7420 |
ms.level=0; |
| 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); |
| 7427 |
} |
| 7428 |
} |
| 7429 |
return 0; |
| 7430 |
} |
| 7431 |
static int gmatch(lua_State*L){ |
| 7432 |
luaL_checkstring(L,1); |
| 7433 |
luaL_checkstring(L,2); |
| 7434 |
lua_settop(L,2); |
| 7435 |
lua_pushinteger(L,0); |
| 7436 |
lua_pushcclosure(L,gmatch_aux,3); |
| 7437 |
return 1; |
| 7438 |
} |
| 7439 |
static void add_s(MatchState*ms,luaL_Buffer*b,const char*s, |
| 7440 |
const char*e){ |
| 7441 |
size_t l,i; |
| 7442 |
const char*news=lua_tolstring(ms->L,3,&l); |
| 7443 |
for(i=0;i<l;i++){ |
| 7444 |
if(news[i]!='%') |
| 7445 |
luaL_addchar(b,news[i]); |
| 7446 |
else{ |
| 7447 |
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); |
| 7452 |
else{ |
| 7453 |
push_onecapture(ms,news[i]-'1',s,e); |
| 7454 |
luaL_addvalue(b); |
| 7455 |
} |
| 7456 |
} |
| 7457 |
} |
| 7458 |
} |
| 7459 |
static void add_value(MatchState*ms,luaL_Buffer*b,const char*s, |
| 7460 |
const char*e){ |
| 7461 |
lua_State*L=ms->L; |
| 7462 |
switch(lua_type(L,3)){ |
| 7463 |
case 3: |
| 7464 |
case 4:{ |
| 7465 |
add_s(ms,b,s,e); |
| 7466 |
return; |
| 7467 |
} |
| 7468 |
case 6:{ |
| 7469 |
int n; |
| 7470 |
lua_pushvalue(L,3); |
| 7471 |
n=push_captures(ms,s,e); |
| 7472 |
lua_call(L,n,1); |
| 7473 |
break; |
| 7474 |
} |
| 7475 |
case 5:{ |
| 7476 |
push_onecapture(ms,0,s,e); |
| 7477 |
lua_gettable(L,3); |
| 7478 |
break; |
| 7479 |
} |
| 7480 |
} |
| 7481 |
if(!lua_toboolean(L,-1)){ |
| 7482 |
lua_pop(L,1); |
| 7483 |
lua_pushlstring(L,s,e-s); |
| 7484 |
} |
| 7485 |
else if(!lua_isstring(L,-1)) |
| 7486 |
luaL_error(L,"invalid replacement value (a %s)",luaL_typename(L,-1)); |
| 7487 |
luaL_addvalue(b); |
| 7488 |
} |
| 7489 |
static int str_gsub(lua_State*L){ |
| 7490 |
size_t srcl; |
| 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; |
| 7496 |
int n=0; |
| 7497 |
MatchState ms; |
| 7498 |
luaL_Buffer b; |
| 7499 |
luaL_argcheck(L,tr==3||tr==4|| |
| 7500 |
tr==6||tr==5,3, |
| 7501 |
"string/function/table expected"); |
| 7502 |
luaL_buffinit(L,&b); |
| 7503 |
ms.L=L; |
| 7504 |
ms.src_init=src; |
| 7505 |
ms.src_end=src+srcl; |
| 7506 |
while(n<max_s){ |
| 7507 |
const char*e; |
| 7508 |
ms.level=0; |
| 7509 |
e=match(&ms,src,p); |
| 7510 |
if(e){ |
| 7511 |
n++; |
| 7512 |
add_value(&ms,&b,src,e); |
| 7513 |
} |
| 7514 |
if(e&&e>src) |
| 7515 |
src=e; |
| 7516 |
else if(src<ms.src_end) |
| 7517 |
luaL_addchar(&b,*src++); |
| 7518 |
else break; |
| 7519 |
if(anchor)break; |
| 7520 |
} |
| 7521 |
luaL_addlstring(&b,src,ms.src_end-src); |
| 7522 |
luaL_pushresult(&b); |
| 7523 |
lua_pushinteger(L,n); |
| 7524 |
return 2; |
| 7525 |
} |
| 7526 |
static void addquoted(lua_State*L,luaL_Buffer*b,int arg){ |
| 7527 |
size_t l; |
| 7528 |
const char*s=luaL_checklstring(L,arg,&l); |
| 7529 |
luaL_addchar(b,'"'); |
| 7530 |
while(l--){ |
| 7531 |
switch(*s){ |
| 7532 |
case'"':case'\\':case'\n':{ |
| 7533 |
luaL_addchar(b,'\\'); |
| 7534 |
luaL_addchar(b,*s); |
| 7535 |
break; |
| 7536 |
} |
| 7537 |
case'\r':{ |
| 7538 |
luaL_addlstring(b,"\\r",2); |
| 7539 |
break; |
| 7540 |
} |
| 7541 |
case'\0':{ |
| 7542 |
luaL_addlstring(b,"\\000",4); |
| 7543 |
break; |
| 7544 |
} |
| 7545 |
default:{ |
| 7546 |
luaL_addchar(b,*s); |
| 7547 |
break; |
| 7548 |
} |
| 7549 |
} |
| 7550 |
s++; |
| 7551 |
} |
| 7552 |
luaL_addchar(b,'"'); |
| 7553 |
} |
| 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++; |
| 7561 |
if(*p=='.'){ |
| 7562 |
p++; |
| 7563 |
if(isdigit(uchar(*p)))p++; |
| 7564 |
if(isdigit(uchar(*p)))p++; |
| 7565 |
} |
| 7566 |
if(isdigit(uchar(*p))) |
| 7567 |
luaL_error(L,"invalid format (width or precision too long)"); |
| 7568 |
*(form++)='%'; |
| 7569 |
strncpy(form,strfrmt,p-strfrmt+1); |
| 7570 |
form+=p-strfrmt+1; |
| 7571 |
*form='\0'; |
| 7572 |
return p; |
| 7573 |
} |
| 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'; |
| 7580 |
} |
| 7581 |
static int str_format(lua_State*L){ |
| 7582 |
int top=lua_gettop(L); |
| 7583 |
int arg=1; |
| 7584 |
size_t sfl; |
| 7585 |
const char*strfrmt=luaL_checklstring(L,arg,&sfl); |
| 7586 |
const char*strfrmt_end=strfrmt+sfl; |
| 7587 |
luaL_Buffer b; |
| 7588 |
luaL_buffinit(L,&b); |
| 7589 |
while(strfrmt<strfrmt_end){ |
| 7590 |
if(*strfrmt!='%') |
| 7591 |
luaL_addchar(&b,*strfrmt++); |
| 7592 |
else if(*++strfrmt=='%') |
| 7593 |
luaL_addchar(&b,*strfrmt++); |
| 7594 |
else{ |
| 7595 |
char form[(sizeof("-+ #0")+sizeof("l")+10)]; |
| 7596 |
char buff[512]; |
| 7597 |
if(++arg>top) |
| 7598 |
luaL_argerror(L,arg,"no value"); |
| 7599 |
strfrmt=scanformat(L,strfrmt,form); |
| 7600 |
switch(*strfrmt++){ |
| 7601 |
case'c':{ |
| 7602 |
sprintf(buff,form,(int)luaL_checknumber(L,arg)); |
| 7603 |
break; |
| 7604 |
} |
| 7605 |
case'd':case'i':{ |
| 7606 |
addintlen(form); |
| 7607 |
sprintf(buff,form,(long)luaL_checknumber(L,arg)); |
| 7608 |
break; |
| 7609 |
} |
| 7610 |
case'o':case'u':case'x':case'X':{ |
| 7611 |
addintlen(form); |
| 7612 |
sprintf(buff,form,(unsigned long)luaL_checknumber(L,arg)); |
| 7613 |
break; |
| 7614 |
} |
| 7615 |
case'e':case'E':case'f': |
| 7616 |
case'g':case'G':{ |
| 7617 |
sprintf(buff,form,(double)luaL_checknumber(L,arg)); |
| 7618 |
break; |
| 7619 |
} |
| 7620 |
case'q':{ |
| 7621 |
addquoted(L,&b,arg); |
| 7622 |
continue; |
| 7623 |
} |
| 7624 |
case's':{ |
| 7625 |
size_t l; |
| 7626 |
const char*s=luaL_checklstring(L,arg,&l); |
| 7627 |
if(!strchr(form,'.')&&l>=100){ |
| 7628 |
lua_pushvalue(L,arg); |
| 7629 |
luaL_addvalue(&b); |
| 7630 |
continue; |
| 7631 |
} |
| 7632 |
else{ |
| 7633 |
sprintf(buff,form,s); |
| 7634 |
break; |
| 7635 |
} |
| 7636 |
} |
| 7637 |
default:{ |
| 7638 |
return luaL_error(L,"invalid option "LUA_QL("%%%c")" to " |
| 7639 |
LUA_QL("format"),*(strfrmt-1)); |
| 7640 |
} |
| 7641 |
} |
| 7642 |
luaL_addlstring(&b,buff,strlen(buff)); |
| 7643 |
} |
| 7644 |
} |
| 7645 |
luaL_pushresult(&b); |
| 7646 |
return 1; |
| 7647 |
} |
| 7648 |
static const luaL_Reg strlib[]={ |
| 7649 |
{"byte",str_byte}, |
| 7650 |
{"char",str_char}, |
| 7651 |
{"find",str_find}, |
| 7652 |
{"format",str_format}, |
| 7653 |
{"gmatch",gmatch}, |
| 7654 |
{"gsub",str_gsub}, |
| 7655 |
{"lower",str_lower}, |
| 7656 |
{"match",str_match}, |
| 7657 |
{"rep",str_rep}, |
| 7658 |
{"sub",str_sub}, |
| 7659 |
{"upper",str_upper}, |
| 7660 |
{NULL,NULL} |
| 7661 |
}; |
| 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); |
| 7667 |
lua_pop(L,1); |
| 7668 |
lua_pushvalue(L,-2); |
| 7669 |
lua_setfield(L,-2,"__index"); |
| 7670 |
lua_pop(L,1); |
| 7671 |
} |
| 7672 |
static int luaopen_string(lua_State*L){ |
| 7673 |
luaL_register(L,"string",strlib); |
| 7674 |
createmetatable(L); |
| 7675 |
return 1; |
| 7676 |
} |
| 7677 |
static const luaL_Reg lualibs[]={ |
| 7678 |
{"",luaopen_base}, |
| 7679 |
{"table",luaopen_table}, |
| 7680 |
{"io",luaopen_io}, |
| 7681 |
{"os",luaopen_os}, |
| 7682 |
{"string",luaopen_string}, |
| 7683 |
{NULL,NULL} |
| 7684 |
}; |
| 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); |
| 7690 |
lua_call(L,1,0); |
| 7691 |
} |
| 7692 |
} |
| 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"); |
| 7698 |
return(UB)bn.b; |
| 7699 |
} |
| 7700 |
#define BRET(b)lua_pushnumber(L,(lua_Number)(int)(b));return 1; |
| 7701 |
static int tobit(lua_State*L){ |
| 7702 |
BRET(barg(L,1))} |
| 7703 |
static int bnot(lua_State*L){ |
| 7704 |
BRET(~barg(L,1))} |
| 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){ |
| 7724 |
UB b=barg(L,1); |
| 7725 |
int n=lua_isnone(L,2)?8:(int)barg(L,2); |
| 7726 |
const char*hexdigits="0123456789abcdef"; |
| 7727 |
char buf[8]; |
| 7728 |
int i; |
| 7729 |
if(n<0){n=-n;hexdigits="0123456789ABCDEF";} |
| 7730 |
if(n>8)n=8; |
| 7731 |
for(i=(int)n;--i>=0;){buf[i]=hexdigits[b&15];b>>=4;} |
| 7732 |
lua_pushlstring(L,buf,(size_t)n); |
| 7733 |
return 1; |
| 7734 |
} |
| 7735 |
static const struct luaL_Reg bitlib[]={ |
| 7736 |
{"tobit",tobit}, |
| 7737 |
{"bnot",bnot}, |
| 7738 |
{"band",band}, |
| 7739 |
{"bor",bor}, |
| 7740 |
{"bxor",bxor}, |
| 7741 |
{"lshift",lshift}, |
| 7742 |
{"rshift",rshift}, |
| 7743 |
{"arshift",arshift}, |
| 7744 |
{"rol",rol}, |
| 7745 |
{"ror",ror}, |
| 7746 |
{"bswap",bswap}, |
| 7747 |
{"tohex",tohex}, |
| 7748 |
{NULL,NULL} |
| 7749 |
}; |
| 7750 |
int main(int argc,char**argv){ |
| 7751 |
lua_State*L=luaL_newstate(); |
| 7752 |
int i; |
| 7753 |
luaL_openlibs(L); |
| 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])) |
| 7761 |
goto err; |
| 7762 |
for(i=2;i<argc;i++) |
| 7763 |
lua_pushstring(L,argv[i]); |
| 7764 |
if(lua_pcall(L,argc-2,0,0)){ |
| 7765 |
err: |
| 7766 |
fprintf(stderr,"Error: %s\n",lua_tostring(L,-1)); |
| 7767 |
return 1; |
| 7768 |
} |
| 7769 |
lua_close(L); |
| 7770 |
return 0; |
| 7771 |
} |