table.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. ** table.c
  3. ** Module to control static tables
  4. */
  5. char *rcs_table="$Id: table.c,v 2.9 1994/11/03 21:48:36 roberto Exp $";
  6. #include <stdlib.h>
  7. #include <string.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. #define streq(s1,s2) (s1[0]==s2[0]&&strcmp(s1+1,s2+1)==0)
  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. /*
  31. ** Initialise symbol table with internal functions
  32. */
  33. static void lua_initsymbol (void)
  34. {
  35. int n;
  36. lua_maxsymbol = BUFFER_BLOCK;
  37. lua_table = (Symbol *) calloc(lua_maxsymbol, sizeof(Symbol));
  38. if (lua_table == NULL)
  39. {
  40. lua_error ("symbol table: not enough memory");
  41. return;
  42. }
  43. n = lua_findsymbol("type");
  44. s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_type;
  45. n = lua_findsymbol("tonumber");
  46. s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_obj2number;
  47. n = lua_findsymbol("next");
  48. s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_next;
  49. n = lua_findsymbol("nextvar");
  50. s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_nextvar;
  51. n = lua_findsymbol("print");
  52. s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_print;
  53. n = lua_findsymbol("dofile");
  54. s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldofile;
  55. n = lua_findsymbol("dostring");
  56. s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldostring;
  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 = (char **) calloc(lua_maxconstant, sizeof(char *));
  65. if (lua_constant == NULL)
  66. lua_error ("constant table: not enough memory");
  67. }
  68. /*
  69. ** Given a name, search it at symbol table and return its index. If not
  70. ** found, allocate it.
  71. ** On error, return -1.
  72. */
  73. int lua_findsymbol (char *s)
  74. {
  75. char *n;
  76. if (lua_table == NULL)
  77. lua_initsymbol();
  78. n = lua_varcreate(s);
  79. if (n == NULL)
  80. {
  81. lua_error ("create symbol: not enough memory");
  82. return -1;
  83. }
  84. if (indexstring(n) == UNMARKED_STRING)
  85. {
  86. if (lua_ntable == lua_maxsymbol)
  87. {
  88. lua_maxsymbol *= 2;
  89. if (lua_maxsymbol > MAX_WORD)
  90. {
  91. lua_error("symbol table overflow");
  92. return -1;
  93. }
  94. lua_table = (Symbol *)realloc(lua_table, lua_maxsymbol*sizeof(Symbol));
  95. if (lua_table == NULL)
  96. {
  97. lua_error ("symbol table: not enough memory");
  98. return -1;
  99. }
  100. }
  101. indexstring(n) = lua_ntable;
  102. s_tag(lua_ntable) = LUA_T_NIL;
  103. lua_ntable++;
  104. }
  105. return indexstring(n);
  106. }
  107. /*
  108. ** Given a name, search it at constant table and return its index. If not
  109. ** found, allocate it.
  110. ** On error, return -1.
  111. */
  112. int lua_findconstant (char *s)
  113. {
  114. char *n;
  115. if (lua_constant == NULL)
  116. lua_initconstant();
  117. n = lua_constcreate(s);
  118. if (n == NULL)
  119. {
  120. lua_error ("create constant: not enough memory");
  121. return -1;
  122. }
  123. if (indexstring(n) == UNMARKED_STRING)
  124. {
  125. if (lua_nconstant == lua_maxconstant)
  126. {
  127. lua_maxconstant *= 2;
  128. if (lua_maxconstant > MAX_WORD)
  129. {
  130. lua_error("constant table overflow");
  131. return -1;
  132. }
  133. lua_constant = (char**)realloc(lua_constant,lua_maxconstant*sizeof(char*));
  134. if (lua_constant == NULL)
  135. {
  136. lua_error ("constant table: not enough memory");
  137. return -1;
  138. }
  139. }
  140. indexstring(n) = lua_nconstant;
  141. lua_constant[lua_nconstant] = n;
  142. lua_nconstant++;
  143. }
  144. return indexstring(n);
  145. }
  146. /*
  147. ** Traverse symbol table objects
  148. */
  149. void lua_travsymbol (void (*fn)(Object *))
  150. {
  151. int i;
  152. for (i=0; i<lua_ntable; i++)
  153. fn(&s_object(i));
  154. }
  155. /*
  156. ** Mark an object if it is a string or a unmarked array.
  157. */
  158. void lua_markobject (Object *o)
  159. {
  160. if (tag(o) == LUA_T_STRING && indexstring(svalue(o)) == UNMARKED_STRING)
  161. indexstring(svalue(o)) = MARKED_STRING;
  162. else if (tag(o) == LUA_T_ARRAY)
  163. lua_hashmark (avalue(o));
  164. }
  165. /*
  166. ** Garbage collection.
  167. ** Delete all unused strings and arrays.
  168. */
  169. void lua_pack (void)
  170. {
  171. /* mark stack strings */
  172. lua_travstack(lua_markobject);
  173. /* mark symbol table strings */
  174. lua_travsymbol(lua_markobject);
  175. lua_recovered=0;
  176. lua_strcollector();
  177. lua_hashcollector();
  178. lua_nentity = 0; /* reset counter */
  179. lua_block=2*lua_block-3*lua_recovered/2; /* adapt block size */
  180. }
  181. /*
  182. ** If the string isn't allocated, allocate a new string at string tree.
  183. */
  184. char *lua_createstring (char *s)
  185. {
  186. if (s == NULL) return NULL;
  187. return lua_strcreate(s);
  188. }
  189. /*
  190. ** Add a file name at file table, checking overflow. This function also set
  191. ** the external variable "lua_filename" with the function filename set.
  192. ** Return 0 on success or error message on error.
  193. */
  194. char *lua_addfile (char *fn)
  195. {
  196. if (lua_nfile >= MAXFILE-1)
  197. return "too many files";
  198. if ((lua_file[lua_nfile++] = strdup (fn)) == NULL)
  199. return "not enough memory";
  200. return NULL;
  201. }
  202. /*
  203. ** Delete a file from file stack
  204. */
  205. int lua_delfile (void)
  206. {
  207. free(lua_file[lua_nfile--]);
  208. return 1;
  209. }
  210. /*
  211. ** Return the last file name set.
  212. */
  213. char *lua_filename (void)
  214. {
  215. return lua_file[lua_nfile-1];
  216. }
  217. /*
  218. ** Internal function: return next global variable
  219. */
  220. void lua_nextvar (void)
  221. {
  222. char *varname, *next;
  223. Object *o = lua_getparam (1);
  224. if (o == NULL)
  225. { lua_error ("too few arguments to function `nextvar'"); return; }
  226. if (lua_getparam (2) != NULL)
  227. { lua_error ("too many arguments to function `nextvar'"); return; }
  228. if (tag(o) == LUA_T_NIL)
  229. {
  230. varname = 0;
  231. }
  232. else if (tag(o) != LUA_T_STRING)
  233. {
  234. lua_error ("incorrect argument to function `nextvar'");
  235. return;
  236. }
  237. else
  238. {
  239. varname = svalue(o);
  240. }
  241. next = lua_varnext(varname);
  242. if (next == NULL)
  243. {
  244. lua_pushnil();
  245. lua_pushnil();
  246. }
  247. else
  248. {
  249. Object name;
  250. tag(&name) = LUA_T_STRING;
  251. svalue(&name) = next;
  252. if (lua_pushobject (&name)) return;
  253. if (lua_pushobject (&s_object(indexstring(next)))) return;
  254. }
  255. }