fallback.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. ** fallback.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_fallback="$Id: fallback.c,v 2.4 1997/04/07 14:48:53 roberto Exp roberto $";
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "auxlib.h"
  9. #include "luamem.h"
  10. #include "fallback.h"
  11. #include "opcode.h"
  12. #include "lua.h"
  13. #include "table.h"
  14. #include "tree.h"
  15. #include "hash.h"
  16. /* -------------------------------------------
  17. ** Reference routines
  18. */
  19. static struct ref {
  20. TObject o;
  21. enum {LOCK, HOLD, FREE, COLLECTED} status;
  22. } *refArray = NULL;
  23. static int refSize = 0;
  24. int luaI_ref (TObject *object, int lock)
  25. {
  26. int i;
  27. int oldSize;
  28. if (ttype(object) == LUA_T_NIL)
  29. return -1; /* special ref for nil */
  30. for (i=0; i<refSize; i++)
  31. if (refArray[i].status == FREE)
  32. goto found;
  33. /* no more empty spaces */
  34. oldSize = refSize;
  35. refSize = growvector(&refArray, refSize, struct ref, refEM, MAX_WORD);
  36. for (i=oldSize; i<refSize; i++)
  37. refArray[i].status = FREE;
  38. i = oldSize;
  39. found:
  40. refArray[i].o = *object;
  41. refArray[i].status = lock ? LOCK : HOLD;
  42. return i;
  43. }
  44. void lua_unref (int ref)
  45. {
  46. if (ref >= 0 && ref < refSize)
  47. refArray[ref].status = FREE;
  48. }
  49. TObject *luaI_getref (int ref)
  50. {
  51. static TObject nul = {LUA_T_NIL, {0}};
  52. if (ref == -1)
  53. return &nul;
  54. if (ref >= 0 && ref < refSize &&
  55. (refArray[ref].status == LOCK || refArray[ref].status == HOLD))
  56. return &refArray[ref].o;
  57. else
  58. return NULL;
  59. }
  60. void luaI_travlock (int (*fn)(TObject *))
  61. {
  62. int i;
  63. for (i=0; i<refSize; i++)
  64. if (refArray[i].status == LOCK)
  65. fn(&refArray[i].o);
  66. }
  67. void luaI_invalidaterefs (void)
  68. {
  69. int i;
  70. for (i=0; i<refSize; i++)
  71. if (refArray[i].status == HOLD && !luaI_ismarked(&refArray[i].o))
  72. refArray[i].status = COLLECTED;
  73. }
  74. /* -------------------------------------------
  75. * Internal Methods
  76. */
  77. char *luaI_eventname[] = { /* ORDER IM */
  78. "gettable", "settable", "index", "getglobal", "setglobal", "add",
  79. "sub", "mul", "div", "pow", "unm", "lt", "le", "gt", "ge",
  80. "concat", "gc", "function",
  81. NULL
  82. };
  83. static int luaI_checkevent (char *name, char *list[])
  84. {
  85. int e = luaI_findstring(name, list);
  86. if (e < 0)
  87. luaL_verror("`%s' is not a valid event name", name);
  88. return e;
  89. }
  90. struct IM *luaI_IMtable = NULL;
  91. static int IMtable_size = 0;
  92. static int last_tag = LUA_T_NIL; /* ORDER LUA_T */
  93. static char validevents[NUM_TYPES][IM_N] = { /* ORDER LUA_T, ORDER IM */
  94. {1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_USERDATA */
  95. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_LINE */
  96. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_CMARK */
  97. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_MARK */
  98. {1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CFUNCTION */
  99. {1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_FUNCTION */
  100. {0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_ARRAY */
  101. {1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */
  102. {1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1}, /* LUA_T_NUMBER */
  103. {0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0} /* LUA_T_NIL */
  104. };
  105. static int validevent (lua_Type t, int e)
  106. { /* ORDER LUA_T */
  107. return (t < LUA_T_NIL) ? 1 : validevents[-t][e];
  108. }
  109. static void init_entry (int tag)
  110. {
  111. int i;
  112. for (i=0; i<IM_N; i++)
  113. ttype(luaI_getim(tag, i)) = LUA_T_NIL;
  114. }
  115. void luaI_initfallbacks (void)
  116. {
  117. if (luaI_IMtable == NULL) {
  118. int i;
  119. IMtable_size = NUM_TYPES+10;
  120. luaI_IMtable = newvector(IMtable_size, struct IM);
  121. for (i=LUA_T_NIL; i<=LUA_T_USERDATA; i++)
  122. init_entry(i);
  123. }
  124. }
  125. int lua_newtag (void)
  126. {
  127. --last_tag;
  128. if ((-last_tag) >= IMtable_size) {
  129. luaI_initfallbacks();
  130. IMtable_size = growvector(&luaI_IMtable, IMtable_size,
  131. struct IM, memEM, MAX_INT);
  132. }
  133. init_entry(last_tag);
  134. return last_tag;
  135. }
  136. static void checktag (int tag)
  137. {
  138. if (!(last_tag <= tag && tag <= 0))
  139. luaL_verror("%d is not a valid tag", tag);
  140. }
  141. void luaI_realtag (int tag)
  142. {
  143. if (!(last_tag <= tag && tag < LUA_T_NIL))
  144. luaL_verror("tag %d is not result of `newtag'", tag);
  145. }
  146. void luaI_settag (int tag, TObject *o)
  147. {
  148. luaI_realtag(tag);
  149. switch (ttype(o)) {
  150. case LUA_T_ARRAY:
  151. o->value.a->htag = tag;
  152. break;
  153. default:
  154. luaL_verror("cannot change the tag of a %s", luaI_typenames[-ttype(o)]);
  155. }
  156. }
  157. int luaI_efectivetag (TObject *o)
  158. {
  159. lua_Type t = ttype(o);
  160. if (t == LUA_T_USERDATA) {
  161. int tag = o->value.ts->tag;
  162. return (tag >= 0) ? LUA_T_USERDATA : tag;
  163. }
  164. else if (t == LUA_T_ARRAY)
  165. return o->value.a->htag;
  166. else return t;
  167. }
  168. void luaI_gettagmethod (void)
  169. {
  170. int t = (int)luaL_check_number(1);
  171. int e = luaI_checkevent(luaL_check_string(2), luaI_eventname);
  172. checktag(t);
  173. if (validevent(t, e))
  174. luaI_pushobject(luaI_getim(t,e));
  175. }
  176. void luaI_settagmethod (void)
  177. {
  178. int t = (int)luaL_check_number(1);
  179. int e = luaI_checkevent(luaL_check_string(2), luaI_eventname);
  180. lua_Object func = lua_getparam(3);
  181. checktag(t);
  182. if (!validevent(t, e))
  183. luaL_verror("cannot change internal method `%s' for tag %d",
  184. luaI_eventname[e], t);
  185. luaL_arg_check(lua_isnil(func) || lua_isfunction(func),
  186. 3, "function expected");
  187. luaI_pushobject(luaI_getim(t,e));
  188. *luaI_getim(t, e) = *luaI_Address(func);
  189. }
  190. static TObject errorim = {LUA_T_NIL, {NULL}};
  191. TObject *luaI_geterrorim (void)
  192. {
  193. return &errorim;
  194. }
  195. void luaI_seterrormethod (void)
  196. {
  197. lua_Object func = lua_getparam(1);
  198. luaL_arg_check(lua_isnil(func) || lua_isfunction(func),
  199. 1, "function expected");
  200. luaI_pushobject(&errorim);
  201. errorim = *luaI_Address(func);
  202. }
  203. char *luaI_travfallbacks (int (*fn)(TObject *))
  204. {
  205. int e;
  206. if (fn(&errorim))
  207. return "error";
  208. for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */
  209. int t;
  210. for (t=0; t>=last_tag; t--)
  211. if (fn(luaI_getim(t,e)))
  212. return luaI_eventname[e];
  213. }
  214. return NULL;
  215. }
  216. /*
  217. * ===================================================================
  218. * compatibility with old fallback system
  219. */
  220. static void errorFB (void)
  221. {
  222. lua_Object o = lua_getparam(1);
  223. if (lua_isstring(o))
  224. fprintf (stderr, "lua: %s\n", lua_getstring(o));
  225. else
  226. fprintf(stderr, "lua: unknown error\n");
  227. }
  228. static void nilFB (void) { }
  229. static void typeFB (void)
  230. {
  231. lua_error("unexpected type");
  232. }
  233. static void fillvalids (IMS e, TObject *func)
  234. {
  235. int t;
  236. for (t=LUA_T_NIL; t<=LUA_T_USERDATA; t++)
  237. if (validevent(t, e))
  238. *luaI_getim(t, e) = *func;
  239. }
  240. void luaI_setfallback (void)
  241. {
  242. static char *oldnames [] = {"error", "getglobal", "arith", "order", NULL};
  243. TObject oldfunc;
  244. lua_CFunction replace;
  245. char *name = luaL_check_string(1);
  246. lua_Object func = lua_getparam(2);
  247. luaI_initfallbacks();
  248. luaL_arg_check(lua_isfunction(func), 2, "function expected");
  249. switch (luaI_findstring(name, oldnames)) {
  250. case 0: /* old error fallback */
  251. oldfunc = errorim;
  252. errorim = *luaI_Address(func);
  253. replace = errorFB;
  254. break;
  255. case 1: /* old getglobal fallback */
  256. oldfunc = *luaI_getim(LUA_T_NIL, IM_GETGLOBAL);
  257. *luaI_getim(LUA_T_NIL, IM_GETGLOBAL) = *luaI_Address(func);
  258. replace = nilFB;
  259. break;
  260. case 2: { /* old arith fallback */
  261. int i;
  262. oldfunc = *luaI_getim(LUA_T_USERDATA, IM_POW);
  263. for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */
  264. fillvalids(i, luaI_Address(func));
  265. replace = typeFB;
  266. break;
  267. }
  268. case 3: { /* old order fallback */
  269. int i;
  270. oldfunc = *luaI_getim(LUA_T_USERDATA, IM_LT);
  271. for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */
  272. fillvalids(i, luaI_Address(func));
  273. replace = typeFB;
  274. break;
  275. }
  276. default: {
  277. int e;
  278. if ((e = luaI_findstring(name, luaI_eventname)) >= 0) {
  279. oldfunc = *luaI_getim(LUA_T_USERDATA, e);
  280. fillvalids(e, luaI_Address(func));
  281. replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
  282. }
  283. else {
  284. luaL_verror("`%s' is not a valid fallback name", name);
  285. replace = NULL; /* to avoid warnings */
  286. }
  287. }
  288. }
  289. if (oldfunc.ttype != LUA_T_NIL)
  290. luaI_pushobject(&oldfunc);
  291. else
  292. lua_pushcfunction(replace);
  293. }