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