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