9
3

ldump.c 4.4 KB

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