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