Skip to content

Commit

Permalink
Fix new_index logic
Browse files Browse the repository at this point in the history
  • Loading branch information
davits committed Sep 13, 2023
1 parent d097866 commit 3f6bf2f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ RUN wget https://apt.llvm.org/llvm.sh && \
ln -s /usr/bin/clang-16 /usr/bin/clang; \
ln -s /usr/bin/clang++-16 /usr/bin/clang++; \
ln -s /usr/bin/lldb-16 /usr/bin/lldb; \
ln -sf /usr/bin/llvm-cov-16 /usr/bin/gcov;
ln -sf /usr/bin/llvm-cov-16 /usr/bin/gcov; \
ln -sf /usr/bin/clangd-16 /usr/bin/clangd;

# Change these values by your uid/gid during build
ARG USERID=1000
Expand Down
14 changes: 8 additions & 6 deletions include/luabind/bind.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class class_ {

static int new_index(lua_State* L) {
int r = new_index_impl(L);
if (r != 0) return r;
if (r != 0) return 0;
// if there is no result in C++ add new value to the lua table bound to this object
user_data::get_custom_table(L, 1); // custom table
lua_pushvalue(L, 2); // key
Expand All @@ -233,24 +233,26 @@ class class_ {
if (!is_integer && key_type != LUA_TSTRING) {
reportError("Key type should be integer or string, '%s' is provided.", lua_typename(L, key_type));
}

if (is_integer) {
if (info->array_access_setter == nullptr) {
reportError("Type '%s' does not provide array set access.", info->name.c_str());
}
return info->array_access_setter(L);
info->array_access_setter(L);
return 1;
}

// key is string
// key is a string
auto key = value_mirror<std::string_view>::from_lua(L, 2);
auto p_it = info->properties.find(key);
if (p_it != info->properties.end()) {
if (p_it->second.setter == nullptr) {
reportError("Property '%s' is read only.", key.data());
}
return p_it->second.setter(L);
p_it->second.setter(L);
return 1;
}

// lua doesn't do recursive index lookup through metatables
// if __new_index is bound to a C function, so we do it ourselves.
for (type_info* base : info->bases) {
int r = base->new_index(L);
if (r != 0) {
Expand Down
12 changes: 12 additions & 0 deletions tests/lua_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ class LuaTest : public testing::Test {
return t;
}

void runExpectingError(const std::string_view script, const std::string_view expectedMessage) {
int lr = luaL_loadbufferx(L, script.data(), script.size(), "luabind::test", "t");
if (lr != LUA_OK) {
const std::string_view errorMessage = luabind::value_mirror<std::string_view>::from_lua(L, -1);
EXPECT_EQ(errorMessage, expectedMessage);
}
int cr = lua_pcall(L, 0, 0, 0);
EXPECT_NE(cr, LUA_OK);
const std::string_view errorMessage = luabind::value_mirror<std::string_view>::from_lua(L, -1);
EXPECT_EQ(errorMessage, expectedMessage);
}

protected:
lua_State* L = nullptr;
};

0 comments on commit 3f6bf2f

Please sign in to comment.