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

local function eachFieldOfLibrary(source, lib, results)
    if not lib or lib.type ~= 'table' or not lib.child then
        return
    end
    for _, value in pairs(lib.child) do
        results[#results+1] =value
    end
end

local function eachField(source)
    local lib = vm.getLibrary(source)
    local results = guide.requestFields(source)
    eachFieldOfLibrary(source, lib, results)
    return results
end

function vm.eachField(source, callback)
    local cache = vm.cache.eachField[source]
    if cache ~= nil then
        for i = 1, #cache do
            callback(cache[i])
        end
        return
    end
    local unlock = vm.lock('eachField', source)
    if not unlock then
        return
    end
    cache = eachField(source) or false
    vm.cache.eachField[source] = cache
    unlock()
    for i = 1, #cache do
        callback(cache[i])
    end
end