table.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. ** table.c
  3. ** Module to control static tables
  4. */
  5. char *rcs_table="$Id: table.c,v 2.34 1995/10/13 15:16:25 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 "table.h"
  12. #include "inout.h"
  13. #include "lua.h"
  14. #include "fallback.h"
  15. #define BUFFER_BLOCK 256
  16. Symbol *lua_table = NULL;
  17. static Word lua_ntable = 0;
  18. static Long lua_maxsymbol = 0;
  19. TaggedString **lua_constant = NULL;
  20. static Word lua_nconstant = 0;
  21. static Long lua_maxconstant = 0;
  22. #define GARBAGE_BLOCK 1024
  23. #define MIN_GARBAGE_BLOCK (GARBAGE_BLOCK/2)
  24. static void lua_nextvar (void);
  25. static void setglobal (void);
  26. static void getglobal (void);
  27. /*
  28. ** Initialise symbol table with internal functions
  29. */
  30. static void lua_initsymbol (void)
  31. {
  32. Word n;
  33. lua_maxsymbol = BUFFER_BLOCK;
  34. lua_table = newvector(lua_maxsymbol, Symbol);
  35. n = luaI_findsymbolbyname("next");
  36. s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_next;
  37. n = luaI_findsymbolbyname("dofile");
  38. s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldofile;
  39. n = luaI_findsymbolbyname("setglobal");
  40. s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = setglobal;
  41. n = luaI_findsymbolbyname("getglobal");
  42. s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = getglobal;
  43. n = luaI_findsymbolbyname("nextvar");
  44. s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_nextvar;
  45. n = luaI_findsymbolbyname("type");
  46. s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_type;
  47. n = luaI_findsymbolbyname("tonumber");
  48. s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_obj2number;
  49. n = luaI_findsymbolbyname("print");
  50. s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_print;
  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, TaggedString *);
  65. }
  66. /*
  67. ** Given a name, search it at symbol table and return its index. If not
  68. ** found, allocate it.
  69. */
  70. Word luaI_findsymbol (TreeNode *t)
  71. {
  72. if (lua_table == NULL)
  73. lua_initsymbol();
  74. if (t->varindex == NOT_USED)
  75. {
  76. if (lua_ntable == lua_maxsymbol)
  77. {
  78. if (lua_maxsymbol >= MAX_WORD)
  79. lua_error("symbol table overflow");
  80. lua_maxsymbol *= 2;
  81. if (lua_maxsymbol >= MAX_WORD)
  82. lua_maxsymbol = MAX_WORD;
  83. lua_table = growvector(lua_table, lua_maxsymbol, Symbol);
  84. }
  85. t->varindex = lua_ntable;
  86. s_tag(lua_ntable) = LUA_T_NIL;
  87. lua_ntable++;
  88. }
  89. return t->varindex;
  90. }
  91. Word luaI_findsymbolbyname (char *name)
  92. {
  93. return luaI_findsymbol(lua_constcreate(name));
  94. }
  95. /*
  96. ** Given a tree node, check it is has a correspondent constant index. If not,
  97. ** allocate it.
  98. */
  99. Word luaI_findconstant (TreeNode *t)
  100. {
  101. if (lua_constant == NULL)
  102. lua_initconstant();
  103. if (t->constindex == NOT_USED)
  104. {
  105. if (lua_nconstant == lua_maxconstant)
  106. {
  107. if (lua_maxconstant >= MAX_WORD)
  108. lua_error("constant table overflow");
  109. lua_maxconstant *= 2;
  110. if (lua_maxconstant >= MAX_WORD)
  111. lua_maxconstant = MAX_WORD;
  112. lua_constant = growvector(lua_constant, lua_maxconstant, TaggedString *);
  113. }
  114. t->constindex = lua_nconstant;
  115. lua_constant[lua_nconstant] = &(t->ts);
  116. lua_nconstant++;
  117. }
  118. return t->constindex;
  119. }
  120. Word luaI_findconstantbyname (char *name)
  121. {
  122. return luaI_findconstant(lua_constcreate(name));
  123. }
  124. /*
  125. ** Traverse symbol table objects
  126. */
  127. static char *lua_travsymbol (int (*fn)(Object *))
  128. {
  129. Word i;
  130. for (i=0; i<lua_ntable; i++)
  131. if (fn(&s_object(i)))
  132. return luaI_nodebysymbol(i)->ts.str;
  133. return NULL;
  134. }
  135. /*
  136. ** Mark an object if it is a string or a unmarked array.
  137. */
  138. int lua_markobject (Object *o)
  139. {
  140. if (tag(o) == LUA_T_STRING && !tsvalue(o)->marked)
  141. tsvalue(o)->marked = 1;
  142. else if (tag(o) == LUA_T_ARRAY)
  143. lua_hashmark (avalue(o));
  144. else if ((o->tag == LUA_T_FUNCTION || o->tag == LUA_T_MARK)
  145. && !o->value.tf->marked)
  146. o->value.tf->marked = 1;
  147. return 0;
  148. }
  149. /*
  150. ** Garbage collection.
  151. ** Delete all unused strings and arrays.
  152. */
  153. void lua_pack (void)
  154. {
  155. static Long block = GARBAGE_BLOCK; /* when garbage collector will be called */
  156. static Long nentity = 0; /* counter of new entities (strings and arrays) */
  157. Long recovered = 0;
  158. if (nentity++ < block) return;
  159. lua_travstack(lua_markobject); /* mark stack objects */
  160. lua_travsymbol(lua_markobject); /* mark symbol table objects */
  161. luaI_travlock(lua_markobject); /* mark locked objects */
  162. luaI_travfallbacks(lua_markobject); /* mark fallbacks */
  163. recovered += lua_strcollector();
  164. recovered += lua_hashcollector();
  165. recovered += luaI_funccollector();
  166. nentity = 0; /* reset counter */
  167. block=(16*block-7*recovered)/12; /* adapt block size */
  168. if (block < MIN_GARBAGE_BLOCK) block = MIN_GARBAGE_BLOCK;
  169. }
  170. /*
  171. ** Internal function: return next global variable
  172. */
  173. static void lua_nextvar (void)
  174. {
  175. Word next;
  176. lua_Object o = lua_getparam(1);
  177. if (o == LUA_NOOBJECT)
  178. lua_error("too few arguments to function `nextvar'");
  179. if (lua_getparam(2) != LUA_NOOBJECT)
  180. lua_error("too many arguments to function `nextvar'");
  181. if (lua_isnil(o))
  182. next = 0;
  183. else if (!lua_isstring(o))
  184. {
  185. lua_error("incorrect argument to function `nextvar'");
  186. return; /* to avoid warnings */
  187. }
  188. else
  189. next = luaI_findsymbolbyname(lua_getstring(o)) + 1;
  190. while (next < lua_ntable && s_tag(next) == LUA_T_NIL) next++;
  191. if (next >= lua_ntable)
  192. {
  193. lua_pushnil();
  194. lua_pushnil();
  195. }
  196. else
  197. {
  198. TreeNode *t = luaI_nodebysymbol(next);
  199. Object name;
  200. tag(&name) = LUA_T_STRING;
  201. tsvalue(&name) = &(t->ts);
  202. luaI_pushobject(&name);
  203. luaI_pushobject(&s_object(next));
  204. }
  205. }
  206. static void setglobal (void)
  207. {
  208. lua_Object name = lua_getparam(1);
  209. lua_Object value = lua_getparam(2);
  210. if (!lua_isstring(name))
  211. lua_error("incorrect argument to function `setglobal'");
  212. lua_pushobject(value);
  213. lua_storeglobal(lua_getstring(name));
  214. }
  215. static void getglobal (void)
  216. {
  217. lua_Object name = lua_getparam(1);
  218. if (!lua_isstring(name))
  219. lua_error("incorrect argument to function `getglobal'");
  220. lua_pushobject(lua_getglobal(lua_getstring(name)));
  221. }
  222. static lua_CFunction cfunc = NULL;
  223. static int checkfunc (Object *o)
  224. {
  225. return ((o->tag == LUA_T_CMARK || o->tag == LUA_T_CFUNCTION) &&
  226. o->value.f == cfunc);
  227. }
  228. void luaI_funcInfo (struct Object *func, char **filename, char **funcname,
  229. char **objname, int *linedefined)
  230. {
  231. if (func->tag == LUA_T_MARK || func->tag == LUA_T_FUNCTION)
  232. {
  233. TFunc *f = func->value.tf;
  234. *filename = f->fileName;
  235. *funcname = f->name1;
  236. *objname = f->name2;
  237. *linedefined = f->lineDefined;
  238. }
  239. else if (func->tag == LUA_T_CMARK || func->tag == LUA_T_CFUNCTION)
  240. {
  241. /* temporario: */
  242. cfunc = func->value.f;
  243. *filename = "(?)";
  244. *objname = 0;
  245. *linedefined = 0;
  246. *funcname = lua_travsymbol(checkfunc);
  247. if (*funcname == NULL)
  248. *funcname = luaI_travfallbacks(checkfunc);
  249. }
  250. }