blob: 194f78b47e2c5c741632d387fbb3ab7d0157b46e (
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
|
local vm = require 'vm.vm'
local guide = require 'parser.guide'
local util = require 'utility'
NIL = setmetatable({'<nil>'}, { __tostring = function () return 'nil' end })
--- 是否包含某种类型
function vm.hasType(source, type)
local infers = vm.getInfers(source)
for i = 1, #infers do
local infer = infers[i]
if infer.type == type then
return true
end
end
return false
end
function vm.getType(source)
local infers = vm.getInfers(source)
return guide.viewInferType(infers)
end
function vm.getLiteral(source)
local infers = vm.getInfers(source)
local literals = {}
local mark = {}
for _, infer in ipairs(infers) do
local value = infer.value
if value and not mark[value] then
mark[value] = true
literals[#literals+1] = util.viewLiteral(value)
end
end
if #literals == 0 then
return nil
end
table.sort(literals)
return table.concat(literals, '|')
end
--- 获取对象的值
--- 会尝试穿透函数调用
function vm.getInfers(source)
if not source then
return
end
local clock = os.clock()
local infers = guide.requestInfer(source, vm.interface)
if os.clock() - clock > 0.1 then
log.warn(('Request infer takes [%.3f]sec! %s %s'):format(os.clock() - clock, guide.getRoot(source).uri, util.dump(source, { deep = 1 })))
end
return infers
end
|