ltm.c 4.9 KB

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