ltm.c 3.9 KB

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