ltm.c 4.1 KB

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