table.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. ** table.c
  3. ** Module to control static tables
  4. */
  5. char *rcs_table="$Id: table.c,v 2.57 1996/07/12 20:00:26 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. Word n;
  51. lua_maxsymbol = BUFFER_BLOCK;
  52. lua_table = newvector(lua_maxsymbol, Symbol);
  53. for (i=0; i<INTFUNCSIZE; i++)
  54. {
  55. n = luaI_findsymbolbyname(int_funcs[i].name);
  56. s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func;
  57. }
  58. n = luaI_findsymbolbyname("_VERSION_");
  59. s_tag(n) = LUA_T_STRING; s_tsvalue(n) = lua_createstring(LUA_VERSION);
  60. }
  61. /*
  62. ** Initialise constant table with pre-defined constants
  63. */
  64. void luaI_initconstant (void)
  65. {
  66. lua_maxconstant = BUFFER_BLOCK;
  67. lua_constant = newvector(lua_maxconstant, TaggedString *);
  68. /* pre-register mem error messages, to avoid loop when error arises */
  69. luaI_findconstantbyname(tableEM);
  70. luaI_findconstantbyname(memEM);
  71. }
  72. /*
  73. ** Given a name, search it at symbol table and return its index. If not
  74. ** found, allocate it.
  75. */
  76. Word luaI_findsymbol (TaggedString *t)
  77. {
  78. if (t->varindex == NOT_USED)
  79. {
  80. if (lua_ntable == lua_maxsymbol)
  81. lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol,
  82. symbolEM, MAX_WORD);
  83. t->varindex = lua_ntable;
  84. lua_table[lua_ntable].varname = t;
  85. s_tag(lua_ntable) = LUA_T_NIL;
  86. lua_ntable++;
  87. }
  88. return t->varindex;
  89. }
  90. Word luaI_findsymbolbyname (char *name)
  91. {
  92. return luaI_findsymbol(luaI_createfixedstring(name));
  93. }
  94. /*
  95. ** Given a tree node, check it is has a correspondent constant index. If not,
  96. ** allocate it.
  97. */
  98. Word luaI_findconstant (TaggedString *t)
  99. {
  100. if (t->constindex == NOT_USED)
  101. {
  102. if (lua_nconstant == lua_maxconstant)
  103. lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *,
  104. constantEM, MAX_WORD);
  105. t->constindex = lua_nconstant;
  106. lua_constant[lua_nconstant] = t;
  107. lua_nconstant++;
  108. }
  109. return t->constindex;
  110. }
  111. Word luaI_findconstantbyname (char *name)
  112. {
  113. return luaI_findconstant(luaI_createfixedstring(name));
  114. }
  115. TaggedString *luaI_createfixedstring (char *name)
  116. {
  117. TaggedString *ts = lua_createstring(name);
  118. if (!ts->marked)
  119. ts->marked = 2; /* avoid GC */
  120. return ts;
  121. }
  122. /*
  123. ** Traverse symbol table objects
  124. */
  125. static char *lua_travsymbol (int (*fn)(Object *))
  126. {
  127. Word i;
  128. for (i=0; i<lua_ntable; i++)
  129. if (fn(&s_object(i)))
  130. return lua_table[i].varname->str;
  131. return NULL;
  132. }
  133. /*
  134. ** Mark an object if it is a string or a unmarked array.
  135. */
  136. int lua_markobject (Object *o)
  137. {/* if already marked, does not change mark value */
  138. if (tag(o) == LUA_T_STRING && !tsvalue(o)->marked)
  139. tsvalue(o)->marked = 1;
  140. else if (tag(o) == LUA_T_ARRAY)
  141. lua_hashmark (avalue(o));
  142. else if ((o->tag == LUA_T_FUNCTION || o->tag == LUA_T_MARK)
  143. && !o->value.tf->marked)
  144. o->value.tf->marked = 1;
  145. return 0;
  146. }
  147. /*
  148. * returns 0 if the object is going to be (garbage) collected
  149. */
  150. int luaI_ismarked (Object *o)
  151. {
  152. switch (o->tag)
  153. {
  154. case LUA_T_STRING:
  155. return o->value.ts->marked;
  156. case LUA_T_FUNCTION:
  157. return o->value.tf->marked;
  158. case LUA_T_ARRAY:
  159. return o->value.a->mark;
  160. default: /* nil, number, cfunction, or user data */
  161. return 1;
  162. }
  163. }
  164. /*
  165. ** Garbage collection.
  166. ** Delete all unused strings and arrays.
  167. */
  168. Long luaI_collectgarbage (void)
  169. {
  170. Long recovered = 0;
  171. lua_travstack(lua_markobject); /* mark stack objects */
  172. lua_travsymbol(lua_markobject); /* mark symbol table objects */
  173. luaI_travlock(lua_markobject); /* mark locked objects */
  174. luaI_travfallbacks(lua_markobject); /* mark fallbacks */
  175. luaI_invalidaterefs();
  176. recovered += lua_strcollector();
  177. recovered += lua_hashcollector();
  178. recovered += luaI_funccollector();
  179. return recovered;
  180. }
  181. void lua_pack (void)
  182. {
  183. static unsigned long block = GARBAGE_BLOCK;
  184. static unsigned long nentity = 0; /* total of strings, arrays, etc */
  185. unsigned long recovered = 0;
  186. if (nentity++ < block) return;
  187. recovered = luaI_collectgarbage();
  188. block = 2*(block-recovered);
  189. nentity -= recovered;
  190. }
  191. /*
  192. ** Internal function: return next global variable
  193. */
  194. static void lua_nextvar (void)
  195. {
  196. Word next;
  197. lua_Object o = lua_getparam(1);
  198. if (o == LUA_NOOBJECT)
  199. lua_error("too few arguments to function `nextvar'");
  200. if (lua_getparam(2) != LUA_NOOBJECT)
  201. lua_error("too many arguments to function `nextvar'");
  202. if (lua_isnil(o))
  203. next = 0;
  204. else if (!lua_isstring(o))
  205. {
  206. lua_error("incorrect argument to function `nextvar'");
  207. return; /* to avoid warnings */
  208. }
  209. else
  210. next = luaI_findsymbolbyname(lua_getstring(o)) + 1;
  211. while (next < lua_ntable && s_tag(next) == LUA_T_NIL) next++;
  212. if (next < lua_ntable)
  213. {
  214. lua_pushstring(lua_table[next].varname->str);
  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. }