summaryrefslogtreecommitdiff
path: root/script-beta/vm/getGlobals.lua
blob: 53fff173d4e4feb7aa5f3c228e19791945552f93 (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
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 name then
            if not globals[name] then
                globals[name] = {}
            end
            globals[name][#globals[name]+1] = res
        end
    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.getCache('getGlobals')[name]
    if cache ~= nil then
        return cache
    end
    cache = getGlobals(name)
    vm.getCache('getGlobals')[name] = cache
    return cache
end