0

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?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • 3
    How about [`lua_arith`](http://www.lua.org/manual/5.3/manual.html#lua_arith)? – Some programmer dude Nov 24 '15 at 09:14
  • And if you are stuck with Lua 5.1, there is a backport in [Compat-5.2](https://github.com/keplerproject/lua-compat-5.2/blob/5ff9309b4d1079a04843334bfb92563e3519efda/c-api/compat-5.2.c#L464-L474) or [Compat-5.3](https://github.com/keplerproject/lua-compat-5.3/blob/ce386e11eddfc6a98278d5ba302de717635b2f94/c-api/compat-5.3.c#L51-L61). – siffiejoe Nov 24 '15 at 09:20
  • `luaL_dostring(L, "a = t + 1");` – Egor Skriptunoff Nov 24 '15 at 17:51
  • I have found `lua_arith()` in Lua 5.3, but I am working with LuaJIT, which didn't support this function. I have viewed the code in Compat-5.2 and extracted the corresponding code to meet my needs. Thank you. @JoachimPileborg @siffiejoe – b8flowerfire Dec 02 '15 at 07:06

1 Answers1

1
lua_getglobal(L, "t");
lua_pushinteger(L, 1);
lua_arith(L, LUA_OPADD);
lua_setglobal(L, "a");
marsgpl
  • 552
  • 2
  • 12