table.c 5.5 KB

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