summaryrefslogtreecommitdiff
path: root/script/plugins/ffi/searchCode.lua
blob: 86dbc6803a3f5bdc2780f9aa7356a2a241b2cc58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
local vm = require 'vm'

local function getLiterals(arg)
    local literals = vm.getLiterals(arg)
    local res = {}
    if not literals then
        return res
    end
    for k, v in pairs(literals) do
        if type(k) == 'string' then
            res[#res+1] = k
        end
    end
    return res
end

---@return string[]?
local function getCode(CdefReference)
    local target = CdefReference.target
    if not (target.type == 'field' and target.parent.type == 'getfield') then
        return
    end
    target = target.parent.parent
    if target.type == 'call' then
        return getLiterals(target.args and target.args[1])
    elseif target.type == 'local' then
        local res = {}
        for _, o in ipairs(target.ref) do
            if o.parent.type ~= 'call' then
                goto CONTINUE
            end
            local target = o.parent
            local literals = vm.getLiterals(target.args and target.args[1])
            if not literals then
                goto CONTINUE
            end
            for k, v in pairs(literals) do
                if type(k) == 'string' then
                    res[#res+1] = k
                end
            end
            ::CONTINUE::
        end
        return res
    end
end

---@async
return function (CdefReference, target_uri)
    if not CdefReference then
        return nil
    end
    local codeResults
    for i, v in ipairs(CdefReference) do
        if v.uri ~= target_uri then
            goto continue
        end
        local codes = getCode(v)
        if not codes then
            goto continue
        end
        for i, v in ipairs(codes) do
            codeResults = codeResults or {}
            codeResults[#codeResults+1] = v
        end
        ::continue::
    end
    return codeResults
end