ltm.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. ** $Id: ltm.c,v 1.38 2000/03/29 20:19:20 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, arrEM, 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, arrEM, MAX_INT);
  61. init_entry(L, L->last_tag);
  62. return L->last_tag;
  63. }
  64. static void checktag (lua_State *L, int tag) {
  65. if (!(0 <= tag && tag <= L->last_tag))
  66. luaL_verror(L, "%d is not a valid tag", tag);
  67. }
  68. void luaT_realtag (lua_State *L, int tag) {
  69. if (!(NUM_TAGS <= tag && tag <= L->last_tag))
  70. luaL_verror(L, "tag %d was not created by `newtag'", tag);
  71. }
  72. int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
  73. int e;
  74. checktag(L, tagto);
  75. checktag(L, tagfrom);
  76. for (e=0; e<IM_N; e++) {
  77. if (luaT_validevent(tagto, e))
  78. *luaT_getim(L, tagto, e) = *luaT_getim(L, tagfrom, e);
  79. }
  80. return tagto;
  81. }
  82. int luaT_effectivetag (lua_State *L, const TObject *o) {
  83. lua_Type t = ttype(o);
  84. switch (t) {
  85. case TAG_USERDATA: {
  86. int tag = o->value.ts->u.d.tag;
  87. return (tag > L->last_tag) ? TAG_USERDATA : tag; /* deprecated test */
  88. }
  89. case TAG_TABLE: return o->value.a->htag;
  90. default: return t;
  91. }
  92. }
  93. const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event) {
  94. int e;
  95. e = luaI_checkevent(L, event, t);
  96. checktag(L, t);
  97. if (luaT_validevent(t, e))
  98. return luaT_getim(L, t,e);
  99. else
  100. return &luaO_nilobject;
  101. }
  102. void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func) {
  103. TObject temp;
  104. int e;
  105. e = luaI_checkevent(L, event, t);
  106. checktag(L, t);
  107. if (!luaT_validevent(t, e))
  108. luaL_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s",
  109. luaT_eventname[e], luaO_typenames[t],
  110. (t == TAG_TABLE || t == TAG_USERDATA) ? " with default tag"
  111. : "");
  112. temp = *func;
  113. *func = *luaT_getim(L, t,e);
  114. *luaT_getim(L, t, e) = temp;
  115. }
  116. const char *luaT_travtagmethods (lua_State *L,
  117. int (*fn)(lua_State *, TObject *)) { /* ORDER IM */
  118. int e;
  119. for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) {
  120. int t;
  121. for (t=0; t<=L->last_tag; t++)
  122. if (fn(L, luaT_getim(L, t,e)))
  123. return luaT_eventname[e];
  124. }
  125. return NULL;
  126. }