ltm.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. ** $Id: ltm.c,v 2.45 2017/10/04 15:49:05 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. "upvalue", "proto" /* these last cases are 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, const TValue *p3) {
  85. StkId func = L->top;
  86. setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
  87. setobj2s(L, func + 1, p1); /* 1st argument */
  88. setobj2s(L, func + 2, p2); /* 2nd argument */
  89. setobj2s(L, func + 3, p3); /* 3rd argument */
  90. L->top += 4;
  91. /* metamethod may yield only when called from Lua code */
  92. if (isLua(L->ci))
  93. luaD_call(L, func, 0);
  94. else
  95. luaD_callnoyield(L, func, 0);
  96. }
  97. void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
  98. const TValue *p2, StkId res) {
  99. ptrdiff_t result = savestack(L, res);
  100. StkId func = L->top;
  101. setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
  102. setobj2s(L, func + 1, p1); /* 1st argument */
  103. setobj2s(L, func + 2, p2); /* 2nd argument */
  104. L->top += 3;
  105. /* metamethod may yield only when called from Lua code */
  106. if (isLua(L->ci))
  107. luaD_call(L, func, 1);
  108. else
  109. luaD_callnoyield(L, func, 1);
  110. res = restorestack(L, result);
  111. setobjs2s(L, res, --L->top); /* more result to its place */
  112. }
  113. static int 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_callTMres(L, tm, p1, p2, res);
  120. return 1;
  121. }
  122. void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
  123. StkId res, TMS event) {
  124. if (!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. void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2,
  144. int inv, StkId res, TMS event) {
  145. TValue aux; const TValue *p2;
  146. setivalue(&aux, i2);
  147. if (inv) { /* arguments were exchanged? */
  148. p2 = p1; p1 = &aux; /* correct them */
  149. }
  150. else p2 = &aux;
  151. luaT_trybinTM(L, p1, p2, res, event);
  152. }
  153. int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
  154. TMS event) {
  155. if (!callbinTM(L, p1, p2, L->top, event))
  156. return -1; /* no metamethod */
  157. else
  158. return !l_isfalse(s2v(L->top));
  159. }
  160. void luaT_adjustvarargs (lua_State *L, Proto *p, int actual) {
  161. int i;
  162. Table *vtab;
  163. TValue nname;
  164. int nfixparams = p->numparams - 1; /* number of fixed parameters */
  165. actual -= nfixparams; /* number of extra arguments */
  166. vtab = luaH_new(L); /* create vararg table */
  167. sethvalue2s(L, L->top, vtab); /* anchor it for resizing */
  168. L->top++; /* space ensured by caller */
  169. luaH_resize(L, vtab, actual, 1);
  170. for (i = 0; i < actual; i++) /* put extra arguments into vararg table */
  171. setobj2n(L, &vtab->array[i], s2v(L->top - actual + i - 1));
  172. setsvalue(L, &nname, G(L)->nfield); /* get field 'n' */
  173. setivalue(luaH_set(L, vtab, &nname), actual); /* store counter there */
  174. L->top -= actual; /* remove extra elements from the stack */
  175. sethvalue2s(L, L->top - 1, vtab); /* move table to new top */
  176. }
  177. void luaT_getvarargs (lua_State *L, TValue *t, StkId where, int wanted) {
  178. if (!ttistable(t))
  179. luaG_runerror(L, "'vararg' parameter is not a table");
  180. else {
  181. int i;
  182. Table *h = hvalue(t);
  183. if (wanted < 0) { /* get all? */
  184. const TValue *ns = luaH_getstr(h, G(L)->nfield);
  185. int n = (ttisinteger(ns)) ? ivalue(ns) : 0;
  186. wanted = n;
  187. checkstackp(L, n, where);
  188. L->top = where + n;
  189. }
  190. for (i = 0; i < wanted; i++) /* get what is available */
  191. setobj2s(L, where + i, luaH_getint(h, i + 1));
  192. return;
  193. }
  194. }