Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

178 строки
3.2 KiB

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