ltm.c

Go to the documentation of this file.
00001 /*
00002 ** $Id: ltm.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
00003 ** Tag methods
00004 ** See Copyright Notice in lua.h
00005 */
00006 
00007 
00008 #include <string.h>
00009 
00010 #define ltm_c
00011 #define LUA_CORE
00012 
00013 #include "lua.h"
00014 
00015 #include "lobject.h"
00016 #include "lstate.h"
00017 #include "lstring.h"
00018 #include "ltable.h"
00019 #include "ltm.h"
00020 
00021 
00022 
00023 const char *const luaT_typenames[] = {
00024   "nil", "boolean", "userdata", "number",
00025   "string", "table", "function", "userdata", "thread",
00026   "proto", "upval"
00027 };
00028 
00029 
00030 void luaT_init (lua_State *L) {
00031   static const char *const luaT_eventname[] = {  /* ORDER TM */
00032     "__index", "__newindex",
00033     "__gc", "__mode", "__eq",
00034     "__add", "__sub", "__mul", "__div", "__mod",
00035     "__pow", "__unm", "__len", "__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 TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
00051   const TValue *tm = luaH_getstr(events, ename);
00052   lua_assert(event <= TM_EQ);
00053   if (ttisnil(tm)) {  /* no tag method? */
00054     events->flags |= cast_byte(1u<<event);  /* cache this fact */
00055     return NULL;
00056   }
00057   else return tm;
00058 }
00059 
00060 
00061 const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
00062   Table *mt;
00063   switch (ttype(o)) {
00064     case LUA_TTABLE:
00065       mt = hvalue(o)->metatable;
00066       break;
00067     case LUA_TUSERDATA:
00068       mt = uvalue(o)->metatable;
00069       break;
00070     default:
00071       mt = G(L)->mt[ttype(o)];
00072   }
00073   return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject);
00074 }
00075 

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:54 2011 by Doxygen 1.6.1