table.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. ** table.c
  3. ** Module to control static tables
  4. */
  5. char *rcs_table="$Id: table.c,v 2.58 1996/11/01 12:47:45 roberto Exp roberto $";
  6. #include "mem.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_tag(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)(Object *))
  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 (Object *o)
  105. {/* if already marked, does not change mark value */
  106. if (tag(o) == LUA_T_STRING && !tsvalue(o)->marked)
  107. tsvalue(o)->marked = 1;
  108. else if (tag(o) == LUA_T_ARRAY)
  109. lua_hashmark (avalue(o));
  110. else if ((o->tag == LUA_T_FUNCTION || o->tag == LUA_T_MARK)
  111. && !o->value.tf->marked)
  112. o->value.tf->marked = 1;
  113. return 0;
  114. }
  115. /*
  116. * returns 0 if the object is going to be (garbage) collected
  117. */
  118. int luaI_ismarked (Object *o)
  119. {
  120. switch (o->tag)
  121. {
  122. case LUA_T_STRING:
  123. return o->value.ts->marked;
  124. case LUA_T_FUNCTION:
  125. return o->value.tf->marked;
  126. case LUA_T_ARRAY:
  127. return o->value.a->mark;
  128. default: /* nil, number, cfunction, or user data */
  129. return 1;
  130. }
  131. }
  132. /*
  133. ** Garbage collection.
  134. ** Delete all unused strings and arrays.
  135. */
  136. Long luaI_collectgarbage (void)
  137. {
  138. Long recovered = 0;
  139. lua_travstack(lua_markobject); /* mark stack objects */
  140. lua_travsymbol(lua_markobject); /* mark symbol table objects */
  141. luaI_travlock(lua_markobject); /* mark locked objects */
  142. luaI_travfallbacks(lua_markobject); /* mark fallbacks */
  143. luaI_invalidaterefs();
  144. recovered += lua_strcollector();
  145. recovered += lua_hashcollector();
  146. recovered += luaI_funccollector();
  147. return recovered;
  148. }
  149. void lua_pack (void)
  150. {
  151. static unsigned long block = GARBAGE_BLOCK;
  152. static unsigned long nentity = 0; /* total of strings, arrays, etc */
  153. unsigned long recovered = 0;
  154. if (nentity++ < block) return;
  155. recovered = luaI_collectgarbage();
  156. block = 2*(block-recovered);
  157. nentity -= recovered;
  158. }
  159. /*
  160. ** Internal function: return next global variable
  161. */
  162. void luaI_nextvar (void)
  163. {
  164. Word next;
  165. lua_Object o = lua_getparam(1);
  166. if (o == LUA_NOOBJECT)
  167. lua_error("too few arguments to function `nextvar'");
  168. if (lua_getparam(2) != LUA_NOOBJECT)
  169. lua_error("too many arguments to function `nextvar'");
  170. if (lua_isnil(o))
  171. next = 0;
  172. else if (!lua_isstring(o))
  173. {
  174. lua_error("incorrect argument to function `nextvar'");
  175. return; /* to avoid warnings */
  176. }
  177. else
  178. next = luaI_findsymbolbyname(lua_getstring(o)) + 1;
  179. while (next < lua_ntable && s_tag(next) == LUA_T_NIL) next++;
  180. if (next < lua_ntable)
  181. {
  182. lua_pushstring(lua_table[next].varname->str);
  183. luaI_pushobject(&s_object(next));
  184. }
  185. }
  186. static Object *functofind;
  187. static int checkfunc (Object *o)
  188. {
  189. if (o->tag == LUA_T_FUNCTION)
  190. return
  191. ((functofind->tag == LUA_T_FUNCTION || functofind->tag == LUA_T_MARK)
  192. && (functofind->value.tf == o->value.tf));
  193. if (o->tag == LUA_T_CFUNCTION)
  194. return
  195. ((functofind->tag == LUA_T_CFUNCTION || functofind->tag == LUA_T_CMARK)
  196. && (functofind->value.f == o->value.f));
  197. return 0;
  198. }
  199. char *lua_getobjname (lua_Object o, char **name)
  200. { /* try to find a name for given function */
  201. functofind = luaI_Address(o);
  202. if ((*name = luaI_travfallbacks(checkfunc)) != NULL)
  203. return "fallback";
  204. else if ((*name = lua_travsymbol(checkfunc)) != NULL)
  205. return "global";
  206. else return "";
  207. }