ltm.c revision 1.7
1/* $NetBSD: ltm.c,v 1.7 2017/04/26 12:49:34 mbalmer Exp $ */ 2 3/* 4** Id: ltm.c,v 2.38 2016/12/22 13:08:50 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 31static const char udatatypename[] = "userdata"; 32 33LUAI_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 41void 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*/ 68const 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 79const 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/* 96** Return the name of the type of an object. For tables and userdata 97** with metatable, use their '__name' metafield, if present. 98*/ 99const char *luaT_objtypename (lua_State *L, const TValue *o) { 100 Table *mt; 101 if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) || 102 (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) { 103 const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name")); 104 if (ttisstring(name)) /* is '__name' a string? */ 105 return getstr(tsvalue(name)); /* use it as type name */ 106 } 107 return ttypename(ttnov(o)); /* else use standard type name */ 108} 109 110 111void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 112 const TValue *p2, TValue *p3, int hasres) { 113 ptrdiff_t result = savestack(L, p3); 114 StkId func = L->top; 115 setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ 116 setobj2s(L, func + 1, p1); /* 1st argument */ 117 setobj2s(L, func + 2, p2); /* 2nd argument */ 118 L->top += 3; 119 if (!hasres) /* no result? 'p3' is third argument */ 120 setobj2s(L, L->top++, p3); /* 3rd argument */ 121 /* metamethod may yield only when called from Lua code */ 122 if (isLua(L->ci)) 123 luaD_call(L, func, hasres); 124 else 125 luaD_callnoyield(L, func, hasres); 126 if (hasres) { /* if has result, move it to its place */ 127 p3 = restorestack(L, result); 128 setobjs2s(L, p3, --L->top); 129 } 130} 131 132 133int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, 134 StkId res, TMS event) { 135 const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ 136 if (ttisnil(tm)) 137 tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ 138 if (ttisnil(tm)) return 0; 139 luaT_callTM(L, tm, p1, p2, res, 1); 140 return 1; 141} 142 143 144void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 145 StkId res, TMS event) { 146 if (!luaT_callbinTM(L, p1, p2, res, event)) { 147 switch (event) { 148 case TM_CONCAT: 149 luaG_concaterror(L, p1, p2); 150 /* call never returns, but to avoid warnings: *//* FALLTHROUGH */ 151 case TM_BAND: case TM_BOR: case TM_BXOR: 152 case TM_SHL: case TM_SHR: case TM_BNOT: { 153 lua_Number dummy; 154 if (tonumber(p1, &dummy) && tonumber(p2, &dummy)) 155 luaG_tointerror(L, p1, p2); 156 else 157 luaG_opinterror(L, p1, p2, "perform bitwise operation on"); 158 } 159 /* calls never return, but to avoid warnings: *//* FALLTHROUGH */ 160 default: 161 luaG_opinterror(L, p1, p2, "perform arithmetic on"); 162 } 163 } 164} 165 166 167int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, 168 TMS event) { 169 if (!luaT_callbinTM(L, p1, p2, L->top, event)) 170 return -1; /* no metamethod */ 171 else 172 return !l_isfalse(L->top); 173} 174 175