ltm.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. ** $Id: ltm.c,v 1.30 1999/12/23 18:19:57 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",
  16. "sub", "mul", "div", "pow", "unm", "lt", "le", "gt", "ge",
  17. "concat", "gc", "function", NULL
  18. };
  19. static int luaI_checkevent (lua_State *L, const char *name, const char *const list[]) {
  20. int e = luaL_findstring(name, list);
  21. if (e < 0)
  22. luaL_verror(L, "`%.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. /* ORDER LUA_T, ORDER IM */
  29. static const char luaT_validevents[NUM_TAGS][IM_N] = {
  30. {1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_T_USERDATA */
  31. {1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_NUMBER */
  32. {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */
  33. {0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_T_ARRAY */
  34. {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_LPROTO */
  35. {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CPROTO */
  36. {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* LUA_T_NIL */
  37. };
  38. int luaT_validevent (int t, int e) { /* ORDER LUA_T */
  39. #ifdef LUA_COMPAT_GC
  40. if (t == LUA_T_ARRAY && e == IM_GC)
  41. return 1; /* old versions allowed gc tag method for tables */
  42. #endif
  43. return (t < LUA_T_NIL) ? 1 : luaT_validevents[-t][e];
  44. }
  45. static void init_entry (lua_State *L, int tag) {
  46. int i;
  47. for (i=0; i<IM_N; i++)
  48. ttype(luaT_getim(L, tag, i)) = LUA_T_NIL;
  49. }
  50. void luaT_init (lua_State *L) {
  51. int t;
  52. L->last_tag = -(NUM_TAGS-1);
  53. luaM_growvector(L, L->IMtable, 0, NUM_TAGS, struct IM, arrEM, MAX_INT);
  54. for (t=L->last_tag; t<=0; t++)
  55. init_entry(L, t);
  56. }
  57. int lua_newtag (lua_State *L) {
  58. --L->last_tag;
  59. luaM_growvector(L, L->IMtable, -(L->last_tag), 1, struct IM, arrEM, MAX_INT);
  60. init_entry(L, L->last_tag);
  61. return L->last_tag;
  62. }
  63. static void checktag (lua_State *L, int tag) {
  64. if (!(L->last_tag <= tag && tag <= 0))
  65. luaL_verror(L, "%d is not a valid tag", tag);
  66. }
  67. void luaT_realtag (lua_State *L, int tag) {
  68. if (!(L->last_tag <= tag && tag < LUA_T_NIL))
  69. luaL_verror(L, "tag %d was not created by `newtag'", tag);
  70. }
  71. int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
  72. int e;
  73. checktag(L, tagto);
  74. checktag(L, tagfrom);
  75. for (e=0; e<IM_N; e++) {
  76. if (luaT_validevent(tagto, e))
  77. *luaT_getim(L, tagto, e) = *luaT_getim(L, tagfrom, e);
  78. }
  79. return tagto;
  80. }
  81. int luaT_effectivetag (const TObject *o) {
  82. static const int realtag[] = { /* ORDER LUA_T */
  83. LUA_T_USERDATA, LUA_T_NUMBER, LUA_T_STRING, LUA_T_ARRAY,
  84. LUA_T_LPROTO, LUA_T_CPROTO, LUA_T_NIL,
  85. LUA_T_LPROTO, LUA_T_CPROTO, /* LUA_T_LCLOSURE, LUA_T_CCLOSURE */
  86. };
  87. int t;
  88. switch (t = ttype(o)) {
  89. case LUA_T_USERDATA: {
  90. int tag = o->value.ts->u.d.tag;
  91. return (tag >= 0) ? LUA_T_USERDATA : tag; /* deprecated test */
  92. }
  93. case LUA_T_ARRAY: return o->value.a->htag;
  94. default: return realtag[-t];
  95. }
  96. }
  97. const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event) {
  98. int e = luaI_checkevent(L, event, luaT_eventname);
  99. checktag(L, t);
  100. if (luaT_validevent(t, e))
  101. return luaT_getim(L, t,e);
  102. else
  103. return &luaO_nilobject;
  104. }
  105. void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func) {
  106. TObject temp;
  107. int e = luaI_checkevent(L, event, luaT_eventname);
  108. checktag(L, t);
  109. if (!luaT_validevent(t, e))
  110. luaL_verror(L, "cannot change tag method `%.20s' for type `%.20s'%.20s",
  111. luaT_eventname[e], luaO_typenames[-t],
  112. (t == LUA_T_ARRAY || t == LUA_T_USERDATA) ? " with default tag"
  113. : "");
  114. temp = *func;
  115. *func = *luaT_getim(L, t,e);
  116. *luaT_getim(L, t, e) = temp;
  117. }
  118. const char *luaT_travtagmethods (lua_State *L, int (*fn)(lua_State *, 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(L, luaT_getim(L, t,e)))
  124. return luaT_eventname[e];
  125. }
  126. return NULL;
  127. }