ltm.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. ** $Id: ltm.c,v 1.80 2001/10/11 21:41:21 roberto Exp $
  3. ** Tag methods
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "lua.h"
  9. #include "ldo.h"
  10. #include "lmem.h"
  11. #include "lobject.h"
  12. #include "lstate.h"
  13. #include "lstring.h"
  14. #include "ltable.h"
  15. #include "ltm.h"
  16. const char *const luaT_eventname[] = { /* ORDER TM */
  17. "gettable", "settable", "index", "getglobal",
  18. "setglobal", "add", "sub", "mul", "div",
  19. "pow", "unm", "lt", "concat", "gc",
  20. "function",
  21. NULL
  22. };
  23. static int findevent (const char *name) {
  24. int i;
  25. for (i=0; luaT_eventname[i]; i++)
  26. if (strcmp(luaT_eventname[i], name) == 0)
  27. return i;
  28. return -1; /* name not found */
  29. }
  30. static int luaI_checkevent (lua_State *L, const char *name) {
  31. int e = findevent(name);
  32. if (e < 0)
  33. luaO_verror(L, "`%.50s' is not a valid event name", name);
  34. return e;
  35. }
  36. /* events in LUA_TNIL are all allowed, since this is used as a
  37. * `placeholder' for default fallbacks
  38. */
  39. /* ORDER LUA_T, ORDER TM */
  40. static const lu_byte luaT_validevents[NUM_TAGS][TM_N] = {
  41. {1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_TUSERDATA */
  42. {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_TNIL */
  43. {1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* LUA_TNUMBER */
  44. {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_TSTRING */
  45. {0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_TTABLE */
  46. {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0} /* LUA_TFUNCTION */
  47. };
  48. static int luaT_validevent (int t, int e) { /* ORDER LUA_T */
  49. return (t >= NUM_TAGS) ? 1 : cast(int, luaT_validevents[t][e]);
  50. }
  51. void luaT_init (lua_State *L) {
  52. static const char *const typenames[NUM_TAGS] = {
  53. "userdata", "nil", "number", "string",
  54. "table", "function"
  55. };
  56. int i;
  57. for (i=0; i<NUM_TAGS; i++)
  58. luaT_newtag(L, typenames[i], i);
  59. }
  60. int luaT_newtag (lua_State *L, const char *name, int basictype) {
  61. int tag;
  62. int i;
  63. TString *ts = NULL;
  64. luaM_growvector(L, G(L)->TMtable, G(L)->ntag, G(L)->sizeTM, struct TM,
  65. MAX_INT, "tag table overflow");
  66. tag = G(L)->ntag;
  67. if (name) {
  68. const TObject *v;
  69. TObject otag;
  70. ts = luaS_new(L, name);
  71. v = luaH_getstr(G(L)->type2tag, ts);
  72. if (ttype(v) == LUA_TNUMBER) return cast(int, nvalue(v));
  73. setnvalue(&otag, tag);
  74. luaH_setstr(L, G(L)->type2tag, ts, &otag);
  75. }
  76. for (i=0; i<TM_N; i++)
  77. luaT_gettm(G(L), tag, i) = NULL;
  78. G(L)->TMtable[tag].collected = NULL;
  79. G(L)->TMtable[tag].name = ts;
  80. G(L)->TMtable[tag].basictype = basictype;
  81. G(L)->ntag++;
  82. return tag;
  83. }
  84. static void checktag (lua_State *L, int tag) {
  85. if (!(0 <= tag && tag < G(L)->ntag))
  86. luaO_verror(L, "%d is not a valid tag", tag);
  87. }
  88. int luaT_tag (const TObject *o) {
  89. int t = ttype(o);
  90. switch (t) {
  91. case LUA_TUSERDATA: return uvalue(o)->uv.tag;
  92. case LUA_TTABLE: return hvalue(o)->htag;
  93. default: return t;
  94. }
  95. }
  96. const char *luaT_typename (global_State *G, const TObject *o) {
  97. int t = ttype(o);
  98. int tag;
  99. TString *ts;
  100. switch (t) {
  101. case LUA_TUSERDATA:
  102. tag = uvalue(o)->uv.tag;
  103. break;
  104. case LUA_TTABLE:
  105. tag = hvalue(o)->htag;
  106. break;
  107. default:
  108. tag = t;
  109. }
  110. ts = G->TMtable[tag].name;
  111. if (ts == NULL)
  112. ts = G->TMtable[t].name;
  113. return getstr(ts);
  114. }
  115. LUA_API void lua_gettagmethod (lua_State *L, int t, const char *event) {
  116. int e;
  117. lua_lock(L);
  118. e = luaI_checkevent(L, event);
  119. checktag(L, t);
  120. if (luaT_validevent(t, e) && luaT_gettm(G(L), t, e)) {
  121. setclvalue(L->top, luaT_gettm(G(L), t, e));
  122. }
  123. else
  124. setnilvalue(L->top);
  125. incr_top;
  126. lua_unlock(L);
  127. }
  128. LUA_API void lua_settagmethod (lua_State *L, int t, const char *event) {
  129. int e;
  130. lua_lock(L);
  131. e = luaI_checkevent(L, event);
  132. checktag(L, t);
  133. if (!luaT_validevent(t, e))
  134. luaO_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s",
  135. luaT_eventname[e], typenamebytag(G(L), t),
  136. (t == LUA_TTABLE || t == LUA_TUSERDATA) ?
  137. " with default tag" : "");
  138. switch (ttype(L->top - 1)) {
  139. case LUA_TNIL:
  140. luaT_gettm(G(L), t, e) = NULL;
  141. break;
  142. case LUA_TFUNCTION:
  143. luaT_gettm(G(L), t, e) = clvalue(L->top - 1);
  144. break;
  145. default:
  146. luaD_error(L, "tag method must be a function (or nil)");
  147. }
  148. L->top--;
  149. lua_unlock(L);
  150. }