ldump.c
上传用户:yisoukefu
上传日期:2020-08-09
资源大小:39506k
文件大小:3k
源码类别:

其他游戏

开发平台:

Visual C++

  1. /*
  2. ** $Id: ldump.c,v 1.15 2006/02/16 15:53:49 lhf Exp $
  3. ** save precompiled Lua chunks
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stddef.h>
  7. #define ldump_c
  8. #define LUA_CORE
  9. #include "lua.h"
  10. #include "lobject.h"
  11. #include "lstate.h"
  12. #include "lundump.h"
  13. typedef struct {
  14.  lua_State* L;
  15.  lua_Writer writer;
  16.  void* data;
  17.  int strip;
  18.  int status;
  19. } DumpState;
  20. #define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D)
  21. #define DumpVar(x,D)   DumpMem(&x,1,sizeof(x),D)
  22. static void DumpBlock(const void* b, size_t size, DumpState* D)
  23. {
  24.  if (D->status==0)
  25.  {
  26.   lua_unlock(D->L);
  27.   D->status=(*D->writer)(D->L,b,size,D->data);
  28.   lua_lock(D->L);
  29.  }
  30. }
  31. static void DumpChar(int y, DumpState* D)
  32. {
  33.  char x=(char)y;
  34.  DumpVar(x,D);
  35. }
  36. static void DumpInt(int x, DumpState* D)
  37. {
  38.  DumpVar(x,D);
  39. }
  40. static void DumpNumber(lua_Number x, DumpState* D)
  41. {
  42.  DumpVar(x,D);
  43. }
  44. static void DumpVector(const void* b, int n, size_t size, DumpState* D)
  45. {
  46.  DumpInt(n,D);
  47.  DumpMem(b,n,size,D);
  48. }
  49. static void DumpString(const TString* s, DumpState* D)
  50. {
  51.  if (s==NULL || getstr(s)==NULL)
  52.  {
  53.   size_t size=0;
  54.   DumpVar(size,D);
  55.  }
  56.  else
  57.  {
  58.   size_t size=s->tsv.len+1; /* include trailing '' */
  59.   DumpVar(size,D);
  60.   DumpBlock(getstr(s),size,D);
  61.  }
  62. }
  63. #define DumpCode(f,D)  DumpVector(f->code,f->sizecode,sizeof(Instruction),D)
  64. static void DumpFunction(const Proto* f, const TString* p, DumpState* D);
  65. static void DumpConstants(const Proto* f, DumpState* D)
  66. {
  67.  int i,n=f->sizek;
  68.  DumpInt(n,D);
  69.  for (i=0; i<n; i++)
  70.  {
  71.   const TValue* o=&f->k[i];
  72.   DumpChar(ttype(o),D);
  73.   switch (ttype(o))
  74.   {
  75.    case LUA_TNIL:
  76. break;
  77.    case LUA_TBOOLEAN:
  78. DumpChar(bvalue(o),D);
  79. break;
  80.    case LUA_TNUMBER:
  81. DumpNumber(nvalue(o),D);
  82. break;
  83.    case LUA_TSTRING:
  84. DumpString(rawtsvalue(o),D);
  85. break;
  86.    default:
  87. lua_assert(0); /* cannot happen */
  88. break;
  89.   }
  90.  }
  91.  n=f->sizep;
  92.  DumpInt(n,D);
  93.  for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D);
  94. }
  95. static void DumpDebug(const Proto* f, DumpState* D)
  96. {
  97.  int i,n;
  98.  n= (D->strip) ? 0 : f->sizelineinfo;
  99.  DumpVector(f->lineinfo,n,sizeof(int),D);
  100.  n= (D->strip) ? 0 : f->sizelocvars;
  101.  DumpInt(n,D);
  102.  for (i=0; i<n; i++)
  103.  {
  104.   DumpString(f->locvars[i].varname,D);
  105.   DumpInt(f->locvars[i].startpc,D);
  106.   DumpInt(f->locvars[i].endpc,D);
  107.  }
  108.  n= (D->strip) ? 0 : f->sizeupvalues;
  109.  DumpInt(n,D);
  110.  for (i=0; i<n; i++) DumpString(f->upvalues[i],D);
  111. }
  112. static void DumpFunction(const Proto* f, const TString* p, DumpState* D)
  113. {
  114.  DumpString((f->source==p || D->strip) ? NULL : f->source,D);
  115.  DumpInt(f->linedefined,D);
  116.  DumpInt(f->lastlinedefined,D);
  117.  DumpChar(f->nups,D);
  118.  DumpChar(f->numparams,D);
  119.  DumpChar(f->is_vararg,D);
  120.  DumpChar(f->maxstacksize,D);
  121.  DumpCode(f,D);
  122.  DumpConstants(f,D);
  123.  DumpDebug(f,D);
  124. }
  125. static void DumpHeader(DumpState* D)
  126. {
  127.  char h[LUAC_HEADERSIZE];
  128.  luaU_header(h);
  129.  DumpBlock(h,LUAC_HEADERSIZE,D);
  130. }
  131. /*
  132. ** dump Lua function as precompiled chunk
  133. */
  134. int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
  135. {
  136.  DumpState D;
  137.  D.L=L;
  138.  D.writer=w;
  139.  D.data=data;
  140.  D.strip=strip;
  141.  D.status=0;
  142.  DumpHeader(&D);
  143.  DumpFunction(f,NULL,&D);
  144.  return D.status;
  145. }