From ad2653c30b78b2951eb79d303a9e3ab52f4a2def Mon Sep 17 00:00:00 2001 From: Stefan Radomski Date: Thu, 4 Aug 2016 23:04:31 +0200 Subject: Fixed issue 88 --- src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp | 45 +++++++++++++---------- test/issues/test-issue88.scxml | 2 +- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp b/src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp index 225e34c..ca8cc60 100644 --- a/src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp +++ b/src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp @@ -470,21 +470,20 @@ void LuaDataModel::assign(const std::string& location, const Data& data) { int retVals = luaEval(_luaState, location + " = " + location); lua_pop(_luaState, retVals); - // normalize location into dot notation - // TODO: This is still wrong as it treads arrays as kvps! - std::string normedLocation; - normedLocation.resize(location.size()); - for (size_t i = 0, j = 0; i < location.size(); i++, j++) { - if (location[i] == ']') { - j++; - } else if (location[i] == '[') { - normedLocation[i] = '.'; - } else { - normedLocation[i] = location[i]; + std::list > idPath; + size_t start = 0; + for (size_t i = 0; i < location.size(); i++) { + if (location[i] == '.' || location[i] == '[') { + idPath.push_back(std::make_pair(location.substr(start, i - start), false)); + start = i + 1; + } else if (location[i] == ']') { + idPath.push_back(std::make_pair(location.substr(start, i - start), true)); + start = i + 1; } } - - std::list idPath = tokenize(normedLocation, '.'); + if (start < location.size()) + idPath.push_back(std::make_pair(location.substr(start, location.size() - start), false)); + if (idPath.size() == 0) return; @@ -495,23 +494,31 @@ void LuaDataModel::assign(const std::string& location, const Data& data) { luabridge::setGlobal(_luaState, lua, location.c_str()); } else { - std::string globalId = idPath.front(); + auto globalId = idPath.front(); idPath.pop_front(); - std::string field = idPath.back(); + auto field = idPath.back(); idPath.pop_back(); - luabridge::LuaRef topValue = luabridge::getGlobal(_luaState, globalId.c_str()); + luabridge::LuaRef topValue = luabridge::getGlobal(_luaState, globalId.first.c_str()); luabridge::LuaRef value = topValue; for (auto ident : idPath) { if (!value.isTable()) value = luabridge::newTable(_luaState); - luabridge::LuaRef tmp = value[ident]; - value = tmp; + if (ident.second) { + luabridge::LuaRef tmp = value[strTo(ident.first)]; + } else { + luabridge::LuaRef tmp = value[ident]; + value = tmp; + } + } + if (field.second) { + value[strTo(field.first)] = lua; + } else { + value[field.first] = lua; } - value[field] = lua; } diff --git a/test/issues/test-issue88.scxml b/test/issues/test-issue88.scxml index 3ac55c7..0a79f77 100644 --- a/test/issues/test-issue88.scxml +++ b/test/issues/test-issue88.scxml @@ -18,7 +18,7 @@ - + -- cgit v0.12