[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Calling C functions from Lua Code
- From: Tim Hill <drtimhill@...>
- Date: Fri, 4 Jul 2014 12:34:32 -0700
On Jul 4, 2014, at 10:05 AM, Austin Einter <firstname.lastname@example.org> wrote:
> Hi All
> I have a requirement as below.
> 1. From C code, call lua function ( I know how to do it)
> 2. While lua function is being executed, call a C function
> 3. Lua function returns
> 4. C code completes and program terminates
> Hope it should be possible.
> Can somebody please help me how to implement above point 2 (how to call a C function from Lua).
The basic API is lua_pushcfunction(), which given a pointer to a C function, will push a Lua value that represents that function. This value can then be called from Lua, in the same way that a Lua function is a value that can be called. Typically, you will store this value in a global variable before you call the Lua function, so that it can be accessed from Lua code.
When you call the C function, arguments are on the Lua stack, and you can return values to Lua via the same stack.