ltm.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. ** $Id: ltm.c,v 1.32 2000/02/22 18:12:46 roberto Exp roberto $
  3. ** Tag methods
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #define LUA_REENTRANT
  9. #include "lauxlib.h"
  10. #include "lmem.h"
  11. #include "lobject.h"
  12. #include "lstate.h"
  13. #include "ltm.h"
  14. const char *const luaT_eventname[] = { /* ORDER IM */
  15. "gettable", "settable", "index", "getglobal", "setglobal", "add", "sub",
  16. "mul", "div", "pow", "unm", "lt", "concat", "gc", "function", NULL
  17. };
  18. static int luaI_checkevent (lua_State *L, const char *name, const char *const list[]) {
  19. int e = luaL_findstring(name, list);
  20. if (e < 0)
  21. luaL_verror(L, "`%.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. /* ORDER LUA_T, ORDER IM */
  28. static const char luaT_validevents[NUM_TAGS][IM_N] = {
  29. {1, 1, 0, 0, 0, 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, 1}, /* LUA_T_NUMBER */
  31. {1, 1, 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, 0, 1}, /* LUA_T_ARRAY */
  33. {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_LPROTO */
  34. {1, 1, 0, 0, 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} /* LUA_T_NIL */
  36. };
  37. int luaT_validevent (int t, int e) { /* ORDER LUA_T */
  38. #ifdef LUA_COMPAT_GC
  39. if (t == LUA_T_ARRAY && e == IM_GC)
  40. return 1; /* old versions allowed gc tag method for tables */
  41. #endif
  42. return (t < LUA_T_NIL) ? 1 : luaT_validevents[-t][e];
  43. }
  44. static void init_entry (lua_State *L, int tag) {
  45. int i;
  46. for (i=0; i<IM_N; i++)
  47. ttype(luaT_getim(L, tag, i)) = LUA_T_NIL;
  48. }
  49. void luaT_init (lua_State *L) {
  50. int t;
  51. L->last_tag = -(NUM_TAGS-1);
  52. luaM_growvector(L, L->IMtable, 0, NUM_TAGS, struct IM, arrEM, MAX_INT);
  53. for (t=L->last_tag; t<=0; t++)
  54. init_entry(L, t);
  55. }
  56. int lua_newtag (lua_State *L) {
  57. --L->last_tag;
  58. luaM_growvector(L, L->IMtable, -(L->last_tag), 1, struct IM, arrEM, MAX_INT);
  59. init_entry(L, L->last_tag);
  60. return L->last_tag;
  61. }
  62. static void checktag (lua_State *L, int tag) {
  63. if (!(L->last_tag <= tag && tag <= 0))
  64. luaL_verror(L, "%d is not a valid tag", tag);
  65. }
  66. void luaT_realtag (lua_State *L, int tag) {
  67. if (!(L->last_tag <= tag && tag < LUA_T_NIL))
  68. luaL_verror(L, "tag %d was not created by `newtag'", tag);
  69. }
  70. int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
  71. int e;
  72. checktag(L, tagto);
  73. checktag(L, tagfrom);
  74. for (e=0; e<IM_N; e++) {
  75. if (luaT_validevent(tagto, e))
  76. *luaT_getim(L, tagto, e) = *luaT_getim(L, tagfrom, e);
  77. }
  78. return tagto;
  79. }
  80. int luaT_effectivetag (const TObject *o) {
  81. static const int realtag[] = { /* ORDER LUA_T */
  82. LUA_T_USERDATA, LUA_T_NUMBER, LUA_T_STRING, LUA_T_ARRAY,
  83. LUA_T_LPROTO, LUA_T_CPROTO, LUA_T_NIL,
  84. LUA_T_LPROTO, LUA_T_CPROTO, /* LUA_T_LCLOSURE, LUA_T_CCLOSURE */
  85. };
  86. int t;
  87. switch (t = ttype(o)) {
  88. case LUA_T_USERDATA: {
  89. int tag = o->value.ts->u.d.tag;
  90. return (tag >= 0) ? LUA_T_USERDATA : tag; /* deprecated test */
  91. }
  92. case LUA_T_ARRAY: return o->value.a->htag;
  93. default: return realtag[-t];
  94. }
  95. }
  96. const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event) {
  97. int e;
  98. #ifdef LUA_COMPAT_ORDER_TM
  99. static const char *old_order[] = {"le", "gt", "ge", NULL};
  100. if (luaL_findstring(event, old_order) >= 0)
  101. return &luaO_nilobject;
  102. #endif
  103. e = luaI_checkevent(L, event, luaT_eventname);
  104. checktag(L, t);
  105. if (luaT_validevent(t, e))
  106. return luaT_getim(L, t,e);
  107. else
  108. return &luaO_nilobject;
  109. }
  110. void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func) {
  111. TObject temp;
  112. int e;
  113. #ifdef LUA_COMPAT_ORDER_TM
  114. static const char *old_order[] = {"le", "gt", "ge", NULL};
  115. if (luaL_findstring(event, old_order) >= 0)
  116. return; /* do nothing for old operators */
  117. #endif
  118. e = luaI_checkevent(L, event, luaT_eventname);
  119. checktag(L, t);
  120. if (!luaT_validevent(t, e))
  121. luaL_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s",
  122. luaT_eventname[e], luaO_typenames[-t],
  123. (t == LUA_T_ARRAY || t == LUA_T_USERDATA) ? " with default tag"
  124. : "");
  125. temp = *func;
  126. *func = *luaT_getim(L, t,e);
  127. *luaT_getim(L, t, e) = temp;
  128. }
  129. const char *luaT_travtagmethods (lua_State *L,
  130. int (*fn)(lua_State *, TObject *)) { /* ORDER IM */
  131. int e;
  132. for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) {
  133. int t;
  134. for (t=0; t>=L->last_tag; t--)
  135. if (fn(L, luaT_getim(L, t,e)))
  136. return luaT_eventname[e];
  137. }
  138. return NULL;
  139. }