summaryrefslogtreecommitdiff
path: root/script-beta/vm/eachRef.lua
blob: cfdc1d19c3fabadeb3d1b8116358e096642a25ad (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
local vm     = require 'vm.vm'
local guide  = require 'parser.guide'
local util   = require 'utility'
local await  = require 'await'

local function getRefs(source, results)
    results = results or {}
    local lock = vm.lock('eachDef', source)
    if not lock then
        return results
    end

    await.delay()

    local clock = os.clock()
    local myResults, count = guide.requestReference(source, vm.interface)
    if DEVELOP and os.clock() - clock > 0.1 then
        log.warn('requestReference', count, os.clock() - clock, guide.getUri(source), util.dump(source, { deep = 1 }))
    end
    vm.mergeResults(results, myResults)

    lock()

    return results
end

function vm.getRefs(source)
    if guide.isGlobal(source) then
        local name = guide.getKeyName(source)
        local cache =  vm.getCache('eachRefOfGlobal')[name]
                    or vm.getCache('eachRef')[source]
                    or getRefs(source)
        vm.getCache('eachRefOfGlobal')[name] = cache
        return cache
    else
        local cache =  vm.getCache('eachRef')[source]
                    or getRefs(source)
        vm.getCache('eachRef')[source] = cache
        return cache
    end
end

function vm.eachRef(source, callback)
    local results = vm.getRefs(source)
    for i = 1, #results do
        callback(results[i])
    end
end