table.c 6.2 KB

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