summaryrefslogtreecommitdiff
path: root/script/vm/getClass.lua
blob: fbd50fc8d17e62387ad80df5853b0d9e063111de (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
---@type vm
local vm       = require 'vm.vm'
local searcher = require 'core.searcher'

local function lookUpDocClass(source)
    local infers = vm.getInfers(source, 0)
    for _, infer in ipairs(infers) do
        if infer.source.type == 'doc.class'
        or infer.source.type == 'doc.type' then
            return searcher.viewInferType(infers)
        end
    end
    return nil
end

local function getClass(source, classes, depth, deep)
    local docClass = lookUpDocClass(source)
    if docClass then
        classes[docClass] = true
        return
    end
    if depth > 3 then
        return
    end
    local value = searcher.getObjectValue(source) or source
    if not deep then
        if value and value.type == 'string' then
            classes[value[1]] = true
        end
    else
        for _, src in ipairs(vm.getDefFields(value)) do
            local key = vm.getKeyName(src)
            if not key then
                goto CONTINUE
            end
            local lkey = key:lower()
            if lkey == 'type'
            or lkey == '__name'
            or lkey == 'name'
            or lkey == 'class' then
                local value = searcher.getObjectValue(src)
                if value and value.type == 'string' then
                    classes[value[1]] = true
                end
            end
            ::CONTINUE::
        end
    end
    if next(classes) then
        return
    end
    vm.eachMeta(source, function (mt)
        getClass(mt, classes, depth + 1, deep)
    end)
end

function vm.getClass(source, deep)
    local classes = {}
    getClass(source, classes, 1, deep)
    if not next(classes) then
        return nil
    end
    return searcher.mergeTypes(classes)
end