ldump.c 3.4 KB

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