ldump.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. ** $Id: ldump.c,v 1.19 2011/11/23 17:48:18 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)
  52. {
  53. size_t size=0;
  54. DumpVar(size,D);
  55. }
  56. else
  57. {
  58. size_t size=s->tsv.len+1; /* include trailing '\0' */
  59. DumpVar(size,D);
  60. DumpBlock(getstr(s),size*sizeof(char),D);
  61. }
  62. }
  63. #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D)
  64. static void DumpFunction(const Proto* f, 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. }
  87. }
  88. n=f->sizep;
  89. DumpInt(n,D);
  90. for (i=0; i<n; i++) DumpFunction(f->p[i],D);
  91. }
  92. static void DumpUpvalues(const Proto* f, DumpState* D)
  93. {
  94. int i,n=f->sizeupvalues;
  95. DumpInt(n,D);
  96. for (i=0; i<n; i++)
  97. {
  98. DumpChar(f->upvalues[i].instack,D);
  99. DumpChar(f->upvalues[i].idx,D);
  100. }
  101. }
  102. static void DumpDebug(const Proto* f, DumpState* D)
  103. {
  104. int i,n;
  105. DumpString((D->strip) ? NULL : f->source,D);
  106. n= (D->strip) ? 0 : f->sizelineinfo;
  107. DumpVector(f->lineinfo,n,sizeof(int),D);
  108. n= (D->strip) ? 0 : f->sizelocvars;
  109. DumpInt(n,D);
  110. for (i=0; i<n; i++)
  111. {
  112. DumpString(f->locvars[i].varname,D);
  113. DumpInt(f->locvars[i].startpc,D);
  114. DumpInt(f->locvars[i].endpc,D);
  115. }
  116. n= (D->strip) ? 0 : f->sizeupvalues;
  117. DumpInt(n,D);
  118. for (i=0; i<n; i++) DumpString(f->upvalues[i].name,D);
  119. }
  120. static void DumpFunction(const Proto* f, DumpState* D)
  121. {
  122. DumpInt(f->linedefined,D);
  123. DumpInt(f->lastlinedefined,D);
  124. DumpChar(f->numparams,D);
  125. DumpChar(f->is_vararg,D);
  126. DumpChar(f->maxstacksize,D);
  127. DumpCode(f,D);
  128. DumpConstants(f,D);
  129. DumpUpvalues(f,D);
  130. DumpDebug(f,D);
  131. }
  132. static void DumpHeader(DumpState* D)
  133. {
  134. lu_byte h[LUAC_HEADERSIZE];
  135. luaU_header(h);
  136. DumpBlock(h,LUAC_HEADERSIZE,D);
  137. }
  138. /*
  139. ** dump Lua function as precompiled chunk
  140. */
  141. int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
  142. {
  143. DumpState D;
  144. D.L=L;
  145. D.writer=w;
  146. D.data=data;
  147. D.strip=strip;
  148. D.status=0;
  149. DumpHeader(&D);
  150. DumpFunction(f,&D);
  151. return D.status;
  152. }