ltm.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. ** $Id: ltm.c,v 2.28 2014/07/18 12:17:54 roberto Exp roberto $
  3. ** Tag methods
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <string.h>
  7. #define ltm_c
  8. #define LUA_CORE
  9. #include "lua.h"
  10. #include "ldebug.h"
  11. #include "ldo.h"
  12. #include "lobject.h"
  13. #include "lstate.h"
  14. #include "lstring.h"
  15. #include "ltable.h"
  16. #include "ltm.h"
  17. #include "lvm.h"
  18. static const char udatatypename[] = "userdata";
  19. LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = {
  20. "no value",
  21. "nil", "boolean", udatatypename, "number",
  22. "string", "table", "function", udatatypename, "thread",
  23. "proto" /* this last case is used for tests only */
  24. };
  25. void luaT_init (lua_State *L) {
  26. static const char *const luaT_eventname[] = { /* ORDER TM */
  27. "__index", "__newindex",
  28. "__gc", "__mode", "__len", "__eq",
  29. "__add", "__sub", "__mul", "__mod", "__pow",
  30. "__div", "__idiv",
  31. "__band", "__bor", "__bxor", "__shl", "__shr",
  32. "__unm", "__bnot", "__lt", "__le",
  33. "__concat", "__call"
  34. };
  35. int i;
  36. for (i=0; i<TM_N; i++) {
  37. G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
  38. luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */
  39. }
  40. }
  41. /*
  42. ** function to be used with macro "fasttm": optimized for absence of
  43. ** tag methods
  44. */
  45. const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
  46. const TValue *tm = luaH_getstr(events, ename);
  47. lua_assert(event <= TM_EQ);
  48. if (ttisnil(tm)) { /* no tag method? */
  49. events->flags |= cast_byte(1u<<event); /* cache this fact */
  50. return NULL;
  51. }
  52. else return tm;
  53. }
  54. const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
  55. Table *mt;
  56. switch (ttnov(o)) {
  57. case LUA_TTABLE:
  58. mt = hvalue(o)->metatable;
  59. break;
  60. case LUA_TUSERDATA:
  61. mt = uvalue(o)->metatable;
  62. break;
  63. default:
  64. mt = G(L)->mt[ttnov(o)];
  65. }
  66. return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject);
  67. }
  68. void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
  69. const TValue *p2, TValue *p3, int hasres) {
  70. ptrdiff_t result = savestack(L, p3);
  71. setobj2s(L, L->top++, f); /* push function */
  72. setobj2s(L, L->top++, p1); /* 1st argument */
  73. setobj2s(L, L->top++, p2); /* 2nd argument */
  74. if (!hasres) /* no result? 'p3' is third argument */
  75. setobj2s(L, L->top++, p3); /* 3rd argument */
  76. /* metamethod may yield only when called from Lua code */
  77. luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci));
  78. if (hasres) { /* if has result, move it to its place */
  79. p3 = restorestack(L, result);
  80. setobjs2s(L, p3, --L->top);
  81. }
  82. }
  83. int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
  84. StkId res, TMS event) {
  85. const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
  86. if (ttisnil(tm))
  87. tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
  88. if (ttisnil(tm)) return 0;
  89. luaT_callTM(L, tm, p1, p2, res, 1);
  90. return 1;
  91. }
  92. void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
  93. StkId res, TMS event) {
  94. if (!luaT_callbinTM(L, p1, p2, res, event)) {
  95. switch (event) {
  96. case TM_CONCAT:
  97. luaG_concaterror(L, p1, p2);
  98. case TM_IDIV: case TM_BAND: case TM_BOR: case TM_BXOR:
  99. case TM_SHL: case TM_SHR: case TM_BNOT: {
  100. lua_Number dummy;
  101. if (tonumber(p1, &dummy) && tonumber(p2, &dummy))
  102. luaG_tointerror(L, p1, p2);
  103. /* else go through */
  104. }
  105. default:
  106. luaG_aritherror(L, p1, p2);
  107. }
  108. }
  109. }
  110. int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
  111. TMS event) {
  112. if (!luaT_callbinTM(L, p1, p2, L->top, event))
  113. return -1; /* no metamethod */
  114. else
  115. return !l_isfalse(L->top);
  116. }