table.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. ** table.c
  3. ** Module to control static tables
  4. */
  5. char *rcs_table="$Id: table.c,v 2.56 1996/06/18 21:12:23 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. {"call", luaI_call},
  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 unsigned long block = GARBAGE_BLOCK;
  181. static unsigned long nentity = 0; /* total of strings, arrays, etc */
  182. unsigned long recovered = 0;
  183. if (nentity++ < block) return;
  184. recovered = luaI_collectgarbage();
  185. block = 2*(block-recovered);
  186. nentity -= recovered;
  187. }
  188. /*
  189. ** Internal function: return next global variable
  190. */
  191. static void lua_nextvar (void)
  192. {
  193. Word next;
  194. lua_Object o = lua_getparam(1);
  195. if (o == LUA_NOOBJECT)
  196. lua_error("too few arguments to function `nextvar'");
  197. if (lua_getparam(2) != LUA_NOOBJECT)
  198. lua_error("too many arguments to function `nextvar'");
  199. if (lua_isnil(o))
  200. next = 0;
  201. else if (!lua_isstring(o))
  202. {
  203. lua_error("incorrect argument to function `nextvar'");
  204. return; /* to avoid warnings */
  205. }
  206. else
  207. next = luaI_findsymbolbyname(lua_getstring(o)) + 1;
  208. while (next < lua_ntable && s_tag(next) == LUA_T_NIL) next++;
  209. if (next < lua_ntable)
  210. {
  211. lua_pushstring(lua_table[next].varname->str);
  212. luaI_pushobject(&s_object(next));
  213. }
  214. }
  215. static Object *functofind;
  216. static int checkfunc (Object *o)
  217. {
  218. if (o->tag == LUA_T_FUNCTION)
  219. return
  220. ((functofind->tag == LUA_T_FUNCTION || functofind->tag == LUA_T_MARK)
  221. && (functofind->value.tf == o->value.tf));
  222. if (o->tag == LUA_T_CFUNCTION)
  223. return
  224. ((functofind->tag == LUA_T_CFUNCTION || functofind->tag == LUA_T_CMARK)
  225. && (functofind->value.f == o->value.f));
  226. return 0;
  227. }
  228. char *lua_getobjname (lua_Object o, char **name)
  229. { /* try to find a name for given function */
  230. functofind = luaI_Address(o);
  231. if ((*name = luaI_travfallbacks(checkfunc)) != NULL)
  232. return "fallback";
  233. else if ((*name = lua_travsymbol(checkfunc)) != NULL)
  234. return "global";
  235. else return "";
  236. }