ltm.c 4.7 KB

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