Skip to content

Commit 9047d5e

Browse files
committed
improve standard library types
1 parent 85a4505 commit 9047d5e

File tree

15 files changed

+678
-499
lines changed

15 files changed

+678
-499
lines changed

nattlua/cli/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ function cli.version()
339339
io.write("LuaJIT " .. jit.version .. "\n")
340340
end
341341

342-
local function sorted_pairs(tbl)
342+
local function sorted_pairs(tbl--[[#: AnyTable]])
343343
local keys = {}
344344

345345
for k in pairs(tbl) do
@@ -398,7 +398,7 @@ function cli.help(command)
398398
end
399399
end
400400

401-
local function copy_and_deep_merge(a, b)
401+
local function copy_and_deep_merge(a--[[#: AnyTable]], b--[[#: AnyTable]])--[[#: AnyTable]]
402402
local result = {}
403403

404404
for k, v in pairs(a) do

nattlua/definitions/lua/debug.nlua

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,57 @@
1-
type debug_getinfo = {
2-
name = string,
3-
namewhat = string,
1+
local type DebugHookCallback = function=(event: "call" | "return" | "tail call" | "count" | "line", line: number | nil)>(nil)
2+
local type DebugHookMask = "c" | "r" | "l" | "cr" | "cl" | "rl" | "crl"
3+
local type LineNumber = -1 .. inf
4+
type DebugGetInfo = {
5+
name = string | nil,
6+
namewhat = "global" | "local" | "method" | "field" | "upvalue" | "",
47
source = string,
58
short_src = string,
6-
linedefined = number,
7-
lastlinedefined = number,
8-
what = string,
9-
currentline = number,
9+
linedefined = LineNumber,
10+
lastlinedefined = LineNumber,
11+
what = "Lua" | "C" | "main" | "tail",
12+
currentline = LineNumber,
1013
istailcall = boolean,
11-
nups = number,
12-
nparams = number,
14+
nups = 0 .. inf,
15+
nparams = 0 .. inf,
1316
isvararg = boolean,
14-
func = any,
15-
activelines = {[number] = boolean},
17+
func = AnyFunction | nil,
18+
activelines = {[LineNumber] = boolean} | nil,
1619
}
20+
-- Main debug library interface
1721
type debug = {
18-
sethook = function=(thread: thread, hook: empty_function, mask: string, count: number)>(nil) | function=(thread: thread, hook: empty_function, mask: string)>(nil) | function=(hook: empty_function, mask: string)>(nil),
19-
getregistry = function=()>(nil),
20-
traceback = function=(thread: thread, message: any, level: number)>(string) | function=(thread: thread, message: any)>(string) | function=(thread: thread)>(string) | function=()>(string),
21-
setlocal = function=(thread: thread, level: number, local_: number, value: any)>(string | nil) | function=(level: number, local_: number, value: any)>(string | nil),
22-
getinfo = function=(thread: thread, f: empty_function | number, what: nil | string)>(debug_getinfo | nil) | function=(thread: thread, f: empty_function | number, what: nil | string)>(debug_getinfo | nil) | function=(f: empty_function | number, what: nil | string)>(debug_getinfo | nil),
23-
upvalueid = function=(f: empty_function, n: number)>(userdata),
24-
setupvalue = function=(f: empty_function, up: number, value: any)>(string | nil),
25-
getlocal = function=(thread: thread, f: number | empty_function, local_: number)>(string | nil, any) | function=(f: number | empty_function, local_: number)>(string | nil, any),
26-
upvaluejoin = function=(f1: empty_function, n1: number, f2: empty_function, n2: number)>(nil),
27-
getupvalue = function=(f: empty_function, up: number)>(string | nil, any),
28-
getmetatable = function=(value: any)>(Table | nil),
29-
setmetatable = function=(value: any, Table: Table | nil)>(any),
30-
gethook = function=(thread: thread)>(empty_function, string, number) | function=()>(empty_function, string, number),
31-
getuservalue = function=(u: userdata)>(Table | nil),
3222
debug = function=()>(nil),
33-
getfenv = function=(o: any)>(Table),
34-
setfenv = function=(object: any, Table: Table)>(any),
35-
setuservalue = function=(udata: userdata, value: Table | nil)>(userdata),
23+
gethook = function=()>((DebugHookCallback | nil, DebugHookMask | nil, number | nil)),
24+
sethook = function=(hook: DebugHookCallback | nil, mask: DebugHookMask | nil, count: number | nil)>(nil),
25+
getinfo = function=(f: AnyFunction | 1 .. inf, what: $"^[nSltufL]+$" | nil)>(DebugGetInfo | nil),
26+
getlocal = function=(level: 1 .. inf | Function, local_idx: 1 .. inf)>((string, any) | (nil)),
27+
setlocal = function=(level: 1 .. inf | Function, local_idx: 1 .. inf, value: any)>(string | nil),
28+
getmetatable = function=(value: any)>(AnyTable | nil),
29+
setmetatable = function=(value: any, metatable: AnyTable | nil)>(any),
30+
getregistry = function=()>(AnyTable),
31+
getupvalue = function=(f: AnyFunction, up_idx: 1 .. inf)>((string, any) | (nil)),
32+
setupvalue = function=(f: AnyFunction, up_idx: 1 .. inf, value: any)>(),
33+
upvalueid = function=(f: AnyFunction, n: 1 .. inf)>(userdata),
34+
upvaluejoin = function=(f1: AnyFunction, n1: 1 .. inf, f2: AnyFunction, n2: 1 .. inf)>(nil),
35+
getuservalue = function=(u: userdata)>(any),
36+
setuservalue = function=(udata: userdata, value: any)>(userdata),
37+
traceback = function=(message: any | nil, level: 1 .. inf | nil)>(string),
3638
}
37-
type debug.getfenv = getfenv
38-
type debug.setfenv = setfenv
39+
40+
analyzer function debug.setfenv(val: Function, table: Table)
41+
if val and (val:IsLiteral() or val.Type == "function") then
42+
if val.Type == "number" then
43+
analyzer:SetEnvironmentOverride(analyzer.environment_nodes[val:GetData()], table, "runtime")
44+
elseif val:GetFunctionBodyNode() then
45+
analyzer:SetEnvironmentOverride(val:GetFunctionBodyNode(), table, "runtime")
46+
end
47+
end
48+
end
49+
50+
analyzer function debug.getfenv(func: Function | nil)
51+
if not func then return analyzer:GetDefaultEnvironment("typesystem") end
52+
53+
return analyzer:GetGlobalEnvironmentOverride(func:GetFunctionBodyNode() or func, "runtime")
54+
end
55+
56+
type getfenv = debug.getfenv
57+
type setfenv = debug.setfenv

nattlua/definitions/lua/globals.nlua

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,9 @@
1+
_G.arg = _ as List<|any|>
2+
type _ = any
13
type @Name = "_G"
2-
type setmetatable = function=(table: Table, metatable: Table | nil)>(Table)
3-
type select = function=(index: number | string, ...)>(...)
44
type rawlen = function=(v: Table | string)>(number)
5-
type unpack = function=(list: Table, i: number, j: number)>(...) | function=(list: Table, i: number)>(...) | function=(list: Table)>(...)
6-
type require = function=(modname: string)>(any)
7-
type rawset = function=(table: Table, index: any, value: any)>(Table)
8-
type getmetatable = function=(object: any)>(Table | nil)
9-
type type = function=(v: any)>(string)
10-
type collectgarbage = function=(opt: string, arg: number)>(...) | function=(opt: string)>(...) | function=()>(...)
11-
type getfenv = function=(f: empty_function | number)>(Table) | function=()>(Table)
12-
type pairs = function=(t: Table)>(empty_function, Table, nil)
135
type rawequal = function=(v1: any, v2: any)>(boolean)
14-
type loadfile = function=(filename: string, mode: string, env: Table)>(empty_function | nil, string | nil) | function=(filename: string, mode: string)>(empty_function | nil, string | nil) | function=(filename: string)>(empty_function | nil, string | nil) | function=()>(empty_function | nil, string | nil)
15-
type dofile = function=(filename: string)>(...) | function=()>(...)
16-
type ipairs = function=(t: Table)>(empty_function, Table, number)
17-
type tonumber = function=(e: number | string, base: number | nil)>(number | nil)
18-
type _ = any
19-
_G.arg = _ as List<|any|>
20-
21-
analyzer function setfenv(val: Function, table: Table)
22-
if val and (val:IsLiteral() or val.Type == "function") then
23-
if val.Type == "number" then
24-
analyzer:SetEnvironmentOverride(analyzer.environment_nodes[val:GetData()], table, "runtime")
25-
elseif val:GetFunctionBodyNode() then
26-
analyzer:SetEnvironmentOverride(val:GetFunctionBodyNode(), table, "runtime")
27-
end
28-
end
29-
end
30-
31-
analyzer function getfenv(func: Function | nil)
32-
if not func then return analyzer:GetDefaultEnvironment("typesystem") end
33-
34-
return analyzer:GetGlobalEnvironmentOverride(func:GetFunctionBodyNode() or func, "runtime")
35-
end
6+
type collectgarbage = function=(opt: string, arg: number)>(...) | function=(opt: string)>(...) | function=()>(...)
367

378
analyzer function type_print(...: ...any)
389
print(...)

nattlua/definitions/lua/io.nlua

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type File = {
2626
close = function=(self)>(boolean | nil, string, number | nil),
2727
write = function=(self, ...(number | string))>(self | nil, string | nil),
2828
flush = function=(self)>(boolean | nil, string | nil),
29-
read = function <|self: File, ...: ...any|>
29+
read = function <|self: File, ...: ...$"%**[Lanl].*" | number|>
3030
return check_read_args<|...|>
3131
end,
3232
lines = function=(self)>(Function),
@@ -36,22 +36,11 @@ type File = {
3636
type io.open = function=(string, string | nil)>(nil | File)
3737
type io.popen = function=(string, string | nil)>(nil | File)
3838
type io.output = function=()>(File)
39+
type io.type = function=(File)>("file" | "closed file" | nil)
3940
type io.stdout = File
4041
type io.stdin = File
4142
type io.stderr = File
4243

43-
analyzer function io.type(obj: any)
44-
local flags = types.Union()
45-
flags:AddType(types.ConstString("file"))
46-
flags:AddType(types.ConstString("closed file"))
47-
print(("%p"):format(obj), ("%p"):format(env.typesystem.File))
48-
49-
if false and obj:IsSubsetOf(env.typesystem.File) then return flags end
50-
51-
flags:AddType(types.Nil())
52-
return flags
53-
end
54-
5544
analyzer function io.write(...: ...string)
5645
for i, v in ipairs({...}) do
5746
if not v:IsLiteral() then return end

0 commit comments

Comments
 (0)