🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

call a lua function from c

Started by
3 comments, last by trolocsis 20 years ago
Hi, for some time I'm trying to find out if it is possible to execute a function at the lua stack from C++? For example:

-- LUA Stack L
function foo( str1 )
   print( "My String was " .. str1)
end
This function can be executed at the LUA Stack. But can I somehow execute this function out of C++? I see that it could be a bit problematic cause C doesn't know the name of the function. But is there a way for something like this (pseudo): lua_executeAtStack(L, "foo", "test") thanx alot
Advertisement
You may be able to use lua_load for this; just pass in the Lua code that you want to execute (eg. "foo(test)"). I think you then have to call another function to execute the top of the stack.
If you use LuaLoad and load your file with the lua_functions, then you can use something like this, which I use in all of my lua/C/C++ interfaces:

#define luafunc0(_NAME, R) R lua##_NAME(){	R ret;	lua_getglobal(L, #_NAME);	lua_call(L, 0, 1);	ret = (R)lua_tonumber(L, -1);	lua_pop(L, 1);	return ret;}#define luafunc1(_NAME, R, T1) R lua##_NAME(T1 a1){	R ret;	lua_getglobal(L, #_NAME);	lua_pushnumber(L, a1);	lua_call(L, 1, 1);	ret = (R)lua_tonumber(L, -1);	lua_pop(L, 1);	return ret;}#define luafunc2(_NAME, R, T1, T2) R lua##_NAME(T1 a1, T2 a2){	R ret;	lua_getglobal(L, #_NAME);	lua_pushnumber(L, a1);	lua_pushnumber(L, a2);	lua_call(L, 2, 1);	ret = (R)lua_tonumber(L, -1);	lua_pop(L, 1);	return ret;}...#define luafunc0v(_NAME) void lua##_NAME(){	lua_getglobal(L, #_NAME);	lua_call(L, 0, 0);	return;}#define luafunc1v(_NAME, T1) void lua##_NAME(T1 a1){	lua_getglobal(L, #_NAME);	lua_pushnumber(L, a1);	lua_call(L, 1, 0);	return;}#define luafunc2v(_NAME, T1, T2) void lua##_NAME(T1 a1, T2 a2){	lua_getglobal(L, #_NAME);	lua_pushnumber(L, a1);	lua_pushnumber(L, a2);	lua_call(L, 2, 1);	return;}...


I think I have the defines going all of the way up to 16 arguments or something like that. To use the defines, do something like this:

-- lua codefunction add ( x, y )	return x + yendfunction fn ( )	print( "Inside fn()" )end// C/C++ Codeluafunc2(add, int, int, int)luafunc0v(fn)cout << "2 + 3 = " << luaadd(2, 3) << endl;luafn();


If you would rather do it by hand and create a wrapper function which takes variable # of arguments along with some info about the calling function like it's name, the number of arguments it takes, and if there is a return value (lua is OK with multi returning values, but C isn't so you dont have to worry about it)

Hopefully those examples made some sence and clearified how C can interface to the lua functions provided in lualib.

Hope I've helped!

Dwiel

EDIT: Make sure you put the backslashes in for the defines if you use my #defines, they messed up the formatting so I removed them
lua_dostring does just that.

Do lua_dostring( L, "foo( 'test' )" );

Requires to have the lua instance L to parse the function before (duh!).

You can also do it via the stack, but i don't know the sequence how to put the params on the stack off my head. lua_pushstring the params, then lua_pushstring the function name and then call lua_call (spelling?). I might have messed up this second possibility, i usually use the first one.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

The Lua Book has a really nice example on how to do this with va_args.

This topic is closed to new replies.

Advertisement