ltm.c 4.1 KB

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