ltm.c 6.2 KB

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