summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/datamodel/ecmascript/JavaScriptCore/JSCDataModel.cpp
blob: f856f9675d1b18a353952bd28144b12b924d8449 (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
#include "uscxml/Common.h"
#include "JSCDataModel.h"
#include "dom/JSCDOM.h"
#include "dom/JSCDocument.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 connect(pluma::Host& host) {
	host.add( new JSCDataModelProvider() );
	return true;
}
#endif

JSCDataModel::JSCDataModel() {
}

boost::shared_ptr<DataModelImpl> JSCDataModel::create(InterpreterImpl* interpreter) {
	boost::shared_ptr<JSCDataModel> dm = boost::shared_ptr<JSCDataModel>(new JSCDataModel());

	dm->_ctx = JSGlobalContextCreate(NULL);
	dm->_interpreter = interpreter;

	Arabica::DOM::JSCDOM* dom = new Arabica::DOM::JSCDOM();
	//  dom->interpreter = interpreter;
	dom->xpath = new Arabica::XPath::XPath<std::string>();
	dom->xpath->setNamespaceContext(interpreter->getNSContext());

	dm->eval("_ioprocessors = {};");

	Arabica::DOM::JSCDocument::JSCDocumentPrivate* privData = new Arabica::DOM::JSCDocument::JSCDocumentPrivate();
	privData->nativeObj = new Arabica::DOM::Document<std::string>(interpreter->getDocument());
	privData->dom = dom;

	JSObjectRef documentObject = JSObjectMake(dm->_ctx, Arabica::DOM::JSCDocument::getTmpl(), privData);
	JSObjectRef globalObject = JSContextGetGlobalObject(dm->_ctx);
	JSObjectSetProperty(dm->_ctx, globalObject, JSStringCreateWithUTF8CString("document"), documentObject, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, NULL);

	return dm;
}

JSCDataModel::~JSCDataModel() {
	JSGlobalContextRelease(_ctx);
}

void JSCDataModel::pushContext() {
}

void JSCDataModel::popContext() {
}

void JSCDataModel::setEvent(const Event& event) {
	LOG(ERROR) << "setEvent not implemented in JSC";
}

Data JSCDataModel::getStringAsData(const std::string& content) {
	JSValueRef result = evalAsValue(content);
	Data data = getValueAsData(result);
	return data;
}

Data JSCDataModel::getValueAsData(const JSValueRef value) {
	Data data;
	JSValueRef exception = NULL;
	switch(JSValueGetType(_ctx, value)) {
	case kJSTypeUndefined:
		LOG(ERROR) << "IsUndefined is unimplemented";
		break;
	case kJSTypeNull:
		LOG(ERROR) << "IsNull is unimplemented";
		break;
	case kJSTypeBoolean:
		data.atom = (JSValueToBoolean(_ctx, value) ? "true" : "false");
		break;
	case kJSTypeNumber:
		data.atom = toStr(JSValueToNumber(_ctx, value, &exception));
		if (exception)
			handleException(exception);
		break;
	case kJSTypeString: {
		JSStringRef stringValue = JSValueToStringCopy( _ctx, value, &exception);
		if (exception)
			handleException(exception);

		char* buf = (char*)malloc(JSStringGetMaximumUTF8CStringSize(stringValue));
		JSStringGetUTF8CString(stringValue, buf, sizeof(buf));
		data.atom = std::string(buf, sizeof(buf));
		free(buf);
		break;
	}
	case kJSTypeObject: {
		JSObjectRef objValue = JSValueToObject(_ctx, value, &exception);
		if (exception)
			handleException(exception);
		std::set<std::string> propertySet;
		JSPropertyNameArrayRef properties = JSObjectCopyPropertyNames(_ctx, objValue);
		size_t paramCount = JSPropertyNameArrayGetCount(properties);
		bool isArray = true;
		for (size_t i = 0; i < paramCount; i++) {
			JSStringRef stringValue = JSPropertyNameArrayGetNameAtIndex(properties, i);
			char* buf = (char*)malloc(JSStringGetMaximumUTF8CStringSize(stringValue));
			JSStringGetUTF8CString(stringValue, buf, sizeof(buf));
			std::string property(buf, sizeof(buf));
			if (!isNumeric(property.c_str(), 10))
				isArray = false;
			propertySet.insert(property);
			free(buf);
		}
		std::set<std::string>::iterator propIter = propertySet.begin();
		while(propIter != propertySet.end()) {
			if (isArray) {
				JSValueRef nestedValue = JSObjectGetPropertyAtIndex(_ctx, objValue, strTo<int>(*propIter), &exception);
				if (exception)
					handleException(exception);
				data.array.push_back(getValueAsData(nestedValue));
			} else {
				JSStringRef jsString = JSStringCreateWithUTF8CString(propIter->c_str());
				JSValueRef nestedValue = JSObjectGetProperty(_ctx, objValue, jsString, &exception);
				JSStringRelease(jsString);
				if (exception)
					handleException(exception);
				data.compound[*propIter] = getValueAsData(nestedValue);
			}
			propIter++;
		}

		break;
	}
	}
	return data;
}

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

uint32_t JSCDataModel::getLength(const std::string& expr) {
	LOG(ERROR) << "I am not sure whether getLength() works :(";
	JSValueRef result = evalAsValue(expr);
	JSValueRef exception = NULL;
	double length = JSValueToNumber(_ctx, result, &exception);
	if (exception)
		handleException(exception);

	return (uint32_t)length;
}

void JSCDataModel::eval(const std::string& expr) {
	evalAsValue(expr);
}

bool JSCDataModel::evalAsBool(const std::string& expr) {
	JSValueRef result = evalAsValue(expr);
	return JSValueToBoolean(_ctx, result);
}

std::string JSCDataModel::evalAsString(const std::string& expr) {
	JSValueRef result = evalAsValue(expr);
	JSValueRef exception = NULL;
	JSStringRef stringValue = JSValueToStringCopy( _ctx, result, &exception);
	if (exception)
		handleException(exception);

	char* data = (char*)malloc(JSStringGetMaximumUTF8CStringSize(stringValue));
	JSStringGetUTF8CString(stringValue, data, sizeof(data));
	std::string retString(data, sizeof(data));

	JSStringRelease(stringValue);
	free(data);
	return retString;
}

JSValueRef JSCDataModel::evalAsValue(const std::string& expr) {
	JSStringRef scriptJS = JSStringCreateWithUTF8CString(expr.c_str());
	JSValueRef exception = NULL;
	JSValueRef result = JSEvaluateScript(_ctx, scriptJS, NULL, NULL, 0, &exception);
	if (exception)
		handleException(exception);

	JSStringRelease(scriptJS);
	return result;
}

void JSCDataModel::assign(const Arabica::DOM::Element<std::string>& assignElem,
                          const Arabica::DOM::Document<std::string>& doc,
                          const std::string& content) {
}

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

void JSCDataModel::init(const Arabica::DOM::Element<std::string>& dataElem,
                        const Arabica::DOM::Document<std::string>& doc,
                        const std::string& content) {
}

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

void JSCDataModel::handleException(JSValueRef exception) {
	assert(false);
}

}