ldump.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. ** $Id: ldump.c,v 2.28 2014/03/27 15:58:05 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->tsv.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, 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(rawtsvalue(o), D);
  88. break;
  89. default:
  90. lua_assert(0);
  91. }
  92. }
  93. n = f->sizep;
  94. DumpInt(n, D);
  95. for (i = 0; i < n; i++)
  96. DumpFunction(f->p[i], D);
  97. }
  98. static void DumpUpvalues (const Proto *f, DumpState *D) {
  99. int i, n = f->sizeupvalues;
  100. DumpInt(n, D);
  101. for (i = 0; i < n; i++) {
  102. DumpByte(f->upvalues[i].instack, D);
  103. DumpByte(f->upvalues[i].idx, D);
  104. }
  105. }
  106. static void DumpDebug (const Proto *f, DumpState *D) {
  107. int i, n;
  108. DumpString((D->strip) ? NULL : f->source, D);
  109. n = (D->strip) ? 0 : f->sizelineinfo;
  110. DumpInt(n, D);
  111. DumpVector(f->lineinfo, n, D);
  112. n = (D->strip) ? 0 : f->sizelocvars;
  113. DumpInt(n, D);
  114. for (i = 0; i < n; i++) {
  115. DumpString(f->locvars[i].varname, D);
  116. DumpInt(f->locvars[i].startpc, D);
  117. DumpInt(f->locvars[i].endpc, D);
  118. }
  119. n = (D->strip) ? 0 : f->sizeupvalues;
  120. DumpInt(n, D);
  121. for (i = 0; i < n; i++)
  122. DumpString(f->upvalues[i].name, D);
  123. }
  124. static void DumpFunction (const Proto *f, DumpState *D) {
  125. DumpInt(f->linedefined, D);
  126. DumpInt(f->lastlinedefined, D);
  127. DumpByte(f->numparams, D);
  128. DumpByte(f->is_vararg, D);
  129. DumpByte(f->maxstacksize, D);
  130. DumpCode(f, D);
  131. DumpConstants(f, D);
  132. DumpUpvalues(f, D);
  133. DumpDebug(f, D);
  134. }
  135. static void DumpHeader (DumpState *D) {
  136. DumpLiteral(LUA_SIGNATURE, D);
  137. DumpByte(LUAC_VERSION, D);
  138. DumpByte(LUAC_FORMAT, D);
  139. DumpLiteral(LUAC_DATA, D);
  140. DumpByte(sizeof(int), D);
  141. DumpByte(sizeof(size_t), D);
  142. DumpByte(sizeof(Instruction), D);
  143. DumpByte(sizeof(lua_Integer), D);
  144. DumpByte(sizeof(lua_Number), D);
  145. DumpInteger(LUAC_INT, D);
  146. DumpNumber(LUAC_NUM, D);
  147. }
  148. /*
  149. ** dump Lua function as precompiled chunk
  150. */
  151. int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data,
  152. int strip) {
  153. DumpState D;
  154. D.L = L;
  155. D.writer = w;
  156. D.data = data;
  157. D.strip = strip;
  158. D.status = 0;
  159. DumpHeader(&D);
  160. DumpByte(f->sizeupvalues, &D);
  161. DumpFunction(f, &D);
  162. return D.status;
  163. }