summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/datamodel/ecmascript/SpiderMonkey/SpiderMonkeyDataModel.cpp
blob: c867b22797b75142fb11aae4548d3f776570c506 (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
/**
 *  @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/config.h"
#include "SpiderMonkeyDataModel.h"

#include "uscxml/Message.h"
#include "uscxml/DOMUtils.h"
#include <glog/logging.h>

#ifdef BUILD_AS_PLUGINS
#include <Pluma/Connector.hpp>
#endif

namespace uscxml {

using namespace Arabica::XPath;
using namespace Arabica::DOM;

#ifdef BUILD_AS_PLUGINS
PLUMA_CONNECTOR
bool pluginConnect(pluma::Host& host) {
	host.add( new SpiderMonkeyDataModelProvider() );
	return true;
}
#endif

static JSClass global_class = { "global",
                                JSCLASS_NEW_RESOLVE | JSCLASS_GLOBAL_FLAGS,
                                JS_PropertyStub,
                                JS_PropertyStub,
                                JS_PropertyStub,
                                JS_PropertyStub,
                                JS_EnumerateStub,
                                JS_ResolveStub,
                                JS_ConvertStub,
                                nullptr,
                                JSCLASS_NO_OPTIONAL_MEMBERS
                              };


JSRuntime* SpiderMonkeyDataModel::_jsRuntime = NULL;

SpiderMonkeyDataModel::SpiderMonkeyDataModel() {
	_jsCtx = NULL;
}

SpiderMonkeyDataModel::~SpiderMonkeyDataModel() {
	if (_jsCtx)
		JS_DestroyContext(_jsCtx);
}

void SpiderMonkeyDataModel::reportError(JSContext *cx, const char *message, JSErrorReport *report) {
#if 0
	struct JSErrorReport {
		const char      *filename;      /* source file name, URL, etc., or null */
		uintN           lineno;         /* source line number */
		const char      *linebuf;       /* offending source line without final \n */
		const char      *tokenptr;      /* pointer to error token in linebuf */
		const jschar    *uclinebuf;     /* unicode (original) line buffer */
		const jschar    *uctokenptr;    /* unicode (original) token pointer */
		uintN           flags;          /* error/warning, etc. */
		uintN           errorNumber;    /* the error number, e.g. see js.msg */
		const jschar    *ucmessage;     /* the (default) error message */
		const jschar    **messageArgs;  /* arguments for the error message */
	};
	exceptionEvent.data.compound["stacktrace"] = Data(stackTrace, Data::VERBATIM);
#endif

	Event exceptionEvent;
	exceptionEvent.name = "error.execution";
	exceptionEvent.eventType = Event::PLATFORM;

	exceptionEvent.data.compound["cause"] = Data(message, Data::VERBATIM);;
	exceptionEvent.data.compound["filename"] = Data(report->filename, Data::VERBATIM);
	exceptionEvent.data.compound["sourceline"] = Data(report->linebuf, Data::VERBATIM);
	exceptionEvent.data.compound["linenumber"] = Data(report->lineno, Data::INTERPRETED);

	std::stringstream ssUnderline;
	for (int i = 0; i < (report->tokenptr - report->linebuf); i++)
		ssUnderline << " ";
	ssUnderline << "^";

	exceptionEvent.data.compound["sourcemark"] = Data(ssUnderline.str(), Data::VERBATIM);
	throw exceptionEvent;
}

boost::shared_ptr<DataModelImpl> SpiderMonkeyDataModel::create(InterpreterImpl* interpreter) {
	if (_jsRuntime == NULL) {
		JSRuntime *rt = JS_NewRuntime(8L * 1024L * 1024L);
		if (!rt) {
			throw std::bad_alloc();
		}
	}

	boost::shared_ptr<SpiderMonkeyDataModel> dm = boost::shared_ptr<SpiderMonkeyDataModel>(new SpiderMonkeyDataModel());
	dm->_interpreter = interpreter;
	dm->_jsCtx = JS_NewContext(_jsRuntime, 8192);
	if (!dm->_jsCtx) {
		throw std::bad_alloc();
	}
	JS_SetOptions(dm->_jsCtx, JSOPTION_VAROBJFIX);
	JS_SetErrorReporter(dm->_jsCtx, reportError);

	dm->_global = JS_NewObject(dm->_jsCtx, &global_class, nullptr, nullptr);
	if (!JS_InitStandardClasses(dm->_jsCtx, dm->_global)) {
		throw std::bad_alloc();
	}

	return dm;
}

void SpiderMonkeyDataModel::pushContext() {
}

void SpiderMonkeyDataModel::popContext() {
}

void SpiderMonkeyDataModel::initialize() {
}

void SpiderMonkeyDataModel::setEvent(const Event& event) {
}

Data SpiderMonkeyDataModel::getStringAsData(const std::string& content) {
	Data data;
	return data;
}


bool SpiderMonkeyDataModel::validate(const std::string& location, const std::string& schema) {
	return true;
}

bool SpiderMonkeyDataModel::isLocation(const std::string& expr) {
	return true;
}

uint32_t SpiderMonkeyDataModel::getLength(const std::string& expr) {
	return 0;
}

void SpiderMonkeyDataModel::setForeach(const std::string& item,
                                       const std::string& array,
                                       const std::string& index,
                                       uint32_t iteration) {
}

void SpiderMonkeyDataModel::eval(const Element<std::string>& scriptElem,
                                 const std::string& expr) {
}

bool SpiderMonkeyDataModel::isDeclared(const std::string& expr) {
	return true;
}

bool SpiderMonkeyDataModel::evalAsBool(const std::string& expr) {
	return evalAsBool(Arabica::DOM::Element<std::string>(), expr);
}

bool SpiderMonkeyDataModel::evalAsBool(const Arabica::DOM::Element<std::string>& node, const std::string& expr) {
}

std::string SpiderMonkeyDataModel::evalAsString(const std::string& expr) {
	return "";
}

double SpiderMonkeyDataModel::evalAsNumber(const std::string& expr) {
	return 0;
}

void SpiderMonkeyDataModel::assign(const Element<std::string>& assignElem,
                                   const Node<std::string>& node,
                                   const std::string& content) {
}

void SpiderMonkeyDataModel::assign(const std::string& location,
                                   const Data& data) {
}

void SpiderMonkeyDataModel::init(const Element<std::string>& dataElem,
                                 const Node<std::string>& doc,
                                 const std::string& content) {
};

void SpiderMonkeyDataModel::init(const std::string& location,
                                 const Data& data) {
}

std::string SpiderMonkeyDataModel::andExpressions(std::list<std::string> expressions) {
	if (expressions.size() == 0)
		return "";

	if (expressions.size() == 1)
		return *(expressions.begin());

	std::ostringstream exprSS;
	exprSS << "(";
	std::string conjunction = "";
	for (std::list<std::string>::const_iterator exprIter = expressions.begin();
	        exprIter != expressions.end();
	        exprIter++) {
		exprSS << conjunction << "(" << *exprIter << ")";
		conjunction = " && ";
	}
	exprSS << ")";
	return exprSS.str();
}

}