@thigasdevelopment/luam 1.0.0 → 1.0.2
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 +301 -47
- package/package.json +1 -1
package/lua/async.lua
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
---@class Async
|
|
2
2
|
---@field tasks Threads
|
|
3
3
|
---@field interval number
|
|
4
|
-
---@field map fun(self: Async, array: table, func: fun(value: any, index: number): any, callback?: fun(elapsed: number): any): number
|
|
5
|
-
---@field iterate fun(self: Async, from: number, to: number, increment: number, func: fun(i: number): any, callback?: fun(elapsed: number): any): number
|
|
6
|
-
---@field foreach fun(self: Async, object: table<any, any>, func: fun(value: any, key: any): any, callback?: fun(elapsed: number): any): number
|
|
4
|
+
---@field map fun(self: Async, array: table, func: fun(value: any, index: number): any, callback?: fun(elapsed: number): any): number, Promise
|
|
5
|
+
---@field iterate fun(self: Async, from: number, to: number, increment: number, func: fun(i: number): any, callback?: fun(elapsed: number): any): number, Promise
|
|
6
|
+
---@field foreach fun(self: Async, object: table<any, any>, func: fun(value: any, key: any): any, callback?: fun(elapsed: number): any): number, Promise
|
|
7
7
|
---@field getInterval fun(self: Async): number
|
|
8
8
|
---@field setInterval fun(self: Async, interval: number): boolean
|
|
9
9
|
Async = {
|
|
@@ -23,7 +23,7 @@ Async = {
|
|
|
23
23
|
---@param array table
|
|
24
24
|
---@param func fun(value: any, index: number): any
|
|
25
25
|
---@param callback? fun(elapsed: number): any
|
|
26
|
-
---@return number
|
|
26
|
+
---@return number, Promise
|
|
27
27
|
map = function (self, array, func, callback)
|
|
28
28
|
return self.tasks:add (
|
|
29
29
|
function (self)
|
|
@@ -49,7 +49,7 @@ Async = {
|
|
|
49
49
|
---@param increment number
|
|
50
50
|
---@param func fun(i: number): any
|
|
51
51
|
---@param callback? fun(elapsed: number): any
|
|
52
|
-
---@return number
|
|
52
|
+
---@return number, Promise
|
|
53
53
|
iterate = function (self, from, to, increment, func, callback)
|
|
54
54
|
return self.tasks:add (
|
|
55
55
|
function (self)
|
|
@@ -74,7 +74,7 @@ Async = {
|
|
|
74
74
|
---@param object table<any, any>
|
|
75
75
|
---@param func fun(value: any, key: any): any
|
|
76
76
|
---@param callback? fun(elapsed: number): any
|
|
77
|
-
---@return number
|
|
77
|
+
---@return number, Promise
|
|
78
78
|
foreach = function (self, object, func, callback)
|
|
79
79
|
return self.tasks:add (
|
|
80
80
|
function (self)
|
package/lua/class.lua
CHANGED
|
@@ -1,280 +1,308 @@
|
|
|
1
|
-
local classes = {}
|
|
2
|
-
|
|
3
|
-
local constructors = {}
|
|
1
|
+
local classes = { };
|
|
2
|
+
local constructors = { };
|
|
4
3
|
|
|
5
4
|
local ALLOWED_METAMETHODS = {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
5
|
+
['__tostring'] = true,
|
|
6
|
+
['__eq'] = true,
|
|
7
|
+
['__lt'] = true,
|
|
8
|
+
['__le'] = true,
|
|
9
|
+
['__len'] = true,
|
|
10
|
+
['__concat'] = true,
|
|
11
|
+
['__unm'] = true,
|
|
12
|
+
['__add'] = true,
|
|
13
|
+
['__sub'] = true,
|
|
14
|
+
['__mul'] = true,
|
|
15
|
+
['__div'] = true,
|
|
16
|
+
['__mod'] = true,
|
|
17
|
+
['__pow'] = true,
|
|
18
|
+
};
|
|
20
19
|
|
|
21
20
|
local BLOCKED_METAMETHODS = {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
21
|
+
['__call'] = true,
|
|
22
|
+
['__index'] = true,
|
|
23
|
+
['__newindex'] = true,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
---@param value fun(self: table, ...): any
|
|
27
|
+
---@param inherited fun(self: table, ...): any
|
|
28
|
+
---@return fun(self: table, ...): any
|
|
29
|
+
local function bindSuper (value, inherited)
|
|
30
|
+
return function (self, ...)
|
|
31
|
+
local previous = rawget (self, 'super');
|
|
32
|
+
|
|
33
|
+
self.super = function (_, ...)
|
|
34
|
+
return inherited (self, ...);
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
local result = value (self, ...);
|
|
38
|
+
self.super = previous;
|
|
39
|
+
|
|
40
|
+
return result;
|
|
41
|
+
end
|
|
41
42
|
end
|
|
42
43
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
---@param definition table
|
|
45
|
+
---@param key string
|
|
46
|
+
---@param value fun(self: table, ...): any
|
|
47
|
+
---@return fun(self: table, ...): any
|
|
48
|
+
local function bindLateSuper (definition, key, value)
|
|
49
|
+
return function (self, ...)
|
|
50
|
+
local inherited = definition.__super and definition.__super[key];
|
|
46
51
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
52
|
+
if (type (inherited) ~= 'function') then
|
|
53
|
+
return value (self, ...);
|
|
54
|
+
end
|
|
50
55
|
|
|
51
|
-
|
|
52
|
-
|
|
56
|
+
return bindSuper (value, inherited) (self, ...);
|
|
57
|
+
end
|
|
53
58
|
end
|
|
54
59
|
|
|
55
|
-
|
|
56
|
-
|
|
60
|
+
---@param name string
|
|
61
|
+
---@return table
|
|
62
|
+
local function declare (name)
|
|
63
|
+
local existing = classes[name];
|
|
64
|
+
if (existing) then
|
|
65
|
+
return existing;
|
|
66
|
+
end
|
|
57
67
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
end
|
|
68
|
+
local definition = { __name = name, __pending = true };
|
|
69
|
+
classes[name] = definition;
|
|
61
70
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
classes[name] = definition
|
|
65
|
-
|
|
66
|
-
return definition
|
|
71
|
+
return definition;
|
|
67
72
|
end
|
|
68
73
|
|
|
69
|
-
|
|
70
|
-
|
|
74
|
+
---@param definition table
|
|
75
|
+
---@return string?
|
|
76
|
+
local function pendingAncestor (definition)
|
|
77
|
+
local current = definition.__super;
|
|
71
78
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
79
|
+
while (current) do
|
|
80
|
+
if (current.__pending) then
|
|
81
|
+
return current.__name;
|
|
82
|
+
end
|
|
76
83
|
|
|
77
|
-
|
|
78
|
-
|
|
84
|
+
current = current.__super;
|
|
85
|
+
end
|
|
79
86
|
|
|
80
|
-
|
|
87
|
+
return nil;
|
|
81
88
|
end
|
|
82
89
|
|
|
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
|
-
|
|
90
|
+
---@param name string
|
|
91
|
+
---@param struct table
|
|
92
|
+
---@param options table
|
|
93
|
+
---@return table
|
|
94
|
+
local function create (name, struct, options)
|
|
95
|
+
local definition = classes[name];
|
|
96
|
+
|
|
97
|
+
if (definition) and (not definition.__pending) then
|
|
98
|
+
error ('Class ' .. tostring (name) .. ' already exists.');
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
definition = definition or { __name = name };
|
|
102
|
+
definition.__pending = false;
|
|
103
|
+
definition.__super = options.super;
|
|
104
|
+
definition.__metamethods = options.metamethods;
|
|
105
|
+
|
|
106
|
+
local pending = definition.__super and definition.__super.__pending;
|
|
107
|
+
|
|
108
|
+
for key, value in pairs (struct) do
|
|
109
|
+
local inherited = definition.__super and definition.__super[key];
|
|
110
|
+
|
|
111
|
+
if (type (value) ~= 'function') then
|
|
112
|
+
definition[key] = value;
|
|
113
|
+
elseif (pending) then
|
|
114
|
+
definition[key] = bindLateSuper (definition, key, value);
|
|
115
|
+
elseif (type (inherited) == 'function') then
|
|
116
|
+
definition[key] = bindSuper (value, inherited);
|
|
117
|
+
else
|
|
118
|
+
definition[key] = value;
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
if (definition.__super) then
|
|
123
|
+
setmetatable (definition, { __index = definition.__super });
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
classes[name] = definition;
|
|
127
|
+
|
|
128
|
+
return definition;
|
|
119
129
|
end
|
|
120
130
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
extends = function(self, super)
|
|
126
|
-
options.super = declare(super)
|
|
131
|
+
---@param name string
|
|
132
|
+
---@return table
|
|
133
|
+
function class (name)
|
|
134
|
+
local options = { super = nil, metamethods = nil };
|
|
127
135
|
|
|
128
|
-
|
|
129
|
-
|
|
136
|
+
local modifiers = {
|
|
137
|
+
extends = function (self, super)
|
|
138
|
+
options.super = declare (super);
|
|
130
139
|
|
|
131
|
-
|
|
132
|
-
|
|
140
|
+
return self;
|
|
141
|
+
end,
|
|
133
142
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
143
|
+
metamethods = function (self, metamethods)
|
|
144
|
+
options.metamethods = metamethods;
|
|
137
145
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
return modifiers[key]
|
|
142
|
-
end
|
|
146
|
+
return self;
|
|
147
|
+
end,
|
|
148
|
+
};
|
|
143
149
|
|
|
144
|
-
|
|
150
|
+
return setmetatable ({ }, {
|
|
151
|
+
__index = function (_, key)
|
|
152
|
+
if (modifiers[key]) then
|
|
153
|
+
return modifiers[key];
|
|
154
|
+
end
|
|
145
155
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
156
|
+
local definition = classes[name];
|
|
157
|
+
if (not definition) then
|
|
158
|
+
return nil;
|
|
159
|
+
end
|
|
149
160
|
|
|
150
|
-
|
|
151
|
-
|
|
161
|
+
return definition[key];
|
|
162
|
+
end,
|
|
152
163
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
164
|
+
__call = function (_, struct)
|
|
165
|
+
return create (name, struct, options);
|
|
166
|
+
end,
|
|
167
|
+
});
|
|
157
168
|
end
|
|
158
169
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
170
|
+
---@param meta table
|
|
171
|
+
---@param source table
|
|
172
|
+
local function installMetamethods (meta, source)
|
|
173
|
+
for key, value in pairs (source) do
|
|
174
|
+
if (BLOCKED_METAMETHODS[key]) then
|
|
175
|
+
error ('Cannot override metamethod ' .. tostring (key) .. '.');
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
if (ALLOWED_METAMETHODS[key]) then
|
|
179
|
+
meta[key] = value;
|
|
180
|
+
end
|
|
181
|
+
end
|
|
169
182
|
end
|
|
170
183
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
184
|
+
---@param meta table
|
|
185
|
+
---@param definition table
|
|
186
|
+
local function inheritedMetamethods (meta, definition)
|
|
187
|
+
local chain = { };
|
|
188
|
+
local current = definition.__super;
|
|
174
189
|
|
|
175
|
-
|
|
176
|
-
|
|
190
|
+
while (type (current) == 'table') do
|
|
191
|
+
table.insert (chain, current);
|
|
177
192
|
|
|
178
|
-
|
|
179
|
-
|
|
193
|
+
current = current.__super;
|
|
194
|
+
end
|
|
180
195
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
196
|
+
for index = #chain, 1, -1 do
|
|
197
|
+
installMetamethods (meta, chain[index]);
|
|
198
|
+
end
|
|
184
199
|
end
|
|
185
200
|
|
|
186
|
-
|
|
187
|
-
|
|
201
|
+
---@param definition table
|
|
202
|
+
---@return table
|
|
203
|
+
local function instanceMetatable (definition)
|
|
204
|
+
local meta = { __index = definition };
|
|
188
205
|
|
|
189
|
-
|
|
190
|
-
|
|
206
|
+
inheritedMetamethods (meta, definition);
|
|
207
|
+
installMetamethods (meta, definition);
|
|
191
208
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
209
|
+
if (definition.__metamethods) then
|
|
210
|
+
installMetamethods (meta, definition.__metamethods);
|
|
211
|
+
end
|
|
195
212
|
|
|
196
|
-
|
|
213
|
+
return meta;
|
|
197
214
|
end
|
|
198
215
|
|
|
199
|
-
|
|
200
|
-
|
|
216
|
+
---@param definition table
|
|
217
|
+
---@return fun(...): table
|
|
218
|
+
local function createConstructor (definition)
|
|
219
|
+
local meta = instanceMetatable (definition);
|
|
201
220
|
|
|
202
|
-
|
|
203
|
-
|
|
221
|
+
return function (...)
|
|
222
|
+
local instance = setmetatable ({ }, meta);
|
|
204
223
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
224
|
+
if (type (instance.constructor) == 'function') then
|
|
225
|
+
instance:constructor (...);
|
|
226
|
+
end
|
|
208
227
|
|
|
209
|
-
|
|
210
|
-
|
|
228
|
+
return instance;
|
|
229
|
+
end
|
|
211
230
|
end
|
|
212
231
|
|
|
213
|
-
|
|
214
|
-
|
|
232
|
+
---@param name string
|
|
233
|
+
---@return fun(...): table
|
|
234
|
+
function new (name)
|
|
235
|
+
local definition = classes[name];
|
|
215
236
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
237
|
+
if (not definition) or (definition.__pending) then
|
|
238
|
+
error ('Class ' .. tostring (name) .. ' is not defined.');
|
|
239
|
+
end
|
|
219
240
|
|
|
220
|
-
|
|
241
|
+
local constructor = constructors[definition];
|
|
242
|
+
if (not constructor) then
|
|
243
|
+
local missing = pendingAncestor (definition);
|
|
221
244
|
|
|
222
|
-
|
|
223
|
-
|
|
245
|
+
if (missing) then
|
|
246
|
+
error ('Class ' .. tostring (name) .. ' extends ' .. tostring (missing) .. ', which is not defined.');
|
|
247
|
+
end
|
|
224
248
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
249
|
+
constructor = createConstructor (definition);
|
|
250
|
+
constructors[definition] = constructor;
|
|
251
|
+
end
|
|
228
252
|
|
|
229
|
-
|
|
230
|
-
constructors[definition] = constructor
|
|
231
|
-
end
|
|
232
|
-
|
|
233
|
-
return constructor
|
|
253
|
+
return constructor;
|
|
234
254
|
end
|
|
235
255
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
256
|
+
---@param func fun(...): any
|
|
257
|
+
---@param self table
|
|
258
|
+
---@return fun(...): any
|
|
259
|
+
function bind (func, self)
|
|
260
|
+
if (type (func) ~= 'function') then
|
|
261
|
+
error ('Cannot bind a value of type ' .. type (func) .. '.');
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
return function (...)
|
|
265
|
+
return func (self, ...);
|
|
266
|
+
end
|
|
244
267
|
end
|
|
245
268
|
|
|
246
|
-
|
|
247
|
-
|
|
269
|
+
---@param names table
|
|
270
|
+
---@return table<string, number>
|
|
271
|
+
function enum (names)
|
|
272
|
+
local values = { };
|
|
248
273
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
274
|
+
if (type (names) ~= 'table') then
|
|
275
|
+
return values;
|
|
276
|
+
end
|
|
252
277
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
278
|
+
for index = 1, #names do
|
|
279
|
+
values[names[index]] = index - 1;
|
|
280
|
+
end
|
|
256
281
|
|
|
257
|
-
|
|
282
|
+
return values;
|
|
258
283
|
end
|
|
259
284
|
|
|
260
|
-
|
|
261
|
-
|
|
285
|
+
---@return table<string, table>
|
|
286
|
+
function getClasses ()
|
|
287
|
+
local defined = { };
|
|
262
288
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
289
|
+
for name, definition in pairs (classes) do
|
|
290
|
+
if (not definition.__pending) then
|
|
291
|
+
defined[name] = definition;
|
|
292
|
+
end
|
|
293
|
+
end
|
|
268
294
|
|
|
269
|
-
|
|
295
|
+
return defined;
|
|
270
296
|
end
|
|
271
297
|
|
|
272
|
-
|
|
273
|
-
|
|
298
|
+
---@param name string
|
|
299
|
+
---@return table?
|
|
300
|
+
function getClass (name)
|
|
301
|
+
local definition = classes[name];
|
|
274
302
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
303
|
+
if (not definition) or (definition.__pending) then
|
|
304
|
+
return nil;
|
|
305
|
+
end
|
|
278
306
|
|
|
279
|
-
|
|
307
|
+
return definition;
|
|
280
308
|
end
|
|
@@ -1,13 +1,19 @@
|
|
|
1
|
-
local luamOriginalOutputDebugString = outputDebugString
|
|
2
|
-
local luamDevelopmentEvent =
|
|
3
|
-
local luamMaximumMessageLength = __LUAM_MAX_MESSAGE_LENGTH__
|
|
1
|
+
local luamOriginalOutputDebugString = outputDebugString;
|
|
2
|
+
local luamDevelopmentEvent = 'luam:development-log';
|
|
3
|
+
local luamMaximumMessageLength = __LUAM_MAX_MESSAGE_LENGTH__; -- Replaced at build time with the limit the manifest sets.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
---@param message string
|
|
6
|
+
---@param level? number
|
|
7
|
+
---@param red? number
|
|
8
|
+
---@param green? number
|
|
9
|
+
---@param blue? number
|
|
10
|
+
---@return boolean
|
|
11
|
+
function outputDebugString (message, level, red, green, blue)
|
|
12
|
+
local result = luamOriginalOutputDebugString (message, level, red, green, blue);
|
|
7
13
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
14
|
+
if (type (message) == 'string') and (#message <= luamMaximumMessageLength) and (level == nil or type (level) == 'number') then
|
|
15
|
+
triggerServerEvent (luamDevelopmentEvent, resourceRoot, message, level or 3);
|
|
16
|
+
end
|
|
11
17
|
|
|
12
|
-
|
|
18
|
+
return result;
|
|
13
19
|
end
|