table.c 6.2 KB

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