1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
|
/**
* @file
* @author 2012-2014 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de)
* @copyright Simplified BSD
*
* @cond
* This program is free software: you can redistribute it and/or modify
* it under the terms of the FreeBSD license as published by the FreeBSD
* project.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the FreeBSD license along with this
* program. If not, see <http://www.opensource.org/licenses/bsd-license>.
* @endcond
*/
#include <boost/algorithm/string.hpp>
#include "uscxml/Common.h"
#include "uscxml/util/String.h"
#include "LuaDataModel.h"
// disable forcing to bool performance warning
#pragma warning(push)
#pragma warning(disable : 4800)
#include "LuaBridge.h"
#pragma warning(pop)
#include "uscxml/dom/DOMUtils.h"
#include "uscxml/Message.h"
#include <glog/logging.h>
#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;
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;
}
}
if (data.compound.size() > 0) {
luaData = luabridge::newTable(_luaState);
std::map<std::string, Data>::const_iterator compoundIter = data.compound.begin();
while(compoundIter != data.compound.end()) {
luaData[compoundIter->first] = getDataAsLua(_luaState, compoundIter->second);
compoundIter++;
}
luaData["inspect"] = luaInspect;
return luaData;
}
if (data.array.size() > 0) {
luaData = luabridge::newTable(_luaState);
std::list<Data>::const_iterator arrayIter = data.array.begin();
uint32_t index = 0;
while(arrayIter != data.array.end()) {
luaData[index++] = getDataAsLua(_luaState, *arrayIter);
arrayIter++;
}
luaData["inspect"] = luaInspect;
return luaData;
}
if (data.atom.size() > 0) {
switch (data.type) {
case Data::VERBATIM: {
// luaData = "\"" + data.atom + "\"";
luaData = data.atom;
break;
}
case Data::INTERPRETED: {
if (isNumeric(data.atom.c_str(), 10)) {
if (data.atom.find(".") != std::string::npos) {
luaData = strTo<double>(data.atom);
} else {
luaData = strTo<long>(data.atom);
}
} else {
luaData = data.atom;
}
}
}
return luaData;
}
return luaData;
}
LuaDataModel::LuaDataModel() {
_luaState = NULL;
}
static int luaInFunction(lua_State * l) {
luabridge::LuaRef ref = luabridge::getGlobal(l, "__interpreter");
InterpreterInfo* interpreter = ref.cast<InterpreterInfo*>();
int stackSize = lua_gettop(l);
for (size_t i = 0; i < stackSize; i++) {
if (!lua_isstring(l, -1 - i))
continue;
std::string stateName = lua_tostring(l, -1 - i);
if (interpreter->isInState(stateName))
continue;
lua_pushboolean(l, 0);
return 1;
}
lua_pushboolean(l, 1);
return 1;
}
boost::shared_ptr<DataModelImpl> LuaDataModel::create(InterpreterInfo* interpreter) {
boost::shared_ptr<LuaDataModel> dm = boost::shared_ptr<LuaDataModel>(new LuaDataModel());
dm->_interpreter = interpreter;
dm->_luaState = luaL_newstate();
luaL_openlibs(dm->_luaState);
try {
const luabridge::LuaRef& requireFunc = luabridge::getGlobal(dm->_luaState, "require");
const luabridge::LuaRef& resultLxp = requireFunc("lxp");
const luabridge::LuaRef& resultLxpLOM = requireFunc("lxp.lom");
// MSVC compiler bug with implicit cast operators, see comments in LuaRef class
if ((bool)resultLxp && (bool)resultLxpLOM) {
_luaHasXMLParser = true;
luabridge::setGlobal(dm->_luaState, resultLxp, "lxp");
luabridge::setGlobal(dm->_luaState, resultLxpLOM, "lxp.lom");
}
} catch (luabridge::LuaException e) {
LOG(INFO) << e.what();
}
luabridge::getGlobalNamespace(dm->_luaState).beginClass<InterpreterInfo>("Interpreter").endClass();
luabridge::setGlobal(dm->_luaState, dm->_interpreter, "__interpreter");
luabridge::getGlobalNamespace(dm->_luaState).addCFunction("In", luaInFunction);
luabridge::LuaRef ioProcTable = luabridge::newTable(dm->_luaState);
std::map<std::string, IOProcessor>::const_iterator ioProcIter = dm->_interpreter->getIOProcessors().begin();
while(ioProcIter != dm->_interpreter->getIOProcessors().end()) {
Data ioProcData = ioProcIter->second.getDataModelVariables();
ioProcTable[ioProcIter->first] = getDataAsLua(dm->_luaState, ioProcData);
ioProcIter++;
}
luabridge::setGlobal(dm->_luaState, ioProcTable, "_ioprocessors");
luabridge::setGlobal(dm->_luaState, dm->_interpreter->getName(), "_name");
luabridge::setGlobal(dm->_luaState, dm->_interpreter->getSessionId(), "_sessionid");
return dm;
}
LuaDataModel::~LuaDataModel() {
if (_luaState != NULL)
lua_close(_luaState);
}
void LuaDataModel::pushContext() {
}
void LuaDataModel::popContext() {
}
void LuaDataModel::initialize() {
}
static Data getLuaAsData(const luabridge::LuaRef& lua) {
Data data;
if (lua.isFunction()) {
// TODO: this might lead to a stack-overflow
luabridge::LuaRef luaEvald = lua();
return getLuaAsData(luaEvald);
} else if(lua.isLightUserdata() || lua.isUserdata()) {
// not sure what to do
} else if(lua.isThread()) {
// not sure what to do
} else if(lua.isNil()) {
data.atom = "undefined";
data.type = Data::INTERPRETED;
} else if(lua.isString()) {
data.atom = lua.tostring();
data.type = Data::VERBATIM;
} else if(lua.isNumber()) {
data.atom = lua.tostring();
data.type = Data::INTERPRETED;
} else if(lua.isTable()) {
for (luabridge::Iterator iter (lua); !iter.isNil (); ++iter) {
luabridge::LuaRef luaKey = iter.key();
luabridge::LuaRef luaVal = *iter;
data.compound[luaKey.tostring()] = getLuaAsData(luaVal);
}
}
return data;
}
void LuaDataModel::setEvent(const Event& event) {
luabridge::LuaRef luaEvent(_luaState);
luaEvent = luabridge::newTable(_luaState);
luaEvent["name"] = event.name;
luaEvent["raw"] = event.raw;
luaEvent["xml"] = event.xml;
luaEvent["origin"] = event.origin;
luaEvent["origintype"] = event.origintype;
luaEvent["content"] = event.content;
luaEvent["invokeId"] = event.invokeid;
luaEvent["sendId"] = event.sendid;
luaEvent["inspect"] = luaInspect;
switch (event.eventType) {
case Event::INTERNAL:
luaEvent["type"] = "internal";
break;
case Event::EXTERNAL:
luaEvent["type"] = "external";
break;
case Event::PLATFORM:
luaEvent["type"] = "platform";
break;
default:
break;
}
if (event.dom) {
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.dom;
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");
}
} else if (event.content.length() > 0) {
// _event.data is a string or JSON
Data json = Data::fromJSON(event.content);
if (!json.empty()) {
luaEvent["data"] = getDataAsLua(_luaState, json);
} else {
// test179
std::string trimmed = boost::trim_copy(event.content);
if ((boost::starts_with(trimmed, "'") && boost::ends_with(trimmed, "'")) ||
(boost::starts_with(trimmed, "\"") && boost::ends_with(trimmed, "\""))) {
luaEvent["data"] = spaceNormalize(event.content);
} else {
Data tmp(event.content, Data::INTERPRETED);
luaEvent["data"] = getDataAsLua(_luaState, tmp);
}
}
} else {
// _event.data is KVP
Event eventCopy(event);
if (!eventCopy.params.empty()) {
Event::params_t::iterator paramIter = eventCopy.params.begin();
while(paramIter != eventCopy.params.end()) {
eventCopy.data.compound[paramIter->first] = paramIter->second;
paramIter++;
}
}
if (!eventCopy.namelist.empty()) {
Event::namelist_t::iterator nameListIter = eventCopy.namelist.begin();
while(nameListIter != eventCopy.namelist.end()) {
eventCopy.data.compound[nameListIter->first] = nameListIter->second;
nameListIter++;
}
}
if (!eventCopy.data.empty()) {
luabridge::LuaRef luaData = getDataAsLua(_luaState, eventCopy.data);
assert(luaEvent.isTable());
assert(luaData.isTable());
luaEvent["data"] = luaData;
}
}
luabridge::setGlobal(_luaState, luaEvent, "_event");
}
Data LuaDataModel::getStringAsData(const std::string& content) {
// Data data = Data::fromJSON(content);
Data data;
if (data.empty()) {
std::string trimmedExpr = boost::trim_copy(content);
if (!boost::starts_with(trimmedExpr, "return")) {
trimmedExpr = "return(" + trimmedExpr + ");";
}
int retVals = luaEval(Arabica::DOM::Element<std::string>(), trimmedExpr);
if (retVals == 1) {
data = getLuaAsData(luabridge::LuaRef::fromStack(_luaState, -1));
}
lua_pop(_luaState, retVals);
}
return data;
}
bool LuaDataModel::validate(const std::string& location, const std::string& schema) {
return true;
}
bool LuaDataModel::isLocation(const std::string& expr) {
return true;
}
bool LuaDataModel::isValidSyntax(const std::string& expr) {
int preStack = lua_gettop(_luaState);
int err = luaL_loadstring (_luaState, expr.c_str());
// clean stack again
lua_pop(_luaState, lua_gettop(_luaState) - preStack);
if (err == LUA_ERRSYNTAX)
return false;
return true;
}
uint32_t LuaDataModel::getLength(const std::string& expr) {
// we need the result of the expression on the lua stack -> has to "return"!
std::string trimmedExpr = boost::trim_copy(expr);
if (!boost::starts_with(trimmedExpr, "return")) {
trimmedExpr = "return(#" + trimmedExpr + ")";
}
int retVals = luaEval(Arabica::DOM::Element<std::string>(), trimmedExpr);
if (retVals == 1 && lua_isnumber(_luaState, -1)) {
int result = lua_tointeger(_luaState, -1);
lua_pop(_luaState, 1);
return result;
}
lua_pop(_luaState, retVals);
ERROR_EXECUTION_THROW("'" + expr + "' does not evaluate to an array.");
return 0;
}
void LuaDataModel::setForeach(const std::string& item,
const std::string& array,
const std::string& index,
uint32_t iteration) {
iteration++; // test153: arrays start at 1
const luabridge::LuaRef& arrRef = luabridge::getGlobal(_luaState, array.c_str());
if (arrRef.isTable()) {
// trigger syntax error for invalid items
int retVals = luaEval(Arabica::DOM::Element<std::string>(), "return(" + item + ");");
lua_pop(_luaState, retVals);
const luabridge::LuaRef& val = arrRef[iteration];
luabridge::setGlobal(_luaState, val, item.c_str());
if (index.length() > 0) {
// assign iteration element to index
luabridge::setGlobal(_luaState, iteration, index.c_str());
}
}
}
void LuaDataModel::eval(const Arabica::DOM::Element<std::string>& scriptElem,
const std::string& expr) {
luaEval(scriptElem, expr);
}
int LuaDataModel::luaEval(const Arabica::DOM::Element<std::string>& scriptElem,
const std::string& expr) {
int preStack = lua_gettop(_luaState);
int error = luaL_loadstring(_luaState, expr.c_str()) || lua_pcall(_luaState, 0, LUA_MULTRET, 0);
if (error) {
std::string errMsg = lua_tostring(_luaState, -1);
lua_pop(_luaState, 1); /* pop error message from the stack */
ERROR_EXECUTION_THROW(errMsg);
}
int postStack = lua_gettop(_luaState);
return postStack - preStack;
}
bool LuaDataModel::isDeclared(const std::string& expr) {
// see: http://lua-users.org/wiki/DetectingUndefinedVariables
return true;
}
void LuaDataModel::assign(const Arabica::DOM::Element<std::string>& assignElem,
const Arabica::DOM::Node<std::string>& node,
const std::string& content) {
std::string key;
if (HAS_ATTR(assignElem, "id")) {
key = ATTR(assignElem, "id");
} else if (HAS_ATTR(assignElem, "location")) {
key = ATTR(assignElem, "location");
}
if (key.length() == 0) {
ERROR_EXECUTION_THROW("Assign element has neither id nor location");
}
// flags on attribute are ignored?
if (key.compare("_sessionid") == 0) // test 322
ERROR_EXECUTION_THROW("Cannot assign to _sessionId");
if (key.compare("_name") == 0)
ERROR_EXECUTION_THROW("Cannot assign to _name");
if (key.compare("_ioprocessors") == 0) // test 326
ERROR_EXECUTION_THROW("Cannot assign to _ioprocessors");
if (key.compare("_invokers") == 0)
ERROR_EXECUTION_THROW("Cannot assign to _invokers");
if (key.compare("_event") == 0)
ERROR_EXECUTION_THROW("Cannot assign to _event");
// lua_pushnil(_luaState);
// lua_setglobal(_luaState, key.c_str());
// luabridge::setGlobal(_luaState, luabridge::Nil(), key.c_str());
// luabridge::LuaRef val = luabridge::getGlobal(_luaState, key.c_str());
// std::cout << val.tostring() << std::endl;
int retVals = 0;
if (HAS_ATTR(assignElem, "expr")) {
retVals = luaEval(Arabica::DOM::Element<std::string>(), key + " = " + ATTR(assignElem, "expr") + ";");
} else if (node) {
ERROR_EXECUTION_THROW("Cannot assign xml nodes in lua datamodel");
} else if (content.size() > 0) {
try {
eval(Arabica::DOM::Element<std::string>(), key + " = " + content + ";");
} catch (...) {
eval(Arabica::DOM::Element<std::string>(), key + " = " + "\"" + spaceNormalize(content) + "\";");
}
} else {
eval(Arabica::DOM::Element<std::string>(), key + " = " + "nil;");
}
// val = luabridge::getGlobal(_luaState, key.c_str());
// std::cout << val.tostring() << std::endl;
}
void LuaDataModel::assign(const std::string& location, const Data& data) {
luabridge::setGlobal(_luaState, getDataAsLua(_luaState, data), location.c_str());
}
void LuaDataModel::init(const Arabica::DOM::Element<std::string>& dataElem,
const Arabica::DOM::Node<std::string>& node,
const std::string& content) {
assign(dataElem, node, content);
}
void LuaDataModel::init(const std::string& location, const Data& data) {
assign(location, data);
}
/**
* The boolean expression language consists of the In predicate only. It has the
* form 'In(id)', where id is the id of a state in the enclosing state machine.
* The predicate must return 'true' if and only if that state is in the current
* state configuration.
*/
bool LuaDataModel::evalAsBool(const Arabica::DOM::Element<std::string>& node, const std::string& expr) {
// we need the result of the expression on the lua stack -> has to "return"!
std::string trimmedExpr = boost::trim_copy(expr);
if (!boost::starts_with(trimmedExpr, "return")) {
trimmedExpr = "return(" + trimmedExpr + ");";
}
int retVals = luaEval(Arabica::DOM::Element<std::string>(), trimmedExpr);
if (retVals == 1 && lua_isboolean(_luaState, -1)) {
bool result = lua_toboolean(_luaState, -1);
lua_pop(_luaState, 1);
return result;
}
lua_pop(_luaState, retVals);
return false;
}
std::string LuaDataModel::evalAsString(const std::string& expr) {
std::string trimmedExpr = boost::trim_copy(expr);
if (!boost::starts_with(trimmedExpr, "return")) {
trimmedExpr = "return(" + trimmedExpr + ")";
}
int retVals = luaEval(Arabica::DOM::Element<std::string>(), trimmedExpr);
if (retVals == 1 && lua_isstring(_luaState, -1)) {
std::string result = lua_tostring(_luaState, -1);
lua_pop(_luaState, 1);
return result;
}
lua_pop(_luaState, retVals);
return "";
}
std::string LuaDataModel::andExpressions(std::list<std::string> exprs) {
std::stringstream exprSS;
std::list<std::string>::const_iterator listIter;
std::string andExpr;
for (listIter = exprs.begin(); listIter != exprs.end(); listIter++) {
exprSS << andExpr << *listIter;
andExpr = " && ";
}
return exprSS.str();
}
}
|