lua-users home
lua-l archive

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


On Feb 26, 2018, at 10:37 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:

> 2018-02-26 16:10 GMT+02:00 albertmcchan <albertmcchan@yahoo.com>:
>> 
>> using ... from unknown source is dangerous, ... can be anything
>> 
>> A.lua: loadfile "B.lua" "A"  -- running B standalone
>>      if package.loading(...) then print "requiring A" else print "standalone A"end
>> 
>> B.lua: if package.loading(...) then print "requiring B" else print "standalone B" end
>> 
>> lua> require "A"
>> requiring "B"   -- wrong !!! tested package.loading "A"
>> requiring "A"
>> 
>> -- replaced (...) with actual modname, we get
>> lua> require "A"
>> standalone B
>> requiring A
> 
> All of whch TL;DR attempts to demonstrate why "..." may fail, but does
> not answer my question, which is "why is debug.getlocal better?"
> 

No ... I don't think debug.getlocal is better

I replace package.loading(...) with getlocal test for above:

if pcall(debug.getlocal, 4, 1) then
  print(select('#', ...) == 0 and "dofile mod" or "requiring mod")
else
  print "standalone mod"
end

-- this is what I get:
c:\lua> lua A.lua
requiring B -- wrong !!!
standalone A

lua> require "A"
requiring B  -- wrong !!!
requiring A

I don't like indirect test, even if it work (sometimes?)
Direct test with package.loading "mod" is better.