Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kunitoki committed Mar 3, 2024
1 parent 38c99f5 commit d4c42bc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
9 changes: 7 additions & 2 deletions Source/LuaBridge/detail/Namespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,10 @@ class Namespace : public detail::Registrar
lua_pushcfunction_x(L, &detail::index_metamethod<false>, "__index");
rawsetfield(L, -2, "__index"); // Stack: ns

// ns.__newindex = newindex_static_metamethod
lua_pushcfunction_x(L, &detail::newindex_metamethod<false>, "__newindex");
rawsetfield(L, -2, "__newindex"); // Stack: pns, ns

lua_newtable(L); // Stack: ns, mt, propget table (pg)
lua_rawsetp(L, -2, detail::getPropgetKey()); // ns [propgetKey] = pg. Stack: ns

Expand Down Expand Up @@ -1988,6 +1992,7 @@ class Namespace : public detail::Registrar
}

using GetType = decltype(get);

lua_newuserdata_aligned<GetType>(L, std::move(get)); // Stack: ns, function userdata (ud)
lua_pushcclosure_x(L, &detail::invoke_proxy_functor<GetType>, name, 1); // Stack: ns, ud, getter
detail::add_property_getter(L, name, -2); // Stack: ns, ud, getter
Expand Down Expand Up @@ -2030,8 +2035,8 @@ class Namespace : public detail::Registrar
using SetType = decltype(set);

lua_newuserdata_aligned<SetType>(L, std::move(set)); // Stack: ns, function userdata (ud)
lua_pushcclosure_x(L, &detail::invoke_proxy_functor<SetType>, name, 1); // Stack: ns, ud, getter
detail::add_property_setter(L, name, -2); // Stack: ns, ud, getter
lua_pushcclosure_x(L, &detail::invoke_proxy_functor<SetType>, name, 1); // Stack: ns, ud, setter
detail::add_property_setter(L, name, -2); // Stack: ns, ud, setter

return *this;
}
Expand Down
56 changes: 54 additions & 2 deletions Tests/Source/NamespaceTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,58 @@ TEST_F(NamespaceTests, NamespaceFromStack)
ASSERT_EQ(42, resultTable["result"].cast<int>());
}

TEST_F(NamespaceTests, NamespaceFromStackProperties)
{
int x = 0;
int wx = 10;
const int cx = 100;

lua_newtable(L);
luabridge::getNamespaceFromStack(L)
.addProperty("valueX", &x, false)
.addProperty("valueWX", &wx, true)
.addProperty("valueCX", &cx)
.addProperty("value1", +[] { return 1; })
.addProperty("value2", +[] { return 2; }, +[](int) {})
.addProperty("value_getX", [&x] { return x; })
.addProperty("value_getSetX", [&x] { return x; }, [&x](int v) { x = v; })
;

auto table = luabridge::LuaRef::fromStack(L);

luabridge::setGlobal(L, table, "tab");

runLua("result = tab.valueX");
ASSERT_TRUE(result().isNumber());
EXPECT_EQ(0, result<int>());

runLua("result = tab.valueWX");
ASSERT_TRUE(result().isNumber());
EXPECT_EQ(10, result<int>());

runLua("result = tab.valueCX");
ASSERT_TRUE(result().isNumber());
EXPECT_EQ(100, result<int>());

runLua("result = tab.value1");
ASSERT_TRUE(result().isNumber());
EXPECT_EQ(1, result<int>());

runLua("result = tab.value2");
ASSERT_TRUE(result().isNumber());
EXPECT_EQ(2, result<int>());

runLua("result = tab.value_getX");
ASSERT_TRUE(result().isNumber());
EXPECT_EQ(0, result<int>());

runLua("result = tab.value_getSetX");
ASSERT_TRUE(result().isNumber());
EXPECT_EQ(0, result<int>());

runLua("tab.value_getSetX = 111");
EXPECT_EQ(111, x);
}

TEST_F(NamespaceTests, Properties_ProxyCFunctions)
{
Expand All @@ -376,12 +428,12 @@ TEST_F(NamespaceTests, Properties_ProxyCFunctions)

Storage<int>::value = 1;
runLua("ns.value = 2");
ASSERT_EQ(2, Storage<int>::value);
EXPECT_EQ(2, Storage<int>::value);

Storage<int>::value = 3;
runLua("result = ns.value");
ASSERT_TRUE(result().isNumber());
ASSERT_EQ(3, result<int>());
EXPECT_EQ(3, result<int>());
}

TEST_F(NamespaceTests, Properties_ProxyCFunctions_ReadOnly)
Expand Down

0 comments on commit d4c42bc

Please sign in to comment.