Access to return values ​​of Lua functions in C ++ and vice versa

I have some functions in Lua and some functions in C ++, and I managed to get the correct results from my addition, power and division. The only problem I encountered is related to factorial, because I cannot pass the correct number to it, which should be the result of division.

in Lua:

function powLua(a, n)
   b = 1
   for i=1,n do b = b * a end
   return b
end

function divisionLua(a, b)
   c = b/a
   return c
end

aLua = additionLua(2, 3)
bLua = additionLua(1, 3)

print("Result of additionLua is a = "..aLua.."\nResult of additionLua is b = "..bLua.."\n")

fac = factorialLua(divLua) <---- this is the part that doesn't work

print("factorial of divLua is "..fac.."\n")

in C ++:

int addition(lua_State* L)
{
    int x = lua_tonumber(L, 1);
    int y = lua_tonumber(L, 2);

    lua_pushnumber(L, x + y);

    return 1;
}

int fac_calc(int x)
{
    if (x <= 1)
        return 1;

    return x * fac_calc(x-1);
}

int factorial(lua_State* L)
{   
    int fac = lua_tonumber(L, 1);

    lua_pushnumber(L, fac_calc(fac));

    return 1;
}

int power(lua_State* L, int a, int n)
{
    int b;

    lua_getglobal(L, "powLua");
    lua_pushnumber(L, a);
    lua_pushnumber(L, n);

    if (lua_pcall(L, 2, 1, 0) != 0)
        printf("error running function 'powLua': %s", lua_tostring(L, -1));

    if (!lua_isnumber(L, -1))
        printf("function `powLua' must return a number\n");
    b = lua_tonumber(L, -1);
    lua_pop(L, 1);  /* pop returned value */
    return b;
}

int division(lua_State* L, int a, int b)
{
    int c;

    lua_getglobal(L, "divisionLua");
    lua_pushnumber(L, a);
    lua_pushnumber(L, b);

    lua_pcall(L, 2, 1, 0);

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

    return c;
}

int main(int argc, char* argv[])
{
    lua_State* L = luaL_newstate();

    luaL_openlibs(L); 

    lua_pushcfunction(L, addition); 
    lua_setglobal(L, "additionLua"); 

    lua_pushcfunction(L, factorial);
    lua_setglobal(L, "factorialLua");

    luaL_dofile(L, "test01.lua");

    lua_getglobal(L, "aLua");
    int a = lua_tonumber(L, -1);
    lua_pop(L, -1);

    lua_getglobal(L, "bLua");
    int b = lua_tonumber(L, -1);
    lua_pop(L, -1);

    int pwr = power(L, a, b);

    int div = division(L, a, pwr);
    lua_pushnumber(L, div);
    lua_setglobal(L, "divLua");

    cout << "Result of a^b in powLua is pwr: " << a << "^" << b << " = " << pwr << endl;

    cout << "Result of pwr/a in divisionLua is: " << pwr << "/" << a << " = " << div << endl;

    lua_close(L);
}

my Output:

The result of the Lua complement is a = 5

The result of adding Lua - b = 4

factorial of divLua - 1

The result a ^ b in powLua is pwr: 5 ^ 4 = 625

The result of pwr / a in the Lua section is: 625/5 = 125

I think I need to push the result of dividing onto the stack before I open the file test01.lua, but it does not work either.

I also successfully test a factorial function by giving it a direct value. For example, factorialLua (5) gives me a result of 120.

- , ?

Edit:

"divLua" script , :

int div = 5;
lua_pushnumber(L, div);
lua_setglobal(L, "divLua");
...
...
luaL_dofile(L, "test01.lua");

factorialLua (divLua), , "divLua" div(), :

int div = division(L, 5, 30); //div should be 6 now
lua_pushnumber(L, div);
lua_setglobal(L, "divLua");
...
...
luaL_dofile(L, "test01.lua");

factorialLua (divLua) 1, 720 .

, () luaL_dofile, lua_setglobal (L, "divLua" ) . "div" , .

?

+4
1

:

  • factorialLua(divLua) script, divLua ++.
  • lua_pop , .
  • , , 125!, .

, , 1 factorialLua, lua_tonumber 0, . divLua - nil, , factorialLua(0).

:

  • lua_setglobal(L, "divLua"), script.
  • ++ , divLua .
+5

All Articles