summaryrefslogtreecommitdiff
path: root/script-beta/vm/getLibrary.lua
blob: ac6402de1b81ec67c10ecf4fc5f9ad0d24a8a3e8 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
local vm      = require 'vm.vm'
local library = require 'library'
local guide   = require 'parser.guide'

local function checkStdLibrary(source)
    if source.library then
        return source
    end
    local globalName = vm.getGlobal(source)
    if not globalName then
        return nil
    end
    local name = globalName:match '^s|(.+)$'
    if library.global[name] then
        return library.global[name]
    end
end

local function getLibInNode(source, nodeLib)
    if not nodeLib then
        return nil
    end
    if not nodeLib.child then
        return nil
    end
    local key = guide.getName(source)
    local defLib = nodeLib.child[key]
    return defLib
end

local function getNodeAsTable(source)
    local node = source.node
    local nodeGlobalName = vm.getGlobal(node)
    if not nodeGlobalName then
        return nil
    end
    local nodeName = nodeGlobalName:match '^s|(.+)$'
    return getLibInNode(source, library.global[nodeName])
end

local function getNodeAsObject(source)
    local node = source.node
    local values = vm.getValue(node)
    if not values then
        return nil
    end
    for i = 1, #values do
        local value = values[i]
        local type = value.type
        local nodeLib = library.object[type]
        local lib = getLibInNode(source, nodeLib)
        if lib then
            return lib
        end
    end
    return nil
end

local function checkNode(source)
    if source.type == 'method' then
        source = source.parent
    elseif source.type == 'field' then
        source = source.parent
    end
    if source.type == 'getfield'
    or source.type == 'getmethod'
    or source.type == 'getindex' then
        return getNodeAsTable(source)
            or getNodeAsObject(source)
    end
end

local function checkRef(source)
    local results = guide.requestReference(source)
    for _, src in ipairs(results) do
        local lib = checkStdLibrary(src) or checkNode(src)
        if lib then
            return lib
        end
    end
    return nil
end

local function getLibrary(source)
    return checkNode(source)
        or checkStdLibrary(source)
        or checkRef(source)
end

function vm.getLibrary(source)
    local cache = vm.cache.getLibrary[source]
    if cache ~= nil then
        return cache
    end
    local unlock = vm.lock('getLibrary', source)
    if not unlock then
        return
    end
    cache = getLibrary(source) or false
    vm.cache.getLibrary[source] = cache
    unlock()
    return cache
end