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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
|
/**
* @file
* @author 2012-2013 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 "JSCDataModel.h"
#include "JSCDOM.h"
#include "dom/JSCDocument.h"
#include "dom/JSCElement.h"
#include "dom/JSCText.h"
#include "dom/JSCCDATASection.h"
#include "dom/JSCSCXMLEvent.h"
#include "dom/JSCArrayBuffer.h"
#include "dom/JSCInt8Array.h"
#include "dom/JSCUint8Array.h"
#include "dom/JSCUint8ClampedArray.h"
#include "dom/JSCInt16Array.h"
#include "dom/JSCUint16Array.h"
#include "dom/JSCInt32Array.h"
#include "dom/JSCUint32Array.h"
#include "dom/JSCFloat32Array.h"
#include "dom/JSCFloat64Array.h"
#include "dom/JSCDataView.h"
#include "uscxml/Message.h"
#include "uscxml/DOMUtils.h"
#include <glog/logging.h>
#ifdef BUILD_AS_PLUGINS
#include <Pluma/Connector.hpp>
#endif
#define TO_JSC_DOMVALUE(type) \
struct JSC##type::JSC##type##Private* privData = new JSC##type::JSC##type##Private(); \
privData->dom = _dom; \
privData->nativeObj = new type<std::string>(node); \
JSObjectRef retObj = JSObjectMake(_ctx, JSC##type::getTmpl(), privData);\
return retObj;
#define JSC_ADD_GLOBAL_OBJECT(name, constructor)\
JSStringRef name##Name = JSStringCreateWithUTF8CString(#name);\
JSObjectRef name = JSObjectMake(dm->_ctx, constructor, NULL);\
JSObjectSetProperty(dm->_ctx, JSContextGetGlobalObject(dm->_ctx), name##Name, name, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, NULL);\
JSStringRelease(name##Name);
namespace uscxml {
using namespace Arabica::XPath;
using namespace Arabica::DOM;
#ifdef BUILD_AS_PLUGINS
PLUMA_CONNECTOR
bool pluginConnect(pluma::Host& host) {
host.add( new JSCDataModelProvider() );
return true;
}
#endif
JSCDataModel::JSCDataModel() {
_dom = NULL;
_ctx = NULL;
}
JSCDataModel::~JSCDataModel() {
if (_dom)
delete _dom;
if (_ctx)
JSGlobalContextRelease(_ctx);
}
#if 0
typedef struct {
int version; /* current (and only) version is 0 */
JSClassAttributes attributes;
const char* className;
JSClassRef parentClass;
const JSStaticValue* staticValues;
const JSStaticFunction* staticFunctions;
JSObjectInitializeCallback initialize;
JSObjectFinalizeCallback finalize;
JSObjectHasPropertyCallback hasProperty;
JSObjectGetPropertyCallback getProperty;
JSObjectSetPropertyCallback setProperty;
JSObjectDeletePropertyCallback deleteProperty;
JSObjectGetPropertyNamesCallback getPropertyNames;
JSObjectCallAsFunctionCallback callAsFunction;
JSObjectCallAsConstructorCallback callAsConstructor;
JSObjectHasInstanceCallback hasInstance;
JSObjectConvertToTypeCallback convertToType;
} JSClassDefinition;
#endif
// functions need to be objects to hold private data in JSC
JSClassDefinition JSCDataModel::jsInClassDef = { 0, 0, "In", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, jsIn, 0, 0, 0 };
JSClassDefinition JSCDataModel::jsPrintClassDef = { 0, 0, "print", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, jsPrint, 0, 0, 0 };
JSClassDefinition JSCDataModel::jsIOProcessorsClassDef = { 0, 0, "ioProcessors", 0, 0, 0, 0, 0, jsIOProcessorHasProp, jsIOProcessorGetProp, 0, 0, jsIOProcessorListProps, 0, 0, 0, 0 };
JSClassDefinition JSCDataModel::jsInvokersClassDef = { 0, 0, "invokers", 0, 0, 0, 0, 0, jsInvokerHasProp, jsInvokerGetProp, 0, 0, jsInvokerListProps, 0, 0, 0, 0 };
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;
dm->_dom = new JSCDOM();
dm->_dom->xpath = new XPath<std::string>();
dm->_dom->xpath->setNamespaceContext(interpreter->getNSContext());
dm->_dom->storage = new Storage(URL::getResourceDir() + PATH_SEPERATOR + interpreter->getName() + ".storage");
// introduce global functions as objects for private data
JSClassRef jsInClassRef = JSClassCreate(&jsInClassDef);
JSObjectRef jsIn = JSObjectMake(dm->_ctx, jsInClassRef, dm.get());
JSStringRef inName = JSStringCreateWithUTF8CString("In");
JSObjectSetProperty(dm->_ctx, JSContextGetGlobalObject(dm->_ctx), inName, jsIn, kJSPropertyAttributeNone, NULL);
JSStringRelease(inName);
JSClassRef jsPrintClassRef = JSClassCreate(&jsPrintClassDef);
JSObjectRef jsPrint = JSObjectMake(dm->_ctx, jsPrintClassRef, dm.get());
JSStringRef printName = JSStringCreateWithUTF8CString("print");
JSObjectSetProperty(dm->_ctx, JSContextGetGlobalObject(dm->_ctx), printName, jsPrint, kJSPropertyAttributeNone, NULL);
JSStringRelease(printName);
JSClassRef jsInvokerClassRef = JSClassCreate(&jsInvokersClassDef);
JSObjectRef jsInvoker = JSObjectMake(dm->_ctx, jsInvokerClassRef, dm.get());
JSStringRef invokerName = JSStringCreateWithUTF8CString("_invokers");
JSObjectSetProperty(dm->_ctx, JSContextGetGlobalObject(dm->_ctx), invokerName, jsInvoker, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, NULL);
JSStringRelease(invokerName);
JSClassRef jsIOProcClassRef = JSClassCreate(&jsIOProcessorsClassDef);
JSObjectRef jsIOProc = JSObjectMake(dm->_ctx, jsIOProcClassRef, dm.get());
JSStringRef ioProcName = JSStringCreateWithUTF8CString("_ioprocessors");
JSObjectSetProperty(dm->_ctx, JSContextGetGlobalObject(dm->_ctx), ioProcName, jsIOProc, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, NULL);
JSStringRelease(ioProcName);
JSStringRef nameName = JSStringCreateWithUTF8CString("_name");
JSStringRef name = JSStringCreateWithUTF8CString(dm->_interpreter->getName().c_str());
JSObjectSetProperty(dm->_ctx, JSContextGetGlobalObject(dm->_ctx), nameName, JSValueMakeString(dm->_ctx, name), kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, NULL);
JSStringRelease(nameName);
JSStringRelease(name);
JSStringRef sessionIdName = JSStringCreateWithUTF8CString("_sessionid");
JSStringRef sessionId = JSStringCreateWithUTF8CString(dm->_interpreter->getSessionId().c_str());
JSObjectSetProperty(dm->_ctx, JSContextGetGlobalObject(dm->_ctx), sessionIdName, JSValueMakeString(dm->_ctx, sessionId), kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, NULL);
JSStringRelease(sessionIdName);
JSStringRelease(sessionId);
JSC_ADD_GLOBAL_OBJECT(ArrayBuffer, JSCArrayBuffer::getTmpl());
JSC_ADD_GLOBAL_OBJECT(Int8Array, JSCInt8Array::getTmpl());
JSC_ADD_GLOBAL_OBJECT(Uint8Array, JSCUint8Array::getTmpl());
JSC_ADD_GLOBAL_OBJECT(Uint8ClampedArray, JSCUint8ClampedArray::getTmpl());
JSC_ADD_GLOBAL_OBJECT(Int16Array, JSCInt16Array::getTmpl());
JSC_ADD_GLOBAL_OBJECT(Uint16Array, JSCUint16Array::getTmpl());
JSC_ADD_GLOBAL_OBJECT(Int32Array, JSCInt32Array::getTmpl());
JSC_ADD_GLOBAL_OBJECT(Uint32Array, JSCUint32Array::getTmpl());
JSC_ADD_GLOBAL_OBJECT(Float32Array, JSCFloat32Array::getTmpl());
JSC_ADD_GLOBAL_OBJECT(Float64Array, JSCFloat64Array::getTmpl());
JSC_ADD_GLOBAL_OBJECT(DataView, JSCDataView::getTmpl());
JSCDocument::JSCDocumentPrivate* privData = new JSCDocument::JSCDocumentPrivate();
if (interpreter) {
privData->nativeObj = new Document<std::string>(interpreter->getDocument());
privData->dom = dm->_dom;
JSObjectRef documentObject = JSObjectMake(dm->_ctx, JSCDocument::getTmpl(), privData);
JSObjectRef globalObject = JSContextGetGlobalObject(dm->_ctx);
JSStringRef documentName = JSStringCreateWithUTF8CString("document");
JSObjectSetProperty(dm->_ctx, globalObject, documentName, documentObject, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, NULL);
JSStringRelease(documentName);
}
dm->eval(Element<std::string>(), "_x = {};");
return dm;
}
void JSCDataModel::pushContext() {
}
void JSCDataModel::popContext() {
}
void JSCDataModel::setEvent(const Event& event) {
JSCSCXMLEvent::JSCSCXMLEventPrivate* privData = new JSCSCXMLEvent::JSCSCXMLEventPrivate();
privData->nativeObj = new Event(event);
privData->dom = _dom;
JSObjectRef eventObj = JSObjectMake(_ctx, JSCSCXMLEvent::getTmpl(), privData);
JSObjectRef globalObject = JSContextGetGlobalObject(_ctx);
JSValueRef exception = NULL;
if (event.dom) {
JSStringRef propName = JSStringCreateWithUTF8CString("data");
JSObjectSetProperty(_ctx, eventObj, propName, getNodeAsValue(event.dom), 0, &exception);
JSStringRelease(propName);
if (exception)
handleException(exception);
} else if (event.content.length() > 0) {
// _event.data is a string or JSON
Data json = Data::fromJSON(event.content);
if (json) {
JSStringRef propName = JSStringCreateWithUTF8CString("data");
JSObjectSetProperty(_ctx, eventObj, propName, getDataAsValue(json), 0, &exception);
JSStringRelease(propName);
if (exception)
handleException(exception);
} else {
JSStringRef propName = JSStringCreateWithUTF8CString("data");
JSStringRef contentStr = JSStringCreateWithUTF8CString(Interpreter::spaceNormalize(event.content).c_str());
JSObjectSetProperty(_ctx, eventObj, propName, JSValueMakeString(_ctx, contentStr), 0, &exception);
JSStringRelease(propName);
JSStringRelease(contentStr);
if (exception)
handleException(exception);
}
} 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 > 0) {
JSStringRef propName = JSStringCreateWithUTF8CString("data");
JSObjectSetProperty(_ctx, eventObj, propName, getDataAsValue(eventCopy.data), 0, &exception);
JSStringRelease(propName);
if (exception)
handleException(exception);
} else {
// test 343 / test 488
JSStringRef propName = JSStringCreateWithUTF8CString("data");
JSObjectSetProperty(_ctx, eventObj, propName, JSValueMakeUndefined(_ctx), 0, &exception);
JSStringRelease(propName);
if (exception)
handleException(exception);
}
}
JSStringRef eventName = JSStringCreateWithUTF8CString("_event");
JSObjectSetProperty(_ctx, globalObject, eventName, eventObj, kJSPropertyAttributeDontDelete, &exception);
JSStringRelease(eventName);
if (exception)
handleException(exception);
}
Data JSCDataModel::getStringAsData(const std::string& content) {
JSValueRef result = evalAsValue(content);
Data data = getValueAsData(result);
return data;
}
JSValueRef JSCDataModel::getDataAsValue(const Data& data) {
JSValueRef exception = NULL;
if (data.compound.size() > 0) {
JSObjectRef value = JSObjectMake(_ctx, 0, 0);
std::map<std::string, Data>::const_iterator compoundIter = data.compound.begin();
while(compoundIter != data.compound.end()) {
JSStringRef key = JSStringCreateWithUTF8CString(compoundIter->first.c_str());
JSObjectSetProperty(_ctx, value, key, getDataAsValue(compoundIter->second), 0, &exception);
JSStringRelease(key);
if (exception)
handleException(exception);
compoundIter++;
}
return value;
}
if (data.array.size() > 0) {
JSValueRef elements[data.array.size()];
std::list<Data>::const_iterator arrayIter = data.array.begin();
uint32_t index = 0;
while(arrayIter != data.array.end()) {
elements[index++] = getDataAsValue(*arrayIter);
arrayIter++;
}
JSObjectRef value = JSObjectMakeArray(_ctx, data.array.size(), elements, &exception);
if (exception)
handleException(exception);
return value;
}
if (data.atom.size() > 0) {
switch (data.type) {
case Data::VERBATIM: {
JSStringRef stringRef = JSStringCreateWithUTF8CString(data.atom.c_str());
JSValueRef value = JSValueMakeString(_ctx, stringRef);
JSStringRelease(stringRef);
return value;
break;
}
case Data::INTERPRETED: {
return evalAsValue(data.atom);
break;
}
}
}
if (data.binary) {
uscxml::ArrayBuffer* localInstance = new uscxml::ArrayBuffer(data.binary);
JSClassRef retClass = JSCArrayBuffer::getTmpl();
struct JSCArrayBuffer::JSCArrayBufferPrivate* retPrivData = new JSCArrayBuffer::JSCArrayBufferPrivate();
retPrivData->nativeObj = localInstance;
JSObjectRef retObj = JSObjectMake(_ctx, retClass, retPrivData);
return retObj;
}
return JSValueMakeUndefined(_ctx);
}
Data JSCDataModel::getValueAsData(const JSValueRef value) {
Data data;
JSValueRef exception = NULL;
switch(JSValueGetType(_ctx, value)) {
case kJSTypeUndefined:
case kJSTypeNull:
data.atom = "null";
data.type = Data::INTERPRETED;
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);
size_t maxSize = JSStringGetMaximumUTF8CStringSize(stringValue);
char* buf = new char[maxSize];
JSStringGetUTF8CString(stringValue, buf, maxSize);
data.atom = std::string(buf);
data.type = Data::VERBATIM;
JSStringRelease(stringValue);
free(buf);
break;
}
case kJSTypeObject: {
JSObjectRef objValue = JSValueToObject(_ctx, value, &exception);
if (exception)
handleException(exception);
if (JSValueIsObjectOfClass(_ctx, value, JSCArrayBuffer::getTmpl())) {
// binary data
JSCArrayBuffer::JSCArrayBufferPrivate* privObj = (JSCArrayBuffer::JSCArrayBufferPrivate*)JSObjectGetPrivate(objValue);
data.binary = privObj->nativeObj->_buffer;
return data;
} else if (JSValueIsObjectOfClass(_ctx, value, JSCNode::getTmpl())) {
// dom node
JSCNode::JSCNodePrivate* privObj = (JSCNode::JSCNodePrivate*)JSObjectGetPrivate(objValue);
data.node = *privObj->nativeObj;
return data;
}
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);
size_t maxSize = JSStringGetMaximumUTF8CStringSize(stringValue);
char* buf = new char[maxSize];
JSStringGetUTF8CString(stringValue, buf, maxSize);
std::string property(buf);
if (!isNumeric(property.c_str(), 10))
isArray = false;
propertySet.insert(property);
JSStringRelease(stringValue);
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) {
JSValueRef result;
result = evalAsValue("(" + expr + ").length");
JSType type = JSValueGetType(_ctx, result);
if (type == kJSTypeNull || type == kJSTypeUndefined) {
Event exceptionEvent;
exceptionEvent.data.compound["exception"] = Data("'" + expr + "' does not evaluate to an array.", Data::VERBATIM);
exceptionEvent.name = "error.execution";
exceptionEvent.eventType = Event::PLATFORM;
throw(exceptionEvent);
}
JSValueRef exception = NULL;
double length = JSValueToNumber(_ctx, result, &exception);
if (exception)
handleException(exception);
return (uint32_t)length;
}
void JSCDataModel::setForeach(const std::string& item,
const std::string& array,
const std::string& index,
uint32_t iteration) {
if (!isDeclared(item)) {
assign(item, Data());
}
// assign array element to item
std::stringstream ss;
ss << array << "[" << iteration << "]";
assign(item, ss.str());
if (index.length() > 0) {
// assign iteration element to index
std::stringstream ss;
ss << iteration;
assign(index, ss.str());
}
}
bool JSCDataModel::isDeclared(const std::string& expr) {
JSStringRef scriptJS = JSStringCreateWithUTF8CString(expr.c_str());
JSValueRef exception = NULL;
JSValueRef result = JSEvaluateScript(_ctx, scriptJS, NULL, NULL, 0, &exception);
JSStringRelease(scriptJS);
if (exception || JSValueIsNull(_ctx, result)) {
return false;
}
return true;
}
void JSCDataModel::eval(const Element<std::string>& scriptElem,
const std::string& expr) {
evalAsValue(expr);
}
bool JSCDataModel::evalAsBool(const Arabica::DOM::Node<std::string>& node, 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);
size_t maxSize = JSStringGetMaximumUTF8CStringSize(stringValue);
char data[maxSize];
JSStringGetUTF8CString(stringValue, data, maxSize);
std::string retString(data);
JSStringRelease(stringValue);
return retString;
}
JSValueRef JSCDataModel::evalAsValue(const std::string& expr, bool dontThrow) {
JSStringRef scriptJS = JSStringCreateWithUTF8CString(expr.c_str());
JSValueRef exception = NULL;
JSValueRef result = JSEvaluateScript(_ctx, scriptJS, NULL, NULL, 0, &exception);
JSStringRelease(scriptJS);
if (exception && !dontThrow)
handleException(exception);
return result;
}
void JSCDataModel::assign(const Element<std::string>& assignElem,
const Node<std::string>& node,
const std::string& content) {
std::string key;
JSValueRef exception = NULL;
if (HAS_ATTR(assignElem, "id")) {
key = ATTR(assignElem, "id");
} else if (HAS_ATTR(assignElem, "location")) {
key = ATTR(assignElem, "location");
}
if (key.length() == 0)
throw Event("error.execution", Event::PLATFORM);
// flags on attribute are ignored?
if (key.compare("_sessionid") == 0)
throw Event("error.execution", Event::PLATFORM);
if (key.compare("_name") == 0)
throw Event("error.execution", Event::PLATFORM);
if (key.compare("_ioprocessors") == 0)
throw Event("error.execution", Event::PLATFORM);
if (key.compare("_invokers") == 0)
throw Event("error.execution", Event::PLATFORM);
if (HAS_ATTR(assignElem, "expr")) {
evalAsValue(key + " = " + ATTR(assignElem, "expr"));
} else if (node) {
JSObjectSetProperty(_ctx, JSContextGetGlobalObject(_ctx), JSStringCreateWithUTF8CString(key.c_str()), getNodeAsValue(node), 0, &exception);
if (exception)
handleException(exception);
} else if (content.size() > 0) {
try {
evalAsValue(key + " = " + content);
} catch (...) {
evalAsValue(key + " = " + "\"" + Interpreter::spaceNormalize(content) + "\"");
}
} else {
JSObjectSetProperty(_ctx, JSContextGetGlobalObject(_ctx), JSStringCreateWithUTF8CString(key.c_str()), JSValueMakeUndefined(_ctx), 0, &exception);
if (exception)
handleException(exception);
}
}
JSValueRef JSCDataModel::getNodeAsValue(const Node<std::string>& node) {
switch (node.getNodeType()) {
case Node_base::ELEMENT_NODE: {
TO_JSC_DOMVALUE(Element);
}
case Node_base::TEXT_NODE: {
TO_JSC_DOMVALUE(Text);
}
case Node_base::CDATA_SECTION_NODE: {
TO_JSC_DOMVALUE(CDATASection);
}
case Node_base::DOCUMENT_NODE: {
TO_JSC_DOMVALUE(Document);
}
default: {
TO_JSC_DOMVALUE(Node);
}
}
}
void JSCDataModel::assign(const std::string& location, const Data& data) {
std::stringstream ssJSON;
ssJSON << data;
evalAsValue(location + " = " + ssJSON.str());
}
void JSCDataModel::init(const Element<std::string>& dataElem,
const Node<std::string>& node,
const std::string& content) {
try {
assign(dataElem, node, content);
} catch (Event e) {
// test 277
std::string key;
if (HAS_ATTR(dataElem, "id")) {
key = ATTR(dataElem, "id");
} else if (HAS_ATTR(dataElem, "location")) {
key = ATTR(dataElem, "location");
}
evalAsValue(key + " = undefined", true);
throw e;
}
}
void JSCDataModel::init(const std::string& location, const Data& data) {
try {
assign(location, data);
} catch (Event e) {
// test 277
evalAsValue(location + " = undefined", true);
throw e;
}
}
void JSCDataModel::handleException(JSValueRef exception) {
JSStringRef exceptionStringRef = JSValueToStringCopy(_ctx, exception, NULL);
size_t maxSize = JSStringGetMaximumUTF8CStringSize(exceptionStringRef);
char buffer[maxSize];
JSStringGetUTF8CString(exceptionStringRef, buffer, maxSize);
JSStringRelease(exceptionStringRef);
std::string exceptionMsg(buffer);
Event exceptionEvent;
exceptionEvent.data.compound["exception"] = Data(exceptionMsg, Data::VERBATIM);
exceptionEvent.name = "error.execution";
exceptionEvent.eventType = Event::PLATFORM;
throw(exceptionEvent);
}
JSValueRef JSCDataModel::jsPrint(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
JSCDataModel* INSTANCE = (JSCDataModel*)JSObjectGetPrivate(function);
for (unsigned int i = 0; i < argumentCount; i++) {
if (JSValueIsString(ctx, arguments[i])) {
JSStringRef stringRef = JSValueToStringCopy(ctx, arguments[i], exception);
if (*exception)
INSTANCE->handleException(*exception);
size_t maxSize = JSStringGetMaximumUTF8CStringSize(stringRef);
char* buffer = new char[maxSize];
JSStringGetUTF8CString(stringRef, buffer, maxSize);
JSStringRelease(stringRef);
std::string msg(buffer);
free(buffer);
std::cout << msg;
}
}
return JSValueMakeUndefined(ctx);
}
JSValueRef JSCDataModel::jsIn(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
JSCDataModel* INSTANCE = (JSCDataModel*)JSObjectGetPrivate(function);
for (unsigned int i = 0; i < argumentCount; i++) {
if (JSValueIsString(ctx, arguments[i])) {
JSStringRef stringRef = JSValueToStringCopy(ctx, arguments[i], exception);
if (*exception)
INSTANCE->handleException(*exception);
size_t maxSize = JSStringGetMaximumUTF8CStringSize(stringRef);
char* buffer = new char[maxSize];
JSStringGetUTF8CString(stringRef, buffer, maxSize);
JSStringRelease(stringRef);
std::string stateName(buffer);
free(buffer);
if (Interpreter::isMember(INSTANCE->_interpreter->getState(stateName), INSTANCE->_interpreter->getConfiguration())) {
continue;
}
}
return JSValueMakeBoolean(ctx, false);
}
return JSValueMakeBoolean(ctx, true);
}
bool JSCDataModel::jsIOProcessorHasProp(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName) {
JSCDataModel* INSTANCE = (JSCDataModel*)JSObjectGetPrivate(object);
std::map<std::string, IOProcessor> ioProcessors = INSTANCE->_interpreter->getIOProcessors();
size_t maxSize = JSStringGetMaximumUTF8CStringSize(propertyName);
char buffer[maxSize];
JSStringGetUTF8CString(propertyName, buffer, maxSize);
std::string prop(buffer);
return ioProcessors.find(prop) != ioProcessors.end();
}
JSValueRef JSCDataModel::jsIOProcessorGetProp(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) {
JSCDataModel* INSTANCE = (JSCDataModel*)JSObjectGetPrivate(object);
std::map<std::string, IOProcessor> ioProcessors = INSTANCE->_interpreter->getIOProcessors();
size_t maxSize = JSStringGetMaximumUTF8CStringSize(propertyName);
char buffer[maxSize];
JSStringGetUTF8CString(propertyName, buffer, maxSize);
std::string prop(buffer);
if (ioProcessors.find(prop) != ioProcessors.end()) {
return INSTANCE->getDataAsValue(ioProcessors.find(prop)->second.getDataModelVariables());
}
return JSValueMakeUndefined(ctx);
}
void JSCDataModel::jsIOProcessorListProps(JSContextRef ctx, JSObjectRef object, JSPropertyNameAccumulatorRef propertyNames) {
JSCDataModel* INSTANCE = (JSCDataModel*)JSObjectGetPrivate(object);
std::map<std::string, IOProcessor> ioProcessors = INSTANCE->_interpreter->getIOProcessors();
std::map<std::string, IOProcessor>::const_iterator ioProcIter = ioProcessors.begin();
while(ioProcIter != ioProcessors.end()) {
JSStringRef ioProcName = JSStringCreateWithUTF8CString(ioProcIter->first.c_str());
JSPropertyNameAccumulatorAddName(propertyNames, ioProcName);
ioProcIter++;
}
}
bool JSCDataModel::jsInvokerHasProp(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName) {
JSCDataModel* INSTANCE = (JSCDataModel*)JSObjectGetPrivate(object);
std::map<std::string, Invoker> invokers = INSTANCE->_interpreter->getInvokers();
size_t maxSize = JSStringGetMaximumUTF8CStringSize(propertyName);
char buffer[maxSize];
JSStringGetUTF8CString(propertyName, buffer, maxSize);
std::string prop(buffer);
return invokers.find(prop) != invokers.end();
}
JSValueRef JSCDataModel::jsInvokerGetProp(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) {
JSCDataModel* INSTANCE = (JSCDataModel*)JSObjectGetPrivate(object);
std::map<std::string, Invoker> invokers = INSTANCE->_interpreter->getInvokers();
size_t maxSize = JSStringGetMaximumUTF8CStringSize(propertyName);
char buffer[maxSize];
JSStringGetUTF8CString(propertyName, buffer, maxSize);
std::string prop(buffer);
if (invokers.find(prop) != invokers.end()) {
return INSTANCE->getDataAsValue(invokers.find(prop)->second.getDataModelVariables());
}
return JSValueMakeUndefined(ctx);
}
void JSCDataModel::jsInvokerListProps(JSContextRef ctx, JSObjectRef object, JSPropertyNameAccumulatorRef propertyNames) {
JSCDataModel* INSTANCE = (JSCDataModel*)JSObjectGetPrivate(object);
std::map<std::string, Invoker> invokers = INSTANCE->_interpreter->getInvokers();
std::map<std::string, Invoker>::const_iterator invokerIter = invokers.begin();
while(invokerIter != invokers.end()) {
JSStringRef invokeName = JSStringCreateWithUTF8CString(invokerIter->first.c_str());
JSPropertyNameAccumulatorAddName(propertyNames, invokeName);
JSStringRelease(invokeName);
invokerIter++;
}
}
}
|