summaryrefslogtreecommitdiff
path: root/script/vm/getGlobals.lua
blob: d794a394f9a6064d28a7cc3e97405fc6844c89c6 (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
local collector = require 'core.collector'
local guide     = require 'parser.guide'
---@diagnostic disable-next-line
---@class vm
local vm        = require 'vm.vm'
local noder     = require 'core.noder'

function vm.hasGlobalSets(name)
    local id
    if type(name) == 'string' then
        id = ('def:g:%s%s'):format(noder.STRING_CHAR, name)
    else
        id = ('def:g:%s'):format(noder.STRING_CHAR, name)
    end
    return collector.has(id)
end

function vm.getGlobalSets(name)
    local cache = vm.getCache 'getGlobalSets'
    if cache[name] then
        return cache[name]
    end
    local results = {}
    cache[name] = results
    if name == '*' then
        for noders in collector.each('def:g:') do
            for id in noder.eachID(noders) do
                if  id:sub(1, 2) == 'g:'
                and not id:find(noder.SPLIT_CHAR) then
                    for source in noder.eachSource(noders, id) do
                        if guide.isSet(source) then
                            results[#results+1] = source
                        end
                    end
                end
            end
        end
    else
        local id
        if type(name) == 'string' then
            id = ('g:%s%s'):format(noder.STRING_CHAR, name)
        else
            id = ('g:%s'):format(noder.STRING_CHAR, name)
        end
        for noders in collector.each('def:' .. id) do
            for source in noder.eachSource(noders, id) do
                if guide.isSet(source) then
                    results[#results+1] = source
                end
            end
        end
    end
    return results
end