ldump.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 <limits.h>
  10. #include <stddef.h>
  11. #include "lua.h"
  12. #include "lobject.h"
  13. #include "lstate.h"
  14. #include "lundump.h"
  15. typedef struct {
  16. lua_State *L;
  17. lua_Writer writer;
  18. void *data;
  19. int strip;
  20. int status;
  21. } DumpState;
  22. /*
  23. ** All high-level dumps go through dumpVector; you can change it to
  24. ** change the endianness of the result
  25. */
  26. #define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0]))
  27. #define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char))
  28. static void dumpBlock (DumpState *D, const void *b, size_t size) {
  29. if (D->status == 0 && size > 0) {
  30. lua_unlock(D->L);
  31. D->status = (*D->writer)(D->L, b, size, D->data);
  32. lua_lock(D->L);
  33. }
  34. }
  35. #define dumpVar(D,x) dumpVector(D,&x,1)
  36. static void dumpByte (DumpState *D, int y) {
  37. lu_byte x = (lu_byte)y;
  38. dumpVar(D, x);
  39. }
  40. /*
  41. ** 'dumpSize' buffer size: each byte can store up to 7 bits. (The "+6"
  42. ** rounds up the division.)
  43. */
  44. #define DIBS ((sizeof(size_t) * CHAR_BIT + 6) / 7)
  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. static void dumpString (DumpState *D, const TString *s) {
  65. if (s == NULL)
  66. dumpSize(D, 0);
  67. else {
  68. size_t size = tsslen(s);
  69. const char *str = getstr(s);
  70. dumpSize(D, size + 1);
  71. dumpVector(D, str, size);
  72. }
  73. }
  74. static void dumpCode (DumpState *D, const Proto *f) {
  75. dumpInt(D, f->sizecode);
  76. dumpVector(D, f->code, f->sizecode);
  77. }
  78. static void dumpFunction(DumpState *D, const Proto *f, TString *psource);
  79. static void dumpConstants (DumpState *D, const Proto *f) {
  80. int i;
  81. int n = f->sizek;
  82. dumpInt(D, n);
  83. for (i = 0; i < n; i++) {
  84. const TValue *o = &f->k[i];
  85. int tt = ttypetag(o);
  86. dumpByte(D, tt);
  87. switch (tt) {
  88. case LUA_VNUMFLT:
  89. dumpNumber(D, fltvalue(o));
  90. break;
  91. case LUA_VNUMINT:
  92. dumpInteger(D, ivalue(o));
  93. break;
  94. case LUA_VSHRSTR:
  95. case LUA_VLNGSTR:
  96. dumpString(D, tsvalue(o));
  97. break;
  98. default:
  99. lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE);
  100. }
  101. }
  102. }
  103. static void dumpProtos (DumpState *D, const Proto *f) {
  104. int i;
  105. int n = f->sizep;
  106. dumpInt(D, n);
  107. for (i = 0; i < n; i++)
  108. dumpFunction(D, f->p[i], f->source);
  109. }
  110. static void dumpUpvalues (DumpState *D, const Proto *f) {
  111. int i, n = f->sizeupvalues;
  112. dumpInt(D, n);
  113. for (i = 0; i < n; i++) {
  114. dumpByte(D, f->upvalues[i].instack);
  115. dumpByte(D, f->upvalues[i].idx);
  116. dumpByte(D, f->upvalues[i].kind);
  117. }
  118. }
  119. static void dumpDebug (DumpState *D, const Proto *f) {
  120. int i, n;
  121. n = (D->strip) ? 0 : f->sizelineinfo;
  122. dumpInt(D, n);
  123. dumpVector(D, f->lineinfo, n);
  124. n = (D->strip) ? 0 : f->sizeabslineinfo;
  125. dumpInt(D, n);
  126. for (i = 0; i < n; i++) {
  127. dumpInt(D, f->abslineinfo[i].pc);
  128. dumpInt(D, f->abslineinfo[i].line);
  129. }
  130. n = (D->strip) ? 0 : f->sizelocvars;
  131. dumpInt(D, n);
  132. for (i = 0; i < n; i++) {
  133. dumpString(D, f->locvars[i].varname);
  134. dumpInt(D, f->locvars[i].startpc);
  135. dumpInt(D, f->locvars[i].endpc);
  136. }
  137. n = (D->strip) ? 0 : f->sizeupvalues;
  138. dumpInt(D, n);
  139. for (i = 0; i < n; i++)
  140. dumpString(D, f->upvalues[i].name);
  141. }
  142. static void dumpFunction (DumpState *D, const Proto *f, TString *psource) {
  143. if (D->strip || f->source == psource)
  144. dumpString(D, NULL); /* no debug info or same source as its parent */
  145. else
  146. dumpString(D, f->source);
  147. dumpInt(D, f->linedefined);
  148. dumpInt(D, f->lastlinedefined);
  149. dumpByte(D, f->numparams);
  150. dumpByte(D, f->is_vararg);
  151. dumpByte(D, f->maxstacksize);
  152. dumpCode(D, f);
  153. dumpConstants(D, f);
  154. dumpUpvalues(D, f);
  155. dumpProtos(D, f);
  156. dumpDebug(D, f);
  157. }
  158. static void dumpHeader (DumpState *D) {
  159. dumpLiteral(D, LUA_SIGNATURE);
  160. dumpByte(D, LUAC_VERSION);
  161. dumpByte(D, LUAC_FORMAT);
  162. dumpLiteral(D, LUAC_DATA);
  163. dumpByte(D, sizeof(Instruction));
  164. dumpByte(D, sizeof(lua_Integer));
  165. dumpByte(D, sizeof(lua_Number));
  166. dumpInteger(D, LUAC_INT);
  167. dumpNumber(D, LUAC_NUM);
  168. }
  169. /*
  170. ** dump Lua function as precompiled chunk
  171. */
  172. int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data,
  173. int strip) {
  174. DumpState D;
  175. D.L = L;
  176. D.writer = w;
  177. D.data = data;
  178. D.strip = strip;
  179. D.status = 0;
  180. dumpHeader(&D);
  181. dumpByte(&D, f->sizeupvalues);
  182. dumpFunction(&D, f, NULL);
  183. return D.status;
  184. }