ltm.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. ** $Id: ltm.c,v 1.42 2000/06/08 17:48:31 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_REENTRANT
  9. #include "lua.h"
  10. #include "lauxlib.h"
  11. #include "lmem.h"
  12. #include "lobject.h"
  13. #include "lstate.h"
  14. #include "ltm.h"
  15. const char *const luaT_eventname[] = { /* ORDER IM */
  16. "gettable", "settable", "index", "getglobal", "setglobal", "add", "sub",
  17. "mul", "div", "pow", "unm", "lt", "concat", "gc", "function",
  18. "le", "gt", "ge", /* deprecated options!! */
  19. NULL
  20. };
  21. static int luaI_checkevent (lua_State *L, const char *name, int t) {
  22. int e = luaL_findstring(name, luaT_eventname);
  23. if (e >= IM_N)
  24. luaL_verror(L, "event `%.50s' is deprecated", name);
  25. if (e == IM_GC && t == TAG_TABLE)
  26. luaL_verror(L, "event `gc' for tables is deprecated");
  27. if (e < 0)
  28. luaL_verror(L, "`%.50s' is not a valid event name", name);
  29. return e;
  30. }
  31. /* events in TAG_NIL are all allowed, since this is used as a
  32. * 'placeholder' for "default" fallbacks
  33. */
  34. /* ORDER LUA_T, ORDER IM */
  35. static const char luaT_validevents[NUM_TAGS][IM_N] = {
  36. {1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* TAG_USERDATA */
  37. {1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* TAG_NUMBER */
  38. {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* TAG_STRING */
  39. {0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* TAG_TABLE */
  40. {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* TAG_LCLOSURE */
  41. {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* TAG_CCLOSURE */
  42. {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* TAG_NIL */
  43. };
  44. int luaT_validevent (int t, int e) { /* ORDER LUA_T */
  45. return (t > TAG_NIL) ? 1 : luaT_validevents[t][e];
  46. }
  47. static void init_entry (lua_State *L, int tag) {
  48. int i;
  49. for (i=0; i<IM_N; i++)
  50. ttype(luaT_getim(L, tag, i)) = TAG_NIL;
  51. L->IMtable[tag].collected = NULL;
  52. }
  53. void luaT_init (lua_State *L) {
  54. int t;
  55. L->last_tag = NUM_TAGS-1;
  56. luaM_growvector(L, L->IMtable, 0, NUM_TAGS, struct IM, "", MAX_INT);
  57. for (t=0; t<=L->last_tag; t++)
  58. init_entry(L, t);
  59. }
  60. int lua_newtag (lua_State *L) {
  61. ++L->last_tag;
  62. luaM_growvector(L, L->IMtable, L->last_tag, 1, struct IM,
  63. "tag table overflow", MAX_INT);
  64. init_entry(L, L->last_tag);
  65. return L->last_tag;
  66. }
  67. static void checktag (lua_State *L, int tag) {
  68. if (!(0 <= tag && tag <= L->last_tag))
  69. luaL_verror(L, "%d is not a valid tag", tag);
  70. }
  71. void luaT_realtag (lua_State *L, int tag) {
  72. if (!(NUM_TAGS <= tag && tag <= L->last_tag))
  73. luaL_verror(L, "tag %d was not created by `newtag'", tag);
  74. }
  75. int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
  76. int e;
  77. checktag(L, tagto);
  78. checktag(L, tagfrom);
  79. for (e=0; e<IM_N; e++) {
  80. if (luaT_validevent(tagto, e))
  81. *luaT_getim(L, tagto, e) = *luaT_getim(L, tagfrom, e);
  82. }
  83. return tagto;
  84. }
  85. int luaT_effectivetag (lua_State *L, const TObject *o) {
  86. lua_Type t = ttype(o);
  87. switch (t) {
  88. case TAG_USERDATA: {
  89. int tag = tsvalue(o)->u.d.tag;
  90. return (tag > L->last_tag) ? TAG_USERDATA : tag; /* deprecated test */
  91. }
  92. case TAG_TABLE: return hvalue(o)->htag;
  93. default: return t;
  94. }
  95. }
  96. const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event) {
  97. int e;
  98. e = luaI_checkevent(L, event, t);
  99. checktag(L, t);
  100. if (luaT_validevent(t, e))
  101. return luaT_getim(L, t,e);
  102. else
  103. return &luaO_nilobject;
  104. }
  105. void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func) {
  106. TObject temp;
  107. int e;
  108. e = luaI_checkevent(L, event, t);
  109. checktag(L, t);
  110. if (!luaT_validevent(t, e))
  111. luaL_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s",
  112. luaT_eventname[e], luaO_typenames[t],
  113. (t == TAG_TABLE || t == TAG_USERDATA) ? " with default tag"
  114. : "");
  115. temp = *func;
  116. *func = *luaT_getim(L, t,e);
  117. *luaT_getim(L, t, e) = temp;
  118. }
  119. const char *luaT_travtagmethods (lua_State *L,
  120. int (*fn)(lua_State *, TObject *)) { /* ORDER IM */
  121. int e;
  122. for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) {
  123. int t;
  124. for (t=0; t<=L->last_tag; t++)
  125. if (fn(L, luaT_getim(L, t,e)))
  126. return luaT_eventname[e];
  127. }
  128. return NULL;
  129. }