static int Pclip(lua_State *L, int op) { gpc_polygon *p=Pget(L,1); gpc_polygon *q=Pget(L,2); gpc_polygon *r=Pnew(L); gpc_polygon_clip(op,p,q,r); return 1; }
static int Lreset(lua_State *L) /** reset(c) */ { MD5_CTX *c=Pget(L,1); MD5Init(c); lua_settop(L,1); return 1; }
static int Lget(lua_State *L) /** get(p,[c,[i]]) */ { int m=lua_gettop(L); gpc_polygon *p=Pget(L,1); int n=p->num_contours; int c,i; if (m==1) { lua_pushnumber(L,n); return 1; } c=luaL_checkint(L,2); if (c<1 || c>n) return 0; n=p->contour[--c].num_vertices; if (m==2) { lua_pushnumber(L,n); lua_pushboolean(L,p->hole ? p->hole[c] : 0); return 2; } i=luaL_checkint(L,3); if (i<=n) { --i; lua_pushnumber(L,p->contour[c].vertex[i].x); lua_pushnumber(L,p->contour[c].vertex[i].y); return 2; } else return 0; }
static int Lclone(lua_State *L) /** clone(c) */ { MT *c=Pget(L,1); MT *d=Pnew(L); *d=*c; return 1; }
static int Ldigest(lua_State *L) /** digest(c or s,[raw]) */ { unsigned char digest[N]; if (lua_isuserdata(L,1)) { MD5_CTX c=*Pget(L,1); MD5Final(digest,&c); } else { size_t l; const char *s=luaL_checklstring(L,1,&l); MD5_CTX c; MD5Init(&c); MD5Update(&c,s,l); MD5Final(digest,&c); } if (lua_toboolean(L,2)) lua_pushlstring(L,(char*)digest,sizeof(digest)); else { char *digit="0123456789abcdef"; char hex[2*N],*h; int i; for (h=hex,i=0; i<N; i++) { *h++=digit[digest[i] >> 4]; *h++=digit[digest[i] & 0x0F]; } lua_pushlstring(L,hex,sizeof(hex)); } return 1; }
static int Lvalue(lua_State *L) /** value(c,[a,b]) */ { MT *c=Pget(L,1); double a,b,r=genrand(c); switch (lua_gettop(L)) { case 1: lua_pushnumber(L,r); return 1; case 2: a=1; b=luaL_checknumber(L,2); break; default: a=luaL_checknumber(L,2); b=luaL_checknumber(L,3); break; } if (a>b) { double t=a; a=b; b=t; } a=ceil(a); b=floor(b); if (a>b) return 0; r=a+floor(r*(b-a+1)); lua_pushnumber(L,r); return 1; }
static int Lseed(lua_State *L) /** seed(c,[seed]) */ { MT *c=Pget(L,1); init_genrand(c,luaL_optnumber(L,2,SEED)); lua_settop(L,1); return 1; }
static int do_getinstruction(lua_State *L) /** getinstruction(f,i) */ { const Proto* f=Pget(L,1); int pc=luaL_checkinteger(L,2); if (pc<=0 || pc>f->sizecode || f->code==NULL) return 0; pc--; { const Instruction* code=f->code; Instruction i=code[pc]; OpCode o=GET_OPCODE(i); int a=GETARG_A(i); int b=GETARG_B(i); int c=GETARG_C(i); int ax=GETARG_Ax(i); int bx=GETARG_Bx(i); int sbx=GETARG_sBx(i); int line=getfuncline(f,pc); if (line>0) lua_pushinteger(L,line); else lua_pushnil(L); lua_pushstring(L,luaP_opnames[o]); switch (getOpMode(o)) { case iABC: lua_pushinteger(L,a); if (getBMode(o)!=OpArgN) lua_pushinteger(L,ISK(b) ? (MYK(INDEXK(b))) : b); else lua_pushnil(L); if (getCMode(o)!=OpArgN) lua_pushinteger(L,ISK(c) ? (MYK(INDEXK(c))) : c); else lua_pushnil(L); break; case iABx: lua_pushinteger(L,a); if (getBMode(o)==OpArgK) lua_pushinteger(L,MYK(bx)); else lua_pushinteger(L,bx); lua_pushnil(L); break; case iAsBx: lua_pushinteger(L,a); lua_pushinteger(L,sbx); lua_pushnil(L); break; case iAx: lua_pushinteger(L,MYK(ax)); lua_pushnil(L); lua_pushnil(L); break; } switch (o) { case OP_JMP: case OP_FORLOOP: case OP_FORPREP: case OP_TFORLOOP: lua_pop(L,1); lua_pushinteger(L,sbx+pc+2); break; default: break; } } return 5; }
static int do_getfunction(lua_State *L) /** getfunction(f,i) */ { const Proto* f=Pget(L,1); int i=luaL_checkinteger(L,2); if (i<=0 || i>f->sizep) return 0; i--; lua_pushlightuserdata(L,f->p[i]); return 1; }
static int do_getupvalue(lua_State *L) /** getupvalue(f,i) */ { const Proto* f=Pget(L,1); int i=luaL_checkinteger(L,2); if (i<=0 || i>f->sizeupvalues || f->upvalues==NULL) return 0; i--; lua_pushstring(L,getstr(f->upvalues[i])); return 1; }
static int do_setconstant(lua_State *L) /** setconstant(f,i,v) */ { const Proto* f=Pget(L,1); int i=luaL_checkinteger(L,2); if (i<=0 || i>f->sizek || f->k==NULL) return 0; i--; lua_settop(L,3); f->k[i]=L->top[-1]; return 0; }
static int Lstrip(lua_State *L) /** strip(p) */ { gpc_tristrip s; gpc_polygon *p=Pget(L,1); gpc_polygon *r=Pnew(L); gpc_polygon_to_tristrip(p,&s); r->num_contours=s.num_strips; r->contour=s.strip; return 1; }
static int do_getconstant(lua_State *L) /** getconstant(f,i) */ { const Proto* f=Pget(L,1); int i=luaL_checkinteger(L,2); if (i<=0 || i>f->sizek || f->k==NULL) return 0; i--; lua_pushnil(L); L->top[-1]=f->k[i]; return 1; }
static int do_getupvalue(lua_State *L) /** getupvalue(f,i) */ { const Proto* f=Pget(L,1); int i=luaL_checkinteger(L,2); if (i<=0 || i>f->sizeupvalues || f->upvalues==NULL) return 0; i--; lua_pushstring(L,UPVALNAME(i)); lua_pushinteger(L,f->upvalues[i].instack); lua_pushinteger(L,f->upvalues[i].idx); return 3; }
static int do_getlocal(lua_State *L) /** getlocal(f,i) */ { const Proto* f=Pget(L,1); int i=luaL_checkinteger(L,2); if (i<=0 || i>f->sizelocvars || f->locvars==NULL) return 0; i--; lua_pushstring(L,getstr(f->locvars[i].varname)); lua_pushinteger(L,f->locvars[i].startpc+1); lua_pushinteger(L,f->locvars[i].endpc+1); return 3; }
static int Lupdate(lua_State *L) /** update(c,s,...) */ { MD5_CTX *c=Pget(L,1); int i,n=lua_gettop(L); for (i=2; i<=n; i++) { size_t l; const char *s=luaL_checklstring(L,i,&l); MD5Update(c,s,l); } lua_settop(L,1); return 1; }
static int Lvaluei(lua_State *L) /** valuei(c,a,[b]) */ { MT *c=Pget(L,1); int a,b; if (lua_gettop(L)==2) { a=1; b=luaL_checkint(L,2); } else { a=luaL_checkint(L,2); b=luaL_checkint(L,3); } lua_pushnumber(L,floor(a+genrand_real2(c)*(b-a+1))); return 1; }
static int Pio(lua_State *L, const char *mode) { gpc_polygon *p=Pget(L,1); const char *file=lua_tostring(L,2); int holes=lua_toboolean(L,3); int reading=(*mode=='r'); FILE *f= (file==NULL) ? (reading ? stdin : stdout) : fopen(file,mode); if (f==NULL) { lua_pushnil(L); lua_pushstring(L,strerror(errno)); return 2; } (reading ? gpc_read_polygon : gpc_write_polygon)(f,holes,p); if (file!=NULL) fclose(f); lua_settop(L,1); return 1; }
static int Ladd(lua_State *L) /** add(p,c,[hole]) */ { int i,n; gpc_vertex_list c; gpc_polygon *p=Pget(L,1); luaL_checktype(L,2,LUA_TTABLE); n=luaL_getn(L,2)/2; c.num_vertices=n; c.vertex=malloc(c.num_vertices*sizeof(*c.vertex)); for (i=0; i<n; i++) { lua_rawgeti(L,2,2*i+1); c.vertex[i].x=lua_tonumber(L,-1); lua_rawgeti(L,2,2*i+2); c.vertex[i].y=lua_tonumber(L,-1); lua_pop(L,2); } gpc_add_contour(p,&c,lua_toboolean(L,3)); free(c.vertex); lua_settop(L,1); return 1; }
static int do_getheader(lua_State *L) /** getheader(f,i) */ { const Proto* f=Pget(L,1); const char* s=f->source ? getstr(f->source) : "=?"; if (*s=='@' || *s=='=') s++; else if (*s==LUA_SIGNATURE[0]) s="(bstring)"; else s="(string)"; lua_newtable(L); setsfield(L,"source",s); setifield(L,"line",f->linedefined); setifield(L,"lastline",f->lastlinedefined); setifield(L,"instructions",f->sizecode); setifield(L,"params",f->numparams); setbfield(L,"isvararg",f->is_vararg); setifield(L,"slots",f->maxstacksize); setifield(L,"upvalues",f->sizeupvalues); setifield(L,"locals",f->sizelocvars); setifield(L,"constants",f->sizek); setifield(L,"functions",f->sizep); return 1; }
static int Ltostring(lua_State *L) /** tostring(c) */ { MT *c=Pget(L,1); lua_pushfstring(L,"%s %p",MYTYPE,(void*)c); return 1; }
static int Lvaluex(lua_State *L) /** valuex(c) */ { MT *c=Pget(L,1); lua_pushnumber(L,genrand_res53(c)); return 1; }
static int Lseed(lua_State *L) /** seed(c,[seed]) */ { MT *c=Pget(L,1); init_genrand(c,luaL_optlong(L,2,SEED)); return 0; }
static int Ltostring(lua_State *L) { gpc_polygon *p=Pget(L,1); lua_pushfstring(L,"%s %p",MYTYPE,(void*)p); return 1; }
static int Lgc(lua_State *L) { gpc_polygon *p=Pget(L,1); gpc_free_polygon(p); return 0; }