table.c 5.7 KB

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