2
0

ldump.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. ** $Id: ldump.c,v 2.5 2005/05/05 20:47:02 roberto Exp roberto $
  3. ** save pre-compiled 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 "lopcodes.h"
  12. #include "lstate.h"
  13. #include "lundump.h"
  14. #define DumpVector(b,n,size,D) DumpBlock(b,(n)*(size),D)
  15. #define DumpLiteral(s,D) DumpBlock("" s,(sizeof(s))-1,D)
  16. typedef struct {
  17. lua_State* L;
  18. lua_Writer writer;
  19. void* data;
  20. int strip;
  21. int status;
  22. } DumpState;
  23. static void DumpBlock(const void* b, size_t size, DumpState* D)
  24. {
  25. if (D->status==0)
  26. {
  27. lua_unlock(D->L);
  28. D->status=(*D->writer)(D->L,b,size,D->data);
  29. lua_lock(D->L);
  30. }
  31. }
  32. static void DumpByte(int y, DumpState* D)
  33. {
  34. char x=(char)y;
  35. DumpBlock(&x,sizeof(x),D);
  36. }
  37. static void DumpInt(int x, DumpState* D)
  38. {
  39. DumpBlock(&x,sizeof(x),D);
  40. }
  41. static void DumpSize(size_t x, DumpState* D)
  42. {
  43. DumpBlock(&x,sizeof(x),D);
  44. }
  45. static void DumpNumber(lua_Number x, DumpState* D)
  46. {
  47. DumpBlock(&x,sizeof(x),D);
  48. }
  49. static void DumpString(const TString* s, DumpState* D)
  50. {
  51. if (s==NULL || getstr(s)==NULL)
  52. DumpSize(0,D);
  53. else
  54. {
  55. size_t size=s->tsv.len+1; /* include trailing '\0' */
  56. DumpSize(size,D);
  57. DumpBlock(getstr(s),size,D);
  58. }
  59. }
  60. static void DumpCode(const Proto* f, DumpState* D)
  61. {
  62. DumpInt(f->sizecode,D);
  63. DumpVector(f->code,f->sizecode,sizeof(*f->code),D);
  64. }
  65. static void DumpLocals(const Proto* f, DumpState* D)
  66. {
  67. int i,n=f->sizelocvars;
  68. DumpInt(n,D);
  69. for (i=0; i<n; i++)
  70. {
  71. DumpString(f->locvars[i].varname,D);
  72. DumpInt(f->locvars[i].startpc,D);
  73. DumpInt(f->locvars[i].endpc,D);
  74. }
  75. }
  76. static void DumpLines(const Proto* f, DumpState* D)
  77. {
  78. DumpInt(f->sizelineinfo,D);
  79. DumpVector(f->lineinfo,f->sizelineinfo,sizeof(*f->lineinfo),D);
  80. }
  81. static void DumpUpvalues(const Proto* f, DumpState* D)
  82. {
  83. int i,n=f->sizeupvalues;
  84. DumpInt(n,D);
  85. for (i=0; i<n; i++) DumpString(f->upvalues[i],D);
  86. }
  87. static void DumpFunction(const Proto* f, const TString* p, DumpState* D);
  88. static void DumpConstants(const Proto* f, DumpState* D)
  89. {
  90. int i,n;
  91. DumpInt(n=f->sizek,D);
  92. for (i=0; i<n; i++)
  93. {
  94. const TValue* o=&f->k[i];
  95. DumpByte(ttype(o),D);
  96. switch (ttype(o))
  97. {
  98. case LUA_TNUMBER:
  99. DumpNumber(nvalue(o),D);
  100. break;
  101. case LUA_TSTRING:
  102. DumpString(rawtsvalue(o),D);
  103. break;
  104. case LUA_TNIL:
  105. break;
  106. case LUA_TBOOLEAN:
  107. DumpByte(bvalue(o),D);
  108. break;
  109. default:
  110. lua_assert(0); /* cannot happen */
  111. break;
  112. }
  113. }
  114. DumpInt(n=f->sizep,D);
  115. for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D);
  116. }
  117. static void DumpFunction(const Proto* f, const TString* p, DumpState* D)
  118. {
  119. DumpString((f->source==p) ? NULL : f->source,D);
  120. DumpInt(f->linedefined,D);
  121. DumpInt(f->lastlinedefined,D);
  122. DumpByte(f->nups,D);
  123. DumpByte(f->numparams,D);
  124. DumpByte(f->is_vararg,D);
  125. DumpByte(f->maxstacksize,D);
  126. if (D->strip) DumpInt(0,D); else DumpLines(f,D);
  127. if (D->strip) DumpInt(0,D); else DumpLocals(f,D);
  128. if (D->strip) DumpInt(0,D); else DumpUpvalues(f,D);
  129. DumpConstants(f,D);
  130. DumpCode(f,D);
  131. }
  132. static void DumpHeader(DumpState* D)
  133. {
  134. DumpLiteral(LUA_SIGNATURE,D);
  135. DumpByte(VERSION,D);
  136. DumpByte(luaU_endianness(),D);
  137. DumpByte(sizeof(int),D);
  138. DumpByte(sizeof(size_t),D);
  139. DumpByte(sizeof(Instruction),D);
  140. DumpByte(sizeof(lua_Number),D);
  141. DumpNumber(TEST_NUMBER,D);
  142. }
  143. /*
  144. ** dump Lua function as precompiled chunk
  145. */
  146. int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data,
  147. int strip)
  148. {
  149. DumpState D;
  150. D.L=L;
  151. D.writer=w;
  152. D.data=data;
  153. D.strip=strip;
  154. D.status=0;
  155. DumpHeader(&D);
  156. DumpFunction(f,NULL,&D);
  157. return D.status;
  158. }