Skip to content

Commit d5da409

Browse files
phanenibhagwan
authored andcommitted
fix: copcall for puc lua
1 parent c2d9b86 commit d5da409

21 files changed

Lines changed: 144 additions & 27 deletions

lua/fzf-lua/core.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ M.fzf_wrap = function(cmd, opts, convert_actions)
251251
coroutine.wrap(function()
252252
_co = coroutine.running()
253253
-- xpcall to get full traceback https://www.lua.org/pil/8.5.html
254-
local _, err = xpcall(function()
254+
local _, err = (jit and xpcall or require("fzf-lua.lib.copcall").xpcall)(function()
255255
if type(opts.cb_co) == "function" then opts.cb_co(_co) end
256256
local selected, exit_code = M.fzf(cmd, opts)
257257
-- If aborted (e.g. unhide process kill), do nothing

lua/fzf-lua/lib/copcall.lua

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---@diagnostic disable
2+
-------------------------------------------------------------------------------
3+
-- (Not needed for LuaJIT or Lua 5.2+)
4+
--
5+
-- Coroutine safe xpcall and pcall versions
6+
--
7+
-- https://keplerproject.github.io/coxpcall/
8+
--
9+
-- Encapsulates the protected calls with a coroutine based loop, so errors can
10+
-- be dealed without the usual Lua 5.x pcall/xpcall issues with coroutines
11+
-- yielding inside the call to pcall or xpcall.
12+
--
13+
-- Authors: Roberto Ierusalimschy and Andre Carregal
14+
-- Contributors: Thomas Harning Jr., Ignacio Burgueño, Fabio Mascarenhas
15+
--
16+
-- Copyright 2005 - Kepler Project
17+
--
18+
-- $Id: coxpcall.lua,v 1.13 2008/05/19 19:20:02 mascarenhas Exp $
19+
-------------------------------------------------------------------------------
20+
21+
-------------------------------------------------------------------------------
22+
-- Checks if (x)pcall function is coroutine safe
23+
-------------------------------------------------------------------------------
24+
local function isCoroutineSafe(func)
25+
local co = coroutine.create(function()
26+
return func(coroutine.yield, function() end)
27+
end)
28+
29+
coroutine.resume(co)
30+
return coroutine.resume(co)
31+
end
32+
33+
-- No need to do anything if pcall and xpcall are already safe.
34+
if isCoroutineSafe(pcall) and isCoroutineSafe(xpcall) then
35+
_G.copcall = pcall
36+
_G.coxpcall = xpcall
37+
return { pcall = pcall, xpcall = xpcall, running = coroutine.running }
38+
end
39+
40+
-------------------------------------------------------------------------------
41+
-- Implements xpcall with coroutines
42+
-------------------------------------------------------------------------------
43+
---@diagnostic disable-next-line
44+
local performResume
45+
local oldpcall, oldxpcall = pcall, xpcall
46+
local pack = table.pack or function(...) return {n = select("#", ...), ...} end
47+
local unpack = table.unpack or unpack
48+
local running = coroutine.running
49+
--- @type table<thread,thread>
50+
local coromap = setmetatable({}, { __mode = "k" })
51+
52+
local function handleReturnValue(err, co, status, ...)
53+
if not status then
54+
return false, err(debug.traceback(co, (...)), ...)
55+
end
56+
if coroutine.status(co) == 'suspended' then
57+
return performResume(err, co, coroutine.yield(...))
58+
else
59+
return true, ...
60+
end
61+
end
62+
63+
function performResume(err, co, ...)
64+
return handleReturnValue(err, co, coroutine.resume(co, ...))
65+
end
66+
67+
--- @diagnostic disable-next-line: unused-vararg
68+
local function id(trace, ...)
69+
return trace
70+
end
71+
72+
function _G.coxpcall(f, err, ...)
73+
local current = running()
74+
if not current then
75+
if err == id then
76+
return oldpcall(f, ...)
77+
else
78+
if select("#", ...) > 0 then
79+
local oldf, params = f, pack(...)
80+
f = function() return oldf(unpack(params, 1, params.n)) end
81+
end
82+
return oldxpcall(f, err)
83+
end
84+
else
85+
local res, co = oldpcall(coroutine.create, f)
86+
if not res then
87+
local newf = function(...) return f(...) end
88+
co = coroutine.create(newf)
89+
end
90+
coromap[co] = current
91+
return performResume(err, co, ...)
92+
end
93+
end
94+
95+
--- @param coro? thread
96+
local function corunning(coro)
97+
if coro ~= nil then
98+
assert(type(coro)=="thread", "Bad argument; expected thread, got: "..type(coro))
99+
else
100+
coro = running()
101+
end
102+
while coromap[coro] do
103+
coro = coromap[coro]
104+
end
105+
if coro == "mainthread" then return nil end
106+
return coro
107+
end
108+
109+
-------------------------------------------------------------------------------
110+
-- Implements pcall with coroutines
111+
-------------------------------------------------------------------------------
112+
113+
function _G.copcall(f, ...)
114+
return coxpcall(f, id, ...)
115+
end
116+
117+
return { pcall = copcall, xpcall = coxpcall, running = corunning }

tests/screenshots/tests-api_spec.lua---api---fzf_exec---rg---1-+-args-{-1-}

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
01|
33
02|~
44
03|~ ╭─────────────────────────────────────────────────╮
5-
04|~ │> 85/85
5+
04|~ │> 86/86
66
05|~ │──────────────────────────────────────────────── │
77
06|~ │▌ LICENSE ││
88
07|~ │ Makefile ││
9-
08|~ │ OPTIONS.md
9+
08|~ │ OPTIONS.md
1010
09|~ │ README-Win.md │
1111
10|~ │ README.md │
1212
11|~ │ autoload/fzf_lua.vim │

tests/screenshots/tests-api_spec.lua---api---fzf_exec---rg---1-+-args-{-false-}

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
01|
33
02|~
44
03|~ ╭─────────────────────────────────────────────────╮
5-
04|~ │> 86/86
5+
04|~ │> 87/87
66
05|~ │──────────────────────────────────────────────── │
77
06|~ │▌ [DEBUG] [st] rg --files -g !.git -g !tests/*··││
88
07|~ │ LICENSE ││

tests/screenshots/tests-api_spec.lua---api---fzf_exec---rg---1-+-args-{-true-}

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
01|
33
02|~
44
03|~ ╭─────────────────────────────────────────────────╮
5-
04|~ │> 86/86
5+
04|~ │> 87/87
66
05|~ │──────────────────────────────────────────────── │
77
06|~ │▌ [DEBUG] [mt] rg --files -g !.git -g !tests/*··││
88
07|~ │ LICENSE ││

tests/screenshots/tests-files_spec.lua---files---close-abort---1-+-args-{-'-c-c-'-}

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
01|
33
02|~
44
03|~ ╭───────────────────── Files ─────────────────────╮
5-
04|~ │> 86/86 (0) │
5+
04|~ │> 87/87 (0) │
66
05|~ │──────────────────────────────────────────────── │
77
06|~ │▌ [DEBUG] [mt] rg --files --sort=path -g !test··││
88
07|~ │ LICENSE ││

tests/screenshots/tests-files_spec.lua---files---close-abort---1-+-args-{-'-esc-'-}

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
01|
33
02|~
44
03|~ ╭───────────────────── Files ─────────────────────╮
5-
04|~ │> 86/86 (0) │
5+
04|~ │> 87/87 (0) │
66
05|~ │──────────────────────────────────────────────── │
77
06|~ │▌ [DEBUG] [mt] rg --files --sort=path -g !test··││
88
07|~ │ LICENSE ││

tests/screenshots/tests-files_spec.lua---files---executable---1-+-args-{-'fd'-}

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
01|
33
02|~
44
03|~ ╭─────────────────── Files h ────────────────────╮
5-
04|~ │> 98/98 (0) │
5+
04|~ │> 99/99 (0) │
66
05|~ │──────────────────────────────────────────────── │
77
06|~ │▌ [DEBUG] [mt] fd --hidden --color=never --typ··││
88
07|~ │ .editorconfig ││

tests/screenshots/tests-files_spec.lua---files---executable---1-+-args-{-'find-dir'-}

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
01|
33
02|~
44
03|~ ╭───────────────────── Files ─────────────────────╮
5-
04|~ │> 98/98 (0) │
5+
04|~ │> 99/99 (0) │
66
05|~ │──────────────────────────────────────────────── │
77
06|~ │▌ [DEBUG] [mt] find . -type f \! -path '*/.git··││
88
07|~ │ .editorconfig ││

tests/screenshots/tests-files_spec.lua---files---executable---1-+-args-{-'rg'-}

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
01|
33
02|~
44
03|~ ╭─────────────────── Files h ────────────────────╮
5-
04|~ │> 98/98 (0) │
5+
04|~ │> 99/99 (0) │
66
05|~ │──────────────────────────────────────────────── │
77
06|~ │▌ [DEBUG] [mt] rg --hidden --files -g "!.git" ··││
88
07|~ │ .editorconfig ││

0 commit comments

Comments
 (0)