aboutsummaryrefslogtreecommitdiff
path: root/ltm.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2017-06-29 12:06:44 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2017-06-29 12:06:44 -0300
commitf96497397addca22f22a6ba6eeabc906be43f16b (patch)
treeaf8d27b9af36dfe0b0b6e0f765ea90b95b110efc /ltm.c
parent5a1c8d8ef343bf0157851a4832c2c937b812b64f (diff)
downloadlua-f96497397addca22f22a6ba6eeabc906be43f16b.tar.gz
lua-f96497397addca22f22a6ba6eeabc906be43f16b.tar.bz2
lua-f96497397addca22f22a6ba6eeabc906be43f16b.zip
new type 'StackValue' for stack elements
(we may want to put extra info there in the future)
Diffstat (limited to 'ltm.c')
-rw-r--r--ltm.c46
1 files changed, 29 insertions, 17 deletions
diff --git a/ltm.c b/ltm.c
index 9da191f6..3629dc69 100644
--- a/ltm.c
+++ b/ltm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.c,v 2.40 2017/05/08 15:57:23 roberto Exp roberto $ 2** $Id: ltm.c,v 2.41 2017/05/13 12:57:20 roberto Exp roberto $
3** Tag methods 3** Tag methods
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -100,24 +100,36 @@ const char *luaT_objtypename (lua_State *L, const TValue *o) {
100 100
101 101
102void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 102void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
103 const TValue *p2, TValue *p3, int hasres) { 103 const TValue *p2, const TValue *p3) {
104 ptrdiff_t result = savestack(L, p3); 104 StkId func = L->top;
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 */
109 L->top += 4;
110 /* metamethod may yield only when called from Lua code */
111 if (isLua(L->ci))
112 luaD_call(L, func, 0);
113 else
114 luaD_callnoyield(L, func, 0);
115}
116
117
118void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
119 const TValue *p2, StkId res) {
120 ptrdiff_t result = savestack(L, res);
105 StkId func = L->top; 121 StkId func = L->top;
106 setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ 122 setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
107 setobj2s(L, func + 1, p1); /* 1st argument */ 123 setobj2s(L, func + 1, p1); /* 1st argument */
108 setobj2s(L, func + 2, p2); /* 2nd argument */ 124 setobj2s(L, func + 2, p2); /* 2nd argument */
109 L->top += 3; 125 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 */ 126 /* metamethod may yield only when called from Lua code */
113 if (isLua(L->ci)) 127 if (isLua(L->ci))
114 luaD_call(L, func, hasres); 128 luaD_call(L, func, 1);
115 else 129 else
116 luaD_callnoyield(L, func, hasres); 130 luaD_callnoyield(L, func, 1);
117 if (hasres) { /* if has result, move it to its place */ 131 res = restorestack(L, result);
118 p3 = restorestack(L, result); 132 setobjs2s(L, res, --L->top); /* more result to its place */
119 setobjs2s(L, p3, --L->top);
120 }
121} 133}
122 134
123 135
@@ -127,7 +139,7 @@ static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
127 if (ttisnil(tm)) 139 if (ttisnil(tm))
128 tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ 140 tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
129 if (ttisnil(tm)) return 0; 141 if (ttisnil(tm)) return 0;
130 luaT_callTM(L, tm, p1, p2, res, 1); 142 luaT_callTMres(L, tm, p1, p2, res);
131 return 1; 143 return 1;
132} 144}
133 145
@@ -160,7 +172,7 @@ int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
160 if (!callbinTM(L, p1, p2, L->top, event)) 172 if (!callbinTM(L, p1, p2, L->top, event))
161 return -1; /* no metamethod */ 173 return -1; /* no metamethod */
162 else 174 else
163 return !l_isfalse(L->top); 175 return !l_isfalse(s2v(L->top));
164} 176}
165 177
166 178
@@ -171,19 +183,19 @@ void luaT_adjustvarargs (lua_State *L, Proto *p, int actual) {
171 int nfixparams = p->numparams - 1; /* number of fixed parameters */ 183 int nfixparams = p->numparams - 1; /* number of fixed parameters */
172 actual -= nfixparams; /* number of extra arguments */ 184 actual -= nfixparams; /* number of extra arguments */
173 vtab = luaH_new(L); /* create vararg table */ 185 vtab = luaH_new(L); /* create vararg table */
174 sethvalue(L, L->top, vtab); /* anchor it for resizing */ 186 sethvalue2s(L, L->top, vtab); /* anchor it for resizing */
175 L->top++; /* space ensured by caller */ 187 L->top++; /* space ensured by caller */
176 luaH_resize(L, vtab, actual, 1); 188 luaH_resize(L, vtab, actual, 1);
177 for (i = 0; i < actual; i++) /* put extra arguments into vararg table */ 189 for (i = 0; i < actual; i++) /* put extra arguments into vararg table */
178 setobj2n(L, &vtab->array[i], L->top - actual + i - 1); 190 setobj2n(L, &vtab->array[i], s2v(L->top - actual + i - 1));
179 setsvalue(L, &nname, luaS_newliteral(L, "n")); /* get field 'n' */ 191 setsvalue(L, &nname, luaS_newliteral(L, "n")); /* get field 'n' */
180 setivalue(luaH_set(L, vtab, &nname), actual); /* store counter there */ 192 setivalue(luaH_set(L, vtab, &nname), actual); /* store counter there */
181 L->top -= actual; /* remove extra elements from the stack */ 193 L->top -= actual; /* remove extra elements from the stack */
182 sethvalue(L, L->top - 1, vtab); /* move table to new top */ 194 sethvalue2s(L, L->top - 1, vtab); /* move table to new top */
183} 195}
184 196
185 197
186void luaT_getvarargs (lua_State *L, StkId t, StkId where, int wanted) { 198void luaT_getvarargs (lua_State *L, TValue *t, StkId where, int wanted) {
187 if (!ttistable(t)) 199 if (!ttistable(t))
188 luaG_runerror(L, "'vararg' parameter is not a table"); 200 luaG_runerror(L, "'vararg' parameter is not a table");
189 else { 201 else {