ltm.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. ** $Id: ltm.c,v 1.29 1999/11/22 13:12:07 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. LUA_T_LPROTO, LUA_T_CPROTO, /* LUA_T_LCLMARK, LUA_T_CCLMARK */
  87. LUA_T_LPROTO, LUA_T_CPROTO /* LUA_T_LMARK, LUA_T_CMARK */
  88. };
  89. int t;
  90. switch (t = ttype(o)) {
  91. case LUA_T_USERDATA: {
  92. int tag = o->value.ts->u.d.tag;
  93. return (tag >= 0) ? LUA_T_USERDATA : tag; /* deprecated test */
  94. }
  95. case LUA_T_ARRAY: return o->value.a->htag;
  96. default: return realtag[-t];
  97. }
  98. }
  99. const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event) {
  100. int e = luaI_checkevent(L, event, luaT_eventname);
  101. checktag(L, t);
  102. if (luaT_validevent(t, e))
  103. return luaT_getim(L, t,e);
  104. else
  105. return &luaO_nilobject;
  106. }
  107. void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func) {
  108. TObject temp;
  109. int e = luaI_checkevent(L, event, luaT_eventname);
  110. checktag(L, t);
  111. if (!luaT_validevent(t, e))
  112. luaL_verror(L, "cannot change tag method `%.20s' for type `%.20s'%.20s",
  113. luaT_eventname[e], luaO_typenames[-t],
  114. (t == LUA_T_ARRAY || t == LUA_T_USERDATA) ? " with default tag"
  115. : "");
  116. temp = *func;
  117. *func = *luaT_getim(L, t,e);
  118. *luaT_getim(L, t, e) = temp;
  119. }
  120. const char *luaT_travtagmethods (lua_State *L, int (*fn)(lua_State *, TObject *)) { /* ORDER IM */
  121. int e;
  122. for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) {
  123. int t;
  124. for (t=0; t>=L->last_tag; t--)
  125. if (fn(L, luaT_getim(L, t,e)))
  126. return luaT_eventname[e];
  127. }
  128. return NULL;
  129. }