Skip to content

Commit

Permalink
add seek functions
Browse files Browse the repository at this point in the history
  • Loading branch information
peacalm committed Apr 23, 2023
1 parent 970a6ec commit 33eb20b
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
34 changes: 34 additions & 0 deletions include/peacalm/lua_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ inline int COUNTER0(lua_State* L) {
} // namespace luafunc

class lua_wrapper {
using self_t = lua_wrapper;
lua_State* L_;

public:
Expand Down Expand Up @@ -632,6 +633,39 @@ class lua_wrapper {

/** @}*/

///////////////////////// seek fields ////////////////////////////////////////

/// Get a global value by name and push it onto the stack, or push a nil if
/// the name does not exist.
self_t& gseek(const char* name) {
getglobal(name);
return *this;
}
self_t& gseek(const std::string& name) { return gseek(name.c_str()); }

/// Push t[name] onto the stack where t is the value at the given index `idx`,
/// or push a nil on failed.
self_t& seek(const char* name, int idx = -1) {
if (gettop() > 0 && istable(idx)) {
lua_getfield(L_, idx, name);
} else {
lua_pushnil(L_);
}
return *this;
}
self_t& seek(const std::string& name) { return seek(name.c_str()); }

/// Push t[n] onto the stack where t is the value at the given index `idx`, or
/// push a nil on failed.
self_t& seek(int n, int idx = -1) {
if (gettop() > 0 && istable(idx)) {
lua_geti(L_, idx, n);
} else {
lua_pushnil(L_);
}
return *this;
}

///////////////////////// set global variables ///////////////////////////////

void set_integer(const char* name, long long value) {
Expand Down
83 changes: 83 additions & 0 deletions test/unit_test/unit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,89 @@ TEST(lua_wrapper, template_eval) {
}
}

TEST(lua_wrapper, seek) {
lua_wrapper l;
l.gseek("g");
EXPECT_EQ(l.gettop(), 1);
EXPECT_TRUE(l.isnil(-1));
l.seek("gg");
EXPECT_EQ(l.gettop(), 2);
EXPECT_TRUE(l.isnil(-1));
l.seek(1);
EXPECT_EQ(l.gettop(), 3);
EXPECT_TRUE(l.isnil(-1));

l.settop(0);
l.dostring("g={a=1, gg={a=11,b='bb'}, list={1,2,3}}");
l.gseek("g");
EXPECT_EQ(l.gettop(), 1);
EXPECT_TRUE(l.istable(-1));
l.seek("a");
EXPECT_EQ(l.gettop(), 2);
EXPECT_EQ(l.to<int>(), 1);
l.pop();
l.seek("gg");
EXPECT_EQ(l.gettop(), 2);
EXPECT_TRUE(l.istable(-1));
l.seek("a");
EXPECT_EQ(l.gettop(), 3);
EXPECT_EQ(l.to<int>(), 11);
l.pop();
l.seek("b");
EXPECT_EQ(l.gettop(), 3);
EXPECT_EQ(l.to<std::string>(), "bb");
l.pop(2);
l.seek("list");
int list_idx = l.gettop();
EXPECT_EQ(l.seek(2).to_int(), 2);
l.pop();
EXPECT_EQ(l.seek(3).to<long>(), 3);
EXPECT_EQ(l.seek(1, list_idx).to_double(), 1);
EXPECT_EQ(l.gettop(), 4);

l.settop(0);
EXPECT_EQ(l.gseek("g").seek("gg").seek("a").to_int(), 11);
EXPECT_EQ(l.gseek("g").seek("list").seek(2).to_int(), 2);
EXPECT_EQ(l.gettop(), 6);

l.seek(3);
EXPECT_TRUE(l.isnil());
l.pop(2);
l.seek(3);
EXPECT_FALSE(l.isnil());
EXPECT_EQ(l.to_uint(), 3);

l.settop(0);
l.dostring("g={{1,2,3.0}, {'a', 'b', 'c'}, m={{a=1},{a=2}} }");
EXPECT_EQ(l.gseek("g").seek(1).seek(1).to_int(), 1);
l.settop(0);
EXPECT_EQ(l.gseek("g").seek(1).seek(3).to_int(), 3);
l.settop(0);
EXPECT_EQ(l.gseek("g").seek(1).seek(3).to_string(), "3.0");
l.settop(0);
EXPECT_EQ(l.gseek("g").seek(2).seek(3).to<std::string>(), "c");
l.settop(0);
EXPECT_EQ(l.gseek("g").seek("m").seek(1).seek("a").to_int(), 1);
l.settop(0);
EXPECT_EQ(l.gseek("g").seek("m").seek(2).seek("a").to_int(), 2);

l.settop(0);
EXPECT_EQ(l.gseek("g").seek(1).to<std::vector<int>>(),
(std::vector<int>{1, 2, 3}));
l.settop(0);
EXPECT_EQ(l.gseek("g").seek(1).to<std::vector<std::string>>(),
(std::vector<std::string>{"1", "2", "3.0"}));
l.pop();
EXPECT_EQ((l.seek("m").seek(2).to<std::map<std::string, int>>()),
(std::map<std::string, int>{{"a", 2}}));

l.seek(3);
EXPECT_TRUE(l.isnil());
l.pop();

l.settop(0);
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);

Expand Down

0 comments on commit 33eb20b

Please sign in to comment.