table.c 6.7 KB

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