summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp
blob: 2071d396cdc56ebad66a684a3573ffa223784c79 (plain)
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/**
 *  @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 "uscxml/Common.h"
#include "uscxml/util/URL.h"
#include "uscxml/util/String.h"

#include "LuaDataModel.h"

// disable forcing to bool performance warning
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4800)
#endif

#include "LuaBridge.h"

#ifdef _MSC_VER
#pragma warning(pop)
#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"

#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 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);
	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;
}

static Data getLuaAsData(lua_State* _luaState, const luabridge::LuaRef& lua) {
	Data data;
	if (lua.isFunction()) {
		// TODO: this might lead to a stack-overflow
		luabridge::LuaRef luaEvald = lua();
		return getLuaAsData(_luaState, 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 = "nil";
		data.type = Data::INTERPRETED;
	} else if(lua.isNumber()) {
		data.atom = toStr(lua.cast<double>());
		data.type = Data::INTERPRETED;
	} else if(lua.isString()) {
		data.atom = lua.cast<std::string>();
		data.type = Data::VERBATIM;
	} else if(lua.isTable()) {
//		bool isArray = false;
//		bool isMap = false;
		for (luabridge::Iterator iter (lua); !iter.isNil(); ++iter) {
			luabridge::LuaRef luaKey = iter.key();
			luabridge::LuaRef luaVal = *iter;
			if (luaKey.isString()) {
//				assert(!isArray);
//				isMap = true;
				// luaKey.tostring() is not working?! see issue84
				data.compound[luaKey.cast<std::string>()] = getLuaAsData(_luaState, luaVal);
			} else {
//				assert(!isMap);
//				isArray = true;
				data.array.push_back(getLuaAsData(_luaState, luaVal));
			}
		}
	}
	return data;
}

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);
			luaData.append(getDataAsLua(_luaState, *arrayIter));
			arrayIter++;
		}
//		luaData["inspect"] = luaInspect;
		return luaData;
	}
	if (data.atom.size() > 0) {
		switch (data.type) {
		case Data::VERBATIM: {
			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 {
				int retVals = luaEval(_luaState, "return(" + data.atom + ");");
				if (retVals == 1) {
					luaData = luabridge::LuaRef::fromStack(_luaState, -1);
				}
				lua_pop(_luaState, retVals);
			}
		}
		}
		return luaData;
	}
	// hopefully this is nil
	return luabridge::LuaRef(_luaState);
}

LuaDataModel::LuaDataModel() {
	_luaState = NULL;
}

int LuaDataModel::luaInFunction(lua_State * l) {
	luabridge::LuaRef ref = luabridge::getGlobal(l, "__datamodel");
	LuaDataModel* dm = ref.cast<LuaDataModel*>();

	int stackSize = lua_gettop(l);
	for (int i = 0; i < stackSize; i++) {
		if (!lua_isstring(l, -1 - i))
			continue;
		std::string stateName = lua_tostring(l, -1 - i);
		if (dm->_callbacks->isInState(stateName))
			continue;
		lua_pushboolean(l, 0);
		return 1;
	}
	lua_pushboolean(l, 1);
	return 1;
}

std::shared_ptr<DataModelImpl> LuaDataModel::create(DataModelCallbacks* callbacks) {
	std::shared_ptr<LuaDataModel> dm(new LuaDataModel());

	dm->_callbacks = callbacks;
	dm->_luaState = luaL_newstate();
	luaL_openlibs(dm->_luaState);

	try {
		const luabridge::LuaRef& requireFunc = luabridge::getGlobal(dm->_luaState, "require");
		if (requireFunc.isFunction()) {
			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(dm->_callbacks->getLogger(), USCXML_INFO) << e.what() << std::endl;
	}

	luabridge::getGlobalNamespace(dm->_luaState).beginClass<LuaDataModel>("DataModel").endClass();
	luabridge::setGlobal(dm->_luaState, dm.get(), "__datamodel");

	luabridge::getGlobalNamespace(dm->_luaState).addCFunction("In", luaInFunction);

	luabridge::LuaRef ioProcTable = luabridge::newTable(dm->_luaState);
	std::map<std::string, IOProcessor> ioProcs = dm->_callbacks->getIOProcessors();
	std::map<std::string, IOProcessor>::const_iterator ioProcIter = ioProcs.begin();
	while(ioProcIter != ioProcs.end()) {
		Data ioProcData = ioProcIter->second.getDataModelVariables();
		ioProcTable[ioProcIter->first] = getDataAsLua(dm->_luaState, ioProcData);
		ioProcIter++;
	}
	luabridge::setGlobal(dm->_luaState, ioProcTable, "_ioprocessors");

	luabridge::LuaRef invTable = luabridge::newTable(dm->_luaState);
	std::map<std::string, Invoker> invokers = dm->_callbacks->getInvokers();
	std::map<std::string, Invoker>::const_iterator invIter = invokers.begin();
	while(invIter != invokers.end()) {
		Data invData = invIter->second.getDataModelVariables();
		invTable[invIter->first] = getDataAsLua(dm->_luaState, invData);
		invIter++;
	}
	luabridge::setGlobal(dm->_luaState, invTable, "_invokers");

	luabridge::setGlobal(dm->_luaState, dm->_callbacks->getName(), "_name");
	luabridge::setGlobal(dm->_luaState, dm->_callbacks->getSessionId(), "_sessionid");

	return dm;
}

LuaDataModel::~LuaDataModel() {
	if (_luaState != NULL)
		lua_close(_luaState);
}

void LuaDataModel::addExtension(DataModelExtension* ext) {
	ERROR_EXECUTION_THROW("Extensions unimplemented in lua datamodel");
}

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;
	if (event.origin.size() > 0)
		luaEvent["origin"] = event.origin;
	if (event.origintype.size() > 0)
		luaEvent["origintype"] = event.origintype;
	if (event.invokeid.size() > 0)
		luaEvent["invokeid"] = event.invokeid;
	if (!event.hideSendId)
		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.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");
		}
	} else {
		// _event.data is KVP
		Data d = event.data;

		if (!event.params.empty()) {
			Event::params_t::const_iterator paramIter = event.params.begin();
			while(paramIter != event.params.end()) {
				d.compound[paramIter->first] = paramIter->second;
				paramIter++;
			}
		}
		if (!event.namelist.empty()) {
			Event::namelist_t::const_iterator nameListIter = event.namelist.begin();
			while(nameListIter != event.namelist.end()) {
				d.compound[nameListIter->first] = nameListIter->second;
				nameListIter++;
			}
		}

		if (!d.empty()) {
			luabridge::LuaRef luaData = getDataAsLua(_luaState, d);
			assert(luaEvent.isTable());
			// assert(luaData.isTable()); // not necessarily test179
			luaEvent["data"] = luaData;
		}
	}

	luabridge::setGlobal(_luaState, luaEvent, "_event");
}

Data LuaDataModel::evalAsData(const std::string& content) {
	Data data;
	ErrorEvent originalError;

	std::string trimmedExpr = boost::trim_copy(content);

	try {
		int retVals = luaEval(_luaState, "return(" + trimmedExpr + ")");
		if (retVals == 1) {
			data = getLuaAsData(_luaState, luabridge::LuaRef::fromStack(_luaState, -1));
		}
		lua_pop(_luaState, retVals);
		return data;
	} catch (ErrorEvent e) {
		originalError = e;
	}

	int retVals = 0;
	try {
		// evaluate again without the return()
		retVals = luaEval(_luaState, trimmedExpr);
	} catch (ErrorEvent e) {
		throw originalError; // we will assume syntax error and throw
	}

#if 1
	// Note: Empty result is not an error test302, but how to do test488?
	if (retVals == 0 && !isValidSyntax(trimmedExpr)) {
		throw originalError; // we will assume syntax error and throw
	}
#endif

	try {
		if (retVals == 1) {
			data = getLuaAsData(_luaState, luabridge::LuaRef::fromStack(_luaState, -1));
		}
		lua_pop(_luaState, retVals);
		return data;

	} catch (ErrorEvent e) {
		throw e; // we will assume syntax error and throw
	}


	return data;
}

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(_luaState, trimmedExpr);

#if 1

	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;
#else

	if (retVals == 1) {
		luabridge::LuaRef luaData = luabridge::LuaRef::fromStack(_luaState, -1);
		if (luaData.isNumber()) {
			lua_pop(_luaState, retVals);
			return luaData.cast<int>();
		}
	}
	lua_pop(_luaState, retVals);

	ERROR_EXECUTION_THROW("'" + expr + "' does not evaluate to an array.");
	return 0;

#endif
}

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()) {

		// triggers syntax error for invalid items, test 152
		int retVals = luaEval(_luaState, item + " = " + array + "[" + toStr(iteration) + "]");
		lua_pop(_luaState, retVals);

		if (index.length() > 0) {
			int retVals = luaEval(_luaState, index + " = " + toStr(iteration));
			lua_pop(_luaState, retVals);
		}
	}
}

bool LuaDataModel::isDeclared(const std::string& expr) {
	// see: http://lua-users.org/wiki/DetectingUndefinedVariables
	return true;
}


void LuaDataModel::assign(const std::string& location, const Data& data, const std::map<std::string, std::string>& attr) {
	if (location.length() == 0) {
		ERROR_EXECUTION_THROW("Assign element has neither id nor location");
	}

	// flags on attribute are ignored?
	if (location.compare("_sessionid") == 0) // test 322
		ERROR_EXECUTION_THROW("Cannot assign to _sessionId");
	if (location.compare("_name") == 0)
		ERROR_EXECUTION_THROW("Cannot assign to _name");
	if (location.compare("_ioprocessors") == 0)  // test 326
		ERROR_EXECUTION_THROW("Cannot assign to _ioprocessors");
	if (location.compare("_invokers") == 0)
		ERROR_EXECUTION_THROW("Cannot assign to _invokers");
	if (location.compare("_event") == 0)
		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);


//        JSObjectSetProperty(_ctx, JSContextGetGlobalObject(_ctx), JSStringCreateWithUTF8CString(location.c_str()), getNodeAsValue(data.node), 0, &exception);
	} else {

		// trigger error.execution for undefined locations, test286 test311
		int retVals = luaEval(_luaState, location + " = " + location);
		lua_pop(_luaState, retVals);

		std::list<std::pair<std::string, bool> > 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;
			}
		}
		if (start < location.size())
			idPath.push_back(std::make_pair(location.substr(start, location.size() - start), false));

		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 {
			auto globalId = idPath.front();
			idPath.pop_front();

			auto field = idPath.back();
			idPath.pop_back();

			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);

				if (ident.second) {
					luabridge::LuaRef tmp = value[strTo<long>(ident.first)];
				} else {
					luabridge::LuaRef tmp = value[ident];
					value = tmp;
				}
			}
			if (field.second) {
				if (field.first.length() == 0) {
					ERROR_EXECUTION_THROW("Subscript operator is empty");
				}

				if (!isNumeric(field.first.c_str(), 10) && field.first[0] != '"' && field.first[0] != '\'') {
					// evaluate subscript as variable
					Data subscript = evalAsData(field.first);
					if (subscript.atom.length() > 0) {
						field.first = subscript.atom;
					} else {
						ERROR_EXECUTION_THROW("Evaluated subscript operator '" + subscript.asJSON() + "' is invalid");
					}
				}

				if (isNumeric(field.first.c_str(), 10)) {
					// numeric array subscript
					value[strTo<long>(field.first)] = lua;
				} else {
					// string array subscript
					value[field.first] = lua;
				}
			} else {
				value[field.first] = lua;
			}

		}

//        std::cout << Data::toJSON(evalAsData(location)) << std::endl;
	}
}

void LuaDataModel::init(const std::string& location, const Data& data, const std::map<std::string, std::string>& attr) {
	luabridge::setGlobal(_luaState, luabridge::Nil(), location.c_str());
	assign(location, data);
}

bool LuaDataModel::evalAsBool(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);

	int retVals = luaEval(_luaState, "return(" + trimmedExpr + ")");

	if (retVals == 1) {
		bool result = lua_toboolean(_luaState, -1);
		lua_pop(_luaState, 1);
		return result;
	}
	lua_pop(_luaState, retVals);

	return false;
}

Data LuaDataModel::getAsData(const std::string& content) {
	Data data;
	std::string trimmedExpr = boost::trim_copy(content);

	int retVals = luaEval(_luaState, "__tmp = " + content + "; return __tmp");
	if (retVals == 1) {
		data = getLuaAsData(_luaState, luabridge::LuaRef::fromStack(_luaState, -1));
	}
	lua_pop(_luaState, retVals);

	// escape as a string, this is sometimes the case with <content>
	if (data.atom == "nil" && data.type == Data::INTERPRETED) {
		int retVals = luaEval(_luaState, "__tmp = '" + content + "'; return __tmp");
		if (retVals == 1) {
			data = getLuaAsData(_luaState, luabridge::LuaRef::fromStack(_luaState, -1));
		}
		lua_pop(_luaState, retVals);
	}

	return data;
}

}