ldump.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "lgc.h"
  12. #include "lobject.h"
  13. #include "lstate.h"
  14. #include "ltable.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. Table *h; /* table to track saved strings */
  23. lua_Integer nstr; /* counter to number saved strings */
  24. } DumpState;
  25. /*
  26. ** All high-level dumps go through dumpVector; you can change it to
  27. ** change the endianness of the result
  28. */
  29. #define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0]))
  30. #define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char))
  31. static void dumpBlock (DumpState *D, const void *b, size_t size) {
  32. if (D->status == 0 && size > 0) {
  33. lua_unlock(D->L);
  34. D->status = (*D->writer)(D->L, b, size, D->data);
  35. lua_lock(D->L);
  36. }
  37. }
  38. #define dumpVar(D,x) dumpVector(D,&x,1)
  39. static void dumpByte (DumpState *D, int y) {
  40. lu_byte x = (lu_byte)y;
  41. dumpVar(D, x);
  42. }
  43. /* dumpInt Buff Size */
  44. #define DIBS ((sizeof(size_t) * 8 / 7) + 1)
  45. static void dumpSize (DumpState *D, size_t x) {
  46. lu_byte buff[DIBS];
  47. int n = 0;
  48. do {
  49. buff[DIBS - (++n)] = x & 0x7f; /* fill buffer in reverse order */
  50. x >>= 7;
  51. } while (x != 0);
  52. buff[DIBS - 1] |= 0x80; /* mark last byte */
  53. dumpVector(D, buff + DIBS - n, n);
  54. }
  55. static void dumpInt (DumpState *D, int x) {
  56. dumpSize(D, x);
  57. }
  58. static void dumpNumber (DumpState *D, lua_Number x) {
  59. dumpVar(D, x);
  60. }
  61. static void dumpInteger (DumpState *D, lua_Integer x) {
  62. dumpVar(D, x);
  63. }
  64. /*
  65. ** Dump a String. First dump its "size": size==0 means NULL;
  66. ** size==1 is followed by an index and means "reuse saved string with
  67. ** that index"; size>=2 is followed by the string contents with real
  68. ** size==size-2 and means that string, which will be saved with
  69. ** the next available index.
  70. */
  71. static void dumpString (DumpState *D, TString *s) {
  72. if (s == NULL)
  73. dumpSize(D, 0);
  74. else {
  75. const TValue *idx = luaH_getstr(D->h, s);
  76. if (ttisinteger(idx)) { /* string already saved? */
  77. dumpSize(D, 1); /* reuse a saved string */
  78. dumpInt(D, ivalue(idx)); /* index of saved string */
  79. }
  80. else { /* must write and save the string */
  81. TValue key, value; /* to save the string in the hash */
  82. size_t size = tsslen(s);
  83. dumpSize(D, size + 2);
  84. dumpVector(D, getstr(s), size);
  85. D->nstr++; /* one more saved string */
  86. setsvalue(D->L, &key, s); /* the string is the key */
  87. setivalue(&value, D->nstr); /* its index is the value */
  88. luaH_finishset(D->L, D->h, &key, idx, &value); /* h[s] = nstr */
  89. /* integer value does not need barrier */
  90. }
  91. }
  92. }
  93. static void dumpCode (DumpState *D, const Proto *f) {
  94. dumpInt(D, f->sizecode);
  95. dumpVector(D, f->code, f->sizecode);
  96. }
  97. static void dumpFunction(DumpState *D, const Proto *f);
  98. static void dumpConstants (DumpState *D, const Proto *f) {
  99. int i;
  100. int n = f->sizek;
  101. dumpInt(D, n);
  102. for (i = 0; i < n; i++) {
  103. const TValue *o = &f->k[i];
  104. int tt = ttypetag(o);
  105. dumpByte(D, tt);
  106. switch (tt) {
  107. case LUA_VNUMFLT:
  108. dumpNumber(D, fltvalue(o));
  109. break;
  110. case LUA_VNUMINT:
  111. dumpInteger(D, ivalue(o));
  112. break;
  113. case LUA_VSHRSTR:
  114. case LUA_VLNGSTR:
  115. dumpString(D, tsvalue(o));
  116. break;
  117. default:
  118. lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE);
  119. }
  120. }
  121. }
  122. static void dumpProtos (DumpState *D, const Proto *f) {
  123. int i;
  124. int n = f->sizep;
  125. dumpInt(D, n);
  126. for (i = 0; i < n; i++)
  127. dumpFunction(D, f->p[i]);
  128. }
  129. static void dumpUpvalues (DumpState *D, const Proto *f) {
  130. int i, n = f->sizeupvalues;
  131. dumpInt(D, n);
  132. for (i = 0; i < n; i++) {
  133. dumpByte(D, f->upvalues[i].instack);
  134. dumpByte(D, f->upvalues[i].idx);
  135. dumpByte(D, f->upvalues[i].kind);
  136. }
  137. }
  138. static void dumpDebug (DumpState *D, const Proto *f) {
  139. int i, n;
  140. n = (D->strip) ? 0 : f->sizelineinfo;
  141. dumpInt(D, n);
  142. dumpVector(D, f->lineinfo, n);
  143. n = (D->strip) ? 0 : f->sizeabslineinfo;
  144. dumpInt(D, n);
  145. for (i = 0; i < n; i++) {
  146. dumpInt(D, f->abslineinfo[i].pc);
  147. dumpInt(D, f->abslineinfo[i].line);
  148. }
  149. n = (D->strip) ? 0 : f->sizelocvars;
  150. dumpInt(D, n);
  151. for (i = 0; i < n; i++) {
  152. dumpString(D, f->locvars[i].varname);
  153. dumpInt(D, f->locvars[i].startpc);
  154. dumpInt(D, f->locvars[i].endpc);
  155. }
  156. n = (D->strip) ? 0 : f->sizeupvalues;
  157. dumpInt(D, n);
  158. for (i = 0; i < n; i++)
  159. dumpString(D, f->upvalues[i].name);
  160. }
  161. static void dumpFunction (DumpState *D, const Proto *f) {
  162. if (D->strip)
  163. dumpString(D, NULL); /* no debug info */
  164. else
  165. dumpString(D, f->source);
  166. dumpInt(D, f->linedefined);
  167. dumpInt(D, f->lastlinedefined);
  168. dumpByte(D, f->numparams);
  169. dumpByte(D, f->is_vararg);
  170. dumpByte(D, f->maxstacksize);
  171. dumpCode(D, f);
  172. dumpConstants(D, f);
  173. dumpUpvalues(D, f);
  174. dumpProtos(D, f);
  175. dumpDebug(D, f);
  176. }
  177. static void dumpHeader (DumpState *D) {
  178. dumpLiteral(D, LUA_SIGNATURE);
  179. dumpByte(D, LUAC_VERSION);
  180. dumpByte(D, LUAC_FORMAT);
  181. dumpLiteral(D, LUAC_DATA);
  182. dumpByte(D, sizeof(Instruction));
  183. dumpByte(D, sizeof(lua_Integer));
  184. dumpByte(D, sizeof(lua_Number));
  185. dumpInteger(D, LUAC_INT);
  186. dumpNumber(D, LUAC_NUM);
  187. }
  188. /*
  189. ** dump Lua function as precompiled chunk
  190. */
  191. int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data,
  192. int strip, Table *h) {
  193. DumpState D;
  194. D.L = L;
  195. D.writer = w;
  196. D.data = data;
  197. D.strip = strip;
  198. D.status = 0;
  199. D.h = h;
  200. D.nstr = 0;
  201. dumpHeader(&D);
  202. dumpByte(&D, f->sizeupvalues);
  203. dumpFunction(&D, f);
  204. return D.status;
  205. }