luabundle icon indicating copy to clipboard operation
luabundle copied to clipboard

Module returning `nil` executed again next time it's required

Open ikegami opened this issue 3 years ago • 1 comments

A module returning false, nil or nothing is executed again next time it's required.

loaded holds the value returned by a module.

loaded is also used to determine if a module has already been loaded.

Using this setup, there is no way to distinguish a module that hasn't been loaded from a module that has been loaded and returned nil.

ikegami avatar Apr 03 '22 08:04 ikegami

The Lua 5.2 manual suggests require should return true when a module returns nil.

Once a loader is found, require calls the loader with two arguments: modname and an extra value dependent on how it got the loader. (If the loader came from a file, this extra value is the file name.) If the loader returns any non-nil value, require assigns the returned value to package.loaded[modname]. If the loader does not return a non-nil value and has not assigned any value to package.loaded[modname], then require assigns true to this entry. In any case, require returns the final value of package.loaded[modname].

So,

loaded[name] = loadedModule

should be changed to

if loadedModule == nil then
   loaded[name] = true
else
   loaded[name] = loadedModule
end

and

if loadedModule then

should be changed to

if loadedModule == nil then

ikegami avatar Apr 03 '22 08:04 ikegami