fallback.c 8.5 KB

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