ltm.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. ** $Id: ltm.c,v 2.17 2013/04/25 16:07:52 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 "ldo.h"
  11. #include "lobject.h"
  12. #include "lstate.h"
  13. #include "lstring.h"
  14. #include "ltable.h"
  15. #include "ltm.h"
  16. #include "lvm.h"
  17. static const char udatatypename[] = "userdata";
  18. LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = {
  19. "no value",
  20. "nil", "boolean", udatatypename, "number",
  21. "string", "table", "function", udatatypename, "thread",
  22. "proto", "upval" /* these last two cases are used for tests only */
  23. };
  24. void luaT_init (lua_State *L) {
  25. static const char *const luaT_eventname[] = { /* ORDER TM */
  26. "__index", "__newindex",
  27. "__gc", "__mode", "__len", "__eq",
  28. "__add", "__sub", "__mul", "__div", "__idiv", "__mod",
  29. "__pow", "__unm", "__lt", "__le",
  30. "__concat", "__call"
  31. };
  32. int i;
  33. for (i=0; i<TM_N; i++) {
  34. G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
  35. luaS_fix(G(L)->tmname[i]); /* never collect these names */
  36. }
  37. }
  38. /*
  39. ** function to be used with macro "fasttm": optimized for absence of
  40. ** tag methods
  41. */
  42. const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
  43. const TValue *tm = luaH_getstr(events, ename);
  44. lua_assert(event <= TM_EQ);
  45. if (ttisnil(tm)) { /* no tag method? */
  46. events->flags |= cast_byte(1u<<event); /* cache this fact */
  47. return NULL;
  48. }
  49. else return tm;
  50. }
  51. const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
  52. Table *mt;
  53. switch (ttnov(o)) {
  54. case LUA_TTABLE:
  55. mt = hvalue(o)->metatable;
  56. break;
  57. case LUA_TUSERDATA:
  58. mt = uvalue(o)->metatable;
  59. break;
  60. default:
  61. mt = G(L)->mt[ttnov(o)];
  62. }
  63. return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject);
  64. }
  65. void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
  66. const TValue *p2, TValue *p3, int hasres) {
  67. ptrdiff_t result = savestack(L, p3);
  68. setobj2s(L, L->top++, f); /* push function */
  69. setobj2s(L, L->top++, p1); /* 1st argument */
  70. setobj2s(L, L->top++, p2); /* 2nd argument */
  71. if (!hasres) /* no result? 'p3' is third argument */
  72. setobj2s(L, L->top++, p3); /* 3rd argument */
  73. /* metamethod may yield only when called from Lua code */
  74. luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci));
  75. if (hasres) { /* if has result, move it to its place */
  76. p3 = restorestack(L, result);
  77. setobjs2s(L, p3, --L->top);
  78. }
  79. }
  80. int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
  81. StkId res, TMS event) {
  82. const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
  83. if (ttisnil(tm))
  84. tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
  85. if (ttisnil(tm)) return 0;
  86. luaT_callTM(L, tm, p1, p2, res, 1);
  87. return 1;
  88. }
  89. const TValue *luaT_getequalTM (lua_State *L, Table *mt1, Table *mt2) {
  90. const TValue *tm1 = fasttm(L, mt1, TM_EQ);
  91. const TValue *tm2;
  92. if (tm1 == NULL) return NULL; /* no metamethod */
  93. if (mt1 == mt2) return tm1; /* same metatables => same metamethods */
  94. tm2 = fasttm(L, mt2, TM_EQ);
  95. if (tm2 == NULL) return NULL; /* no metamethod */
  96. if (luaV_rawequalobj(tm1, tm2)) /* same metamethods? */
  97. return tm1;
  98. return NULL;
  99. }
  100. int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
  101. TMS event) {
  102. if (!luaT_callbinTM(L, p1, p2, L->top, event))
  103. return -1; /* no metamethod */
  104. else
  105. return !l_isfalse(L->top);
  106. }