ldump.c 4.3 KB

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