Skip to content

Commit

Permalink
fix some bugs in C module wrt metatables, wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
mascarenhas committed Jul 9, 2016
1 parent 1478e04 commit 2a42012
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
6 changes: 1 addition & 5 deletions src/taggedcoro.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,6 @@ static int taggedcoro_cowrap (lua_State *L) {
return luaL_error(L, "attempt to wrap an untagged coroutine");
} else lua_pop(L, 1);
lua_pushvalue(L, 1);
lua_createtable(L, 4, 0); /* meta = { <tag>, <stacked>, <parent>, <yielder> } */
lua_pushvalue(L, 1); /* copy tag to top */
lua_rawseti(L, -2, 1); /* meta[1] = tag */
lua_rawset(L, lua_upvalueindex(1)); /* coroset[co] = meta */
} else taggedcoro_cocreate(L);
lua_pushvalue(L, -1);
lua_pushvalue(L, lua_upvalueindex(1));
Expand Down Expand Up @@ -641,8 +637,8 @@ static const luaL_Reg mt_funcs[] = {
static int taggedcoro_install(lua_State *L) {
lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD);
lua_createtable(L, 0, 2);
lua_pushvalue(L, lua_upvalueindex(1)); /* extra metadada for each coroutine */
luaL_newlibtable(L, mt_funcs); /* __index */
lua_pushvalue(L, lua_upvalueindex(1)); /* extra metadada for each coroutine */
luaL_setfuncs(L, mt_funcs, 1);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, taggedcoro_cocall); /* __call */
Expand Down
7 changes: 6 additions & 1 deletion src/taggedcoro.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ function callk(co, meta, ok, ...)
end
elseif meta.tag ~= tag then
if not isyieldable() then
return callk(co, meta, resume(co, MARK, "coroutine for tag " .. tostring(tag) .. " not found"))
local _, ismain = running()
if ismain then
return callk(co, meta, resume(co, MARK, "coroutine for tag " .. tostring(tag) .. " not found"))
else
return callk(co, meta, resume(co, MARK, "attempt to yield across a C-call boundary"))
end
elseif coros[running()] then -- parent is tagged, pass it along
meta.stacked = true
return callkk(co, meta, pcall(yield, ...))
Expand Down
31 changes: 31 additions & 0 deletions test/traceback.lua
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,37 @@ do
zero = oldzero
end

do
usetc = false
local oldzero, cozero = zero
zero, cozero = tc.wrap("zero", zero)
local ctwo = coroutine.wrap(two)
local ok, tb = xpcall(ctwo, function (msg)
return tc.traceback(msg, 1)
end)
assert(ok)
assert(tb == "zero")
zero = oldzero
end

do
local oldzero = zero
zero = function ()
tc.yield("two", "zero")
end
local cone = tc.wrap("one", one)
local ctwo = tc.wrap("two", function ()
return xpcall(function () assert(false) end, function (msg)
local ok, err = pcall(cone)
assert(not ok)
return err
end)
end)
local _, err = ctwo()
assert(err:match("attempt to yield"))
zero = oldzero
end

if _VERSION ~= "Lua 5.1" then
tc = tc.install()

Expand Down

0 comments on commit 2a42012

Please sign in to comment.