ltm.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. ** $Id: ltm.c,v 1.46 2000/08/09 19:16:57 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 "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. L->IMtable[tag].collected = NULL;
  51. }
  52. void luaT_init (lua_State *L) {
  53. int t;
  54. luaM_growvector(L, L->IMtable, 0, NUM_TAGS, struct IM, "", MAX_INT);
  55. L->last_tag = NUM_TAGS-1;
  56. for (t=0; t<=L->last_tag; t++)
  57. init_entry(L, t);
  58. }
  59. int lua_newtag (lua_State *L) {
  60. luaM_growvector(L, L->IMtable, L->last_tag, 1, struct IM,
  61. "tag table overflow", MAX_INT);
  62. L->last_tag++;
  63. init_entry(L, L->last_tag);
  64. return L->last_tag;
  65. }
  66. static void checktag (lua_State *L, int tag) {
  67. if (!(0 <= tag && tag <= L->last_tag))
  68. luaL_verror(L, "%d is not a valid tag", tag);
  69. }
  70. void luaT_realtag (lua_State *L, int tag) {
  71. if (!(NUM_TAGS <= tag && tag <= L->last_tag))
  72. luaL_verror(L, "tag %d was not created by `newtag'", tag);
  73. }
  74. int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
  75. int e;
  76. checktag(L, tagto);
  77. checktag(L, tagfrom);
  78. for (e=0; e<IM_N; e++) {
  79. if (luaT_validevent(tagto, e))
  80. *luaT_getim(L, tagto, e) = *luaT_getim(L, tagfrom, e);
  81. }
  82. return tagto;
  83. }
  84. int luaT_effectivetag (lua_State *L, const TObject *o) {
  85. lua_Type t = ttype(o);
  86. switch (t) {
  87. case TAG_USERDATA: {
  88. int tag = tsvalue(o)->u.d.tag;
  89. return (tag > L->last_tag) ? TAG_USERDATA : tag; /* deprecated test */
  90. }
  91. case TAG_TABLE: return hvalue(o)->htag;
  92. default: return t;
  93. }
  94. }
  95. void lua_gettagmethod (lua_State *L, int t, const char *event) {
  96. int e;
  97. e = luaI_checkevent(L, event, t);
  98. checktag(L, t);
  99. if (luaT_validevent(t, e))
  100. *L->top = *luaT_getim(L, t,e);
  101. else
  102. ttype(L->top) = TAG_NIL;
  103. L->top++;
  104. }
  105. void lua_settagmethod (lua_State *L, int t, const char *event) {
  106. TObject temp;
  107. int e;
  108. LUA_ASSERT(lua_isnil(L, -1) || lua_isfunction(L, -1),
  109. "function or nil expected");
  110. e = luaI_checkevent(L, event, t);
  111. checktag(L, t);
  112. if (!luaT_validevent(t, e))
  113. luaL_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s",
  114. luaT_eventname[e], luaO_typenames[t],
  115. (t == TAG_TABLE || t == TAG_USERDATA) ? " with default tag"
  116. : "");
  117. temp = *(L->top - 1);
  118. *(L->top - 1) = *luaT_getim(L, t,e);
  119. *luaT_getim(L, t, e) = temp;
  120. }