lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

  On Fri Jan 18 5:27 , Jean-Claude Wippler sent:

  Could someone well-versed in Lua's intricate details please fill in
  the gaps, or suggest better solutions, or else expose some fatal flaw
  in all of this?


See Doug Rogers patch on the 1st Jan.

It needs a couple of changes to be truly Lua like, as without them
it will not allow a function to all 3 values (including step).

Unfortunately changes to the luavm are necessary, as the following
program needs to be valid:

"for i = 1,3,nil do print(i) end"

where the nil is treated as 1. Current Lua will raise an error at runtime.
(This obviously will also break luajit compatibility, although easily fixed)

The changes are:

-   if (nexps < 3) {
-     adjust_assign(ls, 2, nexps, &v);
-     luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
-     luaK_reserveregs(fs, 1);
-   }
-   else {
     adjust_assign(ls, 3, nexps, &v);
-   }

And to the luavm:

          else if (!tonumber(plimit, ra+1))
            luaG_runerror(L, LUA_QL("for") " limit must be a number");
+        else if (ttisnil(pstep))
+          { setnvalue(pstep, 1) }
          else if (!tonumber(pstep, ra+2))
            luaG_runerror(L, LUA_QL("for") " step must be a number");

(setnvalue is a bad macro that doesn't allow a semicolon when used in an
if else - should be a " do { setnvalue() } while (0) } " macro or similar)

It's worth checking if it's nil before checking if it's a number or coercable string
- nil is almost definitely the most common case.