2 ** $Id: ltm.c,v 2.37 2016/02/26 19:20:15 roberto Exp $
4 ** See Copyright Notice in lua.h
27 static const char udatatypename[] = "userdata";
29 LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = {
31 "nil", "boolean", udatatypename, "number",
32 "string", "table", "function", udatatypename, "thread",
33 "proto" /* this last case is used for tests only */
37 void luaT_init (lua_State *L) {
38 static const char *const luaT_eventname[] = { /* ORDER TM */
39 "__index", "__newindex",
40 "__gc", "__mode", "__len", "__eq",
41 "__add", "__sub", "__mul", "__mod", "__pow",
43 "__band", "__bor", "__bxor", "__shl", "__shr",
44 "__unm", "__bnot", "__lt", "__le",
48 for (i=0; i<TM_N; i++) {
49 G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
50 luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */
56 ** function to be used with macro "fasttm": optimized for absence of
59 const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
60 const TValue *tm = luaH_getshortstr(events, ename);
61 lua_assert(event <= TM_EQ);
62 if (ttisnil(tm)) { /* no tag method? */
63 events->flags |= cast_byte(1u<<event); /* cache this fact */
70 const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
74 mt = hvalue(o)->metatable;
77 mt = uvalue(o)->metatable;
80 mt = G(L)->mt[ttnov(o)];
82 return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject);
87 ** Return the name of the type of an object. For tables and userdata
88 ** with metatable, use their '__name' metafield, if present.
90 const char *luaT_objtypename (lua_State *L, const TValue *o) {
92 if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) ||
93 (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) {
94 const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name"));
95 if (ttisstring(name)) /* is '__name' a string? */
96 return getstr(tsvalue(name)); /* use it as type name */
98 return ttypename(ttnov(o)); /* else use standard type name */
102 void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
103 const TValue *p2, TValue *p3, int hasres) {
104 ptrdiff_t result = savestack(L, p3);
106 setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
107 setobj2s(L, func + 1, p1); /* 1st argument */
108 setobj2s(L, func + 2, p2); /* 2nd argument */
110 if (!hasres) /* no result? 'p3' is third argument */
111 setobj2s(L, L->top++, p3); /* 3rd argument */
112 /* metamethod may yield only when called from Lua code */
114 luaD_call(L, func, hasres);
116 luaD_callnoyield(L, func, hasres);
117 if (hasres) { /* if has result, move it to its place */
118 p3 = restorestack(L, result);
119 setobjs2s(L, p3, --L->top);
124 int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
125 StkId res, TMS event) {
126 const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
128 tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
129 if (ttisnil(tm)) return 0;
130 luaT_callTM(L, tm, p1, p2, res, 1);
135 void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
136 StkId res, TMS event) {
137 if (!luaT_callbinTM(L, p1, p2, res, event)) {
140 luaG_concaterror(L, p1, p2);
141 /* call never returns, but to avoid warnings: *//* FALLTHROUGH */
142 case TM_BAND: case TM_BOR: case TM_BXOR:
143 case TM_SHL: case TM_SHR: case TM_BNOT: {
145 if (tonumber(p1, &dummy) && tonumber(p2, &dummy))
146 luaG_tointerror(L, p1, p2);
148 luaG_opinterror(L, p1, p2, "perform bitwise operation on");
150 /* calls never return, but to avoid warnings: *//* FALLTHROUGH */
152 luaG_opinterror(L, p1, p2, "perform arithmetic on");
158 int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
160 if (!luaT_callbinTM(L, p1, p2, L->top, event))
161 return -1; /* no metamethod */
163 return !l_isfalse(L->top);