/lua/ldump.c

https://bitbucket.org/cesbo/astra · C · 173 lines · 145 code · 20 blank · 8 comment · 8 complexity · 6e89f5428baf4bc956c58602597c0208 MD5 · raw file

  1. /*
  2. ** $Id: ldump.c,v 2.17.1.1 2013/04/12 18:48:47 roberto 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(ttypenv(o),D);
  73. switch (ttypenv(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: lua_assert(0);
  87. }
  88. }
  89. n=f->sizep;
  90. DumpInt(n,D);
  91. for (i=0; i<n; i++) DumpFunction(f->p[i],D);
  92. }
  93. static void DumpUpvalues(const Proto* f, DumpState* D)
  94. {
  95. int i,n=f->sizeupvalues;
  96. DumpInt(n,D);
  97. for (i=0; i<n; i++)
  98. {
  99. DumpChar(f->upvalues[i].instack,D);
  100. DumpChar(f->upvalues[i].idx,D);
  101. }
  102. }
  103. static void DumpDebug(const Proto* f, DumpState* D)
  104. {
  105. int i,n;
  106. DumpString((D->strip) ? NULL : f->source,D);
  107. n= (D->strip) ? 0 : f->sizelineinfo;
  108. DumpVector(f->lineinfo,n,sizeof(int),D);
  109. n= (D->strip) ? 0 : f->sizelocvars;
  110. DumpInt(n,D);
  111. for (i=0; i<n; i++)
  112. {
  113. DumpString(f->locvars[i].varname,D);
  114. DumpInt(f->locvars[i].startpc,D);
  115. DumpInt(f->locvars[i].endpc,D);
  116. }
  117. n= (D->strip) ? 0 : f->sizeupvalues;
  118. DumpInt(n,D);
  119. for (i=0; i<n; i++) DumpString(f->upvalues[i].name,D);
  120. }
  121. static void DumpFunction(const Proto* f, DumpState* D)
  122. {
  123. DumpInt(f->linedefined,D);
  124. DumpInt(f->lastlinedefined,D);
  125. DumpChar(f->numparams,D);
  126. DumpChar(f->is_vararg,D);
  127. DumpChar(f->maxstacksize,D);
  128. DumpCode(f,D);
  129. DumpConstants(f,D);
  130. DumpUpvalues(f,D);
  131. DumpDebug(f,D);
  132. }
  133. static void DumpHeader(DumpState* D)
  134. {
  135. lu_byte h[LUAC_HEADERSIZE];
  136. luaU_header(h);
  137. DumpBlock(h,LUAC_HEADERSIZE,D);
  138. }
  139. /*
  140. ** dump Lua function as precompiled chunk
  141. */
  142. int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
  143. {
  144. DumpState D;
  145. D.L=L;
  146. D.writer=w;
  147. D.data=data;
  148. D.strip=strip;
  149. D.status=0;
  150. DumpHeader(&D);
  151. DumpFunction(f,&D);
  152. return D.status;
  153. }