2
0

ldump.c 4.3 KB

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