ldump.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0]))
  26. #define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char))
  27. static void dumpBlock (DumpState *D, const void *b, size_t size) {
  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(D,x) dumpVector(D,&x,1)
  35. static void dumpByte (DumpState *D, int y) {
  36. lu_byte x = (lu_byte)y;
  37. dumpVar(D, x);
  38. }
  39. /* dumpInt Buff Size */
  40. #define DIBS ((sizeof(size_t) * 8 / 7) + 1)
  41. static void dumpSize (DumpState *D, size_t x) {
  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(D, buff + DIBS - n, n);
  50. }
  51. static void dumpInt (DumpState *D, int x) {
  52. dumpSize(D, x);
  53. }
  54. static void dumpNumber (DumpState *D, lua_Number x) {
  55. dumpVar(D, x);
  56. }
  57. static void dumpInteger (DumpState *D, lua_Integer x) {
  58. dumpVar(D, x);
  59. }
  60. static void dumpString (DumpState *D, const TString *s) {
  61. if (s == NULL)
  62. dumpSize(D, 0);
  63. else {
  64. size_t size = tsslen(s);
  65. const char *str = getstr(s);
  66. dumpSize(D, size + 1);
  67. dumpVector(D, str, size);
  68. }
  69. }
  70. static void dumpCode (DumpState *D, const Proto *f) {
  71. dumpInt(D, f->sizecode);
  72. dumpVector(D, f->code, f->sizecode);
  73. }
  74. static void dumpFunction(DumpState *D, const Proto *f, TString *psource);
  75. static void dumpConstants (DumpState *D, const Proto *f) {
  76. int i;
  77. int n = f->sizek;
  78. dumpInt(D, n);
  79. for (i = 0; i < n; i++) {
  80. const TValue *o = &f->k[i];
  81. int tt = ttypetag(o);
  82. dumpByte(D, tt);
  83. switch (tt) {
  84. case LUA_VNUMFLT:
  85. dumpNumber(D, fltvalue(o));
  86. break;
  87. case LUA_VNUMINT:
  88. dumpInteger(D, ivalue(o));
  89. break;
  90. case LUA_VSHRSTR:
  91. case LUA_VLNGSTR:
  92. dumpString(D, tsvalue(o));
  93. break;
  94. default:
  95. lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE);
  96. }
  97. }
  98. }
  99. static void dumpProtos (DumpState *D, const Proto *f) {
  100. int i;
  101. int n = f->sizep;
  102. dumpInt(D, n);
  103. for (i = 0; i < n; i++)
  104. dumpFunction(D, f->p[i], f->source);
  105. }
  106. static void dumpUpvalues (DumpState *D, const Proto *f) {
  107. int i, n = f->sizeupvalues;
  108. dumpInt(D, n);
  109. for (i = 0; i < n; i++) {
  110. dumpByte(D, f->upvalues[i].instack);
  111. dumpByte(D, f->upvalues[i].idx);
  112. dumpByte(D, f->upvalues[i].kind);
  113. }
  114. }
  115. static void dumpDebug (DumpState *D, const Proto *f) {
  116. int i, n;
  117. n = (D->strip) ? 0 : f->sizelineinfo;
  118. dumpInt(D, n);
  119. dumpVector(D, f->lineinfo, n);
  120. n = (D->strip) ? 0 : f->sizeabslineinfo;
  121. dumpInt(D, n);
  122. for (i = 0; i < n; i++) {
  123. dumpInt(D, f->abslineinfo[i].pc);
  124. dumpInt(D, f->abslineinfo[i].line);
  125. }
  126. n = (D->strip) ? 0 : f->sizelocvars;
  127. dumpInt(D, n);
  128. for (i = 0; i < n; i++) {
  129. dumpString(D, f->locvars[i].varname);
  130. dumpInt(D, f->locvars[i].startpc);
  131. dumpInt(D, f->locvars[i].endpc);
  132. }
  133. n = (D->strip) ? 0 : f->sizeupvalues;
  134. dumpInt(D, n);
  135. for (i = 0; i < n; i++)
  136. dumpString(D, f->upvalues[i].name);
  137. }
  138. static void dumpFunction (DumpState *D, const Proto *f, TString *psource) {
  139. if (D->strip || f->source == psource)
  140. dumpString(D, NULL); /* no debug info or same source as its parent */
  141. else
  142. dumpString(D, f->source);
  143. dumpInt(D, f->linedefined);
  144. dumpInt(D, f->lastlinedefined);
  145. dumpByte(D, f->numparams);
  146. dumpByte(D, f->is_vararg);
  147. dumpByte(D, f->maxstacksize);
  148. dumpCode(D, f);
  149. dumpConstants(D, f);
  150. dumpUpvalues(D, f);
  151. dumpProtos(D, f);
  152. dumpDebug(D, f);
  153. }
  154. static void dumpHeader (DumpState *D) {
  155. dumpLiteral(D, LUA_SIGNATURE);
  156. dumpByte(D, LUAC_VERSION);
  157. dumpByte(D, LUAC_FORMAT);
  158. dumpLiteral(D, LUAC_DATA);
  159. dumpByte(D, sizeof(Instruction));
  160. dumpByte(D, sizeof(lua_Integer));
  161. dumpByte(D, sizeof(lua_Number));
  162. dumpInteger(D, LUAC_INT);
  163. dumpNumber(D, LUAC_NUM);
  164. }
  165. /*
  166. ** dump Lua function as precompiled chunk
  167. */
  168. int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data,
  169. int strip) {
  170. DumpState D;
  171. D.L = L;
  172. D.writer = w;
  173. D.data = data;
  174. D.strip = strip;
  175. D.status = 0;
  176. dumpHeader(&D);
  177. dumpByte(&D, f->sizeupvalues);
  178. dumpFunction(&D, f, NULL);
  179. return D.status;
  180. }