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

local function getLibrary(source)
    local defs = vm.getDefs(source)
    for _, def in ipairs(defs) do
        if def.library then
            return def
        end
    end
    return nil
end

function vm.getLibrary(source)
    local cache = vm.getCache('getLibrary')[source]
    if cache ~= nil then
        return cache
    end
    local unlock = vm.lock('getLibrary', source)
    if not unlock then
        return
    end
    cache = getLibrary(source) or false
    vm.getCache('getLibrary')[source] = cache
    unlock()
    return cache
end