The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ltm.cpp
Go to the documentation of this file.
1 /*
2 ** Tag methods
3 ** See Copyright Notice in lua.h
4 */
5 
6 
7 #include <string.h>
8 
9 #define ltm_c
10 #define LUA_CORE
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 static const char udatatypename[] = "userdata";
22 
23 LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = {
24  "no value",
25  "nil", "boolean", udatatypename, "number",
26  "string", "table", "function", udatatypename, "thread",
27  "proto", "upval" /* these last two cases are used for tests only */
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", "__len", "__eq",
35  "__add", "__sub", "__mul", "__div", "__mod",
36  "__pow", "__unm", "__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 */
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 (ttypenv(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[ttypenv(o)];
73  }
74  return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject);
75 }
76 
Definition: lobject.h:559
#define LUA_TUSERDATA
Definition: lua.h:84
const TValue * luaH_getstr(Table *t, TString *key)
Definition: ltable.cpp:465
#define luaS_fix(s)
Definition: lstring.h:21
#define ttisnil(o)
Definition: lobject.h:133
#define cast_byte(i)
Definition: llimits.h:94
#define G(L)
Definition: lstate.h:178
#define uvalue(o)
Definition: lobject.h:158
TString * luaS_new(lua_State *L, const char *str)
Definition: lstring.cpp:169
#define luaO_nilobject
Definition: lobject.h:587
#define ttypenv(o)
Definition: lobject.h:126
static const char udatatypename[]
Definition: ltm.cpp:21
#define LUAI_DDEF
Definition: luaconf.h:189
void luaT_init(lua_State *L)
Definition: ltm.cpp:31
Definition: ltm.h:35
#define LUA_TOTALTAGS
Definition: lobject.h:28
const TValue * luaT_gettmbyobj(lua_State *L, const TValue *o, TMS event)
Definition: ltm.cpp:62
#define lua_assert(c)
Definition: llimits.h:65
size_t i
Definition: function.cpp:1057
lu_byte flags
Definition: lobject.h:561
Definition: ltm.h:23
Handling of system events.
Definition: manager.hpp:42
TMS
Definition: ltm.h:17
cl_event event
Definition: glew.h:3070
const TValue * luaT_gettm(Table *events, TMS event, TString *ename)
Definition: ltm.cpp:51
#define LUA_TTABLE
Definition: lua.h:82
LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS]
Definition: ltm.cpp:23
#define hvalue(o)
Definition: lobject.h:163