Search code examples
clualua-api

how to call lua operator in c?


The following example shows how the c program can do the equivalent to the Lua code:

a = f(t)

Here it is in C:

lua_getglobal(L, "f");    // function to be called
lua_getglobal(L, "t");    // 1 argument
lua_call(L, 1, 1);        // call "f" with 1 argument and 1 result
lua_setglobal(L, "a");    // set "a"

So, what is the equivalent C code of the following Lua code?

a = t + 1

Since we have no information about t, we should call the underlying + operator in c code, but HOW?


Solution

  • lua_getglobal(L, "t");
    lua_pushinteger(L, 1);
    lua_arith(L, LUA_OPADD);
    lua_setglobal(L, "a");