summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Scribunto/includes/engines/LuaCommon/lualib/libraryUtil.lua
blob: f14a7950f0547b35ad0c0a05065d345968acb41d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
local libraryUtil = {}

function libraryUtil.checkType( name, argIdx, arg, expectType, nilOk )
	if arg == nil and nilOk then
		return
	end
	if type( arg ) ~= expectType then
		local msg = string.format( "bad argument #%d to '%s' (%s expected, got %s)",
			argIdx, name, expectType, type( arg )
		)
		error( msg, 3 )
	end
end

function libraryUtil.checkTypeMulti( name, argIdx, arg, expectTypes )
	local argType = type( arg )
	for _, expectType in ipairs( expectTypes ) do
		if argType == expectType then
			return
		end
	end
	local n = #expectTypes
	local typeList
	if n > 1 then
		typeList = table.concat( expectTypes, ', ', 1, n - 1 ) .. ' or ' .. expectTypes[n]
	else
		typeList = expectTypes[1]
	end
	local msg = string.format( "bad argument #%d to '%s' (%s expected, got %s)",
		argIdx,
		name,
		typeList,
		type( arg )
	)
	error( msg, 3 )
end

function libraryUtil.checkTypeForIndex( index, value, expectType )
	if type( value ) ~= expectType then
		local msg = string.format( "value for index '%s' must be %s, %s given",
			index, expectType, type( value )
		)
		error( msg, 3 )
	end
end

function libraryUtil.checkTypeForNamedArg( name, argName, arg, expectType, nilOk )
	if arg == nil and nilOk then
		return
	end
	if type( arg ) ~= expectType then
		local msg = string.format( "bad named argument %s to '%s' (%s expected, got %s)",
			argName, name, expectType, type( arg )
		)
		error( msg, 3 )
	end
end

function libraryUtil.makeCheckSelfFunction( libraryName, varName, selfObj, selfObjDesc )
	return function ( self, method )
		if self ~= selfObj then
			error( string.format(
				"%s: invalid %s. Did you call %s with a dot instead of a colon, i.e. " ..
				"%s.%s() instead of %s:%s()?",
				libraryName, selfObjDesc, method, varName, method, varName, method
			), 3 )
		end
	end
end

return libraryUtil