Go to the documentation of this file.00001
00002
00003
00004
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"
00029 };
00030
00031
00032 void luaT_init (lua_State *L) {
00033 static const char *const luaT_eventname[] = {
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]);
00044 }
00045 }
00046
00047
00048
00049
00050
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)) {
00056 events->flags |= cast_byte(1u<<event);
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