1 /* $NetBSD: ltm.c,v 1.5 2016/01/28 14:41:39 lneto Exp $ */ 2 3 /* 4 ** Id: ltm.c,v 2.36 2015/11/03 15:47:30 roberto Exp 5 ** Tag methods 6 ** See Copyright Notice in lua.h 7 */ 8 9 #define ltm_c 10 #define LUA_CORE 11 12 #include "lprefix.h" 13 14 15 #ifndef _KERNEL 16 #include <string.h> 17 #endif /* _KERNEL */ 18 19 #include "lua.h" 20 21 #include "ldebug.h" 22 #include "ldo.h" 23 #include "lobject.h" 24 #include "lstate.h" 25 #include "lstring.h" 26 #include "ltable.h" 27 #include "ltm.h" 28 #include "lvm.h" 29 30 31 static const char udatatypename[] = "userdata"; 32 33 LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { 34 "no value", 35 "nil", "boolean", udatatypename, "number", 36 "string", "table", "function", udatatypename, "thread", 37 "proto" /* this last case is used for tests only */ 38 }; 39 40 41 void luaT_init (lua_State *L) { 42 static const char *const luaT_eventname[] = { /* ORDER TM */ 43 "__index", "__newindex", 44 "__gc", "__mode", "__len", "__eq", 45 #ifndef _KERNEL 46 "__add", "__sub", "__mul", "__mod", "__pow", 47 "__div", "__idiv", 48 #else /* _KERNEL */ 49 "__add", "__sub", "__mul", "__mod", 50 "__idiv", 51 #endif /* _KERNEL */ 52 "__band", "__bor", "__bxor", "__shl", "__shr", 53 "__unm", "__bnot", "__lt", "__le", 54 "__concat", "__call" 55 }; 56 int i; 57 for (i=0; i<TM_N; i++) { 58 G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]); 59 luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */ 60 } 61 } 62 63 64 /* 65 ** function to be used with macro "fasttm": optimized for absence of 66 ** tag methods 67 */ 68 const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { 69 const TValue *tm = luaH_getshortstr(events, ename); 70 lua_assert(event <= TM_EQ); 71 if (ttisnil(tm)) { /* no tag method? */ 72 events->flags |= cast_byte(1u<<event); /* cache this fact */ 73 return NULL; 74 } 75 else return tm; 76 } 77 78 79 const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) { 80 Table *mt; 81 switch (ttnov(o)) { 82 case LUA_TTABLE: 83 mt = hvalue(o)->metatable; 84 break; 85 case LUA_TUSERDATA: 86 mt = uvalue(o)->metatable; 87 break; 88 default: 89 mt = G(L)->mt[ttnov(o)]; 90 } 91 return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject); 92 } 93 94 95 void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 96 const TValue *p2, TValue *p3, int hasres) { 97 ptrdiff_t result = savestack(L, p3); 98 StkId func = L->top; 99 setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ 100 setobj2s(L, func + 1, p1); /* 1st argument */ 101 setobj2s(L, func + 2, p2); /* 2nd argument */ 102 L->top += 3; 103 if (!hasres) /* no result? 'p3' is third argument */ 104 setobj2s(L, L->top++, p3); /* 3rd argument */ 105 /* metamethod may yield only when called from Lua code */ 106 if (isLua(L->ci)) 107 luaD_call(L, func, hasres); 108 else 109 luaD_callnoyield(L, func, hasres); 110 if (hasres) { /* if has result, move it to its place */ 111 p3 = restorestack(L, result); 112 setobjs2s(L, p3, --L->top); 113 } 114 } 115 116 117 int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, 118 StkId res, TMS event) { 119 const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ 120 if (ttisnil(tm)) 121 tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ 122 if (ttisnil(tm)) return 0; 123 luaT_callTM(L, tm, p1, p2, res, 1); 124 return 1; 125 } 126 127 128 void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 129 StkId res, TMS event) { 130 if (!luaT_callbinTM(L, p1, p2, res, event)) { 131 switch (event) { 132 case TM_CONCAT: 133 luaG_concaterror(L, p1, p2); 134 /* call never returns, but to avoid warnings: *//* FALLTHROUGH */ 135 case TM_BAND: case TM_BOR: case TM_BXOR: 136 case TM_SHL: case TM_SHR: case TM_BNOT: { 137 lua_Number dummy; 138 if (tonumber(p1, &dummy) && tonumber(p2, &dummy)) 139 luaG_tointerror(L, p1, p2); 140 else 141 luaG_opinterror(L, p1, p2, "perform bitwise operation on"); 142 } 143 /* calls never return, but to avoid warnings: *//* FALLTHROUGH */ 144 default: 145 luaG_opinterror(L, p1, p2, "perform arithmetic on"); 146 } 147 } 148 } 149 150 151 int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, 152 TMS event) { 153 if (!luaT_callbinTM(L, p1, p2, L->top, event)) 154 return -1; /* no metamethod */ 155 else 156 return !l_isfalse(L->top); 157 } 158 159