lua-users home
lua-l archive

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


On 19.10.2011 19:31, Petite Abeille wrote:

[...]

function module( aName, ... )
    local aModule = package.loaded[ aName ]

    if type( aModule ) ~= 'table' then
        aModule = {}
        aModule._M = aModule
        aModule._NAME = aName
        aModule._PACKAGE = aName:sub( 1, aName:len() - ( aName:reverse():find( '.', 1, true ) or 0 ) )

        package.loaded[ aName ] = aModule

        for anIndex = 1, select( '#', ... ) do
            select( anIndex, ... )( aModule )
        end
    end

    return aModule
end


What happens if the name passed to require and the name passed to module differ? Wouldn't require just return true and the actual module ends up somewhere hidden in package.loaded? It seems to me that the name parameter to module might be problematic if you don't set globals.

Maybe you shouldn't unconditionally replace all non-table values from package.loaded in case some other module placed a function or a string there (which could happen if "require-name" and "module-name" differed). The proposed module-function doesn't mess with globals anymore, but it does mess with package.loaded and therefore should make sure that it does not disrupt other legitimate uses of package.loaded.

Philipp