table.c 7.0 KB

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