summaryrefslogtreecommitdiff
path: root/script-beta/vm/getClass.lua
blob: c4bde74a64137c8eeb38e58da8c425f2a6992530 (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
local vm = require 'vm.vm'

local function getClass(source, classes, deep)
    if deep > 3 then
        return
    end
    vm.eachField(source, function (src)
        local key = vm.getKeyName(src)
        local lkey = key:lower()
        if lkey == 's|type'
        or lkey == 's|__name'
        or lkey == 's|name'
        or lkey == 's|class' then
            if src.value and src.value.type == 'string' then
                classes[#classes+1] = src.value[1]
            end
        end
    end)
    if source.value then
        vm.eachField(source.value, function (src)
            local key = vm.getKeyName(src)
            local lkey = key:lower()
            if lkey == 's|type'
            or lkey == 's|__name'
            or lkey == 's|name'
            or lkey == 's|class' then
                if src.value and src.value.type == 'string' then
                    classes[#classes+1] = src.value[1]
                end
            end
        end)
    end
    if #classes ~= 0 then
        return
    end
    vm.eachMeta(source, function (mt)
        getClass(mt, classes, deep + 1)
    end)
    if source.value then
        vm.eachMeta(source.value, function (mt)
            getClass(mt, classes, deep + 1)
        end)
    end
end

function vm.getClass(source)
    local classes = {}
    getClass(source, classes, 1)
    if #classes == 0 then
        return nil
    end
    return vm.mergeTypeViews(table.unpack(classes))
end