Skip to content

Return type of getmetatable being cached and added into type infer. #3191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<!-- Add all new changes here. They will be moved under a version at release -->
* `FIX` cannot debug in Linux due to lua-debug expecting host process to have lua54 symbols available
* `FIX` support hex color codes with `#` in `textDocument/documentColor`
* `NEW` add type infer from the return type of `getmetatable`

## 3.14.0
`2025-4-7`
Expand Down
1 change: 1 addition & 0 deletions script/parser/compile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ local Specials = {
['rawset'] = true,
['rawget'] = true,
['setmetatable'] = true,
['getmetatable'] = true,
['require'] = true,
['dofile'] = true,
['loadfile'] = true,
Expand Down
64 changes: 62 additions & 2 deletions script/vm/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,21 @@ local function getReturnOfSetMetaTable(args)
local mt = args[2]
local node = vm.createNode()
if tbl then
node:merge(vm.compileNode(tbl))
local tblNode = vm.compileNode(tbl)
node:merge(tblNode)

-- 存储元表信息,将mt节点作为源节点的元表
if mt then
local mtNode = vm.compileNode(mt)
-- 为节点添加metatable属性
for n in tblNode:eachObject() do
n.metatable = mtNode
end
end
end

if mt then
-- 合并__index属性到返回节点
vm.compileByParentNodeAll(mt, '__index', function (src)
for n in vm.compileNode(src):eachObject() do
if n.type == 'global'
Expand All @@ -646,8 +658,49 @@ local function getReturnOfSetMetaTable(args)
end
end)
end

--过滤nil
node:remove 'nil'
node:remove 'nil'
return node
end

---@param args parser.object[]
---@return vm.node
local function getReturnOfGetMetaTable(args)
local obj = args[1]
local node = vm.createNode()
if not obj then
return node
end

local objNode = vm.compileNode(obj)
-- 尝试遍历对象的所有可能类型
for n in objNode:eachObject() do
-- 检查是否有metatable属性
if n.metatable then
node:merge(n.metatable)
end

-- 检查是否有setmetatable调用
if n.value and n.value.type == 'call' and n.value.node and n.value.node.special == 'setmetatable' and n.value.args and n.value.args[2] then
-- 直接返回setmetatable的第二个参数
node:merge(vm.compileNode(n.value.args[2]))
end

-- 如果是__metatable字段的处理,优先使用它
vm.compileByParentNodeAll(obj, '__metatable', function (src)
for metaObj in vm.compileNode(src):eachObject() do
node:merge(metaObj)
end
end)
end

-- 如果没有找到任何元表信息,创建一个空表类型
if node:isEmpty() then
node:addTable()
end

node:remove 'nil'
return node
end

Expand Down Expand Up @@ -1865,6 +1918,13 @@ local compilerSwitch = util.switch()
vm.setNode(source, getReturnOfSetMetaTable(args))
return
end
if func.special == 'getmetatable' then
if not args then
return
end
vm.setNode(source, getReturnOfGetMetaTable(args))
return
end
if func.special == 'pcall' and index > 1 then
if not args then
return
Expand Down
Loading