0

Let we have a code in "luafunc.lua":

function foo(a, b)
   return a + b
end

a = io.read('*n')
b = io.read('*n')
print (foo(a, b))

Let's try to use function foo from C++ file:

#include <iostream>

using namespace std;

extern "C"{
    #include <lua.h>
    #include <lualib.h>
    #include <lauxlib.h>
};

int main()
{
    lua_State *lvm = lua_open();
    luaL_openlibs(lvm);
    luaL_loadfile(lvm, "luafunc.lua");

    int a, b;
    cin >> a >> b;
    lua_pcall(lvm, 0, LUA_MULTRET, 0);
    lua_getglobal(lvm, "foo");
    lua_pushnumber(lvm, a);
    lua_pushnumber(lvm, b);
    if (lua_pcall(lvm, 2, 1, 0))
    {
        cout << "Error: " << lua_tostring(lvm, -1) << endl;
        return 0;
    }

    cout << "The result is: " << lua_tonumber(lvm, -1) << endl;

    lua_close(lvm);
    return 0;
}

So, the problem is that this C++ code executes the whole luafunc.lua. Naturally I can remove reading part from lua-file and then from C++ only foo is executed. But can I use function foo from C++ even if there's other stuff in lua-file?

2 Answers 2

4

If you need to be able to use that function without also running that code, separate the code and function into two separate scripts, a script with foo in it and a script that loads that script and tests foo.

A function is not defined until the script containing it is executed. Executing that script will define foo and then run the other 3 lines as well.

When you load a file with loaL_loadfile (or any of the other load calls) the entire script is turned into a function; to execute it you have to call that function, with lua_pcall or whatever. Until then the script that defines foo is just an unnamed, unexecuted chunk of code on the stack.

There is no function to execute just part of a script, or execute only the function definitions.

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

3 Comments

You can load code without executing it, via Lua or the API. In the OP's case, luaL_loadfile loads it without executing it, leaving a compiled "chunk" on the stack. He then executes it on the following line (lua_pcall).
@Mud: Will that allow him to call foo without executing the global code? Doesn't sound like that solves the problem.
No, not at all. He's going to need to execute the chunk for foo to be defined at all, because functions are defined at runtime in Lua. Your answer solves his problem. I was clarifying that your statement "there's no way to get lua to load that file without also executing the code" is not technically correct. Loading a file and executing it are separate steps in the OP's program, and you can do the same in Lua with load or loadfile which both load and compile a Lua script without executing it (dofile loads, compiles and executes a script).
3

can I use function foo from C++ even if there's other stuff in lua-file?

Yes.

Can you use it without executing the other parts of that file? No.

Lua functions are defined at runtime. Simply loading and compiling that script is not enough, you have to run the resulting chunk for foo to be defined in your Lua state.

Comments

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.