ltm.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. ** $Id: ltm.c,v 2.19 2013/04/29 16:56:50 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", "upval" /* these last two cases are 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", "__div", "__idiv", "__mod",
  30. "__pow", "__unm", "__lt", "__le",
  31. "__concat", "__call"
  32. };
  33. int i;
  34. for (i=0; i<TM_N; i++) {
  35. G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
  36. luaS_fix(G(L)->tmname[i]); /* never collect these names */
  37. }
  38. }
  39. /*
  40. ** function to be used with macro "fasttm": optimized for absence of
  41. ** tag methods
  42. */
  43. const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
  44. const TValue *tm = luaH_getstr(events, ename);
  45. lua_assert(event <= TM_EQ);
  46. if (ttisnil(tm)) { /* no tag method? */
  47. events->flags |= cast_byte(1u<<event); /* cache this fact */
  48. return NULL;
  49. }
  50. else return tm;
  51. }
  52. const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
  53. Table *mt;
  54. switch (ttnov(o)) {
  55. case LUA_TTABLE:
  56. mt = hvalue(o)->metatable;
  57. break;
  58. case LUA_TUSERDATA:
  59. mt = uvalue(o)->metatable;
  60. break;
  61. default:
  62. mt = G(L)->mt[ttnov(o)];
  63. }
  64. return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject);
  65. }
  66. void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
  67. const TValue *p2, TValue *p3, int hasres) {
  68. ptrdiff_t result = savestack(L, p3);
  69. setobj2s(L, L->top++, f); /* push function */
  70. setobj2s(L, L->top++, p1); /* 1st argument */
  71. setobj2s(L, L->top++, p2); /* 2nd argument */
  72. if (!hasres) /* no result? 'p3' is third argument */
  73. setobj2s(L, L->top++, p3); /* 3rd argument */
  74. /* metamethod may yield only when called from Lua code */
  75. luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci));
  76. if (hasres) { /* if has result, move it to its place */
  77. p3 = restorestack(L, result);
  78. setobjs2s(L, p3, --L->top);
  79. }
  80. }
  81. int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
  82. StkId res, TMS event) {
  83. const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
  84. if (ttisnil(tm))
  85. tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
  86. if (ttisnil(tm)) return 0;
  87. luaT_callTM(L, tm, p1, p2, res, 1);
  88. return 1;
  89. }
  90. void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
  91. StkId res, TMS event) {
  92. if (!luaT_callbinTM(L, p1, p2, res, event)) {
  93. if (event == TM_CONCAT)
  94. luaG_concaterror(L, p1, p2);
  95. else if (event == TM_IDIV && ttisnumber(p1) && ttisnumber(p2))
  96. luaG_tointerror(L, p1, p2);
  97. else
  98. luaG_aritherror(L, p1, p2);
  99. }
  100. }
  101. const TValue *luaT_getequalTM (lua_State *L, Table *mt1, Table *mt2) {
  102. const TValue *tm1 = fasttm(L, mt1, TM_EQ);
  103. const TValue *tm2;
  104. if (tm1 == NULL) return NULL; /* no metamethod */
  105. if (mt1 == mt2) return tm1; /* same metatables => same metamethods */
  106. tm2 = fasttm(L, mt2, TM_EQ);
  107. if (tm2 == NULL) return NULL; /* no metamethod */
  108. if (luaV_rawequalobj(tm1, tm2)) /* same metamethods? */
  109. return tm1;
  110. return NULL;
  111. }
  112. int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
  113. TMS event) {
  114. if (!luaT_callbinTM(L, p1, p2, L->top, event))
  115. return -1; /* no metamethod */
  116. else
  117. return !l_isfalse(L->top);
  118. }