rpm
4.5
|
00001 /* 00002 ** $Id: ltm.c,v 1.1 2004/03/16 21:58:30 niemeyer Exp $ 00003 ** Tag methods 00004 ** See Copyright Notice in lua.h 00005 */ 00006 00007 00008 #include <string.h> 00009 00010 #define ltm_c 00011 00012 #include "lua.h" 00013 00014 #include "lobject.h" 00015 #include "lstate.h" 00016 #include "lstring.h" 00017 #include "ltable.h" 00018 #include "ltm.h" 00019 00020 00021 00022 /*@observer@*/ 00023 const char *const luaT_typenames[] = { 00024 "nil", "boolean", "userdata", "number", 00025 "string", "table", "function", "userdata", "thread" 00026 }; 00027 00028 00029 void luaT_init (lua_State *L) { 00030 /*@observer@*/ 00031 static const char *const luaT_eventname[] = { /* ORDER TM */ 00032 "__index", "__newindex", 00033 "__gc", "__mode", "__eq", 00034 "__add", "__sub", "__mul", "__div", 00035 "__pow", "__unm", "__lt", "__le", 00036 "__concat", "__call" 00037 }; 00038 int i; 00039 for (i=0; i<TM_N; i++) { 00040 G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]); 00041 luaS_fix(G(L)->tmname[i]); /* never collect these names */ 00042 } 00043 } 00044 00045 00046 /* 00047 ** function to be used with macro "fasttm": optimized for absence of 00048 ** tag methods 00049 */ 00050 const TObject *luaT_gettm (Table *events, TMS event, TString *ename) { 00051 const TObject *tm = luaH_getstr(events, ename); 00052 lua_assert(event <= TM_EQ); 00053 if (ttisnil(tm)) { /* no tag method? */ 00054 events->flags |= cast(lu_byte, 1u<<event); /* cache this fact */ 00055 return NULL; 00056 } 00057 else return tm; 00058 } 00059 00060 00061 const TObject *luaT_gettmbyobj (lua_State *L, const TObject *o, TMS event) 00062 { 00063 TString *ename = G(L)->tmname[event]; 00064 switch (ttype(o)) { 00065 case LUA_TTABLE: 00066 return luaH_getstr(hvalue(o)->metatable, ename); 00067 case LUA_TUSERDATA: 00068 return luaH_getstr(uvalue(o)->uv.metatable, ename); 00069 default: 00070 return &luaO_nilobject; 00071 } 00072 } 00073