Skip to content
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
17 changes: 15 additions & 2 deletions luaApp/src/luaEpics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -652,14 +652,27 @@ epicsShareFunc std::string luaMacrosFromTable(lua_State* state, int index)
/* Duplicate the value before converting -- lua_tostring on a
* non-string value modifies it in-place, which corrupts lua_next */
lua_pushvalue(state, -1);
const char* val = lua_tostring(state, -1);
lua_pop(state, 1);
const char* val = nullptr;
switch (lua_type(state, -1))
{
case LUA_TBOOLEAN:
val = lua_toboolean(state, -1) ? "true" : "false";
break;
case LUA_TNUMBER:
case LUA_TSTRING:
val = lua_tostring(state, -1);
break;
default:
break;
}

if (val)
{
if (!result.empty()) { result += ","; }
result += std::string(key) + "=" + std::string(val);
}

lua_pop(state, 1);
}
lua_pop(state, 1);
}
Expand Down
21 changes: 20 additions & 1 deletion luaApp/test/luaShellTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -999,14 +999,33 @@ static void testLuaCmdTableMacros(void)
lua_pushstring(state, "sensor");
lua_setfield(state, -2, "R");

lua_pushboolean(state, 1);
lua_setfield(state, -2, "enabled");
lua_pushboolean(state, 0);
lua_setfield(state, -2, "disabled");

lua_pushinteger(state, 42);
lua_setfield(state, -2, "ivalue");

lua_pushnumber(state, 3.14);
lua_setfield(state, -2, "fvalue");

std::string macros = luaMacrosFromTable(state, lua_gettop(state));
lua_pop(state, 1);

/* Order of keys in Lua tables is not guaranteed, check both */
/* Order of keys in Lua tables is not guaranteed */
testOk(macros.find("P=dev1:") != std::string::npos,
"macros contains P=dev1: : '%s'", macros.c_str());
testOk(macros.find("R=sensor") != std::string::npos,
"macros contains R=sensor : '%s'", macros.c_str());
testOk(macros.find("enabled=true") != std::string::npos,
"macros contains enabled=true: '%s'", macros.c_str());
testOk(macros.find("disabled=false") != std::string::npos,
"macros contains disabled=false: '%s'", macros.c_str());
testOk(macros.find("ivalue=42") != std::string::npos,
"macros contains ivalue=42: '%s'", macros.c_str());
testOk(macros.find("fvalue=3.14") != std::string::npos,
"macros contains fvalue=3.14: '%s'", macros.c_str());

lua_close(state);
}
Expand Down
Loading