ltm.c 6.3 KB

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