ltm.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. ** $Id: ltm.c,v 1.51 2000/10/02 20:10:55 roberto Exp roberto $
  3. ** Tag methods
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "lua.h"
  9. #include "ldo.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 findevent (const char *name) {
  21. int i;
  22. for (i=0; luaT_eventname[i]; i++)
  23. if (strcmp(luaT_eventname[i], name) == 0)
  24. return i;
  25. return -1; /* name not found */
  26. }
  27. static int luaI_checkevent (lua_State *L, const char *name, int t) {
  28. int e = findevent(name);
  29. if (e >= IM_N)
  30. luaO_verror(L, "event `%.50s' is deprecated", name);
  31. if (e == IM_GC && t == TAG_TABLE)
  32. luaO_verror(L, "event `gc' for tables is deprecated");
  33. if (e < 0)
  34. luaO_verror(L, "`%.50s' is not a valid event name", name);
  35. return e;
  36. }
  37. /* events in TAG_NIL are all allowed, since this is used as a
  38. * 'placeholder' for "default" fallbacks
  39. */
  40. /* ORDER LUA_T, ORDER IM */
  41. static const char luaT_validevents[NUM_TAGS][IM_N] = {
  42. {1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* TAG_USERDATA */
  43. {1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* TAG_NUMBER */
  44. {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* TAG_STRING */
  45. {0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* TAG_TABLE */
  46. {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* TAG_LCLOSURE */
  47. {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* TAG_CCLOSURE */
  48. {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* TAG_NIL */
  49. };
  50. int luaT_validevent (int t, int e) { /* ORDER LUA_T */
  51. return (t > TAG_NIL) ? 1 : luaT_validevents[t][e];
  52. }
  53. static void init_entry (lua_State *L, int tag) {
  54. int i;
  55. for (i=0; i<IM_N; i++)
  56. ttype(luaT_getim(L, tag, i)) = TAG_NIL;
  57. L->IMtable[tag].collected = NULL;
  58. }
  59. void luaT_init (lua_State *L) {
  60. int t;
  61. luaM_growvector(L, L->IMtable, 0, NUM_TAGS, struct IM, "", MAX_INT);
  62. L->nblocks += NUM_TAGS*sizeof(struct IM);
  63. L->last_tag = NUM_TAGS-1;
  64. for (t=0; t<=L->last_tag; t++)
  65. init_entry(L, t);
  66. }
  67. int lua_newtag (lua_State *L) {
  68. luaM_growvector(L, L->IMtable, L->last_tag, 1, struct IM,
  69. "tag table overflow", MAX_INT);
  70. L->nblocks += sizeof(struct IM);
  71. L->last_tag++;
  72. init_entry(L, L->last_tag);
  73. return L->last_tag;
  74. }
  75. static void checktag (lua_State *L, int tag) {
  76. if (!(0 <= tag && tag <= L->last_tag))
  77. luaO_verror(L, "%d is not a valid tag", tag);
  78. }
  79. void luaT_realtag (lua_State *L, int tag) {
  80. if (!(NUM_TAGS <= tag && tag <= L->last_tag))
  81. luaO_verror(L, "tag %d was not created by `newtag'", tag);
  82. }
  83. int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
  84. int e;
  85. checktag(L, tagto);
  86. checktag(L, tagfrom);
  87. for (e=0; e<IM_N; e++) {
  88. if (luaT_validevent(tagto, e))
  89. *luaT_getim(L, tagto, e) = *luaT_getim(L, tagfrom, e);
  90. }
  91. return tagto;
  92. }
  93. const TObject *luaT_gettagmethods (lua_State *L, const TObject *o) {
  94. lua_Tag t = ttype(o);
  95. switch (t) {
  96. case TAG_USERDATA: {
  97. int tag = tsvalue(o)->u.d.tag;
  98. if (tag > L->last_tag)
  99. return L->IMtable[TAG_USERDATA].int_method;
  100. else
  101. return L->IMtable[tag].int_method;
  102. }
  103. case TAG_TABLE:
  104. return L->IMtable[hvalue(o)->htag].int_method;
  105. default:
  106. return L->IMtable[(int)t].int_method;;
  107. }
  108. }
  109. void lua_gettagmethod (lua_State *L, int t, const char *event) {
  110. int e;
  111. e = luaI_checkevent(L, event, t);
  112. checktag(L, t);
  113. if (luaT_validevent(t, e))
  114. *L->top = *luaT_getim(L, t,e);
  115. else
  116. ttype(L->top) = TAG_NIL;
  117. incr_top;
  118. }
  119. void lua_settagmethod (lua_State *L, int t, const char *event) {
  120. TObject temp;
  121. int e;
  122. LUA_ASSERT(lua_isnil(L, -1) || lua_isfunction(L, -1),
  123. "function or nil expected");
  124. e = luaI_checkevent(L, event, t);
  125. checktag(L, t);
  126. if (!luaT_validevent(t, e))
  127. luaO_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s",
  128. luaT_eventname[e], lua_typename(L, luaO_tag2type(t)),
  129. (t == TAG_TABLE || t == TAG_USERDATA) ? " with default tag"
  130. : "");
  131. temp = *(L->top - 1);
  132. *(L->top - 1) = *luaT_getim(L, t,e);
  133. *luaT_getim(L, t, e) = temp;
  134. }