table.c 5.8 KB

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