Skip to content

Commit

Permalink
fix(coroutine): rethrow errors from cb_to_co
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorias committed Jun 10, 2024
1 parent 5e5270d commit a11ed71
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lua/coerce/coroutine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ M.cb_to_co = function(f)
-- If we are suspended, then we f_co has yielded control after calling f.
-- Use the caller of this callback to resume computation until the next yield.
local cb_ret = { coroutine.resume(this) }
if not cb_ret[1] then
error(cb_ret[2])
end
return unpack(shift(cb_ret))
end
end, ...)
Expand Down
3 changes: 3 additions & 0 deletions neovim.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ globals:
- type: bool
- type: string
required: false
assert.has.errors:
args:
- type: function
assert.are.same:
args:
- type: any
Expand Down
22 changes: 22 additions & 0 deletions tests/coerce/coroutine_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,27 @@ describe("coerce.coroutine", function()
assert.are.same(3, f_co_ret_sum)
assert.are.same(2, f_co_ret_mul)
end)

it("rethrows errors after callback", function()
local f_cb = function() end

-- f is a function that will “execute” the callback asynchronously through f_cb.
local f = function(cb)
f_cb = function()
return cb()
end
end

local f_co = coroutine.create(function()
cco.cb_to_co(f)()
error("Catch me!")
end)

-- Resume the coroutine, which will call f and yield.
coroutine.resume(f_co)

-- Simulate the callback being called asynchronously.
assert.has.errors(f_cb)
end)
end)
end)

0 comments on commit a11ed71

Please sign in to comment.