2
0

table.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. ** table.c
  3. ** Module to control static tables
  4. */
  5. char *rcs_table="$Id: table.c,v 2.51 1996/03/21 18:54:29 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 1024
  23. #define MIN_GARBAGE_BLOCK (GARBAGE_BLOCK/2)
  24. static void lua_nextvar (void);
  25. /*
  26. ** Internal functions
  27. */
  28. static struct {
  29. char *name;
  30. lua_CFunction func;
  31. } int_funcs[] = {
  32. {"assert", luaI_assert},
  33. {"dofile", lua_internaldofile},
  34. {"dostring", lua_internaldostring},
  35. {"error", luaI_error},
  36. {"getglobal", luaI_getglobal},
  37. {"next", lua_next},
  38. {"nextvar", lua_nextvar},
  39. {"print", luaI_print},
  40. {"setfallback", luaI_setfallback},
  41. {"setglobal", luaI_setglobal},
  42. {"tonumber", lua_obj2number},
  43. {"tostring", luaI_tostring},
  44. {"type", luaI_type}
  45. };
  46. #define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
  47. void luaI_initsymbol (void)
  48. {
  49. int i;
  50. lua_maxsymbol = BUFFER_BLOCK;
  51. lua_table = newvector(lua_maxsymbol, Symbol);
  52. for (i=0; i<INTFUNCSIZE; i++)
  53. {
  54. Word n = luaI_findsymbolbyname(int_funcs[i].name);
  55. s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func;
  56. }
  57. }
  58. /*
  59. ** Initialise constant table with pre-defined constants
  60. */
  61. void luaI_initconstant (void)
  62. {
  63. lua_maxconstant = BUFFER_BLOCK;
  64. lua_constant = newvector(lua_maxconstant, TaggedString *);
  65. /* pre-register mem error messages, to avoid loop when error arises */
  66. luaI_findconstantbyname(tableEM);
  67. luaI_findconstantbyname(memEM);
  68. }
  69. /*
  70. ** Given a name, search it at symbol table and return its index. If not
  71. ** found, allocate it.
  72. */
  73. Word luaI_findsymbol (TaggedString *t)
  74. {
  75. if (t->varindex == NOT_USED)
  76. {
  77. if (lua_ntable == lua_maxsymbol)
  78. lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol,
  79. symbolEM, MAX_WORD);
  80. t->varindex = lua_ntable;
  81. lua_table[lua_ntable].varname = t;
  82. s_tag(lua_ntable) = LUA_T_NIL;
  83. lua_ntable++;
  84. }
  85. return t->varindex;
  86. }
  87. Word luaI_findsymbolbyname (char *name)
  88. {
  89. return luaI_findsymbol(luaI_createfixedstring(name));
  90. }
  91. /*
  92. ** Given a tree node, check it is has a correspondent constant index. If not,
  93. ** allocate it.
  94. */
  95. Word luaI_findconstant (TaggedString *t)
  96. {
  97. if (t->constindex == NOT_USED)
  98. {
  99. if (lua_nconstant == lua_maxconstant)
  100. lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *,
  101. constantEM, MAX_WORD);
  102. t->constindex = lua_nconstant;
  103. lua_constant[lua_nconstant] = t;
  104. lua_nconstant++;
  105. }
  106. return t->constindex;
  107. }
  108. Word luaI_findconstantbyname (char *name)
  109. {
  110. return luaI_findconstant(luaI_createfixedstring(name));
  111. }
  112. TaggedString *luaI_createfixedstring (char *name)
  113. {
  114. TaggedString *ts = lua_createstring(name);
  115. if (!ts->marked)
  116. ts->marked = 2; /* avoid GC */
  117. return ts;
  118. }
  119. /*
  120. ** Traverse symbol table objects
  121. */
  122. static char *lua_travsymbol (int (*fn)(Object *))
  123. {
  124. Word i;
  125. for (i=0; i<lua_ntable; i++)
  126. if (fn(&s_object(i)))
  127. return lua_table[i].varname->str;
  128. return NULL;
  129. }
  130. /*
  131. ** Mark an object if it is a string or a unmarked array.
  132. */
  133. int lua_markobject (Object *o)
  134. {/* if already marked, does not change mark value */
  135. if (tag(o) == LUA_T_STRING && !tsvalue(o)->marked)
  136. tsvalue(o)->marked = 1;
  137. else if (tag(o) == LUA_T_ARRAY)
  138. lua_hashmark (avalue(o));
  139. else if ((o->tag == LUA_T_FUNCTION || o->tag == LUA_T_MARK)
  140. && !o->value.tf->marked)
  141. o->value.tf->marked = 1;
  142. return 0;
  143. }
  144. /*
  145. * returns 0 if the object is going to be (garbage) collected
  146. */
  147. int luaI_ismarked (Object *o)
  148. {
  149. switch (o->tag)
  150. {
  151. case LUA_T_STRING:
  152. return o->value.ts->marked;
  153. case LUA_T_FUNCTION:
  154. return o->value.tf->marked;
  155. case LUA_T_ARRAY:
  156. return o->value.a->mark;
  157. default: /* nil, number, cfunction, or user data */
  158. return 1;
  159. }
  160. }
  161. /*
  162. ** Garbage collection.
  163. ** Delete all unused strings and arrays.
  164. */
  165. Long luaI_collectgarbage (void)
  166. {
  167. Long recovered = 0;
  168. lua_travstack(lua_markobject); /* mark stack objects */
  169. lua_travsymbol(lua_markobject); /* mark symbol table objects */
  170. luaI_travlock(lua_markobject); /* mark locked objects */
  171. luaI_travfallbacks(lua_markobject); /* mark fallbacks */
  172. luaI_invalidaterefs();
  173. recovered += lua_strcollector();
  174. recovered += lua_hashcollector();
  175. recovered += luaI_funccollector();
  176. return recovered;
  177. }
  178. void lua_pack (void)
  179. {
  180. static Long block = GARBAGE_BLOCK; /* when garbage collector will be called */
  181. static Long nentity = 0; /* counter of new entities (strings and arrays) */
  182. Long recovered = 0;
  183. if (nentity++ < block) return;
  184. recovered = luaI_collectgarbage();
  185. nentity = 0; /* reset counter */
  186. block=(16*block-7*recovered)/12; /* adapt block size */
  187. if (block < MIN_GARBAGE_BLOCK) block = MIN_GARBAGE_BLOCK;
  188. }
  189. /*
  190. ** Internal function: return next global variable
  191. */
  192. static void lua_nextvar (void)
  193. {
  194. Word next;
  195. lua_Object o = lua_getparam(1);
  196. if (o == LUA_NOOBJECT)
  197. lua_error("too few arguments to function `nextvar'");
  198. if (lua_getparam(2) != LUA_NOOBJECT)
  199. lua_error("too many arguments to function `nextvar'");
  200. if (lua_isnil(o))
  201. next = 0;
  202. else if (!lua_isstring(o))
  203. {
  204. lua_error("incorrect argument to function `nextvar'");
  205. return; /* to avoid warnings */
  206. }
  207. else
  208. next = luaI_findsymbolbyname(lua_getstring(o)) + 1;
  209. while (next < lua_ntable && s_tag(next) == LUA_T_NIL) next++;
  210. if (next >= lua_ntable)
  211. {
  212. lua_pushnil();
  213. lua_pushnil();
  214. }
  215. else
  216. {
  217. TaggedString *t = lua_table[next].varname;
  218. Object name;
  219. tag(&name) = LUA_T_STRING;
  220. tsvalue(&name) = t;
  221. luaI_pushobject(&name);
  222. luaI_pushobject(&s_object(next));
  223. }
  224. }
  225. static Object *functofind;
  226. static int checkfunc (Object *o)
  227. {
  228. if (o->tag == LUA_T_FUNCTION)
  229. return
  230. ((functofind->tag == LUA_T_FUNCTION || functofind->tag == LUA_T_MARK)
  231. && (functofind->value.tf == o->value.tf));
  232. if (o->tag == LUA_T_CFUNCTION)
  233. return
  234. ((functofind->tag == LUA_T_CFUNCTION || functofind->tag == LUA_T_CMARK)
  235. && (functofind->value.f == o->value.f));
  236. return 0;
  237. }
  238. char *lua_getobjname (lua_Object o, char **name)
  239. { /* try to find a name for given function */
  240. functofind = luaI_Address(o);
  241. if ((*name = luaI_travfallbacks(checkfunc)) != NULL)
  242. return "fallback";
  243. else if ((*name = lua_travsymbol(checkfunc)) != NULL)
  244. return "global";
  245. else return "";
  246. }