ltm.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. ** $Id: ltm.c $
  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 "lgc.h"
  14. #include "lobject.h"
  15. #include "lstate.h"
  16. #include "lstring.h"
  17. #include "ltable.h"
  18. #include "ltm.h"
  19. #include "lvm.h"
  20. static const char udatatypename[] = "userdata";
  21. LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTYPES] = {
  22. "no value",
  23. "nil", "boolean", udatatypename, "number",
  24. "string", "table", "function", udatatypename, "thread",
  25. "upvalue", "proto" /* these last cases are used for tests only */
  26. };
  27. void luaT_init (lua_State *L) {
  28. static const char *const luaT_eventname[] = { /* ORDER TM */
  29. "__index", "__newindex",
  30. "__gc", "__mode", "__len", "__eq",
  31. "__add", "__sub", "__mul", "__mod", "__pow",
  32. "__div", "__idiv",
  33. "__band", "__bor", "__bxor", "__shl", "__shr",
  34. "__unm", "__bnot", "__lt", "__le",
  35. "__concat", "__call", "__close"
  36. };
  37. int i;
  38. for (i=0; i<TM_N; i++) {
  39. G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
  40. luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */
  41. }
  42. }
  43. /*
  44. ** function to be used with macro "fasttm": optimized for absence of
  45. ** tag methods
  46. */
  47. const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
  48. const TValue *tm = luaH_getshortstr(events, ename);
  49. lua_assert(event <= TM_EQ);
  50. if (notm(tm)) { /* no tag method? */
  51. events->flags |= cast_byte(1u<<event); /* cache this fact */
  52. return NULL;
  53. }
  54. else return tm;
  55. }
  56. const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
  57. Table *mt;
  58. switch (ttype(o)) {
  59. case LUA_TTABLE:
  60. mt = hvalue(o)->metatable;
  61. break;
  62. case LUA_TUSERDATA:
  63. mt = uvalue(o)->metatable;
  64. break;
  65. default:
  66. mt = G(L)->mt[ttype(o)];
  67. }
  68. return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue);
  69. }
  70. /*
  71. ** Return the name of the type of an object. For tables and userdata
  72. ** with metatable, use their '__name' metafield, if present.
  73. */
  74. const char *luaT_objtypename (lua_State *L, const TValue *o) {
  75. Table *mt;
  76. if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) ||
  77. (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) {
  78. const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name"));
  79. if (ttisstring(name)) /* is '__name' a string? */
  80. return getstr(tsvalue(name)); /* use it as type name */
  81. }
  82. return ttypename(ttype(o)); /* else use standard type name */
  83. }
  84. void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
  85. const TValue *p2, const TValue *p3) {
  86. StkId func = L->top.p;
  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. setobj2s(L, func + 3, p3); /* 3rd argument */
  91. L->top.p = func + 4;
  92. /* metamethod may yield only when called from Lua code */
  93. if (isLuacode(L->ci))
  94. luaD_call(L, func, 0);
  95. else
  96. luaD_callnoyield(L, func, 0);
  97. }
  98. void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
  99. const TValue *p2, StkId res) {
  100. ptrdiff_t result = savestack(L, res);
  101. StkId func = L->top.p;
  102. setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
  103. setobj2s(L, func + 1, p1); /* 1st argument */
  104. setobj2s(L, func + 2, p2); /* 2nd argument */
  105. L->top.p += 3;
  106. /* metamethod may yield only when called from Lua code */
  107. if (isLuacode(L->ci))
  108. luaD_call(L, func, 1);
  109. else
  110. luaD_callnoyield(L, func, 1);
  111. res = restorestack(L, result);
  112. setobjs2s(L, res, --L->top.p); /* move result to its place */
  113. }
  114. static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
  115. StkId res, TMS event) {
  116. const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
  117. if (notm(tm))
  118. tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
  119. if (notm(tm)) return 0;
  120. luaT_callTMres(L, tm, p1, p2, res);
  121. return 1;
  122. }
  123. void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
  124. StkId res, TMS event) {
  125. if (l_unlikely(!callbinTM(L, p1, p2, res, event))) {
  126. switch (event) {
  127. case TM_BAND: case TM_BOR: case TM_BXOR:
  128. case TM_SHL: case TM_SHR: case TM_BNOT: {
  129. if (ttisnumber(p1) && ttisnumber(p2))
  130. luaG_tointerror(L, p1, p2);
  131. else
  132. luaG_opinterror(L, p1, p2, "perform bitwise operation on");
  133. }
  134. /* calls never return, but to avoid warnings: *//* FALLTHROUGH */
  135. default:
  136. luaG_opinterror(L, p1, p2, "perform arithmetic on");
  137. }
  138. }
  139. }
  140. void luaT_tryconcatTM (lua_State *L) {
  141. StkId top = L->top.p;
  142. if (l_unlikely(!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2,
  143. TM_CONCAT)))
  144. luaG_concaterror(L, s2v(top - 2), s2v(top - 1));
  145. }
  146. void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2,
  147. int flip, StkId res, TMS event) {
  148. if (flip)
  149. luaT_trybinTM(L, p2, p1, res, event);
  150. else
  151. luaT_trybinTM(L, p1, p2, res, event);
  152. }
  153. void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2,
  154. int flip, StkId res, TMS event) {
  155. TValue aux;
  156. setivalue(&aux, i2);
  157. luaT_trybinassocTM(L, p1, &aux, flip, res, event);
  158. }
  159. /*
  160. ** Calls an order tag method.
  161. ** For lessequal, LUA_COMPAT_LT_LE keeps compatibility with old
  162. ** behavior: if there is no '__le', try '__lt', based on l <= r iff
  163. ** !(r < l) (assuming a total order). If the metamethod yields during
  164. ** this substitution, the continuation has to know about it (to negate
  165. ** the result of r<l); bit CIST_LEQ in the call status keeps that
  166. ** information.
  167. */
  168. int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
  169. TMS event) {
  170. if (callbinTM(L, p1, p2, L->top.p, event)) /* try original event */
  171. return !l_isfalse(s2v(L->top.p));
  172. #if defined(LUA_COMPAT_LT_LE)
  173. else if (event == TM_LE) {
  174. /* try '!(p2 < p1)' for '(p1 <= p2)' */
  175. L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */
  176. if (callbinTM(L, p2, p1, L->top.p, TM_LT)) {
  177. L->ci->callstatus ^= CIST_LEQ; /* clear mark */
  178. return l_isfalse(s2v(L->top.p));
  179. }
  180. /* else error will remove this 'ci'; no need to clear mark */
  181. }
  182. #endif
  183. luaG_ordererror(L, p1, p2); /* no metamethod found */
  184. return 0; /* to avoid warnings */
  185. }
  186. int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
  187. int flip, int isfloat, TMS event) {
  188. TValue aux; const TValue *p2;
  189. if (isfloat) {
  190. setfltvalue(&aux, cast_num(v2));
  191. }
  192. else
  193. setivalue(&aux, v2);
  194. if (flip) { /* arguments were exchanged? */
  195. p2 = p1; p1 = &aux; /* correct them */
  196. }
  197. else
  198. p2 = &aux;
  199. return luaT_callorderTM(L, p1, p2, event);
  200. }
  201. void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci,
  202. const Proto *p) {
  203. int i;
  204. int actual = cast_int(L->top.p - ci->func.p) - 1; /* number of arguments */
  205. int nextra = actual - nfixparams; /* number of extra arguments */
  206. ci->u.l.nextraargs = nextra;
  207. luaD_checkstack(L, p->maxstacksize + 1);
  208. /* copy function to the top of the stack */
  209. setobjs2s(L, L->top.p++, ci->func.p);
  210. /* move fixed parameters to the top of the stack */
  211. for (i = 1; i <= nfixparams; i++) {
  212. setobjs2s(L, L->top.p++, ci->func.p + i);
  213. setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */
  214. }
  215. ci->func.p += actual + 1;
  216. ci->top.p += actual + 1;
  217. lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p);
  218. }
  219. void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) {
  220. int i;
  221. int nextra = ci->u.l.nextraargs;
  222. if (wanted < 0) {
  223. wanted = nextra; /* get all extra arguments available */
  224. checkstackGCp(L, nextra, where); /* ensure stack space */
  225. L->top.p = where + nextra; /* next instruction will need top */
  226. }
  227. for (i = 0; i < wanted && i < nextra; i++)
  228. setobjs2s(L, where + i, ci->func.p - nextra + i);
  229. for (; i < wanted; i++) /* complete required results with nil */
  230. setnilvalue(s2v(where + i));
  231. }