From 900cc31dce7e640fc8df6f729a7209819e42c8e6 Mon Sep 17 00:00:00 2001 From: Ryan Young Date: Thu, 5 May 2022 02:00:21 -0700 Subject: [PATCH 1/7] feat: add type declarations for lua 5.0 --- 5.0.d.ts | 2 + core/debug.d.ts | 141 --------- core/global.d.ts | 79 +----- core/io.d.ts | 24 -- core/math.d.ts | 17 -- core/metatable.d.ts | 15 - core/modules.d.ts | 108 ------- core/string.d.ts | 45 --- special/5.0-only.d.ts | 219 ++++++++++++++ special/5.0-or-5.1-or-jit.d.ts | 38 +++ special/5.0-or-5.1.d.ts | 66 +++++ special/5.0.d.ts | 4 + special/5.1-only.d.ts | 65 ----- special/5.1-or-jit.d.ts | 37 --- special/5.1-plus-and-5.3-pre.d.ts | 18 ++ special/5.1-plus-or-jit.d.ts | 456 ++++++++++++++++++++++++++++++ special/5.1.d.ts | 4 + special/5.2.d.ts | 2 + special/5.3-pre.d.ts | 15 - special/5.3.d.ts | 1 + special/5.4.d.ts | 1 + special/jit.d.ts | 3 + 22 files changed, 815 insertions(+), 545 deletions(-) create mode 100644 5.0.d.ts create mode 100644 special/5.0-only.d.ts create mode 100644 special/5.0-or-5.1-or-jit.d.ts create mode 100644 special/5.0-or-5.1.d.ts create mode 100644 special/5.0.d.ts create mode 100644 special/5.1-plus-and-5.3-pre.d.ts create mode 100644 special/5.1-plus-or-jit.d.ts diff --git a/5.0.d.ts b/5.0.d.ts new file mode 100644 index 0000000..b5af446 --- /dev/null +++ b/5.0.d.ts @@ -0,0 +1,2 @@ +/// +/// diff --git a/core/debug.d.ts b/core/debug.d.ts index 59c2a20..bde7507 100644 --- a/core/debug.d.ts +++ b/core/debug.d.ts @@ -28,15 +28,6 @@ declare namespace debug { */ function debug(): void; - /** - * Returns the current hook settings of the thread, as three values: the - * current hook function, the current hook mask, and the current hook count - * (as set by the debug.sethook function). - */ - function gethook( - thread?: LuaThread - ): LuaMultiReturn<[undefined, 0] | [Function, number, string?]>; - interface FunctionInfo { /** * The function itself. @@ -74,51 +65,6 @@ declare namespace debug { nups: number; } - /** - * Returns a table with information about a function. You can give the - * function directly or you can give a number as the value of f, which means - * the function running at level f of the call stack of the given thread: - * level 0 is the current function (getinfo itself); level 1 is the function - * that called getinfo (except for tail calls, which do not count on the - * stack); and so on. If f is a number larger than the number of active - * functions, then getinfo returns nil. - * - * The returned table can contain all the fields returned by lua_getinfo, with - * the string what describing which fields to fill in. The default for what is - * to get all information available, except the table of valid lines. If - * present, the option 'f' adds a field named func with the function itself. - * If present, the option 'L' adds a field named activelines with the table of - * valid lines. - * - * For instance, the expression debug.getinfo(1,"n").name returns a name for - * the current function, if a reasonable name can be found, and the expression - * debug.getinfo(print) returns a table with all available information about - * the print function. - */ - function getinfo(f: T): FunctionInfo; - function getinfo(f: T, what: string): Partial>; - function getinfo(thread: LuaThread, f: T): FunctionInfo; - function getinfo( - thread: LuaThread, - f: T, - what: string - ): Partial>; - function getinfo(f: number): FunctionInfo | undefined; - function getinfo(f: number, what: string): Partial | undefined; - function getinfo(thread: LuaThread, f: number): FunctionInfo | undefined; - function getinfo(thread: LuaThread, f: number, what: string): Partial | undefined; - - /** - * Returns the metatable of the given value or nil if it does not have a - * metatable. - */ - function getmetatable(value: T): LuaMetatable | undefined; - - /** - * Returns the registry table (see §4.5). - */ - function getregistry(): Record; - /** * This function returns the name and the value of the upvalue with index up * of the function f. The function returns nil if there is no upvalue with the @@ -130,80 +76,6 @@ declare namespace debug { */ function getupvalue(f: Function, up: number): LuaMultiReturn<[string, any] | []>; - /** - * Returns the Lua value associated to u. If u is not a full userdata, returns - * nil. - */ - function getuservalue(u: LuaUserdata): any; - - /** - * Sets the given function as a hook. The string mask and the number count - * describe when the hook will be called. The string mask may have any - * combination of the following characters, with the given meaning: - * - * * 'c': the hook is called every time Lua calls a function; - * * 'r': the hook is called every time Lua returns from a function; - * * 'l': the hook is called every time Lua enters a new line of code. - * - * Moreover, with a count different from zero, the hook is called also after - * every count instructions. - * - * When called without arguments, debug.sethook turns off the hook. - * - * When the hook is called, its first parameter is a string describing the - * event that has triggered its call: "call" (or "tail call"), "return", - * "line", and "count". For line events, the hook also gets the new line - * number as its second parameter. Inside a hook, you can call getinfo with - * level 2 to get more information about the running function (level 0 is the - * getinfo function, and level 1 is the hook function). - */ - function sethook(): void; - function sethook( - hook: (event: 'call' | 'return' | 'line' | 'count', line?: number) => any, - mask: string, - count?: number - ): void; - function sethook( - thread: LuaThread, - hook: (event: 'call' | 'return' | 'line' | 'count', line?: number) => any, - mask: string, - count?: number - ): void; - - /** - * This function assigns the value value to the local variable with index - * local of the function at level level of the stack. The function returns nil - * if there is no local variable with the given index, and raises an error - * when called with a level out of range. (You can call getinfo to check - * whether the level is valid.) Otherwise, it returns the name of the local - * variable. - * - * See debug.getlocal for more information about variable indices and names. - */ - function setlocal(level: number, local: number, value: any): string | undefined; - function setlocal( - thread: LuaThread, - level: number, - local: number, - value: any - ): string | undefined; - - /** - * Sets the metatable for the given value to the given table (which can be - * nil). Returns value. - */ - function setmetatable< - T extends object, - TIndex extends object | ((this: T, key: any) => any) | undefined = undefined - >( - value: T, - table?: LuaMetatable | null - ): TIndex extends (this: T, key: infer TKey) => infer TValue - ? T & { [K in TKey & string]: TValue } - : TIndex extends object - ? T & TIndex - : T; - /** * This function assigns the value value to the upvalue with index up of the * function f. The function returns nil if there is no upvalue with the given @@ -218,17 +90,4 @@ declare namespace debug { * Returns udata. */ function setuservalue(udata: LuaUserdata, value: any): LuaUserdata; - - /** - * If message is present but is neither a string nor nil, this function - * returns message without further processing. Otherwise, it returns a string - * with a traceback of the call stack. The optional message string is appended - * at the beginning of the traceback. An optional level number tells at which - * level to start the traceback (default is 1, the function calling - * traceback). - */ - function traceback(message?: string | null, level?: number | null): string; - function traceback(thread?: LuaThread, message?: string | null, level?: number | null): string; - function traceback(message: T): T; - function traceback(thread: LuaThread, message: T): T; } diff --git a/core/global.d.ts b/core/global.d.ts index e8652c1..d339973 100644 --- a/core/global.d.ts +++ b/core/global.d.ts @@ -11,7 +11,7 @@ type LuaUserdata = { readonly __internal__: unique symbol }; * A global variable (not a function) that holds a string containing the running * Lua version. */ -declare const _VERSION: 'Lua 5.1' | 'Lua 5.2' | 'Lua 5.3' | 'Lua 5.4'; +declare const _VERSION: 'Lua 5.0' | 'Lua 5.1' | 'Lua 5.2' | 'Lua 5.3' | 'Lua 5.4'; /** * A global variable (not a function) that holds the global environment (see @@ -31,61 +31,6 @@ declare function assert( ...args: A ): LuaMultiReturn<[Exclude, ...A]>; -/** - * This function is a generic interface to the garbage collector. It performs - * different functions according to its first argument, opt. - * - * Performs a full garbage-collection cycle. This is the default option. - */ -declare function collectgarbage(opt?: 'collect'): void; - -/** - * This function is a generic interface to the garbage collector. It performs - * different functions according to its first argument, opt. - * - * Stops automatic execution of the garbage collector. The collector will run - * only when explicitly invoked, until a call to restart it. - */ -declare function collectgarbage(opt: 'stop'): void; - -/** - * This function is a generic interface to the garbage collector. It performs - * different functions according to its first argument, opt. - * - * Restarts automatic execution of the garbage collector. - */ -declare function collectgarbage(opt: 'restart'): void; - -/** - * This function is a generic interface to the garbage collector. It performs - * different functions according to its first argument, opt. - * - * Sets arg as the new value for the pause of the collector (see §2.5). Returns - * the previous value for pause. - */ -declare function collectgarbage(opt: 'setpause', arg: number): number; - -/** - * This function is a generic interface to the garbage collector. It performs - * different functions according to its first argument, opt. - * - * Sets arg as the new value for the step multiplier of the collector (see - * §2.5). Returns the previous value for step. - */ -declare function collectgarbage(opt: 'setstepmul', arg: number): number; - -/** - * This function is a generic interface to the garbage collector. It performs - * different functions according to its first argument, opt. - * - * Performs a garbage-collection step. The step "size" is controlled by arg. - * With a zero value, the collector will perform one basic (indivisible) step. - * For non-zero values, the collector will perform as if that amount of memory - * (in KBytes) had been allocated by Lua. Returns true if the step finished a - * collection cycle. - */ -declare function collectgarbage(opt: 'step', arg: number): boolean; - /** * Opens the named file and executes its contents as a Lua chunk. When called * without arguments, dofile executes the contents of the standard input @@ -204,12 +149,6 @@ declare function rawequal(v1: T, v2: T): boolean; */ declare function rawget(table: T, index: K): T[K]; -/** - * Returns the length of the object v, which must be a table or a string, - * without invoking the __len metamethod. Returns an integer. - */ -declare function rawlen(v: object | string): number; - /** * Sets the real value of table[index] to value, without invoking the __newindex * metamethod. table must be a table, index any value different from nil and @@ -219,22 +158,6 @@ declare function rawlen(v: object | string): number; */ declare function rawset(table: T, index: K, value: T[K]): T; -/** - * If index is a number, returns all arguments after argument number index; a - * negative number indexes from the end (-1 is the last argument). Otherwise, - * index must be the string "#", and select returns the total number of extra - * arguments it received. - */ -declare function select(index: number, ...args: T[]): LuaMultiReturn; - -/** - * If index is a number, returns all arguments after argument number index; a - * negative number indexes from the end (-1 is the last argument). Otherwise, - * index must be the string "#", and select returns the total number of extra - * arguments it received. - */ -declare function select(index: '#', ...args: T[]): number; - /** * Sets the metatable for the given table. (To change the metatable of other * types from Lua code, you must use the debug library (§6.10).) If metatable is diff --git a/core/io.d.ts b/core/io.d.ts index 9c35097..c996fb2 100644 --- a/core/io.d.ts +++ b/core/io.d.ts @@ -91,15 +91,6 @@ declare namespace io { */ function output(file?: string | LuaFile): LuaFile; - /** - * This function is system dependent and is not available on all platforms. - * - * Starts program prog in a separated process and returns a file handle that - * you can use to read data from this program (if mode is "r", the default) or - * to write data to this program (if mode is "w"). - */ - function popen(prog: string, mode?: 'r' | 'w'): LuaMultiReturn<[LuaFile] | [undefined, string]>; - /** * Equivalent to io.input():read(···). */ @@ -240,21 +231,6 @@ interface LuaFile { offset?: number ): LuaMultiReturn<[number] | [undefined, string]>; - /** - * Sets the buffering mode for an output file. There are three available - * modes: - * - * * "no": no buffering; the result of any output operation appears - * immediately. - * * "full": full buffering; output operation is performed only when the - * buffer is full or when you explicitly flush the file (see io.flush). - * * "line": line buffering; output is buffered until a newline is output or - * there is any input from some special files (such as a terminal device). - * For the last two cases, size specifies the size of the buffer, in bytes. - * The default is an appropriate size. - */ - setvbuf(mode: 'no' | 'full' | 'line', size?: number): void; - /** * Writes the value of each of its arguments to file. The arguments must be * strings or numbers. diff --git a/core/math.d.ts b/core/math.d.ts index 794c8b6..de01094 100644 --- a/core/math.d.ts +++ b/core/math.d.ts @@ -51,17 +51,6 @@ declare namespace math { */ function floor(x: number): number; - /** - * Returns the remainder of the division of x by y that rounds the quotient - * towards zero. (integer/float) - */ - function fmod(x: number, y: number): number; - - /** - * The float value HUGE_VAL, a value larger than any other numeric value. - */ - const huge: number; - /** * Returns the argument with the maximum value, according to the Lua operator * <. (integer/float) @@ -74,12 +63,6 @@ declare namespace math { */ function min(x: number, ...numbers: number[]): number; - /** - * Returns the integral part of x and the fractional part of x. Its second - * result is always a float. - */ - function modf(x: number): LuaMultiReturn<[number, number]>; - /** * The value of π. */ diff --git a/core/metatable.d.ts b/core/metatable.d.ts index 8d4cfab..b2479b1 100644 --- a/core/metatable.d.ts +++ b/core/metatable.d.ts @@ -34,11 +34,6 @@ interface LuaMetatable< */ __div?(this: T, operand: any): any; - /** - * the modulo (%) operation. Behavior similar to the addition operation. - */ - __mod?(this: T, operand: any): any; - /** * the exponentiation (^) operation. Behavior similar to the addition * operation. @@ -58,16 +53,6 @@ interface LuaMetatable< */ __concat?(this: T, operand: any): any; - /** - * the length (#) operation. If the object is not a string, Lua will try its - * metamethod. If there is a metamethod, Lua calls it with the object as - * argument, and the result of the call (always adjusted to one value) is the - * result of the operation. If there is no metamethod but the object is a - * table, then Lua uses the table length operation (see §3.4.7). Otherwise, - * Lua raises an error. - */ - __len?(this: T): any; - /** * the equal (==) operation. Behavior similar to the addition operation, * except that Lua will try a metamethod only when the values being compared diff --git a/core/modules.d.ts b/core/modules.d.ts index a0bc907..0c29f96 100644 --- a/core/modules.d.ts +++ b/core/modules.d.ts @@ -31,111 +31,3 @@ * loader for the module, then require raises an error. */ declare function require(modname: string): any; - -/** - * The package library provides basic facilities for loading modules in Lua. It - * exports one function directly in the global environment: require. Everything - * else is exported in a table package. - */ -declare namespace package { - /** - * A string describing some compile-time configurations for packages. This - * string is a sequence of lines: - * * The first line is the directory separator string. Default is '\' for - * Windows and '/' for all other systems. - * * The second line is the character that separates templates in a path. - * Default is ';'. - * * The third line is the string that marks the substitution points in a - * template. Default is '?'. - * * The fourth line is a string that, in a path in Windows, is replaced by - * the executable's directory. Default is '!'. - * * The fifth line is a mark to ignore all text after it when building the - * luaopen_ function name. Default is '-'. - */ - var config: string; - - /** - * 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_5_3, or the - * environment variable LUA_CPATH, or a default path defined in luaconf.h. - */ - var 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. - * - * This variable is only a reference to the real table; assignments to this - * variable do not change the table used by require. - */ - const loaded: Record; - - /** - * 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). - */ - function loadlib( - libname: string, - funcname: string - ): [Function] | [undefined, string, 'open' | 'init']; - - /** - * 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_5_3 or the environment variable LUA_PATH or - * with a default path defined in luaconf.h, if those environment variables - * are not defined. Any ";;" in the value of the environment variable is - * replaced by the default path. - */ - var path: string; - - /** - * A table to store loaders for specific modules (see require). - * - * This variable is only a reference to the real table; assignments to this - * variable do not change the table used by require. - */ - const preload: Record any>; - - /** - * 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.) - */ - function searchpath(name: string, path: string, sep?: string, rep?: string): string; -} diff --git a/core/string.d.ts b/core/string.d.ts index 0a10e5d..df83dfc 100644 --- a/core/string.d.ts +++ b/core/string.d.ts @@ -92,37 +92,6 @@ declare namespace string { */ function format(formatstring: string, ...args: any[]): string; - /** - * Returns an iterator function that, each time it is called, returns the next - * captures from pattern (see §6.4.1) over the string s. If pattern specifies - * no captures, then the whole match is produced in each call. - * - * As an example, the following loop will iterate over all the words from - * string s, printing one per line: - * - * ``` - * s = "hello world from Lua" - * for w in string.gmatch(s, "%a+") do - * print(w) - * end - * ``` - * - * 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 caret '^' at the start of a pattern does not work as - * an anchor, as this would prevent the iteration. - */ - function gmatch(s: string, pattern: string): LuaIterable>; - /** * Returns a copy of s in which all (or the first n, if given) occurrences of * the pattern (see §6.4.1) have been replaced by a replacement string @@ -170,25 +139,11 @@ declare namespace string { */ function lower(s: string): string; - /** - * Looks for the first match of pattern (see §6.4.1) 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 numeric argument init specifies where to start - * the search; its default value is 1 and can be negative. - */ - function match(s: string, pattern: string, init?: number): LuaMultiReturn; - /** * Returns a string that is the concatenation of `n` copies of the string `s`. */ function rep(s: string, n: number): string; - /** - * Returns a string that is the string s reversed. - */ - function reverse(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 diff --git a/special/5.0-only.d.ts b/special/5.0-only.d.ts new file mode 100644 index 0000000..153b77e --- /dev/null +++ b/special/5.0-only.d.ts @@ -0,0 +1,219 @@ +/** @noSelfInFile */ + +/** + * Sets the garbage-collection threshold to the given limit (in Kbytes) and + * checks it against the byte counter. If the new threshold is smaller than the + * byte counter, then Lua immediately runs the garbage collector (see 2.9). If + * limit is absent, it defaults to zero (thus forcing a garbage-collection + * cycle). + */ +declare function collectgarbage(limit: number): void; + +/** + * Returns all elements from the given list. This function is equivalent to + * + * `return list[1], list[2], ..., list[n]` + * + * except that the above code can be written only for a fixed n. The number n is + * the size of the list, as defined for the table.getn function. + */ +declare function unpack(list: T): LuaMultiReturn; + +declare namespace debug { + /** + * Returns the current hook settings of the thread, as three values: the + * current hook function, the current hook mask, and the current hook count + * (as set by the debug.sethook function). + */ + function gethook(): LuaMultiReturn<[undefined, 0] | [Function, number, string?]>; + + /** + * Returns a table with information about a function. You can give the + * function directly or you can give a number as the value of f, which means + * the function running at level f of the call stack of the given thread: + * level 0 is the current function (getinfo itself); level 1 is the function + * that called getinfo (except for tail calls, which do not count on the + * stack); and so on. If f is a number larger than the number of active + * functions, then getinfo returns nil. + * + * The returned table can contain all the fields returned by lua_getinfo, with + * the string what describing which fields to fill in. The default for what is + * to get all information available, except the table of valid lines. If + * present, the option 'f' adds a field named func with the function itself. + * If present, the option 'L' adds a field named activelines with the table of + * valid lines. + * + * For instance, the expression debug.getinfo(1,"n").name returns a name for + * the current function, if a reasonable name can be found, and the expression + * debug.getinfo(print) returns a table with all available information about + * the print function. + */ + function getinfo(f: T): FunctionInfo; + function getinfo(f: T, what: string): Partial>; + function getinfo(f: number): FunctionInfo | undefined; + function getinfo(f: number, what: string): Partial | undefined; + + /** + * This function returns the name and the value of the local variable with + * index local of the function at level level of the stack. (The first + * parameter or local variable has index 1, and so on, until the last active + * local variable.) The function returns nil if there is no local variable + * with the given index, and raises an error when called with a level out of + * range. (You can call debug.getinfo to check whether the level is valid.) + */ + function getlocal(level: number, local: number): LuaMultiReturn<[string, any]>; + + /** + * Sets the given function as a hook. The string mask and the number count + * describe when the hook will be called. The string mask may have any + * combination of the following characters, with the given meaning: + * + * * 'c': the hook is called every time Lua calls a function; + * * 'r': the hook is called every time Lua returns from a function; + * * 'l': the hook is called every time Lua enters a new line of code. + * + * Moreover, with a count different from zero, the hook is called also after + * every count instructions. + * + * When called without arguments, debug.sethook turns off the hook. + * + * When the hook is called, its first parameter is a string describing the + * event that has triggered its call: "call" (or "tail call"), "return", + * "line", and "count". For line events, the hook also gets the new line + * number as its second parameter. Inside a hook, you can call getinfo with + * level 2 to get more information about the running function (level 0 is the + * getinfo function, and level 1 is the hook function). + */ + function sethook(): void; + function sethook( + hook: (event: 'call' | 'return' | 'line' | 'count', line?: number) => any, + mask: string, + count?: number + ): void; + + /** + * This function assigns the value value to the local variable with index + * local of the function at level level of the stack. The function returns nil + * if there is no local variable with the given index, and raises an error + * when called with a level out of range. (You can call getinfo to check + * whether the level is valid.) Otherwise, it returns the name of the local + * variable. + * + * See debug.getlocal for more information about variable indices and names. + */ + function setlocal(level: number, local: number, value: any): string | undefined; + + /** + * Returns a string with a traceback of the call stack. An optional message + * string is appended at the beginning of the traceback. This function is + * typically used with xpcall to produce better error messages. + */ + function traceback(message: T): T; +} + +/** + * Sets the garbage-collection threshold to the given limit (in Kbytes) and + * checks it against the byte counter. If the new threshold is smaller than the + * byte counter, then Lua immediately runs the garbage collector (see 2.9). If + * limit is absent, it defaults to zero (thus forcing a garbage-collection + * cycle). + */ +declare function collectgarbage(limit: number): void; + +/** + * Returns two results: the number of Kbytes of dynamic memory that Lua is using + * and the current garbage collector threshold (also in Kbytes). + */ +declare function gcinfo(): LuaMultiReturn<[number, number]>; + +/** + * Links the program with the dynamic C library libname. Inside this library, + * looks for a function funcname and returns this function as a C function. + * + * libname must be the complete file name of the C library, including any + * eventual path and extension. + * + * This function is not supported by ANSI C. As such, it is only available on + * some platforms (Windows, Linux, Solaris, BSD, plus other Unix systems that + * support the dlfcn standard). + */ + declare function loadlib(libname: string, funcname: string): undefined | Function; + +declare namespace string { + /** + * Returns the internal numerical code of the i-th character of s, or nil if + * the index is out of range. If i is absent, then it is assumed to be 1. i + * may be negative. + * + * Note that numerical codes are not necessarily portable across platforms. + */ + function byte(s: string, i?: number): number; + + /** + * Returns an iterator function that, each time it is called, returns the + * next captures from pattern pat over string s. + * + * If pat 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.gfind(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.gfind(s, "(%w+)=(%w+)") do + * t[k] = v + * end + * ``` + */ + function gfind(s: string, pattern: string): LuaIterable>; +} + +declare namespace table { + /** + * Executes the given f over all elements of table. For each element, f is + * called with the index and respective value as arguments. If f returns a + * non-nil value, then the loop is broken, and this value is returned as the + * final value of foreach. + * + * See the next function for extra information about table traversals. + */ + function foreach(table: object, f: (index: any, value: any) => any): any; + + /** + * Executes the given f over the numerical indices of table. For each index, + * f is called with the index and respective value as arguments. Indices are + * visited in sequential order, from 1 to n, where n is the size of the + * table (see 5.4). If f returns a non-nil value, then the loop is broken + * and this value is returned as the result of foreachi. + */ + function foreachi(table: object, f: (index: number, value: any) => any): any; + + /** + * Returns the size of a table, when seen as a list. If the table has an n + * field with a numeric value, this value is the size of the table. + * Otherwise, if there was a previous call to table.setn over this table, + * the respective value is returned. Otherwise, the size is one less the + * first integer index with a nil value. + */ + function getn(table: object): number; + + /** + * Updates the size of a table. If the table has a field "n" with a + * numerical value, that value is changed to the given n. Otherwise, it + * updates an internal state so that subsequent calls to table.getn(table) + * return n. + */ + function setn(table: object, n: number): void; +} diff --git a/special/5.0-or-5.1-or-jit.d.ts b/special/5.0-or-5.1-or-jit.d.ts new file mode 100644 index 0000000..fb24d8d --- /dev/null +++ b/special/5.0-or-5.1-or-jit.d.ts @@ -0,0 +1,38 @@ +/** @noSelfInFile */ + +/** + * 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(f?: Function | number): any; + +/** + * 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(f: T, table: object): T; +declare function setfenv(f: 0, table: object): Function; +declare function setfenv(f: number, table: object): void; + +declare namespace os { + /** + * This function is equivalent to the C function system. It passes command to + * be executed by an operating system shell. It returns a status code, which + * is system-dependent. If command is absent, then it returns nonzero if a + * shell is available and zero otherwise. + */ + function execute(command?: string): number; +} + +declare namespace math { + /** + * Returns the base-10 logarithm of x. + */ + function log10(x: number): number; +} diff --git a/special/5.0-or-5.1.d.ts b/special/5.0-or-5.1.d.ts new file mode 100644 index 0000000..a75b7e2 --- /dev/null +++ b/special/5.0-or-5.1.d.ts @@ -0,0 +1,66 @@ +/** @noSelfInFile */ + +/** + * Similar to load, but gets the chunk from file filename or from the standard + * input, if no file name is given. + */ +declare function loadfile(filename?: string): LuaMultiReturn<[() => any] | [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. + */ +declare function loadstring( + string: string, + chunkname?: string +): LuaMultiReturn<[() => any] | [undefined, string]>; + +/** + * This function is similar to pcall, except that it sets a new message handler + * msgh. + * + * xpcall calls function f in protected mode, using err as the error handler. + * Any error inside f is not propagated; instead, xpcall catches the error, + * calls the err function with the original error object, and returns a status + * code. Its first result is the status code (a boolean), which is true if the + * call succeeds without errors. In this case, xpcall also returns all results + * from the call, after this first result. In case of any error, xpcall returns + * false plus the result from err. + */ +declare function xpcall( + f: () => R, + err: (err: any) => E +): LuaMultiReturn<[true, R] | [false, E]>; + +declare namespace math { + /** + * Returns the logarithm of x. + */ + function log(x: number): number; +} + +declare namespace string { + /** + * Returns a string that is the concatenation of n copies of the string s. + */ + function rep(s: string, n: number): string; +} + +declare namespace os { + /** + * Calls the C function exit, with an optional code, to terminate the host + * program. The default value for code is the success code. + */ + function exit(code?: number): never; +} + +declare namespace io { + type FileReadNumberFormat = '*n'; + type FileReadLineFormat = '*l'; + type FileReadFormat = FileReadNumberFormat | FileReadLineFormat | '*a' | number; +} diff --git a/special/5.0.d.ts b/special/5.0.d.ts new file mode 100644 index 0000000..666cf03 --- /dev/null +++ b/special/5.0.d.ts @@ -0,0 +1,4 @@ +/// +/// +/// +/// diff --git a/special/5.1-only.d.ts b/special/5.1-only.d.ts index aeeadce..4a104ba 100644 --- a/special/5.1-only.d.ts +++ b/special/5.1-only.d.ts @@ -17,65 +17,6 @@ declare function load( chunkname?: string ): LuaMultiReturn<[() => any] | [undefined, string]>; -/** - * Similar to load, but gets the chunk from file filename or from the standard - * input, if no file name is given. - */ -declare function loadfile(filename?: string): LuaMultiReturn<[() => any] | [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. - */ -declare function loadstring( - string: string, - chunkname?: string -): LuaMultiReturn<[() => any] | [undefined, string]>; - -/** - * This function is similar to pcall, except that it sets a new message handler - * msgh. - * - * xpcall calls function f in protected mode, using err as the error handler. - * Any error inside f is not propagated; instead, xpcall catches the error, - * calls the err function with the original error object, and returns a status - * code. Its first result is the status code (a boolean), which is true if the - * call succeeds without errors. In this case, xpcall also returns all results - * from the call, after this first result. In case of any error, xpcall returns - * false plus the result from err. - */ -declare function xpcall( - f: () => R, - err: (err: any) => E -): LuaMultiReturn<[true, R] | [false, E]>; - -declare namespace math { - /** - * Returns the logarithm of x. - */ - function log(x: number): number; -} - -declare namespace string { - /** - * Returns a string that is the concatenation of n copies of the string s. - */ - function rep(s: string, n: number): string; -} - -declare namespace os { - /** - * Calls the C function exit, with an optional code, to terminate the host - * program. The default value for code is the success code. - */ - function exit(code?: number): never; -} - declare namespace debug { /** * This function returns the name and the value of the local variable with @@ -95,9 +36,3 @@ declare namespace debug { local: number ): LuaMultiReturn<[string, any]>; } - -declare namespace io { - type FileReadNumberFormat = '*n'; - type FileReadLineFormat = '*l'; - type FileReadFormat = FileReadNumberFormat | FileReadLineFormat | '*a' | number; -} diff --git a/special/5.1-or-jit.d.ts b/special/5.1-or-jit.d.ts index 19fe7cf..597bd18 100644 --- a/special/5.1-or-jit.d.ts +++ b/special/5.1-or-jit.d.ts @@ -22,26 +22,6 @@ declare function collectgarbage(opt: 'count'): number; declare function unpack(list: T): LuaMultiReturn; declare function unpack(list: T[], i: number, j?: number): LuaMultiReturn; -/** - * 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(f?: Function | number): any; - -/** - * 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(f: T, table: object): T; -declare function setfenv(f: 0, table: object): Function; -declare function setfenv(f: number, table: object): void; - declare namespace debug { /** * Returns the environment of object o. @@ -105,23 +85,6 @@ declare namespace package { )[]; } -declare namespace os { - /** - * This function is equivalent to the C function system. It passes command to - * be executed by an operating system shell. It returns a status code, which - * is system-dependent. If command is absent, then it returns nonzero if a - * shell is available and zero otherwise. - */ - function execute(command?: string): number; -} - -declare namespace math { - /** - * Returns the base-10 logarithm of x. - */ - function log10(x: number): number; -} - declare namespace table { /** * Returns the largest positive numerical index of the given table, or zero if diff --git a/special/5.1-plus-and-5.3-pre.d.ts b/special/5.1-plus-and-5.3-pre.d.ts new file mode 100644 index 0000000..cb6ebeb --- /dev/null +++ b/special/5.1-plus-and-5.3-pre.d.ts @@ -0,0 +1,18 @@ +/** @noSelfInFile */ + +declare namespace math { + /** + * Returns the hyperbolic cosine of x. + */ + function cosh(x: number): number; + + /** + * Returns the hyperbolic sine of x. + */ + function sinh(x: number): number; + + /** + * Returns the hyperbolic tangent of x. + */ + function tanh(x: number): number; +} diff --git a/special/5.1-plus-or-jit.d.ts b/special/5.1-plus-or-jit.d.ts new file mode 100644 index 0000000..f0a645a --- /dev/null +++ b/special/5.1-plus-or-jit.d.ts @@ -0,0 +1,456 @@ +/** @noSelfInFile */ + +declare namespace debug { + /** + * Returns the current hook settings of the thread, as three values: the + * current hook function, the current hook mask, and the current hook count + * (as set by the debug.sethook function). + */ + function gethook( + thread?: LuaThread + ): LuaMultiReturn<[undefined, 0] | [Function, number, string?]>; + + /** + * Returns a table with information about a function. You can give the + * function directly or you can give a number as the value of f, which means + * the function running at level f of the call stack of the given thread: + * level 0 is the current function (getinfo itself); level 1 is the function + * that called getinfo (except for tail calls, which do not count on the + * stack); and so on. If f is a number larger than the number of active + * functions, then getinfo returns nil. + * + * The returned table can contain all the fields returned by lua_getinfo, with + * the string what describing which fields to fill in. The default for what is + * to get all information available, except the table of valid lines. If + * present, the option 'f' adds a field named func with the function itself. + * If present, the option 'L' adds a field named activelines with the table of + * valid lines. + * + * For instance, the expression debug.getinfo(1,"n").name returns a name for + * the current function, if a reasonable name can be found, and the expression + * debug.getinfo(print) returns a table with all available information about + * the print function. + */ + function getinfo(f: T): FunctionInfo; + function getinfo(f: T, what: string): Partial>; + function getinfo(thread: LuaThread, f: T): FunctionInfo; + function getinfo( + thread: LuaThread, + f: T, + what: string + ): Partial>; + function getinfo(f: number): FunctionInfo | undefined; + function getinfo(f: number, what: string): Partial | undefined; + function getinfo(thread: LuaThread, f: number): FunctionInfo | undefined; + function getinfo(thread: LuaThread, f: number, what: string): Partial | undefined; + + /** + * Returns the metatable of the given value or nil if it does not have a + * metatable. + */ + function getmetatable(value: T): LuaMetatable | undefined; + + /** + * Returns the registry table (see §4.5). + */ + function getregistry(): Record; + + /** + * Returns the Lua value associated to u. If u is not a full userdata, returns + * nil. + */ + function getuservalue(u: LuaUserdata): any; + + /** + * Sets the given function as a hook. The string mask and the number count + * describe when the hook will be called. The string mask may have any + * combination of the following characters, with the given meaning: + * + * * 'c': the hook is called every time Lua calls a function; + * * 'r': the hook is called every time Lua returns from a function; + * * 'l': the hook is called every time Lua enters a new line of code. + * + * Moreover, with a count different from zero, the hook is called also after + * every count instructions. + * + * When called without arguments, debug.sethook turns off the hook. + * + * When the hook is called, its first parameter is a string describing the + * event that has triggered its call: "call" (or "tail call"), "return", + * "line", and "count". For line events, the hook also gets the new line + * number as its second parameter. Inside a hook, you can call getinfo with + * level 2 to get more information about the running function (level 0 is the + * getinfo function, and level 1 is the hook function). + */ + function sethook(): void; + function sethook( + hook: (event: 'call' | 'return' | 'line' | 'count', line?: number) => any, + mask: string, + count?: number + ): void; + function sethook( + thread: LuaThread, + hook: (event: 'call' | 'return' | 'line' | 'count', line?: number) => any, + mask: string, + count?: number + ): void; + + /** + * This function assigns the value value to the local variable with index + * local of the function at level level of the stack. The function returns nil + * if there is no local variable with the given index, and raises an error + * when called with a level out of range. (You can call getinfo to check + * whether the level is valid.) Otherwise, it returns the name of the local + * variable. + * + * See debug.getlocal for more information about variable indices and names. + */ + function setlocal(level: number, local: number, value: any): string | undefined; + function setlocal( + thread: LuaThread, + level: number, + local: number, + value: any + ): string | undefined; + + /** + * Sets the metatable for the given value to the given table (which can be + * nil). Returns value. + */ + function setmetatable< + T extends object, + TIndex extends object | ((this: T, key: any) => any) | undefined = undefined + >( + value: T, + table?: LuaMetatable | null + ): TIndex extends (this: T, key: infer TKey) => infer TValue + ? T & { [K in TKey & string]: TValue } + : TIndex extends object + ? T & TIndex + : T; + + /** + * If message is present but is neither a string nor nil, this function + * returns message without further processing. Otherwise, it returns a string + * with a traceback of the call stack. The optional message string is appended + * at the beginning of the traceback. An optional level number tells at which + * level to start the traceback (default is 1, the function calling + * traceback). + */ + function traceback(message?: string | null, level?: number | null): string; + function traceback(thread?: LuaThread, message?: string | null, level?: number | null): string; + function traceback(message: T): T; + function traceback(thread: LuaThread, message: T): T; +} + +/** + * This function is a generic interface to the garbage collector. It performs + * different functions according to its first argument, opt. + * + * Performs a full garbage-collection cycle. This is the default option. + */ +declare function collectgarbage(opt?: 'collect'): void; + +/** + * This function is a generic interface to the garbage collector. It performs + * different functions according to its first argument, opt. + * + * Stops automatic execution of the garbage collector. The collector will run + * only when explicitly invoked, until a call to restart it. + */ +declare function collectgarbage(opt: 'stop'): void; + +/** + * This function is a generic interface to the garbage collector. It performs + * different functions according to its first argument, opt. + * + * Restarts automatic execution of the garbage collector. + */ +declare function collectgarbage(opt: 'restart'): void; + +/** + * This function is a generic interface to the garbage collector. It performs + * different functions according to its first argument, opt. + * + * Sets arg as the new value for the pause of the collector (see §2.5). Returns + * the previous value for pause. + */ +declare function collectgarbage(opt: 'setpause', arg: number): number; + +/** + * This function is a generic interface to the garbage collector. It performs + * different functions according to its first argument, opt. + * + * Sets arg as the new value for the step multiplier of the collector (see + * §2.5). Returns the previous value for step. + */ +declare function collectgarbage(opt: 'setstepmul', arg: number): number; + +/** + * This function is a generic interface to the garbage collector. It performs + * different functions according to its first argument, opt. + * + * Performs a garbage-collection step. The step "size" is controlled by arg. + * With a zero value, the collector will perform one basic (indivisible) step. + * For non-zero values, the collector will perform as if that amount of memory + * (in KBytes) had been allocated by Lua. Returns true if the step finished a + * collection cycle. + */ +declare function collectgarbage(opt: 'step', arg: number): boolean; + +/** + * Returns the length of the object v, which must be a table or a string, + * without invoking the __len metamethod. Returns an integer. + */ +declare function rawlen(v: object | string): number; + +/** + * If index is a number, returns all arguments after argument number index; a + * negative number indexes from the end (-1 is the last argument). Otherwise, + * index must be the string "#", and select returns the total number of extra + * arguments it received. + */ +declare function select(index: number, ...args: T[]): LuaMultiReturn; + +/** + * If index is a number, returns all arguments after argument number index; a + * negative number indexes from the end (-1 is the last argument). Otherwise, + * index must be the string "#", and select returns the total number of extra + * arguments it received. + */ +declare function select(index: '#', ...args: T[]): number; + +declare namespace io { + /** + * This function is system dependent and is not available on all platforms. + * + * Starts program prog in a separated process and returns a file handle that + * you can use to read data from this program (if mode is "r", the default) + * or to write data to this program (if mode is "w"). + */ + function popen(prog: string, mode?: 'r' | 'w'): LuaMultiReturn<[LuaFile] | [undefined, string]>; +} + +interface LuaFile { + /** + * Sets the buffering mode for an output file. There are three available + * modes: + * + * * "no": no buffering; the result of any output operation appears + * immediately. + * * "full": full buffering; output operation is performed only when the + * buffer is full or when you explicitly flush the file (see io.flush). + * * "line": line buffering; output is buffered until a newline is output or + * there is any input from some special files (such as a terminal device). + * For the last two cases, size specifies the size of the buffer, in bytes. + * The default is an appropriate size. + */ + setvbuf(mode: 'no' | 'full' | 'line', size?: number): void; +} + +declare namespace math { + /** + * Returns the remainder of the division of x by y that rounds the quotient + * towards zero. (integer/float) + */ + function fmod(x: number, y: number): number; + + /** + * The float value HUGE_VAL, a value larger than any other numeric value. + */ + const huge: number; + + /** + * Returns the integral part of x and the fractional part of x. Its second + * result is always a float. + */ + function modf(x: number): LuaMultiReturn<[number, number]>; +} + +interface LuaMetatable< + T, + TIndex extends object | ((this: T, key: any) => any) | undefined = + | object + | ((this: T, key: any) => any) + | undefined +> { + /** + * the modulo (%) operation. Behavior similar to the addition operation. + */ + __mod?(this: T, operand: any): any; + + /** + * the length (#) operation. If the object is not a string, Lua will try its + * metamethod. If there is a metamethod, Lua calls it with the object as + * argument, and the result of the call (always adjusted to one value) is the + * result of the operation. If there is no metamethod but the object is a + * table, then Lua uses the table length operation (see §3.4.7). Otherwise, + * Lua raises an error. + */ + __len?(this: T): any; +} + +/** + * The package library provides basic facilities for loading modules in Lua. It + * exports one function directly in the global environment: require. Everything + * else is exported in a table package. + */ +declare namespace package { + /** + * A string describing some compile-time configurations for packages. This + * string is a sequence of lines: + * * The first line is the directory separator string. Default is '\' for + * Windows and '/' for all other systems. + * * The second line is the character that separates templates in a path. + * Default is ';'. + * * The third line is the string that marks the substitution points in a + * template. Default is '?'. + * * The fourth line is a string that, in a path in Windows, is replaced by + * the executable's directory. Default is '!'. + * * The fifth line is a mark to ignore all text after it when building the + * luaopen_ function name. Default is '-'. + */ + var config: string; + + /** + * 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_5_3, or the + * environment variable LUA_CPATH, or a default path defined in luaconf.h. + */ + var 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. + * + * This variable is only a reference to the real table; assignments to this + * variable do not change the table used by require. + */ + const loaded: Record; + + /** + * 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). + */ + function loadlib( + libname: string, + funcname: string + ): [Function] | [undefined, string, 'open' | 'init']; + + /** + * 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_5_3 or the environment variable LUA_PATH or + * with a default path defined in luaconf.h, if those environment variables + * are not defined. Any ";;" in the value of the environment variable is + * replaced by the default path. + */ + var path: string; + + /** + * A table to store loaders for specific modules (see require). + * + * This variable is only a reference to the real table; assignments to this + * variable do not change the table used by require. + */ + const preload: Record any>; + + /** + * 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.) + */ + function searchpath(name: string, path: string, sep?: string, rep?: string): string; +} + +declare namespace string { + /** + * Returns the internal numeric 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. These + * indices are corrected following the same rules of function string.sub. + * + * Numeric codes are not necessarily portable across platforms. + */ + function byte(s: string, i?: number): number; + function byte(s: string, i?: number, j?: number): LuaMultiReturn; + + /** + * Returns an iterator function that, each time it is called, returns the next + * captures from pattern (see §6.4.1) over the string s. If pattern specifies + * no captures, then the whole match is produced in each call. + * + * As an example, the following loop will iterate over all the words from + * string s, printing one per line: + * + * ``` + * s = "hello world from Lua" + * for w in string.gmatch(s, "%a+") do + * print(w) + * end + * ``` + * + * 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 caret '^' at the start of a pattern does not work as + * an anchor, as this would prevent the iteration. + */ + function gmatch(s: string, pattern: string): LuaIterable>; + + /** + * Looks for the first match of pattern (see §6.4.1) 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 numeric argument init specifies where to start + * the search; its default value is 1 and can be negative. + */ + function match(s: string, pattern: string, init?: number): LuaMultiReturn; + + /** + * Returns a string that is the string s reversed. + */ + function reverse(s: string): string; +} diff --git a/special/5.1.d.ts b/special/5.1.d.ts index 3d4cac0..889834c 100644 --- a/special/5.1.d.ts +++ b/special/5.1.d.ts @@ -1,4 +1,8 @@ +/// +/// /// /// +/// +/// /// /// diff --git a/special/5.2.d.ts b/special/5.2.d.ts index aa82b3e..7e92448 100644 --- a/special/5.2.d.ts +++ b/special/5.2.d.ts @@ -1,3 +1,5 @@ +/// +/// /// /// /// diff --git a/special/5.3-pre.d.ts b/special/5.3-pre.d.ts index 82a5aa3..4111466 100644 --- a/special/5.3-pre.d.ts +++ b/special/5.3-pre.d.ts @@ -13,11 +13,6 @@ declare namespace math { */ function atan2(y: number, x: number): number; - /** - * Returns the hyperbolic cosine of x. - */ - function cosh(x: number): number; - /** * Returns m and e such that x = m2e, e is an integer and the absolute value * of m is in the range [0.5, 1) (or zero when x is zero). @@ -33,14 +28,4 @@ declare namespace math { * Returns xy. (You can also use the expression x^y to compute this value.) */ function pow(x: number, y: number): number; - - /** - * Returns the hyperbolic sine of x. - */ - function sinh(x: number): number; - - /** - * Returns the hyperbolic tangent of x. - */ - function tanh(x: number): number; } diff --git a/special/5.3.d.ts b/special/5.3.d.ts index e532228..609c68d 100644 --- a/special/5.3.d.ts +++ b/special/5.3.d.ts @@ -1,3 +1,4 @@ +/// /// /// /// diff --git a/special/5.4.d.ts b/special/5.4.d.ts index a4a8f18..5676d98 100644 --- a/special/5.4.d.ts +++ b/special/5.4.d.ts @@ -1,3 +1,4 @@ +/// /// /// /// diff --git a/special/jit.d.ts b/special/jit.d.ts index fcc7c40..e9e557c 100644 --- a/special/jit.d.ts +++ b/special/jit.d.ts @@ -1,5 +1,8 @@ +/// /// +/// /// +/// /// /// /// From 34b728a846eedda2d6e52cf44d6fb38ee615fd8c Mon Sep 17 00:00:00 2001 From: Ryan Young Date: Sat, 7 May 2022 23:58:57 -0700 Subject: [PATCH 2/7] fix: add minor versions for lua 5.0 --- core/global.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/global.d.ts b/core/global.d.ts index d339973..df1122f 100644 --- a/core/global.d.ts +++ b/core/global.d.ts @@ -11,7 +11,7 @@ type LuaUserdata = { readonly __internal__: unique symbol }; * A global variable (not a function) that holds a string containing the running * Lua version. */ -declare const _VERSION: 'Lua 5.0' | 'Lua 5.1' | 'Lua 5.2' | 'Lua 5.3' | 'Lua 5.4'; +declare const _VERSION: 'Lua 5.0' | 'Lua 5.0.1' | 'Lua 5.0.2' | 'Lua 5.0.3' | 'Lua 5.1' | 'Lua 5.2' | 'Lua 5.3' | 'Lua 5.4'; /** * A global variable (not a function) that holds the global environment (see From c2c7fa96f2f4c4e6223a4b47f182c76165fb356b Mon Sep 17 00:00:00 2001 From: Ryan Young Date: Fri, 22 Jul 2022 22:09:50 -0700 Subject: [PATCH 3/7] refactor: move lua 5.0 stuff to separate files, because it's so different --- 5.0.d.ts | 2 +- special/5.0-only.d.ts => core/5.0/debug.d.ts | 206 ++++----- core/5.0/global.d.ts | 215 +++++++++ core/5.0/io.d.ts | 242 ++++++++++ core/5.0/math.d.ts | 146 ++++++ core/5.0/metatable.d.ts | 151 ++++++ core/5.0/modules.d.ts | 46 ++ core/5.0/os.d.ts | 213 +++++++++ core/5.0/string.d.ts | 196 ++++++++ core/5.0/table.d.ts | 94 ++++ core/debug.d.ts | 141 ++++++ core/global.d.ts | 79 +++- core/index-5.0.d.ts | 12 + core/io.d.ts | 24 + core/math.d.ts | 17 + core/metatable.d.ts | 15 + core/modules.d.ts | 108 +++++ core/string.d.ts | 45 ++ special/5.0-or-5.1-or-jit.d.ts | 38 -- special/5.0-or-5.1.d.ts | 66 --- special/5.0.d.ts | 93 +++- special/5.1-only.d.ts | 65 +++ special/5.1-or-jit.d.ts | 37 ++ special/5.1-plus-and-5.3-pre.d.ts | 18 - special/5.1-plus-or-jit.d.ts | 456 ------------------- special/5.1.d.ts | 4 - special/5.2.d.ts | 2 - special/5.3-pre.d.ts | 15 + special/5.3.d.ts | 1 - special/5.4.d.ts | 1 - special/jit.d.ts | 3 - 31 files changed, 2035 insertions(+), 716 deletions(-) rename special/5.0-only.d.ts => core/5.0/debug.d.ts (50%) create mode 100644 core/5.0/global.d.ts create mode 100644 core/5.0/io.d.ts create mode 100644 core/5.0/math.d.ts create mode 100644 core/5.0/metatable.d.ts create mode 100644 core/5.0/modules.d.ts create mode 100644 core/5.0/os.d.ts create mode 100644 core/5.0/string.d.ts create mode 100644 core/5.0/table.d.ts create mode 100644 core/index-5.0.d.ts delete mode 100644 special/5.0-or-5.1-or-jit.d.ts delete mode 100644 special/5.0-or-5.1.d.ts delete mode 100644 special/5.1-plus-and-5.3-pre.d.ts delete mode 100644 special/5.1-plus-or-jit.d.ts diff --git a/5.0.d.ts b/5.0.d.ts index b5af446..e67711d 100644 --- a/5.0.d.ts +++ b/5.0.d.ts @@ -1,2 +1,2 @@ -/// +/// /// diff --git a/special/5.0-only.d.ts b/core/5.0/debug.d.ts similarity index 50% rename from special/5.0-only.d.ts rename to core/5.0/debug.d.ts index 153b77e..321e6ff 100644 --- a/special/5.0-only.d.ts +++ b/core/5.0/debug.d.ts @@ -1,25 +1,70 @@ -/** @noSelfInFile */ +// Based on https://www.lua.org/manual/5.3/manual.html#6.10 -/** - * Sets the garbage-collection threshold to the given limit (in Kbytes) and - * checks it against the byte counter. If the new threshold is smaller than the - * byte counter, then Lua immediately runs the garbage collector (see 2.9). If - * limit is absent, it defaults to zero (thus forcing a garbage-collection - * cycle). - */ -declare function collectgarbage(limit: number): void; +/** @noSelfInFile */ /** - * Returns all elements from the given list. This function is equivalent to + * This library provides the functionality of the debug interface (§4.9) to Lua + * programs. You should exert care when using this library. Several of its + * functions violate basic assumptions about Lua code (e.g., that variables + * local to a function cannot be accessed from outside; that userdata metatables + * cannot be changed by Lua code; that Lua programs do not crash) and therefore + * can compromise otherwise secure code. Moreover, some functions in this + * library may be slow. * - * `return list[1], list[2], ..., list[n]` - * - * except that the above code can be written only for a fixed n. The number n is - * the size of the list, as defined for the table.getn function. + * All functions in this library are provided inside the debug table. All + * functions that operate over a thread have an optional first argument which is + * the thread to operate over. The default is always the current thread. */ -declare function unpack(list: T): LuaMultiReturn; - declare namespace debug { + /** + * Enters an interactive mode with the user, running each string that the user + * enters. Using simple commands and other debug facilities, the user can + * inspect global and local variables, change their values, evaluate + * expressions, and so on. A line containing only the word cont finishes this + * function, so that the caller continues its execution. + * + * Note that commands for debug.debug are not lexically nested within any + * function and so have no direct access to local variables. + */ + function debug(): void; + + interface FunctionInfo { + /** + * The function itself. + */ + func: T; + + /** + * A reasonable name for the function. + */ + name?: string; + /** + * What the `name` field means. The empty string means that Lua did not find + * a name for the function. + */ + namewhat: 'global' | 'local' | 'method' | 'field' | ''; + + source: string; + /** + * A short version of source (up to 60 characters), useful for error + * messages. + */ + short_src: string; + linedefined: number; + lastlinedefined: number; + /** + * What this function is. + */ + what: 'Lua' | 'C' | 'main'; + + currentline: number; + + /** + * Number of upvalues of that function. + */ + nups: number; + } + /** * Returns the current hook settings of the thread, as three values: the * current hook function, the current hook mask, and the current hook count @@ -63,6 +108,17 @@ declare namespace debug { */ function getlocal(level: number, local: number): LuaMultiReturn<[string, any]>; + /** + * This function returns the name and the value of the upvalue with index up + * of the function f. The function returns nil if there is no upvalue with the + * given index. + * + * Variable names starting with '(' (open parenthesis) represent variables + * with no known names (variables from chunks saved without debug + * information). + */ + function getupvalue(f: Function, up: number): LuaMultiReturn<[string, any] | []>; + /** * Sets the given function as a hook. The string mask and the number count * describe when the hook will be called. The string mask may have any @@ -104,116 +160,24 @@ declare namespace debug { function setlocal(level: number, local: number, value: any): string | undefined; /** - * Returns a string with a traceback of the call stack. An optional message - * string is appended at the beginning of the traceback. This function is - * typically used with xpcall to produce better error messages. + * This function assigns the value value to the upvalue with index up of the + * function f. The function returns nil if there is no upvalue with the given + * index. Otherwise, it returns the name of the upvalue. */ - function traceback(message: T): T; -} + function setupvalue(f: Function, up: number, value: any): string | undefined; -/** - * Sets the garbage-collection threshold to the given limit (in Kbytes) and - * checks it against the byte counter. If the new threshold is smaller than the - * byte counter, then Lua immediately runs the garbage collector (see 2.9). If - * limit is absent, it defaults to zero (thus forcing a garbage-collection - * cycle). - */ -declare function collectgarbage(limit: number): void; - -/** - * Returns two results: the number of Kbytes of dynamic memory that Lua is using - * and the current garbage collector threshold (also in Kbytes). - */ -declare function gcinfo(): LuaMultiReturn<[number, number]>; - -/** - * Links the program with the dynamic C library libname. Inside this library, - * looks for a function funcname and returns this function as a C function. - * - * libname must be the complete file name of the C library, including any - * eventual path and extension. - * - * This function is not supported by ANSI C. As such, it is only available on - * some platforms (Windows, Linux, Solaris, BSD, plus other Unix systems that - * support the dlfcn standard). - */ - declare function loadlib(libname: string, funcname: string): undefined | Function; - -declare namespace string { - /** - * Returns the internal numerical code of the i-th character of s, or nil if - * the index is out of range. If i is absent, then it is assumed to be 1. i - * may be negative. - * - * Note that numerical codes are not necessarily portable across platforms. - */ - function byte(s: string, i?: number): number; - - /** - * Returns an iterator function that, each time it is called, returns the - * next captures from pattern pat over string s. - * - * If pat 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.gfind(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.gfind(s, "(%w+)=(%w+)") do - * t[k] = v - * end - * ``` - */ - function gfind(s: string, pattern: string): LuaIterable>; -} - -declare namespace table { /** - * Executes the given f over all elements of table. For each element, f is - * called with the index and respective value as arguments. If f returns a - * non-nil value, then the loop is broken, and this value is returned as the - * final value of foreach. - * - * See the next function for extra information about table traversals. - */ - function foreach(table: object, f: (index: any, value: any) => any): any; - - /** - * Executes the given f over the numerical indices of table. For each index, - * f is called with the index and respective value as arguments. Indices are - * visited in sequential order, from 1 to n, where n is the size of the - * table (see 5.4). If f returns a non-nil value, then the loop is broken - * and this value is returned as the result of foreachi. - */ - function foreachi(table: object, f: (index: number, value: any) => any): any; - - /** - * Returns the size of a table, when seen as a list. If the table has an n - * field with a numeric value, this value is the size of the table. - * Otherwise, if there was a previous call to table.setn over this table, - * the respective value is returned. Otherwise, the size is one less the - * first integer index with a nil value. + * Sets the given value as the Lua value associated to the given udata. udata + * must be a full userdata. + * + * Returns udata. */ - function getn(table: object): number; + function setuservalue(udata: LuaUserdata, value: any): LuaUserdata; /** - * Updates the size of a table. If the table has a field "n" with a - * numerical value, that value is changed to the given n. Otherwise, it - * updates an internal state so that subsequent calls to table.getn(table) - * return n. + * Returns a string with a traceback of the call stack. An optional message + * string is appended at the beginning of the traceback. This function is + * typically used with xpcall to produce better error messages. */ - function setn(table: object, n: number): void; + function traceback(message: T): T; } diff --git a/core/5.0/global.d.ts b/core/5.0/global.d.ts new file mode 100644 index 0000000..b7f575d --- /dev/null +++ b/core/5.0/global.d.ts @@ -0,0 +1,215 @@ +// Based on https://www.lua.org/manual/5.3/manual.html#6.1 + +/// + +/** @noSelfInFile */ + +type LuaThread = { readonly __internal__: unique symbol }; +type LuaUserdata = { readonly __internal__: unique symbol }; + +/** + * A global variable (not a function) that holds a string containing the running + * Lua version. + */ +declare const _VERSION: 'Lua 5.0' | 'Lua 5.0.1' | 'Lua 5.0.2' | 'Lua 5.0.3' + +/** + * A global variable (not a function) that holds the global environment (see + * §2.2). Lua itself does not use this variable; changing its value does not + * affect any environment, nor vice versa. + */ +declare const _G: typeof globalThis; + +/** + * Calls error if the value of its argument `v` is false (i.e., nil or false); + * otherwise, returns all its arguments. In case of error, `message` is the + * error object; when absent, it defaults to "assertion failed!" + */ +declare function assert(v: V): Exclude; +declare function assert( + v: V, + ...args: A +): LuaMultiReturn<[Exclude, ...A]>; + +/** + * 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). + */ +declare function dofile(filename?: string): any; + +/** + * Terminates the last protected function called and returns message as the + * error object. Function error never returns. + * + * Usually, error adds some information about the error position at the + * beginning of the message, if the message is a string. 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(message: string, level?: number): never; + +/** + * 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(object: T): LuaMetatable | undefined; + +/** + * 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 key–value pairs (1,t[1]), (2,t[2]), ..., up to the + * first nil value. + */ +declare function ipairs( + t: Record +): LuaIterable]>>; + +/** + * 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 numerical order, use a numerical + * for.) + * + * 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. + */ +declare function next(table: object, index?: any): LuaMultiReturn<[any, any] | []>; + +/** + * 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: LuaTable +): LuaIterable]>>; +declare function pairs(t: T): LuaIterable]>>; + +/** + * 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. + */ +declare function pcall( + f: (this: This, ...args: Args) => R, + context: This, + ...args: Args +): LuaMultiReturn<[true, R] | [false, string]>; + +declare function pcall( + f: (this: void, ...args: A) => R, + ...args: A +): LuaMultiReturn<[true, R] | [false, string]>; + +/** + * Receives any number of arguments and prints their values to stdout, using the + * tostring function to convert each argument to a string. print is not intended + * for formatted output, but only as a quick way to show a value, for instance + * for debugging. For complete control over the output, use string.format and + * io.write. + */ +declare function print(...args: any[]): void; + +/** + * Checks whether v1 is equal to v2, without invoking the __eq metamethod. + * Returns a boolean. + */ +declare function rawequal(v1: T, v2: T): boolean; + +/** + * Gets the real value of table[index], without invoking the __index metamethod. + * table must be a table; index may be any value. + */ +declare function rawget(table: T, index: K): T[K]; + +/** + * Sets the real value of table[index] to value, without invoking the __newindex + * metamethod. table must be a table, index any value different from nil and + * NaN, and value any Lua value. + * + * This function returns table. + */ +declare function rawset(table: T, index: K, value: T[K]): T; + +/** + * Sets the metatable for the given table. (To change the metatable of other + * types from Lua code, you must use the debug library (§6.10).) 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, + TIndex extends object | ((this: T, key: any) => any) | undefined = undefined +>( + table: T, + metatable?: LuaMetatable | null +): TIndex extends (this: T, key: infer TKey) => infer TValue + ? T & { [K in TKey & string]: TValue } + : TIndex extends object + ? T & TIndex + : T; + +/** + * When called with no base, tonumber 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. + * + * The conversion of strings can result in integers or floats, according to the + * lexical conventions of Lua (see §3.1). (The string may have leading and + * trailing spaces and a sign.) + * + * When called with base, then e must be a string to be interpreted as an + * integer numeral in that base. 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. If + * the string e is not a valid numeral in the given base, the function returns + * nil. + */ +declare function tonumber(e: any, base?: number): number | undefined; + +/** + * Receives a value of any type and converts it to a string in a human-readable + * format. (For complete control of how numbers are converted, use + * string.format.) + * + * If the metatable of v has a __tostring field, then tostring calls the + * corresponding value with v as argument, and uses the result of the call as + * its result. + */ +declare function tostring(v: any): string; + +/** + * Returns the type of its only argument, coded as a string. + */ +declare function type( + v: any +): 'nil' | 'number' | 'string' | 'boolean' | 'table' | 'function' | 'thread' | 'userdata'; diff --git a/core/5.0/io.d.ts b/core/5.0/io.d.ts new file mode 100644 index 0000000..c996fb2 --- /dev/null +++ b/core/5.0/io.d.ts @@ -0,0 +1,242 @@ +// Based on https://www.lua.org/manual/5.3/manual.html#6.8 + +/** @noSelfInFile */ + +/** + * The I/O library provides two different styles for file manipulation. The + * first one uses implicit file handles; that is, there are operations to set a + * default input file and a default output file, and all input/output operations + * are over these default files. The second style uses explicit file handles. + * + * When using implicit file handles, all operations are supplied by table io. + * When using explicit file handles, the operation io.open returns a file handle + * and then all operations are supplied as methods of the file handle. + * + * The table io also provides three predefined file handles with their usual + * meanings from C: io.stdin, io.stdout, and io.stderr. The I/O library never + * closes these files. + * + * Unless otherwise stated, all I/O functions return nil on failure (plus an + * error message as a second result and a system-dependent error code as a third + * result) and some value different from nil on success. On non-POSIX systems, + * the computation of the error message and error code in case of errors may be + * not thread safe, because they rely on the global C variable errno. + */ +declare namespace io { + /** + * Equivalent to file:close(). Without a file, closes the default output file. + */ + function close(file?: LuaFile): boolean; + + /** + * Equivalent to io.output():flush(). + */ + function flush(): boolean; + + /** + * When called with a file name, it opens the named file (in text mode), and + * sets its handle as the default input file. When called with a file handle, + * it simply sets this file handle as the default input file. When called + * without parameters, it returns the current default input file. + * + * In case of errors this function raises the error, instead of returning an + * error code. + */ + function input(file?: string | LuaFile): LuaFile; + + /** + * Opens the given file name in read mode and returns an iterator function + * that works like file:lines(···) over the opened file. When the iterator + * function detects the end of file, it returns no values (to finish the loop) + * and automatically closes the file. + * + * The call io.lines() (with no file name) is equivalent to + * io.input():lines("*l"); that is, it iterates over the lines of the default + * input file. In this case it does not close the file when the loop ends. + * + * In case of errors this function raises the error, instead of returning an + * error code. + */ + function lines( + filename?: string, + ...formats: T + ): LuaIterable< + LuaMultiReturn<[] extends T ? [string] : { [P in keyof T]: FileReadFormatToType }> + >; + + /** + * This function opens a file, in the mode specified in the string mode. In + * case of success, it returns a new file handle. + * + * The mode string can be any of the following: + * + * * "r": read mode (the default); + * * "w": write mode; + * * "a": append mode; + * * "r+": update mode, all previous data is preserved; + * * "w+": update mode, all previous data is erased; + * * "a+": append update mode, previous data is preserved, writing is only + * allowed at the end of file. + * + * The mode string can also have a 'b' at the end, which is needed in some + * systems to open the file in binary mode. + */ + function open( + filename: string, + mode?: string + ): LuaMultiReturn<[LuaFile] | [undefined, string, number]>; + + /** + * Similar to io.input, but operates over the default output file. + */ + function output(file?: string | LuaFile): LuaFile; + + /** + * Equivalent to io.input():read(···). + */ + function read(): io.FileReadFormatToType | undefined; + function read(format: T): io.FileReadFormatToType | undefined; + function read( + ...formats: T + ): LuaMultiReturn<{ [P in keyof T]?: io.FileReadFormatToType }>; + + /** + * Predefined file handle for standard error stream. The I/O library never closes this file. + */ + const stderr: LuaFile; + + /** + * Predefined file handle for standard input stream. The I/O library never closes this file. + */ + const stdin: LuaFile; + + /** + * Predefined file handle for standard output stream. The I/O library never closes this file. + */ + const stdout: LuaFile; + + /** + * In case of success, returns a handle for a temporary file. This file is + * opened in update mode and it is automatically removed when the program + * ends. + */ + function tmpfile(): LuaFile; + + /** + * Checks whether obj is a valid file handle. Returns the string "file" if obj + * is an open file handle, "closed file" if obj is a closed file handle, or + * nil if obj is not a file handle. + */ + function type(obj: any): 'file' | 'closed file' | undefined; + + /** + * Equivalent to io.output():write(···). + */ + function write(...args: (string | number)[]): LuaMultiReturn<[LuaFile] | [undefined, string]>; + + type FileReadFormatToType = T extends FileReadNumberFormat ? number : string; +} + +interface LuaFile { + /** + * Closes file. Note that files are automatically closed when their handles + * are garbage collected, but that takes an unpredictable amount of time to + * happen. + * + * When closing a file handle created with io.popen, file:close returns the + * same values returned by os.execute. + */ + close(): boolean; + + /** + * Saves any written data to file. + */ + flush(): boolean; + + /** + * Returns an iterator function that, each time it is called, reads the file + * according to the given formats. When no format is given, uses "l" as a + * default. As an example, the construction + * + * `for c in file:lines(1) do body end` + * + * will iterate over all characters of the file, starting at the current + * position. Unlike io.lines, this function does not close the file when the + * loop ends. + * + * In case of errors this function raises the error, instead of returning an + * error code. + */ + lines( + ...formats: T + ): LuaIterable< + LuaMultiReturn<[] extends T ? [string] : { [P in keyof T]: io.FileReadFormatToType }> + >; + + /** + * Reads the file file, according to the given formats, which specify what to + * read. For each format, the function returns a string or a number with the + * characters read, or nil if it cannot read data with the specified format. + * (In this latter case, the function does not read subsequent formats.) When + * called without formats, it uses a default format that reads the next line + * (see below). + * + * The available formats are + * + * * "n": reads a numeral and returns it as a float or an integer, following + * the lexical conventions of Lua. (The numeral may have leading spaces and + * a sign.) This format always reads the longest input sequence that is a + * valid prefix for a numeral; if that prefix does not form a valid numeral + * (e.g., an empty string, "0x", or "3.4e-"), it is discarded and the + * function returns nil. + * * "a": reads the whole file, starting at the current position. On end of + * file, it returns the empty string. + * * "l": reads the next line skipping the end of line, returning nil on end + * of file. This is the default format. + * * "L": reads the next line keeping the end-of-line character (if present), + * returning nil on end of file. + * * number: reads a string with up to this number of bytes, returning nil on + * end of file. If number is zero, it reads nothing and returns an empty + * string, or nil on end of file. + * + * The formats "l" and "L" should be used only for text files. + */ + read(): io.FileReadFormatToType | undefined; + read(format: T): io.FileReadFormatToType | undefined; + read( + ...formats: T + ): LuaMultiReturn<{ [P in keyof T]?: io.FileReadFormatToType }>; + + /** + * Sets and geionts the file position, measured from the beginning of the + * file, to the posit given by offset plus a base specified by the string + * whence, as follows: + * + * * "set": base is position 0 (beginning of the file); + * * "cur": base is current position; + * * "end": base is end of file; + * + * In case of success, seek returns the final file position, measured in bytes + * from the beginning of the file. If seek fails, it returns nil, plus a + * string describing the error. + * + * The default value for whence is "cur", and for offset is 0. Therefore, the + * call file:seek() returns the current file position, without changing it; + * the call file:seek("set") sets the position to the beginning of the file + * (and returns 0); and the call file:seek("end") sets the position to the end + * of the file, and returns its size. + */ + seek( + whence?: 'set' | 'cur' | 'end', + offset?: number + ): LuaMultiReturn<[number] | [undefined, string]>; + + /** + * Writes the value of each of its arguments to file. The arguments must be + * strings or numbers. + * + * In case of success, this function returns file. Otherwise it returns nil + * plus a string describing the error. + */ + write(...args: (string | number)[]): LuaMultiReturn<[LuaFile] | [undefined, string]>; +} diff --git a/core/5.0/math.d.ts b/core/5.0/math.d.ts new file mode 100644 index 0000000..5468a7f --- /dev/null +++ b/core/5.0/math.d.ts @@ -0,0 +1,146 @@ +// Based on https://www.lua.org/manual/5.3/manual.html#6.7 + +/** @noSelfInFile */ + +/** + * This library provides basic mathematical functions. It provides all its + * functions and constants inside the table math. Functions with the annotation + * "integer/float" give integer results for integer arguments and float results + * for float (or mixed) arguments. Rounding functions (math.ceil, math.floor, + * and math.modf) return an integer when the result fits in the range of an + * integer, or a float otherwise. + */ +declare namespace math { + /** + * Returns the absolute value of x. (integer/float) + */ + function abs(x: number): number; + + /** + * Returns the arc cosine of x (in radians). + */ + function acos(x: number): number; + + /** + * Returns the arc sine of x (in radians). + */ + function asin(x: number): number; + + /** + * Returns the arc tangent of x (in radians). + */ + function atan(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.) + */ + function atan2(y: number, x: number): number; + + /** + * Returns the smallest integral value larger than or equal to x. + */ + function ceil(x: number): number; + + /** + * Returns the cosine of x (assumed to be in radians). + */ + function cos(x: number): number; + + /** + * Converts the angle x from radians to degrees. + */ + function deg(x: number): number; + + /** + * Returns the value ex (where e is the base of natural logarithms). + */ + function exp(x: number): number; + + /** + * Returns the largest integral value smaller than or equal to x. + */ + function floor(x: number): number; + + /** + * Returns m and e such that x = m2e, e is an integer and the absolute value + * of m is in the range [0.5, 1) (or zero when x is zero). + */ + function frexp(x: number): number; + /** + * Returns m2e (e should be an integer). + */ + function ldexp(m: number, e: number): number; + + /** + * Returns the logarithm of x. + */ + function log(x: number): number; + + /** + * Returns the base-10 logarithm of x. + */ + function log10(x: number): number; + + /** + * Returns the argument with the maximum value, according to the Lua operator + * <. (integer/float) + */ + function max(x: number, ...numbers: number[]): number; + + /** + * Returns the argument with the minimum value, according to the Lua operator + * <. (integer/float) + */ + function min(x: number, ...numbers: number[]): number; + + /** + * The value of π. + */ + const pi: number; + + /** + * Returns xy. (You can also use the expression x^y to compute this value.) + */ + function pow(x: number, y: number): number; + + /** + * Converts the angle x from degrees to radians. + */ + function rad(x: number): number; + + /** + * When called without arguments, returns a pseudo-random float with uniform + * distribution in the range [0,1). When called with two integers m and n, + * math.random returns a pseudo-random integer with uniform distribution in + * the range [m, n]. (The value n-m cannot be negative and must fit in a Lua + * integer.) The call math.random(n) is equivalent to math.random(1,n). + * + * This function is an interface to the underling pseudo-random generator + * function provided by C. + */ + function random(m?: number, n?: number): number; + + /** + * Sets x as the "seed" for the pseudo-random generator: equal seeds produce + * equal sequences of numbers. + */ + function randomseed(x: number): number; + + /** + * Returns the sine of x (assumed to be in radians). + */ + function sin(x: number): number; + + /** + * Returns the square root of x. (You can also use the expression x^0.5 to + * compute this value.) + */ + function sqrt(x: number): number; + + /** + * Returns the tangent of x (assumed to be in radians). + */ + function tan(x: number): number; +} diff --git a/core/5.0/metatable.d.ts b/core/5.0/metatable.d.ts new file mode 100644 index 0000000..b2479b1 --- /dev/null +++ b/core/5.0/metatable.d.ts @@ -0,0 +1,151 @@ +// Based on https://www.lua.org/manual/5.3/manual.html#2.4 + +interface LuaMetatable< + T, + TIndex extends object | ((this: T, key: any) => any) | undefined = + | object + | ((this: T, key: any) => any) + | undefined +> { + /** + * the addition (+) operation. If any operand for an addition is not a number + * (nor a string coercible to a number), Lua will try to call a metamethod. + * First, Lua will check the first operand (even if it is valid). If that + * operand does not define a metamethod for __add, then Lua will check the + * second operand. If Lua can find a metamethod, it calls the metamethod with + * the two operands as arguments, and the result of the call (adjusted to one + * value) is the result of the operation. Otherwise, it raises an error. + */ + __add?(this: T, operand: any): any; + + /** + * the subtraction (-) operation. Behavior similar to the addition operation. + */ + __sub?(this: T, operand: any): any; + + /** + * the multiplication (*) operation. Behavior similar to the addition + * operation. + */ + __mul?(this: T, operand: any): any; + + /** + * the division (/) operation. Behavior similar to the addition operation. + */ + __div?(this: T, operand: any): any; + + /** + * the exponentiation (^) operation. Behavior similar to the addition + * operation. + */ + __pow?(this: T, operand: any): any; + + /** + * the negation (unary -) operation. Behavior similar to the addition + * operation. + */ + __unm?(this: T, operand: any): any; + + /** + * the concatenation (..) operation. Behavior similar to the addition + * operation, except that Lua will try a metamethod if any operand is neither + * a string nor a number (which is always coercible to a string). + */ + __concat?(this: T, operand: any): any; + + /** + * the equal (==) operation. Behavior similar to the addition operation, + * except that Lua will try a metamethod only when the values being compared + * are either both tables or both full userdata and they are not primitively + * equal. The result of the call is always converted to a boolean. + */ + __eq?(this: T, operand: any): boolean; + + /** + * the less than (<) operation. Behavior similar to the addition operation, + * except that Lua will try a metamethod only when the values being compared + * are neither both numbers nor both strings. The result of the call is always + * converted to a boolean. + */ + __lt?(this: T, operand: any): boolean; + + /** + * the less equal (<=) operation. Unlike other operations, the less-equal + * operation can use two different events. First, Lua looks for the __le + * metamethod in both operands, like in the less than operation. If it cannot + * find such a metamethod, then it will try the __lt metamethod, assuming that + * a <= b is equivalent to not (b < a). As with the other comparison + * operators, the result is always a boolean. (This use of the __lt event can + * be removed in future versions; it is also slower than a real __le + * metamethod.) + */ + __le?(this: T, operand: any): boolean; + + /** + * The indexing access table[key]. This event happens when table is not a + * table or when key is not present in table. The metamethod is looked up in + * table. + * + * Despite the name, the metamethod for this event can be either a function or + * a table. If it is a function, it is called with table and key as arguments, + * and the result of the call (adjusted to one value) is the result of the + * operation. If it is a table, the final result is the result of indexing + * this table with key. (This indexing is regular, not raw, and therefore can + * trigger another metamethod.) + */ + __index?: TIndex; + + /** + * The indexing assignment table[key] = value. Like the index event, this + * event happens when table is not a table or when key is not present in + * table. The metamethod is looked up in table. + * + * Like with indexing, the metamethod for this event can be either a function + * or a table. If it is a function, it is called with table, key, and value as + * arguments. If it is a table, Lua does an indexing assignment to this table + * with the same key and value. (This assignment is regular, not raw, and + * therefore can trigger another metamethod.) + * + * Whenever there is a __newindex metamethod, Lua does not perform the + * primitive assignment. (If necessary, the metamethod itself can call rawset + * to do the assignment.) + */ + __newindex?: object | ((this: T, key: any, value: any) => void); + + /** + * The call operation func(args). This event happens when Lua tries to call a + * non-function value (that is, func is not a function). The metamethod is + * looked up in func. If present, the metamethod is called with func as its + * first argument, followed by the arguments of the original call (args). All + * results of the call are the result of the operation. (This is the only + * metamethod that allows multiple results.) + */ + __call?(this: T, ...args: any[]): any; + + /** + * If the metatable of v has a __tostring field, then tostring calls the + * corresponding value with v as argument, and uses the result of the call as + * its result. + */ + __tostring?(this: T): string; + + /** + * If this field is a string containing the character 'k', the keys in the + * table are weak. If it contains 'v', the values in the table are weak. + */ + __mode?: 'k' | 'v' | 'kv'; + + /** + * If the object's metatable has this field, `getmetatable` returns the + * associated value. + */ + __metatable?: any; + + /** + * Userdata finalizer code. When userdata is set to be garbage collected, if + * the metatable has a __gc field pointing to a function, that function is + * first invoked, passing the userdata to it. The __gc metamethod is not + * called for tables. + */ + __gc?(this: T): void; +} diff --git a/core/5.0/modules.d.ts b/core/5.0/modules.d.ts new file mode 100644 index 0000000..7783130 --- /dev/null +++ b/core/5.0/modules.d.ts @@ -0,0 +1,46 @@ +// Based on https://www.lua.org/manual/5.3/manual.html#6.3 + +/** @noSelfInFile */ + +/** + * Links the program with the dynamic C library libname. Inside this library, + * looks for a function funcname and returns this function as a C function. + * + * libname must be the complete file name of the C library, including any + * eventual path and extension. + * + * This function is not supported by ANSI C. As such, it is only available on + * some platforms (Windows, Linux, Solaris, BSD, plus other Unix systems that + * support the dlfcn standard). + */ +declare function loadlib(libname: string, funcname: string): undefined | Function; + +/** + * 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.searchers sequence. By + * changing this sequence, we can change how require looks for a module. The + * following explanation is based on the default configuration for + * package.searchers. + * + * First require queries package.preload[modname]. If it has a value, this value + * (which must 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.searchers). + * + * Once a loader is found, require calls the loader with two arguments: modname + * and an extra value dependent on how it got the loader. (If the loader came + * from a file, this extra value is the file name.) If the loader returns any + * non-nil value, require assigns the returned value to package.loaded[modname]. + * If the loader does not return a non-nil 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 raises an error. + */ +declare function require(modname: string): any; diff --git a/core/5.0/os.d.ts b/core/5.0/os.d.ts new file mode 100644 index 0000000..6799ad3 --- /dev/null +++ b/core/5.0/os.d.ts @@ -0,0 +1,213 @@ +// Based on https://www.lua.org/manual/5.3/manual.html#6.9 + +/** @noSelfInFile */ + +interface LuaDateInfo { + year: number; + month: number; + day: number; + hour?: number; + min?: number; + sec?: number; + isdst?: boolean; +} + +interface LuaDateInfoResult { + year: number; + month: number; + day: number; + hour: number; + min: number; + sec: number; + isdst: boolean; + yday: number; + wday: number; +} + +/** + * Operating System Facilities + */ +declare namespace os { + /** + * Returns an approximation of the amount in seconds of CPU time used by the + * program. + */ + function clock(): number; + + /** + * Returns a string or a table containing date and time, formatted according + * to the given string format. + * + * If the time argument is present, this is the time to be formatted (see the + * os.time function for a description of this value). Otherwise, date formats + * the current time. + * + * If format starts with '!', then the date is formatted in Coordinated + * Universal Time. After this optional character, if format is the string + * "*t", then date returns a table with the following fields: year, month + * (1–12), day (1–31), hour (0–23), min (0–59), sec (0–61), wday (weekday, + * 1–7, Sunday is 1), yday (day of the year, 1–366), and isdst (daylight + * saving flag, a boolean). This last field may be absent if the information + * is not available. + * + * If format is not "*t", then date returns the date as a string, formatted + * according to the same rules as the ISO C function strftime. + * + * When called without arguments, date returns a reasonable date and time + * representation that depends on the host system and on the current locale. + * (More specifically, os.date() is equivalent to os.date("%c").) + * + * On non-POSIX systems, this function may be not thread safe because of its + * reliance on C function gmtime and C function localtime. + */ + function date(format?: string, time?: number): string; + + /** + * Returns a string or a table containing date and time, formatted according + * to the given string format. + * + * If the time argument is present, this is the time to be formatted (see the + * os.time function for a description of this value). Otherwise, date formats + * the current time. + * + * If format starts with '!', then the date is formatted in Coordinated + * Universal Time. After this optional character, if format is the string + * "*t", then date returns a table with the following fields: year, month + * (1–12), day (1–31), hour (0–23), min (0–59), sec (0–61), wday (weekday, + * 1–7, Sunday is 1), yday (day of the year, 1–366), and isdst (daylight + * saving flag, a boolean). This last field may be absent if the information + * is not available. + * + * If format is not "*t", then date returns the date as a string, formatted + * according to the same rules as the ISO C function strftime. + * + * When called without arguments, date returns a reasonable date and time + * representation that depends on the host system and on the current locale. + * (More specifically, os.date() is equivalent to os.date("%c").) + * + * On non-POSIX systems, this function may be not thread safe because of its + * reliance on C function gmtime and C function localtime. + */ + function date(format: '*t', time?: number): LuaDateInfoResult; + + /** + * Returns the difference, in seconds, from time t1 to time t2 (where the + * times are values returned by os.time). In POSIX, Windows, and some other + * systems, this value is exactly t2-t1. + */ + function difftime(t1: number, t2: number): number; + + /** + * This function is equivalent to the C function system. It passes command to + * be executed by an operating system shell. It returns a status code, which + * is system-dependent. If command is absent, then it returns nonzero if a + * shell is available and zero otherwise. + */ + function execute(command?: string): number; + + /** + * Calls the C function exit, with an optional code, to terminate the host + * program. The default value for code is the success code. + */ + function exit(code?: number): never; + + /** + * Returns the value of the process environment variable varname, or nil if + * the variable is not defined. + */ + function getenv(varname: string): string | undefined; + + /** + * Deletes the file (or empty directory, on POSIX systems) with the given + * name. If this function fails, it returns nil, plus a string describing the + * error and the error code. Otherwise, it returns true. + */ + function remove(filename: string): LuaMultiReturn<[true] | [undefined, string]>; + + /** + * Renames the file or directory named oldname to newname. If this function + * fails, it returns nil, plus a string describing the error and the error + * code. Otherwise, it returns true. + */ + function rename(oldname: string, newname: string): LuaMultiReturn<[true] | [undefined, string]>; + + /** + * Sets the current locale of the program. locale is a system-dependent string + * specifying a locale; category is an optional string describing which + * category to change: "all", "collate", "ctype", "monetary", "numeric", or + * "time"; the default category is "all". The function returns the name of the + * new locale, or nil if the request cannot be honored. + * + * If locale is the empty string, the current locale is set to an + * implementation-defined native locale. If locale is the string "C", the + * current locale is set to the standard C locale. + * + * When called with nil as the first argument, this function only returns the + * name of the current locale for the given category. + * + * This function may be not thread safe because of its reliance on C function + * setlocale. + */ + function setlocale( + locale?: string, + category?: 'all' | 'collate' | 'ctype' | 'monetary' | 'numeric' | 'time' + ): string | undefined; + + /** + * Returns the current time when called without arguments, or a time + * representing the local date and time specified by the given table. This + * table must have fields year, month, and day, and may have fields hour + * (default is 12), min (default is 0), sec (default is 0), and isdst (default + * is nil). Other fields are ignored. For a description of these fields, see + * the os.date function. + * + * The values in these fields do not need to be inside their valid ranges. For + * instance, if sec is -10, it means -10 seconds from the time specified by + * the other fields; if hour is 1000, it means +1000 hours from the time + * specified by the other fields. + * + * The returned value is a number, whose meaning depends on your system. In + * POSIX, Windows, and some other systems, this number counts the number of + * seconds since some given start time (the "epoch"). In other systems, the + * meaning is not specified, and the number returned by time can be used only + * as an argument to os.date and os.difftime. + */ + function time(): number; + + /** + * Returns the current time when called without arguments, or a time + * representing the local date and time specified by the given table. This + * table must have fields year, month, and day, and may have fields hour + * (default is 12), min (default is 0), sec (default is 0), and isdst (default + * is nil). Other fields are ignored. For a description of these fields, see + * the os.date function. + * + * The values in these fields do not need to be inside their valid ranges. For + * instance, if sec is -10, it means -10 seconds from the time specified by + * the other fields; if hour is 1000, it means +1000 hours from the time + * specified by the other fields. + * + * The returned value is a number, whose meaning depends on your system. In + * POSIX, Windows, and some other systems, this number counts the number of + * seconds since some given start time (the "epoch"). In other systems, the + * meaning is not specified, and the number returned by time can be used only + * as an argument to os.date and os.difftime. + */ + function time(table: LuaDateInfo): number; + + /** + * Returns a string with a file name that can be used for a temporary file. + * The file must be explicitly opened before its use and explicitly removed + * when no longer needed. + * + * On POSIX systems, this function also creates a file with that name, to + * avoid security risks. (Someone else might create the file with wrong + * permissions in the time between getting the name and creating the file.) + * You still have to open the file to use it and to remove it (even if you do + * not use it). + * + * When possible, you may prefer to use io.tmpfile, which automatically + * removes the file when the program ends. + */ + function tmpname(): string; +} diff --git a/core/5.0/string.d.ts b/core/5.0/string.d.ts new file mode 100644 index 0000000..7db9529 --- /dev/null +++ b/core/5.0/string.d.ts @@ -0,0 +1,196 @@ +// Based on https://www.lua.org/manual/5.3/manual.html#6.4 + +/** @noSelfInFile */ + +/** + * This library provides generic functions for string manipulation, such as + * finding and extracting substrings, and pattern matching. When indexing a + * string in Lua, the first character is at position 1 (not at 0, as in C). + * Indices are allowed to be negative and are interpreted as indexing backwards, + * from the end of the string. Thus, the last character is at position -1, and + * so on. + * + * The string library provides all its functions inside the table string. It + * also sets a metatable for strings where the __index field points to the + * string table. Therefore, you can use the string functions in object-oriented + * style. For instance, string.byte(s,i) can be written as s:byte(i). + * + * The string library assumes one-byte character encodings. + */ +declare namespace string { + /** + * Returns the internal numerical code of the i-th character of s, or nil if + * the index is out of range. If i is absent, then it is assumed to be 1. i + * may be negative. + * + * Note that numerical codes are not necessarily portable across platforms. + */ + function byte(s: string, i?: number): number; + + /** + * Receives zero or more integers. Returns a string with length equal to the + * number of arguments, in which each character has the internal numeric code + * equal to its corresponding argument. + * + * Numeric codes are not necessarily portable across platforms. + */ + function char(...args: number[]): string; + + /** + * Returns a string containing a binary representation of the given function, + * so that a later load on this string returns a copy of the function (but + * with new upvalues). + */ + function dump(func: Function): string; + + /** + * Looks for the first match of pattern (see §6.4.1) 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 numeric + * 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. + */ + function find( + s: string, + pattern: string, + init?: number, + plain?: boolean + ): LuaMultiReturn<[number, number, ...string[]] | []>; + + /** + * 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, a, E, e, f, G, and g all expect a number as + * argument. Options c, d, i, o, u, X, and x expect an integer. When Lua is + * compiled with a C89 compiler, options A and a (hexadecimal floats) do not + * support any modifier (flags, width, length). + * + * Option s expects a string; if its argument is not a string, it is converted + * to one following the same rules of tostring. If the option has any modifier + * (flags, width, length), the string argument should not contain embedded + * zeros. + */ + function format(formatstring: string, ...args: any[]): string; + + /** + * Returns an iterator function that, each time it is called, returns the + * next captures from pattern pat over string s. + * + * If pat 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.gfind(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.gfind(s, "(%w+)=(%w+)") do + * t[k] = v + * end + * ``` + */ + function gfind(s: string, pattern: string): LuaIterable>; + + /** + * Returns a copy of s in which all (or the first n, if given) occurrences of + * the pattern (see §6.4.1) 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. + * The name gsub comes from Global SUBstitution. + * + * 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 %d, with d + * between 1 and 9, stands for the value of the d-th captured substring. 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 repl is a function, then this function is called every time a match + * occurs, with all captured substrings passed as arguments, in order. + * + * In any case, if the pattern specifies no captures, then it behaves as if + * the whole pattern was inside a capture. + * + * 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). + */ + function gsub( + s: string, + pattern: string, + repl: string | Record | ((...matches: string[]) => string), + n?: number + ): LuaMultiReturn<[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. + */ + function len(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. + */ + function lower(s: string): string; + + /** + * Returns a string that is the concatenation of `n` copies of the string `s`. + */ + function rep(s: string, n: number): 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) (for a positive i) returns a suffix of s with length i. + * + * If, after the translation of negative indices, i is less than 1, it is + * corrected to 1. If j is greater than the string length, it is corrected to + * that length. If, after these corrections, i is greater than j, the function + * returns the empty string. + */ + function sub(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. + */ + function upper(s: string): string; +} diff --git a/core/5.0/table.d.ts b/core/5.0/table.d.ts new file mode 100644 index 0000000..3b2107d --- /dev/null +++ b/core/5.0/table.d.ts @@ -0,0 +1,94 @@ +// Based on https://www.lua.org/manual/5.3/manual.html#6.6 + +/** @noSelfInFile */ + +/** + * This library provides generic functions for table manipulation. It provides + * all its functions inside the table table. + * + * Remember that, whenever an operation needs the length of a table, all caveats + * about the length operator apply (see §3.4.7). All functions ignore + * non-numeric keys in the tables given as arguments. + */ +declare namespace table { + /** + * Given a list where all elements are strings or numbers, returns the string + * list[i]..sep..list[i+1] ··· sep..list[j]. The default value for sep is the + * empty string, the default for i is 1, and the default for j is #list. If i + * is greater than j, returns the empty string. + */ + function concat(list: (string | number)[], sep?: string, i?: number, j?: number): string; + + /** + * Executes the given f over all elements of table. For each element, f is + * called with the index and respective value as arguments. If f returns a + * non-nil value, then the loop is broken, and this value is returned as the + * final value of foreach. + * + * See the next function for extra information about table traversals. + */ + function foreach(table: object, f: (index: any, value: any) => any): any; + + /** + * Executes the given f over the numerical indices of table. For each index, + * f is called with the index and respective value as arguments. Indices are + * visited in sequential order, from 1 to n, where n is the size of the + * table (see 5.4). If f returns a non-nil value, then the loop is broken + * and this value is returned as the result of foreachi. + */ + function foreachi(table: object, f: (index: number, value: any) => any): any; + + /** + * Returns the size of a table, when seen as a list. If the table has an n + * field with a numeric value, this value is the size of the table. + * Otherwise, if there was a previous call to table.setn over this table, + * the respective value is returned. Otherwise, the size is one less the + * first integer index with a nil value. + */ + function getn(table: object): number; + + /** + * Inserts element value at position pos in list, shifting up the elements + * list[pos], list[pos+1], ···, list[#list]. The default value for pos is + * #list+1, so that a call table.insert(t,x) inserts x at the end of list t. + */ + function insert(list: T[], value: T): void; + function insert(list: T[], pos: number, value: T): void; + + /** + * Removes from list the element at position pos, returning the value of the + * removed element. When pos is an integer between 1 and #list, it shifts down + * the elements list[pos+1], list[pos+2], ···, list[#list] and erases element + * list[#list]; The index pos can also be 0 when #list is 0, or #list + 1; in + * those cases, the function erases the element list[pos]. + * + * The default value for pos is #list, so that a call table.remove(l) removes + * the last element of list l. + */ + function remove(list: T[], pos?: number): T | undefined; + + /** + * Updates the size of a table. If the table has a field "n" with a + * numerical value, that value is changed to the given n. Otherwise, it + * updates an internal state so that subsequent calls to table.getn(table) + * return n. + */ + function setn(table: object, n: number): void; + + /** + * Sorts list elements in a given order, in-place, from list[1] to + * list[#list]. If comp is given, then it must be a function that receives two + * list elements and returns true when the first element must come before the + * second in the final order (so that, after the sort, i < j implies not + * comp(list[j],list[i])). If comp is not given, then the standard Lua + * operator < is used instead. + * + * Note that the comp function must define a strict partial order over the + * elements in the list; that is, it must be asymmetric and transitive. + * Otherwise, no valid sort may be possible. + * + * The sort algorithm is not stable: elements considered equal by the given + * order may have their relative positions changed by the sort. + */ + function sort(list: T[], comp?: (a: T, b: T) => boolean): void; +} diff --git a/core/debug.d.ts b/core/debug.d.ts index bde7507..59c2a20 100644 --- a/core/debug.d.ts +++ b/core/debug.d.ts @@ -28,6 +28,15 @@ declare namespace debug { */ function debug(): void; + /** + * Returns the current hook settings of the thread, as three values: the + * current hook function, the current hook mask, and the current hook count + * (as set by the debug.sethook function). + */ + function gethook( + thread?: LuaThread + ): LuaMultiReturn<[undefined, 0] | [Function, number, string?]>; + interface FunctionInfo { /** * The function itself. @@ -65,6 +74,51 @@ declare namespace debug { nups: number; } + /** + * Returns a table with information about a function. You can give the + * function directly or you can give a number as the value of f, which means + * the function running at level f of the call stack of the given thread: + * level 0 is the current function (getinfo itself); level 1 is the function + * that called getinfo (except for tail calls, which do not count on the + * stack); and so on. If f is a number larger than the number of active + * functions, then getinfo returns nil. + * + * The returned table can contain all the fields returned by lua_getinfo, with + * the string what describing which fields to fill in. The default for what is + * to get all information available, except the table of valid lines. If + * present, the option 'f' adds a field named func with the function itself. + * If present, the option 'L' adds a field named activelines with the table of + * valid lines. + * + * For instance, the expression debug.getinfo(1,"n").name returns a name for + * the current function, if a reasonable name can be found, and the expression + * debug.getinfo(print) returns a table with all available information about + * the print function. + */ + function getinfo(f: T): FunctionInfo; + function getinfo(f: T, what: string): Partial>; + function getinfo(thread: LuaThread, f: T): FunctionInfo; + function getinfo( + thread: LuaThread, + f: T, + what: string + ): Partial>; + function getinfo(f: number): FunctionInfo | undefined; + function getinfo(f: number, what: string): Partial | undefined; + function getinfo(thread: LuaThread, f: number): FunctionInfo | undefined; + function getinfo(thread: LuaThread, f: number, what: string): Partial | undefined; + + /** + * Returns the metatable of the given value or nil if it does not have a + * metatable. + */ + function getmetatable(value: T): LuaMetatable | undefined; + + /** + * Returns the registry table (see §4.5). + */ + function getregistry(): Record; + /** * This function returns the name and the value of the upvalue with index up * of the function f. The function returns nil if there is no upvalue with the @@ -76,6 +130,80 @@ declare namespace debug { */ function getupvalue(f: Function, up: number): LuaMultiReturn<[string, any] | []>; + /** + * Returns the Lua value associated to u. If u is not a full userdata, returns + * nil. + */ + function getuservalue(u: LuaUserdata): any; + + /** + * Sets the given function as a hook. The string mask and the number count + * describe when the hook will be called. The string mask may have any + * combination of the following characters, with the given meaning: + * + * * 'c': the hook is called every time Lua calls a function; + * * 'r': the hook is called every time Lua returns from a function; + * * 'l': the hook is called every time Lua enters a new line of code. + * + * Moreover, with a count different from zero, the hook is called also after + * every count instructions. + * + * When called without arguments, debug.sethook turns off the hook. + * + * When the hook is called, its first parameter is a string describing the + * event that has triggered its call: "call" (or "tail call"), "return", + * "line", and "count". For line events, the hook also gets the new line + * number as its second parameter. Inside a hook, you can call getinfo with + * level 2 to get more information about the running function (level 0 is the + * getinfo function, and level 1 is the hook function). + */ + function sethook(): void; + function sethook( + hook: (event: 'call' | 'return' | 'line' | 'count', line?: number) => any, + mask: string, + count?: number + ): void; + function sethook( + thread: LuaThread, + hook: (event: 'call' | 'return' | 'line' | 'count', line?: number) => any, + mask: string, + count?: number + ): void; + + /** + * This function assigns the value value to the local variable with index + * local of the function at level level of the stack. The function returns nil + * if there is no local variable with the given index, and raises an error + * when called with a level out of range. (You can call getinfo to check + * whether the level is valid.) Otherwise, it returns the name of the local + * variable. + * + * See debug.getlocal for more information about variable indices and names. + */ + function setlocal(level: number, local: number, value: any): string | undefined; + function setlocal( + thread: LuaThread, + level: number, + local: number, + value: any + ): string | undefined; + + /** + * Sets the metatable for the given value to the given table (which can be + * nil). Returns value. + */ + function setmetatable< + T extends object, + TIndex extends object | ((this: T, key: any) => any) | undefined = undefined + >( + value: T, + table?: LuaMetatable | null + ): TIndex extends (this: T, key: infer TKey) => infer TValue + ? T & { [K in TKey & string]: TValue } + : TIndex extends object + ? T & TIndex + : T; + /** * This function assigns the value value to the upvalue with index up of the * function f. The function returns nil if there is no upvalue with the given @@ -90,4 +218,17 @@ declare namespace debug { * Returns udata. */ function setuservalue(udata: LuaUserdata, value: any): LuaUserdata; + + /** + * If message is present but is neither a string nor nil, this function + * returns message without further processing. Otherwise, it returns a string + * with a traceback of the call stack. The optional message string is appended + * at the beginning of the traceback. An optional level number tells at which + * level to start the traceback (default is 1, the function calling + * traceback). + */ + function traceback(message?: string | null, level?: number | null): string; + function traceback(thread?: LuaThread, message?: string | null, level?: number | null): string; + function traceback(message: T): T; + function traceback(thread: LuaThread, message: T): T; } diff --git a/core/global.d.ts b/core/global.d.ts index df1122f..e8652c1 100644 --- a/core/global.d.ts +++ b/core/global.d.ts @@ -11,7 +11,7 @@ type LuaUserdata = { readonly __internal__: unique symbol }; * A global variable (not a function) that holds a string containing the running * Lua version. */ -declare const _VERSION: 'Lua 5.0' | 'Lua 5.0.1' | 'Lua 5.0.2' | 'Lua 5.0.3' | 'Lua 5.1' | 'Lua 5.2' | 'Lua 5.3' | 'Lua 5.4'; +declare const _VERSION: 'Lua 5.1' | 'Lua 5.2' | 'Lua 5.3' | 'Lua 5.4'; /** * A global variable (not a function) that holds the global environment (see @@ -31,6 +31,61 @@ declare function assert( ...args: A ): LuaMultiReturn<[Exclude, ...A]>; +/** + * This function is a generic interface to the garbage collector. It performs + * different functions according to its first argument, opt. + * + * Performs a full garbage-collection cycle. This is the default option. + */ +declare function collectgarbage(opt?: 'collect'): void; + +/** + * This function is a generic interface to the garbage collector. It performs + * different functions according to its first argument, opt. + * + * Stops automatic execution of the garbage collector. The collector will run + * only when explicitly invoked, until a call to restart it. + */ +declare function collectgarbage(opt: 'stop'): void; + +/** + * This function is a generic interface to the garbage collector. It performs + * different functions according to its first argument, opt. + * + * Restarts automatic execution of the garbage collector. + */ +declare function collectgarbage(opt: 'restart'): void; + +/** + * This function is a generic interface to the garbage collector. It performs + * different functions according to its first argument, opt. + * + * Sets arg as the new value for the pause of the collector (see §2.5). Returns + * the previous value for pause. + */ +declare function collectgarbage(opt: 'setpause', arg: number): number; + +/** + * This function is a generic interface to the garbage collector. It performs + * different functions according to its first argument, opt. + * + * Sets arg as the new value for the step multiplier of the collector (see + * §2.5). Returns the previous value for step. + */ +declare function collectgarbage(opt: 'setstepmul', arg: number): number; + +/** + * This function is a generic interface to the garbage collector. It performs + * different functions according to its first argument, opt. + * + * Performs a garbage-collection step. The step "size" is controlled by arg. + * With a zero value, the collector will perform one basic (indivisible) step. + * For non-zero values, the collector will perform as if that amount of memory + * (in KBytes) had been allocated by Lua. Returns true if the step finished a + * collection cycle. + */ +declare function collectgarbage(opt: 'step', arg: number): boolean; + /** * Opens the named file and executes its contents as a Lua chunk. When called * without arguments, dofile executes the contents of the standard input @@ -149,6 +204,12 @@ declare function rawequal(v1: T, v2: T): boolean; */ declare function rawget(table: T, index: K): T[K]; +/** + * Returns the length of the object v, which must be a table or a string, + * without invoking the __len metamethod. Returns an integer. + */ +declare function rawlen(v: object | string): number; + /** * Sets the real value of table[index] to value, without invoking the __newindex * metamethod. table must be a table, index any value different from nil and @@ -158,6 +219,22 @@ declare function rawget(table: T, index: K) */ declare function rawset(table: T, index: K, value: T[K]): T; +/** + * If index is a number, returns all arguments after argument number index; a + * negative number indexes from the end (-1 is the last argument). Otherwise, + * index must be the string "#", and select returns the total number of extra + * arguments it received. + */ +declare function select(index: number, ...args: T[]): LuaMultiReturn; + +/** + * If index is a number, returns all arguments after argument number index; a + * negative number indexes from the end (-1 is the last argument). Otherwise, + * index must be the string "#", and select returns the total number of extra + * arguments it received. + */ +declare function select(index: '#', ...args: T[]): number; + /** * Sets the metatable for the given table. (To change the metatable of other * types from Lua code, you must use the debug library (§6.10).) If metatable is diff --git a/core/index-5.0.d.ts b/core/index-5.0.d.ts new file mode 100644 index 0000000..4ffe7c8 --- /dev/null +++ b/core/index-5.0.d.ts @@ -0,0 +1,12 @@ +/// + +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// diff --git a/core/io.d.ts b/core/io.d.ts index c996fb2..9c35097 100644 --- a/core/io.d.ts +++ b/core/io.d.ts @@ -91,6 +91,15 @@ declare namespace io { */ function output(file?: string | LuaFile): LuaFile; + /** + * This function is system dependent and is not available on all platforms. + * + * Starts program prog in a separated process and returns a file handle that + * you can use to read data from this program (if mode is "r", the default) or + * to write data to this program (if mode is "w"). + */ + function popen(prog: string, mode?: 'r' | 'w'): LuaMultiReturn<[LuaFile] | [undefined, string]>; + /** * Equivalent to io.input():read(···). */ @@ -231,6 +240,21 @@ interface LuaFile { offset?: number ): LuaMultiReturn<[number] | [undefined, string]>; + /** + * Sets the buffering mode for an output file. There are three available + * modes: + * + * * "no": no buffering; the result of any output operation appears + * immediately. + * * "full": full buffering; output operation is performed only when the + * buffer is full or when you explicitly flush the file (see io.flush). + * * "line": line buffering; output is buffered until a newline is output or + * there is any input from some special files (such as a terminal device). + * For the last two cases, size specifies the size of the buffer, in bytes. + * The default is an appropriate size. + */ + setvbuf(mode: 'no' | 'full' | 'line', size?: number): void; + /** * Writes the value of each of its arguments to file. The arguments must be * strings or numbers. diff --git a/core/math.d.ts b/core/math.d.ts index de01094..794c8b6 100644 --- a/core/math.d.ts +++ b/core/math.d.ts @@ -51,6 +51,17 @@ declare namespace math { */ function floor(x: number): number; + /** + * Returns the remainder of the division of x by y that rounds the quotient + * towards zero. (integer/float) + */ + function fmod(x: number, y: number): number; + + /** + * The float value HUGE_VAL, a value larger than any other numeric value. + */ + const huge: number; + /** * Returns the argument with the maximum value, according to the Lua operator * <. (integer/float) @@ -63,6 +74,12 @@ declare namespace math { */ function min(x: number, ...numbers: number[]): number; + /** + * Returns the integral part of x and the fractional part of x. Its second + * result is always a float. + */ + function modf(x: number): LuaMultiReturn<[number, number]>; + /** * The value of π. */ diff --git a/core/metatable.d.ts b/core/metatable.d.ts index b2479b1..8d4cfab 100644 --- a/core/metatable.d.ts +++ b/core/metatable.d.ts @@ -34,6 +34,11 @@ interface LuaMetatable< */ __div?(this: T, operand: any): any; + /** + * the modulo (%) operation. Behavior similar to the addition operation. + */ + __mod?(this: T, operand: any): any; + /** * the exponentiation (^) operation. Behavior similar to the addition * operation. @@ -53,6 +58,16 @@ interface LuaMetatable< */ __concat?(this: T, operand: any): any; + /** + * the length (#) operation. If the object is not a string, Lua will try its + * metamethod. If there is a metamethod, Lua calls it with the object as + * argument, and the result of the call (always adjusted to one value) is the + * result of the operation. If there is no metamethod but the object is a + * table, then Lua uses the table length operation (see §3.4.7). Otherwise, + * Lua raises an error. + */ + __len?(this: T): any; + /** * the equal (==) operation. Behavior similar to the addition operation, * except that Lua will try a metamethod only when the values being compared diff --git a/core/modules.d.ts b/core/modules.d.ts index 0c29f96..a0bc907 100644 --- a/core/modules.d.ts +++ b/core/modules.d.ts @@ -31,3 +31,111 @@ * loader for the module, then require raises an error. */ declare function require(modname: string): any; + +/** + * The package library provides basic facilities for loading modules in Lua. It + * exports one function directly in the global environment: require. Everything + * else is exported in a table package. + */ +declare namespace package { + /** + * A string describing some compile-time configurations for packages. This + * string is a sequence of lines: + * * The first line is the directory separator string. Default is '\' for + * Windows and '/' for all other systems. + * * The second line is the character that separates templates in a path. + * Default is ';'. + * * The third line is the string that marks the substitution points in a + * template. Default is '?'. + * * The fourth line is a string that, in a path in Windows, is replaced by + * the executable's directory. Default is '!'. + * * The fifth line is a mark to ignore all text after it when building the + * luaopen_ function name. Default is '-'. + */ + var config: string; + + /** + * 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_5_3, or the + * environment variable LUA_CPATH, or a default path defined in luaconf.h. + */ + var 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. + * + * This variable is only a reference to the real table; assignments to this + * variable do not change the table used by require. + */ + const loaded: Record; + + /** + * 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). + */ + function loadlib( + libname: string, + funcname: string + ): [Function] | [undefined, string, 'open' | 'init']; + + /** + * 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_5_3 or the environment variable LUA_PATH or + * with a default path defined in luaconf.h, if those environment variables + * are not defined. Any ";;" in the value of the environment variable is + * replaced by the default path. + */ + var path: string; + + /** + * A table to store loaders for specific modules (see require). + * + * This variable is only a reference to the real table; assignments to this + * variable do not change the table used by require. + */ + const preload: Record any>; + + /** + * 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.) + */ + function searchpath(name: string, path: string, sep?: string, rep?: string): string; +} diff --git a/core/string.d.ts b/core/string.d.ts index df83dfc..0a10e5d 100644 --- a/core/string.d.ts +++ b/core/string.d.ts @@ -92,6 +92,37 @@ declare namespace string { */ function format(formatstring: string, ...args: any[]): string; + /** + * Returns an iterator function that, each time it is called, returns the next + * captures from pattern (see §6.4.1) over the string s. If pattern specifies + * no captures, then the whole match is produced in each call. + * + * As an example, the following loop will iterate over all the words from + * string s, printing one per line: + * + * ``` + * s = "hello world from Lua" + * for w in string.gmatch(s, "%a+") do + * print(w) + * end + * ``` + * + * 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 caret '^' at the start of a pattern does not work as + * an anchor, as this would prevent the iteration. + */ + function gmatch(s: string, pattern: string): LuaIterable>; + /** * Returns a copy of s in which all (or the first n, if given) occurrences of * the pattern (see §6.4.1) have been replaced by a replacement string @@ -139,11 +170,25 @@ declare namespace string { */ function lower(s: string): string; + /** + * Looks for the first match of pattern (see §6.4.1) 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 numeric argument init specifies where to start + * the search; its default value is 1 and can be negative. + */ + function match(s: string, pattern: string, init?: number): LuaMultiReturn; + /** * Returns a string that is the concatenation of `n` copies of the string `s`. */ function rep(s: string, n: number): string; + /** + * Returns a string that is the string s reversed. + */ + function reverse(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 diff --git a/special/5.0-or-5.1-or-jit.d.ts b/special/5.0-or-5.1-or-jit.d.ts deleted file mode 100644 index fb24d8d..0000000 --- a/special/5.0-or-5.1-or-jit.d.ts +++ /dev/null @@ -1,38 +0,0 @@ -/** @noSelfInFile */ - -/** - * 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(f?: Function | number): any; - -/** - * 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(f: T, table: object): T; -declare function setfenv(f: 0, table: object): Function; -declare function setfenv(f: number, table: object): void; - -declare namespace os { - /** - * This function is equivalent to the C function system. It passes command to - * be executed by an operating system shell. It returns a status code, which - * is system-dependent. If command is absent, then it returns nonzero if a - * shell is available and zero otherwise. - */ - function execute(command?: string): number; -} - -declare namespace math { - /** - * Returns the base-10 logarithm of x. - */ - function log10(x: number): number; -} diff --git a/special/5.0-or-5.1.d.ts b/special/5.0-or-5.1.d.ts deleted file mode 100644 index a75b7e2..0000000 --- a/special/5.0-or-5.1.d.ts +++ /dev/null @@ -1,66 +0,0 @@ -/** @noSelfInFile */ - -/** - * Similar to load, but gets the chunk from file filename or from the standard - * input, if no file name is given. - */ -declare function loadfile(filename?: string): LuaMultiReturn<[() => any] | [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. - */ -declare function loadstring( - string: string, - chunkname?: string -): LuaMultiReturn<[() => any] | [undefined, string]>; - -/** - * This function is similar to pcall, except that it sets a new message handler - * msgh. - * - * xpcall calls function f in protected mode, using err as the error handler. - * Any error inside f is not propagated; instead, xpcall catches the error, - * calls the err function with the original error object, and returns a status - * code. Its first result is the status code (a boolean), which is true if the - * call succeeds without errors. In this case, xpcall also returns all results - * from the call, after this first result. In case of any error, xpcall returns - * false plus the result from err. - */ -declare function xpcall( - f: () => R, - err: (err: any) => E -): LuaMultiReturn<[true, R] | [false, E]>; - -declare namespace math { - /** - * Returns the logarithm of x. - */ - function log(x: number): number; -} - -declare namespace string { - /** - * Returns a string that is the concatenation of n copies of the string s. - */ - function rep(s: string, n: number): string; -} - -declare namespace os { - /** - * Calls the C function exit, with an optional code, to terminate the host - * program. The default value for code is the success code. - */ - function exit(code?: number): never; -} - -declare namespace io { - type FileReadNumberFormat = '*n'; - type FileReadLineFormat = '*l'; - type FileReadFormat = FileReadNumberFormat | FileReadLineFormat | '*a' | number; -} diff --git a/special/5.0.d.ts b/special/5.0.d.ts index 666cf03..e4b0df5 100644 --- a/special/5.0.d.ts +++ b/special/5.0.d.ts @@ -1,4 +1,89 @@ -/// -/// -/// -/// +/** @noSelfInFile */ + +/** + * Sets the garbage-collection threshold to the given limit (in Kbytes) and + * checks it against the byte counter. If the new threshold is smaller than the + * byte counter, then Lua immediately runs the garbage collector (see 2.9). If + * limit is absent, it defaults to zero (thus forcing a garbage-collection + * cycle). + */ +declare function collectgarbage(limit: number): void; + +/** + * Similar to load, but gets the chunk from file filename or from the standard + * input, if no file name is given. + */ +declare function loadfile(filename?: string): LuaMultiReturn<[() => any] | [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. + */ +declare function loadstring( + string: string, + chunkname?: string +): LuaMultiReturn<[() => any] | [undefined, string]>; + +/** + * Returns all elements from the given list. This function is equivalent to + * + * `return list[1], list[2], ..., list[n]` + * + * except that the above code can be written only for a fixed n. The number n is + * the size of the list, as defined for the table.getn function. + */ +declare function unpack(list: T): LuaMultiReturn; + +/** + * Returns two results: the number of Kbytes of dynamic memory that Lua is using + * and the current garbage collector threshold (also in Kbytes). + */ +declare function gcinfo(): LuaMultiReturn<[number, number]>; + +/** + * 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(f?: Function | number): any; + +declare namespace io { + type FileReadNumberFormat = '*n'; + type FileReadLineFormat = '*l'; + type FileReadFormat = FileReadNumberFormat | FileReadLineFormat | '*a' | 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(f: T, table: object): T; +declare function setfenv(f: 0, table: object): Function; +declare function setfenv(f: number, table: object): void; + +/** + * This function is similar to pcall, except that it sets a new message handler + * msgh. + * + * xpcall calls function f in protected mode, using err as the error handler. + * Any error inside f is not propagated; instead, xpcall catches the error, + * calls the err function with the original error object, and returns a status + * code. Its first result is the status code (a boolean), which is true if the + * call succeeds without errors. In this case, xpcall also returns all results + * from the call, after this first result. In case of any error, xpcall returns + * false plus the result from err. + */ +declare function xpcall( + f: () => R, + err: (err: any) => E +): LuaMultiReturn<[true, R] | [false, E]>; diff --git a/special/5.1-only.d.ts b/special/5.1-only.d.ts index 4a104ba..aeeadce 100644 --- a/special/5.1-only.d.ts +++ b/special/5.1-only.d.ts @@ -17,6 +17,65 @@ declare function load( chunkname?: string ): LuaMultiReturn<[() => any] | [undefined, string]>; +/** + * Similar to load, but gets the chunk from file filename or from the standard + * input, if no file name is given. + */ +declare function loadfile(filename?: string): LuaMultiReturn<[() => any] | [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. + */ +declare function loadstring( + string: string, + chunkname?: string +): LuaMultiReturn<[() => any] | [undefined, string]>; + +/** + * This function is similar to pcall, except that it sets a new message handler + * msgh. + * + * xpcall calls function f in protected mode, using err as the error handler. + * Any error inside f is not propagated; instead, xpcall catches the error, + * calls the err function with the original error object, and returns a status + * code. Its first result is the status code (a boolean), which is true if the + * call succeeds without errors. In this case, xpcall also returns all results + * from the call, after this first result. In case of any error, xpcall returns + * false plus the result from err. + */ +declare function xpcall( + f: () => R, + err: (err: any) => E +): LuaMultiReturn<[true, R] | [false, E]>; + +declare namespace math { + /** + * Returns the logarithm of x. + */ + function log(x: number): number; +} + +declare namespace string { + /** + * Returns a string that is the concatenation of n copies of the string s. + */ + function rep(s: string, n: number): string; +} + +declare namespace os { + /** + * Calls the C function exit, with an optional code, to terminate the host + * program. The default value for code is the success code. + */ + function exit(code?: number): never; +} + declare namespace debug { /** * This function returns the name and the value of the local variable with @@ -36,3 +95,9 @@ declare namespace debug { local: number ): LuaMultiReturn<[string, any]>; } + +declare namespace io { + type FileReadNumberFormat = '*n'; + type FileReadLineFormat = '*l'; + type FileReadFormat = FileReadNumberFormat | FileReadLineFormat | '*a' | number; +} diff --git a/special/5.1-or-jit.d.ts b/special/5.1-or-jit.d.ts index 597bd18..19fe7cf 100644 --- a/special/5.1-or-jit.d.ts +++ b/special/5.1-or-jit.d.ts @@ -22,6 +22,26 @@ declare function collectgarbage(opt: 'count'): number; declare function unpack(list: T): LuaMultiReturn; declare function unpack(list: T[], i: number, j?: number): LuaMultiReturn; +/** + * 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(f?: Function | number): any; + +/** + * 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(f: T, table: object): T; +declare function setfenv(f: 0, table: object): Function; +declare function setfenv(f: number, table: object): void; + declare namespace debug { /** * Returns the environment of object o. @@ -85,6 +105,23 @@ declare namespace package { )[]; } +declare namespace os { + /** + * This function is equivalent to the C function system. It passes command to + * be executed by an operating system shell. It returns a status code, which + * is system-dependent. If command is absent, then it returns nonzero if a + * shell is available and zero otherwise. + */ + function execute(command?: string): number; +} + +declare namespace math { + /** + * Returns the base-10 logarithm of x. + */ + function log10(x: number): number; +} + declare namespace table { /** * Returns the largest positive numerical index of the given table, or zero if diff --git a/special/5.1-plus-and-5.3-pre.d.ts b/special/5.1-plus-and-5.3-pre.d.ts deleted file mode 100644 index cb6ebeb..0000000 --- a/special/5.1-plus-and-5.3-pre.d.ts +++ /dev/null @@ -1,18 +0,0 @@ -/** @noSelfInFile */ - -declare namespace math { - /** - * Returns the hyperbolic cosine of x. - */ - function cosh(x: number): number; - - /** - * Returns the hyperbolic sine of x. - */ - function sinh(x: number): number; - - /** - * Returns the hyperbolic tangent of x. - */ - function tanh(x: number): number; -} diff --git a/special/5.1-plus-or-jit.d.ts b/special/5.1-plus-or-jit.d.ts deleted file mode 100644 index f0a645a..0000000 --- a/special/5.1-plus-or-jit.d.ts +++ /dev/null @@ -1,456 +0,0 @@ -/** @noSelfInFile */ - -declare namespace debug { - /** - * Returns the current hook settings of the thread, as three values: the - * current hook function, the current hook mask, and the current hook count - * (as set by the debug.sethook function). - */ - function gethook( - thread?: LuaThread - ): LuaMultiReturn<[undefined, 0] | [Function, number, string?]>; - - /** - * Returns a table with information about a function. You can give the - * function directly or you can give a number as the value of f, which means - * the function running at level f of the call stack of the given thread: - * level 0 is the current function (getinfo itself); level 1 is the function - * that called getinfo (except for tail calls, which do not count on the - * stack); and so on. If f is a number larger than the number of active - * functions, then getinfo returns nil. - * - * The returned table can contain all the fields returned by lua_getinfo, with - * the string what describing which fields to fill in. The default for what is - * to get all information available, except the table of valid lines. If - * present, the option 'f' adds a field named func with the function itself. - * If present, the option 'L' adds a field named activelines with the table of - * valid lines. - * - * For instance, the expression debug.getinfo(1,"n").name returns a name for - * the current function, if a reasonable name can be found, and the expression - * debug.getinfo(print) returns a table with all available information about - * the print function. - */ - function getinfo(f: T): FunctionInfo; - function getinfo(f: T, what: string): Partial>; - function getinfo(thread: LuaThread, f: T): FunctionInfo; - function getinfo( - thread: LuaThread, - f: T, - what: string - ): Partial>; - function getinfo(f: number): FunctionInfo | undefined; - function getinfo(f: number, what: string): Partial | undefined; - function getinfo(thread: LuaThread, f: number): FunctionInfo | undefined; - function getinfo(thread: LuaThread, f: number, what: string): Partial | undefined; - - /** - * Returns the metatable of the given value or nil if it does not have a - * metatable. - */ - function getmetatable(value: T): LuaMetatable | undefined; - - /** - * Returns the registry table (see §4.5). - */ - function getregistry(): Record; - - /** - * Returns the Lua value associated to u. If u is not a full userdata, returns - * nil. - */ - function getuservalue(u: LuaUserdata): any; - - /** - * Sets the given function as a hook. The string mask and the number count - * describe when the hook will be called. The string mask may have any - * combination of the following characters, with the given meaning: - * - * * 'c': the hook is called every time Lua calls a function; - * * 'r': the hook is called every time Lua returns from a function; - * * 'l': the hook is called every time Lua enters a new line of code. - * - * Moreover, with a count different from zero, the hook is called also after - * every count instructions. - * - * When called without arguments, debug.sethook turns off the hook. - * - * When the hook is called, its first parameter is a string describing the - * event that has triggered its call: "call" (or "tail call"), "return", - * "line", and "count". For line events, the hook also gets the new line - * number as its second parameter. Inside a hook, you can call getinfo with - * level 2 to get more information about the running function (level 0 is the - * getinfo function, and level 1 is the hook function). - */ - function sethook(): void; - function sethook( - hook: (event: 'call' | 'return' | 'line' | 'count', line?: number) => any, - mask: string, - count?: number - ): void; - function sethook( - thread: LuaThread, - hook: (event: 'call' | 'return' | 'line' | 'count', line?: number) => any, - mask: string, - count?: number - ): void; - - /** - * This function assigns the value value to the local variable with index - * local of the function at level level of the stack. The function returns nil - * if there is no local variable with the given index, and raises an error - * when called with a level out of range. (You can call getinfo to check - * whether the level is valid.) Otherwise, it returns the name of the local - * variable. - * - * See debug.getlocal for more information about variable indices and names. - */ - function setlocal(level: number, local: number, value: any): string | undefined; - function setlocal( - thread: LuaThread, - level: number, - local: number, - value: any - ): string | undefined; - - /** - * Sets the metatable for the given value to the given table (which can be - * nil). Returns value. - */ - function setmetatable< - T extends object, - TIndex extends object | ((this: T, key: any) => any) | undefined = undefined - >( - value: T, - table?: LuaMetatable | null - ): TIndex extends (this: T, key: infer TKey) => infer TValue - ? T & { [K in TKey & string]: TValue } - : TIndex extends object - ? T & TIndex - : T; - - /** - * If message is present but is neither a string nor nil, this function - * returns message without further processing. Otherwise, it returns a string - * with a traceback of the call stack. The optional message string is appended - * at the beginning of the traceback. An optional level number tells at which - * level to start the traceback (default is 1, the function calling - * traceback). - */ - function traceback(message?: string | null, level?: number | null): string; - function traceback(thread?: LuaThread, message?: string | null, level?: number | null): string; - function traceback(message: T): T; - function traceback(thread: LuaThread, message: T): T; -} - -/** - * This function is a generic interface to the garbage collector. It performs - * different functions according to its first argument, opt. - * - * Performs a full garbage-collection cycle. This is the default option. - */ -declare function collectgarbage(opt?: 'collect'): void; - -/** - * This function is a generic interface to the garbage collector. It performs - * different functions according to its first argument, opt. - * - * Stops automatic execution of the garbage collector. The collector will run - * only when explicitly invoked, until a call to restart it. - */ -declare function collectgarbage(opt: 'stop'): void; - -/** - * This function is a generic interface to the garbage collector. It performs - * different functions according to its first argument, opt. - * - * Restarts automatic execution of the garbage collector. - */ -declare function collectgarbage(opt: 'restart'): void; - -/** - * This function is a generic interface to the garbage collector. It performs - * different functions according to its first argument, opt. - * - * Sets arg as the new value for the pause of the collector (see §2.5). Returns - * the previous value for pause. - */ -declare function collectgarbage(opt: 'setpause', arg: number): number; - -/** - * This function is a generic interface to the garbage collector. It performs - * different functions according to its first argument, opt. - * - * Sets arg as the new value for the step multiplier of the collector (see - * §2.5). Returns the previous value for step. - */ -declare function collectgarbage(opt: 'setstepmul', arg: number): number; - -/** - * This function is a generic interface to the garbage collector. It performs - * different functions according to its first argument, opt. - * - * Performs a garbage-collection step. The step "size" is controlled by arg. - * With a zero value, the collector will perform one basic (indivisible) step. - * For non-zero values, the collector will perform as if that amount of memory - * (in KBytes) had been allocated by Lua. Returns true if the step finished a - * collection cycle. - */ -declare function collectgarbage(opt: 'step', arg: number): boolean; - -/** - * Returns the length of the object v, which must be a table or a string, - * without invoking the __len metamethod. Returns an integer. - */ -declare function rawlen(v: object | string): number; - -/** - * If index is a number, returns all arguments after argument number index; a - * negative number indexes from the end (-1 is the last argument). Otherwise, - * index must be the string "#", and select returns the total number of extra - * arguments it received. - */ -declare function select(index: number, ...args: T[]): LuaMultiReturn; - -/** - * If index is a number, returns all arguments after argument number index; a - * negative number indexes from the end (-1 is the last argument). Otherwise, - * index must be the string "#", and select returns the total number of extra - * arguments it received. - */ -declare function select(index: '#', ...args: T[]): number; - -declare namespace io { - /** - * This function is system dependent and is not available on all platforms. - * - * Starts program prog in a separated process and returns a file handle that - * you can use to read data from this program (if mode is "r", the default) - * or to write data to this program (if mode is "w"). - */ - function popen(prog: string, mode?: 'r' | 'w'): LuaMultiReturn<[LuaFile] | [undefined, string]>; -} - -interface LuaFile { - /** - * Sets the buffering mode for an output file. There are three available - * modes: - * - * * "no": no buffering; the result of any output operation appears - * immediately. - * * "full": full buffering; output operation is performed only when the - * buffer is full or when you explicitly flush the file (see io.flush). - * * "line": line buffering; output is buffered until a newline is output or - * there is any input from some special files (such as a terminal device). - * For the last two cases, size specifies the size of the buffer, in bytes. - * The default is an appropriate size. - */ - setvbuf(mode: 'no' | 'full' | 'line', size?: number): void; -} - -declare namespace math { - /** - * Returns the remainder of the division of x by y that rounds the quotient - * towards zero. (integer/float) - */ - function fmod(x: number, y: number): number; - - /** - * The float value HUGE_VAL, a value larger than any other numeric value. - */ - const huge: number; - - /** - * Returns the integral part of x and the fractional part of x. Its second - * result is always a float. - */ - function modf(x: number): LuaMultiReturn<[number, number]>; -} - -interface LuaMetatable< - T, - TIndex extends object | ((this: T, key: any) => any) | undefined = - | object - | ((this: T, key: any) => any) - | undefined -> { - /** - * the modulo (%) operation. Behavior similar to the addition operation. - */ - __mod?(this: T, operand: any): any; - - /** - * the length (#) operation. If the object is not a string, Lua will try its - * metamethod. If there is a metamethod, Lua calls it with the object as - * argument, and the result of the call (always adjusted to one value) is the - * result of the operation. If there is no metamethod but the object is a - * table, then Lua uses the table length operation (see §3.4.7). Otherwise, - * Lua raises an error. - */ - __len?(this: T): any; -} - -/** - * The package library provides basic facilities for loading modules in Lua. It - * exports one function directly in the global environment: require. Everything - * else is exported in a table package. - */ -declare namespace package { - /** - * A string describing some compile-time configurations for packages. This - * string is a sequence of lines: - * * The first line is the directory separator string. Default is '\' for - * Windows and '/' for all other systems. - * * The second line is the character that separates templates in a path. - * Default is ';'. - * * The third line is the string that marks the substitution points in a - * template. Default is '?'. - * * The fourth line is a string that, in a path in Windows, is replaced by - * the executable's directory. Default is '!'. - * * The fifth line is a mark to ignore all text after it when building the - * luaopen_ function name. Default is '-'. - */ - var config: string; - - /** - * 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_5_3, or the - * environment variable LUA_CPATH, or a default path defined in luaconf.h. - */ - var 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. - * - * This variable is only a reference to the real table; assignments to this - * variable do not change the table used by require. - */ - const loaded: Record; - - /** - * 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). - */ - function loadlib( - libname: string, - funcname: string - ): [Function] | [undefined, string, 'open' | 'init']; - - /** - * 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_5_3 or the environment variable LUA_PATH or - * with a default path defined in luaconf.h, if those environment variables - * are not defined. Any ";;" in the value of the environment variable is - * replaced by the default path. - */ - var path: string; - - /** - * A table to store loaders for specific modules (see require). - * - * This variable is only a reference to the real table; assignments to this - * variable do not change the table used by require. - */ - const preload: Record any>; - - /** - * 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.) - */ - function searchpath(name: string, path: string, sep?: string, rep?: string): string; -} - -declare namespace string { - /** - * Returns the internal numeric 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. These - * indices are corrected following the same rules of function string.sub. - * - * Numeric codes are not necessarily portable across platforms. - */ - function byte(s: string, i?: number): number; - function byte(s: string, i?: number, j?: number): LuaMultiReturn; - - /** - * Returns an iterator function that, each time it is called, returns the next - * captures from pattern (see §6.4.1) over the string s. If pattern specifies - * no captures, then the whole match is produced in each call. - * - * As an example, the following loop will iterate over all the words from - * string s, printing one per line: - * - * ``` - * s = "hello world from Lua" - * for w in string.gmatch(s, "%a+") do - * print(w) - * end - * ``` - * - * 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 caret '^' at the start of a pattern does not work as - * an anchor, as this would prevent the iteration. - */ - function gmatch(s: string, pattern: string): LuaIterable>; - - /** - * Looks for the first match of pattern (see §6.4.1) 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 numeric argument init specifies where to start - * the search; its default value is 1 and can be negative. - */ - function match(s: string, pattern: string, init?: number): LuaMultiReturn; - - /** - * Returns a string that is the string s reversed. - */ - function reverse(s: string): string; -} diff --git a/special/5.1.d.ts b/special/5.1.d.ts index 889834c..3d4cac0 100644 --- a/special/5.1.d.ts +++ b/special/5.1.d.ts @@ -1,8 +1,4 @@ -/// -/// /// /// -/// -/// /// /// diff --git a/special/5.2.d.ts b/special/5.2.d.ts index 7e92448..aa82b3e 100644 --- a/special/5.2.d.ts +++ b/special/5.2.d.ts @@ -1,5 +1,3 @@ -/// -/// /// /// /// diff --git a/special/5.3-pre.d.ts b/special/5.3-pre.d.ts index 4111466..82a5aa3 100644 --- a/special/5.3-pre.d.ts +++ b/special/5.3-pre.d.ts @@ -13,6 +13,11 @@ declare namespace math { */ function atan2(y: number, x: number): number; + /** + * Returns the hyperbolic cosine of x. + */ + function cosh(x: number): number; + /** * Returns m and e such that x = m2e, e is an integer and the absolute value * of m is in the range [0.5, 1) (or zero when x is zero). @@ -28,4 +33,14 @@ declare namespace math { * Returns xy. (You can also use the expression x^y to compute this value.) */ function pow(x: number, y: number): number; + + /** + * Returns the hyperbolic sine of x. + */ + function sinh(x: number): number; + + /** + * Returns the hyperbolic tangent of x. + */ + function tanh(x: number): number; } diff --git a/special/5.3.d.ts b/special/5.3.d.ts index 609c68d..e532228 100644 --- a/special/5.3.d.ts +++ b/special/5.3.d.ts @@ -1,4 +1,3 @@ -/// /// /// /// diff --git a/special/5.4.d.ts b/special/5.4.d.ts index 5676d98..a4a8f18 100644 --- a/special/5.4.d.ts +++ b/special/5.4.d.ts @@ -1,4 +1,3 @@ -/// /// /// /// diff --git a/special/jit.d.ts b/special/jit.d.ts index e9e557c..fcc7c40 100644 --- a/special/jit.d.ts +++ b/special/jit.d.ts @@ -1,8 +1,5 @@ -/// /// -/// /// -/// /// /// /// From 11649f4f0d315da8d4e74ee09c0bf72dad684310 Mon Sep 17 00:00:00 2001 From: Ryan Young Date: Sun, 24 Jul 2022 16:19:34 +0000 Subject: [PATCH 4/7] style: run prettier --- core/5.0/debug.d.ts | 2 +- core/5.0/global.d.ts | 2 +- core/5.0/modules.d.ts | 4 ++-- core/5.0/string.d.ts | 12 ++++++------ core/5.0/table.d.ts | 4 ++-- special/5.0.d.ts | 4 ++-- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/core/5.0/debug.d.ts b/core/5.0/debug.d.ts index 321e6ff..19bdcbd 100644 --- a/core/5.0/debug.d.ts +++ b/core/5.0/debug.d.ts @@ -177,7 +177,7 @@ declare namespace debug { /** * Returns a string with a traceback of the call stack. An optional message * string is appended at the beginning of the traceback. This function is - * typically used with xpcall to produce better error messages. + * typically used with xpcall to produce better error messages. */ function traceback(message: T): T; } diff --git a/core/5.0/global.d.ts b/core/5.0/global.d.ts index b7f575d..b65d531 100644 --- a/core/5.0/global.d.ts +++ b/core/5.0/global.d.ts @@ -11,7 +11,7 @@ type LuaUserdata = { readonly __internal__: unique symbol }; * A global variable (not a function) that holds a string containing the running * Lua version. */ -declare const _VERSION: 'Lua 5.0' | 'Lua 5.0.1' | 'Lua 5.0.2' | 'Lua 5.0.3' +declare const _VERSION: 'Lua 5.0' | 'Lua 5.0.1' | 'Lua 5.0.2' | 'Lua 5.0.3'; /** * A global variable (not a function) that holds the global environment (see diff --git a/core/5.0/modules.d.ts b/core/5.0/modules.d.ts index 7783130..ad74a29 100644 --- a/core/5.0/modules.d.ts +++ b/core/5.0/modules.d.ts @@ -5,10 +5,10 @@ /** * Links the program with the dynamic C library libname. Inside this library, * looks for a function funcname and returns this function as a C function. - * + * * libname must be the complete file name of the C library, including any * eventual path and extension. - * + * * This function is not supported by ANSI C. As such, it is only available on * some platforms (Windows, Linux, Solaris, BSD, plus other Unix systems that * support the dlfcn standard). diff --git a/core/5.0/string.d.ts b/core/5.0/string.d.ts index 7db9529..6f2e512 100644 --- a/core/5.0/string.d.ts +++ b/core/5.0/string.d.ts @@ -22,7 +22,7 @@ declare namespace string { * Returns the internal numerical code of the i-th character of s, or nil if * the index is out of range. If i is absent, then it is assumed to be 1. i * may be negative. - * + * * Note that numerical codes are not necessarily portable across platforms. */ function byte(s: string, i?: number): number; @@ -94,23 +94,23 @@ declare namespace string { /** * Returns an iterator function that, each time it is called, returns the * next captures from pattern pat over string s. - * + * * If pat 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.gfind(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" diff --git a/core/5.0/table.d.ts b/core/5.0/table.d.ts index 3b2107d..430a897 100644 --- a/core/5.0/table.d.ts +++ b/core/5.0/table.d.ts @@ -24,8 +24,8 @@ declare namespace table { * called with the index and respective value as arguments. If f returns a * non-nil value, then the loop is broken, and this value is returned as the * final value of foreach. - * - * See the next function for extra information about table traversals. + * + * See the next function for extra information about table traversals. */ function foreach(table: object, f: (index: any, value: any) => any): any; diff --git a/special/5.0.d.ts b/special/5.0.d.ts index e4b0df5..253f3ee 100644 --- a/special/5.0.d.ts +++ b/special/5.0.d.ts @@ -6,7 +6,7 @@ * byte counter, then Lua immediately runs the garbage collector (see 2.9). If * limit is absent, it defaults to zero (thus forcing a garbage-collection * cycle). - */ + */ declare function collectgarbage(limit: number): void; /** @@ -35,7 +35,7 @@ declare function loadstring( * `return list[1], list[2], ..., list[n]` * * except that the above code can be written only for a fixed n. The number n is - * the size of the list, as defined for the table.getn function. + * the size of the list, as defined for the table.getn function. */ declare function unpack(list: T): LuaMultiReturn; From 28e86821705a2f175dfbf908aed3b45cd7875777 Mon Sep 17 00:00:00 2001 From: Ryan Young Date: Sun, 24 Jul 2022 16:20:25 +0000 Subject: [PATCH 5/7] fix: add lua 5.0 versions to version constant so tstl will build --- core/global.d.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/core/global.d.ts b/core/global.d.ts index e8652c1..b46ff18 100644 --- a/core/global.d.ts +++ b/core/global.d.ts @@ -11,7 +11,12 @@ type LuaUserdata = { readonly __internal__: unique symbol }; * A global variable (not a function) that holds a string containing the running * Lua version. */ -declare const _VERSION: 'Lua 5.1' | 'Lua 5.2' | 'Lua 5.3' | 'Lua 5.4'; +declare const _VERSION: + | ('Lua 5.0' | 'Lua 5.0.1' | 'Lua 5.0.2' | 'Lua 5.0.3') + | 'Lua 5.1' + | 'Lua 5.2' + | 'Lua 5.3' + | 'Lua 5.4'; /** * A global variable (not a function) that holds the global environment (see From 706f79bb3aaa6f3e9575dc3140cee5841eb280bb Mon Sep 17 00:00:00 2001 From: Ryan Young Date: Wed, 10 Aug 2022 08:06:16 -0700 Subject: [PATCH 6/7] docs: fix references to 5.0 manual --- core/5.0/debug.d.ts | 2 +- core/5.0/global.d.ts | 2 +- core/5.0/io.d.ts | 2 +- core/5.0/math.d.ts | 2 +- core/5.0/metatable.d.ts | 2 +- core/5.0/modules.d.ts | 2 +- core/5.0/os.d.ts | 2 +- core/5.0/string.d.ts | 2 +- core/5.0/table.d.ts | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/core/5.0/debug.d.ts b/core/5.0/debug.d.ts index 19bdcbd..1c4ef6a 100644 --- a/core/5.0/debug.d.ts +++ b/core/5.0/debug.d.ts @@ -1,4 +1,4 @@ -// Based on https://www.lua.org/manual/5.3/manual.html#6.10 +// Based on https://www.lua.org/manual/5.0/manual.html#5.8 /** @noSelfInFile */ diff --git a/core/5.0/global.d.ts b/core/5.0/global.d.ts index b65d531..8bd3ba6 100644 --- a/core/5.0/global.d.ts +++ b/core/5.0/global.d.ts @@ -1,4 +1,4 @@ -// Based on https://www.lua.org/manual/5.3/manual.html#6.1 +// Based on https://www.lua.org/manual/5.0/manual.html#5.1 /// diff --git a/core/5.0/io.d.ts b/core/5.0/io.d.ts index c996fb2..6109d6f 100644 --- a/core/5.0/io.d.ts +++ b/core/5.0/io.d.ts @@ -1,4 +1,4 @@ -// Based on https://www.lua.org/manual/5.3/manual.html#6.8 +// Based on https://www.lua.org/manual/5.0/manual.html#5.6 /** @noSelfInFile */ diff --git a/core/5.0/math.d.ts b/core/5.0/math.d.ts index 5468a7f..fcbfa1f 100644 --- a/core/5.0/math.d.ts +++ b/core/5.0/math.d.ts @@ -1,4 +1,4 @@ -// Based on https://www.lua.org/manual/5.3/manual.html#6.7 +// Based on https://www.lua.org/manual/5.0/manual.html#5.5 /** @noSelfInFile */ diff --git a/core/5.0/metatable.d.ts b/core/5.0/metatable.d.ts index b2479b1..f7a8a5b 100644 --- a/core/5.0/metatable.d.ts +++ b/core/5.0/metatable.d.ts @@ -1,4 +1,4 @@ -// Based on https://www.lua.org/manual/5.3/manual.html#2.4 +// Based on https://www.lua.org/manual/5.0/manual.html#2.8 interface LuaMetatable< T, diff --git a/core/5.0/modules.d.ts b/core/5.0/modules.d.ts index ad74a29..dfadafa 100644 --- a/core/5.0/modules.d.ts +++ b/core/5.0/modules.d.ts @@ -1,4 +1,4 @@ -// Based on https://www.lua.org/manual/5.3/manual.html#6.3 +// Based on https://www.lua.org/manual/5.0/manual.html#5.1 /** @noSelfInFile */ diff --git a/core/5.0/os.d.ts b/core/5.0/os.d.ts index 6799ad3..3508daa 100644 --- a/core/5.0/os.d.ts +++ b/core/5.0/os.d.ts @@ -1,4 +1,4 @@ -// Based on https://www.lua.org/manual/5.3/manual.html#6.9 +// Based on https://www.lua.org/manual/5.0/manual.html#5.7 /** @noSelfInFile */ diff --git a/core/5.0/string.d.ts b/core/5.0/string.d.ts index 6f2e512..c46a756 100644 --- a/core/5.0/string.d.ts +++ b/core/5.0/string.d.ts @@ -1,4 +1,4 @@ -// Based on https://www.lua.org/manual/5.3/manual.html#6.4 +// Based on https://www.lua.org/manual/5.0/manual.html#5.3 /** @noSelfInFile */ diff --git a/core/5.0/table.d.ts b/core/5.0/table.d.ts index 430a897..e367f1e 100644 --- a/core/5.0/table.d.ts +++ b/core/5.0/table.d.ts @@ -1,4 +1,4 @@ -// Based on https://www.lua.org/manual/5.3/manual.html#6.6 +// Based on https://www.lua.org/manual/5.0/manual.html#5.4 /** @noSelfInFile */ From 5b379fef0be91d8185ab29d47eb5e4eca3f73f07 Mon Sep 17 00:00:00 2001 From: Ryan Young Date: Wed, 10 Aug 2022 08:07:54 -0700 Subject: [PATCH 7/7] style: remove unnecessary iterable reference --- core/5.0/global.d.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/5.0/global.d.ts b/core/5.0/global.d.ts index 8bd3ba6..0b58897 100644 --- a/core/5.0/global.d.ts +++ b/core/5.0/global.d.ts @@ -1,7 +1,5 @@ // Based on https://www.lua.org/manual/5.0/manual.html#5.1 -/// - /** @noSelfInFile */ type LuaThread = { readonly __internal__: unique symbol };