table.c 6.7 KB

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