Lua 5.1.4: ldump.c


L0001    /*
L0002    ** $Id: ldump.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
L0003    ** save precompiled Lua chunks
L0004    ** See Copyright Notice in lua.h
L0005    */
L0006    
L0007    #include <stddef.h>
L0008    
L0009    #define ldump_c
L0010    #define LUA_CORE
L0011    
L0012    #include "lua.h"
L0013    
L0014    #include "lobject.h"
L0015    #include "lstate.h"
L0016    #include "lundump.h"
L0017    
L0018    typedef struct {
L0019     lua_State* L;
L0020     lua_Writer writer;
L0021     void* data;
L0022     int strip;
L0023     int status;
L0024    } DumpState;
L0025    
L0026    #define DumpMem(b,n,size,D)	DumpBlock(b,(n)*(size),D)
L0027    #define DumpVar(x,D)	 	DumpMem(&x,1,sizeof(x),D)
L0028    
L0029    static void DumpBlock(const void* b, size_t size, DumpState* D)
L0030    {
L0031     if (D->status==0)
L0032     {
L0033      lua_unlock(D->L);
L0034      D->status=(*D->writer)(D->L,b,size,D->data);
L0035      lua_lock(D->L);
L0036     }
L0037    }
L0038    
L0039    static void DumpChar(int y, DumpState* D)
L0040    {
L0041     char x=(char)y;
L0042     DumpVar(x,D);
L0043    }
L0044    
L0045    static void DumpInt(int x, DumpState* D)
L0046    {
L0047     DumpVar(x,D);
L0048    }
L0049    
L0050    static void DumpNumber(lua_Number x, DumpState* D)
L0051    {
L0052     DumpVar(x,D);
L0053    }
L0054    
L0055    static void DumpVector(const void* b, int n, size_t size, DumpState* D)
L0056    {
L0057     DumpInt(n,D);
L0058     DumpMem(b,n,size,D);
L0059    }
L0060    
L0061    static void DumpString(const TString* s, DumpState* D)
L0062    {
L0063     if (s==NULL || getstr(s)==NULL)
L0064     {
L0065      size_t size=0;
L0066      DumpVar(size,D);
L0067     }
L0068     else
L0069     {
L0070      size_t size=s->tsv.len+1;		/* include trailing '\0' */
L0071      DumpVar(size,D);
L0072      DumpBlock(getstr(s),size,D);
L0073     }
L0074    }
L0075    
L0076    #define DumpCode(f,D)	 DumpVector(f->code,f->sizecode,sizeof(Instruction),D)
L0077    
L0078    static void DumpFunction(const Proto* f, const TString* p, DumpState* D);
L0079    
L0080    static void DumpConstants(const Proto* f, DumpState* D)
L0081    {
L0082     int i,n=f->sizek;
L0083     DumpInt(n,D);
L0084     for (i=0; i<n; i++)
L0085     {
L0086      const TValue* o=&f->k[i];
L0087      DumpChar(ttype(o),D);
L0088      switch (ttype(o))
L0089      {
L0090       case LUA_TNIL:
L0091    	break;
L0092       case LUA_TBOOLEAN:
L0093    	DumpChar(bvalue(o),D);
L0094    	break;
L0095       case LUA_TNUMBER:
L0096    	DumpNumber(nvalue(o),D);
L0097    	break;
L0098       case LUA_TSTRING:
L0099    	DumpString(rawtsvalue(o),D);
L0100    	break;
L0101       default:
L0102    	lua_assert(0);			/* cannot happen */
L0103    	break;
L0104      }
L0105     }
L0106     n=f->sizep;
L0107     DumpInt(n,D);
L0108     for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D);
L0109    }
L0110    
L0111    static void DumpDebug(const Proto* f, DumpState* D)
L0112    {
L0113     int i,n;
L0114     n= (D->strip) ? 0 : f->sizelineinfo;
L0115     DumpVector(f->lineinfo,n,sizeof(int),D);
L0116     n= (D->strip) ? 0 : f->sizelocvars;
L0117     DumpInt(n,D);
L0118     for (i=0; i<n; i++)
L0119     {
L0120      DumpString(f->locvars[i].varname,D);
L0121      DumpInt(f->locvars[i].startpc,D);
L0122      DumpInt(f->locvars[i].endpc,D);
L0123     }
L0124     n= (D->strip) ? 0 : f->sizeupvalues;
L0125     DumpInt(n,D);
L0126     for (i=0; i<n; i++) DumpString(f->upvalues[i],D);
L0127    }
L0128    
L0129    static void DumpFunction(const Proto* f, const TString* p, DumpState* D)
L0130    {
L0131     DumpString((f->source==p || D->strip) ? NULL : f->source,D);
L0132     DumpInt(f->linedefined,D);
L0133     DumpInt(f->lastlinedefined,D);
L0134     DumpChar(f->nups,D);
L0135     DumpChar(f->numparams,D);
L0136     DumpChar(f->is_vararg,D);
L0137     DumpChar(f->maxstacksize,D);
L0138     DumpCode(f,D);
L0139     DumpConstants(f,D);
L0140     DumpDebug(f,D);
L0141    }
L0142    
L0143    static void DumpHeader(DumpState* D)
L0144    {
L0145     char h[LUAC_HEADERSIZE];
L0146     luaU_header(h);
L0147     DumpBlock(h,LUAC_HEADERSIZE,D);
L0148    }
L0149    
L0150    /*
L0151    ** dump Lua function as precompiled chunk
L0152    */
L0153    int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
L0154    {
L0155     DumpState D;
L0156     D.L=L;
L0157     D.writer=w;
L0158     D.data=data;
L0159     D.strip=strip;
L0160     D.status=0;
L0161     DumpHeader(&D);
L0162     DumpFunction(f,NULL,&D);
L0163     return D.status;
L0164    }

Generated by pretty.lua