source: trunk/src/lua/ltm.c @ 2542

Last change on this file since 2542 was 2542, checked in by sam, 10 years ago

lua: compilation fixes for non-quite-POSIX platforms.

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