fallback.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. ** fallback.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_fallback="$Id: fallback.c,v 2.3 1997/04/06 14:08:08 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. static struct IM {
  91. TObject int_method[IM_N];
  92. } *luaI_IMtable = NULL;
  93. static int IMtable_size = 0;
  94. static int last_tag = LUA_T_NIL; /* ORDER LUA_T */
  95. static char validevents[NUM_TYPES][IM_N] = { /* ORDER LUA_T, ORDER IM */
  96. {1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_USERDATA */
  97. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_LINE */
  98. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_CMARK */
  99. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_MARK */
  100. {1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CFUNCTION */
  101. {1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_FUNCTION */
  102. {0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_ARRAY */
  103. {1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */
  104. {1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1}, /* LUA_T_NUMBER */
  105. {0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0} /* LUA_T_NIL */
  106. };
  107. static int validevent (lua_Type t, int e)
  108. { /* ORDER LUA_T */
  109. return (t < LUA_T_NIL) ? 1 : validevents[-t][e];
  110. }
  111. static void init_entry (int tag)
  112. {
  113. int i;
  114. for (i=0; i<IM_N; i++)
  115. luaI_IMtable[-tag].int_method[i].ttype = LUA_T_NIL;
  116. }
  117. void luaI_initfallbacks (void)
  118. {
  119. if (luaI_IMtable == NULL) {
  120. int i;
  121. IMtable_size = NUM_TYPES+10;
  122. luaI_IMtable = newvector(IMtable_size, struct IM);
  123. for (i=LUA_T_NIL; i<=LUA_T_USERDATA; i++)
  124. init_entry(i);
  125. }
  126. }
  127. int lua_newtag (void)
  128. {
  129. --last_tag;
  130. if ((-last_tag) >= IMtable_size) {
  131. luaI_initfallbacks();
  132. IMtable_size = growvector(&luaI_IMtable, IMtable_size,
  133. struct IM, memEM, MAX_INT);
  134. }
  135. init_entry(last_tag);
  136. return last_tag;
  137. }
  138. static void checktag (int tag)
  139. {
  140. if (!(last_tag <= tag && tag <= 0))
  141. luaL_verror("%d is not a valid tag", tag);
  142. }
  143. void luaI_realtag (int tag)
  144. {
  145. if (!(last_tag <= tag && tag < LUA_T_NIL))
  146. luaL_verror("tag %d is not result of `newtag'", tag);
  147. }
  148. void luaI_settag (int tag, TObject *o)
  149. {
  150. luaI_realtag(tag);
  151. switch (ttype(o)) {
  152. case LUA_T_ARRAY:
  153. o->value.a->htag = tag;
  154. break;
  155. default:
  156. luaL_verror("cannot change the tag of a %s", luaI_typenames[-ttype(o)]);
  157. }
  158. }
  159. int luaI_tag (TObject *o)
  160. {
  161. lua_Type t = ttype(o);
  162. if (t == LUA_T_USERDATA)
  163. return o->value.ts->tag;
  164. else if (t == LUA_T_ARRAY)
  165. return o->value.a->htag;
  166. else return t;
  167. }
  168. TObject *luaI_getim (int tag, IMS event)
  169. {
  170. if (tag > LUA_T_USERDATA)
  171. tag = LUA_T_USERDATA; /* default for non-registered tags */
  172. return &luaI_IMtable[-tag].int_method[event];
  173. }
  174. void luaI_gettagmethod (void)
  175. {
  176. int t = (int)luaL_check_number(1);
  177. int e = luaI_checkevent(luaL_check_string(2), luaI_eventname);
  178. checktag(t);
  179. if (validevent(t, e))
  180. luaI_pushobject(&luaI_IMtable[-t].int_method[e]);
  181. }
  182. void luaI_settagmethod (void)
  183. {
  184. int t = (int)luaL_check_number(1);
  185. int e = luaI_checkevent(luaL_check_string(2), luaI_eventname);
  186. lua_Object func = lua_getparam(3);
  187. checktag(t);
  188. if (!validevent(t, e))
  189. luaL_verror("cannot change internal method `%s' for tag %d",
  190. luaI_eventname[e], t);
  191. luaL_arg_check(lua_isnil(func) || lua_isfunction(func),
  192. 3, "function expected");
  193. luaI_pushobject(&luaI_IMtable[-t].int_method[e]);
  194. luaI_IMtable[-t].int_method[e] = *luaI_Address(func);
  195. }
  196. static TObject errorim = {LUA_T_NIL, {NULL}};
  197. TObject *luaI_geterrorim (void)
  198. {
  199. return &errorim;
  200. }
  201. void luaI_seterrormethod (void)
  202. {
  203. lua_Object func = lua_getparam(1);
  204. luaL_arg_check(lua_isnil(func) || lua_isfunction(func),
  205. 1, "function expected");
  206. luaI_pushobject(&errorim);
  207. errorim = *luaI_Address(func);
  208. }
  209. char *luaI_travfallbacks (int (*fn)(TObject *))
  210. {
  211. int e;
  212. if (fn(&errorim))
  213. return "error";
  214. for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */
  215. int t;
  216. for (t=0; t>=last_tag; t--)
  217. if (fn(&luaI_IMtable[-t].int_method[e]))
  218. return luaI_eventname[e];
  219. }
  220. return NULL;
  221. }
  222. /*
  223. * ===================================================================
  224. * compatibility with old fallback system
  225. */
  226. static void errorFB (void)
  227. {
  228. lua_Object o = lua_getparam(1);
  229. if (lua_isstring(o))
  230. fprintf (stderr, "lua: %s\n", lua_getstring(o));
  231. else
  232. fprintf(stderr, "lua: unknown error\n");
  233. }
  234. static void nilFB (void) { }
  235. static void typeFB (void)
  236. {
  237. lua_error("unexpected type");
  238. }
  239. static void fillvalids (IMS e, TObject *func)
  240. {
  241. int t;
  242. for (t=LUA_T_NIL; t<=LUA_T_USERDATA; t++)
  243. if (validevent(t, e))
  244. luaI_IMtable[-t].int_method[e] = *func;
  245. }
  246. void luaI_setfallback (void)
  247. {
  248. static char *oldnames [] = {"error", "getglobal", "arith", "order", NULL};
  249. TObject oldfunc;
  250. lua_CFunction replace;
  251. char *name = luaL_check_string(1);
  252. lua_Object func = lua_getparam(2);
  253. luaI_initfallbacks();
  254. luaL_arg_check(lua_isfunction(func), 2, "function expected");
  255. switch (luaI_findstring(name, oldnames)) {
  256. case 0: /* old error fallback */
  257. oldfunc = errorim;
  258. errorim = *luaI_Address(func);
  259. replace = errorFB;
  260. break;
  261. case 1: /* old getglobal fallback */
  262. oldfunc = luaI_IMtable[-LUA_T_NIL].int_method[IM_GETGLOBAL];
  263. luaI_IMtable[-LUA_T_NIL].int_method[IM_GETGLOBAL] = *luaI_Address(func);
  264. replace = nilFB;
  265. break;
  266. case 2: { /* old arith fallback */
  267. int i;
  268. oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[IM_POW];
  269. for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */
  270. fillvalids(i, luaI_Address(func));
  271. replace = typeFB;
  272. break;
  273. }
  274. case 3: { /* old order fallback */
  275. int i;
  276. oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[IM_LT];
  277. for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */
  278. fillvalids(i, luaI_Address(func));
  279. replace = typeFB;
  280. break;
  281. }
  282. default: {
  283. int e;
  284. if ((e = luaI_findstring(name, luaI_eventname)) >= 0) {
  285. oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[e];
  286. fillvalids(e, luaI_Address(func));
  287. replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
  288. }
  289. else {
  290. luaL_verror("`%s' is not a valid fallback name", name);
  291. replace = NULL; /* to avoid warnings */
  292. }
  293. }
  294. }
  295. if (oldfunc.ttype != LUA_T_NIL)
  296. luaI_pushobject(&oldfunc);
  297. else
  298. lua_pushcfunction(replace);
  299. }