Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure sufficient stack size for Lua values #4482

Merged
merged 4 commits into from Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions code/scripting/lua/LuaFunction.cpp
Expand Up @@ -172,12 +172,15 @@ LuaValueList LuaFunction::call(lua_State* L, const LuaValueList& args) const {
stackTop = lua_gettop(L);
}

if(!lua_checkstack(L, (int)args.size() + 1))
throw LuaException("Lua Stack Overflow!");

// Push the function onto the stack
this->pushValue(L);
this->pushValue(L, true);

// Push the arguments onto the stack
for (const auto& arg : args) {
arg.pushValue(L);
arg.pushValue(L, true);
}

// actually call the function now!
Expand Down
5 changes: 4 additions & 1 deletion code/scripting/lua/LuaReference.cpp
Expand Up @@ -80,10 +80,13 @@ bool UniqueLuaReference::isValid() const {
return true;
}

void UniqueLuaReference::pushValue(lua_State* thread) const
void UniqueLuaReference::pushValue(lua_State* thread, bool guaranteeStack) const
{
Assertion(thread != nullptr, "Valid thread state must be specified!");

if(!guaranteeStack)
lua_checkstack(thread, 1);
BMagnu marked this conversation as resolved.
Show resolved Hide resolved

if (this->isValid()) {
lua_rawgeti(thread, LUA_REGISTRYINDEX, this->getReference());
}
Expand Down
2 changes: 1 addition & 1 deletion code/scripting/lua/LuaReference.h
Expand Up @@ -101,7 +101,7 @@ class UniqueLuaReference {
* @brief Pushes the referenced value onto the stack.
* @param thread A specific thread state to push the value to. nullptr for the default state of this reference
*/
void pushValue(lua_State* thread) const;
void pushValue(lua_State* thread, bool guaranteeStack = false) const;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a "random" boolean variable to this call would, in my opinion, make this API confusing to use. Furthermore, since you do the relevant call before the Lua function anyway, I do not think adding this parameter (and also to LuaValue) is necessary.

Copy link
Member Author

@BMagnu BMagnu Jul 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I make this call in this instance, it is necessary to do this for every case this function is called. Which is from quite a lot of places. You have to compromise at some point, either by asking the coder to always manually ensure the stack size before each call of pushValue (both for this and value), or eat the performance penalty of doing the allocation / check more than once if you push many values, or have the option to pass this as the API parameter.
Since I would really not want to have to rely on the programmer to always remember manually ensuring the stack size, it's basically a question of "do we want to waste performance in a low level function" vs "do we want one more parameter in the low level function".
IMO, with this case we should pick performance over pretty API.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I can understand that reasoning.
One thing I noticed though is that the current name suggests that a true value will trigger the lua_checkstack call but that is not the case. Maybe a parameter name like checkStack would be more appropriate here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, clarified variable name and added documentation

};
}

Expand Down
4 changes: 2 additions & 2 deletions code/scripting/lua/LuaValue.cpp
Expand Up @@ -88,10 +88,10 @@ bool LuaValue::isValid() const {
return _reference && _reference->isValid();
}

bool LuaValue::pushValue(lua_State* thread) const
bool LuaValue::pushValue(lua_State* thread, bool guaranteeStack) const
{
if (this->_reference->isValid()) {
this->_reference->pushValue(thread);
this->_reference->pushValue(thread, guaranteeStack);
return true;
} else {
return false;
Expand Down
2 changes: 1 addition & 1 deletion code/scripting/lua/LuaValue.h
Expand Up @@ -186,7 +186,7 @@ class LuaValue {
* @param thread The thread stack onto which this value should be pushed. May be nullptr for the default state of
* this value
*/
bool pushValue(lua_State* thread) const;
bool pushValue(lua_State* thread, bool guaranteeStack = false) const;

lua_State* getLuaState() const;

Expand Down