summaryrefslogtreecommitdiff
path: root/script-beta/vm/getLibrary.lua
blob: 86f1ae58135125e8361aba7397a85cd34a4f66ea (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
local vm      = require 'vm.vm'
local guide   = require 'parser.guide'
local library = require 'library'

local function getLibrary(source, deep)
    if source.type == 'library' then
        return source
    end
    if source.library then
        return source.library
    end
    if source.type == 'getglobal'
    or source.type == 'setglobal' then
        if source.node and source.node.type == 'local' then
            local lib = library.global[guide.getName(source)]
            if lib then
                return lib
            end
        end
    end

    local unlock = vm.lock('getLibrary', source)
    if not unlock then
        return
    end

    local defs = vm.getDefs(source, deep)
    unlock()

    for _, def in ipairs(defs) do
        if def.type == 'library' then
            return def
        end
    end

    return nil
end

function vm.getLibrary(source, deep)
    if guide.isGlobal(source) then
        local name = guide.getKeyName(source)
        local cache =  vm.getCache('getLibraryOfGlobal')[name]
                    or vm.getCache('getLibrary')[source]
                    or getLibrary(source, 'deep')
        vm.getCache('getLibraryOfGlobal')[name] = cache
        vm.getCache('getLibrary')[source] = cache
        return cache
    else
        local cache =  vm.getCache('getLibrary')[source]
                    or getLibrary(source, deep)
        if deep then
            vm.getCache('getLibrary')[source] = cache
        end
        return cache
    end
end

function vm.getLibraryName(source, deep)
    local lib = vm.getLibrary(source, deep)
    if lib then
        return lib.name
    end
    return nil
end

local globalLibraryNames = {
    'arg', 'assert', 'collectgarbage', 'dofile', '_G', 'getfenv',
    'getmetatable', 'ipairs', 'load', 'loadfile', 'loadstring',
    'module', 'next', 'pairs', 'pcall', 'print', 'rawequal',
    'rawget', 'rawlen', 'rawset', 'select', 'setfenv',
    'setmetatable', 'tonumber', 'tostring', 'type', '_VERSION',
    'warn', 'xpcall', 'require', 'unpack', 'bit32', 'coroutine',
    'debug', 'io', 'math', 'os', 'package', 'string', 'table',
    'utf8',
}
local globalLibraryNamesMap
function vm.isGlobalLibraryName(name)
    if not globalLibraryNamesMap then
        globalLibraryNamesMap = {}
        for _, v in ipairs(globalLibraryNames) do
            globalLibraryNamesMap[v] = true
        end
    end
    return globalLibraryNamesMap[name] or false
end