diff src/lua/ldump.c @ 11:27763b933818

raise lua sources up one level
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 11:07:39 -0600
parents src/lua/src/ldump.c@f9f4f1b99eed
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/lua/ldump.c	Sat Mar 03 11:07:39 2012 -0600
     1.3 @@ -0,0 +1,164 @@
     1.4 +/*
     1.5 +** $Id: ldump.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
     1.6 +** save precompiled Lua chunks
     1.7 +** See Copyright Notice in lua.h
     1.8 +*/
     1.9 +
    1.10 +#include <stddef.h>
    1.11 +
    1.12 +#define ldump_c
    1.13 +#define LUA_CORE
    1.14 +
    1.15 +#include "lua.h"
    1.16 +
    1.17 +#include "lobject.h"
    1.18 +#include "lstate.h"
    1.19 +#include "lundump.h"
    1.20 +
    1.21 +typedef struct {
    1.22 + lua_State* L;
    1.23 + lua_Writer writer;
    1.24 + void* data;
    1.25 + int strip;
    1.26 + int status;
    1.27 +} DumpState;
    1.28 +
    1.29 +#define DumpMem(b,n,size,D)	DumpBlock(b,(n)*(size),D)
    1.30 +#define DumpVar(x,D)	 	DumpMem(&x,1,sizeof(x),D)
    1.31 +
    1.32 +static void DumpBlock(const void* b, size_t size, DumpState* D)
    1.33 +{
    1.34 + if (D->status==0)
    1.35 + {
    1.36 +  lua_unlock(D->L);
    1.37 +  D->status=(*D->writer)(D->L,b,size,D->data);
    1.38 +  lua_lock(D->L);
    1.39 + }
    1.40 +}
    1.41 +
    1.42 +static void DumpChar(int y, DumpState* D)
    1.43 +{
    1.44 + char x=(char)y;
    1.45 + DumpVar(x,D);
    1.46 +}
    1.47 +
    1.48 +static void DumpInt(int x, DumpState* D)
    1.49 +{
    1.50 + DumpVar(x,D);
    1.51 +}
    1.52 +
    1.53 +static void DumpNumber(lua_Number x, DumpState* D)
    1.54 +{
    1.55 + DumpVar(x,D);
    1.56 +}
    1.57 +
    1.58 +static void DumpVector(const void* b, int n, size_t size, DumpState* D)
    1.59 +{
    1.60 + DumpInt(n,D);
    1.61 + DumpMem(b,n,size,D);
    1.62 +}
    1.63 +
    1.64 +static void DumpString(const TString* s, DumpState* D)
    1.65 +{
    1.66 + if (s==NULL || getstr(s)==NULL)
    1.67 + {
    1.68 +  size_t size=0;
    1.69 +  DumpVar(size,D);
    1.70 + }
    1.71 + else
    1.72 + {
    1.73 +  size_t size=s->tsv.len+1;		/* include trailing '\0' */
    1.74 +  DumpVar(size,D);
    1.75 +  DumpBlock(getstr(s),size,D);
    1.76 + }
    1.77 +}
    1.78 +
    1.79 +#define DumpCode(f,D)	 DumpVector(f->code,f->sizecode,sizeof(Instruction),D)
    1.80 +
    1.81 +static void DumpFunction(const Proto* f, const TString* p, DumpState* D);
    1.82 +
    1.83 +static void DumpConstants(const Proto* f, DumpState* D)
    1.84 +{
    1.85 + int i,n=f->sizek;
    1.86 + DumpInt(n,D);
    1.87 + for (i=0; i<n; i++)
    1.88 + {
    1.89 +  const TValue* o=&f->k[i];
    1.90 +  DumpChar(ttype(o),D);
    1.91 +  switch (ttype(o))
    1.92 +  {
    1.93 +   case LUA_TNIL:
    1.94 +	break;
    1.95 +   case LUA_TBOOLEAN:
    1.96 +	DumpChar(bvalue(o),D);
    1.97 +	break;
    1.98 +   case LUA_TNUMBER:
    1.99 +	DumpNumber(nvalue(o),D);
   1.100 +	break;
   1.101 +   case LUA_TSTRING:
   1.102 +	DumpString(rawtsvalue(o),D);
   1.103 +	break;
   1.104 +   default:
   1.105 +	lua_assert(0);			/* cannot happen */
   1.106 +	break;
   1.107 +  }
   1.108 + }
   1.109 + n=f->sizep;
   1.110 + DumpInt(n,D);
   1.111 + for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D);
   1.112 +}
   1.113 +
   1.114 +static void DumpDebug(const Proto* f, DumpState* D)
   1.115 +{
   1.116 + int i,n;
   1.117 + n= (D->strip) ? 0 : f->sizelineinfo;
   1.118 + DumpVector(f->lineinfo,n,sizeof(int),D);
   1.119 + n= (D->strip) ? 0 : f->sizelocvars;
   1.120 + DumpInt(n,D);
   1.121 + for (i=0; i<n; i++)
   1.122 + {
   1.123 +  DumpString(f->locvars[i].varname,D);
   1.124 +  DumpInt(f->locvars[i].startpc,D);
   1.125 +  DumpInt(f->locvars[i].endpc,D);
   1.126 + }
   1.127 + n= (D->strip) ? 0 : f->sizeupvalues;
   1.128 + DumpInt(n,D);
   1.129 + for (i=0; i<n; i++) DumpString(f->upvalues[i],D);
   1.130 +}
   1.131 +
   1.132 +static void DumpFunction(const Proto* f, const TString* p, DumpState* D)
   1.133 +{
   1.134 + DumpString((f->source==p || D->strip) ? NULL : f->source,D);
   1.135 + DumpInt(f->linedefined,D);
   1.136 + DumpInt(f->lastlinedefined,D);
   1.137 + DumpChar(f->nups,D);
   1.138 + DumpChar(f->numparams,D);
   1.139 + DumpChar(f->is_vararg,D);
   1.140 + DumpChar(f->maxstacksize,D);
   1.141 + DumpCode(f,D);
   1.142 + DumpConstants(f,D);
   1.143 + DumpDebug(f,D);
   1.144 +}
   1.145 +
   1.146 +static void DumpHeader(DumpState* D)
   1.147 +{
   1.148 + char h[LUAC_HEADERSIZE];
   1.149 + luaU_header(h);
   1.150 + DumpBlock(h,LUAC_HEADERSIZE,D);
   1.151 +}
   1.152 +
   1.153 +/*
   1.154 +** dump Lua function as precompiled chunk
   1.155 +*/
   1.156 +int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
   1.157 +{
   1.158 + DumpState D;
   1.159 + D.L=L;
   1.160 + D.writer=w;
   1.161 + D.data=data;
   1.162 + D.strip=strip;
   1.163 + D.status=0;
   1.164 + DumpHeader(&D);
   1.165 + DumpFunction(f,NULL,&D);
   1.166 + return D.status;
   1.167 +}