-->
Page 1 of 1

Calling Lua from c

PostPosted: Fri Jun 19, 2015 12:57 pm
by hbouzas
I am trying to call a lua function such as wifi.sta.getip from c. So, how do you load the module wifi and call wifi.sta.getip?
The following code is not working, lua_pcall returns 0

lua_getglobal(L, "wifi.sta.getip"); /* function to be called */
lua_pcall(L, 0,3, 0); /* should return 3 results */

Can anybody help? Thanks

Re: Calling Lua from c

PostPosted: Fri Jun 19, 2015 5:03 pm
by cal
Moin,

You are writing so little that its difficult to understand what you know and what you don't know.
If you don't know that lua does use the stack for value exchange I suggest reading

viewtopic.php?f=24&t=2526

and some of it's links.

Cal

Re: Calling Lua from c

PostPosted: Fri Jun 19, 2015 6:14 pm
by TerryE
nodeMCU is essentially a port of eLua. See my links below for more references and read the PiL chapters on this. Also look at how the callback code in the various library modules such as tmr.c do this. But remember that nodeMCU assumes that it is executed through init_user so you will need to extend Lua to add your own hooks or module and then include this type of callback mechanism. Not for a a beginning Lua developer, I think.

Re: Calling Lua from c

PostPosted: Sat Jun 20, 2015 8:55 am
by hbouzas
Sorry for being brief, I though less is more, and the question was clear how do you call a lua module and function from c. Got it, and here it is

This will call wifi.sta.getip() and return the 3 values (no error checking)

static int snaptekk_getip_test (lua_State* L )
{
lua_getglobal(L, "require");
lua_pushstring(L, "wifi");
lua_getglobal(L, "wifi");
lua_getfield(L, -1, "sta");
lua_getfield(L, -1, "getip");
lua_pcall(L, 0, 3, 0);

return 3;
}