71 lines
2.1 KiB
Lua
71 lines
2.1 KiB
Lua
-- Private -----------------------------------------------------------------------------------------
|
|
|
|
local private = {
|
|
enabled = false
|
|
}
|
|
|
|
--[[
|
|
Disable all checks.
|
|
|
|
By default, checks start out disabled already. This function should be used to turn off checks
|
|
when they are no longer needed following a call to `enable`.
|
|
]]--
|
|
function private.disable()
|
|
private.enabled = false
|
|
end
|
|
|
|
--[[
|
|
Enable all checks.
|
|
|
|
By default, all checks are *disabled* for better performance. By calling this function, one can
|
|
turn on checks for a given patch of code.
|
|
]]--
|
|
function private.enable()
|
|
private.enabled = true
|
|
end
|
|
|
|
--[[
|
|
Assert that a given value is a key in a certain table.
|
|
|
|
Args:
|
|
name:string - the name used for the value in error messages
|
|
value:any - the value being tested
|
|
table_name:string - the name used for the table in error messages
|
|
table_value:table - the table which must contain the indicated value
|
|
|
|
Errors:
|
|
if the given `value` does not refer to a non-`nil` value in `table_value`
|
|
]]--
|
|
function private.is_key(name, value, table_name, table_value)
|
|
if not private.enabled then return end
|
|
if table_value[value] ~= nil then return end
|
|
error(name.." should be a key in "..table_name..", but "..value.." isn't present")
|
|
end
|
|
|
|
--[[
|
|
Assert that a given value is a certain type.
|
|
|
|
Args:
|
|
name:string - the name used for the value in error messages
|
|
value:any - the value being tested
|
|
type_name:string - the string which *should* be produced by `type(value)`
|
|
|
|
Errors:
|
|
if the `type(value)` does not produce `type_name`
|
|
]]--
|
|
function private.is_type(name, value, type_name)
|
|
if not private.enabled then return end
|
|
if type(value) == type_name then return end
|
|
error(name.." should be a "..type_name..", but was a "..type(value))
|
|
end
|
|
|
|
|
|
-- Export ------------------------------------------------------------------------------------------
|
|
|
|
_G.jvmi.check = {
|
|
disable = private.disable,
|
|
enable = private.enable,
|
|
is_key = private.is_key,
|
|
is_type = private.is_type
|
|
}
|