lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


As I said this is very simple transpiler, basically WIP of proof of concept.
Of course the stack will leak. I don't use -1 because it has information about real stack index
On 24 Jan 2022, 11:21 +0300, SimplSam X <simplsam.x@gmail.com>, wrote:
This is a great tool and will help plenty, as I am just learning Lua C API. I think however there is an issue with the following:

In:
print(math.pi)

Out:
static int lua_openmodule(lua_State* L) {
    lua_getglobal(L, "print");
    lua_getglobal(L, "math");
    lua_getfield(L, 1, "pi");
    lua_call(L, 1, 0);
    return 0;
}


The above appears to fail on 2 points:
1) lua_getfield(L, 1, "pi"); should be: lua_getfield(L, -1, "pi"); as we don't nesc. know what is 1st on Stack
2) There is a need to remove "math" from the Stack. i.e. lua_remove(L, -2); so the lua_call will act on func: "print" and val of: "pi"

Working:
static int lua_openmodule(lua_State* L) {
    lua_getglobal(L, "print");
    lua_getglobal(L, "math");
    lua_getfield(L, -1, "pi");
    lua_remove(L, -2);
    lua_call(L, 1, 0);
    return 0;
}

--
Sam