-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.lua
168 lines (127 loc) · 4.83 KB
/
debug.lua
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
--- File: Debugging Tools
--- Module: otlib
module( "otlib", package.seeall )
local function VardumpHelper( value, depth, key, done )
local str = string.rep( " ", depth )
if key ~= nil then
local t = type( key )
if t == "string" then
str = str .. string.format( "%q", key )
else
str = str .. tostring( key )
end
str = str .. " = "
end
local t = type( value )
if t == "table" and not done[ value ] then
done[ value ] = true
str = str .. string.format( "(table: array size=%i, total values=%i)\n", #value, Count(value) )
for k, v in pairs( value ) do
str = str .. VardumpHelper( v, depth+1, k, done )
end
elseif t == "string" then
str = str .. string.format( "%q\n", value )
else
str = str .. tostring( value ) .. "\n"
end
return str
end
--[[
Function: Vardump
Returns useful, readable information about variables.
Parameters:
... - Accepts any number of parameters of *any type* and prints them one by one.
Returns:
A readable *string* serialization of the data passed in.
Example:
:Vardump( { "foo", apple="green", floor=41, shopping={ "milk", "cookies" } } )
returns the string...
:(table: array size=1, total values=4)
: 1 = "foo"
: "apple" = "green"
: "floor" = 41
: "shopping" = (table: array size=2, total values=2)
: 1 = "milk"
: 2 = "cookies"
Notes:
* A string will always be surrounded by quotes and a number will always stand by itself.
This is to make it easier to identify numbers stored as strings.
* Array size and total size are shown in the table header. Array size is the result of the
pound operator (#) on the table, total size is the result of <Count>. Array size is
useful debug information when iterating over a table with ipairs or fori.
Revisions:
v1.00 - Initial.
]]
function Vardump( ... )
local str = ""
local t = { ... }
for i=1, select( "#", ... ) do
str = str .. VardumpHelper( t[ i ], 0, nil, {} )
end
return str:sub( 1, -2 ) -- Remove last newline
end
--[[
Function: ThrowBadArg
"Throws" an error similar to the lua standard error of "bad argument #x to <fn_name> (<type>
expected, got <type>)".
Parameters:
argnum - The *optional argument number* that was bad.
fn_name - The *optional string* of the function name being called.
expected - The *optional string or list table* of the type(s) you expected.
data - *Optional and any type*, the actual data you got.
throw_level - The *optional number* of how many levels up to throw the error. Defaults to _3_.
Returns:
*Never returns, throws an error.*
Revisions:
v1.00 - Initial.
]]
function ThrowBadArg( argnum, fn_name, expected, data, throw_level )
if expected and type( expected ) == "string" then
expected = { expected }
end
local str = "bad argument"
if argnum then
str = str .. " #" .. tostring( argnum )
end
if fn_name then
str = str .. " to " .. fn_name
end
if expected or data then
str = str .. " ("
if expected then
str = str .. table.concat( expected, " or " ) .. " expected"
end
if expected and data then
str = str .. ", "
end
if data then
str = str .. "got " .. type( data )
end
str = str .. ")"
end
error( str, throw_level or 3 )
end
--[[
Function: CheckArg
Used to check to see if a function argument matches what is expected. If it doesn't, call
<ThrowBadArg>. This function is primarily useful at the beginning of a function definition to
ensure that the correct type of data was passed in.
Parameters:
argnum - The *optional argument number* that was bad.
fn_name - The *optional string* of the function name being called.
expected - The *optional string or list table* of the type(s) you expected.
data - *Optional and any type*, the actual data you got.
throw_level - The *optional number* of how many levels up to throw the error. Defaults to _4_.
Returns:
*Never returns* if the data is bad since it throws an error. Otherwise returns *true*.
Revisions:
v1.00 - Initial.
]]
function CheckArg( argnum, fn_name, expected, data, throw_level )
local is_str = type( expected ) == "string"
if (is_str and type( data ) == expected) or (not is_str and (HasValueI( expected, type( data ) ))) then
return true
else
return ThrowBadArg( argnum, fn_name, expected, data, throw_level or 4 )
end
end