lua-users home
lua-l archive

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


François Perrad wrote:
> 1) numeric for with step = 0 causes infinite loop
>     $ luajit -e 'for i = 5, 7, 0 do end'

This is testing implementation-defined behavior, i.e. you'll get
undefined results. Should 'for i=a,b,0' run upwards or downwards?
And what about 'for i=a,b,-0'?

LJ2 chooses to look at the sign bit and runs downwards it it's
set. Lua runs upwards if 0 < step. Now, please don't ask what
happens if step is NaN. :-)

> 2) losing arguments with metamethod __call
>     m = {}
>     setmetatable(m, {
>         __call = function (obj, ...)
>                      print('m.__call ' .. tostring(obj) .. ', ' ..
> table.concat(arg, ', '))
>                  end
>     })
>     m()
>     m(1, 2) --> arguments are lost

'arg' is a Lua 5.0 feature. Not supported. Use {...}.

> and some new minor issues or delta :
> 1) getfenv(-3) gives a more general message :
>     bad argument #1 to 'getfenv' (invalid level)
> instead of
>     bad argument #1 to 'getfenv' (level must be non-negative)
> same issue with setfenv().

Intentional change. The common error message is precise enough.

> 2) tostring of C function
>     $ luajit -e 'print(tostring(print))'
>     function: fast#29
> when lua gives :
>     function: 0x9a5d020

The exact format of of a tostring'ized object is implementation-
defined. Giving more information (since it's there) is intentional.

> 3) xpcall without error handler
>     $ luajit -e 'print(xpcall(assert, nil))'
>     luajit: (command line):1: bad argument #2 to 'xpcall' (function
> expected, got nil)
> instead of :
>     false	error in error handling

I'm testing that the error handler is a function. This is
apparently missing in the Lua codebase and it's erroring out
later (which is not useful). File a bug against Lua.

> 4) table.maxn
>     $ luajit -e 't = {3,4}; print(table.maxn(t))'
>     1
> instead of :
>     2

Definitely a bug. Patch attached. Thank you!

> 5) deprecated table.setn
>     $ luajit -e 'table.setn({},7)'
>     luajit: (command line):1: attempt to call field 'setn' (a nil value)
> instead of :
>     lua: (command line):1: 'setn' is obsolete

Intentional.

> 6) math.random with extra arguments
>     $ luajit -e 'print(math.random(1, 2, 3))'
>     2
> instead of :
>     lua: (command line):1: wrong number of arguments

Intentional. None of the other library functions care when more
arguments than necessary are passed.

Thanks for helping with testing!

--Mike
--- a/src/lib_table.c
+++ b/src/lib_table.c
@@ -74,14 +74,14 @@ LJLIB_CF(table_maxn)
   TValue *array = tvref(t->array);
   Node *node;
   lua_Number m = 0;
-  uint32_t i;
-  for (i = 0; i < t->asize; i++)
+  ptrdiff_t i;
+  for (i = (ptrdiff_t)t->asize - 1; i >= 0; i--)
     if (!tvisnil(&array[i])) {
-      m = (lua_Number)i;
+      m = (lua_Number)(int32_t)i;
       break;
     }
   node = noderef(t->node);
-  for (i = 0; i <= t->hmask; i++)
+  for (i = (ptrdiff_t)t->hmask - 1; i >= 0; i--)
     if (tvisnum(&node[i].key) && numV(&node[i].key) > m)
       m = numV(&node[i].key);
   setnumV(L->top-1, m);