2
0

ltm.c 4.1 KB

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