summaryrefslogtreecommitdiff
path: root/script-beta/vm/eachMeta.lua
blob: 096c0b0a990c7cf30d7c04383b1335991fbd4c1f (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
local vm = require 'vm.vm'

function vm.setMeta(source, meta)
    if not source or not meta then
        return
    end
    vm.cache.getMeta[source] = meta
end

--- 获取所有的引用
function vm.eachMeta(source, callback)
    local cache = vm.cache.eachMeta[source]
    if cache then
        for i = 1, #cache do
            local res = callback(cache[i])
            if res ~= nil then
                return res
            end
        end
        return
    end
    local unlock = vm.lock('eachMeta', source)
    if not unlock then
        return
    end
    cache = {}
    vm.eachRef(source, function (info)
        local src = info.source
        vm.cache.eachMeta[src] = cache
    end)
    unlock()
    vm.eachRef(source, function (info)
        local src = info.source
        cache[#cache+1] = vm.cache.getMeta[src]
    end)
    for i = 1, #cache do
        local res = callback(cache[i])
        if res ~= nil then
            return res
        end
    end
end