lua-users home
lua-l archive

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


Hi.

  Some guy on luahub found specific case about lua 5.2 load(). While
searching for
explanation for that behavior I found exact spot, and can't decide how
to treat it. Is
that a bug or not.
Test case:

=== cut ===
function test()
    return 1
end

fn = load(string.dump(test), nil, "b", _ENV)
print(fn)
=== cut ===

Instead of loaded chunk function load() returns table passed as 4th
argument.
I checked with lua sources (v.5.2.1) and found the cause:

lbaselib.c: 323
  if (status == LUA_OK && top >= 4) {  /* is there an 'env' argument */
    lua_pushvalue(L, 4);  /* environment for loaded function */
    lua_setupvalue(L, -2, 1);  /* set it as 1st upvalue */
  }

lua_setupvalue can't set _ENV as upvalue, because there's no upvalues in
that specific
test chunk, there's no global values referenced. Error is silently
ignored, and table remains
on stack, user gets table instead of expected function.

So, is that a bug worth fixing, or expected behavior that needs special
attention?

--