Attempt to yield across C-call boundary
Posted: Fri Jun 02, 2017 7:35 am
I'm trying to make a presentation about Lua in Lua and I picked Löve for doing this.
The idea is simple (and I have done that in the past):
1) Loadstring(code)
2) create coroutine
3) set debug hook on *that* coroutine
4) step through the code by yielding in the hook method
The requirement for doing this is having coco or LuaJIT (which has coco built in).
Löve comes with LuaJIT, so I didn't expect this not to work, but it doesn't - for several reasons.
Firstly, I looks like to me that Löve tries to make debugging easier and simply hooks the debug.sethook function into every coroutine so the debugging code will get called just everywhere (in native Lua you have to deal with it - and while it's complicated, you can deal with this).
While this is certainly convenient, it's a bit nasty if you specifically just want to set the hook on one particular coroutine. If you want to use coroutines for simulating agents, you currently can't just debug one single agent - the entire engine will be subject of debugging.
The next thing is the yielding: I am puzzled why this doesn't work. LuaJIT can do this. I assume coroutine.create passes a -1 to the wrapped LuaJIT method to signal it shouldn't use a C-Stack?
Are there ways how to deal with that? The Löve.Thread API doesn't look I want to use it for this, it would make things a good deal more complex.
The idea is simple (and I have done that in the past):
1) Loadstring(code)
2) create coroutine
3) set debug hook on *that* coroutine
4) step through the code by yielding in the hook method
The requirement for doing this is having coco or LuaJIT (which has coco built in).
Löve comes with LuaJIT, so I didn't expect this not to work, but it doesn't - for several reasons.
Firstly, I looks like to me that Löve tries to make debugging easier and simply hooks the debug.sethook function into every coroutine so the debugging code will get called just everywhere (in native Lua you have to deal with it - and while it's complicated, you can deal with this).
While this is certainly convenient, it's a bit nasty if you specifically just want to set the hook on one particular coroutine. If you want to use coroutines for simulating agents, you currently can't just debug one single agent - the entire engine will be subject of debugging.
The next thing is the yielding: I am puzzled why this doesn't work. LuaJIT can do this. I assume coroutine.create passes a -1 to the wrapped LuaJIT method to signal it shouldn't use a C-Stack?
Are there ways how to deal with that? The Löve.Thread API doesn't look I want to use it for this, it would make things a good deal more complex.