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