table.c 5.7 KB

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