lua-users home
lua-l archive

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


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

> 2018-02-26 5:43 GMT+02:00 Paul K <paul@zerobrane.com>:
>>> what if later versions of dofile allow optional command-line args ?
>> 
>> We'll cross this bridge when we get there. For now this logic should
>> work with all Lua 5.1+ versions.
> 
> Why is it better to use debug.getlocal than "'..."?
> 

require "mod"
lua mod.lua mod
loadfile "mod.lua" "mod"

ALL of above, ... = "mod", so ... is a bad test for require vs standalone

my solution is to patch require, using false as sentinel, like this:

function require(name)                          -- pseudo-code for loadlib.c ll_require()
  local cache = package.loaded[name]
  if cache ~= nil then return cache end  -- return even if false, avoid infinite loops
  package.loaded[name] = false            -- signal loading "mod" INSIDE require()
  -- do actual require "mod"
  return package.loaded[name]             -- assert bool(return) == true ("mod" loaded)
end

function package.loading(name)           -- direct test for require "mod"
  return package.loaded[name] == false 
end

so, inside mod.lua, I can test for require "mod" like this:

if package.loading "mod" then return mod_func end