lua/ltm.c

Go to the documentation of this file.
00001 /*
00002 ** $Id: ltm.c,v 2.14 2011/06/02 19:31:40 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 static const char udatatypename[] = "userdata";
00023 
00024 LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = {
00025   "no value",
00026   "nil", "boolean", udatatypename, "number",
00027   "string", "table", "function", udatatypename, "thread",
00028   "proto", "upval"  /* these last two cases are used for tests only */
00029 };
00030 
00031 
00032 void luaT_init (lua_State *L) {
00033   static const char *const luaT_eventname[] = {  /* ORDER TM */
00034     "__index", "__newindex",
00035     "__gc", "__mode", "__len", "__eq",
00036     "__add", "__sub", "__mul", "__div", "__mod",
00037     "__pow", "__unm", "__lt", "__le",
00038     "__concat", "__call"
00039   };
00040   int i;
00041   for (i=0; i<TM_N; i++) {
00042     G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
00043     luaS_fix(G(L)->tmname[i]);  /* never collect these names */
00044   }
00045 }
00046 
00047 
00048 /*
00049 ** function to be used with macro "fasttm": optimized for absence of
00050 ** tag methods
00051 */
00052 const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
00053   const TValue *tm = luaH_getstr(events, ename);
00054   lua_assert(event <= TM_EQ);
00055   if (ttisnil(tm)) {  /* no tag method? */
00056     events->flags |= cast_byte(1u<<event);  /* cache this fact */
00057     return NULL;
00058   }
00059   else return tm;
00060 }
00061 
00062 
00063 const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
00064   Table *mt;
00065   switch (ttypenv(o)) {
00066     case LUA_TTABLE:
00067       mt = hvalue(o)->metatable;
00068       break;
00069     case LUA_TUSERDATA:
00070       mt = uvalue(o)->metatable;
00071       break;
00072     default:
00073       mt = G(L)->mt[ttypenv(o)];
00074   }
00075   return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject);
00076 }
00077 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:04 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs