table.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. ** table.c
  3. ** Module to control static tables
  4. */
  5. char *rcs_table="$Id: table.c,v 2.50 1996/03/21 16:31:32 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. ** Garbage collection.
  146. ** Delete all unused strings and arrays.
  147. */
  148. Long luaI_collectgarbage (void)
  149. {
  150. Long recovered = 0;
  151. lua_travstack(lua_markobject); /* mark stack objects */
  152. lua_travsymbol(lua_markobject); /* mark symbol table objects */
  153. luaI_travlock(lua_markobject); /* mark locked objects */
  154. luaI_travfallbacks(lua_markobject); /* mark fallbacks */
  155. recovered += lua_strcollector();
  156. recovered += lua_hashcollector();
  157. recovered += luaI_funccollector();
  158. return recovered;
  159. }
  160. void lua_pack (void)
  161. {
  162. static Long block = GARBAGE_BLOCK; /* when garbage collector will be called */
  163. static Long nentity = 0; /* counter of new entities (strings and arrays) */
  164. Long recovered = 0;
  165. if (nentity++ < block) return;
  166. recovered = luaI_collectgarbage();
  167. nentity = 0; /* reset counter */
  168. block=(16*block-7*recovered)/12; /* adapt block size */
  169. if (block < MIN_GARBAGE_BLOCK) block = MIN_GARBAGE_BLOCK;
  170. }
  171. /*
  172. ** Internal function: return next global variable
  173. */
  174. static void lua_nextvar (void)
  175. {
  176. Word next;
  177. lua_Object o = lua_getparam(1);
  178. if (o == LUA_NOOBJECT)
  179. lua_error("too few arguments to function `nextvar'");
  180. if (lua_getparam(2) != LUA_NOOBJECT)
  181. lua_error("too many arguments to function `nextvar'");
  182. if (lua_isnil(o))
  183. next = 0;
  184. else if (!lua_isstring(o))
  185. {
  186. lua_error("incorrect argument to function `nextvar'");
  187. return; /* to avoid warnings */
  188. }
  189. else
  190. next = luaI_findsymbolbyname(lua_getstring(o)) + 1;
  191. while (next < lua_ntable && s_tag(next) == LUA_T_NIL) next++;
  192. if (next >= lua_ntable)
  193. {
  194. lua_pushnil();
  195. lua_pushnil();
  196. }
  197. else
  198. {
  199. TaggedString *t = lua_table[next].varname;
  200. Object name;
  201. tag(&name) = LUA_T_STRING;
  202. tsvalue(&name) = t;
  203. luaI_pushobject(&name);
  204. luaI_pushobject(&s_object(next));
  205. }
  206. }
  207. static Object *functofind;
  208. static int checkfunc (Object *o)
  209. {
  210. if (o->tag == LUA_T_FUNCTION)
  211. return
  212. ((functofind->tag == LUA_T_FUNCTION || functofind->tag == LUA_T_MARK)
  213. && (functofind->value.tf == o->value.tf));
  214. if (o->tag == LUA_T_CFUNCTION)
  215. return
  216. ((functofind->tag == LUA_T_CFUNCTION || functofind->tag == LUA_T_CMARK)
  217. && (functofind->value.f == o->value.f));
  218. return 0;
  219. }
  220. char *lua_getobjname (lua_Object o, char **name)
  221. { /* try to find a name for given function */
  222. functofind = luaI_Address(o);
  223. if ((*name = luaI_travfallbacks(checkfunc)) != NULL)
  224. return "fallback";
  225. else if ((*name = lua_travsymbol(checkfunc)) != NULL)
  226. return "global";
  227. else return "";
  228. }