-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.luajit52.d.ts
2490 lines (2234 loc) · 124 KB
/
lib.luajit52.d.ts
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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//MIT License
//
//Copyright (c) 2020 Tom Blind
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
declare interface LuaThread { readonly ____luaThread: never; }
declare interface LuaUserData { readonly ____luaUserData: never; }
declare interface LuaLightUserData { readonly ____luaLightUserData: never; }
/**
* Issues an error when the value of its argument `v` is false (i.e., nil or false); otherwise, returns all its
* arguments. `message` is an error message; when absent, it defaults to "assertion failed!"
*/
declare function assert<V>(this: void, v: V): Exclude<V, undefined | null | false>;
/**
* Issues an error when the value of its argument `v` is false (i.e., nil or false); otherwise, returns all its
* arguments. `message` is an error message; when absent, it defaults to "assertion failed!"
*/
/** @tupleReturn */
declare function assert<V extends [unknown, ...unknown[]]>(
this: void,
...v: V
): { [I in keyof V]: I extends "0" ? Exclude<V[I], undefined | null | false> : V[I] };
/**
* This function is a generic interface to the garbage collector. It performs different functions according to its first
* argument, `opt`:
*
* - "collect": performs a full garbage-collection cycle. This is the default option.
* - "stop": stops the garbage collector.
* - "restart": restarts the garbage collector.
* - "count": returns the total memory in use by Lua (in Kbytes).
* - "step": performs a garbage-collection step. The step "size" is controlled by `arg` (larger values mean more steps)
* in a non-specified way. If you want to control the step size you must experimentally tune the value of `arg`.
* Returns true if the step finished a collection cycle.
* - "setpause": sets `arg` as the new value for the pause of the collector (see 2.10). Returns the previous value for
* pause.
* - "setstepmul": sets `arg` as the new value for the step multiplier of the collector (see 2.10). Returns the previous
* value for step.
*/
declare function collectgarbage(this: void, opt?: "collect" | "stop" | "restart"): void;
/**
* This function is a generic interface to the garbage collector. It performs different functions according to its first
* argument, `opt`:
*
* - "collect": performs a full garbage-collection cycle. This is the default option.
* - "stop": stops the garbage collector.
* - "restart": restarts the garbage collector.
* - "count": returns the total memory in use by Lua (in Kbytes).
* - "step": performs a garbage-collection step. The step "size" is controlled by `arg` (larger values mean more steps)
* in a non-specified way. If you want to control the step size you must experimentally tune the value of `arg`.
* Returns true if the step finished a collection cycle.
* - "setpause": sets `arg` as the new value for the pause of the collector (see 2.10). Returns the previous value for
* pause.
* - "setstepmul": sets `arg` as the new value for the step multiplier of the collector (see 2.10). Returns the previous
* value for step.
*/
declare function collectgarbage(this: void, opt: "count"): number;
/**
* This function is a generic interface to the garbage collector. It performs different functions according to its first
* argument, `opt`:
*
* - "collect": performs a full garbage-collection cycle. This is the default option.
* - "stop": stops the garbage collector.
* - "restart": restarts the garbage collector.
* - "count": returns the total memory in use by Lua (in Kbytes).
* - "step": performs a garbage-collection step. The step "size" is controlled by `arg` (larger values mean more steps)
* in a non-specified way. If you want to control the step size you must experimentally tune the value of `arg`.
* Returns true if the step finished a collection cycle.
* - "setpause": sets `arg` as the new value for the pause of the collector (see 2.10). Returns the previous value for
* pause.
* - "setstepmul": sets `arg` as the new value for the step multiplier of the collector (see 2.10). Returns the previous
* value for step.
*/
declare function collectgarbage(this: void, opt: "step", arg: number): boolean;
/**
* This function is a generic interface to the garbage collector. It performs different functions according to its first
* argument, `opt`:
*
* - "collect": performs a full garbage-collection cycle. This is the default option.
* - "stop": stops the garbage collector.
* - "restart": restarts the garbage collector.
* - "count": returns the total memory in use by Lua (in Kbytes).
* - "step": performs a garbage-collection step. The step "size" is controlled by `arg` (larger values mean more steps)
* in a non-specified way. If you want to control the step size you must experimentally tune the value of `arg`.
* Returns true if the step finished a collection cycle.
* - "setpause": sets `arg` as the new value for the pause of the collector (see 2.10). Returns the previous value for
* pause.
* - "setstepmul": sets `arg` as the new value for the step multiplier of the collector (see 2.10). Returns the previous
* value for step.
*/
declare function collectgarbage(this: void, opt: "setpause" | "setstepmul", arg: number): number;
/**
* Opens the named file and executes its contents as a Lua chunk. When called without arguments, `dofile` executes the
* contents of the standard input (`stdin`). Returns all values returned by the chunk. In case of errors, `dofile`
* propagates the error to its caller (that is, `dofile` does not run in protected mode).
*/
/** @tupleReturn */
declare function dofile(this: void, filename?: string): unknown[];
/**
* Terminates the last protected function called and returns `message` as the error message. Function `error` never
* returns. Usually, `error` adds some information about the error position at the beginning of the message. The
* `level` argument specifies how to get the error position. With level 1 (the default), the error position is where
* the `error` function was called. Level 2 points the error to where the function that called `error` was called; and
* so on. Passing a level 0 avoids the addition of error position information to the message.
*/
declare function error(this: void, message: string, level?: number): never;
/**
* A global variable (not a function) that holds the global environment (that is, `_G._G = _G`). Lua itself does not use
* this variable; changing its value does not affect any environment, nor vice-versa. (Use `setfenv` to change
* environments.)
*/
declare const _G: typeof globalThis & Record<string, unknown>;
/**
* Returns the current environment in use by the function. `f` can be a Lua function or a number that specifies the
* function at that stack level: Level 1 is the function calling `getfenv`. If the given function is not a Lua
* function, or if `f` is 0, `getfenv` returns the global environment. The default for `f` is 1.
*/
declare function getfenv(this: void, f: Function | number): unknown;
/**
* If `object` does not have a metatable, returns nil. Otherwise, if the object's metatable has a `"__metatable"` field,
* returns the associated value. Otherwise, returns the metatable of the given object.
*/
declare function getmetatable(this: void, object: unknown): unknown;
/** @luaIterator @tupleReturn */
declare interface LuaIpairsIterable<T> extends Array<[number, Exclude<T, null | undefined>]> {}
/**
* If `t` has a metamethod `__ipairs`, calls it with `t` as argument and returns the first three results from the call.
*
* Otherwise, returns three values: an iterator function, the table `t`, and 0, so that the construction
*
* for i,v in ipairs(t) do body end
*
* will iterate over the pairs (`1,t[1]`), (`2,t[2]`), ..., up to the first integer key absent from the table.
*/
declare function ipairs<T>(this: void, t: T[]): LuaIpairsIterable<T>;
/**
* Loads a chunk.
*
* If `ld` is a string, the chunk is this string. If `ld` is a function, `load` calls it repeatedly to get the chunk
* pieces. Each call to `ld` must return a string that concatenates with previous results. A return of an empty
* string, nil, or no value signals the end of the chunk.
*
* If there are no syntactic errors, returns the compiled chunk as a function; otherwise, returns nil plus the error
* message.
*
* If the resulting function has upvalues, the first upvalue is set to the value of `env`, if that parameter is given,
* or to the value of the global environment. (When you load a main chunk, the resulting function will always have
* exactly one upvalue, the `_ENV` variable (see 2.2). When you load a binary chunk created from a function (see
* `string.dump`), the resulting function can have arbitrary upvalues.)
*
* `source` is used as the source of the chunk for error messages and debug information (see 4.9). When absent, it
* defaults to `ld`, if `ld` is a string, or to "`=(load)`" otherwise.
*
* The string `mode` controls whether the chunk can be text or binary (that is, a precompiled chunk). It may be the
* string "`b`" (only binary chunks), "`t`" (only text chunks), or "`bt`" (both binary and text). The default is
* "`bt`".
*/
/** @tupleReturn */
declare function load(
this: void,
ld: string | { (this: void): string | undefined; },
source?: string,
mode?: "b" | "t" | "bt",
env?: unknown
): [{ (this: void): unknown; }, undefined] | [undefined, string];
/**
* Similar to `load`, but gets the chunk from file `filename` or from the standard input, if no file name is given.
*/
/** @tupleReturn */
declare function loadfile(
this: void,
filename?: string,
mode?: "b" | "t" | "bt",
env?: unknown
): [{ (this: void): unknown; }, undefined] | [undefined, string];
/**
* Similar to `load`, but gets the chunk from the given string.
*
* To load and run a given string, use the idiom
*
* assert(loadstring(s))()
*
* When absent, `chunkname` defaults to the given string.
*/
/** @tupleReturn */
declare function loadstring(
this: void,
string_?: string,
chunkname?: string
): [{ (this: void): unknown; }, undefined] | [undefined, string];
/**
* Allows a program to traverse all fields of a table. Its first argument is a table and its second argument is an index
* in this table. `next` returns the next index of the table and its associated value. When called with nil as its
* second argument, `next` returns an initial index and its associated value. When called with the last index, or with
* nil in an empty table, `next` returns nil. If the second argument is absent, then it is interpreted as nil. In
* particular, you can use `next(t)` to check whether a table is empty.
*
* The order in which the indices are enumerated is not specified, even for numeric indices. (To traverse a table in
* numeric order, use a numerical for or the `ipairs` function.)
*
* The behavior of `next` is undefined if, during the traversal, you assign any value to a non-existent field in the
* table. You may however modify existing fields. In particular, you may clear existing fields.
*/
/** @tupleReturn */
declare function next<T extends object>(this: void, table: T, index?: keyof T): [keyof T, T[keyof T]];
/** @luaIterator @tupleReturn */
declare interface LuaPairsIterable<T> extends Array<[keyof T, Exclude<T[keyof T], null | undefined>]> {}
/**
* If `t` has a metamethod `__pairs`, calls it with `t` as argument and returns the first three results from the call.
*
* Otherwise, returns three values: the `next` function, the table `t`, and nil, so that the construction
*
* for k,v in pairs(t) do body end
*
* will iterate over all key-value pairs of table `t`.
*
* See function `next` for the caveats of modifying the table during its traversal.
*/
declare function pairs<T extends object>(this: void, t: T): LuaPairsIterable<T>;
/**
* Calls function `f` with the given arguments in protected mode. This means that any error inside `f` is not
* propagated; instead, `pcall` catches the error and returns a status code. Its first result is the status code (a
* boolean), which is true if the call succeeds without errors. In such case, `pcall` also returns all results from
* the call, after this first result. In case of any error, `pcall` returns false plus the error message.
*/
/** @tupleReturn */
declare function pcall<T, A extends unknown[], R>(
this: void,
f: { (this: T, ...args: A): R; },
self: T,
...args: A
): [true, R] | [false, string];
/**
* Calls function `f` with the given arguments in protected mode. This means that any error inside `f` is not
* propagated; instead, `pcall` catches the error and returns a status code. Its first result is the status code (a
* boolean), which is true if the call succeeds without errors. In such case, `pcall` also returns all results from
* the call, after this first result. In case of any error, `pcall` returns false plus the error message.
*/
/** @tupleReturn */
declare function pcall<A extends unknown[], R>(
this: void,
f: { (this: void, ...args: A): R; },
...args: A
): [true, R] | [false, string];
/**
* Receives any number of arguments, and prints their values to `stdout`, using the `tostring` function to convert them
* to strings. `print` is not intended for formatted output, but only as a quick way to show a value, typically for
* debugging. For formatted output, use `string.format`.
*/
declare function print(this: void, ...args: unknown[]): void;
/**
* Checks whether `v1` is equal to `v2`, without invoking any metamethod. Returns a boolean.
*/
declare function rawequal(this: void, v1: unknown, v2: unknown): boolean;
/**
* Gets the real value of `table[index]`, without invoking any metamethod. `table` must be a table; `index` may be any
* value.
*/
declare function rawget<T extends object, I extends keyof T>(this: void, table: T, index: I): T[I];
/**
* Sets the real value of `table[index]` to `value`, without invoking any metamethod. `table` must be a table, `index`
* any value different from nil, and `value` any Lua value. This function returns `table`.
*/
declare function rawset<T extends object, I extends keyof T>(this: void, table: T, index: I, value: T[I]): void;
/**
* If `index` is a number, returns all arguments after argument number `index`. Otherwise, `index` must be the string
* `"#"`, and `select` returns the total number of extra arguments it received.
*/
/** @tupleReturn */
declare function select<A extends unknown[]>(this: void, index: number, ...args: A): A[number][];
/**
* If `index` is a number, returns all arguments after argument number `index`. Otherwise, `index` must be the string
* `"#"`, and `select` returns the total number of extra arguments it received.
*/
declare function select(this: void, index: "#", ...args: unknown[]): number;
/**
* Sets the environment to be used by the given function. `f` can be a Lua function or a number that specifies the
* function at that stack level: Level 1 is the function calling `setfenv`. `setfenv` returns the given function.
*
* As a special case, when `f` is 0 `setfenv` changes the environment of the running thread. In this case, `setfenv`
* returns no values.
*/
declare function setfenv<T extends object>(this: void, f: 0, table: T): void;
/**
* Sets the environment to be used by the given function. `f` can be a Lua function or a number that specifies the
* function at that stack level: Level 1 is the function calling `setfenv`. `setfenv` returns the given function.
*
* As a special case, when `f` is 0 `setfenv` changes the environment of the running thread. In this case, `setfenv`
* returns no values.
*/
declare function setfenv<T extends object>(this: void, f: Function | number, table: T): Function | undefined;
declare interface LuaNewIndexMetaMethod<K extends string, V> {
__newindex(this: unknown, key: K, value: V): void;
}
declare interface LuaIndexMetaMethod<K extends string, V> {
__index(this: unknown, key: K): V;
}
declare interface LuaIndexMetaTable<T> {
__index: T;
}
type LuaIndexMeta<M> = M extends LuaIndexMetaMethod<infer K, infer V>
? { [I in K]: V }
: M extends LuaIndexMetaTable<infer T> ? T : {};
type LuaMetaExtractReadonly<I, E> = {
+readonly [K in Exclude<keyof I, E>]: I[K]
};
type LuaMeta<M> = M extends LuaNewIndexMetaMethod<infer K, infer V>
? { [I in K]: V } & LuaMetaExtractReadonly<LuaIndexMeta<M>, K>
: LuaIndexMeta<M>;
/**
* Sets the metatable for the given table. (You cannot change the metatable of other types from Lua, only from C.) If
* `metatable` is nil, removes the metatable of the given table. If the original metatable has a `"__metatable"`
* field, raises an error.
*
* This function returns `table`.
*/
declare function setmetatable<T extends object, M extends object>(this: void, table: T, metatable?: M): T & LuaMeta<M>;
/**
* Tries to convert its argument to a number. If the argument is already a number or a string convertible to a number,
* then `tonumber` returns this number; otherwise, it returns nil. An optional argument specifies the base to
* interpret the numeral. The base may be any integer between 2 and 36, inclusive. In bases above 10, the letter '`A`'
* (in either upper or lower case) represents 10, '`B`' represents 11, and so forth, with '`Z`' representing 35. In
* base 10 (the default), the number can have a decimal part, as well as an optional exponent part (see 2.1). In other
* bases, only unsigned integers are accepted.
*/
declare function tonumber(this: void, e: unknown): number | undefined;
/**
* Tries to convert its argument to a number. If the argument is already a number or a string convertible to a number,
* then `tonumber` returns this number; otherwise, it returns nil. An optional argument specifies the base to
* interpret the numeral. The base may be any integer between 2 and 36, inclusive. In bases above 10, the letter '`A`'
* (in either upper or lower case) represents 10, '`B`' represents 11, and so forth, with '`Z`' representing 35. In
* base 10 (the default), the number can have a decimal part, as well as an optional exponent part (see 2.1). In other
* bases, only unsigned integers are accepted.
*/
declare function tonumber(this: void, e: string, base: number): number | undefined;
/**
* Receives an argument of any type and converts it to a string in a reasonable format. For complete control of how
* numbers are converted, use `string.format`. If the metatable of `e` has a `"__tostring"` field, then `tostring`
* calls the corresponding value with `e` as argument, and uses the result of the call as its result.
*/
declare function tostring(this: void, e: unknown): string;
/**
* Returns the type of its only argument, coded as a string. The possible results of this function are "`nil`" (a
* string, not the value nil), "`number`", "`string`", "`boolean`", "`table`", "`function`", "`thread`", and
* "`userdata`".
*/
declare function type(
this: void,
v: unknown
): "nil" | "number" | "string" | "boolean" | "table" | "function" | "thread" | "userdata" | "cdata";
/**
* Returns the elements from the given table. This function is equivalent to
* return list[i], list[i+1], ..., list[j]
*
* except that the above code can be written only for a fixed number of elements. By default, `i` is 1 and `j` is the
* length of the list, as defined by the length operator (see 2.5.5).
*/
/** @tupleReturn */
declare function unpack<A extends unknown[]>(this: void, list: A): A;
/**
* Returns the elements from the given table. This function is equivalent to
* return list[i], list[i+1], ..., list[j]
*
* except that the above code can be written only for a fixed number of elements. By default, `i` is 1 and `j` is the
* length of the list, as defined by the length operator (see 2.5.5).
*/
/** @tupleReturn */
declare function unpack<T>(this: void, list: T[], i: number, j?: number): T[];
/**
* A global variable (not a function) that holds a string containing the current interpreter version. The current
* contents of this variable is "`Lua 5.1`".
*/
declare const _VERSION: "Lua 5.1";
/**
* This function is similar to `pcall`, except that it sets a new message handler `msgh`.
*/
/** @tupleReturn */
declare function xpcall<T, A extends unknown[], R>(
this: void,
f: { (this: T, ...args: A): R; },
msgh: { (this: void, msg: string): void; },
self: T,
...args: A
): [true, R] | [false, string];
/**
* This function is similar to `pcall`, except that it sets a new message handler `msgh`.
*/
/** @tupleReturn */
declare function xpcall<A extends unknown[], R>(
this: void,
f: { (this: void, ...args: A): R; },
msgh: { (this: void, msg: string): void; },
...args: A
): [true, R] | [false, string];
declare namespace coroutine {
/**
* Creates a new coroutine, with body `f`. `f` must be a Lua function. Returns this new coroutine, an object with
* type `"thread"`.
*/
export function create(this: void, f: Function): LuaThread;
/**
* Starts or continues the execution of coroutine `co`. The first time you resume a coroutine, it starts running its
* body. The values `val1`, ... are passed as the arguments to the body function. If the coroutine has yielded,
* `resume` restarts it; the values `val1`, ... are passed as the results from the yield.
*
* If the coroutine runs without any errors, `resume` returns true plus any values passed to `yield` (if the
* coroutine yields) or any values returned by the body function (if the coroutine terminates). If there is any
* error, `resume` returns false plus the error message.
*/
/** @tupleReturn */
export function resume(this: void, co: LuaThread, ...args: unknown[]): [true, ...unknown[]] | [false, string];
/**
* Returns the running coroutine plus a boolean, true when the running coroutine is the main one.
*/
/** @tupleReturn */
export function running(this: void): [LuaThread, boolean];
/**
* Returns the status of coroutine `co`, as a string: `"running"`, if the coroutine is running (that is, it called
* `status`); `"suspended"`, if the coroutine is suspended in a call to `yield`, or if it has not started running
* yet; `"normal"` if the coroutine is active but not running (that is, it has resumed another coroutine); and
* `"dead"` if the coroutine has finished its body function, or if it has stopped with an error.
*/
export function status(this: void, co: LuaThread): "running" | "suspended" | "normal" | "dead";
/** @tupleReturn */
export interface WrappedFunction { (this: void, ...args: unknown[]): unknown[]; }
/**
* Creates a new coroutine, with body `f`. `f` must be a Lua function. Returns a function that resumes the coroutine
* each time it is called. Any arguments passed to the function behave as the extra arguments to `resume`. Returns
* the same values returned by `resume`, except the first boolean. In case of error, propagates the error.
*/
export function wrap(this: void, f: Function): WrappedFunction;
/**
* Suspends the execution of the calling coroutine. The coroutine cannot be running a C function, a metamethod, or
* an iterator. Any arguments to `yield` are passed as extra results to `resume`.
*/
/** @tupleReturn */
export function yield(this: void, ...args: unknown[]): unknown[];
}
declare type LuaModule = { _NAME: string; _M: LuaModule; } & { [key: string]: unknown; };
/**
* Creates a module. If there is a table in `package.loaded[name]`, this table is the module. Otherwise, if there is a
* global table `t` with the given name, this table is the module. Otherwise creates a new table `t` and sets it as
* the value of the global `name` and the value of `package.loaded[name]`. This function also initializes `t._NAME`
* with the given name, `t._M` with the module (`t` itself), and `t._PACKAGE` with the package name (the full module
* name minus last component; see below). Finally, `module` sets `t` as the new environment of the current function
* and the new value of `package.loaded[name]`, so that `require` returns `t`.
*
* If `name` is a compound name (that is, one with components separated by dots), `module` creates (or reuses, if they
* already exist) tables for each component. For instance, if `name` is `a.b.c`, then `module` stores the module table
* in field `c` of field `b` of global `a`.
*
* This function can receive optional options after the module name, where each option is a function to be applied over
* the module.
*/
declare function module(this: void, name: string, ...args: ({ (this: void, module: LuaModule): void; })[]): void;
/**
* Loads the given module. The function starts by looking into the `package.loaded` table to determine whether `modname`
* is already loaded. If it is, then `require` returns the value stored at `package.loaded[modname]`. Otherwise, it
* tries to find a loader for the module.
*
* To find a loader, `require` is guided by the `package.loaders` array. By changing this array, we can change how
* `require` looks for a module. The following explanation is based on the default configuration for
* `package.loaders`.
*
* First `require` queries `package.preload[modname]`. If it has a value, this value (which should be a function) is the
* loader. Otherwise `require` searches for a Lua loader using the path stored in `package.path`. If that also fails,
* it searches for a C loader using the path stored in `package.cpath`. If that also fails, it tries an all-in-one
* loader (see `package.loaders`).
*
* Once a loader is found, `require` calls the loader with a single argument, `modname`. If the loader returns any
* value, `require` assigns the returned value to `package.loaded[modname]`. If the loader returns no value and has
* not assigned any value to `package.loaded[modname]`, then `require` assigns true to this entry. In any case,
* `require` returns the final value of `package.loaded[modname]`.
*
* If there is any error loading or running the module, or if it cannot find any loader for the module, then `require`
* signals an error.
*/
declare function require(this: void, modname: string): unknown;
declare namespace package {
/**
* The path used by `require` to search for a C loader.
*
* Lua initializes the C path `package.cpath` in the same way it initializes the Lua path `package.path`, using the
* environment variable `LUA_CPATH` or a default path defined in `luaconf.h`.
*/
export let cpath: string;
/**
* A table used by `require` to control which modules are already loaded. When you require a module `modname` and
* `package.loaded[modname]` is not false, `require` simply returns the value stored there.
*/
export const loaded: Record<string, unknown>;
export interface Loader { (this: void, modname: string): unknown; }
export interface Searcher { (this: void, modname: string): Loader | string | undefined; }
/**
* A table used by `require` to control how to load modules.
*
* Each entry in this table is a searcher function. When looking for a module, `require` calls each of these
* searchers in ascending order, with the module name (the argument given to `require`) as its sole parameter. The
* function can return another function (the module loader) or a string explaining why it did not find that module
* (or nil if it has nothing to say). Lua initializes this table with four functions.
*
* The first searcher simply looks for a loader in the `package.preload` table.
*
* The second searcher looks for a loader as a Lua library, using the path stored at `package.path`. A path is a
* sequence of templates separated by semicolons. For each template, the searcher will change each interrogation
* mark in the template by `filename`, which is the module name with each dot replaced by a "directory separator"
* (such as "`/`" in Unix); then it will try to open the resulting file name. So, for instance, if the Lua path is
* the string
*
* "./?.lua;./?.lc;/usr/local/?/init.lua"
*
* the search for a Lua file for module `foo` will try to open the files `./foo.lua`, `./foo.lc`, and
* `/usr/local/foo/init.lua`, in that order.
*
* The third searcher looks for a loader as a C library, using the path given by the variable `package.cpath`. For
* instance, if the C path is the string
*
* "./?.so;./?.dll;/usr/local/?/init.so"
*
* the searcher for module `foo` will try to open the files `./foo.so`, `./foo.dll`, and `/usr/local/foo/init.so`,
* in that order. Once it finds a C library, this searcher first uses a dynamic link facility to link the
* application with the library. Then it tries to find a C function inside the library to be used as the loader.
* The name of this C function is the string "`luaopen_`" concatenated with a copy of the module name where each
* dot is replaced by an underscore. Moreover, if the module name has a hyphen, its prefix up to (and including)
* the first hyphen is removed. For instance, if the module name is `a.v1-b.c`, the function name will be
* `luaopen_b_c`.
*
* The fourth searcher tries an all-in-one loader. It searches the C path for a library for the root name of the
* given module. For instance, when requiring `a.b.c`, it will search for a C library for `a`. If found, it looks
* into it for an open function for the submodule; in our example, that would be `luaopen_a_b_c`. With this
* facility, a package can pack several C submodules into one single library, with each submodule keeping its
* original open function.
*/
export const loaders: Searcher[];
/**
* Searches for the given `name` in the given `path`.
*
* A path is a string containing a sequence of templates separated by semicolons. For each template, the function
* replaces each interrogation mark (if any) in the template with a copy of `name` wherein all occurrences of
* `sep` (a dot, by default) were replaced by `rep` (the system's directory separator, by default), and then tries
* to open the resulting file name.
*
* For instance, if the path is the string
*
* "./?.lua;./?.lc;/usr/local/?/init.lua"
*
* the search for the name `foo.a` will try to open the files `./foo/a.lua`, `./foo/a.lc`, and
* `/usr/local/foo/a/init.lua`, in that order.
*
* Returns the resulting name of the first file that it can open in read mode (after closing the file), or nil plus
* an error message if none succeeds. (This error message lists all file names it tried to open.)
*/
export function searchpath(
this: void,
name: string,
path: string,
sep?: string,
rep?: string
): [string] | [undefined, string];
/**
* Dynamically links the host program with the C library `libname`.
*
* If `funcname` is "`*`", then it only links with the library, making the symbols exported by the library available
* to other dynamically linked libraries. Otherwise, it looks for a function `funcname` inside the library and
* returns this function as a C function. So, `funcname` must follow the `lua_CFunction` prototype (see
* `lua_CFunction`).
*
* This is a low-level function. It completely bypasses the package and module system. Unlike `require`, it does not
* perform any path searching and does not automatically adds extensions. `libname` must be the complete file name
* of the C library, including if necessary a path and an extension. `funcname` must be the exact name exported by
* the C library (which may depend on the C compiler and linker used).
*
* This function is not supported by Standard C. As such, it is only available on some platforms (Windows, Linux,
* Mac OS X, Solaris, BSD, plus other Unix systems that support the `dlfcn` standard).
*/
export function loadlib(this: void, libname: string, funcname: "*"): void;
/**
* Dynamically links the host program with the C library `libname`.
*
* If `funcname` is "`*`", then it only links with the library, making the symbols exported by the library available
* to other dynamically linked libraries. Otherwise, it looks for a function `funcname` inside the library and
* returns this function as a C function. So, `funcname` must follow the `lua_CFunction` prototype (see
* `lua_CFunction`).
*
* This is a low-level function. It completely bypasses the package and module system. Unlike `require`, it does not
* perform any path searching and does not automatically adds extensions. `libname` must be the complete file name
* of the C library, including if necessary a path and an extension. `funcname` must be the exact name exported by
* the C library (which may depend on the C compiler and linker used).
*
* This function is not supported by Standard C. As such, it is only available on some platforms (Windows, Linux,
* Mac OS X, Solaris, BSD, plus other Unix systems that support the `dlfcn` standard).
*/
export function loadlib(this: void, libname: string, funcname: string): Function;
/**
* The path used by `require` to search for a Lua loader.
*
* At start-up, Lua initializes this variable with the value of the environment variable `LUA_PATH` or with a
* default path defined in `luaconf.h`, if the environment variable is not defined. Any "`;;`" in the value of the
* environment variable is replaced by the default path.
*/
export let path: string;
/**
* A table to store loaders for specific modules (see `require`).
*/
export const preload: Record<string, Loader>;
/**
* Sets a metatable for `module` with its `__index` field referring to the global environment, so that this module
* inherits values from the global environment. To be used as an option to function `module`.
*/
export function seeall(this: void, module: unknown): unknown;
}
declare namespace string {
/**
* Returns the internal numerical codes of the characters `s[i]`, `s[i+1]`, ..., `s[j]`. The default value for `i`
* is 1; the default value for `j` is `i`. Note that numerical codes are not necessarily portable across
* platforms.
*/
/** @tupleReturn */
export function byte(this: void, s: string, i?: string, j?: string): number[];
/**
* Receives zero or more integers. Returns a string with length equal to the number of arguments, in which each
* character has the internal numerical code equal to its corresponding argument. Note that numerical codes are
* not necessarily portable across platforms.
*/
export function char(this: void, ...args: number[]): string;
/**
* Returns a string containing a binary representation of the given function, so that a later `loadstring` on this
* string returns a copy of the function. `function` must be a Lua function without upvalues.
*/
export function dump(this: void, function_: Function, strip?: boolean): string;
/**
* Looks for the first match of `pattern` in the string `s`. If it finds a match, then `find` returns the indices of
* `s` where this occurrence starts and ends; otherwise, it returns nil. A third, optional numerical argument
* `init` specifies where to start the search; its default value is 1 and can be negative. A value of true as a
* fourth, optional argument `plain` turns off the pattern matching facilities, so the function does a plain "find
* substring" operation, with no characters in `pattern` being considered "magic". Note that if `plain` is given,
* then `init` must be given as well. If the pattern has captures, then in a successful match the captured values
* are also returned, after the two indices.
*/
/** @tupleReturn */
export function find(
this: void,
s: string,
pattern: string,
init?: number,
plain?: boolean
): [number, number, ...string[]] | [undefined];
/**
* Returns a formatted version of its variable number of arguments following the description given in its first
* argument (which must be a string). The format string follows the same rules as the ISO C function `sprintf`.
* The only differences are that the options/modifiers `*`, `h`, `L`, `l`, `n`, and `p` are not supported and that
* there is an extra option, `q`. The `q` option formats a string between double quotes, using escape sequences
* when necessary to ensure that it can safely be read back by the Lua interpreter. For instance, the call
*
* string.format('%q', 'a string with "quotes" and \n new line')
*
* may produce the string:
*
* "a string with \"quotes\" and \
* new line"
*
* Options `A` and `a` (when available), `E`, `e`, `f`, `G`, and `g` all expect a number as argument. Options `c`,
* `d`, `i`, `o`, `u`, `X`, and `x` also expect a number, but the range of that number may be limited by the
* underlying C implementation. For options `o`, `u`, `X`, and `x`, the number cannot be negative. Option `q`
* expects a string; option `s` expects a string without embedded zeros. If the argument to option `s` is not a
* string, it is converted to one following the same rules of `tostring`.
*/
export function format(this: void, formatstring: string, ...args: unknown[]): string;
/** @luaIterator @tupleReturn */
export interface GmatchIterable extends Array<string[]> {}
/**
* Returns an iterator function that, each time it is called, returns the next captures from `pattern` over string
* `s`. If `pattern` specifies no captures, then the whole match is produced in each call. As an example, the
* following loop
*
* s = "hello world from Lua"
* for w in string.gmatch(s, "%a+") do
* print(w)
* end
*
* will iterate over all the words from string `s`, printing one per line. The next example collects all pairs
* `key=value` from the given string into a table:
*
* t = {}
* s = "from=world, to=Lua"
* for k, v in string.gmatch(s, "(%w+)=(%w+)") do
* t[k] = v
* end
*
* For this function, a '`^`' at the start of a pattern does not work as an anchor, as this would prevent the
* iteration.
*/
export function gmatch(this: void, s: string, pattern: string): string.GmatchIterable;
/**
* Returns a copy of `s` in which all (or the first `n`, if given) occurrences of the `pattern` have been replaced
* by a replacement string specified by `repl`, which can be a string, a table, or a function. `gsub` also
* returns, as its second value, the total number of matches that occurred. If `repl` is a string, then its value
* is used for replacement. The character `%` works as an escape character: any sequence in `repl` of the form
* `%n`, with n between 1 and 9, stands for the value of the n-th captured substring (see below). The sequence
* `%0` stands for the whole match. The sequence `%%` stands for a single `%`.
*
* If `repl` is a table, then the table is queried for every match, using the first capture as the key; if the
* pattern specifies no captures, then the whole match is used as the key.
*
* If `repl` is a function, then this function is called every time a match occurs, with all captured substrings
* passed as arguments, in order; if the pattern specifies no captures, then the whole match is passed as a sole
* argument.
*
* If the value returned by the table query or by the function call is a string or a number, then it is used as the
* replacement string; otherwise, if it is false or nil, then there is no replacement (that is, the original match
* is kept in the string).
*
* Here are some examples:
*
* x = string.gsub("hello world", "(%w+)", "%1 %1")
* -->; x="hello hello world world"
*
* x = string.gsub("hello world", "%w+", "%0 %0", 1)
* -->; x="hello hello world"
*
* x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")
* -->; x="world hello Lua from"
*
* x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv)
* -->; x="home = /home/roberto, user = roberto"
*
* x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s)
* return loadstring(s)()
* end)
* -->; x="4+5 = 9"
*
* local t = {name="lua", version="5.1"}
* x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t)
* -->; x="lua-5.1.tar.gz"
*/
/** @tupleReturn */
export function gsub(
this: void,
s: string,
pattern: string,
repl: string | Record<string, string | number> | { (this: void, ...args: string[]): string | number; },
n?: number
): [string, number];
/**
* Receives a string and returns its length. The empty string `""` has length 0. Embedded zeros are counted, so
* `"a\000bc\000"` has length 5.
*/
export function len(this: void, s: string): number;
/**
* Receives a string and returns a copy of this string with all uppercase letters changed to lowercase. All other
* characters are left unchanged. The definition of what an uppercase letter is depends on the current locale.
*/
export function lower(this: void, s: string): string;
/**
* Looks for the first match of `pattern` in the string `s`. If it finds one, then `match` returns the captures from
* the pattern; otherwise it returns nil. If `pattern` specifies no captures, then the whole match is returned. A
* third, optional numerical argument `init` specifies where to start the search; its default value is 1 and can
* be negative.
*/
/** @tupleReturn */
export function match(this: void, s: string, pattern: string, init?: number): string[] | [undefined];
/**
* Returns a string that is the concatenation of `n` copies of the string `s` separated by the string `sep`. The
* default value for `sep` is the empty string (that is, no separator).
*/
export function rep(this: void, s: string, n: number, sep?: string): string;
/**
* Returns a string that is the string `s` reversed.
*/
export function reverse(this: void, s: string): string;
/**
* Returns the substring of `s` that starts at `i` and continues until `j`; `i` and `j` can be negative. If `j` is
* absent, then it is assumed to be equal to -1 (which is the same as the string length). In particular, the call
* `string.sub(s,1,j)` returns a prefix of `s` with length `j`, and `string.sub(s, -i)` returns a suffix of `s`
* with length `i`.
*/
export function sub(this: void, s: string, i: number, j?: number): string;
/**
* Receives a string and returns a copy of this string with all lowercase letters changed to uppercase. All other
* characters are left unchanged. The definition of what a lowercase letter is depends on the current locale.
*/
export function upper(this: void, s: string): string;
}
declare namespace table {
/**
* Given an array where all elements are strings or numbers, returns `table[i]..sep..table[i+1] ... sep..table[j]`.
* The default value for `sep` is the empty string, the default for `i` is 1, and the default for `j` is the
* length of the table. If `i` is greater than `j`, returns the empty string.
*/
export function concat(this: void, table: (string | number)[], sep?: string, i?: number, j?: number): string;
/**
* Inserts element `value` at position `pos` in `table`, shifting up other elements to open space, if necessary. The
* default value for `pos` is `n+1`, where `n` is the length of the table (see 2.5.5), so that a call
* `table.insert(t,x)` inserts `x` at the end of table `t`.
*/
export function insert<T>(this: void, table: T[], pos: number, value: T): void;
/**
* Inserts element `value` at position `pos` in `table`, shifting up other elements to open space, if necessary. The
* default value for `pos` is `n+1`, where `n` is the length of the table (see 2.5.5), so that a call
* `table.insert(t,x)` inserts `x` at the end of table `t`.
*/
export function insert<T>(this: void, table: T[], value: T): void;
/**
* Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical
* indices. (To do its job this function does a linear traversal of the whole table.)
*/
export function maxn<T extends object>(this: void, table: T): number;
/**
* Returns a new table with all parameters stored into keys 1, 2, etc. and with a field "`n`" with the total number
* of parameters. Note that the resulting table may not be a sequence.
*/
export function pack<A extends unknown[]>(this: void, ...args: A): A & {n: number};
/**
* Removes from `table` the element at position `pos`, shifting down other elements to close the space, if
* necessary. Returns the value of the removed element. The default value for `pos` is `n`, where `n` is the
* length of the table, so that a call `table.remove(t)` removes the last element of table `t`.
*/
export function remove<T>(this: void, table: T[], pos?: number): T | undefined;
/**
* Sorts table elements in a given order, in-place, from `table[1]` to `table[n]`, where `n` is the length of the
* table. If `comp` is given, then it must be a function that receives two table elements, and returns true when
* the first is less than the second (so that `not comp(a[i+1],a[i])` will be true after the sort). If `comp` is
* not given, then the standard Lua operator `<;` is used instead. The sort algorithm is not stable; that is,
* elements considered equal by the given order may have their relative positions changed by the sort.
*/
export function sort<T>(this: void, table: T[], comp?: { (this: void, a: T, b: T): boolean; }): void;
/**
* Returns the elements from the given table. This function is equivalent to
*
* return list[i], list[i+1], ..., list[j]
*
* By default, `i` is 1 and `j` is `#list`.
*/
/** @tupleReturn */
export function unpack<A extends unknown[]>(this: void, list: A): A;
/**
* Returns the elements from the given table. This function is equivalent to
*
* return list[i], list[i+1], ..., list[j]
*
* By default, `i` is 1 and `j` is `#list`.
*/
/** @tupleReturn */
export function unpack<T>(this: void, list: T[], i: number, j?: number): T[];
}
declare namespace math {
/**
* Returns the absolute value of `x`.
*/
export function abs(this: void, x: number): number;
/**
* Returns the arc cosine of `x` (in radians).
*/
export function acos(this: void, x: number): number;
/**
* Returns the arc sine of `x` (in radians).
*/
export function asin(this: void, x: number): number;
/**
* Returns the arc tangent of `x` (in radians).
*/
export function atan(this: void, x: number): number;
/**
* Returns the arc tangent of `y/x` (in radians), but uses the signs of both parameters to find the quadrant of the
* result. (It also handles correctly the case of `x` being zero.)
*/
export function atan2(this: void, y: number, x: number): number;
/**
* Returns the smallest integer larger than or equal to `x`.
*/
export function ceil(this: void, x: number): number;
/**
* Returns the cosine of `x` (assumed to be in radians).
*/
export function cos(this: void, x: number): number;
/**
* Returns the hyperbolic cosine of `x`.
*/
export function cosh(this: void, x: number): number;
/**
* Returns the angle `x` (given in radians) in degrees.
*/
export function deg(this: void, x: number): number;
/**
* Returns the value ex.