ltm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. */
  167. int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
  168. TMS event) {
  169. int tag = callbinTM(L, p1, p2, L->top.p, event); /* try original event */
  170. if (tag >= 0) /* found tag method? */
  171. return !tagisfalse(tag);
  172. luaG_ordererror(L, p1, p2); /* no metamethod found */
  173. return 0; /* to avoid warnings */
  174. }
  175. int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
  176. int flip, int isfloat, TMS event) {
  177. TValue aux; const TValue *p2;
  178. if (isfloat) {
  179. setfltvalue(&aux, cast_num(v2));
  180. }
  181. else
  182. setivalue(&aux, v2);
  183. if (flip) { /* arguments were exchanged? */
  184. p2 = p1; p1 = &aux; /* correct them */
  185. }
  186. else
  187. p2 = &aux;
  188. return luaT_callorderTM(L, p1, p2, event);
  189. }
  190. /*
  191. ** Create a vararg table at the top of the stack, with 'n' elements
  192. ** starting at 'f'.
  193. */
  194. static void createvarargtab (lua_State *L, StkId f, int n) {
  195. int i;
  196. TValue key, value;
  197. Table *t = luaH_new(L);
  198. sethvalue(L, s2v(L->top.p), t);
  199. L->top.p++;
  200. luaH_resize(L, t, cast_uint(n), 1);
  201. setsvalue(L, &key, luaS_new(L, "n")); /* key is "n" */
  202. setivalue(&value, n); /* value is n */
  203. /* No need to anchor the key: Due to the resize, the next operation
  204. cannot trigger a garbage collection */
  205. luaH_set(L, t, &key, &value); /* t.n = n */
  206. for (i = 0; i < n; i++)
  207. luaH_setint(L, t, i + 1, s2v(f + i));
  208. luaC_checkGC(L);
  209. }
  210. /*
  211. ** initial stack: func arg1 ... argn extra1 ...
  212. ** ^ ci->func ^ L->top
  213. ** final stack: func nil ... nil extra1 ... func arg1 ... argn
  214. ** ^ ci->func
  215. */
  216. static void buildhiddenargs (lua_State *L, CallInfo *ci, const Proto *p,
  217. int totalargs, int nfixparams, int nextra) {
  218. int i;
  219. ci->u.l.nextraargs = nextra;
  220. luaD_checkstack(L, p->maxstacksize + 1);
  221. /* copy function to the top of the stack, after extra arguments */
  222. setobjs2s(L, L->top.p++, ci->func.p);
  223. /* move fixed parameters to after the copied function */
  224. for (i = 1; i <= nfixparams; i++) {
  225. setobjs2s(L, L->top.p++, ci->func.p + i);
  226. setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */
  227. }
  228. ci->func.p += totalargs + 1; /* 'func' now lives after hidden arguments */
  229. ci->top.p += totalargs + 1;
  230. }
  231. void luaT_adjustvarargs (lua_State *L, CallInfo *ci, const Proto *p) {
  232. int totalargs = cast_int(L->top.p - ci->func.p) - 1;
  233. int nfixparams = p->numparams;
  234. int nextra = totalargs - nfixparams; /* number of extra arguments */
  235. if (p->flag & PF_VATAB) { /* does it need a vararg table? */
  236. lua_assert(!(p->flag & PF_VAHID));
  237. createvarargtab(L, ci->func.p + nfixparams + 1, nextra);
  238. /* move table to proper place (last parameter) */
  239. setobjs2s(L, ci->func.p + nfixparams + 1, L->top.p - 1);
  240. }
  241. else { /* no table */
  242. lua_assert(p->flag & PF_VAHID);
  243. buildhiddenargs(L, ci, p, totalargs, nfixparams, nextra);
  244. /* set vararg parameter to nil */
  245. setnilvalue(s2v(ci->func.p + nfixparams + 1));
  246. lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p);
  247. }
  248. }
  249. void luaT_getvararg (CallInfo *ci, StkId ra, TValue *rc) {
  250. int nextra = ci->u.l.nextraargs;
  251. lua_Integer n;
  252. if (tointegerns(rc, &n)) { /* integral value? */
  253. if (l_castS2U(n) - 1 < cast_uint(nextra)) {
  254. StkId slot = ci->func.p - nextra + cast_int(n) - 1;
  255. setobjs2s(((lua_State*)NULL), ra, slot);
  256. return;
  257. }
  258. }
  259. else if (ttisstring(rc)) { /* string value? */
  260. size_t len;
  261. const char *s = getlstr(tsvalue(rc), len);
  262. if (len == 1 && s[0] == 'n') { /* key is "n"? */
  263. setivalue(s2v(ra), nextra);
  264. return;
  265. }
  266. }
  267. setnilvalue(s2v(ra)); /* else produce nil */
  268. }
  269. /*
  270. ** Get the number of extra arguments in a vararg function. If vararg
  271. ** table has been optimized away, that number is in the call info.
  272. ** Otherwise, get the field 'n' from the vararg table and check that it
  273. ** has a proper value (non-negative integer not larger than the stack
  274. ** limit).
  275. */
  276. static int getnumargs (lua_State *L, CallInfo *ci, Table *h) {
  277. if (h == NULL) /* no vararg table? */
  278. return ci->u.l.nextraargs;
  279. else {
  280. TValue res;
  281. if (luaH_getshortstr(h, luaS_new(L, "n"), &res) != LUA_VNUMINT ||
  282. l_castS2U(ivalue(&res)) > cast_uint(INT_MAX/2))
  283. luaG_runerror(L, "vararg table has no proper 'n'");
  284. return cast_int(ivalue(&res));
  285. }
  286. }
  287. /*
  288. ** Get 'wanted' vararg arguments and put them in 'where'. 'vatab' is
  289. ** the register of the vararg table or -1 if there is no vararg table.
  290. */
  291. void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted,
  292. int vatab) {
  293. Table *h = (vatab < 0) ? NULL : hvalue(s2v(ci->func.p + vatab + 1));
  294. int nargs = getnumargs(L, ci, h); /* number of available vararg args. */
  295. int i, touse; /* 'touse' is minimum between 'wanted' and 'nargs' */
  296. if (wanted < 0) {
  297. touse = wanted = nargs; /* get all extra arguments available */
  298. checkstackp(L, nargs, where); /* ensure stack space */
  299. L->top.p = where + nargs; /* next instruction will need top */
  300. }
  301. else
  302. touse = (nargs > wanted) ? wanted : nargs;
  303. if (h == NULL) { /* no vararg table? */
  304. for (i = 0; i < touse; i++) /* get vararg values from the stack */
  305. setobjs2s(L, where + i, ci->func.p - nargs + i);
  306. }
  307. else { /* get vararg values from vararg table */
  308. for (i = 0; i < touse; i++) {
  309. lu_byte tag = luaH_getint(h, i + 1, s2v(where + i));
  310. if (tagisempty(tag))
  311. setnilvalue(s2v(where + i));
  312. }
  313. }
  314. for (; i < wanted; i++) /* complete required results with nil */
  315. setnilvalue(s2v(where + i));
  316. }