diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-04-12 15:48:47 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-04-12 15:48:47 -0300 |
commit | 2ca518141350667e38941eac35b76980095d2fdc (patch) | |
tree | 82595f0dfd57df5782905621298c8aae68b46389 /ltm.c | |
parent | f5ae26ec6c2eb69f03f316e59c1e1977c52c7c23 (diff) | |
download | lua-2ca518141350667e38941eac35b76980095d2fdc.tar.gz lua-2ca518141350667e38941eac35b76980095d2fdc.tar.bz2 lua-2ca518141350667e38941eac35b76980095d2fdc.zip |
Branch 5.2 - new releases for 5.2 go from here, main trunk goes
for next version
Diffstat (limited to 'ltm.c')
-rw-r--r-- | ltm.c | 77 |
1 files changed, 77 insertions, 0 deletions
@@ -0,0 +1,77 @@ | |||
1 | /* | ||
2 | ** $Id: ltm.c,v 2.14 2011/06/02 19:31:40 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 | static const char udatatypename[] = "userdata"; | ||
23 | |||
24 | LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { | ||
25 | "no value", | ||
26 | "nil", "boolean", udatatypename, "number", | ||
27 | "string", "table", "function", udatatypename, "thread", | ||
28 | "proto", "upval" /* these last two cases are used for tests only */ | ||
29 | }; | ||
30 | |||
31 | |||
32 | void luaT_init (lua_State *L) { | ||
33 | static const char *const luaT_eventname[] = { /* ORDER TM */ | ||
34 | "__index", "__newindex", | ||
35 | "__gc", "__mode", "__len", "__eq", | ||
36 | "__add", "__sub", "__mul", "__div", "__mod", | ||
37 | "__pow", "__unm", "__lt", "__le", | ||
38 | "__concat", "__call" | ||
39 | }; | ||
40 | int i; | ||
41 | for (i=0; i<TM_N; i++) { | ||
42 | G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]); | ||
43 | luaS_fix(G(L)->tmname[i]); /* never collect these names */ | ||
44 | } | ||
45 | } | ||
46 | |||
47 | |||
48 | /* | ||
49 | ** function to be used with macro "fasttm": optimized for absence of | ||
50 | ** tag methods | ||
51 | */ | ||
52 | const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { | ||
53 | const TValue *tm = luaH_getstr(events, ename); | ||
54 | lua_assert(event <= TM_EQ); | ||
55 | if (ttisnil(tm)) { /* no tag method? */ | ||
56 | events->flags |= cast_byte(1u<<event); /* cache this fact */ | ||
57 | return NULL; | ||
58 | } | ||
59 | else return tm; | ||
60 | } | ||
61 | |||
62 | |||
63 | const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) { | ||
64 | Table *mt; | ||
65 | switch (ttypenv(o)) { | ||
66 | case LUA_TTABLE: | ||
67 | mt = hvalue(o)->metatable; | ||
68 | break; | ||
69 | case LUA_TUSERDATA: | ||
70 | mt = uvalue(o)->metatable; | ||
71 | break; | ||
72 | default: | ||
73 | mt = G(L)->mt[ttypenv(o)]; | ||
74 | } | ||
75 | return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); | ||
76 | } | ||
77 | |||