lua-users home
lua-l archive

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




On Tuesday, October 8, 2013, Tom N Harris wrote:
On Monday, October 07, 2013 03:15:15 PM Andre Arpin wrote:
> This is verbatim from the wiki.
>
> (function()
>   local require = require
>   local print = print
>   local module = module
>   module("yourmodule");
>
>   (function() module("mymodule") end)()
>
>   print(mymodule ~= nil) -- prints false (where is it?)
> end)();

This led me to discover something interesting.

    (function()
      local print = print
      local module = module

      module("yourmodule")

      print(module) -- function: 0x108f960

      (function()
        module("mymodule")
      end)()

      print(module) -- table: 0x10b0470
    end)()

The module function is mutating the first upvalue of the calling function
without regard to whether it is the _ENV table.

--
tom <telliamed@whoopdedo.org>

In lua 5.2, my understanding is that _ENV is always the first upvalue. Is this not true?

-Andrew