table.c 6.2 KB

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