Skip to content

Commit

Permalink
Merge pull request #2834 from Wild-W/fix-vm-plugins
Browse files Browse the repository at this point in the history
Fix VM plugins
  • Loading branch information
sumneko authored Sep 5, 2024
2 parents 5083f1c + 36fe749 commit 08dd0ca
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 58 deletions.
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 -->
* `NEW` Custom documentation exporter
* `NEW` Setting: `Lua.docScriptPath`: Path to a script that overrides `cli.doc.export`, allowing user-specified documentation exporting.
* `FIX` Fix `VM.OnCompileFunctionParam` function in plugins
* `FIX` Lua 5.1: fix incorrect warning when using setfenv with an int as first parameter

## 3.10.5
Expand Down
52 changes: 2 additions & 50 deletions script/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@ local scope = require 'workspace.scope'
local ws = require 'workspace'
local fs = require 'bee.filesystem'

---@class pluginInterfaces
local pluginConfigs = {
-- create plugin for vm module
VM = {
OnCompileFunctionParam = function (next, func, source)
end
}
}

---@class plugin
local m = {}

Expand Down Expand Up @@ -60,14 +51,13 @@ function m.dispatch(event, uri, ...)
return failed == 0, res1, res2
end

function m.getVmPlugin(uri)
function m.getPluginInterfaces(uri)
local scp = scope.getScope(uri)
---@type pluginInterfaces
local interfaces = scp:get('pluginInterfaces')
if not interfaces then
return
end
return interfaces.VM
return interfaces
end

---@async
Expand Down Expand Up @@ -100,40 +90,6 @@ local function checkTrustLoad(scp)
return true
end

local function createMethodGroup(interfaces, key, methods)
local methodGroup = {}

for method in pairs(methods) do
local funcs = setmetatable({}, {
__call = function (t, next, ...)
if #t == 0 then
return next(...)
else
local result
for _, fn in ipairs(t) do
result = fn(next, ...)
end
return result
end
end
})
for _, interface in ipairs(interfaces) do
local func = interface[method]
if not func then
local namespace = interface[key]
if namespace then
func = namespace[method]
end
end
if func then
funcs[#funcs+1] = func
end
end
methodGroup[method] = funcs
end
return #methodGroup>0 and methodGroup or nil
end

---@param uri uri
local function initPlugin(uri)
await.call(function () ---@async
Expand Down Expand Up @@ -206,10 +162,6 @@ local function initPlugin(uri)
interfaces[#interfaces+1] = interface
end

for key, config in pairs(pluginConfigs) do
interfaces[key] = createMethodGroup(interfaces, key, config)
end

ws.resetFiles(scp)
end)
end
Expand Down
15 changes: 11 additions & 4 deletions script/vm/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1385,10 +1385,17 @@ local function compileLocal(source)
end
if source.parent.type == 'funcargs' and not hasMarkDoc and not hasMarkParam then
local func = source.parent.parent
local vmPlugin = plugin.getVmPlugin(guide.getUri(source))
local hasDocArg = vmPlugin and vmPlugin.OnCompileFunctionParam(compileFunctionParam, func, source)
or compileFunctionParam(func, source)
if not hasDocArg then
local interfaces = plugin.getPluginInterfaces(guide.getUri(source))
local hasDocArg = false
if interfaces then
for _, interface in ipairs(interfaces) do
if interface.VM then
hasDocArg = interface.VM.OnCompileFunctionParam(compileFunctionParam, func, source)
if hasDocArg then break end
end
end
end
if not hasDocArg and not compileFunctionParam(func, source) then
vm.setNode(source, vm.declareGlobal('type', 'any'))
end
end
Expand Down
6 changes: 3 additions & 3 deletions test/plugins/node/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ local function TestPlugin(script)
---@field b string
]]
---@param checker fun(state:parser.state)
return function (plugin, checker)
return function (interfaces, checker)
files.open(TESTURI)
files.setText(TESTURI, prefix .. script, true)
scope.getScope(TESTURI):set('pluginInterfaces', plugin)
scope.getScope(TESTURI):set('pluginInterfaces', interfaces)
local state = files.getState(TESTURI)
assert(state)
checker(state)
Expand All @@ -45,7 +45,7 @@ TestPlugin [[
local function t(a)
a.components:test()
end
]](myplugin, function (state)
]]({ myplugin }, function (state)
guide.eachSourceType(state.ast, 'local', function (src)
if guide.getKeyName(src) == 'a' then
local node = vm.compileNode(src)
Expand Down
2 changes: 1 addition & 1 deletion test/type_inference/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function TEST(wanted)
if wanted ~= result then
vm.getInfer(source):view(TESTURI)
end
assert(wanted == result)
assert(wanted == result, "Assertion failed! Wanted: " .. tostring(wanted) .. " Got: " .. tostring(result))
files.remove(TESTURI)
end
end
Expand Down

0 comments on commit 08dd0ca

Please sign in to comment.