JSCDataModel.h
Go to the documentation of this file.
1 
20 #ifndef JSCDATAMODEL_H_KN8TWG0V
21 #define JSCDATAMODEL_H_KN8TWG0V
22 
23 #include "uscxml/config.h"
25 #include <list>
26 #include <set>
27 #include <mutex>
28 
29 #if defined(HAS_JSC_JAVASCRIPTCORE_H)
30 #include <JavaScriptCore/JavaScriptCore.h>
31 #elif defined(HAS_JSC_JAVASCRIPT_H)
32 #include <JavaScriptCore/JavaScript.h>
33 #else
34 #error "Did not find header for JSC?"
35 #endif
36 
37 #ifdef BUILD_AS_PLUGINS
38 #include "uscxml/plugins/Plugins.h"
39 #endif
40 
41 namespace uscxml {
42 class Event;
43 class Data;
44 }
45 
46 namespace uscxml {
47 
53 class JSCDataModel : public DataModelImpl {
54 public:
55  JSCDataModel();
56  virtual ~JSCDataModel();
57  virtual std::shared_ptr<DataModelImpl> create(DataModelCallbacks* callbacks);
58 
59  virtual void addExtension(DataModelExtension* ext);
60 
61  virtual std::list<std::string> getNames() {
62  std::list<std::string> names;
63  names.push_back("ecmascript");
64  return names;
65  }
66 
67  virtual bool isValidSyntax(const std::string& expr);
68 
69  virtual void setEvent(const Event& event);
70 
71  // foreach
72  virtual uint32_t getLength(const std::string& expr);
73  virtual void setForeach(const std::string& item,
74  const std::string& array,
75  const std::string& index,
76  uint32_t iteration);
77 
78  virtual Data getAsData(const std::string& content);
79  virtual Data evalAsData(const std::string& expr);
80  virtual bool evalAsBool(const std::string& expr);
81 
82  virtual bool isDeclared(const std::string& expr);
83 
84  virtual void assign(const std::string& location,
85  const Data& data,
86  const std::map<std::string, std::string>& attr = std::map<std::string, std::string>());
87  virtual void init(const std::string& location,
88  const Data& data,
89  const std::map<std::string, std::string>& attr = std::map<std::string, std::string>());
90 
91 protected:
92 
93  static JSClassDefinition jsInClassDef;
94  static JSValueRef jsIn(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
95  static JSClassDefinition jsPrintClassDef;
96  static JSValueRef jsPrint(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
97  static JSClassDefinition jsExtensionClassDef;
98  static JSValueRef jsExtension(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
99 
100  static JSClassDefinition jsIOProcessorsClassDef;
101  static bool jsIOProcessorHasProp(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName);
102  static JSValueRef jsIOProcessorGetProp(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception);
103  static void jsIOProcessorListProps(JSContextRef ctx, JSObjectRef object, JSPropertyNameAccumulatorRef propertyNames);
104 
105  static JSClassDefinition jsInvokersClassDef;
106  static bool jsInvokerHasProp(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName);
107  static JSValueRef jsInvokerGetProp(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception);
108  static void jsInvokerListProps(JSContextRef ctx, JSObjectRef object, JSPropertyNameAccumulatorRef propertyNames);
109 
110  JSValueRef getNodeAsValue(const XERCESC_NS::DOMNode* node);
111  JSValueRef getDataAsValue(const Data& data);
112  Data getValueAsData(const JSValueRef value);
113  JSValueRef evalAsValue(const std::string& expr, bool dontThrow = false);
114 
115  void handleException(JSValueRef exception);
116 
117  std::string _sessionId;
118  std::string _name;
119 
120  std::set<DataModelExtension*> _extensions;
121 
122  Event _event;
123  JSGlobalContextRef _ctx;
124 
125  static std::mutex _initMutex;
126 
127 };
128 
129 #ifdef BUILD_AS_PLUGINS
130 PLUMA_INHERIT_PROVIDER(JSCDataModel, DataModelImpl)
131 #endif
132 
133 }
134 
135 #endif /* end of include guard: JSCDATAMODEL_H_KN8TWG0V */
virtual bool isValidSyntax(const std::string &expr)
Determine whether a given string constitutes valid syntax in the data-model's language.
Definition: JSCDataModel.cpp:657
virtual bool isDeclared(const std::string &expr)
Determine whether a given variable / location is declared.
Definition: JSCDataModel.cpp:669
virtual std::list< std::string > getNames()
Return a list of names to be matched by the datamodel attribute in SCXML.
Definition: JSCDataModel.h:61
Definition: Breakpoint.cpp:26
ECMAScript data-model via JavaScriptCore.
Definition: JSCDataModel.h:53
virtual void setEvent(const Event &event)
Set the given event as _event in the data-model's global scope.
Definition: JSCDataModel.cpp:309
Definition: DataModelImpl.h:61
Abstract base class for all data-model implementations.
Definition: DataModelImpl.h:77
virtual void init(const std::string &location, const Data &data, const std::map< std::string, std::string > &attr=std::map< std::string, std::string >())
Initialize a variable / location in the data-model with a given data object.
Definition: JSCDataModel.cpp:757
virtual void setForeach(const std::string &item, const std::string &array, const std::string &index, uint32_t iteration)
Set a given item to the object at a given index for one iteration.
Definition: JSCDataModel.cpp:631
virtual void addExtension(DataModelExtension *ext)
Register an extension to get data into and out of the data-model.
Definition: JSCDataModel.cpp:112
virtual Data getAsData(const std::string &content)
Return a string as an unevaluated Data object.
Definition: JSCDataModel.cpp:430
virtual Data evalAsData(const std::string &expr)
Return a string as an evaluated Data object.
Definition: JSCDataModel.cpp:419
Definition: Event.h:94
virtual uint32_t getLength(const std::string &expr)
Evaluate the given expression as something iterable and return its length.
Definition: JSCDataModel.cpp:614
Callbacks available for every data-model.
Definition: DataModelImpl.h:47
virtual std::shared_ptr< DataModelImpl > create(DataModelCallbacks *callbacks)
The Factory wants to instantiate a new instance.
Definition: JSCDataModel.cpp:247
virtual void assign(const std::string &location, const Data &data, const std::map< std::string, std::string > &attr=std::map< std::string, std::string >())
Assign a data object to a location in the data-model.
Definition: JSCDataModel.cpp:723
Definition: Data.h:44
virtual bool evalAsBool(const std::string &expr)
Evaluate a given expression as a boolean.
Definition: JSCDataModel.cpp:681