MCPcopy Create free account
hub / github.com/EmmyLua/EmmyLuaDebugger / forprep

Function forprep

third-party/lua-5.4.6/src/lvm.c:208–260  ·  view source on GitHub ↗

** Prepare a numerical for loop (opcode OP_FORPREP). ** Return true to skip the loop. Otherwise, ** after preparation, stack will be as follows: ** ra : internal index (safe copy of the control variable) ** ra + 1 : loop counter (integer loops) or limit (float loops) ** ra + 2 : step ** ra + 3 : control variable */

Source from the content-addressed store, hash-verified

206** ra + 3 : control variable
207*/
208static int forprep (lua_State *L, StkId ra) {
209 TValue *pinit = s2v(ra);
210 TValue *plimit = s2v(ra + 1);
211 TValue *pstep = s2v(ra + 2);
212 if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */
213 lua_Integer init = ivalue(pinit);
214 lua_Integer step = ivalue(pstep);
215 lua_Integer limit;
216 if (step == 0)
217 luaG_runerror(L, "'for' step is zero");
218 setivalue(s2v(ra + 3), init); /* control variable */
219 if (forlimit(L, init, plimit, &limit, step))
220 return 1; /* skip the loop */
221 else { /* prepare loop counter */
222 lua_Unsigned count;
223 if (step > 0) { /* ascending loop? */
224 count = l_castS2U(limit) - l_castS2U(init);
225 if (step != 1) /* avoid division in the too common case */
226 count /= l_castS2U(step);
227 }
228 else { /* step < 0; descending loop */
229 count = l_castS2U(init) - l_castS2U(limit);
230 /* 'step+1' avoids negating 'mininteger' */
231 count /= l_castS2U(-(step + 1)) + 1u;
232 }
233 /* store the counter in place of the limit (which won't be
234 needed anymore) */
235 setivalue(plimit, l_castU2S(count));
236 }
237 }
238 else { /* try making all values floats */
239 lua_Number init; lua_Number limit; lua_Number step;
240 if (l_unlikely(!tonumber(plimit, &limit)))
241 luaG_forerror(L, plimit, "limit");
242 if (l_unlikely(!tonumber(pstep, &step)))
243 luaG_forerror(L, pstep, "step");
244 if (l_unlikely(!tonumber(pinit, &init)))
245 luaG_forerror(L, pinit, "initial value");
246 if (step == 0)
247 luaG_runerror(L, "'for' step is zero");
248 if (luai_numlt(0, step) ? luai_numlt(limit, init)
249 : luai_numlt(init, limit))
250 return 1; /* skip the loop */
251 else {
252 /* make sure internal values are all floats */
253 setfltvalue(plimit, limit);
254 setfltvalue(pstep, step);
255 setfltvalue(s2v(ra), init); /* internal index */
256 setfltvalue(s2v(ra + 3), init); /* control variable */
257 }
258 }
259 return 0;
260}
261
262
263/*

Callers 1

luaV_executeFunction · 0.70

Calls 3

luaG_runerrorFunction · 0.70
forlimitFunction · 0.70
luaG_forerrorFunction · 0.70

Tested by

no test coverage detected