summaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
Diffstat (limited to 'script')
-rw-r--r--script/vm/compiler.lua3
-rw-r--r--script/vm/generic.lua40
-rw-r--r--script/vm/infer.lua4
-rw-r--r--script/vm/local-manager.lua2
-rw-r--r--script/vm/node.lua12
-rw-r--r--script/vm/type.lua4
-rw-r--r--script/vm/union.lua12
7 files changed, 41 insertions, 36 deletions
diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua
index c74d2780..2393544c 100644
--- a/script/vm/compiler.lua
+++ b/script/vm/compiler.lua
@@ -370,7 +370,7 @@ local function getReturn(func, index, args)
return m.compileNode(ast)
end
local node = m.compileNode(func)
- ---@type vm.node.union
+ ---@type vm.union
local result
if node then
for cnode in nodeMgr.eachObject(node) do
@@ -1519,6 +1519,7 @@ function m.resumeCache()
end
---@param source vm.object
+---@param uri? uri
---@return vm.node
function m.compileNode(source, uri)
if not source then
diff --git a/script/vm/generic.lua b/script/vm/generic.lua
index d2627388..b9e01efd 100644
--- a/script/vm/generic.lua
+++ b/script/vm/generic.lua
@@ -13,17 +13,22 @@ mt.type = 'generic'
---@param source parser.object
---@param resolved? table<string, vm.node>
----@return vm.node
+---@return parser.object | vm.union
local function cloneObject(source, resolved)
if not resolved then
return source
end
if source.type == 'doc.generic.name' then
local key = source[1]
- if not resolved[key] then
- return source
- end
- return resolved[key] or source
+ local newName = {
+ type = source.type,
+ start = source.start,
+ finish = source.finish,
+ parent = source.parent,
+ [1] = source[1],
+ }
+ nodeMgr.setNode(newName, resolved[key], true)
+ return newName
end
if source.type == 'doc.type' then
local newType = {
@@ -35,7 +40,6 @@ local function cloneObject(source, resolved)
}
for i, typeUnit in ipairs(source.types) do
local newObj = cloneObject(typeUnit, resolved)
- newObj.parent = newType
newType.types[i] = newObj
end
return newType
@@ -49,8 +53,6 @@ local function cloneObject(source, resolved)
name = source.name,
extends = cloneObject(source.extends, resolved)
}
- newArg.name.parent = newArg
- newArg.extends.parent = newArg
return newArg
end
if source.type == 'doc.type.array' then
@@ -61,7 +63,6 @@ local function cloneObject(source, resolved)
parent = source.parent,
node = cloneObject(source.node, resolved),
}
- newArray.node.parent = newArray
return newArray
end
if source.type == 'doc.type.table' then
@@ -81,8 +82,6 @@ local function cloneObject(source, resolved)
name = cloneObject(field.name, resolved),
extends = cloneObject(field.extends, resolved),
}
- newField.name.parent = newField
- newField.extends.parent = newField
newTable.fields[i] = newField
end
return newTable
@@ -97,15 +96,18 @@ local function cloneObject(source, resolved)
returns = {},
}
for i, arg in ipairs(source.args) do
- local newObj = cloneObject(arg, resolved)
- newObj.parent = newDocFunc
- newObj.optional = arg.optional
+ local newObj = cloneObject(arg, resolved)
+ if arg.optional and newObj.type == 'vm.union' then
+ newObj:addOptional()
+ end
newDocFunc.args[i] = newObj
end
for i, ret in ipairs(source.returns) do
- local newObj = cloneObject(ret, resolved)
- newObj.parent = newDocFunc
- newObj.optional = ret.optional
+ local newObj = cloneObject(ret, resolved)
+ newObj.parent = newDocFunc
+ if ret.optional and newObj.type == 'vm.union' then
+ newObj:addOptional()
+ end
newDocFunc.returns[i] = cloneObject(ret, resolved)
end
return newDocFunc
@@ -117,10 +119,12 @@ end
---@param args parser.object
---@return parser.object
function mt:resolve(uri, args)
+ local compiler = require 'vm.compiler'
local resolved = self.sign:resolve(uri, args)
local result = union()
for nd in nodeMgr.eachObject(self.proto) do
- result:merge(cloneObject(nd, resolved))
+ local clonedNode = compiler.compileNode(cloneObject(nd, resolved))
+ result:merge(clonedNode)
end
return result
end
diff --git a/script/vm/infer.lua b/script/vm/infer.lua
index 7f7dbb8e..1cb57e4c 100644
--- a/script/vm/infer.lua
+++ b/script/vm/infer.lua
@@ -164,14 +164,14 @@ function m.getInfer(source)
return m.NULL
end
-- TODO: more cache?
- if node.type == 'union' and node.lastInfer then
+ if node.type == 'vm.union' and node.lastInfer then
return node.lastInfer
end
local infer = setmetatable({
node = node,
uri = guide.getUri(source),
}, mt)
- if node.type == 'union' then
+ if node.type == 'vm.union' then
node.lastInfer = infer
end
diff --git a/script/vm/local-manager.lua b/script/vm/local-manager.lua
index 318dd4bc..9ffee344 100644
--- a/script/vm/local-manager.lua
+++ b/script/vm/local-manager.lua
@@ -29,7 +29,7 @@ function m.subscribeLocal(source, node)
if not node then
return
end
- if node.type == 'union' then
+ if node.type == 'vm.union' then
node:subscribeLocal(source)
return
end
diff --git a/script/vm/node.lua b/script/vm/node.lua
index eb1ee69d..12e2fd53 100644
--- a/script/vm/node.lua
+++ b/script/vm/node.lua
@@ -1,7 +1,7 @@
local union = require 'vm.union'
local files = require 'files'
----@alias vm.node vm.node.union
+---@alias vm.node vm.union
---@alias vm.object parser.object | vm.global | vm.generic
---@class vm.node-manager
@@ -37,7 +37,7 @@ function m.setNode(source, node, cover)
end
local me = m.nodeCache[source]
if not me then
- if node.type == 'union' then
+ if node.type == 'vm.union' then
m.nodeCache[source] = node
else
m.nodeCache[source] = union(node)
@@ -55,7 +55,7 @@ end
---@param node vm.node?
---@return vm.node
function m.addOptional(node)
- if not node or node.type ~= 'union' then
+ if not node or node.type ~= 'vm.union' then
node = union(node)
end
node = node:addOptional()
@@ -63,12 +63,12 @@ function m.addOptional(node)
end
---@param node vm.node?
----@return vm.node.union?
+---@return vm.union?
function m.removeOptional(node)
if not node then
return node
end
- if node.type ~= 'union' then
+ if node.type ~= 'vm.union' then
node = union(node)
end
node = node:removeOptional()
@@ -80,7 +80,7 @@ function m.eachObject(node)
if not node then
return DUMMY_FUNCTION
end
- if node.type == 'union' then
+ if node.type == 'vm.union' then
return node:eachNode()
end
local first = true
diff --git a/script/vm/type.lua b/script/vm/type.lua
index 56964df8..3124b03e 100644
--- a/script/vm/type.lua
+++ b/script/vm/type.lua
@@ -74,7 +74,7 @@ end
---@param uri uri
---@param tnode vm.node
---@param knode vm.node
----@return vm.node.union?
+---@return vm.union?
function vm.getTableValue(uri, tnode, knode)
local result = union()
for tn in nodeMgr.eachObject(tnode) do
@@ -115,7 +115,7 @@ end
---@param uri uri
---@param tnode vm.node
---@param vnode vm.node
----@return vm.node.union?
+---@return vm.union?
function vm.getTableKey(uri, tnode, vnode)
local result = union()
for tn in nodeMgr.eachObject(tnode) do
diff --git a/script/vm/union.lua b/script/vm/union.lua
index 5be52de9..f816952a 100644
--- a/script/vm/union.lua
+++ b/script/vm/union.lua
@@ -1,15 +1,15 @@
local localMgr = require 'vm.local-manager'
----@class vm.node.union
+---@class vm.union
local mt = {}
mt.__index = mt
-mt.type = 'union'
+mt.type = 'vm.union'
mt.optional = nil
mt.lastInfer = nil
---@param me vm.node
---@param node vm.node
----@return vm.node.union
+---@return vm.union
local function createUnion(me, node)
local union = setmetatable({}, mt)
union:merge(me)
@@ -22,7 +22,7 @@ function mt:merge(node)
if not node then
return
end
- if node.type == 'union' then
+ if node.type == 'vm.union' then
for _, c in ipairs(node) do
if not self[c] then
self[c] = true
@@ -65,7 +65,7 @@ function mt:eachNode()
end
end
----@return vm.node.union
+---@return vm.union
function mt:addOptional()
if self:isOptional() then
return self
@@ -74,7 +74,7 @@ function mt:addOptional()
return self
end
----@return vm.node.union
+---@return vm.union
function mt:removeOptional()
self.optional = nil
if not self:isOptional() then