ltm.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. ** $Id: ltm.c,v 1.28 1999/10/04 17:51:04 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",
  16. "sub", "mul", "div", "pow", "unm", "lt", "le", "gt", "ge",
  17. "concat", "gc", "function", NULL
  18. };
  19. static int luaI_checkevent (lua_State *L, const char *name, const char *const list[]) {
  20. int e = luaL_findstring(name, list);
  21. if (e < 0)
  22. luaL_verror(L, "`%.50s' is not a valid event name", name);
  23. return e;
  24. }
  25. /* events in LUA_T_NIL are all allowed, since this is used as a
  26. * 'placeholder' for "default" fallbacks
  27. */
  28. /* ORDER LUA_T, ORDER IM */
  29. static const char luaT_validevents[NUM_TAGS][IM_N] = {
  30. {1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_T_USERDATA */
  31. {1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_NUMBER */
  32. {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */
  33. {0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_T_ARRAY */
  34. {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_PROTO */
  35. {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CPROTO */
  36. {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* LUA_T_NIL */
  37. };
  38. int luaT_validevent (int t, int e) { /* ORDER LUA_T */
  39. #ifdef LUA_COMPAT_GC
  40. if (t == LUA_T_ARRAY && e == IM_GC)
  41. return 1; /* old versions allowed gc tag method for tables */
  42. #endif
  43. return (t < LUA_T_NIL) ? 1 : luaT_validevents[-t][e];
  44. }
  45. static void init_entry (lua_State *L, int tag) {
  46. int i;
  47. for (i=0; i<IM_N; i++)
  48. ttype(luaT_getim(L, tag, i)) = LUA_T_NIL;
  49. }
  50. void luaT_init (lua_State *L) {
  51. int t;
  52. L->last_tag = -(NUM_TAGS-1);
  53. luaM_growvector(L, L->IMtable, 0, NUM_TAGS, struct IM, arrEM, MAX_INT);
  54. for (t=L->last_tag; t<=0; t++)
  55. init_entry(L, t);
  56. }
  57. int lua_newtag (lua_State *L) {
  58. --L->last_tag;
  59. luaM_growvector(L, L->IMtable, -(L->last_tag), 1, struct IM, arrEM, MAX_INT);
  60. init_entry(L, L->last_tag);
  61. return L->last_tag;
  62. }
  63. static void checktag (lua_State *L, int tag) {
  64. if (!(L->last_tag <= tag && tag <= 0))
  65. luaL_verror(L, "%d is not a valid tag", tag);
  66. }
  67. void luaT_realtag (lua_State *L, int tag) {
  68. if (!(L->last_tag <= tag && tag < LUA_T_NIL))
  69. luaL_verror(L, "tag %d was not created by `newtag'", tag);
  70. }
  71. int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
  72. int e;
  73. checktag(L, tagto);
  74. checktag(L, tagfrom);
  75. for (e=0; e<IM_N; e++) {
  76. if (luaT_validevent(tagto, e))
  77. *luaT_getim(L, tagto, e) = *luaT_getim(L, tagfrom, e);
  78. }
  79. return tagto;
  80. }
  81. int luaT_effectivetag (const TObject *o) {
  82. int t;
  83. switch (t = ttype(o)) {
  84. case LUA_T_ARRAY:
  85. return o->value.a->htag;
  86. case LUA_T_USERDATA: {
  87. int tag = o->value.ts->u.d.tag;
  88. return (tag >= 0) ? LUA_T_USERDATA : tag;
  89. }
  90. case LUA_T_CLOSURE:
  91. return o->value.cl->consts[0].ttype;
  92. #ifdef DEBUG
  93. case LUA_T_PMARK: case LUA_T_CMARK:
  94. case LUA_T_CLMARK: case LUA_T_LINE:
  95. LUA_INTERNALERROR(L, "invalid type");
  96. #endif
  97. default:
  98. return t;
  99. }
  100. }
  101. const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event) {
  102. int e = luaI_checkevent(L, event, luaT_eventname);
  103. checktag(L, t);
  104. if (luaT_validevent(t, e))
  105. return luaT_getim(L, t,e);
  106. else
  107. return &luaO_nilobject;
  108. }
  109. void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func) {
  110. TObject temp;
  111. int e = luaI_checkevent(L, event, luaT_eventname);
  112. checktag(L, t);
  113. if (!luaT_validevent(t, e))
  114. luaL_verror(L, "cannot change tag method `%.20s' for type `%.20s'%.20s",
  115. luaT_eventname[e], luaO_typenames[-t],
  116. (t == LUA_T_ARRAY || t == LUA_T_USERDATA) ? " with default tag"
  117. : "");
  118. temp = *func;
  119. *func = *luaT_getim(L, t,e);
  120. *luaT_getim(L, t, e) = temp;
  121. }
  122. const char *luaT_travtagmethods (lua_State *L, int (*fn)(lua_State *, TObject *)) { /* ORDER IM */
  123. int e;
  124. for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) {
  125. int t;
  126. for (t=0; t>=L->last_tag; t--)
  127. if (fn(L, luaT_getim(L, t,e)))
  128. return luaT_eventname[e];
  129. }
  130. return NULL;
  131. }