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