table.c 6.3 KB

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