Page 1 of 1

Lua 5.2 and LuaJIT resume/yield issue

Posted: Tue Jun 04, 2013 1:21 am
by desadoc
Hi,

I'm desadoc/Erivaldo from Brazil, I'm new to love2D and this is my first post here. Please forgive my english and typos ^^

Early today I wrote some code with resume/yield that didn't work. The code was +- like this:

Code: Select all

-- main.lua
function love.load()
 local co = coroutine.create(function() dofile("myscript.lua") end)
 coroutine.resume(co)
end

-- myscript.lua
coroutine.yield()
The stack trace told me the problem was an "attempt to yield across metamethod/C-call boundary...". I googled that and found that Lua 5.2 and Coco/LuaJIT supposedly support that kind of yield. I decided to try love2D with Lua 5.2 first and the error, as I expected, disappeared. Then I tried LuaJIT. All builds that I found here in the forum gave me the same error. The last test I did was to download the latest stable LuaJIT, compile it and try the following code (that runs fine with Lua 5.2 too):

Code: Select all

local function hook(event, line)
 print(debug.traceback())
 coroutine.yield()
end

local pr = assert(load("print\"hi?\""))
local cr = coroutine.create(pr)
debug.sethook(cr, hook, "clr")

print(coroutine.resume(cr))
Is LuaJIT really broken? Or is it my fault? And in that case, is there some way to write that code so it works with LuaJIT too?

Re: Lua 5.2 and LuaJIT resume/yield issue

Posted: Tue Jun 04, 2013 3:58 am
by markgo
Hi, please take a look at Lua 5.1 manual: http://www.lua.org/manual/5.1/manual.ht ... tine.yield
LuaJIT is based on Lua 5.1. I believe your problem is that coroutines in 5.1 cannot yield across a C function (dofile). If you want your code to work in 5.1, you should use loadfile to return a Lua function and pass it to coroutine.create.

Re: Lua 5.2 and LuaJIT resume/yield issue

Posted: Wed Jun 05, 2013 3:26 am
by desadoc
Thanks, markgo, that worked :)

But I noticed that although the first code works now with loadfile in Lua 5.1 or LuaJIT (or dofile only in Lua 5.2), the second code snippet I posted only works while using Lua 5.2 or LuaJIT through love2D. The stand alone interpreter of both Lua 5.2 and LuaJIT 2.0.2 crashes with that error.

Re: Lua 5.2 and LuaJIT resume/yield issue

Posted: Tue Sep 10, 2013 8:46 pm
by raidho36
I'm having the problem with yield: doing it from the hook kills the coroutine.

Code: Select all

function f ( )
 print ( "enter" )
 function hook ( )
  print ( "hooked" )
  coroutine.yield ( )
 end
 
 debug.sethook ( hook, "", 10 )
 while true do
  print ( "test" )
 end
end

local c = coroutine.create ( f )
coroutine.resume ( c )
print ( coroutine.status ( c ) )
coroutine.resume ( c )
print ( coroutine.status ( c ) )


output:
enter
test
test
hooked
dead
dead
It works the same way both in LÖVE and Lua demo on the official website, although I couldn't find any evidence of this on the net(silly me) it's the same error as in OP, but suggestions didn't worked. Additionally, reducing the snippet to the bare minimum, with only printing coroutine status after it's resumed (no C calls until after yield) gives same result. Does anyone knows how do I work around this?