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

local function getGlobals(root)
    local env  = guide.getENV(root)
    if not env then
        return nil
    end
    local cache = {}
    local fields = guide.requestFields(env)
    for _, src in ipairs(fields) do
        local name = vm.getKeyName(src)
        if not name then
            return
        end
        if not cache[name] then
            cache[name] = {
                key  = name,
            }
        end
        cache[name][#cache[name]+1] = src
        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