]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* |
2 | ** $Id: ltm.c,v 2.34 2015/03/30 15:42:27 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_getstr(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_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); | |
83 | } | |
84 | ||
85 | ||
86 | void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, | |
87 | const TValue *p2, TValue *p3, int hasres) { | |
88 | ptrdiff_t result = savestack(L, p3); | |
89 | setobj2s(L, L->top++, f); /* push function (assume EXTRA_STACK) */ | |
90 | setobj2s(L, L->top++, p1); /* 1st argument */ | |
91 | setobj2s(L, L->top++, p2); /* 2nd argument */ | |
92 | if (!hasres) /* no result? 'p3' is third argument */ | |
93 | setobj2s(L, L->top++, p3); /* 3rd argument */ | |
94 | /* metamethod may yield only when called from Lua code */ | |
95 | luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci)); | |
96 | if (hasres) { /* if has result, move it to its place */ | |
97 | p3 = restorestack(L, result); | |
98 | setobjs2s(L, p3, --L->top); | |
99 | } | |
100 | } | |
101 | ||
102 | ||
103 | int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, | |
104 | StkId res, TMS event) { | |
105 | const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ | |
106 | if (ttisnil(tm)) | |
107 | tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ | |
108 | if (ttisnil(tm)) return 0; | |
109 | luaT_callTM(L, tm, p1, p2, res, 1); | |
110 | return 1; | |
111 | } | |
112 | ||
113 | ||
114 | void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, | |
115 | StkId res, TMS event) { | |
116 | if (!luaT_callbinTM(L, p1, p2, res, event)) { | |
117 | switch (event) { | |
118 | case TM_CONCAT: | |
119 | luaG_concaterror(L, p1, p2); | |
120 | /* call never returns, but to avoid warnings: *//* FALLTHROUGH */ | |
121 | case TM_BAND: case TM_BOR: case TM_BXOR: | |
122 | case TM_SHL: case TM_SHR: case TM_BNOT: { | |
123 | lua_Number dummy; | |
124 | if (tonumber(p1, &dummy) && tonumber(p2, &dummy)) | |
125 | luaG_tointerror(L, p1, p2); | |
126 | else | |
127 | luaG_opinterror(L, p1, p2, "perform bitwise operation on"); | |
128 | } | |
129 | /* calls never return, but to avoid warnings: *//* FALLTHROUGH */ | |
130 | default: | |
131 | luaG_opinterror(L, p1, p2, "perform arithmetic on"); | |
132 | } | |
133 | } | |
134 | } | |
135 | ||
136 | ||
137 | int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, | |
138 | TMS event) { | |
139 | if (!luaT_callbinTM(L, p1, p2, L->top, event)) | |
140 | return -1; /* no metamethod */ | |
141 | else | |
142 | return !l_isfalse(L->top); | |
143 | } | |
144 |