2
0

ltm.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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_Hgetshortstr(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_Hgetshortstr(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_Hgetshortstr(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. lu_byte 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. return ttypetag(s2v(res)); /* return tag of the result */
  114. }
  115. static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
  116. StkId res, TMS event) {
  117. const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
  118. if (notm(tm))
  119. tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
  120. if (notm(tm))
  121. return -1; /* tag method not found */
  122. else /* call tag method and return the tag of the result */
  123. return luaT_callTMres(L, tm, p1, p2, res);
  124. }
  125. void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
  126. StkId res, TMS event) {
  127. if (l_unlikely(callbinTM(L, p1, p2, res, event) < 0)) {
  128. switch (event) {
  129. case TM_BAND: case TM_BOR: case TM_BXOR:
  130. case TM_SHL: case TM_SHR: case TM_BNOT: {
  131. if (ttisnumber(p1) && ttisnumber(p2))
  132. luaG_tointerror(L, p1, p2);
  133. else
  134. luaG_opinterror(L, p1, p2, "perform bitwise operation on");
  135. }
  136. /* calls never return, but to avoid warnings: *//* FALLTHROUGH */
  137. default:
  138. luaG_opinterror(L, p1, p2, "perform arithmetic on");
  139. }
  140. }
  141. }
  142. /*
  143. ** The use of 'p1' after 'callbinTM' is safe because, when a tag
  144. ** method is not found, 'callbinTM' cannot change the stack.
  145. */
  146. void luaT_tryconcatTM (lua_State *L) {
  147. StkId p1 = L->top.p - 2; /* first argument */
  148. if (l_unlikely(callbinTM(L, s2v(p1), s2v(p1 + 1), p1, TM_CONCAT) < 0))
  149. luaG_concaterror(L, s2v(p1), s2v(p1 + 1));
  150. }
  151. void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2,
  152. int flip, StkId res, TMS event) {
  153. if (flip)
  154. luaT_trybinTM(L, p2, p1, res, event);
  155. else
  156. luaT_trybinTM(L, p1, p2, res, event);
  157. }
  158. void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2,
  159. int flip, StkId res, TMS event) {
  160. TValue aux;
  161. setivalue(&aux, i2);
  162. luaT_trybinassocTM(L, p1, &aux, flip, res, event);
  163. }
  164. /*
  165. ** Calls an order tag method.
  166. ** For lessequal, LUA_COMPAT_LT_LE keeps compatibility with old
  167. ** behavior: if there is no '__le', try '__lt', based on l <= r iff
  168. ** !(r < l) (assuming a total order). If the metamethod yields during
  169. ** this substitution, the continuation has to know about it (to negate
  170. ** the result of r<l); bit CIST_LEQ in the call status keeps that
  171. ** information.
  172. */
  173. int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
  174. TMS event) {
  175. int tag = callbinTM(L, p1, p2, L->top.p, event); /* try original event */
  176. if (tag >= 0) /* found tag method? */
  177. return !tagisfalse(tag);
  178. #if defined(LUA_COMPAT_LT_LE)
  179. else if (event == TM_LE) {
  180. /* try '!(p2 < p1)' for '(p1 <= p2)' */
  181. L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */
  182. tag = callbinTM(L, p2, p1, L->top.p, TM_LT);
  183. L->ci->callstatus ^= CIST_LEQ; /* clear mark */
  184. if (tag >= 0) /* found tag method? */
  185. return tagisfalse(tag);
  186. }
  187. #endif
  188. luaG_ordererror(L, p1, p2); /* no metamethod found */
  189. return 0; /* to avoid warnings */
  190. }
  191. int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
  192. int flip, int isfloat, TMS event) {
  193. TValue aux; const TValue *p2;
  194. if (isfloat) {
  195. setfltvalue(&aux, cast_num(v2));
  196. }
  197. else
  198. setivalue(&aux, v2);
  199. if (flip) { /* arguments were exchanged? */
  200. p2 = p1; p1 = &aux; /* correct them */
  201. }
  202. else
  203. p2 = &aux;
  204. return luaT_callorderTM(L, p1, p2, event);
  205. }
  206. void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci,
  207. const Proto *p) {
  208. int i;
  209. int actual = cast_int(L->top.p - ci->func.p) - 1; /* number of arguments */
  210. int nextra = actual - nfixparams; /* number of extra arguments */
  211. ci->u.l.nextraargs = nextra;
  212. luaD_checkstack(L, p->maxstacksize + 1);
  213. /* copy function to the top of the stack */
  214. setobjs2s(L, L->top.p++, ci->func.p);
  215. /* move fixed parameters to the top of the stack */
  216. for (i = 1; i <= nfixparams; i++) {
  217. setobjs2s(L, L->top.p++, ci->func.p + i);
  218. setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */
  219. }
  220. ci->func.p += actual + 1;
  221. ci->top.p += actual + 1;
  222. lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p);
  223. }
  224. void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) {
  225. int i;
  226. int nextra = ci->u.l.nextraargs;
  227. if (wanted < 0) {
  228. wanted = nextra; /* get all extra arguments available */
  229. checkstackp(L, nextra, where); /* ensure stack space */
  230. L->top.p = where + nextra; /* next instruction will need top */
  231. }
  232. for (i = 0; i < wanted && i < nextra; i++)
  233. setobjs2s(L, where + i, ci->func.p - nextra + i);
  234. for (; i < wanted; i++) /* complete required results with nil */
  235. setnilvalue(s2v(where + i));
  236. }