2

Is it possible to only call one specific function in a Lua script from C. Currently, I have a Lua script which calls a C function. Now, I need this C function to call just one Lua function from the mentioned script.

EDIT: The C functions look like this:

#include <lua.h>
#include <lauxlib.h> 
#include <lualib.h>


static double E1(double x) {

    double xint = x;
    double z;


    lua_State *L;
    L = luaL_newstate();

    luaL_openlibs(L);

    luaL_loadfile(L, "luascript.lua");

    lua_pcall(L, 0, 0, 0);

    lua_getglobal(L, "func");
    lua_pushnumber(L, x);

    lua_pcall(L, 1, 1, 0);

    z = lua_tonumber(L, -1);
    lua_pop(L, 1);

    lua_close(L);

    return z;
}

static int Ret(lua_State *L){

    double y = lua_tonumber(L, -1);

    lua_pushnumber(L, E1(y));

    return 1;
}

int luaopen_func2lua(lua_State *L){
    lua_register(
                    L,
                    "Ret",
                    Ret
                    );
    return 0;
}

The Lua script looks like this:

 require "func2lua"

 function func (x)
     -- some mathematical stuff
     return value
 end

 x = 23.1

 print(Ret(x)) -- Ret is the C function from the top c-file
3
  • Can't you write this lua function in C? Usually it is lua that calls C not other way around Commented May 13, 2016 at 12:48
  • Unfortunately, a third program depends on Lua. So, for me, there is no way around it. Commented May 13, 2016 at 14:38
  • Perhaps some interpretation of the Lua file will help. When you execute the Lua file it: 1) loads and runs func2lua.lua, sets the global func to a function value, sets the global x to a number, invokes the value of the global Ret variable as a function and invokes the value of the global print variable as a function. Until func is set, its value will be nil. It's unclear who you want whom to call and when. You probably don't want to load and execute the file more than once. Commented May 13, 2016 at 20:28

1 Answer 1

1

Yes you can. C function will need a way to get that function. Depending on your requirements you can either pass that Lua function to C function as one of arguments, or store that Lua function where C can reach it - either in global environment (then C will lua_getglobal() that function), or in some predefined table, belonging to that script.

Sign up to request clarification or add additional context in comments.

5 Comments

But first you need execute chunk (e.g. file or buffer) so you actually create this function. You can use lua_loadfile/lua_call
Script that calls C function is already being executed, no need to prepare it separately. But yes, if Lua function to be called will be stored in global environment, then it must be stored there before first call to C function.
I mean that if author do not call lua_call/pcall but just lua_loadfile there no any function.
By definition there's "Lua script which calls a C function". By the moment C function called - calling script is already running.
Thanks for your quick reply. I am sorry but I am a newbie to Lua and C. Therefore I am not completely familiar with all terms you used. So far I have the Lua script which calls the C function which in turn calls another C function which then, in theory, calls a function from the original Lua script. The C--> Lua call is invoked via luaL_loadfile, lua_getglobal, and lua_pcall. But, this lead to an infinite loop. Long story, short: How can I specifically address the one Lua function I want to call using lua_getglobal()? Again, thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.