How to Detect Optional First Parameter in Lua Callback? - c++

I have a LUA callback in C/++ and I'm trying to use lua_gettop() to detect an optional first parameter. The problem is that lua_gettop(L) == 1 is always true, even when I don't provide any parameters in LUA.
Here's the basics of my callback function in C/++:
int LuaFile::l_cpp_my_callback(lua_State *L) {
LuaFilePtr my_this = lua_map_[L];
if ( lua_gettop(L) == 0 ) {
// I never get here
} else if(lua_gettop(L) == 1) {
// Always gets here
} else {
return 1;
}
return 1;
}
This is the context of the call in the LUA code:
function my_function()
if ... then
if not ... then
if ... then
someOtherCallback(param, param, param)
someOtherCallback(param)
end
if ... then
someOtherCallback(param)
end
end
else
if not ... then
if ... then
someOtherCallback(param, param, param)
someOtherCallback(param)
end
if ... then
someOtherCallback(param)
end
end
var = my_callback()
...
end
end
I used lua_gettype(L, 1) to debug and it returns '5', which lua_typename(L, 5) says is a 'table'. I'm guessing this is a metatable of the stack?
I understand that 0 isn't a valid index in the LUA stack but then what's the proper way to determine optional first parameters?

As #nate mentioned in his comment, any "object-based" function call, that is, calling a member function--or method, if you prefer--results in the object being the first thing on the Lua stack. lua_gettop would return 1, assuming nothing else was passed to the member function as an argument.
As for checking an optional first argument to the member function, which would be located at index 2 on the Lua stack if you were using an object-based call, functions like luaL_optstring are very helpful. (Reference page here. In case you are unaware, functions prefixed with luaL are located in lauxlib.h.) There are also equivalent functions for other types. It allows one to provide an index and a default argument: if a value exists at the given index on the Lua stack, it is returned; otherwise, the default value supplied to the function is returned.

Related

Calling a Function with integer [[mayb_unused]] macro and default argument boolean type parameter

