@codua/codeoutline-win32-x64 0.2.4 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/README.md +205 -3
  2. package/README.zh-CN.md +40 -2
  3. package/bin/xnet.exe +0 -0
  4. package/build-info.json +1 -1
  5. package/package.json +1 -1
  6. package/scripts/codeoutline/affected.lua +145 -0
  7. package/scripts/codeoutline/automatic_events.lua +202 -0
  8. package/scripts/codeoutline/cache_repairs.lua +47 -0
  9. package/scripts/codeoutline/callers.lua +56 -16
  10. package/scripts/codeoutline/cargo_toml.lua +125 -0
  11. package/scripts/codeoutline/command.lua +26 -4
  12. package/scripts/codeoutline/common.lua +65 -9
  13. package/scripts/codeoutline/completion.lua +221 -65
  14. package/scripts/codeoutline/coverage.lua +63 -0
  15. package/scripts/codeoutline/cpp_bindings.lua +151 -0
  16. package/scripts/codeoutline/cpp_fragments.lua +143 -0
  17. package/scripts/codeoutline/cpp_implementations.lua +340 -0
  18. package/scripts/codeoutline/cpp_includes.lua +156 -0
  19. package/scripts/codeoutline/cpp_macros.lua +279 -0
  20. package/scripts/codeoutline/cpp_receivers.lua +342 -0
  21. package/scripts/codeoutline/cpp_types.lua +456 -0
  22. package/scripts/codeoutline/csharp_bindings.lua +498 -0
  23. package/scripts/codeoutline/definition_candidates.lua +180 -0
  24. package/scripts/codeoutline/dispatch_aliases.lua +479 -0
  25. package/scripts/codeoutline/documents.lua +32 -9
  26. package/scripts/codeoutline/explore.lua +274 -119
  27. package/scripts/codeoutline/external_headers.lua +171 -0
  28. package/scripts/codeoutline/foreign.lua +79 -14
  29. package/scripts/codeoutline/foreign_lua.lua +146 -19
  30. package/scripts/codeoutline/function_values.lua +69 -0
  31. package/scripts/codeoutline/go_methods.lua +931 -0
  32. package/scripts/codeoutline/go_modules.lua +329 -0
  33. package/scripts/codeoutline/graph.lua +346 -63
  34. package/scripts/codeoutline/implementations.lua +403 -0
  35. package/scripts/codeoutline/index.lua +165 -27
  36. package/scripts/codeoutline/index_worker.lua +11 -4
  37. package/scripts/codeoutline/indirect.lua +116 -40
  38. package/scripts/codeoutline/js_evidence.lua +32 -3
  39. package/scripts/codeoutline/js_resolution.lua +72 -10
  40. package/scripts/codeoutline/js_workspace.lua +325 -0
  41. package/scripts/codeoutline/lang/c.lua +15 -2
  42. package/scripts/codeoutline/lang/cpp.lua +7 -4
  43. package/scripts/codeoutline/lang/csharp.lua +15 -3
  44. package/scripts/codeoutline/lang/js.lua +56 -20
  45. package/scripts/codeoutline/lang/rust.lua +9 -1
  46. package/scripts/codeoutline/language_detect.lua +100 -0
  47. package/scripts/codeoutline/lsp.lua +173 -23
  48. package/scripts/codeoutline/lua_bindings.lua +228 -54
  49. package/scripts/codeoutline/lua_callback_loops.lua +210 -0
  50. package/scripts/codeoutline/lua_literal.lua +38 -0
  51. package/scripts/codeoutline/main.lua +32 -7
  52. package/scripts/codeoutline/mcp.lua +52 -9
  53. package/scripts/codeoutline/navigation.lua +593 -147
  54. package/scripts/codeoutline/on_demand.lua +93 -0
  55. package/scripts/codeoutline/ordinary_bindings.lua +396 -0
  56. package/scripts/codeoutline/parse.lua +18 -2
  57. package/scripts/codeoutline/parse_pool.lua +1 -1
  58. package/scripts/codeoutline/parse_worker.lua +1 -1
  59. package/scripts/codeoutline/python_bindings.lua +112 -0
  60. package/scripts/codeoutline/query_filters.lua +65 -0
  61. package/scripts/codeoutline/query_report.lua +135 -8
  62. package/scripts/codeoutline/resolution.lua +23 -0
  63. package/scripts/codeoutline/rules.lua +127 -0
  64. package/scripts/codeoutline/rust_packages.lua +203 -0
  65. package/scripts/codeoutline/rust_resolution.lua +301 -0
  66. package/scripts/codeoutline/search_ranking.lua +70 -0
  67. package/scripts/codeoutline/service.lua +47 -7
  68. package/scripts/codeoutline/shared_events.lua +369 -0
  69. package/scripts/codeoutline/static_dispatch.lua +569 -0
  70. package/scripts/codeoutline/text.lua +3 -2
  71. package/scripts/codeoutline/ts_implementations.lua +81 -0
  72. package/scripts/codeoutline/type_bases.lua +61 -0
  73. package/scripts/codeoutline/type_hierarchy.lua +324 -0
  74. package/scripts/codeoutline/type_references.lua +290 -0
  75. package/scripts/codeoutline/type_uses.lua +260 -0
  76. package/scripts/codeoutline/update_sequence.lua +1 -1
  77. package/scripts/codeoutline/version.lua +1 -1
