ltm.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. ** $Id: ltm.c,v 1.73 2001/06/15 20:36: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_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"),
  58. l_s("table"), l_s("function")
  59. };
  60. int i;
  61. for (i=0; i<NUM_TAGS; i++)
  62. luaT_newtag(L, typenames[i], i);
  63. }
  64. int luaT_newtag (lua_State *L, const l_char *name, int basictype) {
  65. int tag;
  66. int i;
  67. TString *ts;
  68. luaM_growvector(L, G(L)->TMtable, G(L)->ntag, G(L)->sizeTM, struct TM,
  69. MAX_INT, l_s("tag table overflow"));
  70. tag = G(L)->ntag;
  71. if (name == NULL)
  72. ts = NULL;
  73. else {
  74. TObject *v;
  75. ts = luaS_new(L, name);
  76. v = luaH_setstr(L, G(L)->type2tag, ts);
  77. if (ttype(v) != LUA_TNIL) return LUA_TNONE; /* invalid name */
  78. setnvalue(v, tag);
  79. }
  80. for (i=0; i<TM_N; i++)
  81. luaT_gettm(G(L), tag, i) = NULL;
  82. G(L)->TMtable[tag].collected = NULL;
  83. G(L)->TMtable[tag].name = ts;
  84. G(L)->TMtable[tag].basictype = basictype;
  85. G(L)->ntag++;
  86. return tag;
  87. }
  88. static void checktag (lua_State *L, int tag) {
  89. if (!(0 <= tag && tag < G(L)->ntag))
  90. luaO_verror(L, l_s("%d is not a valid tag"), tag);
  91. }
  92. LUA_API int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
  93. int e;
  94. lua_lock(L);
  95. checktag(L, tagto);
  96. checktag(L, tagfrom);
  97. for (e=0; e<TM_N; e++) {
  98. if (luaT_validevent(tagto, e))
  99. luaT_gettm(G(L), tagto, e) = luaT_gettm(G(L), tagfrom, e);
  100. }
  101. lua_unlock(L);
  102. return tagto;
  103. }
  104. int luaT_tag (const TObject *o) {
  105. int t = ttype(o);
  106. switch (t) {
  107. case LUA_TUSERDATA: return uvalue(o)->uv.tag;
  108. case LUA_TTABLE: return hvalue(o)->htag;
  109. default: return t;
  110. }
  111. }
  112. const l_char *luaT_typename (global_State *G, const TObject *o) {
  113. int t = ttype(o);
  114. int tag;
  115. TString *ts;
  116. switch (t) {
  117. case LUA_TUSERDATA:
  118. tag = uvalue(o)->uv.tag;
  119. break;
  120. case LUA_TTABLE:
  121. tag = hvalue(o)->htag;
  122. break;
  123. default:
  124. tag = t;
  125. }
  126. ts = G->TMtable[tag].name;
  127. if (ts == NULL)
  128. ts = G->TMtable[t].name;
  129. return getstr(ts);
  130. }
  131. LUA_API void lua_gettagmethod (lua_State *L, int t, const l_char *event) {
  132. int e;
  133. lua_lock(L);
  134. e = luaI_checkevent(L, event, t);
  135. checktag(L, t);
  136. if (luaT_validevent(t, e) && luaT_gettm(G(L), t, e)) {
  137. setclvalue(L->top, luaT_gettm(G(L), t, e));
  138. }
  139. else
  140. setnilvalue(L->top);
  141. incr_top;
  142. lua_unlock(L);
  143. }
  144. LUA_API void lua_settagmethod (lua_State *L, int t, const l_char *event) {
  145. int e;
  146. lua_lock(L);
  147. e = luaI_checkevent(L, event, t);
  148. checktag(L, t);
  149. if (!luaT_validevent(t, e))
  150. luaO_verror(L, l_s("cannot change `%.20s' tag method for type `%.20s'%.20s"),
  151. luaT_eventname[e], typenamebytag(G(L), t),
  152. (t == LUA_TTABLE || t == LUA_TUSERDATA) ?
  153. l_s(" with default tag") : l_s(""));
  154. switch (ttype(L->top - 1)) {
  155. case LUA_TNIL:
  156. luaT_gettm(G(L), t, e) = NULL;
  157. break;
  158. case LUA_TFUNCTION:
  159. luaT_gettm(G(L), t, e) = clvalue(L->top - 1);
  160. break;
  161. default:
  162. luaD_error(L, l_s("tag method must be a function (or nil)"));
  163. }
  164. L->top--;
  165. lua_unlock(L);
  166. }