I am writing a Function with 2 Parameters, 1st is integer type marked [[maybe_unused]] and 2nd is Boolean Type with Default Argument false.
int preOrderTraversial([[maybe_unused]] int searchData, bool forDisplay = false)
This is the function declaration. The function is used for pre-order Traversal in Binary Search Tree. I want to use the same function for Displaying the Tree and Traversing it for searching a Node with specific Data. But only for 1 of both problems at once. Not for searching AND displaying you see.
So, obviously, it works fine for the searching part but when I call the function for Displaying the function call looks like this:
preOrderTraversal(true)
or
bool forDisplay = true;
preOrderTraversal(forDisplay);
Now the Compiler (even with the parameter DECLARED as a BOOL) still considers this bool parameter as an int and uses it for searchData (integer) parameter of the function and not for the forDisplay (bool) parameter.
Is there a way to force or tell compiler which of both parameters I am trying to work with?
[[maybe_unused]] means that the function body may not use it, but the signature of the function always have it. Actually event the default value for the boolean does not modify the signature of your function; Thus your function always take 2 arguments, the first being a integer, and the second being a boolean (that may be set to the default if you didn't put it). You end up with calling:
bool forDisplay = true;
preOrderTraversal(/* integer searchData; bool geting casted to int */ forDisplay,
false /* the forDisplay (2nd param) default value*/);
which does not meet your expectations.
[EDIT]
how to overcome?
There is not builtin way to use named parameters in C++ but you may workaround using a helper class:
struct PreOrderTraversal {
bool _forDisplay = false;
int _searchData = 0; /* FIXME What is the default here? */
auto& forDisplay() { _forDisplay = true; return *this; }
auto& searchData(int x) { _searchData = x; return *this; }
void run() { if (_forDisplay) ;//do the right things
else ;// use data
}
};
then you can use it with:
PreOrderTraversal().forDisplay().run();
PreOrderTraversal().searchData(123).run();
This kinda mimics that named parameter feature.

Lua checkudata type C++

I've been to the end of Google and back trying to solve this problem.
I have a few userdata objects that I push from C++ to Lua.
I have a function that should get the X value of either a 2D or 3D object.
When I try to get the userdata object, taking into consideration that it could be either a 2D element or 3D object, I need to be able to get the X for whichever the user chooses.
Here is what I tried:
int getX(lua_State* L)
{
Object3D* a = static_cast<Object3D*>(luaL_checkudata(L, 1, "Object3D"));
if (!a)
{
Object2D* b = static_cast<Object2D*>(luaL_checkudata(L, 1, "Object2D"));
if (b)
{
lua_pushnumber(L, b:getX());
}
else
{
lua_pushnil(L);
}
}
else
{
lua_pushnumber(L, a:getX());
}
return 1;
}
Unfortunately if the userdata type is not Object3D, it fails and exits on an lua error without continuing to try Object2D.
Therefore, it will only work in the above code if the object being passed is of type Object3D.
luaL_testudata
void *luaL_testudata (lua_State *L, int arg, const char *tname);
This function works like luaL_checkudata, except that, when the test fails, it returns NULL instead of raising an error.
The lua(L)_check* functions throw Lua errors on failure, the lua(L)_to* functions return NULL. For whatever reason, this one deviates from the naming convention and is named lua(L)_test* instead, which makes it a bit harder to find.
Your code is incomplete and doesn't compile as-is so I can't be bothered to check, but if I'm not mistaken, just replacing luaL_checkudata with luaL_testudata should make it work as intended.
Solved by using rawequal to see which class the registry matches.

Functions as values in a dictionary being iterated instead of being selected

I have a function that looks similar to this:
def some_function(self, case, index):
with threading.Lock:
return{
'case1':function1(index),
'case2':function2(index)
}[case]
And elsewhere this function is being called as such:
for i in range(4)
self.module.some_function('case1', i)
Which should iterate function1 for indices within the specified range. Instead, both function1 and function2 are iterated for the indices. Is there a problem with the implementation above? What could be causing both function1 and function2 to be called when only a single key is given?
Indeed, this dict literal expression is evaluated first:
{
'case1':function1(index),
'case2':function2(index)
}
and only then the key you specify after it, is selected from it, but at that time the two functions have already executed.
So, you should do it differently: first the correct function must be selected using the case variable, and only after that selection the function should be executed by passing the argument to it.
There are several solutions, but here is one:
return [function1, function2][case == 'case2'](index)
Or with a dict literal:
return {
'case1': function1,
'case2': function2
}[case](index)
Or with an inline if:
return function1(index) if case == 'case1' else function2(index)
Or avoiding the repetition of (index):
return (function1 if case == 'case1' else function2)(index)
Or you can use a plain if:
if case == 'case1':
return function1(index)
return function2(index)

Calling lua function that returns table

I know the basics of interacting with lua and C, and I am currently trying to perform the following line of lua in c++
Func1():Func2().Table1.value1
I am trying to get the value of "value2" and use it in my C program. The following is the code I wrote to attempt to get this value in C.
int GetNumber()
{
int retn = 0;
g_clientlua.lua_getfield(LUA_REGISTRYINDEX, "Player");
g_clientlua.lua_getfield(-1, "Func2");
g_clientlua.lua_getfield(LUA_GLOBALSINDEX, "Func1");
g_clientlua.lua_call(0, 1);
g_clientlua.lua_call(1, 1);
if (g_clientlua.lua_isnil(-1))
return retn;
g_clientlua.lua_getfield(-1, "Table1");
if (g_clientlua.lua_isnil(-1))
return retn;
g_clientlua.lua_getfield(-1, "value1");
if (g_clientlua.lua_isnil(-1))
return retn;
retn = (int)g_clientlua.lua_tointeger(-1);
}
The clientlua thing is an object that basically just allows me to call a method which calls it's lua_* function equivalent and fills the lua_state pointer parameter with a member variable that is a pointer to the lua state.
Every time I call this, it complains about me causing a lua stack leak. To solve this, I tried adding a lua_pop(3) to the end, but then it just crashes my program without reporting an error, so I assume I am doing something wrong.
Anyone have any words of wisdom for me? Kinda lost here. I doubt the above code is even written properly, how would I write the above lua call in C?
You need to call Func1 before you try to get Func2 as Func2 comes from the table that Func1 returns (and not from the global table).
Then you need to call Func2 and look up Table1 in that returned value, etc.
What "stack leak" complaint are you getting? If you are calling this function from C directly then yes, you need to be sure that anything you put on the lua stack (that isn't for consumption by the caller, etc.) is popped from the lua stack before you return.
The GetNumber function isn't doing exactly the same as the lua snippet you're going for. Specifically GetNumber is getting the value of "Func2" from the registry while your lua snippet is getting the value of "Func2" from the table returned by Func1(). Unless you're certain that registry.Player.Func2 == Func1().Func2 is always true, your C++ version will not have the same behavior.
Let's break down Func1():Func2().Table1.value1 into more explicit steps to help with the C translation:
Get function associated with _G.Func1
Call that function and get a table back
Get function associated with "Func2" from the returned table in step 2
Call that function and pass as argument the table from step 2. Get another table back as result
I found it helpful to track what the stack contains as a side-comment as the operations are performed:
int GetNumber()
{
// Func1()
gclientlua.lua_getfield(LUA_GLOBALSINDEX, "Func1"); // Func1
g_clientlua.lua_call(0, 1); // {}
// Func2( {} )
g_clientlua.lua_getfield(-1, "Func2"); // {}, Func2
g_clientlua.lua_insert(-2); // Func2, {}
g_clientlua.lua_call(1, 1); // {}
if( g_clientlua.lua_type(-1) != LUA_TTABLE )
{
g_clientlua.lua_pop(1);
return 0;
}
// {}.Table1
g_clientlua.lua_getfield(-1, "Table1"); // {}, {}(Table1)
if( g_clientlua.lua_type(-1) != LUA_TTABLE )
{
g_clientlua.lua_pop(2);
return 0;
}
// tonumber( Table1.value1 )
g_clientlua.lua_getfield(-1, "value1"); // {}, {}(Table1), value1
int retn = g_clientlua.lua_tointeger(-1);
g_clientlua.lua_pop(3);
return retn;
}
Notice that GetNumber pops off all the arguments it places on the stack before returning. This ensures that GetNumber leaves the lua stack the way it was found. This can probably be automated with RAII if you're using C++.

Lua: getting global function failing after loading file

I'm attempting to call a function inside of a lua file called test2.lua
This is the contents of test2.lua:
function abc(path)
t = {}
table.insert(t, "a")
return t
end
As you can see it takes a single input and returns a string.
Here is my C code. It's pretty simple. However my call getglobal in order to call that function does not work... lua_getglobal says it isn't a function when I test it... Any reason why this is? Shouldn't abc be a global function returnable inside of the source file? Why then does it only find nil for this global?
L = lua_open();
luaL_openlibs(L);
luaL_loadfile(L, "src/test2.lua");
lua_getglobal(L, "abc");
lua_pushstring(L, "coollll");
int error = 0;
if ((error = lua_pcall(L, 1, 1, 0)) == 0)
{
std::cout << "cool";
}
EDIT:
calling lua_getglobal is causing my program to break control regardless of using loadfile or dofile... any idea why?
lua_getglobal crashing program
The function luaL_loadfile() reads, parses, and compiles the named Lua file. It does not execute any of its content. This is important in your case because the statement function abc(path)...end has no visible effect until it is executed. The function keyword as you've used it is equivalent to writing
abc = function(path)
t = {}
table.insert(t, "a")
return t
end
In this form, it is clearer that the variable named abc is not actually assigned a value until the code executes.
When luaL_loadfile() returns, it has pushed an anonymous function on the top of the Lua stack that is the result of compiling your file. You need to call it, and lua_pcall() will do the trick. Replace your reference to luaL_loadfile() with this:
if (luaL_loadfile(L, "src/test2.lua") || lua_pcall(L, 0, LUA_MULTRET, 0)) {
// do something with the error
}
At this point, test2.lua has been executed and any functions it defined or other global variables it modified are available.
This is a common enough idiom, that the function luaL_dofile() is provided to load and call a file by name.
There is a second, more subtle issue in your code as presented. The function abc() uses a variable named t, but you should be aware that t as used is a global variable. You probably meant to write local t = {} at the top of abc().
It's not enough to call luaL_loadfile: this puts a chunk onto the stack. Either follow up with luaL_[p]call to execute the chunk (thus making the function available), or use luaL_dofile.