lua-users home
lua-l archive

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

On 2018-02-27 12:12 PM, albertmcchan wrote:
On Feb 27, 2018, at 8:29 AM, "Soni \"They/Them\" L." <> wrote:

Here's an idea:

if debug.getinfo(2).func == require then
     -- required
elseif debug.getinfo(2).func == dofile then
     -- dofiled
     -- loadfiled, luaed, etc
Since debug.getinfo check the stack, can I wrap the code like this
(changing 2 to 3 correct ?)

function package.loading()
   return debug.getinfo(3).func == require

Untested, but:

function package.loading()
  local i = 3
  local x = debug.getinfo(2)
  local n = x.source
  while debug.getinfo(i).source == n do
    i = i + 1
  return debug.getinfo(i).func == require

(this supports modules such as "return (function(...) print(package.loading()) end)(...)")

Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.