summaryrefslogtreecommitdiff
path: root/script-beta/vm/getGlobals.lua
blob: 699dd2700161efe9b2d686f29b114b27b3790626 (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
local guide = require 'parser.guide'
local vm    = require 'vm.vm'

local function getGlobals(root)
    local env  = guide.getENV(root)
    local cache = {}
    local mark = {}
    vm.eachField(env, function (info)
        local src = info.source
        if mark[src] then
            return
        end
        mark[src] = true
        local name = info.key
        if not name then
            return
        end
        if not cache[name] then
            cache[name] = {
                key  = name,
                mode = {},
            }
        end
        cache[name][#cache[name]+1] = info
        cache[name].mode[info.mode] = true
        vm.cache.getGlobal[src] = name
    end)
    return cache
end

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