fallback.c 8.4 KB

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