summaryrefslogtreecommitdiff
path: root/lundump.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-03-10 14:56:32 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-03-10 14:56:32 -0300
commit197e249433027e72e5d4ef1a13c4edc6c24bca6c (patch)
treeaf0eb4632ab7f0332fb0db448586c087166e51bd /lundump.c
parent2aff75f58eb202dbf245787f2ae98c43124724f4 (diff)
downloadlua-github-197e249433027e72e5d4ef1a13c4edc6c24bca6c.tar.gz
"indent -kr -i2 -br -brf -nut" plus a few manual formating
Diffstat (limited to 'lundump.c')
-rw-r--r--lundump.c429
1 files changed, 222 insertions, 207 deletions
diff --git a/lundump.c b/lundump.c
index 9a3ee218..ae2d19f9 100644
--- a/lundump.c
+++ b/lundump.c
@@ -1,5 +1,5 @@
/*
-** $Id: lundump.c,v 2.29 2014/02/28 16:13:01 roberto Exp roberto $
+** $Id: lundump.c,v 2.30 2014/03/01 15:18:44 roberto Exp roberto $
** load precompiled Lua chunks
** See Copyright Notice in lua.h
*/
@@ -20,227 +20,242 @@
#include "lundump.h"
#include "lzio.h"
-typedef struct {
- lua_State* L;
- ZIO* Z;
- Mbuffer* b;
- const char* name;
-} LoadState;
-
-static l_noret error(LoadState* S, const char* why)
-{
- luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why);
- luaD_throw(S->L,LUA_ERRSYNTAX);
-}
#define LoadVar(S,x) LoadBlock(S,&x,sizeof(x))
#define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0]))
#if !defined(luai_verifycode)
-#define luai_verifycode(L,b,f) /* empty */
+#define luai_verifycode(L,b,f) /* empty */
#endif
-static void LoadBlock(LoadState* S, void* b, size_t size)
-{
- if (luaZ_read(S->Z,b,size)!=0) error(S,"truncated");
-}
-
-static lu_byte LoadByte(LoadState* S)
-{
- lu_byte x;
- LoadVar(S,x);
- return x;
-}
-
-static int LoadInt(LoadState* S)
-{
- int x;
- LoadVar(S,x);
- if (x<0) error(S,"corrupted");
- return x;
-}
-
-static lua_Number LoadNumber(LoadState* S)
-{
- lua_Number x;
- LoadVar(S,x);
- return x;
-}
-
-static lua_Integer LoadInteger(LoadState* S)
-{
- lua_Integer x;
- LoadVar(S,x);
- return x;
-}
-
-static TString* LoadString(LoadState* S)
-{
- size_t size = LoadByte(S);
- if (size == 0xFF) LoadVar(S,size);
- if (size==0)
- return NULL;
- else
- {
- char* s=luaZ_openspace(S->L,S->b,--size);
- LoadVector(S,s,size);
- return luaS_newlstr(S->L,s,size);
- }
-}
-
-static void LoadCode(LoadState* S, Proto* f)
-{
- int n=LoadInt(S);
- f->code=luaM_newvector(S->L,n,Instruction);
- f->sizecode=n;
- LoadVector(S,f->code,n);
-}
-
-static void LoadFunction(LoadState* S, Proto* f);
-
-static void LoadConstants(LoadState* S, Proto* f)
-{
- int i,n;
- n=LoadInt(S);
- f->k=luaM_newvector(S->L,n,TValue);
- f->sizek=n;
- for (i=0; i<n; i++) setnilvalue(&f->k[i]);
- for (i=0; i<n; i++)
- {
- TValue* o=&f->k[i];
- int t=LoadByte(S);
- switch (t)
- {
- case LUA_TNIL:
- setnilvalue(o);
- break;
- case LUA_TBOOLEAN:
- setbvalue(o,LoadByte(S));
- break;
- case LUA_TNUMFLT:
- setnvalue(o,LoadNumber(S));
- break;
- case LUA_TNUMINT:
- setivalue(o,LoadInteger(S));
- break;
- case LUA_TSHRSTR: case LUA_TLNGSTR:
- setsvalue2n(S->L,o,LoadString(S));
- break;
- default: lua_assert(0);
+
+typedef struct {
+ lua_State *L;
+ ZIO *Z;
+ Mbuffer *b;
+ const char *name;
+} LoadState;
+
+
+static l_noret error(LoadState *S, const char *why) {
+ luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why);
+ luaD_throw(S->L, LUA_ERRSYNTAX);
+}
+
+
+static void LoadBlock (LoadState *S, void *b, size_t size) {
+ if (luaZ_read(S->Z, b, size) != 0)
+ error(S, "truncated");
+}
+
+
+static lu_byte LoadByte (LoadState *S) {
+ lu_byte x;
+ LoadVar(S, x);
+ return x;
+}
+
+
+static int LoadInt (LoadState *S) {
+ int x;
+ LoadVar(S, x);
+ if (x < 0)
+ error(S, "corrupted");
+ return x;
+}
+
+
+static lua_Number LoadNumber (LoadState *S) {
+ lua_Number x;
+ LoadVar(S, x);
+ return x;
+}
+
+
+static lua_Integer LoadInteger (LoadState *S) {
+ lua_Integer x;
+ LoadVar(S, x);
+ return x;
+}
+
+
+static TString *LoadString (LoadState *S) {
+ size_t size = LoadByte(S);
+ if (size == 0xFF)
+ LoadVar(S, size);
+ if (size == 0)
+ return NULL;
+ else {
+ char *s = luaZ_openspace(S->L, S->b, --size);
+ LoadVector(S, s, size);
+ return luaS_newlstr(S->L, s, size);
}
- }
- n=LoadInt(S);
- f->p=luaM_newvector(S->L,n,Proto*);
- f->sizep=n;
- for (i=0; i<n; i++) f->p[i]=NULL;
- for (i=0; i<n; i++)
- {
- f->p[i]=luaF_newproto(S->L);
- LoadFunction(S,f->p[i]);
- }
-}
-
-static void LoadUpvalues(LoadState* S, Proto* f)
-{
- int i,n;
- n=LoadInt(S);
- f->upvalues=luaM_newvector(S->L,n,Upvaldesc);
- f->sizeupvalues=n;
- for (i=0; i<n; i++) f->upvalues[i].name=NULL;
- for (i=0; i<n; i++)
- {
- f->upvalues[i].instack=LoadByte(S);
- f->upvalues[i].idx=LoadByte(S);
- }
-}
-
-static void LoadDebug(LoadState* S, Proto* f)
-{
- int i,n;
- f->source=LoadString(S);
- n=LoadInt(S);
- f->lineinfo=luaM_newvector(S->L,n,int);
- f->sizelineinfo=n;
- LoadVector(S,f->lineinfo,n);
- n=LoadInt(S);
- f->locvars=luaM_newvector(S->L,n,LocVar);
- f->sizelocvars=n;
- for (i=0; i<n; i++) f->locvars[i].varname=NULL;
- for (i=0; i<n; i++)
- {
- f->locvars[i].varname=LoadString(S);
- f->locvars[i].startpc=LoadInt(S);
- f->locvars[i].endpc=LoadInt(S);
- }
- n=LoadInt(S);
- for (i=0; i<n; i++) f->upvalues[i].name=LoadString(S);
-}
-
-static void LoadFunction(LoadState* S, Proto* f)
-{
- f->linedefined=LoadInt(S);
- f->lastlinedefined=LoadInt(S);
- f->numparams=LoadByte(S);
- f->is_vararg=LoadByte(S);
- f->maxstacksize=LoadByte(S);
- LoadCode(S,f);
- LoadConstants(S,f);
- LoadUpvalues(S,f);
- LoadDebug(S,f);
-}
-
-static void checkstring(LoadState *S, const char *s, const char *msg)
-{
- char buff[sizeof(LUA_SIGNATURE)+sizeof(LUAC_DATA)]; /* larger than each */
- LoadVector(S,buff,strlen(s)+1);
- if (strcmp(s,buff)!=0) error(S,msg);
-}
-
-static void fchecksize(LoadState *S, size_t size, const char *tname)
-{
- if (LoadByte(S) != size)
- error(S,luaO_pushfstring(S->L,"%s size mismatch in",tname));
}
+
+static void LoadCode (LoadState *S, Proto *f) {
+ int n = LoadInt(S);
+ f->code = luaM_newvector(S->L, n, Instruction);
+ f->sizecode = n;
+ LoadVector(S, f->code, n);
+}
+
+
+static void LoadFunction(LoadState *S, Proto *f);
+
+
+static void LoadConstants (LoadState *S, Proto *f) {
+ int i, n;
+ n = LoadInt(S);
+ f->k = luaM_newvector(S->L, n, TValue);
+ f->sizek = n;
+ for (i = 0; i < n; i++)
+ setnilvalue(&f->k[i]);
+ for (i = 0; i < n; i++) {
+ TValue *o = &f->k[i];
+ int t = LoadByte(S);
+ switch (t) {
+ case LUA_TNIL:
+ setnilvalue(o);
+ break;
+ case LUA_TBOOLEAN:
+ setbvalue(o, LoadByte(S));
+ break;
+ case LUA_TNUMFLT:
+ setnvalue(o, LoadNumber(S));
+ break;
+ case LUA_TNUMINT:
+ setivalue(o, LoadInteger(S));
+ break;
+ case LUA_TSHRSTR:
+ case LUA_TLNGSTR:
+ setsvalue2n(S->L, o, LoadString(S));
+ break;
+ default:
+ lua_assert(0);
+ }
+ }
+ n = LoadInt(S);
+ f->p = luaM_newvector(S->L, n, Proto *);
+ f->sizep = n;
+ for (i = 0; i < n; i++)
+ f->p[i] = NULL;
+ for (i = 0; i < n; i++) {
+ f->p[i] = luaF_newproto(S->L);
+ LoadFunction(S, f->p[i]);
+ }
+}
+
+
+static void LoadUpvalues (LoadState *S, Proto *f) {
+ int i, n;
+ n = LoadInt(S);
+ f->upvalues = luaM_newvector(S->L, n, Upvaldesc);
+ f->sizeupvalues = n;
+ for (i = 0; i < n; i++)
+ f->upvalues[i].name = NULL;
+ for (i = 0; i < n; i++) {
+ f->upvalues[i].instack = LoadByte(S);
+ f->upvalues[i].idx = LoadByte(S);
+ }
+}
+
+
+static void LoadDebug (LoadState *S, Proto *f) {
+ int i, n;
+ f->source = LoadString(S);
+ n = LoadInt(S);
+ f->lineinfo = luaM_newvector(S->L, n, int);
+ f->sizelineinfo = n;
+ LoadVector(S, f->lineinfo, n);
+ n = LoadInt(S);
+ f->locvars = luaM_newvector(S->L, n, LocVar);
+ f->sizelocvars = n;
+ for (i = 0; i < n; i++)
+ f->locvars[i].varname = NULL;
+ for (i = 0; i < n; i++) {
+ f->locvars[i].varname = LoadString(S);
+ f->locvars[i].startpc = LoadInt(S);
+ f->locvars[i].endpc = LoadInt(S);
+ }
+ n = LoadInt(S);
+ for (i = 0; i < n; i++)
+ f->upvalues[i].name = LoadString(S);
+}
+
+
+static void LoadFunction (LoadState *S, Proto *f) {
+ f->linedefined = LoadInt(S);
+ f->lastlinedefined = LoadInt(S);
+ f->numparams = LoadByte(S);
+ f->is_vararg = LoadByte(S);
+ f->maxstacksize = LoadByte(S);
+ LoadCode(S, f);
+ LoadConstants(S, f);
+ LoadUpvalues(S, f);
+ LoadDebug(S, f);
+}
+
+
+static void checkstring (LoadState *S, const char *s, const char *msg) {
+ char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than each */
+ LoadVector(S, buff, strlen(s) + 1);
+ if (strcmp(s, buff) != 0)
+ error(S, msg);
+}
+
+
+static void fchecksize (LoadState *S, size_t size, const char *tname) {
+ if (LoadByte(S) != size)
+ error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname));
+}
+
+
#define checksize(S,t) fchecksize(S,sizeof(t),#t)
-static void checkHeader(LoadState* S)
-{
- checkstring(S,LUA_SIGNATURE+1,"not a");
- checkstring(S,LUAC_DATA,"corrupted");
- if (LoadByte(S) != LUAC_VERSION) error(S,"version mismatch in");
- if (LoadByte(S) != LUAC_FORMAT) error(S,"format mismatch in");
- checksize(S,int);
- checksize(S,size_t);
- checksize(S,Instruction);
- checksize(S,lua_Integer);
- checksize(S,lua_Number);
- if (LoadInteger(S) != LUAC_INT) error(S,"endianess mismatch in");
- if (LoadNumber(S) != LUAC_NUM) error(S,"float format mismatch in");
+static void checkHeader (LoadState *S) {
+ checkstring(S, LUA_SIGNATURE + 1, "not a");
+ checkstring(S, LUAC_DATA, "corrupted");
+ if (LoadByte(S) != LUAC_VERSION)
+ error(S, "version mismatch in");
+ if (LoadByte(S) != LUAC_FORMAT)
+ error(S, "format mismatch in");
+ checksize(S, int);
+ checksize(S, size_t);
+ checksize(S, Instruction);
+ checksize(S, lua_Integer);
+ checksize(S, lua_Number);
+ if (LoadInteger(S) != LUAC_INT)
+ error(S, "endianess mismatch in");
+ if (LoadNumber(S) != LUAC_NUM)
+ error(S, "float format mismatch in");
}
+
/*
** load precompiled chunk
*/
-Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
-{
- LoadState S;
- Closure* cl;
- if (*name=='@' || *name=='=')
- S.name=name+1;
- else if (*name==LUA_SIGNATURE[0])
- S.name="binary string";
- else
- S.name=name;
- S.L=L;
- S.Z=Z;
- S.b=buff;
- checkHeader(&S);
- cl=luaF_newLclosure(L,LoadByte(&S));
- setclLvalue(L,L->top,cl); incr_top(L);
- cl->l.p=luaF_newproto(L);
- LoadFunction(&S,cl->l.p);
- lua_assert(cl->l.nupvalues==cl->l.p->sizeupvalues);
- luai_verifycode(L,buff,cl->l.p);
- return cl;
+Closure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff,
+ const char *name) {
+ LoadState S;
+ Closure *cl;
+ if (*name == '@' || *name == '=')
+ S.name = name + 1;
+ else if (*name == LUA_SIGNATURE[0])
+ S.name = "binary string";
+ else
+ S.name = name;
+ S.L = L;
+ S.Z = Z;
+ S.b = buff;
+ checkHeader(&S);
+ cl = luaF_newLclosure(L, LoadByte(&S));
+ setclLvalue(L, L->top, cl);
+ incr_top(L);
+ cl->l.p = luaF_newproto(L);
+ LoadFunction(&S, cl->l.p);
+ lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues);
+ luai_verifycode(L, buff, cl->l.p);
+ return cl;
}
+