ltm.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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;
  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 = 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;
  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 += 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); /* 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 (!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;
  142. if (!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2, TM_CONCAT))
  143. luaG_concaterror(L, s2v(top - 2), s2v(top - 1));
  144. }
  145. void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2,
  146. int flip, StkId res, TMS event) {
  147. if (flip)
  148. luaT_trybinTM(L, p2, p1, res, event);
  149. else
  150. luaT_trybinTM(L, p1, p2, res, event);
  151. }
  152. void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2,
  153. int flip, StkId res, TMS event) {
  154. TValue aux;
  155. setivalue(&aux, i2);
  156. luaT_trybinassocTM(L, p1, &aux, flip, res, event);
  157. }
  158. /*
  159. ** Calls an order tag method.
  160. ** For lessequal, LUA_COMPAT_LT_LE keeps compatibility with old
  161. ** behavior: if there is no '__le', try '__lt', based on l <= r iff
  162. ** !(r < l) (assuming a total order). If the metamethod yields during
  163. ** this substitution, the continuation has to know about it (to negate
  164. ** the result of r<l); bit CIST_LEQ in the call status keeps that
  165. ** information.
  166. */
  167. int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
  168. TMS event) {
  169. if (callbinTM(L, p1, p2, L->top, event)) /* try original event */
  170. return !l_isfalse(s2v(L->top));
  171. #if defined(LUA_COMPAT_LT_LE)
  172. else if (event == TM_LE) {
  173. /* try '!(p2 < p1)' for '(p1 <= p2)' */
  174. L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */
  175. if (callbinTM(L, p2, p1, L->top, TM_LT)) {
  176. L->ci->callstatus ^= CIST_LEQ; /* clear mark */
  177. return l_isfalse(s2v(L->top));
  178. }
  179. /* else error will remove this 'ci'; no need to clear mark */
  180. }
  181. #endif
  182. luaG_ordererror(L, p1, p2); /* no metamethod found */
  183. return 0; /* to avoid warnings */
  184. }
  185. int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
  186. int flip, int isfloat, TMS event) {
  187. TValue aux; const TValue *p2;
  188. if (isfloat) {
  189. setfltvalue(&aux, cast_num(v2));
  190. }
  191. else
  192. setivalue(&aux, v2);
  193. if (flip) { /* arguments were exchanged? */
  194. p2 = p1; p1 = &aux; /* correct them */
  195. }
  196. else
  197. p2 = &aux;
  198. return luaT_callorderTM(L, p1, p2, event);
  199. }
  200. void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci,
  201. const Proto *p) {
  202. int i;
  203. int actual = cast_int(L->top - ci->func) - 1; /* number of arguments */
  204. int nextra = actual - nfixparams; /* number of extra arguments */
  205. ci->u.l.nextraargs = nextra;
  206. luaD_checkstack(L, p->maxstacksize + 1);
  207. /* copy function to the top of the stack */
  208. setobjs2s(L, L->top++, ci->func);
  209. /* move fixed parameters to the top of the stack */
  210. for (i = 1; i <= nfixparams; i++) {
  211. setobjs2s(L, L->top++, ci->func + i);
  212. setnilvalue(s2v(ci->func + i)); /* erase original parameter (for GC) */
  213. }
  214. ci->func += actual + 1;
  215. ci->top += actual + 1;
  216. lua_assert(L->top <= ci->top && ci->top <= L->stack_last);
  217. }
  218. void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) {
  219. int i;
  220. int nextra = ci->u.l.nextraargs;
  221. if (wanted < 0) {
  222. wanted = nextra; /* get all extra arguments available */
  223. checkstackGCp(L, nextra, where); /* ensure stack space */
  224. L->top = where + nextra; /* next instruction will need top */
  225. }
  226. for (i = 0; i < wanted && i < nextra; i++)
  227. setobjs2s(L, where + i, ci->func - nextra + i);
  228. for (; i < wanted; i++) /* complete required results with nil */
  229. setnilvalue(s2v(where + i));
  230. }