ltm.c 4.7 KB

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