diff options
author | Stefan Radomski <github@mintwerk.de> | 2016-08-03 21:58:05 (GMT) |
---|---|---|
committer | Stefan Radomski <github@mintwerk.de> | 2016-08-03 21:58:05 (GMT) |
commit | 398630710aef647a2d68705f46c4691994c0f5c9 (patch) | |
tree | 34fb7e66b750e75a506eade0483fb38bc9826217 /src/uscxml/plugins/datamodel | |
parent | e46dd37dc472b467ac8da137677bdcd3c4770b29 (diff) | |
download | uscxml-398630710aef647a2d68705f46c4691994c0f5c9.zip uscxml-398630710aef647a2d68705f46c4691994c0f5c9.tar.gz uscxml-398630710aef647a2d68705f46c4691994c0f5c9.tar.bz2 |
Fixed parts of issue 88
Diffstat (limited to 'src/uscxml/plugins/datamodel')
-rw-r--r-- | src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp | 49 |
1 files changed, 46 insertions, 3 deletions
diff --git a/src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp b/src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp index 717def9..225e34c 100644 --- a/src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp +++ b/src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp @@ -70,7 +70,7 @@ static Data getLuaAsData(lua_State* _luaState, const luabridge::LuaRef& lua) { data.atom = "nil"; data.type = Data::INTERPRETED; } else if(lua.isNumber()) { - data.atom = toStr(lua.cast<int>()); + data.atom = toStr(lua.cast<double>()); data.type = Data::INTERPRETED; } else if(lua.isString()) { data.atom = lua.cast<std::string>(); @@ -470,9 +470,52 @@ void LuaDataModel::assign(const std::string& location, const Data& data) { int retVals = luaEval(_luaState, location + " = " + location); lua_pop(_luaState, retVals); - luabridge::LuaRef lua = getDataAsLua(_luaState, data); - luabridge::setGlobal(_luaState, lua, location.c_str()); + // 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<std::string> idPath = tokenize(normedLocation, '.'); + if (idPath.size() == 0) + return; + + luabridge::LuaRef lua = getDataAsLua(_luaState, data); + + if (idPath.size() == 1) { + // trivial case where we reference a simple toplevel identifier + luabridge::setGlobal(_luaState, lua, location.c_str()); + + } else { + std::string globalId = idPath.front(); + idPath.pop_front(); + + std::string field = idPath.back(); + idPath.pop_back(); + + luabridge::LuaRef topValue = luabridge::getGlobal(_luaState, globalId.c_str()); + luabridge::LuaRef value = topValue; + + for (auto ident : idPath) { + if (!value.isTable()) + value = luabridge::newTable(_luaState); + + luabridge::LuaRef tmp = value[ident]; + value = tmp; + } + value[field] = lua; + } + + // std::cout << Data::toJSON(evalAsData(location)) << std::endl; } } |