1/* $NetBSD$ */ 2 3/* 4** Id: ltm.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp 5** Tag methods 6** See Copyright Notice in lua.h 7*/ 8 9 10#include <string.h> 11 12#define ltm_c 13#define LUA_CORE 14 15#include "lua.h" 16 17#include "lobject.h" 18#include "lstate.h" 19#include "lstring.h" 20#include "ltable.h" 21#include "ltm.h" 22 23 24 25const char *const luaT_typenames[] = { 26 "nil", "boolean", "userdata", "number", 27 "string", "table", "function", "userdata", "thread", 28 "proto", "upval" 29}; 30 31 32void luaT_init (lua_State *L) { 33 static const char *const luaT_eventname[] = { /* ORDER TM */ 34 "__index", "__newindex", 35 "__gc", "__mode", "__eq", 36 "__add", "__sub", "__mul", "__div", "__mod", 37 "__pow", "__unm", "__len", "__lt", "__le", 38 "__concat", "__call" 39 }; 40 int i; 41 for (i=0; i<TM_N; i++) { 42 G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]); 43 luaS_fix(G(L)->tmname[i]); /* never collect these names */ 44 } 45} 46 47 48/* 49** function to be used with macro "fasttm": optimized for absence of 50** tag methods 51*/ 52const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { 53 const TValue *tm = luaH_getstr(events, ename); 54 lua_assert(event <= TM_EQ); 55 if (ttisnil(tm)) { /* no tag method? */ 56 events->flags |= cast_byte(1u<<event); /* cache this fact */ 57 return NULL; 58 } 59 else return tm; 60} 61 62 63const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) { 64 Table *mt; 65 switch (ttype(o)) { 66 case LUA_TTABLE: 67 mt = hvalue(o)->metatable; 68 break; 69 case LUA_TUSERDATA: 70 mt = uvalue(o)->metatable; 71 break; 72 default: 73 mt = G(L)->mt[ttype(o)]; 74 } 75 return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); 76} 77 78