lua-users home
lua-l archive

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


The script below shows a difference of behaviour between LuaJIT and Lua 5.1.4 (or Lua 5.2.0 alpha).
LuaJIT produces an error and it's a good behaviour (see with Mike Pall)
At the first call, 'reader' returns 'nil'.
At the second call, 'reader' returns a string with code.
But Lua 5.1.4 bypasses the first 'nil'.

The attached patch allows to Lua 5.1.4 to fail in the same way.

Obviously, it is a pathologic case, the variable 'i' must be initialized with 0 instead of -1.
(an array starts at index 1, not 0)

François.

t = { [[
function bar (x)
    return x
end
]] }
i = -1
function reader ()
    i = i + 1
    print('==>', t[i])
    return t[i]
end
f, msg = load(reader)

f()
print(bar('ok'))


$ luajit -v load.lua
LuaJIT 2.0.0-beta6 -- Copyright (C) 2005-2011 Mike Pall. http://luajit.org/
==>    nil
luajit: load.lua:16: attempt to call global 'bar' (a nil value)
stack traceback:
    load.lua:16: in main chunk
    [C]: ?

$ lua -v load.lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
==>    nil
==>    function bar (x)
    return x
end

==>    nil
ok

diff --git a/src/ldo.c b/src/ldo.c
index 8de05f7..feea5d6 100644
--- a/src/ldo.c
+++ b/src/ldo.c
@@ -493,6 +493,7 @@ static void f_parser (lua_State *L, void *ud) {
   Closure *cl;
   struct SParser *p = cast(struct SParser *, ud);
   int c = luaZ_lookahead(p->z);
+  if (c == EOZ) return;
   luaC_checkGC(L);
   tf = ((c == LUA_SIGNATURE[0]) ? luaU_undump : luaY_parser)(L, p->z,
                                                              &p->buff, p->name);
t = { [[
function bar (x)
    return x
end
]] }
i = -1
function reader ()
    i = i + 1
    print('==>', t[i])
    return t[i]
end
f, msg = load(reader)

f()
print(bar('ok'))