Lua 5.2 and LuaJIT resume/yield issue

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
desadoc
Prole
Posts: 4
Joined: Tue Jun 04, 2013 12:17 am

Lua 5.2 and LuaJIT resume/yield issue

Post 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?
User avatar
markgo
Party member
Posts: 190
Joined: Sat Jan 05, 2013 12:21 am
Location: USA

Re: Lua 5.2 and LuaJIT resume/yield issue

Post 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.
User avatar
desadoc
Prole
Posts: 4
Joined: Tue Jun 04, 2013 12:17 am

Re: Lua 5.2 and LuaJIT resume/yield issue

Post 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.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Lua 5.2 and LuaJIT resume/yield issue

Post 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?
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot] and 9 guests