ltm.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. ** $Id: ltm.c,v 2.38.1.1 2017/04/19 17:39:34 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. 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. "__band", "__bor", "__bxor", "__shl", "__shr",
  32. "__unm", "__bnot", "__lt", "__le",
  33. "__concat", "__call"
  34. };
  35. void luaT_init (lua_State *L) {
  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. #define N_EVENTS sizeof(luaT_eventname)/sizeof(*luaT_eventname)
  43. #define N_TYPES sizeof(luaT_typenames_)/sizeof(*luaT_typenames_)
  44. /* Access method to expose luaT_fixed strings */
  45. const char *luaT_getstr (unsigned int i) {
  46. if (i < N_EVENTS)
  47. return luaT_eventname[i];
  48. if (i < N_EVENTS + N_TYPES)
  49. return luaT_typenames_[i - N_EVENTS];
  50. return NULL;
  51. }
  52. /*
  53. ** function to be used with macro "fasttm": optimized for absence of
  54. ** tag methods
  55. */
  56. const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
  57. const TValue *tm = luaH_getshortstr(events, ename);
  58. lua_assert(event <= TM_EQ);
  59. if (ttisnil(tm)) { /* no tag method? */
  60. events->flags |= cast_byte(1u<<event); /* cache this fact */
  61. return NULL;
  62. }
  63. else return tm;
  64. }
  65. const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
  66. Table *mt;
  67. switch (ttnov(o)) {
  68. case LUA_TTABLE:
  69. mt = hvalue(o)->metatable;
  70. break;
  71. case LUA_TUSERDATA:
  72. mt = uvalue(o)->metatable;
  73. break;
  74. default:
  75. mt = G(L)->mt[ttnov(o)];
  76. }
  77. return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject);
  78. }
  79. /*
  80. ** Return the name of the type of an object. For tables and userdata
  81. ** with metatable, use their '__name' metafield, if present.
  82. */
  83. const char *luaT_objtypename (lua_State *L, const TValue *o) {
  84. Table *mt;
  85. if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) ||
  86. (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) {
  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. ptrdiff_t result = savestack(L, p3);
  96. StkId func = L->top;
  97. setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
  98. setobj2s(L, func + 1, p1); /* 1st argument */
  99. setobj2s(L, func + 2, p2); /* 2nd argument */
  100. L->top += 3;
  101. if (!hasres) /* no result? 'p3' is third argument */
  102. setobj2s(L, L->top++, p3); /* 3rd argument */
  103. /* metamethod may yield only when called from Lua code */
  104. if (isLua(L->ci))
  105. luaD_call(L, func, hasres);
  106. else
  107. luaD_callnoyield(L, func, hasres);
  108. if (hasres) { /* if has result, move it to its place */
  109. p3 = restorestack(L, result);
  110. setobjs2s(L, p3, --L->top);
  111. }
  112. }
  113. int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
  114. StkId res, TMS event) {
  115. const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
  116. if (ttisnil(tm))
  117. tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
  118. if (ttisnil(tm)) return 0;
  119. luaT_callTM(L, tm, p1, p2, res, 1);
  120. return 1;
  121. }
  122. void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
  123. StkId res, TMS event) {
  124. if (!luaT_callbinTM(L, p1, p2, res, event)) {
  125. switch (event) {
  126. case TM_CONCAT:
  127. luaG_concaterror(L, p1, p2);
  128. /* call never returns, but to avoid warnings: *//* FALLTHROUGH */
  129. case TM_BAND: case TM_BOR: case TM_BXOR:
  130. case TM_SHL: case TM_SHR: case TM_BNOT: {
  131. lua_Number dummy;
  132. if (tonumber(p1, &dummy) && tonumber(p2, &dummy))
  133. luaG_tointerror(L, p1, p2);
  134. else
  135. luaG_opinterror(L, p1, p2, "perform bitwise operation on");
  136. }
  137. /* calls never return, but to avoid warnings: *//* FALLTHROUGH */
  138. default:
  139. luaG_opinterror(L, p1, p2, "perform arithmetic on");
  140. }
  141. }
  142. }
  143. int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
  144. TMS event) {
  145. if (!luaT_callbinTM(L, p1, p2, L->top, event))
  146. return -1; /* no metamethod */
  147. else
  148. return !l_isfalse(L->top);
  149. }