ltm.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. ** $Id: ltm.c,v 1.26 1999/08/16 20:52:00 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, 1, 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. return (t < LUA_T_NIL) ? 1 : luaT_validevents[-t][e];
  39. }
  40. static void init_entry (int tag) {
  41. int i;
  42. for (i=0; i<IM_N; i++)
  43. ttype(luaT_getim(tag, i)) = LUA_T_NIL;
  44. }
  45. void luaT_init (void) {
  46. int t;
  47. L->last_tag = -(NUM_TAGS-1);
  48. luaM_growvector(L->IMtable, 0, NUM_TAGS, struct IM, arrEM, MAX_INT);
  49. for (t=L->last_tag; t<=0; t++)
  50. init_entry(t);
  51. }
  52. int lua_newtag (void) {
  53. --L->last_tag;
  54. luaM_growvector(L->IMtable, -(L->last_tag), 1, struct IM, arrEM, MAX_INT);
  55. init_entry(L->last_tag);
  56. return L->last_tag;
  57. }
  58. static void checktag (int tag) {
  59. if (!(L->last_tag <= tag && tag <= 0))
  60. luaL_verror("%d is not a valid tag", tag);
  61. }
  62. void luaT_realtag (int tag) {
  63. if (!(L->last_tag <= tag && tag < LUA_T_NIL))
  64. luaL_verror("tag %d was not created by `newtag'", tag);
  65. }
  66. int lua_copytagmethods (int tagto, int tagfrom) {
  67. int e;
  68. checktag(tagto);
  69. checktag(tagfrom);
  70. for (e=0; e<IM_N; e++) {
  71. if (luaT_validevent(tagto, e))
  72. *luaT_getim(tagto, e) = *luaT_getim(tagfrom, e);
  73. }
  74. return tagto;
  75. }
  76. int luaT_effectivetag (const TObject *o) {
  77. int t;
  78. switch (t = ttype(o)) {
  79. case LUA_T_ARRAY:
  80. return o->value.a->htag;
  81. case LUA_T_USERDATA: {
  82. int tag = o->value.ts->u.d.tag;
  83. return (tag >= 0) ? LUA_T_USERDATA : tag;
  84. }
  85. case LUA_T_CLOSURE:
  86. return o->value.cl->consts[0].ttype;
  87. #ifdef DEBUG
  88. case LUA_T_PMARK: case LUA_T_CMARK:
  89. case LUA_T_CLMARK: case LUA_T_LINE:
  90. LUA_INTERNALERROR("invalid type");
  91. #endif
  92. default:
  93. return t;
  94. }
  95. }
  96. const TObject *luaT_gettagmethod (int t, const char *event) {
  97. int e = luaI_checkevent(event, luaT_eventname);
  98. checktag(t);
  99. if (luaT_validevent(t, e))
  100. return luaT_getim(t,e);
  101. else
  102. return &luaO_nilobject;
  103. }
  104. void luaT_settagmethod (int t, const char *event, TObject *func) {
  105. TObject temp;
  106. int e = luaI_checkevent(event, luaT_eventname);
  107. checktag(t);
  108. if (!luaT_validevent(t, e))
  109. luaL_verror("cannot change tag method `%.20s' for type `%.20s'%.20s",
  110. luaT_eventname[e], luaO_typenames[-t],
  111. (t == LUA_T_ARRAY || t == LUA_T_USERDATA) ? " with default tag"
  112. : "");
  113. temp = *func;
  114. *func = *luaT_getim(t,e);
  115. *luaT_getim(t, e) = temp;
  116. }
  117. const char *luaT_travtagmethods (int (*fn)(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(luaT_getim(t,e)))
  123. return luaT_eventname[e];
  124. }
  125. return NULL;
  126. }