V8DataModel.h
Go to the documentation of this file.
1 
20 #ifndef V8DATAMODEL_H_KN8TWG0V
21 #define V8DATAMODEL_H_KN8TWG0V
22 
23 #include "uscxml/config.h"
25 
26 #include <list>
27 #include <set>
28 #include <v8.h>
29 
30 #ifdef BUILD_AS_PLUGINS
31 #include "uscxml/plugins/Plugins.h"
32 #endif
33 
34 namespace uscxml {
35 class Event;
36 class Data;
37 }
38 
39 namespace uscxml {
40 
46 class V8DataModel : public DataModelImpl {
47 public:
48  V8DataModel();
49  virtual ~V8DataModel();
50  virtual std::shared_ptr<DataModelImpl> create(DataModelCallbacks* callbacks);
51 
52  virtual void addExtension(DataModelExtension* ext);
53 
54  virtual std::list<std::string> getNames() {
55  std::list<std::string> names;
56  names.push_back("ecmascript");
57  return names;
58  }
59 
60  virtual bool isValidSyntax(const std::string& expr);
61 
62  virtual void setEvent(const Event& event);
63 
64  // foreach
65  virtual uint32_t getLength(const std::string& expr);
66  virtual void setForeach(const std::string& item,
67  const std::string& array,
68  const std::string& index,
69  uint32_t iteration);
70 
71  virtual bool evalAsBool(const std::string& expr);
72  virtual Data evalAsData(const std::string& expr);
73  virtual Data getAsData(const std::string& content);
74 
75  virtual bool isDeclared(const std::string& expr);
76 
77  virtual void assign(const std::string& location,
78  const Data& data,
79  const std::map<std::string, std::string>& attr = std::map<std::string, std::string>());
80  virtual void init(const std::string& location,
81  const Data& data,
82  const std::map<std::string, std::string>& attr = std::map<std::string, std::string>());
83 
84 protected:
85 
86  static void jsExtension(const v8::FunctionCallbackInfo<v8::Value>& info);
87  static void jsIn(const v8::FunctionCallbackInfo<v8::Value>& info);
88  static void jsPrint(const v8::FunctionCallbackInfo<v8::Value>& info);
89 
90  //v8::Local<v8::Object> _event; // Persistent events leak ..
91  v8::Persistent<v8::Context> _context;
92  static v8::Isolate* _isolate;
93 
94  v8::Persistent<v8::Object> _ioProcessors;
95  v8::Persistent<v8::Object> _invokers;
96 
97  static void getIOProcessors(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info);
98  static void getInvokers(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info);
99  static void getAttribute(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info);
100  static void setWithException(v8::Local<v8::String> property,
101  v8::Local<v8::Value> value,
102  const v8::PropertyCallbackInfo<void>& info);
103 
104  v8::Local<v8::Value> evalAsValue(const std::string& expr, bool dontThrow = false);
105  v8::Local<v8::Value> getDataAsValue(const Data& data);
106  Data getValueAsData(const v8::Local<v8::Value>& value);
107  v8::Local<v8::Value> getNodeAsValue(const XERCESC_NS::DOMNode* node);
108  void throwExceptionEvent(const v8::TryCatch& tryCatch);
109 
110  std::set<DataModelExtension*> _extensions;
111 
112 private:
113  Data getValueAsData(const v8::Local<v8::Value>& value, std::set<v8::Value*>& alreadySeen);
114 
115  static std::mutex _initMutex;
116 
117 };
118 
119 #ifdef BUILD_AS_PLUGINS
120 PLUMA_INHERIT_PROVIDER(V8DataModel, DataModelImpl);
121 #endif
122 
123 }
124 
125 #endif /* end of include guard: V8DATAMODEL_H_KN8TWG0V */
ECMAScript data-model via Google's V8.
Definition: V8DataModel.h:46
virtual bool isValidSyntax(const std::string &expr)
Determine whether a given string constitutes valid syntax in the data-model's language.
Definition: V8DataModel.cpp:672
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: V8DataModel.cpp:812
Definition: Breakpoint.cpp:26
virtual Data getAsData(const std::string &content)
Return a string as an unevaluated Data object.
Definition: V8DataModel.cpp:452
virtual bool isDeclared(const std::string &expr)
Determine whether a given variable / location is declared.
Definition: V8DataModel.cpp:739
Definition: DataModelImpl.h:61
Abstract base class for all data-model implementations.
Definition: DataModelImpl.h:77
virtual std::list< std::string > getNames()
Return a list of names to be matched by the datamodel attribute in SCXML.
Definition: V8DataModel.h:54
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: V8DataModel.cpp:710
virtual void addExtension(DataModelExtension *ext)
Register an extension to get data into and out of the data-model.
Definition: V8DataModel.cpp:88
virtual Data evalAsData(const std::string &expr)
Return a string as an evaluated Data object.
Definition: V8DataModel.cpp:475
Definition: Event.h:94
virtual bool evalAsBool(const std::string &expr)
Evaluate a given expression as a boolean.
Definition: V8DataModel.cpp:765
Callbacks available for every data-model.
Definition: DataModelImpl.h:47
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: V8DataModel.cpp:778
virtual void setEvent(const Event &event)
Set the given event as _event in the data-model's global scope.
Definition: V8DataModel.cpp:336
virtual std::shared_ptr< DataModelImpl > create(DataModelCallbacks *callbacks)
The Factory wants to instantiate a new instance.
Definition: V8DataModel.cpp:176
virtual uint32_t getLength(const std::string &expr)
Evaluate the given expression as something iterable and return its length.
Definition: V8DataModel.cpp:695
Definition: Data.h:44