summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp')
-rw-r--r--src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp117
1 files changed, 61 insertions, 56 deletions
diff --git a/src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp b/src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp
index 92866ee..15cbf9d 100644
--- a/src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp
+++ b/src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp
@@ -36,34 +36,44 @@
#endif
#include "uscxml/messages/Event.h"
+
#ifndef NO_XERCESC
#include "uscxml/util/DOM.h"
#endif
+
#include "uscxml/interpreter/Logging.h"
#include <boost/algorithm/string.hpp>
-//#include "LuaDOM.cpp.inc"
+//#include "LuaDOM.cpp.inc" // TODO: activate XercesC bindings for test 530
#ifdef BUILD_AS_PLUGINS
#include <Pluma/Connector.hpp>
#endif
-namespace uscxml {
-
-#ifdef BUILD_AS_PLUGINS
-PLUMA_CONNECTOR
-bool pluginConnect(pluma::Host& host) {
- host.add( new LuaDataModelProvider() );
- return true;
-}
-#endif
-
//static int luaInspect(lua_State * l) {
// return 0;
//}
-bool _luaHasXMLParser = false;
+//bool _luaHasXMLParser = false;
+using namespace XERCESC_NS;
+
+#ifndef NO_XERCESC
+#include "LuaDOM.cpp.inc"
+#else
+#include "LuaEvent.cpp.inc"
+#endif
+
+namespace uscxml {
+
+#ifdef BUILD_AS_PLUGINS
+ PLUMA_CONNECTOR
+ bool pluginConnect(pluma::Host& host) {
+ host.add( new LuaDataModelProvider() );
+ return true;
+ }
+#endif
+
static int luaEval(lua_State* luaState, const std::string& expr) {
int preStack = lua_gettop(luaState);
int error = luaL_loadstring(luaState, expr.c_str()) || lua_pcall(luaState, 0, LUA_MULTRET, 0);
@@ -86,8 +96,13 @@ static Data getLuaAsData(lua_State* _luaState, const luabridge::LuaRef& lua) {
data.atom = "__tmpFunc";
data.type = Data::INTERPRETED;
} else if(lua.isLightUserdata() || lua.isUserdata()) {
- // not sure what to do
- } else if(lua.isThread()) {
+#ifndef NO_XERCESC
+ int err = SWIG_Lua_ConvertPtr(_luaState, 0, (void**)&(data.node), SWIGTYPE_p_XERCES_CPP_NAMESPACE__DOMNode, SWIG_POINTER_DISOWN);
+ if (err == SWIG_ERROR)
+ data.node = NULL;
+#endif
+
+ } else if(lua.isThread()) {
// not sure what to do
} else if(lua.type() == LUA_TBOOLEAN) {
// why is there no isBoolean in luabridge?
@@ -106,7 +121,7 @@ static Data getLuaAsData(lua_State* _luaState, const luabridge::LuaRef& lua) {
} else if(lua.isString()) {
data.atom = lua.cast<std::string>();
data.type = Data::VERBATIM;
- } else if(lua.isTable()) {
+ } else if(lua.isTable()) {
// check whether it is to be interpreted as a map or an array
bool isArray = true;
std::map<std::string, luabridge::LuaRef> luaItems;
@@ -145,20 +160,14 @@ static luabridge::LuaRef getDataAsLua(lua_State* _luaState, const Data& data) {
luabridge::LuaRef luaData (_luaState);
if (data.node) {
- if (_luaHasXMLParser) {
- const luabridge::LuaRef& luaLom = luabridge::getGlobal(_luaState, "lxp.lom");
- const luabridge::LuaRef& luaLomParse = luaLom["parse"];
- assert(luaLomParse.isFunction());
- std::stringstream luaXMLSS;
- luaXMLSS << data.node;
- try {
- luaData = luaLomParse(luaXMLSS.str());
- } catch (luabridge::LuaException e) {
- ERROR_EXECUTION_THROW(e.what());
- }
- return luaData;
- }
- }
+#ifndef NO_XERCESC
+ SWIG_Lua_NewPointerObj(_luaState, data.node, SWIGTYPE_p_XERCES_CPP_NAMESPACE__DOMNode, SWIG_POINTER_DISOWN);
+ luaData = luabridge::LuaRef::fromStack(_luaState, 1);
+ return luaData;
+#else
+ ERROR_EXECUTION_THROW("No DOM support in Lua datamodel");
+#endif
+ }
if (data.compound.size() > 0) {
luaData = luabridge::newTable(_luaState);
std::map<std::string, Data>::const_iterator compoundIter = data.compound.begin();
@@ -248,6 +257,9 @@ void LuaDataModel::setup() {
_luaState = luaL_newstate();
luaL_openlibs(_luaState);
+ SWIG_init(_luaState);
+
+#if 0
try {
const luabridge::LuaRef& requireFunc = luabridge::getGlobal(_luaState, "require");
if (requireFunc.isFunction()) {
@@ -264,7 +276,8 @@ void LuaDataModel::setup() {
} catch (luabridge::LuaException e) {
LOG(_callbacks->getLogger(), USCXML_INFO) << e.what() << std::endl;
}
-
+#endif
+
luabridge::getGlobalNamespace(_luaState).beginClass<LuaDataModel>("DataModel").endClass();
luabridge::setGlobal(_luaState, this, "__datamodel");
@@ -308,7 +321,6 @@ void LuaDataModel::setEvent(const Event& event) {
luabridge::LuaRef luaEvent(_luaState);
luaEvent = luabridge::newTable(_luaState);
-
luaEvent["name"] = event.name;
if (event.raw.size() > 0)
luaEvent["raw"] = event.raw;
@@ -338,20 +350,12 @@ void LuaDataModel::setEvent(const Event& event) {
}
if (event.data.node) {
- if (_luaHasXMLParser) {
- const luabridge::LuaRef& luaLom = luabridge::getGlobal(_luaState, "lxp.lom");
- const luabridge::LuaRef& luaLomParse = luaLom["parse"];
- assert(luaLomParse.isFunction());
- std::stringstream luaXMLSS;
- luaXMLSS << event.data.node;
- try {
- luaEvent["data"] = luaLomParse(luaXMLSS.str());
- } catch (luabridge::LuaException e) {
- ERROR_EXECUTION_THROW(e.what());
- }
- } else {
- ERROR_EXECUTION_THROW("No DOM support in Lua datamodel");
- }
+#ifndef NO_XERCESC
+ SWIG_Lua_NewPointerObj(_luaState, event.data.node, SWIGTYPE_p_XERCES_CPP_NAMESPACE__DOMNode, SWIG_POINTER_DISOWN);
+ luaEvent["data"] = luabridge::LuaRef::fromStack(_luaState, 1);
+#else
+ ERROR_EXECUTION_THROW("No DOM support in Lua datamodel");
+#endif
} else {
// _event.data is KVP
Data d = event.data;
@@ -380,6 +384,7 @@ void LuaDataModel::setEvent(const Event& event) {
}
luabridge::setGlobal(_luaState, luaEvent, "_event");
+
}
Data LuaDataModel::evalAsData(const std::string& content) {
@@ -403,6 +408,10 @@ void LuaDataModel::eval(const std::string& content) {
lua_pop(_luaState, retVals);
}
+bool LuaDataModel::isLegalDataValue(const std::string& expr) {
+ return isValidSyntax("local __tmp = " + expr);
+}
+
bool LuaDataModel::isValidSyntax(const std::string& expr) {
int preStack = lua_gettop(_luaState);
int err = luaL_loadstring (_luaState, expr.c_str());
@@ -499,18 +508,14 @@ void LuaDataModel::assign(const std::string& location, const Data& data, const s
ERROR_EXECUTION_THROW("Cannot assign to _event");
if (data.node) {
- ERROR_EXECUTION_THROW("Cannot assign xml nodes in lua datamodel");
-
- // TODO: DOM is prepared by swig
-
-// return SWIG_JSC_NewPointerObj(_ctx,
-// (void*)node,
-// SWIG_TypeDynamicCast(SWIGTYPE_p_XERCES_CPP_NAMESPACE__DOMNode,
-// SWIG_as_voidptrptr(&node)),
-// 0);
-
+#ifndef NO_XERCESC
-// JSObjectSetProperty(_ctx, JSContextGetGlobalObject(_ctx), JSStringCreateWithUTF8CString(location.c_str()), getNodeAsValue(data.node), 0, &exception);
+ SWIG_Lua_NewPointerObj(_luaState, data.node, SWIGTYPE_p_XERCES_CPP_NAMESPACE__DOMNode, SWIG_POINTER_DISOWN);
+ lua_setglobal(_luaState, "__tmpAssign");
+ eval(location + "= __tmpAssign");
+#else
+ ERROR_EXECUTION_THROW("Cannot assign xml nodes in lua datamodel");
+#endif
} else {