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