ldump.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. ** $Id: ldump.c $
  3. ** save precompiled Lua chunks
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define ldump_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <stddef.h>
  10. #include "lua.h"
  11. #include "lobject.h"
  12. #include "lstate.h"
  13. #include "lundump.h"
  14. typedef struct {
  15. lua_State *L;
  16. lua_Writer writer;
  17. void *data;
  18. int strip;
  19. int status;
  20. } DumpState;
  21. /*
  22. ** All high-level dumps go through DumpVector; you can change it to
  23. ** change the endianness of the result
  24. */
  25. #define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D)
  26. #define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D)
  27. static void DumpBlock (const void *b, size_t size, DumpState *D) {
  28. if (D->status == 0 && size > 0) {
  29. lua_unlock(D->L);
  30. D->status = (*D->writer)(D->L, b, size, D->data);
  31. lua_lock(D->L);
  32. }
  33. }
  34. #define DumpVar(x,D) DumpVector(&x,1,D)
  35. static void DumpByte (int y, DumpState *D) {
  36. lu_byte x = (lu_byte)y;
  37. DumpVar(x, D);
  38. }
  39. /* DumpInt Buff Size */
  40. #define DIBS ((sizeof(size_t) * 8 / 7) + 1)
  41. static void DumpSize (size_t x, DumpState *D) {
  42. lu_byte buff[DIBS];
  43. int n = 0;
  44. do {
  45. buff[DIBS - (++n)] = x & 0x7f; /* fill buffer in reverse order */
  46. x >>= 7;
  47. } while (x != 0);
  48. buff[DIBS - 1] |= 0x80; /* mark last byte */
  49. DumpVector(buff + DIBS - n, n, D);
  50. }
  51. static void DumpInt (int x, DumpState *D) {
  52. DumpSize(x, D);
  53. }
  54. static void DumpNumber (lua_Number x, DumpState *D) {
  55. DumpVar(x, D);
  56. }
  57. static void DumpInteger (lua_Integer x, DumpState *D) {
  58. DumpVar(x, D);
  59. }
  60. static void DumpString (const TString *s, DumpState *D) {
  61. if (s == NULL)
  62. DumpSize(0, D);
  63. else {
  64. size_t size = tsslen(s);
  65. const char *str = getstr(s);
  66. DumpSize(size + 1, D);
  67. DumpVector(str, size, D);
  68. }
  69. }
  70. static void DumpCode (const Proto *f, DumpState *D) {
  71. DumpInt(f->sizecode, D);
  72. DumpVector(f->code, f->sizecode, D);
  73. }
  74. static void DumpFunction(const Proto *f, TString *psource, DumpState *D);
  75. static void DumpConstants (const Proto *f, DumpState *D) {
  76. int i;
  77. int n = f->sizek;
  78. DumpInt(n, D);
  79. for (i = 0; i < n; i++) {
  80. const TValue *o = &f->k[i];
  81. DumpByte(ttypetag(o), D);
  82. switch (ttypetag(o)) {
  83. case LUA_TNIL:
  84. break;
  85. case LUA_TBOOLEAN:
  86. DumpByte(bvalue(o), D);
  87. break;
  88. case LUA_TNUMFLT:
  89. DumpNumber(fltvalue(o), D);
  90. break;
  91. case LUA_TNUMINT:
  92. DumpInteger(ivalue(o), D);
  93. break;
  94. case LUA_TSHRSTR:
  95. case LUA_TLNGSTR:
  96. DumpString(tsvalue(o), D);
  97. break;
  98. default: lua_assert(0);
  99. }
  100. }
  101. }
  102. static void DumpProtos (const Proto *f, DumpState *D) {
  103. int i;
  104. int n = f->sizep;
  105. DumpInt(n, D);
  106. for (i = 0; i < n; i++)
  107. DumpFunction(f->p[i], f->source, D);
  108. }
  109. static void DumpUpvalues (const Proto *f, DumpState *D) {
  110. int i, n = f->sizeupvalues;
  111. DumpInt(n, D);
  112. for (i = 0; i < n; i++) {
  113. DumpByte(f->upvalues[i].instack, D);
  114. DumpByte(f->upvalues[i].idx, D);
  115. DumpByte(f->upvalues[i].kind, D);
  116. }
  117. }
  118. static void DumpDebug (const Proto *f, DumpState *D) {
  119. int i, n;
  120. n = (D->strip) ? 0 : f->sizelineinfo;
  121. DumpInt(n, D);
  122. DumpVector(f->lineinfo, n, D);
  123. n = (D->strip) ? 0 : f->sizeabslineinfo;
  124. DumpInt(n, D);
  125. for (i = 0; i < n; i++) {
  126. DumpInt(f->abslineinfo[i].pc, D);
  127. DumpInt(f->abslineinfo[i].line, D);
  128. }
  129. n = (D->strip) ? 0 : f->sizelocvars;
  130. DumpInt(n, D);
  131. for (i = 0; i < n; i++) {
  132. DumpString(f->locvars[i].varname, D);
  133. DumpInt(f->locvars[i].startpc, D);
  134. DumpInt(f->locvars[i].endpc, D);
  135. }
  136. n = (D->strip) ? 0 : f->sizeupvalues;
  137. DumpInt(n, D);
  138. for (i = 0; i < n; i++)
  139. DumpString(f->upvalues[i].name, D);
  140. }
  141. static void DumpFunction (const Proto *f, TString *psource, DumpState *D) {
  142. if (D->strip || f->source == psource)
  143. DumpString(NULL, D); /* no debug info or same source as its parent */
  144. else
  145. DumpString(f->source, D);
  146. DumpInt(f->linedefined, D);
  147. DumpInt(f->lastlinedefined, D);
  148. DumpByte(f->numparams, D);
  149. DumpByte(f->is_vararg, D);
  150. DumpByte(f->maxstacksize, D);
  151. DumpCode(f, D);
  152. DumpConstants(f, D);
  153. DumpUpvalues(f, D);
  154. DumpProtos(f, D);
  155. DumpDebug(f, D);
  156. }
  157. static void DumpHeader (DumpState *D) {
  158. DumpLiteral(LUA_SIGNATURE, D);
  159. DumpInt(LUAC_VERSION, D);
  160. DumpByte(LUAC_FORMAT, D);
  161. DumpLiteral(LUAC_DATA, D);
  162. DumpByte(sizeof(Instruction), D);
  163. DumpByte(sizeof(lua_Integer), D);
  164. DumpByte(sizeof(lua_Number), D);
  165. DumpInteger(LUAC_INT, D);
  166. DumpNumber(LUAC_NUM, D);
  167. }
  168. /*
  169. ** dump Lua function as precompiled chunk
  170. */
  171. int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data,
  172. int strip) {
  173. DumpState D;
  174. D.L = L;
  175. D.writer = w;
  176. D.data = data;
  177. D.strip = strip;
  178. D.status = 0;
  179. DumpHeader(&D);
  180. DumpByte(f->sizeupvalues, &D);
  181. DumpFunction(f, NULL, &D);
  182. return D.status;
  183. }