@thigasdevelopment/luam 0.19.12 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lua/async.lua +6 -6
- package/lua/class.lua +239 -211
- package/lua/development-logs-client.lua +15 -9
- package/lua/development-logs-server.lua +41 -44
- package/lua/math.lua +21 -18
- package/lua/promise.lua +446 -0
- package/lua/string.lua +78 -62
- package/lua/table.lua +57 -49
- package/lua/threads.lua +326 -269
- package/lua/validate.lua +230 -185
- package/luam.mjs +969 -134
- package/package.json +1 -1
package/lua/validate.lua
CHANGED
|
@@ -1,239 +1,284 @@
|
|
|
1
|
-
local MAX_DEPTH = 16
|
|
2
|
-
local MAX_ENTRIES = 4096
|
|
3
|
-
local MAX_STRING = 65536
|
|
1
|
+
local MAX_DEPTH = 16;
|
|
2
|
+
local MAX_ENTRIES = 4096;
|
|
3
|
+
local MAX_STRING = 65536;
|
|
4
4
|
|
|
5
|
-
local describe
|
|
5
|
+
local describe, check;
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
---@param path string
|
|
8
|
+
---@param segment string
|
|
9
|
+
---@return string
|
|
10
|
+
local function joinPath (path, segment)
|
|
11
|
+
if (path == '') then
|
|
12
|
+
return segment;
|
|
13
|
+
end
|
|
11
14
|
|
|
12
|
-
|
|
15
|
+
return path .. '.' .. segment;
|
|
13
16
|
end
|
|
14
17
|
|
|
15
|
-
|
|
16
|
-
|
|
18
|
+
---@param descriptor table
|
|
19
|
+
---@return string
|
|
20
|
+
local function describeUnion (descriptor)
|
|
21
|
+
local parts = { };
|
|
17
22
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
23
|
+
for index = 1, #descriptor.options do
|
|
24
|
+
parts[index] = describe (descriptor.options[index]);
|
|
25
|
+
end
|
|
21
26
|
|
|
22
|
-
|
|
27
|
+
return table.concat (parts, ' | ');
|
|
23
28
|
end
|
|
24
29
|
|
|
25
|
-
|
|
26
|
-
|
|
30
|
+
---@param descriptor table
|
|
31
|
+
---@return string
|
|
32
|
+
describe = function (descriptor)
|
|
33
|
+
local kind = descriptor.kind;
|
|
27
34
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
35
|
+
if (kind == 'literal') then
|
|
36
|
+
if (type (descriptor.value) == 'string') then
|
|
37
|
+
return '\'' .. descriptor.value .. '\'';
|
|
38
|
+
end
|
|
32
39
|
|
|
33
|
-
|
|
34
|
-
|
|
40
|
+
return tostring (descriptor.value);
|
|
41
|
+
end
|
|
35
42
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
43
|
+
if (kind == 'optional') then
|
|
44
|
+
return describe (descriptor.element) .. '?';
|
|
45
|
+
end
|
|
39
46
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
47
|
+
if (kind == 'array') then
|
|
48
|
+
return describe (descriptor.element) .. '[]';
|
|
49
|
+
end
|
|
43
50
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
51
|
+
if (kind == 'map') then
|
|
52
|
+
return 'table<' .. describe (descriptor.key) .. ', ' .. describe (descriptor.value) .. '>';
|
|
53
|
+
end
|
|
47
54
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
55
|
+
if (kind == 'union') then
|
|
56
|
+
return describeUnion (descriptor);
|
|
57
|
+
end
|
|
51
58
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
59
|
+
if (kind == 'record') or (kind == 'instance') then
|
|
60
|
+
return descriptor.name;
|
|
61
|
+
end
|
|
55
62
|
|
|
56
|
-
|
|
63
|
+
return kind;
|
|
57
64
|
end
|
|
58
65
|
|
|
59
|
-
|
|
60
|
-
|
|
66
|
+
---@param path string
|
|
67
|
+
---@param descriptor table
|
|
68
|
+
---@param reason? string
|
|
69
|
+
local function fail (path, descriptor, reason)
|
|
70
|
+
local place = (path == '') and 'value' or ('"' .. path .. '"');
|
|
61
71
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
72
|
+
if (reason ~= nil) then
|
|
73
|
+
error ('luam-validate: ' .. place .. ' ' .. reason, 0);
|
|
74
|
+
end
|
|
65
75
|
|
|
66
|
-
|
|
76
|
+
error ('luam-validate: ' .. place .. ' expected "' .. describe (descriptor) .. '"', 0);
|
|
67
77
|
end
|
|
68
78
|
|
|
69
|
-
|
|
70
|
-
|
|
79
|
+
---@param value table
|
|
80
|
+
---@param path string
|
|
81
|
+
---@param descriptor table
|
|
82
|
+
---@return number
|
|
83
|
+
local function countEntries (value, path, descriptor)
|
|
84
|
+
local total = 0;
|
|
71
85
|
|
|
72
|
-
|
|
73
|
-
|
|
86
|
+
for _ in pairs (value) do
|
|
87
|
+
total = total + 1;
|
|
74
88
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
89
|
+
if (total > MAX_ENTRIES) then
|
|
90
|
+
fail (path, descriptor, 'has more than ' .. MAX_ENTRIES .. ' entries');
|
|
91
|
+
end
|
|
92
|
+
end
|
|
79
93
|
|
|
80
|
-
|
|
94
|
+
return total;
|
|
81
95
|
end
|
|
82
96
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
97
|
+
---@param value table
|
|
98
|
+
---@param name string
|
|
99
|
+
---@return boolean
|
|
100
|
+
local function isInstanceOf (value, name)
|
|
101
|
+
local meta = getmetatable (value);
|
|
102
|
+
local definition = meta and meta.__index;
|
|
103
|
+
local depth = 0;
|
|
87
104
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
105
|
+
while (type (definition) == 'table') and (depth <= MAX_DEPTH) do
|
|
106
|
+
if (definition.__name == name) then
|
|
107
|
+
return true;
|
|
108
|
+
end
|
|
92
109
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
110
|
+
definition = definition.__super;
|
|
111
|
+
depth = depth + 1;
|
|
112
|
+
end
|
|
96
113
|
|
|
97
|
-
|
|
114
|
+
return false;
|
|
98
115
|
end
|
|
99
116
|
|
|
100
|
-
|
|
117
|
+
---@param value table
|
|
118
|
+
---@param descriptor table
|
|
119
|
+
---@param path string
|
|
120
|
+
---@param depth number
|
|
121
|
+
---@return table
|
|
122
|
+
local function checkRecord (value, descriptor, path, depth)
|
|
123
|
+
countEntries (value, path, descriptor);
|
|
101
124
|
|
|
102
|
-
|
|
103
|
-
|
|
125
|
+
for index = 1, #descriptor.members do
|
|
126
|
+
local member = descriptor.members[index];
|
|
104
127
|
|
|
105
|
-
|
|
106
|
-
|
|
128
|
+
check (value[member.key], member.value, joinPath (path, member.key), depth + 1);
|
|
129
|
+
end
|
|
107
130
|
|
|
108
|
-
|
|
109
|
-
end
|
|
110
|
-
|
|
111
|
-
return value
|
|
131
|
+
return value;
|
|
112
132
|
end
|
|
113
133
|
|
|
114
|
-
|
|
115
|
-
|
|
134
|
+
---@param value table
|
|
135
|
+
---@param descriptor table
|
|
136
|
+
---@param path string
|
|
137
|
+
---@param depth number
|
|
138
|
+
---@return table
|
|
139
|
+
local function checkArray (value, descriptor, path, depth)
|
|
140
|
+
local total = countEntries (value, path, descriptor);
|
|
116
141
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
142
|
+
for index = 1, total do
|
|
143
|
+
check (value[index], descriptor.element, path .. '[' .. index .. ']', depth + 1);
|
|
144
|
+
end
|
|
120
145
|
|
|
121
|
-
|
|
146
|
+
return value;
|
|
122
147
|
end
|
|
123
148
|
|
|
124
|
-
|
|
125
|
-
|
|
149
|
+
---@param value table
|
|
150
|
+
---@param descriptor table
|
|
151
|
+
---@param path string
|
|
152
|
+
---@param depth number
|
|
153
|
+
---@return table
|
|
154
|
+
local function checkMap (value, descriptor, path, depth)
|
|
155
|
+
countEntries (value, path, descriptor);
|
|
126
156
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
157
|
+
for key, entry in pairs (value) do
|
|
158
|
+
check (key, descriptor.key, joinPath (path, tostring (key)) .. ' (key)', depth + 1);
|
|
159
|
+
check (entry, descriptor.value, joinPath (path, tostring (key)), depth + 1);
|
|
160
|
+
end
|
|
131
161
|
|
|
132
|
-
|
|
162
|
+
return value;
|
|
133
163
|
end
|
|
134
164
|
|
|
135
|
-
|
|
136
|
-
|
|
165
|
+
---@param value any
|
|
166
|
+
---@param descriptor table
|
|
167
|
+
---@param depth number
|
|
168
|
+
---@return boolean
|
|
169
|
+
local function matches (value, descriptor, depth)
|
|
170
|
+
local ok = pcall (check, value, descriptor, '', depth);
|
|
137
171
|
|
|
138
|
-
|
|
172
|
+
return ok;
|
|
139
173
|
end
|
|
140
174
|
|
|
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
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
175
|
+
---@param value any
|
|
176
|
+
---@param descriptor table
|
|
177
|
+
---@param path string
|
|
178
|
+
---@param depth number
|
|
179
|
+
---@return any
|
|
180
|
+
check = function (value, descriptor, path, depth)
|
|
181
|
+
if (depth > MAX_DEPTH) then
|
|
182
|
+
fail (path, descriptor, 'is nested deeper than ' .. MAX_DEPTH .. ' levels');
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
local kind = descriptor.kind;
|
|
186
|
+
|
|
187
|
+
if (kind == 'any') then
|
|
188
|
+
return value;
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
if (kind == 'optional') then
|
|
192
|
+
if (value == nil) then
|
|
193
|
+
return value;
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
return check (value, descriptor.element, path, depth);
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
if (kind == 'nil') then
|
|
200
|
+
if (value ~= nil) then
|
|
201
|
+
fail (path, descriptor);
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
return value;
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
if (kind == 'literal') then
|
|
208
|
+
if (value ~= descriptor.value) then
|
|
209
|
+
fail (path, descriptor);
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
return value;
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
if (kind == 'string') or (kind == 'number') or (kind == 'boolean') or (kind == 'userdata') or (kind == 'thread') or (kind == 'function') then
|
|
216
|
+
if (type (value) ~= kind) then
|
|
217
|
+
fail (path, descriptor);
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
if (kind == 'string') and (#value > MAX_STRING) then
|
|
221
|
+
fail (path, descriptor, 'is longer than ' .. MAX_STRING .. ' characters');
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
return value;
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
if (kind == 'table') then
|
|
228
|
+
if (type (value) ~= 'table') then
|
|
229
|
+
fail (path, descriptor);
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
return value;
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
if (kind == 'union') then
|
|
236
|
+
for index = 1, #descriptor.options do
|
|
237
|
+
if (matches (value, descriptor.options[index], depth)) then
|
|
238
|
+
return value;
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
fail (path, descriptor);
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
if (type (value) ~= 'table') then
|
|
246
|
+
fail (path, descriptor);
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
if (kind == 'record') then
|
|
250
|
+
return checkRecord (value, descriptor, path, depth);
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
if (kind == 'array') then
|
|
254
|
+
return checkArray (value, descriptor, path, depth);
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
if (kind == 'map') then
|
|
258
|
+
return checkMap (value, descriptor, path, depth);
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
if (kind == 'instance') then
|
|
262
|
+
if (not isInstanceOf (value, descriptor.name)) then
|
|
263
|
+
fail (path, descriptor);
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
return value;
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
return value;
|
|
231
270
|
end
|
|
232
271
|
|
|
233
|
-
|
|
234
|
-
|
|
272
|
+
---@param value any
|
|
273
|
+
---@param descriptor table
|
|
274
|
+
---@return any
|
|
275
|
+
function __luam_validate (value, descriptor)
|
|
276
|
+
return check (value, descriptor, '', 1);
|
|
235
277
|
end
|
|
236
278
|
|
|
237
|
-
|
|
238
|
-
|
|
279
|
+
---@param value any
|
|
280
|
+
---@param descriptor table
|
|
281
|
+
---@return boolean
|
|
282
|
+
function __luam_matches (value, descriptor)
|
|
283
|
+
return matches (value, descriptor, 1);
|
|
239
284
|
end
|