4 ** See Copyright Notice in lua.h
28 static const char udatatypename
[] = "userdata";
30 LUAI_DDEF
const char *const luaT_typenames_
[LUA_TOTALTYPES
] = {
32 "nil", "boolean", udatatypename
, "number",
33 "string", "table", "function", udatatypename
, "thread",
34 "upvalue", "proto" /* these last cases are used for tests only */
38 void luaT_init(lua_State
*L
) {
39 static const char *const luaT_eventname
[] = { /* ORDER TM */
40 "__index", "__newindex",
41 "__gc", "__mode", "__len", "__eq",
42 "__add", "__sub", "__mul", "__mod", "__pow",
44 "__band", "__bor", "__bxor", "__shl", "__shr",
45 "__unm", "__bnot", "__lt", "__le",
46 "__concat", "__call", "__close"
49 for (i
= 0; i
< TM_N
; i
++) {
50 G(L
)->tmname
[i
] = luaS_new(L
, luaT_eventname
[i
]);
51 luaC_fix(L
, obj2gco(G(L
)->tmname
[i
])); /* never collect these names */
57 ** function to be used with macro "fasttm": optimized for absence of
60 const TValue
*luaT_gettm(Table
*events
, TMS event
, TString
*ename
) {
61 const TValue
*tm
= luaH_getshortstr(events
, ename
);
62 lua_assert(event
<= TM_EQ
);
63 if (notm(tm
)) { /* no tag method? */
64 events
->flags
|= cast_byte(1u << event
); /* cache this fact */
70 const TValue
*luaT_gettmbyobj(lua_State
*L
, const TValue
*o
, TMS event
) {
74 mt
= hvalue(o
)->metatable
;
77 mt
= uvalue(o
)->metatable
;
80 mt
= G(L
)->mt
[ttype(o
)];
82 return (mt
? luaH_getshortstr(mt
, G(L
)->tmname
[event
]) : &G(L
)->nilvalue
);
87 ** Return the name of the type of an object. For tables and userdata
88 ** with metatable, use their '__name' metafield, if present.
90 const char *luaT_objtypename(lua_State
*L
, const TValue
*o
) {
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 */
98 return ttypename(ttype(o
)); /* else use standard type name */
102 void luaT_callTM(lua_State
*L
, const TValue
*f
, const TValue
*p1
,
103 const TValue
*p2
, const TValue
*p3
) {
104 StkId func
= L
->top
.p
;
105 setobj2s(L
, func
, f
); /* push function (assume EXTRA_STACK) */
106 setobj2s(L
, func
+ 1, p1
); /* 1st argument */
107 setobj2s(L
, func
+ 2, p2
); /* 2nd argument */
108 setobj2s(L
, func
+ 3, p3
); /* 3rd argument */
110 /* metamethod may yield only when called from Lua code */
111 if (isLuacode(L
->ci
))
112 luaD_call(L
, func
, 0);
114 luaD_callnoyield(L
, func
, 0);
118 void luaT_callTMres(lua_State
*L
, const TValue
*f
, const TValue
*p1
,
119 const TValue
*p2
, StkId res
) {
120 ptrdiff_t result
= savestack(L
, res
);
121 StkId func
= L
->top
.p
;
122 setobj2s(L
, func
, f
); /* push function (assume EXTRA_STACK) */
123 setobj2s(L
, func
+ 1, p1
); /* 1st argument */
124 setobj2s(L
, func
+ 2, p2
); /* 2nd argument */
126 /* metamethod may yield only when called from Lua code */
127 if (isLuacode(L
->ci
))
128 luaD_call(L
, func
, 1);
130 luaD_callnoyield(L
, func
, 1);
131 res
= restorestack(L
, result
);
132 setobjs2s(L
, res
, --L
->top
.p
); /* move result to its place */
136 static int callbinTM(lua_State
*L
, const TValue
*p1
, const TValue
*p2
,
137 StkId res
, TMS event
) {
138 const TValue
*tm
= luaT_gettmbyobj(L
, p1
, event
); /* try first operand */
140 tm
= luaT_gettmbyobj(L
, p2
, event
); /* try second operand */
141 if (notm(tm
)) return 0;
142 luaT_callTMres(L
, tm
, p1
, p2
, res
);
147 void luaT_trybinTM(lua_State
*L
, const TValue
*p1
, const TValue
*p2
,
148 StkId res
, TMS event
) {
149 if (l_unlikely(!callbinTM(L
, p1
, p2
, res
, event
))) {
157 if (ttisnumber(p1
) && ttisnumber(p2
))
158 luaG_tointerror(L
, p1
, p2
);
160 luaG_opinterror(L
, p1
, p2
, "perform bitwise operation on");
162 /* calls never return, but to avoid warnings: *//* FALLTHROUGH */
164 luaG_opinterror(L
, p1
, p2
, "perform arithmetic on");
170 void luaT_tryconcatTM(lua_State
*L
) {
171 StkId top
= L
->top
.p
;
172 if (l_unlikely(!callbinTM(L
, s2v(top
- 2), s2v(top
- 1), top
- 2,
174 luaG_concaterror(L
, s2v(top
- 2), s2v(top
- 1));
178 void luaT_trybinassocTM(lua_State
*L
, const TValue
*p1
, const TValue
*p2
,
179 int flip
, StkId res
, TMS event
) {
181 luaT_trybinTM(L
, p2
, p1
, res
, event
);
183 luaT_trybinTM(L
, p1
, p2
, res
, event
);
187 void luaT_trybiniTM(lua_State
*L
, const TValue
*p1
, lua_Integer i2
,
188 int flip
, StkId res
, TMS event
) {
191 luaT_trybinassocTM(L
, p1
, &aux
, flip
, res
, event
);
196 ** Calls an order tag method.
197 ** For lessequal, LUA_COMPAT_LT_LE keeps compatibility with old
198 ** behavior: if there is no '__le', try '__lt', based on l <= r iff
199 ** !(r < l) (assuming a total order). If the metamethod yields during
200 ** this substitution, the continuation has to know about it (to negate
201 ** the result of r<l); bit CIST_LEQ in the call status keeps that
204 int luaT_callorderTM(lua_State
*L
, const TValue
*p1
, const TValue
*p2
,
206 if (callbinTM(L
, p1
, p2
, L
->top
.p
, event
)) /* try original event */
207 return !l_isfalse(s2v(L
->top
.p
));
208 #if defined(LUA_COMPAT_LT_LE)
209 else if (event
== TM_LE
) {
210 /* try '!(p2 < p1)' for '(p1 <= p2)' */
211 L
->ci
->callstatus
|= CIST_LEQ
; /* mark it is doing 'lt' for 'le' */
212 if (callbinTM(L
, p2
, p1
, L
->top
.p
, TM_LT
)) {
213 L
->ci
->callstatus
^= CIST_LEQ
; /* clear mark */
214 return l_isfalse(s2v(L
->top
.p
));
216 /* else error will remove this 'ci'; no need to clear mark */
219 luaG_ordererror(L
, p1
, p2
); /* no metamethod found */
220 return 0; /* to avoid warnings */
224 int luaT_callorderiTM(lua_State
*L
, const TValue
*p1
, int v2
,
225 int flip
, int isfloat
, TMS event
) {
229 setfltvalue(&aux
, cast_num(v2
));
232 if (flip
) { /* arguments were exchanged? */
234 p1
= &aux
; /* correct them */
237 return luaT_callorderTM(L
, p1
, p2
, event
);
241 void luaT_adjustvarargs(lua_State
*L
, int nfixparams
, CallInfo
*ci
,
244 int actual
= cast_int(L
->top
.p
- ci
->func
.p
) - 1; /* number of arguments */
245 int nextra
= actual
- nfixparams
; /* number of extra arguments */
246 ci
->u
.l
.nextraargs
= nextra
;
247 luaD_checkstack(L
, p
->maxstacksize
+ 1);
248 /* copy function to the top of the stack */
249 setobjs2s(L
, L
->top
.p
++, ci
->func
.p
);
250 /* move fixed parameters to the top of the stack */
251 for (i
= 1; i
<= nfixparams
; i
++) {
252 setobjs2s(L
, L
->top
.p
++, ci
->func
.p
+ i
);
253 setnilvalue(s2v(ci
->func
.p
+ i
)); /* erase original parameter (for GC) */
255 ci
->func
.p
+= actual
+ 1;
256 ci
->top
.p
+= actual
+ 1;
257 lua_assert(L
->top
.p
<= ci
->top
.p
&& ci
->top
.p
<= L
->stack_last
.p
);
261 void luaT_getvarargs(lua_State
*L
, CallInfo
*ci
, StkId where
, int wanted
) {
263 int nextra
= ci
->u
.l
.nextraargs
;
265 wanted
= nextra
; /* get all extra arguments available */
266 checkstackGCp(L
, nextra
, where
); /* ensure stack space */
267 L
->top
.p
= where
+ nextra
; /* next instruction will need top */
269 for (i
= 0; i
< wanted
&& i
< nextra
; i
++)
270 setobjs2s(L
, where
+ i
, ci
->func
.p
- nextra
+ i
);
271 for (; i
< wanted
; i
++) /* complete required results with nil */
272 setnilvalue(s2v(where
+ i
));