table.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. ** table.c
  3. ** Module to control static tables
  4. */
  5. char *rcs_table="$Id: table.c,v 2.67 1997/04/06 14:08:08 roberto Exp roberto $";
  6. #include "luamem.h"
  7. #include "auxlib.h"
  8. #include "opcode.h"
  9. #include "tree.h"
  10. #include "hash.h"
  11. #include "table.h"
  12. #include "inout.h"
  13. #include "lua.h"
  14. #include "fallback.h"
  15. #include "luadebug.h"
  16. #define BUFFER_BLOCK 256
  17. Symbol *lua_table = NULL;
  18. Word lua_ntable = 0;
  19. static Long lua_maxsymbol = 0;
  20. TaggedString **lua_constant = NULL;
  21. Word lua_nconstant = 0;
  22. static Long lua_maxconstant = 0;
  23. #define GARBAGE_BLOCK 50
  24. void luaI_initsymbol (void)
  25. {
  26. lua_maxsymbol = BUFFER_BLOCK;
  27. lua_table = newvector(lua_maxsymbol, Symbol);
  28. luaI_predefine();
  29. }
  30. /*
  31. ** Initialise constant table with pre-defined constants
  32. */
  33. void luaI_initconstant (void)
  34. {
  35. lua_maxconstant = BUFFER_BLOCK;
  36. lua_constant = newvector(lua_maxconstant, TaggedString *);
  37. /* pre-register mem error messages, to avoid loop when error arises */
  38. luaI_findconstantbyname(tableEM);
  39. luaI_findconstantbyname(memEM);
  40. }
  41. /*
  42. ** Given a name, search it at symbol table and return its index. If not
  43. ** found, allocate it.
  44. */
  45. Word luaI_findsymbol (TaggedString *t)
  46. {
  47. if (t->varindex == NOT_USED)
  48. {
  49. if (lua_ntable == lua_maxsymbol)
  50. lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol,
  51. symbolEM, MAX_WORD);
  52. t->varindex = lua_ntable;
  53. lua_table[lua_ntable].varname = t;
  54. s_ttype(lua_ntable) = LUA_T_NIL;
  55. lua_ntable++;
  56. }
  57. return t->varindex;
  58. }
  59. Word luaI_findsymbolbyname (char *name)
  60. {
  61. return luaI_findsymbol(luaI_createfixedstring(name));
  62. }
  63. /*
  64. ** Given a tree node, check it is has a correspondent constant index. If not,
  65. ** allocate it.
  66. */
  67. Word luaI_findconstant (TaggedString *t)
  68. {
  69. if (t->constindex == NOT_USED)
  70. {
  71. if (lua_nconstant == lua_maxconstant)
  72. lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *,
  73. constantEM, MAX_WORD);
  74. t->constindex = lua_nconstant;
  75. lua_constant[lua_nconstant] = t;
  76. lua_nconstant++;
  77. }
  78. return t->constindex;
  79. }
  80. Word luaI_findconstantbyname (char *name)
  81. {
  82. return luaI_findconstant(luaI_createfixedstring(name));
  83. }
  84. TaggedString *luaI_createfixedstring (char *name)
  85. {
  86. TaggedString *ts = lua_createstring(name);
  87. if (!ts->marked)
  88. ts->marked = 2; /* avoid GC */
  89. return ts;
  90. }
  91. int luaI_globaldefined (char *name)
  92. {
  93. return ttype(&lua_table[luaI_findsymbolbyname(name)].object) != LUA_T_NIL;
  94. }
  95. /*
  96. ** Traverse symbol table objects
  97. */
  98. static char *lua_travsymbol (int (*fn)(TObject *))
  99. {
  100. Word i;
  101. for (i=0; i<lua_ntable; i++)
  102. if (fn(&s_object(i)))
  103. return lua_table[i].varname->str;
  104. return NULL;
  105. }
  106. /*
  107. ** Mark an object if it is a string or a unmarked array.
  108. */
  109. int lua_markobject (TObject *o)
  110. {/* if already marked, does not change mark value */
  111. if (ttype(o) == LUA_T_USERDATA ||
  112. (ttype(o) == LUA_T_STRING && !tsvalue(o)->marked))
  113. tsvalue(o)->marked = 1;
  114. else if (ttype(o) == LUA_T_ARRAY)
  115. lua_hashmark (avalue(o));
  116. else if ((o->ttype == LUA_T_FUNCTION || o->ttype == LUA_T_MARK)
  117. && !o->value.tf->marked)
  118. o->value.tf->marked = 1;
  119. return 0;
  120. }
  121. /*
  122. * returns 0 if the object is going to be (garbage) collected
  123. */
  124. int luaI_ismarked (TObject *o)
  125. {
  126. switch (o->ttype)
  127. {
  128. case LUA_T_STRING:
  129. return o->value.ts->marked;
  130. case LUA_T_FUNCTION:
  131. return o->value.tf->marked;
  132. case LUA_T_ARRAY:
  133. return o->value.a->mark;
  134. default: /* nil, number, cfunction, or user data */
  135. return 1;
  136. }
  137. }
  138. static void call_nilIM (void)
  139. { /* signals end of garbage collection */
  140. TObject t;
  141. ttype(&t) = LUA_T_NIL;
  142. luaI_gcIM(&t); /* end of list */
  143. }
  144. /*
  145. ** Garbage collection.
  146. ** Delete all unused strings and arrays.
  147. */
  148. Long luaI_collectgarbage (void)
  149. {
  150. Long recovered = 0;
  151. lua_travstack(lua_markobject); /* mark stack objects */
  152. lua_travsymbol(lua_markobject); /* mark symbol table objects */
  153. luaI_travlock(lua_markobject); /* mark locked objects */
  154. luaI_travfallbacks(lua_markobject); /* mark fallbacks */
  155. luaI_hashcallIM();
  156. luaI_strcallIM();
  157. call_nilIM();
  158. luaI_invalidaterefs();
  159. recovered += lua_strcollector();
  160. recovered += lua_hashcollector();
  161. recovered += luaI_funccollector();
  162. return recovered;
  163. }
  164. void lua_pack (void)
  165. {
  166. static unsigned long block = GARBAGE_BLOCK;
  167. static unsigned long nentity = 0; /* total of strings, arrays, etc */
  168. unsigned long recovered = 0;
  169. if (nentity++ < block) return;
  170. recovered = luaI_collectgarbage();
  171. block = 2*(block-recovered);
  172. nentity -= recovered;
  173. }
  174. /*
  175. ** Internal function: return next global variable
  176. */
  177. void luaI_nextvar (void)
  178. {
  179. Word next;
  180. if (lua_isnil(lua_getparam(1)))
  181. next = 0;
  182. else
  183. next = luaI_findsymbolbyname(luaL_check_string(1)) + 1;
  184. while (next < lua_ntable && s_ttype(next) == LUA_T_NIL)
  185. next++;
  186. if (next < lua_ntable) {
  187. lua_pushstring(lua_table[next].varname->str);
  188. luaI_pushobject(&s_object(next));
  189. }
  190. }
  191. static TObject *functofind;
  192. static int checkfunc (TObject *o)
  193. {
  194. if (o->ttype == LUA_T_FUNCTION)
  195. return
  196. ((functofind->ttype == LUA_T_FUNCTION || functofind->ttype == LUA_T_MARK)
  197. && (functofind->value.tf == o->value.tf));
  198. if (o->ttype == LUA_T_CFUNCTION)
  199. return
  200. ((functofind->ttype == LUA_T_CFUNCTION ||
  201. functofind->ttype == LUA_T_CMARK) &&
  202. (functofind->value.f == o->value.f));
  203. return 0;
  204. }
  205. char *lua_getobjname (lua_Object o, char **name)
  206. { /* try to find a name for given function */
  207. functofind = luaI_Address(o);
  208. if ((*name = luaI_travfallbacks(checkfunc)) != NULL)
  209. return "fallback";
  210. else if ((*name = lua_travsymbol(checkfunc)) != NULL)
  211. return "global";
  212. else return "";
  213. }