Interrupting the Lua VM from C (Lua 3.1)

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Interrupting the Lua VM from C (Lua 3.1)

Cyril Zorin
Hullo!

Say I have two functions, one Lua function called MyLuaFunc, and a C  
function called my_c_func(). Now, suppose that MyLuaFunc calls  
my_c_func() -- is there a way, from inside my_c_func() to make it so  
the Lua VM returns from MyLuaFunc right after my_c_func() is done? In  
other words

-- some code
my_c_func()
-- I want my_c_func() to tell the Lua VM to return from MyLuaFunc  
right after my_c_func() returns
-- more code

Is there a way to do this?

Let me know if the description needs more clarification.

Thanks,
Cyril.
Reply | Threaded
Open this post in threaded view
|

Re: Interrupting the Lua VM from C (Lua 3.1)

David Jones-2

On May 21, 2006, at 19:52, Cyril Zorin wrote:

> Hullo!
>
> Say I have two functions, one Lua function called MyLuaFunc, and a C
> function called my_c_func(). Now, suppose that MyLuaFunc calls
> my_c_func() -- is there a way, from inside my_c_func() to make it so
> the Lua VM returns from MyLuaFunc right after my_c_func() is done? In
> other words
>
> -- some code
> my_c_func()
> -- I want my_c_func() to tell the Lua VM to return from MyLuaFunc
> right after my_c_func() returns
> -- more code
>
> Is there a way to do this?

Use setjmp in MyLuaFunc and longjmp in _c_func.  Can't say I recommend
it though.  It would be simpler to just have _c_func return some value
to MyLuaFunc that indicated that it wanted an immediate return to Lua.

David Jones

Reply | Threaded
Open this post in threaded view
|

Re: Interrupting the Lua VM from C (Lua 3.1)

David Jones-2
In reply to this post by Cyril Zorin

On May 21, 2006, at 19:52, Cyril Zorin wrote:

> Hullo!
>
> Say I have two functions, one Lua function called MyLuaFunc, and a C
> function called my_c_func(). Now, suppose that MyLuaFunc calls
> my_c_func() -- is there a way, from inside my_c_func() to make it so
> the Lua VM returns from MyLuaFunc right after my_c_func() is done? In
> other words
>
> -- some code
> my_c_func()
> -- I want my_c_func() to tell the Lua VM to return from MyLuaFunc
> right after my_c_func() returns
> -- more code
>
> Is there a way to do this?

Sorry, just realised my previous reply was horrible confused and wrong.
  I don't knowing anything about Lua 3.1 (it was 3.2 when I first
started using Lua) but it's not the sort of thing that became possible
until coroutines (Lua 5.0).

drj

Reply | Threaded
Open this post in threaded view
|

Re: Interrupting the Lua VM from C (Lua 3.1)

Cyril Zorin
In reply to this post by David Jones-2

On 21-May-06, at 4:16 PM, David Jones wrote:

>
> On May 21, 2006, at 19:52, Cyril Zorin wrote:
>
>> Hullo!
>>
>> Say I have two functions, one Lua function called MyLuaFunc, and a  
>> C function called my_c_func(). Now, suppose that MyLuaFunc calls  
>> my_c_func() -- is there a way, from inside my_c_func() to make it  
>> so the Lua VM returns from MyLuaFunc right after my_c_func() is  
>> done? In other words
>>
>> -- some code
>> my_c_func()
>> -- I want my_c_func() to tell the Lua VM to return from MyLuaFunc  
>> right after my_c_func() returns
>> -- more code
>>
>> Is there a way to do this?
>
> Use setjmp in MyLuaFunc and longjmp in _c_func.  Can't say I  
> recommend it though.  It would be simpler to just have _c_func  
> return some value to MyLuaFunc that indicated that it wanted an  
> immediate return to Lua.

Unfortunately I have no control over the Lua code. I'm just  
implementing the C stubs, and there's a stub that requires that a Lua  
function returns after the C stub returns.

>
> David Jones
>

Reply | Threaded
Open this post in threaded view
|

Re: Interrupting the Lua VM from C (Lua 3.1)

Cyril Zorin
In reply to this post by David Jones-2
On 5/21/06, David Jones <[hidden email]> wrote:

On May 21, 2006, at 19:52, Cyril Zorin wrote:

> Hullo!
>
> Say I have two functions, one Lua function called MyLuaFunc, and a C
> function called my_c_func(). Now, suppose that MyLuaFunc calls
> my_c_func() -- is there a way, from inside my_c_func() to make it so
> the Lua VM returns from MyLuaFunc right after my_c_func() is done? In
> other words
>
> -- some code
> my_c_func()
> -- I want my_c_func() to tell the Lua VM to return from MyLuaFunc
> right after my_c_func() returns
> -- more code
>
> Is there a way to do this?

Sorry, just realised my previous reply was horrible confused and wrong.
  I don't knowing anything about Lua 3.1 (it was 3.2 when I first
started using Lua) but it's not the sort of thing that became possible
until coroutines (Lua 5.0).

No problem.

So, anyone have some thoughts? Right now I think the "easiest" (heh!) way of doing this is to instrument the Lua function's bytecode at runtime (from the C function), and insert a "RETCODE" or equivalent in the appropriate spot, although I'm not espcially happy about this.

Any suggestions?

drj