Module:load

Hello, you have come here looking for the meaning of the word Module:load. In DICTIOUS you will not only get to know all the dictionary meanings for the word Module:load, but we will also tell you about its etymology, its characteristics and you will know how to say Module:load in singular and plural. Everything you need to know about the word Module:load you have here. The definition of the word Module:load will help you to be more precise and correct when speaking or writing your texts. Knowing the definition ofModule:load, as well as those of other words, enriches your vocabulary and provides you with more and better linguistic resources.

This module exports various functions for loading other modules.

Detailed documentation

export.safe_require

function export.safe_require(modname)

Like require, but returns nil if a module does not exist, instead of throwing an error.

Outputs are cached, which is faster for all modules, but much faster for nonexistent modules, since require will attempt to use the full loader each time because loading failures don't get cached.

export.load_data

function export.load_data(modname)

Like mw.loadData, but it does not generate a new table each time the same module is loaded via this function. Instead, it caches the returned data so that the same table can be returned each time, which is more efficient.

export.safe_load_data

function export.safe_load_data(modname)

Like export.load_data (itself a variant of mw.loadData), but returns nil if a module does not exist, instead of throwing an error.

Outputs are cached, which is faster for all modules, but much faster for nonexistent modules, since mw.loadData will attempt to use the full loader each time because loading failures don't get cached.


local export = {}

local load_data = mw.loadData
local loaded = package.loaded
local main_loader = package.loaders
local require = require
local setmetatable = setmetatable

local loaders, loaded_data, mt

local function get_mt()
	mt, get_mt = {__mode = "kv"}, nil
	return mt
end

-- main_loader returns a loader function if the module exists, or nil if it doesn't.
local function get_loader(modname)
	if loaders == nil then
		local loader = main_loader(modname)
		loaders = setmetatable({ = loader or false}, mt or get_mt())
		return loader
	end
	local loader = loaders
	if loader == nil then
		loader = main_loader(modname)
		loaders = loader or false
	end
	return loader or nil
end

local function get_data(modname, loader)
	if loaded_data == nil then
		local data = load_data(modname)
		loaded_data = setmetatable({ = data}, mt or get_mt())
		return data
	end
	local data = loaded_data
	if data == nil then
		data = load_data(modname)
		loaded_data = data
	end
	return data
end

--[==[
Like `require`, but returns `nil` if a module does not exist, instead of throwing an error.

Outputs are cached, which is faster for all modules, but much faster for nonexistent modules, since `require` will attempt to use the full loader each time because loading failures don't get cached.]==]
function export.safe_require(modname)
	local mod = loaded
	if mod == nil and get_loader(modname) then
		-- Call with require instead of the loader directly, as it protects against infinite loading loops.
		return require(modname)
	end
	return mod
end

--[==[
Like `mw.loadData`, but it does not generate a new table each time the same module is loaded via this function. Instead, it caches the returned data so that the same table can be returned each time, which is more efficient.]==]
function export.load_data(modname)
	return get_data(modname, get_loader(modname))
end

--[==[
Like `export.load_data` (itself a variant of `mw.loadData`), but returns `nil` if a module does not exist, instead of throwing an error.

Outputs are cached, which is faster for all modules, but much faster for nonexistent modules, since `mw.loadData` will attempt to use the full loader each time because loading failures don't get cached.]==]
function export.safe_load_data(modname)
	local loader = get_loader(modname)
	if loader == nil then
		return nil
	end
	return get_data(modname, loader)
end

return export