summaryrefslogtreecommitdiff
path: root/server-beta/src/searcher/getLibrary.lua
blob: 3b9714a312043626fcb522473abe5a2a900b1748 (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
local searcher = require 'searcher.searcher'
local guide    = require 'parser.guide'
local library  = require 'library'

local function getLibrary(source)
    local name = guide.getKeyName(source)
    if not name then
        return
    end
    local sname = name:match '^s|(.+)$'
    if not sname then
        return
    end
    if searcher.isGlobal(source) then
        return library.global[sname]
    end
    local node = source.node
    if not node then
        return
    end

    local lib
    searcher.eachRef(node, function (info)
        local src = info.source
        if info.mode == 'get' and searcher.isGlobal(node) then
            local nodeName = guide.getKeyName(src)
            if not nodeName then
                return
            end
            local sNodeName = nodeName:match '^s|(.+)$'
            if not sNodeName then
                return
            end
            local tbl = library.global[sNodeName]
            if tbl then
                lib = lib or tbl[sname]
            end
        end
    end)
    return lib
end

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