How to pass the lua function to the C function and perform Lua functions multiple times?

What I want to do is create a function that will iterate some objects and call a function for each function. I am using BlitzMax, not C, but other than that , Because it has a complete Lua C function wrapper. Lua has a lua_pushcfunction() command, but where is its lua_pushfunction() command? It is very easy to call a function with a name, but how to call a function passed as a parameter?

It’s like:

ForEach( PlanetList, function (planet)
if(planet.exists == true) then
Planet_Count = Planet_Count + 1
end
end )

Usually you just say “lua_getglobal(L,name)” and it puts the lua function nicely on the stack On, but how to get it from a parameter?

Edit

I went back and actually tried to use luaL_ref() from this question I found earlier. What I am doing is using luaL_ref() to pop the function value from the top of the stack and put Put it into a temporary register, I use the value returned from luaL_ref () to use lua_rawgeti() for each item in the list. Then use luaL_unref() to release the register after the list is complete.

Because I am a newbie to Lua, I have the same problem. Because, in my opinion, there is no satisfactory answer , I think I will write one, even if this question may never be closed. Hope this will help others in this situation.

main.c

#include 
#include
#include

/* this keeps our Lua reference to the Lua function */
int callback_reference = 0;

/* this is called by Lua to register its function */
int lua_registerCallback( lua_State *L) {

/* store the reference to the Lua function in a variable to be used later */
callback_reference = luaL_ref( L, LUA_REGISTRYINDEX );

return 0;
}

/* calls our Lua callback function and resets the callback reference */
void call_callback( lua_State *L) {

/* push the callback on to the stack using the Lua reference we */
/* stored in the registry */
lua_rawgeti( L, LUA_REGISTRYINDEX, callback_reference );

/* duplicate the value on the stack */
/* NOTE: This is unnecessary, but it shows how you keep the */
/* callback for later */
lua_pushvalue( L, 1 );

/* call the callback */
/* NOTE: This is using the one we duplicated with lua_pushvalue */
if (0 != lua_pcall( L, 0, 0, 0)) {
printf("Failed to call the callback! %s ", lua_tostring( L, -1) );
return;
}

/* get a new reference to the Lua function and store it again */
/* NOTE: This is only used in conjunction with the lua_pushvalue */
/* above and can be removed if you remove that */
callback_reference = luaL_ref( L, LUA_REGISTRYINDEX );
}

int main( void) {

/* set up Lua */< br /> lua_State *L = lua_open();
luaL_openlibs( L );

/* register the lua_registerCallback function as */
/* "RegisterCallback" so it can be called by Lua */
lua_pushcfunction( L, lua_registerCallback );
lua_setglobal( L, "RegisterCallback ");

/* run our Lua file */
if (0 != luaL_dofile( L, "callback.lua")) {
printf("Failed to load calback.lua! %s",
lua_tostring( L, -1) );
lua_close( L );
return 1;
}

/* call the callback */
call_callback( L );

/* call the callback again if you want (because we restored */
/* the Lua function reference) */
call_callback( L );

/* remove the reference to the callback */
/* NOTE: This is also unnecessary if you didn't re- add the */
/* function to the registry */
luaL_unref( L, LUA_REGISTRYINDEX, callback_reference );

/* uninitialize Lua */
lua_close( L );

return 0;
}

callback.lua

function MyCallback()
print("Hello World!")
end

RegisterCallback( MyCallback )

What I want to do is create a function that will iterate some objects and call a function for each function. I am using BlitzMax, not C, but other than that, because it There is a complete Lua C function wrapper. Lua has a lua_pushcfunction() command, but where is its lua_pushfunction() command? It is very easy to call a function with a name, but how to call a function passed as a parameter?

It’s like:

ForEach( PlanetList, function (planet)
if(planet.exists == true) then
Planet_Count = Planet_Count + 1
end
end )

Usually you just say “lua_getglobal(L,name)” and it puts the lua function nicely on the stack On, but how to get it from a parameter?

Edit

I went back and actually tried to use luaL_ref() from this question I found earlier. What I am doing is using luaL_ref() to pop the function value from the top of the stack and put Put it into a temporary register, I use the value returned from luaL_ref () to use lua_rawgeti() for each item in the list. Then use luaL_unref() to release the register after the list is complete.

Because I am new to Lua myself, I have the same problem. Because, in my opinion, there is no satisfactory answer, I think I will write one, even if this question may Will never be closed. Hope this will help others in this situation.

main.c

#include 
#include
#include

/* this keeps our Lua reference to the Lua function */
int callback_reference = 0;

/* this is called by Lua to register its function */
int lua_registerCallback( lua_State *L) {

/* store the reference to the Lua function in a variable to be used later */
callback_reference = luaL_ref( L, LUA_REGISTRYINDEX );

return 0;
}

/* calls our Lua callback function and resets the callback reference */
void call_callback( lua_State *L) {

/* push the callback onto the stack using the Lua reference we */
/* st ored in the registry */
lua_rawgeti( L, LUA_REGISTRYINDEX, callback_reference );

/* duplicate the value on the stack */
/* NOTE: This is unnecessary, but it shows how you keep the */
/* callback for later */
lua_pushvalue( L, 1 );

/* call the callback */
/ * NOTE: This is using the one we duplicated with lua_pushvalue */
if (0 != lua_pcall( L, 0, 0, 0)) {
printf("Failed to call the callback! %s ", lua_tostring( L, -1) );
return;
}

/* get a new reference to the Lua function and store it again */
/* NOTE: This is only used in conjunction with the lua_pushvalue */
/* above and can be removed if you remove that */
callback_reference = luaL_ref( L, LUA_REGISTRYINDEX );< br />}

int main( void) {

/* set up Lua */
lua_State *L = lua_open();
luaL_openlibs (L );

/* register the lua_registerCallback function as */
/* "RegisterCallback" so it can be called by Lua */
lua_pushcfunction( L, lua_registerCallback );
lua_setglobal( L, "RegisterCallback" );

/* run our Lua file */
if (0 != luaL_dofile( L, "callback.lua")) {
printf("Failed to load calback.lua! %s",< br /> lua_tostring( L, -1) );
lua_close( L );
return 1;
}

/* call the callback */
call_callback( L );

/* call the callback again if you want (because we restored */
/* the Lua function reference) */
call_callback( L );

/* remove the reference to the callback */
/* NOTE: This is also unnecessary if you didn't re-add the */
/* function to the registry */
luaL_unref( L, LUA_REGISTRYINDEX, callback_reference );

/* uninitialize Lua */
lua_close( L );

return 0;
}

callback.lua

function MyCallback()
print("Hello Wo rld!")
end

RegisterCallback( MyCallback )

Leave a Comment

Your email address will not be published.