diff options
author | Yaco <franco@reevo.org> | 2021-10-19 20:30:39 -0300 |
---|---|---|
committer | Yaco <franco@reevo.org> | 2021-10-19 20:30:39 -0300 |
commit | 8c201ace3699b4928daf41eb7b4cdcb4565c6f3b (patch) | |
tree | fbd98f026864e9c1919d3ee740b6799ca0c651e2 /www/wiki/extensions/Scribunto/includes/engines/LuaCommon/lualib/libraryUtil.lua | |
parent | e3880a1c86acaa3bbd05786ad2f5c586e6511a58 (diff) |
adds Scribunto
Diffstat (limited to 'www/wiki/extensions/Scribunto/includes/engines/LuaCommon/lualib/libraryUtil.lua')
-rw-r--r-- | www/wiki/extensions/Scribunto/includes/engines/LuaCommon/lualib/libraryUtil.lua | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/www/wiki/extensions/Scribunto/includes/engines/LuaCommon/lualib/libraryUtil.lua b/www/wiki/extensions/Scribunto/includes/engines/LuaCommon/lualib/libraryUtil.lua new file mode 100644 index 00000000..f14a7950 --- /dev/null +++ b/www/wiki/extensions/Scribunto/includes/engines/LuaCommon/lualib/libraryUtil.lua @@ -0,0 +1,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 |