@codua/codeoutline-win32-x64 0.2.1 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/bin/xnet.exe +0 -0
- package/build-info.json +1 -1
- package/package.json +1 -1
- package/scripts/codeoutline/callers.lua +116 -0
- package/scripts/codeoutline/command.lua +7 -3
- package/scripts/codeoutline/common.lua +29 -3
- package/scripts/codeoutline/completion.lua +162 -62
- package/scripts/codeoutline/cpp_bindings.lua +116 -0
- package/scripts/codeoutline/cpp_types.lua +209 -0
- package/scripts/codeoutline/csharp_bindings.lua +498 -0
- package/scripts/codeoutline/dispatch_aliases.lua +474 -0
- package/scripts/codeoutline/documents.lua +11 -1
- package/scripts/codeoutline/explore.lua +73 -12
- package/scripts/codeoutline/foreign.lua +125 -0
- package/scripts/codeoutline/foreign_lua.lua +273 -0
- package/scripts/codeoutline/graph.lua +80 -22
- package/scripts/codeoutline/index.lua +15 -6
- package/scripts/codeoutline/index_worker.lua +15 -7
- package/scripts/codeoutline/indirect.lua +148 -50
- package/scripts/codeoutline/js_evidence.lua +30 -2
- package/scripts/codeoutline/js_resolution.lua +34 -0
- package/scripts/codeoutline/lang/csharp.lua +15 -3
- package/scripts/codeoutline/lang/lua.lua +17 -2
- package/scripts/codeoutline/lsp.lua +58 -34
- package/scripts/codeoutline/lua_bindings.lua +242 -34
- package/scripts/codeoutline/lua_literal.lua +38 -0
- package/scripts/codeoutline/main.lua +24 -3
- package/scripts/codeoutline/mcp.lua +13 -4
- package/scripts/codeoutline/navigation.lua +337 -96
- package/scripts/codeoutline/ordinary_bindings.lua +220 -0
- package/scripts/codeoutline/parse.lua +2 -0
- package/scripts/codeoutline/python_bindings.lua +112 -0
- package/scripts/codeoutline/query_report.lua +126 -0
- package/scripts/codeoutline/rules.lua +127 -0
- package/scripts/codeoutline/service.lua +11 -3
- package/scripts/codeoutline/static_dispatch.lua +534 -0
- package/scripts/codeoutline/update_sequence.lua +1 -1
- package/scripts/codeoutline/version.lua +1 -1
|
@@ -214,11 +214,14 @@ end
|
|
|
214
214
|
-- Shortest call path a -> b (out edges), up to depth; returns id list.
|
|
215
215
|
local function path_between(G, a, b, depth)
|
|
216
216
|
local prev, frontier = { [a] = a }, { a }
|
|
217
|
+
local expansion_limited = false
|
|
217
218
|
for _ = 1, depth do
|
|
218
219
|
local next_frontier = {}
|
|
219
220
|
for _, x in ipairs(frontier) do
|
|
220
221
|
control.check()
|
|
221
|
-
local out = G.out[x]
|
|
222
|
+
local out = G.out[x]
|
|
223
|
+
if out == nil and G.incomplete then expansion_limited = true end
|
|
224
|
+
out = out or {}
|
|
222
225
|
for i = 1, #out, graph.EDGE do
|
|
223
226
|
if i % 128 == 1 then control.check() end
|
|
224
227
|
local dst = out[i]
|
|
@@ -234,9 +237,10 @@ local function path_between(G, a, b, depth)
|
|
|
234
237
|
end
|
|
235
238
|
end
|
|
236
239
|
frontier = next_frontier
|
|
237
|
-
if #frontier == 0
|
|
240
|
+
if #frontier == 0 then return nil, false, prev, expansion_limited end
|
|
241
|
+
if #frontier > 20000 then return nil, true, prev, expansion_limited end
|
|
238
242
|
end
|
|
239
|
-
return nil
|
|
243
|
+
return nil, #frontier > 0, prev, expansion_limited
|
|
240
244
|
end
|
|
241
245
|
|
|
242
246
|
local file_cache = {}
|
|
@@ -245,7 +249,7 @@ local function file_lines(idx, rel)
|
|
|
245
249
|
local lines = file_cache[key]
|
|
246
250
|
if lines then return lines end
|
|
247
251
|
lines = {}
|
|
248
|
-
local src = idx:query_source(rel)
|
|
252
|
+
local src = assert(idx:query_source(rel), 'cannot read source: ' .. rel)
|
|
249
253
|
if src then
|
|
250
254
|
for line in (src .. '\n'):gmatch('([^\n]*)\n') do lines[#lines + 1] = (line:gsub('\r$', '')) end
|
|
251
255
|
end
|
|
@@ -281,7 +285,7 @@ local function node_block(G, idx, id, full)
|
|
|
281
285
|
for _, k in ipairs(kids) do
|
|
282
286
|
local kn = G.nodes[k]
|
|
283
287
|
if not rows[kn.line] then row_lines[#row_lines + 1] = kn.line end
|
|
284
|
-
rows[kn.line] = kn.sig or kn.name
|
|
288
|
+
rows[kn.line] = require('codeoutline.navigation').detail(G, G.node_file[k], kn).sig or kn.name
|
|
285
289
|
end
|
|
286
290
|
for _, ln in ipairs(row_lines) do
|
|
287
291
|
out[#out + 1] = string.format('%d\t %s', ln, rows[ln])
|
|
@@ -293,7 +297,7 @@ local function node_block(G, idx, id, full)
|
|
|
293
297
|
render_range(lines, b - M.CUT_TAIL + 1, b, out)
|
|
294
298
|
end
|
|
295
299
|
end
|
|
296
|
-
return table.concat(out, '\n')
|
|
300
|
+
return table.concat(out, '\n'), span > full
|
|
297
301
|
end
|
|
298
302
|
|
|
299
303
|
local function label(G, id)
|
|
@@ -307,7 +311,10 @@ function M.run(G, idx, query, opts)
|
|
|
307
311
|
control.check()
|
|
308
312
|
opts = opts or {}
|
|
309
313
|
local budget = M.validate(query, opts)
|
|
310
|
-
|
|
314
|
+
local query_opts = {}
|
|
315
|
+
for key, value in pairs(opts) do query_opts[key] = value end
|
|
316
|
+
query_opts.explain = true
|
|
317
|
+
G = graph.query(G, query_opts)
|
|
311
318
|
file_cache = {}
|
|
312
319
|
local ranked, named_files = find_seeds(G, query)
|
|
313
320
|
|
|
@@ -334,11 +341,17 @@ function M.run(G, idx, query, opts)
|
|
|
334
341
|
|
|
335
342
|
-- Spine: nodes on call paths between seeds.
|
|
336
343
|
local flows, spine, on_spine = {}, {}, {}
|
|
344
|
+
local reports = require('codeoutline.query_report')
|
|
345
|
+
local searches, missed, connected = {}, {}, {}
|
|
346
|
+
local path_limited = false
|
|
337
347
|
for _, a in ipairs(seeds) do
|
|
338
348
|
for _, b in ipairs(seeds) do
|
|
339
349
|
if a ~= b then
|
|
340
|
-
local p = path_between(G, a, b, M.PATH_DEPTH)
|
|
350
|
+
local p, limited, prev, expansion_limited = path_between(G, a, b, M.PATH_DEPTH)
|
|
351
|
+
path_limited = path_limited or limited or false
|
|
341
352
|
if p then
|
|
353
|
+
connected[a] = connected[a] or {}
|
|
354
|
+
connected[a][b] = true
|
|
342
355
|
local names = {}
|
|
343
356
|
for _, id in ipairs(p) do
|
|
344
357
|
names[#names + 1] = G.nodes[id].name
|
|
@@ -348,6 +361,13 @@ function M.run(G, idx, query, opts)
|
|
|
348
361
|
end
|
|
349
362
|
end
|
|
350
363
|
flows[#flows + 1] = table.concat(names, ' -> ')
|
|
364
|
+
else
|
|
365
|
+
if not missed[a] then
|
|
366
|
+
missed[a] = { root = a, targets = {}, limited = limited, incomplete = expansion_limited,
|
|
367
|
+
sites = reports.capture_paths(G.report, prev, a) }
|
|
368
|
+
searches[#searches + 1] = missed[a]
|
|
369
|
+
end
|
|
370
|
+
missed[a].targets[#missed[a].targets + 1] = b
|
|
351
371
|
end
|
|
352
372
|
end
|
|
353
373
|
end
|
|
@@ -416,6 +436,11 @@ function M.run(G, idx, query, opts)
|
|
|
416
436
|
end
|
|
417
437
|
|
|
418
438
|
if G.load_incoming then G:load_incoming(seeds) end
|
|
439
|
+
if #seeds == 1 then
|
|
440
|
+
searches[1] = { root = seeds[1], targets = {}, single = true,
|
|
441
|
+
sites = reports.capture_paths(G.report, { [seeds[1]] = seeds[1] }, seeds[1]) }
|
|
442
|
+
end
|
|
443
|
+
local breakpoints = reports.path_breakpoints(G.report, G, searches, connected)
|
|
419
444
|
|
|
420
445
|
-- Confirm every file whose source or symbol labels this query can emit.
|
|
421
446
|
-- GBK reparsing invalidates node IDs, so rebuild and select again before
|
|
@@ -428,12 +453,33 @@ function M.run(G, idx, query, opts)
|
|
|
428
453
|
for i = 1, #edges, graph.EDGE do selected[G.node_file[edges[i]]] = true end
|
|
429
454
|
end
|
|
430
455
|
end
|
|
456
|
+
for _, entry in ipairs(breakpoints.entries) do
|
|
457
|
+
for _, id in ipairs(entry.path) do selected[G.node_file[id]] = true end
|
|
458
|
+
if entry.goal then selected[G.node_file[entry.goal]] = true end
|
|
459
|
+
end
|
|
431
460
|
for f in pairs(selected) do
|
|
432
|
-
local
|
|
461
|
+
local path = G.files[f].path -- a decoding repair replaces this file ID
|
|
462
|
+
local source, repaired = idx:query_source(path)
|
|
463
|
+
assert(source, 'cannot read source: ' .. path)
|
|
433
464
|
changed = repaired or changed
|
|
434
465
|
end
|
|
435
466
|
if changed then return nil, { encoding_retry = true } end
|
|
436
467
|
|
|
468
|
+
local evidence_text = reports.render(G.report, G)
|
|
469
|
+
local evidence_budget = math.min(4096, math.floor(budget / 4))
|
|
470
|
+
local breakpoint_text = reports.render_breakpoints(breakpoints, G)
|
|
471
|
+
local breakpoint_budget = breakpoint_text and math.floor(evidence_budget / 2) or 0
|
|
472
|
+
evidence_budget = evidence_budget - breakpoint_budget
|
|
473
|
+
local breakpoint_cut = breakpoint_text and (#breakpoint_text > breakpoint_budget or breakpoints.truncated) or false
|
|
474
|
+
if breakpoint_budget >= 256 then emit(text.limit(breakpoint_text, breakpoint_budget, breakpoints.truncated))
|
|
475
|
+
elseif breakpoint_text then breakpoint_cut = true end
|
|
476
|
+
omitted = omitted or breakpoint_cut
|
|
477
|
+
local evidence_cut = #evidence_text > evidence_budget
|
|
478
|
+
if evidence_budget >= 256 then
|
|
479
|
+
emit(text.limit(evidence_text, evidence_budget, G.report.truncated))
|
|
480
|
+
else evidence_cut = #G.report.entries > 0 end
|
|
481
|
+
omitted = omitted or evidence_cut or G.report.truncated or #flows > M.LIST_CAP
|
|
482
|
+
|
|
437
483
|
local reserve = math.floor(budget * 0.15) -- keep room for sections 3-4
|
|
438
484
|
for _, f in ipairs(file_order) do
|
|
439
485
|
local rec = G.files[f]
|
|
@@ -445,7 +491,7 @@ function M.run(G, idx, query, opts)
|
|
|
445
491
|
local ol = {}
|
|
446
492
|
for _, node in ipairs(rec.nodes) do
|
|
447
493
|
if not node.parent and node.kind ~= 'macro' then
|
|
448
|
-
ol[#ol + 1] = string.format('%d\t%s %s', node.line, node.kind, node.sig or node.name)
|
|
494
|
+
ol[#ol + 1] = string.format('%d\t%s %s', node.line, node.kind, require('codeoutline.navigation').detail(G, f, node).sig or node.name)
|
|
449
495
|
end
|
|
450
496
|
if #ol >= 60 then ol[#ol + 1] = '\t...'; break end
|
|
451
497
|
end
|
|
@@ -454,7 +500,8 @@ function M.run(G, idx, query, opts)
|
|
|
454
500
|
end
|
|
455
501
|
for _, item in ipairs(items) do
|
|
456
502
|
if not shown[item.id] then
|
|
457
|
-
local block = node_block(G, idx, item.id, item.full)
|
|
503
|
+
local block, source_cut = node_block(G, idx, item.id, item.full)
|
|
504
|
+
omitted = omitted or source_cut
|
|
458
505
|
local need = #block + (wrote_header and 0 or #header + 1) + 1
|
|
459
506
|
if used + need < budget - reserve then
|
|
460
507
|
if not wrote_header then emit(header); wrote_header = true end
|
|
@@ -480,6 +527,7 @@ function M.run(G, idx, query, opts)
|
|
|
480
527
|
seen_in[src] = true
|
|
481
528
|
callers[#callers + 1] = string.format('%s (%s:%d)', G.nodes[src].name, G.files[G.node_file[src]].path, inn[i + 1])
|
|
482
529
|
.. (inn[i + 2]:match('^indirect:') and (' [' .. inn[i + 2] .. ']') or '')
|
|
530
|
+
elseif not seen_in[src] then omitted = true
|
|
483
531
|
end
|
|
484
532
|
end
|
|
485
533
|
for i = 1, #out, graph.EDGE do
|
|
@@ -488,6 +536,7 @@ function M.run(G, idx, query, opts)
|
|
|
488
536
|
seen_out[dst] = true
|
|
489
537
|
callees[#callees + 1] = G.nodes[dst].name .. ' (' .. graph.where(G, dst) .. ')'
|
|
490
538
|
.. (out[i + 2]:match('^indirect:') and (' [' .. out[i + 2] .. ']') or '')
|
|
539
|
+
elseif not seen_out[dst] then omitted = true
|
|
491
540
|
end
|
|
492
541
|
end
|
|
493
542
|
if #callers > 0 or #callees > 0 then
|
|
@@ -523,9 +572,21 @@ function M.run(G, idx, query, opts)
|
|
|
523
572
|
table.insert(parts, 1, 'Call-path expansion limit reached; relationships are incomplete.')
|
|
524
573
|
omitted = true
|
|
525
574
|
end
|
|
575
|
+
if path_limited then
|
|
576
|
+
table.insert(parts, 1, 'Call-path search reached its depth/frontier limit; no path found is not proof of no runtime connection.')
|
|
577
|
+
end
|
|
578
|
+
if evidence_cut or G.report.truncated then
|
|
579
|
+
table.insert(parts, 1, 'Relationship explanations omitted due to evidence/output limits.')
|
|
580
|
+
end
|
|
581
|
+
if breakpoint_cut then
|
|
582
|
+
table.insert(parts, 1, 'Call-path breakpoint explanations omitted due to site/evidence/output limits.')
|
|
583
|
+
end
|
|
526
584
|
local result, truncated = text.limit(table.concat(parts, '\n'), budget, omitted or #pointers > 0)
|
|
527
585
|
return result, { seeds = seeds, spine = spine, shown = shown, flows = flows, truncated = truncated,
|
|
528
|
-
relationships_incomplete = G.incomplete or false, query_stats = G.stats
|
|
586
|
+
relationships_incomplete = G.incomplete or false, query_stats = G.stats,
|
|
587
|
+
evidence = G.report.entries, evidence_truncated = evidence_cut or G.report.truncated,
|
|
588
|
+
path_search_limited = path_limited, evidence_scope = G.report.scope,
|
|
589
|
+
path_breakpoints = breakpoints.entries, path_breakpoints_truncated = breakpoint_cut }
|
|
529
590
|
end
|
|
530
591
|
|
|
531
592
|
return M
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
-- Language-neutral native exports. Adapters supply registration evidence and
|
|
2
|
+
-- guest parameters; implementation nodes retain their original host signature.
|
|
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
|
|
56
|
+
|
|
57
|
+
function M.register(rec, language, module, exports)
|
|
58
|
+
rec.foreign_exports = rec.foreign_exports or {}
|
|
59
|
+
rec.foreign_exports[language] = rec.foreign_exports[language] or {}
|
|
60
|
+
rec.foreign_exports[language][module] = exports
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
function M.exports(rec, language, module)
|
|
64
|
+
local exports = records[rec] or rec.foreign_exports
|
|
65
|
+
return exports and exports[language] and exports[language][module]
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
function M.collect(rec, T)
|
|
69
|
+
require('codeoutline.foreign_lua').collect(rec, T)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
function M.modules(G, language, name, ctx)
|
|
73
|
+
ctx = ctx or {}
|
|
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
|
|
83
|
+
ctx.foreign[lang] = ctx.foreign[lang] or {}
|
|
84
|
+
for module in pairs(modules) do
|
|
85
|
+
local names = ctx.foreign[lang]
|
|
86
|
+
names[module] = names[module] or {}
|
|
87
|
+
names[module][#names[module] + 1] = f
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
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
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
-- Parameter labels are structured data, never reparsed as host-language code.
|
|
99
|
+
function M.signature(node, language, name)
|
|
100
|
+
local signatures = contracts[node] or node.foreign_signatures
|
|
101
|
+
local contract = signatures and signatures[language]
|
|
102
|
+
if not contract then return nil end
|
|
103
|
+
local labels, parameters = {}, {}
|
|
104
|
+
for i, p in ipairs(contract.parameters) do
|
|
105
|
+
local label = (p.name or ('arg' .. i)) .. (p.optional and '?' or '') .. ':' .. (p.type or 'unknown')
|
|
106
|
+
if p.default then label = label .. ' = ' .. p.default end
|
|
107
|
+
labels[#labels + 1] = label
|
|
108
|
+
parameters[#parameters + 1] = { label = label }
|
|
109
|
+
end
|
|
110
|
+
if contract.unknown_tail then
|
|
111
|
+
labels[#labels + 1] = '...:unknown'
|
|
112
|
+
parameters[#parameters + 1] = { label = '...:unknown' }
|
|
113
|
+
end
|
|
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
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
return M
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
-- Lua C API adapter for the language-neutral foreign interface model.
|
|
2
|
+
local M = {}
|
|
3
|
+
local control = require('codeoutline.control')
|
|
4
|
+
local function literal(T, i)
|
|
5
|
+
local s = T:text(i)
|
|
6
|
+
if T.k[i] ~= 'str' or s:find('\\', 1, true) then return nil end
|
|
7
|
+
return s:sub(2, -2)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
local READ = {
|
|
11
|
+
luaL_checkinteger = 'int', luaL_optinteger = 'int', lua_tointeger = 'int', lua_tointegerx = 'int',
|
|
12
|
+
luaL_checknumber = 'number', luaL_optnumber = 'number', lua_tonumber = 'number', lua_tonumberx = 'number',
|
|
13
|
+
luaL_checkstring = 'string', luaL_checklstring = 'string', luaL_optstring = 'string', luaL_optlstring = 'string',
|
|
14
|
+
lua_tostring = 'string', lua_tolstring = 'string', lua_toboolean = 'boolean',
|
|
15
|
+
luaL_checkudata = 'userdata', luaL_testudata = 'userdata', lua_touserdata = 'userdata',
|
|
16
|
+
luaL_checkany = 'unknown', luaL_checktype = 'unknown',
|
|
17
|
+
lua_isinteger = 'int', lua_isnumber = 'number', lua_isstring = 'string', lua_isboolean = 'boolean',
|
|
18
|
+
lua_istable = 'table', lua_isfunction = 'function', lua_isthread = 'thread', lua_isuserdata = 'userdata',
|
|
19
|
+
}
|
|
20
|
+
local TYPES = { LUA_TNUMBER = 'number', LUA_TSTRING = 'string', LUA_TBOOLEAN = 'boolean',
|
|
21
|
+
LUA_TTABLE = 'table', LUA_TFUNCTION = 'function', LUA_TTHREAD = 'thread',
|
|
22
|
+
LUA_TUSERDATA = 'userdata', LUA_TLIGHTUSERDATA = 'lightuserdata', LUA_TNIL = 'nil' }
|
|
23
|
+
|
|
24
|
+
-- offset: leading C arguments the guest never passes (a method's self).
|
|
25
|
+
local function parameters(T, node, macros, offset)
|
|
26
|
+
local state
|
|
27
|
+
for i = node.ti, node.tj - 2 do
|
|
28
|
+
if T:text(i) == 'lua_State' and T:text(i + 1) == '*' and T.k[i + 2] == 'id' then
|
|
29
|
+
state = T:text(i + 2); break
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
local slots, highest, unknown = {}, 0, false
|
|
33
|
+
for i = node.ti, node.tj do
|
|
34
|
+
if i % 4096 == 0 then control.check() end
|
|
35
|
+
local api = T:text(i)
|
|
36
|
+
if READ[api] and T:text(i + 1) == '(' and T:text(i + 2) == state and T:text(i + 3) == ',' then
|
|
37
|
+
local position = tonumber(T:text(i + 4))
|
|
38
|
+
local ending = T:text(i + 5)
|
|
39
|
+
if position and position > 0 and position <= 64 and position == math.floor(position)
|
|
40
|
+
and (ending == ',' or ending == ')') then
|
|
41
|
+
highest = math.max(highest, position)
|
|
42
|
+
local p = slots[position] or { types = {} }
|
|
43
|
+
slots[position] = p
|
|
44
|
+
local kind = READ[api]
|
|
45
|
+
if api == 'luaL_checktype' then kind = TYPES[T:text(i + 6)] or 'unknown' 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
|
|
49
|
+
p.types[kind] = true
|
|
50
|
+
local optional = api:match('^luaL_opt') ~= nil
|
|
51
|
+
if p.optional == nil then p.optional = optional else p.optional = p.optional and optional end
|
|
52
|
+
if optional then
|
|
53
|
+
local close = T.m[i + 1]
|
|
54
|
+
local last = close and close - 1
|
|
55
|
+
if last and api == 'luaL_optlstring' then
|
|
56
|
+
last = i + 6
|
|
57
|
+
while last < close and T:text(last) ~= ',' do last = last + 1 end
|
|
58
|
+
last = last - 1
|
|
59
|
+
end
|
|
60
|
+
if last and last >= i + 6 then p.default = T.src:sub(T.s[i + 6], T.e[last]) end
|
|
61
|
+
end
|
|
62
|
+
-- Only a direct assignment supplies a public argument name.
|
|
63
|
+
-- Casts between '=' and the API call are harmless; expressions aren't.
|
|
64
|
+
local j = i - 1
|
|
65
|
+
while j >= node.ti and T:text(j) ~= ';' and T:text(j) ~= '{' and T:text(j) ~= '}' do
|
|
66
|
+
if T:text(j) == '=' then
|
|
67
|
+
local direct = j == i - 1 or (T:text(j + 1) == '(' and T.m[j + 1] == i - 1)
|
|
68
|
+
if direct and not api:match('^lua_is') and api ~= 'luaL_checktype'
|
|
69
|
+
and T.k[j - 1] == 'id' and T:text(j - 2) ~= '.' and T:text(j - 2) ~= '->' then
|
|
70
|
+
local name = T:text(j - 1)
|
|
71
|
+
if not p.name then p.name = name elseif p.name ~= name then p.ambiguous = true end
|
|
72
|
+
end
|
|
73
|
+
break
|
|
74
|
+
end
|
|
75
|
+
j = j - 1
|
|
76
|
+
end
|
|
77
|
+
elseif T:text(i + 4) ~= '-' and T:text(i + 4) ~= 'lua_upvalueindex' then
|
|
78
|
+
unknown = true
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
local result, used = { parameters = {}, unknown_tail = unknown }, {}
|
|
83
|
+
offset = offset or 0
|
|
84
|
+
for i = 1 + offset, highest do
|
|
85
|
+
local p = slots[i] or { types = {} }
|
|
86
|
+
p.types.unknown = nil
|
|
87
|
+
local types = {}
|
|
88
|
+
for kind in pairs(p.types) do types[#types + 1] = kind end
|
|
89
|
+
table.sort(types)
|
|
90
|
+
local name = not p.ambiguous and p.name or nil
|
|
91
|
+
if name and used[name] then name = nil end
|
|
92
|
+
if name then used[name] = true end
|
|
93
|
+
result.parameters[i - offset] = { name = name, type = #types > 0 and table.concat(types, '|') or 'unknown',
|
|
94
|
+
optional = p.optional or nil, default = p.optional and p.default or nil }
|
|
95
|
+
end
|
|
96
|
+
return result
|
|
97
|
+
end
|
|
98
|
+
|
|
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.
|
|
195
|
+
function M.collect(r, T)
|
|
196
|
+
local macros = string_macros(T)
|
|
197
|
+
local tables, functions = {}, {}
|
|
198
|
+
for n, node in ipairs(r.nodes) do
|
|
199
|
+
if node.kind == 'function' then functions[node.name] = n end
|
|
200
|
+
end
|
|
201
|
+
for i = 1, T.n do
|
|
202
|
+
if i % 4096 == 0 then control.check() end
|
|
203
|
+
if T:text(i) == 'luaL_Reg' and T.k[i + 1] == 'id' then
|
|
204
|
+
local j = i + 2
|
|
205
|
+
if T:text(j) == '[' and T.m[j] then j = T.m[j] + 1 end
|
|
206
|
+
if T:text(j) == '=' and T:text(j + 1) == '{' and T.m[j + 1] then
|
|
207
|
+
local entries = {}
|
|
208
|
+
for k = j + 2, T.m[j + 1] - 1 do
|
|
209
|
+
if T:text(k) == '{' and T.k[k + 1] == 'str' and T:text(k + 2) == ',' then
|
|
210
|
+
local name, target = literal(T, k + 1), functions[T:text(k + 3)]
|
|
211
|
+
if name and target and T:text(k + 4) == '}' then entries[name] = target end
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
tables[T:text(i + 1)] = entries
|
|
215
|
+
end
|
|
216
|
+
end
|
|
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
|
|
227
|
+
for _, node in ipairs(r.nodes) do
|
|
228
|
+
local module = node.kind == 'function' and node.name:match('^luaopen_(.+)$')
|
|
229
|
+
if module then
|
|
230
|
+
local exports, metas, meta, discard = {}, {}, nil, {}
|
|
231
|
+
for i = node.ti, node.tj do
|
|
232
|
+
local call = T:text(i)
|
|
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
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
if next(exports) then
|
|
266
|
+
require('codeoutline.foreign').register(r, 'lua', module, exports)
|
|
267
|
+
for _, n in pairs(exports) do signature(n, nil) end
|
|
268
|
+
end
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
return M
|