ltm.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. ** $Id: ltm.c,v 1.15 1998/03/11 13:59:50 roberto Exp roberto $
  3. ** Tag methods
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "lauxlib.h"
  9. #include "lmem.h"
  10. #include "lobject.h"
  11. #include "lstate.h"
  12. #include "ltm.h"
  13. char *luaT_eventname[] = { /* ORDER IM */
  14. "gettable", "settable", "index", "getglobal", "setglobal", "add",
  15. "sub", "mul", "div", "pow", "unm", "lt", "le", "gt", "ge",
  16. "concat", "gc", "function", NULL
  17. };
  18. static int luaI_checkevent (char *name, char *list[])
  19. {
  20. int e = luaL_findstring(name, list);
  21. if (e < 0)
  22. luaL_verror("`%.50s' is not a valid event name", name);
  23. return e;
  24. }
  25. /* events in LUA_T_NIL are all allowed, since this is used as a
  26. * 'placeholder' for "default" fallbacks
  27. */
  28. static char validevents[NUM_TAGS][IM_N] = { /* ORDER LUA_T, ORDER IM */
  29. {1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_T_USERDATA */
  30. {1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_NUMBER */
  31. {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */
  32. {0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_ARRAY */
  33. {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_PROTO */
  34. {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CPROTO */
  35. {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* LUA_T_NIL */
  36. };
  37. static int validevent (int t, int e)
  38. { /* ORDER LUA_T */
  39. return (t < LUA_T_NIL) ? 1 : validevents[-t][e];
  40. }
  41. static void init_entry (int tag)
  42. {
  43. int i;
  44. for (i=0; i<IM_N; i++)
  45. ttype(luaT_getim(tag, i)) = LUA_T_NIL;
  46. }
  47. void luaT_init (void)
  48. {
  49. int t;
  50. L->IMtable_size = NUM_TAGS*2;
  51. L->last_tag = -(NUM_TAGS-1);
  52. L->IMtable = luaM_newvector(L->IMtable_size, struct IM);
  53. for (t=L->last_tag; t<=0; t++)
  54. init_entry(t);
  55. }
  56. int lua_newtag (void)
  57. {
  58. --L->last_tag;
  59. if ((-L->last_tag) >= L->IMtable_size)
  60. L->IMtable_size = luaM_growvector(&L->IMtable, L->IMtable_size,
  61. struct IM, memEM, MAX_INT);
  62. init_entry(L->last_tag);
  63. return L->last_tag;
  64. }
  65. static void checktag (int tag)
  66. {
  67. if (!(L->last_tag <= tag && tag <= 0))
  68. luaL_verror("%d is not a valid tag", tag);
  69. }
  70. void luaT_realtag (int tag)
  71. {
  72. if (!(L->last_tag <= tag && tag < LUA_T_NIL))
  73. luaL_verror("tag %d is not result of `newtag'", tag);
  74. }
  75. int lua_copytagmethods (int tagto, int tagfrom)
  76. {
  77. int e;
  78. checktag(tagto);
  79. checktag(tagfrom);
  80. for (e=0; e<IM_N; e++) {
  81. if (validevent(tagto, e))
  82. *luaT_getim(tagto, e) = *luaT_getim(tagfrom, e);
  83. }
  84. return tagto;
  85. }
  86. int luaT_efectivetag (TObject *o)
  87. {
  88. int t;
  89. switch (t = ttype(o)) {
  90. case LUA_T_ARRAY:
  91. return o->value.a->htag;
  92. case LUA_T_USERDATA: {
  93. int tag = o->value.ts->u.d.tag;
  94. return (tag >= 0) ? LUA_T_USERDATA : tag;
  95. }
  96. case LUA_T_CLOSURE:
  97. return o->value.cl->consts[0].ttype;
  98. #ifdef DEBUG
  99. case LUA_T_PMARK: case LUA_T_CMARK:
  100. case LUA_T_CLMARK: case LUA_T_LINE:
  101. LUA_INTERNALERROR("invalid type");
  102. #endif
  103. default:
  104. return t;
  105. }
  106. }
  107. TObject *luaT_gettagmethod (int t, char *event)
  108. {
  109. int e = luaI_checkevent(event, luaT_eventname);
  110. checktag(t);
  111. if (validevent(t, e))
  112. return luaT_getim(t,e);
  113. else
  114. return &luaO_nilobject;
  115. }
  116. void luaT_settagmethod (int t, char *event, TObject *func)
  117. {
  118. TObject temp = *func;
  119. int e = luaI_checkevent(event, luaT_eventname);
  120. checktag(t);
  121. if (!validevent(t, e))
  122. luaL_verror("settagmethod: cannot change tag method `%.20s' for tag %d",
  123. luaT_eventname[e], t);
  124. *func = *luaT_getim(t,e);
  125. *luaT_getim(t, e) = temp;
  126. }
  127. char *luaT_travtagmethods (int (*fn)(TObject *))
  128. {
  129. int e;
  130. if (fn(&L->errorim))
  131. return "error";
  132. for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */
  133. int t;
  134. for (t=0; t>=L->last_tag; t--)
  135. if (fn(luaT_getim(t,e)))
  136. return luaT_eventname[e];
  137. }
  138. return NULL;
  139. }
  140. /*
  141. * ===================================================================
  142. * compatibility with old fallback system
  143. */
  144. #ifdef LUA_COMPAT2_5
  145. #include "lapi.h"
  146. static void errorFB (void)
  147. {
  148. lua_Object o = lua_getparam(1);
  149. if (lua_isstring(o))
  150. fprintf(stderr, "lua: %s\n", lua_getstring(o));
  151. else
  152. fprintf(stderr, "lua: unknown error\n");
  153. }
  154. static void nilFB (void) { }
  155. static void typeFB (void)
  156. {
  157. lua_error("unexpected type");
  158. }
  159. static void fillvalids (IMS e, TObject *func)
  160. {
  161. int t;
  162. for (t=LUA_T_NIL; t<=LUA_T_USERDATA; t++)
  163. if (validevent(t, e))
  164. *luaT_getim(t, e) = *func;
  165. }
  166. void luaT_setfallback (void)
  167. {
  168. static char *oldnames [] = {"error", "getglobal", "arith", "order", NULL};
  169. TObject oldfunc;
  170. lua_CFunction replace;
  171. char *name = luaL_check_string(1);
  172. lua_Object func = lua_getparam(2);
  173. luaL_arg_check(lua_isfunction(func), 2, "function expected");
  174. switch (luaL_findstring(name, oldnames)) {
  175. case 0: /* old error fallback */
  176. oldfunc = L->errorim;
  177. L->errorim = *luaA_Address(func);
  178. replace = errorFB;
  179. break;
  180. case 1: /* old getglobal fallback */
  181. oldfunc = *luaT_getim(LUA_T_NIL, IM_GETGLOBAL);
  182. *luaT_getim(LUA_T_NIL, IM_GETGLOBAL) = *luaA_Address(func);
  183. replace = nilFB;
  184. break;
  185. case 2: { /* old arith fallback */
  186. int i;
  187. oldfunc = *luaT_getim(LUA_T_NUMBER, IM_POW);
  188. for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */
  189. fillvalids(i, luaA_Address(func));
  190. replace = typeFB;
  191. break;
  192. }
  193. case 3: { /* old order fallback */
  194. int i;
  195. oldfunc = *luaT_getim(LUA_T_NIL, IM_LT);
  196. for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */
  197. fillvalids(i, luaA_Address(func));
  198. replace = typeFB;
  199. break;
  200. }
  201. default: {
  202. int e;
  203. if ((e = luaL_findstring(name, luaT_eventname)) >= 0) {
  204. oldfunc = *luaT_getim(LUA_T_NIL, e);
  205. fillvalids(e, luaA_Address(func));
  206. replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
  207. }
  208. else {
  209. luaL_verror("`%.50s' is not a valid fallback name", name);
  210. replace = NULL; /* to avoid warnings */
  211. }
  212. }
  213. }
  214. if (oldfunc.ttype != LUA_T_NIL)
  215. luaA_pushobject(&oldfunc);
  216. else
  217. lua_pushcfunction(replace);
  218. }
  219. #endif