ltm.c 3.9 KB

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