lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 06/16/2014 01:26 PM, Thiago L. wrote:

On 16/06/2014 08:16, Thomas Jericke wrote:
On 06/16/2014 01:11 PM, Thiago L. wrote:

On 16/06/2014 03:14, Thomas Jericke wrote:
On 06/15/2014 04:20 PM, Thiago L. wrote:
I'm messing with coroutines and sandboxing and I need a way to yield from a debug hook. This would let my code check for interrupts on every instruction and stuff... Does anyone have a patch for doing this?


You don't need a patch to do this, at least not in Lua 5.2
--
Thomas

Uhh really? Why didn't it work then? (tested w/ lua 5.2.3)

You have to call your Lua script with lua_resume. Using lua_call or lua_pcall will give you an error when you try to yield.
--
Thomas

Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio
> co = coroutine.create(function() while true do end end) debug.sethook(co,funct ion() coroutine.yield() end, "clr", 1) print(coroutine.resume(co)) print(debug.t
raceback(co))
false   attempt to yield across a C-call boundary
stack traceback:
        [C]: in function 'yield'
        stdin:1: in function <stdin:1>
        stdin:1: in function <stdin:1>


Well, I never tried it in pure Lua, I resume the Lua script from C, and the yield is also from a C hook.

BTW I think you can write:
debug.sethook(co,coroutine.yield, "clr", 1)

No reason to wrap the yield function as long as it's the only thing you do in the hook.
--
Thomas