summaryrefslogtreecommitdiff
path: root/script-beta/vm/getGlobals.lua
blob: 8ac63090dc1d2cb61c0a31c1177c8f613ea0242a (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 files = require 'files'

local function getGlobalsOfFile(uri)
    local globals = {}
    local ast = files.getAst(uri)
    if not ast then
        return globals
    end
    local env = guide.getENV(ast.ast)
    local results = guide.requestFields(env)
    for _, res in ipairs(results) do
        local name = guide.getSimpleName(res)
        if not globals[name] then
            globals[name] = {}
        end
        globals[name][#globals[name]+1] = res
    end
    return globals
end

local function getGlobals(name)
    local results = {}
    for uri in files:eachFile() do
        local cache = files.getCache(uri)
        cache.globals = cache.globals or getGlobalsOfFile(uri)
        if cache.globals[name] then
            for _, source in ipairs(cache.globals[name]) do
                results[#results+1] = source
            end
        end
    end
    return results
end

function vm.getGlobals(name)
    local cache = vm.cache.getGlobals[name]
    if cache ~= nil then
        return cache
    end
    cache = getGlobals(name)
    vm.cache.getGlobals[name] = cache
    return cache
end