The Battle for Wesnoth  1.15.1+dev
ltm.cpp
Go to the documentation of this file.
1 /*
2 ** $Id: ltm.c,v 2.38.1.1 2017/04/19 17:39:34 roberto Exp $
3 ** Tag methods
4 ** See Copyright Notice in lua.h
5 */
6 
7 #define ltm_c
8 #define LUA_CORE
9 
10 #include "lprefix.h"
11 
12 
13 #include <string.h>
14 
15 #include "lua.h"
16 
17 #include "ldebug.h"
18 #include "ldo.h"
19 #include "lobject.h"
20 #include "lstate.h"
21 #include "lstring.h"
22 #include "ltable.h"
23 #include "ltm.h"
24 #include "lvm.h"
25 
26 
27 static const char udatatypename[] = "userdata";
28 
29 LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = {
30  "no value",
31  "nil", "boolean", udatatypename, "number",
32  "string", "table", "function", udatatypename, "thread",
33  "proto" /* this last case is used for tests only */
34 };
35 
36 
37 void luaT_init (lua_State *L) {
38  static const char *const luaT_eventname[] = { /* ORDER TM */
39  "__index", "__newindex",
40  "__gc", "__mode", "__len", "__eq",
41  "__add", "__sub", "__mul", "__mod", "__pow",
42  "__div", "__idiv",
43  "__band", "__bor", "__bxor", "__shl", "__shr",
44  "__unm", "__bnot", "__lt", "__le",
45  "__concat", "__call"
46  };
47  int i;
48  for (i=0; i<TM_N; i++) {
49  G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
50  luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */
51  }
52 }
53 
54 
55 /*
56 ** function to be used with macro "fasttm": optimized for absence of
57 ** tag methods
58 */
59 const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
60  const TValue *tm = luaH_getshortstr(events, ename);
61  lua_assert(event <= TM_EQ);
62  if (ttisnil(tm)) { /* no tag method? */
63  events->flags |= cast_byte(1u<<event); /* cache this fact */
64  return NULL;
65  }
66  else return tm;
67 }
68 
69 
70 const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
71  Table *mt;
72  switch (ttnov(o)) {
73  case LUA_TTABLE:
74  mt = hvalue(o)->metatable;
75  break;
76  case LUA_TUSERDATA:
77  mt = uvalue(o)->metatable;
78  break;
79  default:
80  mt = G(L)->mt[ttnov(o)];
81  }
82  return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject);
83 }
84 
85 
86 /*
87 ** Return the name of the type of an object. For tables and userdata
88 ** with metatable, use their '__name' metafield, if present.
89 */
90 const char *luaT_objtypename (lua_State *L, const TValue *o) {
91  Table *mt;
92  if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) ||
93  (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) {
94  const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name"));
95  if (ttisstring(name)) /* is '__name' a string? */
96  return getstr(tsvalue(name)); /* use it as type name */
97  }
98  return ttypename(ttnov(o)); /* else use standard type name */
99 }
100 
101 
102 void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
103  const TValue *p2, TValue *p3, int hasres) {
104  ptrdiff_t result = savestack(L, p3);
105  StkId func = L->top;
106  setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
107  setobj2s(L, func + 1, p1); /* 1st argument */
108  setobj2s(L, func + 2, p2); /* 2nd argument */
109  L->top += 3;
110  if (!hasres) /* no result? 'p3' is third argument */
111  setobj2s(L, L->top++, p3); /* 3rd argument */
112  /* metamethod may yield only when called from Lua code */
113  if (isLua(L->ci))
114  luaD_call(L, func, hasres);
115  else
116  luaD_callnoyield(L, func, hasres);
117  if (hasres) { /* if has result, move it to its place */
118  p3 = restorestack(L, result);
119  setobjs2s(L, p3, --L->top);
120  }
121 }
122 
123 
124 int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
125  StkId res, TMS event) {
126  const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
127  if (ttisnil(tm))
128  tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
129  if (ttisnil(tm)) return 0;
130  luaT_callTM(L, tm, p1, p2, res, 1);
131  return 1;
132 }
133 
134 
135 void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
136  StkId res, TMS event) {
137  if (!luaT_callbinTM(L, p1, p2, res, event)) {
138  switch (event) {
139  case TM_CONCAT:
140  luaG_concaterror(L, p1, p2);
141  /* call never returns, but to avoid warnings: *//* FALLTHROUGH */
142  case TM_BAND: case TM_BOR: case TM_BXOR:
143  case TM_SHL: case TM_SHR: case TM_BNOT: {
145  if (tonumber(p1, &dummy) && tonumber(p2, &dummy))
146  luaG_tointerror(L, p1, p2);
147  else
148  luaG_opinterror(L, p1, p2, "perform bitwise operation on");
149  }
150  /* calls never return, but to avoid warnings: *//* FALLTHROUGH */
151  default:
152  luaG_opinterror(L, p1, p2, "perform arithmetic on");
153  }
154  }
155 }
156 
157 
158 int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
159  TMS event) {
160  if (!luaT_callbinTM(L, p1, p2, L->top, event))
161  return -1; /* no metamethod */
162  else
163  return !l_isfalse(L->top);
164 }
165 
Definition: ltm.h:41
#define ttistable(o)
Definition: lobject.h:151
Definition: ltm.h:34
Definition: lobject.h:497
#define obj2gco(v)
Definition: lstate.h:238
int dummy
Definition: lstrlib.cpp:1125
#define LUA_TUSERDATA
Definition: lua.h:71
#define setobjs2s
Definition: lobject.h:269
#define ttisstring(o)
Definition: lobject.h:148
#define ttisfulluserdata(o)
Definition: lobject.h:157
Definition: ltm.h:33
Definition: ltm.h:35
void luaT_trybinTM(lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event)
Definition: ltm.cpp:135
#define ttisnil(o)
Definition: lobject.h:145
#define cast_byte(i)
Definition: llimits.h:114
#define G(L)
Definition: lstate.h:205
#define setobj2s
Definition: lobject.h:271
#define uvalue(o)
Definition: lobject.h:170
Definition: ltm.h:38
const TValue * luaH_getshortstr(Table *t, TString *key)
Definition: ltable.cpp:539
TString * luaS_new(lua_State *L, const char *str)
Definition: lstring.cpp:219
#define luaO_nilobject
Definition: lobject.h:525
StkId top
Definition: lstate.h:182
int luaT_callbinTM(lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event)
Definition: ltm.cpp:124
#define restorestack(L, n)
Definition: ldo.h:33
static const char udatatypename[]
Definition: ltm.cpp:27
static const char * name(const std::vector< SDL_Joystick *> &joysticks, const std::size_t index)
Definition: joystick.cpp:48
#define getstr(ts)
Definition: lobject.h:328
#define tonumber(o, n)
Definition: lvm.h:40
CallInfo * ci
Definition: lstate.h:184
#define LUAI_DDEF
Definition: luaconf.h:286
const char * luaT_objtypename(lua_State *L, const TValue *o)
Definition: ltm.cpp:90
void luaT_init(lua_State *L)
Definition: ltm.cpp:37
Definition: ltm.h:43
#define LUA_TOTALTAGS
Definition: lobject.h:28
const TValue * luaT_gettmbyobj(lua_State *L, const TValue *o, TMS event)
Definition: ltm.cpp:70
#define tsvalue(o)
Definition: lobject.h:169
#define lua_assert(c)
Definition: llimits.h:89
std::size_t i
Definition: function.cpp:933
void luaC_fix(lua_State *L, GCObject *o)
Definition: lgc.cpp:194
#define ttypename(x)
Definition: ltm.h:53
Definition: ltm.h:36
int luaT_callorderTM(lua_State *L, const TValue *p1, const TValue *p2, TMS event)
Definition: ltm.cpp:158
Definition: ltm.h:32
l_noret luaG_tointerror(lua_State *L, const TValue *p1, const TValue *p2)
Definition: ldebug.cpp:609
l_noret luaG_opinterror(lua_State *L, const TValue *p1, const TValue *p2, const char *msg)
Definition: ldebug.cpp:597
lu_byte flags
Definition: lobject.h:499
void luaD_call(lua_State *L, StkId func, int nResults)
Definition: ldo.cpp:495
Definition: ltm.h:24
Handling of system events.
Definition: manager.hpp:41
void luaD_callnoyield(lua_State *L, StkId func, int nResults)
Definition: ldo.cpp:507
#define savestack(L, p)
Definition: ldo.h:32
#define ttnov(o)
Definition: lobject.h:136
TMS
Definition: ltm.h:18
#define f
void luaT_callTM(lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, TValue *p3, int hasres)
Definition: ltm.cpp:102
l_noret luaG_concaterror(lua_State *L, const TValue *p1, const TValue *p2)
Definition: ldebug.cpp:591
#define l_isfalse(o)
Definition: lobject.h:181
const TValue * luaT_gettm(Table *events, TMS event, TString *ename)
Definition: ltm.cpp:59
#define LUA_TTABLE
Definition: lua.h:69
#define isLua(ci)
Definition: lstate.h:127
LUA_NUMBER lua_Number
Definition: lua.h:89
LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS]
Definition: ltm.cpp:29
#define hvalue(o)
Definition: lobject.h:175