keplerproject/coxpcall — pull request #7 by Tieske (2014-01-24 22:31:34+00:00)
Title: coroutine.running replacement for transparent 5.1 use

Because the `pcall`/`xpcall` implementation uses a coroutine, the `coroutine.running` result is different inside the `pcall`. This is different behaviour from 5.1 without the `coxpcall` module.

example code;
```lua
print(_VERSION)

--pcall = require("coxpcall").pcall
--xpcall = require("coxpcall").xpcall
--coroutine.running = require("coxpcall").running

local mythread
local testprotect = function()
if mythread == coroutine.running() then
print("I'm in the same coroutine")
else
print("I'm in a different coroutine")
end
end

local function test()
pcall(testprotect)
xpcall(testprotect, function() end)
end

mythread = coroutine.create(test)
coroutine.resume(mythread)
````

When run on Lua 5.1 it will return;
````
Lua 5.1
I'm in the same coroutine
I'm in the same coroutine
````
Now uncomment the `pcall` and `xpcall` replacements and the result will be;
````
Lua 5.1
I'm in a different coroutine
I'm in a different coroutine
````
Now also uncomment the `coroutine.running` replacement and the result will be the original 5.1 behaviour again.

I ran into this when writing a coroutine scheduler that indexes a table by the threads, which didn't work when using `coxpcall`.