summaryrefslogtreecommitdiff
path: root/script-beta/vm/eachField.lua
blob: 12d1d908307665e41bb3fabad3747e7f1d80db37 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
local vm      = require 'vm.vm'
local guide   = require 'parser.guide'
local library = require 'library'
local await   = require 'await'

local function eachFieldInLibrary(source, lib, results)
    if not lib or not lib.child then
        return
    end
    for _, value in pairs(lib.child) do
        if value.name:sub(1, 1) ~= '@' then
            results[#results+1] = value
        end
    end
end

local function eachFieldOfLibrary(results)
    for _, lib in pairs(library.global) do
        results[#results+1] = lib
    end
end

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)
    if source.special == '_G' then
        eachFieldOfLibrary(results)
    end
    if library.object[source.type] then
        eachFieldInLibrary(source, library.object[source.type], results)
    end

    unlock()
    return results
end

function vm.getFields(source, deep)
    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