@@ -0,0 +1,171 @@
1
+ -- Declaration navigation in selected external headers. Never extend the index.
2
+ local M = { MAX_FILES = 64, MAX_PROJECT_FILES = 128, MAX_BYTES = 4 * 1024 * 1024,
3
+ MAX_FILE_BYTES = 1024 * 1024, CACHE_FILES = 32, CACHE_BYTES = 8 * 1024 * 1024, TIME_MS = 500 }
4
+ local paths, control = require('codeoutline.path'), require('codeoutline.control')
5
+ local cache, order, cache_bytes, scanner = {}, {}, 0, nil
6
+ local function directory(file) return file:match('^(.*)/[^/]+$') end
7
+ local function existing(file)
8
+ control.check()
9
+ local stat = xutils.stat(file)
10
+ if stat and stat.type == 'file' then
11
+ local real = xutils.realpath(file)
12
+ return real and paths.normalize(real)
13
+ end
14
+ end
15
+ local function read(file, info)
16
+ local stream = io.open(file, 'rb')
17
+ if not stream then return nil end
18
+ local raw = stream:read(M.MAX_FILE_BYTES + 1) or ''; stream:close()
19
+ info.files, info.bytes = info.files + 1, info.bytes + #raw
20
+ if #raw > M.MAX_FILE_BYTES or info.bytes > M.MAX_BYTES then info.truncated = true; return nil end
21
+ local crc = xcompress and xcompress.crc32 and xcompress.crc32(raw) or xutils.sha256_hex(raw)
22
+ local key, entry = paths.key(file), cache[paths.key(file)]
23
+ if entry and entry.crc == crc and entry.size == #raw then return entry end
24
+ if entry then
25
+ cache_bytes = cache_bytes - entry.weight; cache[key] = nil
26
+ for i, old in ipairs(order) do if old == key then table.remove(order, i); break end end
27
+ end
28
+ local source = xutils.to_utf8(raw)
29
+ if not source then return nil end
30
+ if source:sub(1, 3) == '\239\187\191' then source = source:sub(4) end
31
+ if not scanner then
32
+ local config = {}; for k, v in pairs(require('codeoutline.lang.c').config) do config[k] = v end
33
+ config.pp_first_branch = false
34
+ scanner = require('codeoutline.xscan').lang(config)
35
+ end
36
+ local T = scanner:tokenize(source)
37
+ local imports = {}
38
+ for i = 1, T.n do
39
+ if i % 4096 == 0 then control.check() end
40
+ if T.k[i] == 'dir' then
41
+ local text = T:text(i)
42
+ local name = text:match('^#%s*include%s*[<"]([^>"]+)[>"]')
43
+ if name then imports[#imports + 1] = { path = name, system = text:match('^#%s*include%s*<') ~= nil } end
44
+ end
45
+ end
46
+ entry = { file = file, source = source, imports = imports, crc = crc, size = #raw,
47
+ weight = #source * 2 + #imports * 128 + 256 }
48
+ while #order > 0 and (#order >= M.CACHE_FILES or cache_bytes + entry.weight > M.CACHE_BYTES) do
49
+ local old = table.remove(order, 1); cache_bytes = cache_bytes - cache[old].weight; cache[old] = nil
50
+ end
51
+ if entry.weight <= M.CACHE_BYTES then
52
+ cache[key] = entry; order[#order + 1] = key; cache_bytes = cache_bytes + entry.weight
53
+ end
54
+ return entry
55
+ end
56
+ local function declarations(entry, name)
57
+ local escaped = name:gsub('([^%w])', '%%%1')
58
+ if not entry.source:find('%f[%w_$]' .. escaped .. '%f[^%w_$]') then return {} end
59
+ if not entry.nodes then
60
+ local rec = require('codeoutline.parse').parse(entry.file, entry.source, { type_uses = false, analysis = false })
61
+ entry.nodes = rec and rec.nodes or {}
62
+ local weight = #entry.nodes * 256
63
+ if cache[paths.key(entry.file)] == entry then
64
+ cache_bytes, entry.weight = cache_bytes + weight, entry.weight + weight
65
+ while #order > 0 and cache_bytes > M.CACHE_BYTES do
66
+ local old = table.remove(order, 1); cache_bytes = cache_bytes - cache[old].weight; cache[old] = nil
67
+ end
68
+ end
69
+ end
70
+ local result = {}
71
+ for _, node in ipairs(entry.nodes) do
72
+ if node.name == name and node.qualified == name and not node.parent and node.name_start
73
+ and (node.kind == 'prototype' or node.kind == 'function') then
74
+ result[#result + 1] = { node = node, file = entry.file, source = entry.source,
75
+ declaration = node.kind == 'prototype' or node.decl == true }
76
+ end
77
+ end
78
+ return result
79
+ end
80
+
81
+ function M.find(G, f, site, ctx, system_directories)
82
+ local info = { files = 0, bytes = 0, truncated = false }
83
+ if not site or site.recv or site.kind ~= 'call' or not require('codeoutline.definition_candidates').allowed(site) then return {}, info end
84
+ local includes = require('codeoutline.cpp_includes')
85
+ local headers, queued, projects, visited, project_queued = {}, {}, { f }, {}, { [f] = true }
86
+ local defaults, started = nil, xtimer.now_ms()
87
+ local function expired()
88
+ control.check()
89
+ if xtimer.now_ms() - started >= M.TIME_MS then info.truncated = true; return true end
90
+ return false
91
+ end
92
+ local function dirs(owner)
93
+ local policy = includes.policy(G, owner, ctx)
94
+ local result = {}
95
+ local function add_root(dir)
96
+ control.check()
97
+ local real = xutils.realpath(dir)
98
+ if real then result[#result + 1] = paths.normalize(real) end
99
+ end
100
+ for _, dir in ipairs(policy.paths) do add_root(dir) end
101
+ if not policy.configured then
102
+ defaults = defaults or (type(system_directories) == 'function' and system_directories() or system_directories or {})
103
+ for _, dir in ipairs(defaults) do add_root(dir) end
104
+ end
105
+ return result
106
+ end
107
+ local function add(file, roots)
108
+ if file and not paths.contains(G.root, file) and not queued[paths.key(file)] then
109
+ queued[paths.key(file)] = true; headers[#headers + 1] = { file = file, roots = roots }
110
+ end
111
+ end
112
+ local function locate(imp, roots, parent)
113
+ local written = imp.path:gsub('\\', '/')
114
+ if written:find('[%z\r\n]') or written:match('^[/\\]') or written:match('^%a:') then return end
115
+ if not imp.system and parent then
116
+ local file = existing(directory(parent) .. '/' .. written)
117
+ if file then
118
+ for _, root in ipairs(roots) do if paths.contains(root, file) then return file end end
119
+ end
120
+ end
121
+ for _, root in ipairs(roots) do
122
+ local file = existing(root .. '/' .. written)
123
+ if file and paths.contains(root, file) then return file end
124
+ end
125
+ end
126
+ local pi = 1
127
+ while pi <= #projects do
128
+ if expired() then return {}, info end
129
+ local owner = projects[pi]; pi = pi + 1
130
+ if not visited[owner] then
131
+ if pi > M.MAX_PROJECT_FILES + 1 then info.truncated = true; break end
132
+ visited[owner] = true
133
+ for _, imp in ipairs(G.files[owner].imports or {}) do
134
+ if owner ~= f or not site.line or imp.line <= site.line then
135
+ local ids, selected, file = includes.resolve(G, owner, imp, ctx)
136
+ for _, id in ipairs(ids) do
137
+ if not project_queued[id] then
138
+ if #projects >= M.MAX_PROJECT_FILES then info.truncated = true
139
+ else project_queued[id] = true; projects[#projects + 1] = id end
140
+ end
141
+ end
142
+ if #ids == 0 then
143
+ local roots = dirs(owner)
144
+ if file then
145
+ file = existing(file)
146
+ if file and not paths.contains(G.root, file) then
147
+ roots[#roots + 1] = directory(file); add(file, roots)
148
+ end
149
+ elseif not selected then add(locate(imp, roots), roots) end
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
155
+ local best, hi = {}, 1
156
+ while hi <= #headers do
157
+ if info.files >= M.MAX_FILES or expired() then info.truncated = true; break end
158
+ local target = headers[hi]; hi = hi + 1
159
+ local entry = read(target.file, info)
160
+ if entry then
161
+ local found = declarations(entry, site.name)
162
+ for _, hit in ipairs(found) do
163
+ if hit.declaration then return { hit }, info end
164
+ if #best < 8 then best[#best + 1] = hit end
165
+ end
166
+ for _, imp in ipairs(entry.imports) do add(locate(imp, target.roots, target.file), target.roots) end
167
+ end
168
+ end
169
+ return best, info
170
+ end
171
+ return M
@@ -1,6 +1,58 @@
1
1
  -- Language-neutral native exports. Adapters supply registration evidence and
2
2
  -- guest parameters; implementation nodes retain their original host signature.
3
3
  local M = {}
4
+ -- Side caches never mutate index records or nodes. Entries follow record identity
5
+ -- (including overlays) and share navigation's bounded full-parse cache.
6
+ local records, contracts, order = setmetatable({}, { __mode = 'k' }), setmetatable({}, { __mode = 'k' }), {}
7
+ local function load(G, f)
8
+ local rec = G.files[f]
9
+ if records[rec] ~= nil then return records[rec] or nil end
10
+ local full = require('codeoutline.navigation').full(G, f, true)
11
+ if not full then return nil end
12
+ -- Node indices are stable only while the source still matches the index.
13
+ for n, node in ipairs(rec.nodes) do
14
+ local current = full.nodes[n]
15
+ if not current or current.name ~= node.name or current.line ~= node.line then return nil end
16
+ end
17
+ if #full.nodes ~= #rec.nodes then return nil end
18
+ local entry = full.foreign_exports or false
19
+ records[rec] = entry
20
+ if entry then
21
+ for n, node in ipairs(rec.nodes) do contracts[node] = full.nodes[n].foreign_signatures end
22
+ end
23
+ order[#order + 1] = rec
24
+ if #order > 64 then
25
+ local old = table.remove(order, 1)
26
+ records[old] = nil
27
+ for _, node in ipairs(old.nodes) do contracts[node] = nil end
28
+ end
29
+ return entry or nil
30
+ end
31
+
32
+ -- Find candidate files from existing symbols without reading their sources.
33
+ local function candidates(G, ctx)
34
+ if ctx.foreign_candidates then return ctx.foreign_candidates end
35
+ local result, seen = {}, {}
36
+ local function add(f)
37
+ if seen[f] then return end
38
+ seen[f] = true
39
+ local rec = G.files[f]
40
+ if rec.language ~= 'c' and rec.language ~= 'cpp' then return end
41
+ for _, node in ipairs(rec.nodes) do
42
+ local name = node.name:match('^luaopen_(.+)$')
43
+ if name then
44
+ result[name] = result[name] or {}
45
+ result[name][#result[name] + 1] = f
46
+ end
47
+ end
48
+ end
49
+ for _, f in ipairs(G.file_order or {}) do add(f) end
50
+ for _, f in ipairs(G.new_files or {}) do add(f) end
51
+ ctx.foreign_candidates = result
52
+ return result
53
+ end
54
+
55
+ function M.has_candidates(G, ctx) return next(candidates(G, ctx)) ~= nil end
4
56
 
5
57
  function M.register(rec, language, module, exports)
6
58
  rec.foreign_exports = rec.foreign_exports or {}
@@ -9,7 +61,8 @@ function M.register(rec, language, module, exports)
9
61
  end
10
62
 
11
63
  function M.exports(rec, language, module)
12
- return rec.foreign_exports and rec.foreign_exports[language] and rec.foreign_exports[language][module]
64
+ local exports = records[rec] or rec.foreign_exports
65
+ return exports and exports[language] and exports[language][module]
13
66
  end
14
67
 
15
68
  function M.collect(rec, T)
@@ -18,30 +71,34 @@ end
18
71
 
19
72
  function M.modules(G, language, name, ctx)
20
73
  ctx = ctx or {}
21
- if not ctx.foreign then
22
- ctx.foreign = {}
23
- local seen = {}
24
- local function add(f)
25
- if seen[f] then return end
26
- seen[f] = true
27
- for lang, modules in pairs(G.files[f].foreign_exports or {}) do
74
+ ctx.foreign = ctx.foreign or {}
75
+ ctx.foreign_loaded = ctx.foreign_loaded or {}
76
+ for _, f in ipairs(candidates(G, ctx)[name] or {}) do
77
+ if not ctx.foreign_loaded[f] then
78
+ local exports = load(G, f)
79
+ -- Failed reads can be skipped within this query, but are never
80
+ -- retained on the record across later query contexts.
81
+ ctx.foreign_loaded[f] = true
82
+ for lang, modules in pairs(exports or {}) do
28
83
  ctx.foreign[lang] = ctx.foreign[lang] or {}
29
84
  for module in pairs(modules) do
30
85
  local names = ctx.foreign[lang]
31
86
  names[module] = names[module] or {}
32
- table.insert(names[module], f)
87
+ names[module][#names[module] + 1] = f
33
88
  end
34
89
  end
35
90
  end
36
- for _, f in ipairs(G.file_order or {}) do add(f) end
37
- for _, f in ipairs(G.new_files or {}) do add(f) end
38
91
  end
39
- return (ctx.foreign[language] or {})[name] or {}
92
+ local files = (ctx.foreign[language] or {})[name] or {}
93
+ -- A long query may have evicted a previously used record.
94
+ for _, f in ipairs(files) do load(G, f) end
95
+ return files
40
96
  end
41
97
 
42
98
  -- Parameter labels are structured data, never reparsed as host-language code.
43
99
  function M.signature(node, language, name)
44
- local contract = node.foreign_signatures and node.foreign_signatures[language]
100
+ local signatures = contracts[node] or node.foreign_signatures
101
+ local contract = signatures and signatures[language]
45
102
  if not contract then return nil end
46
103
  local labels, parameters = {}, {}
47
104
  for i, p in ipairs(contract.parameters) do
@@ -54,7 +111,15 @@ function M.signature(node, language, name)
54
111
  labels[#labels + 1] = '...:unknown'
55
112
  parameters[#parameters + 1] = { label = '...:unknown' }
56
113
  end
57
- return { label = (name or node.name) .. '(' .. table.concat(labels, ', ') .. ')', parameters = parameters }
114
+ return { label = (name or node.name) .. '(' .. table.concat(labels, ', ') .. ')'
115
+ .. (contract.returns and ' -> ' .. contract.returns or ''), parameters = parameters }
116
+ end
117
+
118
+ -- Guest-facing result type: the native metatable the implementation returns.
119
+ function M.returns(node, language)
120
+ local signatures = contracts[node] or node.foreign_signatures
121
+ local contract = signatures and signatures[language]
122
+ return contract and contract.returns or nil
58
123
  end
59
124
 
60
125
  return M
@@ -21,7 +21,8 @@ local TYPES = { LUA_TNUMBER = 'number', LUA_TSTRING = 'string', LUA_TBOOLEAN = '
21
21
  LUA_TTABLE = 'table', LUA_TFUNCTION = 'function', LUA_TTHREAD = 'thread',
22
22
  LUA_TUSERDATA = 'userdata', LUA_TLIGHTUSERDATA = 'lightuserdata', LUA_TNIL = 'nil' }
23
23
 
24
- local function parameters(T, node)
24
+ -- offset: leading C arguments the guest never passes (a method's self).
25
+ local function parameters(T, node, macros, offset)
25
26
  local state
26
27
  for i = node.ti, node.tj - 2 do
27
28
  if T:text(i) == 'lua_State' and T:text(i + 1) == '*' and T.k[i + 2] == 'id' then
@@ -42,7 +43,9 @@ local function parameters(T, node)
42
43
  slots[position] = p
43
44
  local kind = READ[api]
44
45
  if api == 'luaL_checktype' then kind = TYPES[T:text(i + 6)] or 'unknown' end
45
- if api == 'luaL_checkudata' or api == 'luaL_testudata' then kind = literal(T, i + 6) or kind end
46
+ if api == 'luaL_checkudata' or api == 'luaL_testudata' then
47
+ kind = literal(T, i + 6) or (macros and T.k[i + 6] == 'id' and macros[T:text(i + 6)]) or kind
48
+ end
46
49
  p.types[kind] = true
47
50
  local optional = api:match('^luaL_opt') ~= nil
48
51
  if p.optional == nil then p.optional = optional else p.optional = p.optional and optional end
@@ -77,7 +80,8 @@ local function parameters(T, node)
77
80
  end
78
81
  end
79
82
  local result, used = { parameters = {}, unknown_tail = unknown }, {}
80
- for i = 1, highest do
83
+ offset = offset or 0
84
+ for i = 1 + offset, highest do
81
85
  local p = slots[i] or { types = {} }
82
86
  p.types.unknown = nil
83
87
  local types = {}
@@ -86,15 +90,110 @@ local function parameters(T, node)
86
90
  local name = not p.ambiguous and p.name or nil
87
91
  if name and used[name] then name = nil end
88
92
  if name then used[name] = true end
89
- result.parameters[i] = { name = name, type = #types > 0 and table.concat(types, '|') or 'unknown',
93
+ result.parameters[i - offset] = { name = name, type = #types > 0 and table.concat(types, '|') or 'unknown',
90
94
  optional = p.optional or nil, default = p.optional and p.default or nil }
91
95
  end
92
96
  return result
93
97
  end
94
98
 
95
- -- Only explicit registration tables wired inside luaopen_* are exports.
96
- -- Keep the original C nodes, including their source ranges and signatures.
99
+ -- `#define NAME "value"`: metatable names are usually macros, not literals.
100
+ local function string_macros(T)
101
+ local map = {}
102
+ for i = 1, T.n do
103
+ if T.k[i] == 'dir' then
104
+ local name, value = T:text(i):match('^#%s*define%s+([%a_][%w_]*)%s+"([^"]*)"%s*$')
105
+ if name then map[name] = value end
106
+ end
107
+ end
108
+ return map
109
+ end
110
+
111
+ -- Metatable name at argument token i.
112
+ local function meta_name(T, i, macros)
113
+ if T.k[i] == 'str' then return literal(T, i) end
114
+ if T.k[i] == 'id' then return macros[T:text(i)] end
115
+ end
116
+
117
+ -- Recognize constructor tails with a small symbolic stack, not C dataflow.
118
+ -- Unknown stack mutations invalidate the tail. All normal returns must agree;
119
+ -- luaL_error paths do not return to the guest. Void push helpers are supported.
120
+ local function returned_meta(T, r, functions, macros, cache, index, depth)
121
+ local node = r.nodes[index]
122
+ if not node or not node.ti then return nil end
123
+ depth = depth or 6
124
+ if depth <= 0 then return nil end
125
+ if cache[index] ~= nil then return cache[index] or nil end
126
+ cache[index] = false
127
+ local stack, result, returns, invalid = {}, nil, 0, false
128
+ local function accept(meta)
129
+ returns = returns + 1
130
+ if not meta or result and result ~= meta then invalid = true end
131
+ result = result or meta
132
+ end
133
+ local i, block_depth = node.ti, 0
134
+ while i <= node.tj do
135
+ if i % 4096 == 0 then control.check() end
136
+ local word = T:text(i)
137
+ if word == '{' then block_depth = block_depth + 1 end
138
+ if word == '}' then block_depth = block_depth - 1 end
139
+ if word == 'return' then
140
+ local value = T:text(i + 1)
141
+ if value == '1' and T:text(i + 2) == ';' then
142
+ accept(stack[#stack])
143
+ elseif value == 'luaL_error' then
144
+ -- This API raises, so it is not a competing normal return.
145
+ elseif functions[value] and T:text(i + 2) == '(' and T.m[i + 2]
146
+ and T:text(T.m[i + 2] + 1) == ';' then
147
+ accept(returned_meta(T, r, functions, macros, cache, functions[value], depth - 1))
148
+ else accept(nil) end
149
+ elseif T:text(i + 1) == '(' and T.m[i + 1] and i > node.ti
150
+ and T:text(i - 1) ~= 'return' then
151
+ local close = T.m[i + 1]
152
+ local conditional = block_depth > 1 or T:text(i - 1) == ')' and T.m[i - 1]
153
+ and T:text(T.m[i - 1] - 1) == 'if'
154
+ if word == 'lua_newuserdata' or word == 'lua_newuserdatauv' then
155
+ stack[#stack + 1] = false
156
+ elseif word == 'luaL_setmetatable' then
157
+ if conditional then invalid = true end
158
+ if #stack > 0 then stack[#stack] = meta_name(T, i + 4, macros) or false end
159
+ elseif word == 'lua_pushvalue' then
160
+ stack[#stack + 1] = false
161
+ elseif word == 'luaL_ref' then
162
+ stack[#stack] = nil
163
+ elseif word == 'lua_pop' or word == 'lua_settop' or word == 'lua_remove'
164
+ or word == 'lua_replace' or word == 'lua_insert' or word == 'lua_rotate' then
165
+ stack = {}
166
+ elseif word:match('^lua_push') or word:match('^lua_get') and word ~= 'lua_gettop'
167
+ or word == 'lua_newtable' or word == 'lua_createtable' then
168
+ stack[#stack + 1] = false
169
+ elseif functions[word] and functions[word] ~= index then
170
+ local meta = returned_meta(T, r, functions, macros, cache, functions[word], depth - 1)
171
+ if meta then
172
+ if conditional then invalid = true end
173
+ stack[#stack + 1] = meta
174
+ elseif #stack > 0 then stack = {} end
175
+ elseif word:match('^lua_') and word ~= 'lua_gettop'
176
+ and not word:match('^lua_to') and not word:match('^lua_is')
177
+ and word ~= 'lua_type' then
178
+ stack = {}
179
+ end
180
+ -- Arguments are not independent statements or constructor results.
181
+ i = close
182
+ end
183
+ i = i + 1
184
+ end
185
+ if returns == 0 and (node.sig or ''):match('^%s*static%s+void%s') then result = stack[#stack] end
186
+ if invalid then result = nil end
187
+ cache[index] = result or false
188
+ return result
189
+ end
190
+
191
+ -- Only explicit registration tables wired inside luaopen_* are exports. A
192
+ -- metatable's methods are exported under the metatable name, which is the type
193
+ -- guests see, so `handle:get(...)` completes and tips without hand-written
194
+ -- metadata. Keep the original C nodes, ranges and signatures.
97
195
  function M.collect(r, T)
196
+ local macros = string_macros(T)
98
197
  local tables, functions = {}, {}
99
198
  for n, node in ipairs(r.nodes) do
100
199
  if node.kind == 'function' then functions[node.name] = n end
@@ -116,28 +215,56 @@ function M.collect(r, T)
116
215
  end
117
216
  end
118
217
  end
218
+ local cache = {}
219
+ local function signature(n, method_of)
220
+ local target = r.nodes[n]
221
+ target.foreign_signatures = target.foreign_signatures or {}
222
+ if target.foreign_signatures.lua then return end
223
+ local contract = parameters(T, target, macros, method_of and 1 or 0)
224
+ contract.returns = returned_meta(T, r, functions, macros, cache, n)
225
+ target.foreign_signatures.lua = contract
226
+ end
119
227
  for _, node in ipairs(r.nodes) do
120
228
  local module = node.kind == 'function' and node.name:match('^luaopen_(.+)$')
121
229
  if module then
122
- local exports = {}
230
+ local exports, metas, meta, discard = {}, {}, nil, {}
123
231
  for i = node.ti, node.tj do
124
232
  local call = T:text(i)
125
- -- A fresh table replaces registrations for earlier metatables.
126
- if T:text(i + 1) == '(' and (call == 'luaL_newlib' or call == 'lua_newtable'
127
- or call == 'lua_createtable' or call == 'luaL_newmetatable') then exports = {} end
128
- if T:text(i + 1) == '(' and T:text(i + 3) == ',' then
129
- local arg = (call == 'luaL_newlib' or call == 'luaL_setfuncs') and i + 4
130
- if call == 'luaL_register' and T:text(i + 5) == ',' then arg = i + 6 end
131
- for name, n in pairs(tables[arg and T:text(arg)] or {}) do exports[name] = n end
233
+ if T:text(i + 1) == '(' then
234
+ local into
235
+ if call == 'luaL_newmetatable' then
236
+ meta = meta_name(T, i + 4, macros)
237
+ if meta then metas[meta] = metas[meta] or {} end
238
+ into = meta and metas[meta] or discard
239
+ else
240
+ -- `lua_pop` finishes a metatable; a fresh library table
241
+ -- starts the module. A plain table inside a metatable
242
+ -- block is that metatable's `__index`, not the module.
243
+ if call == 'luaL_newlib' then
244
+ meta, exports = nil, {}
245
+ elseif call == 'lua_pop' then
246
+ meta = nil
247
+ elseif not meta and (call == 'lua_newtable' or call == 'lua_createtable') then
248
+ exports = {}
249
+ end
250
+ into = meta and metas[meta] or exports
251
+ end
252
+ if T:text(i + 3) == ',' then
253
+ local arg = (call == 'luaL_newlib' or call == 'luaL_setfuncs') and i + 4
254
+ if call == 'luaL_register' and T:text(i + 5) == ',' then arg = i + 6 end
255
+ for name, n in pairs(tables[arg and T:text(arg)] or {}) do into[name] = n end
256
+ end
257
+ end
258
+ end
259
+ for name, entries in pairs(metas) do
260
+ if next(entries) then
261
+ require('codeoutline.foreign').register(r, 'lua', name, entries)
262
+ for _, n in pairs(entries) do signature(n, name) end
132
263
  end
133
264
  end
134
265
  if next(exports) then
135
266
  require('codeoutline.foreign').register(r, 'lua', module, exports)
136
- for _, n in pairs(exports) do
137
- local target = r.nodes[n]
138
- target.foreign_signatures = target.foreign_signatures or {}
139
- target.foreign_signatures.lua = target.foreign_signatures.lua or parameters(T, target)
140
- end
267
+ for _, n in pairs(exports) do signature(n, nil) end
141
268
  end
142
269
  end
143
270
  end
@@ -0,0 +1,69 @@
1
+ -- Compact function-as-value sites. Resolution reuses query-owned bindings;
2
+ -- these are references, never proof of a callback being executed.
3
+ local M = {}
4
+ local supported = { javascript = true, typescript = true, arkts = true, python = true, java = true }
5
+ local callable = { ['function'] = true, method = true }
6
+ local before = { ['('] = true, [','] = true, ['='] = true, ['['] = true, [':'] = true }
7
+ local after = { [')'] = true, [','] = true, [';'] = true, [']'] = true, ['}'] = true }
8
+
9
+ function M.collect(r, T)
10
+ if not supported[r.language] then return end
11
+ -- The ordinary parser does not represent every anonymous scope. Do not
12
+ -- borrow outer binding evidence where an unindexed parameter can shadow it.
13
+ if r.language ~= 'java' then
14
+ local headers = {}
15
+ for _, n in ipairs(r.nodes) do
16
+ if callable[n.kind] then
17
+ for i = n.ti, n.tj do
18
+ local s = T:text(i)
19
+ if s == 'function' or s == '=>' then headers[i] = true end
20
+ if s == '{' then break end
21
+ end
22
+ end
23
+ end
24
+ for i = 1, T.n do
25
+ if (T:text(i) == '=>' or T:text(i) == 'function') and not headers[i]
26
+ or r.language == 'python' and T:text(i) == 'lambda' then return end
27
+ end
28
+ end
29
+ local names, roots, seen = {}, {}, {}
30
+ for _, n in ipairs(r.nodes) do if callable[n.kind] then names[n.name] = true end end
31
+ if r.language ~= 'java' then
32
+ for _, imp in ipairs(require('codeoutline.dispatch_aliases').imports(r, T)) do
33
+ names[imp.name], roots[imp.name] = true, true
34
+ end
35
+ end
36
+ for _, ref in ipairs(r.refs) do seen[ref.tok] = true end
37
+ local aliases = {}
38
+ for i = 2, T.n - 1 do
39
+ if T:text(i) == '=' and T.k[i - 1] == 'id' and T.k[i + 1] == 'id'
40
+ and (after[T:text(i + 2)] or T.k[i + 2] == 'nl') then
41
+ aliases[#aliases + 1] = { T:text(i - 1), T:text(i + 1) }
42
+ end
43
+ end
44
+ for _ = 1, 8 do
45
+ local changed = false
46
+ for _, alias in ipairs(aliases) do
47
+ if names[alias[2]] and not names[alias[1]] then names[alias[1]], changed = true, true end
48
+ end
49
+ if not changed then break end
50
+ end
51
+ for i = 1, T.n do
52
+ if i % 4096 == 0 then require('codeoutline.control').check() end
53
+ if T.k[i] == 'id' and not seen[i] then
54
+ local prev, next = T:text(i - 1), T:text(i + 1)
55
+ local explicit = prev == '::' and r.language == 'java'
56
+ local recv = (prev == '.' or explicit) and
57
+ (T.k[i - 2] == 'id' or T:text(i - 2) == 'this' or T:text(i - 2) == 'self' or T:text(i - 2) == 'cls') and T:text(i - 2)
58
+ local first = recv and i - 2 or i
59
+ local simple = before[T:text(first - 1)] and
60
+ (after[next] or T.k[i + 1] == 'nl' or i == T.n)
61
+ if (explicit or r.language ~= 'java' and simple and (names[T:text(i)] or roots[recv]))
62
+ and not (recv and (T:text(first - 1) == '.' or T:text(first - 1) == '?.')) then
63
+ r.refs[#r.refs + 1] = { tok = i, name = T:text(i), kind = 'ref', recv = recv, line = T:line(i) }
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ return M