ltm.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. ** $Id: ltm.c,v 2.38 2016/12/22 13:08:50 roberto Exp $
  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. {
  22. "no value",
  23. "nil", "boolean", udatatypename, "number",
  24. "string", "table", "function", udatatypename, "thread",
  25. "proto" /* this last case is used for tests only */
  26. };
  27. void luaT_init(lua_State *L)
  28. {
  29. static const char *const luaT_eventname[] = /* ORDER TM */
  30. {
  31. "__index", "__newindex",
  32. "__gc", "__mode", "__len", "__eq",
  33. "__add", "__sub", "__mul", "__mod", "__pow",
  34. "__div", "__idiv",
  35. "__band", "__bor", "__bxor", "__shl", "__shr",
  36. "__unm", "__bnot", "__lt", "__le",
  37. "__concat", "__call"
  38. };
  39. int i;
  40. for (i = 0; i < TM_N; i++)
  41. {
  42. G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
  43. luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */
  44. }
  45. }
  46. /*
  47. ** function to be used with macro "fasttm": optimized for absence of
  48. ** tag methods
  49. */
  50. const TValue *luaT_gettm(Table *events, TMS event, TString *ename)
  51. {
  52. const TValue *tm = luaH_getshortstr(events, ename);
  53. lua_assert(event <= TM_EQ);
  54. if (ttisnil(tm)) /* no tag method? */
  55. {
  56. events->flags |= cast_byte(1u << event); /* cache this fact */
  57. return NULL;
  58. }
  59. else return tm;
  60. }
  61. const TValue *luaT_gettmbyobj(lua_State *L, const TValue *o, TMS event)
  62. {
  63. Table *mt;
  64. switch (ttnov(o))
  65. {
  66. case LUA_TTABLE:
  67. mt = hvalue(o)->metatable;
  68. break;
  69. case LUA_TUSERDATA:
  70. mt = uvalue(o)->metatable;
  71. break;
  72. default:
  73. mt = G(L)->mt[ttnov(o)];
  74. }
  75. return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject);
  76. }
  77. /*
  78. ** Return the name of the type of an object. For tables and userdata
  79. ** with metatable, use their '__name' metafield, if present.
  80. */
  81. const char *luaT_objtypename(lua_State *L, const TValue *o)
  82. {
  83. Table *mt;
  84. if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) ||
  85. (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL))
  86. {
  87. const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name"));
  88. if (ttisstring(name)) /* is '__name' a string? */
  89. return getstr(tsvalue(name)); /* use it as type name */
  90. }
  91. return ttypename(ttnov(o)); /* else use standard type name */
  92. }
  93. void luaT_callTM(lua_State *L, const TValue *f, const TValue *p1,
  94. const TValue *p2, TValue *p3, int hasres)
  95. {
  96. ptrdiff_t result = savestack(L, p3);
  97. StkId func = L->top;
  98. setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
  99. setobj2s(L, func + 1, p1); /* 1st argument */
  100. setobj2s(L, func + 2, p2); /* 2nd argument */
  101. L->top += 3;
  102. if (!hasres) /* no result? 'p3' is third argument */
  103. setobj2s(L, L->top++, p3); /* 3rd argument */
  104. /* metamethod may yield only when called from Lua code */
  105. if (isLua(L->ci))
  106. luaD_call(L, func, hasres);
  107. else
  108. luaD_callnoyield(L, func, hasres);
  109. if (hasres) /* if has result, move it to its place */
  110. {
  111. p3 = restorestack(L, result);
  112. setobjs2s(L, p3, --L->top);
  113. }
  114. }
  115. int luaT_callbinTM(lua_State *L, const TValue *p1, const TValue *p2,
  116. StkId res, TMS event)
  117. {
  118. const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
  119. if (ttisnil(tm))
  120. tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
  121. if (ttisnil(tm)) return 0;
  122. luaT_callTM(L, tm, p1, p2, res, 1);
  123. return 1;
  124. }
  125. void luaT_trybinTM(lua_State *L, const TValue *p1, const TValue *p2,
  126. StkId res, TMS event)
  127. {
  128. if (!luaT_callbinTM(L, p1, p2, res, event))
  129. {
  130. switch (event)
  131. {
  132. case TM_CONCAT:
  133. luaG_concaterror(L, p1, p2);
  134. /* call never returns, but to avoid warnings: *//* FALLTHROUGH */
  135. case TM_BAND:
  136. case TM_BOR:
  137. case TM_BXOR:
  138. case TM_SHL:
  139. case TM_SHR:
  140. case TM_BNOT:
  141. {
  142. lua_Number dummy;
  143. if (tonumber(p1, &dummy) && tonumber(p2, &dummy))
  144. luaG_tointerror(L, p1, p2);
  145. else
  146. luaG_opinterror(L, p1, p2, "perform bitwise operation on");
  147. }
  148. /* calls never return, but to avoid warnings: *//* FALLTHROUGH */
  149. default:
  150. luaG_opinterror(L, p1, p2, "perform arithmetic on");
  151. }
  152. }
  153. }
  154. int luaT_callorderTM(lua_State *L, const TValue *p1, const TValue *p2,
  155. TMS event)
  156. {
  157. if (!luaT_callbinTM(L, p1, p2, L->top, event))
  158. return -1; /* no metamethod */
  159. else
  160. return !l_isfalse(L->top);
  161. }