summaryrefslogtreecommitdiff
path: root/script/vm/eachField.lua
blob: ce0e392868d096536001749287d4b74344bbc2ea (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 vm      = require 'vm.vm'
local guide   = require 'parser.guide'
local await   = require 'await'

local function eachField(source, deep)
    local unlock = vm.lock('eachField', source)
    if not unlock then
        return {}
    end

    while source.type == 'paren' do
        source = source.exp
        if not source then
            return {}
        end
    end

    await.delay()
    local results = guide.requestFields(source, vm.interface, deep)

    unlock()
    return results
end

function vm.getFields(source, deep)
    if source.special == '_G' then
        return vm.getGlobals '*'
    end
    if guide.isGlobal(source) then
        local name = guide.getKeyName(source)
        local cache =  vm.getCache('eachFieldOfGlobal')[name]
                    or vm.getCache('eachField')[source]
                    or eachField(source, 'deep')
        vm.getCache('eachFieldOfGlobal')[name] = cache
        vm.getCache('eachField')[source] = cache
        return cache
    else
        local cache =  vm.getCache('eachField')[source]
                    or eachField(source, deep)
        if deep then
            vm.getCache('eachField')[source] = cache
        end
        return cache
    end
end