table.c 6.1 KB

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