[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Modules with standalone main program
- From: albertmcchan <albertmcchan@...>
- Date: Thu, 1 Mar 2018 08:23:08 -0500
On Feb 27, 2018, at 10:19 AM, "Soni \"They/Them\" L." <fakedme@gmail.com> wrote:
> On 2018-02-27 12:12 PM, albertmcchan wrote:
>>
>> Since debug.getinfo check the stack, can I wrap the code like this
>>
>> function package.loading()
>> return debug.getinfo(3).func == require
>> end
>
> 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
> end
> return debug.getinfo(i).func == require
> end
>
> (this supports modules such as "return (function(...) print(package.loading()) end)(...)")
>
It work !
I just use the simpler version (i=3), and save the result if it really
need to go "deep" into the stack (say, first line in mod.lua)
It even work with old module(...) setup, by NOT looking the cache table.
And, less typing, thus less error !
Before, I use package.loading "mod", but it wil fail if user do require "mod.lua"
And worse, I might misspell the "mod", and it just check the wrong thing.
And, it satisfy my desire for explicit test for require "mod.lua"
-- package.loading was called from mod.lua
-- it actually test if require called mod.lua
if I understand correctly about the stacks:
stack 1 = package.loading
stack 2 = what called package.loading = mod.lua
stack 3 = what called mod.lua, and is it require ?
Thanks.