table.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. ** table.c
  3. ** Module to control static tables
  4. */
  5. char *rcs_table="$Id: table.c,v 2.60 1997/03/11 18:44:28 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_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)(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 (ttype(o) == LUA_T_STRING && !tsvalue(o)->marked)
  107. tsvalue(o)->marked = 1;
  108. else if (ttype(o) == LUA_T_ARRAY)
  109. lua_hashmark (avalue(o));
  110. else if ((o->ttype == LUA_T_FUNCTION || o->ttype == 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->ttype)
  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_hashcallIM();
  144. luaI_strcallIM();
  145. luaI_invalidaterefs();
  146. recovered += lua_strcollector();
  147. recovered += lua_hashcollector();
  148. recovered += luaI_funccollector();
  149. return recovered;
  150. }
  151. void lua_pack (void)
  152. {
  153. static unsigned long block = GARBAGE_BLOCK;
  154. static unsigned long nentity = 0; /* total of strings, arrays, etc */
  155. unsigned long recovered = 0;
  156. if (nentity++ < block) return;
  157. recovered = luaI_collectgarbage();
  158. block = 2*(block-recovered);
  159. nentity -= recovered;
  160. }
  161. /*
  162. ** Internal function: return next global variable
  163. */
  164. void luaI_nextvar (void)
  165. {
  166. Word next;
  167. lua_Object o = lua_getparam(1);
  168. if (o == LUA_NOOBJECT)
  169. lua_error("too few arguments to function `nextvar'");
  170. if (lua_getparam(2) != LUA_NOOBJECT)
  171. lua_error("too many arguments to function `nextvar'");
  172. if (lua_isnil(o))
  173. next = 0;
  174. else if (!lua_isstring(o))
  175. {
  176. lua_error("incorrect argument to function `nextvar'");
  177. return; /* to avoid warnings */
  178. }
  179. else
  180. next = luaI_findsymbolbyname(lua_getstring(o)) + 1;
  181. while (next < lua_ntable && s_ttype(next) == LUA_T_NIL) next++;
  182. if (next < lua_ntable)
  183. {
  184. lua_pushstring(lua_table[next].varname->str);
  185. luaI_pushobject(&s_object(next));
  186. }
  187. }
  188. static Object *functofind;
  189. static int checkfunc (Object *o)
  190. {
  191. if (o->ttype == LUA_T_FUNCTION)
  192. return
  193. ((functofind->ttype == LUA_T_FUNCTION || functofind->ttype == LUA_T_MARK)
  194. && (functofind->value.tf == o->value.tf));
  195. if (o->ttype == LUA_T_CFUNCTION)
  196. return
  197. ((functofind->ttype == LUA_T_CFUNCTION ||
  198. functofind->ttype == LUA_T_CMARK) &&
  199. (functofind->value.f == o->value.f));
  200. return 0;
  201. }
  202. char *lua_getobjname (lua_Object o, char **name)
  203. { /* try to find a name for given function */
  204. functofind = luaI_Address(o);
  205. if ((*name = luaI_travfallbacks(checkfunc)) != NULL)
  206. return "fallback";
  207. else if ((*name = lua_travsymbol(checkfunc)) != NULL)
  208. return "global";
  209. else return "";
  210. }