Advertisement
Naheulf

ComputerCraft/modules/main/cc/expect.lua

Oct 20th, 2020 (edited)
1,821
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.53 KB | None | 0 0
  1. --- This file come from cc-tweaked-1.15.2-1.90.0
  2. --- The @{cc.expect} library provides helper functions for verifying that
  3. -- function arguments are well-formed and of the correct type.
  4. --
  5. -- @module cc.expect
  6.  
  7. local native_select, native_type = select, type
  8.  
  9. local function get_type_names(...)
  10.     local types = table.pack(...)
  11.     for i = types.n, 1, -1 do
  12.         if types[i] == "nil" then table.remove(types, i) end
  13.     end
  14.  
  15.     if #types <= 1 then
  16.         return tostring(...)
  17.     else
  18.         return table.concat(types, ", ", 1, #types - 1) .. " or " .. types[#types]
  19.     end
  20. end
  21. --- Expect an argument to have a specific type.
  22. --
  23. -- @tparam number index The 1-based argument index.
  24. -- @param value The argument's value.
  25. -- @tparam string ... The allowed types of the argument.
  26. -- @return The given `value`.
  27. -- @throws If the value is not one of the allowed types.
  28. local function expect(index, value, ...)
  29.     local t = native_type(value)
  30.     for i = 1, native_select("#", ...) do
  31.         if t == native_select(i, ...) then return value end
  32.     end
  33.  
  34.     -- If we can determine the function name with a high level of confidence, try to include it.
  35.     local name
  36.     if native_type(debug) == "table" and native_type(debug.getinfo) == "function" then
  37.         local ok, info = pcall(debug.getinfo, 3, "nS")
  38.         if ok and info.name and #info.name ~= "" and info.what ~= "C" then name = info.name end
  39.     end
  40.  
  41.     local type_names = get_type_names(...)
  42.     if name then
  43.         error(("bad argument #%d to '%s' (expected %s, got %s)"):format(index, name, type_names, t), 3)
  44.     else
  45.         error(("bad argument #%d (expected %s, got %s)"):format(index, type_names, t), 3)
  46.     end
  47. end
  48.  
  49. --- Expect an field to have a specific type.
  50. --
  51. -- @tparam table tbl The table to index.
  52. -- @tparam string index The field name to check.
  53. -- @tparam string ... The allowed types of the argument.
  54. -- @return The contents of the given field.
  55. -- @throws If the field is not one of the allowed types.
  56. local function field(tbl, index, ...)
  57.     expect(1, tbl, "table")
  58.     expect(2, index, "string")
  59.  
  60.     local value = tbl[index]
  61.     local t = native_type(value)
  62.     for i = 1, native_select("#", ...) do
  63.         if t == native_select(i, ...) then return value end
  64.     end
  65.  
  66.     if value == nil then
  67.         error(("field '%s' missing from table"):format(index), 3)
  68.     else
  69.         error(("bad field '%s' (expected %s, got %s)"):format(index, get_type_names(...), t), 3)
  70.     end
  71. end
  72.  
  73. return {
  74.     expect = expect,
  75.     field = field,
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement