ltm.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. "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_getshortstr(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_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject);
  68. }
  69. /*
  70. ** Return the name of the type of an object. For tables and userdata
  71. ** with metatable, use their '__name' metafield, if present.
  72. */
  73. const char *luaT_objtypename (lua_State *L, const TValue *o) {
  74. Table *mt;
  75. if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) ||
  76. (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) {
  77. const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name"));
  78. if (ttisstring(name)) /* is '__name' a string? */
  79. return getstr(tsvalue(name)); /* use it as type name */
  80. }
  81. return ttypename(ttnov(o)); /* else use standard type name */
  82. }
  83. void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
  84. const TValue *p2, TValue *p3, int hasres) {
  85. ptrdiff_t result = savestack(L, p3);
  86. StkId func = L->top;
  87. setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
  88. setobj2s(L, func + 1, p1); /* 1st argument */
  89. setobj2s(L, func + 2, p2); /* 2nd argument */
  90. L->top += 3;
  91. if (!hasres) /* no result? 'p3' is third argument */
  92. setobj2s(L, L->top++, p3); /* 3rd argument */
  93. /* metamethod may yield only when called from Lua code */
  94. if (isLua(L->ci))
  95. luaD_call(L, func, hasres);
  96. else
  97. luaD_callnoyield(L, func, hasres);
  98. if (hasres) { /* if has result, move it to its place */
  99. p3 = restorestack(L, result);
  100. setobjs2s(L, p3, --L->top);
  101. }
  102. }
  103. int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
  104. StkId res, TMS event) {
  105. const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
  106. if (ttisnil(tm))
  107. tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
  108. if (ttisnil(tm)) return 0;
  109. luaT_callTM(L, tm, p1, p2, res, 1);
  110. return 1;
  111. }
  112. void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
  113. StkId res, TMS event) {
  114. if (!luaT_callbinTM(L, p1, p2, res, event)) {
  115. switch (event) {
  116. case TM_CONCAT:
  117. luaG_concaterror(L, p1, p2);
  118. /* call never returns, but to avoid warnings: *//* FALLTHROUGH */
  119. case TM_BAND: case TM_BOR: case TM_BXOR:
  120. case TM_SHL: case TM_SHR: case TM_BNOT: {
  121. lua_Number dummy;
  122. if (tonumber(p1, &dummy) && tonumber(p2, &dummy))
  123. luaG_tointerror(L, p1, p2);
  124. else
  125. luaG_opinterror(L, p1, p2, "perform bitwise operation on");
  126. }
  127. /* calls never return, but to avoid warnings: *//* FALLTHROUGH */
  128. default:
  129. luaG_opinterror(L, p1, p2, "perform arithmetic on");
  130. }
  131. }
  132. }
  133. int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
  134. TMS event) {
  135. if (!luaT_callbinTM(L, p1, p2, L->top, event))
  136. return -1; /* no metamethod */
  137. else
  138. return !l_isfalse(L->top);
  139. }