ltm.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. ** $Id: ltm.c,v 2.54 2017/12/19 16:40:17 roberto Exp roberto $
  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 = (isLuacode(L->ci)) ? L->ci->top : 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 = func + 4;
  91. /* metamethod may yield only when called from Lua code */
  92. if (isLuacode(L->ci))
  93. luaD_call(L, func, 0);
  94. else
  95. luaD_callnoyield(L, func, 0);
  96. }
  97. static void reallycallTMres (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 (isLuacode(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); /* move result to its place */
  112. }
  113. void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
  114. const TValue *p2, StkId res) {
  115. if (isLuacode(L->ci))
  116. L->top = L->ci->top; /* prepare top */
  117. reallycallTMres(L, f, p1, p2, res);
  118. }
  119. static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
  120. StkId res, TMS event) {
  121. const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
  122. if (ttisnil(tm))
  123. tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
  124. if (ttisnil(tm)) return 0;
  125. reallycallTMres(L, tm, p1, p2, res);
  126. return 1;
  127. }
  128. void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
  129. StkId res, TMS event) {
  130. if (event != TM_CONCAT && isLuacode(L->ci))
  131. L->top = L->ci->top; /* prepare top */
  132. if (!callbinTM(L, p1, p2, res, event)) {
  133. switch (event) {
  134. case TM_CONCAT:
  135. luaG_concaterror(L, p1, p2);
  136. /* call never returns, but to avoid warnings: *//* FALLTHROUGH */
  137. case TM_BAND: case TM_BOR: case TM_BXOR:
  138. case TM_SHL: case TM_SHR: case TM_BNOT: {
  139. if (ttisnumber(p1) && ttisnumber(p2))
  140. luaG_tointerror(L, p1, p2);
  141. else
  142. luaG_opinterror(L, p1, p2, "perform bitwise operation on");
  143. }
  144. /* calls never return, but to avoid warnings: *//* FALLTHROUGH */
  145. default:
  146. luaG_opinterror(L, p1, p2, "perform arithmetic on");
  147. }
  148. }
  149. }
  150. void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2,
  151. StkId res, int inv, TMS event) {
  152. if (inv)
  153. luaT_trybinTM(L, p2, p1, res, event);
  154. else
  155. luaT_trybinTM(L, p1, p2, res, event);
  156. }
  157. void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2,
  158. int inv, StkId res, TMS event) {
  159. TValue aux;
  160. setivalue(&aux, i2);
  161. luaT_trybinassocTM(L, p1, &aux, res, inv, event);
  162. }
  163. int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
  164. TMS event) {
  165. if (isLuacode(L->ci))
  166. L->top = L->ci->top; /* prepare top */
  167. if (callbinTM(L, p1, p2, L->top, event)) /* try original event */
  168. return !l_isfalse(s2v(L->top));
  169. else if (event == TM_LE) {
  170. /* try '!(p2 < p1)' for '(p1 <= p2)' */
  171. L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */
  172. if (callbinTM(L, p2, p1, L->top, TM_LT)) {
  173. L->ci->callstatus ^= CIST_LEQ; /* clear mark */
  174. return l_isfalse(s2v(L->top));
  175. }
  176. /* else error will remove this 'ci'; no need to clear mark */
  177. }
  178. luaG_ordererror(L, p1, p2); /* no metamethod found */
  179. return 0; /* to avoid warnings */
  180. }
  181. int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
  182. int inv, TMS event) {
  183. TValue aux; const TValue *p2;
  184. setivalue(&aux, v2);
  185. if (inv) { /* arguments were exchanged? */
  186. p2 = p1; p1 = &aux; /* correct them */
  187. event = (event == TM_LE) ? TM_LT : TM_LE;
  188. }
  189. else
  190. p2 = &aux;
  191. return (luaT_callorderTM(L, p1, p2, event) != inv);
  192. }
  193. void luaT_adjustvarargs (lua_State *L, Proto *p, int actual) {
  194. int i;
  195. Table *vtab;
  196. TValue nname;
  197. int nfixparams = p->numparams; /* number of fixed parameters */
  198. actual -= nfixparams; /* number of extra arguments */
  199. vtab = luaH_new(L); /* create vararg table */
  200. sethvalue2s(L, L->top, vtab); /* anchor it for resizing */
  201. L->top++; /* space ensured by caller */
  202. luaH_resize(L, vtab, actual, 1);
  203. for (i = 0; i < actual; i++) /* put extra arguments into vararg table */
  204. setobj2n(L, &vtab->array[i], s2v(L->top - actual + i - 1));
  205. setsvalue(L, &nname, G(L)->nfield); /* get field 'n' */
  206. setivalue(luaH_set(L, vtab, &nname), actual); /* store counter there */
  207. L->top -= actual; /* remove extra elements from the stack */
  208. sethvalue2s(L, L->top - 1, vtab); /* move table to new top */
  209. }
  210. void luaT_getvarargs (lua_State *L, TValue *t, StkId where, int wanted) {
  211. if (!ttistable(t))
  212. luaG_runerror(L, "'vararg' parameter is not a table");
  213. else {
  214. int i;
  215. Table *h = hvalue(t);
  216. if (wanted < 0) { /* get all? */
  217. const TValue *ns = luaH_getstr(h, G(L)->nfield);
  218. int n = (ttisinteger(ns)) ? cast_int(ivalue(ns)) : 0;
  219. wanted = n;
  220. checkstackp(L, n, where);
  221. L->top = where + n;
  222. }
  223. for (i = 0; i < wanted; i++) /* get what is available */
  224. setobj2s(L, where + i, luaH_getint(h, i + 1));
  225. return;
  226. }
  227. }