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

Fix Lua 0-indexing #247

Merged
merged 4 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Include/RmlUi/Lua/Utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ void RMLUILUA_API PushVariant(lua_State* L, const Variant* var);
/** Populate the variant based on the Lua value at the given index */
void RMLUILUA_API GetVariant(lua_State* L, int index, Variant* variant);

// Converts index from 0-based to 1-based before pushing it to the stack
void RMLUILUA_API PushIndex(lua_State* L, int index);

// Returns 0-based index after retrieving from stack and converting from 1-based
// The index parameter refers to the position on the stack and is not affected
// by the conversion
int RMLUILUA_API GetIndex(lua_State* L, int index);

//Helper function, so that the types don't have to define individual functions themselves
// to fill the Elements.As table
template<typename ToType>
Expand Down
2 changes: 1 addition & 1 deletion Source/Lua/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ int ElementGetElementsByTagName(lua_State* L, Element* obj)
lua_newtable(L);
for(unsigned int i = 0; i < list.size(); i++)
{
lua_pushinteger(L,i);
PushIndex(L,i);
LuaType<Element>::push(L,list[i],false);
lua_settable(L,-3); //-3 is the table
}
Expand Down
8 changes: 4 additions & 4 deletions Source/Lua/Elements/ElementFormControlSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ int ElementFormControlSelectAdd(lua_State* L, ElementFormControlSelect* obj)
const char* value = luaL_checkstring(L,2);
int before = -1; //default
if(lua_gettop(L) >= 3)
before = (int)luaL_checkinteger(L,3);
before = GetIndex(L,3);

int index = obj->Add(rml,value,before);
lua_pushinteger(L,index);
Expand All @@ -54,7 +54,7 @@ int ElementFormControlSelectAdd(lua_State* L, ElementFormControlSelect* obj)

int ElementFormControlSelectRemove(lua_State* L, ElementFormControlSelect* obj)
{
int index = (int)luaL_checkinteger(L,1);
int index = GetIndex(L,1);
obj->Remove(index);
return 0;
}
Expand All @@ -81,7 +81,7 @@ int ElementFormControlSelectGetAttrselection(lua_State* L)
ElementFormControlSelect* obj = LuaType<ElementFormControlSelect>::check(L,1);
RMLUI_CHECK_OBJ(obj);
int selection = obj->GetSelection();
lua_pushinteger(L,selection);
PushIndex(L,selection);
return 1;
}

Expand All @@ -91,7 +91,7 @@ int ElementFormControlSelectSetAttrselection(lua_State* L)
{
ElementFormControlSelect* obj = LuaType<ElementFormControlSelect>::check(L,1);
RMLUI_CHECK_OBJ(obj);
int selection = (int)luaL_checkinteger(L,2);
int selection = GetIndex(L,2);
obj->SetSelection(selection);
return 0;
}
Expand Down
8 changes: 4 additions & 4 deletions Source/Lua/Elements/ElementTabSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace Lua {
int ElementTabSetSetPanel(lua_State* L, ElementTabSet* obj)
{
RMLUI_CHECK_OBJ(obj);
int index = (int)luaL_checkinteger(L,1);
int index = GetIndex(L,1);
const char* rml = luaL_checkstring(L,2);

obj->SetPanel(index,rml);
Expand All @@ -48,7 +48,7 @@ int ElementTabSetSetPanel(lua_State* L, ElementTabSet* obj)
int ElementTabSetSetTab(lua_State* L, ElementTabSet* obj)
{
RMLUI_CHECK_OBJ(obj);
int index = (int)luaL_checkinteger(L,1);
int index = GetIndex(L,1);
const char* rml = luaL_checkstring(L,2);

obj->SetTab(index,rml);
Expand All @@ -62,7 +62,7 @@ int ElementTabSetGetAttractive_tab(lua_State* L)
ElementTabSet* obj = LuaType<ElementTabSet>::check(L,1);
RMLUI_CHECK_OBJ(obj);
int tab = obj->GetActiveTab();
lua_pushinteger(L,tab);
PushIndex(L,tab);
return 1;
}

Expand All @@ -81,7 +81,7 @@ int ElementTabSetSetAttractive_tab(lua_State* L)
{
ElementTabSet* obj = LuaType<ElementTabSet>::check(L,1);
RMLUI_CHECK_OBJ(obj);
int tab = (int)luaL_checkinteger(L,2);
int tab = GetIndex(L,2);
obj->SetActiveTab(tab);
return 0;
}
Expand Down
11 changes: 10 additions & 1 deletion Source/Lua/EventParametersProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <RmlUi/Core/Variant.h>
#include <RmlUi/Core/Dictionary.h>
#include "Pairs.h"
#include <cstring>


namespace Rml {
Expand All @@ -55,7 +56,15 @@ int EventParametersProxy__index(lua_State* L)
const char* key = lua_tostring(L,2);
auto it = obj->owner->GetParameters().find(key);
const Variant* param = (it == obj->owner->GetParameters().end() ? nullptr : &it->second);
PushVariant(L,param);
if (obj->owner->GetId() == EventId::Tabchange &&
std::strcmp(key, "tab_index") == 0 &&
param &&
param->GetType() == Variant::Type::INT)
{
PushIndex(L,param->Get<int>());
}
else
PushVariant(L,param);
return 1;
}
else
Expand Down
11 changes: 11 additions & 0 deletions Source/Lua/Utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,16 @@ void GetVariant(lua_State* L, int index, Variant* variant)
break;
}
}

void PushIndex(lua_State* L, int index)
{
lua_pushinteger(L, index + 1);
}

int GetIndex(lua_State* L, int index)
{
return (int)luaL_checkinteger(L, index) - 1;
}

} // namespace Lua
} // namespace Rml